blib/lib/MarpaX/Hoonlint/yahc.pm

filter: Statementstmt 36.3% 120 / 330 Branchbran 12.7% 13 / 102 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 68.1% 15 / 22 Podpod n/a totaltotal 31.8% 151 / 474 CC 57 SCAR 46.6
CC
57
Cov
31%
read
279.2
prune
137.5
getCram
56.0
CRAP 105.9
« blib/lib/MarpaX/Hoonlint/Policy/Test/Whitespace.pm ↑ Summary blib/lib/MarpaX/Hoonlint.pm »
1 7 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Marpa::R2 6.000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # This code uses as its grammar reference the code in
10 # the arvo repo: https://github.com/urbit/arvo
11 # File sys/hoon.hoon: https://github.com/urbit/arvo/blob/master/sys/hoon.hoon
12 # as of commit 7dc3eb1cfacaaafd917697a544bdcf7f22e09eeb
13  
14 package MarpaX::Hoonlint::YAHC;
15  
16 7 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub deprecated {
Subroutine deprecated: not called CC=1 SCAR=6.9
19 0 my $slg = $Marpa::R2::Context::slg;
20 0 my $rule_id = $Marpa::R2::Context::rule;
21 0 my ($lhs_id) = $slg->rule_expand($rule_id);
22 0 return [ 'deprecated', $slg->symbol_display_form($lhs_id) ];
23 }
24  
25  
26 # === Automatically generated Marpa rules ===
27  
28 # Here is meta-programming to write piece 2
29  
30 # ace and gap are not really char names,
31 # and are omitted
32 7 my %glyphs = (
33 bar => '|',
34 bas => '\x5c', # '\'
35 buc => '$',
36 cab => '_',
37 cen => '%',
38 col => ':',
39 com => ',',
40 doq => '"',
41 dot => '.',
42 fas => '/',
43 gal => '<',
44 gar => '>',
45 hax => '#',
46 hep => '-',
47 kel => '{',
48 ker => '}',
49 ket => '\\^',
50 lus => '+',
51 pal => '(',
52 pam => '&',
53 par => ')',
54 pat => '@',
55 pel => '(',
56 per => ')',
57 sel => '\x5b', # '['
58 sem => ';',
59 ser => '\x5d', # ']'
60 sig => '~',
61 soq => '\'',
62 tar => '*',
63 tec => '`',
64 tis => '=',
65 wut => '?',
66 zap => '!',
67 );
68  
69 7 my @glyphRules = ();
70 7 for my $glyphName (sort keys %glyphs) {
71 238 my $glyph = $glyphs{$glyphName};
72 238 my $ucGlyphName = uc $glyphName;
73 238 my $uc4hGlyphName = $ucGlyphName . '4H';
74 238 my $lcGlyphName = $glyphName . '4h';
75 238 push @glyphRules, "$ucGlyphName ~ $lcGlyphName";
76 238 push @glyphRules, "$uc4hGlyphName ~ $lcGlyphName";
77 238 push @glyphRules, "$lcGlyphName ~ [" . $glyph . q{]};
78 238 push @glyphRules, "inaccessible_ok ::= $ucGlyphName";
79 238 push @glyphRules, "inaccessible_ok ::= $uc4hGlyphName";
80 }
81 7 my $glyphAutoRules = join "\n", @glyphRules;
82  
83 7 my $mainDSL = do { $RS = undef; <DATA> };
84  
85 7 my @dslAutoRules = ();
86 7 DESC: for my $desc (split "\n", $mainDSL) {
87 13447 my $originalDesc = $desc;
88 13447 chomp $desc; # remove newline
89 13447 ▶ next DESC if not $desc =~ s/^[#] FIXED: //;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc =~ s/^[#] FIXED: // 12957 490 13447
90 490 $desc =~ s/^\s+//; # eliminate leading spaces
91 490 $desc =~ s/\s+$//; # eliminate trailing spaces
92 490 my ($rune, @samples) = split /\s+/, $desc;
93 490 ▶ die $originalDesc if not $rune;
Branch: 1 branch
BranchTrueFalseTotal
unless $rune 0 490 490
94 490 push @dslAutoRules, doFixedRune( $rune, @samples );
95 }
96 7 my $dslAutoRules = join "\n", @dslAutoRules;
97  
98 # Assemble the base BSL
99 7 my $baseDSL = join "\n", $mainDSL, $glyphAutoRules, $dslAutoRules;
100  
101 7 my $defaultSemantics = <<'EOS';
102 # start and length will be needed for production
103 # :default ::= action => [name,start,length,values]
104 :default ::= action => [name,values]
105 lexeme default = latm => 1
106 EOS
107  
108 0 ▶sub divergence {
Subroutine divergence: not called CC=1 SCAR=6.9
109 0 die join '', 'Unrecoverable internal error: ', @_;
110 }
111  
112 # Given an input and an offset into that input,
113 # it reads a triple quote ('''). The return values
114 # are the parse value and a new offset in the input.
115 # Errors are thrown.
116  
117 0 ▶sub getTripleQuote {
Subroutine getTripleQuote: not called CC=5 SCAR=34.0
118 0 my ( $input, $offset ) = @_;
119 0 my $input_length = length ${$input};
120 0 my $resume_pos;
121 my $this_pos;
122  
123 0 my $nextNL = index ${$input}, "\n", $offset;
124 0 ▶ if ($nextNL < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextNL < 0) 0 0 0
125 0 die join '', 'Newline missing after triple quotes: "', ${$input}, '"'
126 }
127 0 my $initiator = substr ${$input}, $offset, $nextNL-$offset;
128 0 ▶ if ($initiator ne "'''" and $initiator !~ m/^''' *::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initiator ne "'''" and not $initiator =~ /^''' *::/) 0 0 0
Condition0%0/3
$initiator ne "'''" and not $initiator =~ /^''' *::/
!ll&&!rl&&r
000
MC/DC0%0/2
$initiator ne "'''" and not $initiator =~ /^''' *::/
$initiator ne "'''" not $initiator =~ /^''' *::/
Truth table0%0/3
A and not(B)
A= $initiator ne "'''"B= not $initiator =~ /^''' *::/
ABresult
0X0
100
111
129 0 die join '', 'Disallowed characters after initial triple quotes: "', $initiator, '"'
130 }
131  
132 0 pos ${$input} = $offset;
133 0 my ($indent) = ${$input} =~ /\G( *)[^ ]/g;
134 0 my $terminator = $indent . "'''";
135  
136 0 my $terminatorPos = index ${$input}, $terminator, $nextNL;
137 0 my $value = substr ${$input}, $nextNL+1, ($terminatorPos - $nextNL);
138  
139 0 ▶ say STDERR "Left main READ loop" if $MarpaX::Hoonlint::YAHC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
140  
141 # Return ref to value and new offset
142 0 return \$value, $terminatorPos + length $terminator;
143 }
144  
145 # Given an input and an offset into that input,
146 # it reads a triple double quote ("""). The return values
147 # are the parse value and a new offset in the input.
148 # Errors are thrown.
149  
150 # TODO: Needs to implement reading of sump(5d)
151  
152 0 ▶sub getTripleDoubleQuote {
Subroutine getTripleDoubleQuote: not called CC=4 SCAR=30.0
153 0 my ( $input, $offset ) = @_;
154 0 my $input_length = length ${$input};
155 0 my $resume_pos;
156 my $this_pos;
157  
158 0 my $nextNL = index ${$input}, "\n", $offset;
159 0 ▶ if ($nextNL < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextNL < 0) 0 0 0
160 die join '', 'Newline missing after triple double quotes: "',
161 0 ${$input}, '"'
162 }
163 0 my $initiator = substr ${$input}, $offset, $nextNL-$offset;
164 0 ▶ if ($initiator ne q{"""}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initiator ne "\"\"\"") 0 0 0
165 0 die join '',
166 'Disallowed characters after initial triple double quotes: "', $initiator, '"'
167 }
168  
169 0 pos ${$input} = $offset;
170 0 my ($indent) = ${$input} =~ /\G( *)[^ ]/g;
171 0 my $terminator = $indent . q{"""};
172  
173 0 my $terminatorPos = index ${$input}, $terminator, $nextNL;
174 0 my $value = substr ${$input}, $nextNL+1, ($terminatorPos - $nextNL);
175  
176 0 ▶ say STDERR "Left main READ loop" if $MarpaX::Hoonlint::YAHC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
177  
178 # Return ref to value and new offset
179 0 return \$value, $terminatorPos + length $terminator;
180 }
181  
182 # Given an input and an offset into that input,
183 # it reads unmarkdown. The return values
184 # are the parse value and a new offset in the input.
185 # Reading is not intelligent -- it finds a terminator, and
186 # treats the unmarkdown as a string.
187 # Errors are thrown.
188  
189 0 ▶sub getCram {
Subroutine getCram: not called CC=7 SCAR=40.3
190 # $DB::single = 1;
191  
192 0 my ( $input, $origOffset ) = @_;
193 0 my $input_length = length ${$input};
194 0 my $resume_pos;
195 my $this_pos;
196  
197 0 my $semiPos = rindex ${$input}, ';', $origOffset;
198 0 my $previousNlPos = rindex ${$input}, "\n", $semiPos;
199 0 my $indent = $semiPos - ($previousNlPos + 1);
200 0 my $firstNlPos = index ${$input}, "\n", $semiPos;
201 0 my $valueStartPos = $semiPos + 2;
202 0 my $nextNlPos = $firstNlPos;
203 # say STDERR qq{origOffset: }, substr(${$input}, $origOffset, 20);
204 # say STDERR qq{First NL pos: }, substr(${$input}, $firstNlPos, 20);
205  
206 0 ▶ if ($indent <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent <= 0) 0 0 0
207 # say STDERR "indent=$indent; nextNlPos=$nextNlPos";
208 0 ▶ LINE: while ($nextNlPos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextNlPos >= 0) 0 0 0
209 0 pos ${$input} = $nextNlPos + 1;
210 0 ▶ if ( ${$input} =~ m/\G [ ]* == [\n]/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$input;} =~ /\G [ ]* == [\n]/msx) 0 0 0
211 0 my $terminatorStartPos = $LAST_MATCH_START[0];
212 0 my $terminatorEndPos = $LAST_MATCH_END[0];
213 0 my $value = substr( ${$input}, $valueStartPos,
214 $terminatorStartPos - $valueStartPos );
215 0 return \$value, $nextNlPos;
216 }
217 0 $nextNlPos = index ${$input}, "\n", $nextNlPos+1;
218 }
219 # If here, end of string is EOF
220 0 my $inputLength = length ${$input};
221 0 my $value = substr ${$input}, $valueStartPos, $inputLength - $valueStartPos;
222 0 return \$value, $inputLength;
223 }
224  
225 # If here, indent > 0
226 0 my $indentString = (' ' x $indent);
227  
228 0 ▶ LINE: while ($nextNlPos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextNlPos >= 0) 0 0 0
229 # say STDERR "LINE: indent=$indent; nextNlPos=$nextNlPos";
230 0 pos ${$input} = $nextNlPos + 1;
231 # say STDERR qq{Pos set to: }, substr(${$input}, $nextNlPos+1, 20);
232 0 ▶ if ( ${$input} =~ m/\G $indentString [ ]* == [\n]/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$input;} =~ /\G $indentString [ ]* == [\n]/msx) 0 0 0
233 0 my $terminatorStartPos = $LAST_MATCH_START[0];
234 # say STDERR qq{TISTIS found: }, substr(${$input}, $terminatorStartPos, 20);
235 0 my $value = substr( ${$input}, $valueStartPos,
236 $terminatorStartPos - $valueStartPos );
237 # Continue parsing after TISTIS? Or before?
238 0 return \$value, $nextNlPos;
239 }
240
241 0 ▶ if ( (substr ${$input}, $nextNlPos+1, $indent) eq $indentString ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr(${$input;}, $nextNlPos + 1, $indent) eq $indentString) 0 0 0
242 0 $nextNlPos = index ${$input}, "\n", $nextNlPos+1;
243 # say STDERR qq{Continuing cram, nextNlPos=$nextNlPos};
244 # say STDERR qq{Continuing cram: }, substr(${$input}, $nextNlPos, 20);
245 0 next LINE;
246 }
247 # If here, outdent
248 # say STDERR qq{Outdent, returning at: }, substr(${$input}, $nextNlPos+1, 20);
249 0 my $value = substr ${$input}, $valueStartPos,
250 ($nextNlPos + 1) - $valueStartPos;
251 0 return \$value, $nextNlPos+1;
252 }
253  
254 # Premature EOF if here
255 0 return;
256 }
257  
258 # The 'semantics' named argument must be considered "internal"
259 # for now -- any change in the grammar could break any or all of
260 # apps. When the grammar can be frozen, the 'semantics' argument
261 # can become a "documented" feature.
262 #
263 # In the meantime, applications which want stability can simply
264 # copy in this file lexically, losing the advantage of updates,
265 # but guaranteeing stability.
266 7 ▶sub new {
Subroutine new: called CC=7 SCAR=20.2
267 7 my ($class, @argHashes) = @_;
268 7 my $self = {};
269 7 for my $argHash (@argHashes) {
270 7 ARG_NAME: for my $argName ( keys %{$argHash} ) {
271 14 ▶ if ( $argName eq 'all_symbols' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argName eq "all_symbols") 7 7 14
272 7 $self->{all_symbols} = $argHash->{all_symbols};
273 7 next ARG_NAME;
274 }
275 7 ▶ if ( $argName eq 'semantics' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argName eq "semantics") 7 0 7
276 7 $self->{semantics} = $argHash->{semantics};
277 7 next ARG_NAME;
278 }
279 0 die "MarpaX::Hoonlint::YAHC::new() called with unknown arg name: $argName";
280 }
281 }
282 7 ▶ my $semantics = $self->{semantics} // $defaultSemantics;
Condition33%1/3
$self->{"semantics"} // $defaultSemantics
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->{"semantics"} // $defaultSemantics
$self->{"semantics"} $defaultSemantics
Truth table33%1/3
A // B
A= $self->{"semantics"}B= $defaultSemantics
ABresult
1X1
011
000
283 7 ▶ if ( $self->{all_symbols} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'all_symbols'}) { } 7 0 7
284 ## show all symbols
285 7 $baseDSL =~ s/[(][-] //g;
286 7 $baseDSL =~ s/ [-][)]//g;
287 }
288 else {
289 ## hide selected symbols
290 0 $baseDSL =~ s/[(][-] /(/g;
291 0 $baseDSL =~ s/ [-][)]/)/g;
292 }
293 7 my $dsl = $semantics . $baseDSL;
294  
295 7 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
296 7 $self->{dsl} = $dsl;
297 7 $self->{grammar} = $grammar;
298 7 return bless $self, $class;
299 }
300  
301 12 ▶sub recceStart {
Subroutine recceStart: called CC=3 SCAR=11.3
302 12 my ($self) = @_;
303 12 my $debug = $MarpaX::Hoonlint::YAHC::DEBUG;
304 my $recce = Marpa::R2::Scanless::R->new(
305 {
306 grammar => $self->{grammar},
307 12 ▶ ranking_method => 'high_rule_only',
Branch: 2 branches
BranchTrueFalseTotal
$debug ? : 0 12 12
$debug ? : 0 12 12
308 trace_lexers => ( $debug ? 1 : 0 ),
309 trace_terminals => ( $debug ? 1 : 0 ),
310 }
311 );
312 12 $self->{recce} = $recce;
313 12 return $self;
314 }
315  
316 7 ▶sub dsl {
Subroutine dsl: called CC=1 SCAR=0.0
317 7 my ($self) = @_;
318 7 return $self->{dsl};
319 }
320  
321 7 ▶sub rawGrammar {
Subroutine rawGrammar: called CC=1 SCAR=0.0
322 7 my ($self) = @_;
323 7 return $self->{grammar};
324 }
325  
326 12 ▶sub rawRecce {
Subroutine rawRecce: called CC=1 SCAR=0.0
327 12 my ($self) = @_;
328 12 return $self->{recce};
329 }
330  
331 12 ▶sub read {
Subroutine read: called CC=21 SCAR=56.3
332 12 my ($self, $input) = @_;
333 12 $self->recceStart();
334 12 my $recce = $self->{recce};
335 12 my $debug = $MarpaX::Hoonlint::YAHC::DEBUG;
336 12 my $input_length = length ${$input};
337 12 my $this_pos;
338 12 my $ok = eval { $this_pos = $recce->read( $input ) ; 1; };
339 12 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 12 12
340 0 ▶ say STDERR $recce->show_progress(0, -1) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
341 0 die $EVAL_ERROR;
342 }
343  
344 # The main read loop. Read starting at $offset.
345 # If interrupted execute the handler logic,
346 # and, possibly, resume.
347 12 ▶ say STDERR "this_pos=$this_pos ; input_length=$input_length" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
348  
349 READ:
350 12 ▶ while ( $this_pos < $input_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_pos < $input_length) 0 12 12
351  
352 0 my $resume_pos;
353  
354 # Only one event at a time is expected -- more
355 # than one is an error. No event means parsing
356 # is exhausted.
357  
358 0 my $events = $recce->events();
359 0 my $event_count = scalar @{$events};
360 0 ▶ if ( $event_count < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_count < 0) 0 0 0
361 0 last READ;
362 }
363 0 ▶ if ( $event_count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_count != 1) 0 0 0
364 0 divergence("One event expected, instead got $event_count");
365 }
366  
367 # Find the event name
368  
369 0 my $event = $events->[0];
370 0 my $eventName = $event->[0];
371  
372 0 ▶ say STDERR "$eventName event" if $MarpaX::Hoonlint::YAHC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
373  
374 0 ▶ if ( $eventName eq 'tripleQuote' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eventName eq "tripleQuote") 0 0 0
375 0 my $value_ref;
376 0 ( $value_ref, $resume_pos ) = getTripleQuote( $input, $this_pos );
377 0 ▶ return if not $value_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_ref 0 0 0
378 my $result = $recce->lexeme_read(
379 'TRIPLE_QUOTE_STRING',
380 $this_pos,
381 0 ( length ${$value_ref} ),
382 0 [ ${$value_ref} ]
383 );
384 0 ▶ say STDERR "lexeme_read('TRIPLE_QUOTE_STRING',...) returned ",
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
385 Data::Dumper::Dumper( \$result )
386 if $MarpaX::Hoonlint::YAHC::DEBUG;
387 }
388  
389 # TODO: tripeDoubleQuote must allow sump(5d)
390 0 ▶ if ( $eventName eq 'tripleDoubleQuote' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eventName eq "tripleDoubleQuote") 0 0 0
391 0 my $value_ref;
392 0 ( $value_ref, $resume_pos )
393 = getTripleDoubleQuote( $input, $this_pos );
394 0 ▶ return if not $value_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_ref 0 0 0
395 my $result = $recce->lexeme_read(
396 'TRIPLE_DOUBLE_QUOTE_STRING',
397 $this_pos,
398 0 ( length ${$value_ref} ),
399 0 [ ${$value_ref} ]
400 );
401 0 ▶ say STDERR "lexeme_read('TRIPLE_DOUBLE_QUOTE_STRING',...) returned ",
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
402 Data::Dumper::Dumper( \$result )
403 if $MarpaX::Hoonlint::YAHC::DEBUG;
404 }
405  
406 0 ▶ if ( $eventName eq '^CRAM' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eventName eq "^CRAM") 0 0 0
407 0 my $value_ref;
408 0 ( $value_ref, $resume_pos )
409 = getCram( $input, $this_pos );
410 0 ▶ if (not $value_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value_ref) 0 0 0
411 # TODO: After development, add "if $debug"
412 0 say STDERR $recce->show_progress( 0, -1 );
413 0 my $badStart = substr ${$input}, $this_pos, 50;
414 0 die join '', 'Problem in getCram: "', $badStart, '"';
415 }
416 my $result = $recce->lexeme_read(
417 'CRAM',
418 $this_pos,
419 0 ( length ${$value_ref} ),
420 0 [ ${$value_ref} ]
421 );
422 0 ▶ say STDERR "lexeme_read('CRAM',...) returned ",
Branch: 1 branch
BranchTrueFalseTotal
if $MarpaX::Hoonlint::YAHC::DEBUG 0 0 0
423 Data::Dumper::Dumper( \$result )
424 if $MarpaX::Hoonlint::YAHC::DEBUG;
425 }
426  
427 0 ▶ if (not $resume_pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resume_pos) 0 0 0
428 0 die "read() ended prematurely\n",
429 " input length = $input_length\n",
430 " length read = $this_pos\n",
431 qq{ the cause was an "$eventName" event};
432 }
433  
434 0 ▶ say STDERR "this_pos=$this_pos ; input_length=$input_length" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
435  
436 # say STDERR qq{Resuming at "}, substr ${$input}, $resume_pos, 50;
437  
438 0 my $ok = eval { $this_pos = $recce->resume($resume_pos); 1; };
439 0 ▶ if ( not $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
440 0 ▶ say STDERR $recce->show_progress( 0, -1 ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
441 0 die $EVAL_ERROR;
442 }
443  
444 }
445 12 return;
446 }
447  
448 0 ▶sub parse {
Subroutine parse: not called CC=3 SCAR=24.8
449 0 my ($input) = @_;
450 0 my $debug = $MarpaX::Hoonlint::YAHC::DEBUG;
451 0 my $self = MarpaX::Hoonlint::YAHC->new();
452 0 $self->read($input);
453 0 my $recce = $self->{recce};
454  
455 0 if ( 0 ) {
456 # if ( $recce->ambiguity_metric() > 1 ) {
457  
458 # The calls in this section are experimental as of Marpa::R2 2.090
459 my $asf = Marpa::R2::ASF->new( { slr => $recce } );
460 say STDERR 'No ASF' if not defined $asf;
461 my $ambiguities = Marpa::R2::Internal::ASF::ambiguities($asf);
462 my @ambiguities = grep { defined } @{$ambiguities}[ 0 .. 1 ];
463 die
464 "Parse of BNF/Scanless source is ambiguous\n",
465 Marpa::R2::Internal::ASF::ambiguities_show( $asf, \@ambiguities );
466 } ## end if ( $recce->ambiguity_metric() > 1 )
467 # }
468  
469 0 my $valueRef = $recce->value();
470 0 ▶ if ( !$valueRef ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valueRef) 0 0 0
471 0 ▶ say STDERR $recce->show_progress( 0, -1 ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
472 0 die "input read, but there was no parse";
473 }
474  
475 0 return $valueRef;
476 }
477  
478 # Takes one argument and returns a ref to an array of acceptable
479 # nodes. The array may be empty. All scalars are acceptable
480 # leaf nodes. Acceptable interior nodes have length at least 1.
481 0 ▶sub prune {
Subroutine prune: not called CC=12 SCAR=49.2
482 7 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
483 0 my ($v) = @_;
484  
485 0 state $deleteIfEmpty = {
486 optKets => 1,
487 };
488  
489 0 state $nonSemantic = {
490 doubleStringElements => 1,
491 fordFile => 1,
492 fordHoop => 1,
493 fordHoopSeq => 1,
494 hoonExpression => 1,
495 wideLong5d => 1,
496 norm5d => 1,
497 norm5dMold => 1,
498 rope5d => 1,
499 rump5d => 1,
500 scad5d => 1,
501 scat5d => 1,
502 tall5d => 1,
503 tall5dSeq => 1,
504 teakChoice => 1,
505 till5d => 1,
506 till5dSeq => 1,
507 togaElements => 1,
508 wedeFirst => 1,
509 wide5d => 1,
510 wide5dChoices => 1,
511 wide5dJog => 1,
512 wide5dJogging => 1,
513 wide5dJogs => 1,
514 wide5dSeq => 1,
515 wideNorm5d => 1,
516 wideNorm5dMold => 1,
517 wideTeakChoice => 1,
518 wyde5d => 1,
519 wyde5dSeq => 1,
520 };
521  
522 0 ▶ return [] if not defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
523 0 my $reftype = ref $v;
524 0 ▶ return [$v] if not $reftype; # An acceptable leaf node
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype 0 0 0
525 0 ▶ return prune($$v) if $reftype eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if $reftype eq "REF" 0 0 0
526 0 ▶ divergence("Tree node has reftype $reftype") if $reftype ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $reftype ne "ARRAY" 0 0 0
527 0 my @source = grep { defined } @{$v};
528 0 my $element_count = scalar @source;
529 0 ▶ return [] if $element_count <= 0; # must have at least one element
Branch: 1 branch
BranchTrueFalseTotal
if $element_count <= 0 0 0 0
530 0 my $name = shift @source;
531 0 my $nameReftype = ref $name;
532 # divergence("Tree node name has reftype $nameReftype") if $nameReftype;
533 0 ▶ if ($nameReftype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nameReftype) 0 0 0
534 0 my @result = ();
535 0 ELEMENT:for my $element ($name, @source) {
536 0 ▶ if (ref $element eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $element eq "ARRAY") 0 0 0
537 0 push @result, grep { defined }
538 0 map { @{$_}; }
539 0 map { prune($_); }
540 0 @{$element}
541 ;
542 0 next ELEMENT;
543 }
544 0 push @result, $_;
545 }
546 0 return [@result];
547 }
548 0 ▶ if (defined $deleteIfEmpty->{$name} and $element_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deleteIfEmpty->{$name} and $element_count == 1) 0 0 0
Condition0%0/3
defined $deleteIfEmpty->{$name} and $element_count == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $deleteIfEmpty->{$name} and $element_count == 1
defined $deleteIfEmpty->{$name} $element_count == 1
Truth table0%0/3
A and B
A= defined $deleteIfEmpty->{$name}B= $element_count == 1
ABresult
0X0
100
111
549 0 return [];
550 }
551 0 ▶ if (defined $nonSemantic->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nonSemantic->{$name}) 0 0 0
552 # Not an acceptable branch node, but (hopefully)
553 # its children are acceptable
554 0 return [ grep { defined }
555 0 map { @{$_}; }
556 0 map { prune($_); }
557 @source
558 ];
559 }
560  
561 # An acceptable branch node
562 0 my @result = ($name);
563 0 push @result, grep { defined }
564 0 map { @{$_}; }
565 0 map { prune($_); }
566 @source;
567 0 return [\@result];
568 }
569  
570 # takes LC alphanumeric rune name and samples
571 # for N-fixed rune and returns the Marpa rules
572 # for the tall and the 2 regular wide forms.
573 490 ▶sub doFixedRune {
Subroutine doFixedRune: called CC=3 SCAR=11.0
574 490 my ($runeName, @samples) = @_;
575 490 my @result = (join ' ', '#', (uc $runeName), @samples);
576 490 my $glyphName1 = substr($runeName, 0, 3);
577 490 my $glyphName2 = substr($runeName, 3, 3);
578 490 ▶ my $glyph1 = $glyphs{$glyphName1} or die "no glyph for $glyphName1";
Branch: 1 branch
BranchTrueFalseTotal
my $glyph1 = $glyphs{$glyphName1} or die "no glyph for $glyphName1" 490 0 490
579 490 my $glyph2 = $glyphs{$glyphName2};
580 490 my $glyphLexeme1 = ($glyphName1) . '4h';
581 490 my $glyphLexeme2 = ($glyphName2) . '4h';
582 490 my $tallLHS = 'tall' . ucfirst $runeName;
583 490 my $wideLHS = 'wide' . ucfirst $runeName;
584 490 my $tallRuneLexeme = (uc $runeName) . 'GAP';
585 490 my $wideRuneLexeme = (uc $runeName) . 'PEL';
586  
587 # norm5d ::= tallBarhep
588 490 push @result, 'norm5d ::= ' . $tallLHS;
589  
590 # wideNorm5d ::= wideBarhep
591 490 push @result, 'wideNorm5d ::= ' . $wideLHS;
592  
593 # tallBarhep ::= (- BAR4H HEP4H GAP -) tall5d (- GAP -) tall5d
594 490 push @result, $tallLHS . ' ::= (- '
595 . $tallRuneLexeme
596 . ' -) ' . (join ' (- GAP -) ', @samples);
597 490 state $wideEquiv = {
598 bont5d => 'wideBont5d',
599 bonz5d => 'wideBonz5d',
600 mold => 'wyde5d',
601 tall5d => 'wide5d',
602 rack5d => 'wideRack5d',
603 rick5d => 'wideRick5d',
604 ruck5d => 'wideRuck5d',
605 teak5d => 'wideTeak5d',
606 };
607 490 ▶ my @wideSamples = map { $wideEquiv->{$_} // $_; } @samples;
Condition66%2/3
$wideEquiv->{$_} // $_
l!l&&r!l&&!r
8401750
MC/DC0%0/2
$wideEquiv->{$_} // $_
$wideEquiv->{$_} $_
Truth table66%2/3
A // B
A= $wideEquiv->{$_}B= $_
ABresult
1X1
011
000
608  
609 # wideBarhep ::= (- BARHEPPEL -) wide5d (- ACE -) wide5d (- PER -)
610 490 push @result, $wideLHS . ' ::= (- '
611 . $wideRuneLexeme
612 . ' -) ' . (join ' (- ACE -) ', @wideSamples) . q{ (- PER -)};
613  
614 # BARHEPGAP ~ bar4h hep4h gap4k
615 # BARHEPPEL ~ bar4h hep4h pel4h
616 490 push @result, "$tallRuneLexeme ~ $glyphLexeme1 $glyphLexeme2 gap4k";
617 490 push @result, "$wideRuneLexeme ~ $glyphLexeme1 $glyphLexeme2 pel4h";
618  
619 490 return join "\n", @result, '';
620 }
621  
622 7 1;
623  
624 # The "FIXED:" comments lines are descriptons of the fixed length runes
625 # (1-fixed, 2-fixed, 3-fixed and 4-fixed) for auto-generation
626 # of Marpa rules for the various regular formats, both
627 # tall and wide.
628 #
629 # The format is
630 #
631 # rune type1 type2 ...
632  
633 # Organization is by hoon.hoon (and Hoon Library) sections: 4a, 5d, etc.;
634 # and within that alphabetically by "face" name
635  
636 __DATA__
« blib/lib/MarpaX/Hoonlint/Policy/Test/Whitespace.pm ↑ Summary blib/lib/MarpaX/Hoonlint.pm »