forked from antlr4-go/antlr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
atn_state.go
461 lines (380 loc) · 10.2 KB
/
atn_state.go
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// 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.
package antlr
import (
"fmt"
"os"
"strconv"
)
// Constants for serialization.
const (
ATNStateInvalidType = 0
ATNStateBasic = 1
ATNStateRuleStart = 2
ATNStateBlockStart = 3
ATNStatePlusBlockStart = 4
ATNStateStarBlockStart = 5
ATNStateTokenStart = 6
ATNStateRuleStop = 7
ATNStateBlockEnd = 8
ATNStateStarLoopBack = 9
ATNStateStarLoopEntry = 10
ATNStatePlusLoopBack = 11
ATNStateLoopEnd = 12
ATNStateInvalidStateNumber = -1
)
//goland:noinspection GoUnusedGlobalVariable
var ATNStateInitialNumTransitions = 4
type ATNState interface {
GetEpsilonOnlyTransitions() bool
GetRuleIndex() int
SetRuleIndex(int)
GetNextTokenWithinRule() *IntervalSet
SetNextTokenWithinRule(*IntervalSet)
GetATN() *ATN
SetATN(*ATN)
GetStateType() int
GetStateNumber() int
SetStateNumber(int)
GetTransitions() []Transition
SetTransitions([]Transition)
AddTransition(Transition, int)
String() string
Hash() int
Equals(Collectable[ATNState]) bool
}
type BaseATNState struct {
// NextTokenWithinRule caches lookahead during parsing. Not used during construction.
NextTokenWithinRule *IntervalSet
// atn is the current ATN.
atn *ATN
epsilonOnlyTransitions bool
// ruleIndex tracks the Rule index because there are no Rule objects at runtime.
ruleIndex int
stateNumber int
stateType int
// Track the transitions emanating from this ATN state.
transitions []Transition
}
func NewATNState() *BaseATNState {
return &BaseATNState{stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateInvalidType}
}
func (as *BaseATNState) GetRuleIndex() int {
return as.ruleIndex
}
func (as *BaseATNState) SetRuleIndex(v int) {
as.ruleIndex = v
}
func (as *BaseATNState) GetEpsilonOnlyTransitions() bool {
return as.epsilonOnlyTransitions
}
func (as *BaseATNState) GetATN() *ATN {
return as.atn
}
func (as *BaseATNState) SetATN(atn *ATN) {
as.atn = atn
}
func (as *BaseATNState) GetTransitions() []Transition {
return as.transitions
}
func (as *BaseATNState) SetTransitions(t []Transition) {
as.transitions = t
}
func (as *BaseATNState) GetStateType() int {
return as.stateType
}
func (as *BaseATNState) GetStateNumber() int {
return as.stateNumber
}
func (as *BaseATNState) SetStateNumber(stateNumber int) {
as.stateNumber = stateNumber
}
func (as *BaseATNState) GetNextTokenWithinRule() *IntervalSet {
return as.NextTokenWithinRule
}
func (as *BaseATNState) SetNextTokenWithinRule(v *IntervalSet) {
as.NextTokenWithinRule = v
}
func (as *BaseATNState) Hash() int {
return as.stateNumber
}
func (as *BaseATNState) String() string {
return strconv.Itoa(as.stateNumber)
}
func (as *BaseATNState) Equals(other Collectable[ATNState]) bool {
if ot, ok := other.(ATNState); ok {
return as.stateNumber == ot.GetStateNumber()
}
return false
}
func (as *BaseATNState) isNonGreedyExitState() bool {
return false
}
func (as *BaseATNState) AddTransition(trans Transition, index int) {
if len(as.transitions) == 0 {
as.epsilonOnlyTransitions = trans.GetIsEpsilon()
} else if as.epsilonOnlyTransitions != trans.GetIsEpsilon() {
_, _ = fmt.Fprintf(os.Stdin, "ATN state %d has both epsilon and non-epsilon transitions.\n", as.stateNumber)
as.epsilonOnlyTransitions = false
}
// TODO: Check code for already present compared to the Java equivalent
//alreadyPresent := false
//for _, t := range as.transitions {
// if t.getTarget().GetStateNumber() == trans.getTarget().GetStateNumber() {
// if t.getLabel() != nil && trans.getLabel() != nil && trans.getLabel().Equals(t.getLabel()) {
// alreadyPresent = true
// break
// }
// } else if t.getIsEpsilon() && trans.getIsEpsilon() {
// alreadyPresent = true
// break
// }
//}
//if !alreadyPresent {
if index == -1 {
as.transitions = append(as.transitions, trans)
} else {
as.transitions = append(as.transitions[:index], append([]Transition{trans}, as.transitions[index:]...)...)
// TODO: as.transitions.splice(index, 1, trans)
}
//} else {
// _, _ = fmt.Fprintf(os.Stderr, "Transition already present in state %d\n", as.stateNumber)
//}
}
type BasicState struct {
BaseATNState
}
func NewBasicState() *BasicState {
return &BasicState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateBasic,
},
}
}
type DecisionState interface {
ATNState
getDecision() int
setDecision(int)
getNonGreedy() bool
setNonGreedy(bool)
}
type BaseDecisionState struct {
BaseATNState
decision int
nonGreedy bool
}
func NewBaseDecisionState() *BaseDecisionState {
return &BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateBasic,
},
decision: -1,
}
}
func (s *BaseDecisionState) getDecision() int {
return s.decision
}
func (s *BaseDecisionState) setDecision(b int) {
s.decision = b
}
func (s *BaseDecisionState) getNonGreedy() bool {
return s.nonGreedy
}
func (s *BaseDecisionState) setNonGreedy(b bool) {
s.nonGreedy = b
}
type BlockStartState interface {
DecisionState
getEndState() *BlockEndState
setEndState(*BlockEndState)
}
// BaseBlockStartState is the start of a regular (...) block.
type BaseBlockStartState struct {
BaseDecisionState
endState *BlockEndState
}
func NewBlockStartState() *BaseBlockStartState {
return &BaseBlockStartState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateBasic,
},
decision: -1,
},
}
}
func (s *BaseBlockStartState) getEndState() *BlockEndState {
return s.endState
}
func (s *BaseBlockStartState) setEndState(b *BlockEndState) {
s.endState = b
}
type BasicBlockStartState struct {
BaseBlockStartState
}
func NewBasicBlockStartState() *BasicBlockStartState {
return &BasicBlockStartState{
BaseBlockStartState: BaseBlockStartState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateBlockStart,
},
},
},
}
}
var _ BlockStartState = &BasicBlockStartState{}
// BlockEndState is a terminal node of a simple (a|b|c) block.
type BlockEndState struct {
BaseATNState
startState ATNState
}
func NewBlockEndState() *BlockEndState {
return &BlockEndState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateBlockEnd,
},
startState: nil,
}
}
// RuleStopState is the last node in the ATN for a rule, unless that rule is the
// start symbol. In that case, there is one transition to EOF. Later, we might
// encode references to all calls to this rule to compute FOLLOW sets for error
// handling.
type RuleStopState struct {
BaseATNState
}
func NewRuleStopState() *RuleStopState {
return &RuleStopState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateRuleStop,
},
}
}
type RuleStartState struct {
BaseATNState
stopState ATNState
isPrecedenceRule bool
}
func NewRuleStartState() *RuleStartState {
return &RuleStartState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateRuleStart,
},
}
}
// PlusLoopbackState is a decision state for A+ and (A|B)+. It has two
// transitions: one to the loop back to start of the block, and one to exit.
type PlusLoopbackState struct {
BaseDecisionState
}
func NewPlusLoopbackState() *PlusLoopbackState {
return &PlusLoopbackState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStatePlusLoopBack,
},
},
}
}
// PlusBlockStartState is the start of a (A|B|...)+ loop. Technically it is a
// decision state; we don't use it for code generation. Somebody might need it,
// it is included for completeness. In reality, PlusLoopbackState is the real
// decision-making node for A+.
type PlusBlockStartState struct {
BaseBlockStartState
loopBackState ATNState
}
func NewPlusBlockStartState() *PlusBlockStartState {
return &PlusBlockStartState{
BaseBlockStartState: BaseBlockStartState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStatePlusBlockStart,
},
},
},
}
}
var _ BlockStartState = &PlusBlockStartState{}
// StarBlockStartState is the block that begins a closure loop.
type StarBlockStartState struct {
BaseBlockStartState
}
func NewStarBlockStartState() *StarBlockStartState {
return &StarBlockStartState{
BaseBlockStartState: BaseBlockStartState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateStarBlockStart,
},
},
},
}
}
var _ BlockStartState = &StarBlockStartState{}
type StarLoopbackState struct {
BaseATNState
}
func NewStarLoopbackState() *StarLoopbackState {
return &StarLoopbackState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateStarLoopBack,
},
}
}
type StarLoopEntryState struct {
BaseDecisionState
loopBackState ATNState
precedenceRuleDecision bool
}
func NewStarLoopEntryState() *StarLoopEntryState {
// False precedenceRuleDecision indicates whether s state can benefit from a precedence DFA during SLL decision making.
return &StarLoopEntryState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateStarLoopEntry,
},
},
}
}
// LoopEndState marks the end of a * or + loop.
type LoopEndState struct {
BaseATNState
loopBackState ATNState
}
func NewLoopEndState() *LoopEndState {
return &LoopEndState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateLoopEnd,
},
}
}
// TokensStartState is the Tokens rule start state linking to each lexer rule start state.
type TokensStartState struct {
BaseDecisionState
}
func NewTokensStartState() *TokensStartState {
return &TokensStartState{
BaseDecisionState: BaseDecisionState{
BaseATNState: BaseATNState{
stateNumber: ATNStateInvalidStateNumber,
stateType: ATNStateTokenStart,
},
},
}
}