From f4b488a729a462a9092f9cb9cfa07088a85e38c9 Mon Sep 17 00:00:00 2001 From: pmichaud Date: Fri, 9 Oct 2009 03:42:12 -0500 Subject: [PATCH] Add min/max ** quantifiers. Clean up some tests. --- src/Regex/P6Regex/Actions.pm | 8 ++++++++ src/Regex/P6Regex/Grammar.pm | 7 +++++++ t/p6regex/rx_charclass | 8 ++++---- t/p6regex/rx_quantifiers | 21 --------------------- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/Regex/P6Regex/Actions.pm b/src/Regex/P6Regex/Actions.pm index 9aeab7b..eccdc38 100644 --- a/src/Regex/P6Regex/Actions.pm +++ b/src/Regex/P6Regex/Actions.pm @@ -73,6 +73,14 @@ method quantifier:sym($/) { make $past; } +method quantifier:sym<**>($/) { + my $past := $.ast; + $past.min(+$); + if ! $ { $past.max(+$); } + elsif $[0] ne '*' { $past.max(+$[0]); } + make $past; +} + method quantmod($/) { my $past := PAST::Regex.new( :pasttype('quant') ); my $str := ~$/; diff --git a/src/Regex/P6Regex/Grammar.pm b/src/Regex/P6Regex/Grammar.pm index 9fec740..9f92a1a 100644 --- a/src/Regex/P6Regex/Grammar.pm +++ b/src/Regex/P6Regex/Grammar.pm @@ -43,6 +43,13 @@ grammar Regex::P6Regex::Grammar is PCT::Grammar; token quantifier:sym<*> { $=['*'] {*} } token quantifier:sym<+> { $=['+'] {*} } token quantifier:sym { $=['?'] {*} } + token quantifier:sym<**> { + $=['**'] + [ + | $=[\d+] [ '..' $=[\d+|'*'] ]? + ] + {*} + } token quantmod { ':'? [ '?' | '!' | '+' ]? {*} } diff --git a/t/p6regex/rx_charclass b/t/p6regex/rx_charclass index 96abe7f..3ebeab2 100644 --- a/t/p6regex/rx_charclass +++ b/t/p6regex/rx_charclass @@ -3,12 +3,12 @@ # todo :pugs <[ z ]> abc def n character class ignores ws # todo :pugs -<[dcb]>**{3} abcdef y repeated character class +<[dcb]>**3 abcdef y repeated character class ^<[a]> abcdef y anchored character class <-[e]> abcdef y negated character class ^<[a]>? abcdef y anchored optional character class <-[e]>? abcdef y negated optional character class -<-[dcb]>**{3} abcdef n repeated negated character class +<-[dcb]>**3 abcdef n repeated negated character class ^<-[e]> abcdef y anchored negated character class ^<-[a]> abcdef n anchored negated character class <[b..d]> abcdef y character range @@ -45,9 +45,9 @@ ^\><[<]> >< y lt character class ^<[>]>\< >< y gt character class # todo :pugs -^<[><]>**{2} >< y gt, lt character class +^<[><]>**2 >< y gt, lt character class # todo :pugs -^<[<>]>**{2} >< y lt, gt character class +^<[<>]>**2 >< y lt, gt character class ^<-[><]> >< n not gt, lt character class ^<-[<>]> >< n not lt, gt character class '... --- ...' ... --- ... y literal match (\') diff --git a/t/p6regex/rx_quantifiers b/t/p6regex/rx_quantifiers index 43b1d1e..04367ff 100644 --- a/t/p6regex/rx_quantifiers +++ b/t/p6regex/rx_quantifiers @@ -160,27 +160,6 @@ xa?:a xay n ques cut 1 :ratchet xa?!a xay / ques ratchet greedy 1 -## Quantifier closure -.**{2} a n only one character -.**{2} ab y two characters -a**{2} foobar n only one "a" character -a**{2} baabaa y two "a" characters -a**{0..4} bbbbbbb y no "a" characters -a**{2..4} bababab n not two consecutive "a" characters -a**{2..4} baabbbb y two "a" characters -a**{2..4} baaabbb y three "a" characters -a**{2..4} baaaabb y four "a" characters -a**{2..4} baaaaaa y four "a" characters -a**{2..*} baaaaaa y six "a" characters -a**?{2..*} baaaaaa y two "a" characters (non-greedy) -a**:?{2..*} baaaaaa y two "a" characters (non-greedy) -a**!{2..*} baaaaaa y six "a" characters (explicit greed) -a**:!{2..*} baaaaaa y six "a" characters (explicit greed) -a**?{2..4} baaabbb y two "a" characters (non-greedy) -a**:?{2..4} baaabbb y two "a" characters (non-greedy) -a**!{2..4} baaabbb y three "a" characters (explicit greed) -a**:!{2..4} baaabbb y three "a" characters (explicit greed) - ## Quantifier bare range .**2 a n only one character .**2 ab y two characters