Repository navigation
Python: fix: CodeTokenizer: handle spaces inside quoted named-arg values - #14478
Wu Shuwen (dajiaohuang) wants to merge 2 commits into
Conversation
When parsing templates like {{func key='hello world'}}, the tokenizer
incorrectly included spaces inside quoted named-arg values because
the VALUE state did not check for a closing delimiter when a space
was encountered.
Two changes:
1. A space in VALUE mode now only ends the value when followed by a
non-alphanumeric, non-$-, non-quote character (i.e., a genuine
token separator, not part of the quoted text).
2. The closing delimiter check now additionally verifies that the
character after the quote is not alphanumeric, which correctly
handles quoted values adjacent to punctuation like }}.
Fixes microsoft#14460.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The tokenizer does not enter named-argument value handling, so the reported case remains unresolved.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Fixes CodeTokenizer handling of spaces in quoted named-argument values.
Changes:
- Adds quoted-value space handling.
- Adds closing-quote lookahead logic.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if current_char == Symbols.SPACE and next_char not in ( | ||
| Symbols.SGL_QUOTE, | ||
| Symbols.DBL_QUOTE, | ||
| Symbols.VAR_PREFIX, | ||
| ) and not next_char.isalnum() and next_char not in ("_", "-"): |
Eduard van Valkenburg (eavanvalkenburg)
left a comment
There was a problem hiding this comment.
Review finding (blocking): Quoted named-argument values with spaces are still split
The advertised {{func key='hello world'}} case still starts in the FUNCTION_ID state, and the tokenizer never enters the new named-argument value branch while scanning the equals sign and quote. The space after hello therefore still takes the generic separator path and raises NamedArgBlockSyntaxError. Please enter a quoted named-argument state when = is followed by a quote, and add a regression test for this exact template.
The previous fix had two issues: 1. It added a broken space-handling branch that incorrectly terminated ValBlocks on spaces, causing tokens after quoted values to be consumed into the ValBlock. 2. The closing-delimiter check used text[index+2] (off-by-one), preventing legitimate closing quotes (e.g. in escape sequences like 'f\\'xy') from being detected. 3. It never added the state transition from FUNCTION_ID to NAMED_ARG when the opening quote appears mid-token after '=', which is the root cause of the original bug. The root cause was that when parsing 'key='hello world'', the opening quote appears after '=' (still mid-FUNCTION_ID token), not after a space separator. The code never switched into quoted-value mode, so the space inside 'hello world' incorrectly terminated the FUNCTION_ID token. Fix: - Remove the broken space-handling and incorrect index+2 check. - When in FUNCTION_ID mode and the current char is a quote and '=' is already present in the token content, switch to NAMED_ARG mode (setting text_value_delimiter) so that spaces inside the quoted value are preserved and the closing quote ends the NamedArgBlock. - When closing a quoted block, create NamedArgBlock (not ValBlock) if in NAMED_ARG mode. - Handle NAMED_ARG in the end-of-loop final-capture. - Add regression test for issue microsoft#14460.


Description
When parsing templates like
{{func key='hello world'}}, the CodeTokenizer incorrectly included spaces inside quoted named-arg values, causingNamedArgBlockSyntaxError.Root cause
The VALUE state in the tokenizer's main loop appends every character to
current_token_contentwithout checking whether a space is a token separator. When a space inside a quoted value is followed by an alphanumeric character (the continuation of the quoted text), the space is incorrectly treated as a token boundary.Two changes fix this:
Space handling in VALUE mode: A space now ends the quoted value only when the following character is not a valid quoted-value continuation (alphanumeric,
_,-,$, quote,VAR_PREFIX).Closing delimiter check: The closing quote is recognized as a block boundary only when the character after it is not alphanumeric. This correctly handles quoted values adjacent to punctuation like
}}.Reproduction
Fixes #14460.