Skip to content

lexer/parser: report non-ASCII unexpected chars as valid UTF-8#395

Merged
davydog187 merged 1 commit into
mainfrom
fix/parser-invalid-utf8-unexpected-char
Jul 16, 2026
Merged

lexer/parser: report non-ASCII unexpected chars as valid UTF-8#395
davydog187 merged 1 commit into
mainfrom
fix/parser-invalid-utf8-unexpected-char

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Summary

Fixes #394. A non-ASCII "unexpected character" made the parser emit an error whose message was not valid UTF-8, crashing any consumer that JSON-encodes it (e.g. a Sauron LiveView that bricked when a box-drawing character was pasted in).

The lexer matched a single byte for an unexpected character, so a multibyte UTF-8 char (, E2 94 82) was reported by its lone lead byte 0xE2. The parser spliced that raw byte into "Unexpected character: #{<<char>>}", and Lua.Parser.Error.to_map/2's message — despite its "wire-safe" contract — ended in a lone 0xE2, so Jason.encode! raised invalid byte 0xE2.

Changes

All three linked defects from the issue:

  1. Lexer (lib/lua/lexer.ex) — the unexpected-character clause matches <<cp::utf8, _>> and carries the full codepoint; a new {:invalid_byte, byte, pos} fallback handles genuinely-malformed bytes.
  2. Parser (lib/lua/parser.ex) — renders the codepoint safely via <<cp::utf8>> and names it U+XXXX (e.g. Unexpected character: │ (U+2502)); adds an :invalid_byteInvalid byte 0xFF message.
  3. Column counting (lib/lua/lexer.ex)advance_utf8/2 advances column per codepoint while keeping byte_offset per byte, applied to the string, comment, and long-string scanner loops so positions after a multibyte char are correct.

Plus making the wire-safe guarantee real: when the source itself contains malformed bytes, to_map/2 previously embedded them in source_context.lines[].text. clean/1 and the source-context builder now scrub with String.replace_invalid/1, and the docstring states that every string field is valid UTF-8.

Tests

  • test/lua/lexer_test.exs — multibyte unexpected char → {:unexpected_character, 0x2502, _}; invalid byte → {:invalid_byte, 0xFF, _}; multibyte char counts as one column inside strings/comments/long strings (with byte_offset +1).
  • test/lua/parser/error_to_map_test.exs — regression: the issue repro and a malformed-byte source both yield maps where every string field is valid UTF-8 (the invariant that makes the map JSON-encodable — the library carries no JSON dependency).

Verification

  • mix test — full suite green (2618 passed, 7 skipped); mix format clean.
  • Differential check against luac -p: line numbers agree; the rendered message is strictly friendlier than PUC-Lua's own byte-oriented <\226>.

The lexer matched a single byte for an unexpected character, capturing
only the UTF-8 lead byte of a multibyte character. The parser spliced
that raw byte into the error message, producing invalid UTF-8 that
crashed any JSON encoder — despite Lua.Parser.Error.to_map/2's
"wire-safe" contract.

- Lexer: match the full codepoint (<<cp::utf8>>); add an :invalid_byte
  fallback for genuinely malformed bytes.
- Parser: render the codepoint safely and name it U+XXXX; add an
  :invalid_byte message.
- Advance `column` per codepoint (not per byte) in the string, comment,
  and long-string scanners so positions stay UTF-8-correct.
- Make to_map/2's wire-safe guarantee real: scrub source-context text
  and messages with String.replace_invalid/1, and document that every
  string field is valid UTF-8.

Fixes #394
@davydog187
davydog187 merged commit e2607f8 into main Jul 16, 2026
5 checks passed
@davydog187
davydog187 deleted the fix/parser-invalid-utf8-unexpected-char branch July 16, 2026 18:24
@davydog187 davydog187 mentioned this pull request Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser emits invalid UTF-8 for a non-ASCII "unexpected character"

1 participant