Ned Batchelder avatar Ned Batchelder committed 05fe9b3

Keywords have to match entire words.

Comments (0)

Files changed (2)

         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]+"),
         ("0xa123ghi", ["hnum 0xa123", "id ghi"]),
         # keywords
         ("function Function FUNCTION", ["keyword function", "id Function", "id FUNCTION"]),
+        ("const constructor in inherits", ["keyword const", "id constructor", "keyword in", "id inherits"]),
+        ("true true_enough", ["reserved true", "id true_enough"]),
         # strings
         (''' 'hello' "hello" ''', ["string 'hello'", 'string "hello"']),
         (r""" 'don\'t' "don\"t" '"' "'" '\'' "\"" """, 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.