/
Actions.pm6
67 lines (49 loc) · 1.55 KB
/
Actions.pm6
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
use v6;
=begin pod
=head1 Grammar::Common::Actions
L<Grammar::Common::Actions> gives you a library of common grammar action roles
to use in your own code, from simple numbers and strings to vaidation tools.
=head1 Synopsis
use Grammar::Common::Expression::Infix;
use Grammar::Common::Expression::Infix::Actions;
grammar PostScript does Grammar::Common::Expression::Infix {
token value { <[ - + ]>? <[ 0 .. 9 ]>+ }
rule TOP { <expression> }
}
class PostScript::Actions {
also does Grammar::Common::Expression::Infix::Actions;
}
my $x = PostScript.parse( '+ 1 3', :actions( PostScript::Actions.new ).ast;
say $x.perl;
=head1 Documentation
=head2 grammar roles
=item L<Expression::Infix::Actions>
Create an expression tree from '+ 2 / -6 3' and similar expressions.
Use C<< <expression> >> to match prefix expressions.
=end pod
role Grammar::Common::Expression::Infix::Actions {
method operation:sym<plus>( $/ ) {
make self.plus-operation( $/<lhs>.ast, $/<rhs>.ast );
}
method operation:sym<minus>( $/ ) {
make self.minus-operation( $/<lhs>.ast, $/<rhs>.ast );
}
method operation:sym<times>( $/ ) {
make self.times-operation( $/<lhs>.ast, $/<rhs>.ast );
}
method operation:sym<divide>( $/ ) {
make self.divide-operation( $/<lhs>.ast, $/<rhs>.ast );
}
method operation:sym<modulo>( $/ ) {
make self.modulo-operation( $/<lhs>.ast, $/<rhs>.ast );
}
method expression( $/ ) {
if $/<value> {
make $/<value>.ast;
}
elsif $/<operation> {
make $/<operation>.made;
}
}
}
# vim: ft=perl6