UNPKG

7.42 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 { ATN } from "./atn/ATN";
6import { ATNState } from "./atn/ATNState";
7import { BitSet } from "./misc/BitSet";
8import { DecisionState } from "./atn/DecisionState";
9import { InterpreterRuleContext } from "./InterpreterRuleContext";
10import { Parser } from "./Parser";
11import { ParserRuleContext } from "./ParserRuleContext";
12import { RecognitionException } from "./RecognitionException";
13import { Token } from "./Token";
14import { TokenStream } from "./TokenStream";
15import { Vocabulary } from "./Vocabulary";
16/** A parser simulator that mimics what ANTLR's generated
17 * parser code does. A ParserATNSimulator is used to make
18 * predictions via adaptivePredict but this class moves a pointer through the
19 * ATN to simulate parsing. ParserATNSimulator just
20 * makes us efficient rather than having to backtrack, for example.
21 *
22 * This properly creates parse trees even for left recursive rules.
23 *
24 * We rely on the left recursive rule invocation and special predicate
25 * transitions to make left recursive rules work.
26 *
27 * See TestParserInterpreter for examples.
28 */
29export declare class ParserInterpreter extends Parser {
30 protected _grammarFileName: string;
31 protected _atn: ATN;
32 /** This identifies StarLoopEntryState's that begin the (...)*
33 * precedence loops of left recursive rules.
34 */
35 protected pushRecursionContextStates: BitSet;
36 protected _ruleNames: string[];
37 private _vocabulary;
38 /** This stack corresponds to the _parentctx, _parentState pair of locals
39 * that would exist on call stack frames with a recursive descent parser;
40 * in the generated function for a left-recursive rule you'd see:
41 *
42 * private EContext e(int _p) {
43 * ParserRuleContext _parentctx = _ctx; // Pair.a
44 * int _parentState = state; // Pair.b
45 * ...
46 * }
47 *
48 * Those values are used to create new recursive rule invocation contexts
49 * associated with left operand of an alt like "expr '*' expr".
50 */
51 protected readonly _parentContextStack: Array<[ParserRuleContext, number]>;
52 /** We need a map from (decision,inputIndex)->forced alt for computing ambiguous
53 * parse trees. For now, we allow exactly one override.
54 */
55 protected overrideDecision: number;
56 protected overrideDecisionInputIndex: number;
57 protected overrideDecisionAlt: number;
58 protected overrideDecisionReached: boolean;
59 /** What is the current context when we override a decisions? This tells
60 * us what the root of the parse tree is when using override
61 * for an ambiguity/lookahead check.
62 */
63 protected _overrideDecisionRoot?: InterpreterRuleContext;
64 protected _rootContext: InterpreterRuleContext;
65 /** A copy constructor that creates a new parser interpreter by reusing
66 * the fields of a previous interpreter.
67 *
68 * @param old The interpreter to copy
69 *
70 * @since 4.5
71 */
72 constructor(/*@NotNull*/ old: ParserInterpreter);
73 constructor(grammarFileName: string, /*@NotNull*/ vocabulary: Vocabulary, ruleNames: string[], atn: ATN, input: TokenStream);
74 reset(resetInput?: boolean): void;
75 get atn(): ATN;
76 get vocabulary(): Vocabulary;
77 get ruleNames(): string[];
78 get grammarFileName(): string;
79 /** Begin parsing at startRuleIndex */
80 parse(startRuleIndex: number): ParserRuleContext;
81 enterRecursionRule(localctx: ParserRuleContext, state: number, ruleIndex: number, precedence: number): void;
82 protected get atnState(): ATNState;
83 protected visitState(p: ATNState): void;
84 /** Method visitDecisionState() is called when the interpreter reaches
85 * a decision state (instance of DecisionState). It gives an opportunity
86 * for subclasses to track interesting things.
87 */
88 protected visitDecisionState(p: DecisionState): number;
89 /** Provide simple "factory" for InterpreterRuleContext's.
90 * @since 4.5.1
91 */
92 protected createInterpreterRuleContext(parent: ParserRuleContext | undefined, invokingStateNumber: number, ruleIndex: number): InterpreterRuleContext;
93 protected visitRuleStopState(p: ATNState): void;
94 /** Override this parser interpreters normal decision-making process
95 * at a particular decision and input token index. Instead of
96 * allowing the adaptive prediction mechanism to choose the
97 * first alternative within a block that leads to a successful parse,
98 * force it to take the alternative, 1..n for n alternatives.
99 *
100 * As an implementation limitation right now, you can only specify one
101 * override. This is sufficient to allow construction of different
102 * parse trees for ambiguous input. It means re-parsing the entire input
103 * in general because you're never sure where an ambiguous sequence would
104 * live in the various parse trees. For example, in one interpretation,
105 * an ambiguous input sequence would be matched completely in expression
106 * but in another it could match all the way back to the root.
107 *
108 * s : e '!'? ;
109 * e : ID
110 * | ID '!'
111 * ;
112 *
113 * Here, x! can be matched as (s (e ID) !) or (s (e ID !)). In the first
114 * case, the ambiguous sequence is fully contained only by the root.
115 * In the second case, the ambiguous sequences fully contained within just
116 * e, as in: (e ID !).
117 *
118 * Rather than trying to optimize this and make
119 * some intelligent decisions for optimization purposes, I settled on
120 * just re-parsing the whole input and then using
121 * {link Trees#getRootOfSubtreeEnclosingRegion} to find the minimal
122 * subtree that contains the ambiguous sequence. I originally tried to
123 * record the call stack at the point the parser detected and ambiguity but
124 * left recursive rules create a parse tree stack that does not reflect
125 * the actual call stack. That impedance mismatch was enough to make
126 * it it challenging to restart the parser at a deeply nested rule
127 * invocation.
128 *
129 * Only parser interpreters can override decisions so as to avoid inserting
130 * override checking code in the critical ALL(*) prediction execution path.
131 *
132 * @since 4.5
133 */
134 addDecisionOverride(decision: number, tokenIndex: number, forcedAlt: number): void;
135 get overrideDecisionRoot(): InterpreterRuleContext | undefined;
136 /** Rely on the error handler for this parser but, if no tokens are consumed
137 * to recover, add an error node. Otherwise, nothing is seen in the parse
138 * tree.
139 */
140 protected recover(e: RecognitionException): void;
141 protected recoverInline(): Token;
142 /** Return the root of the parse, which can be useful if the parser
143 * bails out. You still can access the top node. Note that,
144 * because of the way left recursive rules add children, it's possible
145 * that the root will not have any children if the start rule immediately
146 * called and left recursive rule that fails.
147 *
148 * @since 4.5.1
149 */
150 get rootContext(): InterpreterRuleContext;
151}