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
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::fmt::Debug;

use crate::atn_state::ATNStateRef;
use crate::interval_set::IntervalSet;
use crate::lexer::{LEXER_MAX_CHAR_VALUE, LEXER_MIN_CHAR_VALUE};
use crate::semantic_context::SemanticContext;

const _TRANSITION_NAMES: [&'static str; 11] = [
    "INVALID",
    "EPSILON",
    "RANGE",
    "RULE",
    "PREDICATE",
    "ATOM",
    "ACTION",
    "SET",
    "NOT_SET",
    "WILDCARD",
    "PRECEDENCE",
];

pub const TRANSITION_EPSILON: isize = 1;
pub const TRANSITION_RANGE: isize = 2;
pub const TRANSITION_RULE: isize = 3;
pub const TRANSITION_PREDICATE: isize = 4;
pub const TRANSITION_ATOM: isize = 5;
pub const TRANSITION_ACTION: isize = 6;
pub const TRANSITION_SET: isize = 7;
pub const TRANSITION_NOTSET: isize = 8;
pub const TRANSITION_WILDCARD: isize = 9;
pub const TRANSITION_PRECEDENCE: isize = 10;

#[allow(non_camel_case_types)]
#[derive(Debug, Eq, PartialEq)]
pub enum TransitionType {
    TRANSITION_EPSILON = 1,
    TRANSITION_RANGE,
    TRANSITION_RULE,
    TRANSITION_PREDICATE,
    TRANSITION_ATOM,
    TRANSITION_ACTION,
    TRANSITION_SET,
    TRANSITION_NOTSET,
    TRANSITION_WILDCARD,
    TRANSITION_PRECEDENCE,
}

// todo remove trait because it is too slow
/// Transition between ATNStates
pub trait Transition: Sync + Send + Debug + Any {
    fn get_target(&self) -> ATNStateRef;
    fn set_target(&mut self, s: ATNStateRef);
    fn is_epsilon(&self) -> bool { false }
    fn get_label(&self) -> Option<Cow<'_, IntervalSet>> { None }
    fn get_serialization_type(&self) -> TransitionType;
    fn matches(&self, symbol: isize, min_vocab_symbol: isize, max_vocab_symbol: isize) -> bool;
    fn get_predicate(&self) -> Option<SemanticContext> { None }
    fn get_reachable_target(&self, symbol: isize) -> Option<ATNStateRef> {
        //        println!("reachable target called on {:?}", self);
        if self.matches(symbol, LEXER_MIN_CHAR_VALUE, LEXER_MAX_CHAR_VALUE) {
            return Some(self.get_target());
        }
        None
    }
}

impl dyn Transition {
    #[inline]
    pub fn cast<T: Transition>(&self) -> &T {
        assert_eq!(self.type_id(), TypeId::of::<T>());
        unsafe { &*(self as *const dyn Transition as *const T) }
    }
}

#[derive(Debug)]
pub struct AtomTransition {
    pub target: ATNStateRef,
    pub label: isize,
}

impl Transition for AtomTransition {
    fn get_target(&self) -> ATNStateRef { self.target }

    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn get_label(&self) -> Option<Cow<'_, IntervalSet>> {
        let mut r = IntervalSet::new();
        r.add_one(self.label);
        Some(Cow::Owned(r))
    }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_ATOM }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        _symbol == self.label
    }
}

#[derive(Debug)]
pub struct RuleTransition {
    pub target: ATNStateRef,
    pub follow_state: ATNStateRef,
    pub rule_index: isize,
    pub precedence: isize,
}

impl Transition for RuleTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn is_epsilon(&self) -> bool { true }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_RULE }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        unimplemented!()
    }
}

#[derive(Debug)]
pub struct EpsilonTransition {
    pub target: ATNStateRef,
    pub outermost_precedence_return: isize,
}

impl Transition for EpsilonTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn is_epsilon(&self) -> bool { true }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_EPSILON }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        false
    }
}

#[derive(Debug)]
pub struct RangeTransition {
    pub target: ATNStateRef,
    pub start: isize,
    pub stop: isize,
}

impl Transition for RangeTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn get_label(&self) -> Option<Cow<'_, IntervalSet>> {
        let mut r = IntervalSet::new();
        r.add_range(self.start, self.stop);
        Some(Cow::Owned(r))
    }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_RANGE }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        _symbol >= self.start && _symbol <= self.stop
    }
}

#[derive(Debug)]
pub struct ActionTransition {
    pub target: ATNStateRef,
    pub is_ctx_dependent: bool,
    pub rule_index: isize,
    pub action_index: isize,
    pub pred_index: isize,
}

impl Transition for ActionTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn is_epsilon(&self) -> bool { true }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_ACTION }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        false
    }
}

#[derive(Debug)]
pub struct SetTransition {
    pub target: ATNStateRef,
    pub set: IntervalSet,
}

impl Transition for SetTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn get_label(&self) -> Option<Cow<'_, IntervalSet>> { Some(Cow::Borrowed(&self.set)) }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_SET }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        self.set.contains(_symbol)
    }
}

#[derive(Debug)]
pub struct NotSetTransition {
    pub target: ATNStateRef,
    pub set: IntervalSet,
}

impl Transition for NotSetTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn get_label(&self) -> Option<Cow<'_, IntervalSet>> { Some(Cow::Borrowed(&self.set)) }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_NOTSET }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        _symbol >= _min_vocab_symbol && _symbol <= _max_vocab_symbol && !self.set.contains(_symbol)
    }
}

#[derive(Debug)]
pub struct WildcardTransition {
    pub target: ATNStateRef,
}

impl Transition for WildcardTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_WILDCARD }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        _symbol < _max_vocab_symbol && _symbol > _min_vocab_symbol
    }
}

#[derive(Debug)]
pub struct PredicateTransition {
    pub target: ATNStateRef,
    pub is_ctx_dependent: bool,
    pub rule_index: isize,
    pub pred_index: isize,
}

impl Transition for PredicateTransition {
    fn get_target(&self) -> ATNStateRef { self.target }

    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn is_epsilon(&self) -> bool { true }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_PREDICATE }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        false
    }

    fn get_predicate(&self) -> Option<SemanticContext> {
        Some(SemanticContext::Predicate {
            rule_index: self.rule_index,
            pred_index: self.pred_index,
            is_ctx_dependent: self.is_ctx_dependent,
        })
    }
}

#[derive(Debug)]
pub struct PrecedencePredicateTransition {
    pub target: ATNStateRef,
    pub precedence: isize,
}

impl Transition for PrecedencePredicateTransition {
    fn get_target(&self) -> ATNStateRef { self.target }
    fn set_target(&mut self, s: ATNStateRef) { self.target = s }

    fn is_epsilon(&self) -> bool { true }

    fn get_serialization_type(&self) -> TransitionType { TransitionType::TRANSITION_PRECEDENCE }

    fn matches(&self, _symbol: isize, _min_vocab_symbol: isize, _max_vocab_symbol: isize) -> bool {
        false
    }

    fn get_predicate(&self) -> Option<SemanticContext> {
        Some(SemanticContext::Precedence(self.precedence))
    }
}