{"record":{"id":"8dc8a12bb6ebfd2a","repo":"antlr/antlr4","slug":"invalid-tokens-or-characters-at-index-pos-in-p","errorCode":null,"errorMessage":"Invalid tokens or characters at index \"+pos+\" in path '\"+path+\"'","messagePattern":"Invalid tokens or characters at index \"\\+pos\\+\" in path '\"\\+path\\+\"'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java","lineNumber":106,"sourceCode":"\t\t\tin = new ANTLRInputStream(new StringReader(path));\n\t\t}\n\t\tcatch (IOException ioe) {\n\t\t\tthrow new IllegalArgumentException(\"Could not read path: \"+path, ioe);\n\t\t}\n\t\tXPathLexer lexer = new XPathLexer(in) {\n\t\t\t@Override\n\t\t\tpublic void recover(LexerNoViableAltException e) { throw e;\t}\n\t\t};\n\t\tlexer.removeErrorListeners();\n\t\tlexer.addErrorListener(new XPathLexerErrorListener());\n\t\tCommonTokenStream tokenStream = new CommonTokenStream(lexer);\n\t\ttry {\n\t\t\ttokenStream.fill();\n\t\t}\n\t\tcatch (LexerNoViableAltException e) {\n\t\t\tint pos = lexer.getCharPositionInLine();\n\t\t\tString msg = \"Invalid tokens or characters at index \"+pos+\" in path '\"+path+\"'\";\n\t\t\tthrow new IllegalArgumentException(msg, e);\n\t\t}\n\n\t\tList<Token> tokens = tokenStream.getTokens();\n//\t\tSystem.out.println(\"path=\"+path+\"=>\"+tokens);\n\t\tList<XPathElement> elements = new ArrayList<XPathElement>();\n\t\tint n = tokens.size();\n\t\tint i=0;\nloop:\n\t\twhile ( i<n ) {\n\t\t\tToken el = tokens.get(i);\n\t\t\tToken next = null;\n\t\t\tswitch ( el.getType() ) {\n\t\t\t\tcase XPathLexer.ROOT :\n\t\t\t\tcase XPathLexer.ANYWHERE :\n\t\t\t\t\tboolean anywhere = el.getType() == XPathLexer.ANYWHERE;\n\t\t\t\t\ti++;\n\t\t\t\t\tnext = tokens.get(i);\n\t\t\t\t\tboolean invert = next.getType()==XPathLexer.BANG;","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java#L88-L124","documentation":"While lexing an XPath path string, the XPathLexer hit a character sequence that matches no lexer rule (LexerNoViableAltException is rethrown instead of recovered). The resulting IllegalArgumentException reports the character position so you can fix the path syntax.","triggerScenarios":"Calling XPath.findAll(tree, xpath, parser) or new XPath(parser, path) with characters the XPath grammar does not support, e.g. '..', '@attr', '[1]', spaces, or unicode punctuation.","commonSituations":"Assuming ANTLR tree XPath supports XML XPath syntax (predicates, axes, parent navigation); copy-pasting XPath expressions from XML tooling into tree queries.","solutions":["Restrict paths to supported syntax: '/', '//', '*', TOKEN_REF (uppercase), ruleRef (lowercase), and 'tokenName:\"literal\"'","Remove XML-XPath constructs like predicates, '..', or '@' from the path","Check the reported character index in the path string to locate the offending character"],"exampleFix":"// before\nXPath.findAll(tree, \"/statement/expr[1]\", parser);\n\n// after\nXPath.findAll(tree, \"/statement/expr\", parser);","handlingStrategy":"validation","validationCode":"// allow only chars/syntax the tree XPath grammar supports\nboolean ok = path.matches(\"[/A-Za-z0-9_:\\\"*]+\") && !path.contains(\"..\") && !path.contains(\"[\");","typeGuard":null,"tryCatchPattern":"try { XPath.findAll(tree, path, parser); } catch (IllegalArgumentException e) { /* e.getMessage() contains the bad index; log and reject the query */ }","preventionTips":["Learn the ANTLR tree XPath subset before writing queries","When paths come from users, validate against the supported syntax before evaluation"],"tags":["antlr","xpath","syntax","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}