{"record":{"id":"e5806944d7a9e8b6","repo":"antlr/antlr4","slug":"startruledoesnotconsumefullpattern","errorCode":null,"errorMessage":"StartRuleDoesNotConsumeFullPattern","messagePattern":"StartRuleDoesNotConsumeFullPattern","errorType":"exception","errorClass":"StartRuleDoesNotConsumeFullPattern","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/tree/ParseTreePatternMatcher.py","lineNumber":181,"sourceCode":"        tokenSrc = ListTokenSource(tokenList)\n        tokens = CommonTokenStream(tokenSrc)\n        from ..ParserInterpreter import ParserInterpreter\n        parserInterp = ParserInterpreter(self.parser.grammarFileName, self.parser.tokenNames,\n                                self.parser.ruleNames, self.parser.getATNWithBypassAlts(),tokens)\n        tree = None\n        try:\n            parserInterp.setErrorHandler(BailErrorStrategy())\n            tree = parserInterp.parse(patternRuleIndex)\n        except ParseCancellationException as e:\n            raise e.cause\n        except RecognitionException as e:\n            raise e\n        except Exception as e:\n            raise CannotInvokeStartRule(e)\n\n        # Make sure tree pattern compilation checks for a complete parse\n        if tokens.LA(1)!=Token.EOF:\n            raise StartRuleDoesNotConsumeFullPattern()\n\n        from ..tree.ParseTreePattern import ParseTreePattern\n        return ParseTreePattern(self, pattern, patternRuleIndex, tree)\n\n    #\n    # Recursively walk {@code tree} against {@code patternTree}, filling\n    # {@code match.}{@link ParseTreeMatch#labels labels}.\n    #\n    # @return the first node encountered in {@code tree} which does not match\n    # a corresponding node in {@code patternTree}, or {@code null} if the match\n    # was successful. The specific node returned depends on the matching\n    # algorithm used by the implementation, and may be overridden.\n    #\n    def matchImpl(self, tree:ParseTree, patternTree:ParseTree, labels:dict):\n        if tree is None:\n            raise Exception(\"tree cannot be null\")\n        if patternTree is None:\n            raise Exception(\"patternTree cannot be null\")","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/tree/ParseTreePatternMatcher.py#L163-L199","documentation":"StartRuleDoesNotConsumeFullPattern is raised during ParseTreePatternMatcher.compileTreePattern() when the pattern string still has tokens left after the start rule finishes parsing. Tree patterns must be a complete, valid sentence of the start rule; trailing garbage means the pattern is malformed relative to the grammar.","triggerScenarios":"Calling parser.compilePattern(pattern, ruleIndex) or matcher.compileTreePattern(pattern, patternRuleIndex) with a pattern that does not fully match the start rule, e.g. '<expr> + extra ;' when the start rule 'statement' does not accept the trailing tokens.","commonSituations":"Pattern written against a different grammar version, a pattern longer than the start rule allows, or forgetting that tags must still fit the rule's structure.","solutions":["Rewrite the pattern so it is exactly one full sentence of the chosen start rule","Pass the correct patternRuleIndex that actually derives the whole pattern","Test the pattern by feeding the equivalent concrete text to the grammar and confirming it parses to EOF"],"exampleFix":"# before: trailing ';' not allowed by 'expr' start rule\npattern = parser.compilePattern('<expr> ;', ruleIndex=exprRuleIndex)\n\n# after\npattern = parser.compilePattern('<expr>', ruleIndex=exprRuleIndex)","handlingStrategy":"try-catch","validationCode":"tokens = lexer.tokenize_equivalent(pattern)  # parse pattern text with the grammar first\n# confirm a full parse reaches EOF before compiling","typeGuard":null,"tryCatchPattern":"try:\n    p = matcher.compileTreePattern(pattern, ruleIndex)\nexcept StartRuleDoesNotConsumeFullPattern:\n    # report and adjust pattern/rule index\n    raise","preventionTips":["Keep patterns aligned with the start rule's full sentence shape","Re-verify patterns after grammar changes"],"tags":["antlr","python","parse-tree-matching","pattern-compile"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}