{"record":{"id":"33a8d97dce961b21","repo":"twigphp/Twig","slug":"the-lexer-or-the-parser-ended-up-in-an-unsupported-state","errorCode":null,"errorMessage":"The lexer or the parser ended up in an unsupported state.","messagePattern":"The lexer or the parser ended up in an unsupported state\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Parser.php","lineNumber":256,"sourceCode":"                        $node = $subparser->parse($token);\n                        $documentationTarget = $this->documentationTargets[$documentationTargetIndex];\n                    } finally {\n                        array_pop($this->documentationTargets);\n                    }\n                    if (!$node) {\n                        trigger_deprecation('twig/twig', '3.12', 'Returning \"null\" from \"%s\" is deprecated and forbidden by \"TokenParserInterface\".', $subparser::class);\n                    } else {\n                        $node->setNodeTag($subparser->getTag());\n                        NodeDocumentation::add($node, $startToken);\n                        if (null !== $documentationTarget && $node !== $documentationTarget) {\n                            NodeDocumentation::move($node, $documentationTarget);\n                        }\n                        $rv[] = $node;\n                    }\n                    break;\n\n                default:\n                    throw new SyntaxError('The lexer or the parser ended up in an unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext());\n            }\n        }\n\n        if (1 === \\count($rv)) {\n            return $rv[0];\n        }\n\n        return new Nodes($rv, $lineno);\n    }\n\n    public function getBlockStack(): array\n    {\n        trigger_deprecation('twig/twig', '3.12', 'Method \"%s()\" is deprecated.', __METHOD__);\n\n        return $this->blockStack;\n    }\n\n    /**","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Parser.php#L238-L274","documentation":"This is a defensive internal error: Parser::subparse's switch reached a default branch for a token type it cannot handle (only BLOCK_START, VAR_START and COMMENT_START are expected). It means the lexer produced a token sequence the parser was not designed to receive, i.e. an internal invariant violation rather than a user syntax problem.","triggerScenarios":"The lexer emitted a token type other than BLOCK_START_TYPE/VAR_START_TYPE/COMMENT_START_TYPE at a position where subparse expects markup; typically caused by a custom Lexer/TokenStream modification, a broken subclass overriding lexer/parser internals, or a Twig core version mismatch.","commonSituations":"Custom Twig extensions or forks that alter token types; mismatched twig/twig3 component versions after a partial dependency upgrade; monkey-patching or decorating the Lexer incorrectly.","solutions":["Ensure all twig/twig packages are at one consistent version (composer update / composer why-not)","Remove or fix custom Lexer/Parser subclasses or overrides","Reproduce with a minimal template and stock Twig configuration to isolate the custom code","Report to the extension maintainer if a third-party lexer/parser hook causes it"],"exampleFix":"// before\ncomposer require twig/twig:^3.9  # while another package pins ^3.0\n// after\ncomposer update twig/twig  # resolve so a single twig/twig version is installed","handlingStrategy":"try-catch","validationCode":"// verify consistent twig/twig version: composer show twig/twig and composer why-not twig/twig","typeGuard":null,"tryCatchPattern":"try { $env->render($name); } catch (\\Twig\\Error\\SyntaxError $e) { if (str_contains($e->getMessage(), 'unsupported state')) { /* internal inconsistency: check custom lexers/parsers and version pins */ } }","preventionTips":["Keep a single twig/twig version across the dependency tree","Avoid subclassing/monkey-patching Lexer or Parser","Test custom extensions against stock Twig with minimal templates"],"tags":["twig","parser","internal-state","lexer"],"backgroundTag":"internal-invariant-violation","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}