dbt-antlr4 1.2.1

Dbt fork of ANTLR4 runtime for Rust
Documentation
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::ptr::NonNull;
use std::sync::OnceLock;

use crate::interval_set::IntervalSet;
use crate::transition::Transition;

pub(crate) const ATNSTATE_INVALID_TYPE: i32 = 0;
pub(crate) const ATNSTATE_BASIC: i32 = 1;
pub(crate) const ATNSTATE_RULE_START: i32 = 2;
pub(crate) const ATNSTATE_BLOCK_START: i32 = 3;
pub(crate) const ATNSTATE_PLUS_BLOCK_START: i32 = 4;
pub(crate) const ATNSTATE_STAR_BLOCK_START: i32 = 5;
pub(crate) const ATNSTATE_TOKEN_START: i32 = 6;
pub(crate) const ATNSTATE_RULE_STOP: i32 = 7;
pub(crate) const ATNSTATE_BLOCK_END: i32 = 8;
pub(crate) const ATNSTATE_STAR_LOOP_BACK: i32 = 9;
pub(crate) const ATNSTATE_STAR_LOOP_ENTRY: i32 = 10;
pub(crate) const ATNSTATE_PLUS_LOOP_BACK: i32 = 11;
pub(crate) const ATNSTATE_LOOP_END: i32 = 12;
pub(crate) const ATNSTATE_INVALID_STATE_NUMBER: i32 = -1;

pub enum ATNState {
    RuleStart(RuleStartState),
    RuleStop(RuleStopState),
    BlockEnd(BlockEndState),
    LoopEnd(LoopEndState),
    StarLoopback(StarLoopbackState),
    Basic(BasicState),
    Decision(DecisionState),
    Invalid(BaseATNState),
}

#[doc(hidden)]
#[derive(Eq, PartialEq)]
pub enum ATNDecisionState {
    StarLoopEntry {
        loop_back_state: ATNStateRef,
        is_precedence: bool,
    },
    TokenStartState,
    PlusLoopBack,
    BlockStartState {
        end_state: ATNStateRef,
        en: ATNBlockStart,
    },
}

#[doc(hidden)]
#[derive(Eq, PartialEq)]
pub enum ATNBlockStart {
    BasicBlockStart,
    StarBlockStart,
    PlusBlockStart(ATNStateRef),
}

impl ATNState {
    pub fn has_epsilon_only_transitions(&self) -> bool {
        match self {
            ATNState::RuleStart(s) => s.has_epsilon_only_transitions(),
            ATNState::RuleStop(s) => s.has_epsilon_only_transitions(),
            ATNState::BlockEnd(s) => s.has_epsilon_only_transitions(),
            ATNState::LoopEnd(s) => s.has_epsilon_only_transitions(),
            ATNState::StarLoopback(s) => s.has_epsilon_only_transitions(),
            ATNState::Basic(s) => s.has_epsilon_only_transitions(),
            ATNState::Decision(s) => s.has_epsilon_only_transitions(),
            ATNState::Invalid(s) => s.has_epsilon_only_transitions(),
        }
    }

    pub fn get_rule_index(&self) -> i32 {
        match self {
            ATNState::RuleStart(s) => s.get_rule_index(),
            ATNState::RuleStop(s) => s.get_rule_index(),
            ATNState::BlockEnd(s) => s.get_rule_index(),
            ATNState::LoopEnd(s) => s.get_rule_index(),
            ATNState::StarLoopback(s) => s.get_rule_index(),
            ATNState::Basic(s) => s.get_rule_index(),
            ATNState::Decision(s) => s.get_rule_index(),
            ATNState::Invalid(s) => s.get_rule_index(),
        }
    }

    pub fn get_next_tokens_within_rule(&self) -> &OnceLock<IntervalSet> {
        match self {
            ATNState::RuleStart(s) => s.get_next_tokens_within_rule(),
            ATNState::RuleStop(s) => s.get_next_tokens_within_rule(),
            ATNState::BlockEnd(s) => s.get_next_tokens_within_rule(),
            ATNState::LoopEnd(s) => s.get_next_tokens_within_rule(),
            ATNState::StarLoopback(s) => s.get_next_tokens_within_rule(),
            ATNState::Basic(s) => s.get_next_tokens_within_rule(),
            ATNState::Decision(s) => s.get_next_tokens_within_rule(),
            ATNState::Invalid(s) => s.get_next_tokens_within_rule(),
        }
    }

    pub fn get_state_type_id(&self) -> i32 {
        match self {
            ATNState::RuleStart(s) => s.get_state_type_id(),
            ATNState::RuleStop(s) => s.get_state_type_id(),
            ATNState::BlockEnd(s) => s.get_state_type_id(),
            ATNState::LoopEnd(s) => s.get_state_type_id(),
            ATNState::StarLoopback(s) => s.get_state_type_id(),
            ATNState::Basic(s) => s.get_state_type_id(),
            ATNState::Decision(s) => s.get_state_type_id(),
            ATNState::Invalid(s) => s.get_state_type_id(),
        }
    }

    pub fn get_state_number(&self) -> i32 {
        match self {
            ATNState::RuleStart(s) => s.get_state_number(),
            ATNState::RuleStop(s) => s.get_state_number(),
            ATNState::BlockEnd(s) => s.get_state_number(),
            ATNState::LoopEnd(s) => s.get_state_number(),
            ATNState::StarLoopback(s) => s.get_state_number(),
            ATNState::Basic(s) => s.get_state_number(),
            ATNState::Decision(s) => s.get_state_number(),
            ATNState::Invalid(s) => s.get_state_number(),
        }
    }

    pub fn get_transitions(&self) -> &Vec<Transition> {
        match self {
            ATNState::RuleStart(s) => s.get_transitions(),
            ATNState::RuleStop(s) => s.get_transitions(),
            ATNState::BlockEnd(s) => s.get_transitions(),
            ATNState::LoopEnd(s) => s.get_transitions(),
            ATNState::StarLoopback(s) => s.get_transitions(),
            ATNState::Basic(s) => s.get_transitions(),
            ATNState::Decision(s) => s.get_transitions(),
            ATNState::Invalid(s) => s.get_transitions(),
        }
    }

    pub fn add_transition(&mut self, trans: Transition) {
        match self {
            ATNState::RuleStart(s) => s.add_transition(trans),
            ATNState::RuleStop(s) => s.add_transition(trans),
            ATNState::BlockEnd(s) => s.add_transition(trans),
            ATNState::LoopEnd(s) => s.add_transition(trans),
            ATNState::StarLoopback(s) => s.add_transition(trans),
            ATNState::Basic(s) => s.add_transition(trans),
            ATNState::Decision(s) => s.add_transition(trans),
            ATNState::Invalid(s) => s.add_transition(trans),
        }
    }
}

impl Default for ATNState {
    fn default() -> Self {
        ATNState::Invalid(BaseATNState::new(
            ATNSTATE_INVALID_STATE_NUMBER,
            -1,
            ATNSTATE_INVALID_TYPE,
        ))
    }
}

#[derive(Debug)]
pub struct BaseATNState {
    next_tokens_within_rule: OnceLock<IntervalSet>,

    epsilon_only_transitions: bool,

    pub rule_index: i32,

    pub state_number: i32,

    pub state_type_id: i32,

    transitions: Vec<Transition>,
}

impl BaseATNState {
    pub fn new(state_number: i32, rule_index: i32, state_type_id: i32) -> Self {
        BaseATNState {
            next_tokens_within_rule: OnceLock::new(),
            epsilon_only_transitions: false,
            rule_index,
            state_number,
            state_type_id,
            transitions: Vec::new(),
        }
    }
}

impl BaseATNState {
    pub fn has_epsilon_only_transitions(&self) -> bool {
        self.epsilon_only_transitions
    }
    pub fn get_rule_index(&self) -> i32 {
        self.rule_index
    }

    pub fn get_next_tokens_within_rule(&self) -> &OnceLock<IntervalSet> {
        &self.next_tokens_within_rule
    }

    pub fn get_state_type_id(&self) -> i32 {
        self.state_type_id
    }

    pub fn get_state_number(&self) -> i32 {
        self.state_number
    }

    pub fn get_transitions(&self) -> &Vec<Transition> {
        &self.transitions
    }

    pub fn add_transition(&mut self, trans: Transition) {
        if self.transitions.is_empty() {
            self.epsilon_only_transitions = trans.is_epsilon()
        } else {
            self.epsilon_only_transitions &= trans.is_epsilon()
        }

        let mut already_present = false;
        for existing in self.transitions.iter() {
            if existing.get_target() == trans.get_target() {
                #[allow(clippy::if_same_then_else)]
                if existing.get_label().is_some()
                    && trans.get_label().is_some()
                    && existing.get_label() == trans.get_label()
                {
                    already_present = true;
                    break;
                } else if existing.is_epsilon() && trans.is_epsilon() {
                    already_present = true;
                    break;
                }
            }
        }
        if !already_present {
            self.transitions.push(trans);
        }
    }
}

pub struct RuleStartState {
    base: BaseATNState,
    pub stop_state: ATNStateRef,
    pub is_left_recursive: bool,
}

impl RuleStartState {
    pub fn new(base: BaseATNState, stop_state: ATNStateRef, is_left_recursive: bool) -> Self {
        RuleStartState {
            base,
            stop_state,
            is_left_recursive,
        }
    }
}

pub struct RuleStopState {
    base: BaseATNState,
}

impl RuleStopState {
    pub fn new(base: BaseATNState) -> Self {
        RuleStopState { base }
    }
}

pub struct BlockEndState {
    base: BaseATNState,
    pub end_state: ATNStateRef,
}

impl BlockEndState {
    pub fn new(base: BaseATNState, end_state: ATNStateRef) -> Self {
        BlockEndState { base, end_state }
    }
}

pub struct LoopEndState {
    base: BaseATNState,
    pub loop_back_state: ATNStateRef,
}

impl LoopEndState {
    pub fn new(base: BaseATNState, loop_back_state: ATNStateRef) -> Self {
        LoopEndState {
            base,
            loop_back_state,
        }
    }
}

pub struct StarLoopbackState {
    base: BaseATNState,
}

impl StarLoopbackState {
    pub fn new(base: BaseATNState) -> Self {
        StarLoopbackState { base }
    }
}

pub struct BasicState {
    base: BaseATNState,
}

impl BasicState {
    pub fn new(base: BaseATNState) -> Self {
        BasicState { base }
    }
}

pub struct DecisionState {
    base: BaseATNState,
    pub decision: i32,
    pub nongreedy: bool,
    pub state: ATNDecisionState,
}

impl DecisionState {
    pub fn new(
        base: BaseATNState,
        decision: i32,
        nongreedy: bool,
        state: ATNDecisionState,
    ) -> Self {
        DecisionState {
            base,
            decision,
            nongreedy,
            state,
        }
    }
}

impl Deref for RuleStartState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for RuleStartState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<RuleStartState> for ATNState {
    fn from(value: RuleStartState) -> Self {
        ATNState::RuleStart(value)
    }
}

impl Deref for RuleStopState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for RuleStopState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<RuleStopState> for ATNState {
    fn from(value: RuleStopState) -> Self {
        ATNState::RuleStop(value)
    }
}

impl Deref for BlockEndState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for BlockEndState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<BlockEndState> for ATNState {
    fn from(value: BlockEndState) -> Self {
        ATNState::BlockEnd(value)
    }
}

impl Deref for LoopEndState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for LoopEndState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<LoopEndState> for ATNState {
    fn from(value: LoopEndState) -> Self {
        ATNState::LoopEnd(value)
    }
}

impl Deref for StarLoopbackState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for StarLoopbackState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<StarLoopbackState> for ATNState {
    fn from(value: StarLoopbackState) -> Self {
        ATNState::StarLoopback(value)
    }
}

impl Deref for BasicState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for BasicState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<BasicState> for ATNState {
    fn from(value: BasicState) -> Self {
        ATNState::Basic(value)
    }
}

impl Deref for DecisionState {
    type Target = BaseATNState;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl DerefMut for DecisionState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.base
    }
}

impl From<DecisionState> for ATNState {
    fn from(value: DecisionState) -> Self {
        ATNState::Decision(value)
    }
}

pub struct ATNStateRef(NonNull<ATNState>);

impl ATNStateRef {
    #[inline]
    pub fn invalid() -> Self {
        static INVALID_STATE: ATNState = ATNState::Invalid(BaseATNState {
            next_tokens_within_rule: OnceLock::new(),
            epsilon_only_transitions: false,
            rule_index: -1,
            state_number: ATNSTATE_INVALID_STATE_NUMBER,
            state_type_id: ATNSTATE_INVALID_TYPE,
            transitions: Vec::new(),
        });

        ATNStateRef(NonNull::from(&INVALID_STATE))
    }

    #[allow(clippy::mut_from_ref)]
    #[inline]
    pub(crate) unsafe fn as_mut(&self) -> &mut ATNState {
        unsafe { &mut *(self.0.as_ptr()) }
    }

    pub fn as_usize(&self) -> usize {
        self.0.as_ptr() as usize
    }
}

impl From<&Pin<Box<ATNState>>> for ATNStateRef {
    fn from(value: &Pin<Box<ATNState>>) -> Self {
        ATNStateRef(NonNull::from(&**value))
    }
}

impl PartialEq for ATNStateRef {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl Eq for ATNStateRef {}

impl PartialOrd for ATNStateRef {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ATNStateRef {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.get_state_number().cmp(&other.get_state_number())
    }
}

impl Deref for ATNStateRef {
    type Target = ATNState;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.0.as_ptr() }
    }
}

impl AsRef<ATNState> for ATNStateRef {
    fn as_ref(&self) -> &ATNState {
        unsafe { &*self.0.as_ptr() }
    }
}

impl Clone for ATNStateRef {
    fn clone(&self) -> Self {
        *self
    }
}

impl Copy for ATNStateRef {}

impl std::hash::Hash for ATNStateRef {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_usize(self.0.as_ptr() as usize);
    }
}

impl Debug for ATNStateRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ATNStateRef({})", self.get_state_number())
    }
}

unsafe impl Send for ATNStateRef {}
unsafe impl Sync for ATNStateRef {}