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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::cmp::max;
use std::fmt::{Debug, Error, Formatter};
use std::hash::{Hash, Hasher};

use bit_set::BitSet;
use fxhash::{hash64, FxHasher64};
use hashbrown::HashTable;

use crate::atn_config::{ATNConfig, ATNConfigType, LexerATNConfig};
use crate::parser_atn_simulator::MergeCache;
use crate::prediction_context::PredictionContext;
use crate::semantic_context::SemanticContext;
use crate::PredictionContextCache;

pub trait ConfigSet<'ephemeral>: PartialEq + Eq + Hash {
    type ConfigType: ATNConfigType<'ephemeral>;
    type FinalizedType<'x>: ConfigSet<'x>;

    fn new_empty() -> Self;

    fn hash_code(&self) -> u64;

    fn finalize<'sim>(self, cache: &'sim PredictionContextCache<'sim>)
        -> Self::FinalizedType<'sim>;
}

#[derive(PartialEq, Eq, Hash, Debug, Clone)]
struct ATNConfigSetBase {
    dips_into_outer_context: bool,

    full_ctx: bool,

    has_semantic_context: bool,

    unique_alt: i32,
}

pub struct ATNConfigSet<'ephemeral> {
    base: ATNConfigSetBase,

    configs: ConfigSetStore<'ephemeral, ATNConfig<'ephemeral>>,

    pub(crate) conflicting_alts: BitSet,
}

impl Debug for ATNConfigSet<'_> {
    fn fmt(&self, _f: &mut Formatter<'_>) -> Result<(), Error> {
        _f.write_str("ATNConfigSet")?;
        _f.debug_list().entries(self.configs.iter()).finish()?;
        if self.base.has_semantic_context {
            _f.write_str(",hasSemanticContext=true")?
        }
        if self.conflicting_alts.is_empty() {
            _f.write_fmt(format_args!(",uniqueAlt={}", self.base.unique_alt))
        } else {
            _f.write_fmt(format_args!(",conflictingAlts={:?}", self.conflicting_alts))
        }
    }
}

impl PartialEq for ATNConfigSet<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.base == other.base
            && self.configs == other.configs
            && self.conflicting_alts == other.conflicting_alts
    }
}

impl Eq for ATNConfigSet<'_> {}

impl<'ephemeral> Hash for ATNConfigSet<'ephemeral> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.configs.iter().for_each(|c| c.hash(state));
    }
}

impl<'ephemeral> IntoIterator for ATNConfigSet<'ephemeral> {
    type Item = ATNConfig<'ephemeral>;
    type IntoIter = <ConfigSetStore<'ephemeral, ATNConfig<'ephemeral>> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.configs.into_iter()
    }
}

impl<'ephemeral> ConfigSet<'ephemeral> for ATNConfigSet<'ephemeral> {
    type ConfigType = ATNConfig<'ephemeral>;
    type FinalizedType<'x> = ATNConfigSet<'x>;

    fn new_empty() -> Self {
        ATNConfigSet {
            base: ATNConfigSetBase {
                dips_into_outer_context: false,
                full_ctx: true,
                has_semantic_context: false,
                unique_alt: 0,
            },
            configs: ConfigSetStore::<ATNConfig<'static>>::new_empty(),
            conflicting_alts: Default::default(),
        }
    }

    fn hash_code(&self) -> u64 {
        self.configs.hash_code()
    }

    fn finalize<'sim>(
        self,
        cache: &'sim PredictionContextCache<'sim>,
    ) -> Self::FinalizedType<'sim> {
        // unsafe {
        //     std::mem::transmute(ATNConfigSet {
        //         configs: self.configs.finalize(cache),
        //         ..self
        //     })
        // }
        ATNConfigSet {
            configs: self.configs.finalize(cache),
            ..self
        }
    }
}

impl<'ephemeral> ATNConfigSet<'ephemeral> {
    pub fn new(arena: &'ephemeral bumpalo::Bump, full_ctx: bool) -> ATNConfigSet<'ephemeral> {
        ATNConfigSet {
            base: ATNConfigSetBase {
                dips_into_outer_context: false,
                full_ctx,
                has_semantic_context: false,
                unique_alt: 0,
            },
            configs: ConfigSetStore::new_ephemeral(arena),
            conflicting_alts: Default::default(),
        }
    }

    // for parser
    pub(crate) fn add_cached(
        &mut self,
        config: ATNConfig<'ephemeral>,
        merge_cache: &mut MergeCache<'ephemeral>,
    ) -> bool {
        let store = match &mut self.configs {
            ConfigSetStore::Scratch(s) => s,
            ConfigSetStore::Final(_) => panic!("Cannot add to read-only ATNConfigSet"),
        };

        if config.semantic_context() != &SemanticContext::NONE {
            self.base.has_semantic_context = true
        }

        if config.get_reaches_into_outer_context() > 0 {
            self.base.dips_into_outer_context = true
        }

        let key = Key::partial(&config, store.configs.len());

        if let Some(key) = store
            .lookup
            .find(key.hash_code(), |k| k.partial_eq(&config, &store.configs))
        {
            let existing = &mut store.configs[key.index()];
            let root_is_wildcard = !self.base.full_ctx;

            let merged = PredictionContext::merge(
                existing.get_context().unwrap(),
                config.get_context().unwrap(),
                root_is_wildcard,
                merge_cache,
            );

            let v1 = existing.get_reaches_into_outer_context();
            let v2 = config.get_reaches_into_outer_context();
            existing.set_reaches_into_outer_context(max(v1, v2));

            if config.is_precedence_filter_suppressed() {
                existing.set_precedence_filter_suppressed(true)
            }

            existing.set_context(merged);
        } else {
            store.configs.push(config);
            store
                .lookup
                .insert_unique(key.hash_code(), key, Key::hash_code);
        }
        true
    }

    pub(crate) fn add(&mut self, config: ATNConfig<'ephemeral>) -> bool {
        let store = match &mut self.configs {
            ConfigSetStore::Scratch(s) => s,
            ConfigSetStore::Final(_) => panic!("Cannot add to read-only ATNConfigSet"),
        };

        if config.semantic_context() != &SemanticContext::NONE {
            self.base.has_semantic_context = true
        }

        if config.get_reaches_into_outer_context() > 0 {
            self.base.dips_into_outer_context = true
        }

        let key = Key::partial(&config, store.configs.len());

        if store
            .lookup
            .find(key.hash_code(), |k| k.partial_eq(&config, &store.configs))
            .is_none()
        {
            store.configs.push(config);
            store
                .lookup
                .insert_unique(key.hash_code(), key, Key::hash_code);
        }
        true
    }

    pub fn get_items(&self) -> impl Iterator<Item = &ATNConfig<'ephemeral>> {
        self.configs.iter()
    }

    pub fn length(&self) -> usize {
        self.configs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.configs.is_empty()
    }

    pub fn has_semantic_context(&self) -> bool {
        self.base.has_semantic_context
    }

    pub fn set_has_semantic_context(&mut self, _v: bool) {
        self.base.has_semantic_context = _v;
    }

    pub fn read_only(&self) -> bool {
        self.configs.read_only()
    }

    pub fn full_context(&self) -> bool {
        self.base.full_ctx
    }

    //duplicate of the self.conflicting_alts???
    pub fn get_alts(&self) -> BitSet {
        self.configs.iter().fold(BitSet::new(), |mut acc, c| {
            acc.insert(c.get_alt() as usize);
            acc
        })
    }

    pub fn get_unique_alt(&self) -> i32 {
        self.base.unique_alt
    }

    pub fn set_unique_alt(&mut self, _v: i32) {
        self.base.unique_alt = _v
    }

    pub fn get_dips_into_outer_context(&self) -> bool {
        self.base.dips_into_outer_context
    }

    pub fn set_dips_into_outer_context(&mut self, _v: bool) {
        self.base.dips_into_outer_context = _v
    }
}

#[derive(PartialEq, Eq)]
pub struct LexerATNConfigSet<'ephemeral> {
    base: ATNConfigSetBase,

    configs: ConfigSetStore<'ephemeral, LexerATNConfig<'ephemeral>>,
}

impl Debug for LexerATNConfigSet<'_> {
    fn fmt(&self, _f: &mut Formatter<'_>) -> Result<(), Error> {
        _f.write_str("LexerATNConfigSet")?;
        _f.debug_list().entries(self.configs.iter()).finish()
    }
}

impl Hash for LexerATNConfigSet<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.configs.iter().for_each(|c| c.hash(state));
    }
}

impl<'ephemeral> IntoIterator for LexerATNConfigSet<'ephemeral> {
    type Item = LexerATNConfig<'ephemeral>;
    type IntoIter =
        <ConfigSetStore<'ephemeral, LexerATNConfig<'ephemeral>> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.configs.into_iter()
    }
}

impl<'ephemeral> ConfigSet<'ephemeral> for LexerATNConfigSet<'ephemeral> {
    type ConfigType = LexerATNConfig<'ephemeral>;
    type FinalizedType<'x> = LexerATNConfigSet<'x>;

    fn new_empty() -> Self {
        LexerATNConfigSet {
            base: ATNConfigSetBase {
                dips_into_outer_context: false,
                full_ctx: true,
                has_semantic_context: false,
                unique_alt: 0,
            },
            configs: ConfigSetStore::<LexerATNConfig>::new_empty(),
        }
    }

    fn hash_code(&self) -> u64 {
        self.configs.hash_code()
    }

    fn finalize<'sim>(self, cache: &'sim PredictionContextCache) -> Self::FinalizedType<'sim> {
        LexerATNConfigSet {
            configs: self.configs.finalize(cache),
            ..self
        }
    }
}

impl<'ephemeral> LexerATNConfigSet<'ephemeral> {
    pub fn new(arena: &'ephemeral bumpalo::Bump) -> Self {
        LexerATNConfigSet {
            base: ATNConfigSetBase {
                dips_into_outer_context: false,
                full_ctx: true,
                has_semantic_context: false,
                unique_alt: 0,
            },
            configs: ConfigSetStore::new_ephemeral(arena),
        }
    }

    pub(crate) fn add(&mut self, config: LexerATNConfig<'ephemeral>) -> bool {
        let store = match &mut self.configs {
            ConfigSetStore::Scratch(s) => s,
            ConfigSetStore::Final(_) => panic!("Cannot add to read-only ATNConfigSet"),
        };

        if config.semantic_context() != &SemanticContext::NONE {
            self.base.has_semantic_context = true
        }

        if config.get_reaches_into_outer_context() > 0 {
            self.base.dips_into_outer_context = true
        }

        let key = Key::full(&config, store.configs.len());

        if store
            .lookup
            .find(key.hash_code(), |k| k.full_eq(&config, &store.configs))
            .is_none()
        {
            store.configs.push(config);
            store
                .lookup
                .insert_unique(key.hash_code(), key, Key::hash_code);
        }
        true
    }

    pub fn get_items(&self) -> impl Iterator<Item = &LexerATNConfig<'ephemeral>> {
        self.configs.iter()
    }

    pub fn length(&self) -> usize {
        self.configs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.configs.is_empty()
    }

    pub fn has_semantic_context(&self) -> bool {
        self.base.has_semantic_context
    }

    pub fn set_has_semantic_context(&mut self, _v: bool) {
        self.base.has_semantic_context = _v;
    }
}

enum Key {
    Full(u64, usize),
    Partial(u64, usize),
}

impl Key {
    fn full(config: &LexerATNConfig, index: usize) -> Self {
        Key::Full(Self::full_hash(config), index)
    }

    fn full_hash(config: &LexerATNConfig) -> u64 {
        hash64(config)
    }

    fn partial(config: &ATNConfig, index: usize) -> Self {
        Key::Partial(Self::partial_hash(config), index)
    }

    fn partial_hash(config: &ATNConfig) -> u64 {
        let mut hasher = FxHasher64::default();
        config.get_state().hash(&mut hasher);
        config.get_alt().hash(&mut hasher);
        config.semantic_context().hash(&mut hasher);
        hasher.finish()
    }

    fn hash_code(&self) -> u64 {
        match self {
            Key::Full(hash, _) => *hash,
            Key::Partial(hash, _) => *hash,
        }
    }

    fn index(&self) -> usize {
        match self {
            Key::Full(_, index) => *index,
            Key::Partial(_, index) => *index,
        }
    }

    fn partial_eq(&self, other: &ATNConfig, configs: &[ATNConfig]) -> bool {
        match self {
            Key::Full(..) => panic!("Full keys should not be compared with parser configs"),
            Key::Partial(_, index) => {
                let left = &configs[*index];
                left.get_state() == other.get_state()
                    && left.get_alt() == other.get_alt()
                    && left.semantic_context() == other.semantic_context()
            }
        }
    }

    fn full_eq(&self, other: &LexerATNConfig, configs: &[LexerATNConfig]) -> bool {
        match self {
            Key::Partial(..) => panic!("Partial keys should not be compared with lexer configs"),
            Key::Full(_, index) => {
                let left = &configs[*index];
                left == other
            }
        }
    }
}

pub enum ConfigSetStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral> + 'ephemeral,
{
    Scratch(ScratchStore<'ephemeral, AC>),
    Final(ImmutableStore<'ephemeral, AC>),
}

pub struct ScratchStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral>,
{
    lookup: HashTable<Key, &'ephemeral bumpalo::Bump>,
    configs: bumpalo::collections::Vec<'ephemeral, AC>,
}

impl<'ephemeral, AC> ScratchStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral>,
{
    fn hash_code(&self) -> u64 {
        let mut hasher = FxHasher64::default();
        self.configs.iter().for_each(|c| c.hash(&mut hasher));
        hasher.finish()
    }
}

pub struct ImmutableStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral>,
{
    cached_hash: u64,
    configs: &'ephemeral [AC],
}

impl<'ephemeral, AC> PartialEq for ConfigSetStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral>,
{
    fn eq(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }

        for (a, b) in self.iter().zip(other.iter()) {
            if a != b {
                return false;
            }
        }

        true
    }
}

impl<'ephemeral, AC> Eq for ConfigSetStore<'ephemeral, AC> where AC: ATNConfigType<'ephemeral> {}

impl<'ephemeral, AC> IntoIterator for ConfigSetStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral> + 'ephemeral,
{
    type Item = AC;
    type IntoIter = bumpalo::collections::vec::IntoIter<'ephemeral, AC>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            ConfigSetStore::Scratch(s) => s.configs.into_iter(),
            ConfigSetStore::Final(..) => panic!("Cannot consume a read-only ConfigSetStore"),
        }
    }
}

impl<'ephemeral, AC> ConfigSetStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral>,
{
    fn len(&self) -> usize {
        match self {
            ConfigSetStore::Scratch(s) => s.configs.len(),
            ConfigSetStore::Final(s) => s.configs.len(),
        }
    }

    fn is_empty(&self) -> bool {
        match self {
            ConfigSetStore::Scratch(s) => s.configs.is_empty(),
            ConfigSetStore::Final(s) => s.configs.is_empty(),
        }
    }

    fn read_only(&self) -> bool {
        match self {
            ConfigSetStore::Scratch(_) => false,
            ConfigSetStore::Final(_) => true,
        }
    }
}

impl ConfigSetStore<'static, ATNConfig<'static>> {
    fn new_empty() -> Self {
        static EMPTY_ATNCONFIGS: [ATNConfig; 0] = [];

        ConfigSetStore::Final(ImmutableStore {
            cached_hash: hash64(&EMPTY_ATNCONFIGS),
            configs: &EMPTY_ATNCONFIGS,
        })
    }
}

impl ConfigSetStore<'static, LexerATNConfig<'static>> {
    fn new_empty() -> Self {
        static EMPTY_LEXERATNCONFIGS: [LexerATNConfig; 0] = [];

        ConfigSetStore::Final(ImmutableStore {
            cached_hash: hash64(&EMPTY_LEXERATNCONFIGS),
            configs: &EMPTY_LEXERATNCONFIGS,
        })
    }
}

impl<'ephemeral, AC> ConfigSetStore<'ephemeral, AC>
where
    AC: ATNConfigType<'ephemeral> + 'ephemeral,
{
    fn new_ephemeral(ephemerals: &'ephemeral bumpalo::Bump) -> Self {
        ConfigSetStore::Scratch(ScratchStore {
            lookup: HashTable::with_capacity_in(7, ephemerals),
            configs: bumpalo::collections::Vec::new_in(ephemerals),
        })
    }

    fn iter(&self) -> impl Iterator<Item = &AC> {
        match self {
            ConfigSetStore::Scratch(s) => s.configs.iter(),
            ConfigSetStore::Final(s) => s.configs.iter(),
        }
    }

    fn hash_code(&self) -> u64 {
        match self {
            ConfigSetStore::Scratch(s) => s.hash_code(),
            ConfigSetStore::Final(s) => s.cached_hash,
        }
    }
}

impl<'ephemeral> ConfigSetStore<'ephemeral, ATNConfig<'ephemeral>> {
    fn finalize<'sim>(
        self,
        cache: &'sim PredictionContextCache,
    ) -> ConfigSetStore<'sim, ATNConfig<'sim>> {
        match self {
            ConfigSetStore::Scratch(s) => {
                let cached_hash = s.hash_code();

                ConfigSetStore::Final(ImmutableStore {
                    cached_hash,
                    configs: cache
                        .arena()
                        .alloc_slice_fill_iter(s.configs.into_iter().map(|c| c.finalize(cache))),
                })
            }
            ConfigSetStore::Final(_) => unsafe {
                std::mem::transmute::<Self, ConfigSetStore<'sim, ATNConfig<'sim>>>(self)
            },
        }
    }
}

impl<'ephemeral> ConfigSetStore<'ephemeral, LexerATNConfig<'ephemeral>> {
    fn finalize<'sim>(
        self,
        cache: &'sim PredictionContextCache,
    ) -> ConfigSetStore<'sim, LexerATNConfig<'sim>> {
        match self {
            ConfigSetStore::Scratch(s) => {
                let cached_hash = s.hash_code();

                ConfigSetStore::Final(ImmutableStore {
                    cached_hash,
                    configs: cache
                        .arena()
                        .alloc_slice_fill_iter(s.configs.into_iter().map(|c| c.finalize(cache))),
                })
            }
            ConfigSetStore::Final(_) => unsafe {
                std::mem::transmute::<Self, ConfigSetStore<'sim, LexerATNConfig<'sim>>>(self)
            },
        }
    }
}