antlr/antlr4 · error · ArgumentException

Missing path element at end of path

Error message

Missing path element at end of path

What it means

GetXPathElement() is called with the token that follows a '/' or '//'. If that token is EOF, the path ends immediately after a separator, so no rule, token, or wildcard element exists. ANTLR rejects such paths because every separator must introduce an element.

Source

Thrown at runtime/CSharp/src/Tree/Xpath/XPath.cs:204

        /// or
        /// <c>ID</c>
        /// or
        /// <c>expr</c>
        /// to a path
        /// element.
        /// <paramref name="anywhere"/>
        /// is
        /// <see langword="true"/>
        /// if
        /// <c>//</c>
        /// precedes the
        /// word.
        /// </summary>
        protected internal virtual XPathElement GetXPathElement(IToken wordToken, bool anywhere)
        {
            if (wordToken.Type == TokenConstants.EOF)
            {
                throw new ArgumentException("Missing path element at end of path");
            }
            string word = wordToken.Text;
            int ttype = parser.GetTokenType(word);
            int ruleIndex = parser.GetRuleIndex(word);
            switch (wordToken.Type)
            {
                case XPathLexer.Wildcard:
                {
                    return anywhere ? new XPathWildcardAnywhereElement() : (XPathElement)new XPathWildcardElement();
                }

                case XPathLexer.TokenRef:
                case XPathLexer.String:
                {
                    if (ttype == TokenConstants.InvalidType)
                    {
                        throw new ArgumentException(word + " at index " + wordToken.StartIndex + " isn't a valid token name");
                    }

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Remove the trailing '/', '//', or '/!' from the XPath string.
  2. If the final component was intended, add its name or '*' after the separator.
  3. Trim trailing separators before constructing XPath when assembling paths dynamically.

Example fix

// before
var xpath = "/program/";

// after
var xpath = "/program";
Defensive patterns

Strategy: validation

Validate before calling

static bool HasElementAfterEverySeparator(string path) {
    return !path.EndsWith("/") && !path.EndsWith("//") && !path.EndsWith("!");
}

Try / catch

try { var p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, xpath); }
catch (ArgumentException ex) when (ex.Message == "Missing path element at end of path") { /* trim separator or add element */ }

Prevention

When it happens

Trigger: Paths such as "/prog/", "//stmt//", or "/prog/!" end with a separator (or separator plus inversion) and then EOF. The same occurs when a loop appends '/' after the last component.

Common situations: File-system style paths with a harmless trailing slash are passed to XPath; paths are built by joining components with '/' and the final delimiter is not trimmed; or an optional element is omitted but its separator remains.

Related errors


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/fcb748abdf6a6c16. Report an issue: GitHub.