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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* 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;
using System.Collections.Generic;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
using Antlr4.Runtime.Tree;
using Antlr4.Runtime.Tree.Pattern;
namespace Antlr4.Runtime.Tree.Pattern
{
/// <summary>
/// Represents the result of matching a
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// against a tree pattern.
/// </summary>
public class ParseTreeMatch
{
/// <summary>
/// This is the backing field for
/// <see cref="Tree()"/>
/// .
/// </summary>
private readonly IParseTree tree;
/// <summary>
/// This is the backing field for
/// <see cref="Pattern()"/>
/// .
/// </summary>
private readonly ParseTreePattern pattern;
/// <summary>
/// This is the backing field for
/// <see cref="Labels()"/>
/// .
/// </summary>
private readonly MultiMap<string, IParseTree> labels;
/// <summary>
/// This is the backing field for
/// <see cref="MismatchedNode()"/>
/// .
/// </summary>
private readonly IParseTree mismatchedNode;
/// <summary>
/// Constructs a new instance of
/// <see cref="ParseTreeMatch"/>
/// from the specified
/// parse tree and pattern.
/// </summary>
/// <param name="tree">The parse tree to match against the pattern.</param>
/// <param name="pattern">The parse tree pattern.</param>
/// <param name="labels">
/// A mapping from label names to collections of
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// objects located by the tree pattern matching process.
/// </param>
/// <param name="mismatchedNode">
/// The first node which failed to match the tree
/// pattern during the matching process.
/// </param>
/// <exception>
/// IllegalArgumentException
/// if
/// <paramref name="tree"/>
/// is
/// <see langword="null"/>
/// </exception>
/// <exception>
/// IllegalArgumentException
/// if
/// <paramref name="pattern"/>
/// is
/// <see langword="null"/>
/// </exception>
/// <exception>
/// IllegalArgumentException
/// if
/// <paramref name="labels"/>
/// is
/// <see langword="null"/>
/// </exception>
public ParseTreeMatch(IParseTree tree, ParseTreePattern pattern, MultiMap<string, IParseTree> labels, IParseTree mismatchedNode)
{
if (tree == null)
{
throw new ArgumentException("tree cannot be null");
}
if (pattern == null)
{
throw new ArgumentException("pattern cannot be null");
}
if (labels == null)
{
throw new ArgumentException("labels cannot be null");
}
this.tree = tree;
this.pattern = pattern;
this.labels = labels;
this.mismatchedNode = mismatchedNode;
}
/// <summary>
/// Get the last node associated with a specific
/// <paramref name="label"/>
/// .
/// <p>For example, for pattern
/// <c><id:ID></c>
/// ,
/// <c>get("id")</c>
/// returns the
/// node matched for that
/// <c>ID</c>
/// . If more than one node
/// matched the specified label, only the last is returned. If there is
/// no node associated with the label, this returns
/// <see langword="null"/>
/// .</p>
/// <p>Pattern tags like
/// <c><ID></c>
/// and
/// <c><expr></c>
/// without labels are
/// considered to be labeled with
/// <c>ID</c>
/// and
/// <c>expr</c>
/// , respectively.</p>
/// </summary>
/// <param name="label">The label to check.</param>
/// <returns>
/// The last
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// to match a tag with the specified
/// label, or
/// <see langword="null"/>
/// if no parse tree matched a tag with the label.
/// </returns>
[return: Nullable]
public virtual IParseTree Get(string label)
{
IList<IParseTree> parseTrees = labels.Get(label);
if (parseTrees == null || parseTrees.Count == 0)
{
return null;
}
return parseTrees[parseTrees.Count - 1];
}
// return last if multiple
/// <summary>Return all nodes matching a rule or token tag with the specified label.</summary>
/// <remarks>
/// Return all nodes matching a rule or token tag with the specified label.
/// <p>If the
/// <paramref name="label"/>
/// is the name of a parser rule or token in the
/// grammar, the resulting list will contain both the parse trees matching
/// rule or tags explicitly labeled with the label and the complete set of
/// parse trees matching the labeled and unlabeled tags in the pattern for
/// the parser rule or token. For example, if
/// <paramref name="label"/>
/// is
/// <c>"foo"</c>
/// ,
/// the result will contain <em>all</em> of the following.</p>
/// <ul>
/// <li>Parse tree nodes matching tags of the form
/// <c><foo:anyRuleName></c>
/// and
/// <c><foo:AnyTokenName></c>
/// .</li>
/// <li>Parse tree nodes matching tags of the form
/// <c><anyLabel:foo></c>
/// .</li>
/// <li>Parse tree nodes matching tags of the form
/// <c><foo></c>
/// .</li>
/// </ul>
/// </remarks>
/// <param name="label">The label.</param>
/// <returns>
/// A collection of all
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// nodes matching tags with
/// the specified
/// <paramref name="label"/>
/// . If no nodes matched the label, an empty list
/// is returned.
/// </returns>
[return: NotNull]
public virtual IList<IParseTree> GetAll(string label)
{
IList<IParseTree> nodes = labels.Get(label);
if (nodes == null)
{
return Sharpen.Collections.EmptyList<IParseTree>();
}
return nodes;
}
/// <summary>Return a mapping from label → [list of nodes].</summary>
/// <remarks>
/// Return a mapping from label → [list of nodes].
/// <p>The map includes special entries corresponding to the names of rules and
/// tokens referenced in tags in the original pattern. For additional
/// information, see the description of
/// <see cref="GetAll(string)"/>
/// .</p>
/// </remarks>
/// <returns>
/// A mapping from labels to parse tree nodes. If the parse tree
/// pattern did not contain any rule or token tags, this map will be empty.
/// </returns>
[NotNull]
public virtual MultiMap<string, IParseTree> Labels
{
get
{
return labels;
}
}
/// <summary>Get the node at which we first detected a mismatch.</summary>
/// <remarks>Get the node at which we first detected a mismatch.</remarks>
/// <returns>
/// the node at which we first detected a mismatch, or
/// <see langword="null"/>
/// if the match was successful.
/// </returns>
[Nullable]
public virtual IParseTree MismatchedNode
{
get
{
return mismatchedNode;
}
}
/// <summary>Gets a value indicating whether the match operation succeeded.</summary>
/// <remarks>Gets a value indicating whether the match operation succeeded.</remarks>
/// <returns>
///
/// <see langword="true"/>
/// if the match operation succeeded; otherwise,
/// <see langword="false"/>
/// .
/// </returns>
public virtual bool Succeeded
{
get
{
return mismatchedNode == null;
}
}
/// <summary>Get the tree pattern we are matching against.</summary>
/// <remarks>Get the tree pattern we are matching against.</remarks>
/// <returns>The tree pattern we are matching against.</returns>
[NotNull]
public virtual ParseTreePattern Pattern
{
get
{
return pattern;
}
}
/// <summary>Get the parse tree we are trying to match to a pattern.</summary>
/// <remarks>Get the parse tree we are trying to match to a pattern.</remarks>
/// <returns>
/// The
/// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
/// we are trying to match to a pattern.
/// </returns>
[NotNull]
public virtual IParseTree Tree
{
get
{
return tree;
}
}
/// <summary><inheritDoc/></summary>
public override string ToString()
{
return string.Format("Match {0}; found {1} labels", Succeeded ? "succeeded" : "failed", Labels.Count);
}
}
}