Anonymous avatar Anonymous committed 416edb0

Support more delimiters for perl regular expressions, as discussed in #258

Comments (0)

Files changed (3)

     more keywords.
   * Irc logs lexer now handles xchat's default time format (#340)
     and correctly highlights lines ending in ``>``
+  * Support more delimiters for perl regular expressions (#258)
 
 - Added "Visual Studio" style.
 

pygments/lexers/agile.py

     flags = re.DOTALL | re.MULTILINE
     # TODO: give this a perl guy who knows how to parse perl...
     tokens = {
+        'balanced-regex': [
+            (r'/(\\\\|\\/|[^/])*/[egimosx]*', String.Regex, '#pop'),
+            (r'!(\\\\|\\!|[^!])*![egimosx]*', String.Regex, '#pop'),
+            (r'\\(\\\\|[^\\])*\\[egimosx]*', String.Regex, '#pop'),
+            (r'{(\\\\|\\}|[^}])*}[egimosx]*', String.Regex, '#pop'),
+            (r'<(\\\\|\\>|[^>])*>[egimosx]*', String.Regex, '#pop'),
+            (r'\[(\\\\|\\\]|[^\]])*\][egimosx]*', String.Regex, '#pop'),
+            (r'\((\\\\|\\\)|[^\)])*\)[egimosx]*', String.Regex, '#pop'),
+            (r'@(\\\\|\\\@|[^\@])*@[egimosx]*', String.Regex, '#pop'),
+            (r'%(\\\\|\\\%|[^\%])*%[egimosx]*', String.Regex, '#pop'),
+            (r'\$(\\\\|\\\$|[^\$])*\$[egimosx]*', String.Regex, '#pop'),
+            (r'!(\\\\|\\!|[^!])*![egimosx]*', String.Regex, '#pop'),
+        ],
         'root': [
             (r'\#.*?$', Comment.Single),
             (r'=[a-zA-Z0-9]+\s+.*?\n[.\n]*?\n\s*=cut', Comment.Multiline),
             (r'(format)(\s+)([a-zA-Z0-9_]+)(\s*)(=)(\s*\n)',
              bygroups(Keyword, Text, Name, Text, Punctuation, Text), 'format'),
             (r'(eq|lt|gt|le|ge|ne|not|and|or|cmp)\b', Operator.Word),
+            # common delimiters
             (r's/(\\\\|\\/|[^/])*/(\\\\|\\/|[^/])*/[egimosx]*', String.Regex),
+            (r's!(\\\\|\\!|[^!])*!(\\\\|\\!|[^!])*![egimosx]*', String.Regex),
+            (r's\\(\\\\|[^\\])*\\(\\\\|[^\\])*\\[egimosx]*', String.Regex),
+            (r's@(\\\\|\\@|[^@])*@(\\\\|\\@|[^@])*@[egimosx]*', String.Regex),
+            (r's%(\\\\|\\%|[^%])*%(\\\\|\\%|[^%])*%[egimosx]*', String.Regex),
+            # balanced delimiters
+            (r's{(\\\\|\\}|[^}])*}\s*', String.Regex, 'balanced-regex'),
+            (r's<(\\\\|\\>|[^>])*>\s*', String.Regex, 'balanced-regex'),
+            (r's\[(\\\\|\\\]|[^\]])*\]\s*', String.Regex, 'balanced-regex'),
+            (r's\((\\\\|\\\)|[^\)])*\)\s*', String.Regex, 'balanced-regex'),
+
             (r'm?/(\\\\|\\/|[^/\n])*/[gcimosx]*', String.Regex),
             (r'((?<==~)|(?<=\())\s*/(\\\\|\\/|[^/])*/[gcimosx]*', String.Regex),
             (r'\s+', Text),

tests/examplefiles/regex-delims.pl

+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+
+# common delimiters
+print "a: ";
+my $a = "foo";
+print $a, " - ";
+$a =~ s/foo/bar/;
+print $a, "\n";
+
+print "b: ";
+my $b = "foo";
+print $b, " - ";
+$b =~ s!foo!bar!;
+print $b, "\n";
+
+print "c: ";
+my $c = "foo";
+print $c, " - ";
+$c =~ s@foo@bar@;
+print $c, "\n";
+
+print "d: ";
+my $d = "foo";
+print $d, " - ";
+$d =~ s\foo\bar\;
+print $d, "\n";
+
+print "\n";
+
+# balanced delimiters
+print "e: ";
+my $e = "foo";
+print $e, " - ";
+$e =~ s{foo}{bar};
+print $e, "\n";
+
+print "f: ";
+my $f = "foo";
+print $f, " - ";
+$f =~ s(foo)(bar);
+print $f, "\n";
+
+print "g: ";
+my $g = "foo";
+print $g, " - ";
+$g =~ s<foo><bar>;
+print $g, "\n";
+
+print "h: ";
+my $h = "foo";
+print $h, " - ";
+$h =~ s[foo][bar];
+print $h, "\n";
+
+print "\n";
+
+# balanced delimiters with whitespace
+print "i: ";
+my $i = "foo";
+print $i, " - ";
+$i =~ s{foo} {bar};
+print $i, "\n";
+
+print "j: ";
+my $j = "foo";
+print $j, " - ";
+$j =~ s<foo>		<bar>;
+print $j, "\n";
+
+print "k: ";
+my $k = "foo";
+print $k, " - ";
+$k =~
+	s(foo)
+
+	(bar);
+print $k, "\n";
+
+print "\n";
+
+# mixed delimiters
+print "l: ";
+my $l = "foo";
+print $l, " - ";
+$l =~ s{foo} <bar>;
+print $l, "\n";
+
+print "m: ";
+my $m = "foo";
+print $m, " - ";
+$m =~ s(foo) !bar!;
+print $m, "\n";
+
+print "n: ";
+my $n = "foo";
+print $n, " - ";
+$n =~ s[foo] $bar$;
+print $n, "\n";
+
+print "\n";
+
+# /x modifier
+print "o: ";
+my $o = "foo";
+print $o, " - ";
+$o =~ s{
+				foo
+			 } {bar}x;
+print $o, "\n";
+
+print "p: ";
+my $p = "foo";
+print $p, " - ";
+$p =~ s%
+  foo
+  %bar%x;
+print $p, "\n";
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.