Pugs/Grammar/Expression.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 235 CC 29 SCAR 65.0
CC
29
Cov
0%
ast
870.0
BEGIN
2.0
BEGIN
2.0
CRAP 664.4
1 package Pugs::Grammar::Expression;
2  
3 use strict;
4 use warnings;
5  
6 #use Pugs::Compiler::Rule;
7 use Pugs::Grammar::Precedence;
8 use Pugs::Grammar::Term;
9 use Pugs::Grammar::Operator;
10 use Pugs::Grammar::StatementControl;
11 use base 'Pugs::Grammar::Base';
12 use Carp;
13  
14 use Data::Dumper;
15 $Data::Dumper::Indent = 1;
16 $Data::Dumper::Sortkeys = 1;
17  
18 # XXX - PCR is not calling this
19 *ws = &Pugs::Grammar::BaseCategory::ws;
20  
21 my $rx_end_with_blocks = qr/
22 ^ \s* (?:
23 [});]
24 | if \s
25 | unless \s
26 | for \s
27 | $
28 )
29 /xs;
30 my $rx_end_no_blocks = qr/
31 ^
32 (?:
33 \s+ {
34 | \s* (?:
35 [});]
36 | if \s
37 | unless \s
38 | for \s
39 | ->
40 | $
41 )
42 )
43 /xs;
44  
45 sub ast {
46 my $match = shift;
47 my $param = shift;
48  
49 my $no_blocks = exists $param->{args}{no_blocks} ? 1 : 0;
50 # warn "don't parse blocks: $no_blocks ";
51 my $rx_end = $no_blocks
52 ? $rx_end_no_blocks
53 : $rx_end_with_blocks;
54  
55 $match .= '';
56 if ( $match =~ /$rx_end/ ) {
57 # end of parse
58 return (undef, $match);
59 }
60 #print "Grammar::Expression::ast '$match' \n";
61 my $p;
62 my $last = length( $match );
63
64 my $lex = sub {
65 #print "Grammar::Expression::ast::lex '$match' \n";
66 if ( $match =~ /$rx_end/ ) {
67 #warn "end of expression at: [",substr($match,0,10),"]";
68 return ('', '');
69 }
70 #print "still here\n";
71 my $m;
72 my $whitespace_before = 0;
73  
74 my @expect = $p->YYExpect; # XXX is this expensive?
75
76 for ( 1 ) {
77 $m = Pugs::Grammar::BaseCategory->ws( $match );
78 # <ws> is nonstandard in that it returns a hashref instead of a Match
79 # print "match is ",Dumper($m),"\n";
80 if ( $m->{bool} ) {
81 $match = $m->{tail};
82 $whitespace_before = 1;
83 }
84
85 # print "tail $match \n";
86
87 # XXX @expect should use symbolic names; better use TABLE instead of 'literal'
88 #print " @{[ sort @expect ]} \n";
89 # if ( grep { $_ eq '++' || $_ eq '{' } @expect ) {
90
91 # XXX temporary hack - matching options in 'expected' order should fix this
92 if ( $match =~ /^</ ) { # && ! $whitespace_before ) {
93 # XXX - angle quotes are always tried even if it were expecting a simple '<'
94
95 # after whitespace means '<' (default)
96 # without whitespace means '<str>'
97 #print "checking angle quote ... [$whitespace_before]\n";
98 $m = Pugs::Grammar::Term->angle_quoted( substr($match, 1), { p => 1 } );
99 if ( $m ) {
100 #print "Match: ",Dumper $m->();
101 if ( grep { $_ eq 'NUM' } @expect ) {
102 # expects a term
103 $m = Pugs::Runtime::Match->new( {
104 bool => 1,
105 match => $m,
106 tail => $$m->{tail},
107 capture => { angle_quoted => $m->() },
108 } );
109 #print "Match: ",Dumper $m->();
110 last;
111 }
112 # expects an op
113 # x < 1 --- less than
114 # x<1 --- starts angle-quote
115 unless ( $whitespace_before ) {
116 $m = Pugs::Runtime::Match->new( {
117 bool => 1,
118 match => $m,
119 tail => $$m->{tail},
120 capture => { op => "ANGLE", angle_quoted => $m->() },
121 } );
122 #print "Match: ",Dumper $m->();
123 last;
124 }
125 }
126 }
127  
128 my $m1 = Pugs::Grammar::Operator->parse( $match, { p => 1 } );
129 my $m2 = Pugs::Grammar::Term->parse( $match, { p => 1 } );
130 #warn "m1 = " . Dumper($m1->()) . "m2 = " . Dumper($m2->());
131  
132 while(1) {
133 # term.meth() is high-precedence
134 if ( $m2 && $$m2->{tail} && $$m2->{tail} =~ /^\.[^.]/ ) {
135 my $meth = Pugs::Grammar::Term->parse( $$m2->{tail}, { p => 1 } );
136 $$meth->{capture} = {
137 op1 => 'method_call',
138 self => $m2->(),
139 method => $meth->(),
140 param => undef,
141 };
142 $m2 = $meth;
143 next;
144 }
145 # term() is high-precedence
146 if ( $m2 && $$m2->{tail} && $$m2->{tail} =~ /^\(/ ) {
147 my $paren = Pugs::Grammar::Term->parse( $$m2->{tail}, { p => 1 } );
148 if ( exists $m2->()->{dot_bareword} ) {
149 $$paren->{capture} = {
150 op1 => 'method_call',
151 self => { 'scalar' => '$_' },
152 method => $m2->(),
153 param => $paren->(),
154 };
155 }
156 elsif ( exists $m2->()->{op1}
157 && $m2->()->{op1} eq 'method_call'
158 && ! defined $m2->()->{param}
159 ) {
160 $$paren->{capture} = {
161 %{$m2->()},
162 param => $paren->(),
163 };
164 }
165 else {
166 $$paren->{capture} = {
167 op1 => 'call',
168 sub => $m2->(),
169 param => $paren->(),
170 };
171 }
172 $m2 = $paren;
173 next;
174 }
175 last;
176 }
177  
178 # longest token
179 if ( $m1 && $m2 ) {
180 if ( length($$m1->{tail}) > length($$m2->{tail}) ) {
181 $m = $m2
182 }
183 else {
184 $m = $m1
185 }
186 }
187 else {
188 $m = $m1 if $m1;
189 $m = $m2 if $m2;
190 }
191 last if $m;
192
193 local $Carp::CarpLevel = 2;
194 carp "unrecognized token '",substr($match,0,10),"'\n"
195 if $match;
196 } # /for
197
198 my $tail = $$m->{tail};
199  
200 # method call
201 # if ( defined $tail && $tail =~ /^\./ ) {
202 # # TODO - long dot
203 # my $meth = Pugs::Grammar::Term->parse( $tail, { p => 1 } );
204 # $meth->()->{self} = $m->();
205 # $m = $meth;
206 # $tail = $$m->{tail};
207 # #print "Method: ",Dumper $m->();
208  
209 # TODO -
210 # <fglock> like: ( name 1, 2 or 3 ) - is it parsed as name(1,2 or 3) or (name(1,2) or 3)
211 # <TimToady> it will be taken provisionally as a listop, with listop precedence
212 # <TimToady> so name(1,2) or 3
213 # <TimToady> but it will fail compilation if name is not supplied by CHECK time.
214 # <TimToady> it will also fail if name is declared as a unary or 0-ary func.
215  
216 # }
217  
218  
219 {
220 # trim tail
221 my $tmp = $tail;
222 $match = $tmp if defined $tmp; # match failure doesn't kill $match (PCR "bug")
223 }
224  
225 #print Dumper $m;
226 #print $match;
227 my $ast = $m->();
228  
229 $ast->{pos} = $last - length( $match );
230 my $t;
231 if ( exists $ast->{stmt} ) {
232 # unused!
233 if ( $ast->{stmt} eq '{' ) {
234 $t = [ 'BLOCK_START' => $ast ]
235 }
236 elsif ( $ast->{stmt} eq '}' ) {
237 $t = [ 'BLOCK_END' => $ast ]
238 }
239 else {
240 $t = [ $ast->{stmt} => $ast ]
241 }
242 }
243 elsif ( exists $ast->{op} ) {
244 if ( $ast->{op} eq 'my'
245 || $ast->{op} eq 'our'
246 || $ast->{op} eq 'has' ) {
247 $t = [ 'MY' => $ast ]
248 }
249 else {
250 $t = [ $ast->{op} => $ast ];
251 }
252 }
253 elsif ( exists $ast->{bareword} ) {
254 $t = [ 'BAREWORD' => $ast ]
255 }
256 elsif ( exists $ast->{dot_bareword} ) {
257 $t = [ 'DOT_BAREWORD' => $ast ]
258 }
259 else {
260 $t = [ 'NUM' => $ast ]
261 }
262 #warn "T ",Dumper($t), "MATCH $match\n";
263 $t=['',''] unless $ast; #$match; # defined($t);
264  
265 #print "expect NUM \n" if grep { $_ eq 'NUM' } @expect;
266 #print "expect '/' \n" if grep { $_ eq '/' } @expect;
267  
268 #print "token: $$t[0] ", Dumper( $$t[1] ); #, $match;
269 #print "expect: ", Dumper( @expect );
270  
271 return($$t[0],$$t[1]);
272 };
273  
274 $p = Pugs::Grammar::Operator->new(
275 yylex => $lex,
276 yyerror => sub {
277 local $Carp::CarpLevel = 2;
278 carp "parsing error in Expression: ..." . substr($match,0,30) . "... ";
279 },
280 );
281  
282 my $out=$p->YYParse(yydebug => 0);
283 #print Dumper $out;
284 return ( $out, $match );
285 }
286  
287 1;