AST/Grammar/Base.pm

filter: Statementstmt 33.8% 61 / 180 Branchbran 6.2% 3 / 48 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 70.3% 19 / 27 Podpod 100% 17 / 17 totaltotal 31.6% 100 / 316 CC 37 SCAR 44.9
CC
37
Cov
25%
parse
272.0
value
90.0
_callback
56.0
CRAP 89.4
« AST/Grammar/ECMAScript_262_5/Program.pm ↑ Summary AST/Grammar/ECMAScript_262_5/Lexical/StringLiteral/Semantics.pm »
1 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package MarpaX::Languages::ECMAScript::AST::Grammar::Base;
5 16 ▶use MarpaX::Languages::ECMAScript::AST::Util qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use MarpaX::Languages::ECMAScript::AST::Impl qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use constant SEARCH_KEYWORD_IN_GRAMMAR => '# DO NOT REMOVE NOR MODIFY THIS LINE';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use MarpaX::Languages::ECMAScript::AST::Exceptions qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: ECMAScript, grammars base package
12  
13 16 our $VERSION = '0.020'; # VERSION
14  
15 #
16 # Note: because this module is usually subclasses, internal methods are called
17 # using _method($self, ...) instead of $self->_method(...)
18 #
19  
20  
21 21 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
22 21 my ($class, $spec) = @_;
23  
24 21 ▶ InternalError(error => 'Missing ECMAScript specification') if (! defined($spec));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 0 21 21
25  
26 21 my $self = {
27 _content => $class->make_content($spec),
28 _grammar_option => $class->make_grammar_option($spec),
29 _recce_option => $class->make_recce_option($spec),
30 };
31  
32 21 bless($self, $class);
33  
34 21 return $self;
35 }
36  
37  
38 15 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
39 15 my ($self) = @_;
40 15 return $self->{_content};
41 }
42  
43  
44 42 ▶sub make_content {
Subroutine make_content: called CC=2 SCAR=7.1
Pod documented
45 42 my ($class, $spec) = @_;
46  
47 42 my $content = $class->make_grammar_content;
48  
49 #
50 # Too painful to write MarpaX::Languages::ECMAScript::AST::Grammar::${spec}::CharacterClasses::IsSomething
51 # so I change it on-the-fly here
52 #
53 42 ▶ if ($spec eq 'ECMAScript-262-5') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "ECMAScript-262-5") 0 42 42
54 0 $spec = 'ECMAScript_262_5';
55 }
56 42 my $characterClass = "\\p{MarpaX::Languages::ECMAScript::AST::Grammar::${spec}::CharacterClasses::Is";
57 42 $content =~ s/\\p\{Is/$characterClass/g;
58  
59 42 return $content;
60 }
61  
62  
63 15 ▶sub extract {
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
64 15 my ($self) = @_;
65 15 my $rc = '';
66  
67 15 my $content = $self->content;
68 15 my $index = index($content, SEARCH_KEYWORD_IN_GRAMMAR);
69 15 ▶ if ($index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 15 0 15
70 15 $rc = substr($content, $index);
71 15 $rc =~ s/\baction[ \t]*=>[ \t]*\w+//g;
72 15 $rc =~ s/(__\w+)[ \t]*::=[ \t]*/$1 ~ /g;
73 }
74  
75 15 return $rc;
76 }
77  
78  
79 21 ▶sub make_grammar_option {
Subroutine make_grammar_option: called CC=1 SCAR=0.0
Pod documented
80 21 my ($class, $spec) = @_;
81 21 return {bless_package => $class->make_bless_package,
82 source => \$class->make_content($spec, $class->make_grammar_content)};
83 }
84  
85  
86 0 ▶sub make_grammar_content {
Subroutine make_grammar_content: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($class) = @_;
88 0 return undef;
89 }
90  
91  
92 21 ▶sub make_bless_package {
Subroutine make_bless_package: called CC=1 SCAR=0.0
Pod documented
93 21 my ($class) = @_;
94 21 return $class;
95 }
96  
97  
98 6 ▶sub grammar_option {
Subroutine grammar_option: called CC=1 SCAR=0.0
Pod documented
99 6 my ($self) = @_;
100 6 return $self->{_grammar_option};
101 }
102  
103  
104 6 ▶sub recce_option {
Subroutine recce_option: called CC=1 SCAR=0.0
Pod documented
105 6 my ($self) = @_;
106 6 return $self->{_recce_option};
107 }
108  
109  
110 21 ▶sub make_recce_option {
Subroutine make_recce_option: called CC=1 SCAR=0.0
Pod documented
111 21 my ($class, $spec) = @_;
112 21 return {ranking_method => $class->make_ranking_method,
113 semantics_package => $class->make_semantics_package,
114 too_many_earley_items => $class->make_too_many_earley_items};
115 }
116  
117  
118 21 ▶sub make_ranking_method {
Subroutine make_ranking_method: called CC=1 SCAR=0.0
Pod documented
119 21 my ($class) = @_;
120 21 return 'high_rule_only';
121 }
122  
123  
124 10 ▶sub make_semantics_package {
Subroutine make_semantics_package: called CC=1 SCAR=0.0
Pod documented
125 10 my ($class) = @_;
126 10 return join('::', __PACKAGE__, 'DefaultSemanticsPackage');
127 }
128  
129  
130 21 ▶sub make_too_many_earley_items {
Subroutine make_too_many_earley_items: called CC=1 SCAR=0.0
Pod documented
131 21 my ($class) = @_;
132 21 return 0;
133 }
134  
135  
136 0 ▶sub _callback {
Subroutine _callback: not called CC=7 SCAR=40.3
137 0 my ($self, $source, $pos, $max, $impl, $callbackp, $originalErrorString, @args) = @_;
138  
139 0 my $rc = $pos;
140  
141 0 eval {$rc = &$callbackp(@args, $source, $pos, $max, $impl)};
142 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
143 0 my $callbackErrorString = $@;
144 0 my $line_columnp;
145 0 eval {$line_columnp = lineAndCol($impl)};
146 0 my $context = _context($self, $impl);
147 #
148 # Now we can destroy the recognizer
149 #
150 0 $impl->destroy_R;
151 0 ▶ if (! $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 0 0
152 0 ▶ if (defined($originalErrorString) && $originalErrorString) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $originalErrorString and $originalErrorString) { } 0 0 0
Condition0%0/3
defined $originalErrorString and $originalErrorString
!ll&&!rl&&r
000
MC/DC0%0/2
defined $originalErrorString and $originalErrorString
defined $originalErrorString $originalErrorString
Truth table0%0/3
A and B
A= defined $originalErrorStringB= $originalErrorString
ABresult
0X0
100
111
153 0 SyntaxError(error => sprintf("%s\n%s\n\n%s%s", $originalErrorString, $callbackErrorString, showLineAndCol(@{$line_columnp}, $source), $context));
154 } else {
155 0 SyntaxError(error => sprintf("%s\n\n%s%s", $callbackErrorString, showLineAndCol(@{$line_columnp}, $source), $context));
156 }
157 } else {
158 0 ▶ if (defined($originalErrorString) && $originalErrorString) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $originalErrorString and $originalErrorString) { } 0 0 0
Condition0%0/3
defined $originalErrorString and $originalErrorString
!ll&&!rl&&r
000
MC/DC0%0/2
defined $originalErrorString and $originalErrorString
defined $originalErrorString $originalErrorString
Truth table0%0/3
A and B
A= defined $originalErrorStringB= $originalErrorString
ABresult
0X0
100
111
159 0 SyntaxError(error => sprintf("%s\n%s\n%s", $originalErrorString, $callbackErrorString, $context));
160 } else {
161 0 SyntaxError(error => sprintf("%s\n%s", $callbackErrorString, $context));
162 }
163 }
164 }
165  
166 0 return $rc;
167 }
168  
169 0 ▶sub parse {
Subroutine parse: not called CC=16 SCAR=56.1
Pod documented
170 0 my ($self, $source, $impl, $optionsp, $start, $length) = @_;
171  
172 0 ▶ $optionsp //= {};
Condition0%0/2
$optionsp //= {}
l!l
00
MC/DC0%0/1
$optionsp //= {}
$optionsp
Truth table0%0/2
A
A= $optionsp
Aresult
11
00
173 0 my $callbackp = $optionsp->{callback};
174 0 ▶ my $callbackargsp = $optionsp->{callbackargs} // [];
Condition0%0/2
$optionsp->{"callbackargs"} // []
l!l
00
MC/DC0%0/1
$optionsp->{"callbackargs"} // []
$optionsp->{"callbackargs"}
Truth table0%0/2
A
A= $optionsp->{"callbackargs"}
Aresult
11
00
175 0 my @callbackargs = @{$callbackargsp};
176 0 my $failurep = $optionsp->{failure};
177 0 ▶ my $failureargsp = $optionsp->{failureargs} // [];
Condition0%0/2
$optionsp->{"failureargs"} // []
l!l
00
MC/DC0%0/1
$optionsp->{"failureargs"} // []
$optionsp->{"failureargs"}
Truth table0%0/2
A
A= $optionsp->{"failureargs"}
Aresult
11
00
178 0 my @failureargs = @{$failureargsp};
179 0 my $endp = $optionsp->{end};
180 0 ▶ my $endargsp = $optionsp->{endargs} // [];
Condition0%0/2
$optionsp->{"endargs"} // []
l!l
00
MC/DC0%0/1
$optionsp->{"endargs"} // []
$optionsp->{"endargs"}
Truth table0%0/2
A
A= $optionsp->{"endargs"}
Aresult
11
00
181 0 my @endargs = @{$endargsp};
182  
183 0 ▶ $start //= 0;
Condition0%0/2
$start //= 0
l!l
00
MC/DC0%0/1
$start //= 0
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
184 0 ▶ $length //= -1;
Condition0%0/2
$length //= -1
l!l
00
MC/DC0%0/1
$length //= -1
$length
Truth table0%0/2
A
A= $length
Aresult
11
00
185  
186 0 my $sourceMaxPos = length($source) - 1;
187 0 ▶ if ($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
188 0 $start += $sourceMaxPos + 1;
189 }
190 0 ▶ my $max = ($length < 0) ? ($length + $sourceMaxPos + 1) : ($start + $length);
Branch: 1 branch
BranchTrueFalseTotal
$length < 0 ? : 0 0 0
191  
192 0 my $pos = $start;
193 0 my $stop;
194 my $newpos;
195  
196 #
197 # Create a recognizer
198 #
199 0 $impl->make_R;
200 #
201 # Lexer can fail
202 #
203 0 eval {$newpos = $impl->read(\$source, $pos, $length)};
204 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
205 #
206 # Failure callback
207 #
208 0 ▶ if (defined($failurep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $failurep) { } 0 0 0
209 0 $pos = _callback($self, $source, $pos, $max, $impl, $failurep, $@, @failureargs);
210 } else {
211 0 my $line_columnp = lineAndCol($impl);
212 0 my $context = _context($self, $impl);
213 0 $impl->destroy_R;
214 0 SyntaxError(error => sprintf("%s\n\n%s%s", $@, showLineAndCol(@{$line_columnp}, $source), $context));
215 }
216 } else {
217 0 $pos = $newpos;
218 }
219 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$pos <= $max and do { do { if (defined $callbackp) { $pos = _callback($self, $source, $pos, $max, $impl, $callbackp, undef, @callbackargs); } ; eval { do { $newpos = $impl->resume($pos) } }; if ($@) { if (defined $failurep) { $pos = _callback($self, $source, $pos, $max, $impl, $failurep, $@, @failureargs); } else { my $line_columnp = lineAndCol($impl); my $context = _context($self, $impl); $impl->destroy_R; SyntaxError("error", sprintf("%s\n\n%s%s", $@, showLineAndCol(@{$line_columnp;}, $source), $context)); } ; } else { $pos = $newpos; } } } 0 0 0
220 #
221 # Events
222 #
223 0 ▶ if (defined($callbackp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callbackp) 0 0 0
224 0 $pos = _callback($self, $source, $pos, $max, $impl, $callbackp, undef, @callbackargs);
225 }
226 #
227 # Lexer can fail
228 #
229 0 eval {$newpos = $impl->resume($pos)};
230 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
231 0 ▶ if (defined($failurep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $failurep) { } 0 0 0
232 #
233 # Failure callback
234 #
235 0 $pos = _callback($self, $source, $pos, $max, $impl, $failurep, $@, @failureargs);
236 } else {
237 0 my $line_columnp = lineAndCol($impl);
238 0 my $context = _context($self, $impl);
239 0 $impl->destroy_R;
240 0 SyntaxError(error => sprintf("%s\n\n%s%s", $@, showLineAndCol(@{$line_columnp}, $source), $context));
241 }
242 } else {
243 0 $pos = $newpos;
244 }
245 } while ($pos <= $max);
246  
247 0 ▶ if (defined($endp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $endp) 0 0 0
248 #
249 # End callback
250 #
251 0 _callback($self, $source, $pos, $max, $impl, $endp, undef, @endargs);
252 }
253  
254 0 return $self;
255 }
256  
257  
258 0 ▶sub value {
Subroutine value: not called CC=9 SCAR=45.0
Pod documented
259 0 my ($self, $impl, $optionsp) = @_;
260  
261 0 ▶ $optionsp //= {};
Condition0%0/2
$optionsp //= {}
l!l
00
MC/DC0%0/1
$optionsp //= {}
$optionsp
Truth table0%0/2
A
A= $optionsp
Aresult
11
00
262 0 my $traverserp = $optionsp->{traverser};
263 0 ▶ my $traverserscratchpadp = $optionsp->{traverserscratchpad} // {};
Condition0%0/2
$optionsp->{"traverserscratchpad"} // {}
l!l
00
MC/DC0%0/1
$optionsp->{"traverserscratchpad"} // {}
$optionsp->{"traverserscratchpad"}
Truth table0%0/2
A
A= $optionsp->{"traverserscratchpad"}
Aresult
11
00
264  
265 0 ▶ my $asf = defined($traverserp) ? Marpa::R2::ASF->new({slr => $impl->R}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $traverserp ? : 0 0 0
266 0 ▶ my $rc = (defined($asf) ? $asf->traverse($traverserscratchpadp, $traverserp) : $impl->value()) || do {
Branch: 1 branch
BranchTrueFalseTotal
defined $asf ? : 0 0 0
Condition0%0/3
(defined $asf ? $asf->traverse($traverserscratchpadp, $traverserp) : $impl->value) || do { my $lastExpression = _show_last_expression($self, $impl); $impl->destroy_R; InternalError('error', sprintf('%s', $lastExpression)) }
l!l&&r!l&&!r
000
MC/DC0%0/2
(defined $asf ? $asf->traverse($traverserscratchpadp, $traverserp) : $impl->value) || do { my $lastExpression = _show_last_expression($self, $impl); $impl->destroy_R; InternalError('error', sprintf('%s', $lastExpression)) }
(defined $asf ? $asf->traverse($traverserscratchpadp, $traverserp) : $impl->value) do { my $lastExpression = _show_last_expression($self, $impl); $impl->destroy_R; InternalError('error', sprintf('%s', $lastExpression)) }
Truth table0%0/3
A || B
A= (defined $asf ? $asf->traverse($traverserscratchpadp, $traverserp) : $impl->value)B= do { my $lastExpression = _show_last_expression($self, $impl); $impl->destroy_R; InternalError('error', sprintf('%s', $lastExpression)) }
ABresult
1X1
011
000
267 0 my $lastExpression = _show_last_expression($self, $impl);
268 0 $impl->destroy_R;
269 0 InternalError(error => sprintf('%s', $lastExpression))
270 };
271  
272 0 ▶ if (! defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
273 0 $impl->destroy_R;
274 0 InternalError(error => 'Undefined parse tree value');
275 }
276 0 ▶ if ((! defined($asf)) && defined(my $rc2 = $impl->value())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $asf and defined(my $rc2 = $impl->value)) 0 0 0
Condition0%0/3
not defined $asf and defined(my $rc2 = $impl->value)
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $asf and defined(my $rc2 = $impl->value)
not defined $asf defined(my $rc2 = $impl->value)
Truth table0%0/3
not(A) and B
A= not defined $asfB= defined(my $rc2 = $impl->value)
ABresult
0X0
100
111
277 0 $impl->destroy_R;
278 0 InternalError(error => 'More than one parse tree value');
279 }
280 0 $impl->destroy_R;
281  
282 0 return ${$rc};
283 }
284  
285 # ----------------------------------------------------------------------------------------
286  
287 0 ▶sub _context {
Subroutine _context: not called CC=2 SCAR=17.9
288 0 my ($self, $impl) = @_;
289  
290 0 ▶ my $context = $log->is_debug ?
Branch: 1 branch
BranchTrueFalseTotal
$MarpaX::Languages::ECMAScript::AST::Grammar::Base::log->is_debug ? : 0 0 0
291 sprintf("\n\nContext:\n\n%s", $impl->show_progress()) :
292 '';
293  
294 0 return $context;
295 }
296  
297  
298 # ----------------------------------------------------------------------------------------
299  
300 0 ▶sub getLexeme {
Subroutine getLexeme: not called CC=2 SCAR=17.9
Pod documented
301 0 my ($self, $lexemeHashp, $impl) = @_;
302  
303 0 my $rc = 0;
304 #
305 # Get paused lexeme
306 #
307 0 my $lexeme = $impl->pause_lexeme();
308 0 ▶ if (defined($lexeme)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lexeme) 0 0 0
309 0 $lexemeHashp->{name} = $lexeme;
310 0 ($lexemeHashp->{start}, $lexemeHashp->{length}) = $impl->pause_span();
311 0 ($lexemeHashp->{line}, $lexemeHashp->{column}) = $impl->line_column($lexemeHashp->{start});
312 0 $lexemeHashp->{value} = $impl->literal($lexemeHashp->{start}, $lexemeHashp->{length});
313 0 $rc = 1;
314 }
315  
316 0 return $rc;
317 }
318  
319 # ----------------------------------------------------------------------------------------
320  
321  
322 # ----------------------------------------------------------------------------------------
323  
324 0 ▶sub getLastLexeme {
Subroutine getLastLexeme: not called CC=2 SCAR=17.9
Pod documented
325 0 my ($self, $lexemeHashp, $impl) = @_;
326  
327 0 my $rc = 0;
328 #
329 # Get last lexeme span
330 #
331 0 my ($start, $length) = lastLexemeSpan($impl);
332 0 ▶ if (defined($start)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 0 0 0
333 0 ($lexemeHashp->{start}, $lexemeHashp->{length}) = ($start, $length);
334 0 $lexemeHashp->{value} = $impl->literal($lexemeHashp->{start}, $lexemeHashp->{length});
335 0 $rc = 1;
336 }
337  
338 0 return $rc;
339 }
340  
341 # ----------------------------------------------------------------------------------------
342  
343 0 ▶sub _show_last_expression {
Subroutine _show_last_expression: not called CC=2 SCAR=17.9
344 0 my ($self, $impl) = @_;
345  
346 0 my ($start, $end) = $impl->last_completed_range('SourceElement');
347 0 ▶ return 'No source element was successfully parsed' if (! defined($start));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 0 0
348 0 my $lastExpression = $impl->range_to_string($start, $end);
349 0 return "Last SourceElement successfully parsed was: $lastExpression";
350 }
351  
352  
353 16 1;
354  
355 __END__
« AST/Grammar/ECMAScript_262_5/Program.pm ↑ Summary AST/Grammar/ECMAScript_262_5/Lexical/StringLiteral/Semantics.pm »