Permalink
Browse files

Implement backreferences.

  • Loading branch information...
1 parent 9a42dc5 commit 5c4d6b2786934c5ff8c3032fc71d1cf6502fabb9 @jnthn jnthn committed Sep 29, 2012
Showing with 12 additions and 3 deletions.
  1. +9 −2 src/QRegex/P5Regex/Actions.nqp
  2. +1 −1 src/QRegex/P5Regex/Grammar.nqp
  3. +2 −0 t/p5regex/rx_captures
View
11 src/QRegex/P5Regex/Actions.nqp
@@ -164,8 +164,15 @@ class QRegex::P5Regex::Actions is HLL::Actions {
}
method p5backslash:sym<misc>($/) {
- my $qast := QAST::Regex.new( ~$/ , :rxtype('literal'), :node($/) );
- make $qast;
+ if $<litchar> {
+ make QAST::Regex.new( ~$<litchar> , :rxtype('literal'), :node($/) );
+ }
+ else {
+ make QAST::Regex.new( :rxtype<subrule>, :subtype<method>, :node($/),
+ QAST::Node.new(
+ QAST::SVal.new( :value('!BACKREF') ),
+ QAST::SVal.new( :value(~$<number> - 1) ) ) );
+ }
}
method p5quantifier:sym<*>($/) {
View
2 src/QRegex/P5Regex/Grammar.nqp
@@ -83,7 +83,7 @@ grammar QRegex::P5Regex::Grammar is HLL::Grammar {
token p5backslash:sym<b> { $<sym>=[<[bB]>] }
token p5backslash:sym<s> { $<sym>=[<[dDnNsSwW]>] }
- token p5backslash:sym<misc> { \W }
+ token p5backslash:sym<misc> { $<litchar>=(\W) | $<number>=(\d+) }
token p5backslash:sym<oops> { <.panic: "Unrecognized Perl 5 regex backslash sequence"> }
proto token p5assertion { <...> }
View
2 t/p5regex/rx_captures
@@ -14,3 +14,5 @@ d()ef zzzabcdefzzz y empty capture
(a)b(c) abc y multiple matches
(a)b(c) abc <mob[0]: a @ 0> basic $0
(a)b(c) abc <mob[1]: c @ 2> basic $1
+b(.)\1 bookkeeper y backreference
+b(.)\1(.)\2 bookkeeper y backreference

0 comments on commit 5c4d6b2

Please sign in to comment.