-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathLL1Analyzer.js
More file actions
193 lines (187 loc) · 8.4 KB
/
LL1Analyzer.js
File metadata and controls
193 lines (187 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
import Token from '../Token.js';
import ATNConfig from './ATNConfig.js';
import IntervalSet from '../misc/IntervalSet.js';
import RuleStopState from '../state/RuleStopState.js';
import RuleTransition from '../transition/RuleTransition.js';
import NotSetTransition from '../transition/NotSetTransition.js';
import WildcardTransition from '../transition/WildcardTransition.js';
import AbstractPredicateTransition from './AbstractPredicateTransition.js';
import { predictionContextFromRuleContext } from '../context/PredictionContextUtils.js';
import PredictionContext from '../context/PredictionContext.js';
import SingletonPredictionContext from '../context/SingletonPredictionContext.js';
import BitSet from "../misc/BitSet.js";
import HashSet from "../misc/HashSet.js";
export default class LL1Analyzer {
constructor(atn) {
this.atn = atn;
}
/**
* Calculates the SLL(1) expected lookahead set for each outgoing transition
* of an {@link ATNState}. The returned array has one element for each
* outgoing transition in {@code s}. If the closure from transition
* <em>i</em> leads to a semantic predicate before matching a symbol, the
* element at index <em>i</em> of the result will be {@code null}.
*
* @param s the ATN state
* @return the expected symbols for each outgoing transition of {@code s}.
*/
getDecisionLookahead(s) {
if (s === null) {
return null;
}
let count = s.transitions.length;
let look = [];
for(let alt=0; alt< count; alt++) {
look[alt] = new IntervalSet();
let lookBusy = new HashSet();
let seeThruPreds = false; // fail to get lookahead upon pred
this._LOOK(s.transition(alt).target, null, PredictionContext.EMPTY,
look[alt], lookBusy, new BitSet(), seeThruPreds, false);
// Wipe out lookahead for this alternative if we found nothing
// or we had a predicate when we !seeThruPreds
if (look[alt].length===0 || look[alt].contains(LL1Analyzer.HIT_PRED)) {
look[alt] = null;
}
}
return look;
}
/**
* Compute set of tokens that can follow {@code s} in the ATN in the
* specified {@code ctx}.
*
* <p>If {@code ctx} is {@code null} and the end of the rule containing
* {@code s} is reached, {@link Token//EPSILON} is added to the result set.
* If {@code ctx} is not {@code null} and the end of the outermost rule is
* reached, {@link Token//EOF} is added to the result set.</p>
*
* @param s the ATN state
* @param stopState the ATN state to stop at. This can be a
* {@link BlockEndState} to detect epsilon paths through a closure.
* @param ctx the complete parser context, or {@code null} if the context
* should be ignored
*
* @return The set of tokens that can follow {@code s} in the ATN in the
* specified {@code ctx}.
*/
LOOK(s, stopState, ctx) {
let r = new IntervalSet();
let seeThruPreds = true; // ignore preds; get all lookahead
ctx = ctx || null;
let lookContext = ctx!==null ? predictionContextFromRuleContext(s.atn, ctx) : null;
this._LOOK(s, stopState, lookContext, r, new HashSet(), new BitSet(), seeThruPreds, true);
return r;
}
/**
* Compute set of tokens that can follow {@code s} in the ATN in the
* specified {@code ctx}.
*
* <p>If {@code ctx} is {@code null} and {@code stopState} or the end of the
* rule containing {@code s} is reached, {@link Token//EPSILON} is added to
* the result set. If {@code ctx} is not {@code null} and {@code addEOF} is
* {@code true} and {@code stopState} or the end of the outermost rule is
* reached, {@link Token//EOF} is added to the result set.</p>
*
* @param s the ATN state.
* @param stopState the ATN state to stop at. This can be a
* {@link BlockEndState} to detect epsilon paths through a closure.
* @param ctx The outer context, or {@code null} if the outer context should
* not be used.
* @param look The result lookahead set.
* @param lookBusy A set used for preventing epsilon closures in the ATN
* from causing a stack overflow. Outside code should pass
* {@code new CustomizedSet<ATNConfig>} for this argument.
* @param calledRuleStack A set used for preventing left recursion in the
* ATN from causing a stack overflow. Outside code should pass
* {@code new BitSet()} for this argument.
* @param seeThruPreds {@code true} to true semantic predicates as
* implicitly {@code true} and "see through them", otherwise {@code false}
* to treat semantic predicates as opaque and add {@link //HIT_PRED} to the
* result if one is encountered.
* @param addEOF Add {@link Token//EOF} to the result if the end of the
* outermost context is reached. This parameter has no effect if {@code ctx}
* is {@code null}.
*/
_LOOK(s, stopState , ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF) {
let c = new ATNConfig({state:s, alt:0, context: ctx}, null);
if (lookBusy.has(c)) {
return;
}
lookBusy.add(c);
if (s === stopState) {
if (ctx ===null) {
look.addOne(Token.EPSILON);
return;
} else if (ctx.isEmpty() && addEOF) {
look.addOne(Token.EOF);
return;
}
}
if (s instanceof RuleStopState ) {
if (ctx ===null) {
look.addOne(Token.EPSILON);
return;
} else if (ctx.isEmpty() && addEOF) {
look.addOne(Token.EOF);
return;
}
if (ctx !== PredictionContext.EMPTY) {
let removed = calledRuleStack.get(s.ruleIndex);
try {
calledRuleStack.clear(s.ruleIndex);
// run thru all possible stack tops in ctx
for (let i = 0; i < ctx.length; i++) {
let returnState = this.atn.states[ctx.getReturnState(i)];
this._LOOK(returnState, stopState, ctx.getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
}
}finally {
if (removed) {
calledRuleStack.set(s.ruleIndex);
}
}
return;
}
}
for(let j=0; j<s.transitions.length; j++) {
let t = s.transitions[j];
if (t.constructor === RuleTransition) {
if (calledRuleStack.get(t.target.ruleIndex)) {
continue;
}
let newContext = SingletonPredictionContext.create(ctx, t.followState.stateNumber);
try {
calledRuleStack.set(t.target.ruleIndex);
this._LOOK(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} finally {
calledRuleStack.clear(t.target.ruleIndex);
}
} else if (t instanceof AbstractPredicateTransition ) {
if (seeThruPreds) {
this._LOOK(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} else {
look.addOne(LL1Analyzer.HIT_PRED);
}
} else if( t.isEpsilon) {
this._LOOK(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} else if (t.constructor === WildcardTransition) {
look.addRange( Token.MIN_USER_TOKEN_TYPE, this.atn.maxTokenType );
} else {
let set = t.label;
if (set !== null) {
if (t instanceof NotSetTransition) {
set = set.complement(Token.MIN_USER_TOKEN_TYPE, this.atn.maxTokenType);
}
look.addSet(set);
}
}
}
}
}
/**
* Special value added to the lookahead sets to indicate that we hit
* a predicate during analysis if {@code seeThruPreds==false}.
*/
LL1Analyzer.HIT_PRED = Token.INVALID_TYPE;