/
parse_and_parsefile.t
84 lines (69 loc) 路 2.43 KB
/
parse_and_parsefile.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use v6;
use Test;
plan 16;
# tests .parse and .parsefile methods on a grammar
grammar Foo { token TOP { \d+ } }
grammar Bar { token untop { \d+ } }
grammar Baz { token TOP { \d+ \n } }
nok(~Foo.parse("abc123xyz"), ".parse method invokes TOP rule, no match");
is(~Foo.parse("123"), "123", ".parse method invokes TOP rule, match");
nok(Foo.parse("123xyz"), ".parse method requires match to end");
is(~Foo.subparse("123xyz"), "123", ".subparse method doesn't require match to end");
dies-ok({ Bar.parse("abc123xyz") }, "dies if no TOP rule");
my $fh = open("parse_and_parsefile_test", :w);
$fh.say("abc\n123\nxyz");
$fh.close();
#?niecza skip 'Unable to resolve method parsefile in class Foo'
nok(Foo.parsefile("parse_and_parsefile_test"), ".parsefile method invokes TOP rule, no match");
unlink("parse_and_parsefile_test");
$fh = open("parse_and_parsefile_test", :w);
$fh.say("123");
$fh.close();
#?niecza skip 'Unable to resolve method parsefile in class Foo'
is(~Baz.parsefile("parse_and_parsefile_test"), "123\n", ".parsefile method invokes TOP rule, match");
dies-ok({ Bar.parsefile("parse_and_parsefile_test") }, "dies if no TOP rule");
dies-ok({ Foo.parsefile("non_existent_file") }, "dies if file not found");
unlink("parse_and_parsefile_test");
grammar A::B {
token TOP { \d+ }
}
nok(A::B.parse("zzz42zzz"), ".parse works with namespaced grammars, no match");
is(~A::B.parse("42"), "42", ".parse works with namespaced grammars, match");
# TODO: Check for a good error message, not just the absence of a bad one.
throws-like '::No::Such::Grammar.parse()', Exception, '.parse on missing grammar dies';
# RT #71062
{
grammar Integer { rule TOP { x } };
lives-ok { Integer.parse('x') }, 'can .parse grammar named "Integer"';
}
# RT #76884
{
grammar grr {
token TOP {
<line>*
}
token line { .* \n }
}
my $match = grr.parse('foo bar asd');
is $match[0].perl, "Any", 'empty match is Any, not Null PMC access';
}
# RT #116597
{
grammar RT116597 {
token TOP() { <lit 'a'> };
token lit($s) { $s };
}
lives-ok { RT116597.parse('a') },
'can use <rule "param"> form of rule invocation in grammar';
}
# RT #111768
{
grammar RT111768 {
token e {
| 'a' <e> { make ';' ~ $<e>.ast }
| ';' { make 'a' }
}
}
is RT111768.parse("aaaa;", :rule<e>).ast, ';;;;a', "Recursive .ast calls work";
}
# vim: ft=perl6 expandtab sw=4