From 12c62a423c93fd1732cfba994644705e0b578c35 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Fri, 17 Jul 2009 16:56:32 -0400 Subject: [PATCH] gory perl fixes inspired by gory perl modules --HG-- branch : pmacs2 --- mode/perl.py | 58 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/mode/perl.py b/mode/perl.py index 31f6842..5610a3d 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -58,6 +58,14 @@ def _make_string_rules(forbidden): PatternRule('cast', r"[\$\@\%\&]{.+?}"), PatternRule('array', r"@\$*" + word2), ] + if forbidden == ')': + rules.append(PatternRule('data', r"\([^\(\)]*\)")) + elif forbidden == '}': + rules.append(PatternRule('data', r"{[^{}]*}")) + elif forbidden == ']': + rules.append(PatternRule('data', r"\[[^\[\]]*\]")) + elif forbidden == '>': + rules.append(PatternRule('data', r"<[^<>]*>")) return rules class QuotedWords(Grammar): rules = [ @@ -72,9 +80,14 @@ class NoAngle(Grammar): rules = [PatternRule('data', '[^>]+')] class NoHash(Grammar): rules = [PatternRule('data', '[^#]+')] class DataGrammar(Grammar): rules = [PatternRule('data', '.+')] -class StrictStringGrammar(Grammar): rules = [PatternRule('escaped', r"\\'")] -class StringGrammar(Grammar): rules = _make_string_rules('"') -class EvalGrammar(Grammar): rules = _make_string_rules('`') +class StrictStringGrammar(Grammar): rules = [ + PatternRule('escaped', r"\\[\\']"), + PatternRule('data', r"[^\\']+"), +] +class StringGrammar(Grammar): + rules = _make_string_rules('"') + [PatternRule('data', '[^\\"]+')] +class EvalGrammar(Grammar): + rules = _make_string_rules('`') + [PatternRule('data', '[^\\`]+')] class TranslateGrammar1(Grammar): rules = [PatternRule('data', r"(?:\\.|[^\\/])")] @@ -83,6 +96,7 @@ class TranslateGrammar2(Grammar): class TranslateGrammarX(Grammar): rules = [PatternRule('data', r"(?:\\.|[^\\%(delim)s])")] +class MatchGrammar0(Grammar): rules = _make_string_rules(None) class MatchGrammar1(Grammar): rules = _make_string_rules('/') class MatchGrammar2(Grammar): rules = _make_string_rules('#') class MatchGrammar3(Grammar): rules = _make_string_rules(')') @@ -122,13 +136,26 @@ class PerlGrammar(Grammar): RegionRule('perl.string', '"', StringGrammar, '"'), RegionRule('perl.string', "'", StrictStringGrammar, "'"), RegionRule('perl.evalstring', "`", EvalGrammar, "`"), - PatternRule('perl.number', r'0?\.[0-9]+|[0-9]+(?:\.[0-9]+)?'), - PatternRule('perl.keyword', "(?)(?:STDIN|STDERR|STDOUT|continue|do|else|elsif|eval|foreach|for|if|last|my|next|our|package|require|return|sub|undef|unless|until|use|while)(?![a-zA-Z0-9_])"), + PatternRule('perl.keyword', "(?)(?:STDIN|STDERR|STDOUT|continue|do|else|elsif|eval|foreach|for|if|last|my|next|no|our|package|require|return|sub|undef|unless|until|use|while)(?![a-zA-Z0-9_])"), PatternRule('perl.hash_key', '(?<={)' + wchr2 + '+(?=})'), PatternRule('perl.method', '(?<=->)' + word1), PatternRule('perl.hash_key', wchr2 + '+(?= *=>)'), PatternRule('perl.length', r"\$#" + word2), PatternRule('perl.cast', r'[\$\@\%\&\*](?= *{)'), + + PatternRule('perl.number', r'0[xX][0-9A-Fa-f]+'), + PatternRule('perl.number', r'0?\.[0-9]+|[0-9]+(?:\.[0-9]+)?'), + + # built-in scalars + PatternRule('perl.scalar', r'\$[_ab&`\'\+\*\./|,\\";#\%=\-~\^:\?!@\$<>()\[\]](?!' + wchr2 + ')'), + PatternRule('perl.scalar', r'\$\d+(?!' + wchr2 +')'), + PatternRule('perl.scalar', r'\$\^(?:' + word1 + '|' + wchr1 + ')'), + PatternRule('perl.scalar', r'\$\^O'), + PatternRule('perl.scalar', r'\${\^' + word1 + '}'), + + PatternRule('perl.array', r'\@[\+\-]'), + PatternRule('perl.hash', r'\%(?:[!]|^H)'), + PatternRule('perl.scalar', r"\$[\[\]<>ab/'\"_@\?#\$!%^|&*()](?!" + wchr2 + ")"), PatternRule('perl.array', "@_"), @@ -152,18 +179,10 @@ class PerlGrammar(Grammar): RegionRule('perl.match', r'(?:(?<==~)|(?<=!~)|(?<=\()|(?<=split)|(?<=if)|(?<=unless)|(?<=while)|(?<=until)|(?<=\|\|)|(?<=&&)|(?<==)) *(?P/)', MatchGrammar1, '/[a-z]*'), RegionRule('perl.match', 'm *(?P/)', MatchGrammar1, '/[a-z]*'), RegionRule('perl.match', 'm *(?P[^ #a-zA-Z0-9_])', - StringGrammar, '%(delim)s[a-z]*'), + MatchGrammar0, '%(delim)s[a-z]*'), RegionRule('perl.match', 'm(?P#)', MatchGrammar2, '#[a-z]*'), # match regexes; paired delimiters - #RegionRule('perl.replace', r's *(?P\()', MatchGrammar3, - # r'\) *\(', MatchGrammar3, r'\)[a-z]*'), - #RegionRule('perl.replace', r's *(?P\[)', MatchGrammar4, - # r'\] *\[', MatchGrammar4, r'\][a-z]*'), - #RegionRule('perl.replace', r's *(?P\{)', MatchGrammar5, - # r'\} *\{', MatchGrammar5, r'\}[a-z]*'), - #RegionRule('perl.replace', r's *(?P\<)', MatchGrammar6, - # r'\> *\<', MatchGrammar6, r'\>[a-z]*'), RegionRule('perl.replace', r's *(?P\()', MatchGrammar3, r'\)', WhitespaceGrammar, '\(', MatchGrammar3, r'\)[a-z]*'), RegionRule('perl.replace', r's *(?P\[)', MatchGrammar4, @@ -177,8 +196,8 @@ class PerlGrammar(Grammar): RegionRule('perl.replace', 's *(?P/)', MatchGrammar1, '/', MatchGrammar1, '/[a-z]*'), RegionRule('perl.replace', 's *(?P[^ a-zA-Z0-9_])', - StringGrammar, '%(delim)s', - StringGrammar, '%(delim)s[a-z]*'), + MatchGrammar0, '%(delim)s', + MatchGrammar0, '%(delim)s[a-z]*'), RegionRule('perl.replace', 's(?P#)', MatchGrammar2, '#', MatchGrammar2, '#[a-z]*'), @@ -195,6 +214,7 @@ class PerlGrammar(Grammar): PatternRule('perl.package', "(?<=package )" + word2), PatternRule('perl.sub', "(?<=sub )" + word2), PatternRule('perl.use', "(?<=use )" + word2), + PatternRule('perl.use', "(?<=no )" + word2), PatternRule('perl.require', "(?<=require )" + word2), PatternRule('perl.label', word1 + ':(?!:)'), PatternRule('perl.function', r"&\$*" + word2), @@ -207,8 +227,8 @@ class PerlGrammar(Grammar): RegionRule('perl.quoted', r'q[rqx] *(?P\[)', QuotedGrammar4, r'\]'), RegionRule('perl.quoted', "q[rqx] *(?P')", Grammar, "'"), RegionRule('perl.quoted', 'q[rqx] *(?P[^ a-zA-Z0-9#])', - StringGrammar, '%(delim)s'), - RegionRule('perl.quoted', 'q[rqx](?P#)', StringGrammar, '#'), + MatchGrammar0, '%(delim)s'), + RegionRule('perl.quoted', 'q[rqx](?P#)', MatchGrammar0, '#'), # quote operator: q() and qw() do not interpolate RegionRule('perl.quoted', r'qw? *\(', QuotedWords, r'\)'), @@ -781,6 +801,7 @@ class Perl(Fundamental): # match regex 'match.start': ('cyan', 'default', 'bold'), 'match.end': ('cyan', 'default', 'bold'), + 'match.data': ('cyan', 'default', 'bold'), 'match.null': ('cyan', 'default', 'bold'), # replace regex @@ -788,6 +809,7 @@ class Perl(Fundamental): 'replace.middle0': ('cyan', 'default', 'bold'), 'replace.middle1': ('cyan', 'default', 'bold'), 'replace.end': ('cyan', 'default', 'bold'), + 'replace.data': ('cyan', 'default', 'bold'), 'replace.null': ('cyan', 'default', 'bold'), 'replace.escaped': ('magenta', 'default', 'bold'), 'replace.deref': ('yellow', 'default', 'bold'),