M4/Impl/Parser.pm

filter: Statementstmt 95.2% 357 / 375 Branchbran 69.0% 98 / 142 Conditioncond 60.0% 36 / 60 MC/DCmcdc 38.8% 14 / 36 Subroutinesub 96.0% 48 / 50 Podpod 0.0% 0 / 1 totaltotal 83.2% 553 / 664 CC 93 SCAR 36.5
CC
93
Cov
85%
_parseByGrammar
72.1
_trigger__parse_level
48.0
_parseByTokens
8.4
CRAP 38.5
↑ Summary M4/Impl/Default.pm »
1 4 ▶use Moops;
Subroutine __ANON__: called CC=1 SCAR=0.0
2  
3 # PODNAME: MarpaX::Languages::M4::Impl::Parser
4  
5 # ABSTRACT: M4 Marpa parser
6  
7 #
8 # Expose only the base implementation of parse() method
9 # required by MarpaX::Languages::M4::Role::Parser
10 4 #
11 4 ▶class MarpaX::Languages::M4::Impl::Parser {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶ use MarpaX::Languages::M4::Impl::Parser::Actions;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 4 ▶ use MarpaX::Languages::M4::Type::Macro -all;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 ▶ use MarpaX::Languages::M4::Impl::Value;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 4 ▶ use Marpa::R2; # 2.103_004;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4 ▶ use Scalar::Util qw/readonly/;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 ▶ use Types::Common::Numeric -all;
Subroutine __ANON__: called CC=1 SCAR=0.0
18 #
19 # --nesting_limit (SHOULD) take care of that
20 #
21 4 ▶ no warnings 'recursion';
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 3 our $VERSION = '0.020'; # VERSION
24  
25 3 our $AUTHORITY = 'cpan:JDDPAUSE'; # AUTHORITY
26  
27 3 our $BASE_GRAMMAR = q{
28 inaccessible is ok by default
29 :default ::= action => ::first
30 lexeme default = latm => 1
31  
32 #
33 # Note: this part of the grammar is giving ambiguity
34 # but we do not mind because the effect is always
35 # the same: global concatenatation
36 #
37 argumentsGroup ::= tokens action => createArgumentsGroup rank => 1
38 | argumentsGroup LPAREN argumentsGroup RPAREN argumentsGroup action => mergeArgumentsGroup
39  
40 arguments ::= argumentsGroup action => firstArg
41 | arguments (COMMA) argumentsGroup action => nextArg
42  
43 tokens ::= token* action => create
44 event '^token' = predicted <token> # This event is to force $r->read() to not advance its location
45 event 'token$' = completed <token>
46 token ::= WORD
47 | macro
48 | QUOTEDSTRING
49 | CHARACTER
50 | COMMENT
51 | ANYTHING
52  
53 macro ::= MACRO # Never lexeme_read'ed
54 | MACRO_ARGS macroArguments # Ditto
55  
56 event 'macroArguments$' = completed <macroArguments>
57 macroArguments ::= (LPAREN) arguments (RPAREN)
58 macroArguments ::= (NOPARAM) action => fakeOneVoidParam
59  
60 _ANYTHING ~ [\s\S]
61 _NOTHING ~ [^\s\S]
62 WORD ~ _NOTHING
63 MACRO ~ _NOTHING
64 MACRO_ARGS ~ _NOTHING
65 QUOTEDSTRING ~ _NOTHING
66 CHARACTER ~ _NOTHING
67 COMMENT ~ _NOTHING
68 #
69 # ANYTHING will automatically eat a character each time we resume the SLIF: we
70 # do not want this to happen
71 #
72 :lexeme ~ <ANYTHING> pause => before event => '^ANYTHING'
73 ANYTHING ~ _ANYTHING
74  
75 _WS ~ [\s]
76 _WS_any ~ _WS*
77  
78 :lexeme ~ <LPAREN> priority => 1 pause => before event => '^LPAREN'
79 LPAREN ~ '(' _WS_any
80  
81 :lexeme ~ <RPAREN> priority => 1 pause => before event => '^RPAREN'
82 RPAREN ~ ')'
83  
84 :lexeme ~ <NOPARAM> priority => 1
85 NOPARAM ~ '(' _WS_any ')'
86  
87 #
88 # Quote from the spec (M4 POSIX):
89 # If a macro name is followed by a <left-parenthesis>, its arguments are the <comma>-separated tokens
90 # between the <left-parenthesis> and the matching <right-parenthesis>. Unquoted white-space characters
91 # preceding each argument shall be ignored. All other characters, including trailing white-space characters,
92 # are retained.
93 # <comma> characters enclosed between <left-parenthesis> and <right-parenthesis> characters do not delimit arguments.
94  
95 :lexeme ~ <COMMA> priority => 1
96 COMMA ~ ',' _WS_any
97 };
98  
99 3 our $BYTOKEN_G = do {
100 my $g = Marpa::R2::Scanless::G->new(
101 3 { source => \do {":start ::= tokens\n$BASE_GRAMMAR"}
102 }
103 );
104 };
105 3 our $BYMACROARGUMENTS_G = do {
106 my $g = Marpa::R2::Scanless::G->new(
107 3 { source => \do {":start ::= macroArguments\n$BASE_GRAMMAR"}
108 }
109 );
110 };
111  
112 BEGIN {
113 #
114 # Marpa internal optimisation: we do not want the closures to be rechecked every time
115 # we call $r->value(). This is a static information, although determined at run-time
116 # the first time $r->value() is called on a recognizer.
117 #
118 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
119  
120 2024 ▶ sub Marpa::R2::Recognizer::registrations {
Subroutine registrations: called CC=6 SCAR=19.9
Pod undocumented
121 2024 my $recce = shift;
122 2024 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2022 2 2024
123 2022 my $hash = shift;
124 2022 ▶ if (! defined($hash) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $hash or ref $hash ne "HASH" or grep {not exists $hash->{$_};} "NULL_VALUES", "REGISTRATIONS", "CLOSURE_BY_SYMBOL_ID", "CLOSURE_BY_RULE_ID", "RESOLVE_PACKAGE", "RESOLVE_PACKAGE_SOURCE", "PER_PARSE_CONSTRUCTOR") 0 2022 2022
Condition33%2/6
not defined $hash or ref $hash ne "HASH" or grep {not exists $hash->{$_};} "NULL_VALUES", "REGISTRATIONS", "CLOSURE_BY_SYMBOL_ID", "CLOSURE_BY_RULE_ID", "RESOLVE_PACKAGE", "RESOLVE_PACKAGE_SOURCE", "PER_PARSE_CONSTRUCTOR"
l!l&&r!l&&!r
002022
not defined $hash or ref $hash ne "HASH"
l!l&&r!l&&!r
002022
MC/DC0%0/3
not defined $hash or ref $hash ne "HASH" or grep {not exists $hash->{$_};} "NULL_VALUES", "REGISTRATIONS", "CLOSURE_BY_SYMBOL_ID", "CLOSURE_BY_RULE_ID", "RESOLVE_PACKAGE", "RESOLVE_PACKAGE_SOURCE", "PER_PARSE_CONSTRUCTOR"
not defined $hash ref $hash ne "HASH" grep {not exists $hash->{$_};} "NULL_VALUES", "REGISTRATIONS", "CLOSURE_BY_SYMBOL_ID", "CLOSURE_BY_RULE_ID", "RESOLVE_PACKAGE", "RESOLVE_PACKAGE_SOURCE", "PER_PARSE_CONSTRUCTOR"
Truth table25%1/4
not(A) or B or C
A= not defined $hashB= ref $hash ne "HASH"C= grep {not exists $hash->{$_};} "NULL_VALUES", "REGISTRATIONS", "CLOSURE_BY_SYMBOL_ID", "CLOSURE_BY_RULE_ID", "RESOLVE_PACKAGE", "RESOLVE_PACKAGE_SOURCE", "PER_PARSE_CONSTRUCTOR"
ABCresult
1XX1
01X1
0011
0000
125 ref($hash) ne 'HASH' ||
126 14154 grep {! exists($hash->{$_})} qw/
127 NULL_VALUES
128 REGISTRATIONS
129 CLOSURE_BY_SYMBOL_ID
130 CLOSURE_BY_RULE_ID
131 RESOLVE_PACKAGE
132 RESOLVE_PACKAGE_SOURCE
133 PER_PARSE_CONSTRUCTOR
134 /) {
135 Marpa::R2::exception(
136 "Attempt to reuse registrations failed:\n",
137 " Registration data is not a hash containing all necessary keys:\n",
138 0 ▶ " Got : " . ((ref($hash) eq 'HASH') ? join(', ', sort keys %{$hash}) : '') . "\n",
Branch: 1 branch
BranchTrueFalseTotal
ref $hash eq 'HASH' ? : 0 0 0
139 " Want: CLOSURE_BY_RULE_ID, CLOSURE_BY_SYMBOL_ID, NULL_VALUES, PER_PARSE_CONSTRUCTOR, REGISTRATIONS, RESOLVE_PACKAGE, RESOLVE_PACKAGE_SOURCE\n"
140 );
141 }
142 2022 $recce->[Marpa::R2::Internal::Recognizer::NULL_VALUES] = $hash->{NULL_VALUES};
143 2022 $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS] = $hash->{REGISTRATIONS};
144 2022 $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_SYMBOL_ID] = $hash->{CLOSURE_BY_SYMBOL_ID};
145 2022 $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_RULE_ID] = $hash->{CLOSURE_BY_RULE_ID};
146 2022 $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE] = $hash->{RESOLVE_PACKAGE};
147 2022 $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE] = $hash->{RESOLVE_PACKAGE_SOURCE};
148 2022 $recce->[Marpa::R2::Internal::Recognizer::PER_PARSE_CONSTRUCTOR] = $hash->{PER_PARSE_CONSTRUCTOR};
149 }
150 return {
151 2024 NULL_VALUES => $recce->[Marpa::R2::Internal::Recognizer::NULL_VALUES],
152 REGISTRATIONS => $recce->[Marpa::R2::Internal::Recognizer::REGISTRATIONS],
153 CLOSURE_BY_SYMBOL_ID => $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_SYMBOL_ID],
154 CLOSURE_BY_RULE_ID => $recce->[Marpa::R2::Internal::Recognizer::CLOSURE_BY_RULE_ID],
155 RESOLVE_PACKAGE => $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE],
156 RESOLVE_PACKAGE_SOURCE => $recce->[Marpa::R2::Internal::Recognizer::RESOLVE_PACKAGE_SOURCE],
157 PER_PARSE_CONSTRUCTOR => $recce->[Marpa::R2::Internal::Recognizer::PER_PARSE_CONSTRUCTOR]
158 };
159 } ## end sub registrations
160  
161 2024 ▶ sub Marpa::R2::Scanless::R::registrations {
Subroutine registrations: called CC=1 SCAR=0.0
162 2024 my $slr = shift;
163 2024 my $thick_g1_recce =
164 $slr->[Marpa::R2::Internal::Scanless::R::THICK_G1_RECCE];
165 2024 return $thick_g1_recce->registrations(@_);
166 } ## end sub Marpa::R2::Scanless::R::registrations
167  
168 }
169  
170 3 has _parse_level => (
171 is => 'rwp',
172 isa => PositiveOrZeroInt,
173  
174 # trigger => 1,
175 default => 0
176 );
177  
178 0 ▶ method _trigger__parse_level (PositiveOrZeroInt $parse_level, @rest) {
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine _trigger__parse_level: not called CC=9 SCAR=38.7
Branch: 6 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ < 1 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::PositiveOrZeroInt($parse_level) or _failed_check(48, "PositiveOrZeroInt", $parse_level, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 0 0
179 #
180 # GNU testing of nesting_limit is another implementation, this
181 # has no meaning for us. So, even if supported, this option does
182 # nothing: the trigger is NOT enabled.
183 #
184 0 my $nesting_limit = $self->impl_nestingLimit;
185 0 ▶ if ( $nesting_limit > 0 && $parse_level > $nesting_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nesting_limit > 0 and $parse_level > $nesting_limit) 0 0 0
Condition0%0/3
$nesting_limit > 0 and $parse_level > $nesting_limit
!ll&&!rl&&r
000
MC/DC0%0/2
$nesting_limit > 0 and $parse_level > $nesting_limit
$nesting_limit > 0 $parse_level > $nesting_limit
Truth table0%0/3
A and B
A= $nesting_limit > 0B= $parse_level > $nesting_limit
ABresult
0X0
100
111
186 0 $self->impl_raiseException(
187 sprintf( 'Nesting limit of %d exceeded', $nesting_limit ) );
188 }
189 }
190  
191 4 ▶ method parser_parse (Str $input --> Str) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine parser_parse: called CC=6 SCAR=18.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 164 0 164
if @_ != 1 0 164 164
$#_ >= 0 ? : 164 0 164
if ($#_ >= 0) 164 0 164
Type::Tiny::XS::Str($input) or _failed_check(9, "Str", $input, "exception_class", "Error::TypeTiny::Assertion") 164 0 164
192 #
193 # We will modify the buffer in-place, therefore we want to
194 # have OUR version of the input.
195 # In addition, this will solve the eventual problem of $input
196 # being a read-only variable
197 #
198 164 my $work = $input;
199 164 return $self->_parseByTokens( \$work );
200 }
201  
202 #
203 # By token => ConsumerOf['MarpaX::Languages::M4::Role::Value by default']
204 # By arguments => ArrayRef[ConsumerOf['MarpaX::Languages::M4::Role::Value']]
205 #
206 4 ▶ method _parseByGrammar (Ref['SCALAR'] $inputRef, PositiveOrZeroInt $pos, InstanceOf['Marpa::R2::Scanless::G'] $g, Undef|M4Macro $macro? --> Undef|Dict[pos => PositiveOrZeroInt, value => ConsumerOf['MarpaX::Languages::M4::Role::Value']]) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _parseByGrammar: called CC=67 SCAR=42.8
Branch: 15 branches
BranchTrueFalseTotal
@_ ? : 2018 0 2018
if @_ < 3 0 2018 2018
if @_ > 4 0 2018 2018
$#_ >= 0 ? : 2018 0 2018
if ($#_ >= 0) 2018 0 2018
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR" or _failed_check(98, "Ref[SCALAR]", $inputRef, "exception_class", "Error::TypeTiny::Assertion") 2018 0 2018
$#_ >= 1 ? : 2018 0 2018
if ($#_ >= 1) 2018 0 2018
Type::Tiny::XS::PositiveOrZeroInt($pos) or _failed_check(48, "PositiveOrZeroInt", $pos, "exception_class", "Error::TypeTiny::Assertion") 2018 0 2018
$#_ >= 2 ? : 2018 0 2018
if ($#_ >= 2) 2018 0 2018
Type::Tiny::XS::AUTO::TC3($g) or _failed_check(99, "InstanceOf[\"Marpa::R2::Scanless::G\"]", $g, "exception_class", "Error::TypeTiny::Assertion") 2018 0 2018
$#_ >= 3 ? : 1854 164 2018
if ($#_ >= 3) 1854 164 2018
not defined $macro or do { builtin::blessed($macro) and do { my $method = $macro->can("DOES") || $macro->can("isa"); $macro->$method("MarpaX::Languages::M4::Role::Macro") } } or _failed_check(100, "Undef|M4Macro", $macro, "exception_class", "Error::TypeTiny::Assertion") 1854 0 1854
Condition33%4/12
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR"
!ll&&!rl&&r
002018
not defined $macro or do { builtin::blessed($macro) and do { my $method = $macro->can("DOES") || $macro->can("isa"); $macro->$method("MarpaX::Languages::M4::Role::Macro") } }
l!l&&r!l&&!r
018540
builtin::blessed($macro) && do { my $method = $macro->can('DOES') || $macro->can('isa'); $macro->$method('MarpaX::Languages::M4::Role::Macro') }
!ll&&!rl&&r
001854
$macro->can('DOES') || $macro->can('isa')
l!l&&r!l&&!r
185400
MC/DC0%0/2
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR"
ref $inputRef builtin::reftype($inputRef) eq "SCALAR"
MC/DC0%0/2
not defined $macro or do { builtin::blessed($macro) and do { my $method = $macro->can("DOES") || $macro->can("isa"); $macro->$method("MarpaX::Languages::M4::Role::Macro") } }
not defined $macro do { builtin::blessed($macro) and do { my $method = $macro->can("DOES") || $macro->can("isa"); $macro->$method("MarpaX::Languages::M4::Role::Macro") } }
MC/DC0%0/2
builtin::blessed($macro) && do { my $method = $macro->can('DOES') || $macro->can('isa'); $macro->$method('MarpaX::Languages::M4::Role::Macro') }
builtin::blessed($macro) do { my $method = $macro->can('DOES') || $macro->can('isa'); $macro->$method('MarpaX::Languages::M4::Role::Macro') }
MC/DC0%0/2
$macro->can('DOES') || $macro->can('isa')
$macro->can('DOES') $macro->can('isa')
Truth table33%1/3
A and B
A= ref $inputRefB= builtin::reftype($inputRef) eq "SCALAR"
ABresult
0X0
100
111
Truth table33%1/3
not(A) or B
A= not defined $macroB= do { builtin::blessed($macro) and do { my $method = $macro->can("DOES") || $macro->can("isa"); $macro->$method("MarpaX::Languages::M4::Role::Macro") } }
ABresult
1X1
011
000
Truth table33%1/3
A && B
A= builtin::blessed($macro)B= do { my $method = $macro->can('DOES') || $macro->can('isa'); $macro->$method('MarpaX::Languages::M4::Role::Macro') }
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $macro->can('DOES')B= $macro->can('isa')
ABresult
1X1
011
000
207  
208 2018 my $maxPos = length( ${$inputRef} ) - 1;
209 #
210 # Protect the case of empty string.
211 # This is affecting all parsing levels.
212 #
213 2018 ▶ if ( $pos > $maxPos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $maxPos) 1 2017 2018
214 1 return;
215 }
216 #
217 # Get the lexemes ordering
218 #
219 2017 my @lexemeNames = $self->parser_tokensPriority();
220 #
221 # In the context of a macroArguments, unquoted parenthesis
222 # have higher priorities over everything, except quoted string
223 # and comment.
224 #
225 2017 ▶ if ( $g == $BYMACROARGUMENTS_G ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G) 1854 163 2017
226 1854 unshift( @lexemeNames, 'NOPARAM', 'LPAREN', 'RPAREN', 'COMMA' );
227 }
228  
229 2017 my $value = MarpaX::Languages::M4::Impl::Value->new();
230 2017 my %rc = ( pos => $pos, value => $value );
231  
232 #
233 # Instanciate and start recognizer
234 #
235 2017 my $r = Marpa::R2::Scanless::R->new(
236 { grammar => $g,
237 semantics_package => 'MarpaX::Languages::M4::Impl::Parser::Actions',
238 exhaustion => 'event',
239  
240 # trace_terminals => 1,
241 # trace_values => 1
242 }
243 );
244 #
245 # $prevPos is used only for logging
246 #
247 2017 my $prevPos = $rc{pos} = $pos;
248 #
249 # The stream itself is of no importance.
250 # We use Marpa to drive the context, nothing more.
251 # But is it NOT a hasard that I use the constant '(':
252 # when doing a recursive call using
253 # the macroByarguments grammar, Marpa will expect this
254 # character at the first read, EVEN
255 # if we paused before it.
256 #
257 2017 $r->read( \'(' );
258  
259 again:
260 4440 ▶ while ( $rc{pos} <= $maxPos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc{"pos"} <= $maxPos) 23409 187 23596
261  
262 # $self->logger_debug( '[%d..%d/%d] 20 first characters: %s',
263 # $rc{pos}, $rc{pos}, $maxPos,
264 # substr( ${$inputRef}, $rc{pos}, 20 ) );
265  
266 23409 my %expected = map { $_ => 1 } @{ $r->terminals_expected };
267  
268 # $self->logger_debug(
269 # '[%d..%d/%d] Expected terminals: %s',
270 # $rc{pos}, $rc{pos}, $maxPos,
271 # [ keys %expected ]
272 # );
273  
274 23409 ▶ if ( $r->exhausted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->exhausted) 1823 21586 23409
275  
276 # $self->logger_debug( '[%d..%d/%d] Parse is exhausted',
277 # $rc{pos}, $rc{pos}, $maxPos );
278  
279 1823 last;
280 }
281  
282 21586 my $lexemeValue;
283 my $lexemeLength;
284 21586 my $lexeme;
285  
286 21586 my $QuotedstringValue;
287 21586 my $QuotedstringLength;
288 21586 my $isQuotedString = false;
289  
290 21586 my $CommentValue;
291 my $CommentLength;
292 21586 my $isComment = false;
293  
294 21586 my $canCollectArguments = true;
295 21586 ▶ if ( $g == $BYMACROARGUMENTS_G ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G) 16948 4638 21586
296 16948 my $blockOk = false;
297 16948 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
298 $isQuotedString
299 16948 = $self->parser_isQuotedstring( ${$inputRef},
300 $rc{pos},
301 16948 $maxPos, \$QuotedstringValue, \$QuotedstringLength );
302 $isComment
303 16948 = $self->parser_isComment( ${$inputRef}, $rc{pos},
304 16948 $maxPos, \$CommentValue, \$CommentLength );
305 16948 ▶ $canCollectArguments = !$isQuotedString && !$isComment;
Condition100%3/3
$isQuotedString || $isComment
l!l&&r!l&&!r
4068512875
MC/DC100%2/2
$isQuotedString || $isComment
$isQuotedString $isComment
Truth table100%3/3
A || B
A= $isQuotedStringB= $isComment
ABresult
1X1
011
000
306 16948 $blockOk = true;
307 16948 };
308 16948 ▶ if ( !$blockOk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blockOk) 0 16948 16948
309 0 goto return_error;
310 }
311 }
312  
313 21586 foreach (@lexemeNames) {
314 112029 ▶ if ( $_ eq 'NOPARAM' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($_ eq 'NOPARAM') { } 16948 95081 112029
elsif ($_ eq 'LPAREN') { } 16933 78148 95081
elsif ($_ eq 'RPAREN') { } 15063 63085 78148
elsif ($_ eq 'COMMA') { } 13194 49891 63085
315 16948 ▶ if ( exists( $expected{NOPARAM} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected{"NOPARAM"} and $canCollectArguments) 1854 15094 16948
Condition66%2/3
exists $expected{"NOPARAM"} and $canCollectArguments
!ll&&!rl&&r
1509401854
MC/DC50%1/2
exists $expected{"NOPARAM"} and $canCollectArguments
exists $expected{"NOPARAM"} $canCollectArguments
Truth table66%2/3
A and B
A= exists $expected{"NOPARAM"}B= $canCollectArguments
ABresult
0X0
100
111
316 && $canCollectArguments )
317 {
318 1854 pos( ${$inputRef} ) = $rc{pos};
319 1854 ▶ if ( ${$inputRef} =~ /\G\(\s*\)/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$inputRef;} =~ /\G\(\s*\)/su) 15 1839 1854
320 15 $lexeme = 'NOPARAM';
321 15 $lexemeValue = substr( ${$inputRef}, $-[0],
322 $+[0] - $-[0] );
323 15 $lexemeLength = $+[0] - $-[0];
324 15 last;
325 }
326 }
327 }
328 elsif ( $_ eq 'LPAREN' ) {
329 16933 ▶ if ( exists( $expected{LPAREN} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected{"LPAREN"} and $canCollectArguments and do { pos ${$inputRef;} = $rc{"pos"}; 1 } and ${$inputRef;} =~ /\G\(\s*/u) 1870 15063 16933
Condition77%7/9
exists $expected{"LPAREN"} and $canCollectArguments and do { pos ${$inputRef;} = $rc{"pos"}; 1 } and ${$inputRef;} =~ /\G\(\s*/u
!ll&&!rl&&r
4073109901870
exists $expected{"LPAREN"} and $canCollectArguments and do { pos ${$inputRef;} = $rc{"pos"}; 1 }
!ll&&!rl&&r
4073012860
exists $expected{"LPAREN"} and $canCollectArguments
!ll&&!rl&&r
0407312860
MC/DC50%2/4
exists $expected{"LPAREN"} and $canCollectArguments and do { pos ${$inputRef;} = $rc{"pos"}; 1 } and ${$inputRef;} =~ /\G\(\s*/u
exists $expected{"LPAREN"} $canCollectArguments do { pos ${$inputRef;} = $rc{"pos"}; 1 } ${$inputRef;} =~ /\G\(\s*/u
Truth table60%3/5
A and B and C and D
A= exists $expected{"LPAREN"}B= $canCollectArgumentsC= do { pos ${$inputRef;} = $rc{"pos"}; 1 }D= ${$inputRef;} =~ /\G\(\s*/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
330 && $canCollectArguments
331 12860 && do { pos( ${$inputRef} ) = $rc{pos}; 1 }
332 12860 && ${$inputRef} =~ /\G\(\s*/
333 )
334 {
335 1870 $lexeme = 'LPAREN';
336 1870 $lexemeLength = $+[0] - $-[0];
337 $lexemeValue
338 1870 = substr( ${$inputRef}, $-[0], $lexemeLength );
339 1870 last;
340 }
341 }
342 elsif ( $_ eq 'RPAREN' ) {
343 15063 ▶ if ( exists( $expected{RPAREN} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected{"RPAREN"} and $canCollectArguments and substr(${$inputRef;}, $rc{"pos"}, 1) eq ")") 1869 13194 15063
Condition83%5/6
exists $expected{"RPAREN"} and $canCollectArguments and substr(${$inputRef;}, $rc{"pos"}, 1) eq ")"
!ll&&!rl&&r
407391211869
exists $expected{"RPAREN"} and $canCollectArguments
!ll&&!rl&&r
0407310990
MC/DC66%2/3
exists $expected{"RPAREN"} and $canCollectArguments and substr(${$inputRef;}, $rc{"pos"}, 1) eq ")"
exists $expected{"RPAREN"} $canCollectArguments substr(${$inputRef;}, $rc{"pos"}, 1) eq ")"
Truth table75%3/4
A and B and C
A= exists $expected{"RPAREN"}B= $canCollectArgumentsC= substr(${$inputRef;}, $rc{"pos"}, 1) eq ")"
ABCresult
0XX0
10X0
1100
1111
344 && $canCollectArguments
345 10990 && substr( ${$inputRef}, $rc{pos}, 1 ) eq ')' )
346 {
347 1869 $lexeme = 'RPAREN';
348 1869 $lexemeValue = ')';
349 1869 $lexemeLength = 1;
350 1869 last;
351 }
352 }
353 elsif ( $_ eq 'COMMA' ) {
354 13194 ▶ if ( exists( $expected{COMMA} ) && $canCollectArguments )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected{"COMMA"} and $canCollectArguments) 9025 4169 13194
Condition100%3/3
exists $expected{"COMMA"} and $canCollectArguments
!ll&&!rl&&r
11840519025
MC/DC100%2/2
exists $expected{"COMMA"} and $canCollectArguments
exists $expected{"COMMA"} $canCollectArguments
Truth table100%3/3
A and B
A= exists $expected{"COMMA"}B= $canCollectArguments
ABresult
0X0
100
111
355 {
356 9025 pos( ${$inputRef} ) = $rc{pos};
357 9025 ▶ if ( ${$inputRef} =~ /\G,\s*/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$inputRef;} =~ /\G,\s*/su) 2666 6359 9025
358 2666 $lexeme = 'COMMA';
359 2666 $lexemeValue = substr( ${$inputRef}, $-[0],
360 $+[0] - $-[0] );
361 2666 $lexemeLength = $+[0] - $-[0];
362 2666 last;
363 }
364 }
365 }
366 else {
367 49891 ▶ if ( $g == $BYMACROARGUMENTS_G && $_ eq 'QUOTEDSTRING' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq 'QUOTEDSTRING') { } 9654 40237 49891
elsif ($g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq 'COMMENT') { } 10528 29709 40237
Condition100%3/3
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq "QUOTEDSTRING"
!ll&&!rl&&r
13600266379654
MC/DC100%2/2
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq "QUOTEDSTRING"
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G $_ eq "QUOTEDSTRING"
Truth table100%3/3
A and B
A= $g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_GB= $_ eq "QUOTEDSTRING"
ABresult
0X0
100
111
368 #
369 # Already done in the context of macro arguments grammar
370 #
371 9654 ▶ if ($isQuotedString) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isQuotedString) 4068 5586 9654
372 4068 $lexeme = $_;
373 4068 $lexemeValue = $QuotedstringValue;
374 4068 $lexemeLength = $QuotedstringLength;
375 4068 last;
376 }
377 }
378 ▶ elsif ( $g == $BYMACROARGUMENTS_G && $_ eq 'COMMENT' ) {
Condition100%3/3
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq "COMMENT"
!ll&&!rl&&r
136001610910528
MC/DC100%2/2
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G and $_ eq "COMMENT"
$g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_G $_ eq "COMMENT"
Truth table100%3/3
A and B
A= $g == $MarpaX::Languages::M4::Impl::Parser::BYMACROARGUMENTS_GB= $_ eq "COMMENT"
ABresult
0X0
100
111
379 #
380 # Already done in the context of macro arguments grammar
381 #
382 10528 ▶ if ($isComment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isComment) 5 10523 10528
383 5 $lexeme = $_;
384 5 $lexemeValue = $CommentValue;
385 5 $lexemeLength = $CommentLength;
386 5 last;
387 }
388 }
389 else {
390 #
391 # QUOTEDSTRING is not already done if not
392 # in the context of macro arguments grammar
393 # Ditto for COMMENT.
394 #
395 29709 my $method = 'parser_is' . ucfirst( lc($_) );
396 29709 my $blockOk = false;
397 29709 my $isToken;
398 29709 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
399 29709 $isToken = $self->$method( ${$inputRef}, $rc{pos},
400 29709 $maxPos, \$lexemeValue, \$lexemeLength );
401 29705 $blockOk = true;
402 29709 };
403 29709 ▶ if ( !$blockOk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blockOk) 4 29705 29709
404 4 goto return_error;
405 }
406 29705 ▶ if ($isToken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isToken) 11089 18616 29705
407 11089 $lexeme = $_;
408 11089 last;
409 }
410 }
411 }
412 }
413 #
414 # Nothing ?
415 #
416 21582 ▶ if ( Undef->check($lexeme) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Undef()->check($lexeme)) 0 21582 21582
417 0 goto return_error;
418 }
419 #
420 # If it is a word, check if this is eventually a macro
421 #
422 21582 ▶ if ( $lexeme eq 'WORD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lexeme eq "WORD") 3089 18493 21582
423 3089 my $thisMacro;
424 my $lparenPos;
425 3089 ▶ if ($self->parser_isMacro(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parser_isMacro(${$inputRef;}, $rc{'pos'}, $maxPos, $lexemeValue, $lexemeLength, \$thisMacro, \$lparenPos)) { } 2441 648 3089
426 3089 ${$inputRef}, $rc{pos}, $maxPos,
427 $lexemeValue, $lexemeLength, \$thisMacro,
428 \$lparenPos
429 )
430 )
431 {
432 2441 my $canTrace = $self->_canTrace($thisMacro);
433 2441 my $macroCallId
434 = $self->impl_macroExecuteHeader( $thisMacro,
435 $canTrace );
436 #
437 # Collect macro arguments
438 #
439 2441 my @args = ();
440  
441 2441 ▶ if ( $lparenPos >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lparenPos >= 0) 1854 587 2441
442  
443 1854 $self->_set__parse_level( $self->_parse_level + 1 );
444 1854 my $dict = $self->_parseByGrammar(
445 $inputRef, $lparenPos,
446 $BYMACROARGUMENTS_G, $thisMacro
447 );
448 1853 $self->_set__parse_level( $self->_parse_level - 1 );
449 1853 ▶ if ( Undef->check($dict) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Undef()->check($dict)) 0 1853 1853
450 0 goto return_error;
451 }
452 1853 @args = $dict->{value}->value_elements;
453 1853 $lexemeLength = $dict->{pos} - $rc{pos};
454 }
455 #
456 # It is the reponsability of implementation to make sure
457 # that a macro never croaks.
458 #
459 $lexemeValue
460 2440 = $self->impl_macroExecuteNoHeader( $thisMacro,
461 $macroCallId, $canTrace, @args );
462 #
463 # Eventual postmatch length
464 #
465 $lexemeLength
466 += $thisMacro->macro_postMatchLengthExecute( $self,
467 2438 ${$inputRef}, $rc{pos} + $lexemeLength, $maxPos );
468 #
469 # Input is changing
470 #
471 2438 ▶ if ( M4Macro->check($lexemeValue) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&M4Macro()->check($lexemeValue)) { } 15 2423 2438
472 #
473 # Protect the case of M4Macro
474 #
475 15 $lexeme = 'ANYTHING';
476 }
477 else {
478 # $self->logger_debug(
479 # '[%d..%d/%d] Input is changing: replace %s by %s',
480 # $rc{pos},
481 # $rc{pos},
482 # $maxPos,
483 # substr(
484 # ${$inputRef}, $rc{pos}, $lexemeLength
485 # ),
486 # $lexemeValue
487 # );
488 substr(
489 2423 ${$inputRef}, $rc{pos},
490 2423 $lexemeLength, $lexemeValue
491 );
492  
493 # $self->logger_debug( '[%d..%d/%d] New input: %s',
494 # $rc{pos}, $rc{pos}, $maxPos, ${$inputRef} );
495  
496 2423 $maxPos = length( ${$inputRef} ) - 1;
497 2423 goto again;
498 }
499 }
500 else {
501 # $self->logger_debug(
502 # '[%d..%d/%d] %s is not an acceptable macro call',
503 # $rc{pos}, $rc{pos}, $maxPos, $lexemeValue );
504 }
505 }
506 #
507 # When _parse_level is zero, the current token has been fully
508 # parsed, and its output is to
509 # to be immediately "flushed".
510 # We do not need to do a lexeme_read(), nor a resume in this case:
511 # we know where we are.
512 #
513 19156 ▶ if ( $self->_parse_level > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_level > 0) { } 16277 2879 19156
514 16277 $r->lexeme_read( $lexeme, 0, 1, $lexemeValue );
515 16277 $prevPos = $rc{pos};
516 16277 $rc{pos} += $lexemeLength;
517  
518 # $self->logger_debug( '[%d->%d/%d] %s: %s',
519 # $prevPos, $rc{pos}, $maxPos, $lexeme, $lexemeValue );
520  
521 #
522 # We can safely ignore the events from lexeme_read(),
523 # because we made sure in the grammar that resume() will
524 # NOT advance the position, thanks to those events:
525 #
526 # ^token
527 # ^ANYTHING
528 # macroArguments$
529 # token$
530 #
531 # macroArguments$, if it happens will always be standalone.
532 # token$, when it happen, can be mixed with ^token or
533 # ^ANYTHING.
534 #
535 # Please note that we do not use resume().
536 #
537 }
538 else {
539 #
540 # We are at the top level: flush to current diversion
541 #
542 # $self->logger_debug( '[%d->%d/%d] %s: %s',
543 # $prevPos, $rc{pos}, $maxPos, $lexeme, $lexemeValue );
544  
545 2879 my $tmpValue = MarpaX::Languages::M4::Impl::Value->new()
546 ->value_push($lexemeValue);
547 2879 $self->impl_appendValue(
548 $tmpValue->value_concat->value_firstElement );
549 2879 $prevPos = $rc{pos};
550 2879 $rc{pos} += $lexemeLength;
551 }
552 }
553  
554 2010 ▶ if ( $self->_parse_level > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_level > 0) { } 1856 154 2010
555 #
556 # We are in the context of a recursive call: the output
557 # is of concern for a macro that called us.
558 #
559 1856 local $MarpaX::Languages::M4::Impl::Parser::macro = $macro;
560 #
561 # For $r->value() optimisations
562 #
563 1856 state $registrations = undef;
564  
565 1856 ▶ if (defined($registrations)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $registrations) 1855 1 1856
566 1855 $r->registrations($registrations);
567 }
568 1856 my $valueRef = $r->value;
569 1856 ▶ if (! defined($registrations)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $registrations) 1 1855 1856
570 1 $registrations = $r->registrations();
571 }
572 1856 ▶ if ( Undef->check($valueRef) || Undef->check( ${$valueRef} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Undef()->check($valueRef) or &Undef()->check(${$valueRef;})) 1 1855 1856
Condition66%2/3
&Undef()->check($valueRef) or &Undef()->check(${$valueRef;})
l!l&&r!l&&!r
101855
MC/DC50%1/2
&Undef()->check($valueRef) or &Undef()->check(${$valueRef;})
&Undef()->check($valueRef) &Undef()->check(${$valueRef;})
Truth table66%2/3
A or B
A= &Undef()->check($valueRef)B= &Undef()->check(${$valueRef;})
ABresult
1X1
011
000
573 1 goto return_error;
574 }
575  
576 1855 $rc{value} = ${$valueRef};
577 }
578 else {
579 #
580 # We are at the top level: the output has already been
581 # "flushed" to whatever the current diversion said to.
582 #
583 }
584  
585 # $self->logger_debug( '[%d..%d/%d] => %s',
586 # $rc{pos}, $rc{pos}, $maxPos, \%rc );
587  
588 2009 return \%rc;
589  
590 return_error:
591 #
592 # We propagate the undef to all levels except number 0
593 #
594 5 ▶ if ( $self->_parse_level > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_level > 0) { } 1 4 5
595 1 ▶ if ( $self->_parse_level == 1 && $self->impl_eoi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_level == 1 and $self->impl_eoi) 1 0 1
Condition33%1/3
$self->_parse_level == 1 and $self->impl_eoi
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_parse_level == 1 and $self->impl_eoi
$self->_parse_level == 1 $self->impl_eoi
Truth table33%1/3
A and B
A= $self->_parse_level == 1B= $self->impl_eoi
ABresult
0X0
100
111
596 #
597 # We want the parsing to stop now
598 #
599 1 $self->impl_raiseException('EOF during argument collection');
600 }
601 0 return;
602 }
603 else {
604 4 return \%rc;
605 }
606 }
607  
608 #
609 # M4 says that a token is processed as soon as it is recognized.
610 # So we loop on token recognition
611 #
612 4 ▶ method _parseByTokens (Ref['SCALAR'] $inputRef --> Str) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _parseByTokens: called CC=8 SCAR=21.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 164 0 164
if @_ != 1 0 164 164
$#_ >= 0 ? : 164 0 164
if ($#_ >= 0) 164 0 164
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR" or _failed_check(98, "Ref[SCALAR]", $inputRef, "exception_class", "Error::TypeTiny::Assertion") 164 0 164
Condition33%1/3
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR"
!ll&&!rl&&r
00164
MC/DC0%0/2
ref $inputRef and builtin::reftype($inputRef) eq "SCALAR"
ref $inputRef builtin::reftype($inputRef) eq "SCALAR"
Truth table33%1/3
A and B
A= ref $inputRefB= builtin::reftype($inputRef) eq "SCALAR"
ABresult
0X0
100
111
613  
614 164 my $rc = $self->_parseByGrammar( $inputRef, 0, $BYTOKEN_G );
615 161 ▶ if ( !Undef->check($rc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Undef()->check($rc)) 160 1 161
616 160 return substr( ${$inputRef}, $rc->{pos} );
617 }
618  
619 1 return ${$inputRef};
620 }
621  
622 }
623  
624 4 1;
625  
626 __END__
↑ Summary M4/Impl/Default.pm »