chainlint: annotate original test definition rather than token stream
commit73c768dae9ea4838736693965b25ba34e941ac88
authorEric Sunshine <sunshine@sunshineco.com>
Tue, 8 Nov 2022 19:08:30 +0000 (8 19:08 +0000)
committerTaylor Blau <me@ttaylorr.com>
Tue, 8 Nov 2022 20:10:49 +0000 (8 15:10 -0500)
treeae10d04568781f3ef531d4b73832eb25bf2b6b99
parent5f0321a9f25bd1366a6f13c2e2b51d6e1e2a5ded
chainlint: annotate original test definition rather than token stream

When chainlint detects problems in a test, such as a broken &&-chain, it
prints out the test with "?!FOO?!" annotations inserted at each problem
location. However, rather than annotating the original test definition,
it instead dumps out a parsed token representation of the test. Since it
lacks comments, indentations, here-doc bodies, and so forth, this
tokenized representation can be difficult for the test author to digest
and relate back to the original test definition.

However, now that each parsed token carries positional information, the
location of a detected problem can be pinpointed precisely in the
original test definition. Therefore, take advantage of this information
to annotate the test definition itself rather than annotating the parsed
token stream, thus making it easier for a test author to relate a
problem back to the source.

Maintaining the positional meta-information associated with each
detected problem requires a slight change in how the problems are
managed internally. In particular, shell syntax such as:

    msg="total: $(cd data; wc -w *.txt) words"

requires the lexical analyzer to recursively invoke the parser in order
to detect problems within the $(...) expression inside the double-quoted
string. In this case, the recursive parse context will detect the broken
&&-chain between the `cd` and `wc` commands, returning the token stream:

    cd data ; ?!AMP?! wc -w *.txt

However, the parent parse context will see everything inside the
double-quotes as a single string token:

    "total: $(cd data ; ?!AMP?! wc -w *.txt) words"

losing whatever positional information was attached to the ";" token
where the problem was detected.

One way to preserve the positional information of a detected problem in
a recursive parse context within a string would be to attach the
positional information to the annotation textually; for instance:

    "total: $(cd data ; ?!AMP:21:22?! wc -w *.txt) words"

and then extract the positional information when annotating the original
test definition.

However, a cleaner and much simpler approach is to maintain the list of
detected problems separately rather than embedding the problems as
annotations directly in the parsed token stream. Not only does this
ensure that positional information within recursive parse contexts is
not lost, but it keeps the token stream free from non-token pollution,
which may simplify implementation of validations added in the future
since they won't have to handle non-token "?!FOO!?" items specially.

Finally, the chainlint self-test "expect" files need a few mechanical
adjustments now that the original test definitions are emitted rather
than the parsed token stream. In particular, the following items missing
from the historic parsed-token output are now preserved verbatim:

    * indentation (and whitespace, in general)

    * comments

    * here-doc bodies

    * here-doc tag quoting (i.e. "\EOF")

    * line-splices (i.e. "\" at the end of a line)

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
22 files changed:
t/chainlint.pl
t/chainlint/block-comment.expect
t/chainlint/case-comment.expect
t/chainlint/close-subshell.expect
t/chainlint/comment.expect
t/chainlint/double-here-doc.expect
t/chainlint/empty-here-doc.expect
t/chainlint/for-loop.expect
t/chainlint/here-doc-close-subshell.expect
t/chainlint/here-doc-indent-operator.expect
t/chainlint/here-doc-multi-line-command-subst.expect
t/chainlint/here-doc-multi-line-string.expect
t/chainlint/here-doc.expect
t/chainlint/if-then-else.expect
t/chainlint/incomplete-line.expect
t/chainlint/inline-comment.expect
t/chainlint/loop-detect-status.expect
t/chainlint/nested-here-doc.expect
t/chainlint/nested-subshell-comment.expect
t/chainlint/subshell-here-doc.expect
t/chainlint/t7900-subtree.expect
t/chainlint/while-loop.expect