Fix ValueError when formatting procedures using label syntax (bar:LOOP)#185
Open
VCourdy wants to merge 1 commit into
Open
Fix ValueError when formatting procedures using label syntax (bar:LOOP)#185VCourdy wants to merge 1 commit into
VCourdy wants to merge 1 commit into
Conversation
A colon immediately preceded by a word character is a label separator
(MySQL/Oracle label:LOOP), never a bind-variable prefix. The tokenizer
lexed bar:LOOP as WORD('bar') + VARIABLE(':LOOP'), swallowing the LOOP
keyword: the indent-opener branch in SqlFormatter::format() never fired
while END LOOP and END still decremented the indent level, driving it
to -1 and crashing str_repeat() with a ValueError.
Guard the ':' alternative of the VARIABLE pattern with a negative
lookbehind (?<!\w); '@' keeps its unconditional behavior.
Co-Authored-By: Claude Fable 5 <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.
Bug
SqlFormatter::format()crashes withValueError: str_repeat(): Argument #2 ($times) must be greater than or equal to 0on MySQL stored procedures using label syntax with no space after the colon:bar: LOOP(with a space) does not crash; the trigger is specifically:glued toLOOP.Root cause
The tokenizer lexes
:as a bind-variable prefix, sobar:LOOPtokenizes asWORD('bar')+VARIABLE(':LOOP'). TheLOOPkeyword is swallowed and the indent-opener branch inSqlFormatter::format()never fires, whileEND LOOPand the finalENDeach still decrement the indentation level. It reaches-1andstr_repeat($tab, $indentLevel)throws.Fix
A colon immediately preceded by a word character is never a bind-variable prefix in any supported dialect — it is a label separator (MySQL/Oracle
label:LOOP). This PR guards the:alternative of theVARIABLEpattern with a negative lookbehind:@keeps its old unconditional behavior; only:gets the guard.Impact on existing behavior
tests/sql.sqlrender byte-identically across all five expected-output files (only the new regression section is appended).:param,@var,WHERE x = :idstill tokenize asTOKEN_TYPE_VARIABLE(covered by a new tokenizer test). PostgreSQL casts (1::text,'{}'::json) are unaffected — in::the second:is preceded by:, not a word character.:glued to a preceding identifier or number (e.g. the:5in a PostgreSQL array slicearr[1:5]) wasVARIABLEand becomesBOUNDARY+WORD. No fixture covers it and rendered output is equivalent; such a colon is never a placeholder in any supported dialect.Tests
bar:loop→WORD+BOUNDARY+WORD, and:param→VARIABLE.bin/regenerate-expected-output.phpunit,phpstan analyseandphpcsall pass.Relates to #118 (formatter must never fail).
🤖 Generated with Claude Code