lexer/parser: report non-ASCII unexpected chars as valid UTF-8#395
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 byte0xE2. The parser spliced that raw byte into"Unexpected character: #{<<char>>}", andLua.Parser.Error.to_map/2'smessage— despite its "wire-safe" contract — ended in a lone0xE2, soJason.encode!raisedinvalid byte 0xE2.Changes
All three linked defects from the issue:
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.lib/lua/parser.ex) — renders the codepoint safely via<<cp::utf8>>and names itU+XXXX(e.g.Unexpected character: │ (U+2502)); adds an:invalid_byte→Invalid byte 0xFFmessage.lib/lua/lexer.ex) —advance_utf8/2advancescolumnper codepoint while keepingbyte_offsetper 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/2previously embedded them insource_context.lines[].text.clean/1and the source-context builder now scrub withString.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 (withbyte_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 formatclean.luac -p: line numbers agree; the rendered message is strictly friendlier than PUC-Lua's own byte-oriented<\226>.