diff --git a/mode_perl.py b/mode_perl.py index 51b8840..c41f6ff 100644 --- a/mode_perl.py +++ b/mode_perl.py @@ -1,6 +1,104 @@ import re, sets, string, sys -import color, commands, default, lex2, lex2_perl, method, mode2, regex, tab_perl +import color, commands, default, lex2, method, mode2, regex, tab_perl from point2 import Point +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'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_]|::)*"), + ], + +g = Grammar() +pg = PodGrammar() +sg = StringGrammar() + +class PerlGrammar(Grammar): + rules = [ + RegionRule(name=r'heredoc1', start=r"<<(?P[a-zA-Z0-9_]+) *;", grammar=sg, end=r'^%(heredoc)s$'), + RegionRule(name=r'heredoc1', start=r'<< *"(?P[a-zA-Z0-9_]+)" *;', grammar=sg, end=r'^%(heredoc)s$'), + RegionRule(name=r'heredoc2', start=r"<< *'(?P[a-zA-Z0-9_]+)' *;", grammar=g, end=r'^%(heredoc)s$'), + RegionRule(name=r'eval_heredoc', start=r"<< *`(?P[a-zA-Z0-9_]+)` *;", grammar=sg, end=r'^%(heredoc)s$'), + + RegionRule(name=r'endblock', start=r"^__END__|__DATA__ *$", grammar=g, end=r''), + RegionRule(name=r'pod', start=r'^=[a-zA-Z0-9_]+', grammar=pg, end=r'^=cut'), + + PatternRule(name=r'comment', pattern=r'#.*$'), + RegionRule(name=r'string1', start=r'"', grammar=sg, end=r'"'), + RegionRule(name=r'string2', start=r"'", grammar=g, end=r"'"), + RegionRule(name=r'evalstring', start=r"`", grammar=sg, 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_]|::)*"), + PatternRule(name=r'cast', pattern=r'[\$\@\%\^\&](?= *{)'), + PatternRule(name=r'scalar', 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'deref', pattern=r"[@%\$&\*](?={)"), + + RegionRule(name=r'quoted', start=r'q[rqwx]? *\(', grammar=g, end=r'\)'), + RegionRule(name=r'quoted', start=r'q[rqwx]? *{', grammar=g, end=r'}'), + RegionRule(name=r'quoted', start=r'q[rqwx]? *<', grammar=g, end=r'>'), + RegionRule(name=r'quoted', start=r'q[rqwx]? *\[', grammar=g, end=r'\]'), + RegionRule(name=r'quoted', start=r'q[rqwx]? *(?P[^ #])', grammar=g, end=r'%(delim)s'), + RegionRule(name=r'quoted', start=r'q[rqwx]?#', grammar=g, end=r'#'), + + # match regexes + RegionRule(name=r'match', start=r'(?:(?<==~)|(?<=!~)|(?<=\()|(?<=split)) *(?P/)', grammar=sg, end=r'/[a-z]*'), + RegionRule(name=r'match', start=r'm *(?P[^ #a-zA-Z0-9_])', grammar=sg, end=r'%(delim)s[a-z]*'), + RegionRule(name=r'match', start=r'm(?P#)', grammar=sg, end=r'#[a-z]*'), + + # replace regexes + DualRegionRule(name=r'replace', start=r's *(?P[^ a-zA-Z0-9_])', grammar1=sg, middle=r'%(delim)s', grammar2=sg, end=r'%(delim)s[a-z]*'), + DualRegionRule(name=r'replace', start=r's(?P#)', grammar1=sg, middle=r'#', grammar2=sg, end=r'#[a-z]*'), + + # translate operator + DualRegionRule(name=r'translate', start=r'(?:y|tr) *(?P[^ a-zA-Z0-9_])', grammar1=g, middle=r'%(delim)s', grammar2=g, end=r'%(delim)s[a-z]*'), + DualRegionRule(name=r'translate', start=r'(?:y|tr)#', grammar1=g, middle=r'#', grammar2=g, 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'bareword', pattern=r'(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*'), + ] class Perl(mode2.Fundamental): def __init__(self, w):