UNPKG

5.44 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 { Parser } from "./Parser";
6import { Recognizer } from "./Recognizer";
7import { RuleNode } from "./tree/RuleNode";
8import { ParseTree } from "./tree/ParseTree";
9import { Interval } from "./misc/Interval";
10import { ParseTreeVisitor } from "./tree/ParseTreeVisitor";
11/** A rule context is a record of a single rule invocation.
12 *
13 * We form a stack of these context objects using the parent
14 * pointer. A parent pointer of `undefined` indicates that the current
15 * context is the bottom of the stack. The ParserRuleContext subclass
16 * as a children list so that we can turn this data structure into a
17 * tree.
18 *
19 * The root node always has a `undefined` pointer and invokingState of -1.
20 *
21 * Upon entry to parsing, the first invoked rule function creates a
22 * context object (a subclass specialized for that rule such as
23 * SContext) and makes it the root of a parse tree, recorded by field
24 * Parser._ctx.
25 *
26 * public final SContext s() throws RecognitionException {
27 * SContext _localctx = new SContext(_ctx, state); <-- create new node
28 * enterRule(_localctx, 0, RULE_s); <-- push it
29 * ...
30 * exitRule(); <-- pop back to _localctx
31 * return _localctx;
32 * }
33 *
34 * A subsequent rule invocation of r from the start rule s pushes a
35 * new context object for r whose parent points at s and use invoking
36 * state is the state with r emanating as edge label.
37 *
38 * The invokingState fields from a context object to the root
39 * together form a stack of rule indication states where the root
40 * (bottom of the stack) has a -1 sentinel value. If we invoke start
41 * symbol s then call r1, which calls r2, the would look like
42 * this:
43 *
44 * SContext[-1] <- root node (bottom of the stack)
45 * R1Context[p] <- p in rule s called r1
46 * R2Context[q] <- q in rule r1 called r2
47 *
48 * So the top of the stack, _ctx, represents a call to the current
49 * rule and it holds the return address from another rule that invoke
50 * to this rule. To invoke a rule, we must always have a current context.
51 *
52 * The parent contexts are useful for computing lookahead sets and
53 * getting error information.
54 *
55 * These objects are used during parsing and prediction.
56 * For the special case of parsers, we use the subclass
57 * ParserRuleContext.
58 *
59 * @see ParserRuleContext
60 */
61export declare class RuleContext extends RuleNode {
62 _parent: RuleContext | undefined;
63 invokingState: number;
64 constructor();
65 constructor(parent: RuleContext | undefined, invokingState: number);
66 static getChildContext(parent: RuleContext, invokingState: number): RuleContext;
67 depth(): number;
68 /** A context is empty if there is no invoking state; meaning nobody called
69 * current context.
70 */
71 get isEmpty(): boolean;
72 get sourceInterval(): Interval;
73 get ruleContext(): RuleContext;
74 get parent(): RuleContext | undefined;
75 /** @since 4.7. {@see ParseTree#setParent} comment */
76 setParent(parent: RuleContext): void;
77 get payload(): RuleContext;
78 /** Return the combined text of all child nodes. This method only considers
79 * tokens which have been added to the parse tree.
80 *
81 * Since tokens on hidden channels (e.g. whitespace or comments) are not
82 * added to the parse trees, they will not appear in the output of this
83 * method.
84 */
85 get text(): string;
86 get ruleIndex(): number;
87 /** For rule associated with this parse tree internal node, return
88 * the outer alternative number used to match the input. Default
89 * implementation does not compute nor store this alt num. Create
90 * a subclass of ParserRuleContext with backing field and set
91 * option contextSuperClass.
92 * to set it.
93 *
94 * @since 4.5.3
95 */
96 get altNumber(): number;
97 /** Set the outer alternative number for this context node. Default
98 * implementation does nothing to avoid backing field overhead for
99 * trees that don't need it. Create
100 * a subclass of ParserRuleContext with backing field and set
101 * option contextSuperClass.
102 *
103 * @since 4.5.3
104 */
105 set altNumber(altNumber: number);
106 getChild(i: number): ParseTree;
107 get childCount(): number;
108 accept<T>(visitor: ParseTreeVisitor<T>): T;
109 /** Print out a whole tree, not just a node, in LISP format
110 * (root child1 .. childN). Print just a node if this is a leaf.
111 * We have to know the recognizer so we can get rule names.
112 */
113 toStringTree(recog: Parser): string;
114 /** Print out a whole tree, not just a node, in LISP format
115 * (root child1 .. childN). Print just a node if this is a leaf.
116 */
117 toStringTree(ruleNames: string[] | undefined): string;
118 toStringTree(): string;
119 toString(): string;
120 toString(recog: Recognizer<any, any> | undefined): string;
121 toString(ruleNames: string[] | undefined): string;
122 toString(recog: Recognizer<any, any> | undefined, stop: RuleContext | undefined): string;
123 toString(ruleNames: string[] | undefined, stop: RuleContext | undefined): string;
124}