from lex2 import Grammar, ConstantRule, PatternRule, ContextPatternRule, RegionRule, DualRegionRule class StringGrammar(Grammar): rules = [ PatternRule( name=r'octal', pattern=r'\\[0-7]{3}', ), PatternRule( name=r'escaped', pattern=r'\\.', ), #PatternRule( # name=r'format', # pattern=r'%(?:\([a-zA-Z_]+\))?[-# +]*(?:[0-9]+|\*)?\.?(?:[0-9]+|\*)?[hlL]?[a-zA-Z%]', #), ] class PythonGrammar(Grammar): rules = [ PatternRule( name=r'import', pattern=r'(?:^|(?<= ))import(?= |$)', ), PatternRule( name=r'methodname', pattern=r'(?<=def )[a-zA-Z_][a-zA-Z0-9_]*', ), PatternRule( name=r'classname', pattern=r'(?<=class )[a-zA-Z_][a-zA-Z0-9_]*', ), PatternRule( name=r'reserved', pattern=r'(?:True|None|False|Exception|self)(?![a-zA-Z0-9_])', ), PatternRule( name=r'keyword', pattern=r'(?:yield|while|try|return|raise|print|pass|or|not|lambda|is|in|import|if|global|from|for|finally|exec|except|else|elif|del|def|continue|class|break|assert|as|and)(?![a-zA-Z0-9_])', ), PatternRule( name=r"builtin_method", pattern=r'(?>=|<<=|\*\*=', ), PatternRule( name=r"operator", pattern=r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%", ), PatternRule( name=r"integer", pattern=r"(?"""|\'\'\')', grammar=Grammar(), end=r'%(tag)s', ), RegionRule( name=r'tq_string', start=r'(?P"""|\'\'\')', grammar=Grammar(), end=r'%(tag)s', ), RegionRule( name=r'string', start=r'(?P"|\')', grammar=StringGrammar(), end=r'%(tag)s', ), PatternRule( name=r'comment', pattern=r'#.*$', ), PatternRule( name=r'continuation', pattern=r'\\$', ), ]