Skip to content

Commit

Permalink
3 more passing regex/grammar test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Feb 16, 2013
1 parent c664f92 commit f91c444
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions t/nqp/31-grammar.t
@@ -0,0 +1,27 @@
#! nqp

# Test grammars and regexes

plan(6);

grammar ABC {
token TOP { ok ' ' <integer> }
token integer { \d+ }
token TOP2 { ok ' ' <int-num> }
token int-num { \d+ }
}

my $match := ABC.parse('not ok');
ok( !$match, 'parse method works on negative match');

ok( $match.chars == 0, 'failed match has 0 .chars');

$match := ABC.parse('ok 123');
ok( ?$match, 'parse method works on positive match');

ok( $match<integer> == 123, 'captured $<integer>');

$match := ABC.parse('ok 123', :rule<TOP2> );
ok( ?$match, 'parse method works with :rule');

ok( $match<int-num> == 123, 'captured $<int-num>');
36 changes: 36 additions & 0 deletions t/nqp/32-protoregex.t
@@ -0,0 +1,36 @@
#! nqp

# test protoregexes in grammars

plan(7);

grammar ABC {

token TOP { <symbols> .* }

proto token symbols { <...> }

token symbols:sym<abc> { <sym> }
token symbols:sym<a> { <sym> }
token symbols:sym<bang> { $<sym>=['!'] }
token symbols:sym<===> { <sym> }
}


my $/ := ABC.parse('abcdef');
ok( ?$/ , 'successfully matched grammar' );
ok( $/ eq 'abcdef', 'successful string match' );
ok( $<symbols> eq 'abc', 'successful protoregex match');
ok( $<symbols><sym> eq 'abc', 'correct proto candidate match' );

$/ := ABC.parse('adef');
ok( ?$/ , 'successfully matched grammar' );

$/ := ABC.parse('xxx');
ok( !$/ , 'successfully failed protoregex match' );

$/ := ABC.parse('xxx', :rule<symbols>);
ok( !$/ , 'successfully failed protoregex match' );



32 changes: 32 additions & 0 deletions t/nqp/34-rxcodeblock.t
@@ -0,0 +1,32 @@
#! nqp

plan(12);

grammar ABC {
token TOP { { ok(1, 'basic code assertion'); } }
}
ABC.parse('abc');

grammar BCD {
token TOP { $<bcd>=[.*] { ok( $<bcd> eq 'bcd', 'match in assertion' ); } }
}
BCD.parse('bcd');

grammar CDE {
token TOP { \d+ <?{ +$/ < 255}> cde }
}
ok( ?CDE.parse('123cde'), 'passes assertion, match after');
ok( !CDE.parse('1234cde'), 'fails assertion');
ok( ?CDE.parse('0cde'), 'passes assertion, match after');
ok( !CDE.parse('1234'), 'fails assertion');
ok( !CDE.parse('123'), 'fails regex after passing assertion');

grammar DEF {
token TOP { \d+ <!{ +$/ < 255 }> def }
}
ok( !DEF.parse('123def'), 'fails assertion');
ok( ?DEF.parse('1234def'), 'passes assertion, text after');
ok( !DEF.parse('0def'), 'fails assertion');
ok( !DEF.parse('1234'), 'passes assertion, fails text after');
ok( ?DEF.parse('999def'), 'passes assertion, text after');

0 comments on commit f91c444

Please sign in to comment.