Hoonlint.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 SCAR 0.0
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Hoonlint/yahc.pm ↑ Summary
1 # Hoon "tidy" utility
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package MarpaX::Hoonlint;
9  
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw(looks_like_number weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use MarpaX::Hoonlint::yahc;
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 use vars qw($VERSION $STRING_VERSION @ISA $DEBUG);
18 $VERSION = '1.009_001';
19 $STRING_VERSION = $VERSION;
20 ## no critic (BuiltinFunctions::ProhibitStringyEval)
21 $VERSION = eval $VERSION;
22 ## use critic
23 $DEBUG = 0;
24  
25 my %separator = qw(
26 hyf4jSeq DOT
27 singleQuoteCord gon4k
28 dem4k gon4k
29 timePeriodKernel DOT
30 optBonzElements GAP
31 optWideBonzElements ACE
32 till5dSeq GAP
33 wyde5dSeq ACE
34 gash5d FAS
35 togaElements ACE
36 wide5dJogs wide5dJoggingSeparator
37 rope5d DOT
38 rick5d GAP
39 wideRick5d commaAce
40 ruck5d GAP
41 wideRuck5d commaAce
42 tallTopKidSeq GAP_SEM
43 wideInnerTops ACE
44 wideAttrBody commaAce
45 scriptStyleTailElements GAP
46 moldInfixCol2 COL
47 lusSoilSeq DOG4I
48 hepSoilSeq DOG4I
49 infixDot DOG4I
50 waspElements GAP
51 whap5d GAP
52 hornSeq GAP
53 wideHornSeq ACE
54 fordHoopSeq GAP
55 tall5dSeq GAP
56 wide5dSeq ACE
57 fordFascomElements GAP
58 optFordHithElements FAS
59 fordHoofSeq commaWS
60 );
61  
62 sub internalError {
63 my ($instance) = @_;
64 my $fileName = $instance->{fileName} // "[No file name]";
65 my @pieces = ( "$PROGRAM_NAME $fileName: Internal Error\n", @_ );
66 push @pieces, "\n" unless $pieces[$#pieces] =~ m/\n$/;
67 my ( undef, $codeFilename, $codeLine ) = caller;
68 die join q{}, @pieces,
69 "Internal error was at $codeFilename, line $codeLine";
70 }
71  
72 sub doNode {
73 my ( $instance, @argChildren ) = @_;
74 my $pSource = $instance->{pHoonSource};
75 my @results = ();
76 my $childCount = scalar @argChildren;
77 no warnings 'once';
78 my $ruleID = $Marpa::R2::Context::rule;
79 use warnings;
80 my ( $lhs, @rhs ) =
81 map { $MarpaX::Hoonlint::grammar->symbol_display_form($_) }
82 $MarpaX::Hoonlint::grammar->rule_expand($ruleID);
83 my ( $first_g1, $last_g1 ) = Marpa::R2::Context::location();
84 my ($lhsStart) =
85 $MarpaX::Hoonlint::recce->g1_location_to_span( $first_g1 + 1 );
86  
87 my $node;
88 CREATE_NODE: {
89 if ( $childCount <= 0 ) {
90 $node = {
91 type => 'null',
92 symbol => $lhs,
93 start => $lhsStart,
94 length => 0,
95 };
96 last CREATE_NODE;
97 }
98 my ( $last_g1_start, $last_g1_length ) =
99 $MarpaX::Hoonlint::recce->g1_location_to_span($last_g1);
100 my $lhsLength = $last_g1_start + $last_g1_length - $lhsStart;
101 RESULT: {
102 CHILD: for my $childIX ( 0 .. $#argChildren ) {
103 my $child = $argChildren[$childIX];
104 my $refType = ref $child;
105 next CHILD unless $refType eq 'ARRAY';
106  
107 my ( $lexemeStart, $lexemeLength, $lexemeName ) = @{$child};
108  
109 if ( $lexemeName eq 'TRIPLE_DOUBLE_QUOTE_STRING' ) {
110 my $terminator = q{"""};
111 my $terminatorPos = index ${$pSource},
112 $terminator,
113 $lexemeStart + $lexemeLength;
114 $lexemeLength =
115 $terminatorPos + ( length $terminator ) - $lexemeStart;
116 }
117 if ( $lexemeName eq 'TRIPLE_QUOTE_STRING' ) {
118 my $terminator = q{'''};
119 my $terminatorPos = index ${$pSource},
120 $terminator,
121 $lexemeStart + $lexemeLength;
122 $lexemeLength =
123 $terminatorPos + ( length $terminator ) - $lexemeStart;
124 }
125 $argChildren[$childIX] = {
126 type => 'lexeme',
127 start => $lexemeStart,
128 length => $lexemeLength,
129 symbol => $lexemeName,
130 };
131 }
132  
133 my $lastLocation = $lhsStart;
134 if ( ( scalar @rhs ) != $childCount ) {
135  
136 # This is a non-trivial (that is, longer than one item) sequence rule.
137 my $childIX = 0;
138 my $lastSeparator;
139 CHILD: for ( ; ; ) {
140  
141 my $child = $argChildren[$childIX];
142 my $childType = $child->{type};
143 $childIX++;
144 ITEM: {
145 if ( defined $lastSeparator ) {
146 my $length =
147 $child->{start} - $lastSeparator->{start};
148 $lastSeparator->{length} = $length;
149 }
150 push @results, $child;
151 $lastLocation = $child->{start} + $child->{length};
152 }
153 last RESULT if $childIX > $#argChildren;
154 my $separator = $separator{$lhs};
155 next CHILD unless $separator;
156 $lastSeparator = {
157 type => 'separator',
158 symbol => $separator,
159 start => $lastLocation,
160  
161 # length supplied later
162 };
163 push @results, $lastSeparator;
164 }
165 last RESULT;
166 }
167  
168 # All other rules
169 CHILD: for my $childIX ( 0 .. $#argChildren ) {
170 my $child = $argChildren[$childIX];
171 push @results, $child;
172 }
173 }
174  
175 $node = {
176 type => 'node',
177 ruleID => $ruleID,
178 start => $lhsStart,
179 length => $lhsLength,
180 children => \@results,
181 };
182 }
183  
184 # Add weak links
185 my $children = $node->{children};
186 if ( $children and scalar @{$children} >= 1 ) {
187 CHILD: for my $childIX ( 0 .. $#$children ) {
188 my $child = $children->[$childIX];
189 $child->{PARENT} = $node;
190 weaken( $child->{PARENT} );
191 }
192 CHILD: for my $childIX ( 1 .. $#$children ) {
193 my $thisChild = $children->[$childIX];
194 my $prevChild = $children->[ $childIX - 1 ];
195 $thisChild->{PREV} = $prevChild;
196 weaken( $thisChild->{PREV} );
197 $prevChild->{NEXT} = $thisChild;
198 weaken( $prevChild->{NEXT} );
199 }
200 }
201  
202 my $nodeCount = $instance->{nodeCount};
203 $node->{IX} = $nodeCount;
204 $instance->{nodeCount} = $nodeCount + 1;
205  
206 return $node;
207 }
208  
209 sub describeRange {
210 my ( $firstLine, $firstColumn, $lastLine, $lastColumn ) = @_;
211 return sprintf "@%d:%d-%d:%d", $firstLine, $firstColumn, $lastLine,
212 $lastColumn
213 if $firstLine != $lastLine;
214 return sprintf "@%d:%d-%d", $firstLine, $firstColumn, $lastColumn
215 if $firstColumn != $lastColumn;
216 return sprintf "@%d:%d", $firstLine, $firstColumn;
217 }
218  
219 sub describeNodeRange {
220 my ( $instance, $node ) = @_;
221 my $firstPos = $node->{start};
222 my $length = $node->{length};
223 my $lastPos = $firstPos + $length;
224 my ( $firstLine, $firstColumn ) = $instance->line_column($firstPos);
225 my ( $lastLine, $lastColumn ) = $instance->line_column($lastPos);
226 return describeRange( $firstLine, $firstColumn, $lastLine, $lastColumn );
227 }
228  
229 sub lexeme {
230 my ( $instance, $line, $column ) = @_;
231 my $literal = $instance->literalLine($line);
232 my $lexeme = substr $literal, $column;
233 $lexeme =~ s/[\s].*\z//xms;
234 return $lexeme;
235 }
236  
237 sub literalNode {
238 my ( $instance, $node ) = @_;
239 my $start = $node->{start};
240 my $length = $node->{length};
241 return $instance->literal( $start, $length );
242 }
243  
244 sub literalLine {
245 my ( $instance, $lineNum ) = @_;
246 my $lineToPos = $instance->{lineToPos};
247 my $startPos = $lineToPos->[$lineNum];
248 $DB::single = 1 if not defined $lineToPos->[ $lineNum + 1 ];
249 my $line =
250 $instance->literal( $startPos,
251 ( $lineToPos->[ $lineNum + 1 ] - $startPos ) );
252 return $line;
253 }
254  
255 sub literal {
256 my ( $instance, $start, $length ) = @_;
257 my $pSource = $instance->{pHoonSource};
258 return '' if $start >= length ${$pSource};
259 return substr ${$pSource}, $start, $length;
260 }
261  
262 sub column {
263 my ( $instance, $pos ) = @_;
264 my $pSource = $instance->{pHoonSource};
265 return $pos - ( rindex ${$pSource}, "\n", $pos - 1 );
266 }
267  
268 sub maxNumWidth {
269 my ($instance) = @_;
270 return length q{} . $#{ $instance->{lineToPos} };
271 }
272  
273 sub contextDisplay {
274 my ($instance) = @_;
275 my $pTopicLines = $instance->{topicLines};
276 my $pMistakeLines = $instance->{mistakeLines};
277 my $contextSize = $instance->{contextSize};
278 my $displayDetails = $instance->{displayDetails};
279 my $lineToPos = $instance->{lineToPos};
280 my @pieces = ();
281 my %tag = map { $_ => q{>} } keys %{$pTopicLines};
282 $tag{$_} = q{!} for keys %{$pMistakeLines};
283 my @sortedLines = sort { $a <=> $b } map { $_ + 0; } keys %tag;
284  
285 # say STDERR join " ", __FILE__, __LINE__, "# of sorted lines:", (scalar @sortedLines);
286 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper(\@sortedLines);
287 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($pMistakeLines);
288 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($lineToPos);
289  
290 if ( $contextSize <= 0 ) {
291 for my $lineNum (@sortedLines) {
292 my $mistakeDescs = $pMistakeLines->{$lineNum};
293 for my $mistakeDesc ( @{$mistakeDescs} ) {
294 my ( $mistake, $desc ) = @{$mistakeDesc};
295 push @pieces, $desc, "\n";
296 }
297 }
298 return join q{}, @pieces;
299 }
300  
301 my $maxNumWidth = $instance->maxNumWidth();
302 my $lineNumFormat = q{%} . $maxNumWidth . 'd';
303  
304 # Add to @pieces a set of lines to be displayed consecutively
305 my $doConsec = sub () {
306 my ( $start, $end ) = @_;
307 $start = 1 if $start < 1;
308 $end = $#$lineToPos - 1 if $end >= $#$lineToPos;
309 for my $lineNum ( $start .. $end ) {
310 my $startPos = $lineToPos->[$lineNum];
311 my $line = $instance->literalLine($lineNum);
312 my $tag = $tag{$lineNum} // q{ };
313 my $mistakeDescs = $pMistakeLines->{$lineNum};
314 for my $mistakeDesc ( @{$mistakeDescs} ) {
315 my ( $mistake, $desc ) = @{$mistakeDesc};
316 my $details = $mistake->{details};
317 if ( $details and scalar @{$details} and $displayDetails > 0 ) {
318 push @pieces, '[ ', $desc, "\n";
319  
320 # detail levels are not currently used, but are for future
321 # extensions.
322 for my $detailLevel ( @{$details} ) {
323 for my $detail ( @{$detailLevel} ) {
324 push @pieces, q{ }, $detail, "\n";
325 }
326 }
327 push @pieces, "]\n";
328 }
329 else {
330 push @pieces, '[ ', $desc, " ]\n";
331 }
332 }
333 push @pieces, ( sprintf $lineNumFormat, $lineNum ), $tag, q{ },
334 $line;
335 }
336 };
337  
338 my $lastIX = -1;
339 CONSEC_RANGE: while ( $lastIX < $#sortedLines ) {
340 my $firstIX = $lastIX + 1;
341  
342 # Divider line if after first consecutive range
343 push @pieces, ( '-' x ( $maxNumWidth + 2 ) ), "\n" if $firstIX > 0;
344 $lastIX = $firstIX;
345 SET_LAST_IX: while (1) {
346 my $nextIX = $lastIX + 1;
347 last SET_LAST_IX if $nextIX > $#sortedLines;
348  
349 # We combine lines if by doing so, we make the listing shorter.
350 # This is calculated by
351 # 1.) Taking the current last line.
352 # 2.) Add the context lines for the last and next lines (2*($contextSize-1))
353 # 3.) Adding 1 for the divider line, which we save if we combine ranges.
354 # 4.) Adding 1 because we test if they abut, not overlap
355 # Doing the arithmetic, we get
356 last SET_LAST_IX
357 if $sortedLines[$lastIX] + 2 * $contextSize <
358 $sortedLines[$nextIX];
359 $lastIX = $nextIX;
360 }
361 $doConsec->(
362 $sortedLines[$firstIX] - ( $contextSize - 1 ),
363 $sortedLines[$lastIX] + ( $contextSize - 1 )
364 );
365 }
366  
367 return join q{}, @pieces;
368 }
369  
370 # Set lists of topic and mistake lines in instance
371 sub reportItem {
372 my ( $instance, $mistake, $mistakeDesc, $topicLineArg, $mistakeLineArg ) =
373 @_;
374  
375 my $inclusions = $instance->{inclusions};
376 my $suppressions = $instance->{suppressions};
377 my $reportPolicy = $mistake->{policy};
378  
379 # TODO: Is subpolicy everywhere? Can the tag
380 # named argument be eliminated?
381 my $mistakeSubpolicy = $mistake->{subpolicy};
382  
383 # TODO: Change subpolicy to ALWAYS be an array
384 # and eliminate the following code
385 my @reportSubpolicy = ();
386 SET_SUBPOLICY: {
387 my $refType = ref $mistakeSubpolicy;
388 if ($refType eq 'ARRAY') {
389 push @reportSubpolicy, @{$mistakeSubpolicy};
390 last SET_SUBPOLICY;
391 }
392 push @reportSubpolicy, $mistakeSubpolicy;
393 }
394 my $reportSubpolicy = join ':', @reportSubpolicy;
395  
396 # TODO: Usually a default of parentLine, parentColumn has already
397 # been enforced. This is a mistake and should change.
398 # Add reportLine/reportColumn to all mistakes, and do not use
399 # line/column. (Can line/column be eliminated?)
400 my $reportLine = $mistake->{reportLine} // $mistake->{line};
401 my $reportColumn = $mistake->{reportColumn} // $mistake->{column};
402 my $reportLC = join ':', $reportLine, $reportColumn + 1;
403 my $suppressThisItem = 0;
404 my $excludeThisItem = 0;
405  
406 $excludeThisItem = 1
407 if $inclusions
408 and not $inclusions->{$reportLC}{$reportPolicy}{$reportSubpolicy};
409 my $suppression =
410 $suppressions->{$reportLC}->{$reportPolicy}->{$reportSubpolicy};
411 if ( defined $suppression ) {
412 $suppressThisItem = 1;
413 $instance->{unusedSuppressions}->{$reportLC}->{$reportPolicy}
414 ->{$reportSubpolicy} = undef;
415 }
416  
417 return if $excludeThisItem;
418 return if $suppressThisItem;
419  
420 my $fileName = $instance->{fileName};
421 my $mistakeLines = $instance->{mistakeLines};
422  
423 my $topicLines = $instance->{topicLines};
424 my @topicLines = ();
425 push @topicLines, ref $topicLineArg ? @{$topicLineArg} : $topicLineArg;
426 push @topicLines,
427 grep { defined $_ }
428 ( $mistakeLineArg, $mistake->{line},
429 $mistake->{parentLine}, $reportLine );
430 for my $topicLine (@topicLines) {
431 $topicLines->{$topicLine} = 1;
432 }
433  
434 my $thisMistakeDescs = $mistakeLines->{$mistakeLineArg};
435 $thisMistakeDescs = [] if not defined $thisMistakeDescs;
436 push @{$thisMistakeDescs},
437 [
438 $mistake,
439 "$fileName $reportLC $reportPolicy $reportSubpolicy $mistakeDesc"
440 ];
441 $mistakeLines->{$mistakeLineArg} = $thisMistakeDescs;
442  
443 }
444  
445 sub lhsName {
446 my ( $instance, $node ) = @_;
447 my $grammar = $instance->{grammar};
448 my $type = $node->{type};
449 return if $type ne 'node';
450 my $ruleID = $node->{ruleID};
451 my ( $lhs, @rhs ) = $grammar->rule_expand($ruleID);
452 return $grammar->symbol_name($lhs);
453 }
454  
455 # The "symbol" of a node. Not necessarily unique.
456 sub symbol {
457 my ( $instance, $node ) = @_;
458 # local $Data::Dumper::Maxdepth = 1;
459 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($node);
460 my $name = $node->{symbol};
461 return $name if defined $name;
462 my $type = $node->{type};
463 $DB::single = 1 if not $type;
464 die Data::Dumper::Dumper($node) if not $type;
465 return $instance->lhsName($node) if $type eq 'node';
466 return "[$type]";
467 }
468  
469 # Can be used as test of "brick-ness"
470 sub brickName {
471 my ( $instance, $node ) = @_;
472 # local $Data::Dumper::Maxdepth = 1;
473 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($node);
474 my $type = $node->{type};
475 return $instance->symbol($node) if $type ne 'node';
476 my $lhsName = $instance->lhsName($node);
477 return $lhsName if not $instance->{mortarLHS}->{$lhsName};
478 return;
479 }
480  
481 # Return the name of a brick by recursively climbing,
482 # and die if this fails.
483 sub forceBrickName {
484 my ( $instance, $node ) = @_;
485 my $brickNode = $instance->brickNode($node);
486 return $instance->brickName($brickNode) if $brickNode;
487 $DB::single = 1;
488 die;
489 }
490  
491 # The name of a node for diagnostics purposes. Prefers
492 # "brick" symbols over "mortar" symbols.
493 sub diagName {
494 my ( $instance, $node ) = @_;
495 my $brickNode = $instance->brickNode($node);
496 return $instance->brickName($brickNode) if $brickNode;
497 return $instance->name($node);
498 }
499  
500 # The "name" of a node. Not necessarily unique
501 sub name {
502 my ( $instance, $node ) = @_;
503 my $type = $node->{type};
504 my $symbol = $instance->symbol($node);
505 return $symbol if $type ne 'node';
506 return $instance->lhsName($node);
507 }
508  
509 # Determine how many spaces we need.
510 # Arguments are an array of strings (intended
511 # to be concatenated) and an integer, representing
512 # the number of spaces needed by the app.
513 # (For hoon this will always between 0 and 2.)
514 # Hoon's notation of spacing, in which a newline is equivalent
515 # a gap and therefore two spaces, is used.
516 #
517 # Return value is the number of spaces needed after
518 # the trailing part of the argument string array is
519 # taken into account. It is always less than or
520 # equal to the `spacesNeeded` argument.
521 sub spacesNeeded {
522 my ( $strings, $spacesNeeded ) = @_;
523 for ( my $arrayIX = $#$strings ; $arrayIX >= 0 ; $arrayIX-- ) {
524  
525 my $string = $strings->[$arrayIX];
526  
527 for (
528 my $stringIX = ( length $string ) - 1 ;
529 $stringIX >= 0 ;
530 $stringIX--
531 )
532 {
533 my $char = substr $string, $stringIX, 1;
534 return 0 if $char eq "\n";
535 return $spacesNeeded if $char ne q{ };
536 $spacesNeeded--;
537 return 0 if $spacesNeeded <= 0;
538 }
539 }
540  
541 # No spaces needed at beginning of string;
542 return 0;
543 }
544  
545 sub testStyleCensus {
546 my ($instance) = @_;
547 my $ruleDB = $instance->{ruleDB};
548 my $symbolDB = $instance->{symbolDB};
549 my $symbolReverseDB = $instance->{symbolReverseDB};
550 my $grammar = $instance->{grammar};
551  
552 SYMBOL:
553 for my $symbolID ( $grammar->symbol_ids() ) {
554 my $name = $grammar->symbol_name($symbolID);
555 my $data = {};
556 $data->{name} = $name;
557 $data->{id} = $symbolID;
558 $data->{lexeme} = 1; # default to lexeme
559 $data->{gap} = 1 if $name eq 'GAP';
560 if ( $name =~ m/^[B-Z][AEOIU][B-Z][B-Z][AEIOU][B-Z]GAP$/ ) {
561 $data->{gap} = 1;
562 $data->{runeGap} = 1;
563 }
564 $symbolDB->[$symbolID] = $data;
565 $symbolReverseDB->{$name} = $data;
566 }
567 my $gapID = $symbolReverseDB->{'GAP'}->{id};
568 RULE:
569 for my $ruleID ( $grammar->rule_ids() ) {
570 my $data = { id => $ruleID };
571 my ( $lhs, @rhs ) = $grammar->rule_expand($ruleID);
572 $data->{symbols} = [ $lhs, @rhs ];
573 my $lhsName = $grammar->symbol_name($lhs);
574 my $separatorName = $separator{$lhsName};
575 if ($separatorName) {
576 my $separatorID = $symbolReverseDB->{$separatorName}->{id};
577 $data->{separator} = $separatorID;
578 if ( $separatorID == $gapID ) {
579 $data->{gapiness} = -1;
580 }
581 }
582 if ( not defined $data->{gapiness} ) {
583 for my $rhsID (@rhs) {
584 $data->{gapiness}++ if $symbolDB->[$rhsID]->{gap};
585 }
586 }
587 $ruleDB->[$ruleID] = $data;
588  
589 # say STDERR join " ", __FILE__, __LINE__, "setting rule $ruleID gapiness to", $data->{gapiness} // 'undef';
590 $symbolReverseDB->{$lhs}->{lexeme} = 0;
591 }
592  
593 }
594  
595 sub gapNode {
596 my ( $instance, $node ) = @_;
597 my $symbolReverseDB = $instance->{symbolReverseDB};
598 my $symbol = $node->{symbol};
599 return if not defined $symbol;
600 return $symbolReverseDB->{$symbol}->{gap};
601 }
602  
603 sub runeGapNode {
604 my ( $instance, $node ) = @_;
605 my $symbolReverseDB = $instance->{symbolReverseDB};
606 my $symbol = $node->{symbol};
607 return if not defined $symbol;
608 return $symbolReverseDB->{$symbol}->{runeGap};
609 }
610  
611 # Assumes the node *is* a gap
612 sub gapLength {
613 my ( $instance, $node ) = @_;
614 if ( $instance->runeGapNode($node) ) {
615 my $gapLiteral = $instance->literalNode($node);
616 return (length $gapLiteral) - 2;
617 }
618 return $node->{length};
619 }
620  
621 sub line_column {
622 my ( $instance, $pos ) = @_;
623 $Data::Dumper::Maxdepth = 3;
624 die Data::Dumper::Dumper($instance) if not defined $instance->{recce};
625 my ( $line, $column ) = $instance->{recce}->line_column($pos);
626 $column--;
627 return $line, $column;
628 }
629  
630 sub ancestorByBrickName {
631 my ( $instance, $node, $name ) = @_;
632 my $thisNode = $node;
633 PARENT: while ($thisNode) {
634 my $thisName = $instance->brickName($thisNode);
635 return $thisNode if defined $thisName and $thisName eq $name;
636 $thisNode = $thisNode->{PARENT};
637 }
638 return;
639 }
640  
641 sub ancestorByLHS {
642 my ( $instance, $node, $names ) = @_;
643 my $thisNode = $node;
644 PARENT: while ($thisNode) {
645 my $thisName = $instance->lhsName($thisNode);
646 return $thisNode if defined $thisName and $names->{$thisName};
647 $thisNode = $thisNode->{PARENT};
648 }
649 return;
650 }
651  
652 sub ancestor {
653 my ( $instance, $node, $generations ) = @_;
654 my $thisNode = $node;
655 PARENT: while ($thisNode) {
656 return $thisNode if $generations <= 0;
657 $generations--;
658 $thisNode = $thisNode->{PARENT};
659 }
660 return;
661 }
662  
663 sub nodeLC {
664 my ( $instance, $node ) = @_;
665 return $instance->line_column( $node->{start} );
666 }
667  
668 sub brickNode {
669 my ( $instance, $node ) = @_;
670 my $thisNode = $node;
671 while ($thisNode) {
672 return $thisNode if $instance->brickName($thisNode);
673 $thisNode = $thisNode->{PARENT};
674 }
675 return;
676 }
677  
678 # Return a brick descendent, if there is one.
679 # Only singletons are followed.
680 sub brickDescendant {
681 my ( $instance, $node ) = @_;
682 # local $Data::Dumper::Maxdepth = 1;
683 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($node);
684 my $thisNode = $node;
685 while ($thisNode) {
686 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($thisNode);
687 return $thisNode if $instance->brickName($thisNode);
688 my $children = $thisNode->{children};
689 return if not $children;
690 $thisNode = $children->[0];
691 }
692 return;
693 }
694  
695 sub brickLC {
696 my ( $instance, $node ) = @_;
697 return $instance->nodeLC( $instance->brickNode($node) );
698 }
699  
700 # first brick node in $node's line --
701 # $node if there is no prior brick node
702 sub firstBrickOfLine {
703 my ( $instance, $node ) = @_;
704 my ($currentLine) = $instance->nodeLC($node);
705 my $thisNode = $node;
706 my $firstBrickNode;
707 NODE: while ($thisNode) {
708 my ($thisLine) = $instance->nodeLC($thisNode);
709 last NODE if $thisLine != $currentLine;
710 $firstBrickNode = $thisNode if $instance->brickName($thisNode);
711 $thisNode = $thisNode->{PARENT};
712 }
713 return $firstBrickNode // $node;
714 }
715  
716 # first brick node in $node's line,
717 # by inclusion list.
718 # $node if there is no prior included brick node
719 sub firstBrickOfLineInc {
720 my ( $instance, $node, $inclusions ) = @_;
721  
722 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($inclusions);
723 my ($currentLine) = $instance->nodeLC($node);
724 my $thisNode = $node;
725 my $firstBrickNode = $node;
726 NODE: while ($thisNode) {
727 my ($thisLine) = $instance->nodeLC($thisNode);
728  
729 # say STDERR join " ", __FILE__, __LINE__, 'LC', $instance->nodeLC($thisNode);
730 # say STDERR join " ", __FILE__, __LINE__, $thisLine, $currentLine;
731 last NODE if $thisLine != $currentLine;
732 PICK_NODE: {
733 my $brickName = $instance->brickName($thisNode);
734  
735 # say STDERR join " ", __FILE__, __LINE__, ($brickName // '[undef]');
736 last PICK_NODE if not defined $brickName;
737 $firstBrickNode = $thisNode if $inclusions->{$brickName};
738  
739 # say STDERR join " ", __FILE__, __LINE__, $brickName;
740 }
741 $thisNode = $thisNode->{PARENT};
742 }
743 return $firstBrickNode;
744 }
745  
746 # first brick node in $node's line,
747 # with exclusions.
748 # $node if there is no prior unexcluded brick node
749 sub firstBrickOfLineExc {
750 my ( $instance, $node, $exclusions ) = @_;
751  
752 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($exclusions);
753 my ($currentLine) = $instance->nodeLC($node);
754 my $thisNode = $node;
755 my $firstBrickNode = $node;
756 NODE: while ($thisNode) {
757 my ($thisLine) = $instance->nodeLC($thisNode);
758  
759 # say STDERR join " ", __FILE__, __LINE__, 'LC', $instance->nodeLC($thisNode);
760 # say STDERR join " ", __FILE__, __LINE__, $thisLine, $currentLine;
761 last NODE if $thisLine != $currentLine;
762 PICK_NODE: {
763 my $brickName = $instance->brickName($thisNode);
764  
765 # say STDERR join " ", __FILE__, __LINE__, ($brickName // '[undef]');
766 last PICK_NODE if not defined $brickName;
767  
768 # say STDERR join " ", __FILE__, __LINE__, $brickName;
769 last PICK_NODE if $exclusions->{$brickName};
770  
771 # say STDERR join " ", __FILE__, __LINE__, $brickName;
772 $firstBrickNode = $thisNode;
773 }
774 $thisNode = $thisNode->{PARENT};
775 }
776  
777 # say STDERR join " ", __FILE__, __LINE__, "returning from firstBrickOfLine";
778  
779 return $firstBrickNode;
780 }
781  
782 # nearest (in syntax tree) brick node in $node's line,
783 # from inclusion list
784 # $node if there is no nearest included brick node on same line
785 sub nearestBrickOfLineInc {
786 my ( $instance, $node, $inclusions ) = @_;
787  
788 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($inclusions);
789 my ($currentLine) = $instance->nodeLC($node);
790 my $thisNode = $node;
791 NODE: while ($thisNode) {
792 my ($thisLine) = $instance->nodeLC($thisNode);
793  
794 # say STDERR join " ", __FILE__, __LINE__, 'LC', $instance->nodeLC($thisNode);
795 # say STDERR join " ", __FILE__, __LINE__, $thisLine, $currentLine;
796 last NODE if $thisLine != $currentLine;
797 PICK_NODE: {
798 my $brickName = $instance->brickName($thisNode);
799  
800 # say STDERR join " ", __FILE__, __LINE__, ($brickName // '[undef]');
801 last PICK_NODE if not defined $brickName;
802  
803 # say STDERR join " ", __FILE__, __LINE__, $brickName;
804 # say STDERR join " ", __FILE__, __LINE__, $brickName;
805 return $thisNode if $inclusions->{$brickName};
806 }
807 $thisNode = $thisNode->{PARENT};
808 }
809  
810 # say STDERR join " ", __FILE__, __LINE__, "returning from nearestBrickOfLineInc";
811  
812 return $node;
813 }
814  
815 # nearest (in syntax tree) brick node in $node's line --
816 # with exclusions.
817 # $node if there is no nearest unexcluded brick node on same line
818 sub nearestBrickOfLineExc {
819 my ( $instance, $node, $exclusions ) = @_;
820  
821 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($exclusions);
822 my ($currentLine) = $instance->nodeLC($node);
823 my $thisNode = $node;
824 NODE: while ($thisNode) {
825 my ($thisLine) = $instance->nodeLC($thisNode);
826  
827 # say STDERR join " ", __FILE__, __LINE__, 'LC', $instance->nodeLC($thisNode);
828 # say STDERR join " ", __FILE__, __LINE__, $thisLine, $currentLine;
829 last NODE if $thisLine != $currentLine;
830 PICK_NODE: {
831 my $brickName = $instance->brickName($thisNode);
832  
833 # say STDERR join " ", __FILE__, __LINE__, ($brickName // '[undef]');
834 last PICK_NODE if not defined $brickName;
835  
836 # say STDERR join " ", __FILE__, __LINE__, $brickName;
837 last PICK_NODE if $exclusions->{$brickName};
838  
839 # say STDERR join " ", __FILE__, __LINE__, $brickName;
840 return $thisNode;
841 }
842 $thisNode = $thisNode->{PARENT};
843 }
844  
845 # say STDERR join " ", __FILE__, __LINE__, "returning from nearestBrickOfLine";
846  
847 return $node;
848 }
849  
850 my $semantics = <<'EOS';
851 :default ::= action=>MarpaX::Hoonlint::doNode
852 lexeme default = latm => 1 action=>[start,length,name]
853 EOS
854  
855 my $parser =
856 MarpaX::Hoonlint::YAHC->new( { semantics => $semantics, all_symbols => 1 } );
857 my $dsl = $parser->dsl();
858  
859 $MarpaX::Hoonlint::grammar = $parser->rawGrammar();
860 my %baseLintInstance = ();
861 $baseLintInstance{parser} = $parser;
862 $baseLintInstance{grammar} = $MarpaX::Hoonlint::grammar;
863  
864 my %NYI_Rule = ();
865 $NYI_Rule{$_} = 1 for qw();
866 $baseLintInstance{NYI_Rule} = \%NYI_Rule;
867  
868 my %tallRuneRule = map { +( $_, 1 ) } grep {
869 /^tall[B-Z][aeoiu][b-z][b-z][aeiou][b-z]$/
870 or /^tall[B-Z][aeoiu][b-z][b-z][aeiou][b-z]Mold$/
871 } map { $MarpaX::Hoonlint::grammar->symbol_name($_); }
872 $MarpaX::Hoonlint::grammar->symbol_ids();
873 $baseLintInstance{tallRuneRule} = \%tallRuneRule;
874  
875 # TODO: Check that these are all backdented,
876 my %tallNoteRule = map { +( $_, 1 ) } qw(
877 tallBarhep tallBardot
878 tallBuccab
879 tallCendot tallColcab
880 tallKetbar tallKethep tallKetlus tallKetsig tallKetwut
881 tallSigbar tallSigcab tallSigfas tallSiglus
882 tallTisbar tallTiscom tallTisgal
883 tallWutgal tallWutgar tallWuttis
884 tallZapgar
885 );
886 $baseLintInstance{tallNoteRule} = \%tallNoteRule;
887  
888 my %mortarLHS = map { +( $_, 1 ) }
889 qw(rick5dJog ruck5dJog rick5d ruck5d till5dSeq tall5dSeq
890 fordFile fordHoop fordHoopSeq norm5d tall5d
891 boog5d wisp5d whap5d);
892 $baseLintInstance{mortarLHS} = \%mortarLHS;
893  
894 my %tallBodyRule =
895 map { +( $_, 1 ) } grep { not $tallNoteRule{$_} } keys %tallRuneRule;
896 $baseLintInstance{tallBodyRule} = \%tallBodyRule;
897  
898 # Will include:
899 # BuccenMold BuccolMold BucwutMold
900 # Buccen Buccol Bucwut Colsig Coltar Wutbar Wutpam
901 my %tall_0RunningRule = map { +( $_, 1 ) } qw(
902 tallBuccen tallBuccenMold
903 tallBuccol tallBuccolMold
904 tallBucwut tallBucwutMold
905 tallColsig tallColtar tallTissig
906 tallWutbar tallWutpam);
907 $baseLintInstance{tall_0RunningRule} = \%tall_0RunningRule;
908  
909 my %tall_1RunningRule =
910 map { +( $_, 1 ) } qw( tallDotket tallSemcol tallSemsig tallCencolMold );
911 $baseLintInstance{tall_1RunningRule} = \%tall_1RunningRule;
912  
913 my %tall_1JoggingRule =
914 map { +( $_, 1 ) } qw(tallCentis tallCencab tallWuthep);
915 $baseLintInstance{tall_1JoggingRule} = \%tall_1JoggingRule;
916  
917 my %tall_2JoggingRule = map { +( $_, 1 ) } qw(tallCentar tallWutlus);
918 $baseLintInstance{tall_2JoggingRule} = \%tall_2JoggingRule;
919  
920 my %tallJogging1_Rule = map { +( $_, 1 ) } qw(tallTiscol);
921 $baseLintInstance{tallJogging1_Rule} = \%tallJogging1_Rule;
922  
923 my %joggingRule = map { +( $_, 1 ) } (
924 keys %tall_1JoggingRule,
925 keys %tall_2JoggingRule,
926 keys %tallJogging1_Rule
927 );
928 $baseLintInstance{joggingRule} = \%joggingRule;
929  
930 my %tallLuslusRule =
931 map { +( $_, 1 ) } qw(LuslusCell LushepCell LustisCell);
932 $baseLintInstance{tallLuslusRule} = \%tallLuslusRule;
933  
934 my %barcenAnchorExceptions = ();
935 $barcenAnchorExceptions{$_} = 1
936 for qw(tallTisgar tallTisgal LuslusCell LushepCell LustisCell);
937 $baseLintInstance{barcenAnchorExceptions} = \%barcenAnchorExceptions;
938  
939 my %tallJogRule = map { +( $_, 1 ) } qw(rick5dJog ruck5dJog);
940 $baseLintInstance{tallJogRule} = \%tallJogRule;
941  
942 my %tallBackdentRule = map { +( $_, 1 ) } qw(
943 bonz5d
944 fordFascol
945 fordFasket
946 fordFaspam
947 fordFassem
948 tallBarcol
949 tallBarsig
950 tallBartar
951 tallBartis
952 tallBuchep
953 tallBuchepMold
954 tallBucket
955 tallBucketMold
956 tallBucpat
957 tallBuctisMold
958 tallCenhep
959 tallCenhepMold
960 tallCenket
961 tallCenlus
962 tallCenlusMold
963 tallCensig
964 tallCentar
965 tallColhep
966 tallColket
967 tallCollus
968 tallDottar
969 tallDottis
970 tallKetcen
971 tallKettis
972 tallSigbuc
973 tallSigcen
974 tallSiggar
975 tallSigpam
976 tallSigwut
977 tallSigzap
978 tallTisdot
979 tallTisfas
980 tallTisgar
981 tallTishep
982 tallTisket
983 tallTislus
984 tallTissem
985 tallTistar
986 tallTiswut
987 tallWutcol
988 tallWutdot
989 tallWutket
990 tallWutpat
991 tallWutsig
992 tallZapcol
993 tallZapdot
994 tallZaptis
995 tallZapwut
996 );
997 $baseLintInstance{backdentedRule} = \%tallBackdentRule;
998  
999 $baseLintInstance{ruleDB} = [];
1000 $baseLintInstance{symbolDB} = [];
1001 $baseLintInstance{symbolReverseDB} = {};
1002  
1003 testStyleCensus(\%baseLintInstance);
1004  
1005 sub new {
1006 my ( $class, $config ) = (@_);
1007 my $fileName = $config->{fileName};
1008 my %lint = (%{$config}, %baseLintInstance);
1009 my $lintInstance = \%lint;
1010  
1011 bless $lintInstance, "MarpaX::Hoonlint";
1012 my $policies = $lintInstance->{policies};
1013 my $pSource = $lintInstance->{pHoonSource};
1014 my $parser = $lintInstance->{parser};
1015 $lintInstance->{topicLines} = {};
1016 $lintInstance->{mistakeLines} = {};
1017  
1018 my @data = ();
1019  
1020 $parser->read($pSource);
1021  
1022 $MarpaX::Hoonlint::recce = $parser->rawRecce();
1023 $lintInstance->{recce} = $MarpaX::Hoonlint::recce;
1024 $lintInstance->{nodeCount} = 0;
1025  
1026 $parser = undef; # free up memory
1027 my $astRef = $MarpaX::Hoonlint::recce->value($lintInstance);
1028  
1029 my @lineToPos = ( -1, 0 );
1030 {
1031 my $lastPos = 0;
1032 LINE: while (1) {
1033 my $newPos = index ${$pSource}, "\n", $lastPos;
1034  
1035 # say $newPos;
1036 last LINE if $newPos < 0;
1037 $lastPos = $newPos + 1;
1038 push @lineToPos, $lastPos;
1039 }
1040 }
1041 $lintInstance->{lineToPos} = \@lineToPos;
1042  
1043 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper(\@lineToPos);
1044  
1045 die "Parse failed" if not $astRef;
1046  
1047 # local $Data::Dumper::Deepcopy = 1;
1048 # local $Data::Dumper::Terse = 1;
1049 # local $Data::Dumper::Maxdepth = 3;
1050  
1051 my $astValue = ${$astRef};
1052  
1053 for my $policyShortName ( keys %{$policies} ) {
1054 my $policyFullName = $policies->{$policyShortName};
1055 my $constructor = UNIVERSAL::can( $policyFullName, 'new' );
1056 my $policy = $constructor->( $policyFullName, $lintInstance );
1057 $policy->{shortName} = $policyShortName;
1058 $policy->{fullName} = $policyFullName;
1059 $policy->{perNode} = {};
1060 $policy->validate($astValue);
1061 }
1062  
1063 print $lintInstance->contextDisplay();
1064  
1065 my $unusedSuppressions = $lintInstance->{unusedSuppressions};
1066 for my $lc ( keys %{$unusedSuppressions} ) {
1067 my $perLCSuppressions = $unusedSuppressions->{$lc};
1068 for my $policy (
1069 grep { $perLCSuppressions->{$_} }
1070 keys %{$perLCSuppressions}
1071 )
1072 {
1073 my $perPolicySuppressions = $perLCSuppressions->{$policy};
1074 for my $subpolicy (
1075 grep { $perPolicySuppressions->{$_} }
1076 keys %{$perPolicySuppressions}
1077 )
1078 {
1079 say "Unused suppression: $fileName $lc $policy $subpolicy";
1080 }
1081 }
1082 }
1083  
1084 return $lintInstance;
1085 }
1086  
1087 1;
1088  
1089 # vim: expandtab shiftwidth=4:
« Hoonlint/yahc.pm ↑ Summary