diagnostics: beware of zero-width characters

Currenly we rely on (visual) width of the characters to decide where
to open and close the styling of the quoted lines.  This breaks when
we deal with zero-width characters: we cannot just rely on (visual)
columns, we need to know whether we are before, inside, or after the
highlighted portion.

* src/location.c (location_caret): col_end: no longer add 1, "regular"
characters have a width of 1, only 0-width characters have 0-width.
opened: replace with 'state', a three-valued enum.
Don't reopen the style if we already did.
* tests/diagnostics.at (Zero-width characters): New.
This commit is contained in:
Akim Demaille
2020-03-07 12:59:09 +01:00
parent e21ff47f5d
commit b638603477
3 changed files with 45 additions and 23 deletions

View File

@@ -37,15 +37,15 @@ AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([[input.y]], [$2])
AT_DATA([experr], [$4])
# For some reason, literal ^M in the input are removed and don't end
# in `input.y`. So use the two-character ^M represent it, and let
# Perl insert real CR characters.
if grep '\^M' input.y >/dev/null; then
AT_PERL_REQUIRE([-pi -e 's{\^M}{\r}gx' input.y])
if $EGREP ['\^M|\\[0-9][0-9][0-9]'] input.y experr >/dev/null; then
AT_PERL_REQUIRE([-pi -e 's{\^M}{\r}g;s{\\(\d{3}|.)}{$v = $[]1; $v =~ /\A\d+\z/ ? chr($v) : $v}ge' input.y experr])
fi
AT_DATA([experr], [$4])
AT_CHECK([LC_ALL="$locale" $5 bison -fcaret --color=debug -Wall input.y], [$3], [], [experr])
# When no style, same messages, but without style.
@@ -193,6 +193,24 @@ input.y:12.8-10: previous declaration
]])
## ----------------------- ##
## Zero-width characters. ##
## ----------------------- ##
# We used to open twice the styling for characters that have a
# zero-width on display (e.g., \005).
AT_TEST([[Zero-width characters]],
[[%%
exp: an\005error.
]],
[1],
[[input.y:10.8: <error>error:</error> invalid character: '\\005'
10 | exp: an<error>\005</error>error.
| <error>^</error>
]])
## -------------------------------------- ##
## Tabulations and multibyte characters. ##
## -------------------------------------- ##