feat: handle trailing comments at EOF and complete EOF tolerance - #59
Merged
Conversation
Add EolComment token to match `;...` at EOF (without trailing newline), preventing error tokens that break the final_eol mechanism. EolIndentHandler converts EolComment to zero-width Eol for UTF-8 safety. Also complete EOF tolerance in parsers.rs (11 remaining directive types not covered by tesujimath#54) and harden resolve_span against invalid byte ranges.
Owner
|
This is great, thanks! |
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
This PR addresses two related issues with files that don't end with a newline:
1. Trailing comments at EOF produce error tokens
The
lex()function'sfinal_eolmechanism (line 306) indicates the intent to support files without trailing newlines — it injects a syntheticEolwhen the token stream ends. However, when the last line contains an inline comment (; ...) without a following\n, the;cannot matchEol(which requires\n) and becomes an error token, defeatingfinal_eol.Fix: Add an
EolCommenttoken (#[regex(r";[^\r\n]*")]) that matches trailing comments at EOF. Logos longest-match ensuresEolstill wins when\nis present.EolIndentHandlerconvertsEolCommentto a zero-widthEolbefore it reaches the parser, maintaining full transparency.2. Complete EOF tolerance in parsers (extends #54)
PR #54 added
choice((Eol, end()))at 4 structural points. This PR extends the same pattern to the remaining 11 directive types (balance, price, open, close, event, note, document, pad, query, custom, commodity) for consistent behavior across all directives.3. Harden resolve_span against invalid byte ranges
resolve_spanused direct slice indexing (source_content[byte_span.start..byte_span.end]) which can panic on invalid byte boundaries. Changed to.get().unwrap_or("")for safety.Motivation
final_eolmechanism already demonstrates the intent to handle missing trailing newlinesTest plan