114 lines
3.7 KiB
Python
114 lines
3.7 KiB
Python
|
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'(?<!\.)(?:zip|xrange|vars|unicode|unichr|type|tuple|super|sum|str|staticmethod|sorted|slice|setattr|set|round|repr|reduce|raw_input|range|property|pow|ord|open|oct|object|max|min|map|long|locals|list|len|iter|issubclass|isinstance|int|input|id|hex|hash|hasattr|globals|getattr|frozenset|float|filter|file|execfile|eval|enumerate|divmod|dir|dict|delattr|complex|compile|coerce|cmp|classmethod|chr|callable|bool)(?![a-zA-Z0-9_])',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'bound_method',
|
||
|
pattern=r'(?<=\. )[a-zA-Z_][a-zA-Z0-9_]*(?= *\()',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'system_identifier',
|
||
|
pattern=r'__[a-zA-Z0-9_]+__',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'private_identifier',
|
||
|
pattern=r'__[a-zA-Z0-9_]*',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'hidden_identifier',
|
||
|
pattern=r'_[a-zA-Z0-9_]*',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'identifier',
|
||
|
pattern=r'[a-zA-Z_][a-zA-Z0-9_]*',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'delimiter',
|
||
|
pattern=r'\(|\)|\[|\]|{|}|@|,|:|\.|`|=|;|\+=|-=|\*=|/=|//=|%=|&=|\|=|\^=|>>=|<<=|\*\*=',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r"operator",
|
||
|
pattern=r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%",
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r"integer",
|
||
|
pattern=r"(?:0|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?",
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r"float",
|
||
|
pattern=r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+",
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r"imaginary",
|
||
|
pattern=r"[0-9]+|(?:[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+)[jJ]",
|
||
|
),
|
||
|
|
||
|
RegionRule(
|
||
|
name=r'docstring',
|
||
|
start=r'^ *(?P<tag>"""|\'\'\')',
|
||
|
grammar=Grammar(),
|
||
|
end=r'%(tag)s',
|
||
|
),
|
||
|
RegionRule(
|
||
|
name=r'tq_string',
|
||
|
start=r'(?P<tag>"""|\'\'\')',
|
||
|
grammar=Grammar(),
|
||
|
end=r'%(tag)s',
|
||
|
),
|
||
|
RegionRule(
|
||
|
name=r'string',
|
||
|
start=r'(?P<tag>"|\')',
|
||
|
grammar=StringGrammar(),
|
||
|
end=r'%(tag)s',
|
||
|
),
|
||
|
|
||
|
PatternRule(
|
||
|
name=r'comment',
|
||
|
pattern=r'#.*$',
|
||
|
),
|
||
|
PatternRule(
|
||
|
name=r'continuation',
|
||
|
pattern=r'\\$',
|
||
|
),
|
||
|
]
|