class CallbackLexer(purplex.Lexer): FOO = purplex.TokenDef(r'foo') BAR = purplex.TokenDef(r'bar') BAZ = purplex.TokenDef(r'baz') def on_BAR(self, token): pass def on_BAZ(self, token): pass
class MyLexer(purplex.Lexer): LABEL = purplex.TokenDef(r'[A-Z]+') AND = purplex.TokenDef(r'\&') BICOND = purplex.TokenDef(r'<->') COND = purplex.TokenDef(r'->') NOT = purplex.TokenDef(r'~') OR = purplex.TokenDef(r'v') LPAREN = purplex.TokenDef(r'\(') RPAREN = purplex.TokenDef(r'\)') WHITESPACE = purplex.TokenDef(r'[\s\n]+', ignore=True)
class JavaScriptLexer(purplex.Lexer): """Lexer for a subset of JavaScript.""" # TODO negatives? floats? INTEGER = purplex.TokenDef(r'\d+') FLOAT = purplex.TokenDef(r'[-+]?\d*[.]\d+') NULL = purplex.TokenDef(r'null') TRUE = purplex.TokenDef(r'true') FALSE = purplex.TokenDef(r'false') LIST_START = purplex.TokenDef(r'\[') LIST_END = purplex.TokenDef(r'\]') OBJECT_START = purplex.TokenDef(r'\{') OBJECT_END = purplex.TokenDef(r'\}') COMMA = purplex.TokenDef(r',') COLON = purplex.TokenDef(r':') STRING = purplex.TokenDef(_STRING_RE) # TODO more unquoted keys are allowed KEY = purplex.TokenDef(r'[a-zA-Z0-9_$]+') WHITESPACE = purplex.TokenDef(r'[\s\n]+', ignore=True)
class JavaScriptLexer(purplex.Lexer): """Lexer for a subset of JavaScript.""" # TODO: Negative integers INTEGER = purplex.TokenDef(r'\d+') FLOAT = purplex.TokenDef(r'[-+]?\d*[.]\d+') NULL = purplex.TokenDef(r'null') TRUE = purplex.TokenDef(r'true') FALSE = purplex.TokenDef(r'false') LIST_START = purplex.TokenDef(r'\[') LIST_END = purplex.TokenDef(r'\]') OBJECT_START = purplex.TokenDef(r'\{') OBJECT_END = purplex.TokenDef(r'\}') COMMA = purplex.TokenDef(r',') COLON = purplex.TokenDef(r':') STRING = purplex.TokenDef( '(\'(([^\\\\\'])|(\\\\.))*?\')|("(([^\\\\"])|(\\\\.))*?")') # TODO more unquoted keys are allowed KEY = purplex.TokenDef(r'[a-zA-Z0-9_$]+') WHITESPACE = purplex.TokenDef(r'[\s\n]+', ignore=True)
class TieLexer_foosecond(purplex.Lexer): THREE_LETTERS = purplex.TokenDef(r'...') FOO = purplex.TokenDef(r'foo')
class TieLexer_foofirst(purplex.Lexer): FOO = purplex.TokenDef(r'foo') THREE_LETTERS = purplex.TokenDef(r'...')
class SubclassedLongestLexer(LongestLexer): QUAD_FOO = purplex.TokenDef(r'foofoofoofoo')
class LongestLexer(purplex.Lexer): DOUBLE_FOO = purplex.TokenDef(r'foofoo') FOO = purplex.TokenDef(r'foo') TRIPLE_FOO = purplex.TokenDef(r'foofoofoo')
class IgnoreLexer(purplex.Lexer): IDENT = purplex.TokenDef(r'[a-zA-Z]+') WHITESPACE = purplex.TokenDef('[\s\n]+', ignore=True)
class FooBarLexer(purplex.Lexer): FOO = purplex.TokenDef(r'foo') BAR = purplex.TokenDef(r'bar')