dbt-antlr4 1.2.3

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
//! Minimal parser node
use std::borrow::{Borrow, BorrowMut};
use std::fmt::{Debug, Formatter};
use std::iter::from_fn;
use std::marker::PhantomData;

use crate::atn::INVALID_ALT;
use crate::errors::ANTLRError;
use crate::parser_rule_context::{
    BaseParserRuleContext, EmptyParserRuleContext, ParserRuleContext,
};
use crate::tree::{
    ErrorNode, NodeInner, NodeKindType, ParseTreeListener, TerminalNode, Tree, TreeNode,
};
use crate::{
    impl_node_inner, impl_node_kind, impl_parser_rule_context, impl_rule_context,
    impl_tree_trait_delegates,
};
use std::any::type_name;

/// Language-agnostic, dyn-compatible, read-only interface to the AST.
pub trait RuleContext<'arena> {
    /// Internal parser state
    fn get_invoking_state(&self) -> i32;

    /// A context is empty if there is no invoking state; meaning nobody called
    /// current context. Which is usually true for the root of the syntax tree
    fn is_empty(&self) -> bool {
        self.get_invoking_state() == -1
    }

    /// Get parent context
    fn get_parent_ctx(&self) -> Option<&'arena dyn RuleContext<'arena>>;

    /// Rule index that corresponds to this context type
    fn get_rule_index(&self) -> usize;

    fn get_alt_number(&self) -> i32;

    /// Returns text representation of current node type,
    /// rule name for context nodes and token text for terminal nodes
    fn get_node_text(&self, rule_names: &[&str]) -> String;
}

pub(crate) fn states_stack<'input, 'arena, Node>(
    mut node: &'arena TreeNode<'input, 'arena, Node>,
) -> impl Iterator<Item = i32> + 'arena
where
    'input: 'arena,
    Node: NodeKindType<'arena> + 'arena,
{
    from_fn(move || {
        if node.get_invoking_state() < 0 {
            None
        } else {
            let state = node.get_invoking_state();
            node = Tree::get_parent(node).unwrap();
            Some(state)
        }
    })
}

/// Implemented by generated parser for context extension for particular rule
#[allow(missing_docs)]
pub trait CustomRuleContext<'input, 'arena>: Debug + Sized
where
    'input: 'arena,
{
    type NodeKind: NodeKindType<'arena>;

    fn node_tag() -> Self::NodeKind;

    fn make_node(
        arena: &'arena crate::arena::Arena,
        ctx: BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>,
    ) -> *mut TreeNode<'input, 'arena, Self::NodeKind>;

    fn cast_from<'a>(
        node: &'a TreeNode<'input, 'arena, Self::NodeKind>,
    ) -> Option<&'a BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>>;

    fn cast_from_mut<'a>(
        node: &'a mut TreeNode<'input, 'arena, Self::NodeKind>,
    ) -> Option<&'a mut BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>>;

    /// Rule index that corresponds to this context type
    fn get_rule_index(&self) -> usize;

    /// For rule associated with this parse tree internal node, return the outer
    /// alternative number used to match the input. Default implementation does
    /// not compute nor store this alt num. Create a subclass of
    /// ParserRuleContext with backing field and set option contextSuperClass.
    /// to set it.
    ///
    /// @since 4.5.3
    fn get_alt_number(&self) -> i32 {
        INVALID_ALT
    }

    /// Set the outer alternative number for this context node. Default
    /// implementation does nothing to avoid backing field overhead for trees
    /// that don't need it.  Create a subclass of ParserRuleContext with backing
    /// field and set option contextSuperClass.
    ///
    /// @since 4.5.3
    fn set_alt_number(&mut self, _alt_number: i32) {}

    /// Returns text representation of current node type,
    /// rule name for context nodes and token text for terminal nodes
    fn get_node_text(&self, rule_names: &[&str]) -> String {
        let rule_index = self.get_rule_index();
        let rule_name = rule_names[rule_index];
        let alt_number = self.get_alt_number();
        if alt_number != INVALID_ALT {
            return format!("{}:{}", rule_name, alt_number);
        }
        rule_name.to_owned()
    }
}

#[derive(Debug, Default)]
#[doc(hidden)]
pub struct EmptyCustomRuleContext<'input, 'arena>(
    pub(crate) PhantomData<(&'input (), *mut &'arena ())>,
);

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u16)]
pub enum EmptyNodeKind {
    EmptyContext,
    Terminal,
    Error,
}

impl_node_kind! {EmptyNodeKind { EmptyContext(EmptyContextAll),; }; }

#[derive(Debug)]
#[repr(u16)]
pub enum EmptyContextAll<'input, 'arena> {
    EmptyContext(EmptyParserRuleContext<'input, 'arena>),
    Error(EmptyParserRuleContext<'input, 'arena>),
}

impl_node_inner!(EmptyNodeKind::EmptyContext::EmptyContextAll {
    EmptyContext,
    Error,
});
impl_tree_trait_delegates!(EmptyNodeKind::EmptyContextAll {
    EmptyContext,
    Error,
});
impl_rule_context!(EmptyContextAll {} { EmptyContext, Error, });
impl_parser_rule_context!(EmptyContextAll {} { EmptyContext, Error, });
//impl_listener_dispatch!( EmptyListener::EmptyNodeKind::EmptyContextAll { EmptyContext(enter_empty, exit_empty), });

pub type EmptyRuleNode<'input, 'arena> = TreeNode<'input, 'arena, EmptyNodeKind>;

pub trait EmptyListener<'arena>: ParseTreeListener<'arena, EmptyNodeKind> {
    fn enter_empty<'input>(
        &mut self,
        _ctx: &EmptyParserRuleContext<'input, 'arena>,
    ) -> Result<(), ANTLRError> {
        Ok(())
    }

    fn exit_empty<'input>(
        &mut self,
        _ctx: &EmptyParserRuleContext<'input, 'arena>,
    ) -> Result<(), ANTLRError> {
        Ok(())
    }
}

pub trait EmptyVisitor<'input, 'arena>
where
    'input: 'arena,
{
    type Return: Default;

    fn visit(&mut self, node: &EmptyRuleNode<'input, 'arena>) -> Result<Self::Return, ANTLRError>;

    /// Called on terminal(leaf) node
    fn visit_terminal(
        &mut self,
        _node: &TerminalNode<'input, 'arena>,
    ) -> Result<Self::Return, ANTLRError> {
        Ok(Self::Return::default())
    }

    /// Called on error node
    fn visit_error_node(
        &mut self,
        _node: &ErrorNode<'input, 'arena>,
    ) -> Result<Self::Return, ANTLRError> {
        Ok(Self::Return::default())
    }

    fn visit_children(
        &mut self,
        node: &dyn NodeInner<'input, 'arena, EmptyNodeKind>,
    ) -> Result<Self::Return, ANTLRError> {
        let mut result = Self::Return::default();
        for child in node.iter_child_nodes() {
            if !self.should_visit_next_child(child, &result) {
                break;
            }

            let child_result = self.visit(child)?;
            result = self.aggregate_results(result, child_result)?;
        }
        Ok(result)
    }

    fn aggregate_results(
        &self,
        _aggregate: Self::Return,
        next: Self::Return,
    ) -> Result<Self::Return, ANTLRError> {
        Ok(next)
    }

    fn should_visit_next_child(
        &self,
        _node: &EmptyRuleNode<'input, 'arena>,
        _current: &Self::Return,
    ) -> bool {
        true
    }

    fn visit_empty(
        &mut self,
        ctx: &EmptyParserRuleContext<'input, 'arena>,
    ) -> Result<Self::Return, ANTLRError> {
        self.visit_children(ctx)
    }
}

pub trait Visitable<'input, 'arena>
where
    'input: 'arena,
{
    fn accept<V>(
        &'arena self,
        visitor: &mut V,
    ) -> Result<<V as EmptyVisitor<'input, 'arena>>::Return, ANTLRError>
    where
        V: EmptyVisitor<'input, 'arena> + ?Sized;
}

// impl_rule_node!(EmptyRuleNode { ; Empty(enter_empty, exit_empty, visit_empty), }; listener = dyn EmptyListener<'input, 'arena>, visitor = EmptyVisitor, );

// impl_defaults!(EmptyRuleNode);

impl<'input, 'arena> CustomRuleContext<'input, 'arena> for EmptyCustomRuleContext<'input, 'arena>
where
    'input: 'arena,
{
    type NodeKind = EmptyNodeKind;

    fn node_tag() -> EmptyNodeKind {
        EmptyNodeKind::EmptyContext
    }

    fn get_rule_index(&self) -> usize {
        usize::MAX
    }

    fn make_node(
        arena: &'arena crate::arena::Arena,
        ctx: BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>,
    ) -> *mut TreeNode<'input, 'arena, EmptyNodeKind> {
        arena.alloc_labeled_node(EmptyContextAll::EmptyContext(ctx))
    }

    fn cast_from<'a>(
        node: &'a TreeNode<'input, 'arena, Self::NodeKind>,
    ) -> Option<&'a BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>> {
        if node.node_tag == Self::node_tag() {
            let ctx = unsafe { &*(node as *const _ as *const EmptyContextAll<'input, 'arena>) };
            match ctx {
                EmptyContextAll::EmptyContext(ctx) => Some(ctx),
                EmptyContextAll::Error(ctx) => Some(ctx),
            }
        } else {
            None
        }
    }

    fn cast_from_mut<'a>(
        node: &'a mut TreeNode<'input, 'arena, Self::NodeKind>,
    ) -> Option<&'a mut BaseParserRuleContext<'input, 'arena, Self, Self::NodeKind>> {
        if node.node_tag == Self::node_tag() {
            let ctx = unsafe { &mut *(node as *mut _ as *mut EmptyContextAll<'input, 'arena>) };
            match ctx {
                EmptyContextAll::EmptyContext(ctx) => Some(ctx),
                EmptyContextAll::Error(ctx) => Some(ctx),
            }
        } else {
            None
        }
    }
}

pub type EmptyRuleContext<'input, 'arena> =
    BaseRuleContext<'input, 'arena, EmptyCustomRuleContext<'input, 'arena>, EmptyNodeKind>;

/// Core rule context implementation -- this defines the minimal set of states
/// required for the Antlr parsing algorithm to function.
///
/// This is the Rust version of the `RuleContext` "abstract base class", it will
/// be specialized into language-specific concrete types by monomorphizing the
/// `ExtCtx` type parameter, which is implemented by generated code.
#[repr(C)]
pub struct BaseRuleContext<'input, 'arena, Ext, NodeKind>
where
    'input: 'arena,
    NodeKind: NodeKindType<'arena>,
    Ext: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
{
    parent: Option<&'arena TreeNode<'input, 'arena, NodeKind>>,
    pub(crate) ext: Ext,

    // Covariant over 'input, invariant over 'arena
    _marker: PhantomData<(&'input (), *mut &'arena ())>,
}

#[allow(missing_docs)]
impl<'input, 'arena, Ext, NodeKind> BaseRuleContext<'input, 'arena, Ext, NodeKind>
where
    'input: 'arena,
    NodeKind: NodeKindType<'arena>,
    Ext: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
{
    pub(crate) fn new(
        parent: Option<&'arena TreeNode<'input, 'arena, NodeKind>>,
        ext: Ext,
    ) -> Self {
        Self {
            parent,
            ext,
            _marker: PhantomData,
        }
    }

    pub(crate) fn morph<Tgt>(
        self,
        ctor: impl FnOnce(Ext) -> Tgt,
    ) -> BaseRuleContext<'input, 'arena, Tgt, NodeKind>
    where
        Tgt: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
    {
        BaseRuleContext {
            parent: self.parent,
            ext: ctor(self.ext),
            _marker: PhantomData,
        }
    }

    #[inline]
    pub fn parent(&self) -> Option<&'arena TreeNode<'input, 'arena, NodeKind>> {
        self.parent
    }

    #[inline]
    pub fn has_parent(&self) -> bool {
        self.parent.is_some()
    }

    pub(crate) fn set_parent(
        &mut self,
        parent: Option<&'arena TreeNode<'input, 'arena, NodeKind>>,
    ) {
        self.parent = parent;
    }

    pub(crate) fn set_alt_number(&mut self, _alt_number: i32) {
        self.ext.set_alt_number(_alt_number)
    }

    pub(crate) fn get_alt_number(&self) -> i32 {
        self.ext.get_alt_number()
    }

    pub(crate) fn get_rule_index(&self) -> usize {
        self.ext.get_rule_index()
    }

    pub(crate) fn get_node_text(&self, rule_names: &[&str]) -> String {
        self.ext.get_node_text(rule_names)
    }
}

impl<'input, 'arena, Ext, NodeKind> Borrow<Ext> for BaseRuleContext<'input, 'arena, Ext, NodeKind>
where
    'input: 'arena,
    NodeKind: NodeKindType<'arena>,
    Ext: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
{
    fn borrow(&self) -> &Ext {
        &self.ext
    }
}

impl<'input, 'arena, Ext, NodeKind> BorrowMut<Ext>
    for BaseRuleContext<'input, 'arena, Ext, NodeKind>
where
    'input: 'arena,
    NodeKind: NodeKindType<'arena>,
    Ext: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
{
    fn borrow_mut(&mut self) -> &mut Ext {
        &mut self.ext
    }
}

// impl<'input, 'arena, Ext> RuleContext<'arena> for BaseRuleContext<'input, 'arena, Ext>
// where
//     'input: 'arena,
//     Ext: CustomRuleContext<'arena> + 'arena,
// {
//     #[inline(always)]
//     fn get_invoking_state(&self) -> i32 {
//         self.invoking_state
//     }

//     fn get_parent_ctx(&self) -> Option<&'arena dyn RuleContext<'arena>> {
//         self.parent
//             .map(|rc| rc.get_rule_context() as &dyn RuleContext<'arena>)
//     }

//     fn get_rule_index(&self) -> usize {
//         self.ext.get_rule_index()
//     }

//     fn get_alt_number(&self) -> i32 {
//         self.ext.get_alt_number()
//     }

//     fn is_empty(&self) -> bool {
//         self.get_invoking_state() == -1
//     }

//     fn get_node_text(&self, rule_names: &[&str]) -> String {
//         self.ext.get_node_text(rule_names)
//     }
// }

impl<'input, 'arena, Ext, NodeKind> Debug for BaseRuleContext<'input, 'arena, Ext, NodeKind>
where
    'input: 'arena,
    NodeKind: NodeKindType<'arena>,
    Ext: CustomRuleContext<'input, 'arena, NodeKind = NodeKind>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(type_name::<Self>())
            .field("parent", &self.parent)
            .field("ext", &self.ext)
            .field("..", &"..")
            .finish()
    }
}