pmacs3/lex2_perl.py

353 lines
11 KiB
Python
Executable File

from lex2 import Grammar, ConstantRule, PatternRule, ContextPatternRule, RegionRule, DualRegionRule
class PodGrammar(Grammar):
rules = [
PatternRule(
name=r'entry',
pattern=r'(?<=^=head[1-4]) +.*$',
),
PatternRule(
name=r'entry',
pattern=r'(?<=^=over) +.*$',
),
PatternRule(
name=r'entry',
pattern=r'(?<=^=item) +.*$',
),
PatternRule(
name=r'entry',
pattern=r'(?:(?<=^=begin)|(?<=^=end)) +.*$',
),
PatternRule(
name=r'entry',
pattern=r'(?<=^=encoding) +.*$',
),
]
class StringGrammar(Grammar):
rules = [
PatternRule(
name=r'escaped',
pattern=r'\\.',
),
PatternRule(
name=r'deref',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?:->{\$?(?:[a-zA-Z_][a-zA-Z_0-9]*|'(?:\\.|[^'\\])*'|\"(\\.|[^\\\"])*\")}|->\[\$?[0-9a-zA-Z_]+\])+",
),
PatternRule(
name=r'length',
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
ContextPatternRule(
name=r'scalar',
pattern=r"\$[^A-Za-z0-9 %(delim)s](?![A-Za-z0-9_])",
fallback=r"\$[^A-Za-z0-9 ](?![A-Za-z0-9_])",
),
#PatternRule(
# name=r'array',
# pattern=r"@_",
#),
PatternRule(
name=r'scalar',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
PatternRule(
name=r'cast',
pattern=r"[\$\@\%\&]{.*?}",
),
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 = [
# heredocs
RegionRule(
name=r'heredoc1',
start=r"<<(?P<heredoc>[a-zA-Z0-9_]+) *;",
grammar=StringGrammar(),
end=r'^%(heredoc)s$',
),
RegionRule(
name=r'heredoc1',
start=r'<< *"(?P<heredoc>[a-zA-Z0-9_]+)" *;',
grammar=StringGrammar(),
end=r'^%(heredoc)s$',
),
RegionRule(
name=r'heredoc2',
start=r"<< *'(?P<heredoc>[a-zA-Z0-9_]+)' *;",
grammar=Grammar(),
end=r'^%(heredoc)s$',
),
RegionRule(
name=r'eval_heredoc',
start=r"<< *`(?P<heredoc>[a-zA-Z0-9_]+)` *;",
grammar=StringGrammar(),
end=r'^%(heredoc)s$',
),
# end block
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'hash_key',
pattern=r'(?<={)[A-Za-z0-9_]+(?=})',
),
PatternRule(
name=r'hash_key',
pattern=r'[A-Za-z0-9_]+(?= *=>)',
),
PatternRule(
name=r'length',
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
# XYZ
PatternRule(
name=r'cast',
pattern=r'[\$\@\%\^\&](?= *{)',
),
PatternRule(
name=r'scalar',
pattern=r"\$[][><ab/'\"_@\?#\$!%^|&*()](?![A-Za-z0-9_])",
),
PatternRule(
name=r'array',
pattern=r"@_",
),
PatternRule(
name=r'function',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?=-> *\()",
),
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'deref',
pattern=r"[@%\$&\*](?={)",
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]? *\(',
grammar=Grammar(),
end=r'\)',
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]? *{',
grammar=Grammar(),
end=r'}',
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]? *<',
grammar=Grammar(),
end=r'>',
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]? *\[',
grammar=Grammar(),
end=r'\]',
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]? *(?P<delim>[^ #])',
grammar=Grammar(),
end=r'%(delim)s',
),
RegionRule(
name=r'quoted',
start=r'q[rqwx]?#',
grammar=Grammar(),
end=r'#',
),
# match regexes
RegionRule(
name=r'match',
start=r'(?:(?<==~)|(?<=!~)|(?<=\()|(?<=split)) *(?P<delim>/)',
grammar=StringGrammar(),
end=r'/[a-z]*',
),
RegionRule(
name=r'match',
start=r'm *(?P<delim>[^ #a-zA-Z0-9_])',
grammar=StringGrammar(),
end=r'%(delim)s[a-z]*',
),
RegionRule(
name=r'match',
start=r'm(?P<delim>#)',
grammar=StringGrammar(),
end=r'#[a-z]*',
),
# replace regexes
DualRegionRule(
name=r'replace',
start=r's *(?P<delim>[^ a-zA-Z0-9_])',
grammar1=StringGrammar(),
middle=r'%(delim)s',
grammar2=StringGrammar(),
end=r'%(delim)s[a-z]*',
),
DualRegionRule(
name=r'replace',
start=r's(?P<delim>#)',
grammar1=StringGrammar(),
middle=r'#',
grammar2=StringGrammar(),
end=r'#[a-z]*',
),
# translate operator
DualRegionRule(
name=r'translate',
start=r'(?:y|tr) *(?P<delim>[^ a-zA-Z0-9_])',
grammar1=Grammar(),
middle=r'%(delim)s',
grammar2=Grammar(),
end=r'%(delim)s[a-z]*',
),
DualRegionRule(
name=r'translate',
start=r'(?:y|tr)#',
grammar1=Grammar(),
middle=r'#',
grammar2=Grammar(),
end=r'#[a-z]*',
),
# some more basic stuff
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'method',
pattern=r"(?<=->)[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'function',
pattern=r"&\$*(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
name=r'function',
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()",
),
PatternRule(
name=r'builtin',
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'class',
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?=->)",
),
# # nested regions
# 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'\]',
# ),
# some basic stuff
PatternRule(
name=r'delimiter',
pattern=r",|;|->|=>|=|\?|(?<!:):(?!=:)",
),
PatternRule(
name=r'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]*',
),
]