Pegex/Parser.pm

filter: Statementstmt 88.8% 199 / 224 Branchbran 67.7% 107 / 158 Conditioncond 52.3% 34 / 65 MC/DCmcdc 32.4% 12 / 37 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 16 totaltotal 71.1% 372 / 523 CC 107 SCAR 37.6
CC
107
Cov
76%
BUILD
117.6
match_next_with_limit
56.0
parse
21.3
CRAP 42.9
« Pegex/Grammar.pm ↑ Summary Pegex/Optimizer.pm »
1 package Pegex::Parser;
2 18 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶no warnings qw( recursion );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 ▶use Pegex::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Pegex::Optimizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 has grammar => (required => 1);
10 18 has receiver => ();
11 18 has input => ();
12  
13 18 has recursion_count => 0;
14 18 has iteration_count => 0;
15  
16 18 has debug => ();
17 18 has debug_indent => ();
18 18 has debug_color => ();
19 18 has debug_got_color => ();
20 18 has debug_not_color => ();
21  
22 18 has recursion_limit => ();
23 18 has recursion_warn_limit => ();
24 18 has iteration_limit => ();
25  
26 289 ▶sub BUILD {
Subroutine BUILD: called CC=36 SCAR=47.7
Pod undocumented
27 289 my ($self) = @_;
28  
29 289 ▶ $self->{throw_on_error} ||= 1;
Condition50%1/2
$self->{"throw_on_error"} ||= 1
l!l
0289
MC/DC0%0/1
$self->{"throw_on_error"} ||= 1
$self->{"throw_on_error"}
Truth table50%1/2
A
A= $self->{"throw_on_error"}
Aresult
11
00
30  
31 $self->{debug} =
32 defined($ENV{PERL_PEGEX_DEBUG}) ? $ENV{PERL_PEGEX_DEBUG} :
33 defined($Pegex::Parser::Debug) ? $Pegex::Parser::Debug : 0
34 289 ▶ unless defined($self->{debug});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"debug"} 287 2 289
defined $ENV{'PERL_PEGEX_DEBUG'} ? : 0 287 287
defined $Pegex::Parser::Debug ? : 0 287 287
35  
36 $self->{debug_indent} =
37 defined($ENV{PERL_PEGEX_DEBUG_INDENT}) ? $ENV{PERL_PEGEX_DEBUG_INDENT} :
38 defined($Pegex::Parser::DebugIndent) ? $Pegex::Parser::DebugIndent : 1
39 289 ▶ unless defined($self->{debug_indent});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"debug_indent"} 289 0 289
defined $ENV{'PERL_PEGEX_DEBUG_INDENT'} ? : 0 289 289
defined $Pegex::Parser::DebugIndent ? : 0 289 289
40 $self->{debug_indent} = 1 if (
41 not length $self->{debug_indent}
42 or $self->{debug_indent} =~ tr/0-9//c
43 289 ▶ or $self->{debug_indent} < 0
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->{"debug_indent"} or $self->{"debug_indent"} =~ tr/0-9//c or $self->{"debug_indent"} < 0 0 289 289
Condition33%2/6
not length $self->{"debug_indent"} or $self->{"debug_indent"} =~ tr/0-9//c or $self->{"debug_indent"} < 0
l!l&&r!l&&!r
00289
not length $self->{"debug_indent"} or $self->{"debug_indent"} =~ tr/0-9//c
l!l&&r!l&&!r
00289
MC/DC0%0/3
not length $self->{"debug_indent"} or $self->{"debug_indent"} =~ tr/0-9//c or $self->{"debug_indent"} < 0
not length $self->{"debug_indent"} $self->{"debug_indent"} =~ tr/0-9//c $self->{"debug_indent"} < 0
Truth table25%1/4
not(A) or B or C
A= not length $self->{"debug_indent"}B= $self->{"debug_indent"} =~ tr/0-9//cC= $self->{"debug_indent"} < 0
ABCresult
1XX1
01X1
0011
0000
44 );
45  
46 289 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 2 287 289
47 $self->{debug_color} =
48 defined($ENV{PERL_PEGEX_DEBUG_COLOR}) ? $ENV{PERL_PEGEX_DEBUG_COLOR} :
49 defined($Pegex::Parser::DebugColor) ? $Pegex::Parser::DebugColor : 1
50 2 ▶ unless defined($self->{debug_color});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"debug_color"} 2 0 2
defined $ENV{'PERL_PEGEX_DEBUG_COLOR'} ? : 0 2 2
defined $Pegex::Parser::DebugColor ? : 0 2 2
51 2 my ($got, $not);
52 ($self->{debug_color}, $got, $not) =
53 2 split / *, */, $self->{debug_color};
54 2 ▶ $got ||= 'bright_green';
Condition50%1/2
$got ||= "bright_green"
l!l
02
MC/DC0%0/1
$got ||= "bright_green"
$got
Truth table50%1/2
A
A= $got
Aresult
11
00
55 2 ▶ $not ||= 'bright_red';
Condition50%1/2
$not ||= "bright_red"
l!l
02
MC/DC0%0/1
$not ||= "bright_red"
$not
Truth table50%1/2
A
A= $not
Aresult
11
00
56 2 $_ = [split ' ', $_] for ($got, $not);
57 2 $self->{debug_got_color} = $got;
58 2 $self->{debug_not_color} = $not;
59 2 ▶ my $c = defined($self->{debug_color}) ? $self->{debug_color} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'debug_color'} ? : 2 0 2
60 $self->{debug_color} =
61 2 ▶ $c eq 'always' ? 1 :
Branch: 5 branches
BranchTrueFalseTotal
$c eq 'always' ? : 0 2 2
$c eq 'auto' ? : 0 2 2
-t STDERR ? : 0 0 0
$c eq 'never' ? : 0 2 2
$c =~ /^\d+$/ ? : 2 0 2
62 $c eq 'auto' ? (-t STDERR ? 1 : 0) :
63 $c eq 'never' ? 0 :
64 $c =~ /^\d+$/ ? $c : 0;
65 2 ▶ if ($self->{debug_color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug_color"}) 2 0 2
66 2 require Term::ANSIColor;
67 2 ▶ if ($Term::ANSIColor::VERSION < 3.00) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::VERSION < 3) 0 2 2
68 0 s/^bright_// for
69 0 @{$self->{debug_got_color}},
70 0 @{$self->{debug_not_color}};
71 }
72 }
73 }
74 $self->{recursion_limit} =
75 defined($ENV{PERL_PEGEX_RECURSION_LIMIT}) ? $ENV{PERL_PEGEX_RECURSION_LIMIT} :
76 defined($Pegex::Parser::RecursionLimit) ? $Pegex::Parser::RecursionLimit : 0
77 289 ▶ unless defined($self->{recursion_limit});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"recursion_limit"} 289 0 289
defined $ENV{'PERL_PEGEX_RECURSION_LIMIT'} ? : 0 289 289
defined $Pegex::Parser::RecursionLimit ? : 0 289 289
78 $self->{recursion_warn_limit} =
79 defined($ENV{PERL_PEGEX_RECURSION_WARN_LIMIT}) ? $ENV{PERL_PEGEX_RECURSION_WARN_LIMIT} :
80 defined($Pegex::Parser::RecursionWarnLimit) ? $Pegex::Parser::RecursionWarnLimit : 0
81 289 ▶ unless defined($self->{recursion_warn_limit});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"recursion_warn_limit"} 289 0 289
defined $ENV{'PERL_PEGEX_RECURSION_WARN_LIMIT'} ? : 0 289 289
defined $Pegex::Parser::RecursionWarnLimit ? : 0 289 289
82 $self->{iteration_limit} =
83 defined($ENV{PERL_PEGEX_ITERATION_LIMIT}) ? $ENV{PERL_PEGEX_ITERATION_LIMIT} :
84 defined($Pegex::Parser::IterationLimit) ? $Pegex::Parser::IterationLimit : 0
85 289 ▶ unless defined($self->{iteration_limit});
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->{"iteration_limit"} 289 0 289
defined $ENV{'PERL_PEGEX_ITERATION_LIMIT'} ? : 0 289 289
defined $Pegex::Parser::IterationLimit ? : 0 289 289
86 }
87  
88 # XXX Add an optional $position argument. Default to 0. This is the position
89 # to start parsing. Set position and farthest below to this value. Allows for
90 # sub-parsing. Need to somehow return the finishing position of a subparse.
91 # Maybe this all goes in a subparse() method.
92 287 ▶sub parse {
Subroutine parse: called CC=17 SCAR=30.6
Pod undocumented
93 287 my ($self, $input, $start) = @_;
94  
95 287 ▶ $start =~ s/-/_/g if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 0 287 287
96  
97 287 $self->{position} = 0;
98 287 $self->{farthest} = 0;
99  
100 287 ▶ $self->{input} = (not ref $input)
Branch: 1 branch
BranchTrueFalseTotal
!ref($input) ? : 286 1 287
101 ? Pegex::Input->new(string => $input)
102 : $input;
103  
104 $self->{input}->open
105 287 ▶ unless $self->{input}{_is_open};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"input"}{"_is_open"} 287 0 287
106 287 $self->{buffer} = $self->{input}->read;
107 287 $self->{last_line_pos} = 0;
108 287 $self->{last_line} = 1;
109  
110 287 ▶ $self->{grammar}{tree} ||= $self->{grammar}->make_tree;
Condition66%2/3
$self->{"grammar"}{"tree"} ||= $self->{"grammar"}->make_tree
l!l&&r!l&&!r
592280
MC/DC0%0/2
$self->{"grammar"}{"tree"} ||= $self->{"grammar"}->make_tree
$self->{"grammar"}{"tree"} $self->{"grammar"}->make_tree
Truth table66%2/3
A ||= B
A= $self->{"grammar"}{"tree"}B= $self->{"grammar"}->make_tree
ABresult
1X1
011
000
111  
112 my $start_rule_ref = $start ||
113 $self->{grammar}{tree}{'+toprule'} ||
114 273 ▶ $self->{grammar}{tree}{'TOP'} & 'TOP' or
Branch: 1 branch
BranchTrueFalseTotal
my $start_rule_ref = $start || $self->{"grammar"}{"tree"}{"+toprule"} || $self->{"grammar"}{"tree"}{"TOP"} & "TOP" or die "No starting rule for Pegex::Parser::parse" 273 0 273
Condition33%2/6
$start || $self->{'grammar'}{'tree'}{'+toprule'} || $self->{'grammar'}{'tree'}{'TOP'} & 'TOP'
l!l&&r!l&&!r
27300
$start or $self->{"grammar"}{"tree"}{"+toprule"}
l!l&&r!l&&!r
02730
MC/DC0%0/3
$start || $self->{'grammar'}{'tree'}{'+toprule'} || $self->{'grammar'}{'tree'}{'TOP'} & 'TOP'
$start $self->{"grammar"}{"tree"}{"+toprule"} $self->{'grammar'}{'tree'}{'TOP'} & 'TOP'
Truth table25%1/4
A or B || C
A= $startB= $self->{"grammar"}{"tree"}{"+toprule"}C= $self->{'grammar'}{'tree'}{'TOP'} & 'TOP'
ABCresult
1XX1
01X1
0011
0000
115 die "No starting rule for Pegex::Parser::parse";
116  
117 die "No 'receiver'. Can't parse"
118 273 ▶ unless $self->{receiver};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"receiver"} 0 273 273
119  
120 my $optimizer = Pegex::Optimizer->new(
121 parser => $self,
122 grammar => $self->{grammar},
123 receiver => $self->{receiver},
124 273 );
125  
126 273 $optimizer->optimize_grammar($start_rule_ref);
127  
128 # Add circular ref and weaken it.
129 273 $self->{receiver}{parser} = $self;
130 273 Scalar::Util::weaken($self->{receiver}{parser});
131  
132 273 ▶ if ($self->{receiver}->can("initial")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"receiver"}->can("initial")) 0 273 273
133 0 $self->{rule} = $start_rule_ref;
134 0 $self->{parent} = {};
135 0 $self->{receiver}->initial();
136 }
137  
138 273 local *match_next;
139 {
140 18 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 *match_next = (
142 $self->{recursion_warn_limit} or
143 $self->{recursion_limit} or
144 $self->{iteration_limit}
145 273 ▶ ) ? \&match_next_with_limit :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'recursion_warn_limit'} || $self->{'recursion_limit'} || $self->{'iteration_limit'} ? : 0 273 273
Condition33%2/6
$self->{"recursion_warn_limit"} or $self->{"recursion_limit"} or $self->{"iteration_limit"}
l!l&&r!l&&!r
00273
$self->{"recursion_warn_limit"} or $self->{"recursion_limit"}
l!l&&r!l&&!r
00273
MC/DC0%0/3
$self->{"recursion_warn_limit"} or $self->{"recursion_limit"} or $self->{"iteration_limit"}
$self->{"recursion_warn_limit"} $self->{"recursion_limit"} $self->{"iteration_limit"}
Truth table25%1/4
A or B or C
A= $self->{"recursion_warn_limit"}B= $self->{"recursion_limit"}C= $self->{"iteration_limit"}
ABCresult
1XX1
01X1
0011
0000
146 \&match_next_normal;
147 }
148  
149 273 ▶ my $match = $self->debug ? do {
Branch: 1 branch
BranchTrueFalseTotal
$self->debug ? : 1 272 273
150 1 my $method = $optimizer->make_trace_wrapper(\&match_ref);
151 1 $self->$method($start_rule_ref, {'+asr' => 0});
152 } : $self->match_ref($start_rule_ref, {});
153  
154 259 $self->{input}->close;
155  
156 259 ▶ if (not $match or $self->{position} < length ${$self->{buffer}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $match or $self->{"position"} < length ${$self->{"buffer"};}) 2 257 259
Condition100%3/3
not $match or $self->{"position"} < length ${$self->{"buffer"};}
l!l&&r!l&&!r
11257
MC/DC100%2/2
not $match or $self->{"position"} < length ${$self->{"buffer"};}
not $match $self->{"position"} < length ${$self->{"buffer"};}
Truth table100%3/3
not(A) or B
A= not $matchB= $self->{"position"} < length ${$self->{"buffer"};}
ABresult
1X1
011
000
157 2 $self->throw_error("Parse document failed for some reason");
158 0 return; # In case $self->throw_on_error is off
159 }
160  
161 257 ▶ if ($self->{receiver}->can("final")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"receiver"}->can("final")) 255 2 257
162 255 $self->{rule} = $start_rule_ref;
163 255 $self->{parent} = {};
164 255 $match = [ $self->{receiver}->final(@$match) ];
165 }
166  
167 257 $match->[0];
168 }
169  
170 41558 ▶sub match_next_normal {
Subroutine match_next_normal: called CC=17 SCAR=28.4
Pod undocumented
171 41558 my ($self, $next) = @_;
172  
173 my ($rule, $method, $kind, $min, $max, $assertion) =
174 41558 @{$next}{'rule', 'method', 'kind', '+min', '+max', '+asr'};
175  
176 my ($position, $match, $count) =
177 41558 ($self->{position}, [], 0);
178  
179 41558 ▶ while (my $return = $method->($self, $rule, $next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $return = &$method($self, $rule, $next)) 15949 26543 42492
180 15949 ▶ $position = $self->{position} unless $assertion;
Branch: 1 branch
BranchTrueFalseTotal
unless $assertion 15942 7 15949
181 15949 $count++;
182 15949 push @$match, @$return;
183 15949 ▶ last if $max == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max == 1 14845 1104 15949
184 }
185 41388 ▶ if (not $count and $min == 0 and $kind eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $count and $min == 0 and $kind eq "all") 1443 39945 41388
Condition100%6/6
not $count and $min == 0 and $kind eq "all"
!ll&&!rl&&r
395184271443
not $count and $min == 0
!ll&&!rl&&r
15486240321870
MC/DC100%3/3
not $count and $min == 0 and $kind eq "all"
not $count $min == 0 $kind eq "all"
Truth table100%4/4
not(A) and B and C
A= not $countB= $min == 0C= $kind eq "all"
ABCresult
0XX0
10X0
1100
1111
186 1443 $match = [[]];
187 }
188 41388 ▶ if ($max != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max != 1) 1633 39755 41388
189 1633 ▶ if ($next->{-flat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->{'-flat'}) { } 34 1599 1633
190 34 ▶ $match = [ map { (ref($_) eq 'ARRAY') ? (@$_) : ($_) } @$match ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 38 4 42
191 }
192 else {
193 1599 $match = [$match]
194 }
195 }
196 41388 ▶ my $result = ($count >= $min and (not $max or $count <= $max))
Condition66%4/6
$count >= $min && (!$max || $count <= $max)
!ll&&!rl&&r
24032017356
not $max or $count <= $max
l!l&&r!l&&!r
1619157370
MC/DC33%1/3
$count >= $min && (!$max || $count <= $max)
$count >= $min not $max $count <= $max
Truth table75%3/4
A && not(B) or C
A= $count >= $minB= not $maxC= $count <= $max
ABCresult
0XX0
1000
11X1
1011
197 ^ ($assertion == -1);
198 41388 ▶ if (not($result) or $assertion) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $result or $assertion) 24039 17349 41388
Condition100%3/3
not $result or $assertion
l!l&&r!l&&!r
240142517349
MC/DC100%2/2
not $result or $assertion
not $result $assertion
Truth table100%3/3
not(A) or B
A= not $resultB= $assertion
ABresult
1X1
011
000
199 $self->{farthest} = $position
200 24039 ▶ if ($self->{position} = $position) > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"position"} = $position) > $self->{"farthest"} 0 24039 24039
201 }
202  
203 41388 ▶ ($result ? $next->{'-skip'} ? [] : $match : 0);
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 17374 24014 41388
$next->{'-skip'} ? : 8 17366 17374
204 }
205  
206 0 ▶sub match_next_with_limit {
Subroutine match_next_with_limit: not called CC=7 SCAR=40.3
Pod undocumented
207 0 my ($self, $next) = @_;
208  
209 0 ▶ sub limit_msg {
Subroutine limit_msg: not called CC=1 SCAR=6.9
Pod undocumented
210 0 "Deep recursion ($_[0] levels) on Pegex::Parser::match_next\n";
211 }
212  
213 0 $self->{iteration_count}++;
214 0 $self->{recursion_count}++;
215  
216 0 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'recursion_limit'} and $self->{'recursion_count'} >= $self->{'recursion_limit'}) { } 0 0 0
elsif ($self->{'recursion_warn_limit'} and not $self->{'recursion_count'} % $self->{'recursion_warn_limit'}) { } 0 0 0
elsif ($self->{'iteration_limit'} and $self->{'iteration_count'} > $self->{'iteration_limit'}) { } 0 0 0
Condition0%0/3
$self->{"recursion_limit"} and $self->{"recursion_count"} >= $self->{"recursion_limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"recursion_limit"} and $self->{"recursion_count"} >= $self->{"recursion_limit"}
$self->{"recursion_limit"} $self->{"recursion_count"} >= $self->{"recursion_limit"}
Truth table0%0/3
A and B
A= $self->{"recursion_limit"}B= $self->{"recursion_count"} >= $self->{"recursion_limit"}
ABresult
0X0
100
111
217 $self->{recursion_limit} and
218 $self->{recursion_count} >= $self->{recursion_limit}
219 0 ) { die limit_msg $self->{recursion_count} }
220 ▶ elsif (
Condition0%0/3
$self->{"recursion_warn_limit"} and not $self->{"recursion_count"} % $self->{"recursion_warn_limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"recursion_warn_limit"} and not $self->{"recursion_count"} % $self->{"recursion_warn_limit"}
$self->{"recursion_warn_limit"} not $self->{"recursion_count"} % $self->{"recursion_warn_limit"}
Truth table0%0/3
A and not(B)
A= $self->{"recursion_warn_limit"}B= not $self->{"recursion_count"} % $self->{"recursion_warn_limit"}
ABresult
0X0
100
111
221 $self->{recursion_warn_limit} and
222 not ($self->{recursion_count} % $self->{recursion_warn_limit})
223 0 ) { warn limit_msg $self->{recursion_count} }
224 ▶ elsif (
Condition0%0/3
$self->{"iteration_limit"} and $self->{"iteration_count"} > $self->{"iteration_limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"iteration_limit"} and $self->{"iteration_count"} > $self->{"iteration_limit"}
$self->{"iteration_limit"} $self->{"iteration_count"} > $self->{"iteration_limit"}
Truth table0%0/3
A and B
A= $self->{"iteration_limit"}B= $self->{"iteration_count"} > $self->{"iteration_limit"}
ABresult
0X0
100
111
225 $self->{iteration_limit} and
226 $self->{iteration_count} > $self->{iteration_limit}
227 0 ) { die "Pegex iteration limit of $self->{iteration_limit} reached." }
228  
229 0 my $result = $self->match_next_normal($next);
230  
231 0 $self->{recursion_count}--;
232  
233 0 return $result;
234 }
235  
236 1 ▶sub match_rule {
Subroutine match_rule: called CC=4 SCAR=14.2
Pod undocumented
237 1 my ($self, $position, $match) = (@_, []);
238 1 $self->{position} = $position;
239 $self->{farthest} = $position
240 1 ▶ if $position > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if $position > $self->{"farthest"} 1 0 1
241 1 ▶ $match = [ $match ] if @$match > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$match > 1 1 0 1
242 1 my ($ref, $parent) = @{$self}{'rule', 'parent'};
243 1 ▶ my $rule = $self->{grammar}{tree}{$ref}
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $self->{"grammar"}{"tree"}{$ref} or die "No rule defined for '${ref}'" 1 0 1
244 or die "No rule defined for '$ref'";
245  
246 1 [ $rule->{action}->($self->{receiver}, @$match) ];
247 }
248  
249 17470 ▶sub match_ref {
Subroutine match_ref: called CC=4 SCAR=13.9
Pod undocumented
250 17470 my ($self, $ref, $parent) = @_;
251 17470 ▶ my $rule = $self->{grammar}{tree}{$ref}
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $self->{"grammar"}{"tree"}{$ref} or die "No rule defined for '${ref}'" 17470 0 17470
252 or die "No rule defined for '$ref'";
253 17470 ▶ my $match = $self->match_next($rule) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $match = $self->match_next($rule) or return 6942 10457 17399
254 6942 ▶ return $Pegex::Constant::Dummy unless $rule->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"action"} 23 6919 6942
255 6919 @{$self}{'rule', 'parent'} = ($ref, $parent);
256  
257 # XXX Possible API mismatch.
258 # Not sure if we should "splat" the $match.
259 6919 [ $rule->{action}->($self->{receiver}, @$match) ];
260 }
261  
262 15041 ▶sub match_rgx {
Subroutine match_rgx: called CC=4 SCAR=13.9
Pod undocumented
263 15041 my ($self, $regexp) = @_;
264 15041 my $buffer = $self->{buffer};
265  
266 15041 pos($$buffer) = $self->{position};
267 15041 ▶ $$buffer =~ /$regexp/g or return;
Branch: 1 branch
BranchTrueFalseTotal
$$buffer =~ /$regexp/g or return 4640 10401 15041
268  
269 4640 $self->{position} = pos($$buffer);
270  
271 $self->{farthest} = $self->{position}
272 4640 ▶ if $self->{position} > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"position"} > $self->{"farthest"} 2771 1869 4640
273  
274 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 4640 my $captures = [ map $$_, 1..$#+ ];
276 4640 ▶ $captures = [ $captures ] if $#+ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#+ > 1 541 4099 4640
277  
278 4640 return $captures;
279 }
280  
281 7434 ▶sub match_all {
Subroutine match_all: called CC=7 SCAR=19.5
Pod undocumented
282 7434 my ($self, $list) = @_;
283 7434 my $position = $self->{position};
284 7434 my $set = [];
285 7434 my $len = 0;
286 7434 for my $elem (@$list) {
287 13242 ▶ if (my $match = $self->match_next($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->match_next($elem)) { } 8699 4486 13185
288 8699 ▶ if (not ($elem->{'+asr'} or $elem->{'-skip'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->{"+asr"} or $elem->{"-skip"}) 8666 33 8699
Condition100%3/3
$elem->{"+asr"} or $elem->{"-skip"}
l!l&&r!l&&!r
2588666
MC/DC100%2/2
$elem->{"+asr"} or $elem->{"-skip"}
$elem->{"+asr"} $elem->{"-skip"}
Truth table100%3/3
A or B
A= $elem->{"+asr"}B= $elem->{"-skip"}
ABresult
1X1
011
000
289 8666 push @$set, @$match;
290 8666 $len++;
291 }
292 }
293 else {
294 $self->{farthest} = $position
295 4486 ▶ if ($self->{position} = $position) > $self->{farthest};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"position"} = $position) > $self->{"farthest"} 0 4486 4486
296 4486 return;
297 }
298 }
299 2891 ▶ $set = [ $set ] if $len > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 1 2887 4 2891
300 2891 return $set;
301 }
302  
303 2974 ▶sub match_any {
Subroutine match_any: called CC=3 SCAR=11.0
Pod undocumented
304 2974 my ($self, $list) = @_;
305 2974 for my $elem (@$list) {
306 10846 ▶ if (my $match = $self->match_next($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->match_next($elem)) 1733 9071 10804
307 1733 return $match;
308 }
309 }
310 1199 return;
311 }
312  
313 14 ▶sub match_err {
Subroutine match_err: called CC=1 SCAR=0.0
Pod undocumented
314 14 my ($self, $error) = @_;
315 14 $self->throw_error($error);
316 }
317  
318 46 ▶sub trace {
Subroutine trace: called CC=10 SCAR=23.1
Pod undocumented
319 46 my ($self, $action) = @_;
320 46 ▶ my $indent = ($action =~ /^try_/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$action =~ /^try_/ ? : 23 23 46
321 46 ▶ $self->{indent} ||= 0;
Condition100%2/2
$self->{"indent"} ||= 0
l!l
451
MC/DC100%1/1
$self->{"indent"} ||= 0
$self->{"indent"}
Truth table100%2/2
A
A= $self->{"indent"}
Aresult
11
00
322 46 ▶ $self->{indent}-- unless $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless $indent 23 23 46
323  
324 $action = (
325 $action =~ m/got_/ ?
326 Term::ANSIColor::colored($self->{debug_got_color}, $action) :
327 $action =~ m/not_/ ?
328 Term::ANSIColor::colored($self->{debug_not_color}, $action) :
329 $action
330 46 ▶ ) if $self->{debug_color};
Branch: 3 branches
BranchTrueFalseTotal
if $self->{"debug_color"} 46 0 46
$action =~ /got_/ ? : 23 23 46
$action =~ /not_/ ? : 0 23 23
331  
332 46 print STDERR ' ' x ($self->{indent} * $self->{debug_indent});
333 46 ▶ $self->{indent}++ if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 23 23 46
334 46 my $snippet = substr(${$self->{buffer}}, $self->{position});
335 46 ▶ $snippet = substr($snippet, 0, 30) . "..."
Branch: 1 branch
BranchTrueFalseTotal
if length $snippet > 30 18 28 46
336 if length $snippet > 30;
337 46 $snippet =~ s/\n/\\n/g;
338 46 ▶ print STDERR sprintf("%-30s", $action) .
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 23 23 46
339 ($indent ? " >$snippet<\n" : "\n");
340 }
341  
342 16 ▶sub throw_error {
Subroutine throw_error: called CC=2 SCAR=7.0
Pod undocumented
343 16 my ($self, $msg) = @_;
344 16 $@ = $self->{error} = $self->format_error($msg);
345 16 ▶ return undef unless $self->{throw_on_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"throw_on_error"} 0 16 16
346 16 require Carp;
347 16 Carp::croak($self->{error});
348 }
349  
350 16 ▶sub format_error {
Subroutine format_error: called CC=3 SCAR=11.0
Pod undocumented
351 16 my ($self, $msg) = @_;
352 16 my $buffer = $self->{buffer};
353 16 my $position = $self->{farthest};
354 16 my $real_pos = $self->{position};
355  
356 16 my $line = $self->line($position);
357 16 my $column = $position - rindex($$buffer, "\n", $position);
358  
359 16 ▶ my $pretext = substr(
Branch: 2 branches
BranchTrueFalseTotal
$position < 50 ? : 16 0 16
$position < 50 ? : 16 0 16
360 $$buffer,
361 $position < 50 ? 0 : $position - 50,
362 $position < 50 ? $position : 50
363 );
364 16 my $context = substr($$buffer, $position, 50);
365 16 $pretext =~ s/.*\n//gs;
366 16 $context =~ s/\n/\\n/g;
367  
368 16 return <<"...";
369 Error parsing Pegex document:
370 msg: $msg
371 line: $line
372 column: $column
373 context: $pretext$context
374 16 ${\ (' ' x (length($pretext) + 10) . '^')}
375 position: $position ($real_pos pre-lookahead)
376 ...
377 }
378  
379 # TODO Move this to a Parser helper role/subclass
380 0 ▶sub line_column {
Subroutine line_column: not called CC=2 SCAR=17.9
Pod undocumented
381 0 my ($self, $position) = @_;
382 0 ▶ $position ||= $self->{position};
Condition0%0/3
$position ||= $self->{"position"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$position ||= $self->{"position"}
$position $self->{"position"}
Truth table0%0/3
A ||= B
A= $positionB= $self->{"position"}
ABresult
1X1
011
000
383 0 my $buffer = $self->{buffer};
384 0 my $line = $self->line($position);
385 0 my $column = $position - rindex($$buffer, "\n", $position);
386 0 return [$line, $column];
387 }
388  
389 16 ▶sub line {
Subroutine line: called CC=4 SCAR=14.0
Pod undocumented
390 16 my ($self, $position) = @_;
391 16 ▶ $position ||= $self->{position};
Condition66%2/3
$position ||= $self->{"position"}
l!l&&r!l&&!r
1501
MC/DC50%1/2
$position ||= $self->{"position"}
$position $self->{"position"}
Truth table66%2/3
A ||= B
A= $positionB= $self->{"position"}
ABresult
1X1
011
000
392 16 my $buffer = $self->{buffer};
393 16 my $last_line = $self->{last_line};
394 16 my $last_line_pos = $self->{last_line_pos};
395 16 my $len = $position - $last_line_pos;
396 16 ▶ if ($len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) 1 15 16
397 1 return $last_line;
398 }
399 15 my $line;
400 15 ▶ if ($len < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) { } 0 15 15
401 0 $line = $last_line - scalar substr($$buffer, $position, -$len) =~ tr/\n//;
402 } else {
403 15 $line = $last_line + scalar substr($$buffer, $last_line_pos, $len) =~ tr/\n//;
404 }
405 15 $self->{last_line} = $line;
406 15 $self->{last_line_pos} = $position;
407 15 return $line;
408 }
409  
410 # XXX Need to figure out what uses this. (sample.t)
411 {
412 18 package Pegex::Constant;
413 18 our $Null = [];
414 18 our $Dummy = [];
415 }
416  
417 18 1;
« Pegex/Grammar.pm ↑ Summary Pegex/Optimizer.pm »