UNPKG

7.76 kBTypeScriptView Raw
1/*!
2 * Copyright 2016 The ANTLR Project. All rights reserved.
3 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
4 */
5import { ErrorNode } from "./tree/ErrorNode";
6import { Interval } from "./misc/Interval";
7import { Parser } from "./Parser";
8import { ParseTree } from "./tree/ParseTree";
9import { ParseTreeListener } from "./tree/ParseTreeListener";
10import { RecognitionException } from "./RecognitionException";
11import { RuleContext } from "./RuleContext";
12import { TerminalNode } from "./tree/TerminalNode";
13import { Token } from "./Token";
14/** A rule invocation record for parsing.
15 *
16 * Contains all of the information about the current rule not stored in the
17 * RuleContext. It handles parse tree children list, Any ATN state
18 * tracing, and the default values available for rule invocations:
19 * start, stop, rule index, current alt number.
20 *
21 * Subclasses made for each rule and grammar track the parameters,
22 * return values, locals, and labels specific to that rule. These
23 * are the objects that are returned from rules.
24 *
25 * Note text is not an actual field of a rule return value; it is computed
26 * from start and stop using the input stream's toString() method. I
27 * could add a ctor to this so that we can pass in and store the input
28 * stream, but I'm not sure we want to do that. It would seem to be undefined
29 * to get the .text property anyway if the rule matches tokens from multiple
30 * input streams.
31 *
32 * I do not use getters for fields of objects that are used simply to
33 * group values such as this aggregate. The getters/setters are there to
34 * satisfy the superclass interface.
35 */
36export declare class ParserRuleContext extends RuleContext {
37 private static readonly EMPTY;
38 /** If we are debugging or building a parse tree for a visitor,
39 * we need to track all of the tokens and rule invocations associated
40 * with this rule's context. This is empty for parsing w/o tree constr.
41 * operation because we don't the need to track the details about
42 * how we parse this rule.
43 */
44 children?: ParseTree[];
45 /** For debugging/tracing purposes, we want to track all of the nodes in
46 * the ATN traversed by the parser for a particular rule.
47 * This list indicates the sequence of ATN nodes used to match
48 * the elements of the children list. This list does not include
49 * ATN nodes and other rules used to match rule invocations. It
50 * traces the rule invocation node itself but nothing inside that
51 * other rule's ATN submachine.
52 *
53 * There is NOT a one-to-one correspondence between the children and
54 * states list. There are typically many nodes in the ATN traversed
55 * for each element in the children list. For example, for a rule
56 * invocation there is the invoking state and the following state.
57 *
58 * The parser state property updates field s and adds it to this list
59 * if we are debugging/tracing.
60 *
61 * This does not trace states visited during prediction.
62 */
63 _start: Token;
64 _stop: Token | undefined;
65 /**
66 * The exception that forced this rule to return. If the rule successfully
67 * completed, this is `undefined`.
68 */
69 exception?: RecognitionException;
70 constructor();
71 constructor(parent: ParserRuleContext | undefined, invokingStateNumber: number);
72 static emptyContext(): ParserRuleContext;
73 /**
74 * COPY a ctx (I'm deliberately not using copy constructor) to avoid
75 * confusion with creating node with parent. Does not copy children
76 * (except error leaves).
77 *
78 * This is used in the generated parser code to flip a generic XContext
79 * node for rule X to a YContext for alt label Y. In that sense, it is not
80 * really a generic copy function.
81 *
82 * If we do an error sync() at start of a rule, we might add error nodes
83 * to the generic XContext so this function must copy those nodes to the
84 * YContext as well else they are lost!
85 */
86 copyFrom(ctx: ParserRuleContext): void;
87 enterRule(listener: ParseTreeListener): void;
88 exitRule(listener: ParseTreeListener): void;
89 /** Add a parse tree node to this as a child. Works for
90 * internal and leaf nodes. Does not set parent link;
91 * other add methods must do that. Other addChild methods
92 * call this.
93 *
94 * We cannot set the parent pointer of the incoming node
95 * because the existing interfaces do not have a setParent()
96 * method and I don't want to break backward compatibility for this.
97 *
98 * @since 4.7
99 */
100 addAnyChild<T extends ParseTree>(t: T): T;
101 /** Add a token leaf node child and force its parent to be this node. */
102 addChild(t: TerminalNode): void;
103 addChild(ruleInvocation: RuleContext): void;
104 /**
105 * Add a child to this node based upon matchedToken. It
106 * creates a TerminalNodeImpl rather than using
107 * {@link Parser#createTerminalNode(ParserRuleContext, Token)}. I'm leaving this
108 * in for compatibility but the parser doesn't use this anymore.
109 *
110 * @deprecated Use another overload instead.
111 */
112 addChild(matchedToken: Token): TerminalNode;
113 /** Add an error node child and force its parent to be this node.
114 *
115 * @since 4.7
116 */
117 addErrorNode(errorNode: ErrorNode): ErrorNode;
118 /**
119 * Add a child to this node based upon badToken. It
120 * creates a ErrorNode rather than using
121 * {@link Parser#createErrorNode(ParserRuleContext, Token)}. I'm leaving this
122 * in for compatibility but the parser doesn't use this anymore.
123 *
124 * @deprecated Use another overload instead.
125 */
126 addErrorNode(badToken: Token): ErrorNode;
127 /** Used by enterOuterAlt to toss out a RuleContext previously added as
128 * we entered a rule. If we have # label, we will need to remove
129 * generic ruleContext object.
130 */
131 removeLastChild(): void;
132 get parent(): ParserRuleContext | undefined;
133 getChild(i: number): ParseTree;
134 getChild<T extends ParseTree>(i: number, ctxType: {
135 new (...args: any[]): T;
136 }): T;
137 tryGetChild<T extends ParseTree>(i: number, ctxType: {
138 new (...args: any[]): T;
139 }): T | undefined;
140 getToken(ttype: number, i: number): TerminalNode;
141 tryGetToken(ttype: number, i: number): TerminalNode | undefined;
142 getTokens(ttype: number): TerminalNode[];
143 get ruleContext(): this;
144 getRuleContext<T extends ParserRuleContext>(i: number, ctxType: {
145 new (...args: any[]): T;
146 }): T;
147 tryGetRuleContext<T extends ParserRuleContext>(i: number, ctxType: {
148 new (...args: any[]): T;
149 }): T | undefined;
150 getRuleContexts<T extends ParserRuleContext>(ctxType: {
151 new (...args: any[]): T;
152 }): T[];
153 get childCount(): number;
154 get sourceInterval(): Interval;
155 /**
156 * Get the initial token in this context.
157 * Note that the range from start to stop is inclusive, so for rules that do not consume anything
158 * (for example, zero length or error productions) this token may exceed stop.
159 */
160 get start(): Token;
161 /**
162 * Get the final token in this context.
163 * Note that the range from start to stop is inclusive, so for rules that do not consume anything
164 * (for example, zero length or error productions) this token may precede start.
165 */
166 get stop(): Token | undefined;
167 /** Used for rule context info debugging during parse-time, not so much for ATN debugging */
168 toInfoString(recognizer: Parser): string;
169}