UNPKG

11 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2016 The ANTLR Project. All rights reserved.
4 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
5 */
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.ParserRuleContext = void 0;
14// ConvertTo-TS run at 2016-10-04T11:26:56.6285494-07:00
15const ErrorNode_1 = require("./tree/ErrorNode");
16const Interval_1 = require("./misc/Interval");
17const Decorators_1 = require("./Decorators");
18const RuleContext_1 = require("./RuleContext");
19const TerminalNode_1 = require("./tree/TerminalNode");
20/** A rule invocation record for parsing.
21 *
22 * Contains all of the information about the current rule not stored in the
23 * RuleContext. It handles parse tree children list, Any ATN state
24 * tracing, and the default values available for rule invocations:
25 * start, stop, rule index, current alt number.
26 *
27 * Subclasses made for each rule and grammar track the parameters,
28 * return values, locals, and labels specific to that rule. These
29 * are the objects that are returned from rules.
30 *
31 * Note text is not an actual field of a rule return value; it is computed
32 * from start and stop using the input stream's toString() method. I
33 * could add a ctor to this so that we can pass in and store the input
34 * stream, but I'm not sure we want to do that. It would seem to be undefined
35 * to get the .text property anyway if the rule matches tokens from multiple
36 * input streams.
37 *
38 * I do not use getters for fields of objects that are used simply to
39 * group values such as this aggregate. The getters/setters are there to
40 * satisfy the superclass interface.
41 */
42class ParserRuleContext extends RuleContext_1.RuleContext {
43 constructor(parent, invokingStateNumber) {
44 if (invokingStateNumber == null) {
45 super();
46 }
47 else {
48 super(parent, invokingStateNumber);
49 }
50 }
51 static emptyContext() {
52 return ParserRuleContext.EMPTY;
53 }
54 /**
55 * COPY a ctx (I'm deliberately not using copy constructor) to avoid
56 * confusion with creating node with parent. Does not copy children
57 * (except error leaves).
58 *
59 * This is used in the generated parser code to flip a generic XContext
60 * node for rule X to a YContext for alt label Y. In that sense, it is not
61 * really a generic copy function.
62 *
63 * If we do an error sync() at start of a rule, we might add error nodes
64 * to the generic XContext so this function must copy those nodes to the
65 * YContext as well else they are lost!
66 */
67 copyFrom(ctx) {
68 this._parent = ctx._parent;
69 this.invokingState = ctx.invokingState;
70 this._start = ctx._start;
71 this._stop = ctx._stop;
72 // copy any error nodes to alt label node
73 if (ctx.children) {
74 this.children = [];
75 // reset parent pointer for any error nodes
76 for (let child of ctx.children) {
77 if (child instanceof ErrorNode_1.ErrorNode) {
78 this.addChild(child);
79 }
80 }
81 }
82 }
83 // Double dispatch methods for listeners
84 enterRule(listener) {
85 // intentionally empty
86 }
87 exitRule(listener) {
88 // intentionally empty
89 }
90 /** Add a parse tree node to this as a child. Works for
91 * internal and leaf nodes. Does not set parent link;
92 * other add methods must do that. Other addChild methods
93 * call this.
94 *
95 * We cannot set the parent pointer of the incoming node
96 * because the existing interfaces do not have a setParent()
97 * method and I don't want to break backward compatibility for this.
98 *
99 * @since 4.7
100 */
101 addAnyChild(t) {
102 if (!this.children) {
103 this.children = [t];
104 }
105 else {
106 this.children.push(t);
107 }
108 return t;
109 }
110 addChild(t) {
111 let result;
112 if (t instanceof TerminalNode_1.TerminalNode) {
113 t.setParent(this);
114 this.addAnyChild(t);
115 return;
116 }
117 else if (t instanceof RuleContext_1.RuleContext) {
118 // Does not set parent link
119 this.addAnyChild(t);
120 return;
121 }
122 else {
123 // Deprecated code path
124 t = new TerminalNode_1.TerminalNode(t);
125 this.addAnyChild(t);
126 t.setParent(this);
127 return t;
128 }
129 }
130 addErrorNode(node) {
131 if (node instanceof ErrorNode_1.ErrorNode) {
132 const errorNode = node;
133 errorNode.setParent(this);
134 return this.addAnyChild(errorNode);
135 }
136 else {
137 // deprecated path
138 const badToken = node;
139 let t = new ErrorNode_1.ErrorNode(badToken);
140 this.addAnyChild(t);
141 t.setParent(this);
142 return t;
143 }
144 }
145 // public void trace(int s) {
146 // if ( states==null ) states = new ArrayList<Integer>();
147 // states.add(s);
148 // }
149 /** Used by enterOuterAlt to toss out a RuleContext previously added as
150 * we entered a rule. If we have # label, we will need to remove
151 * generic ruleContext object.
152 */
153 removeLastChild() {
154 if (this.children) {
155 this.children.pop();
156 }
157 }
158 get parent() {
159 let parent = super.parent;
160 if (parent === undefined || parent instanceof ParserRuleContext) {
161 return parent;
162 }
163 throw new TypeError("Invalid parent type for ParserRuleContext");
164 }
165 // Note: in TypeScript, order or arguments reversed
166 getChild(i, ctxType) {
167 if (!this.children || i < 0 || i >= this.children.length) {
168 throw new RangeError("index parameter must be between >= 0 and <= number of children.");
169 }
170 if (ctxType == null) {
171 return this.children[i];
172 }
173 let result = this.tryGetChild(i, ctxType);
174 if (result === undefined) {
175 throw new Error("The specified node does not exist");
176 }
177 return result;
178 }
179 tryGetChild(i, ctxType) {
180 if (!this.children || i < 0 || i >= this.children.length) {
181 return undefined;
182 }
183 let j = -1; // what node with ctxType have we found?
184 for (let o of this.children) {
185 if (o instanceof ctxType) {
186 j++;
187 if (j === i) {
188 return o;
189 }
190 }
191 }
192 return undefined;
193 }
194 getToken(ttype, i) {
195 let result = this.tryGetToken(ttype, i);
196 if (result === undefined) {
197 throw new Error("The specified token does not exist");
198 }
199 return result;
200 }
201 tryGetToken(ttype, i) {
202 if (!this.children || i < 0 || i >= this.children.length) {
203 return undefined;
204 }
205 let j = -1; // what token with ttype have we found?
206 for (let o of this.children) {
207 if (o instanceof TerminalNode_1.TerminalNode) {
208 let symbol = o.symbol;
209 if (symbol.type === ttype) {
210 j++;
211 if (j === i) {
212 return o;
213 }
214 }
215 }
216 }
217 return undefined;
218 }
219 getTokens(ttype) {
220 let tokens = [];
221 if (!this.children) {
222 return tokens;
223 }
224 for (let o of this.children) {
225 if (o instanceof TerminalNode_1.TerminalNode) {
226 let symbol = o.symbol;
227 if (symbol.type === ttype) {
228 tokens.push(o);
229 }
230 }
231 }
232 return tokens;
233 }
234 get ruleContext() {
235 return this;
236 }
237 // NOTE: argument order change from Java version
238 getRuleContext(i, ctxType) {
239 return this.getChild(i, ctxType);
240 }
241 tryGetRuleContext(i, ctxType) {
242 return this.tryGetChild(i, ctxType);
243 }
244 getRuleContexts(ctxType) {
245 let contexts = [];
246 if (!this.children) {
247 return contexts;
248 }
249 for (let o of this.children) {
250 if (o instanceof ctxType) {
251 contexts.push(o);
252 }
253 }
254 return contexts;
255 }
256 get childCount() {
257 return this.children ? this.children.length : 0;
258 }
259 get sourceInterval() {
260 if (!this._start) {
261 return Interval_1.Interval.INVALID;
262 }
263 if (!this._stop || this._stop.tokenIndex < this._start.tokenIndex) {
264 return Interval_1.Interval.of(this._start.tokenIndex, this._start.tokenIndex - 1); // empty
265 }
266 return Interval_1.Interval.of(this._start.tokenIndex, this._stop.tokenIndex);
267 }
268 /**
269 * Get the initial token in this context.
270 * Note that the range from start to stop is inclusive, so for rules that do not consume anything
271 * (for example, zero length or error productions) this token may exceed stop.
272 */
273 get start() { return this._start; }
274 /**
275 * Get the final token in this context.
276 * Note that the range from start to stop is inclusive, so for rules that do not consume anything
277 * (for example, zero length or error productions) this token may precede start.
278 */
279 get stop() { return this._stop; }
280 /** Used for rule context info debugging during parse-time, not so much for ATN debugging */
281 toInfoString(recognizer) {
282 let rules = recognizer.getRuleInvocationStack(this).reverse();
283 return "ParserRuleContext" + rules + "{" +
284 "start=" + this._start +
285 ", stop=" + this._stop +
286 "}";
287 }
288}
289ParserRuleContext.EMPTY = new ParserRuleContext();
290__decorate([
291 Decorators_1.Override
292], ParserRuleContext.prototype, "parent", null);
293__decorate([
294 Decorators_1.Override
295], ParserRuleContext.prototype, "childCount", null);
296__decorate([
297 Decorators_1.Override
298], ParserRuleContext.prototype, "sourceInterval", null);
299exports.ParserRuleContext = ParserRuleContext;
300//# sourceMappingURL=ParserRuleContext.js.map
\No newline at end of file