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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Copyright (c) 2012-2017 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.
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Dfa;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime.Atn
{
public class ATN
{
public const int INVALID_ALT_NUMBER = 0;
[NotNull]
public readonly IList<ATNState> states = new List<ATNState>();
/// <summary>
/// Each subrule/rule is a decision point and we must track them so we
/// can go back later and build DFA predictors for them.
/// </summary>
/// <remarks>
/// Each subrule/rule is a decision point and we must track them so we
/// can go back later and build DFA predictors for them. This includes
/// all the rules, subrules, optional blocks, ()+, ()* etc...
/// </remarks>
[NotNull]
public readonly IList<DecisionState> decisionToState = new List<DecisionState>();
/// <summary>Maps from rule index to starting state number.</summary>
/// <remarks>Maps from rule index to starting state number.</remarks>
public RuleStartState[] ruleToStartState;
/// <summary>Maps from rule index to stop state number.</summary>
/// <remarks>Maps from rule index to stop state number.</remarks>
public RuleStopState[] ruleToStopState;
[NotNull]
public readonly IDictionary<string, TokensStartState> modeNameToStartState = new Dictionary<string, TokensStartState>();
/// <summary>The type of the ATN.</summary>
/// <remarks>The type of the ATN.</remarks>
public readonly ATNType grammarType;
/// <summary>The maximum value for any symbol recognized by a transition in the ATN.</summary>
/// <remarks>The maximum value for any symbol recognized by a transition in the ATN.</remarks>
public readonly int maxTokenType;
/// <summary>For lexer ATNs, this maps the rule index to the resulting token type.</summary>
/// <remarks>
/// For lexer ATNs, this maps the rule index to the resulting token type.
/// For parser ATNs, this maps the rule index to the generated bypass token
/// type if the
/// <see cref="ATNDeserializationOptions.GenerateRuleBypassTransitions()"/>
/// deserialization option was specified; otherwise, this is
/// <see langword="null"/>
/// .
/// </remarks>
public int[] ruleToTokenType;
/// <summary>
/// For lexer ATNs, this is an array of
/// <see cref="ILexerAction"/>
/// objects which may
/// be referenced by action transitions in the ATN.
/// </summary>
public ILexerAction[] lexerActions;
[NotNull]
public readonly IList<TokensStartState> modeToStartState = new List<TokensStartState>();
private readonly PredictionContextCache contextCache = new PredictionContextCache();
[NotNull]
public DFA[] decisionToDFA = Collections.EmptyList<DFA>();
[NotNull]
public DFA[] modeToDFA = Collections.EmptyList<DFA>();
protected internal readonly ConcurrentDictionary<int, int> LL1Table = new ConcurrentDictionary<int, int>();
/// <summary>Used for runtime deserialization of ATNs from strings</summary>
public ATN(ATNType grammarType, int maxTokenType)
{
this.grammarType = grammarType;
this.maxTokenType = maxTokenType;
}
public virtual PredictionContext GetCachedContext(PredictionContext context)
{
return PredictionContext.GetCachedContext(context, contextCache, new PredictionContext.IdentityHashMap());
}
/// <summary>
/// Compute the set of valid tokens that can occur starting in state
/// <paramref name="s"/>
/// .
/// If
/// <paramref name="ctx"/>
/// is
/// <see cref="EmptyPredictionContext.Instance"/>
/// , the set of tokens will not include what can follow
/// the rule surrounding
/// <paramref name="s"/>
/// . In other words, the set will be
/// restricted to tokens reachable staying within
/// <paramref name="s"/>
/// 's rule.
/// </summary>
[return: NotNull]
public virtual IntervalSet NextTokens(ATNState s, RuleContext ctx)
{
LL1Analyzer anal = new LL1Analyzer(this);
IntervalSet next = anal.Look(s, ctx);
return next;
}
/// <summary>
/// Compute the set of valid tokens that can occur starting in
/// <paramref name="s"/>
/// and
/// staying in same rule.
/// <see cref="TokenConstants.EPSILON"/>
/// is in set if we reach end of
/// rule.
/// </summary>
[return: NotNull]
public virtual IntervalSet NextTokens(ATNState s)
{
if (s.nextTokenWithinRule != null)
{
return s.nextTokenWithinRule;
}
s.nextTokenWithinRule = NextTokens(s, null);
s.nextTokenWithinRule.SetReadonly(true);
return s.nextTokenWithinRule;
}
public virtual void AddState(ATNState state)
{
if (state != null)
{
state.atn = this;
state.stateNumber = states.Count;
}
states.Add(state);
}
public virtual void RemoveState(ATNState state)
{
states[state.stateNumber] = null;
}
// just free mem, don't shift states in list
public virtual void DefineMode(string name, TokensStartState s)
{
modeNameToStartState[name] = s;
modeToStartState.Add(s);
modeToDFA = Arrays.CopyOf(modeToDFA, modeToStartState.Count);
modeToDFA[modeToDFA.Length - 1] = new DFA(s);
DefineDecisionState(s);
}
public virtual int DefineDecisionState(DecisionState s)
{
decisionToState.Add(s);
s.decision = decisionToState.Count - 1;
decisionToDFA = Arrays.CopyOf(decisionToDFA, decisionToState.Count);
decisionToDFA[decisionToDFA.Length - 1] = new DFA(s, s.decision);
return s.decision;
}
public virtual DecisionState GetDecisionState(int decision)
{
if (decisionToState.Count != 0)
{
return decisionToState[decision];
}
return null;
}
public virtual int NumberOfDecisions
{
get
{
return decisionToState.Count;
}
}
/// <summary>
/// Computes the set of input symbols which could follow ATN state number
/// <paramref name="stateNumber"/>
/// in the specified full
/// <paramref name="context"/>
/// . This method
/// considers the complete parser context, but does not evaluate semantic
/// predicates (i.e. all predicates encountered during the calculation are
/// assumed true). If a path in the ATN exists from the starting state to the
/// <see cref="RuleStopState"/>
/// of the outermost context without matching any
/// symbols,
/// <see cref="TokenConstants.EOF"/>
/// is added to the returned set.
/// <p>If
/// <paramref name="context"/>
/// is
/// <see langword="null"/>
/// , it is treated as
/// <see cref="ParserRuleContext.EmptyContext"/>
/// .</p>
/// </summary>
/// <param name="stateNumber">the ATN state number</param>
/// <param name="context">the full parse context</param>
/// <returns>
/// The set of potentially valid input symbols which could follow the
/// specified state in the specified context.
/// </returns>
/// <exception cref="System.ArgumentException">
/// if the ATN does not contain a state with
/// number
/// <paramref name="stateNumber"/>
/// </exception>
[return: NotNull]
public virtual IntervalSet GetExpectedTokens(int stateNumber, RuleContext context)
{
if (stateNumber < 0 || stateNumber >= states.Count)
{
throw new ArgumentException("Invalid state number.");
}
RuleContext ctx = context;
ATNState s = states[stateNumber];
IntervalSet following = NextTokens(s);
if (!following.Contains(TokenConstants.EPSILON))
{
return following;
}
IntervalSet expected = new IntervalSet();
expected.AddAll(following);
expected.Remove(TokenConstants.EPSILON);
while (ctx != null && ctx.invokingState >= 0 && following.Contains(TokenConstants.EPSILON))
{
ATNState invokingState = states[ctx.invokingState];
RuleTransition rt = (RuleTransition)invokingState.Transition(0);
following = NextTokens(rt.followState);
expected.AddAll(following);
expected.Remove(TokenConstants.EPSILON);
ctx = ctx.Parent;
}
if (following.Contains(TokenConstants.EPSILON))
{
expected.Add(TokenConstants.EOF);
}
return expected;
}
}
}