Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3 more passing regex/grammar test files.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' ); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
|