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
use std::any::{Any, TypeId};
use std::borrow::BorrowMut;
use std::cell::{Cell, RefCell};
use std::fmt::{Debug, Formatter};
use std::iter::from_fn;
use std::marker::PhantomData;
use std::rc::{Rc, Weak};
use crate::atn::INVALID_ALT;
use crate::parser::ParserNodeType;
use crate::parser_rule_context::{BaseParserRuleContext, ParserRuleContext};
use crate::token_factory::{CommonTokenFactory, TokenFactory};
use crate::tree::{ParseTree, ParseTreeListener, ParseTreeVisitor, Tree};
use better_any::{Tid, TidAble};
pub trait RuleContext<'input>: CustomRuleContext<'input> {
fn get_invoking_state(&self) -> isize { -1 }
fn set_invoking_state(&self, t: isize) {}
fn is_empty(&self) -> bool { self.get_invoking_state() == -1 }
fn get_parent_ctx(&self) -> Option<Rc<<Self::Ctx as ParserNodeType<'input>>::Type>> { None }
fn set_parent(&self, parent: &Option<Rc<<Self::Ctx as ParserNodeType<'input>>::Type>>) {}
}
pub(crate) fn states_stack<'input, T: ParserRuleContext<'input> + ?Sized + 'input>(
mut ctx: Rc<T>,
) -> impl Iterator<Item = isize>
where
T::Ctx: ParserNodeType<'input, Type = T>,
{
from_fn(move || {
if ctx.get_invoking_state() < 0 {
None
} else {
let state = ctx.get_invoking_state();
ctx = ctx.get_parent_ctx().unwrap();
Some(state)
}
})
}
#[derive(Tid)]
pub struct EmptyCustomRuleContext<'a, TF: TokenFactory<'a> + 'a>(
pub(crate) PhantomData<&'a TF::Tok>,
);
impl<'a, TF: TokenFactory<'a> + 'a> CustomRuleContext<'a> for EmptyCustomRuleContext<'a, TF> {
type TF = TF;
type Ctx = EmptyContextType<'a, TF>;
fn get_rule_index(&self) -> usize { usize::max_value() }
}
pub type EmptyContext<'a, TF> =
dyn ParserRuleContext<'a, TF = TF, Ctx = EmptyContextType<'a, TF>> + 'a;
#[derive(Tid)]
pub struct EmptyContextType<'a, TF: TokenFactory<'a>>(pub PhantomData<&'a TF>);
impl<'a, TF: TokenFactory<'a>> ParserNodeType<'a> for EmptyContextType<'a, TF> {
type TF = TF;
type Type = dyn ParserRuleContext<'a, TF = Self::TF, Ctx = Self> + 'a;
type Visitor = dyn ParseTreeVisitor<'a, Self> + 'a;
}
pub trait CustomRuleContext<'input> {
type TF: TokenFactory<'input> + 'input;
type Ctx: ParserNodeType<'input, TF = Self::TF>;
fn get_rule_index(&self) -> usize;
fn get_alt_number(&self) -> isize { INVALID_ALT }
fn set_alt_number(&self, _alt_number: isize) {}
}
#[derive(Tid)]
pub struct BaseRuleContext<'input, ExtCtx: CustomRuleContext<'input>> {
pub(crate) parent_ctx: RefCell<Option<Weak<<ExtCtx::Ctx as ParserNodeType<'input>>::Type>>>,
invoking_state: Cell<isize>,
pub(crate) ext: ExtCtx,
}
impl<'input, ExtCtx: CustomRuleContext<'input>> BaseRuleContext<'input, ExtCtx> {
pub(crate) fn new_ctx(
parent_ctx: Option<Rc<<ExtCtx::Ctx as ParserNodeType<'input>>::Type>>,
invoking_state: isize,
ext: ExtCtx,
) -> Self {
BaseRuleContext {
parent_ctx: RefCell::new(parent_ctx.as_ref().map(Rc::downgrade)),
invoking_state: Cell::new(invoking_state),
ext,
}
}
}
impl<'input, ExtCtx: CustomRuleContext<'input>> CustomRuleContext<'input>
for BaseRuleContext<'input, ExtCtx>
{
type TF = ExtCtx::TF;
type Ctx = ExtCtx::Ctx;
fn get_rule_index(&self) -> usize { self.ext.get_rule_index() }
}
impl<'input, ExtCtx: CustomRuleContext<'input>> RuleContext<'input>
for BaseRuleContext<'input, ExtCtx>
{
fn get_invoking_state(&self) -> isize { self.invoking_state.get() }
fn set_invoking_state(&self, t: isize) { self.invoking_state.set(t) }
fn get_parent_ctx(&self) -> Option<Rc<<ExtCtx::Ctx as ParserNodeType<'input>>::Type>> {
self.parent_ctx
.borrow()
.as_ref()
.map(Weak::upgrade)
.flatten()
}
fn set_parent(&self, parent: &Option<Rc<<ExtCtx::Ctx as ParserNodeType<'input>>::Type>>) {
*self.parent_ctx.borrow_mut() = parent.as_ref().map(Rc::downgrade);
}
}
impl<'input, ExtCtx: CustomRuleContext<'input>> Debug for BaseRuleContext<'input, ExtCtx> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { unimplemented!() }
}
impl<'input, ExtCtx: CustomRuleContext<'input>> Tree<'input> for BaseRuleContext<'input, ExtCtx> {}
impl<'input, ExtCtx: CustomRuleContext<'input>> ParseTree<'input>
for BaseRuleContext<'input, ExtCtx>
{
}
impl<'input, ExtCtx: CustomRuleContext<'input> + TidAble<'input>> ParserRuleContext<'input>
for BaseRuleContext<'input, ExtCtx>
{
}