Skip to content

fix: correct tokenizer greediness with subtraction and division#436

Open
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-macro-expression-parsing
Open

fix: correct tokenizer greediness with subtraction and division#436
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-macro-expression-parsing

Conversation

@toddr-bot

Copy link
Copy Markdown
Contributor

Closes #315.

The tokenizer had two bugs that caused incorrect parsing of arithmetic expressions, especially visible when passing expressions as MACRO arguments:

  1. Negative number greediness. 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. Filename swallowing division. 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.

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.

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>
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.

Silent parsing failure when passing expressions to a MACRO

1 participant