dbt-antlr-codegen 0.10.0

ANTLR v4 grammar compiler and Rust source generator
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Bo Lin
//! Optional-postfix decisions (`X Y?`) and their take/skip conflict.
//!
//! A two-alternative block where alternative 1 enters a postfix element and
//! alternative 2 skips straight to the block end is a dangling-else shape. Its
//! take/skip conflict resolves to *take* when the skip reading cannot win on
//! its own. In that case every token the skip reading consumes is also
//! consumed by the take structure, so both readings meet at the same block
//! end.
//!
//! The rule needs two facts about a conflict set:
//!
//! - **Mirror** ([`PostfixShape::skip_mirrors`]): every skip configuration
//!   consumes for the decision's own discipline. Inside the decision
//!   invocation, it consumes the continuation of the decision rule. After a
//!   boundary pop, it consumes on the take path of an enclosing invocation (the
//!   dangling-else reading), or in an invocation whose deepest context frame
//!   returns into the decision's block end. Any other skip consumes tokens
//!   outside the discipline, and skip can genuinely win:
//!   - The alias family bottoms out elsewhere: its clause continuations
//!     (`LIMIT`, `FROM`, ...) follow the skipped alias.
//!   - A new invocation of the decision rule after the skip owns its tokens:
//!     in `s : r r ';'` with `r : 'A' y? | '-' 'B'` and `y : '-' 'B'`, the
//!     second `r` reads the `- B` of `A - B ;`.
//!
//!   [`PostfixShape::is_foreign_call`] marks the call that starts such an
//!   invocation, because widening and merged contexts can remove its frame
//!   later.
//! - **Real take**: a take configuration that is not widened attests that
//!   taking is viable.
//!
//! The analyzer also rejects shapes where an element of the rule follows the
//! optional one and its FIRST set intersects the optional's, whether that
//! element is mandatory or optional. There the skipped input can serve that
//! element instead (`TRIM(x)`, `IN foo`), so skip genuinely wins and the rule
//! must not apply.

use std::collections::{BTreeSet, HashMap, HashSet};
use std::sync::Arc;

use dbt_antlr_runtime::analysis::{
    ATN, ATNStateRef, ATNStateType, BOUNDARY_TAINT, BasicBlockStartState, BitSet,
    FOREIGN_LINEAGE_TAINT, IntervalSetBuf, MIDWAY_TAINT, NotSetTransition, PlusBlockStartState,
    PredictionContextRef, ROOTED_TAINT, RuleTransition, StarBlockStartState,
    TOKEN_MIN_USER_TOKEN_TYPE, TransitionType,
};

use super::closure::{in_popped_frame, positions_owing_input, rule_positions};

/// The optional-postfix shape of one decision.
#[derive(Clone, Debug)]
pub(super) struct PostfixShape {
    /// State number of the decision block's end state. The mirror check
    /// requires every descended skip configuration's deepest frame to return
    /// into this state.
    pub(super) block_end_state: ATNStateRef,
    /// The positions of the take alternative, from the decision to the block
    /// end. Rule calls contribute their follow states.
    take_states: Arc<BitSet>,
    /// The take-path positions that still owe input before the block end.
    midway_states: Arc<BitSet>,
}

impl PostfixShape {
    /// Detects the optional-postfix shape of a decision, or returns `None`.
    pub(super) fn analyze(atn: &ATN, decision_state: ATNStateRef) -> Option<Self> {
        let block = block_end_state(decision_state)?;
        let transitions = decision_state.get_transitions();
        if transitions.len() != 2 {
            return None;
        }
        if !epsilon_path_to(transitions[1].get_target(), block) {
            return None;
        }
        let first = first_element(transitions[0].get_target())?;
        if !first
            .get_transitions()
            .iter()
            .any(|transition| transition.try_as::<RuleTransition>().is_some())
        {
            return None;
        }

        let rule_stop = atn.rule_to_stop_state[decision_state.get_rule_index() as usize];
        let continuation = look(atn, block, rule_stop);
        let take = look(atn, transitions[0].get_target(), block);
        if continuation.intersection(&take).next().is_some() {
            return None;
        }

        Some(Self {
            block_end_state: block,
            take_states: Arc::new(rule_positions(transitions[0].get_target(), Some(block))),
            midway_states: Arc::new(positions_owing_input(
                atn,
                decision_state.get_rule_index(),
                block,
            )),
        })
    }

    /// Whether a skip configuration at `state` consumes for the decision's own
    /// discipline (see the module documentation).
    ///
    /// A configuration that did not pop the decision boundary is inside the
    /// decision invocation, so the decision rule's positions are its own. In
    /// the frame that a configuration popped into, only the take path of the
    /// enclosing invocation mirrors the take reading, and only after the
    /// configuration came back through the decision ([`MIDWAY_TAINT`]). In any
    /// other invocation, the deepest context frame must return into the block
    /// end.
    pub(super) fn skip_mirrors(
        &self,
        state: ATNStateRef,
        context: Option<PredictionContextRef<'_>>,
        taint: i32,
    ) -> bool {
        if taint & (FOREIGN_LINEAGE_TAINT | MIDWAY_TAINT | ROOTED_TAINT) != 0 {
            return false;
        }
        if state.get_rule_index() == self.block_end_state.get_rule_index() {
            if taint & BOUNDARY_TAINT == 0 {
                return true;
            }
            if in_popped_frame(context) {
                return self.is_take_state(state);
            }
        }
        context.is_some_and(|context| {
            bottom_frame_is(context, self.block_end_state, &mut HashSet::new())
        })
    }

    /// Whether a rule call of a boundary-popped configuration at `state`
    /// starts an invocation outside the decision's discipline: the call leaves
    /// the frame that the configuration popped into from a position off the
    /// take path.
    pub(super) fn is_foreign_call(
        &self,
        state: ATNStateRef,
        context: Option<PredictionContextRef<'_>>,
    ) -> bool {
        in_popped_frame(context) && !self.is_take_state(state)
    }

    /// Whether a boundary chase that returns into `state` lands in the middle
    /// of the take path of an enclosing invocation, which still owes input
    /// before its block end.
    pub(super) fn is_midway(&self, state: ATNStateRef) -> bool {
        self.is_take_state(state)
            && self
                .midway_states
                .contains(state.get_state_number() as usize)
    }

    fn is_take_state(&self, state: ATNStateRef) -> bool {
        self.take_states.contains(state.get_state_number() as usize)
    }
}

/// Whether every context frame adjacent to the empty context (the deepest
/// frame of each context branch) is a return into `end_state`.
///
/// A skip configuration mirrors the take reading only when it re-entered the
/// decision through this decision's own take path. A branch that reaches the
/// empty context without such a frame consumed tokens fully outside the
/// decision's discipline.
fn bottom_frame_is(
    context: PredictionContextRef<'_>,
    end_state: ATNStateRef,
    visited: &mut HashSet<*const ()>,
) -> bool {
    if context.is_empty() {
        return false;
    }
    if !visited.insert(context.as_ref() as *const _ as *const ()) {
        return true;
    }
    for index in 0..context.length() {
        let return_state = context.get_return_state(index);
        match context.get_parent(index) {
            Some(parent) if !parent.is_empty() => {
                if !bottom_frame_is(parent, end_state, visited) {
                    return false;
                }
            }
            _ => {
                if return_state != end_state {
                    return false;
                }
            }
        }
    }
    true
}

/// The block start state's end state, when `state` starts a block.
fn block_end_state(state: ATNStateRef) -> Option<ATNStateRef> {
    let number = state.get_state_number();
    if number < 0 {
        return None;
    }
    match state.state_type() {
        ATNStateType::BasicBlockStart => state
            .try_as::<BasicBlockStartState>()
            .map(|start| start.end_state),
        ATNStateType::StarBlockStart => state
            .try_as::<StarBlockStartState>()
            .map(|start| start.end_state),
        ATNStateType::PlusBlockStart => state
            .try_as::<PlusBlockStartState>()
            .map(|start| start.end_state),
        _ => None,
    }
}

/// Whether an epsilon-only path leads from `from` to `to`.
fn epsilon_path_to(from: ATNStateRef, to: ATNStateRef) -> bool {
    let mut visited: BitSet = BitSet::new();
    let mut work = vec![from];
    while let Some(state) = work.pop() {
        if state == to {
            return true;
        }
        if state.get_state_number() < 0 || !visited.insert(state.get_state_number() as usize) {
            continue;
        }
        for transition in state.get_transitions() {
            if transition.is_epsilon() {
                work.push(transition.get_target());
            }
        }
    }
    false
}

/// The first state with a non-epsilon outgoing transition reachable over
/// epsilon links. A rule invocation is an element, not an epsilon link.
fn first_element(start: ATNStateRef) -> Option<ATNStateRef> {
    let mut visited: BitSet = BitSet::new();
    let mut work = vec![start];
    while let Some(state) = work.pop() {
        if state.get_state_number() < 0 || !visited.insert(state.get_state_number() as usize) {
            continue;
        }
        for transition in state.get_transitions() {
            if transition.try_as::<RuleTransition>().is_some() || !transition.is_epsilon() {
                return Some(state);
            }
            work.push(transition.get_target());
        }
    }
    None
}

/// `LOOK(start)` until `stop` or the end of the start state's rule.
fn look(atn: &ATN, start: ATNStateRef, stop: ATNStateRef) -> BitSet {
    let mut tokens = BitSet::new();
    let mut visited: HashSet<(i32, usize)> = HashSet::new();
    let mut work: Vec<(ATNStateRef, Vec<ATNStateRef>)> = vec![(start, Vec::new())];
    while let Some((state, returns)) = work.pop() {
        let number = state.get_state_number();
        if number < 0 || state == stop || !visited.insert((number, returns.len())) {
            continue;
        }
        if state.state_type() == ATNStateType::RuleStop {
            if let Some(follow) = returns.last() {
                let mut next = returns.clone();
                next.pop();
                work.push((*follow, next));
            }
            continue;
        }
        for transition in state.get_transitions() {
            if let Some(rule) = transition.try_as::<RuleTransition>() {
                let mut next = returns.clone();
                next.push(rule.follow_state);
                work.push((transition.get_target(), next));
                continue;
            }
            if transition.is_epsilon() {
                work.push((transition.get_target(), returns.clone()));
                continue;
            }
            let label: IntervalSetBuf = match transition.transition_type() {
                TransitionType::NotSet => transition
                    .try_as::<NotSetTransition>()
                    .expect("not-set transition")
                    .get_label()
                    .expect("not-set label")
                    .complement(TOKEN_MIN_USER_TOKEN_TYPE, atn.max_token_type),
                TransitionType::Wildcard => {
                    let mut all = IntervalSetBuf::new();
                    if atn.max_token_type >= TOKEN_MIN_USER_TOKEN_TYPE {
                        all.add_range(TOKEN_MIN_USER_TOKEN_TYPE, atn.max_token_type);
                    }
                    all
                }
                _ => match transition.get_label() {
                    Some(label) => label.to_interval_set_buf(),
                    None => continue,
                },
            };
            add_intervals(&mut tokens, &label);
        }
    }
    tokens
}

fn add_intervals(tokens: &mut BitSet, label: &IntervalSetBuf) {
    for interval in label.as_interval_set().as_slice() {
        for token in interval.a..=interval.b {
            if token >= 0 {
                tokens.insert(token as usize);
            }
        }
    }
}

/// The ATN states that `start` reaches by epsilon, `start` included. Rule
/// stops are leaves.
pub(super) fn epsilon_closure(start: ATNStateRef) -> BitSet {
    let mut out = BitSet::new();
    let mut work = vec![start];
    while let Some(state) = work.pop() {
        let number = state.get_state_number();
        if number < 0 || !out.insert(number as usize) {
            continue;
        }
        if state.state_type() == ATNStateType::RuleStop {
            continue;
        }
        for transition in state.get_transitions() {
            if transition.is_epsilon() {
                work.push(transition.get_target());
            }
        }
    }
    out
}

/// The guard roots of one skip consumption outside the decision's discipline.
pub(super) fn collect_guard_roots(
    state: ATNStateRef,
    context: Option<PredictionContextRef<'_>>,
    mirror: &dyn Fn(ATNStateRef) -> bool,
    sites: &HashMap<i32, i32>,
    out: &mut BTreeSet<i32>,
) {
    let Some(context) = context.filter(|context| !context.is_empty()) else {
        out.insert(state.get_state_number());
        return;
    };
    let mut visited: HashSet<*const ()> = HashSet::new();
    collect_roots_in(state, &context, mirror, sites, out, &mut visited)
}

fn collect_roots_in(
    state: ATNStateRef,
    context: &PredictionContextRef<'_>,
    mirror: &dyn Fn(ATNStateRef) -> bool,
    sites: &HashMap<i32, i32>,
    out: &mut BTreeSet<i32>,
    visited: &mut HashSet<*const ()>,
) {
    if context.is_empty() {
        out.insert(state.get_state_number());
        return;
    }
    if !visited.insert(context.as_ref() as *const _ as *const ()) {
        return;
    }
    for index in 0..context.length() {
        let return_state = context.get_return_state(index);
        match context.get_parent(index) {
            Some(parent) if !parent.is_empty() => {
                collect_roots_in(state, &parent, mirror, sites, out, visited);
            }
            _ => {
                if return_state == ATNStateRef::empty_return() {
                    out.insert(state.get_state_number());
                } else if !mirror(return_state) {
                    let number = return_state.get_state_number();
                    out.insert(sites.get(&number).copied().unwrap_or(number));
                }
            }
        }
    }
}

/// The follow regions of the rule invocations of an ATN, from which the guard
/// sets of a stack guard come.
///
/// A guard group has a danger set, and all guard groups of a table share one
/// pass set (sorted invoking state numbers):
///
/// - Danger: the state invokes a rule whose follow region can reach a guard
///   root of the group, so the guard defers at such a frame.
/// - Pass: the follow region can reach the stop of its own rule, so the stack
///   walk continues to the caller. The walk tests danger before pass.
///
/// A state with several rule transitions is danger when any follow is
/// dangerous, and pass only when every follow can pop (conservative in both
/// directions).
pub(super) struct GuardRegions {
    /// Per invoking state, the indexes of its follow regions in `regions`.
    calls: Vec<(i32, Vec<usize>)>,
    /// The epsilon closure of every follow state.
    regions: Vec<BitSet>,
    pass: Vec<i32>,
}

impl GuardRegions {
    pub(super) fn of(atn: &ATN) -> Self {
        let mut calls = Vec::new();
        let mut regions = Vec::new();
        let mut region_of: HashMap<i32, usize> = HashMap::new();
        let mut pass = Vec::new();
        for state in atn.iter_states() {
            let rule_index = state.get_rule_index();
            let rule_stop = if rule_index >= 0 {
                atn.rule_to_stop_state[rule_index as usize]
            } else {
                ATNStateRef::invalid()
            };
            let mut all_pass = true;
            let mut follows = Vec::new();
            for transition in state.get_transitions() {
                let Some(rule) = transition.try_as::<RuleTransition>() else {
                    continue;
                };
                let follow = rule.follow_state.get_state_number();
                let index = *region_of.entry(follow).or_insert_with(|| {
                    regions.push(epsilon_closure(rule.follow_state));
                    regions.len() - 1
                });
                if rule_stop.get_state_number() < 0
                    || !regions[index].contains(rule_stop.get_state_number() as usize)
                {
                    all_pass = false;
                }
                follows.push(index);
            }
            if follows.is_empty() {
                continue;
            }
            if all_pass {
                pass.push(state.get_state_number());
            }
            calls.push((state.get_state_number(), follows));
        }
        Self {
            calls,
            regions,
            pass,
        }
    }

    /// The sorted danger set of a guard-root set.
    pub(super) fn danger(&self, roots: &BTreeSet<i32>) -> Vec<i32> {
        self.calls
            .iter()
            .filter(|(_, follows)| {
                follows.iter().any(|&index| {
                    roots
                        .iter()
                        .any(|&root| self.regions[index].contains(root as usize))
                })
            })
            .map(|&(state, _)| state)
            .collect()
    }

    /// The sorted pass set.
    pub(super) fn pass(&self) -> &[i32] {
        &self.pass
    }
}