Skip to content

Commit

Permalink
RakuAST for regex anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jul 7, 2020
1 parent 55e15db commit 89921f7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Raku/Actions.nqp
Expand Up @@ -779,6 +779,12 @@ class Raku::RegexActions is HLL::Actions {
$res := nqp::atkey($res.WHO, $t2) unless nqp::isnull($res);
nqp::ifnull($res, nqp::die("No such node RakuAST::{$t1}::{$t2}"))
}
multi method r($t1, $t2, $t3) {
my $res := nqp::atkey($ast_root, $t1);
$res := nqp::atkey($res.WHO, $t2) unless nqp::isnull($res);
$res := nqp::atkey($res.WHO, $t3) unless nqp::isnull($res);
nqp::ifnull($res, nqp::die("No such node RakuAST::{$t1}::{$t2}::{$t3}"))
}

method nibbler($/) {
make $<termseq>.ast;
Expand Down Expand Up @@ -866,4 +872,28 @@ class Raku::RegexActions is HLL::Actions {
make self.r('Regex', 'Literal').new(~$/);
}
}

method metachar:sym<^>($/) {
make self.r('Regex', 'Anchor', 'BeginningOfString').new;
}

method metachar:sym<^^>($/) {
make self.r('Regex', 'Anchor', 'BeginningOfLine').new;
}

method metachar:sym<$>($/) {
make self.r('Regex', 'Anchor', 'EndOfString').new;
}

method metachar:sym<$$>($/) {
make self.r('Regex', 'Anchor', 'EndOfLine').new;
}

method metachar:sym<lwb>($/) {
make self.r('Regex', 'Anchor', 'LeftWordBoundary').new;
}

method metachar:sym<rwb>($/) {
make self.r('Regex', 'Anchor', 'RightWordBoundary').new;
}
}
41 changes: 41 additions & 0 deletions src/Raku/ast/regex.rakumod
Expand Up @@ -121,3 +121,44 @@ class RakuAST::Regex::Literal is RakuAST::Regex::Atom {
QAST::Regex.new( :rxtype<literal>, $!text )
}
}

#| The base for all kinds of anchor.
class RakuAST::Regex::Anchor is RakuAST::Regex::Atom {
method new() {
nqp::create(self)
}

method IMPL-REGEX-QAST(RakuAST::IMPL::QASTContext $context, %mods) {
QAST::Regex.new( :rxtype<anchor>, :subtype(self.IMPL-QAST-SUBTYPE) )
}
}

#| The beginning of string (^) anchor.
class RakuAST::Regex::Anchor::BeginningOfString is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'bos' }
}

#| The beginning of line (^^) anchor.
class RakuAST::Regex::Anchor::BeginningOfLine is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'bol' }
}

#| The end of string ($) anchor.
class RakuAST::Regex::Anchor::EndOfString is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'eos' }
}

#| The end of line (^^) anchor.
class RakuAST::Regex::Anchor::EndOfLine is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'eol' }
}

#| The left word boundary (<<) anchor.
class RakuAST::Regex::Anchor::LeftWordBoundary is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'lwb' }
}

#| The right word boundary (>>) anchor.
class RakuAST::Regex::Anchor::RightWordBoundary is RakuAST::Regex::Anchor {
method IMPL-QAST-SUBTYPE() { 'rwb' }
}

0 comments on commit 89921f7

Please sign in to comment.