From 5313337236c30fd4e658b88a6249a33b1d496f6d Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Sat, 7 Aug 2010 03:35:31 -0700 Subject: [PATCH] implement grammars properly --- Decl.pm | 2 +- Niecza/Actions.pm | 3 ++- test2.pl | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Decl.pm b/Decl.pm index c97b802f..4d2d4994 100644 --- a/Decl.pm +++ b/Decl.pm @@ -375,7 +375,7 @@ use CgOp; sub preinit_code { my ($self, $body) = @_; - if ($body->type ne 'class') { + if ($body->type !~ /class|grammar|role/) { #TODO: Make this a sorry. die "Tried to set a method outside a class!"; } diff --git a/Niecza/Actions.pm b/Niecza/Actions.pm index a086aff6..36c56fe4 100644 --- a/Niecza/Actions.pm +++ b/Niecza/Actions.pm @@ -42,6 +42,7 @@ sub comment { } sub comment__S_Sharp { } sub spacey { } sub unspacey { } +sub unsp { } sub nofun { } sub curlycheck { } sub pod_comment { } @@ -220,7 +221,7 @@ sub regex_def { my ($cl, $M) = @_; $M->{_ast} = Op::SubDef->new( var => $var, class => 'Regex', method_too => ($scope eq 'has' ? $name : undef), - code => Body->new( + body => Body->new( type => 'regex', signature => $sig, do => RxOp::Export->new(zyg => [$M->{regex_block}{_ast}])->op)); diff --git a/test2.pl b/test2.pl index d6d3ffcd..2702ad24 100644 --- a/test2.pl +++ b/test2.pl @@ -93,6 +93,14 @@ ($C, $str) } } +my class Grammar is Cursor { + method parse($text) { + my @results := self.RAWCREATE("str", $text, "from", 0).TOP\ + .grep({ $_.from == $_.str.chars }); + @results ?? @results.shift !! Any; # TODO List.at-pos + } +} + ok ("a" ~~ /a/), "letter matches itself"; ok !("a" ~~ /b/), "letter does not match other"; ok ("xxa" ~~ /a/), "leading garbage ignored"; @@ -110,4 +118,13 @@ ($C, $str) ok !("adc" ~~ /ab+c/), "plus cannot match other"; ok !("ac" ~~ /ab+c/), "plus cannot match none"; +grammar Bob { + rule TOP {ab*c} +} + +ok Bob.parse("abbc"), "grammars work (1)"; +ok !Bob.parse("adc"), "grammars work (2)"; +ok !Bob.parse("xac"), "grammars anchor (1)"; +ok !Bob.parse("acx"), "grammars anchor (2)"; + done-testing;