{"record":{"id":"8be77637fd2f4165","repo":"karatelabs/karate","slug":"message-position-token-parser-state-state-hint","errorCode":null,"errorMessage":"<message>\n<position> <token>\nparser state: <state>[<hint>]","messagePattern":"<message>\n<position> <token>\nparser state: <state>\\[<hint>\\]","errorType":"exception","errorClass":"io.karatelabs.parser.ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/BaseParser.java","lineNumber":123,"sourceCode":"        }\n        if (stackPointer >= 2) {\n            sb.append(nodeStack[stackPointer - 2].type).append(\" >> \");\n        }\n        sb.append('[').append(nodeStack[stackPointer - 1].type).append(']');\n        return sb.toString();\n    }\n\n    protected void error(String message) {\n        Token token = peekToken();\n        if (errorRecoveryEnabled) {\n            errors.add(new SyntaxError(token, message));\n            return;\n        }\n        String hint = AsiHint.forFailure(tokens, position);\n        if (token.getResource().isFile()) {\n            System.err.println(\"file://\" + token.getResource().getUri().getPath() + \":\" + token.getPositionDisplay() + \" \" + message);\n        }\n        throw new ParserException(message + \"\\n\"\n                + token.getPositionDisplay()\n                + \" \" + token + \"\\nparser state: \" + this\n                + (hint == null ? \"\" : \"\\n\" + hint));\n    }\n\n    protected void error(NodeType... expected) {\n        if (errorRecoveryEnabled) {\n            errors.add(new SyntaxError(peekToken(), \"expected: \" + Arrays.asList(expected), expected[0]));\n            return;\n        }\n        error(\"expected: \" + Arrays.asList(expected));\n    }\n\n    protected void error(TokenType... expected) {\n        if (errorRecoveryEnabled) {\n            errors.add(new SyntaxError(peekToken(), \"expected: \" + Arrays.asList(expected)));\n            return;\n        }","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/BaseParser.java#L105-L141","documentation":"BaseParser.error() raises a ParserException carrying the failure message, the offending token with its position, the parser state (a window of tokens plus the current node-stack path), and optionally an ASI (automatic semicolon insertion) hint. It is the parser's central syntax-error thrower, used by consumeSoft, exit, and consume when an expected token or structure is missing.","triggerScenarios":"Any syntactically invalid Karate/JS script: a missing closing bracket/paren, a keyword used where an expression is expected, an unterminated construct, a missing semicolon that ASI cannot fix.","commonSituations":"Copy-pasted JS with platform-specific syntax, missing braces in feature-file JS blocks, stray characters, editing scripts by hand and dropping a paren or comma.","solutions":["Read the message: it names the expected construct, shows the offending token with file:line:col, and prints parser state.","Apply the ASI hint if present (usually 'add a semicolon here').","Inspect the indicated position and fix the syntax (balance brackets, complete the expression)."],"exampleFix":"// before (script)\nvar x = { a: 1, b: 2 // missing }\n// after\nvar x = { a: 1, b: 2 };","handlingStrategy":"try-catch","validationCode":"// Pre-validate scripts when possible: run through the parser in error-recovery mode\nList<SyntaxError> problems = parser.tryParse(source); // collect before executing","typeGuard":null,"tryCatchPattern":"try {\n    result = karate.eval(script);\n} catch (ParserException e) {\n    // message contains file:line:col, token, parser state, and ASI hint\n    logger.error(\"syntax error:\\n{}\", e.getMessage());\n    throw new IllegalArgumentException(\"invalid script at \" + extractLocation(e.getMessage()), e);\n}","preventionTips":["Read the parser-state block in the message — it shows the exact token window and node path.","Heed ASI hints: many failures are missing semicolons.","Balance brackets/parens; format scripts with an editor that highlights mismatched delimiters.","Lint scripts in CI before shipping feature files."],"tags":["parser","syntax-error","javascript"],"backgroundTag":"syntax-error-parser-state","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}