pmacs3/lex2_perl.py

353 lines
11 KiB
Python
Raw Normal View History

from lex2 import Grammar, ConstantRule, PatternRule, ContextPatternRule, RegionRule, DualRegionRule
2007-03-28 01:09:04 -04:00
class PodGrammar(Grammar):
rules = [
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'entry',
2007-03-28 01:09:04 -04:00
pattern=r'(?<=^=head[1-4]) +.*$',
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'entry',
2007-03-28 01:09:04 -04:00
pattern=r'(?<=^=over) +.*$',
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'entry',
2007-03-28 01:09:04 -04:00
pattern=r'(?<=^=item) +.*$',
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'entry',
2007-03-28 01:09:04 -04:00
pattern=r'(?:(?<=^=begin)|(?<=^=end)) +.*$',
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'entry',
2007-03-28 01:09:04 -04:00
pattern=r'(?<=^=encoding) +.*$',
),
]
class StringGrammar(Grammar):
rules = [
PatternRule(
name=r'escaped',
pattern=r'\\.',
),
PatternRule(
2007-04-01 01:35:10 -04:00
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_]+\])+",
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'length',
2007-03-28 01:09:04 -04:00
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
ContextPatternRule(
2007-04-01 01:35:10 -04:00
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_])",
2007-03-28 01:09:04 -04:00
),
2007-06-12 18:05:09 -04:00
#PatternRule(
# name=r'array',
# pattern=r"@_",
#),
2007-03-28 01:09:04 -04:00
PatternRule(
name=r'scalar',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
2007-06-12 18:05:09 -04:00
PatternRule(
name=r'cast',
pattern=r"[\$\@\%\&]{.*?}",
),
2007-03-28 01:09:04 -04:00
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 = [
2007-04-01 01:35:10 -04:00
# 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$',
),
2007-03-28 01:09:04 -04:00
RegionRule(
2007-04-01 01:35:10 -04:00
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_]+)` *;",
2007-03-28 01:09:04 -04:00
grammar=StringGrammar(),
end=r'^%(heredoc)s$',
),
2007-04-01 01:35:10 -04:00
# end block
2007-03-28 01:09:04 -04:00
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"'",
2007-03-28 18:38:32 -04:00
grammar=Grammar(),
2007-03-28 01:09:04 -04:00
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(
2007-04-01 01:35:10 -04:00
name=r'hash_key',
2007-03-31 21:37:42 -04:00
pattern=r'(?<={)[A-Za-z0-9_]+(?=})',
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'hash_key',
2007-03-28 01:09:04 -04:00
pattern=r'[A-Za-z0-9_]+(?= *=>)',
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'length',
2007-03-28 01:09:04 -04:00
pattern=r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*",
),
2007-06-13 11:44:09 -04:00
# XYZ
PatternRule(
name=r'cast',
pattern=r'[\$\@\%\^\&](?= *{)',
),
2007-03-28 01:09:04 -04:00
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'scalar',
2007-03-28 01:09:04 -04:00
pattern=r"\$[][><ab/'\"_@\?#\$!%^|&*()](?![A-Za-z0-9_])",
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'array',
2007-03-28 01:09:04 -04:00
pattern=r"@_",
),
2007-04-01 01:35:10 -04:00
PatternRule(
name=r'function',
pattern=r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?=-> *\()",
),
2007-03-28 01:09:04 -04:00
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(
2007-04-01 01:35:10 -04:00
name=r'deref',
2007-03-28 01:09:04 -04:00
pattern=r"[@%\$&\*](?={)",
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'quoted',
2007-03-28 01:09:04 -04:00
start=r'q[rqwx]? *\(',
grammar=Grammar(),
end=r'\)',
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'quoted',
2007-03-28 01:09:04 -04:00
start=r'q[rqwx]? *{',
grammar=Grammar(),
end=r'}',
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'quoted',
2007-03-28 01:09:04 -04:00
start=r'q[rqwx]? *<',
grammar=Grammar(),
end=r'>',
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'quoted',
2007-03-28 01:09:04 -04:00
start=r'q[rqwx]? *\[',
grammar=Grammar(),
end=r'\]',
),
2007-06-13 22:18:41 -04:00
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
2007-03-28 01:09:04 -04:00
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'match',
2007-04-02 11:25:57 -04:00
start=r'(?:(?<==~)|(?<=!~)|(?<=\()|(?<=split)) *(?P<delim>/)',
2007-03-28 01:09:04 -04:00
grammar=StringGrammar(),
end=r'/[a-z]*',
2007-03-28 01:09:04 -04:00
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'match',
2007-03-28 18:38:32 -04:00
start=r'm *(?P<delim>[^ #a-zA-Z0-9_])',
2007-03-28 01:09:04 -04:00
grammar=StringGrammar(),
end=r'%(delim)s[a-z]*',
2007-03-28 01:09:04 -04:00
),
RegionRule(
2007-04-01 01:35:10 -04:00
name=r'match',
start=r'm(?P<delim>#)',
2007-03-28 01:09:04 -04:00
grammar=StringGrammar(),
end=r'#[a-z]*',
),
# replace regexes
DualRegionRule(
2007-04-01 01:35:10 -04:00
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]*',
2007-03-28 01:09:04 -04:00
),
DualRegionRule(
2007-04-01 01:35:10 -04:00
name=r'replace',
start=r's(?P<delim>#)',
2007-03-28 01:09:04 -04:00
grammar1=StringGrammar(),
middle=r'#',
2007-03-28 01:09:04 -04:00
grammar2=StringGrammar(),
end=r'#[a-z]*',
2007-03-28 01:09:04 -04:00
),
2007-03-06 10:05:38 -05:00
2007-04-01 01:35:10 -04:00
# 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
2007-03-28 01:09:04 -04:00
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]*",
2007-03-28 01:09:04 -04:00
),
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',
2007-03-28 18:38:32 -04:00
pattern=r'[a-zA-Z_][a-zA-Z0-9_]*:(?!:)',
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'method',
2007-03-28 01:09:04 -04:00
pattern=r"(?<=->)[a-zA-Z_][a-zA-Z_0-9]*",
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'function',
pattern=r"&\$*(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*",
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'function',
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()",
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
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_])",
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'class',
2007-06-13 11:44:09 -04:00
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?=->)",
2007-03-28 01:09:04 -04:00
),
2007-04-01 01:35:10 -04:00
2007-06-13 22:18:41 -04:00
# # 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'\]',
# ),
2007-04-01 01:35:10 -04:00
# some basic stuff
2007-03-28 01:09:04 -04:00
PatternRule(
name=r'delimiter',
2007-03-29 18:37:34 -04:00
pattern=r",|;|->|=>|=|\?|(?<!:):(?!=:)",
2007-03-28 01:09:04 -04:00
),
PatternRule(
2007-04-01 01:35:10 -04:00
name=r'operator',
2007-03-28 01:09:04 -04:00
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]*',
),
]