Pegex/Bootstrap.pm

filter: Statementstmt 94.7% 236 / 249 Branchbran 83.3% 80 / 96 Conditioncond 80.7% 21 / 26 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 100% 30 / 30 Podpod 4.0% 1 / 25 totaltotal 85.4% 377 / 441 CC 61 SCAR 20.3
CC
61
Cov
91%
match_times
16.1
group_ast
13.0
lex
12.1
CRAP 7.6
« Pegex/Input.pm ↑ Summary Pegex/Pegex/AST.pm »
1 package Pegex::Bootstrap;
2 7 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 extends 'Pegex::Compiler';
4  
5 7 ▶use Pegex::Grammar::Atoms;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Pegex::Pegex::AST;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp qw(carp confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #------------------------------------------------------------------------------
11 # The grammar. A DSL data structure. Things with '=' are tokens.
12 #------------------------------------------------------------------------------
13 7 has pointer => 0;
14 7 has groups => [];
15 7 has tokens => [];
16 7 has ast => {};
17 7 has stack => [];
18 7 has tree => {};
19 7 has grammar => {
20 'grammar' => [
21 '=pegex-start',
22 'meta-section',
23 'rule-section',
24 '=pegex-end',
25 ],
26 'meta-section' => 'meta-directive*',
27 'meta-directive' => [
28 '=directive-start',
29 '=directive-value',
30 '=directive-end',
31 ],
32 'rule-section' => 'rule-definition*',
33 'rule-definition' => [
34 '=rule-start',
35 '=rule-sep',
36 'rule-group',
37 '=rule-end',
38 ],
39 'rule-group' => 'any-group',
40 'any-group' => [
41 '=list-alt?',
42 'all-group',
43 [
44 '=list-alt',
45 'all-group',
46 '*',
47 ],
48 ],
49 'all-group' => 'rule-part+',
50 'rule-part' => [
51 'rule-item',
52 [
53 '=list-sep',
54 'rule-item',
55 '?',
56 ],
57 ],
58 'rule-item' => [
59 '|',
60 '=rule-reference',
61 '=quoted-regex',
62 'regular-expression',
63 'bracketed-group',
64 'whitespace-token',
65 '=error-message',
66 ],
67 'regular-expression' => [
68 '=regex-start',
69 '=!regex-end*',
70 '=regex-end',
71 ],
72 'bracketed-group' => [
73 '=group-start',
74 'rule-group',
75 '=group-end',
76 ],
77 'whitespace-token' => [
78 '|',
79 '=whitespace-maybe',
80 '=whitespace-must',
81 ],
82 };
83  
84 #------------------------------------------------------------------------------
85 # Parser logic:
86 #------------------------------------------------------------------------------
87 112 ▶sub parse {
Subroutine parse: called CC=3 SCAR=14.2
Pod documented
88 112 my ($self, $grammar_text) = @_;
89  
90 112 $self->lex($grammar_text);
91 # YYY $self->{tokens};
92 112 $self->{pointer} = 0;
93 112 $self->{farthest} = 0;
94 112 $self->{tree} = {};
95  
96 112 ▶ $self->match_ref('grammar') || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->match_ref("grammar") or do { my $far = $self->{"farthest"}; my $tokens = $self->{"tokens"}; $far -= 4 if $far >= 4; WWW(splice(@$tokens, $far, 9)); die "Bootstrap parse failed" } 112 0 112
97 0 my $far = $self->{farthest};
98 0 my $tokens = $self->{tokens};
99 0 ▶ $far -= 4 if $far >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $far >= 4 0 0 0
100 0 WWW splice @$tokens, $far, 9;
101 0 die "Bootstrap parse failed";
102 };
103 # XXX $self->{tree};
104  
105 112 return $self;
106 }
107  
108 9961 ▶sub match_next {
Subroutine match_next: called CC=6 SCAR=17.9
Pod undocumented
109 9961 my ($self, $next) = @_;
110 9961 my $method;
111 9961 ▶ if (ref $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next) { } 3203 6758 9961
112 3203 $next = [@$next];
113 3203 ▶ if ($next->[0] eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->[0] eq '|') { } 831 2372 3203
114 831 shift @$next;
115 831 $method = 'match_any';
116 }
117 else {
118 2372 $method = 'match_all';
119 }
120 3203 ▶ if ($next->[-1] =~ /^[\?\*\+]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->[-1] =~ /^[\?\*\+]$/) { } 519 2684 3203
121 519 my $quant = pop @$next;
122 519 return $self->match_times($quant, $method => $next);
123 }
124 else {
125 2684 return $self->$method($next);
126 }
127 }
128 else {
129 6758 ▶ $method = ($next =~ s/^=//) ? 'match_token' : 'match_ref';
Branch: 1 branch
BranchTrueFalseTotal
$next =~ s/^=// ? : 4031 2727 6758
130 6758 ▶ if ($next =~ s/([\?\*\+])$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next =~ s/([\?\*\+])$//) { } 681 6077 6758
131 681 return $self->match_times($1, $method => $next);
132 }
133 else {
134 6077 return $self->$method($next);
135 }
136 }
137 }
138  
139 1200 ▶sub match_times {
Subroutine match_times: called CC=12 SCAR=27.8
Pod undocumented
140 1200 my ($self, $quantity, $method, @args) = @_;
141 1200 ▶ my ($min, $max) =
Branch: 4 branches
BranchTrueFalseTotal
$quantity eq '' ? : 0 1200 1200
$quantity eq '?' ? : 519 681 1200
$quantity eq '*' ? : 453 228 681
$quantity eq '+' ? : 228 0 228
142 $quantity eq '' ? (1, 1) :
143 $quantity eq '?' ? (0, 1) :
144 $quantity eq '*' ? (0, 0) :
145 $quantity eq '+' ? (1, 0) : die "Bad quantity '$quantity'";
146 1200 ▶ my $stop = $max || 9999;
Condition100%2/2
$max || 9999
l!l
519681
MC/DC100%1/1
$max || 9999
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
147 1200 my $count = 0;
148 1200 my $pointer = $self->{pointer};
149 1200 ▶ while ($stop-- and $self->$method(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop-- and $self->$method(@args)) 638 1200 1838
Condition100%3/3
$stop-- and $self->$method(@args)
!ll&&!rl&&r
301609199
MC/DC100%2/2
$stop-- and $self->$method(@args)
$stop-- $self->$method(@args)
Truth table100%3/3
A and B
A= $stop--B= $self->$method(@args)
ABresult
0X0
100
111
150 638 $count++;
151 }
152 1200 ▶ return 1 if $count >= $min and (not $max or $count <= $max);
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $min and !$max || $count <= $max 1200 0 1200
Condition50%3/6
$count >= $min and !$max || $count <= $max
!ll&&!rl&&r
001200
not $max or $count <= $max
l!l&&r!l&&!r
6815190
MC/DC0%0/3
$count >= $min and !$max || $count <= $max
$count >= $min not $max $count <= $max
Truth table50%2/4
A and not(B) or C
A= $count >= $minB= not $maxC= $count <= $max
ABCresult
0XX0
1000
11X1
1011
153 0 $self->{pointer} = $pointer;
154 0 ▶ $self->{farthest} = $pointer if $pointer > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if $pointer > $self->{"farthest"} 0 0 0
155 0 return;
156 }
157  
158 831 ▶sub match_any {
Subroutine match_any: called CC=4 SCAR=13.9
Pod undocumented
159 831 my ($self, $any) = @_;
160 831 my $pointer = $self->{pointer};
161 831 for (@$any) {
162 2501 ▶ if ($self->match_next($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match_next($_)) 369 2132 2501
163 369 return 1;
164 }
165 }
166 462 $self->{pointer} = $pointer;
167 462 ▶ $self->{farthest} = $pointer if $pointer > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if $pointer > $self->{"farthest"} 0 462 462
168 462 return;
169 }
170  
171 2415 ▶sub match_all {
Subroutine match_all: called CC=4 SCAR=13.9
Pod undocumented
172 2415 my ($self, $all) = @_;
173 2415 my $pointer = $self->{pointer};
174 2415 for (@$all) {
175 4139 ▶ if (not $self->match_next($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->match_next($_)) 1477 2662 4139
176 1477 $self->{pointer} = $pointer;
177 1477 ▶ $self->{farthest} = $pointer if $pointer > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if $pointer > $self->{"farthest"} 593 884 1477
178 1477 return;
179 }
180 }
181 938 return 1;
182 }
183  
184 3321 ▶sub match_ref {
Subroutine match_ref: called CC=2 SCAR=7.1
Pod undocumented
185 3321 my ($self, $ref) = @_;
186 3321 ▶ my $rule = $self->{grammar}->{$ref}
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $self->{"grammar"}{$ref} or Carp::confess("Not a rule reference: '${ref}'") 3321 0 3321
187 or Carp::confess "Not a rule reference: '$ref'";
188 3321 $self->match_next($rule);
189 }
190  
191 4114 ▶sub match_token {
Subroutine match_token: called CC=6 SCAR=17.9
Pod undocumented
192 4114 my ($self, $token_want) = @_;
193 4114 ▶ my $not = ($token_want =~ s/^\!//) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$token_want =~ s/^\!// ? : 127 3987 4114
194 4114 ▶ return if $self->{pointer} >= @{$self->{tokens}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pointer"} >= @{$self->{"tokens"};} 0 4114 4114
195 4114 my $token = $self->{tokens}[$self->{pointer}];
196 4114 my $token_got = $token->[0];
197 4114 ▶ if (($token_want eq $token_got) xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_want eq $token_got xor $not) 1274 2840 4114
Condition100%4/4
$token_want eq $token_got xor $not
l&&rl&&!r!l&&r!l&&!r
441191832796
MC/DC100%2/2
$token_want eq $token_got xor $not
$token_want eq $token_got $not
Truth table100%4/4
A xor B
A= $token_want eq $token_gotB= $not
ABresult
110
101
011
000
198 1274 $token_got =~ s/-/_/g;
199 1274 my $method = "got_$token_got";
200 1274 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) 902 372 1274
201 # print "$method\n";
202 902 $self->$method($token);
203 }
204 1274 $self->{pointer}++;
205 1274 return 1;
206 }
207 2840 return;
208 }
209  
210 #------------------------------------------------------------------------------
211 # Receiver/ast-generator methods:
212 #------------------------------------------------------------------------------
213 9 ▶sub got_directive_start {
Subroutine got_directive_start: called CC=1 SCAR=0.0
Pod undocumented
214 9 my ($self, $token) = @_;
215 9 $self->{directive_name} = $token->[1];
216 }
217  
218 9 ▶sub got_directive_value {
Subroutine got_directive_value: called CC=3 SCAR=11.0
Pod undocumented
219 9 my ($self, $token) = @_;
220 9 my $value = $token->[1];
221 9 $value =~ s/\s+$//;
222 9 my $name = $self->{directive_name};
223 9 ▶ if (my $old_value = $self->{tree}{"+$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old_value = $self->{'tree'}{"+$name"}) { } 1 8 9
224 1 ▶ if (not ref($old_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $old_value) 1 0 1
225 1 $old_value = $self->{tree}{"+$name"} = [$old_value];
226 }
227 1 push @$old_value, $value;
228 }
229 else {
230 8 $self->{tree}{"+$name"} = $value;
231 }
232 }
233  
234 139 ▶sub got_rule_start {
Subroutine got_rule_start: called CC=2 SCAR=7.0
Pod undocumented
235 139 my ($self, $token) = @_;
236 139 $self->{stack} = [];
237 139 my $rule_name = $token->[1];
238 139 $rule_name =~ s/-/_/g;
239 139 $self->{rule_name} = $rule_name;
240 139 ▶ $self->{tree}{'+toprule'} ||= $rule_name;
Condition66%2/3
$self->{"tree"}{"+toprule"} ||= $rule_name
l!l&&r!l&&!r
301090
MC/DC0%0/2
$self->{"tree"}{"+toprule"} ||= $rule_name
$self->{"tree"}{"+toprule"} $rule_name
Truth table66%2/3
A ||= B
A= $self->{"tree"}{"+toprule"}B= $rule_name
ABresult
1X1
011
000
241 139 $self->{groups} = [[0, ':']];
242 }
243  
244 139 ▶sub got_rule_end {
Subroutine got_rule_end: called CC=1 SCAR=0.0
Pod undocumented
245 139 my ($self) = @_;
246 139 $self->{tree}{$self->{rule_name}} = $self->group_ast;
247 }
248  
249 46 ▶sub got_group_start {
Subroutine got_group_start: called CC=1 SCAR=0.0
Pod undocumented
250 46 my ($self, $token) = @_;
251 46 push @{$self->{groups}}, [scalar(@{$self->{stack}}), $token->[1]];
252 }
253  
254 46 ▶sub got_group_end {
Subroutine got_group_end: called CC=1 SCAR=0.0
Pod undocumented
255 46 my ($self, $token) = @_;
256 46 my $rule = $self->group_ast;
257 46 Pegex::Pegex::AST::set_quantity($rule, $token->[1]);
258 46 push @{$self->{stack}}, $rule;
259 }
260  
261 47 ▶sub got_list_alt {
Subroutine got_list_alt: called CC=1 SCAR=0.0
Pod undocumented
262 47 my ($self) = @_;
263 47 push @{$self->{stack}}, '|';
264 }
265  
266 26 ▶sub got_list_sep {
Subroutine got_list_sep: called CC=1 SCAR=0.0
Pod undocumented
267 26 my ($self, $token) = @_;
268 26 push @{$self->{stack}}, $token->[1];
269 }
270  
271 303 ▶sub got_rule_reference {
Subroutine got_rule_reference: called CC=1 SCAR=0.0
Pod undocumented
272 303 my ($self, $token) = @_;
273 303 my $name = $token->[2];
274 303 $name =~ s/-/_/g;
275 303 $name =~ s/^<(.*)>$/$1/;
276 303 my $rule = { '.ref' => $name };
277 303 Pegex::Pegex::AST::set_modifier($rule, $token->[1]);
278 303 Pegex::Pegex::AST::set_quantity($rule, $token->[3]);
279 303 push @{$self->{stack}}, $rule;
280 }
281  
282 6 ▶sub got_error_message {
Subroutine got_error_message: called CC=1 SCAR=0.0
Pod undocumented
283 6 my ($self, $token) = @_;
284 6 push @{$self->{stack}}, { '.err' => $token->[1] };
285 }
286  
287 18 ▶sub got_whitespace_maybe {
Subroutine got_whitespace_maybe: called CC=1 SCAR=0.0
Pod undocumented
288 18 my ($self) = @_;
289 18 $self->got_rule_reference(['whitespace-maybe', undef, '_', undef]);
290 }
291  
292 12 ▶sub got_whitespace_must {
Subroutine got_whitespace_must: called CC=1 SCAR=0.0
Pod undocumented
293 12 my ($self) = @_;
294 12 $self->got_rule_reference(['whitespace-maybe', undef, '__', undef]);
295 }
296  
297 7 ▶sub got_quoted_regex {
Subroutine got_quoted_regex: called CC=1 SCAR=0.0
Pod undocumented
298 7 my ($self, $token) = @_;
299 7 my $regex = $token->[1];
300 7 $regex =~ s/([^\w\`\%\:\<\/\,\=\;])/\\$1/g;
301 7 push @{$self->{stack}}, { '.rgx' => $regex };
302 }
303  
304 44 ▶sub got_regex_start {
Subroutine got_regex_start: called CC=1 SCAR=0.0
Pod undocumented
305 44 my ($self, $token) = @_;
306 44 push @{$self->{groups}}, [scalar(@{$self->{stack}}), '/', $token->[1]];
307 }
308  
309 44 ▶sub got_regex_end {
Subroutine got_regex_end: called CC=5 SCAR=16.1
Pod undocumented
310 44 my ($self) = @_;
311 44 my ($x, $y, $gmod) = @{$self->{groups}[-1]};
312 my $regex = join '', map {
313 83 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 46 37 83
314 46 my $part;
315 46 ▶ if (defined($part = $_->{'.rgx'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($part = $_->{'.rgx'})) { } 6 40 46
elsif (defined($part = $_->{'.ref'})) { } 40 0 40
316 6 $part;
317 }
318 elsif (defined($part = $_->{'.ref'})) {
319 40 "<$part>";
320 }
321 else {
322 0 XXX $_;
323 }
324 }
325 else {
326 37 $_;
327 }
328 44 } splice(@{$self->{stack}}, (pop @{$self->{groups}})->[0]);
329 44 $regex =~ s!\(([ism]?\:|\=|\!)!(?$1!g;
330 44 my $rgx = {'.rgx' => $regex};
331 44 ▶ Pegex::Pegex::AST::set_modifier($rgx, $gmod)
Branch: 1 branch
BranchTrueFalseTotal
if $gmod 1 43 44
332 if $gmod;
333 44 push @{$self->{stack}}, $rgx;
334 }
335  
336 37 ▶sub got_regex_raw {
Subroutine got_regex_raw: called CC=1 SCAR=0.0
Pod undocumented
337 37 my ($self, $token) = @_;
338 37 push @{$self->{stack}}, $token->[1];
339 }
340  
341 #------------------------------------------------------------------------------
342 # Receiver helper methods:
343 #------------------------------------------------------------------------------
344 185 ▶sub group_ast {
Subroutine group_ast: called CC=13 SCAR=25.7
Pod undocumented
345 185 my ($self) = @_;
346 185 my ($offset, $gmod) = @{pop @{$self->{groups}}};
347 185 ▶ $gmod ||= '';
Condition100%2/2
$gmod ||= ""
l!l
15134
MC/DC100%1/1
$gmod ||= ""
$gmod
Truth table100%2/2
A
A= $gmod
Aresult
11
00
348 185 my $rule = [splice(@{$self->{stack}}, $offset)];
349  
350 185 ▶ for (my $i = 0; $i < @$rule-1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rule - 1) 218 185 403
351 218 ▶ if ($rule->[$i + 1] =~ /^%%?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rule[$i + 1] =~ /^%%?$/) 26 192 218
352 26 $rule->[$i] = Pegex::Pegex::AST::set_separator(
353 $rule->[$i],
354 splice @$rule, $i+1, 2
355 );
356 }
357 }
358 185 my $started = 0;
359 185 ▶ for (
Branch: 2 branches
BranchTrueFalseTotal
@$rule && $rule->[0] eq '|' ? : 4 181 185
if ($i < @$rule - 1) 149 185 334
Condition66%2/3
@$rule and $rule->[0] eq "|"
!ll&&!rl&&r
01814
MC/DC50%1/2
@$rule and $rule->[0] eq "|"
@$rule $rule->[0] eq "|"
Truth table66%2/3
A and B
A= @$ruleB= $rule->[0] eq "|"
ABresult
0X0
100
111
360 my $i = (@$rule and $rule->[0] eq '|') ? 1 : 0;
361 $i < @$rule-1;
362 $i++
363 ) {
364 149 ▶ next if $rule->[$i] eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $rule->[$i] eq "|" 0 149 149
365 149 ▶ if ($rule->[$i+1] eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rule[$i + 1] eq '|') { } 43 106 149
366 43 $i++;
367 43 $started = 0;
368 }
369 else {
370 106 ▶ $rule->[$i] = {'.all' => [$rule->[$i]]}
Branch: 1 branch
BranchTrueFalseTotal
unless $started++ 73 33 106
371 unless $started++;
372 106 push @{$rule->[$i]{'.all'}}, splice @$rule, $i+1, 1;
373 106 $i--
374 }
375 }
376 185 ▶ if (grep {$_ eq '|'} @$rule) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "|";} @$rule) 32 153 185
377 32 $rule = [{'.any' => [ grep {$_ ne '|'} @$rule ]}];
378 }
379  
380 185 ▶ $rule = $rule->[0] if @$rule <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$rule <= 1 185 0 185
381 185 ▶ Pegex::Pegex::AST::set_modifier($rule, $gmod)
Branch: 1 branch
BranchTrueFalseTotal
unless $gmod eq ":" 46 139 185
382 unless $gmod eq ':';
383  
384 185 return $rule;
385 }
386  
387 # DEBUG: wrap/trace parse methods:
388 # for my $method (qw(
389 # match_times match_next match_ref match_token match_any match_all
390 # )) {
391 # no strict 'refs';
392 # no warnings 'redefine';
393 # my $orig = \&$method;
394 # *$method = sub {
395 # my $self = shift;
396 # my $args = join ', ', map {
397 # ref($_) ? '[' . join(', ', @$_) . ']' :
398 # length($_) ? $_ : "''"
399 # } @_;
400 # print "$method($args)\n";
401 # die if $main::x++ > 250;
402 # $orig->($self, @_);
403 # };
404 # }
405  
406 #------------------------------------------------------------------------------
407 # Lexer logic:
408 #------------------------------------------------------------------------------
409 7 my $ALPHA = 'A-Za-z';
410 7 my $DIGIT = '0-9';
411 7 my $DASH = '\-';
412 7 my $SEMI = '\;';
413 7 my $UNDER = '\_';
414 7 my $HASH = '\#';
415 7 my $EOL = '\r?\n';
416 7 my $WORD = "$DASH$UNDER$ALPHA$DIGIT";
417 7 my $WS = "(?:[\ \t]|$HASH.*$EOL)";
418 7 my $MOD = '[\!\=\-\+\.]';
419 7 my $GMOD = '[\.\-]';
420 7 my $QUANT = '(?:[\?\*\+]|\d+(?:\+|\-\d+)?)';
421 7 my $NAME = "$UNDER?[$UNDER$ALPHA](?:[$WORD]*[$ALPHA$DIGIT])?";
422  
423 # Repeated Rules:
424 7 my $rem = [qr/\A(?:$WS+|$EOL+)/];
425 7 my $qr = [qr/\A\'((?:\\.|[^\'])*)\'/, 'quoted-regex'];
426  
427 # Lexer regex tree:
428 7 has regexes => {
429 pegex => [
430 [qr/\A%(grammar|version|extends|include)$WS+/,
431 'directive-start', 'directive'],
432  
433 [qr/\A($NAME)(?=$WS*\:)/,
434 'rule-start', 'rule'],
435  
436 $rem,
437  
438 [qr/\A\z/,
439 'pegex-end', 'end'],
440 ],
441  
442 rule => [
443 [qr/\A(?:$SEMI$WS*$EOL?|\s*$EOL|)(?=$NAME$WS*\:|\z)/,
444 'rule-end', 'end'],
445  
446 [qr/\A\:/,
447 'rule-sep'],
448  
449 [qr/\A(?:\+|\~\~)(?=\s)/,
450 'whitespace-must'],
451 [qr/\A(?:\-|\~)(?=\s)/,
452 'whitespace-maybe'],
453  
454 $qr,
455 [qr/\A($MOD)?($NAME|<$NAME>)($QUANT)?(?!$WS*$NAME\:)/,
456 'rule-reference'],
457 [qr/\A($GMOD)?\//,
458 'regex-start', 'regex'],
459 [qr/\A\`([^\`\n]*?)\`/,
460 'error-message'],
461  
462 [qr/\A($GMOD)?\(/,
463 'group-start'],
464 [qr/\A\)($QUANT)?/,
465 'group-end'],
466 [qr/\A\|/,
467 'list-alt'],
468 [qr/\A(\%\%?)/,
469 'list-sep'],
470  
471 $rem,
472 ],
473  
474 directive => [
475 [qr/\A(\S.*)/,
476 'directive-value'],
477 [qr/\A$EOL/,
478 'directive-end', 'end']
479 ],
480  
481 regex => [
482 [qr/\A$WS+(?:\+|\~\~|\-\-)/,
483 'whitespace-must'],
484 [qr/\A(?:\-|~)(?![-~])/,
485 'whitespace-maybe'],
486 $qr,
487 [qr/\A$WS+()($NAME|<$NAME>)/,
488 'rule-reference'],
489 [qr/\A([^\s\'\/]+)/,
490 'regex-raw'],
491 [qr/\A$WS+/],
492 [qr/\A$EOL+/],
493 [qr/\A\//,
494 'regex-end', 'end'],
495 $rem,
496 ],
497 };
498  
499 112 ▶sub lex {
Subroutine lex: called CC=12 SCAR=24.9
Pod undocumented
500 112 my ($self, $grammar) = @_;
501  
502 112 my $tokens = $self->{tokens} = [['pegex-start']];
503 112 my $stack = ['pegex'];
504 112 my $pos = 0;
505  
506 112 OUTER: while (1) {
507 1665 my $state = $stack->[-1];
508 1665 ▶ my $set = $self->{regexes}->{$state} or die "Invalid state '$state'";
Branch: 1 branch
BranchTrueFalseTotal
my $set = $self->{"regexes"}{$state} or die "Invalid state '${state}'" 1665 0 1665
509 1665 for my $entry (@$set) {
510 11794 my ($regex, $name, $scope) = @$entry;
511 11794 ▶ if (substr($grammar, $pos) =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($grammar, $pos) =~ /$regex/) 1665 10129 11794
512 1665 $pos += length($&);
513 1665 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1160 505 1665
514 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
515 1160 my @captures = map $$_, 1..$#+;
516 pop @captures
517 1160 ▶ while @captures and not defined $captures[-1];
Branch: 1 branch
BranchTrueFalseTotal
@captures and not defined $captures[-1] and do { pop @captures } 308 1160 1468
Condition100%3/3
@captures and not defined $captures[-1]
!ll&&!rl&&r
622538308
MC/DC100%2/2
@captures and not defined $captures[-1]
@captures not defined $captures[-1]
Truth table100%3/3
A and not(B)
A= @capturesB= not defined $captures[-1]
ABresult
0X0
100
111
518 1160 push @$tokens, [$name, @captures];
519 1160 ▶ if ($scope) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope) 496 664 1160
520 496 ▶ if ($scope eq 'end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq 'end') { } 304 192 496
521 304 pop @$stack;
522 }
523 else {
524 192 push @$stack, $scope;
525 # Hack to support /+ …/
526 192 ▶ if ($scope eq 'regex') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "regex") 44 148 192
527 44 ▶ if (substr($grammar, $pos) =~ /\A\+(?=[\s\/])/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($grammar, $pos) =~ m[\A\+(?=[\s/])]) 2 42 44
528 2 $pos += length($&);
529 2 push @$tokens, ['whitespace-must'];
530 }
531 }
532 }
533 }
534 }
535 1665 ▶ last OUTER unless @$stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack 112 1553 1665
536 1553 next OUTER;
537 }
538 }
539 0 my $text = substr($grammar, $pos, 50);
540 0 $text =~ s/\n/\\n/g;
541 0 WWW $tokens;
542 0 die <<"...";
543 Failed to lex $state here-->$text
544 ...
545 }
546 }
547  
548 7 1;
549  
550 # vim: set lisp:
« Pegex/Input.pm ↑ Summary Pegex/Pegex/AST.pm »