Source

jslex / jslex.py

Diff from to
         self.regex = regex
         self.next = next
 
-def literals(choices):
-    """Create a regex from a space-separated list of literal `choices`."""
-    return "|".join(re.escape(c) for c in choices.split())
+def literals(choices, prefix="", suffix=""):
+    """Create a regex from a space-separated list of literal `choices`.
+    
+    If provided, `prefix` and `suffix` will be attached to each choice
+    individually.
+
+    """
+    return "|".join(prefix+re.escape(c)+suffix for c in choices.split())
 
 class Lexer(object):
     """A generic multi-state regex-based lexer."""
                                 finally for function if import in instanceof new
                                 return super switch this throw try typeof var
                                 void while with
-                                """), next='reg'),
-        Tok("reserved",     literals("null true false"), next='div'),
+                                """, suffix=r"\b"), next='reg'),
+        Tok("reserved",     literals("null true false", suffix=r"\b"), next='div'),
         Tok("id",           r"([a-zA-Z_$]|\\u[0-9a-fA-Z]{4})([a-zA-Z_$0-9]|\\u[0-9a-fA-F]{4})*", next='div'),
         Tok("hnum",         r"0[xX][0-9a-fA-F]+", next='div'),
         Tok("onum",         r"0[0-7]+"),