1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
using System.Collections.Generic;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
using Antlr4.Runtime.Tree;
using Antlr4.Runtime.Tree.Pattern;
using Antlr4.Runtime.Tree.Xpath;
namespace Antlr4.Runtime.Tree.Pattern
{
/// <summary>
/// A pattern like
/// <c><ID> = <expr>;</c>
/// converted to a
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// by
/// <see cref="ParseTreePatternMatcher.Compile(string, int)"/>
/// .
/// </summary>
public class ParseTreePattern
{
/// <summary>
/// This is the backing field for
/// <see cref="PatternRuleIndex()"/>
/// .
/// </summary>
private readonly int patternRuleIndex;
/// <summary>
/// This is the backing field for
/// <see cref="Pattern()"/>
/// .
/// </summary>
[NotNull]
private readonly string pattern;
/// <summary>
/// This is the backing field for
/// <see cref="PatternTree()"/>
/// .
/// </summary>
[NotNull]
private readonly IParseTree patternTree;
/// <summary>
/// This is the backing field for
/// <see cref="Matcher()"/>
/// .
/// </summary>
[NotNull]
private readonly ParseTreePatternMatcher matcher;
/// <summary>
/// Construct a new instance of the
/// <see cref="ParseTreePattern"/>
/// class.
/// </summary>
/// <param name="matcher">
/// The
/// <see cref="ParseTreePatternMatcher"/>
/// which created this
/// tree pattern.
/// </param>
/// <param name="pattern">The tree pattern in concrete syntax form.</param>
/// <param name="patternRuleIndex">
/// The parser rule which serves as the root of the
/// tree pattern.
/// </param>
/// <param name="patternTree">
/// The tree pattern in
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// form.
/// </param>
public ParseTreePattern(ParseTreePatternMatcher matcher, string pattern, int patternRuleIndex, IParseTree patternTree)
{
this.matcher = matcher;
this.patternRuleIndex = patternRuleIndex;
this.pattern = pattern;
this.patternTree = patternTree;
}
/// <summary>Match a specific parse tree against this tree pattern.</summary>
/// <remarks>Match a specific parse tree against this tree pattern.</remarks>
/// <param name="tree">The parse tree to match against this tree pattern.</param>
/// <returns>
/// A
/// <see cref="ParseTreeMatch"/>
/// object describing the result of the
/// match operation. The
/// <see cref="ParseTreeMatch.Succeeded()"/>
/// method can be
/// used to determine whether or not the match was successful.
/// </returns>
[return: NotNull]
public virtual ParseTreeMatch Match(IParseTree tree)
{
return matcher.Match(tree, this);
}
/// <summary>Determine whether or not a parse tree matches this tree pattern.</summary>
/// <remarks>Determine whether or not a parse tree matches this tree pattern.</remarks>
/// <param name="tree">The parse tree to match against this tree pattern.</param>
/// <returns>
///
/// <see langword="true"/>
/// if
/// <paramref name="tree"/>
/// is a match for the current tree
/// pattern; otherwise,
/// <see langword="false"/>
/// .
/// </returns>
public virtual bool Matches(IParseTree tree)
{
return matcher.Match(tree, this).Succeeded;
}
/// <summary>
/// Find all nodes using XPath and then try to match those subtrees against
/// this tree pattern.
/// </summary>
/// <remarks>
/// Find all nodes using XPath and then try to match those subtrees against
/// this tree pattern.
/// </remarks>
/// <param name="tree">
/// The
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// to match against this pattern.
/// </param>
/// <param name="xpath">An expression matching the nodes</param>
/// <returns>
/// A collection of
/// <see cref="ParseTreeMatch"/>
/// objects describing the
/// successful matches. Unsuccessful matches are omitted from the result,
/// regardless of the reason for the failure.
/// </returns>
[return: NotNull]
public virtual IList<ParseTreeMatch> FindAll(IParseTree tree, string xpath)
{
ICollection<IParseTree> subtrees = XPath.FindAll(tree, xpath, matcher.Parser);
IList<ParseTreeMatch> matches = new List<ParseTreeMatch>();
foreach (IParseTree t in subtrees)
{
ParseTreeMatch match = Match(t);
if (match.Succeeded)
{
matches.Add(match);
}
}
return matches;
}
/// <summary>
/// Get the
/// <see cref="ParseTreePatternMatcher"/>
/// which created this tree pattern.
/// </summary>
/// <returns>
/// The
/// <see cref="ParseTreePatternMatcher"/>
/// which created this tree
/// pattern.
/// </returns>
[NotNull]
public virtual ParseTreePatternMatcher Matcher
{
get
{
return matcher;
}
}
/// <summary>Get the tree pattern in concrete syntax form.</summary>
/// <remarks>Get the tree pattern in concrete syntax form.</remarks>
/// <returns>The tree pattern in concrete syntax form.</returns>
[NotNull]
public virtual string Pattern
{
get
{
return pattern;
}
}
/// <summary>
/// Get the parser rule which serves as the outermost rule for the tree
/// pattern.
/// </summary>
/// <remarks>
/// Get the parser rule which serves as the outermost rule for the tree
/// pattern.
/// </remarks>
/// <returns>
/// The parser rule which serves as the outermost rule for the tree
/// pattern.
/// </returns>
public virtual int PatternRuleIndex
{
get
{
return patternRuleIndex;
}
}
/// <summary>
/// Get the tree pattern as a
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// . The rule and token tags from
/// the pattern are present in the parse tree as terminal nodes with a symbol
/// of type
/// <see cref="RuleTagToken"/>
/// or
/// <see cref="TokenTagToken"/>
/// .
/// </summary>
/// <returns>
/// The tree pattern as a
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// .
/// </returns>
[NotNull]
public virtual IParseTree PatternTree
{
get
{
return patternTree;
}
}
}
}