{"record":{"id":"bc5e671e183abf7f","repo":"twigphp/Twig","slug":"sunexpected-token-s-s-s-expected-s","errorCode":null,"errorMessage":"%sUnexpected token \"%s\"%s (\"%s\" expected%s).","messagePattern":"(.+?)Unexpected token \"(.+?)\"(.+?) \\(\"(.+?)\" expected(.+?)\\)\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/TokenStream.php","lineNumber":77,"sourceCode":"    /**\n     * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.\n     *\n     * @return Token|null The next token if the condition is true, null otherwise\n     */\n    public function nextIf($primary, $secondary = null)\n    {\n        return $this->tokens[$this->current]->test($primary, $secondary) ? $this->next() : null;\n    }\n\n    /**\n     * Tests a token and returns it or throws a syntax error.\n     */\n    public function expect($type, $value = null, ?string $message = null): Token\n    {\n        $token = $this->tokens[$this->current];\n        if (!$token->test($type, $value)) {\n            $line = $token->getLine();\n            throw new SyntaxError(\\sprintf('%sUnexpected token \"%s\"%s (\"%s\" expected%s).',\n                $message ? $message.'. ' : '',\n                $token->toEnglish(),\n                $token->getValue() ? \\sprintf(' of value \"%s\"', $token->getValue()) : '',\n                Token::typeToEnglish($type), $value ? \\sprintf(' with value \"%s\"', $value) : ''),\n                $line,\n                $this->source,\n                columnno: $this->source->getColumn($token->getOffset() ?? -1),\n            );\n        }\n        $this->next();\n\n        return $token;\n    }\n\n    /**\n     * Looks at the next token.\n     */\n    public function look(int $number = 1): Token","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/TokenStream.php#L59-L95","documentation":"TokenStream::expect() is Twig's generic token-mismatch error: the parser encountered a token of unexpected type/value. The message is prefixed with any custom message (e.g. 'An argument must be a name') and shows what was found versus what was expected.","triggerScenarios":"Any syntax deviation the parser checks with expect(): e.g. '{% macro 123() %}' (argument not a name), missing commas/parentheses in expressions or argument lists, wrong tag spelling so a closing tag is expected but not found.","commonSituations":"Typos in tag names and operators; using reserved keywords as variable names; missing '(', ')', ',', '=', or '%}' in expressions; editing templates by hand without linting.","solutions":["Read the 'Unexpected token X (Y expected)' message and fix the token at the given line to match what was expected","Run a Twig linter on the template to locate the exact mismatch quickly","Check that custom message prefixes in the error for hints about which construct was being parsed (argument list, tag name, etc.)"],"exampleFix":"// before\n{% for user in users if user.active %}\n// after\n{% for user in users|filter(u => u.active) %}","handlingStrategy":"try-catch","validationCode":"$env->parse($env->tokenize(new Source($template, $name))); // lint before deploy","typeGuard":null,"tryCatchPattern":"try {\n    $html = $twig->render($name, $context);\n} catch (SyntaxError $e) {\n    $context = [\n        'message' => $e->getMessage(),\n        'line' => $e->getTemplateLine(),\n        'source' => $e->getSourceContext()->getCode(),\n    ];\n    // render an editor-facing syntax error page instead of a 500\n}","preventionTips":["Run a Twig linter in CI and in editors (twig-language plugins)","Check the 'X expected' part of the message to know exactly which token to fix","Avoid editing production templates by hand; use review + lint workflow"],"tags":["twig","syntax-error","token-stream","template"],"backgroundTag":"unexpected-token","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}