C# Class Antlr4.Runtime.Tree.Pattern.ParseTreePatternMatcher

A tree pattern matching mechanism for ANTLR Antlr4.Runtime.Tree.IParseTree s.

Patterns are strings of source input text with special tags representing token or rule references such as:

<ID> = <expr>;

Given a pattern start rule such as statement , this object constructs a Antlr4.Runtime.Tree.IParseTree with placeholders for the ID and expr subtree. Then the Match(Antlr4.Runtime.Tree.IParseTree, ParseTreePattern) routines can compare an actual Antlr4.Runtime.Tree.IParseTree from a parse with this pattern. Tag <ID> matches any ID token and tag <expr> references the result of the expr rule (generally an instance of ExprContext .

Pattern x = 0; is a similar pattern that matches the same pattern except that it requires the identifier to be x and the expression to be 0 .

The Matches(Antlr4.Runtime.Tree.IParseTree, ParseTreePattern) routines return or based upon a match for the tree rooted at the parameter sent in. The Match(Antlr4.Runtime.Tree.IParseTree, ParseTreePattern) routines return a ParseTreeMatch object that contains the parse tree, the parse tree pattern, and a map from tag name to matched nodes (more below). A subtree that fails to match, returns with ParseTreeMatch.MismatchedNode set to the first tree node that did not match.

For efficiency, you can compile a tree pattern in string form to a ParseTreePattern object.

See TestParseTreeMatcher for lots of examples. ParseTreePattern has two static helper methods: ParseTreePattern.FindAll(Antlr4.Runtime.Tree.IParseTree, string) and ParseTreePattern.Match(Antlr4.Runtime.Tree.IParseTree) that are easy to use but not super efficient because they create new ParseTreePatternMatcher objects each time and have to compile the pattern in string form before using it.

The lexer and parser that you pass into the ParseTreePatternMatcher constructor are used to parse the pattern in string form. The lexer converts the <ID> = <expr>; into a sequence of four tokens (assuming lexer throws out whitespace or puts it on a hidden channel). Be aware that the input stream is reset for the lexer (but not the parser; a Antlr4.Runtime.ParserInterpreter is created to parse the input.). Any user-defined fields you have put into the lexer might get changed when this mechanism asks it to scan the pattern string.

Normally a parser does not accept token <expr> as a valid expr but, from the parser passed in, we create a special version of the underlying grammar representation (an Antlr4.Runtime.Atn.ATN ) that allows imaginary tokens representing rules ( <expr> ) to match entire rules. We call these bypass alternatives.

Delimiters are < and > , with \ as the escape string by default, but you can set them to whatever you want using SetDelimiters(string, string, string) . You must escape both start and stop strings \< and \> .

Datei anzeigen Open project: antlr/antlr4 Class Usage Examples

Protected Properties

Property Type Description
escape string
start string
stop string

Private Properties

Property Type Description
Match ParseTreeMatch
MatchImpl IParseTree
Split IList

Public Methods

Method Description
Compile ( string pattern, int patternRuleIndex ) : ParseTreePattern

For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.

Match ( IParseTree tree, string pattern, int patternRuleIndex ) : ParseTreeMatch

Compare pattern matched as rule patternRuleIndex against tree and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed.

Matches ( IParseTree tree, ParseTreePattern pattern ) : bool

Does pattern matched as rule patternRuleIndex match tree? Pass in a compiled pattern instead of a string representation of a tree pattern.

Matches ( IParseTree tree, string pattern, int patternRuleIndex ) : bool

Does pattern matched as rule patternRuleIndex match tree ?

ParseTreePatternMatcher ( Lexer lexer, Parser parser ) : System

Constructs a ParseTreePatternMatcher or from a Antlr4.Runtime.Lexer and Antlr4.Runtime.Parser object. The lexer input stream is altered for tokenizing the tree patterns. The parser is used as a convenient mechanism to get the grammar name, plus token, rule names.

SetDelimiters ( string start, string stop, string escapeLeft ) : void

Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.

Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.

Tokenize ( string pattern ) : IList

Protected Methods

Method Description
GetRuleTagToken ( IParseTree t ) : RuleTagToken

Is t (expr <expr>) subtree?

Private Methods

Method Description
Match ( IParseTree tree, ParseTreePattern pattern ) : ParseTreeMatch
MatchImpl ( IParseTree tree, IParseTree patternTree, IParseTree>.MultiMap labels ) : IParseTree
Split ( string pattern ) : IList

Split <ID> = <e:expr> ; into 4 chunks for tokenizing by Tokenize(string) .

Method Details

Compile() public method

For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.
public Compile ( string pattern, int patternRuleIndex ) : ParseTreePattern
pattern string
patternRuleIndex int
return ParseTreePattern

GetRuleTagToken() protected method

Is t (expr <expr>) subtree?
protected GetRuleTagToken ( IParseTree t ) : RuleTagToken
t IParseTree
return RuleTagToken

Match() public method

Compare pattern matched as rule patternRuleIndex against tree and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed.
public Match ( IParseTree tree, string pattern, int patternRuleIndex ) : ParseTreeMatch
tree IParseTree
pattern string
patternRuleIndex int
return ParseTreeMatch

Matches() public method

Does pattern matched as rule patternRuleIndex match tree? Pass in a compiled pattern instead of a string representation of a tree pattern.
public Matches ( IParseTree tree, ParseTreePattern pattern ) : bool
tree IParseTree
pattern ParseTreePattern
return bool

Matches() public method

Does pattern matched as rule patternRuleIndex match tree ?
public Matches ( IParseTree tree, string pattern, int patternRuleIndex ) : bool
tree IParseTree
pattern string
patternRuleIndex int
return bool

ParseTreePatternMatcher() public method

Constructs a ParseTreePatternMatcher or from a Antlr4.Runtime.Lexer and Antlr4.Runtime.Parser object. The lexer input stream is altered for tokenizing the tree patterns. The parser is used as a convenient mechanism to get the grammar name, plus token, rule names.
public ParseTreePatternMatcher ( Lexer lexer, Parser parser ) : System
lexer Lexer
parser Parser
return System

SetDelimiters() public method

Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.
Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.
/// IllegalArgumentException /// if /// /// is /// /// or empty. /// /// IllegalArgumentException /// if /// /// is /// /// or empty. ///
public SetDelimiters ( string start, string stop, string escapeLeft ) : void
start string The start delimiter.
stop string The stop delimiter.
escapeLeft string The escape sequence to use for escaping a start or stop delimiter.
return void

Tokenize() public method

public Tokenize ( string pattern ) : IList
pattern string
return IList

Property Details

escape protected_oe property

protected string escape
return string

start protected_oe property

protected string start
return string

stop protected_oe property

protected string stop
return string