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 \> .

파일 보기 프로젝트 열기: antlr/antlr4 1 사용 예제들

보호된 프로퍼티들

프로퍼티 타입 설명
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