stanfordnlp/CoreNLP · error · UnsupportedOperationException

Only the root node can produce the top level matcher

Error message

Only the root node can produce the top level matcher

What it means

The base TsurgeonPattern.matcher() is an unsupported default: only the root TsurgeonPattern (top of the parsed operation tree) is allowed to create a top-level TsurgeonMatcher via the no-argument matcher(). Calling matcher() on a child/inner pattern node throws this UnsupportedOperationException.

Solutions

  1. Call matcher() only on the root TsurgeonPattern returned by Tsurgeon.parseOperation or the parser's Root()
  2. For inner nodes, use matcher(newNodeNames, coindexer) with a node-name map and coindexation generator
  3. Ensure custom TsurgeonPattern subclasses implement the two-argument matcher()

Example fix

// before
TsurgeonPattern child = ((TsurgeonRegexPattern) root).child;
TsurgeonMatcher m = child.matcher(); // throws
// after
TsurgeonMatcher m = root.matcher(); // call on the root only
Defensive patterns

Strategy: type-guard

Validate before calling

null

Type guard

boolean isRoot = pattern instanceof TsurgeonPatternRoot;
if (!isRoot) throw new IllegalStateException("matcher() may only be called on the root TsurgeonPattern");

Try / catch

try { TsurgeonMatcher m = pattern.matcher(); } catch (UnsupportedOperationException e) { throw new IllegalStateException("Use the root pattern's matcher(), not an inner node", e); }

Prevention

When it happens

Trigger: Calling the no-arg matcher() directly on a non-root TsurgeonPattern (e.g. a node obtained from a parsed operation's children), instead of matcher(newNodeNames, coindexer) or the root pattern returned by parseOperation/Root().

Common situations: Programmatically walking the TsurgeonPattern tree and invoking matcher() on sub-nodes; custom tsurgeon node subclasses that fail to override the two-argument matcher; test code assuming matcher() works on any node.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/57ed968d507bfc16. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/tsurgeon/TsurgeonPattern.java:88

  @Override
  public String toString() {
    StringBuilder resultSB = new StringBuilder();
    resultSB.append(label);
    if (children.length > 0) {
      resultSB.append('(');
      for (int i = 0; i < children.length; i++) {
        resultSB.append(children[i]);
        if (i < children.length - 1) {
          resultSB.append(", ");
        }
      }
      resultSB.append(')');
    }
    return resultSB.toString();
  }

  public TsurgeonMatcher matcher() {
    throw new UnsupportedOperationException("Only the root node can produce the top level matcher");
  }

  public abstract TsurgeonMatcher matcher(Map<String,Tree> newNodeNames, CoindexationGenerator coindexer);

}

View on GitHub (pinned to 1b7edd19c4)