Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
clean up longest-alternative.t
* use lexical tokens, reported by miso2217++
* stop relying on outdate :g in scalar context behavior
* Grammar.parse(:action*s*)
  • Loading branch information
moritz committed Apr 15, 2011
1 parent 3d1828e commit 1c3c6f7
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions S05-metasyntax/longest-alternative.t
Expand Up @@ -7,23 +7,21 @@ plan 31;

my $str = 'a' x 7;

#?rakudo skip ':g'
{
ok $str ~~ m:g/a|aa|aaaa/, 'basic sanity with |';
ok $str ~~ m:c(0)/a|aa|aaaa/, 'basic sanity with |';
is ~$/, 'aaaa', 'Longest alternative wins 1';

ok $str ~~ m:g/a|aa|aaaa/, 'Second match still works';
ok $str ~~ m:c(4)/a|aa|aaaa/, 'Second match still works';
is ~$/, 'aa', 'Longest alternative wins 2';

ok $str ~~ m:g/a|aa|aaaa/, 'Third match still works';
ok $str ~~ m:c(6)/a|aa|aaaa/, 'Third match still works';
is ~$/, 'a', 'Only one alternative left';

ok $str !~~ m:g/a|aa|aaaa/, 'No fourth match';
ok $str !~~ m:c(7)/a|aa|aaaa/, 'No fourth match';
}

# now test with different order in the regex - it shouldn't matter at all

#?rakudo skip ':g'
{
ok $str ~~ m:g/aa|a|aaaa/, 'basic sanity with |, different order';
is ~$/, 'aaaa', 'Longest alternative wins 1, different order';
Expand All @@ -37,63 +35,61 @@ my $str = 'a' x 7;
ok $str !~~ m:g/aa|a|aaaa/, 'No fourth match, different order';
}

#?rakudo skip 'interpolation in regexes'
{
my @list = <a aa aaaa>;
ok $str ~~ m:g/ @list /, 'basic sanity with interpolated arrays';
ok $str ~~ m/ @list /, 'basic sanity with interpolated arrays';
is ~$/, 'aaaa', 'Longest alternative wins 1';

ok $str ~~ m:g/ @list /, 'Second match still works';
ok $str ~~ m:c(4)/ @list /, 'Second match still works';
is ~$/, 'aa', 'Longest alternative wins 2';

ok $str ~~ m:g/ @list /, 'Third match still works';
ok $str ~~ m:c(6)/ @list /, 'Third match still works';
is ~$/, 'a', 'Only one alternative left';

ok $str !~~ m:g/ @list /, 'No fourth match';
ok $str !~~ m:c(7)/ @list /, 'No fourth match';
}

# L<S05/Longest-token matching/>

{
token ab { 'ab' };
token abb { 'abb' };
token a_word { a \w* };
token word { \w+ };
token a_star { a* };
token indirect_abb { <ab> 'b' }

ok ('abb' ~~ /<ab> | <abb> /) && ~$/ eq 'abb',
my token ab { 'ab' };
my token abb { 'abb' };
my token a_word { a \w* };
my token word { \w+ };
my token indirect_abb { <ab> 'b' }

ok ('abb' ~~ /<&ab> | <&abb> /) && ~$/ eq 'abb',
'LTM - literals in tokens';

ok ('abb' ~~ /<ab> | <indirect_abb> /) && $/ eq 'abb',
ok ('abb' ~~ /<&ab> | <&indirect_abb> /) && $/ eq 'abb',
'LTM - literals in nested torkens';

ok ('abb' ~~ /'ab' | \w+ / && $/) eq 'abb',
'LTM - longer quantified charclass wins against shorter literal';

ok ('abb' ~~ /<ab> | <a_word> /) && $/ eq 'abb',
ok ('abb' ~~ /<&ab> | <&a_word> /) && $/ eq 'abb',
'LTM - longer quantified atom wins against shorter literal (subrules)';

ok ('abb' ~~ / <abb> | <word> /) && $<abb>,
ok ('abb' ~~ / <abb=&abb> | <&word> /) && $<abb>,
'LTM - literal wins tie against \w*';

# with LTM stoppers
token foo1 {
my token foo1 {
a+
::: # a LTM stopper
.+
}
token foo2 { \w+ }
my token foo2 { \w+ }

ok ('aaab---' ~~ /<foo1> | <foo2> /) && $<foo2>,
ok ('aaab---' ~~ /<&foo1> | <foo2=&foo2> /) && $<foo2>,
'LTM only participated up to the LTM stopper :::';
}

# LTM stopper by implicit <.ws>
{
rule ltm_ws1 {\w+ '-'+}
token ltm_ws2 {\w+ '-'}
ok ('abc---' ~~ /<ltm_ws1> | <ltm_ws2>/) && $<ltm_ws2>,
my rule ltm_ws1 {\w+ '-'+}
my token ltm_ws2 {\w+ '-'}
ok ('abc---' ~~ /<&ltm_ws1> | <ltm_ws2=&ltm_ws2>/) && $<ltm_ws2>,
'implicit <.ws> stops LTM';
}

Expand All @@ -117,7 +113,7 @@ my $str = 'a' x 7;
method c($/) { $!matched_c = 1 };
}
my $o = LTM::T1::Action.new();
ok LTM::T1.parse('aaa---', :action($o)), 'LTM grammar - matched';
ok LTM::T1.parse('aaa---', :actions($o)), 'LTM grammar - matched';
is ~$/, 'aaa---', 'LTM grammar - matched full string';
# TODO: find out if $.matched_a is allowed to be set
ok $o.matched_TOP && $o.matched_b && $o.matched_c,
Expand Down

0 comments on commit 1c3c6f7

Please sign in to comment.