File Coverage

blib/lib/Spp/MatchRule.pm
Criterion Covered Total %
statement 194 387 50.1
branch 47 96 48.9
condition 0 3 0.0
subroutine 23 39 58.9
pod 0 33 0.0
total 264 558 47.3


line stmt bran cond sub pod time code
1             package Spp::MatchRule;
2              
3 2     2   35 use 5.012;
  2         7  
4 2     2   23 no warnings "experimental";
  2         4  
  2         51  
5              
6 2     2   8 use Exporter;
  2         2  
  2         103  
7             our @ISA = qw(Exporter);
8             our @EXPORT = qw(match_rule);
9              
10 2     2   10 use Spp::Builtin;
  2         13  
  2         235  
11 2     2   417 use Spp::Core;
  2         2  
  2         140  
12 2     2   12 use Spp::Cursor;
  2         3  
  2         5118  
13              
14             sub match_rule {
15 1369     1369 0 1889 my ($rule, $cursor) = @_;
16 1369         1587 my ($name, $atom) = @{$rule};
  1369         2336  
17 1369         1871 given ($name) {
18 1369         2036 when ('Rules') { match_group($atom, $cursor) }
  229         377  
19 1140         1433 when ('Group') { match_group($atom, $cursor) }
  0         0  
20 1140         1658 when ('Branch') { match_branch($atom, $cursor) }
  85         160  
21 1055         1315 when ('Rept') { match_rept($atom, $cursor) }
  99         176  
22 956         1191 when ('Look') { match_look($atom, $cursor) }
  0         0  
23 956         1128 when ('Cclass') { match_cclass($atom, $cursor) }
  56         105  
24 900         1054 when ('Chclass') { match_chclass($atom, $cursor) }
  58         120  
25 842         1047 when ('Nchclass') { match_nchclass($atom, $cursor)}
  0         0  
26 842         1010 when ('Str') { match_str($atom, $cursor) }
  76         130  
27 766         911 when ('Char') { match_char($atom, $cursor) }
  311         470  
28 455         542 when ('Assert') { match_assert($atom, $cursor) }
  6         18  
29 449         586 when ('Not') { match_not($atom, $cursor) }
  0         0  
30 449         490 when ('Till') { match_till($atom, $cursor) }
  0         0  
31 449         520 when ('Rtoken') { match_rtoken($atom, $cursor) }
  50         103  
32 399         482 when ('Ctoken') { match_ctoken($atom, $cursor) }
  16         38  
33 383         456 when ('Ntoken') { match_ntoken($atom, $cursor) }
  381         609  
34 2         3 when ('Any') { match_any($atom, $cursor) }
  2         5  
35 0         0 when ('Expr') { match_expr($atom, $cursor) }
  0         0  
36 0         0 when ('Sym') { match_sym($atom, $cursor) }
  0         0  
37 0         0 default {
38 0         0 error("unknown rule type |$name|!")
39             }
40             }
41             }
42              
43             sub match_any {
44 2     2 0 5 my ($any, $cursor) = @_;
45 2         5 my $char = $cursor->get_char;
46 2 50       6 if ($char eq chr(0)) { return False }
  0         0  
47 2         5 $cursor->to_next;
48 2         4 return $char;
49             }
50              
51             sub match_assert {
52 6     6 0 16 my ($assert, $cursor) = @_;
53 6         11 given ($assert) {
54 6         11 when ('^') {
55 0 0       0 return True if $cursor->off == 0;
56 0         0 return False;
57             }
58 6         13 when ('$') {
59 6 50       17 return True if $cursor->get_char eq chr(0);
60 0         0 return False;
61             }
62 0         0 when ('^^') {
63 0 0       0 return True if $cursor->pre_char eq "\n";
64 0 0       0 return True if $cursor->off == 0;
65 0         0 return False;
66             }
67 0         0 when ('$$') {
68 0 0       0 return True if $cursor->get_char eq "\n";
69 0 0       0 return True if $cursor->get_char eq chr(0);
70 0         0 return False;
71             }
72 0         0 default { error("error assert char: <$assert>!") }
  0         0  
73             }
74             }
75              
76             sub match_group {
77 229     229 0 317 my ($rules, $cursor) = @_;
78 229         304 my $gather = True;
79 229         279 for my $rule (@{$rules}) {
  229         383  
80 250         356 my $match = match_rule($rule, $cursor);
81 250 100       527 return False if is_false($match);
82 32         73 $gather = gather_match($gather, $match);
83             }
84 11         63 return $gather;
85             }
86              
87             sub match_branch {
88 85     85 0 129 my ($branch, $cursor) = @_;
89 85         195 my $cache = $cursor->cache;
90 85         129 my $max_match = False;
91 85         111 my $max_cache = $cache;
92 85         101 my $max_off = -1;
93 85         106 for my $rule (@{$branch}) {
  85         156  
94 522         803 my $match = match_rule($rule, $cursor);
95 522 100       901 if (!is_false($match)) {
96 36 100       80 if ($cursor->off > $max_off) {
97 34         63 $max_off = $cursor->off;
98 34         122 $max_cache = $cursor->cache;
99 34         57 $max_match = $match;
100             }
101             }
102 522         1011 $cursor->reset_cache($cache);
103             }
104 85         210 $cursor->reset_cache($max_cache);
105 85         261 return $max_match;
106             }
107              
108             sub match_ntoken {
109 381     381 0 593 my ($name, $cursor) = @_;
110 381         623 my $rule = $cursor->{'ns'}{$name};
111 381         741 my $from = $cursor->off;
112 381         695 my $cache = $cursor->cache;
113 381         615 my $match = match_rule($rule, $cursor);
114 381 100       719 return $match if is_bool($match);
115 16         41 my $len = $cursor->off - $from;
116 16 50       41 if ($len > 0) {
117 16         39 my $str = substr($cursor->str, $from, $len);
118 16         41 my $cname = '$' . $name;
119 16         55 $cursor->{'ns'}{$cname} = ['Str', $str];
120             }
121 16         27 push @{$cache}, $len;
  16         38  
122 16         45 return name_match($name, $match, $cache);
123             }
124              
125             sub match_ctoken {
126 16     16 0 29 my ($name, $cursor) = @_;
127 16         36 my $rule = $cursor->{'ns'}{$name};
128 16         36 my $from = $cursor->off;
129 16         41 my $match = match_rule($rule, $cursor);
130 16 100       35 return $match if is_bool($match);
131 7         17 my $len = $cursor->off - $from;
132 7 50       18 if ($len > 0) {
133 7         13 my $str = substr($cursor->str, $from, $len);
134 7         16 my $cname = '$' . $name;
135 7         21 $cursor->{'ns'}{$cname} = ['Str', $str];
136             }
137 7         16 return $match;
138             }
139              
140             sub match_rtoken {
141 50     50 0 79 my ($name, $cursor) = @_;
142 50         101 my $rule = $cursor->{'ns'}{$name};
143 50         93 my $match = match_rule($rule, $cursor);
144 50 100       107 return False if is_false($match);
145 6         16 return True;
146             }
147              
148             sub match_not {
149 0     0 0 0 my ($rule, $cursor) = @_;
150 0         0 my $cache = $cursor->cache;
151 0         0 my $match = match_rule($rule, $cursor);
152 0 0       0 if (is_false($match)) {
153 0         0 $cursor->reset_cache($cache);
154 0         0 return True;
155             }
156 0         0 return False;
157             }
158              
159             sub match_till {
160 0     0 0 0 my ($rule, $cursor) = @_;
161 0         0 my @buf = ();
162 0         0 while ($cursor->off < $cursor->len) {
163 0         0 my $char = $cursor->get_char;
164 0         0 my $cache = $cursor->cache;
165 0         0 my $match = match_rule($rule, $cursor);
166 0 0       0 if (!is_false($match)) {
167 0         0 my $gather_str = join '', @buf;
168 0         0 return gather_match($gather_str, $match);
169             }
170 0         0 push @buf, $char;
171 0         0 $cursor->reset_cache($cache);
172 0         0 $cursor->to_next;
173             }
174 0         0 return False;
175             }
176              
177             sub match_rept {
178 99     99 0 148 my ($rule, $cursor) = @_;
179 99         130 my $gather = True;
180 99         120 my $time = 0;
181 99         146 my ($rept, $atom) = @{$rule};
  99         202  
182 99         236 my ($min, $max) = get_rept_time($rept);
183 99         238 while ($time != $max) {
184 147         329 my $cache = $cursor->cache;
185 147         340 my $match = match_rule($atom, $cursor);
186 147 100       324 if (is_false($match)) {
187 99 100       294 return False if $time < $min;
188 27         75 $cursor->reset_cache($cache);
189 27         94 return $gather;
190             }
191 48         80 $time++;
192 48         91 $gather = gather_match($gather, $match);
193             }
194 0         0 return $gather;
195             }
196              
197             sub match_look {
198 0     0 0 0 my ($rule, $cursor) = @_;
199 0         0 my ($rept, $atom, $look) = @{$rule};
  0         0  
200 0         0 my ($min, $max) = get_rept_time($rept);
201 0         0 my ($gather, $time) = (True, 0);
202 0         0 while ($time != $max) {
203 0         0 my $cache = $cursor->cache;
204 0         0 my $match = match_rule($atom, $cursor);
205 0 0       0 if (is_false($match)) {
206 0 0       0 return False if $time > $min;
207 0         0 $cursor->reset_cache($cache);
208 0         0 $match = match_rule($look, $cursor);
209 0 0       0 return False if is_false($match);
210 0         0 return gather_match($gather, $match);
211             }
212 0         0 $time++;
213 0         0 $gather = gather_match($gather, $match);
214 0 0       0 if ($time >= $min) {
215 0         0 $cache = $cursor->cache;
216 0         0 $match = match_rule($look, $cursor);
217 0 0       0 if (!is_false($match)) {
218 0         0 return gather_match($gather, $match)
219             }
220 0         0 $cursor->reset_cache($cache);
221             }
222             }
223 0         0 return False;
224             }
225              
226             sub match_str {
227 76     76 0 120 my ($str, $cursor) = @_;
228 76         195 for my $char (split('', $str)) {
229 78 100       185 if ($char ne $cursor->get_char) { return False }
  76         212  
230 2         10 $cursor->to_next;
231             }
232 0         0 return $str;
233             }
234              
235             sub match_char {
236 311     311 0 478 my ($char, $cursor) = @_;
237 311 100       615 return False if $char ne $cursor->get_char;
238 10         28 $cursor->to_next;
239 10         19 return $char;
240             }
241              
242             sub match_chclass {
243 58     58 0 90 my ($atoms, $cursor) = @_;
244 58         130 my $char = $cursor->get_char;
245 58         90 for my $atom (@{$atoms}) {
  58         119  
246 163 100       282 if (match_catom($atom, $char)) {
247 20         72 $cursor->to_next;
248 20         63 return $char;
249             }
250             }
251 38         93 return False;
252             }
253              
254             sub match_nchclass {
255 0     0 0 0 my ($atoms, $cursor) = @_;
256 0         0 my $char = $cursor->get_char;
257 0 0       0 return False if $char eq chr(0);
258 0         0 for my $atom (@{$atoms}) {
  0         0  
259 0 0       0 return False if match_catom($atom, $char);
260             }
261 0         0 $cursor->to_next;
262 0         0 return $char;
263             }
264              
265             sub match_catom {
266 163     163 0 254 my ($atom, $char) = @_;
267 163         195 my ($name, $value) = @{$atom};
  163         251  
268 163         213 given ($name) {
269 163         233 when ("Range") { match_range($value, $char) }
  0         0  
270 163         208 when ("Cclass") { is_match_cclass($value, $char) }
  40         69  
271 123         158 when ("Char") { return $value eq $char }
  123         339  
272             }
273             }
274              
275             sub match_cclass {
276 56     56 0 90 my ($cclass, $cursor) = @_;
277 56         129 my $char = $cursor->get_char;
278 56 100       140 return False if $char eq chr(0);
279 47 100       87 if (is_match_cclass($cclass, $char)) {
280 9         31 $cursor->to_next;
281 9         30 return $char;
282             }
283 38         96 return False;
284             }
285              
286             sub is_match_cclass {
287 87     87 0 150 my ($cchar, $char) = @_;
288 87         112 given ($cchar) {
289 87         141 when ('a') { return is_alpha($char) }
  40         136  
290 47         70 when ('A') { return !is_alpha($char) }
  0         0  
291 47         64 when ('d') { return is_digit($char) }
  13         37  
292 34         52 when ('D') { return !is_digit($char) }
  0         0  
293 34         48 when ('h') { return is_hspace($char) }
  0         0  
294 34         46 when ('H') { return !is_hspace($char) }
  0         0  
295 34         67 when ('l') { return is_lower($char) }
  0         0  
296 34         47 when ('L') { return !is_lower($char) }
  0         0  
297 34         53 when ('s') { return is_space($char) }
  34         86  
298 0         0 when ('S') { return !is_space($char) }
  0         0  
299 0         0 when ('u') { return is_upper($char) }
  0         0  
300 0         0 when ('U') { return !is_upper($char) }
  0         0  
301 0         0 when ('v') { return is_vspace($char) }
  0         0  
302 0         0 when ('V') { return !is_vspace($char) }
  0         0  
303 0         0 when ('w') { return is_words($char) }
  0         0  
304 0         0 when ('W') { return !is_words($char) }
  0         0  
305 0         0 when ('x') { return is_xdigit($char) }
  0         0  
306 0         0 when ('X') { return !is_xdigit($char) }
  0         0  
307             }
308             }
309              
310             sub match_range {
311 0     0 0 0 my ($range, $char) = @_;
312 0         0 my ($from, $to) = @{$range};
  0         0  
313 0   0     0 return ($from le $char && $char le $to);
314             }
315              
316             sub match_expr {
317 0     0 0 0 my ($expr, $cursor) = @_;
318 0         0 my $atom = eval_expr($expr, $cursor);
319 0         0 return match_atom($atom, $cursor);
320             }
321              
322             sub match_sym {
323 0     0 0 0 my ($name, $cursor) = @_;
324 0         0 my $value = eval_sym($name, $cursor);
325 0         0 return match_atom($value, $cursor);
326             }
327              
328             sub match_atom {
329 0     0 0 0 my ($atom, $cursor) = @_;
330 0 0       0 return $atom if is_bool($atom);
331 0         0 my ($name, $value) = @{$atom};
  0         0  
332 0 0       0 return False if len($value) == 0;
333 0         0 given ($name) {
334 0         0 when ('Array') {
335 0         0 return match_branch($value, $cursor)
336             }
337 0         0 when ('Str') {
338 0         0 return match_str($value, $cursor)
339             }
340             }
341             }
342              
343             sub name_match {
344 16     16 0 37 my ($name, $match, $pos) = @_;
345 16 50       37 if (is_true($match)) { return $match }
  0         0  
346 16 100       41 if (is_atom($match)) { return [$name, [$match], $pos] }
  1         4  
347 15         58 return [$name, $match, $pos];
348             }
349              
350             sub gather_match {
351 80     80 0 149 my ($gather, $match) = @_;
352 80 100       152 return $gather if is_true($match);
353 67 100       137 return $match if is_true($gather);
354 29 100       66 if (is_str($match)) {
355 22 100       47 return $gather . $match if is_str($gather);
356 6         18 return $gather;
357             }
358 7 50       23 if (is_str($gather)) { return $match }
  0         0  
359 7 100       37 if (is_atom($gather)) {
360 6 100       13 return [$gather, $match] if is_atom($match);
361 3         10 return [$gather, @{$match}];
  3         15  
362             }
363 1 50       5 return [@{$gather}, $match] if is_atom($match);
  1         5  
364 0           return [@{$gather}, @{$match}];
  0            
  0            
365             }
366              
367             sub eval_atom {
368 0     0 0   my ($atom, $cursor) = @_;
369 0           my ($name, $value) = @{$atom};
  0            
370 0           given ($name) {
371 0           when ('Str') { return $atom }
  0            
372 0           when ('Sym') { return eval_sym($value, $cursor) }
  0            
373 0           when ('Expr') { return eval_expr($value, $cursor) }
  0            
374 0           when ('Array') { return eval_array($value, $cursor) }
  0            
375             }
376             }
377              
378             sub eval_sym {
379 0     0 0   my ($name, $cursor) = @_;
380 0 0         if (exists $cursor->{'ns'}{$name}) {
381 0           return $cursor->{'ns'}{$name};
382             }
383 0           $cursor->error("variable not define: <$name>.");
384             }
385              
386             sub eval_expr {
387 0     0 0   my ($expr, $cursor) = @_;
388 0           my $name = shift @{$expr};
  0            
389 0           given ($name) {
390 0           when ('push') { eval_push($expr, $cursor) }
  0            
391 0           when ('my') { eval_my($expr, $cursor) }
  0            
392 0           when ('say') { eval_say($expr, $cursor) }
  0            
393 0           default {
394 0           $cursor->error("not implement action: <$name>.");
395             }
396             }
397             }
398              
399             sub eval_array {
400 0     0 0   my ($array, $cursor) = @_;
401 0 0         if (len($array) == 0) {
402 0           return ['Array', $array]
403             }
404 0           my $atoms = eval_atoms($array, $cursor);
405 0           return ['Array', $atoms];
406             }
407              
408             sub eval_push {
409 0     0 0   my ($atoms, $cursor) = @_;
410 0           my $sym = $atoms->[0];
411 0 0         if (is_sym($sym)) {
412 0           my $eval_atoms = eval_atoms($atoms, $cursor);
413 0           my ($array, $element) = @{$eval_atoms};
  0            
414 0           push @{ $array->[1] }, $element;
  0            
415 0           my $name = $sym->[1];
416 0           $cursor->{'ns'}{$name} = $array;
417 0           return True;
418             }
419 0           $cursor->error('push only accept array symbol!');
420             }
421              
422             sub eval_my {
423 0     0 0   my ($atoms, $cursor) = @_;
424 0           my $sym = $atoms->[0];
425 0           my $value = eval_atom($atoms->[1], $cursor);
426 0 0         if (is_sym($sym)) {
427 0           my $name = $sym->[1];
428 0           $cursor->{'ns'}{$name} = $value;
429 0           return True;
430             }
431 0           $cursor->error('only assign symbol!');
432             }
433              
434             sub eval_say {
435 0     0 0   my ($atoms, $cursor) = @_;
436 0           my $eval_atoms = eval_atoms($atoms, $cursor);
437 0           my $str = $eval_atoms->[0];
438 0 0         if (is_atom_str($str)) {
439 0           say $str->[1];
440 0           return True;
441             }
442 0           my $type = $str->[0];
443 0           $cursor->error("say only accept Str: <$type>");
444             }
445              
446             sub eval_atoms {
447 0     0 0   my ($atoms, $cursor) = @_;
448 0           return [ map { eval_atom($_, $cursor) } @{$atoms} ];
  0            
  0            
449             }
450              
451             1;