Skip to content

Commit

Permalink
Use {} to much more thoroughly test LTM
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 14, 2010
1 parent 4a9b1cf commit cd0927e
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions test2.pl
@@ -1,24 +1,32 @@
# vim: ft=perl6
use Test;

sub _rxalt($C, $lad, $k, *@alts) {
sub lbody($ix) { @alts[$ix]($C, $k) }

Q:CgOp {
(letn csr (unbox Cursor (@ (l $C)))
lexer (rawnew Lexer (@ (l $C)) (clr_string "")
(unwrap 'LAD[]' (@ (l $lad))))
fates (rawcall (l lexer) Run (getfield backing (l csr))
(getfield pos (l csr)))
i (int 0)
nfate (getfield Length (l fates))
(whileloop 0 0 (< (l i) (l nfate)) (prog
(sink (subcall (@ (l &lbody))
(box Num (cast Double (getindex (l i) (l fates))))))
(l i (+ (l i) (int 1)))))
(null Variable))
};
}
my $x = '';
ok !("a" ~~ / a { $x = 1; } b /), '{} does not terminate regex';
is $x, 1, '{} is run even if regex fails';
$x = '';
ok !("" ~~ / a { $x = 1; } b /), '{} does not affect regex that ends before it';
is $x, '', '{} is only run if reached';
$x = 0;
ok ("aab" ~~ / a* { $x++ } ab /), '{} does not block backtracking';
is $x, 2, '{} is run multiple times when backtracking';

$x = '';
ok ("foo" ~~ / foo { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ foo|foo";
is $x, 1, "with no other constraints, first item is used";
$x = '';
ok ("foo" ~~ / fo* { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ fo*|foo";
is $x, 2, "longer literal prefix wins over seniority";
$x = '';
ok ("fooo" ~~ / fo* { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ fo*|foo";
is $x, 1, "longer length wins over prefix";
$x = '';
ok !("fooo" ~~ / [ fo* { $x = $x ~ 1 } | foo { $x = $x ~ 2 } ] x /),
"foo !~~ [fo*|foo]x";
is $x, '12', "will backtrack into shorter token";

my grammar G5 {
token a { foo }
Expand All @@ -34,4 +42,12 @@ ($C, $lad, $k, *@alts)
ok G5.parse('Afoobarx'), 'LTM works even through subrules';
ok G5.parse('Exxyxx'), 'recursivity does not crash LTM';

my grammar G6 {
token a { fo* { $x = 1 } }
token b { foo { $x = 2 } }
token TOP { <a> | <b> }
}
G6.parse("foo");
is $x, 2, "prefix length testing works in subrules";

done-testing;

0 comments on commit cd0927e

Please sign in to comment.