Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement grammars properly
  • Loading branch information
sorear committed Aug 7, 2010
1 parent e74a2d3 commit 5313337
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Decl.pm
Expand Up @@ -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!";
}
Expand Down
3 changes: 2 additions & 1 deletion Niecza/Actions.pm
Expand Up @@ -42,6 +42,7 @@ sub comment { }
sub comment__S_Sharp { }
sub spacey { }
sub unspacey { }
sub unsp { }
sub nofun { }
sub curlycheck { }
sub pod_comment { }
Expand Down Expand Up @@ -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));
Expand Down
17 changes: 17 additions & 0 deletions test2.pl
Expand Up @@ -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";
Expand All @@ -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;

0 comments on commit 5313337

Please sign in to comment.