Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 113 lines (97 sloc) 2.933 kB
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
1 use v6;
2
3 use Test;
c2b6a86 [t/spec] tests for make() in action stubs
moritz authored
4
0cb99cd [t/spec] test for RT #71514
moritz authored
5 plan 12;
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
6
e10a53f [t/spec] smartlink to new S05 section
moritz authored
7 # L<S05/Grammars/"and optionally pass an action object">
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
8
9 grammar A::Test::Grammar {
10 rule TOP { <a> <b> }
11 token a { 'a' \w+ {*} }
12 token b { 'b' \w+ {*} }
13 }
14
15 class An::Action1 {
16 has $.in-a = 0;
17 has $.in-b = 0;
18 has $.calls = '';
19 method a($/) {
20 $!in-a++;
21 $!calls ~= 'a';
22 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored
23 method b($x) { #OK not used
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
24 $!in-b++;
25 $!calls ~= 'b';
26 }
27 }
28
29 ok A::Test::Grammar.parse('alpha beta'), 'basic sanity: .parse works';
30 my $action = An::Action1.new();
2f872c0 :action should be :actions
bkeeler authored
31 lives_ok { A::Test::Grammar.parse('alpha beta', :actions($action)) },
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
32 'parse with :action (and no make) lives';
33 is $action.in-a, 1, 'first action has been called';
34 is $action.in-b, 1, 'second action has been called';
35 is $action.calls, 'ab', '... and in the right order';
36
c2b6a86 [t/spec] tests for make() in action stubs
moritz authored
37 # L<S05/Bracket rationalization/"An explicit reduction using the make function">
38
39 {
40 grammar Grammar::More::Test {
41 rule TOP { <a> <b><c> {*} }
42 token a { \d+ {*} }
43 token b { \w+ {*} }
44 token c { '' } # no action stub
45 }
46 class Grammar::More::Test::Actions {
47 method TOP($/) {
48 make [ $<a>.ast, $<b>.ast ];
49 }
50 method a($/) {
51 make 3 + $/;
52 }
53 method b($/) {
54 # the given/when is pretty pointless, but rakudo
55 # used to segfault on it, so test it here
56 # http://rt.perl.org/rt3/Ticket/Display.html?id=64208
57 given 2 {
58 when * {
59 make $/ x 3;
60 }
61 }
62 }
63 method c($/) {
2f872c0 :action should be :actions
bkeeler authored
64 #die "don't come here";
65 # There's an implicity {*} at the end now
c2b6a86 [t/spec] tests for make() in action stubs
moritz authored
66 }
67 }
68
69 # there's no reason why we can't use the actions as class methods
2f872c0 :action should be :actions
bkeeler authored
70 my $match = Grammar::More::Test.parse('39 b', :actions(Grammar::More::Test::Actions));
71 ok $match, 'grammar matches';
72 isa_ok $match.ast, Array, '$/.ast is an Array';
73 ok $match.ast.[0] == 42, 'make 3 + $/ worked';
74 is $match.ast.[1], 'bbb', 'make $/ x 3 worked';
c2b6a86 [t/spec] tests for make() in action stubs
moritz authored
75 }
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
76
e169d45 [t/spec] RT #64104, constructing a Range in an action stub
moritz authored
77 # used to be a Rakudo regression, RT #64104
78 {
79 grammar Math {
80 token TOP { ^ <value> $ {*} }
81 token value { \d+ {*} }
82 }
83 class Actions {
84 method value($/) { make 1..$/};
85 method TOP($/) { make 1 + $/<value>};
86 }
2f872c0 :action should be :actions
bkeeler authored
87 my $match = Math.parse('234', :actions(Actions.new));
88 ok $match, 'can parse with action stubs that make() regexes';
89 is $match.ast, 235, 'got the right .ast';
e169d45 [t/spec] RT #64104, constructing a Range in an action stub
moritz authored
90
91 }
0cb99cd [t/spec] test for RT #71514
moritz authored
92
93 # another former rakudo regression, RT #71514
94 {
95 grammar ActionsTestGrammar {
96 token TOP {
97 ^ .+ $
98 }
99 }
100 class TestActions {
101 method TOP($/) {
102 "a\nb".subst(/\n+/, '', :g);
103 make 123;
104 }
105 }
106
107 is ActionsTestGrammar.parse("ab\ncd", :actions(TestActions.new)).ast, 123,
108 'Can call Str.subst in an action method without any trouble';
109 }
110
111
c1520ab [t/spec] tests for action stubs in grammars
moritz authored
112 # vim: ft=perl6
Something went wrong with that request. Please try again.