From 72e63eafc462850c79495a129d8b13b5baa5edcf Mon Sep 17 00:00:00 2001 From: moculus Date: Fri, 17 Aug 2007 04:06:26 +0000 Subject: [PATCH] fixed some small perl bugs --HG-- branch : pmacs2 --- mode/perl.py | 54 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/mode/perl.py b/mode/perl.py index 8ecf82b..8b5f7c9 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -13,17 +13,34 @@ class PodGrammar(Grammar): RegionRule(r'entry', r'(?<=^=encoding) +.*$', Grammar, '^\n$'), ] -class StringGrammar(Grammar): +def _make_string_rules(forbidden=None): + if forbidden: + rule = PatternRule(r'scalar', r"\$[^A-Za-z0-9 \\%s](?![A-Za-z0-9_])" % forbidden) + else: + rule = ContextPatternRule(r'scalar', r"\$[^A-Za-z0-9 %(delim)s](?![A-Za-z0-9_])", r"\$[^A-Za-z0-9 ](?![A-Za-z0-9_])") rules = [ PatternRule(r'octal', r'\\[0-7]{3}'), PatternRule(r'escaped', r'\\.'), PatternRule(r'deref', r"\$+[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*(?:->{\$?(?:[a-zA-Z_][a-zA-Z_0-9]*|'(?:\\.|[^'\\])*'|\"(\\.|[^\\\"])*\")}|->\[\$?[0-9a-zA-Z_]+\])+"), PatternRule(r'length', r"\$#[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*"), - ContextPatternRule(r'scalar', r"\$[^A-Za-z0-9 %(delim)s](?![A-Za-z0-9_])", r"\$[^A-Za-z0-9 ](?![A-Za-z0-9_])"), + rule, PatternRule(r'scalar', r"\$\$*[A-Za-z0-9_](?:[A-Za-z0-9_]|::)*"), PatternRule(r'cast', r"[\$\@\%\&]{.*?}"), PatternRule(r'array', r"@\$*[A-Za-z_](?:[A-Za-z0-9_]|::)*"), ] + return rules + +class StringGrammar(Grammar): + rules = _make_string_rules() + +class QuotedGrammar1(Grammar): + rules = _make_string_rules(')') +class QuotedGrammar2(Grammar): + rules = _make_string_rules('}') +class QuotedGrammar3(Grammar): + rules = _make_string_rules('>') +class QuotedGrammar4(Grammar): + rules = _make_string_rules(']') class PerlGrammar(Grammar): rules = [ @@ -77,13 +94,22 @@ class PerlGrammar(Grammar): PatternRule(r'function', r"&\$*(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*"), PatternRule(r'builtin', 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_])"), - # quote operator - RegionRule(r'quoted', r'q[rqwx]? *\(', Grammar, r'\)'), - RegionRule(r'quoted', r'q[rqwx]? *{', Grammar, r'}'), - RegionRule(r'quoted', r'q[rqwx]? *<', Grammar, r'>'), - RegionRule(r'quoted', r'q[rqwx]? *\[', Grammar, r'\]'), - RegionRule(r'quoted', r'q[rqwx]? *(?P[^ #])', Grammar, r'%(delim)s'), - RegionRule(r'quoted', r'q[rqwx]?#', Grammar, r'#'), + # quote operator: qq(), qx() and qr() usually interpolate + RegionRule(r'quoted', r'q[rqx] *(?P\()', QuotedGrammar1, r'\)'), + RegionRule(r'quoted', r'q[rqx] *(?P{)', QuotedGrammar2, r'}'), + RegionRule(r'quoted', r'q[rqx] *(?P<)', QuotedGrammar3, r'>'), + RegionRule(r'quoted', r'q[rqx] *(?P\[)', QuotedGrammar4, r'\]'), + RegionRule(r'quoted', r"q[rqx] *(?P')", Grammar, r"'"), + RegionRule(r'quoted', r'q[rqx] *(?P[^ #])', StringGrammar, r'%(delim)s'), + RegionRule(r'quoted', r'q[rqx](?P#)', StringGrammar, r'#'), + + # quote operator: q() and qw() do not interpolate + RegionRule(r'quoted', r'qw? *\(', Grammar, r'\)'), + RegionRule(r'quoted', r'qw? *{', Grammar, r'}'), + RegionRule(r'quoted', r'qw? *<', Grammar, r'>'), + RegionRule(r'quoted', r'qw? *\[', Grammar, r'\]'), + RegionRule(r'quoted', r'qw?#', Grammar, r'#'), + RegionRule(r'quoted', r'qw? *(?P[^ #])', Grammar, r'%(delim)s'), PatternRule(r'function', r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()"), PatternRule(r'class', r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?=->)"), @@ -230,10 +256,12 @@ class Perl(mode2.Fundamental): 'evalstring.end': ('cyan', 'default'), # quoted region - 'quoted': ('cyan', 'default'), - 'quoted.start': ('cyan', 'default'), - 'quoted.null': ('cyan', 'default'), - 'quoted.end': ('cyan', 'default'), + 'quoted': ('cyan', 'default'), + 'quoted.start': ('cyan', 'default'), + 'quoted.null': ('cyan', 'default'), + 'quoted.escaped': ('magenta', 'default'), + 'quoted.deref': ('yellow', 'default'), + 'quoted.end': ('cyan', 'default'), # match regex 'match.start': ('cyan', 'default'),