pmacs3/lex2_perl.py

302 lines
10 KiB
Python
Executable File

from lex2 import Grammar, ConstantRule, PatternRule, RegionRule, DualRegionRule
class PodGrammar(Grammar):
rules = [
PatternRule(
name=r'header',
pattern=r'(?<=^=head[1-4]) +.*$',
),
PatternRule(
name=r'indent_level',
pattern=r'(?<=^=over) +.*$',
),
PatternRule(
name=r'item_entry',
pattern=r'(?<=^=item) +.*$',
),
PatternRule(
name=r'format',
pattern=r'(?:(?<=^=begin)|(?<=^=end)) +.*$',
),
PatternRule(
name=r'encoding_type',
pattern=r'(?<=^=encoding) +.*$',
),
]
class StringGrammar(Grammar):
rules = [
PatternRule(
name=r'escaped',
pattern=r'\\.',
),
PatternRule(
name=r'hash_deref',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?:->{(?:[a-zA-Z_][a-zA-Z_0-9]*|'(?:\\.|[^'\\])*'|\"(\\.|[^\\\"])*\")})+",
),
#PatternRule(
# name=r'hash_bareword_index',
# pattern=r'(?<={) *[A-Za-z0-9_]+(?=})',
#),
PatternRule(
name=r'length_scalar',
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'system_scalar',
pattern=r"\$[][><ab/'\"_@\?#\$!%^|&*()](?![A-Za-z0-9_])",
),
PatternRule(
name=r'system_array',
pattern=r"@_",
),
PatternRule(
name=r'scalar',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'array',
pattern=r"@\$*[A-Za-z_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'hash',
pattern=r"%\$*[A-Za-z_](?:[A-Za-z0-9_]|::)*",
),
]
class PerlGrammar(Grammar):
rules = [
RegionRule(
name=r'heredoc',
start=r"<< *(?P<heredoc>[a-zA-Z0-9_]+) *;",
grammar=StringGrammar(),
end=r'^%(heredoc)s$',
),
RegionRule(
name=r'endblock',
start=r"^__END__|__DATA__ *$",
grammar=Grammar(),
end=r'',
),
RegionRule(
name=r'pod',
start=r'^=[a-zA-Z0-9_]+',
grammar=PodGrammar(),
end=r'^=cut',
),
PatternRule(
name=r'comment',
pattern=r'#.*$',
),
RegionRule(
name=r'string1',
start=r'"',
grammar=StringGrammar(),
end=r'"',
),
RegionRule(
name=r'string2',
start=r"'",
grammar=Grammar(),
end=r"'",
),
RegionRule(
name=r'evalstring',
start=r"`",
grammar=StringGrammar(),
end=r"`",
),
PatternRule(
name=r'number',
pattern=r'0?\.[0-9]+|[0-9]+(?:\.[0-9]+)?',
),
PatternRule(
name=r'keyword',
pattern=r"(?<!->)(?:STDIN|STDERR|STDOUT|and|cmp|continue|do|else|elsif|eq|eval|foreach|for|if|last|my|next|ne|not|or|our|package|require|return|sub|undef|unless|until|use|while)(?![a-zA-Z0-9_])",
),
PatternRule(
name=r'bareword_hash_index',
pattern=r'(?<={)[A-Za-z0-9_]+(?=})',
),
PatternRule(
name=r'bareword_hash_key',
pattern=r'[A-Za-z0-9_]+(?= *=>)',
),
PatternRule(
name=r'length_scalar',
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'system_scalar',
pattern=r"\$[][><ab/'\"_@\?#\$!%^|&*()](?![A-Za-z0-9_])",
),
PatternRule(
name=r'system_array',
pattern=r"@_",
),
PatternRule(
name=r'scalar',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'array',
pattern=r"@\$*[A-Za-z_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'hash',
pattern=r"%\$*[A-Za-z_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'dereference',
pattern=r"[@%\$&\*](?={)",
),
RegionRule(
name=r'quoted_region1',
start=r'q[rqwx]? *(?P<delim>[^ #])',
grammar=Grammar(),
end=r'%(delim)s',
),
RegionRule(
name=r'quoted_region2',
start=r'q[rqwx]?#',
grammar=Grammar(),
end=r'#',
),
RegionRule(
name=r'bracket_quoted_region1',
start=r'q[rqwx]? *\(',
grammar=Grammar(),
end=r'\)',
),
RegionRule(
name=r'bracket_quoted_region2',
start=r'q[rqwx]? *{',
grammar=Grammar(),
end=r'}',
),
RegionRule(
name=r'bracket_quoted_region3',
start=r'q[rqwx]? *<',
grammar=Grammar(),
end=r'>',
),
RegionRule(
name=r'bracket_quoted_region4',
start=r'q[rqwx]? *\[',
grammar=Grammar(),
end=r'\]',
),
RegionRule(
name=r'implicit_match_regex',
start=r'(?:(?<==~)|(?<=!~)|(?<=\()) */',
grammar=StringGrammar(),
end=r'/',
),
RegionRule(
name=r'explicit_match_regex1',
start=r'm *(?P<delim>[^ #a-zA-Z0-9_])',
grammar=StringGrammar(),
end=r'%(delim)s',
),
RegionRule(
name=r'explicit_match_regex1',
start=r'm#',
grammar=StringGrammar(),
end=r'#',
),
DualRegionRule(
name=r'replace_regex',
start=r's */',
grammar1=StringGrammar(),
middle=r' */ *',
grammar2=StringGrammar(),
end=r'/ *[a-z]*',
),
# we officially don't support the bullshit s{a}{b} thing perl has going.
# those guys are on crack. we only support things like s#a#b# or s/a/b/.
# same comments as above apply
#{'name': 'replace regex',
# 'expr': r"""(?:y|tr|s)([^<[{(A-Za-z0-9 \t\n])(?:\\.|[^\\])*?\1(?:\\.|[^\\])*?\1[a-z]*""",
# 'action': lex.make_token},
PatternRule(
name=r'package',
pattern=r"(?<=package )(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'sub',
pattern=r"(?<=sub )[a-zA-Z_][a-zA-Z_0-9]*(?= *{)",
),
PatternRule(
name=r'use',
pattern=r"(?<=use )(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'label',
pattern=r'[a-zA-Z_][a-zA-Z0-9_]*:(?!:)',
),
PatternRule(
name=r'instance_method',
pattern=r"(?<=->)[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'static_method',
pattern=r"&?(?:[a-zA-Z_][a-zA-Z_0-9]*::)+[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'builtin_method',
pattern=r"(?<!->)&?(?:write|warn|wantarray|waitpid|wait|vec|values|utime|use|untie|unshift|unpack|unlink|undef|umask|ucfirst|uc|truncate|times|time|tied|tie|telldir|tell|syswrite|system|sysseek|sysread|sysopen|syscall|symlink|substr|sub|study|stat|srand|sqrt|sprintf|split|splice|sort|socketpair|socket|sleep|sin|shutdown|shmwrite|shmread|shmget|shmctl|shift|setsockopt|setservent|setpwent|setprotoent|setpriority|setpgrp|setnetent|sethostent|setgrent|send|semop|semget|semctl|select|seekdir|seek|scalar|rmdir|rindex|rewinddir|reverse|return|reset|require|rename|ref|redo|recv|readpipe|readlink|readline|readdir|read|rand|quotemeta|push|prototype|printf|print|pos|pop|pipe|package|pack|our|ord|opendir|open|oct|no|next|my|msgsnd|msgrcv|msgget|msgctl|mkdir|map|lstat|log|lock|localtime|local|listen|link|length|lcfirst|lc|last|kill|keys|join|ioctl|int|index|import|hex|grep|goto|gmtime|glob|getsockopt|getsockname|getservent|getservbyport|getservbyname|getpwuid|getpwnam|getpwent|getprotoent|getprotobynumber|getprotobyname|getpriority|getppid|getpgrp|getpeername|getnetent|getnetbyname|getnetbyaddr|getlogin|gethostent|gethostbyname|gethostbyaddr|getgrnam|getgrgid|getgrent|getc|formline|format|fork|flock|fileno|fcntl|exp|exit|exists|exec|eval|eof|endservent|endpwent|endprotoent|endnetent|endhostent|endgrent|each|dump|do|die|delete|defined|dbmopen|dbmclose|crypt|cos|continue|connect|closedir|close|chroot|chr|chown|chop|chomp|chmod|chdir|caller|bless|binmode|bind|atan2|alarm|accept|abs)(?![a-zA-Z0-9_])",
),
PatternRule(
name=r'method',
pattern=r"&(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'ref_method',
pattern=r"&\$(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'bareword_method',
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()",
),
#PatternRule(
# name=r'delimiter',
# pattern=r"\(|\)|\[|\]|{|}|,|;|->|=>|=|\?|(?<!:):(?!=:)",
#),
RegionRule(
name=r'paren',
start=r'\(',
grammar=None,
end=r'\)',
),
RegionRule(
name=r'brace',
start=r'{',
grammar=None,
end=r'}',
),
RegionRule(
name=r'bracket',
start=r'\[',
grammar=None,
end=r'\]',
),
PatternRule(
name=r'delimiter',
pattern=r",|;|->|=>|=|\?|(?<!:):(?!=:)",
),
PatternRule(
name=r'unary_operator',
pattern=r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*=",
),
PatternRule(
name=r'operator',
pattern=r"\+|<=>|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|=~|!~|!=|%|!|\.",
),
PatternRule(
name=r'bareword',
pattern=r'(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*',
),
]