antlr/antlr4 · error · Exception
Missing path element at end of path
Error message
Missing path element at end of path
What it means
Raised by XPath.getXPathElement() in the Python3 runtime when the element token is EOF, i.e. the path ends where an element (rule name, token name, wildcard, or string) is required. It is the internal counterpart of the 'Missing element after' checks.
Source
Thrown at runtime/Python3/src/antlr4/xpath/XPath.py:135
# Add searched element
if el.type in [XPathLexer.TOKEN_REF, XPathLexer.RULE_REF, XPathLexer.WILDCARD, XPathLexer.STRING]:
element = self.getXPathElement(el, anywhere)
element.invert = invert
elements.append(element)
elif el.type==Token.EOF:
break
else:
raise Exception("Unknown path element %s" % lexer.symbolicNames[el.type])
return elements
#
# Convert word like {@code#} or {@code ID} or {@code expr} to a path
# element. {@code anywhere} is {@code true} if {@code //} precedes the
# word.
#
def getXPathElement(self, wordToken:Token, anywhere:bool):
if wordToken.type==Token.EOF:
raise Exception("Missing path element at end of path")
word = wordToken.text
if wordToken.type==XPathLexer.WILDCARD :
return XPathWildcardAnywhereElement() if anywhere else XPathWildcardElement()
elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]:
tsource = self.parser.getTokenStream().tokenSource
ttype = Token.INVALID_TYPE
if wordToken.type == XPathLexer.TOKEN_REF:
if word in tsource.ruleNames:
ttype = tsource.ruleNames.index(word) + 1
else:
if word in tsource.literalNames:
ttype = tsource.literalNames.index(word)
if ttype == Token.INVALID_TYPE:
raise Exception("%s at index %d isn't a valid token name" % (word, wordToken.tokenIndex))View on GitHub (pinned to 7d5770395b)
Solutions
- Terminate the path with a concrete element instead of a separator or bang
- Validate assembled paths end with an identifier, '*', or quoted string
- Strip trailing separator/bang characters before evaluation
Example fix
# before nodes = XPath.findAll(tree, "//expr/", parser) # after nodes = XPath.findAll(tree, "//expr", parser)
Defensive patterns
Strategy: validation
Validate before calling
import re
if not re.search(r"([A-Za-z_][A-Za-z0-9_]*|\*|'[^']*')\s*$", xpath.rstrip()):
raise ValueError('XPath must end with an element') Try / catch
try:
nodes = XPath.findAll(tree, xpath, parser)
except Exception as e:
if 'Missing path element' in str(e):
# fix trailing separator/bang
... Prevention
- End every XPath with an identifier, '*', or quoted string
- Validate generated paths before evaluation
When it happens
Trigger: A path whose last meaningful token is a separator or '!', so the element slot receives EOF; commonly reached through getXPathElement directly or edge cases in splitToTokens.
Common situations: Paths ending in '/', '//', or '!', especially dynamically assembled ones.
Related errors
- Missing element after %s
- Unknown path element %s
- %s at index %d isn't a valid token name
- %s at index %d isn't a valid rule name
- The current recognizer does not provide a list of token name
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/0185620c917402af.
Report an issue: GitHub.