C# Класс 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 \> .

Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
escape string
start string
stop string

Private Properties

Свойство Тип Описание
Match ParseTreeMatch
MatchImpl IParseTree
Split IList

Открытые методы

Метод Описание
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

Защищенные методы

Метод Описание
GetRuleTagToken ( IParseTree t ) : RuleTagToken

Is t (expr <expr>) subtree?

Приватные методы

Метод Описание
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) .

Описание методов

Compile() публичный метод

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
Результат ParseTreePattern

GetRuleTagToken() защищенный метод

Is t (expr <expr>) subtree?
protected GetRuleTagToken ( IParseTree t ) : RuleTagToken
t IParseTree
Результат RuleTagToken

Match() публичный метод

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
Результат ParseTreeMatch

Matches() публичный метод

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
Результат bool

Matches() публичный метод

Does pattern matched as rule patternRuleIndex match tree ?
public Matches ( IParseTree tree, string pattern, int patternRuleIndex ) : bool
tree IParseTree
pattern string
patternRuleIndex int
Результат bool

ParseTreePatternMatcher() публичный метод

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
Результат System

SetDelimiters() публичный метод

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.
Результат void

Tokenize() публичный метод

public Tokenize ( string pattern ) : IList
pattern string
Результат IList

Описание свойств

escape защищенное свойство

protected string escape
Результат string

start защищенное свойство

protected string start
Результат string

stop защищенное свойство

protected string stop
Результат string