UNPKG

8.21 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.RuleContext = void 0;
14// ConvertTo-TS run at 2016-10-04T11:26:57.3490837-07:00
15const ATN_1 = require("./atn/ATN");
16const Recognizer_1 = require("./Recognizer");
17const RuleNode_1 = require("./tree/RuleNode");
18const Interval_1 = require("./misc/Interval");
19const Decorators_1 = require("./Decorators");
20const Trees_1 = require("./tree/Trees");
21const ParserRuleContext_1 = require("./ParserRuleContext");
22/** A rule context is a record of a single rule invocation.
23 *
24 * We form a stack of these context objects using the parent
25 * pointer. A parent pointer of `undefined` indicates that the current
26 * context is the bottom of the stack. The ParserRuleContext subclass
27 * as a children list so that we can turn this data structure into a
28 * tree.
29 *
30 * The root node always has a `undefined` pointer and invokingState of -1.
31 *
32 * Upon entry to parsing, the first invoked rule function creates a
33 * context object (a subclass specialized for that rule such as
34 * SContext) and makes it the root of a parse tree, recorded by field
35 * Parser._ctx.
36 *
37 * public final SContext s() throws RecognitionException {
38 * SContext _localctx = new SContext(_ctx, state); <-- create new node
39 * enterRule(_localctx, 0, RULE_s); <-- push it
40 * ...
41 * exitRule(); <-- pop back to _localctx
42 * return _localctx;
43 * }
44 *
45 * A subsequent rule invocation of r from the start rule s pushes a
46 * new context object for r whose parent points at s and use invoking
47 * state is the state with r emanating as edge label.
48 *
49 * The invokingState fields from a context object to the root
50 * together form a stack of rule indication states where the root
51 * (bottom of the stack) has a -1 sentinel value. If we invoke start
52 * symbol s then call r1, which calls r2, the would look like
53 * this:
54 *
55 * SContext[-1] <- root node (bottom of the stack)
56 * R1Context[p] <- p in rule s called r1
57 * R2Context[q] <- q in rule r1 called r2
58 *
59 * So the top of the stack, _ctx, represents a call to the current
60 * rule and it holds the return address from another rule that invoke
61 * to this rule. To invoke a rule, we must always have a current context.
62 *
63 * The parent contexts are useful for computing lookahead sets and
64 * getting error information.
65 *
66 * These objects are used during parsing and prediction.
67 * For the special case of parsers, we use the subclass
68 * ParserRuleContext.
69 *
70 * @see ParserRuleContext
71 */
72class RuleContext extends RuleNode_1.RuleNode {
73 constructor(parent, invokingState) {
74 super();
75 this._parent = parent;
76 this.invokingState = invokingState != null ? invokingState : -1;
77 }
78 static getChildContext(parent, invokingState) {
79 return new RuleContext(parent, invokingState);
80 }
81 depth() {
82 let n = 0;
83 let p = this;
84 while (p) {
85 p = p._parent;
86 n++;
87 }
88 return n;
89 }
90 /** A context is empty if there is no invoking state; meaning nobody called
91 * current context.
92 */
93 get isEmpty() {
94 return this.invokingState === -1;
95 }
96 // satisfy the ParseTree / SyntaxTree interface
97 get sourceInterval() {
98 return Interval_1.Interval.INVALID;
99 }
100 get ruleContext() { return this; }
101 get parent() { return this._parent; }
102 /** @since 4.7. {@see ParseTree#setParent} comment */
103 setParent(parent) {
104 this._parent = parent;
105 }
106 get payload() { return this; }
107 /** Return the combined text of all child nodes. This method only considers
108 * tokens which have been added to the parse tree.
109 *
110 * Since tokens on hidden channels (e.g. whitespace or comments) are not
111 * added to the parse trees, they will not appear in the output of this
112 * method.
113 */
114 get text() {
115 if (this.childCount === 0) {
116 return "";
117 }
118 let builder = "";
119 for (let i = 0; i < this.childCount; i++) {
120 builder += this.getChild(i).text;
121 }
122 return builder.toString();
123 }
124 get ruleIndex() { return -1; }
125 /** For rule associated with this parse tree internal node, return
126 * the outer alternative number used to match the input. Default
127 * implementation does not compute nor store this alt num. Create
128 * a subclass of ParserRuleContext with backing field and set
129 * option contextSuperClass.
130 * to set it.
131 *
132 * @since 4.5.3
133 */
134 get altNumber() { return ATN_1.ATN.INVALID_ALT_NUMBER; }
135 /** Set the outer alternative number for this context node. Default
136 * implementation does nothing to avoid backing field overhead for
137 * trees that don't need it. Create
138 * a subclass of ParserRuleContext with backing field and set
139 * option contextSuperClass.
140 *
141 * @since 4.5.3
142 */
143 set altNumber(altNumber) {
144 // intentionally ignored by the base implementation
145 }
146 getChild(i) {
147 throw new RangeError("i must be greater than or equal to 0 and less than childCount");
148 }
149 get childCount() {
150 return 0;
151 }
152 accept(visitor) {
153 return visitor.visitChildren(this);
154 }
155 toStringTree(recog) {
156 return Trees_1.Trees.toStringTree(this, recog);
157 }
158 toString(arg1, stop) {
159 const ruleNames = (arg1 instanceof Recognizer_1.Recognizer) ? arg1.ruleNames : arg1;
160 stop = stop || ParserRuleContext_1.ParserRuleContext.emptyContext();
161 let buf = "";
162 let p = this;
163 buf += ("[");
164 while (p && p !== stop) {
165 if (!ruleNames) {
166 if (!p.isEmpty) {
167 buf += (p.invokingState);
168 }
169 }
170 else {
171 let ruleIndex = p.ruleIndex;
172 let ruleName = (ruleIndex >= 0 && ruleIndex < ruleNames.length)
173 ? ruleNames[ruleIndex] : ruleIndex.toString();
174 buf += (ruleName);
175 }
176 if (p._parent && (ruleNames || !p._parent.isEmpty)) {
177 buf += (" ");
178 }
179 p = p._parent;
180 }
181 buf += ("]");
182 return buf.toString();
183 }
184}
185__decorate([
186 Decorators_1.Override
187], RuleContext.prototype, "sourceInterval", null);
188__decorate([
189 Decorators_1.Override
190], RuleContext.prototype, "ruleContext", null);
191__decorate([
192 Decorators_1.Override
193], RuleContext.prototype, "parent", null);
194__decorate([
195 Decorators_1.Override
196], RuleContext.prototype, "setParent", null);
197__decorate([
198 Decorators_1.Override
199], RuleContext.prototype, "payload", null);
200__decorate([
201 Decorators_1.Override
202], RuleContext.prototype, "text", null);
203__decorate([
204 Decorators_1.Override
205], RuleContext.prototype, "getChild", null);
206__decorate([
207 Decorators_1.Override
208], RuleContext.prototype, "childCount", null);
209__decorate([
210 Decorators_1.Override
211], RuleContext.prototype, "accept", null);
212__decorate([
213 Decorators_1.Override
214], RuleContext.prototype, "toStringTree", null);
215exports.RuleContext = RuleContext;
216//# sourceMappingURL=RuleContext.js.map
\No newline at end of file