antlr/antlr4 · error · IllegalArgumentException

tag delimiters out of order in pattern: "+pattern

Error message

tag delimiters out of order in pattern: "+pattern

What it means

Error "tag delimiters out of order in pattern: "+pattern" thrown in antlr/antlr4.

Source

Thrown at runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.java:454

				p++;
			}
		}

//		System.out.println("");
//		System.out.println(starts);
//		System.out.println(stops);
		if ( starts.size() > stops.size() ) {
			throw new IllegalArgumentException("unterminated tag in pattern: "+pattern);
		}

		if ( starts.size() < stops.size() ) {
			throw new IllegalArgumentException("missing start tag in pattern: "+pattern);
		}

		int ntags = starts.size();
		for (int i=0; i<ntags; i++) {
			if ( starts.get(i)>=stops.get(i) ) {
				throw new IllegalArgumentException("tag delimiters out of order in pattern: "+pattern);
			}
		}

		// collect into chunks now
		if ( ntags==0 ) {
			String text = pattern.substring(0, n);
			chunks.add(new TextChunk(text));
		}

		if ( ntags>0 && starts.get(0)>0 ) { // copy text up to first tag into chunks
			String text = pattern.substring(0, starts.get(0));
			chunks.add(new TextChunk(text));
		}
		for (int i=0; i<ntags; i++) {
			// copy inside of <tag>
			String tag = pattern.substring(starts.get(i) + start.length(), stops.get(i));
			String ruleOrToken = tag;
			String label = null;

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Make sure the start delimiter appears before the stop delimiter in each tag; reorder the pattern text.
  2. Check for typos like 'expr>' instead of '<expr>'.

Example fix

ParseTreePattern p = matcher.compile("<expr>", MyParser.RULE_expr);

When it happens

Trigger: Start delimiter of a tag appears after its stop delimiter in the pattern.

Common situations: Order delimiters correctly (start before stop); do not swap the configured start/stop strings.


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