antlr/antlr4 · error · ArgumentException

tag cannot be null or empty

Error message

tag cannot be null or empty

What it means

TagChunk's constructor rejects a null or empty tag. A TagChunk is one parsed piece of a pattern — either a tag (<ID>, <expr>, <label:tag>) or text. A tag chunk without a tag name carries no information and would break matching, so it is rejected. The label part may be null; only the tag is validated.

Source

Thrown at runtime/CSharp/src/Tree/Pattern/TagChunk.cs:111

        /// </param>
        /// <param name="tag">
        /// The tag, which should be the name of a parser rule or token
        /// type.
        /// </param>
        /// <exception>
        /// IllegalArgumentException
        /// if
        /// <paramref name="tag"/>
        /// is
        /// <see langword="null"/>
        /// or
        /// empty.
        /// </exception>
        public TagChunk(string label, string tag)
        {
            if (string.IsNullOrEmpty(tag))
            {
                throw new ArgumentException("tag cannot be null or empty");
            }
            this.label = label;
            this.tag = tag;
        }

        /// <summary>Get the tag for this chunk.</summary>
        /// <remarks>Get the tag for this chunk.</remarks>
        /// <returns>The tag for the chunk.</returns>
        [NotNull]
        public string Tag
        {
            get
            {
                return tag;
            }
        }

        /// <summary>Get the label, if any, assigned to this chunk.</summary>

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Always supply the tag name: new TagChunk("label", "ID") or new TagChunk(null, "expr").
  2. Validate pattern strings reject '<>' before they are chunked.
  3. Add unit tests over your pattern-builder helpers asserting tags are non-empty.

Example fix

// before
chunks.Add(new TagChunk(label, tag)); // tag can be ""

// after
if (string.IsNullOrEmpty(tag)) throw new ArgumentException("tag required", nameof(tag));
chunks.Add(new TagChunk(label, tag));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(tag)) throw new ArgumentException("tag required", nameof(tag));
chunks.Add(new TagChunk(label, tag));

Type guard

static bool IsValidTag(string tag) => !string.IsNullOrEmpty(tag);

Prevention

When it happens

Trigger: Direct construction new TagChunk("label", "") or new TagChunk(null, null); a pattern containing an empty tag '<>' reaching chunk construction.

Common situations: Programmatically assembling TagChunks from variables; empty tag '<>' in a user-supplied pattern (usually caught earlier by delimiter checks); refactoring pattern-building helpers that drop the tag argument.

Related errors


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