-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserRuleContext.cs
396 lines (352 loc) · 13.5 KB
/
ParserRuleContext.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* 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;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
using Antlr4.Runtime.Tree;
namespace Antlr4.Runtime
{
/// <summary>A rule invocation record for parsing.</summary>
/// <remarks>
/// A rule invocation record for parsing.
/// Contains all of the information about the current rule not stored in the
/// RuleContext. It handles parse tree children list, Any ATN state
/// tracing, and the default values available for rule indications:
/// start, stop, rule index, current alt number, current
/// ATN state.
/// Subclasses made for each rule and grammar track the parameters,
/// return values, locals, and labels specific to that rule. These
/// are the objects that are returned from rules.
/// Note text is not an actual field of a rule return value; it is computed
/// from start and stop using the input stream's toString() method. I
/// could add a ctor to this so that we can pass in and store the input
/// stream, but I'm not sure we want to do that. It would seem to be undefined
/// to get the .text property anyway if the rule matches tokens from multiple
/// input streams.
/// I do not use getters for fields of objects that are used simply to
/// group values such as this aggregate. The getters/setters are there to
/// satisfy the superclass interface.
/// </remarks>
public class ParserRuleContext : RuleContext
{
public static readonly Antlr4.Runtime.ParserRuleContext EMPTY = new Antlr4.Runtime.ParserRuleContext();
/// <summary>
/// If we are debugging or building a parse tree for a visitor,
/// we need to track all of the tokens and rule invocations associated
/// with this rule's context.
/// </summary>
/// <remarks>
/// If we are debugging or building a parse tree for a visitor,
/// we need to track all of the tokens and rule invocations associated
/// with this rule's context. This is empty for parsing w/o tree constr.
/// operation because we don't the need to track the details about
/// how we parse this rule.
/// </remarks>
public IList<IParseTree> children;
/// <summary>
/// For debugging/tracing purposes, we want to track all of the nodes in
/// the ATN traversed by the parser for a particular rule.
/// </summary>
/// <remarks>
/// For debugging/tracing purposes, we want to track all of the nodes in
/// the ATN traversed by the parser for a particular rule.
/// This list indicates the sequence of ATN nodes used to match
/// the elements of the children list. This list does not include
/// ATN nodes and other rules used to match rule invocations. It
/// traces the rule invocation node itself but nothing inside that
/// other rule's ATN submachine.
/// There is NOT a one-to-one correspondence between the children and
/// states list. There are typically many nodes in the ATN traversed
/// for each element in the children list. For example, for a rule
/// invocation there is the invoking state and the following state.
/// The parser setState() method updates field s and adds it to this list
/// if we are debugging/tracing.
/// This does not trace states visited during prediction.
/// </remarks>
private IToken _start;
/// <summary>
/// For debugging/tracing purposes, we want to track all of the nodes in
/// the ATN traversed by the parser for a particular rule.
/// </summary>
/// <remarks>
/// For debugging/tracing purposes, we want to track all of the nodes in
/// the ATN traversed by the parser for a particular rule.
/// This list indicates the sequence of ATN nodes used to match
/// the elements of the children list. This list does not include
/// ATN nodes and other rules used to match rule invocations. It
/// traces the rule invocation node itself but nothing inside that
/// other rule's ATN submachine.
/// There is NOT a one-to-one correspondence between the children and
/// states list. There are typically many nodes in the ATN traversed
/// for each element in the children list. For example, for a rule
/// invocation there is the invoking state and the following state.
/// The parser setState() method updates field s and adds it to this list
/// if we are debugging/tracing.
/// This does not trace states visited during prediction.
/// </remarks>
private IToken _stop;
/// <summary>The exception that forced this rule to return.</summary>
/// <remarks>
/// The exception that forced this rule to return. If the rule successfully
/// completed, this is
/// <see langword="null"/>
/// .
/// </remarks>
public RecognitionException exception;
public ParserRuleContext()
{
}
public static Antlr4.Runtime.ParserRuleContext EmptyContext
{
get
{
// public List<Integer> states;
return EMPTY;
}
}
/// <summary>
/// COPY a ctx (I'm deliberately not using copy constructor) to avoid
/// confusion with creating node with parent. Does not copy children.
///
/// This is used in the generated parser code to flip a generic XContext
/// node for rule X to a YContext for alt label Y. In that sense, it is
/// not really a generic copy function.
///
/// If we do an error sync() at start of a rule, we might add error nodes
/// to the generic XContext so this function must copy those nodes to
/// the YContext as well else they are lost!
/// </summary>
public virtual void CopyFrom(Antlr4.Runtime.ParserRuleContext ctx)
{
// from RuleContext
this.Parent = ctx.Parent;
this.invokingState = ctx.invokingState;
this._start = ctx._start;
this._stop = ctx._stop;
// copy any error nodes to alt label node
if (ctx.children != null)
{
children = new List<IParseTree>();
// reset parent pointer for any error nodes
foreach (var child in ctx.children)
{
var errorChildNode = child as ErrorNodeImpl;
if (errorChildNode != null)
{
children.Add(errorChildNode);
errorChildNode.Parent = this;
}
}
}
}
public ParserRuleContext(Antlr4.Runtime.ParserRuleContext parent, int invokingStateNumber)
: base(parent, invokingStateNumber)
{
}
// Double dispatch methods for listeners
public virtual void EnterRule(IParseTreeListener listener)
{
}
public virtual void ExitRule(IParseTreeListener listener)
{
}
/// <summary>Does not set parent link; other add methods do that</summary>
public virtual void AddChild(ITerminalNode t)
{
if (children == null)
{
children = new List<IParseTree>();
}
children.Add(t);
}
public virtual void AddChild(RuleContext ruleInvocation)
{
if (children == null)
{
children = new List<IParseTree>();
}
children.Add(ruleInvocation);
}
/// <summary>
/// Used by enterOuterAlt to toss out a RuleContext previously added as
/// we entered a rule.
/// </summary>
/// <remarks>
/// Used by enterOuterAlt to toss out a RuleContext previously added as
/// we entered a rule. If we have # label, we will need to remove
/// generic ruleContext object.
/// </remarks>
public virtual void RemoveLastChild()
{
if (children != null)
{
children.RemoveAt(children.Count - 1);
}
}
// public void trace(int s) {
// if ( states==null ) states = new ArrayList<Integer>();
// states.add(s);
// }
public virtual ITerminalNode AddChild(IToken matchedToken)
{
TerminalNodeImpl t = new TerminalNodeImpl(matchedToken);
AddChild(t);
t.Parent = this;
return t;
}
public virtual IErrorNode AddErrorNode(IToken badToken)
{
ErrorNodeImpl t = new ErrorNodeImpl(badToken);
AddChild(t);
t.Parent = this;
return t;
}
public override IParseTree GetChild(int i)
{
return children != null && i >= 0 && i < children.Count ? children[i] : null;
}
public virtual T GetChild<T>(int i)
where T : IParseTree
{
if (children == null || i < 0 || i >= children.Count)
{
return default(T);
}
int j = -1;
// what element have we found with ctxType?
foreach (IParseTree o in children)
{
if (o is T)
{
j++;
if (j == i)
{
return (T) o;
}
}
}
return default(T);
}
public virtual ITerminalNode GetToken(int ttype, int i)
{
if (children == null || i < 0 || i >= children.Count)
{
return null;
}
int j = -1;
// what token with ttype have we found?
foreach (IParseTree o in children)
{
if (o is ITerminalNode)
{
ITerminalNode tnode = (ITerminalNode) o;
IToken symbol = tnode.Symbol;
if (symbol.Type == ttype)
{
j++;
if (j == i)
{
return tnode;
}
}
}
}
return null;
}
public virtual ITerminalNode[] GetTokens(int ttype)
{
if (children == null)
{
return Collections.EmptyList<ITerminalNode>();
}
List<ITerminalNode> tokens = null;
foreach (IParseTree o in children)
{
if (o is ITerminalNode)
{
ITerminalNode tnode = (ITerminalNode) o;
IToken symbol = tnode.Symbol;
if (symbol.Type == ttype)
{
if (tokens == null)
{
tokens = new List<ITerminalNode>();
}
tokens.Add(tnode);
}
}
}
if (tokens == null)
{
return Collections.EmptyList<ITerminalNode>();
}
return tokens.ToArray();
}
public virtual T GetRuleContext<T>(int i)
where T : Antlr4.Runtime.ParserRuleContext
{
return GetChild<T>(i);
}
public virtual T[] GetRuleContexts<T>()
where T : Antlr4.Runtime.ParserRuleContext
{
if (children == null)
{
return Collections.EmptyList<T>();
}
List<T> contexts = null;
foreach (IParseTree o in children)
{
if (o is T)
{
if (contexts == null)
{
contexts = new List<T>();
}
contexts.Add((T) o);
}
}
if (contexts == null)
{
return Collections.EmptyList<T>();
}
return contexts.ToArray();
}
public override int ChildCount
{
get { return children != null ? children.Count : 0; }
}
public override Interval SourceInterval
{
get
{
if (_start == null || _stop == null)
{
return Interval.Invalid;
}
return Interval.Of(_start.TokenIndex, _stop.TokenIndex);
}
}
public virtual IToken Start
{
get { return _start; }
set { _start = value; }
}
public virtual IToken Stop
{
get { return _stop; }
set { _stop = value; }
}
/// <summary>Used for rule context info debugging during parse-time, not so much for ATN debugging</summary>
public virtual string ToInfoString(Parser recognizer)
{
List<string> rules = new List<string>(recognizer.GetRuleInvocationStack(this));
rules.Reverse();
return "ParserRuleContext" + rules + "{" + "start=" + _start + ", stop=" + _stop + '}';
}
}
}