{"record":{"id":"676e2d79127ea03a","repo":"antlr/antlr4","slug":"unknown-token-tagchunk-gettag-in-pattern","errorCode":null,"errorMessage":"Unknown token \"+tagChunk.getTag()+\" in pattern: \"+pattern","messagePattern":"Unknown token \"\\+tagChunk\\.getTag\\(\\)\\+\" in pattern: \"\\+pattern","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.java","lineNumber":378,"sourceCode":"\t\t\t}\n\t\t}\n\t\treturn null;\n\t}\n\n\tpublic List<? extends Token> tokenize(String pattern) {\n\t\t// split pattern into chunks: sea (raw input) and islands (<ID>, <expr>)\n\t\tList<Chunk> chunks = split(pattern);\n\n\t\t// create token stream from text and tags\n\t\tList<Token> tokens = new ArrayList<Token>();\n\t\tfor (Chunk chunk : chunks) {\n\t\t\tif ( chunk instanceof TagChunk ) {\n\t\t\t\tTagChunk tagChunk = (TagChunk)chunk;\n\t\t\t\t// add special rule token or conjure up new token from name\n\t\t\t\tif ( Character.isUpperCase(tagChunk.getTag().charAt(0)) ) {\n\t\t\t\t\tInteger ttype = parser.getTokenType(tagChunk.getTag());\n\t\t\t\t\tif ( ttype==Token.INVALID_TYPE ) {\n\t\t\t\t\t\tthrow new IllegalArgumentException(\"Unknown token \"+tagChunk.getTag()+\" in pattern: \"+pattern);\n\t\t\t\t\t}\n\t\t\t\t\tTokenTagToken t = new TokenTagToken(tagChunk.getTag(), ttype, tagChunk.getLabel());\n\t\t\t\t\ttokens.add(t);\n\t\t\t\t}\n\t\t\t\telse if ( Character.isLowerCase(tagChunk.getTag().charAt(0)) ) {\n\t\t\t\t\tint ruleIndex = parser.getRuleIndex(tagChunk.getTag());\n\t\t\t\t\tif ( ruleIndex==-1 ) {\n\t\t\t\t\t\tthrow new IllegalArgumentException(\"Unknown rule \"+tagChunk.getTag()+\" in pattern: \"+pattern);\n\t\t\t\t\t}\n\t\t\t\t\tint ruleImaginaryTokenType = parser.getATNWithBypassAlts().ruleToTokenType[ruleIndex];\n\t\t\t\t\ttokens.add(new RuleTagToken(tagChunk.getTag(), ruleImaginaryTokenType, tagChunk.getLabel()));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrow new IllegalArgumentException(\"invalid tag: \"+tagChunk.getTag()+\" in pattern: \"+pattern);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tTextChunk textChunk = (TextChunk)chunk;","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreePatternMatcher.java#L360-L396","documentation":"Error \"Unknown token \"+tagChunk.getTag()+\" in pattern: \"+pattern\" thrown in antlr/antlr4.","triggerScenarios":"A <tag> in a pattern refers to a name that is not a token type defined in the lexer grammar.","commonSituations":"Verify tag names against lexer token vocabulary; token tags are case-sensitive and must match defined token names.","solutions":["Use a token name that exists in the target grammar's token vocabulary inside the tag.","Check token names via lexer.getVocabulary() or the generated XxxParser.ID constants; token tags are case-sensitive and must match the literal name.","If you meant a rule, prefix accordingly — rules start lowercase, tokens uppercase."],"exampleFix":"// grammar defines: ID : [a-z]+; and rule expr\nParseTreePattern p = matcher.compile(\"<ID> = <expr>;\", MyParser.RULE_assign);","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}