forked from rrevenantt/antlr4rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplelrparser.rs
513 lines (458 loc) · 17.2 KB
/
simplelrparser.rs
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
// Generated from SimpleLR.g4 by ANTLR 4.8
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(nonstandard_style)]
#![allow(unused_imports)]
#![allow(unused_mut)]
#![allow(unused_braces)]
use super::simplelrlistener::*;
use antlr_rust::atn::{ATN, INVALID_ALT};
use antlr_rust::atn_deserializer::ATNDeserializer;
use antlr_rust::dfa::DFA;
use antlr_rust::error_strategy::{DefaultErrorStrategy, ErrorStrategy};
use antlr_rust::errors::*;
use antlr_rust::int_stream::EOF;
use antlr_rust::lazy_static;
use antlr_rust::parser::{BaseParser, Parser, ParserNodeType, ParserRecog};
use antlr_rust::parser_atn_simulator::ParserATNSimulator;
use antlr_rust::parser_rule_context::{cast, cast_mut, BaseParserRuleContext, ParserRuleContext};
use antlr_rust::recognizer::{Actions, Recognizer};
use antlr_rust::rule_context::{BaseRuleContext, CustomRuleContext, RuleContext};
use antlr_rust::token::{OwningToken, Token, TOKEN_EOF};
use antlr_rust::token_factory::{CommonTokenFactory, TokenAware, TokenFactory};
use antlr_rust::token_stream::TokenStream;
use antlr_rust::tree::*;
use antlr_rust::vocabulary::{Vocabulary, VocabularyImpl};
use antlr_rust::PredictionContextCache;
use antlr_rust::TokenSource;
use antlr_rust::{TidAble, TidExt};
use std::any::{Any, TypeId};
use std::borrow::{Borrow, BorrowMut};
use std::cell::RefCell;
use std::convert::TryFrom;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::Arc;
pub const ID: isize = 1;
pub const WS: isize = 2;
pub const RULE_s: usize = 0;
pub const RULE_a: usize = 1;
pub const ruleNames: [&'static str; 2] = ["s", "a"];
pub const _LITERAL_NAMES: [Option<&'static str>; 0] = [];
pub const _SYMBOLIC_NAMES: [Option<&'static str>; 3] = [None, Some("ID"), Some("WS")];
lazy_static! {
static ref _shared_context_cache: Arc<PredictionContextCache> =
Arc::new(PredictionContextCache::new());
static ref VOCABULARY: Box<dyn Vocabulary> = Box::new(VocabularyImpl::new(
_LITERAL_NAMES.iter(),
_SYMBOLIC_NAMES.iter(),
None
));
}
type BaseParserType<'input, I> = BaseParser<
'input,
SimpleLRParserExt<'input>,
I,
SimpleLRParserContextType,
dyn SimpleLRListener<'input> + 'input,
>;
type TokenType<'input> = <LocalTokenFactory<'input> as TokenFactory<'input>>::Tok;
pub type LocalTokenFactory<'input> = CommonTokenFactory;
pub type SimpleLRTreeWalker<'input, 'a> =
ParseTreeWalker<'input, 'a, SimpleLRParserContextType, dyn SimpleLRListener<'input> + 'a>;
/// Parser for SimpleLR grammar
pub struct SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
base: BaseParserType<'input, I>,
interpreter: Arc<ParserATNSimulator>,
_shared_context_cache: Box<PredictionContextCache>,
pub err_handler: H,
}
impl<'input, I, H> SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
pub fn get_serialized_atn() -> &'static str {
_serializedATN
}
pub fn set_error_strategy(&mut self, strategy: H) {
self.err_handler = strategy
}
pub fn with_strategy(input: I, strategy: H) -> Self {
antlr_rust::recognizer::check_version("0", "3");
let interpreter = Arc::new(ParserATNSimulator::new(
_ATN.clone(),
_decision_to_DFA.clone(),
_shared_context_cache.clone(),
));
Self {
base: BaseParser::new_base_parser(
input,
Arc::clone(&interpreter),
SimpleLRParserExt {
_pd: Default::default(),
},
),
interpreter,
_shared_context_cache: Box::new(PredictionContextCache::new()),
err_handler: strategy,
}
}
}
type DynStrategy<'input, I> = Box<dyn ErrorStrategy<'input, BaseParserType<'input, I>> + 'input>;
impl<'input, I> SimpleLRParser<'input, I, DynStrategy<'input, I>>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
{
pub fn with_dyn_strategy(input: I) -> Self {
Self::with_strategy(input, Box::new(DefaultErrorStrategy::new()))
}
}
impl<'input, I> SimpleLRParser<'input, I, DefaultErrorStrategy<'input, SimpleLRParserContextType>>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
{
pub fn new(input: I) -> Self {
Self::with_strategy(input, DefaultErrorStrategy::new())
}
}
/// Trait for monomorphized trait object that corresponds to the nodes of parse tree generated for SimpleLRParser
pub trait SimpleLRParserContext<'input>:
for<'x> Listenable<dyn SimpleLRListener<'input> + 'x>
+ ParserRuleContext<'input, TF = LocalTokenFactory<'input>, Ctx = SimpleLRParserContextType>
{
}
antlr_rust::coerce_from! { 'input : SimpleLRParserContext<'input> }
impl<'input> SimpleLRParserContext<'input> for TerminalNode<'input, SimpleLRParserContextType> {}
impl<'input> SimpleLRParserContext<'input> for ErrorNode<'input, SimpleLRParserContextType> {}
antlr_rust::tid! { impl<'input> TidAble<'input> for dyn SimpleLRParserContext<'input> + 'input }
antlr_rust::tid! { impl<'input> TidAble<'input> for dyn SimpleLRListener<'input> + 'input }
pub struct SimpleLRParserContextType;
antlr_rust::tid! {SimpleLRParserContextType}
impl<'input> ParserNodeType<'input> for SimpleLRParserContextType {
type TF = LocalTokenFactory<'input>;
type Type = dyn SimpleLRParserContext<'input> + 'input;
}
impl<'input, I, H> Deref for SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
type Target = BaseParserType<'input, I>;
fn deref(&self) -> &Self::Target {
&self.base
}
}
impl<'input, I, H> DerefMut for SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base
}
}
pub struct SimpleLRParserExt<'input> {
_pd: PhantomData<&'input str>,
}
impl<'input> SimpleLRParserExt<'input> {}
antlr_rust::tid! { SimpleLRParserExt<'a> }
impl<'input> TokenAware<'input> for SimpleLRParserExt<'input> {
type TF = LocalTokenFactory<'input>;
}
impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>>
ParserRecog<'input, BaseParserType<'input, I>> for SimpleLRParserExt<'input>
{
}
impl<'input, I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>>
Actions<'input, BaseParserType<'input, I>> for SimpleLRParserExt<'input>
{
fn get_grammar_file_name(&self) -> &str {
"SimpleLR.g4"
}
fn get_rule_names(&self) -> &[&str] {
&ruleNames
}
fn get_vocabulary(&self) -> &dyn Vocabulary {
&**VOCABULARY
}
fn sempred(
_localctx: Option<&(dyn SimpleLRParserContext<'input> + 'input)>,
rule_index: isize,
pred_index: isize,
recog: &mut BaseParserType<'input, I>,
) -> bool {
match rule_index {
1 => SimpleLRParser::<'input, I, _>::a_sempred(
_localctx.and_then(|x| x.downcast_ref()),
pred_index,
recog,
),
_ => true,
}
}
}
impl<'input, I> SimpleLRParser<'input, I, DefaultErrorStrategy<'input, SimpleLRParserContextType>>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
{
fn a_sempred(
_localctx: Option<&AContext<'input>>,
pred_index: isize,
recog: &mut <Self as Deref>::Target,
) -> bool {
match pred_index {
0 => recog.precpred(None, 2),
_ => true,
}
}
}
//------------------- s ----------------
pub type SContextAll<'input> = SContext<'input>;
pub type SContext<'input> = BaseParserRuleContext<'input, SContextExt<'input>>;
#[derive(Clone)]
pub struct SContextExt<'input> {
ph: PhantomData<&'input str>,
}
impl<'input> SimpleLRParserContext<'input> for SContext<'input> {}
impl<'input, 'a> Listenable<dyn SimpleLRListener<'input> + 'a> for SContext<'input> {
fn enter(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) {
listener.enter_every_rule(self);
listener.enter_s(self);
}
fn exit(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) {
listener.exit_s(self);
listener.exit_every_rule(self);
}
}
impl<'input> CustomRuleContext<'input> for SContextExt<'input> {
type TF = LocalTokenFactory<'input>;
type Ctx = SimpleLRParserContextType;
fn get_rule_index(&self) -> usize {
RULE_s
}
//fn type_rule_index() -> usize where Self: Sized { RULE_s }
}
antlr_rust::tid! {SContextExt<'a>}
impl<'input> SContextExt<'input> {
fn new(
parent: Option<Rc<dyn SimpleLRParserContext<'input> + 'input>>,
invoking_state: isize,
) -> Rc<SContextAll<'input>> {
Rc::new(BaseParserRuleContext::new_parser_ctx(
parent,
invoking_state,
SContextExt { ph: PhantomData },
))
}
}
pub trait SContextAttrs<'input>:
SimpleLRParserContext<'input> + BorrowMut<SContextExt<'input>>
{
fn a(&self) -> Option<Rc<AContextAll<'input>>>
where
Self: Sized,
{
self.child_of_type(0)
}
}
impl<'input> SContextAttrs<'input> for SContext<'input> {}
impl<'input, I, H> SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
pub fn s(&mut self) -> Result<Rc<SContextAll<'input>>, ANTLRError> {
let mut recog = self;
let _parentctx = recog.ctx.take();
let mut _localctx = SContextExt::new(_parentctx.clone(), recog.base.get_state());
recog.base.enter_rule(_localctx.clone(), 0, RULE_s);
let mut _localctx: Rc<SContextAll> = _localctx;
let result: Result<(), ANTLRError> = (|| {
//recog.base.enter_outer_alt(_localctx.clone(), 1);
recog.base.enter_outer_alt(None, 1);
{
/*InvokeRule a*/
recog.base.set_state(4);
recog.a_rec(0)?;
}
let tmp = recog.input.lt(-1).cloned();
recog.ctx.as_ref().unwrap().set_stop(tmp);
println!("test");
Ok(())
})();
match result {
Ok(_) => {}
Err(e @ ANTLRError::FallThrough(_)) => return Err(e),
Err(ref re) => {
//_localctx.exception = re;
recog.err_handler.report_error(&mut recog.base, re);
recog.err_handler.recover(&mut recog.base, re)?;
}
}
recog.base.exit_rule();
Ok(_localctx)
}
}
//------------------- a ----------------
pub type AContextAll<'input> = AContext<'input>;
pub type AContext<'input> = BaseParserRuleContext<'input, AContextExt<'input>>;
#[derive(Clone)]
pub struct AContextExt<'input> {
ph: PhantomData<&'input str>,
}
impl<'input> SimpleLRParserContext<'input> for AContext<'input> {}
impl<'input, 'a> Listenable<dyn SimpleLRListener<'input> + 'a> for AContext<'input> {
fn enter(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) {
listener.enter_every_rule(self);
listener.enter_a(self);
}
fn exit(&self, listener: &mut (dyn SimpleLRListener<'input> + 'a)) {
listener.exit_a(self);
listener.exit_every_rule(self);
}
}
impl<'input> CustomRuleContext<'input> for AContextExt<'input> {
type TF = LocalTokenFactory<'input>;
type Ctx = SimpleLRParserContextType;
fn get_rule_index(&self) -> usize {
RULE_a
}
//fn type_rule_index() -> usize where Self: Sized { RULE_a }
}
antlr_rust::tid! {AContextExt<'a>}
impl<'input> AContextExt<'input> {
fn new(
parent: Option<Rc<dyn SimpleLRParserContext<'input> + 'input>>,
invoking_state: isize,
) -> Rc<AContextAll<'input>> {
Rc::new(BaseParserRuleContext::new_parser_ctx(
parent,
invoking_state,
AContextExt { ph: PhantomData },
))
}
}
pub trait AContextAttrs<'input>:
SimpleLRParserContext<'input> + BorrowMut<AContextExt<'input>>
{
/// Retrieves first TerminalNode corresponding to token ID
/// Returns `None` if there is no child corresponding to token ID
fn ID(&self) -> Option<Rc<TerminalNode<'input, SimpleLRParserContextType>>>
where
Self: Sized,
{
self.get_token(ID, 0)
}
fn a(&self) -> Option<Rc<AContextAll<'input>>>
where
Self: Sized,
{
self.child_of_type(0)
}
}
impl<'input> AContextAttrs<'input> for AContext<'input> {}
impl<'input, I, H> SimpleLRParser<'input, I, H>
where
I: TokenStream<'input, TF = LocalTokenFactory<'input>> + TidAble<'input>,
H: ErrorStrategy<'input, BaseParserType<'input, I>>,
{
pub fn a(&mut self) -> Result<Rc<AContextAll<'input>>, ANTLRError> {
self.a_rec(0)
}
fn a_rec(&mut self, _p: isize) -> Result<Rc<AContextAll<'input>>, ANTLRError> {
let recog = self;
let _parentctx = recog.ctx.take();
let _parentState = recog.base.get_state();
let mut _localctx = AContextExt::new(_parentctx.clone(), recog.base.get_state());
recog
.base
.enter_recursion_rule(_localctx.clone(), 2, RULE_a, _p);
let mut _localctx: Rc<AContextAll> = _localctx;
let mut _prevctx = _localctx.clone();
let _startState = 2;
let result: Result<(), ANTLRError> = (|| {
let mut _alt: isize;
//recog.base.enter_outer_alt(_localctx.clone(), 1);
recog.base.enter_outer_alt(None, 1);
{
{
recog.base.set_state(7);
recog.base.match_token(ID, &mut recog.err_handler)?;
}
let tmp = recog.input.lt(-1).cloned();
recog.ctx.as_ref().unwrap().set_stop(tmp);
recog.base.set_state(13);
recog.err_handler.sync(&mut recog.base)?;
_alt = recog.interpreter.adaptive_predict(0, &mut recog.base)?;
while { _alt != 2 && _alt != INVALID_ALT } {
if _alt == 1 {
recog.trigger_exit_rule_event();
_prevctx = _localctx.clone();
{
{
/*recRuleAltStartAction*/
let mut tmp = AContextExt::new(_parentctx.clone(), _parentState);
recog.push_new_recursion_context(tmp.clone(), _startState, RULE_a);
_localctx = tmp;
recog.base.set_state(9);
if !({ recog.precpred(None, 2) }) {
Err(FailedPredicateError::new(
&mut recog.base,
Some("recog.precpred(None, 2)".to_owned()),
None,
))?;
}
recog.base.set_state(10);
recog.base.match_token(ID, &mut recog.err_handler)?;
}
}
}
recog.base.set_state(15);
recog.err_handler.sync(&mut recog.base)?;
_alt = recog.interpreter.adaptive_predict(0, &mut recog.base)?;
}
}
Ok(())
})();
match result {
Ok(_) => {}
Err(e @ ANTLRError::FallThrough(_)) => return Err(e),
Err(ref re) => {
//_localctx.exception = re;
recog.err_handler.report_error(&mut recog.base, re);
recog.err_handler.recover(&mut recog.base, re)?;
}
}
recog.base.unroll_recursion_context(_parentctx);
Ok(_localctx)
}
}
lazy_static! {
static ref _ATN: Arc<ATN> =
Arc::new(ATNDeserializer::new(None).deserialize(_serializedATN.chars()));
static ref _decision_to_DFA: Arc<Vec<antlr_rust::RwLock<DFA>>> = {
let mut dfa = Vec::new();
let size = _ATN.decision_to_state.len();
for i in 0..size {
dfa.push(DFA::new(_ATN.clone(), _ATN.get_decision_state(i), i as isize).into())
}
Arc::new(dfa)
};
}
const _serializedATN: &'static str =
"\x03\u{608b}\u{a72a}\u{8133}\u{b9ed}\u{417c}\u{3be7}\u{7786}\u{5964}\x03\
\x04\x13\x04\x02\x09\x02\x04\x03\x09\x03\x03\x02\x03\x02\x03\x03\x03\x03\
\x03\x03\x03\x03\x03\x03\x07\x03\x0e\x0a\x03\x0c\x03\x0e\x03\x11\x0b\x03\
\x03\x03\x02\x03\x04\x04\x02\x04\x02\x02\x02\x11\x02\x06\x03\x02\x02\x02\
\x04\x08\x03\x02\x02\x02\x06\x07\x05\x04\x03\x02\x07\x03\x03\x02\x02\x02\
\x08\x09\x08\x03\x01\x02\x09\x0a\x07\x03\x02\x02\x0a\x0f\x03\x02\x02\x02\
\x0b\x0c\x0c\x04\x02\x02\x0c\x0e\x07\x03\x02\x02\x0d\x0b\x03\x02\x02\x02\
\x0e\x11\x03\x02\x02\x02\x0f\x0d\x03\x02\x02\x02\x0f\x10\x03\x02\x02\x02\
\x10\x05\x03\x02\x02\x02\x11\x0f\x03\x02\x02\x02\x03\x0f";