Perlito5/TreeGrammar.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 150 CC 12 SCAR 22.7
CC
12
Cov
0%
scan
20.0
Ref
12.0
Lookup
12.0
CRAP 9.7
1 package Perlito5::TreeGrammar;
2 use Perlito5::Dumper;
3 use strict;
4  
5 my %dispatch = (
6 Ref => sub { Ref(@_) }, # Ref => 'Perlito5::AST::Apply'
7 Lookup => sub { Lookup(@_) }, # Lookup => 'namespace'
8 Index => sub { Index(@_) }, # Index => '0'
9 Value => sub { Value(@_) }, # Value => '123'
10 And => sub { And(@_) },
11 Or => sub { Or(@_) },
12 Not => sub { Not(@_) },
13 Action => sub { Action(@_) },
14 Progn => sub { Progn(@_) }, # same as in Lisp (a list of statements to execute)
15 Star => sub { Star(@_) }, # same as in regex (matches anything)
16 );
17  
18 # render($rule, $node)
19 #
20 # This is the entry point for walking an AST. The first argument
21 # is the macro rule to process, which can be thought of as a
22 # regular expression for trees, which means the rule itself is
23 # composed of smaller sub-rules.
24 #
25 # $node is the AST itself, which render() tries to match against
26 # $rule.
27 #
28 # Each rule is just a list, with the first element being its name,
29 # which in turn is one of the rulenames defined in %dispatch above.
30 # All elements following the rulename in a rule are interpreted as
31 # arguments to the rule. For instance,
32 #
33 # [And => [Ref => 'Perlito5::AST::Block']
34 # [Lookup => 'stmts']
35 # [Index => 0]
36 # [Action => sub { say Perlito5::Dumper::Dumper $_[0]; }]]
37 #
38 # Will dump the first statement of a Block, if it is not empty.
39 #
40 sub render {
41 my ( $rule, $node ) = @_;
42 return $dispatch{ $rule->[0] }->( $rule, $node ); # note that each rule handler gets the whole rule (including its name) as first argument.
43 }
44  
45 sub scan {
46 my ( $rule, $node ) = @_;
47 render( $rule, $node ) if $rule;
48 if ( ref($node) eq 'ARRAY' ) {
49 scan( $rule, $_ ) for @$node;
50 }
51 elsif ( ref($node) ) {
52 scan( $rule, $_ ) for values %$node;
53 }
54 return;
55 }
56  
57 sub Action {
58 my ( $rule, $node ) = @_;
59 $rule->[1]->($node);
60 return 1;
61 }
62  
63 sub Not {
64 my ( $rule, $node ) = @_;
65 my $result;
66 render( $rule->[1], $node ) && return;
67 return 1;
68 }
69  
70 sub Star {
71 my ( $rule, $node ) = @_;
72 my $result;
73 while (1) {
74 render( $rule->[1], $node ) || return;
75 }
76 }
77  
78 sub Progn {
79 my ( $rule, $node ) = @_;
80 my $result;
81 for ( @$rule[ 1 .. $#$rule ] ) {
82 $result = render( $_, $node );
83 }
84 return $result;
85 }
86  
87 sub And {
88 my ( $rule, $node ) = @_;
89 my $result;
90 for ( @$rule[ 1 .. $#$rule ] ) {
91 $result = render( $_, $node ) or return;
92 }
93 return $result;
94 }
95  
96 sub Or {
97 my ( $rule, $node ) = @_;
98 my $result;
99 for ( @$rule[ 1 .. $#$rule ] ) {
100 $result = render( $_, $node ) and return $result;
101 }
102 return;
103 }
104  
105 sub Ref {
106 my ( $rule, $node ) = @_;
107 return if ref($node) ne $rule->[1];
108 return 1 if !$rule->[2];
109 return render( $rule->[2], $node );
110 }
111  
112 sub Lookup {
113 my ( $rule, $node ) = @_;
114 return
115 if !ref($node)
116 || ref($node) eq 'ARRAY'
117 || !exists( $node->{ $rule->[1] } );
118 # Typically, $rule->[2] contains the action to execute on a
119 # successful lookup (although it could be an arbitrary rule).
120 # If nothing is present, however, we just return 1 indicating
121 # a successful match, else we recurse into the following rule.
122 return 1 if !$rule->[2];
123 return render( $rule->[2], $node->{ $rule->[1] } );
124 }
125  
126 sub Index {
127 my ( $rule, $node ) = @_;
128 return
129 if !ref($node)
130 || ref($node) ne 'ARRAY'
131 || !exists( $node->[ $rule->[1] ] );
132 # See the corresponding section in Lookup()
133 return 1 if !$rule->[2];
134 return render( $rule->[2], $node->[ $rule->[1] ] );
135 }
136  
137 sub Value {
138 my ( $rule, $node ) = @_;
139 return if ref($node) || $node ne $rule->[1];
140 return 1 if !$rule->[2];
141 return render( $rule->[2], $node );
142 }
143  
144 1;
145