fix: correct tokenizer greediness with subtraction and division#436
Open
toddr-bot wants to merge 1 commit into
Open
fix: correct tokenizer greediness with subtraction and division#436toddr-bot wants to merge 1 commit into
toddr-bot wants to merge 1 commit into
Conversation
The tokenizer had two bugs that caused incorrect parsing of arithmetic expressions, especially visible when passing expressions as MACRO arguments (GH #315): 1. The number regex `-?\d+` consumed the minus sign as part of a negative number literal even after identifiers, so `x-2` became `IDENT:x NUMBER:-2` instead of `IDENT:x MINUS NUMBER:2`. Fix: only treat leading `-` as unary when not preceded by a word character, closing paren, or closing bracket. 2. The filename regex matched `x/2` as `FILENAME:x/2` because path segments allowed any `\w` characters including pure digits. Fix: require path segments after `/` or `::` to start with a letter or underscore. Closes #315 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Closes #315.
The tokenizer had two bugs that caused incorrect parsing of arithmetic expressions, especially visible when passing expressions as MACRO arguments:
Negative number greediness. The number regex
-?\d+consumed the minus sign as part of a negative number literal even after identifiers, sox-2becameIDENT:x NUMBER:-2instead ofIDENT:x MINUS NUMBER:2.Fix: only treat leading
-as unary when not preceded by a word character, closing paren, or closing bracket.Filename swallowing division. The filename regex matched
x/2asFILENAME:x/2because path segments allowed any\wcharacters including pure digits.Fix: require path segments after
/or::to start with a letter or underscore.Files changed:
lib/Template/Parser.pm,t/binop.t,t/macro.t.Note: PR #433 also touches GH #315 from a different angle (unary minus parsing in the grammar). These may be complementary or one may supersede the other — happy to rebase or close whichever you prefer.