Skip to main content

Module tree_pattern

Module tree_pattern 

Source
Expand description

ANTLR parse-tree pattern matching.

A tree pattern is a string of ordinary grammar input with embedded <tag> placeholders — for example <ID> = <expr>; matched as the rule stat. Literals must match exactly; a <rule> tag matches any subtree of that parser rule and a <TOKEN> tag matches any token of that type. Tags may carry a label, <lhs:ID>, so matched nodes can be looked up by name.

This mirrors ANTLR’s org.antlr.v4.runtime.tree.pattern package. Compiling a pattern lexes its literal chunks with the real lexer, converts each tag into a synthetic rule/token tag token (ANTLR’s RuleTagToken / TokenTagToken), and interprets that hybrid token stream over a rule-bypass ATN (see crate::atn::parser_atn::ParserAtn::with_bypass_alternatives) to build a pattern tree. ParseTreePattern::match_tree then walks a subject tree and the pattern tree in lockstep, binding tag labels.

The ergonomic entry point is the compile_parse_tree_pattern method every generated parser exposes; ParseTreePatternMatcher is the lower-level, reusable compiler behind it.

Structs§

ParseTreeMatch
The result of matching a subject tree against a ParseTreePattern.
ParseTreePattern
A pattern like <ID> = <expr>; compiled to a reusable tree.
ParseTreePatternMatcher
Compiles tree patterns for one grammar.

Enums§

ParseTreePatternError
An invalid tree pattern or a failure while compiling one.

Traits§

PatternLexer
Lexes one literal pattern chunk into the token specs it produces.

Functions§

lex_pattern_chunk
Runs a lexer over one chunk of pattern text and returns its token specs (every non-EOF token, on its original channel), suitable for a PatternLexer.