Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created November 20, 2011 00:40
Show Gist options
  • Save jnthn/1379614 to your computer and use it in GitHub Desktop.
Save jnthn/1379614 to your computer and use it in GitHub Desktop.
diff --git a/src/QRegex/P6Regex/Actions.nqp b/src/QRegex/P6Regex/Actions.nqp
index 6e2ff9f..4fe6772 100644
--- a/src/QRegex/P6Regex/Actions.nqp
+++ b/src/QRegex/P6Regex/Actions.nqp
@@ -396,20 +396,33 @@ class QRegex::P6Regex::Actions is HLL::Actions {
my $qast;
if $<name> {
my $name := ~$<name>;
- $qast := QAST::Regex.new( PAST::Node.new($name), :rxtype<subrule>, :subtype<method>, :node($/) );
+ $qast := QAST::Regex.new( PAST::Node.new($name), :rxtype<subrule>, :subtype<method>,
+ :negate( $<sign> eq '-' ), :node($/) );
}
else {
+ my @alts;
for $<charspec> {
- if $_[1] {
+ if $_<backslash> {
+ my $bs := $_<backslash>.ast;
+ $bs.negate(!$bs.negate) if $<sign> eq '-';
+ @alts.push($bs);
+ }
+ elsif $_[1] {
my $ord0 := nqp::ord($_[0]);
my $ord1 := nqp::ord($_[1][0]);
$str := nqp::concat($str, nqp::chr($ord0++)) while $ord0 <= $ord1;
}
else { $str := $str ~ $_[0]; }
}
- $qast := QAST::Regex.new( $str, :rxtype<enumcharlist>, :node($/) );
+ @alts.push(QAST::Regex.new( $str, :rxtype<enumcharlist>, :node($/), :negate( $<sign> eq '-' ) ))
+ if nqp::chars($str);
+ $qast := +@alts == 1 ?? @alts[0] !!
+ $<sign> eq '-' ??
+ QAST::Regex.new( :rxtype<concat>, :node($/),
+ QAST::Regex.new( :rxtype<conj>, :subtype<zerowidth>, |@alts ),
+ QAST::Regex.new( :rxtype<cclass>, :subtype<.> ) ) !!
+ QAST::Regex.new( :rxtype<alt>, |@alts );
}
- $qast.negate( $<sign> eq '-' );
make $qast;
}
diff --git a/src/QRegex/P6Regex/Grammar.nqp b/src/QRegex/P6Regex/Grammar.nqp
index 8cef973..edd87eb 100644
--- a/src/QRegex/P6Regex/Grammar.nqp
+++ b/src/QRegex/P6Regex/Grammar.nqp
@@ -200,8 +200,9 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
<.normspace>?
[
| '[' $<charspec>=(
- | \s* '-' <!before \s* ']'> <.obs: '- as character range','.. for range, for explicit - in character class, escape it or place as last thing'>
- | \s* [ \\ (.) | (<-[\]\\]>) ] [ \s* '..' \s* (.) ]?
+ || \s* '-' <!before \s* ']'> <.obs: '- as character range','.. for range, for explicit - in character class, escape it or place as last thing'>
+ || \s* '\\' <backslash>
+ || \s* (<-[\]\\]>) [ \s* '..' \s* (.) ]?
)*
\s* ']'
| $<name>=[\w+]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment