Skip to content

Instantly share code, notes, and snippets.

@ungrim97

ungrim97/Patch Secret

Created November 2, 2015 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ungrim97/65905b1393a7f7658dbb to your computer and use it in GitHub Desktop.
Save ungrim97/65905b1393a7f7658dbb to your computer and use it in GitHub Desktop.
diff --git a/src/QRegex/P6Regex/Actions.nqp b/src/QRegex/P6Regex/Actions.nqp
index c92fa02..b814b8c 100644
--- a/src/QRegex/P6Regex/Actions.nqp
+++ b/src/QRegex/P6Regex/Actions.nqp
@@ -161,12 +161,27 @@ class QRegex::P6Regex::Actions is HLL::Actions {
);
}
else {
- my $min := $<min>.ast;
- if $<min>.Str eq '^' { $min := 0 }
+ my $min := 0;
+ if $<min> { $min := $<min>.ast; }
+
my $max := -1;
- if ! $<max> { $max := $min }
+ my $upto := $<upto>;
+
+ if $<from> eq '^' { $min++ }
+
+ if ! $<max> {
+ if $<from> eq '^' {
+ $max := '*'
+ }
+ else {
+ $max := $min
+ }
+ }
elsif $<max> ne '*' {
$max := $<max>.ast;
+ if $<upto> eq '^' {
+ $max--;
+ }
$/.CURSOR.panic("Empty range") if $min > $max;
}
$qast := QAST::Regex.new( :rxtype<quant>, :min($min), :max($max), :node($/) );
diff --git a/src/QRegex/P6Regex/Grammar.nqp b/src/QRegex/P6Regex/Grammar.nqp
index e7f481e..f91b329 100644
--- a/src/QRegex/P6Regex/Grammar.nqp
+++ b/src/QRegex/P6Regex/Grammar.nqp
@@ -75,6 +75,10 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
self.panic('Spaces not allowed in bare range.');
}
+ method throw_unessary_upto_inf() {
+ self.panic('Unecessary use of "** ^*" qunatifier. Did you mean to use the "*" quantifier');
+ }
+
method throw_solitary_quantifier() {
self.panic('Quantifier quantifies nothing.');
}
@@ -254,20 +258,24 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
<.obs: '{N,M} as general quantifier', '** N..M (or ** N..*)'>
}
token quantifier:sym<**> {
+ # 10 | 1..10 | 1^..10 | 1^..^10 | 1..^10 | ^10 | 1..* | 1^..*
<sym> <.normspace>? <backmod> <.normspace>?
[
- | $<min>='^' [ <max=.integer> | $<max>='*' ]
| <min=.integer> \s+ '..' <.throw_spaces_in_bare_range>
- | <min=.integer>
- [ '..'
- [
- | <max=.integer> {
- $/.CURSOR.panic("Negative numbers are not allowed as quantifiers") if $<max>.Str < 0;
- }
- | $<max>=['*']
- | <.throw_malformed_range>
- ]
- ]?
+ | '^' '*' <.throw_unecessary_upto_inf>
+ | $<upto>='^' <max=.integer>
+ | [
+ | <min=.integer> $<from>='^'?
+ [ '..'
+ [
+ | $<upto>='^'? <max=.integer> {
+ $/.CURSOR.panic("Negative numbers are not allowed as quantifiers") if $<max>.Str < 0;
+ }
+ | $<max>=['*']
+ | <.throw_malformed_range>
+ ]
+ ]?
+ ]
{ $/.CURSOR.panic("Negative numbers are not allowed as quantifiers") if $<min>.Str < 0 }
| <?[{]> <codeblock>
]
diff --git a/t/qregex/rx_quantifiers b/t/qregex/rx_quantifiers
index bbe946f..7acdeae 100644
--- a/t/qregex/rx_quantifiers
+++ b/t/qregex/rx_quantifiers
@@ -182,9 +182,13 @@ 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)
xa**0 xaa <x @ 0> nothing matched by ** 0
-xa**0..0 xaa <x @ 0> nothing matched by ** 0..0
-a**^10 aaaaaaaaa y 9 "a" characters
-a**^10 bbbbbbbb y no "a" characters
-a**^* aaaaa y 5 "a" characters
-a**^* bbaaabbb y 3 "a" characters
-a**^* bbbbbbbb y no "a" characters
+xa**0..0 xaa <x @ 0> nothing matched by ** 0..0
+a**^10 aaaaaaaaa y 9 "a" characters
+a**0..^10 baaaaaaaaaab n 10 "a" character
+a**^10 baaaaaaaaaab n 10 "a" characters
+a**^10 bbbbbbbb y no "a" characters
+a**1^ aaa y 2 or more "a" characters
+a**1^ abbb n only 1 "a" character
+a**1..^10 bbaaaaaaaaa y 9 "a" characters
+a**2..^10 bba n only 1 "a" characters
+a**1^..^10 bba n only 1 "a" characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment