Hoonlint/Policy/Test/Whitespace.pm untested

filter: Statementstmt 0.0% 0 / 3085 Branchbran 0.0% 0 / 790 Conditioncond 0.0% 0 / 187 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 88 Podpod 0.0% 0 / 81 totaltotal 0.0% 0 / 4231 SCAR 119.5
CC
392
Cov
0%
validate_node
2970.0
checkGapComments
1122.0
checkBackdented
756.0
CRAP 154056.0
1 # Hoon whitespace "test" policy
2  
3 package MarpaX::Hoonlint::Policy::Test::Whitespace;
4  
5 use 5.010;
6 use strict;
7 use warnings;
8 no warnings 'recursion';
9  
10 # use Carp::Always;
11 use Data::Dumper;
12 use English qw( -no_match_vars );
13 use Scalar::Util qw(looks_like_number weaken);
14  
15 # say STDERR join " ", __FILE__, __LINE__, "hi";
16  
17 my %reanchorings = ();
18  
19 my $reanchorings = \<<'EOS';
20 bardot:
21 kettis # fixes 6, breaks 0
22 cenlus # fixes 4, breaks 0
23 luslus # fixes 3, breaks 2
24 cenhep # fixes 1, breaks 0
25 barhep:
26 # 1 ^=
27 # 1 =.
28 kettis # fixes 1, breaks 0
29 tisdot # fixes 1, breaks 0
30 # bartar
31 bartar:
32 # 1 ++
33 # luslus # fixes 1, breaks 14
34 # bartis
35 bartis:
36 # 2 :-
37 # 2 %+
38 # 1 %-
39 # 1 ++
40 colhep # fixes 2, breaks 0
41 # tallCenlus # fixes 2, breaks 6
42 # tallCenhep # fixes 1, breaks 3
43 # luslus # fixes 1, breaks 24
44 # buctis
45 buctis:
46 # tallBuccol # fixes 1, breaks 2
47 # cendot
48 cendot:
49 # 4 |=
50 # 3 =<
51 # 1 =-
52 # 1 %-
53 bartis # fixes 7, breaks 0
54 # tallTisgal # fixes 1, breaks 2
55 # tallTishep # fixes 0, breaks 1
56 cenhep
57 cenhep:
58 # 6 %-
59 # 4 =+
60 # 1 =<
61 # 1 :-
62 cenhep # fixes 6, breaks 1
63 tislus # fixes 4, breaks 2
64 tisgal # fixes 1, breaks 0
65 colhep # fixes 1, breaks 0
66 # cenket
67 cenket:
68 # 1 =.
69 # tallCendot # fixes 0, breaks 0
70 cenlus:
71 cenhep # fixes 8, breaks 0
72 tislus # fixes 11, breaks 2
73 colcab:
74 colcab # fixes 13, breaks 0
75 cenhep # fixes 12, breaks 0
76 cenlus # fixes 6, breaks 0
77 # 'tallColhep' # fixes 1, breaks 2
78 colhep:
79 # 'tallColhep' # fixes 2, breaks 4
80 cenlus # fixes 3, breaks 0
81 colcab # fixes 3, breaks 0
82 # 'tallColsig' # fixes 0, breaks 5
83 dottis:
84 # 1 ?.
85 kethep:
86 barhep # Needed for examples to work
87 bartis
88 cenhep
89 colhep # fixes 3, breaks 2
90 tistar
91 barsig
92 ketsig
93 tisfas
94 kethep # fixes 4, breaks 0
95 tishep # partially fixes one
96 tisgal # partially fixes one
97 tisdot # fixes 1, breaks 0
98 # 'tallBarket' # fixes 3, breaks 9
99 siglus # fixes 1, breaks 0
100 zapgar # fixes 1, breaks 0
101 cenlus # fixes 3, breaks 0
102 bardot # fixes 1, breaks 0
103 # luslus # breaks tic-tac-toe, line 55
104 # =+ ^= [...] ^- occurs a lot, and the reanchoring
105 # seems to want to be at the KETTIS, not the TISLUS.
106 # But where not followed by KETTIS, reanchoring
107 # at TISLUS seems to be indicated in a lot of places.
108 # This accounts for a lot of aberrations.
109 kettis
110 # 'tallTislus' # fixes 30, breaks 76
111 ketlus:
112 barhep # Needed for examples to work
113 bartis # fixes 97, breaks 0
114 tisgal # fixes 8, breaks 0
115 bardot # fixes 8, breaks 0
116 bartar # fixes 4, breaks 0
117 cenhep # fixes 2, breaks 0
118 # ketsig
119 ketsig:
120 # 1 %+
121 cenlus # fixes 1, breaks 0
122 kettis:
123 # tallTislus # breaks 109, fixes 24
124 ketwut:
125 luslus
126 buccab
127 sigcab:
128 # 1 |=
129 bartis # fixes 1, breaks 0
130 sigfas:
131 luslus # fixes 44, breaks 4
132 # siglus
133 siglus:
134 # 25 %+
135 # 3 ++
136 # 2 |=
137 # 1 =+
138 # 1 |.
139 cenlus # fixes 26, breaks 0
140 # luslus # fixes 1, breaks 1
141 bartis # fixes 1, breaks 0
142 # tislus # fixes 0, breaks 0
143 bardot # fixes 1, breaks 0
144 # tisbar
145 tisbar:
146 # 1 |=
147 # 1 $_
148 bartis # fixes 1, breaks 0
149 buccab # fixes 1, breaks 0
150 # tiscom
151 tiscom:
152 # 1 =,
153 tiscom # fixes 1, breaks 0
154 # tisdot
155 tisdot:
156 # 1 =+
157 tislus # fixes 1, breaks 0
158 tisgal:
159 bartis # fixes 20, breaks 0
160 tisgal # fixes 14, breaks 0
161 cenhep # fixes 7, breaks 0
162 ketlus # fixes 4, breaks 0
163 tisgar # fixes 2, breaks 0
164 cenlus # fixes 1, breaks 1
165 # tisgar
166 tisgar:
167 # 3 %+
168 # 2 ++
169 # 2 =+
170 # 1 =>
171 # 1 =-
172 cenlus # fixes 1, breaks 0
173 # luslus # fixes 0, breaks 1
174 # tislus # fixes 0, breaks 1
175 tisgar # fixes 1, breaks 0
176 # tishep # fixes 0, breaks 1
177 # tislus
178 tislus:
179 wutcol:
180 barhep # fixes 6, breaks 0
181 wutcol # fixes 3, breaks 0
182 # tallTisdot # fixes 0, breaks 8
183 # tallTislus # fixes 0, breaks 1
184 cenhep # fixes 1, breaks 0
185 # wutdot
186 wutdot:
187 # 3 |-
188 # 1 =.
189 # 1 ?.
190 barhep # fixes 5, breaks 0
191 # tallTisdot # fixes 1, breaks 3
192 # tallWutdot # fixes 0, breaks 0
193 # wutgal
194 wutgal:
195 # 1 |-
196 barhep # fixes 1, breaks 0
197 # wutgar
198 wutgar:
199 # 2 ~|
200 sigbar # fixes 2, breaks 0
201 wutsig:
202 wutsig # fixes 3, breaks 0
203 tislus # fixes 2, break 1
204 # tallBarhep # fixes 3, breaks 5
205 zapcol:
206 luslus # fixes 8, breaks 1
207 # zapdot
208 zapdot:
209 # 6 ++
210 luslus # fixes 6, breaks 0
211 zapgar:
212 cenhep # fixes 89, breaks 0
213 EOS
214  
215 {
216  
217 # special cases for grammar names
218 my %grammarNames = (
219 luslus => 'LuslusCell',
220 lustis => 'LustisCell',
221 lushep => 'LushepCell',
222 );
223  
224 my $currentSource;
225  
226 ITEM: for my $itemLine ( split "\n", ${$reanchorings} ) {
227 my $rawItemLine = $itemLine;
228 $itemLine =~ s/\s*[#].*$//; # remove comments and preceding whitespace
229 $itemLine =~ s/^\s*//; # remove leading whitespace
230 $itemLine =~ s/\s*$//; # remove trailing whitespace
231 next ITEM unless $itemLine;
232  
233 if (my ($source) = $itemLine =~ m/^(.*) *:$/) {
234 $currentSource = $source;
235 next ITEM;
236 }
237  
238 die qq{Error in inline reanchorings: No source rune\n}
239 . qq{ Problem with line: $rawItemLine\n}
240 if not $currentSource;
241  
242 chomp $itemLine;
243 my $target = $itemLine;
244  
245 my $grammarName = $grammarNames{$target};
246 if (not defined $grammarName) {
247 $grammarName = 'tall' . ucfirst $target;
248 }
249  
250 $reanchorings{$currentSource}{$grammarName} = 1;
251  
252 }
253 }
254  
255 # die Data::Dumper::Dumper(\%reanchorings);
256  
257 # TODO: delete indents in favor of tree traversal
258  
259 my $gapCommentDSL = <<'END_OF_DSL';
260 :start ::= gapComments
261 gapComments ::= OptExceptions Body
262 gapComments ::= OptExceptions
263 Body ::= InterPart PrePart
264 Body ::= InterPart
265 Body ::= PrePart
266 InterPart ::= InterComponent
267 InterPart ::= InterruptedInterComponents
268 InterPart ::= InterruptedInterComponents InterComponent
269  
270 InterruptedInterComponents ::= InterruptedInterComponent+
271 InterruptedInterComponent ::= InterComponent Exceptions
272 InterComponent ::= Staircases
273 InterComponent ::= Staircases InterComments
274 InterComponent ::= InterComments
275  
276 InterComments ::= InterComment+
277  
278 Staircases ::= Staircase+
279 Staircase ::= UpperRisers Tread LowerRisers
280 UpperRisers ::= UpperRiser+
281 LowerRisers ::= LowerRiser+
282  
283 PrePart ::= ProperPreComponent OptPreComponents
284 ProperPreComponent ::= PreComment
285 OptPreComponents ::= PreComponent*
286 PreComponent ::= ProperPreComponent
287 PreComponent ::= Exception
288  
289 OptExceptions ::= Exception*
290 Exceptions ::= Exception+
291 Exception ::= MetaComment
292 Exception ::= BadComment
293 Exception ::= BlankLine
294  
295 unicorn ~ [^\d\D]
296 BadComment ~ unicorn
297 BlankLine ~ unicorn
298 InterComment ~ unicorn
299 LowerRiser ~ unicorn
300 MetaComment ~ unicorn
301 PreComment ~ unicorn
302 Tread ~ unicorn
303 UpperRiser ~ unicorn
304  
305 END_OF_DSL
306  
307 # Format line and 0-based column as string
308 sub describeLC {
309 my ( $line, $column ) = @_;
310 return '@' . $line . ':' . ( $column + 1 );
311 }
312  
313 sub describeMisindent {
314 my ($difference) = @_;
315 if ( $difference > 0 ) {
316 return "overindented by $difference";
317 }
318 if ( $difference < 0 ) {
319 return "underindented by " . ( -$difference );
320 }
321 return "correctly indented";
322 }
323  
324 sub describeMisindent2 {
325 my ( $got, $sought ) = @_;
326 $DB::single = 1 if not defined $sought;
327 return describeMisindent( $got - $sought );
328 }
329  
330 sub setInheritedAttribute {
331 my ( $policy, $node, $attribute, $value ) = @_;
332 my $nodeIX = $node->{IX};
333 $policy->{perNode}->{$nodeIX}->{$attribute} = $value;
334 }
335  
336 sub getInheritedAttribute {
337 my ( $policy, $node, $attribute ) = @_;
338 my $nodeIX = $node->{IX};
339 my $value = $policy->{perNode}->{$nodeIX}->{$attribute};
340 return $value if defined $value;
341 $node = $node->{PARENT};
342 die qq{Ascended tree but did not find attributee "$attribute"} if not $node;
343 $value = $policy->getInheritedAttribute($node, $attribute);
344 return $value;
345 }
346  
347 sub new {
348 my ( $class, $lintInstance ) = @_;
349 my $policy = {};
350 $policy->{lint} = $lintInstance;
351 my %chainable = ();
352 for my $key (keys %{ $lintInstance->{backdentedRule} }, keys %{ $lintInstance->{tallNoteRule} }) {
353 $chainable{$key} = 1;
354 }
355 $policy->{chainable} = \%chainable;
356 Scalar::Util::weaken( $policy->{lint} );
357 $policy->{gapGrammar} =
358 Marpa::R2::Scanless::G->new( { source => \$gapCommentDSL } );
359 return bless $policy, $class;
360 }
361  
362 # Return Perl true is node is chainable
363 sub chainable {
364 my ( $policy, $node ) = @_;
365 my $chainable = $policy->{chainable};
366 my $instance = $policy->{lint};
367 my $grammar = $instance->{grammar};
368 my $ruleID = $node->{ruleID};
369 return if not $ruleID;
370 my ($lhs) = $grammar->rule_expand( $node->{ruleID} );
371 my $lhsName = $grammar->symbol_name($lhs);
372 # say STDERR join ' ', __FILE__, __LINE__, $lhsName, ($chainable->{$lhsName} // 'na');
373 return $chainable->{$lhsName};
374 }
375  
376 # Return the node tag for the subpolicy field.
377 # Archetypally, this is the 6-character form of
378 # rune for the node's brick.
379 sub runeName {
380 my ( $policy, $node ) = @_;
381 my $instance = $policy->{lint};
382 my $name = $instance->forceBrickName($node);
383 if ( my ($tag) = $name =~ /^(Lus[b-z][aeiou][b-z])Cell$/ ) {
384 return lc $tag;
385 }
386 if ( my ($tag) = $name =~ /^optFord([B-Z][aeoiu][b-z][b-z][aeiou][b-z])$/ ) {
387 return lc $tag;
388 }
389 if ( my ($tag) = $name =~ /^ford([B-Z][aeoiu][b-z][b-z][aeiou][b-z])$/ ) {
390 return lc $tag;
391 }
392 if ( my ($tag) = $name =~ /^tall([B-Z][aeoiu][b-z][b-z][aeiou][b-z])$/ ) {
393 return lc $tag;
394 }
395 if ( my ($tag) = $name =~ /^tall([B-Z][aeoiu][b-z][b-z][aeiou][b-z])Mold$/ )
396 {
397 return lc $tag;
398 }
399 return lc $name;
400 }
401  
402 sub nodeSubpolicy {
403 my ( $policy, $node ) = @_;
404 return $policy->runeName($node);
405 }
406  
407 # return standard anchor "detail" line
408 sub anchorDetailsBasic {
409 my ( $policy, $rune, $anchorColumn ) = @_;
410 my $instance = $policy->{lint};
411 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
412 my $anchorLiteral = $instance->literalLine($runeLine);
413 my $anchorLexeme = substr $anchorLiteral, $anchorColumn;
414 $anchorLexeme =~ s/[\s].*\z//xms;
415 my $typeVerb = ( $anchorColumn == $runeColumn ) ? "anchor" : "re-anchor";
416 return [qq{$typeVerb column is }
417 . describeLC( $runeLine, $anchorColumn )
418 . qq{ "$anchorLexeme"} ];
419 }
420  
421 sub anchorDetails {
422 my ( $policy, $rune, $anchorData ) = @_;
423 my @desc = ();
424 my $instance = $policy->{lint};
425 my $brick = $anchorData->{brick};
426  
427 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
428 my ( $brickLine, $brickColumn ) = $instance->nodeLC($brick);
429 my $anchorColumn = $anchorData->{column};
430 my $offset = $anchorData->{offset};
431 my $runeLineLiteral = $instance->literalLine($runeLine);
432 $runeLineLiteral =~ s/\n\z//xms;
433  
434 if ( $anchorColumn == $runeColumn ) {
435 my $brickLiteral = $instance->literalLine($runeLine);
436 my $brickLexeme = substr $brickLiteral, $brickColumn;
437 $brickLexeme =~ s/[\s].*\z//xms;
438 return [sprintf 'anchor column is "%s" %s',
439 $brickLexeme,
440 describeLC( $runeLine, $anchorColumn )
441 ];
442 }
443 push @desc,
444 sprintf
445 're-anchor column (%d) = anchor brick column (%d) + re-anchor offset (%d)',
446 $anchorColumn + 1, $brickColumn + 1, $offset;
447 my $maxNumWidth = $instance->maxNumWidth();
448 my $pointersPrefix = ( ' ' x $maxNumWidth );
449 my $prefixLength = length $pointersPrefix;
450 push @desc, sprintf '%s%s', $pointersPrefix, $runeLineLiteral;
451 my $pointerLine = ( ' ' x ( $runeColumn + $prefixLength ) ) . q{^};
452 substr( $pointerLine, ( $brickColumn + $prefixLength ), 1 ) = q{^};
453 substr( $pointerLine, ( $anchorColumn + $prefixLength ), 1 ) = q{!};
454 push @desc, $pointerLine;
455 return \@desc;
456 }
457  
458 # first brick node in $node's line,
459 # by inclusion list.
460 # $node if there is no prior included brick node
461 sub reanchorInc {
462 my ( $policy, $node, $inclusions ) = @_;
463 my $instance = $policy->{lint};
464  
465 my ($currentLine) = $instance->nodeLC($node);
466 my $thisNode = $node;
467 my $firstBrickNode = $node;
468 my @nodes = ();
469  
470 # Accumulate a list of the nodes on the same line as
471 # the argument node
472 NODE: while ($thisNode) {
473 my ($thisLine) = $instance->nodeLC($thisNode);
474 last NODE if $thisLine != $currentLine;
475 push @nodes, $thisNode;
476 $thisNode = $thisNode->{PARENT};
477 }
478 my $topNodeIX;
479 my $brick = $node;
480 my $reanchorOffset = 0;
481 SET_DATA: {
482 PICK_NODE: for ( my $nodeIX = $#nodes ; $nodeIX >= 0 ; $nodeIX-- ) {
483 my $thisNode = $nodes[$nodeIX];
484 my $brickName = $instance->brickName($thisNode);
485 if ( defined $brickName and $inclusions->{$brickName} ) {
486 $topNodeIX = $nodeIX;
487 last PICK_NODE;
488 }
489 }
490 last SET_DATA if not defined $topNodeIX;
491 for (
492 my $nodeIX = 1 ; # do not include first node
493 $nodeIX <= $topNodeIX ; $nodeIX++
494 )
495 {
496 my $thisNode = $nodes[$nodeIX];
497 my $nodeID = $thisNode->{IX};
498 my $thisReanchorOffset =
499 $policy->{perNode}->{$nodeID}->{reanchorOffset} // 0;
500 $reanchorOffset += $thisReanchorOffset;
501 }
502 $brick = $nodes[$topNodeIX];
503 }
504 my ( $brickLine, $brickColumn ) = $instance->nodeLC($brick);
505 my $column = $brickColumn + $reanchorOffset;
506 my %results = (
507 brick => $brick,
508 offset => $reanchorOffset,
509 column => $column,
510 line => $brickLine
511 );
512 return $column, \%results;
513 }
514  
515 # A "gapSeq" is an ordered subset of a node's children.
516 # It consists of the first child, followed by zero or more
517 # pairs of nodes, where each pair is a gap and it post-gap
518 # symbol. It is assumed that the first child is not a gap,
519 # and no post-gap child is a gap. The sequence will always
520 # be of odd length.
521 #
522 # Intuitively, this is usually the subset of the children with
523 # information useful for parsing.
524 sub gapSeq {
525 my ( $policy, $node ) = @_;
526 my $instance = $policy->{lint};
527 my $symbolReverseDB = $instance->{symbolReverseDB};
528 my $children = $node->{children};
529 my $child = $children->[0];
530 my @gapSeq = ($child);
531  
532 my $childIX = 1;
533 CHILD: while ( $childIX < $#$children ) {
534 $child = $children->[$childIX];
535 my $symbol = $child->{symbol};
536 if ( not defined $symbol
537 or not $symbolReverseDB->{$symbol}->{gap} )
538 {
539 $childIX++;
540 next CHILD;
541 }
542 my $nextChild = $children->[ $childIX + 1 ];
543 push @gapSeq, $child, $nextChild;
544 $childIX += 2;
545 }
546 return \@gapSeq;
547 }
548  
549 # A variant of "gapSeq" which relaxes the assumption that
550 # the first child is not a gap, and which returns an
551 # alternating sequence of gap and post-gap. It assumes
552 # that a gap does not follow another gap.
553 sub gapSeq0 {
554 my ( $policy, $node ) = @_;
555 my $instance = $policy->{lint};
556 my $symbolReverseDB = $instance->{symbolReverseDB};
557 my $children = $node->{children};
558 my @gapSeq = ();
559  
560 my $childIX = 0;
561 CHILD: while ( $childIX < $#$children ) {
562 my $child = $children->[$childIX];
563 my $symbol = $child->{symbol};
564 if ( not defined $symbol
565 or not $symbolReverseDB->{$symbol}->{gap} )
566 {
567 $childIX++;
568 next CHILD;
569 }
570 my $nextChild = $children->[ $childIX + 1 ];
571 push @gapSeq, $child, $nextChild;
572 $childIX += 2;
573 }
574 return \@gapSeq;
575 }
576  
577 # Checks a gap to see if it is OK as a pseudo-join.
578 # If so, returns the column at which code may resume.
579 # Otherwise returns -1;
580  
581 sub pseudojoinColumn {
582 my ( $policy, $gap ) = @_;
583 my $instance = $policy->{lint};
584 my $gapLiteral = $instance->literalNode($gap);
585 my $gapStart = $gap->{start};
586 my $gapEnd = $gap->{start} + $gap->{length};
587  
588 if ($instance->runeGapNode($gap)) {
589 $gapLiteral = substr( $gapLiteral, 2 );
590 $gapStart += 2;
591 }
592  
593 my ( $startLine, $startColumn ) = $instance->line_column($gapStart);
594 my ( $endLine, $endColumn ) = $instance->line_column($gapEnd);
595  
596 my $commentColumn;
597  
598 # first partial line (must exist)
599 my $firstNewline = index $gapLiteral, "\n";
600 return if $firstNewline < 0;
601 my $firstColon = index $gapLiteral, ':';
602 if ( $firstColon >= 0 and $firstColon < $firstNewline ) {
603 ( undef, $commentColumn ) =
604 $instance->line_column( $gapStart + $firstColon );
605 }
606  
607 # say STDERR join ' ', __FILE__, __LINE__;
608 return -1 if not $commentColumn;
609 # say STDERR join ' ', __FILE__, __LINE__;
610  
611 # If the last line of the gap does not end in a newline,
612 # it **cannot** contain a comment, because the parser would
613 # recognize the whole comment as part of the gap.
614 # So we only look for properly aligned comments in full
615 # (that is, newline-terminated) lines.
616  
617 my $lastFullLine =
618 ( substr $gapLiteral, -1, 1 ) eq "\n" ? $endLine : $endLine - 1;
619 for my $lineNum ( $startLine + 1 .. $lastFullLine ) {
620 my $literalLine = $instance->literalLine($lineNum);
621 my $commentOffset = index $literalLine, ':';
622  
623 # say STDERR join ' ', __FILE__, __LINE__;
624 return -1 if $commentOffset < 0;
625 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset, $commentColumn;
626 return -1 if $commentOffset != $commentColumn;
627 # say STDERR join ' ', __FILE__, __LINE__;
628 }
629 return $commentColumn;
630 }
631  
632 # Is this a valid join gap?
633 # Return undef if not.
634 # Return -1 if flat join.
635 # Return pseudo-join column if pseudo-join.
636 sub checkJoinGap {
637 my ( $policy, $gap ) = @_;
638 my $instance = $policy->{lint};
639 my $gapLiteral = $instance->literalNode($gap);
640  
641 # say join ' ', __FILE__, __LINE__;
642 # say join q{}, '[', $gapLiteral, ']';
643 return -1 if $gapLiteral =~ m/\A [ ]* \z/xms;
644  
645 # say join ' ', __FILE__, __LINE__;
646 my $column = $policy->pseudojoinColumn($gap);
647 return $column if defined $column and $column >= 0;
648 return;
649 }
650  
651 sub deComment {
652 my ( $policy, $string ) = @_;
653 $string =~ s/ ([+][|]|[:][:]|[:][<]|[:][>]) .* \z//xms;
654 return $string;
655 }
656  
657 # Is this a one-line gap, or its equivalent?
658 sub checkOneLineGap {
659 my ( $policy, $gap, $options ) = @_;
660 my $instance = $policy->{lint};
661 my $start = $gap->{start};
662 my $length = $gap->{length};
663 if ( $instance->runeGapNode($gap) ) {
664 $start += 2;
665 $length -= 2;
666 }
667  
668 my $runeName = $options->{runeName};
669 my $details = $options->{details};
670 my $elementNumber = $options->{elementNumber};
671  
672 my @topicLines = ();
673 my $topicLines = $options->{topicLines};
674 push @topicLines, @{$topicLines} if $topicLines;
675  
676 my @mistakes = ();
677 my $end = $start + $length;
678 my ( $startLine, $startColumn ) = $instance->line_column($start);
679 my ( $endLine, $endColumn ) = $instance->line_column($end);
680  
681 # say STDERR Data::Dumper::Dumper($options);
682  
683 my ( $mainLine, $mainColumn );
684 if ( my $mainNode = $options->{mainNode} ) {
685 ( $mainLine, $mainColumn ) = $instance->nodeLC($mainNode);
686 }
687 $mainColumn //= $options->{mainColumn} // -1;
688  
689 my ( $preLine, $preColumn );
690 if ( my $preNode = $options->{preNode} ) {
691 ( $preLine, $preColumn ) = $instance->nodeLC($preNode);
692 }
693 $preColumn //= $options->{preColumn} // -1;
694  
695 my @subpolicyElements = ();
696 SET_SUBPOLICY: {
697 my $subpolicyArg = $options->{subpolicy};
698 last SET_SUBPOLICY if not defined $subpolicyArg;
699 if ( not ref $subpolicyArg ) {
700 push @subpolicyElements, $subpolicyArg;
701 last SET_SUBPOLICY;
702 }
703 push @subpolicyElements, @{$subpolicyArg};
704 }
705  
706 # Criss-cross TISTIS lines are a special case
707 # say STDERR join " ", __FILE__, __LINE__, $startLine, $endLine;
708 if ( $startLine == $endLine
709 and $instance->literal( $start - 2, 2 ) ne '=='
710 and $instance->literal( $start - 2, 2 ) ne '--' )
711 {
712 my $msg = sprintf
713 "%s %s; %s %s",
714 $runeName,
715 describeLC( $startLine, $startColumn ),
716 "missing newline ",
717 , describeLC( $startLine, $startColumn );
718 return [
719 {
720 desc => $msg,
721 subpolicy =>
722 ( join ':', @subpolicyElements, 'missing-newline' ),
723 runeName => $runeName,
724 line => $startLine,
725 column => $startColumn,
726 reportLine => $startLine,
727 reportColumn => $startColumn,
728 topicLines => \@topicLines,
729 details => $details,
730 }
731 ];
732 }
733  
734 my $bodyStartLine = $start == 0 ? $startLine : $startLine + 1;
735 my $lineToPos = $instance->{lineToPos};
736 if ( $bodyStartLine < $#$lineToPos ) {
737 my $literalFirstLine = $instance->literalLine($bodyStartLine);
738 if ( $literalFirstLine =~ /'''/ ) {
739  
740 # say join ' ', __FILE__, __LINE__, qq{"$literalFirstLine"};
741 $bodyStartLine++;
742 }
743 if ( $literalFirstLine =~ /"""/ ) {
744  
745 # say join ' ', __FILE__, __LINE__, qq{"$literalFirstLine"};
746 $bodyStartLine++;
747 }
748 }
749 my $results = $policy->checkGapComments( $bodyStartLine, $endLine - 1,
750 $mainColumn, $preColumn );
751 RESULT: for my $result ( @{$results} ) {
752 my $type = $result->[0];
753 if ( $type eq 'vgap-blank-line' ) {
754 my ( undef, $lineNum, $offset ) = @{$result};
755 my $msg = sprintf
756 "%s %s; %s",
757 $runeName,
758 describeLC( $lineNum, 0 ),
759 "empty line in comment";
760 push @mistakes,
761 {
762 desc => $msg,
763 subpolicy => ( join ':', @subpolicyElements, 'empty-line' ),
764 runeName => $runeName,
765 reportLine => $lineNum,
766 reportColumn => 0,
767 line => $lineNum,
768 column => 0,
769 topicLines => \@topicLines,
770 details => $details,
771 };
772 next RESULT;
773 }
774 if ( $type eq 'vgap-bad-comment' ) {
775 my ( undef, $lineNum, $offset, $expectedOffset ) = @{$result};
776  
777 my $msg;
778 if ( defined $elementNumber ) {
779 $msg = sprintf
780 "%s child %d %s; %s %s",
781 $runeName,
782 $elementNumber,
783 describeLC( $lineNum, $offset ),
784 "comment",
785 describeMisindent2( $offset, $expectedOffset );
786 }
787 else {
788 $msg = sprintf
789 "%s d %s; %s %s",
790 $runeName,
791 describeLC( $lineNum, $offset ),
792 "comment",
793 describeMisindent2( $offset, $expectedOffset );
794 }
795 push @mistakes,
796 {
797 desc => $msg,
798 subpolicy => ( join ':', @subpolicyElements, 'comment-indent' ),
799 runeName => $runeName,
800 reportLine => $lineNum,
801 reportColumn => $offset,
802 line => $lineNum,
803 column => $offset,
804 topicLines => \@topicLines,
805 details => $details,
806 };
807 }
808 }
809  
810 return \@mistakes;
811 }
812  
813  
814 sub checkGapComments {
815 my ( $policy, $firstLine, $lastLine, $interOffset, $preOffset ) = @_;
816  
817 return if $lastLine < $firstLine;
818 my $instance = $policy->{lint};
819 my $pSource = $instance->{pHoonSource};
820 my $lineToPos = $instance->{lineToPos};
821 SET_PREOFFSET: {
822 last SET_PREOFFSET if not defined $preOffset;
823 if ( $preOffset < 0 ) {
824 $preOffset = undef; # negative offset == undefined
825 last SET_PREOFFSET;
826 }
827 if ( $preOffset == $interOffset ) {
828  
829 # Do not allow pre-offset to be equal to inter-offset
830 $preOffset = undef;
831 last SET_PREOFFSET;
832 }
833 }
834 my @mistakes = ();
835  
836 my $grammar = $policy->{gapGrammar};
837 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
838 my $startPos = $lineToPos->[$firstLine];
839 my $input = $instance->literal( $startPos,
840 ( $lineToPos->[ $lastLine + 1 ] - $startPos ) );
841  
842 # say STDERR join ' ', __FILE__, __LINE__, "$firstLine-$lastLine", qq{"$input"};
843  
844 if ( not defined eval { $recce->read( $pSource, $startPos, 0 ); 1 } ) {
845  
846 my $eval_error = $EVAL_ERROR;
847 chomp $eval_error;
848 say STDERR join ' ', __FILE__, __LINE__, "$firstLine-$lastLine",
849 qq{"$input"};
850 die $eval_error, "\n";
851 }
852  
853 my $lineNum = 0;
854 LINE:
855 for ( my $lineNum = $firstLine ; $lineNum <= $lastLine ; $lineNum++ ) {
856 my $line = $instance->literalLine($lineNum);
857  
858 # say STDERR join ' ', __FILE__, __LINE__, $lineNum, qq{"$line"};
859  
860 FIND_ALTERNATIVES: {
861 my $expected = $recce->terminals_expected();
862  
863 # say Data::Dumper::Dumper($expected);
864 my $tier1_ok;
865 my @tier2 = ();
866 my @failedOffsets = ();
867 TIER1: for my $terminal ( @{$expected} ) {
868  
869 # say STDERR join ' ', __FILE__, __LINE__, $terminal;
870 if ( $terminal eq 'InterComment' ) {
871 $line =~ m/^ [ ]* ([+][|]|[:][:]|[:][<]|[:][>]) /x;
872 my $commentOffset = $LAST_MATCH_START[1];
873 $commentOffset //= -1;
874  
875 # say STDERR join ' ', __FILE__, __LINE__, qq{"$line"};
876 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset;
877 if ( $commentOffset == $interOffset ) {
878  
879 # say STDERR join ' ', __FILE__, __LINE__;
880 $recce->lexeme_alternative( $terminal, $line );
881 $tier1_ok = 1;
882 next TIER1;
883 }
884 push @failedOffsets, $interOffset;
885 next TIER1;
886 }
887 if ( $terminal eq 'Tread' ) {
888 $line =~ m/^ [ ]* ([:][:][:][:][ \n]) /x;
889 my $commentOffset = $LAST_MATCH_START[1];
890 $commentOffset //= -1;
891  
892 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset;
893 if ( $commentOffset == $interOffset ) {
894  
895 # say STDERR join ' ', __FILE__, __LINE__;
896 $recce->lexeme_alternative( $terminal, $line );
897 $tier1_ok = 1;
898 next TIER1;
899 }
900 push @failedOffsets, $interOffset;
901 next TIER1;
902 }
903 if ( $terminal eq 'UpperRiser' ) {
904 $line =~ m/^ [ ]* ([:][:]) /x;
905 my $commentOffset = $LAST_MATCH_START[1];
906 $commentOffset //= -1;
907  
908 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset;
909 if ( $commentOffset == $interOffset ) {
910  
911 # say STDERR join ' ', __FILE__, __LINE__;
912 $recce->lexeme_alternative( $terminal, $line );
913 $tier1_ok = 1;
914 next TIER1;
915 }
916 push @failedOffsets, $interOffset;
917 next TIER1;
918 }
919 if ( $terminal eq 'LowerRiser' ) {
920 $line =~ m/^ [ ]* ([:][:]) /x;
921 my $commentOffset = $LAST_MATCH_START[1];
922 $commentOffset //= -1;
923  
924 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset;
925 if ( $commentOffset == $interOffset + 2 ) {
926  
927 # say STDERR join ' ', __FILE__, __LINE__;
928 $recce->lexeme_alternative( $terminal, $line );
929 $tier1_ok = 1;
930 next TIER1;
931 }
932 push @failedOffsets, $interOffset;
933 next TIER1;
934 }
935 push @tier2, $terminal;
936 }
937  
938 # If we found a tier 1 lexeme, do not look for the "backup"
939 # lexemes on the other tiers
940 last FIND_ALTERNATIVES if $tier1_ok;
941  
942 my $tier2_ok;
943 my @tier3 = ();
944 TIER2: for my $terminal (@tier2) {
945 if ( $terminal eq 'PreComment' ) {
946 next TIER2 if not defined $preOffset;
947 $line =~ m/^ [ ]* ([+][|]|[:][:]|[:][<]|[:][>]) /x;
948 my $commentOffset = $LAST_MATCH_START[1];
949 $commentOffset //= -1;
950  
951 # say STDERR join ' ', __FILE__, __LINE__, $commentOffset;
952 if ( $commentOffset == $preOffset ) {
953  
954 # say STDERR join ' ', __FILE__, __LINE__;
955 $recce->lexeme_alternative( $terminal, $line );
956 $tier2_ok = 1;
957 next TIER2;
958 }
959 push @failedOffsets, $preOffset;
960 next TIER2;
961 }
962 push @tier3, $terminal;
963 }
964  
965 # If we found a tier 2 lexeme, do not look for the "backup"
966 # lexemes on the other tiers
967 last FIND_ALTERNATIVES if $tier2_ok;
968  
969 my @tier4 = ();
970 TIER3: for my $terminal (@tier3) {
971 if ( $terminal eq 'MetaComment' ) {
972 $line =~ m/^ [ ]* ([+][|]|[:][:]|[:][<]|[:][>]) /x;
973 my $commentOffset = $LAST_MATCH_START[1];
974 next TIER3 if not defined $commentOffset;
975 if ( $commentOffset == 0 ) {
976 $recce->lexeme_alternative( $terminal, $line );
977  
978 # anything in this tier terminates the finding of alternatives
979 last FIND_ALTERNATIVES;
980 }
981 push @failedOffsets, $interOffset;
982 }
983 push @tier4, $terminal;
984 }
985  
986 TIER4: for my $terminal (@tier4) {
987 if ( $terminal eq 'BlankLine' ) {
988  
989 # say STDERR join ' ', __FILE__, __LINE__, $lineNum, qq{"$line"};
990 if ( $line =~ m/\A [\n ]* \z/xms ) {
991 $recce->lexeme_alternative( $terminal, $line );
992  
993 # anything in this tier terminates the finding of alternatives
994 push @mistakes, [ 'vgap-blank-line', $lineNum ];
995 last FIND_ALTERNATIVES;
996 }
997 }
998 if ( $terminal eq 'BadComment' ) {
999 if ( $line =~ m/^ [ ]* ([+][|]|[:][:]|[:][<]|[:][>]) /x ) {
1000 $recce->lexeme_alternative( $terminal, $line );
1001 my $commentOffset = $LAST_MATCH_START[1];
1002  
1003 my $closestHiOffset;
1004 my $closestLoOffset;
1005  
1006 for my $failedOffset (@failedOffsets) {
1007 if ( $failedOffset > $commentOffset ) {
1008 if ( not defined $closestHiOffset
1009 or $failedOffset < $closestHiOffset )
1010 {
1011 $closestHiOffset = $failedOffset;
1012 }
1013 }
1014 if ( $failedOffset < $commentOffset ) {
1015 if ( not defined $closestLoOffset
1016 or $failedOffset > $closestLoOffset )
1017 {
1018 $closestLoOffset = $failedOffset;
1019 }
1020 }
1021 }
1022 my $closestOffset =
1023 ( $closestLoOffset // $closestHiOffset );
1024  
1025 # say STDERR join ' ', __LINE__, 'vgap-bad-comment', $lineNum, $commentOffset, $closestOffset ;
1026 push @mistakes,
1027 [
1028 'vgap-bad-comment', $lineNum,
1029 $commentOffset, $closestOffset
1030 ];
1031  
1032 # anything in this tier terminates the finding of alternatives
1033 last FIND_ALTERNATIVES;
1034 }
1035 }
1036 }
1037  
1038 }
1039 my $startPos = $lineToPos->[$lineNum];
1040  
1041 # say STDERR join ' ', __FILE__, __LINE__;
1042 my $eval_ok = eval {
1043 $recce->lexeme_complete( $startPos,
1044 ( $lineToPos->[ $lineNum + 1 ] - $startPos ) );
1045 1;
1046 };
1047 if ( not $eval_ok ) {
1048  
1049 my $eval_error = $EVAL_ERROR;
1050 chomp $eval_error;
1051  
1052 # say STDERR join ' ', __FILE__, __LINE__, "$firstLine-$lastLine",
1053 # qq{"$input"};
1054 die $eval_error, "\n";
1055 }
1056 }
1057 my $metric = $recce->ambiguity_metric();
1058 if ( $metric != 1 ) {
1059 my $issue = $metric ? "ambiguous" : "no parse";
1060 say STDERR $recce->show_progress( 0, -1 );
1061 say STDERR $input;
1062  
1063 # say STDERR join " ", __FILE__, __LINE__, $policy, $firstLine, $lastLine, $interOffset, $preOffset;
1064 die "Bad gap combinator parse: $issue\n";
1065 }
1066 return \@mistakes;
1067 }
1068  
1069 # TODO: refactor all pseudojoins to call the
1070 # checkPseudojoin() method.
1071  
1072 # If a pseudo-join,
1073 # return a (possible empty) list of mistakes.
1074 # Otherwise, return undef.
1075 sub checkPseudojoin {
1076 my ( $policy, $gap, $options ) = @_;
1077 my $instance = $policy->{lint};
1078 # say STDERR '[' . $instance->literalNode($gap) . ']';
1079 my @mistakes = ();
1080  
1081 # Assume that the gap is not the top of the tree.
1082 my $parent = $gap->{PARENT};
1083 my ( $parentLine, $parentColumn ) = $instance->nodeLC($parent);
1084  
1085 my $expected = $options->{lengths};
1086 my $desc = $options->{desc};
1087 my $details = $options->{details};
1088 my $runeName = $options->{runeName} // $policy->runeName($parent);
1089 my $subpolicy = $options->{subpolicy} // [$runeName];
1090  
1091 # say STDERR Data::Dumper::Dumper($options);
1092  
1093 my $gapLength = $gap->{length};
1094 my ( $gapLine, $gapColumn ) = $instance->nodeLC($gap);
1095 my $nextLexemeOffset = $gap->{start} + $gapLength;
1096 if ($instance->runeGapNode($gap)) {
1097 $gapColumn += 2;
1098 $gapLength -= 2;
1099 }
1100 my ( $nextLexemeLine, $nextLexemeColumn ) =
1101 $instance->line_column( $nextLexemeOffset );
1102  
1103 if ( not $expected ) {
1104  
1105 # default is to be tightly aligned
1106 $expected = [['tight', 2, 1]];
1107 }
1108  
1109 my @expectedColumns = ();
1110 LENGTH: for my $expectedItem ( @{$expected} ) {
1111 my ($alignDesc, $column, $isLength ) = @{$expectedItem};
1112 # say STDERR "alignDesc, column, isLength; $alignDesc, $column, $isLength ";
1113 # say STDERR "gap L,C: $gapLine, $gapColumn";
1114 $column += $gapColumn if $isLength;
1115 # say STDERR "alignDesc, column, isLength; $alignDesc, $column, $isLength ";
1116 push @expectedColumns, [ $alignDesc, $column ];
1117 }
1118  
1119 # If here, we are checking for pseudojoin
1120 my $pseudojoinColumn = $policy->pseudojoinColumn($gap);
1121 # say STDERR "pseudojoinColumn ", Data::Dumper::Dumper($pseudojoinColumn);
1122  
1123 # Return undef to show vertical gap is not a pseudojoin
1124 return if not defined $pseudojoinColumn;
1125 return if $pseudojoinColumn < 0;
1126  
1127 # Pseudojoin of desired length
1128 my $isPseudojoin;
1129 TEST_FOR_PSEUDOJOIN: for my $expectedColumnItem (@expectedColumns) {
1130 my ($alignDesc, $alignColumn) = @{$expectedColumnItem};
1131 # say STDERR "pseudojoinColumn alignColumn; $pseudojoinColumn, $alignColumn";
1132 if ($pseudojoinColumn == $alignColumn) {
1133 $isPseudojoin = 1;
1134 last TEST_FOR_PSEUDOJOIN;
1135 }
1136 }
1137  
1138 return if not $isPseudojoin;
1139  
1140 return [] if $nextLexemeColumn == $pseudojoinColumn;
1141  
1142 # If here, it is a problematic pseudojoin
1143 my $msg = sprintf
1144 '%s %s; %s',
1145 $desc,
1146 describeLC( $nextLexemeLine, $nextLexemeColumn ),
1147 describeMisindent2( $nextLexemeColumn, $pseudojoinColumn );
1148 push @mistakes, {
1149 desc => $msg,
1150 subpolicy => [ @{$subpolicy}, 'pseudojoin' ],
1151 line => $nextLexemeLine,
1152 column => $nextLexemeColumn,
1153 reportLine => $nextLexemeLine,
1154 reportColumn => $nextLexemeColumn,
1155 topicLines => [$parentLine],
1156 details => $details,
1157 };
1158  
1159 return \@mistakes;
1160 }
1161  
1162 # Replace all TISTIS logic with this
1163 sub checkTistis {
1164 my ( $policy, $tistis, $options ) = @_;
1165 my $expectedColumn = $options->{expectedColumn};
1166 my $tag = $options->{tag};
1167 my $instance = $policy->{lint};
1168 my $parent = $tistis->{PARENT};
1169 my @mistakes = ();
1170  
1171 # TODO: Delete after development
1172 die if defined $options->{subpolicyTag};
1173  
1174 my @subpolicy = ();
1175 SET_SUBPOLICY: {
1176 my $subpolicy = $options->{subpolicy};
1177 if (defined $subpolicy) {
1178 push @subpolicy, @{$subpolicy};
1179 last SET_SUBPOLICY;
1180 }
1181 push @subpolicy, $policy->nodeSubpolicy($parent);
1182 }
1183  
1184 my ( $parentLine, $parentColumn ) = $instance->nodeLC($parent);
1185 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
1186 my $literalLine = $instance->literalLine($tistisLine);
1187  
1188 my $runeName = $policy->runeName($parent);
1189  
1190 $literalLine = $policy->deComment($literalLine);
1191 $literalLine =~ s/\n//g;
1192 $literalLine =~ s/==//g;
1193 if ( $literalLine =~ m/[^ ]/ ) {
1194 my $runeLC = describeLC($parentLine, $parentColumn);
1195 my $msg =
1196 sprintf q{TISTIS %s should only share line with other TISTIS's},
1197 describeLC( $tistisLine, $tistisColumn );
1198 push @mistakes,
1199 {
1200 desc => $msg,
1201 subpolicy => [ @subpolicy, 'tistis-alone' ],
1202 parentLine => $parentLine,
1203 parentColumn => $parentColumn,
1204 line => $tistisLine,
1205 column => $tistisColumn,
1206 reportLine => $tistisLine,
1207 reportColumn => $tistisColumn,
1208 details => [ ["Starts at $runeLC"] ],
1209 };
1210 return \@mistakes;
1211 }
1212  
1213 my $tistisIsMisaligned = $tistisColumn != $expectedColumn;
1214  
1215 if ($tistisIsMisaligned) {
1216 my $lineToPos = $instance->{lineToPos};
1217 my $tistisPos = $lineToPos->[$tistisLine] + $expectedColumn;
1218 my $tistisLiteral = $instance->literal( $tistisPos, 2 );
1219  
1220 $tistisIsMisaligned = $tistisLiteral ne '==';
1221 }
1222 if ($tistisIsMisaligned) {
1223 my $runeLC = describeLC($parentLine, $parentColumn);
1224 my $msg = sprintf 'TISTIS %s; %s',
1225 describeLC( $tistisLine, $tistisColumn ),
1226 describeMisindent2( $tistisColumn, $expectedColumn );
1227 push @mistakes,
1228 {
1229 desc => $msg,
1230 subpolicy => [ @subpolicy, 'tistis-indent' ],
1231 parentLine => $parentLine,
1232 parentColumn => $parentColumn,
1233 line => $tistisLine,
1234 column => $tistisColumn,
1235 reportLine => $tistisLine,
1236 reportColumn => $tistisColumn,
1237 details => [ ["Starts at $runeLC"] ],
1238 };
1239 }
1240  
1241 return \@mistakes;
1242 }
1243  
1244 # SIGGAR/SIGGAL "hints"
1245 sub checkBont {
1246 my ( $policy, $node ) = @_;
1247 my $instance = $policy->{lint};
1248  
1249 # bont5d ::= CEN4H SYM4K (- DOT GAP -) tall5d
1250 # bont5d ::= wideBont5d
1251 my ( $cen, $sym, $dot, $gap, $body ) = @{ $node->{children} };
1252 return if not defined $gap;
1253  
1254 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1255 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1256  
1257 # Hint's are an integral part of SIGGAR/SIGGAL which follow a
1258 # basically standard backdenting scheme, so this is not really
1259 # "re-anchoring".
1260 my $anchor =
1261 $instance->ancestorByLHS( $node, { tallSiggar => 1, tallSiggal => 1 } );
1262 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchor);
1263 my $runeName = $policy->runeName($anchor);
1264  
1265 my @mistakes = ();
1266  
1267 BODY_ISSUES: {
1268 if ( $parentLine == $bodyLine ) {
1269 my $msg =
1270 sprintf 'SIGGAR/SIGGAL hint body must not be on rune line',
1271 describeLC( $bodyLine, $bodyColumn );
1272 push @mistakes,
1273 {
1274 desc => $msg,
1275 subpolicy => [ $runeName, 'hint-body-joined' ],
1276 parentLine => $parentLine,
1277 parentColumn => $parentColumn,
1278 line => $bodyLine,
1279 column => $bodyColumn,
1280 reportLine => $bodyLine,
1281 reportColumn => $bodyColumn,
1282 };
1283 last BODY_ISSUES;
1284 }
1285  
1286 # If here parent line != body line
1287 my $expectedBodyColumn = $anchorColumn + 4;
1288 push @mistakes,
1289 @{
1290 $policy->checkOneLineGap(
1291 $gap,
1292 {
1293 mainColumn => $expectedBodyColumn,
1294 subpolicy => [ $runeName, 'hint' ],
1295 runeName => $runeName,
1296 subpolicy => [$runeName],
1297 }
1298 )
1299 };
1300  
1301 if ( $bodyColumn != $expectedBodyColumn ) {
1302 my $msg =
1303 sprintf 'SIGGAL/SIGGAR hint body %s; %s',
1304 describeLC( $bodyLine, $bodyColumn ),
1305 describeMisindent2( $bodyColumn, $expectedBodyColumn );
1306 push @mistakes,
1307 {
1308 desc => $msg,
1309 subpolicy => [ $runeName, 'hint-body-indent' ],
1310 parentLine => $parentLine,
1311 parentColumn => $parentColumn,
1312 line => $bodyLine,
1313 column => $bodyColumn,
1314 reportLine => $bodyLine,
1315 reportColumn => $bodyColumn,
1316 };
1317 }
1318 }
1319  
1320 return \@mistakes;
1321 }
1322  
1323 # bonz5d ::= (- TIS TIS GAP -) optBonzElements (- GAP TIS TIS -)
1324 # bonz5d ::= wideBonz5d
1325 sub checkBonz5d {
1326 my ( $policy, $node ) = @_;
1327 my $instance = $policy->{lint};
1328  
1329 my ($initialTis1, undef, $gap, $bonzElements, $tistisGap, $finalTistis) = @{$node->{children}};
1330 return [] if not defined $gap;
1331 my $runeName = 'sigcen';
1332 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1333 my ( $elementsLine, $elementsColumn ) = $instance->nodeLC($bonzElements);
1334 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($finalTistis);
1335 my $anchorColumn = $parentColumn;
1336 my $expectedElementsColumn = $anchorColumn + 2;
1337  
1338 my @mistakes = ();
1339 push @mistakes,
1340 @{
1341 $policy->checkOneLineGap(
1342 $gap,
1343 {
1344 mainColumn => $anchorColumn,
1345 preColumn => $elementsColumn,
1346 runeName => $runeName,
1347 subpolicy => [ $runeName, 'formulas-initial-vgap' ],
1348 }
1349 )
1350 };
1351  
1352 if ( $expectedElementsColumn != $elementsColumn ) {
1353  
1354 my $msg = sprintf 'formula %s; %s',
1355 describeLC( $elementsLine, $elementsColumn ),
1356 describeMisindent2( $elementsColumn, $expectedElementsColumn );
1357 push @mistakes,
1358 {
1359 desc => $msg,
1360 subpolicy => [ $runeName, 'formula-body-indent' ],
1361 parentLine => $parentLine,
1362 parentColumn => $parentColumn,
1363 line => $elementsLine,
1364 column => $elementsColumn,
1365 reportLine => $elementsLine,
1366 reportColumn => $elementsColumn,
1367 };
1368 }
1369  
1370 push @mistakes,
1371 @{
1372 $policy->checkOneLineGap(
1373 $tistisGap,
1374 {
1375 mainColumn => $anchorColumn,
1376 preColumn => $elementsColumn,
1377 runeName => $runeName,
1378 subpolicy => [ $runeName, 'formulas-final-gap' ],
1379 topicLines => [$tistisLine],
1380 }
1381 )
1382 };
1383  
1384 push @mistakes,
1385 @{
1386 $policy->checkTistis(
1387 $finalTistis,
1388 {
1389 expectedColumn => $anchorColumn,
1390 tag => $runeName,
1391 subpolicy => [ $runeName, 'formulas-final-tistis' ],
1392 }
1393 )
1394 };
1395  
1396 return \@mistakes;
1397  
1398 }
1399  
1400 # optBonzElements ::= bonzElement* separator=>GAP proper=>1
1401 # bonzElement ::= CEN SYM4K (- GAP -) tall5d
1402 sub checkBonzElements {
1403 my ( $policy, $node ) = @_;
1404 my $children = $node->{children};
1405 my @nodesToAlign = ();
1406 for (my $childIX = 0; $childIX <= $#$children; $childIX += 2) {
1407 my $element = $children->[$childIX];
1408 my ($cen, $sym, $gap, $body) = @{ $element->{children} };
1409 push @nodesToAlign, $gap, $body;
1410 }
1411 my $alignmentData = $policy->findAlignment( \@nodesToAlign );
1412 $policy->setInheritedAttribute($node, 'formulaAlignmentData', $alignmentData);
1413 return $policy->checkSeq( $node, 'sigcen-formula' );
1414 }
1415  
1416 sub checkBonzElement {
1417 my ( $policy, $node ) = @_;
1418 my $instance = $policy->{lint};
1419  
1420 # bonzElement ::= CEN SYM4K (- GAP -) tall5d
1421 my ( $bodyGap, $body ) = @{ $policy->gapSeq0($node) };
1422  
1423 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1424 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1425  
1426 my $alignmentData = $policy->getInheritedAttribute($node, 'formulaAlignmentData');
1427 my $bodyAlignmentColumn = @{$alignmentData};
1428  
1429 my @mistakes = ();
1430 my $runeName = 'sigcen';
1431  
1432 BODY_ISSUES: {
1433 if ( $parentLine != $bodyLine ) {
1434 my $msg = sprintf 'formula body %s; must be on rune line',
1435 describeLC( $bodyLine, $bodyColumn );
1436 push @mistakes,
1437 {
1438 desc => $msg,
1439 subpolicy => [ $runeName, 'formula-split' ],
1440 parentLine => $parentLine,
1441 parentColumn => $parentColumn,
1442 line => $bodyLine,
1443 column => $bodyColumn,
1444 reportLine => $bodyLine,
1445 reportColumn => $bodyColumn,
1446 };
1447 last BODY_ISSUES;
1448 }
1449  
1450 # If here, bodyLine == parentLine
1451 last BODY_ISSUES if $bodyColumn = $bodyAlignmentColumn;
1452 my $gapLiteral = $instance->literalNode($bodyGap);
1453 my $gapLength = $bodyGap->{length};
1454 last BODY_ISSUES if $gapLength == 2;
1455 my ( undef, $bodyGapColumn ) = $instance->nodeLC($bodyGap);
1456  
1457 my $msg = sprintf 'formula body %s; %s',
1458 describeLC( $bodyLine, $bodyColumn ),
1459 describeMisindent2( $gapLength, 2 );
1460 push @mistakes,
1461 {
1462 desc => $msg,
1463 subpolicy => [ $runeName, 'formula-body-indent' ],
1464 parentLine => $parentLine,
1465 parentColumn => $parentColumn,
1466 line => $bodyLine,
1467 column => $bodyColumn,
1468 reportLine => $bodyLine,
1469 reportColumn => $bodyColumn,
1470 };
1471 }
1472  
1473 return \@mistakes;
1474 }
1475  
1476 # assumes this is a <tallAttributes> node
1477 sub sailAttributeBodyAlignment {
1478 my ( $policy, $node ) = @_;
1479 my $instance = $policy->{lint};
1480 my $children = $node->{children};
1481 my $firstBodyColumn;
1482 my %firstLine = ();
1483 my %bodyColumnCount = ();
1484 my @nodesToAlign = ();
1485  
1486 # Traverse first to last to make it easy to record
1487 # first line of occurrence of each body column
1488 CHILD:
1489 for ( my $childIX = $#$children ; $childIX >= 0 ; $childIX-- ) {
1490 my $attribute = $children->[$childIX];
1491 my ( undef, $head, $gap, $body ) = @{ $policy->gapSeq0($attribute) };
1492 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
1493 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1494 next CHILD if $headLine != $bodyLine;
1495 push @nodesToAlign, $gap, $body;
1496 }
1497 return $policy->findAlignment( \@nodesToAlign );
1498 }
1499  
1500 sub checkSailAttribute {
1501 my ( $policy, $node ) = @_;
1502 my $instance = $policy->{lint};
1503  
1504 my ( $headGap, $head, $bodyGap, $body ) = @{ $policy->gapSeq0($node) };
1505  
1506 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
1507 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1508  
1509 my $sailApex = $instance->ancestorByLHS( $node, { sailApex5d => 1 } );
1510 my ( $sailApexLine, $sailApexColumn ) = $instance->nodeLC($sailApex);
1511 my $attributes = $instance->ancestorByLHS( $node, { tallAttributes => 1 } );
1512 my $expectedHeadColumn = $sailApexColumn + 4;
1513 my ($expectedBodyColumn, $expectBodyColumnDetails) = @{$policy->sailAttributeBodyAlignment($attributes)};
1514  
1515 my @mistakes = ();
1516  
1517 # Not really a rune name
1518 my $runeName = 'sail';
1519  
1520 # We deal with the elements list in its own node
1521  
1522 push @mistakes,
1523 @{
1524 $policy->checkOneLineGap(
1525 $headGap,
1526 {
1527 mainColumn => $expectedHeadColumn,
1528 runeName => $runeName,
1529 subpolicy => [ $runeName, 'head-vgap' ],
1530 topicLines => [$headLine],
1531 }
1532 )
1533 };
1534  
1535 if ( $headColumn != $expectedHeadColumn ) {
1536 my $msg = sprintf
1537 "Sail attribute head %s; %s",
1538 describeLC( $headLine, $headColumn ),
1539 describeMisindent2( $headColumn, $expectedHeadColumn );
1540 push @mistakes,
1541 {
1542 desc => $msg,
1543 subpolicy => [ $runeName, 'head-indent' ],
1544 parentLine => $sailApexLine,
1545 parentColumn => $sailApexColumn,
1546 line => $headLine,
1547 column => $headColumn,
1548 reportLine => $headLine,
1549 reportColumn => $headColumn,
1550 topicLines => [$headLine],
1551 };
1552 }
1553  
1554 CHECK_BODY: {
1555 if ( $headLine != $bodyLine ) {
1556 my $msg = sprintf
1557 "Sail split attribute NYI %s",
1558 describeLC( $headLine, $headColumn );
1559 push @mistakes,
1560 {
1561 desc => $msg,
1562 subpolicy => [ $runeName, 'split-sail' ],
1563 parentLine => $sailApexLine,
1564 parentColumn => $sailApexColumn,
1565 line => $headLine,
1566 column => $headColumn,
1567 reportLine => $headLine,
1568 reportColumn => $headColumn,
1569 topicLines => [$headLine],
1570 };
1571 last CHECK_BODY;
1572 }
1573  
1574 my $bodyGapLength = $bodyGap->{length};
1575 CHECK_GAP: {
1576 last CHECK_GAP if $bodyGapLength == 2;
1577 if ( $expectedBodyColumn < 0 ) {
1578 my $msg = sprintf
1579 "Sail attribute body %s; %s",
1580 describeLC( $bodyLine, $bodyColumn ),
1581 describeMisindent2( $bodyGapLength, 2 );
1582 push @mistakes,
1583 {
1584 desc => $msg,
1585 subpolicy => [ $runeName, 'body-tight-indent' ],
1586 parentLine => $sailApexLine,
1587 parentColumn => $sailApexColumn,
1588 line => $bodyLine,
1589 column => $bodyColumn,
1590 reportLine => $bodyLine,
1591 reportColumn => $bodyColumn,
1592 topicLines => [$bodyLine],
1593 };
1594 last CHECK_GAP;
1595 }
1596 if ( $bodyColumn != $expectedBodyColumn ) {
1597 my $msg = sprintf
1598 "Sail attribute body %s; %s",
1599 describeLC( $bodyLine, $bodyColumn ),
1600 describeMisindent2( $bodyColumn, $expectedBodyColumn );
1601 push @mistakes,
1602 {
1603 desc => $msg,
1604 subpolicy => [ $runeName, 'body-align-indent' ],
1605 parentLine => $sailApexLine,
1606 parentColumn => $sailApexColumn,
1607 line => $bodyLine,
1608 column => $bodyColumn,
1609 reportLine => $bodyLine,
1610 reportColumn => $bodyColumn,
1611 topicLines => [$bodyLine],
1612 };
1613 }
1614 }
1615 }
1616  
1617 return \@mistakes;
1618 }
1619  
1620 # tagged sail statement
1621 sub checkTailOfElem {
1622 my ( $policy, $node ) = @_;
1623 my $instance = $policy->{lint};
1624 my $lineToPos = $instance->{lineToPos};
1625  
1626 my ( $tallKids, $tistisGap, $tistis ) = @{ $node->{children} };
1627 return [] if $instance->symbol($tallKids) ne 'tallKidsOfElem';
1628  
1629 my $tallTopSail = $instance->ancestor( $node, 2 );
1630 my ( $tallTopSailLine, $tallTopSailColumn ) =
1631 $instance->nodeLC($tallTopSail);
1632 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1633 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
1634 my $anchorColumn = $policy->getInheritedAttribute($node, 'sailAnchorColumn');
1635  
1636 # There is always a SEM before <tallTopSail> and this is our
1637 # anchor column
1638  
1639 my @mistakes = ();
1640 my $runeName = 'sail';
1641  
1642 my $elementColumn = $anchorColumn + 2;
1643  
1644 push @mistakes,
1645 @{
1646 $policy->checkOneLineGap(
1647 $tistisGap,
1648 {
1649 mainColumn => $anchorColumn,
1650 preColumn => $elementColumn,
1651 runeName => $runeName,
1652 subpolicy => [ $runeName, 'elem-tail' ],
1653 topicLines => [$tistisLine],
1654 }
1655 )
1656 };
1657  
1658 push @mistakes,
1659 @{
1660 $policy->checkTistis(
1661 $tistis,
1662 {
1663 tag => $runeName,
1664 subpolicy => [ $runeName, 'elem-tail' ],
1665 expectedColumn => $anchorColumn,
1666 }
1667 )
1668 };
1669  
1670 return \@mistakes;
1671 }
1672  
1673 sub isTopKidsJoined {
1674 my ( $policy, $topKids ) = @_;
1675 my $instance = $policy->{lint};
1676 my ($gapSem, $tallTopKidSeq) = @{ $topKids->{children} };
1677 my ( $topKidsLine ) = $instance->nodeLC($topKids);
1678 my ( $bodyLine ) = $instance->nodeLC($tallTopKidSeq);
1679 return $topKidsLine == $bodyLine;
1680 }
1681  
1682 # tallTailOfTop ::= tallKidsOfTop (- GAP TIS TIS -)
1683 sub checkTailOfTop {
1684 my ( $policy, $node ) = @_;
1685 my $instance = $policy->{lint};
1686  
1687 my ( $topKids, $tistisGap, $tistis ) = @{ $node->{children} };
1688  
1689 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1690 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
1691 my $anchorColumn = $policy->getInheritedAttribute($node, 'sailAnchorColumn');
1692 my $isJoined = $policy->isTopKidsJoined($topKids);
1693 my $kidColumn = $isJoined ? $anchorColumn + 4 : $anchorColumn + 2;
1694  
1695 my @mistakes = ();
1696 my $runeName = 'sail';
1697  
1698 push @mistakes,
1699 @{
1700 $policy->checkOneLineGap(
1701 $tistisGap,
1702 {
1703 mainColumn => $anchorColumn,
1704 preColumn => $kidColumn,
1705 subpolicy => [ $runeName, 'top-tail' ],
1706 runeName => $runeName,
1707 topicLines => [$tistisLine],
1708 }
1709 )
1710 };
1711  
1712 push @mistakes,
1713 @{
1714 $policy->checkTistis(
1715 $tistis,
1716 {
1717 tag => $runeName,
1718 subpolicy => [ $runeName, 'top-tail' ],
1719 expectedColumn => $anchorColumn,
1720 }
1721 )
1722 };
1723  
1724 return \@mistakes;
1725 }
1726  
1727 # Deals with SEMHEP (;-), SEMLUS (;+), SEMTAR (;*), and SEMCEN (;%).
1728 sub checkTopSail {
1729 my ( $policy, $node ) = @_;
1730 my $instance = $policy->{lint};
1731 my $grammar = $instance->{grammar};
1732 my $ruleID = $node->{ruleID};
1733 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1734  
1735 $policy->setInheritedAttribute($node, 'sailAnchorColumn', $parentColumn - 1);
1736  
1737 # say STDERR "top sail:", '[' . $instance->literalNode($node) . ']';
1738 my ($tunaMode, $bodyGap, $body) = @{$node->{children}};
1739 # Note: GAP before CRAM is treated as free-form
1740 return [] if $instance->symbol($tunaMode) ne 'tunaMode';
1741  
1742 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1743  
1744 my @mistakes = ();
1745  
1746 my $runeName = 'sail';
1747  
1748 my $expectedColumn;
1749  
1750 BODY_ISSUES: {
1751 if ( $parentLine != $bodyLine ) {
1752 my $msg = join " ",
1753 (
1754 sprintf 'Top sail body %s; must be on rune line',
1755 describeLC( $bodyLine, $bodyColumn )
1756 ),
1757 ( map { $grammar->symbol_display_form($_) }
1758 $grammar->rule_expand($ruleID) );
1759 push @mistakes,
1760 {
1761 desc => $msg,
1762 subpolicy => [ $runeName, 'top-sail', 'split' ],
1763 parentLine => $parentLine,
1764 parentColumn => $parentColumn,
1765 line => $bodyLine,
1766 column => $bodyColumn,
1767 reportLine => $bodyLine,
1768 reportColumn => $bodyColumn,
1769 };
1770 last BODY_ISSUES;
1771 }
1772  
1773 # If here, bodyLine == parentLine
1774 my $gapLiteral = $instance->literalNode($bodyGap);
1775 my $gapLength = $bodyGap->{length};
1776 last BODY_ISSUES if $gapLength == 2;
1777  
1778 my $msg = sprintf 'sail runechild %s; %s',
1779 describeLC( $bodyLine, $bodyColumn ),
1780 describeMisindent2( $gapLength, 2 );
1781 push @mistakes,
1782 {
1783 desc => $msg,
1784 subpolicy => [ $runeName, 'top-sail', 'indent' ],
1785 parentLine => $parentLine,
1786 parentColumn => $parentColumn,
1787 line => $bodyLine,
1788 column => $bodyColumn,
1789 reportLine => $bodyLine,
1790 reportColumn => $bodyColumn,
1791 };
1792 }
1793  
1794 return \@mistakes;
1795 }
1796  
1797 sub checkTopKids {
1798 my ( $policy, $node ) = @_;
1799 my $instance = $policy->{lint};
1800 my $grammar = $instance->{grammar};
1801 my $ruleID = $node->{ruleID};
1802  
1803 my $children = $node->{children};
1804 my ($gapSem, $tallTopKidSeq) = @{$children};
1805 return [] if $instance->symbol($tallTopKidSeq) eq 'CRAM';
1806 # Note: GAP before CRAM is treated as free-form
1807  
1808 my $anchorColumn = $policy->getInheritedAttribute($node, 'sailAnchorColumn');
1809 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1810 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($tallTopKidSeq);
1811 $bodyColumn -= 1; # adjust for SEM in GAP_SEM
1812  
1813 my @mistakes = ();
1814  
1815 my $runeName = 'sail';
1816 my $isJoined = $parentLine == $bodyLine;
1817 my $expectedBodyColumn = $isJoined ? $anchorColumn + 4 : $anchorColumn + 2;
1818 my $topSail = $instance->ancestorByLHS( $node, { tallTopSail => 1 } );
1819 $policy->setInheritedAttribute($topSail, 'sailIsJoined', $isJoined);
1820  
1821 FIRST_KID_ISSUES: {
1822 if ( not $isJoined ) {
1823 push @mistakes,
1824 @{
1825 $policy->checkOneLineGap(
1826 $gapSem,
1827 {
1828 mainColumn => $anchorColumn,
1829 preColumn => $expectedBodyColumn,
1830 runeName => $runeName,
1831 subpolicy => [$runeName],
1832 details => [
1833 [
1834 ( sprintf 'sail elem kid #1' ),
1835 'inter-comment indent should be '
1836 . ( $anchorColumn + 1 ),
1837 'pre-comment indent should be '
1838 . ( $expectedBodyColumn + 1 ),
1839 ]
1840 ],
1841 }
1842 )
1843 };
1844  
1845 if ( $expectedBodyColumn != $bodyColumn ) {
1846 my $msg = sprintf 'Sail elem kid #1 body %s; %s',
1847 describeLC( $bodyLine, $bodyColumn ),
1848 describeMisindent2( $bodyColumn, $expectedBodyColumn );
1849 push @mistakes,
1850 {
1851 desc => $msg,
1852 subpolicy => [ $runeName, 'elem-kids', 'body-indent' ],
1853 parentLine => $parentLine,
1854 parentColumn => $parentColumn,
1855 line => $bodyLine,
1856 column => $bodyColumn,
1857 reportLine => $bodyLine,
1858 reportColumn => $bodyColumn,
1859 };
1860 }
1861 last FIRST_KID_ISSUES;
1862 }
1863  
1864 # If here, bodyLine == parentLine
1865 my $gapLiteral = $instance->literalNode($gapSem);
1866 my $gapLength = $gapSem->{length};
1867 last FIRST_KID_ISSUES if $gapLength == 3; # length is 3 to allow for SEM in GAP_SEM
1868 my ( undef, $gapSemColumn ) = $instance->nodeLC($gapSem);
1869  
1870 my $msg = sprintf 'Sail kids body %s; %s',
1871 describeLC( $bodyLine, $bodyColumn ),
1872 describeMisindent2( $gapLength, 2 );
1873 push @mistakes,
1874 {
1875 desc => $msg,
1876 subpolicy => [ $runeName, 'top-kids', 'body-indent' ],
1877 parentLine => $parentLine,
1878 parentColumn => $parentColumn,
1879 line => $bodyLine,
1880 column => $bodyColumn,
1881 reportLine => $bodyLine,
1882 reportColumn => $bodyColumn,
1883 };
1884 }
1885  
1886 # tallTopKidSeq ::= tallTopSail+ separator=>GAP_SEM proper=>1
1887 my $kids = $tallTopKidSeq->{children};
1888 # say STDERR join " ", __FILE__, __LINE__, $instance->symbol($tallTopKidSeq),
1889 # describeLC($instance->nodeLC($node));
1890 # say STDERR join " ", __FILE__, __LINE__, $instance->symbol($tallTopKidSeq),
1891 # "top kids:", ( scalar @{$kids} );
1892 my $childIX = 0;
1893 KID: for (my $childIX = 1; $childIX + 1 <= $#$kids; $childIX+=2) {
1894 my $kidGap = $kids->[$childIX];
1895 my $kid = $kids->[$childIX+1];
1896 my ( $kidLine, $kidColumn ) = $instance->nodeLC($kid);
1897 $kidColumn -= 1; # adjust for SEM in GAP_SEM
1898  
1899 my $kidNumber = ($childIX + 1)/2;
1900 # say STDERR join " ", __FILE__, __LINE__, 'kid of top [' . $instance->literalNode($kid) . ']';
1901 push @mistakes,
1902 @{
1903 $policy->checkOneLineGap(
1904 $kidGap,
1905 {
1906 mainColumn => $anchorColumn,
1907 preColumn => $expectedBodyColumn,
1908 runeName => $runeName,
1909 subpolicy => [$runeName],
1910 details => [
1911 [
1912 ( sprintf 'sail elem kid #%d', $kidNumber),
1913 'inter-comment indent should be '
1914 . ( $anchorColumn + 1 ),
1915 'pre-comment indent should be '
1916 . ( $expectedBodyColumn + 1 ),
1917 ]
1918 ],
1919 }
1920 )
1921 };
1922  
1923 if ( $expectedBodyColumn != $bodyColumn ) {
1924 my $msg = sprintf 'Sail elem kid #%d body %s; %s',
1925 $kidNumber,
1926 describeLC( $bodyLine, $bodyColumn ),
1927 describeMisindent2( $bodyColumn, $expectedBodyColumn );
1928 push @mistakes,
1929 {
1930 desc => $msg,
1931 subpolicy => [ $runeName, 'elem-kids', 'body-indent' ],
1932 parentLine => $parentLine,
1933 parentColumn => $parentColumn,
1934 line => $kidLine,
1935 column => $kidColumn,
1936 reportLine => $kidLine,
1937 reportColumn => $kidColumn,
1938 };
1939 }
1940 }
1941  
1942 return \@mistakes;
1943 }
1944  
1945 sub checkElemKids {
1946 my ( $policy, $node ) = @_;
1947 my $instance = $policy->{lint};
1948 my $grammar = $instance->{grammar};
1949 my $ruleID = $node->{ruleID};
1950  
1951 my $children = $node->{children};
1952 my ( $gapSem, $tallElemKidSeq ) = @{$children};
1953 return [] if $instance->symbol($gapSem) ne 'GAP_SEM';
1954  
1955 my ( $bodyGap, $body ) = @{ $policy->gapSeq0($node) };
1956  
1957 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
1958 my $anchorColumn = $policy->getInheritedAttribute($node, 'sailAnchorColumn');
1959 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
1960  
1961 my @mistakes = ();
1962  
1963 my $runeName = 'sail';
1964  
1965 # tallKidsOfElem ::= tallKidOfElem+
1966 # tallKidOfElem ::= (- GAP_SEM -) tallTopSail
1967 my $kids = $tallElemKidSeq->{children};
1968 my $expectedKidColumn = $anchorColumn + 2;
1969 KID: for ( my $childIX = 0 ; $childIX + 1 <= $#$kids ; $childIX += 2 ) {
1970 # say STDERR join " ", __FILE__, __LINE__, $childIX;
1971 my $kidGap = $kids->[$childIX];
1972 my $kidBody = $kids->[ $childIX + 1 ];
1973 my ( $kidBodyLine, $kidBodyColumn ) = $instance->nodeLC($kidBody);
1974 $kidBodyColumn -= 1; # Adjust for SEM in GAP_SEM
1975  
1976 # say STDERR join " ", __FILE__, __LINE__, "kidBodyColumn: $kidBodyColumn";
1977 # say STDERR join " ", __FILE__, __LINE__, "anchorColumn: $anchorColumn";
1978 # say STDERR join " ", __FILE__, __LINE__, "$childIX: [" . $instance->symbol($kidBody) . ']';
1979 # say STDERR join " ", __FILE__, __LINE__, "$childIX: [" . $instance->literalNode($kidBody) . ']';
1980  
1981 push @mistakes,
1982 @{
1983 $policy->checkOneLineGap(
1984 $kidGap,
1985 {
1986 mainColumn => $anchorColumn,
1987 preColumn => $expectedKidColumn,
1988 runeName => $runeName,
1989 subpolicy => [$runeName],
1990 details => [
1991 [
1992 ( sprintf 'sail elem kid #%d', $childIX ),
1993 'inter-comment indent should be '
1994 . ( $anchorColumn + 1 ),
1995 'pre-comment indent should be '
1996 . ( $kidBodyColumn + 1 ),
1997 ]
1998 ],
1999 }
2000 )
2001 };
2002  
2003 if ( $expectedKidColumn != $kidBodyColumn ) {
2004 my $msg = sprintf 'Sail elem kid #%d body %s; %s',
2005 $childIX,
2006 describeLC( $kidBodyLine, $kidBodyColumn ),
2007 describeMisindent2( $kidBodyColumn, $expectedKidColumn );
2008 push @mistakes,
2009 {
2010 desc => $msg,
2011 subpolicy => [ $runeName, 'elem-kids', 'body-indent' ],
2012 parentLine => $parentLine,
2013 parentColumn => $parentColumn,
2014 line => $kidBodyLine,
2015 column => $kidBodyColumn,
2016 reportLine => $kidBodyLine,
2017 reportColumn => $kidBodyColumn,
2018 };
2019 }
2020  
2021 }
2022  
2023 return \@mistakes;
2024 }
2025  
2026 # Common logic for checking the running element of a hoon.
2027 # returns a (possibly empty) list of mistakes.
2028 #
2029 # TODO: Some of these arguments can (should?) be computed from others.
2030 #
2031 sub checkRunning {
2032 my ( $policy, $options ) = @_;
2033 my $instance = $policy->{lint};
2034 my $runningChildren = $options->{children};
2035 my $anchorColumn = $options->{anchorColumn};
2036 my $expectedColumn = $options->{expectedColumn};
2037 my $pseudojoin = $options->{pseudojoin};
2038  
2039 my @subpolicy = ();
2040 my $subpolicy = $options->{subpolicy};
2041 push @subpolicy, @{$subpolicy} if defined $subpolicy;
2042  
2043 # by default, in fact always at this point, the running can be
2044 # found as the parent of the last running child, and the parent
2045 # can be found as the parent
2046 my $running = $runningChildren->[-1]->{PARENT};
2047 my $parent = $running->{PARENT};
2048  
2049 my ( $runeLine, $runeColumn ) = $instance->nodeLC($parent);
2050 my ( $runningLine, $runningColumn ) = $instance->nodeLC($running);
2051  
2052 my $anchorDetails = $options->{anchorDetails}
2053 // $policy->anchorDetailsBasic( $parent, $anchorColumn );
2054  
2055 my $runeName = $policy->runeName($parent);
2056 my @mistakes = ();
2057  
2058 my $skipFirst = $options->{skipFirst};
2059 my $childIX = $skipFirst ? 3 : 2;
2060  
2061 # Call an column of runsteps with the same row
2062 # position a "pile" because "column" and the other terms are
2063 # too overloaded.
2064 my @runStepsToAlignByPile = ();
2065 RUNNING_LINE: while (1) {
2066  
2067 # The index into the runstep alignment array.
2068 # The alignment of the 2nd runstep
2069 # in a row (or line) is at index 0.
2070 my $pileIX = 0;
2071 RUNSTEP: while (1) {
2072 last RUNNING_LINE if $childIX + 1 > $#$runningChildren;
2073 my $gap = $runningChildren->[$childIX];
2074 my $runStep = $runningChildren->[ $childIX + 1 ];
2075 my ($gapLine) = $instance->nodeLC($gap);
2076 my ( $runStepLine, $runStepColumn ) = $instance->nodeLC($runStep);
2077 last RUNSTEP if $gapLine != $runStepLine;
2078  
2079 # Uses Perl's autoinstantiation
2080 push @{ $runStepsToAlignByPile[$pileIX] }, $gap, $runStep;
2081 $pileIX += 1;
2082 $childIX += 2;
2083 }
2084  
2085 # In this loop, childIX always points to a gap
2086 $childIX += 2;
2087 }
2088  
2089 my @pileAlignments = ();
2090 ELEMENT:
2091 for ( my $pileIX = 0 ; $pileIX <= $#runStepsToAlignByPile ; $pileIX++ ) {
2092 my $runStepsToAlign = $runStepsToAlignByPile[$pileIX];
2093 if ( not $runStepsToAlign ) {
2094 $pileAlignments[$pileIX] = [ -1, [] ];
2095 next ELEMENT;
2096 }
2097 $pileAlignments[$pileIX] =
2098 $policy->findAlignment($runStepsToAlign);
2099 }
2100  
2101 # If there are no alignments by runstep, then we check for
2102 # alignments among the running's children.
2103 my @nodesToAlignByElement;
2104 RUNNING_CHILD_ALIGNMENTS: {
2105 last RUNNING_CHILD_ALIGNMENTS if scalar @pileAlignments;
2106 my @bricks;
2107 RUNSTEP:
2108 for (
2109 my $runStepIX = ( $skipFirst ? 2 : 1 ) ;
2110 $runStepIX <= $#$runningChildren ;
2111 $runStepIX += 2
2112 )
2113 {
2114 my $runStep = $runningChildren->[$runStepIX];
2115  
2116 my $brickDescendant = $instance->brickDescendant($runStep);
2117 last RUNSTEP if not $brickDescendant;
2118  
2119 $instance->literalNode($brickDescendant);
2120 next RUNSTEP if not $policy->chainable($brickDescendant);
2121  
2122 push @bricks, $brickDescendant;
2123 $instance->literalNode($brickDescendant);
2124 my $gapSeq = $policy->gapSeq0($brickDescendant);
2125 BRICK_ELEMENT: for ( my $elementIX = 0 ; ; $elementIX++ ) {
2126 my $seqIX = $elementIX * 2;
2127 last BRICK_ELEMENT if $seqIX >= $#$gapSeq;
2128 my $gap = $gapSeq->[$seqIX];
2129 my $element = $gapSeq->[ $seqIX + 1 ];
2130 push @{ $nodesToAlignByElement[$elementIX] }, $gap, $element;
2131 }
2132 }
2133  
2134 last RUNNING_CHILD_ALIGNMENTS unless scalar @nodesToAlignByElement;
2135  
2136 my @runStepChildAlignments = ();
2137 ELEMENT:
2138 for (
2139 my $elementIX = 0 ;
2140 $elementIX <= $#nodesToAlignByElement ;
2141 $elementIX++
2142 )
2143 {
2144 my $nodesToAlign = $nodesToAlignByElement[$elementIX];
2145 if ( not $nodesToAlign ) {
2146 $runStepChildAlignments[$elementIX] = [ -1, [] ];
2147 next ELEMENT;
2148 }
2149 $runStepChildAlignments[$elementIX] =
2150 $policy->findAlignment($nodesToAlign);
2151 }
2152  
2153  
2154 for my $brick (@bricks) {
2155 my $brickNodeIX = $brick->{IX};
2156 $policy->{perNode}->{$brickNodeIX}->{runningAlignments} =
2157 \@runStepChildAlignments;
2158 }
2159  
2160 }
2161  
2162 $childIX = 0;
2163 # Do the first run step
2164 my $gap = $runningChildren->[$childIX];
2165 my ( $gapLine, $gapColumn ) = $instance->nodeLC($gap);
2166 my $runStep = $runningChildren->[ $childIX + 1 ];
2167 my ( $thisRunStepLine, $runStepColumn ) = $instance->nodeLC($runStep);
2168  
2169 my $runStepCount = 1;
2170 CHECK_FIRST_RUNNING: {
2171 last CHECK_FIRST_RUNNING if $skipFirst;
2172 last CHECK_FIRST_RUNNING if $runStepColumn == $expectedColumn;
2173 my @pseudojoin = ();
2174 push @pseudojoin, 'pseudojoin' if $pseudojoin;
2175 my $msg = sprintf
2176 "runstep #%d %s; %s",
2177 $runStepCount,
2178 describeLC( $thisRunStepLine, $runStepColumn ),
2179 describeMisindent2( $runStepColumn, $expectedColumn );
2180 push @mistakes,
2181 {
2182 desc => $msg,
2183 subpolicy => [ @subpolicy, 'runstep-indent' ],
2184 parentLine => $thisRunStepLine,
2185 parentColumn => $runStepColumn,
2186 line => $thisRunStepLine,
2187 column => $runStepColumn,
2188 reportLine => $thisRunStepLine,
2189 reportColumn => $runStepColumn,
2190 topicLines => [$runeLine],
2191 details => [ [ @pseudojoin, @{$anchorDetails} ] ],
2192 };
2193 }
2194  
2195 my $workingRunStepLine = $thisRunStepLine;
2196  
2197 # Initial runsteps may be on a single line,
2198 # separated by one stop
2199 $childIX = 2;
2200 $runStepCount = 2;
2201 RUN_STEP: while ( $childIX < $#$runningChildren ) {
2202  
2203 my ( $thisRunStepLine, $runStepColumn ) ;
2204  
2205 INLINE_RUN_STEP: while (1) {
2206 if ( $childIX >= $#$runningChildren ) {
2207 last RUN_STEP;
2208 }
2209 $gap = $runningChildren->[$childIX];
2210 ( $gapLine, $gapColumn ) = $instance->nodeLC($gap);
2211 $runStep = $runningChildren->[ $childIX + 1 ];
2212 ( $thisRunStepLine, $runStepColumn ) = $instance->nodeLC($runStep);
2213 if ( $thisRunStepLine != $workingRunStepLine ) {
2214 last INLINE_RUN_STEP;
2215 }
2216 CHECK_COLUMN: {
2217 my $tightColumn = $gapColumn + 2;
2218 last CHECK_COLUMN if $runStepColumn == $tightColumn;
2219 my @allowedColumns =([ $tightColumn => 'tight' ]);
2220  
2221 my ( $pileAlignmentColumn, $pileAlignmentLines );
2222 my $thisAlignment = $pileAlignments[ $runStepCount - 2 ];
2223 if ($thisAlignment) {
2224 ( $pileAlignmentColumn, $pileAlignmentLines ) = @{$thisAlignment};
2225 last CHECK_COLUMN if $pileAlignmentColumn > $tightColumn
2226 and $pileAlignmentColumn == $runStepColumn;
2227 }
2228  
2229 my $details;
2230 my @topicLines = ();
2231 if (defined $pileAlignmentColumn and $pileAlignmentColumn >= $tightColumn) {
2232 push @allowedColumns, [ $pileAlignmentColumn => 'runstep' ];
2233 my $oneBasedColumn = $pileAlignmentColumn + 1;
2234 my $pileAlignmentLines = $pileAlignmentLines;
2235 push @topicLines, @{$pileAlignmentLines};
2236 $details = [
2237 [
2238 sprintf 'runstep alignment is %d, see %s',
2239 $oneBasedColumn,
2240 (
2241 join q{ },
2242 map { $_ . ':' . $oneBasedColumn }
2243 @{$pileAlignmentLines}
2244 )
2245 ]
2246 ];
2247 }
2248 else {
2249 $details = [ [ "no runstep alignment detected" ] ];
2250 }
2251  
2252 my @sortedColumns = sort { $a->[0] <=> $b->[0] } @allowedColumns;
2253 my $allowedDesc = join "; ",
2254 map { sprintf '@%d:%d (%s)', $thisRunStepLine, $_->[0]+1, $_->[1] } @sortedColumns;
2255 if (scalar @sortedColumns >= 2) {
2256 $allowedDesc = 'one of ' . $allowedDesc;
2257 }
2258  
2259 my $msg = sprintf
2260 'runstep #%d of running %s, line %d is at %s; should be %s',
2261 $runStepCount,
2262 describeLC( $runeLine, $runeColumn ),
2263 $thisRunStepLine,
2264 describeLC( $thisRunStepLine, $runStepColumn ),
2265 $allowedDesc;
2266 push @mistakes,
2267 {
2268 desc => $msg,
2269 subpolicy => [ @subpolicy, 'runstep-hgap' ],
2270 parentLine => $runeLine,
2271 parentColumn => $runeColumn,
2272 line => $thisRunStepLine,
2273 column => $runStepColumn,
2274 reportLine => $thisRunStepLine,
2275 reportColumn => $runStepColumn,
2276 topicLines => \@topicLines,
2277 details => $details,
2278 };
2279 }
2280 $childIX += 2;
2281 $runStepCount++;
2282 }
2283  
2284 $workingRunStepLine = $thisRunStepLine;
2285  
2286 # If the run step is mis-indented, complaints about the comments are
2287 # misleading and confusing. Skip them.
2288 # TODO: Complain about blank lines anyway ?
2289 if ( $runStepColumn == $expectedColumn ) {
2290 push @mistakes,
2291 @{
2292 $policy->checkOneLineGap(
2293 $gap,
2294 {
2295 mainColumn => $anchorColumn,
2296 preColumn => $runStepColumn,
2297 runeName => $runeName,
2298 subpolicy => [ @subpolicy, 'runstep-vgap' ],
2299 parent => $runStep,
2300 topicLines => [$runeLine],
2301 details => [
2302 [
2303 'inter-comment indent should be '
2304 . ( $anchorColumn + 1 ),
2305 'pre-comment indent should be '
2306 . ( $expectedColumn + 1 ),
2307 @{$anchorDetails},
2308 ]
2309 ],
2310 }
2311 )
2312 };
2313 }
2314  
2315 if ( $runStepColumn != $expectedColumn ) {
2316 my $msg = sprintf
2317 "runstep #%d %s; %s",
2318 ( $childIX / 2 ) + 1,
2319 describeLC( $thisRunStepLine, $runStepColumn ),
2320 describeMisindent2( $runStepColumn, $expectedColumn );
2321 push @mistakes,
2322 {
2323 desc => $msg,
2324 subpolicy => [ @subpolicy, 'runstep-indent' ],
2325 parentLine => $thisRunStepLine,
2326 parentColumn => $runStepColumn,
2327 line => $thisRunStepLine,
2328 column => $runStepColumn,
2329 reportLine => $thisRunStepLine,
2330 reportColumn => $runStepColumn,
2331 topicLines => [$runeLine],
2332 details => [ [ @{$anchorDetails} ] ],
2333 };
2334 }
2335  
2336 $childIX += 2;
2337 $runStepCount = 2;
2338  
2339 }
2340  
2341 return \@mistakes;
2342  
2343 }
2344  
2345 sub check_0Running {
2346 my ( $policy, $node ) = @_;
2347 my $instance = $policy->{lint};
2348 my ( $rune, $runningGap, $tistisGap, $tistis );
2349 my $runeName = $policy->runeName($node);
2350 my $runningChildren = [];
2351 my ( $runningLine, $runningColumn );
2352 {
2353 my $running;
2354 if ($runeName eq 'tissig') {
2355 my ( $firstRunstep, $firstRunningGap );
2356 ( $rune, $runningGap, $firstRunstep, $firstRunningGap, $running, $tistisGap, $tistis ) =
2357 @{ $policy->gapSeq($node) };
2358 push @{$runningChildren}, $runningGap, $firstRunstep, $firstRunningGap;
2359 ( $runningLine, $runningColumn ) = $instance->nodeLC($firstRunstep);
2360 } else {
2361 ( $rune, $runningGap, $running, $tistisGap, $tistis ) =
2362 @{ $policy->gapSeq($node) };
2363 push @{$runningChildren}, $runningGap;
2364 ( $runningLine, $runningColumn ) = $instance->nodeLC($running);
2365 }
2366 push @{$runningChildren}, @{ $running->{children} };
2367 }
2368  
2369 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
2370 my ( $anchorLine, $anchorColumn ) = ( $runeLine, $runeColumn );
2371 my $anchorData;
2372 CHECK_FOR_ANCHORING: {
2373 if ( $runeName eq 'colsig' ) {
2374  
2375 ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2376 $node,
2377 {
2378 'tallCendot' => 1,
2379 'tallCenhep' => 1,
2380 'tallCenlus' => 1,
2381 'tallCollus' => 1,
2382 'tallKethep' => 1,
2383 'tallTisfas' => 1,
2384 'tallTisgar' => 1,
2385 }
2386 );
2387 last CHECK_FOR_ANCHORING;
2388 }
2389 if ( $runeName eq 'coltar' ) {
2390  
2391 # TODO: Cleanup after development
2392 ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2393 $node,
2394 {
2395 'tallCenhep' => 1,
2396 }
2397 );
2398 last CHECK_FOR_ANCHORING;
2399 }
2400 if ( $runeName eq 'tissig' ) {
2401 ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2402 $node,
2403 {
2404 'tallTisgar' => 1,
2405 'tallWutlus' => 1,
2406 }
2407 );
2408 last CHECK_FOR_ANCHORING;
2409 }
2410 }
2411 my $anchorDetails;
2412 $anchorDetails = $policy->anchorDetails( $node, $anchorData )
2413 if $anchorData;
2414  
2415 # Arguments for the checking methods, called by this method.
2416 my $checkArgs = {
2417 node => $node,
2418 rune => $rune,
2419 runningGap => $runningGap,
2420 runningChildren => $runningChildren,
2421 tistisGap => $tistisGap,
2422 tistis => $tistis,
2423 runningLine => $runningLine,
2424 runningColumn => $runningColumn,
2425 runeName => $runeName,
2426 anchorLine => $anchorLine,
2427 anchorColumn => $anchorColumn,
2428 anchorDetails => $anchorDetails,
2429 };
2430  
2431 # What kind of gap?
2432 my $column = $policy->checkJoinGap($runningGap);
2433  
2434 # If column is undef, then hoon is not joined, it is split.
2435 return $policy->checkSplit_0Running( $checkArgs ) if not defined $column;
2436  
2437 # If column is -1, then hoon is joined.
2438 return $policy->checkJoined_0Running( $checkArgs, $column ) if $column == -1;
2439  
2440 # If here, it is a pseudo-join and $column is the column indicated
2441 # by gap.
2442 $checkArgs->{pseudojoin} = 1;
2443  
2444 # Treat it as a pseudo-join if $column matches the expected join colum
2445 return $policy->checkJoined_0Running( $checkArgs, $column )
2446 if $column == $runeColumn + 4;
2447  
2448 # If here the pseudo-join column is a mismatch: Treat the supposed
2449 # pseudo-join as an ordinary comment and the running as if it was
2450 # split.
2451 return $policy->checkSplit_0Running( $checkArgs );
2452 }
2453  
2454 # assumes this is a <wisp5d> node
2455 # wisp5d ::= (- HEP HEP -)
2456 # wisp5d ::= whap5d GAP (- HEP HEP -)
2457 sub wispCellBodyAlignment {
2458 my ( $policy, $node ) = @_;
2459 my $instance = $policy->{lint};
2460 my ($whap) = @{ $node->{children} };
2461 return [ -1, [] ]
2462 if 'whap5d' ne $instance->symbol($whap);
2463 return $policy->whapCellBodyAlignment($whap);
2464 }
2465  
2466 # assumes this is a <whap5d> node
2467 sub whapCellBodyAlignment {
2468 my ( $policy, $node ) = @_;
2469 my $instance = $policy->{lint};
2470 my $children = $node->{children};
2471 my @nodesToAlign = ();
2472  
2473 CHILD:
2474 for ( my $childIX = 0 ; $childIX <= $#$children ; $childIX += 2 ) {
2475 my $boog = $children->[$childIX];
2476 my $cell = $boog->{children}->[0];
2477 my ( undef, $head, $gap, $body ) = @{ $policy->gapSeq0($cell) };
2478 my ( $headLine ) = $instance->nodeLC($head);
2479 my ( $bodyLine ) = $instance->nodeLC($body);
2480 next CHILD unless $headLine == $bodyLine;
2481 # say STDERR sprintf q{Gap: "%s"}, $instance->literalNode($gap);
2482 # say STDERR sprintf q{Body: "%s"}, $instance->literalNode($body);
2483 push @nodesToAlign, $gap, $body;
2484 }
2485 return $policy->findAlignment( \@nodesToAlign );
2486 }
2487  
2488 sub checkWhap5d {
2489 my ( $policy, $node ) = @_;
2490 my $gapSeq = $policy->gapSeq($node);
2491 my $instance = $policy->{lint};
2492  
2493 my @mistakes = ();
2494 my $runeName = $policy->runeName($node);
2495  
2496 my $anchorNode = $instance->firstBrickOfLine($node);
2497 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchorNode);
2498 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
2499  
2500 # The battery is "joined" iff it starts on the same line as the anchor,
2501 # but at a different column. "Different column" to catch the case where
2502 # the anchor rune *is* the battery rune.
2503 my $joined =
2504 ( $anchorLine == $parentLine and $anchorColumn != $parentColumn );
2505 my $children = $node->{children};
2506 my $childIX = 0;
2507 my $expectedBoogColumn = $joined ? $parentColumn : $anchorColumn;
2508 my $expectedLine = $joined ? $parentLine : $anchorLine + 1;
2509  
2510 CHILD: while ( $childIX <= $#$children ) {
2511 my $boog = $children->[$childIX];
2512 my ( $boogLine, $boogColumn ) = $instance->nodeLC($boog);
2513  
2514 if ( $boogColumn != $expectedBoogColumn ) {
2515 my $msg = sprintf
2516 "cell #%d %s; %s",
2517 ( $childIX / 2 ) + 1,
2518 describeLC( $boogLine, $boogColumn ),
2519 describeMisindent2( $boogColumn, $expectedBoogColumn );
2520 push @mistakes,
2521 {
2522 desc => $msg,
2523 subpolicy => [$runeName, 'arm-indent'],
2524 parentLine => $boogLine,
2525 parentColumn => $boogColumn,
2526 line => $boogLine,
2527 column => $boogColumn,
2528 reportLine => $boogLine,
2529 reportColumn => $boogColumn,
2530 topicLines => [ $parentLine, $expectedLine ],
2531 };
2532 }
2533  
2534 $childIX++;
2535 last CHILD unless $childIX <= $#$children;
2536 my $boogGap = $children->[$childIX];
2537 my ( $boogGapLine, $boogGapColumn ) = $instance->nodeLC($boogGap);
2538  
2539 push @mistakes,
2540 @{
2541 $policy->checkOneLineGap(
2542 $boogGap,
2543 {
2544 mainColumn => $expectedBoogColumn,
2545 preColumn => $expectedBoogColumn+2,
2546 runeName => $runeName,
2547 subpolicy => [$runeName, 'arm-vgap'],
2548 topicLines => [ $parentLine, $boogGapLine ],
2549 }
2550 )
2551 };
2552  
2553 $childIX++;
2554 }
2555  
2556 return \@mistakes;
2557  
2558 }
2559  
2560 # wisp5d ::= (- HEP HEP -)
2561 # wisp5d ::= whap5d GAP (- HEP HEP -)
2562 sub checkWisp5d {
2563 my ( $policy, $node ) = @_;
2564 my @mistakes = ();
2565 my $instance = $policy->{lint};
2566 my $runeName = $policy->runeName($node);
2567 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
2568  
2569 my $battery =
2570 $instance->ancestorByLHS( $node,
2571 { tallBarcab => 1, tallBarcen => 1, tallBarket => 1 } );
2572 my ( $batteryLine, $batteryColumn ) = $instance->nodeLC($battery);
2573 my $batteryLC = describeLC($batteryLine, $batteryColumn);
2574  
2575 my $anchorColumn = $policy->getInheritedAttribute($node, 'anchorColumn');
2576 $anchorColumn //= $batteryColumn;
2577  
2578 my ( $cellBodyColumn, $cellBodyColumnLines ) =
2579 @{ $policy->getInheritedAttribute($node, 'cellBodyAlignmentData') };
2580  
2581 my ( $whap, $gap, $hep ) = @{$node->{children}};
2582 return [] if $instance->symbol($whap) ne 'whap5d';
2583  
2584 push @mistakes,
2585 @{
2586 $policy->checkOneLineGap(
2587 $gap,
2588 {
2589 mainColumn => $parentColumn,
2590 preColumn => $anchorColumn+2,
2591 runeName => $runeName,
2592 subpolicy => [$runeName],
2593 topicLines => [$batteryLine],
2594 details => [ [ "Starts at $batteryLC", ] ],
2595 }
2596 )
2597 };
2598  
2599 my ( $hephepLine, $hephepColumn ) = $instance->nodeLC($hep);
2600  
2601 {
2602 my $literalLine = $instance->literalLine($hephepLine);
2603 $literalLine = $policy->deComment($literalLine);
2604 $literalLine =~ s/\n//g;
2605 $literalLine =~ s/--//g;
2606 if ( $literalLine =~ m/[^ ]/ ) {
2607 my $msg =
2608 sprintf q{HEPHEP %s should only share line with other HEPHEP's},
2609 describeLC( $hephepLine, $hephepColumn );
2610 push @mistakes,
2611 {
2612 desc => $msg,
2613 subpolicy => [ $runeName, 'hephep-alone' ],
2614 parentLine => $parentLine,
2615 parentColumn => $parentColumn,
2616 line => $hephepLine,
2617 column => $hephepColumn,
2618 reportLine => $hephepLine,
2619 reportColumn => $hephepColumn,
2620 details => [ [ "Starts at $batteryLC", ] ],
2621 };
2622 }
2623 }
2624  
2625 my $expectedColumn = $anchorColumn;
2626 my $hephepIsMisaligned = $hephepColumn != $expectedColumn;
2627  
2628 if ($hephepIsMisaligned) {
2629 my $lineToPos = $instance->{lineToPos};
2630 my $hephepPos = $lineToPos->[$hephepLine] + $expectedColumn;
2631 my $hephepLiteral = $instance->literal( $hephepPos, 2 );
2632 $hephepIsMisaligned = $hephepLiteral ne '--';
2633 }
2634 if ($hephepIsMisaligned) {
2635 my $msg = sprintf
2636 'battery hephep %s; %s',
2637 describeLC( $hephepLine, $hephepColumn ),
2638 describeMisindent2( $hephepColumn, $expectedColumn );
2639 push @mistakes,
2640 {
2641 desc => $msg,
2642 subpolicy => [ $runeName, 'hephep-indent' ],
2643 parentLine => $parentLine,
2644 parentColumn => $parentColumn,
2645 line => $hephepLine,
2646 column => $hephepColumn,
2647 reportLine => $hephepLine,
2648 reportColumn => $hephepColumn,
2649 topicLines => [$batteryLine],
2650 details => [ [ "Starts at $batteryLC", ] ],
2651 };
2652 }
2653 return \@mistakes;
2654 }
2655  
2656 sub checkSplitFascom {
2657 my ( $policy, $node ) = @_;
2658 my $instance = $policy->{lint};
2659 my $lineToPos = $instance->{lineToPos};
2660  
2661 # say STDERR join " ", __FILE__, __LINE__, $instance->literalNode($node);
2662 my ( $bodyGap, $body, $tistisGap, $tistis ) =
2663 @{ $policy->gapSeq0($node) };
2664  
2665 my ( $runeLine, $runeColumn ) = $instance->nodeLC($node);
2666 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
2667 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
2668  
2669 my $anchorLine = $runeLine;
2670 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2671 $node,
2672 {
2673 fordFascen => 1,
2674 }
2675 );
2676 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
2677  
2678 my $nodeIX = $node->{IX};
2679 my $chessSide = $policy->{perNode}->{$nodeIX}->{chessSide};
2680 my $queenside = $chessSide eq 'queenside';
2681 # say STDERR join " ", __FILE__, __LINE__, $chessSide;
2682  
2683 my @mistakes = ();
2684 my $runeName = $policy->runeName($node);
2685 my $tag = $runeName;
2686  
2687 # We deal with the elements list itself,
2688 # in its own node
2689  
2690 my $expectedColumn = $anchorColumn + $queenside ? 4 : 2;
2691 my $expectedLine = $runeLine + 1;
2692  
2693 # say STDERR join " ", __FILE__, __LINE__, $chessSide, $expectedColumn;
2694  
2695 push @mistakes,
2696 @{
2697 $policy->checkOneLineGap(
2698 $bodyGap,
2699 {
2700 mainColumn => $anchorColumn,
2701 runeName => $tag,
2702 subpolicy => [ $runeName ],
2703 topicLines => [$bodyLine],
2704 }
2705 )
2706 };
2707  
2708 if ( $bodyColumn != $expectedColumn ) {
2709 my $msg = sprintf
2710 "split %s %s; %s",
2711 $runeName,
2712 describeLC( $bodyLine, $bodyColumn ),
2713 describeMisindent2( $bodyColumn, $expectedColumn );
2714 push @mistakes,
2715 {
2716 desc => $msg,
2717 subpolicy => [ $runeName, 'body-indent' ],
2718 parentLine => $runeLine,
2719 parentColumn => $runeColumn,
2720 line => $bodyLine,
2721 column => $bodyColumn,
2722 reportLine => $bodyLine,
2723 reportColumn => $bodyColumn,
2724 topicLines => [ $runeLine, $expectedLine ],
2725 details => [ [ @{$anchorDetails} ] ],
2726 };
2727 }
2728  
2729 push @mistakes,
2730 @{
2731 $policy->checkOneLineGap(
2732 $tistisGap,
2733 {
2734 mainColumn => $anchorColumn,
2735 runeName => $tag,
2736 subpolicy => [ $runeName ],
2737 topicLines => [ $anchorLine, $tistisLine ],
2738 details => [ [ @{$anchorDetails} ] ],
2739 }
2740 )
2741 };
2742  
2743 push @mistakes,
2744 @{
2745 $policy->checkTistis(
2746 $tistis,
2747 {
2748 tag => $tag,
2749 expectedColumn => $anchorColumn,
2750 }
2751 )
2752 };
2753  
2754 return \@mistakes;
2755 }
2756  
2757 sub checkJoinedFascom {
2758 my ( $policy, $node ) = @_;
2759 my $instance = $policy->{lint};
2760 my $lineToPos = $instance->{lineToPos};
2761  
2762 my ( $bodyGap, $body, $tistisGap, $tistis ) = @{ $policy->gapSeq0($node) };
2763  
2764 my ( $runeLine, $runeColumn ) = $instance->nodeLC($node);
2765 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
2766 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
2767 my ( $anchorLine, $anchorColumn ) = ( $runeLine, $runeColumn );
2768  
2769 my $nodeIX = $node->{IX};
2770  
2771 # Joined FASCOM is always considered queenside.
2772  
2773 my @mistakes = ();
2774 my $runeName = $policy->runeName($node);
2775 my $tag = $runeName;
2776  
2777 # We deal with the elements list in its own node
2778  
2779 my $expectedColumn = $anchorColumn + 4;
2780 my $expectedLine = $runeLine + 1;
2781  
2782 if ( $bodyColumn != $expectedColumn ) {
2783 my $msg = sprintf
2784 "joined %s %s; %s",
2785 $runeName,
2786 describeLC( $bodyLine, $bodyColumn ),
2787 describeMisindent2( $bodyColumn, $expectedColumn );
2788 push @mistakes,
2789 {
2790 desc => $msg,
2791 subpolicy => [ $runeName, 'body-indent' ],
2792 parentLine => $runeLine,
2793 parentColumn => $runeColumn,
2794 line => $bodyLine,
2795 column => $bodyColumn,
2796 reportLine => $bodyLine,
2797 reportColumn => $bodyColumn,
2798 topicLines => [ $runeLine, $expectedLine ],
2799 };
2800 }
2801  
2802 push @mistakes,
2803 @{
2804 $policy->checkOneLineGap(
2805 $tistisGap,
2806 {
2807 mainColumn => $anchorColumn,
2808 runeName => $tag,
2809 subpolicy => [ $runeName ],
2810 topicLines => [ $runeLine, $tistisLine ],
2811 }
2812 )
2813 };
2814  
2815 push @mistakes,
2816 @{
2817 $policy->checkTistis(
2818 $tistis,
2819 {
2820 tag => $tag,
2821 expectedColumn => $anchorColumn,
2822 }
2823 )
2824 };
2825  
2826 return \@mistakes;
2827 }
2828  
2829 sub checkFascom {
2830 my ( $policy, $node ) = @_;
2831 my $instance = $policy->{lint};
2832 # say STDERR join " ", __FILE__, __LINE__, $instance->literalNode($node);
2833 my ( undef, $elements ) = @{ $policy->gapSeq0($node) };
2834  
2835 my ($runeLine, $runeColumn) = $instance->nodeLC($node);
2836 my ($elementsLine) = $instance->nodeLC($elements);
2837  
2838 my $anchorLine = $runeLine;
2839 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2840 $node,
2841 {
2842 fordFascen => 1,
2843 }
2844 );
2845 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
2846  
2847 my $isJoined = $elementsLine == $runeLine ? 1 : 0;
2848 my $chessSide;
2849 # Joined FASCOM's are always considered queenside
2850 if ($isJoined) {
2851 $chessSide = 'queenside';
2852 } else {
2853 $chessSide = $policy->chessSideOfPairSequence($elements, $anchorColumn);
2854 }
2855  
2856 my $nodeIX = $node->{IX};
2857 $policy->{perNode}->{$nodeIX}->{isJoined} = $isJoined;
2858 $policy->{perNode}->{$nodeIX}->{chessSide} = $chessSide;
2859  
2860 return checkJoinedFascom( $policy, $node ) if $isJoined;
2861 return checkSplitFascom( $policy, $node );
2862 }
2863  
2864 sub checkFascomElements {
2865 my ( $policy, $node ) = @_;
2866 my $instance = $policy->{lint};
2867 my $children = $node->{children};
2868  
2869 # say STDERR join " ", __FILE__, __LINE__, 'elements', $instance->literalNode($node);
2870 my $rune = $instance->ancestorByBrickName( $node, 'fordFascom' );
2871 my $runeNodeIX = $rune->{IX};
2872 my $isJoined = $policy->{perNode}->{$runeNodeIX}->{isJoined};
2873 my $chessSide = $policy->{perNode}->{$runeNodeIX}->{chessSide};
2874 my $queenside = $chessSide eq 'queenside';
2875  
2876 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
2877 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
2878  
2879 my $anchorLine = $runeLine;
2880 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
2881 $rune,
2882 {
2883 fordFascen => 1,
2884 }
2885 );
2886 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
2887  
2888 my @mistakes = ();
2889 my $tag = 'fascom-elements';
2890  
2891 my $childIX = 0;
2892 my $expectedColumn = $anchorColumn + ($queenside ? 4 : 2);
2893 CHILD: while ( $childIX <= $#$children ) {
2894 my $element = $children->[$childIX];
2895 my ( $elementLine, $elementColumn ) = $instance->nodeLC($element);
2896 # say STDERR join " ", __FILE__, __LINE__, "element head #$childIX", $instance->literalNode($element);
2897 # say STDERR join " ", __FILE__, __LINE__, "element head #$childIX", 'actual v expected', $elementColumn, $expectedColumn ;
2898  
2899 CHECK_HEAD: {
2900 if ( $isJoined and $childIX == 0 ) {
2901 $expectedColumn = $elementColumn;
2902 last CHECK_HEAD;
2903 }
2904  
2905 if ( $elementColumn != $expectedColumn ) {
2906 my $msg = sprintf
2907 "element %d %s; %s",
2908 ( $childIX / 2 ) + 1,
2909 describeLC( $elementLine, $elementColumn ),
2910 describeMisindent2( $elementColumn, $expectedColumn );
2911 push @mistakes,
2912 {
2913 desc => $msg,
2914 details => [ [ @{$anchorDetails} ] ],
2915 subpolicy => [ $tag, 'indent' ],
2916 parentLine => $parentLine,
2917 parentColumn => $parentColumn,
2918 line => $elementLine,
2919 column => $elementColumn,
2920 reportLine => $elementLine,
2921 reportColumn => $elementColumn,
2922 topicLines => [$runeLine],
2923 };
2924 }
2925 }
2926  
2927 $childIX++;
2928 last CHILD unless $childIX <= $#$children;
2929 my $elementGap = $children->[$childIX];
2930 my ( $elementGapLine, $elementGapColumn ) =
2931 $instance->nodeLC($elementGap);
2932  
2933 push @mistakes,
2934 @{
2935 $policy->checkOneLineGap(
2936 $elementGap,
2937 {
2938 mainColumn => $anchorColumn,
2939 preColumn => $expectedColumn,
2940 runeName => $tag,
2941 subpolicy => [ $tag ],
2942 topicLines => [$runeLine],
2943 details => [ [ @{$anchorDetails} ] ],
2944 }
2945 )
2946 };
2947  
2948 $childIX++;
2949 }
2950  
2951 return \@mistakes;
2952 }
2953  
2954 sub checkFasdot {
2955 my ( $policy, $node ) = @_;
2956 my $instance = $policy->{lint};
2957  
2958 my ( $bodyGap, $body, $tistisGap, $tistis ) = @{ $policy->gapSeq0($node) };
2959  
2960 my ( $runeLine, $runeColumn ) = $instance->nodeLC($node);
2961 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
2962 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
2963  
2964 my @mistakes = ();
2965 my $runeName = $policy->runeName($node);
2966 my $tag = $runeName;
2967  
2968 # We deal with the elements list in its own node
2969  
2970 my $expectedColumn = $runeColumn + 4;
2971  
2972 CHECK_BODY: {
2973 if ( $bodyLine != $runeLine ) {
2974 my $msg = sprintf
2975 "%s %s; body must be on rune line",
2976 $runeName, describeLC( $bodyLine, $bodyColumn );
2977 push @mistakes,
2978 {
2979 desc => $msg,
2980 subpolicy => [ $runeName, 'body-split' ],
2981 parentLine => $runeLine,
2982 parentColumn => $runeColumn,
2983 line => $bodyLine,
2984 column => $bodyColumn,
2985 reportLine => $bodyLine,
2986 reportColumn => $bodyColumn,
2987 topicLines => [$runeLine],
2988 };
2989 last CHECK_BODY;
2990 }
2991  
2992 if ( $bodyColumn != $expectedColumn ) {
2993 my $msg = sprintf
2994 "joined %s %s; %s",
2995 $runeName,
2996 describeLC( $bodyLine, $bodyColumn ),
2997 describeMisindent2( $bodyColumn, $expectedColumn );
2998 push @mistakes,
2999 {
3000 desc => $msg,
3001 subpolicy => [ $runeName, 'body-indent' ],
3002 parentLine => $runeLine,
3003 parentColumn => $runeColumn,
3004 line => $bodyLine,
3005 column => $bodyColumn,
3006 reportLine => $bodyLine,
3007 reportColumn => $bodyColumn,
3008 topicLines => [$runeLine],
3009 };
3010 }
3011 }
3012  
3013 push @mistakes,
3014 @{
3015 $policy->checkOneLineGap(
3016 $tistisGap,
3017 {
3018 mainColumn => $runeColumn,
3019 runeName => $tag,
3020 subpolicy => [$runeName],
3021 topicLines => [ $runeLine, $tistisLine ],
3022 }
3023 )
3024 };
3025  
3026 push @mistakes,
3027 @{
3028 $policy->checkTistis(
3029 $tistis,
3030 {
3031 tag => $tag,
3032 expectedColumn => $runeColumn,
3033 }
3034 )
3035 };
3036  
3037 return \@mistakes;
3038 }
3039  
3040 sub checkJogging {
3041 my ( $policy, $node ) = @_;
3042 my $instance = $policy->{lint};
3043 my $children = $node->{children};
3044 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3045  
3046 my $joggingHoonData = $policy->getInheritedAttribute($node, 'joggingHoonData');
3047 my $jogBaseColumn = $joggingHoonData->{jogBaseColumn};
3048 my $chessSide = $joggingHoonData->{chessSide};
3049 my $joggingHoonNode = $joggingHoonData->{node};
3050 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($joggingHoonNode);
3051 my $runeName = $policy->runeName($joggingHoonNode);
3052  
3053 my @mistakes = ();
3054  
3055 my $expectedColumn = $parentColumn;
3056 CHILD: for ( my $childIX = 1; $childIX <= $#$children; $childIX+=2 ) {
3057 my $jogGap = $children->[$childIX];
3058 my ( $jogGapLine, $jogGapColumn ) =
3059 $instance->nodeLC($jogGap);
3060  
3061 push @mistakes,
3062 @{
3063 $policy->checkOneLineGap(
3064 $jogGap,
3065 {
3066 mainColumn => $anchorColumn,
3067 preColumn => $jogBaseColumn,
3068 runeName => $runeName,
3069 subpolicy => [ $runeName, 'jogging' ],
3070 topicLines => [$jogGapLine],
3071 }
3072 )
3073 };
3074  
3075 }
3076  
3077 return \@mistakes;
3078  
3079 }
3080  
3081 # Check "vanilla" sequence
3082 sub checkSeq {
3083 my ( $policy, $node, $elementDesc ) = @_;
3084 my $instance = $policy->{lint};
3085 my $children = $node->{children};
3086  
3087 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3088  
3089 my $brick = $instance->brickNode($node);
3090 my $runeName = $brick ? $policy->runeName($brick) : 'fordfile';
3091  
3092 my @mistakes = ();
3093  
3094 my $childIX = 0;
3095 my $expectedColumn = $parentColumn;
3096 CHILD: while ( $childIX <= $#$children ) {
3097 my $element = $children->[$childIX];
3098 my ( $elementLine, $elementColumn ) = $instance->nodeLC($element);
3099  
3100 if ( $elementColumn != $expectedColumn ) {
3101 my $msg = sprintf
3102 '%s %d %s; %s',
3103 $runeName,
3104 ( $childIX / 2 ) + 1,
3105 describeLC( $elementLine, $elementColumn ),
3106 describeMisindent2( $elementColumn, $expectedColumn );
3107 push @mistakes,
3108 {
3109 desc => $msg,
3110 subpolicy => [ $runeName, 'sequence-element-indent' ],
3111 parentLine => $parentLine,
3112 parentColumn => $parentColumn,
3113 line => $elementLine,
3114 column => $elementColumn,
3115 reportLine => $elementLine,
3116 reportColumn => $elementColumn,
3117 details => [ [ $elementDesc] ],
3118 };
3119 }
3120  
3121 $childIX++;
3122 last CHILD unless $childIX <= $#$children;
3123 my $elementGap = $children->[$childIX];
3124 my ( $elementGapLine, $elementGapColumn ) =
3125 $instance->nodeLC($elementGap);
3126  
3127 push @mistakes,
3128 @{
3129 $policy->checkOneLineGap(
3130 $elementGap,
3131 {
3132 mainColumn => $expectedColumn,
3133 runeName => $runeName,
3134 subpolicy => [ $runeName, 'sequence-vgap' ],
3135 details => [ [ $elementDesc] ],
3136 topicLines => [$elementGapLine],
3137 }
3138 )
3139 };
3140  
3141 $childIX++;
3142 }
3143  
3144 return \@mistakes;
3145  
3146 }
3147  
3148 # tallBarcab ::= (- BAR CAB GAP -) till5d (- GAP -) wasp5d wisp5d
3149 sub checkBarcab {
3150 my ( $policy, $node ) = @_;
3151 my $instance = $policy->{lint};
3152  
3153 # TODO: reanchoring logic, memoize anchorColumn for checkWisp5d()
3154  
3155 # BARCAB is special, so we need to find the components using low-level
3156 # techniques.
3157 # tallBarcab ::= (- BAR CAB GAP -) till5d (- GAP -) wasp5d wisp5d
3158 my ( undef, undef, $headGap, $head, $wispGap, undef, $wisp ) =
3159 @{ $node->{children} };
3160 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3161 my $anchorNode = $node;
3162 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchorNode);
3163 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
3164 my ( $wispLine, $wispColumn ) = $instance->nodeLC($wisp);
3165  
3166 $policy->setInheritedAttribute( $node, 'anchorColumn', $anchorColumn );
3167  
3168 my $cellBodyAlignmentData = $policy->wispCellBodyAlignment($wisp);
3169 $policy->setInheritedAttribute( $node, 'cellBodyAlignmentData',
3170 $cellBodyAlignmentData );
3171  
3172 my @mistakes = ();
3173 my $runeName = 'barcab';
3174  
3175 my $expectedColumn;
3176  
3177 HEAD_ISSUES: {
3178 if ( $parentLine != $headLine ) {
3179 my $pseudojoinColumn = $policy->pseudojoinColumn($headGap);
3180 if ( $pseudojoinColumn <= 0 ) {
3181 my $msg = sprintf 'Barcab head %s; must be on rune line',
3182 describeLC( $headLine, $headColumn );
3183 push @mistakes,
3184 {
3185 desc => $msg,
3186 subpolicy => [ $runeName, 'head-split' ],
3187 parentLine => $parentLine,
3188 parentColumn => $parentColumn,
3189 line => $headLine,
3190 column => $headColumn,
3191 reportLine => $headLine,
3192 reportColumn => $headColumn,
3193 };
3194 last HEAD_ISSUES;
3195 }
3196 my $expectedHeadColumn = $pseudojoinColumn;
3197 if ( $headColumn != $expectedHeadColumn ) {
3198 my $msg =
3199 sprintf
3200 'Pseudo-joined BARCEN head; head/comment mismatch; head is %s',
3201 describeLC( $headLine, $headColumn ),
3202 describeMisindent2( $headColumn, $expectedHeadColumn );
3203 push @mistakes,
3204 {
3205 desc => $msg,
3206 subpolicy => [ $runeName, 'head-pseudojoin-mismatch' ],
3207 parentLine => $parentLine,
3208 parentColumn => $parentColumn,
3209 reportLine => $headLine,
3210 reportColumn => $headColumn,
3211 };
3212 }
3213 last HEAD_ISSUES;
3214 }
3215  
3216 # If here, headLine == runeLine
3217 my $gapLiteral = $instance->literalNode($headGap);
3218 my $gapLength = $headGap->{length};
3219 last HEAD_ISSUES if $gapLength == 2;
3220 my ( undef, $headGapColumn ) = $instance->nodeLC($headGap);
3221  
3222 my $msg = sprintf 'Barcab head %s; %s',
3223 describeLC( $headLine, $headColumn ),
3224 describeMisindent2( $gapLength, 2 );
3225 push @mistakes,
3226 {
3227 desc => $msg,
3228 subpolicy => [ $runeName, 'head-hgap' ],
3229 parentLine => $parentLine,
3230 parentColumn => $parentColumn,
3231 line => $headLine,
3232 column => $headColumn,
3233 reportLine => $headLine,
3234 reportColumn => $headColumn,
3235 };
3236  
3237 }
3238  
3239 $expectedColumn = $anchorColumn;
3240 push @mistakes,
3241 @{
3242 $policy->checkOneLineGap(
3243 $wispGap,
3244 {
3245 mainColumn => $anchorColumn,
3246 preColumn => $anchorColumn + 2,
3247 runeName => $runeName,
3248 subpolicy => [ $runeName, 'battery-vgap' ],
3249 topicLines => [$wispLine],
3250 }
3251 )
3252 };
3253  
3254 if ( $wispColumn != $expectedColumn ) {
3255 my $msg = sprintf 'Barcab battery %s; %s',
3256 describeLC( $wispLine, $wispColumn ),
3257 describeMisindent2( $wispColumn, $expectedColumn );
3258 push @mistakes,
3259 {
3260 desc => $msg,
3261 subpolicy => [ $runeName, 'battery-indent' ],
3262 parentLine => $parentLine,
3263 parentColumn => $parentColumn,
3264 line => $wispLine,
3265 column => $wispColumn,
3266 reportLine => $wispLine,
3267 reportColumn => $wispColumn,
3268 };
3269 return \@mistakes;
3270 }
3271  
3272 return \@mistakes;
3273 }
3274  
3275 # tallBarcen ::= (- BAR CEN GAP -) wisp5d
3276 sub checkBarcen {
3277 my ( $policy, $node ) = @_;
3278 my ( $rune, undef, $gap, $wisp ) = @{ $node->{children} };
3279 my $wispNodeIX = $wisp->{IX};
3280 my $instance = $policy->{lint};
3281 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3282 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
3283 $node,
3284 {
3285 # LustisCell => 1, # should NOT reanchor at Lustis
3286 # LushepCell => 1, # should NOT reanchor at Lushep
3287 # LuslusCell => 1, # should NOT reanchor at Luslus, per experiment
3288 # Reanchor at TISGAR would make sense in a lot of places in arvo corpus,
3289 # would not work in toe.hoon
3290 # tallTisgar => 1,
3291 tallKetbar => 1,
3292 tallKetwut => 1,
3293 }
3294 );
3295 $policy->setInheritedAttribute($node, 'anchorColumn', $anchorColumn);
3296 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
3297  
3298 my $cellBodyAlignmentData = $policy->wispCellBodyAlignment($wisp);
3299 $policy->setInheritedAttribute($node, 'cellBodyAlignmentData', $cellBodyAlignmentData);
3300  
3301 my ( $wispLine, $wispColumn ) = $instance->nodeLC($wisp);
3302  
3303 my @mistakes = ();
3304 my $runeName = 'barcen';
3305  
3306 my $gapLiteral = $instance->literalNode($gap);
3307 my $expectedColumn;
3308  
3309 if ( $parentLine == $wispLine ) {
3310 my $gapLength = $gap->{length};
3311 return [] if length $gapLiteral == 2;
3312  
3313 my $msg = sprintf 'joined Barcen battery %s; %s',
3314 describeLC( $wispLine, $wispColumn ),
3315 describeMisindent2( $gapLength, 2 );
3316 push @mistakes,
3317 {
3318 desc => $msg,
3319 subpolicy => [ $runeName, 'battery-hgap' ],
3320 parentLine => $parentLine,
3321 parentColumn => $parentColumn,
3322 line => $wispLine,
3323 column => $wispColumn,
3324 reportLine => $wispLine,
3325 reportColumn => $wispColumn,
3326 };
3327 return \@mistakes;
3328 }
3329  
3330 # If here head line != battery line
3331 $expectedColumn = $anchorColumn;
3332 push @mistakes,
3333 @{
3334 $policy->checkOneLineGap(
3335 $gap,
3336 {
3337 mainColumn => $expectedColumn,
3338 runeName => $runeName,
3339 subpolicy => [ $runeName ],
3340 details => [
3341 [ @{ $policy->anchorDetails( $node, $anchorData ) } ]
3342 ],
3343 }
3344 )
3345 };
3346  
3347 if ( $wispColumn != $expectedColumn ) {
3348 my $msg = sprintf 'split Barcen battery %s; %s',
3349 describeLC( $wispLine, $wispColumn ),
3350 describeMisindent2( $wispColumn, $expectedColumn );
3351 push @mistakes,
3352 {
3353 desc => $msg,
3354 subpolicy => [ $runeName, 'battery-indent' ],
3355 parentLine => $parentLine,
3356 parentColumn => $parentColumn,
3357 line => $wispLine,
3358 column => $wispColumn,
3359 reportLine => $wispLine,
3360 reportColumn => $wispColumn,
3361 anchorDetails => $policy->anchorDetails( $node, $anchorData ),
3362 };
3363 return \@mistakes;
3364 }
3365  
3366 return \@mistakes;
3367 }
3368  
3369 # tallBarket ::= (- BAR KET GAP -) tall5d (- GAP -) wisp5d
3370 sub checkBarket {
3371 my ( $policy, $node ) = @_;
3372 my $instance = $policy->{lint};
3373  
3374 # TODO: reanchoring logic, memoize anchorColumn for checkWisp5d()
3375  
3376 my ( $rune, undef, $headGap, $head, $wispGap, $wisp ) =
3377 @{ $node->{children} };
3378 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3379 my $anchorNode = $node;
3380 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchorNode);
3381 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
3382 my ( $wispLine, $wispColumn ) = $instance->nodeLC($wisp);
3383  
3384 $policy->setInheritedAttribute($node, 'anchorColumn', $anchorColumn);
3385  
3386 my $cellBodyAlignmentData = $policy->wispCellBodyAlignment($wisp);
3387 $policy->setInheritedAttribute($node, 'cellBodyAlignmentData', $cellBodyAlignmentData);
3388  
3389 my @mistakes = ();
3390 my $tag = 'barket';
3391 my $runeName = 'barket';
3392  
3393 my $expectedColumn;
3394  
3395 HEAD_ISSUES: {
3396 if ( $parentLine != $headLine ) {
3397 my $pseudojoinColumn = $policy->pseudojoinColumn($headGap);
3398 if ( $pseudojoinColumn <= 0 ) {
3399 my $msg = sprintf 'Barket head %s; must be on rune line',
3400 describeLC( $headLine, $headColumn );
3401 push @mistakes,
3402 {
3403 desc => $msg,
3404 subpolicy => [$runeName, 'head-split'],
3405 parentLine => $parentLine,
3406 parentColumn => $parentColumn,
3407 line => $headLine,
3408 column => $headColumn,
3409 reportLine => $headLine,
3410 reportColumn => $headColumn,
3411 };
3412 last HEAD_ISSUES;
3413 }
3414 my $expectedHeadColumn = $pseudojoinColumn;
3415 if ( $headColumn != $expectedHeadColumn ) {
3416 my $msg =
3417 sprintf
3418 'Pseudo-joined Barket head; head/comment mismatch; head is %s',
3419 describeLC( $headLine, $headColumn ),
3420 describeMisindent2( $headColumn, $expectedHeadColumn );
3421 push @mistakes,
3422 {
3423 desc => $msg,
3424 subpolicy => [$runeName, 'head-pseudojoin-match'],
3425 parentLine => $parentLine,
3426 parentColumn => $parentColumn,
3427 line => $headLine,
3428 column => $headColumn,
3429 reportLine => $headLine,
3430 reportColumn => $headColumn,
3431 };
3432 }
3433 last HEAD_ISSUES;
3434 }
3435  
3436 # If here, headLine == runeLine
3437 my $gapLiteral = $instance->literalNode($headGap);
3438 my $gapLength = $headGap->{length};
3439 last HEAD_ISSUES if $gapLength == 2;
3440 my ( undef, $headGapColumn ) = $instance->nodeLC($headGap);
3441  
3442 my $msg = sprintf 'Barket head %s; %s',
3443 describeLC( $headLine, $headColumn ),
3444 describeMisindent2( $gapLength, 2 );
3445 push @mistakes,
3446 {
3447 desc => $msg,
3448 subpolicy => [$runeName, 'head-hgap'],
3449 parentLine => $parentLine,
3450 parentColumn => $parentColumn,
3451 line => $headLine,
3452 column => $headColumn,
3453 reportLine => $headLine,
3454 reportColumn => $headColumn,
3455 };
3456  
3457 }
3458  
3459 $expectedColumn = $anchorColumn;
3460 push @mistakes,
3461 @{
3462 $policy->checkOneLineGap(
3463 $wispGap,
3464 {
3465 mainColumn => $expectedColumn,
3466 preColumn => $expectedColumn + 2,
3467 runeName => $tag,
3468 subpolicy => [$runeName, 'battery-vgap'],
3469 topicLines => [$wispLine],
3470 }
3471 )
3472 };
3473  
3474 if ( $wispColumn != $expectedColumn ) {
3475 my $msg = sprintf 'Barket battery %s; %s',
3476 describeLC( $wispLine, $wispColumn ),
3477 describeMisindent2( $wispColumn, $expectedColumn );
3478 push @mistakes,
3479 {
3480 desc => $msg,
3481 subpolicy => [$runeName, 'battery-indent'],
3482 parentLine => $parentLine,
3483 parentColumn => $parentColumn,
3484 line => $wispLine,
3485 column => $wispColumn,
3486 reportLine => $wispLine,
3487 reportColumn => $wispColumn,
3488 };
3489 return \@mistakes;
3490 }
3491  
3492 return \@mistakes;
3493 }
3494  
3495 sub checkFordHoofRune {
3496 my ( $policy, $lhsName, $node ) = @_;
3497 my $instance = $policy->{lint};
3498  
3499 # FASWUT is very similar to these runes. Combine them?
3500  
3501 # Ford hoof runes is special, so we need to find the components using low-level
3502 # techniques.
3503 # optFordFashep ::= (- FAS HEP GAP -) fordHoofSeq (- GAP -)
3504 my ( undef, undef, $leaderGap, $body, $trailerGap ) =
3505 @{ $node->{children} };
3506  
3507 my $runeName = $policy->runeName($node);
3508  
3509 # TODO: Should we require that parent column be 0?
3510 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3511 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
3512 my ( $leaderGapLine, $leaderGapColumn ) = $instance->nodeLC($leaderGap);
3513 my ( $anchorLine, $anchorColumn ) = ( $parentLine, $parentColumn );
3514  
3515 my @mistakes = ();
3516 my $tag = $runeName;
3517  
3518 my $expectedColumn;
3519  
3520 BODY_ISSUES: {
3521 if ( $parentLine != $bodyLine ) {
3522 my $msg = sprintf '%s body %s; must be on rune line',
3523 $runeName,
3524 describeLC( $bodyLine, $bodyColumn );
3525 push @mistakes,
3526 {
3527 desc => $msg,
3528 parentLine => $parentLine,
3529 parentColumn => $parentColumn,
3530 line => $bodyLine,
3531 column => $bodyColumn,
3532 reportLine => $bodyLine,
3533 reportColumn => $bodyColumn,
3534 subpolicy => [ $runeName, 'same-line' ],
3535 };
3536 last BODY_ISSUES;
3537 }
3538 my $expectedBodyColumn = $parentColumn + 4;
3539 if ( $bodyColumn != $expectedBodyColumn ) {
3540 my $msg =
3541 sprintf '%s body %s is %s',
3542 $runeName,
3543 describeLC( $bodyLine, $bodyColumn ),
3544 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3545 push @mistakes,
3546 {
3547 desc => $msg,
3548 parentLine => $parentLine,
3549 parentColumn => $parentColumn,
3550 line => $bodyLine,
3551 column => $bodyColumn,
3552 reportLine => $bodyLine,
3553 reportColumn => $bodyColumn,
3554 subpolicy => [ $runeName, 'hgap' ],
3555 };
3556 }
3557 last BODY_ISSUES;
3558  
3559 }
3560  
3561 $expectedColumn = $anchorColumn;
3562 push @mistakes,
3563 @{
3564 $policy->checkOneLineGap(
3565 $trailerGap,
3566 {
3567 mainColumn => $anchorColumn,
3568 runeName => $tag,
3569 subpolicy => [ $runeName ],
3570 }
3571 )
3572 };
3573  
3574 return \@mistakes;
3575 }
3576  
3577 sub checkFordHoop {
3578 my ( $policy, $node ) = @_;
3579 my $instance = $policy->{lint};
3580  
3581 # TODO Split is implemented, but not split Ford-1 hoon
3582 # is represented in the corpus AFAICT
3583  
3584 my $tag = 'fasfas';
3585  
3586 # fordFassig ::= (- FAS SIG GAP -) tall5d
3587 my ( $gap, $body ) = @{ $policy->gapSeq0($node) };
3588  
3589 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3590 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
3591  
3592 my @mistakes = ();
3593  
3594 my $expectedBodyColumn;
3595  
3596 BODY_ISSUES: {
3597 if ( $parentLine == $bodyLine ) {
3598 my $expectedBodyColumn = $parentColumn + 4;
3599 if ( $bodyColumn != $expectedBodyColumn ) {
3600 my $msg =
3601 sprintf 'joined %s body %s is %s',
3602 $tag,
3603 describeLC( $bodyLine, $bodyColumn ),
3604 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3605 push @mistakes,
3606 {
3607 desc => $msg,
3608 subpolicy => [ $tag, 'hgap' ],
3609 parentLine => $parentLine,
3610 parentColumn => $parentColumn,
3611 line => $bodyLine,
3612 column => $bodyColumn,
3613 };
3614 }
3615 last BODY_ISSUES;
3616 }
3617  
3618 # If here parent line != body line
3619 $expectedBodyColumn = $parentColumn;
3620 push @mistakes,
3621 @{
3622 $policy->checkOneLineGap(
3623 $gap,
3624 {
3625 mainColumn => $expectedBodyColumn,
3626 runeName => $tag,
3627 subpolicy => [ $tag ],
3628 }
3629 )
3630 };
3631  
3632 if ( $bodyColumn != $expectedBodyColumn ) {
3633 my $msg =
3634 sprintf 'split %s body %s is %s',
3635 $tag,
3636 describeLC( $bodyLine, $bodyColumn ),
3637 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3638 push @mistakes,
3639 {
3640 desc => $msg,
3641 subpolicy => [ $tag, 'body-indent' ],
3642 parentLine => $parentLine,
3643 parentColumn => $parentColumn,
3644 line => $bodyLine,
3645 column => $bodyColumn,
3646 };
3647 }
3648 }
3649  
3650 return \@mistakes;
3651 }
3652  
3653 sub checkFord_1 {
3654 my ( $policy, $node ) = @_;
3655 my $instance = $policy->{lint};
3656  
3657 # TODO Split is implemented, but not split Ford-1 hoon
3658 # is represented in the corpus AFAICT
3659  
3660 my $runeName = $policy->runeName($node);
3661  
3662 # fordFassig ::= (- FAS SIG GAP -) tall5d
3663 my ( $gap, $body ) = @{ $policy->gapSeq0($node) };
3664  
3665 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3666 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
3667  
3668 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
3669 $node,
3670 {
3671 fordFascen => 1,
3672 }
3673 );
3674 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
3675  
3676 my @mistakes = ();
3677  
3678 my $expectedBodyColumn;
3679  
3680 BODY_ISSUES: {
3681 if ( $parentLine == $bodyLine ) {
3682 my $expectedBodyColumn = $anchorColumn + 4;
3683 if ( $bodyColumn != $expectedBodyColumn ) {
3684 my $msg =
3685 sprintf 'joined %s body %s is %s',
3686 $runeName,
3687 describeLC( $bodyLine, $bodyColumn ),
3688 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3689 push @mistakes,
3690 {
3691 desc => $msg,
3692 subpolicy => [ $runeName, 'hgap' ],
3693 parentLine => $parentLine,
3694 parentColumn => $parentColumn,
3695 reportLine => $bodyLine,
3696 reportColumn => $bodyColumn,
3697 line => $bodyLine,
3698 column => $bodyColumn,
3699 details => [ [ @{$anchorDetails} ] ],
3700 };
3701 }
3702 last BODY_ISSUES;
3703 }
3704  
3705 # If here parent line != body line
3706 $expectedBodyColumn = $anchorColumn;
3707 push @mistakes,
3708 @{
3709 $policy->checkOneLineGap(
3710 $gap,
3711 {
3712 mainColumn => $expectedBodyColumn,
3713 runeName => $runeName,
3714 subpolicy => [$runeName],
3715 details => [ [ @{$anchorDetails}, ], ],
3716 }
3717 )
3718 };
3719  
3720 if ( $bodyColumn != $expectedBodyColumn ) {
3721 my $msg =
3722 sprintf 'split %s body %s is %s',
3723 $runeName,
3724 describeLC( $bodyLine, $bodyColumn ),
3725 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3726 push @mistakes,
3727 {
3728 desc => $msg,
3729 subpolicy => [ $runeName, 'body-indent' ],
3730 details => [ [ @{$anchorDetails} ] ],
3731 parentLine => $parentLine,
3732 parentColumn => $parentColumn,
3733 line => $bodyLine,
3734 column => $bodyColumn,
3735 };
3736 }
3737 }
3738  
3739 return \@mistakes;
3740 }
3741  
3742 sub checkFaswut {
3743 my ( $policy, $node ) = @_;
3744 my $instance = $policy->{lint};
3745  
3746 # FASWUT is special, so we need to find the components using low-level
3747 # techniques.
3748 # fordFaswut ::= (- FAS WUT GAP -) DIT4K_SEQ (- GAP -)
3749 my ( undef, undef, $leaderGap, $body, $trailerGap ) =
3750 @{ $node->{children} };
3751  
3752 # TODO: Should we require that parent column be 0?
3753 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
3754 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
3755  
3756 my @mistakes = ();
3757 my $runeName = 'faswut';
3758 my $tag = 'faswut';
3759  
3760 my $expectedColumn;
3761  
3762 BODY_ISSUES: {
3763 if ( $parentLine != $bodyLine ) {
3764 my $msg = sprintf 'Faswut body %s; must be on rune line',
3765 describeLC( $bodyLine, $bodyColumn );
3766 push @mistakes,
3767 {
3768 desc => $msg,
3769 parentLine => $parentLine,
3770 parentColumn => $parentColumn,
3771 line => $bodyLine,
3772 column => $bodyColumn,
3773 };
3774 last BODY_ISSUES;
3775 }
3776 my $expectedBodyColumn = $parentColumn + 4;
3777 if ( $bodyColumn != $expectedBodyColumn ) {
3778 my $msg =
3779 sprintf 'body %s is %s',
3780 describeLC( $bodyLine, $bodyColumn ),
3781 describeMisindent2( $bodyColumn, $expectedBodyColumn );
3782 push @mistakes,
3783 {
3784 desc => $msg,
3785 parentLine => $parentLine,
3786 parentColumn => $parentColumn,
3787 line => $bodyLine,
3788 column => $bodyColumn,
3789 subpolicy => $policy->nodeSubpolicy($node) . ':hgap',
3790 };
3791 }
3792 last BODY_ISSUES;
3793  
3794 }
3795  
3796 $expectedColumn = $parentColumn;
3797 push @mistakes,
3798 @{
3799 $policy->checkOneLineGap(
3800 $trailerGap,
3801 {
3802 mainColumn => $expectedColumn,
3803 runeName => $tag,
3804 subpolicy => [ $runeName ],
3805 }
3806 )
3807 };
3808  
3809 return \@mistakes;
3810 }
3811  
3812 # The only lutes in the arvo/ corpus are one-liners.
3813 # We treat one-line lutes as free-form -- never any errors.
3814 # We report "not yet implemented" for multi-line lutes.
3815 sub checkLute {
3816 my ( $policy, $node ) = @_;
3817 my $instance = $policy->{lint};
3818  
3819 # lutes are special, so we need to find the components using low-level
3820 # techniques.
3821 # lute5d ::= (- SEL GAP -) tall5dSeq (- GAP SER -)
3822 my $children = $node->{children};
3823 my $sel = $children->[0];
3824 my $ser = $children->[-1];
3825 my ($selLine) = $instance->nodeLC($sel);
3826 my ($serLine) = $instance->nodeLC($ser);
3827  
3828 return $instance->checkNYI($node) if $selLine != $serLine;
3829 return [];
3830 }
3831  
3832 sub checkSplit_0Running {
3833 my ( $policy, $args ) = @_;
3834 my $instance = $policy->{lint};
3835 my $minimumRunsteps = $instance->{minSplit_0RunningSteps} // 0;
3836  
3837 my $node = $args->{node};
3838 my $rune = $args->{rune};
3839 my $runningGap = $args->{runningGap};
3840 my $runningChildren = $args->{runningChildren};
3841 my $tistisGap = $args->{tistisGap};
3842 my $tistis = $args->{tistis};
3843 my $runningLine = $args->{runningLine};
3844 my $runningColumn = $args->{runningColumn};
3845 my $runeName = $args->{runeName};
3846 my $anchorLine = $args->{anchorLine};
3847 my $anchorColumn = $args->{anchorColumn};
3848 my $anchorDetails = $args->{anchorDetails};
3849  
3850 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
3851 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
3852  
3853 my $expectedColumn = $anchorColumn + 2;
3854  
3855 my @mistakes = ();
3856  
3857 # We deal with the running list here, rather than
3858 # in its own node
3859  
3860 my $runStepCount = ( scalar @{$runningChildren} ) / 2;
3861 if ( $runStepCount < $minimumRunsteps ) {
3862  
3863 # Untested
3864  
3865 my $msg = sprintf '%s %s; too few runsteps; has %d, minimum is %d',
3866 $runeName,
3867 describeLC( $runningLine, $runningColumn ),
3868 $runStepCount, $minimumRunsteps;
3869 push @mistakes,
3870 {
3871 desc => $msg,
3872 subpolicy => [ $runeName, 'split-runstep-count' ],
3873 parentLine => $runeLine,
3874 parentColumn => $runeColumn,
3875 line => $runningLine,
3876 column => $runningColumn,
3877 reportLine => $runningLine,
3878 reportColumn => $runningColumn,
3879 };
3880 }
3881  
3882 push @mistakes,
3883 @{
3884 $policy->checkOneLineGap(
3885 $runningGap,
3886 {
3887 mainColumn => $anchorColumn,
3888 preColumn => $expectedColumn,
3889 runeName => $runeName,
3890 subpolicy => [ $runeName, 'running-vgap' ],
3891 }
3892 )
3893 };
3894  
3895 push @mistakes,
3896 @{
3897 $policy->checkRunning(
3898 {
3899 children => $runningChildren,
3900 subpolicy => [ $runeName ],
3901 anchorColumn => $anchorColumn,
3902 expectedColumn => $expectedColumn,
3903 anchorDetails => $anchorDetails,
3904 }
3905 )
3906 };
3907  
3908 push @mistakes,
3909 @{
3910 $policy->checkOneLineGap(
3911 $tistisGap,
3912 {
3913 mainColumn => $anchorColumn,
3914 preColumn => $expectedColumn,
3915 runeName => $runeName,
3916 subpolicy => [ $runeName, 'tistis-vgap' ],
3917 topicLines => [ $anchorLine, $tistisLine ],
3918 }
3919 )
3920 };
3921  
3922 push @mistakes,
3923 @{
3924 $policy->checkTistis(
3925 $tistis,
3926 {
3927 subpolicy => [ $runeName ],
3928 tag => $runeName,
3929 expectedColumn => $anchorColumn,
3930 }
3931 )
3932 };
3933 return \@mistakes;
3934 }
3935  
3936 sub checkJoined_0Running {
3937 my ( $policy, $args, $joinColumn ) = @_;
3938 my $instance = $policy->{lint};
3939 my $maximumRunsteps = $instance->{maxJoined_0RunningSteps};
3940  
3941 my $node = $args->{node};
3942 my $rune = $args->{rune};
3943 my $runningGap = $args->{runningGap};
3944 my $runningChildren = $args->{runningChildren};
3945 my $tistisGap = $args->{tistisGap};
3946 my $tistis = $args->{tistis};
3947 my $runningLine = $args->{runningLine};
3948 my $runningColumn = $args->{runningColumn};
3949 my $runeName = $args->{runeName};
3950 my $anchorLine = $args->{anchorLine};
3951 my $anchorColumn = $args->{anchorColumn};
3952 my $anchorDetails = $args->{anchorDetails};
3953 my $pseudojoin = $args->{pseudojoin};
3954  
3955 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
3956 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
3957  
3958 my @mistakes = ();
3959 my $expectedColumn = $joinColumn >= 0 ? $joinColumn : $runeColumn + 4;
3960  
3961 my $runStepCount = ( scalar @{$runningChildren} ) / 2;
3962 if ( defined $maximumRunsteps and $runStepCount > $maximumRunsteps ) {
3963  
3964 # Untested
3965 my $msg = sprintf
3966 '%s; too many runsteps; has %d, maximum is %d',
3967 describeLC( $runningLine, $runningColumn ),
3968 $runStepCount, $maximumRunsteps;
3969 push @mistakes,
3970 {
3971 desc => $msg,
3972 subpolicy => [ $runeName, 'joined-runstep-count' ],
3973 parentLine => $runeLine,
3974 parentColumn => $runeColumn,
3975 line => $runningLine,
3976 column => $runningColumn,
3977 reportLine => $runningLine,
3978 reportColumn => $runningColumn,
3979 };
3980 }
3981  
3982 push @mistakes,
3983 @{
3984 $policy->checkRunning(
3985 {
3986 children => $runningChildren,
3987 subpolicy => [ $runeName ],
3988 anchorColumn => $anchorColumn,
3989 expectedColumn => $expectedColumn,
3990 pseudojoin => $pseudojoin,
3991 }
3992 )
3993 };
3994  
3995 push @mistakes,
3996 @{
3997 $policy->checkOneLineGap(
3998 $tistisGap,
3999 {
4000 mainColumn => $runeColumn,
4001 preColumn => $expectedColumn,
4002 runeName => $runeName,
4003 subpolicy => [ $runeName, 'tistis-vgap' ],
4004 topicLines => [ $runeLine, $tistisLine ],
4005 }
4006 )
4007 };
4008  
4009 push @mistakes,
4010 @{
4011 $policy->checkTistis(
4012 $tistis,
4013 {
4014 subpolicy => [ $runeName ],
4015 tag => $runeName,
4016 expectedColumn => $runeColumn,
4017 }
4018 )
4019 };
4020 return \@mistakes;
4021 }
4022  
4023 sub check_1Running {
4024 my ( $policy, $node ) = @_;
4025 my $instance = $policy->{lint};
4026 my $lineToPos = $instance->{lineToPos};
4027  
4028 my $runeName = $policy->runeName($node);
4029  
4030 my ( $rune, $headGap, $head, $runningGap, $running, $tistisGap, $tistis ) =
4031 @{ $policy->gapSeq($node) };
4032  
4033 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
4034 my ( $anchorLine, $anchorColumn ) = ( $runeLine, $runeColumn );
4035 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
4036 my ( $runningLine, $runningColumn ) = $instance->nodeLC($running);
4037 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
4038  
4039 my @mistakes = ();
4040 if ( $headLine != $runeLine ) {
4041 my $msg = sprintf
4042 "$runeName head %s; should be on rune line %d",
4043 describeLC( $headLine, $headColumn ),
4044 $runeLine;
4045 push @mistakes,
4046 {
4047 desc => $msg,
4048 subpolicy => [ $runeName, 'head-split' ],
4049 parentLine => $runeLine,
4050 parentColumn => $runeColumn,
4051 line => $headLine,
4052 column => $headColumn,
4053 reportLine => $headLine,
4054 reportColumn => $headColumn,
4055 expectedLine => $runeLine,
4056 };
4057 }
4058  
4059 my $expectedColumn = $runeColumn + 4;
4060 if ( $headColumn != $expectedColumn ) {
4061 my $msg = sprintf
4062 "$runeName head %s; %s",
4063 describeLC( $headLine, $headColumn ),
4064 describeMisindent2( $headColumn, $expectedColumn );
4065 push @mistakes,
4066 {
4067 desc => $msg,
4068 subpolicy => [ $runeName, 'head-hgap' ],
4069 parentLine => $runeLine,
4070 parentColumn => $runeColumn,
4071 line => $headLine,
4072 column => $headColumn,
4073 reportLine => $headLine,
4074 reportColumn => $headColumn,
4075 };
4076 }
4077  
4078 $expectedColumn = $anchorColumn + 2;
4079  
4080 # Note: runnings are never pseudo-joined, at
4081 # least not in the corpus.
4082 if ( $headLine != $runningLine ) {
4083 push @mistakes,
4084 @{
4085 $policy->checkOneLineGap(
4086 $runningGap,
4087 {
4088 mainColumn => $anchorColumn,
4089 preColumn => $runningColumn,
4090 runeName => $runeName,
4091 subpolicy => [ $runeName ],
4092 }
4093 )
4094 };
4095  
4096 my @runningChildren = ( $runningGap, @{ $running->{children} } );
4097  
4098 push @mistakes,
4099 @{
4100 $policy->checkRunning(
4101 {
4102 children => \@runningChildren,
4103 subpolicy => [ $runeName ],
4104 anchorColumn => $anchorColumn,
4105 expectedColumn => $expectedColumn,
4106 }
4107 )
4108 };
4109  
4110 }
4111 else {
4112 # joined, that is, $headLine == $runningLine
4113 my $gapLength = $runningGap->{length};
4114  
4115 if ( $gapLength != 2 ) {
4116 my $msg = sprintf
4117 "1-jogging running 1 %s; %s",
4118 describeLC( $runningLine, $runningColumn ),
4119 describeMisindent2( $gapLength, 2 );
4120 push @mistakes,
4121 {
4122 desc => $msg,
4123 subpolicy => [ $runeName, 'running-hgap' ],
4124 parentLine => $runeLine,
4125 parentColumn => $runeColumn,
4126 line => $runningLine,
4127 column => $runningColumn,
4128 reportLine => $runningLine,
4129 reportColumn => $runningColumn,
4130 };
4131 }
4132  
4133 my @runningChildren = ( $runningGap, @{ $running->{children} } );
4134  
4135 push @mistakes,
4136 @{
4137 $policy->checkRunning(
4138 {
4139 skipFirst => 1,
4140 children => \@runningChildren,
4141 subpolicy => [ $runeName ],
4142 anchorColumn => $anchorColumn,
4143 expectedColumn => $expectedColumn
4144 }
4145 )
4146 };
4147  
4148 }
4149  
4150 # We deal with the running list here, rather that
4151 # in its own node
4152  
4153 push @mistakes,
4154 @{
4155 $policy->checkOneLineGap(
4156 $tistisGap,
4157 {
4158 mainColumn => $runeColumn,
4159 preColumn => $expectedColumn,
4160 runeName => $runeName,
4161 subpolicy => [ $runeName, 'tistis-gap' ],
4162 topicLines => [ $runeLine, $tistisLine ],
4163 }
4164 )
4165 };
4166  
4167 $expectedColumn = $runeColumn;
4168  
4169 push @mistakes,
4170 @{
4171 $policy->checkTistis(
4172 $tistis,
4173 {
4174 tag => $runeName,
4175 subpolicy => [$runeName],
4176 expectedColumn => $runeColumn,
4177 }
4178 )
4179 };
4180  
4181 return \@mistakes;
4182 }
4183  
4184 # Find the oversize alignment for this column of elements.
4185 # of a chain alignment
4186 # $nodes must a ref to an array of repeating
4187 # ( gap, body, backdentColumn ) elements
4188 sub findChainAlignment {
4189 my ( $policy, $nodes ) = @_;
4190 my $instance = $policy->{lint};
4191 my %allAlignments = ();
4192  
4193 # local $Data::Dumper::Maxdepth = 1;
4194 # say STDERR join " ", __FILE__, __LINE__;
4195  
4196 # "wide", meaning an alignment following a gap of more than
4197 # one stop
4198 my %wideAlignments = ();
4199 CHILD: for ( my $bodyIX = 2 ; 1 ; $bodyIX += 3 ) {
4200 my $body = $nodes->[$bodyIX -1];
4201 # say STDERR join " ", __FILE__, __LINE__, $bodyIX, $#$nodes;
4202 # say STDERR join " ", __FILE__, __LINE__, $nodes->[$bodyIX];
4203 last CHILD if not defined $body;
4204 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC( $body );
4205 # say STDERR join " ", __FILE__, __LINE__, $bodyLine, $bodyColumn;
4206 my $backdentColumn = $nodes->[$bodyIX];
4207 next CHILD if $bodyColumn == $backdentColumn;
4208 $allAlignments{$bodyColumn} //= [];
4209 push @{ $allAlignments{$bodyColumn} }, $bodyLine;
4210 my $gap = $nodes->[ $bodyIX - 2 ];
4211 my $gapLength = $instance->gapLength($gap);
4212 next CHILD if $gapLength <= 2;
4213 $wideAlignments{$bodyColumn} //= [];
4214 push @{ $wideAlignments{$bodyColumn} }, $bodyLine;
4215 }
4216  
4217 # say STDERR join " ", __FILE__, __LINE__, scalar @{$nodes};
4218  
4219 return [-1, {lines => []}] if not scalar %wideAlignments;
4220  
4221 # wide alignments, in order first by descending count of wide instances;
4222 # then by descending count of all instances;
4223 # then by ascending first line
4224 my @sortedWideAlignments =
4225 sort {
4226 ( scalar @{ $wideAlignments{$b} } ) <=> ( scalar @{ $wideAlignments{$a} } )
4227 or ( scalar @{ $allAlignments{$b} } ) <=> ( scalar @{ $allAlignments{$a} } )
4228 or $wideAlignments{$a}[0] <=> $wideAlignments{$b}[0]
4229 }
4230 keys %wideAlignments;
4231  
4232 my $topWideColumn = $sortedWideAlignments[0];
4233  
4234
4235 # say STDERR join " ", __FILE__, __LINE__, $topWideColumn;
4236  
4237 # Make sure this is actually an *alignment*, that is,
4238 # that there are at least 2 instances. Otherwise,
4239 # ignore it.
4240 return [-1, {lines => []}] if scalar @{ $allAlignments{$topWideColumn} } <= 1;
4241  
4242 # say STDERR join " ", __FILE__, __LINE__, $topWideColumn;
4243  
4244 return [$topWideColumn, {lines => [splice(@{$allAlignments{$topWideColumn}}, 0, 5)]}];
4245  
4246 }
4247  
4248 sub chainAlignmentData {
4249 my ( $policy, $argNode ) = @_;
4250 my $instance = $policy->{lint};
4251 my $chainable = $policy->{chainable};
4252 my $grammar = $instance->{grammar};
4253  
4254 state $mortar = {
4255 tall5d => 1,
4256 norm5d => 1,
4257 };
4258  
4259 my $argNodeIX = $argNode->{IX};
4260 my $chainAlignmentData =
4261 $policy->{perNode}->{$argNodeIX}->{chainAlignmentData};
4262 return $chainAlignmentData if $chainAlignmentData;
4263  
4264 # Find the base column of the alignment
4265 my $alignmentBaseColumn;
4266 my $chainHead;
4267 my ( $argNodeLine, $argNodeColumn ) = $instance->nodeLC($argNode);
4268 my $thisNode = $argNode;
4269 # say STDERR join " ", __FILE__, __LINE__;
4270 LINK: while ($thisNode) {
4271 # say STDERR join " ", __FILE__, __LINE__;
4272 last LINK if $thisNode->{NEXT}; # Must be rightmost
4273 my $symbolName = $instance->symbol($thisNode);
4274 if ($mortar->{$symbolName}) {
4275 $thisNode = $thisNode->{PARENT};
4276 next LINK;
4277 }
4278  
4279 # TODO -- replace this test by always ending the LINK
4280 # loop on non-recognized-mortar non-chainable.
4281 last LINK if $symbolName =~ m/^(rick5dJog|ruck5dJog|fordHoop)$/;
4282 # say STDERR join " ", __FILE__, __LINE__;
4283 if (not $policy->chainable($thisNode) and not $instance->brickName($thisNode)) {
4284 say STDERR join " ", __FILE__, __LINE__, $instance->symbol($thisNode);
4285 }
4286  
4287 last LINK if not $policy->chainable($thisNode); # Must be chainable
4288 # say STDERR join " ", __FILE__, __LINE__;
4289 my ( $thisNodeLine, $thisNodeColumn ) = $instance->nodeLC($thisNode);
4290 last LINK if $thisNodeLine != $argNodeLine;
4291 # say STDERR join " ", __FILE__, __LINE__;
4292 $alignmentBaseColumn = $thisNodeColumn;
4293 $chainHead = $thisNode;
4294 $thisNode = $thisNode->{PARENT};
4295 }
4296 # say STDERR join " ", __FILE__, __LINE__;
4297  
4298 return if not defined $alignmentBaseColumn; # TODO: delete after development
4299  
4300 # Find the head (first link) of this alignment chain
4301 LINK: while ($thisNode) {
4302 last LINK if $thisNode->{NEXT}; # Must be rightmost
4303 my $symbolName = $instance->symbol($thisNode);
4304 if ($mortar->{$symbolName}) {
4305 $thisNode = $thisNode->{PARENT};
4306 next LINK;
4307 }
4308 last LINK if not $policy->chainable($thisNode); # Must be chainable
4309 my ( $thisNodeLine, $thisNodeColumn ) = $instance->nodeLC($thisNode);
4310 last LINK if $thisNodeColumn < $alignmentBaseColumn;
4311 $chainHead = $thisNode if $thisNodeColumn == $alignmentBaseColumn;
4312 $thisNode = $thisNode->{PARENT};
4313 }
4314  
4315 # Traverse the whole chain, from the head
4316 $thisNode = $chainHead;
4317 my $parentNodeLine = -1;
4318 my $parentElementCount = 0;
4319 my $currentChainOffset = 0;
4320 my $thisNodeIX;
4321  
4322 # Array, indexed by child index, of "silos"
4323 # of [ gap, body ], to align
4324 my @nodesToAlignByChildIX;
4325 LINK: while ($thisNode) {
4326 # say STDERR sprintf q{Traversing %s}, $instance->literalNode($thisNode);
4327  
4328 # End of loop tests --
4329 # Is it chainable?
4330 # Is the line,column right for the current chain?
4331 # CURRENT say STDERR join q{ }, __FILE__, __LINE__;
4332 my $symbolName = $instance->symbol($thisNode);
4333 # Skip if it's one of the appropriate list of mortar symbols.
4334 if ($mortar->{$symbolName}) {
4335 my $children = $thisNode->{children};
4336 $thisNode = $children->[$#$children];
4337 next LINK;
4338 }
4339 last LINK if not $policy->chainable($thisNode);
4340 my ( $thisNodeLine, $thisNodeColumn ) = $instance->nodeLC($thisNode);
4341 if ( $thisNodeColumn == $alignmentBaseColumn ) {
4342 $currentChainOffset = 0;
4343 }
4344 else {
4345 last LINK if $thisNodeLine != $parentNodeLine;
4346 $currentChainOffset += $parentElementCount;
4347 }
4348  
4349 # Tracks "last" node IX, so do not set $thisNodeIX before "end
4350 # of loop" tests.
4351 $thisNodeIX = $thisNode->{IX};
4352 # CURRENT say STDERR join q{ }, __FILE__, __LINE__;
4353  
4354 my @thisGapSeq = @{ $policy->gapSeq0($thisNode) };
4355 my $elementCount = ( scalar @thisGapSeq ) / 2;
4356  
4357 ELEMENT:
4358 for (
4359 my $elementNumber = 1 ;
4360 $elementNumber <= $elementCount ;
4361 $elementNumber++
4362 )
4363 {
4364 my $gap = $thisGapSeq[ $elementNumber * 2 - 2 ];
4365 my $element = $thisGapSeq[ $elementNumber * 2 - 1 ];
4366 my ($gapLine) = $instance->nodeLC($gap);
4367 my ($elementLine) = $instance->nodeLC($element);
4368  
4369 # Not in aligment if not on rune line
4370 next ELEMENT if $elementLine != $thisNodeLine;
4371  
4372 my $backdentColumn =
4373 $thisNodeColumn + ( $elementCount - $elementNumber ) * 2;
4374 push @{ $nodesToAlignByChildIX[$currentChainOffset + $elementNumber-1] }, $gap, $element, $backdentColumn;
4375 }
4376  
4377 $policy->{perNode}->{$thisNodeIX}->{chainAlignmentData} =
4378 { offset => $currentChainOffset };
4379  
4380 # Set "parent" data for next pass
4381 $parentNodeLine = $thisNodeLine;
4382 $parentElementCount = $elementCount;
4383  
4384 # descend to rightmost child
4385 my $children = $thisNode->{children};
4386 $thisNode = $children->[$#$children];
4387 }
4388  
4389 my $lastNodeIX = $thisNodeIX;
4390  
4391 my @chainAlignments = ();
4392 ELEMENT:
4393 for (
4394 my $childIX = 0 ;
4395 $childIX <= $#nodesToAlignByChildIX;
4396 $childIX++
4397 )
4398 {
4399 my $nodesToAlign = $nodesToAlignByChildIX[$childIX];
4400 if (not $nodesToAlign) {
4401 $chainAlignments[ $childIX ] = [ -1, [] ];
4402 next ELEMENT;
4403 }
4404 $chainAlignments[ $childIX ] = $policy->findChainAlignment( $nodesToAlign);
4405 }
4406  
4407 $thisNode = $chainHead;
4408 LINK: while ($thisNode) {
4409 my $thisNodeIX = $thisNode->{IX};
4410 $policy->{perNode}->{$thisNodeIX}->{chainAlignmentData}->{alignments} =
4411 \@chainAlignments;
4412 last LINK if $thisNodeIX == $lastNodeIX;
4413  
4414 # descend to rightmost child
4415 my $children = $thisNode->{children};
4416 $thisNode = $children->[$#$children];
4417 }
4418  
4419 $chainAlignmentData = $policy->{perNode}->{$argNodeIX}->{chainAlignmentData};
4420 return $chainAlignmentData;
4421 }
4422  
4423 sub joggingHoonData {
4424 my ( $policy, $options, $node ) = @_;
4425 my $runeName = $policy->runeName($node);
4426 my %result = ( node => $node, runeName => $runeName );
4427 my $instance = $policy->{lint};
4428 my $children = $node->{children};
4429 my $anchorColumn = $options->{anchorColumn};
4430 if ( not defined $anchorColumn ) {
4431 ( undef, $anchorColumn ) = $instance->nodeLC($node);
4432 }
4433 $result{anchorColumn} = $anchorColumn;
4434 my $firstOKJogLine = $options->{firstOKJogLine} // 0;
4435 $result{firstOKJogLine} = $firstOKJogLine;
4436  
4437 my $previousChild;
4438 CHILD: for my $childIX ( 0 .. $#$children ) {
4439 my $child = $children->[$childIX];
4440 my $symbol = $instance->symbol($child);
4441 if ( $symbol ne 'rick5d' and $symbol ne 'ruck5d' ) {
4442 $previousChild = $child;
4443 next CHILD;
4444 }
4445 my ($previousChildLine) = $instance->nodeLC($previousChild);
4446 if ($runeName eq 'tiscol') {
4447 my $chessSide = 'kingside';
4448 $result{chessSide} = $chessSide;
4449 my $baseColumn = $anchorColumn + 4;
4450 $result{jogBaseColumn} = $baseColumn;
4451 } else {
4452 my $chessSide =
4453 $policy->chessSideOfJogging( $child, $anchorColumn,
4454 $previousChildLine + 1 );
4455 $result{chessSide} = $chessSide;
4456 my $baseColumn = $anchorColumn + ( $chessSide eq 'queenside' ? 4 : 2 );
4457 $result{jogBaseColumn} = $baseColumn;
4458 }
4459 my $jogBodyData = $policy->joggingBodyAlignment($child);
4460 ( $result{bodyAlignment}, $result{alignments} ) = @{$jogBodyData};
4461 return \%result;
4462 }
4463 die "No jogging found for ", $instance->symbol($node);
4464 }
4465  
4466 sub chessSideOfPairSequence {
4467 my ( $policy, $node, $runeColumn ) = @_;
4468 my $instance = $policy->{lint};
4469  
4470 my $symbolReverseDB = $instance->{symbolReverseDB};
4471 my $children = $node->{children};
4472 my %sideCount = ();
4473 my %bodyColumnCount = ();
4474 my $kingsideCount = 0;
4475 my $queensideCount = 0;
4476 my $lastLine = -1;
4477 CHILD: for my $childIX ( 0 .. $#$children ) {
4478 my $jog = $children->[$childIX];
4479 my $symbol = $jog->{symbol};
4480 next CHILD if defined $symbol and $symbolReverseDB->{$symbol}->{gap};
4481 my $head = $jog->{children}->[0];
4482 my ( $line1, $column1 ) = $instance->line_column( $head->{start} );
4483 next CHILD if $line1 == $lastLine;
4484 $lastLine = $line1;
4485 # say STDERR " $column1 - $runeColumn >= 4 ";
4486 if ( $column1 - $runeColumn >= 4 ) {
4487 $queensideCount++;
4488 next CHILD;
4489 }
4490 $kingsideCount++;
4491 }
4492 return $kingsideCount > $queensideCount
4493 ? 'kingside'
4494 : 'queenside';
4495 }
4496  
4497 sub chessSideOfJogging {
4498 my ( $policy, $node, $runeColumn, $firstLine ) = @_;
4499 my $instance = $policy->{lint};
4500  
4501 my $symbolReverseDB = $instance->{symbolReverseDB};
4502 my $children = $node->{children};
4503 my %sideCount = ();
4504 my %bodyColumnCount = ();
4505 my $kingsideCount = 0;
4506 my $queensideCount = 0;
4507 my $lastLine = -1;
4508 CHILD: for my $childIX ( 0 .. $#$children ) {
4509 my $jog = $children->[$childIX];
4510 my $symbol = $jog->{symbol};
4511 next CHILD if defined $symbol and $symbolReverseDB->{$symbol}->{gap};
4512 my $head = $jog->{children}->[0];
4513 my ( $line1, $column1 ) = $instance->line_column( $head->{start} );
4514 # Enforce a "first line" to disallow first line of joggings
4515 # which are not preceded by a vertical gap.
4516 next CHILD if $line1 < $firstLine;
4517 next CHILD if $line1 == $lastLine;
4518 $lastLine = $line1;
4519 # say STDERR " $column1 - $runeColumn >= 4 ";
4520 if ( $column1 - $runeColumn >= 4 ) {
4521 $queensideCount++;
4522 next CHILD;
4523 }
4524 $kingsideCount++;
4525 }
4526 return $kingsideCount > $queensideCount
4527 ? 'kingside'
4528 : 'queenside';
4529 }
4530  
4531 # Find the oversize alignment for this column of elements.
4532 # $nodes must a ref to an array of alternating gap, body,
4533 # nodes.
4534 sub findAlignment {
4535 my ( $policy, $nodes ) = @_;
4536 my $instance = $policy->{lint};
4537 my %allAlignments = ();
4538  
4539 # local $Data::Dumper::Maxdepth = 1;
4540 # say STDERR join " ", __FILE__, __LINE__;
4541  
4542 # "wide", meaning an alignment following a gap of more than
4543 # one stop
4544 my %wideAlignments = ();
4545 CHILD: for ( my $bodyIX = 1 ; 1 ; $bodyIX += 2 ) {
4546 my $body = $nodes->[$bodyIX];
4547 # say STDERR join " ", __FILE__, __LINE__, $bodyIX, $#$nodes;
4548 # say STDERR join " ", __FILE__, __LINE__, $nodes->[$bodyIX];
4549 last CHILD if not defined $body;
4550 # say STDERR join " ", __FILE__, __LINE__, $nodes->[$bodyIX];
4551 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC( $body );
4552 $allAlignments{$bodyColumn} //= [];
4553 push @{ $allAlignments{$bodyColumn} }, $bodyLine;
4554 my $gap = $nodes->[ $bodyIX - 1 ];
4555 my $gapLength = $instance->gapLength($gap);
4556 next CHILD if $gapLength <= 2;
4557 $wideAlignments{$bodyColumn} //= [];
4558 push @{ $wideAlignments{$bodyColumn} }, $bodyLine;
4559 }
4560  
4561 # say STDERR join " ", __FILE__, __LINE__, scalar @{$nodes};
4562  
4563 return [-1, []] if not scalar %wideAlignments;
4564  
4565 # wide alignments, in order first by descending count of wide instances;
4566 # then by descending count of all instances;
4567 # then by ascending first line
4568 my @sortedWideAlignments =
4569 sort {
4570 ( scalar @{ $wideAlignments{$b} } ) <=> ( scalar @{ $wideAlignments{$a} } )
4571 or ( scalar @{ $allAlignments{$b} } ) <=> ( scalar @{ $allAlignments{$a} } )
4572 or $wideAlignments{$a}[0] <=> $wideAlignments{$b}[0]
4573 }
4574 keys %wideAlignments;
4575  
4576 my $topWideColumn = $sortedWideAlignments[0];
4577  
4578 # say STDERR join " ", __FILE__, __LINE__, $topWideColumn;
4579  
4580 # Make sure this is actually an *alignment*, that is,
4581 # that there are at least 2 instances. Otherwise,
4582 # ignore it.
4583 return [-1, []] if scalar @{ $allAlignments{$topWideColumn} } <= 1;
4584  
4585 # say STDERR join " ", __FILE__, __LINE__, $topWideColumn;
4586  
4587 return [$topWideColumn, [splice(@{$allAlignments{$topWideColumn}}, 0, 5)]];
4588  
4589 }
4590  
4591 sub joggingBodyAlignment {
4592 my ( $policy, $jogging ) = @_;
4593 my $instance = $policy->{lint};
4594 my $children = $jogging->{children};
4595  
4596 # Traverse first to last to make it easy to record
4597 # first line of occurrence of each body column
4598 my @nodesToAlign = ();
4599 CHILD:
4600 for ( my $childIX = 0; $childIX <= $#$children ; $childIX+=2 ) {
4601 my $jog = $children->[$childIX];
4602 my $jogChildren = $jog->{children};
4603 my $jogChild1 = $jogChildren->[1];
4604 my $jogChild2 = $jogChildren->[2];
4605 my ( $line1 ) = $instance->nodeLC($jogChild1);
4606 my ( $line2 ) = $instance->nodeLC($jogChild2);
4607 next CHILD if $line1 ne $line2;
4608 push @nodesToAlign, $jogChild1, $jogChild2;
4609 }
4610 return $policy->findAlignment( \@nodesToAlign );
4611 }
4612  
4613 sub check_1Jogging {
4614 my ( $policy, $node ) = @_;
4615 my $instance = $policy->{lint};
4616 my $lineToPos = $instance->{lineToPos};
4617  
4618 my ( $rune, $headGap, $head, $joggingGap, $jogging, $tistisGap, $tistis ) =
4619 @{ $policy->gapSeq($node) };
4620  
4621 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
4622 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
4623 my ( $joggingLine, $joggingColumn ) = $instance->nodeLC($jogging);
4624 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
4625  
4626 my $anchorLine = $runeLine;
4627 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
4628 $node,
4629 {
4630 tallKetlus => 1,
4631 }
4632 );
4633 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
4634  
4635 my $joggingHoonData = $policy->joggingHoonData(
4636 { anchorColumn => $anchorColumn, firstOKJogLine => $headLine + 1 },
4637 $node );
4638 $policy->setInheritedAttribute( $node, 'joggingHoonData',
4639 $joggingHoonData );
4640 my $jogBaseColumn = $joggingHoonData->{jogBaseColumn};
4641 my $chessSide = $joggingHoonData->{chessSide};
4642  
4643 my $joggingRules = $instance->{joggingRule};
4644  
4645 my @mistakes = ();
4646 my $runeName = $policy->runeName($node);
4647  
4648 CHECK_HEAD: {
4649 if ( $headLine != $runeLine ) {
4650 my $msg = sprintf
4651 "%s %s head %s; should be on rune line %d",
4652 $chessSide,
4653 $runeName,
4654 describeLC( $headLine, $headColumn ),
4655 $runeLine;
4656 push @mistakes,
4657 {
4658 desc => $msg,
4659 subpolicy => [ $runeName, 'split' ],
4660 parentLine => $runeLine,
4661 parentColumn => $runeColumn,
4662 line => $headLine,
4663 column => $headColumn,
4664 reportLine => $headLine,
4665 reportColumn => $headColumn,
4666 expectedLine => $runeLine,
4667 details => [ [ @{$anchorDetails} ]],
4668 };
4669 last CHECK_HEAD;
4670 }
4671  
4672 my $gapLength = $headGap->{length};
4673 my $expectedLength = ( $chessSide eq 'kingside' ? 2 : 4 );
4674 if ( $gapLength != $expectedLength ) {
4675 my $msg = sprintf
4676 "%s %s head %s; %s",
4677 $chessSide,
4678 $runeName,
4679 describeLC( $headLine, $headColumn ),
4680 describeMisindent2( $gapLength, $expectedLength );
4681 push @mistakes,
4682 {
4683 desc => $msg,
4684 subpolicy => [ $runeName, 'head-hgap' ],
4685 parentLine => $runeLine,
4686 parentColumn => $runeColumn,
4687 line => $headLine,
4688 column => $headColumn,
4689 reportLine => $headLine,
4690 reportColumn => $headColumn,
4691 details => [ [ @{$anchorDetails} ]],
4692 };
4693 }
4694 }
4695  
4696 CHECK_JOGGING_GAP: {
4697 if ( $headLine == $joggingLine ) {
4698 my $msg = sprintf
4699 "%s %s jogging %s should not be joined",
4700 $chessSide,
4701 $runeName,
4702 describeLC( $joggingLine, $joggingColumn );
4703 push @mistakes,
4704 {
4705 desc => $msg,
4706 subpolicy => [ $runeName, 'jogging-joined' ],
4707 parentLine => $runeLine,
4708 parentColumn => $runeColumn,
4709 line => $joggingLine,
4710 column => $joggingColumn,
4711 reportLine => $joggingLine,
4712 reportColumn => $joggingColumn,
4713 details => [ [ @{$anchorDetails} ]],
4714 };
4715 last CHECK_JOGGING_GAP;
4716 }
4717 push @mistakes,
4718 @{
4719 $policy->checkOneLineGap(
4720 $joggingGap,
4721 {
4722 mainColumn => $anchorColumn,
4723 preColumn => $jogBaseColumn,
4724 runeName => $runeName,
4725 subpolicy => [ $runeName, 'jogging-gap' ],
4726 parent => $rune,
4727 topicLines => [$joggingLine],
4728 details => [ [ @{$anchorDetails} ]],
4729 }
4730 )
4731 };
4732 }
4733  
4734 push @mistakes,
4735 @{
4736 $policy->checkOneLineGap(
4737 $tistisGap,
4738 {
4739 mainColumn => $anchorColumn,
4740 preColumn => $jogBaseColumn,
4741 runeName => $runeName,
4742 subpolicy => [ $runeName, 'tistis-gap' ],
4743 topicLines => [$tistisLine],
4744 details => [ [ @{$anchorDetails} ]],
4745 }
4746 )
4747 };
4748  
4749 push @mistakes,
4750 @{
4751 $policy->checkTistis(
4752 $tistis,
4753 {
4754 tag => $runeName,
4755 subpolicy => [$runeName],
4756 expectedColumn => $anchorColumn,
4757 details => [ [ @{$anchorDetails} ]],
4758 }
4759 )
4760 };
4761  
4762 return \@mistakes;
4763 }
4764  
4765 sub check_2Jogging {
4766 my ( $policy, $node ) = @_;
4767 my $instance = $policy->{lint};
4768 my $lineToPos = $instance->{lineToPos};
4769  
4770 my $runeName = $policy->runeName($node);
4771 my (
4772 $rune, $headGap, $head, $subheadGap, $subhead,
4773 $joggingGap, $jogging, $tistisGap, $tistis
4774 ) = @{ $policy->gapSeq($node) };
4775  
4776 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
4777 my ( $anchorLine, $anchorColumn ) = ( $runeLine, $runeColumn );
4778 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
4779 my ( $subheadLine, $subheadColumn ) = $instance->nodeLC($subhead);
4780 my ( $joggingLine, $joggingColumn ) = $instance->nodeLC($jogging);
4781 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
4782  
4783 my $joggingHoonData = $policy->joggingHoonData({}, $node );
4784 $policy->setInheritedAttribute($node, 'joggingHoonData', $joggingHoonData);
4785 my $jogBaseColumn = $joggingHoonData->{jogBaseColumn};
4786 my $chessSide = $joggingHoonData->{chessSide};
4787 my $isKingside = $chessSide eq 'kingside';
4788  
4789 my $joggingRules = $instance->{joggingRule};
4790  
4791 my @mistakes = ();
4792  
4793 my $isJoined = $headLine == $subheadLine;
4794 my @subpolicy =
4795 ( $runeName, $chessSide, ( $isJoined ? 'joined' : 'split' ) );
4796  
4797 if ( $headLine != $runeLine ) {
4798 my $msg = sprintf
4799 "%s %s head %s; should be on rune line %d",
4800 $chessSide,
4801 $runeName,
4802 describeLC( $headLine, $headColumn ),
4803 $runeLine;
4804 push @mistakes,
4805 {
4806 desc => $msg,
4807 subpolicy => [ @subpolicy, 'head-split' ],
4808 parentLine => $runeLine,
4809 parentColumn => $runeColumn,
4810 reportLine => $headLine,
4811 reportColumn => $headColumn,
4812 line => $headLine,
4813 column => $headColumn,
4814 };
4815 }
4816  
4817 my $expectedHeadColumn = $anchorColumn + ( $isKingside ? 4 : 6 );
4818 if ( $headColumn != $expectedHeadColumn ) {
4819 my $msg = sprintf
4820 "%s %s head %s; %s",
4821 $chessSide,
4822 $runeName,
4823 describeLC( $headLine, $headColumn ),
4824 describeMisindent2( $headColumn, $expectedHeadColumn );
4825 push @mistakes,
4826 {
4827 desc => $msg,
4828 subpolicy => [ @subpolicy, 'head-hgap' ],
4829 parentLine => $runeLine,
4830 parentColumn => $runeColumn,
4831 reportLine => $headLine,
4832 reportColumn => $headColumn,
4833 line => $headLine,
4834 column => $headColumn,
4835 };
4836 }
4837  
4838 if ($isJoined) {
4839 my $subheadLength = $subheadGap->{length};
4840 if ($subheadLength != 2) {
4841 my ( undef, $subheadGapColumn ) = $instance->nodeLC($subheadGap);
4842 my $expectedSubheadColumn = $subheadGapColumn + 2;
4843 my $msg = sprintf
4844 "%s %s subhead %s; %s",
4845 $chessSide,
4846 $runeName,
4847 describeLC( $subheadLine, $subheadColumn ),
4848 describeMisindent2( $subheadLength, 2 );
4849 push @mistakes,
4850 {
4851 desc => $msg,
4852 subpolicy => [ @subpolicy, 'subhead-hgap' ],
4853 parentLine => $runeLine,
4854 parentColumn => $runeColumn,
4855 reportLine => $subheadLine,
4856 reportColumn => $subheadColumn,
4857 line => $subheadLine,
4858 column => $subheadColumn,
4859 };
4860 }
4861 }
4862  
4863 if ( not $isJoined ) {
4864  
4865 # If here, we have "split heads", which should follow the "pseudo-jog"
4866 # format
4867  
4868 push @mistakes,
4869 @{
4870 $policy->checkOneLineGap(
4871 $subheadGap,
4872 {
4873 mainColumn => $anchorColumn,
4874 runeName => $runeName,
4875 subpolicy => [ @subpolicy, 'subhead-vgap' ],
4876 topicLines => [$subheadLine],
4877 }
4878 )
4879 };
4880  
4881 my $expectedSubheadColumn = $headColumn - 2;
4882 if ( $subheadColumn != $expectedSubheadColumn ) {
4883 my $msg = sprintf
4884 "%s %s subhead %s; %s",
4885 $chessSide,
4886 $runeName,
4887 describeLC( $subheadLine, $subheadColumn ),
4888 describeMisindent2( $subheadColumn, $expectedSubheadColumn );
4889 push @mistakes,
4890 {
4891 desc => $msg,
4892 subpolicy => [ @subpolicy, 'subhead-indent' ],
4893 parentLine => $runeLine,
4894 parentColumn => $runeColumn,
4895 reportLine => $subheadLine,
4896 reportColumn => $subheadColumn,
4897 line => $subheadLine,
4898 column => $subheadColumn,
4899 };
4900 }
4901 }
4902  
4903 push @mistakes,
4904 @{
4905 $policy->checkOneLineGap(
4906 $joggingGap,
4907 {
4908 mainColumn => $anchorColumn,
4909 preColumn => $jogBaseColumn,
4910 runeName => $runeName,
4911 subpolicy => [@subpolicy, 'jogging-vgap'],
4912 topicLines => [$joggingLine],
4913 }
4914 )
4915 };
4916  
4917 push @mistakes,
4918 @{
4919 $policy->checkOneLineGap(
4920 $tistisGap,
4921 {
4922 mainColumn => $anchorColumn,
4923 preColumn => $jogBaseColumn,
4924 runeName => $runeName,
4925 subpolicy => [@subpolicy, 'tistis-vgap'],
4926 topicLines => [$tistisLine],
4927 }
4928 )
4929 };
4930  
4931 push @mistakes,
4932 @{
4933 $policy->checkTistis(
4934 $tistis,
4935 {
4936 tag => $runeName,
4937 subpolicy => [@subpolicy],
4938 expectedColumn => $anchorColumn,
4939 }
4940 )
4941 };
4942  
4943 return \@mistakes;
4944 }
4945  
4946 sub check_Jogging1 {
4947 my ( $policy, $node ) = @_;
4948 my $instance = $policy->{lint};
4949 my $lineToPos = $instance->{lineToPos};
4950  
4951 my $runeName = $policy->runeName($node);
4952 my ( $rune, $joggingGap, $jogging, $tistisGap, $tistis, $tailGap, $tail ) =
4953 @{ $policy->gapSeq($node) };
4954  
4955 my ( $runeLine, $runeColumn ) = $instance->nodeLC($rune);
4956 my ( $joggingLine, $joggingColumn ) = $instance->nodeLC($jogging);
4957 my ( $tistisLine, $tistisColumn ) = $instance->nodeLC($tistis);
4958 my ( $tailLine, $tailColumn ) = $instance->nodeLC($tail);
4959 my ( $anchorLine, $anchorColumn ) = ( $runeLine, $runeColumn );
4960  
4961 my $joggingHoonData = $policy->joggingHoonData({}, $node );
4962 $policy->setInheritedAttribute($node, 'joggingHoonData', $joggingHoonData);
4963 my $jogBaseColumn = $joggingHoonData->{jogBaseColumn};
4964 my $chessSide = $joggingHoonData->{chessSide};
4965  
4966 my @mistakes = ();
4967  
4968 if ( $joggingLine != $runeLine ) {
4969 my $msg = sprintf
4970 "jogging %s; should be on rune line %d",
4971 describeLC( $joggingLine, $joggingColumn ),
4972 $runeLine;
4973 push @mistakes, {
4974 desc => $msg,
4975 subpolicy => [ $runeName, 'jogging', 'split' ],
4976 parentLine => $runeLine,
4977 parentColumn => $runeColumn,
4978 line => $joggingLine,
4979 column => $joggingColumn,
4980 reportLine => $joggingLine,
4981 reportColumn => $joggingColumn,
4982 expectedLine => $runeLine,
4983 };
4984 }
4985  
4986 if ( $joggingColumn != $jogBaseColumn ) {
4987 my $msg = sprintf
4988 "jogging %s; %s",
4989 describeLC( $joggingLine, $joggingColumn ),
4990 describeMisindent2( $joggingColumn, $jogBaseColumn );
4991 push @mistakes, {
4992 desc => $msg,
4993 subpolicy => [ $runeName, 'jogging-indent' ],
4994 parentLine => $runeLine,
4995 parentColumn => $runeColumn,
4996 line => $joggingLine,
4997 column => $joggingColumn,
4998 reportLine => $joggingLine,
4999 reportColumn => $joggingColumn,
5000 };
5001 }
5002  
5003 push @mistakes,
5004 @{
5005 $policy->checkOneLineGap(
5006 $tistisGap,
5007 {
5008 mainColumn => $anchorColumn,
5009 preColumn => $jogBaseColumn,
5010 runeName => $runeName,
5011 subpolicy => [$runeName, 'tistis-gap'],
5012 topicLines => [$tistisLine],
5013 }
5014 )
5015 };
5016  
5017 push @mistakes,
5018 @{
5019 $policy->checkTistis(
5020 $tistis,
5021 {
5022 tag => $runeName,
5023 subpolicy => [ $runeName ],
5024 expectedColumn => $anchorColumn + 2,
5025 }
5026 )
5027 };
5028  
5029 push @mistakes,
5030 @{
5031 $policy->checkOneLineGap(
5032 $tailGap,
5033 {
5034 mainColumn => $anchorColumn,
5035 runeName => $runeName,
5036 subpolicy => [$runeName, 'tail-vgap'],
5037 topicLines => [$tailLine],
5038 }
5039 )
5040 };
5041  
5042 my $expectedTailColumn = $anchorColumn;
5043 if ( $tailColumn != $expectedTailColumn ) {
5044 my $msg = sprintf
5045 "1-jogging tail %s; %s",
5046 describeLC( $tailLine, $tailColumn ),
5047 describeMisindent2( $tailColumn, $expectedTailColumn );
5048 push @mistakes,
5049 {
5050 desc => $msg,
5051 subpolicy => [ $runeName, 'tail-indent' ],
5052 parentLine => $runeLine,
5053 parentColumn => $runeColumn,
5054 line => $tailLine,
5055 column => $tailColumn,
5056 reportLine => $tailLine,
5057 reportColumn => $tailColumn,
5058 };
5059 }
5060  
5061 return \@mistakes;
5062 }
5063  
5064 sub fascomBodyAlignment {
5065 my ( $policy, $node ) = @_;
5066 my $instance = $policy->{lint};
5067 my $children = $node->{children};
5068 my $firstBodyColumn;
5069 my %firstLine = ();
5070 my %bodyColumnCount = ();
5071 my @nodesToAlign = ();
5072  
5073 CHILD:
5074 for ( my $childIX = 0 ; $childIX <= $#$children ; $childIX++ ) {
5075 my $jog = $children->[$childIX];
5076 my ( $gap, $body ) = @{ $policy->gapSeq0($jog) };
5077 my ( $headLine, $headColumn ) = $instance->nodeLC($jog);
5078 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
5079 next CHILD unless $headLine == $bodyLine;
5080 push @nodesToAlign, $gap, $body;
5081 }
5082 return $policy->findAlignment( \@nodesToAlign );
5083 }
5084  
5085 # Find the body column, based on alignment within
5086 # a parent hoon.
5087 sub fascomBodyColumn {
5088 my ( $policy, $node ) = @_;
5089 my $nodeIX = $node->{IX};
5090 my $fascomBodyColumn = $policy->{perNode}->{$nodeIX}->{fascomBodyColumn};
5091 return $fascomBodyColumn if defined $fascomBodyColumn;
5092  
5093 my $instance = $policy->{lint};
5094 my $nodeName = $instance->brickName($node);
5095 if ( not $nodeName or not $nodeName eq 'fordFascom' ) {
5096  
5097 my $fascomBodyColumn = $policy->fascomBodyColumn( $node->{PARENT} );
5098 $policy->{perNode}->{$nodeIX}->{fascomBodyColumn} = $fascomBodyColumn;
5099 return $fascomBodyColumn;
5100 }
5101  
5102 my $children = $node->{children};
5103 CHILD: for my $childIX ( 0 .. $#$children ) {
5104 my $child = $children->[$childIX];
5105 my $symbol = $instance->symbol($child);
5106 next CHILD if $symbol ne 'fordFascomBody';
5107 my $children2 = $child->{children};
5108 CHILD2: for my $childIX2 ( 0 .. $#$children2 ) {
5109 my $child2 = $children2->[$childIX2];
5110 my $symbol2 = $instance->symbol($child2);
5111 next CHILD2 if $symbol2 ne 'fordFascomElements';
5112 my $fascomBodyData = $policy->fascomBodyAlignment($child2);
5113 $policy->{perNode}->{$nodeIX}->{fascomBodyData} =
5114 $fascomBodyData;
5115 return $fascomBodyData;
5116 }
5117 }
5118 die "No jogging found for ", $instance->symbol($node);
5119 }
5120  
5121 # TODO: Add a check (optional?) for queenside joggings with no
5122 # split jogs.
5123 sub checkFascomElement {
5124 my ( $policy, $node ) = @_;
5125 my $instance = $policy->{lint};
5126  
5127 my $runeNode = $instance->ancestorByBrickName( $node, 'fordFascom' );
5128 my $runeNodeIX = $runeNode->{IX};
5129 my $isJoined = $policy->{perNode}->{$runeNodeIX}->{isJoined};
5130 my $chessSide = $policy->{perNode}->{$runeNodeIX}->{chessSide};
5131 my $queenside = $chessSide eq 'queenside';
5132  
5133 my ( $runeLine, $runeColumn ) = $instance->nodeLC($runeNode);
5134 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
5135 my ( $headLine, $headColumn ) = ( $parentLine, $parentColumn );
5136  
5137 my ( $gap, $body ) = @{ $policy->gapSeq0($node) };
5138 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
5139  
5140 my $anchorLine = $runeLine;
5141 my ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
5142 $runeNode,
5143 {
5144 fordFascen => 1,
5145 }
5146 );
5147 my $anchorDetails = $policy->anchorDetails( $node, $anchorData );
5148  
5149 my ( $fascomBodyColumn, $fascomBodyColumnDetails ) =
5150 @{ $policy->fascomBodyColumn( $node, { fordFascom => 1 } ) };
5151  
5152 my @mistakes = ();
5153 my $tag = 'fascom-element';
5154  
5155 my $baseColumn = $anchorColumn + ($isJoined ? 4 : 2) + ($queenside ? 2 : 0);
5156  
5157 if ( $headLine == $bodyLine ) {
5158 my $gapLength = $gap->{length};
5159  
5160 if ( $gapLength != 2 and $bodyColumn != $fascomBodyColumn ) {
5161 my $msg = sprintf 'Fascom element body %s; %s',
5162 describeLC( $bodyLine, $bodyColumn ),
5163 describeMisindent2( $bodyColumn, $fascomBodyColumn );
5164 push @mistakes,
5165 {
5166 desc => $msg,
5167 subpolicy => [ $tag, 'body-hgap' ],
5168 details => [ [ @{$anchorDetails} ] ],
5169 parentLine => $parentLine,
5170 parentColumn => $parentColumn,
5171 line => $bodyLine,
5172 column => $bodyColumn,
5173 reportLine => $bodyLine,
5174 reportColumn => $bodyColumn,
5175 topicLines => [$runeLine],
5176 };
5177 }
5178 return \@mistakes;
5179 }
5180  
5181 # If here head line != body line
5182 my $pseudojoinColumn = $policy->pseudojoinColumn($gap);
5183 if ( $pseudojoinColumn >= 0 ) {
5184 my $expectedBodyColumn = $pseudojoinColumn;
5185 if ( $bodyColumn != $expectedBodyColumn ) {
5186 my $msg =
5187 sprintf
5188 'Pseudo-joined Fascom element %s; body/comment mismatch; body is %s',
5189 describeLC( $parentLine, $parentColumn ),
5190 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5191 push @mistakes,
5192 {
5193 desc => $msg,
5194 subpolicy => [ $tag, 'pseudo-comment-indent' ],
5195 details => [ [ @{$anchorDetails} ] ],
5196 parentLine => $parentLine,
5197 parentColumn => $parentColumn,
5198 line => $bodyLine,
5199 column => $bodyColumn,
5200 reportLine => $bodyLine,
5201 reportColumn => $bodyColumn,
5202 topicLines => [$runeLine],
5203 };
5204 }
5205  
5206 # Treat the fascom body alignment as the "expected one"
5207 my $expectedColumn = $fascomBodyColumn;
5208 if ( $bodyColumn != $expectedColumn ) {
5209 my $msg = sprintf 'Pseudo-joined Fascom element %s; body %s',
5210 describeLC( $parentLine, $parentColumn ),
5211 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5212 push @mistakes,
5213 {
5214 desc => $msg,
5215 subpolicy => [ $tag, 'pseudo-hgap' ],
5216 details => [ [ @{$anchorDetails} ] ],
5217 parentLine => $parentLine,
5218 parentColumn => $parentColumn,
5219 line => $bodyLine,
5220 column => $bodyColumn,
5221 reportLine => $bodyLine,
5222 reportColumn => $bodyColumn,
5223 topicLines => [$runeLine],
5224 };
5225 }
5226 return \@mistakes;
5227 }
5228  
5229 # If here, this is (or should be) a split jog
5230 my $expectedBodyColumn = $headColumn + ($queenside ? -2 : 2);
5231  
5232 if ( $bodyColumn != $expectedBodyColumn ) {
5233 my $msg = sprintf 'Fascom element body %s; %s',
5234 describeLC( $bodyLine, $bodyColumn ),
5235 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5236 push @mistakes,
5237 {
5238 desc => $msg,
5239 subpolicy => [ $tag, 'body-indent' ],
5240 details => [ [ @{$anchorDetails} ] ],
5241 parentLine => $parentLine,
5242 parentColumn => $parentColumn,
5243 line => $bodyLine,
5244 column => $bodyColumn,
5245 reportLine => $bodyLine,
5246 reportColumn => $bodyColumn,
5247 topicLines => [$runeLine],
5248 };
5249 return \@mistakes;
5250 }
5251  
5252 push @mistakes,
5253 @{
5254 $policy->checkOneLineGap(
5255 $gap,
5256 {
5257 mainColumn => $expectedBodyColumn,
5258 runeName => $tag,
5259 subpolicy => [ $tag ],
5260 details => [ [ @{$anchorDetails} ] ],
5261 topicLines => [$runeLine],
5262 }
5263 )
5264 };
5265  
5266 return \@mistakes;
5267 }
5268  
5269 sub checkFastis {
5270 my ( $policy, $node ) = @_;
5271 my $instance = $policy->{lint};
5272  
5273 # fordFastis ::= (- FASTISGAP -) SYM4K (- GAP -) horn
5274 my ( $headGap, $symbol, $hornGap, $horn ) = @{ $policy->gapSeq0($node) };
5275 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
5276 my ( $symbolLine, $symbolColumn ) = $instance->nodeLC($symbol);
5277 my ( $hornLine, $hornColumn ) = $instance->nodeLC($horn);
5278  
5279 my @mistakes = ();
5280 my $runeName = 'fastis';
5281 my $tag = 'fastis';
5282  
5283 CHECK_SYMBOL: {
5284 if ( $symbolLine != $parentLine ) {
5285 my $msg = sprintf 'fastis symbol %s; symbol must be on rune line',
5286 describeLC( $symbolLine, $symbolColumn );
5287 push @mistakes,
5288 {
5289 desc => $msg,
5290 subpolicy => [ $runeName, 'same-line' ],
5291 parentLine => $parentLine,
5292 parentColumn => $parentColumn,
5293 line => $symbolLine,
5294 column => $symbolColumn,
5295 reportLine => $symbolLine,
5296 reportColumn => $symbolColumn,
5297 };
5298 last CHECK_SYMBOL;
5299 }
5300  
5301 my $expectedSymbolColumn = $parentColumn + 4;
5302 if ( $symbolColumn != $expectedSymbolColumn ) {
5303 my $msg = sprintf 'fastis symbol %s; %s',
5304 describeLC( $symbolLine, $symbolColumn ),
5305 describeMisindent2( $symbolColumn, $expectedSymbolColumn );
5306 push @mistakes,
5307 {
5308 desc => $msg,
5309 subpolicy => [ $runeName, 'head-hgap' ],
5310 parentLine => $parentLine,
5311 parentColumn => $parentColumn,
5312 line => $symbolLine,
5313 column => $symbolColumn,
5314 reportLine => $symbolLine,
5315 reportColumn => $symbolColumn,
5316 };
5317 }
5318 }
5319  
5320 CHECK_HORN: {
5321 if ( $hornLine == $symbolLine ) {
5322 my $symbolLength = $symbol->{length};
5323 my $expectedHornColumn = $symbolColumn + $symbolLength + 2;
5324 if ( $hornColumn != $expectedHornColumn ) {
5325 my $msg = sprintf 'fastis horn %s; %s',
5326 describeLC( $hornLine, $hornColumn ),
5327 describeMisindent2( $hornColumn, $expectedHornColumn );
5328 push @mistakes,
5329 {
5330 desc => $msg,
5331 subpolicy => [ $runeName, 'body-hgap' ],
5332 parentLine => $parentLine,
5333 parentColumn => $parentColumn,
5334 line => $hornLine,
5335 column => $hornColumn,
5336 reportLine => $hornLine,
5337 reportColumn => $hornColumn,
5338 };
5339 }
5340 last CHECK_HORN;
5341 }
5342  
5343 # if here, horn Line != symbol line
5344 my $expectedHornColumn = $parentColumn + 2;
5345  
5346 push @mistakes,
5347 @{
5348 $policy->checkOneLineGap(
5349 $hornGap,
5350 {
5351 mainColumn => $expectedHornColumn,
5352 runeName => $tag,
5353 subpolicy => [ $runeName ],
5354 }
5355 )
5356 };
5357  
5358 if ( $hornColumn != $expectedHornColumn ) {
5359 my $msg = sprintf 'fastis split horn %s; %s',
5360 describeLC( $hornLine, $hornColumn ),
5361 describeMisindent2( $hornColumn, $expectedHornColumn );
5362 push @mistakes,
5363 {
5364 desc => $msg,
5365 subpolicy => [ $runeName, 'body-indent' ],
5366 parentLine => $parentLine,
5367 parentColumn => $parentColumn,
5368 line => $hornLine,
5369 column => $hornColumn,
5370 reportLine => $hornLine,
5371 reportColumn => $hornColumn,
5372 };
5373 }
5374  
5375 }
5376  
5377 return \@mistakes;
5378 }
5379  
5380 sub checkKingsideJog {
5381 my ( $policy, $node, $expectedHeadColumn ) = @_;
5382 my $instance = $policy->{lint};
5383 my $fileName = $instance->{fileName};
5384 my $grammar = $instance->{grammar};
5385 my $ruleID = $node->{ruleID};
5386 my ( $parentLine, $parentColumn ) =
5387 $instance->line_column( $node->{start} );
5388  
5389 my $joggingHoonData = $policy->getInheritedAttribute($node, 'joggingHoonData');
5390 my $jogBodyColumn = $joggingHoonData->{bodyAlignment};
5391 $jogBodyColumn //= -1;
5392 my $jogBodyColumnLines = $joggingHoonData->{alignments};
5393 my $runeName = $joggingHoonData->{runeName};
5394 my $brickNode = $joggingHoonData->{node};
5395 my ( $brickLine, $brickColumn ) = $instance->nodeLC($brickNode);
5396 my $baseColumn = $joggingHoonData->{jogBaseColumn};
5397  
5398 my @mistakes = ();
5399  
5400 my $children = $node->{children};
5401 my $head = $children->[0];
5402 my $gap = $children->[1];
5403 my $body = $children->[2];
5404 my ( $headLine, $headColumn ) =
5405 $instance->line_column( $head->{start} );
5406 my ( $bodyLine, $bodyColumn ) =
5407 $instance->line_column( $body->{start} );
5408 my $sideDesc = 'kingside';
5409  
5410 if ( $headColumn != $expectedHeadColumn ) {
5411 my $msg = sprintf 'Jog %s head %s; %s',
5412 $sideDesc,
5413 describeLC( $headLine, $headColumn ),
5414 describeMisindent2( $headColumn, $expectedHeadColumn );
5415 push @mistakes,
5416 {
5417 desc => $msg,
5418 subpolicy => [ $runeName, 'jog-head-indent' ],
5419 parentLine => $parentLine,
5420 parentColumn => $parentColumn,
5421 line => $headLine,
5422 column => $headColumn,
5423 reportLine => $headLine,
5424 reportColumn => $headColumn,
5425 topicLines => [$brickLine],
5426 };
5427 }
5428  
5429 if ( $headLine == $bodyLine ) {
5430 my $gapLength = $gap->{length};
5431  
5432 if ( $gapLength != 2 and $bodyColumn != $jogBodyColumn ) {
5433 my $misindent;
5434 my $details;
5435 my @topicLines = ($brickLine);
5436 if ( $jogBodyColumn < 0 ) {
5437 $misindent = describeMisindent2( $gapLength, 2 );
5438 $details = [ [ "no inter-line alignment detected" ] ];
5439 }
5440 else {
5441 $misindent = describeMisindent2( $bodyColumn, $jogBodyColumn );
5442 my $oneBasedColumn = $jogBodyColumn + 1;
5443 push @topicLines, @{$jogBodyColumnLines};
5444 $details = [
5445 [
5446 sprintf 'inter-line alignment is %d, see %s',
5447 $oneBasedColumn,
5448 (
5449 join q{ },
5450 map { $_ . ':' . $oneBasedColumn }
5451 @{$jogBodyColumnLines}
5452 )
5453 ]
5454 ];
5455 }
5456 my $msg = sprintf 'Jog %s body %s; %s',
5457 $sideDesc,
5458 describeLC( $bodyLine, $bodyColumn ),
5459 $misindent;
5460 push @mistakes,
5461 {
5462 desc => $msg,
5463 subpolicy => [ $runeName, 'jog-body-indent' ],
5464 parentLine => $parentLine,
5465 parentColumn => $parentColumn,
5466 line => $bodyLine,
5467 column => $bodyColumn,
5468 reportLine => $bodyLine,
5469 reportColumn => $bodyColumn,
5470 topicLines => \@topicLines,
5471 details => $details,
5472 };
5473 }
5474 return \@mistakes;
5475 }
5476  
5477 # If here head line != body line
5478 my $pseudojoinColumn = $policy->pseudojoinColumn($gap);
5479 if ( $pseudojoinColumn >= 0 ) {
5480 my $expectedBodyColumn = $pseudojoinColumn;
5481 if ( $bodyColumn != $expectedBodyColumn ) {
5482 my $msg =
5483 sprintf
5484 'Pseudo-joined %s Jog %s; body/comment mismatch; body is %s',
5485 $sideDesc,
5486 describeLC( $parentLine, $parentColumn ),
5487 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5488 push @mistakes,
5489 {
5490 desc => $msg,
5491 subpolicy => [ $runeName, 'jog-pseudojoin-mismatch' ],
5492 parentLine => $parentLine,
5493 parentColumn => $parentColumn,
5494 line => $bodyLine,
5495 column => $bodyColumn,
5496 reportLine => $bodyLine,
5497 reportColumn => $bodyColumn,
5498 topicLines => [$brickLine],
5499 };
5500 }
5501 my $headLength = $head->{length};
5502  
5503 # Treat the jogging body alignment as the "expected one"
5504 my $expectedColumn = $jogBodyColumn;
5505 my $raggedColumn = $headColumn + $headLength + 2;
5506 if ( $bodyColumn != $raggedColumn and $bodyColumn != $expectedColumn ) {
5507 my $msg = sprintf 'Pseudo-joined %s Jog %s; body %s',
5508 $sideDesc, describeLC( $parentLine, $parentColumn ),
5509 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5510 push @mistakes,
5511 {
5512 desc => $msg,
5513 subpolicy => [ $runeName, 'jog-pseudojoin-indent' ],
5514 parentLine => $parentLine,
5515 parentColumn => $parentColumn,
5516 line => $bodyLine,
5517 column => $bodyColumn,
5518 reportLine => $bodyLine,
5519 reportColumn => $bodyColumn,
5520 topicLines => [$brickLine],
5521 };
5522 }
5523 return \@mistakes;
5524 }
5525  
5526 # If here, this is (or should be) a split jog
5527 my $expectedBodyColumn = $baseColumn + 2;
5528  
5529 if ( $bodyColumn != $expectedBodyColumn ) {
5530 my $msg = sprintf 'Jog %s body %s; %s',
5531 $sideDesc,
5532 describeLC( $bodyLine, $bodyColumn ),
5533 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5534 push @mistakes,
5535 {
5536 desc => $msg,
5537 subpolicy => [ $runeName, 'jog-body-indent' ],
5538 parentLine => $parentLine,
5539 parentColumn => $parentColumn,
5540 line => $bodyLine,
5541 column => $bodyColumn,
5542 reportLine => $bodyLine,
5543 reportColumn => $bodyColumn,
5544 topicLines => [$brickLine],
5545 details => [
5546 [
5547 sprintf qq{lexeme "%s" %s},
5548 $instance->lexeme( $brickLine, $brickColumn ),
5549 describeLC( $brickLine, $brickColumn )
5550 ]
5551 ],
5552 };
5553 return \@mistakes;
5554 }
5555  
5556 push @mistakes,
5557 @{
5558 $policy->checkOneLineGap(
5559 $gap,
5560 {
5561 mainColumn => $expectedBodyColumn,
5562 runeName => $runeName,
5563 subpolicy => [ $runeName ],
5564 topicLines => [ $bodyLine, $brickLine ],
5565 }
5566 )
5567 };
5568  
5569 return \@mistakes;
5570 }
5571  
5572 sub checkQueensideJog {
5573 my ( $policy, $node, $expectedHeadColumn ) = @_;
5574 my $instance = $policy->{lint};
5575 my ( $parentLine, $parentColumn ) =
5576 $instance->line_column( $node->{start} );
5577 my $ruleID = $node->{ruleID};
5578 my $fileName = $instance->{fileName};
5579 my $grammar = $instance->{grammar};
5580  
5581 my @mistakes = ();
5582  
5583 my $joggingRules = $instance->{joggingRule};
5584 my $joggingHoonData = $policy->getInheritedAttribute($node, 'joggingHoonData');
5585 my $jogBodyColumn = $joggingHoonData->{bodyAlignment};
5586 $jogBodyColumn //= -1;
5587 my $jogBodyColumnLines = $joggingHoonData->{alignments};
5588 my $runeName = $joggingHoonData->{runeName};
5589 my $brickNode = $joggingHoonData->{node};
5590 my ( $brickLine, $brickColumn ) = $instance->nodeLC($brickNode);
5591 my $baseColumn = $joggingHoonData->{jogBaseColumn};
5592  
5593 my $children = $node->{children};
5594 my $head = $children->[0];
5595 my $gap = $children->[1];
5596 my $body = $children->[2];
5597 my ( $headLine, $headColumn ) =
5598 $instance->line_column( $head->{start} );
5599 my ( $bodyLine, $bodyColumn ) =
5600 $instance->line_column( $body->{start} );
5601 my $sideDesc = 'queenside';
5602  
5603 if ( $headColumn != $expectedHeadColumn ) {
5604 my $msg = sprintf 'Jog %s head %s; %s',
5605 $sideDesc,
5606 describeLC( $headLine, $headColumn ),
5607 describeMisindent2( $headColumn, $expectedHeadColumn );
5608 push @mistakes,
5609 {
5610 desc => $msg,
5611 subpolicy => [ $runeName, 'jog-head-indent' ],
5612 parentLine => $parentLine,
5613 parentColumn => $parentColumn,
5614 line => $headLine,
5615 column => $headColumn,
5616 reportLine => $headLine,
5617 reportColumn => $headColumn,
5618 topicLines => [$brickLine],
5619 };
5620 }
5621  
5622 # Check for flat queenside misalignments
5623 my $expectedBodyColumn = $jogBodyColumn;
5624 if ( $headLine == $bodyLine ) {
5625 my $gapLength = $gap->{length};
5626 if ( $gapLength != 2 and $bodyColumn != $jogBodyColumn ) {
5627 my $misindent;
5628 my $details;
5629 my @topicLines = ($brickLine);
5630 if ($jogBodyColumn < 0) {
5631 $misindent = describeMisindent2( $gapLength, 2 );
5632 $details = [
5633 [
5634 "no inter-line alignment detected"
5635 ]
5636 ];
5637 } else {
5638 $misindent = describeMisindent2( $bodyColumn, $jogBodyColumn );
5639 my $oneBasedColumn = $jogBodyColumn+1;
5640 push @topicLines, @{$jogBodyColumnLines};
5641 $details = [
5642 [
5643 sprintf 'inter-line alignment is %d, see %s', $oneBasedColumn,
5644 (join q{ }, map { $_ . ':' . $oneBasedColumn } @{$jogBodyColumnLines})
5645 ]
5646 ];
5647 }
5648 my $msg = sprintf 'Jog %s body %s; %s',
5649 $sideDesc,
5650 describeLC( $bodyLine, $bodyColumn ),
5651 $misindent;
5652 push @mistakes,
5653 {
5654 desc => $msg,
5655 subpolicy => [ $runeName, 'jog-body-indent' ],
5656 parentLine => $parentLine,
5657 parentColumn => $parentColumn,
5658 line => $bodyLine,
5659 column => $bodyColumn,
5660 reportLine => $bodyLine,
5661 reportColumn => $bodyColumn,
5662 topicLines => \@topicLines,
5663 details => $details,
5664 };
5665 }
5666 return \@mistakes;
5667 }
5668  
5669 # If here, this is a split jog
5670 $expectedBodyColumn = $brickColumn + 2;
5671 if ( $bodyColumn != $expectedBodyColumn ) {
5672  
5673 my $msg = sprintf 'Jog %s body %s; %s',
5674 $sideDesc,
5675 describeLC( $bodyLine, $bodyColumn ),
5676 describeMisindent2( $bodyColumn, $expectedBodyColumn );
5677 push @mistakes,
5678 {
5679 desc => $msg,
5680 subpolicy => [ $runeName, 'jog-body-indent' ],
5681 parentLine => $parentLine,
5682 parentColumn => $parentColumn,
5683 line => $bodyLine,
5684 column => $bodyColumn,
5685 reportLine => $bodyLine,
5686 reportColumn => $bodyColumn,
5687 topicLines => [$brickLine],
5688 details => [
5689 [
5690 sprintf qq{lexeme "%s" %s},
5691 $instance->lexeme( $brickLine, $brickColumn ),
5692 describeLC( $brickLine, $brickColumn )
5693 ]
5694 ],
5695 };
5696 return \@mistakes;
5697 }
5698  
5699 push @mistakes,
5700 @{
5701 $policy->checkOneLineGap(
5702 $gap,
5703 {
5704 mainColumn => $expectedBodyColumn,
5705 runeName => $runeName,
5706 subpolicy => [ $runeName ],
5707 topicLines => [ $bodyLine, $brickLine ],
5708 }
5709 )
5710 };
5711  
5712 return \@mistakes;
5713 }
5714  
5715 # TODO: Add a check (optional?) for queenside joggings with no
5716 # split jogs.
5717 sub checkJog {
5718 my ( $policy, $node ) = @_;
5719 my $instance = $policy->{lint};
5720  
5721 my $joggingHoonData = $policy->getInheritedAttribute($node, 'joggingHoonData');
5722 my $jogBaseColumn = $joggingHoonData->{jogBaseColumn};
5723  
5724 # This is to suppress detailed complaints about the first jog
5725 # of a "joined" jogging. A complaint will be made at the jogging
5726 # hoon level, and the detailed ones for the jog are redundant
5727 # and confusing.
5728 my $firstOKJogLine = $joggingHoonData->{firstOKJogLine};
5729 my ($jogLine) = $instance->nodeLC($node);
5730 return [] if $jogLine < $firstOKJogLine;
5731  
5732 my $chessSide = $joggingHoonData->{chessSide};
5733 return $policy->checkQueensideJog($node, $jogBaseColumn)
5734 if $chessSide eq 'queenside';
5735 return $policy->checkKingsideJog($node, $jogBaseColumn);
5736 }
5737  
5738 # not yet implemented
5739 sub checkNYI {
5740 my ( $policy, $node ) = @_;
5741 my $instance = $policy->{lint};
5742 my $grammar = $instance->{grammar};
5743 my ( $parentLine, $parentColumn ) =
5744 $instance->line_column( $node->{start} );
5745 my $ruleID = $node->{ruleID};
5746 my @mistakes = ();
5747  
5748 my $msg = join q{ }, 'NYI', '[' . $instance->symbol($node) . ']',
5749 $instance->describeNodeRange($node),
5750 ( map { $grammar->symbol_display_form($_) }
5751 $grammar->rule_expand($ruleID) );
5752 push @mistakes,
5753 {
5754 desc => $msg,
5755 parentLine => $parentLine,
5756 parentColumn => $parentColumn,
5757 line => $parentLine,
5758 column => $parentColumn,
5759 };
5760 return \@mistakes;
5761 }
5762  
5763 sub checkBackdented {
5764 my ( $policy, $node ) = @_;
5765 my $nodeIX = $node->{IX};
5766 my @gapSeq = @{ $policy->gapSeq0($node) };
5767 my $elementCount = ( scalar @gapSeq ) / 2;
5768 my $instance = $policy->{lint};
5769 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
5770 my @mistakes = ();
5771 my $runeName = $policy->runeName($node);
5772  
5773 my $chainOffset = 0;
5774 my $chainAlignments;
5775 my $chainAlignmentData = $policy->chainAlignmentData($node);
5776 if ($chainAlignmentData) {
5777 $chainOffset = $chainAlignmentData->{offset};
5778 $chainAlignments = $chainAlignmentData->{alignments};
5779 }
5780  
5781 my $runningAlignments =
5782 $policy->{perNode}->{$nodeIX}->{runningAlignments};
5783 # say STDERR join " ", __FILE__, __LINE__, "nodeIX:", $nodeIX;
5784  
5785 my $reanchorOffset; # for re-anchoring logic
5786  
5787 ENFORCE_ELEMENT1_JOINEDNESS: {
5788  
5789 # TODO: Is this right?
5790 my $firstGap = $gapSeq[0];
5791 my ($gapLine) = $instance->nodeLC($firstGap);
5792 last ENFORCE_ELEMENT1_JOINEDNESS if $gapLine == $parentLine;
5793 my $gapLiteral = $instance->literalNode($firstGap);
5794 $gapLiteral = substr( $gapLiteral, 2 )
5795 if $instance->runeGapNode($firstGap);
5796  
5797 # Only enforce if 1st line is spaces --
5798 # comments, etc., are caught by the logic to follow
5799 last ENFORCE_ELEMENT1_JOINEDNESS unless $gapLiteral =~ /^[ ]*\n/;
5800 my $element = $gapSeq[1];
5801 my ( $elementLine, $elementColumn ) = $instance->nodeLC($element);
5802 my $msg = sprintf
5803 '%d-element backdent must be joined %s',
5804 $elementCount,
5805 describeLC( $elementLine, $elementColumn );
5806 push @mistakes,
5807 {
5808 desc => $msg,
5809 subpolicy => [ $runeName, 'element-1-split' ],
5810 parentLine => $parentLine,
5811 parentColumn => $parentColumn,
5812 line => $elementLine,
5813 column => $elementColumn,
5814 reportLine => $elementLine,
5815 reportColumn => $elementColumn,
5816 };
5817 }
5818  
5819 # my $anchorNode = $instance->anchorNode($node);
5820 # my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchorNode);
5821 my ( $anchorLine, $anchorColumn ) = ( $parentLine, $parentColumn );
5822 my $anchorData;
5823 if ( my $sources = $reanchorings{$runeName} ) {
5824 ( $anchorColumn, $anchorData ) =
5825 $policy->reanchorInc( $node, $sources );
5826 }
5827  
5828 my $anchorDetails = [];
5829 $anchorDetails = $policy->anchorDetails( $node, $anchorData )
5830 if $anchorData;
5831  
5832 my ( $elementLine, $elementColumn );
5833 ELEMENT:
5834 for (
5835 my $elementNumber = 1 ;
5836 $elementNumber <= $elementCount ;
5837 $elementNumber++
5838 )
5839 {
5840  
5841 my $element = $gapSeq[ $elementNumber * 2 - 1 ];
5842 my $previousElementLine = $elementLine // -1;
5843 ( $elementLine, $elementColumn ) = $instance->nodeLC($element);
5844 my $gap = $gapSeq[ $elementNumber * 2 - 2 ];
5845 my ( $gapLine, $gapColumn ) = $instance->nodeLC($gap);
5846 my $backdentColumn =
5847 $anchorColumn + ( $elementCount - $elementNumber ) * 2;
5848  
5849 if ( $elementLine == $parentLine ) {
5850  
5851 # OK if at proper alignment for backdent
5852 next ELEMENT if $backdentColumn == $elementColumn;
5853  
5854 my $gapLength = $instance->gapLength($gap);
5855 next ELEMENT if $gapLength == 2;
5856  
5857 my $thisAlignment;
5858 my ( $chainAlignmentColumn, $chainAlignmentDetails );
5859 my $chainAlignmentLines = [];
5860 if ($chainAlignments) {
5861 $thisAlignment =
5862 $chainAlignments->[ $chainOffset + $elementNumber - 1 ];
5863 # say STDERR "$thisAlignment = chainAlignments->[ $chainOffset + $elementNumber - 1 ]";
5864 ( $chainAlignmentColumn, $chainAlignmentDetails ) =
5865 @{$thisAlignment};
5866  
5867 }
5868  
5869 # say STDERR join " ", __FILE__, __LINE__, $elementNumber;
5870 my ( $runningAlignmentColumn, $runningAlignmentDetails );
5871 if ($runningAlignments) {
5872 $thisAlignment = $runningAlignments->[ $elementNumber - 1];
5873 ( $runningAlignmentColumn, $runningAlignmentDetails ) =
5874 @{$thisAlignment};
5875 }
5876  
5877 if ( defined $chainAlignmentColumn
5878 and $chainAlignmentColumn >= 0
5879 and defined $runningAlignmentColumn
5880 and $runningAlignmentColumn >= 0
5881 and $chainAlignmentColumn != $runningAlignmentColumn )
5882 {
5883 my $msg = sprintf
5884 'inter-line alignment conflict; element #%d of %s at %s; running is %d but chain is %d',
5885 $elementNumber,
5886 describeLC( $parentLine, $parentColumn ),
5887 describeLC( $elementLine, $elementColumn ),
5888 describeLC( $elementLine, $runningAlignmentColumn ),
5889 describeLC( $elementLine, $chainAlignmentColumn );
5890 push @mistakes,
5891 {
5892 desc => $msg,
5893 subpolicy => [ $runeName, 'interline-mismatch' ],
5894 parentLine => $parentLine,
5895 parentColumn => $parentColumn,
5896 line => $elementLine,
5897 column => $elementColumn,
5898 reportLine => $elementLine,
5899 reportColumn => $elementColumn,
5900 };
5901 }
5902  
5903 my $interlineAlignmentType;
5904 my $interlineAlignmentColumn;
5905 SET_INTERLINE_ALIGNMENT: {
5906 if ( defined $chainAlignmentColumn
5907 and $chainAlignmentColumn >= 0 )
5908 {
5909 $interlineAlignmentType = 'chain';
5910 $interlineAlignmentColumn = $chainAlignmentColumn;
5911 last SET_INTERLINE_ALIGNMENT;
5912 }
5913 if ( defined $runningAlignmentColumn)
5914 {
5915 $interlineAlignmentType = 'running';
5916 $interlineAlignmentColumn = $runningAlignmentColumn;
5917 }
5918 }
5919  
5920 # say STDERR join " ", __FILE__, __LINE__, $interlineAlignmentColumn;
5921 next ELEMENT
5922 if defined $interlineAlignmentColumn
5923 and $interlineAlignmentColumn >= 0
5924 and $interlineAlignmentColumn == $elementColumn;
5925  
5926 my @topicLines = ();
5927 my $tightColumn = $gapColumn + 2;
5928 my @allowedColumns =([ $tightColumn => 'tight' ]);
5929 if ($backdentColumn > $tightColumn) {
5930 push @allowedColumns, [ $backdentColumn => 'backdent' ];
5931 }
5932 my $details;
5933 if ($interlineAlignmentColumn and $interlineAlignmentColumn >= $tightColumn) {
5934 push @allowedColumns, [ $interlineAlignmentColumn => $interlineAlignmentType ];
5935 my $oneBasedColumn = $interlineAlignmentColumn + 1;
5936 my $alignmentLines =
5937 $interlineAlignmentType eq 'running'
5938 ? $runningAlignmentDetails
5939 : $chainAlignmentDetails->{lines};
5940 push @topicLines, @{$alignmentLines};
5941 $details = [
5942 [
5943 sprintf '%s alignment is %d, see %s',
5944 $interlineAlignmentType,
5945 $oneBasedColumn,
5946 (
5947 join q{ },
5948 map { $_ . ':' . $oneBasedColumn } @{$alignmentLines}
5949 )
5950 ]
5951 ];
5952 }
5953 else {
5954 $details = [ [ "no chain alignment detected" ] ];
5955 }
5956 my @sortedColumns = sort { $a->[0] <=> $b->[0] } @allowedColumns;
5957 my $allowedDesc = join "; ",
5958 map { sprintf '@%d:%d (%s)', $elementLine, $_->[0]+1, $_->[1] } @sortedColumns;
5959 if (scalar @sortedColumns >= 2) {
5960 $allowedDesc = 'one of ' . $allowedDesc;
5961 }
5962  
5963 my $msg = sprintf
5964 'joined backdent element #%d of %s is at %s; should be %s',
5965 $elementNumber,
5966 describeLC( $parentLine, $parentColumn ),
5967 describeLC( $elementLine, $elementColumn ),
5968 $allowedDesc;
5969 push @mistakes,
5970 {
5971 desc => $msg,
5972 subpolicy => [ $runeName, 'hgap' ],
5973 parentLine => $parentLine,
5974 parentColumn => $parentColumn,
5975 line => $elementLine,
5976 column => $elementColumn,
5977 reportLine => $elementLine,
5978 reportColumn => $elementColumn,
5979 subpolicy => [ $runeName, 'hgap'],
5980 topicLines => \@topicLines,
5981 details => $details,
5982 };
5983 next ELEMENT;
5984 }
5985  
5986 CHECK_FOR_PSEUDOJOIN: {
5987 last CHECK_FOR_PSEUDOJOIN if $gapLine != $parentLine;
5988 last CHECK_FOR_PSEUDOJOIN if $backdentColumn == $elementColumn;
5989 my $msg =
5990 sprintf
5991 'Pseudo-joined backdented element %d; element/comment mismatch',
5992 $elementNumber;
5993 my $pseudojoinMistakes = $policy->checkPseudojoin(
5994 $gap,
5995 {
5996 desc => $msg,
5997 subpolicy => [ $runeName, 'pseudojoin-mismatch' ],
5998 expected => [
5999 [ 'tight', $parentColumn + 4 ],
6000 ]
6001 }
6002 );
6003 if ($pseudojoinMistakes) {
6004 push @mistakes, @{$pseudojoinMistakes};
6005 next ELEMENT;
6006 }
6007 }
6008  
6009 # For the use of re-anchoring logic, determine the additional offset
6010 # reguired for the next line after the rune line
6011 if ( not defined $reanchorOffset ) {
6012 $reanchorOffset = 2 + ( $elementCount - $elementNumber ) * 2;
6013 $policy->{perNode}->{$nodeIX}->{reanchorOffset} = $reanchorOffset;
6014 }
6015  
6016 if ( $elementLine == $previousElementLine ) {
6017 my $gapLength = $gap->{length};
6018 next ELEMENT if $gapLength == 2;
6019 my $msg = sprintf
6020 'backdented element #%d %s; %s',
6021 $elementNumber,
6022 describeLC( $elementLine, $elementColumn ),
6023 describeMisindent2( $gapLength, 2 );
6024 push @mistakes,
6025 {
6026 desc => $msg,
6027 subpolicy => [ $runeName, 'hgap' ],
6028 parentLine => $parentLine,
6029 parentColumn => $parentColumn,
6030 line => $elementLine,
6031 column => $elementColumn,
6032 reportLine => $elementLine,
6033 reportColumn => $elementColumn,
6034 details => [ [ @{$anchorDetails}, ] ],
6035 };
6036 next ELEMENT;
6037 }
6038  
6039 push @mistakes, @{
6040 $policy->checkOneLineGap(
6041 $gap,
6042 {
6043 mainColumn => $anchorColumn,
6044 elementNumber => $elementNumber,
6045 preColumn => $elementColumn,
6046 runeName => $runeName,
6047 subpolicy => [ $runeName ],
6048 topicLines => [ $anchorLine, $elementLine ],
6049 details => [
6050 [
6051 @{$anchorDetails},
6052 (sprintf 'inter-comment indent should be %d; see line %d',
6053 ( $anchorColumn + 1 ), $anchorLine),
6054 (sprintf 'pre-comment indent should be %d; see line %d',
6055 ( $elementColumn + 1 ), $elementLine)
6056 ]
6057 ],
6058 }
6059 )
6060 };
6061  
6062 if ( $backdentColumn != $elementColumn ) {
6063 my $msg = sprintf
6064 'backdented element #%d %s; %s',
6065 $elementNumber,
6066 describeLC( $elementLine, $elementColumn ),
6067 describeMisindent2( $elementColumn, $backdentColumn );
6068 push @mistakes,
6069 {
6070 desc => $msg,
6071 subpolicy => [ $runeName, 'indent' ],
6072 line => $elementLine,
6073 column => $elementColumn,
6074 reportLine => $elementLine,
6075 reportColumn => $elementColumn,
6076 topicLines => [ $anchorLine ],
6077 details => [ [ @{$anchorDetails}, ] ],
6078 };
6079 }
6080 }
6081 return \@mistakes;
6082 }
6083  
6084 # Ketdot is slightly different form other backdented hoons
6085 sub checkKetdot {
6086 my ( $policy, $node ) = @_;
6087 my @gapSeq = @{ $policy->gapSeq0($node) };
6088 my $instance = $policy->{lint};
6089 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
6090 my @mistakes = ();
6091 my $runeName = 'ketdot';
6092 my $tag = 'ketdot';
6093  
6094 my $anchorNode =
6095 $instance->firstBrickOfLineInc( $node, { tallKetdot => 1 } );
6096 my ( $anchorLine, $anchorColumn ) = $instance->nodeLC($anchorNode);
6097  
6098 my $gap1 = $gapSeq[0];
6099 my $element1 = $gapSeq[1];
6100 my ( $element1Line, $element1Column ) = $instance->nodeLC($element1);
6101  
6102 ELEMENT: { # Element 1
6103  
6104 my $expectedColumn = $parentColumn + 4;
6105  
6106 if ( $element1Line != $parentLine ) {
6107 my $msg = sprintf
6108 "Ketdot element 1 %s; element 1 expected to be on rune line",
6109 describeLC( $element1Line, $element1Column );
6110 push @mistakes,
6111 {
6112 desc => $msg,
6113 parentLine => $parentLine,
6114 parentColumn => $parentColumn,
6115 line => $element1Line,
6116 column => $element1Column,
6117 };
6118 last ELEMENT;
6119 }
6120  
6121 if ( $expectedColumn != $element1Column ) {
6122 my $msg = sprintf
6123 'Ketdot element 1 %s; %s',
6124 describeLC( $element1Line, $element1Column ),
6125 describeMisindent2( $element1Column, $expectedColumn );
6126 push @mistakes,
6127 {
6128 desc => $msg,
6129 parentLine => $parentLine,
6130 parentColumn => $parentColumn,
6131 line => $element1Line,
6132 column => $element1Column,
6133 };
6134 }
6135 }
6136  
6137 my $gap2 = $gapSeq[2];
6138 my $element2 = $gapSeq[3];
6139 my ( $element2Line, $element2Column ) = $instance->nodeLC($element2);
6140  
6141 ELEMENT2: {
6142 if ( $element1Line != $element2Line ) { # Element 2 split
6143  
6144 my $expectedColumn = $anchorColumn;
6145  
6146 push @mistakes,
6147 @{
6148 $policy->checkOneLineGap(
6149 $gap2,
6150 {
6151 mainColumn => $anchorColumn,
6152 runeName => $runeName,
6153 subpolicy => [ $runeName ],
6154 }
6155 )
6156 };
6157  
6158 if ( $expectedColumn != $element2Column ) {
6159 my $msg = sprintf
6160 'Ketdot element 2 %s; %s',
6161 describeLC( $element2Line, $element2Column ),
6162 describeMisindent2( $element2Column, $expectedColumn );
6163 push @mistakes,
6164 {
6165 desc => $msg,
6166 parentLine => $parentLine,
6167 parentColumn => $parentColumn,
6168 line => $element2Line,
6169 column => $element2Column,
6170 };
6171 }
6172 last ELEMENT2;
6173 }
6174  
6175 # If here, joined element 2
6176  
6177 my $gapLiteral = $instance->literalNode($gap2);
6178 my $gapLength = $gap2->{length};
6179 last ELEMENT2 if $gapLength == 2;
6180 my ( undef, $gap2Column ) = $instance->nodeLC($gap2);
6181  
6182 # expected length is the length if the spaces at the end
6183 # of the gap-equivalent were exactly one stop.
6184 my $expectedLength = $gapLength + ( 2 - length $gapLiteral );
6185 my $expectedColumn = $gap2Column + $expectedLength;
6186  
6187 if ( $expectedColumn != $element2Column ) {
6188 my $msg = sprintf
6189 'Ketdot element 2 %s; %s',
6190 describeLC( $element2Line, $element2Column ),
6191 describeMisindent2( $element2Column, $expectedColumn );
6192 push @mistakes,
6193 {
6194 desc => $msg,
6195 parentLine => $parentLine,
6196 parentColumn => $parentColumn,
6197 line => $element2Line,
6198 column => $element2Column,
6199 };
6200 }
6201 }
6202  
6203 return \@mistakes;
6204 }
6205  
6206 sub checkLuslus {
6207 my ( $policy, $node, $cellLHS ) = @_;
6208 my $nodeIX = $node->{IX};
6209 my $instance = $policy->{lint};
6210 my $runeName = $policy->runeName($node);
6211 my ( $parentLine, $parentColumn ) = $instance->nodeLC($node);
6212  
6213 my $battery = $instance->ancestorByLHS( $node, { whap5d => 1 } );
6214 die "battery not found" if not defined $battery;
6215 my ( $batteryLine, $batteryColumn ) = $instance->nodeLC($battery);
6216 my ( $cellBodyColumn, $cellBodyColumnLines ) =
6217 @{ $policy->getInheritedAttribute($node, 'cellBodyAlignmentData') };
6218  
6219 my $batteryHoon = $instance->brickNode($battery);
6220 my ( $batteryHoonLine, $batteryHoonColumn ) =
6221 $instance->nodeLC($batteryHoon);
6222 my $batteryRuneName = $policy->runeName($batteryHoon);
6223 my $armDesc = join ':', $batteryRuneName, $runeName, 'arm';
6224  
6225 my $anchorData;
6226 my ( $anchorColumn, $anchorLine ) = ( $parentColumn, $parentLine );
6227 if ( $parentLine == $batteryHoonLine ) {
6228 ( $anchorColumn, $anchorData ) = $policy->reanchorInc(
6229 $node,
6230 {
6231 'tallBarcen' => 1,
6232 }
6233 );
6234 }
6235  
6236 my $anchorDetails;
6237 $anchorDetails = $policy->anchorDetails( $node, $anchorData )
6238 if $anchorData;
6239  
6240 $policy->{perNode}->{$nodeIX}->{reanchorOffset} = 2;
6241  
6242 my @mistakes = ();
6243  
6244 # LuslusCell ::= (- LUS LUS GAP -) SYM4K (- GAP -) tall5d
6245 my ( $headGap, $head, $bodyGap, $body ) = @{ $policy->gapSeq0($node) };
6246 my ( $headLine, $headColumn ) = $instance->nodeLC($head);
6247 my ( $bodyLine, $bodyColumn ) = $instance->nodeLC($body);
6248  
6249 my $headGapLength = $headGap->{length};
6250 if ( $headGapLength != 2 ) {
6251 my $msg = sprintf 'Cell head %s; %s',
6252 describeLC( $bodyLine, $bodyColumn ),
6253 describeMisindent2( $headGapLength, 2 );
6254 push @mistakes,
6255 {
6256 desc => $msg,
6257 subpolicy => [ $runeName, 'hgap' ],
6258 parentLine => $parentLine,
6259 parentColumn => $parentColumn,
6260 line => $headLine,
6261 column => $headColumn,
6262 reportLine => $headLine,
6263 reportColumn => $headColumn,
6264 topicLines => [ $batteryLine, $batteryHoonLine ],
6265 details => [
6266 [
6267 $armDesc,
6268 (
6269 sprintf 'starts at %s',
6270 describeLC( $batteryHoonLine, $batteryHoonColumn )
6271 ),
6272 ]
6273 ],
6274 };
6275 }
6276  
6277 if ( $headLine == $bodyLine ) {
6278 my $bodyGapLength = $bodyGap->{length};
6279  
6280 if ( $bodyGapLength != 2 and $bodyColumn != $cellBodyColumn ) {
6281 my @topicLines = ( $batteryLine, $batteryHoonLine );
6282 my @subpolicy = ($runeName);
6283 my $misindent;
6284 my @details = (
6285 $armDesc,
6286 (
6287 sprintf 'starts at %s',
6288 describeLC( $batteryHoonLine, $batteryHoonColumn )
6289 ),
6290 );
6291 if ( $cellBodyColumn < 0 ) {
6292 push @subpolicy, 'bad-tight-indent';
6293 $misindent = describeMisindent2( $bodyGapLength, 2 );
6294 push @details, "no inter-line alignment detected";
6295 }
6296 else {
6297 push @subpolicy, 'bad-interline-indent';
6298 $misindent = describeMisindent2( $bodyColumn, $cellBodyColumn );
6299 my $oneBasedColumn = $cellBodyColumn + 1;
6300 push @topicLines, @{$cellBodyColumnLines};
6301 push @details,
6302 (
6303 sprintf 'inter-line alignment is %d, see %s',
6304 $oneBasedColumn,
6305 (
6306 join q{ },
6307 map { $_ . ':' . $oneBasedColumn }
6308 @{$cellBodyColumnLines}
6309 )
6310 );
6311 }
6312 my $msg = sprintf 'Cell body %s; %s',
6313 describeLC( $bodyLine, $bodyColumn ), $misindent;
6314 push @mistakes,
6315 {
6316 desc => $msg,
6317 subpolicy => \@subpolicy,
6318 parentLine => $parentLine,
6319 parentColumn => $parentColumn,
6320 line => $bodyLine,
6321 column => $bodyColumn,
6322 reportLine => $bodyLine,
6323 reportColumn => $bodyColumn,
6324 topicLines => \@topicLines,
6325 details => [ [@details] ],
6326 };
6327 }
6328 return \@mistakes;
6329 }
6330  
6331 # If here, this is (or should be) a split cell
6332 my $expectedBodyColumn = $anchorColumn + 2;
6333  
6334 # If here head line != body line
6335 CHECK_FOR_PSEUDOJOIN: {
6336 my $pseudojoinColumn = $policy->pseudojoinColumn($bodyGap);
6337 last CHECK_FOR_PSEUDOJOIN if $pseudojoinColumn < 0;
6338 my $headLength = $head->{length};
6339 my $raggedColumn = $headColumn + $headLength + 2;
6340 last CHECK_FOR_PSEUDOJOIN
6341 if $pseudojoinColumn != $raggedColumn
6342 and $pseudojoinColumn != $cellBodyColumn;
6343 if ( $pseudojoinColumn != $bodyColumn ) {
6344  
6345 # Works as a regular split arm, so not a
6346 # pseudojoing after all.
6347 last CHECK_FOR_PSEUDOJOIN if $bodyColumn == $expectedBodyColumn;
6348  
6349 my $msg =
6350 sprintf
6351 'Pseudo-joined cell %s; body/comment mismatch; body is %s',
6352 describeLC( $parentLine, $parentColumn ),
6353 describeMisindent2( $bodyColumn, $pseudojoinColumn );
6354 push @mistakes,
6355 {
6356 desc => $msg,
6357 subpolicy => [ $runeName, 'pseudo-comment-indent' ],
6358 parentLine => $parentLine,
6359 parentColumn => $parentColumn,
6360 line => $bodyLine,
6361 column => $bodyColumn,
6362 reportLine => $bodyLine,
6363 reportColumn => $bodyColumn,
6364 topicLines => [ $batteryLine, $batteryHoonLine ],
6365 details => [
6366 [
6367 $armDesc,
6368 (
6369 sprintf 'starts at %s',
6370 describeLC( $batteryHoonLine, $batteryHoonColumn )
6371 ),
6372 ]
6373 ],
6374 };
6375 }
6376 return \@mistakes;
6377 }
6378  
6379 if ( $bodyColumn != $expectedBodyColumn ) {
6380 my $msg = sprintf 'cell body %s; %s',
6381 describeLC( $bodyLine, $bodyColumn ),
6382 describeMisindent2( $bodyColumn, $expectedBodyColumn );
6383 push @mistakes,
6384 {
6385 desc => $msg,
6386 subpolicy => [ $runeName, 'arm-body-indent' ],
6387 parentLine => $parentLine,
6388 parentColumn => $parentColumn,
6389 line => $bodyLine,
6390 column => $bodyColumn,
6391 reportLine => $bodyLine,
6392 reportColumn => $bodyColumn,
6393 topicLines => [ $batteryLine, $batteryHoonLine ],
6394 details => [
6395 [
6396 $armDesc,
6397 (
6398 sprintf 'starts at %s',
6399 describeLC( $batteryHoonLine, $batteryHoonColumn )
6400 ),
6401 ]
6402 ],
6403 };
6404 return \@mistakes;
6405 }
6406  
6407 push @mistakes,
6408 @{
6409 $policy->checkOneLineGap(
6410 $bodyGap,
6411 {
6412 mainColumn => $expectedBodyColumn,
6413 runeName => $runeName,
6414 subpolicy => [$runeName],
6415 topicLines => [ $bodyLine, $batteryLine, $batteryHoonLine ],
6416 details => [
6417 [
6418 $armDesc,
6419 (
6420 sprintf 'starts at %s',
6421 describeLC( $batteryHoonLine, $batteryHoonColumn )
6422 ),
6423 ]
6424 ],
6425 }
6426 )
6427 };
6428  
6429 return \@mistakes;
6430 }
6431  
6432 sub validate {
6433 my ( $policy, $node ) = @_;
6434 my $instance = $policy->{lint};
6435  
6436 $policy->validate_node($node);
6437 return if $node->{type} ne 'node';
6438 my $children = $node->{children};
6439 CHILD: for my $childIX ( 0 .. $#$children ) {
6440 my $child = $children->[$childIX];
6441 $policy->validate($child);
6442 }
6443 return;
6444 }
6445  
6446 sub reportMistakes {
6447 my ( $policy, $mistakes ) = @_;
6448 my $instance = $policy->{lint};
6449 my $fileName = $instance->{fileName};
6450  
6451 my @pieces = ();
6452 MISTAKE: for my $mistake ( @{$mistakes} ) {
6453  
6454 my $parentLine = $mistake->{parentLine};
6455 my $parentColumn = $mistake->{parentColumn};
6456 my $desc = $mistake->{desc};
6457 my $mistakeLine = $mistake->{line};
6458  
6459 # The default report location should be line, column
6460 # instead of parentLine, parentColumn
6461 $mistake->{reportLine} //= $parentLine;
6462 $mistake->{reportColumn} //= $parentColumn;
6463  
6464 $instance->reportItem( $mistake, $desc,
6465 ( $mistake->{topicLines} // [] ), $mistakeLine, );
6466 }
6467 return;
6468 }
6469  
6470 sub validate_node {
6471 my ( $policy, $node ) = @_;
6472  
6473 my $policyShortName = $policy->{shortName};
6474 my $instance = $policy->{lint};
6475 my $fileName = $instance->{fileName};
6476 my $grammar = $instance->{grammar};
6477 my $recce = $instance->{recce};
6478  
6479 my $NYI_Rule = $instance->{NYI_Rule};
6480 my $backdentedRule = $instance->{backdentedRule};
6481 my $tallRuneRule = $instance->{tallRuneRule};
6482 my $tallJogRule = $instance->{tallJogRule};
6483 my $tallNoteRule = $instance->{tallNoteRule};
6484 my $tallLuslusRule = $instance->{tallLuslusRule};
6485 my $tall_0RunningRule = $instance->{tall_0RunningRule};
6486 my $tall_1RunningRule = $instance->{tall_1RunningRule};
6487 my $tall_1JoggingRule = $instance->{tall_1JoggingRule};
6488 my $tall_2JoggingRule = $instance->{tall_2JoggingRule};
6489 my $tall_Jogging1Rule = $instance->{tallJogging1_Rule};
6490  
6491 my $ruleDB = $instance->{ruleDB};
6492 my $lineToPos = $instance->{lineToPos};
6493 my $symbolReverseDB = $instance->{symbolReverseDB};
6494  
6495 my $parentSymbol = $node->{symbol};
6496 my $parentStart = $node->{start};
6497 my $parentLength = $node->{length};
6498 my $parentRuleID = $node->{ruleID};
6499  
6500 # $Data::Dumper::Maxdepth = 3;
6501 # say Data::Dumper::Dumper($node);
6502  
6503 my ( $parentLine, $parentColumn ) = $instance->line_column($parentStart);
6504 my $parentLC = join ':', $parentLine, $parentColumn + 1;
6505  
6506 my $children = $node->{children};
6507  
6508 my $nodeType = $node->{type};
6509 return if $nodeType ne 'node';
6510  
6511 my $ruleID = $node->{ruleID};
6512 my ( $lhs, @rhs ) = $grammar->rule_expand( $node->{ruleID} );
6513 my $lhsName = $grammar->symbol_name($lhs);
6514  
6515 # tall node
6516  
6517 my $mistakes = [];
6518 my $start = $node->{start};
6519  
6520 GATHER_MISTAKES: {
6521  
6522 state $fnHash = {
6523 tallKidsOfTop => \&checkTopKids,
6524 tallKidsOfElem => \&checkElemKids,
6525 tallTailOfElem => \&checkTailOfElem,
6526 tallTailOfTop => \&checkTailOfTop,
6527 tallTopSail => \&checkTopSail,
6528 bonz5d => \&checkBonz5d,
6529 };
6530 my $fn = $fnHash->{$lhsName};
6531 if ($fn) {
6532 $mistakes = $fn->($policy, $node);
6533 last GATHER_MISTAKES;
6534 }
6535  
6536 if ( $lhsName eq 'optGay4i' ) {
6537 my $gapLength = $node->{length};
6538 return if $gapLength <= 0;
6539 my $start = $node->{start};
6540  
6541 # Special case for final newline
6542 HANDLE_SINGLE_LINE_TRAILER: {
6543 last HANDLE_SINGLE_LINE_TRAILER if $start + $gapLength != $lineToPos->[-1];
6544 my $literal = $instance->literalNode($node);
6545 # say STDERR join " ", __FILE__, __LINE__, $lhsName, '[' . $literal. ']';
6546 return if $literal =~ m/\A [^\n]* \n \z/xms;
6547 # say STDERR join " ", __FILE__, __LINE__, $lhsName, '[' . $literal. ']';
6548 }
6549  
6550 # say STDERR join " ", __FILE__, __LINE__, $lhsName, '[' . $instance->literalNode($node) . ']';
6551 my $runeName = 'fordfile';
6552 $mistakes =
6553 $policy->checkOneLineGap(
6554 $node,
6555 {
6556 mainColumn => 0,
6557 runeName => $runeName,
6558 subpolicy => [ $runeName, 'vgap' ],
6559 }
6560 );
6561 # say STDERR join " ", __FILE__, __LINE__, Data::Dumper::Dumper($mistakes);
6562 last GATHER_MISTAKES;
6563 }
6564  
6565 my $childCount = scalar @{$children};
6566 if ( $childCount <= 1 ) {
6567 return;
6568 }
6569  
6570 my $firstChildIndent = $instance->column( $children->[0]->{start} ) - 1;
6571  
6572 my $gapiness = $ruleDB->[$ruleID]->{gapiness} // 0;
6573  
6574 my $reportType = $gapiness < 0 ? 'sequence' : 'indent';
6575  
6576 # TODO: In another policy, warn on tall children of wide nodes
6577 if ( $gapiness == 0 ) { # wide node
6578 return;
6579 }
6580  
6581 if ( $gapiness < 0 ) { # sequence
6582 my $previousLine = $parentLine;
6583 TYPE_INDENT: {
6584  
6585 if ( $lhsName eq 'rick5d' ) {
6586 $mistakes = $policy->checkJogging($node);
6587 last TYPE_INDENT;
6588 }
6589 if ( $lhsName eq 'ruck5d' ) {
6590 $mistakes = $policy->checkJogging($node);
6591 last TYPE_INDENT;
6592 }
6593  
6594 if ( $lhsName eq 'fordFascomElements' ) {
6595 $mistakes = $policy->checkFascomElements($node);
6596 last TYPE_INDENT;
6597 }
6598  
6599 if ( $lhsName eq 'fordHoopSeq' ) {
6600 $mistakes = $policy->checkSeq( $node, 'hoop' );
6601 last TYPE_INDENT;
6602 }
6603  
6604 if ( $lhsName eq 'hornSeq' ) {
6605 $mistakes = $policy->checkSeq( $node, 'horn' );
6606 last TYPE_INDENT;
6607 }
6608  
6609 if ( $lhsName eq 'optBonzElements' ) {
6610 $mistakes = $policy->checkBonzElements( $node );
6611 last TYPE_INDENT;
6612 }
6613  
6614 my $grandParent = $instance->ancestor( $node, 1 );
6615 my $grandParentName = $instance->brickName($grandParent);
6616 if ( $lhsName eq 'tall5dSeq' or $lhsName eq 'till5dSeq' ) {
6617 if ( $grandParentName eq 'lute5d' ) {
6618 last TYPE_INDENT;
6619 }
6620 if ( $tall_1RunningRule->{$grandParentName} ) {
6621 last TYPE_INDENT;
6622 }
6623 if ( $tall_0RunningRule->{$grandParentName} ) {
6624 last TYPE_INDENT;
6625 }
6626 }
6627  
6628 if ( $lhsName eq 'whap5d' ) {
6629 my $greatGrandParent =
6630 $instance->ancestor( $grandParent, 1 );
6631 my $greatGrandParentName =
6632 $instance->brickName($greatGrandParent);
6633  
6634 # TODO: remove after development?
6635 die
6636 unless $greatGrandParentName eq 'tallBarcab'
6637 or $greatGrandParentName eq 'tallBarcen'
6638 or $greatGrandParentName eq 'tallBarket';
6639 $mistakes = $policy->checkWhap5d($node);
6640 last TYPE_INDENT;
6641 }
6642  
6643 # By default, treat as not yet implemented
6644 $mistakes = $policy->checkNYI($node);
6645 last TYPE_INDENT if @{$mistakes};
6646  
6647 # should never reach here
6648 die "NYI";
6649 }
6650  
6651 last GATHER_MISTAKES;
6652 }
6653  
6654 # if here, gapiness > 0
6655  
6656 TYPE_INDENT: {
6657  
6658 # This would be faster as a hash
6659  
6660 if ( $lhsName eq "bont5d" ) {
6661 $mistakes = $policy->checkBont($node);
6662 last TYPE_INDENT;
6663 }
6664  
6665 if ( $lhsName eq "bonzElement" ) {
6666 $mistakes = $policy->checkBonzElement($node);
6667 last TYPE_INDENT;
6668 }
6669  
6670 if ( $lhsName eq 'fordFascom' ) {
6671 $mistakes = $policy->checkFascom($node);
6672 last TYPE_INDENT;
6673 }
6674  
6675 if ( $lhsName =~ '^fordFas(bar|dot)' ) {
6676 # say STDERR join " ", __FILE__, __LINE__, $lhsName, $instance->literalNode($node);
6677 $mistakes = $policy->checkFasdot($node);
6678 last TYPE_INDENT;
6679 }
6680  
6681 if ( $lhsName =~ m/^fordFas(buc|cab|cen|hax|sig)$/ ) {
6682 $mistakes = $policy->checkFord_1( $node );
6683 last TYPE_INDENT;
6684 }
6685  
6686 if ( $lhsName eq "fordHoop" ) {
6687 $mistakes = $policy->checkFordHoop( $node );
6688 last TYPE_INDENT;
6689 }
6690  
6691 if ( $lhsName eq "fordFastis" ) {
6692 $mistakes = $policy->checkFastis($node);
6693 last TYPE_INDENT;
6694 }
6695  
6696 if ( $lhsName eq "fordFaswut" ) {
6697 $mistakes = $policy->checkFaswut($node);
6698 last TYPE_INDENT;
6699 }
6700  
6701 if ( $lhsName eq "fordFascomElement" ) {
6702 $mistakes = $policy->checkFascomElement($node);
6703 last TYPE_INDENT;
6704 }
6705  
6706 if ( $lhsName eq "lute5d" ) {
6707 $mistakes = $policy->checkLute($node);
6708 last TYPE_INDENT;
6709 }
6710  
6711 if ( $lhsName =~ m/optFordFas(hep|lus)/ ) {
6712 $mistakes = $policy->checkFordHoofRune($lhsName, $node);
6713 last TYPE_INDENT;
6714 }
6715  
6716 if ( $lhsName eq "tallAttribute" ) {
6717 $mistakes = $policy->checkSailAttribute($node);
6718 last TYPE_INDENT;
6719 }
6720  
6721 if ( $lhsName eq "tallBarcab" ) {
6722 $mistakes = $policy->checkBarcab($node);
6723 last TYPE_INDENT;
6724 }
6725  
6726 if ( $lhsName eq "tallBarcen" ) {
6727 $mistakes = $policy->checkBarcen($node);
6728 last TYPE_INDENT;
6729 }
6730  
6731 if ( $lhsName eq "tallBarket" ) {
6732 $mistakes = $policy->checkBarket($node);
6733 last TYPE_INDENT;
6734 }
6735  
6736 if ( $lhsName eq "tallKetdot" ) {
6737 $mistakes = $policy->checkKetdot($node);
6738 last TYPE_INDENT;
6739 }
6740  
6741 if ( $lhsName eq "wisp5d" ) {
6742 $mistakes = $policy->checkWisp5d($node);
6743 last TYPE_INDENT;
6744 }
6745  
6746 if ( $NYI_Rule->{$lhsName} ) {
6747 $mistakes = $policy->checkNYI($node);
6748 last TYPE_INDENT if @{$mistakes};
6749  
6750 # should never reach here
6751 die 'NYI failure';
6752 }
6753  
6754 if ( $tallJogRule->{$lhsName} ) {
6755 $mistakes = $policy->checkJog($node);
6756 last TYPE_INDENT;
6757 }
6758  
6759 if ( $tall_0RunningRule->{$lhsName} ) {
6760 $mistakes = $policy->check_0Running($node);
6761 last TYPE_INDENT;
6762 }
6763  
6764 if ( $tall_1RunningRule->{$lhsName} ) {
6765 $mistakes = $policy->check_1Running($node);
6766 last TYPE_INDENT;
6767 }
6768  
6769 if ( $tall_1JoggingRule->{$lhsName} ) {
6770 $mistakes = $policy->check_1Jogging($node);
6771 last TYPE_INDENT;
6772 }
6773  
6774 if ( $tall_2JoggingRule->{$lhsName} ) {
6775 $mistakes = $policy->check_2Jogging($node);
6776 last TYPE_INDENT;
6777 }
6778  
6779 if ( $tall_Jogging1Rule->{$lhsName} ) {
6780 $mistakes = $policy->check_Jogging1($node);
6781 last TYPE_INDENT;
6782 }
6783  
6784 if ( $tallNoteRule->{$lhsName} ) {
6785 $mistakes = $policy->checkBackdented($node);
6786 last TYPE_INDENT;
6787 }
6788  
6789 if ( $tallLuslusRule->{$lhsName} ) {
6790 $mistakes = $policy->checkLuslus( $node, $lhsName );
6791 last TYPE_INDENT;
6792 }
6793  
6794 if ( $backdentedRule->{$lhsName} ) {
6795 $mistakes = $policy->checkBackdented($node);
6796 last TYPE_INDENT;
6797 }
6798  
6799 # By default, treat as not yet implemented
6800 {
6801 $mistakes = $policy->checkNYI($node);
6802 last TYPE_INDENT if @{$mistakes};
6803  
6804 # should never reach here
6805 die 'NYI failure';
6806 }
6807  
6808 }
6809 }
6810  
6811 PRINT: {
6812 if ( @{$mistakes} ) {
6813 for my $mistake ( @{$mistakes} ) {
6814 $mistake->{policy} = $policyShortName;
6815 $mistake->{subpolicy} = $mistake->{subpolicy}
6816 // $instance->diagName($node);
6817 }
6818 $policy->reportMistakes($mistakes);
6819 last PRINT;
6820 }
6821  
6822 }
6823  
6824 return;
6825 }
6826  
6827 1;
6828  
6829 # vim: expandtab shiftwidth=4: