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
- Always supply the tag name: new TagChunk("label", "ID") or new TagChunk(null, "expr").
- Validate pattern strings reject '<>' before they are chunked.
- 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
- Validate tag strings at the boundary where patterns or chunks are built.
- Remember only the tag is validated; the label may be null.
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
- pattern cannot be null
- ruleName cannot be null or empty.
- text cannot be null
- target cannot be null.
- tokenSource cannot be null
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/518e0454bcef677d.
Report an issue: GitHub.