1
use std::iter;
2

            
3
use itertools::Itertools;
4
use pest::error::ErrorVariant;
5

            
6
use merc_pest_consume::Error;
7
use merc_pest_consume::match_nodes;
8

            
9
use crate::ActDecl;
10
use crate::ActFrm;
11
use crate::Action;
12
use crate::ActionRHS;
13
use crate::ActionRenameDecl;
14
use crate::ActionRenameRule;
15
use crate::Assignment;
16
use crate::BagElement;
17
use crate::CommExpr;
18
use crate::ComplexSort;
19
use crate::Condition;
20
use crate::ConstructorDecl;
21
use crate::DataExpr;
22
use crate::DataExprUnaryOp;
23
use crate::DataExprUpdate;
24
use crate::Eq;
25
use crate::EqnDecl;
26
use crate::EqnSpec;
27
use crate::FixedPointOperator;
28
use crate::IdDecl;
29
use crate::Mcrl2Parser;
30
use crate::MultiAction;
31
use crate::MultiActionLabel;
32
use crate::PbesEquation;
33
use crate::PbesExpr;
34
use crate::PresEquation;
35
use crate::PresExpr;
36
use crate::ProcDecl;
37
use crate::ProcessExpr;
38
use crate::PropVarDecl;
39
use crate::PropVarInst;
40
use crate::RegFrm;
41
use crate::Rename;
42
use crate::Rule;
43
use crate::SortDecl;
44
use crate::SortExpression;
45
use crate::StateFrm;
46
use crate::StateVarAssignment;
47
use crate::StateVarDecl;
48
use crate::UntypedActionRenameSpec;
49
use crate::UntypedDataSpecification;
50
use crate::UntypedPbes;
51
use crate::UntypedPres;
52
use crate::UntypedProcessSpecification;
53
use crate::UntypedStateFrmSpec;
54
use crate::parse_actfrm;
55
use crate::parse_dataexpr;
56
use crate::parse_pbesexpr;
57
use crate::parse_presexpr;
58
use crate::parse_process_expr;
59
use crate::parse_regfrm;
60
use crate::parse_sortexpr;
61
use crate::parse_sortexpr_primary;
62
use crate::parse_statefrm;
63

            
64
/// Type alias for Errors resulting parsing.
65
pub(crate) type ParseResult<T> = std::result::Result<T, Error<Rule>>;
66
pub(crate) type ParseNode<'i> = merc_pest_consume::Node<'i, Rule, ()>;
67

            
68
/// These functions are used to consume the parse tree generated by the `pest` parser into a syntax tree.
69
///
70
/// Functions that are private are only used by other functions within the module, especially within the `matches_nodes!` macro.
71
/// See `pest_consume` documentation for more details on how to use this macro. The main idea is to apply the consume functions to the children of a parse node whenver they matce the arm.
72
///
73
/// Functions that are `pub(crate)` are used by the pratt parser to consume the parse tree and build the syntax tree with priorities
74
/// and associativity. They are defined in `precedence.rs`.
75
#[merc_pest_consume::parser]
76
impl Mcrl2Parser {
77
    // Although these are not public, they are the main entry points for consuming the parse tree.
78
    pub(crate) fn MCRL2Spec(spec: ParseNode) -> ParseResult<UntypedProcessSpecification> {
79
        let mut action_declarations = Vec::new();
80
        let mut map_declarations = Vec::new();
81
        let mut constructor_declarations = Vec::new();
82
        let mut equation_declarations = Vec::new();
83
        let mut global_variables = Vec::new();
84
        let mut process_declarations = Vec::new();
85
        let mut sort_declarations = Vec::new();
86

            
87
        let mut init = None;
88

            
89
        for child in spec.into_children() {
90
            match child.as_rule() {
91
                Rule::ActSpec => {
92
                    action_declarations.extend(Mcrl2Parser::ActSpec(child)?);
93
                }
94
                Rule::ConsSpec => {
95
                    constructor_declarations.append(&mut Mcrl2Parser::ConsSpec(child)?);
96
                }
97
                Rule::MapSpec => {
98
                    map_declarations.append(&mut Mcrl2Parser::MapSpec(child)?);
99
                }
100
                Rule::GlobVarSpec => {
101
                    global_variables.append(&mut Mcrl2Parser::GlobVarSpec(child)?);
102
                }
103
                Rule::EqnSpec => {
104
                    equation_declarations.append(&mut Mcrl2Parser::EqnSpec(child)?);
105
                }
106
                Rule::ProcSpec => {
107
                    process_declarations.append(&mut Mcrl2Parser::ProcSpec(child)?);
108
                }
109
                Rule::SortSpec => {
110
                    sort_declarations.append(&mut Mcrl2Parser::SortSpec(child)?);
111
                }
112
                Rule::Init => {
113
                    if init.is_some() {
114
                        return Err(Error::new_from_span(
115
                            ErrorVariant::CustomError {
116
                                message: "Multiple init expressions are not allowed".to_string(),
117
                            },
118
                            child.as_span(),
119
                        ));
120
                    }
121

            
122
                    init = Some(Mcrl2Parser::Init(child)?);
123
                }
124
                Rule::EOI => {
125
                    // End of input
126
                    break;
127
                }
128
                _ => {
129
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
130
                }
131
            }
132
        }
133

            
134
        let data_specification = UntypedDataSpecification {
135
            map_declarations,
136
            constructor_declarations,
137
            equation_declarations,
138
            sort_declarations,
139
        };
140

            
141
        Ok(UntypedProcessSpecification {
142
            data_specification,
143
            global_variables,
144
            action_declarations,
145
            process_declarations,
146
            init,
147
        })
148
    }
149

            
150
    pub fn PbesSpec(spec: ParseNode) -> ParseResult<UntypedPbes> {
151
        let mut data_specification = None;
152
        let mut global_variables = None;
153
        let mut equations = None;
154
        let mut init = None;
155

            
156
        for child in spec.into_children() {
157
            match child.as_rule() {
158
                Rule::DataSpec => {
159
                    data_specification = Some(Mcrl2Parser::DataSpec(child)?);
160
                }
161
                Rule::GlobVarSpec => {
162
                    global_variables = Some(Mcrl2Parser::GlobVarSpec(child)?);
163
                }
164
                Rule::PbesEqnSpec => {
165
                    equations = Some(Mcrl2Parser::PbesEqnSpec(child)?);
166
                }
167
                Rule::PbesInit => {
168
                    init = Some(Mcrl2Parser::PbesInit(child)?);
169
                }
170
                Rule::EOI => {
171
                    // End of input
172
                    break;
173
                }
174
                _ => {
175
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
176
                }
177
            }
178
        }
179

            
180
        Ok(UntypedPbes {
181
            data_specification: data_specification.unwrap_or_default(),
182
            global_variables: global_variables.unwrap_or_default(),
183
            equations: equations.unwrap(),
184
            init: init.unwrap(),
185
        })
186
    }
187

            
188
    fn PbesInit(init: ParseNode) -> ParseResult<PropVarInst> {
189
        match_nodes!(init.into_children();
190
            [PropVarInst(inst)] => {
191
                Ok(inst)
192
            }
193
        )
194
    }
195

            
196
    fn PbesEqnSpec(spec: ParseNode) -> ParseResult<Vec<PbesEquation>> {
197
        match_nodes!(spec.into_children();
198
            [PbesEqnDecl(equations)..] => {
199
                Ok(equations.collect())
200
            },
201
        )
202
    }
203

            
204
    fn PbesEqnDecl(decl: ParseNode) -> ParseResult<PbesEquation> {
205
        let span = decl.as_span();
206
        match_nodes!(decl.into_children();
207
            [FixedPointOperator(operator), PropVarDecl(variable), PbesExpr(formula)] => {
208
                Ok(PbesEquation {
209
                    operator,
210
                    variable,
211
                    formula,
212
                    span: span.into(),
213
                })
214
            },
215
        )
216
    }
217

            
218
    fn FixedPointOperator(op: ParseNode) -> ParseResult<FixedPointOperator> {
219
        match op.into_children().next().unwrap().as_rule() {
220
            Rule::FixedPointMu => Ok(FixedPointOperator::Least),
221
            Rule::FixedPointNu => Ok(FixedPointOperator::Greatest),
222
            x => unimplemented!("This is not a fixed point operator: {:?}", x),
223
        }
224
    }
225

            
226
    fn PropVarDecl(decl: ParseNode) -> ParseResult<PropVarDecl> {
227
        let span = decl.as_span();
228
        match_nodes!(decl.into_children();
229
            [Id(identifier), VarsDeclList(params)] => {
230
                Ok(PropVarDecl {
231
                    identifier,
232
                    parameters: params,
233
                    span: span.into(),
234
                })
235
            },
236
            [Id(identifier)] => {
237
                Ok(PropVarDecl {
238
                    identifier,
239
                    parameters: Vec::new(),
240
                    span: span.into(),
241
                })
242
            }
243
        )
244
    }
245

            
246
    pub(crate) fn PropVarInst(inst: ParseNode) -> ParseResult<PropVarInst> {
247
        match_nodes!(inst.into_children();
248
            [Id(identifier)] => {
249
                Ok(PropVarInst {
250
                    identifier,
251
                    arguments: Vec::new(),
252
                })
253
            },
254
            [Id(identifier), DataExprList(arguments)] => {
255
                Ok(PropVarInst {
256
                    identifier,
257
                    arguments,
258
                })
259
            }
260
        )
261
    }
262

            
263
    fn PbesExpr(expr: ParseNode) -> ParseResult<PbesExpr> {
264
        parse_pbesexpr(expr.children().as_pairs().clone())
265
    }
266

            
267
    pub fn PresSpec(spec: ParseNode) -> ParseResult<UntypedPres> {
268
        let mut data_specification = None;
269
        let mut global_variables = None;
270
        let mut equations = None;
271
        let mut init = None;
272

            
273
        for child in spec.into_children() {
274
            match child.as_rule() {
275
                Rule::DataSpec => {
276
                    data_specification = Some(Mcrl2Parser::DataSpec(child)?);
277
                }
278
                Rule::GlobVarSpec => {
279
                    global_variables = Some(Mcrl2Parser::GlobVarSpec(child)?);
280
                }
281
                Rule::PresEqnSpec => {
282
                    equations = Some(Mcrl2Parser::PresEqnSpec(child)?);
283
                }
284
                Rule::PbesInit => {
285
                    init = Some(Mcrl2Parser::PbesInit(child)?);
286
                }
287
                Rule::EOI => {
288
                    // End of input
289
                    break;
290
                }
291
                _ => {
292
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
293
                }
294
            }
295
        }
296

            
297
        Ok(UntypedPres {
298
            data_specification: data_specification.unwrap_or_default(),
299
            global_variables: global_variables.unwrap_or_default(),
300
            equations: equations.unwrap(),
301
            init: init.unwrap(),
302
        })
303
    }
304

            
305
    fn PresEqnSpec(spec: ParseNode) -> ParseResult<Vec<PresEquation>> {
306
        match_nodes!(spec.into_children();
307
            [PresEqnDecl(equations)..] => {
308
                Ok(equations.collect())
309
            },
310
        )
311
    }
312

            
313
    fn PresEqnDecl(decl: ParseNode) -> ParseResult<PresEquation> {
314
        let span = decl.as_span();
315
        match_nodes!(decl.into_children();
316
            [FixedPointOperator(operator), PropVarDecl(variable), PresExpr(formula)] => {
317
                Ok(PresEquation {
318
                    operator,
319
                    variable,
320
                    formula,
321
                    span: span.into(),
322
                })
323
            },
324
        )
325
    }
326

            
327
    fn PresExpr(expr: ParseNode) -> ParseResult<PresExpr> {
328
        parse_presexpr(expr.children().as_pairs().clone())
329
    }
330

            
331
    fn ActSpec(spec: ParseNode) -> ParseResult<Vec<ActDecl>> {
332
        match_nodes!(spec.into_children();
333
            [ActDecl(decls)..] => {
334
                Ok(decls.flatten().collect())
335
            },
336
        )
337
    }
338

            
339
    fn ActDecl(decl: ParseNode) -> ParseResult<Vec<ActDecl>> {
340
        let span = decl.as_span();
341
        match_nodes!(decl.into_children();
342
            [IdList(identifiers)] => {
343
1771
                Ok(identifiers.iter().map(|name| ActDecl { identifier: name.clone(), args: Vec::new(), span: span.into() }).collect())
344
            },
345
            [IdList(identifiers), SortProduct(args)] => {
346
12180
                Ok(identifiers.iter().map(|name| ActDecl { identifier: name.clone(), args: args.clone(), span: span.into() }).collect())
347
            },
348
        )
349
    }
350

            
351
    fn SortProduct(sort: ParseNode) -> ParseResult<Vec<SortExpression>> {
352
        let mut iter = sort.into_children();
353

            
354
        // An expression of the shape SortExprPrimary ~ (SortExprProduct ~ SortExprPrimary)*
355
        let mut result = vec![parse_sortexpr_primary(iter.next().unwrap().as_pair().clone())?];
356

            
357
        for mut chunk in &iter.chunks(2) {
358
            if chunk.next().unwrap().as_rule() == Rule::SortExprProduct {
359
                let sort = parse_sortexpr_primary(chunk.next().unwrap().as_pair().clone())?;
360
                result.push(sort);
361
            }
362
        }
363

            
364
        Ok(result)
365
    }
366

            
367
    fn GlobVarSpec(spec: ParseNode) -> ParseResult<Vec<IdDecl>> {
368
        match_nodes!(spec.into_children();
369
            [VarsDeclList(vars)] => {
370
                Ok(vars)
371
            }
372
        )
373
    }
374

            
375
    fn SortExprPrimary(sort: ParseNode) -> ParseResult<SortExpression> {
376
        parse_sortexpr(sort.children().as_pairs().clone())
377
    }
378

            
379
    pub(crate) fn DataSpec(spec: ParseNode) -> ParseResult<UntypedDataSpecification> {
380
        let mut map_declarations = Vec::new();
381
        let mut equation_declarations = Vec::new();
382
        let mut constructor_declarations = Vec::new();
383
        let mut sort_declarations = Vec::new();
384

            
385
        for child in spec.into_children() {
386
            match child.as_rule() {
387
                Rule::ConsSpec => {
388
                    constructor_declarations.append(&mut Mcrl2Parser::ConsSpec(child)?);
389
                }
390
                Rule::MapSpec => {
391
                    map_declarations.append(&mut Mcrl2Parser::MapSpec(child)?);
392
                }
393
                Rule::EqnSpec => {
394
                    equation_declarations.append(&mut Mcrl2Parser::EqnSpec(child)?);
395
                }
396
                Rule::SortSpec => {
397
                    sort_declarations.append(&mut Mcrl2Parser::SortSpec(child)?);
398
                }
399
                Rule::EOI => {
400
                    // End of input
401
                    break;
402
                }
403
                _ => {
404
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
405
                }
406
            }
407
        }
408

            
409
        Ok(UntypedDataSpecification {
410
            map_declarations,
411
            equation_declarations,
412
            constructor_declarations,
413
            sort_declarations,
414
        })
415
    }
416

            
417
    pub fn ActionRenameSpec(spec: ParseNode) -> ParseResult<UntypedActionRenameSpec> {
418
        let mut map_declarations = Vec::new();
419
        let mut equation_declarations = Vec::new();
420
        let mut constructor_declarations = Vec::new();
421
        let mut sort_declarations = Vec::new();
422
        let mut action_declarations = Vec::new();
423
        let mut rename_declarations = Vec::new();
424

            
425
        for child in spec.into_children() {
426
            match child.as_rule() {
427
                Rule::ConsSpec => {
428
                    constructor_declarations.append(&mut Mcrl2Parser::ConsSpec(child)?);
429
                }
430
                Rule::MapSpec => {
431
                    map_declarations.append(&mut Mcrl2Parser::MapSpec(child)?);
432
                }
433
                Rule::EqnSpec => {
434
                    equation_declarations.append(&mut Mcrl2Parser::EqnSpec(child)?);
435
                }
436
                Rule::SortSpec => {
437
                    sort_declarations.append(&mut Mcrl2Parser::SortSpec(child)?);
438
                }
439
                Rule::ActSpec => {
440
                    action_declarations.append(&mut Mcrl2Parser::ActSpec(child)?);
441
                }
442
                Rule::ActionRenameRuleSpec => {
443
                    rename_declarations.append(&mut Mcrl2Parser::ActionRenameRuleSpec(child)?)
444
                }
445
                Rule::EOI => {
446
                    // End of input
447
                    break;
448
                }
449
                _ => {
450
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
451
                }
452
            }
453
        }
454

            
455
        let data_specification = UntypedDataSpecification {
456
            map_declarations,
457
            equation_declarations,
458
            constructor_declarations,
459
            sort_declarations,
460
        };
461

            
462
        Ok(UntypedActionRenameSpec {
463
            data_specification,
464
            action_declarations,
465
            rename_declarations,
466
        })
467
    }
468

            
469
    pub(crate) fn StateFrmId(id: ParseNode) -> ParseResult<StateFrm> {
470
        match_nodes!(id.into_children();
471
            [Id(identifier)] => {
472
                Ok(StateFrm::Id(identifier, Vec::new()))
473
            },
474
            [Id(identifier), DataExprList(expressions)] => {
475
                Ok(StateFrm::Id(identifier, expressions))
476
            },
477
        )
478
    }
479

            
480
    fn MapSpec(spec: ParseNode) -> ParseResult<Vec<IdDecl>> {
481
        match_nodes!(spec.into_children();
482
            [IdsDecl(decls)..] => {
483
                Ok(decls.flatten().collect())
484
            }
485
        )
486
    }
487

            
488
    fn SortSpec(spec: ParseNode) -> ParseResult<Vec<SortDecl>> {
489
        match_nodes!(spec.into_children();
490
            [SortDecl(decls)..] => {
491
                Ok(decls.flatten().collect())
492
            }
493
        )
494
    }
495

            
496
    fn SortDecl(decl: ParseNode) -> ParseResult<Vec<SortDecl>> {
497
        let span = decl.as_span();
498

            
499
        match_nodes!(decl.into_children();
500
            [IdAt(identifier), SortExpr(expr)] => {
501
                Ok(vec![SortDecl::new(identifier, Some(expr), span.into())])
502
            },
503
            [IdList(ids)] => {
504
63
                Ok(ids.iter().map(|identifier| SortDecl::new(identifier.clone(), None, span.into())).collect())
505
            },
506
            [IdsDecl(decl)] => {
507
                Ok(decl.iter().map(|element| SortDecl::new(element.identifier.clone(), Some(element.sort.clone()), span.into())).collect())
508
            }
509
        )
510
    }
511

            
512
    fn ConsSpec(spec: ParseNode) -> ParseResult<Vec<IdDecl>> {
513
        match_nodes!(spec.into_children();
514
            [IdsDecl(decls)..] => {
515
                Ok(decls.flatten().collect())
516
            }
517
        )
518
    }
519

            
520
    fn Init(init: ParseNode) -> ParseResult<ProcessExpr> {
521
        match_nodes!(init.into_children();
522
            [ProcExpr(expr)] => {
523
                Ok(expr)
524
            }
525
        )
526
    }
527

            
528
    fn ProcSpec(spec: ParseNode) -> ParseResult<Vec<ProcDecl>> {
529
        match_nodes!(spec.into_children();
530
            [ProcDecl(decls)..] => {
531
                Ok(decls.collect())
532
            },
533
        )
534
    }
535

            
536
    fn ProcDecl(decl: ParseNode) -> ParseResult<ProcDecl> {
537
        let span = decl.as_span();
538
        match_nodes!(decl.into_children();
539
            [Id(identifier), VarsDeclList(params), ProcExpr(body)] => {
540
                Ok(ProcDecl {
541
                    identifier,
542
                    params,
543
                    body,
544
                    span: span.into(),
545
                })
546
            },
547
            [Id(identifier), ProcExpr(body)] => {
548
                Ok(ProcDecl {
549
                    identifier,
550
                    params: Vec::new(),
551
                    body,
552
                    span: span.into(),
553
                })
554
            }
555
        )
556
    }
557

            
558
    pub(crate) fn ProcExprAt(input: ParseNode) -> ParseResult<DataExpr> {
559
        match_nodes!(input.into_children();
560
            [DataExprUnit(expr)] => {
561
                Ok(expr)
562
            },
563
        )
564
    }
565

            
566
    pub(crate) fn StateFrmExists(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
567
        match_nodes!(input.into_children();
568
            [VarsDeclList(variables)] => {
569
                Ok(variables)
570
            },
571
        )
572
    }
573

            
574
    pub(crate) fn StateFrmForall(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
575
        match_nodes!(input.into_children();
576
            [VarsDeclList(variables)] => {
577
                Ok(variables)
578
            },
579
        )
580
    }
581

            
582
    pub(crate) fn StateFrmMu(input: ParseNode) -> ParseResult<StateVarDecl> {
583
        match_nodes!(input.into_children();
584
            [StateVarDecl(variable)] => {
585
                Ok(variable)
586
            },
587
        )
588
    }
589

            
590
    pub(crate) fn StateFrmNu(input: ParseNode) -> ParseResult<StateVarDecl> {
591
        match_nodes!(input.into_children();
592
            [StateVarDecl(variable)] => {
593
                Ok(variable)
594
            },
595
        )
596
    }
597

            
598
    pub(crate) fn ActFrmExists(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
599
        match_nodes!(input.into_children();
600
            [VarsDeclList(variables)] => {
601
                Ok(variables)
602
            },
603
        )
604
    }
605

            
606
    pub(crate) fn ActFrmForall(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
607
        match_nodes!(input.into_children();
608
            [VarsDeclList(variables)] => {
609
                Ok(variables)
610
            },
611
        )
612
    }
613

            
614
    pub(crate) fn DataExpr(expr: ParseNode) -> ParseResult<DataExpr> {
615
        parse_dataexpr(expr.children().as_pairs().clone())
616
    }
617

            
618
    pub(crate) fn DataExprUnit(expr: ParseNode) -> ParseResult<DataExpr> {
619
        parse_dataexpr(expr.children().as_pairs().clone())
620
    }
621

            
622
    pub(crate) fn DataValExpr(expr: ParseNode) -> ParseResult<DataExpr> {
623
        match_nodes!(expr.into_children();
624
            [DataExpr(expr)] => {
625
                Ok(expr)
626
            },
627
        )
628
    }
629

            
630
    pub(crate) fn DataExprUpdate(expr: ParseNode) -> ParseResult<DataExprUpdate> {
631
        match_nodes!(expr.into_children();
632
            [DataExpr(expr), DataExpr(update)] => {
633
                Ok(DataExprUpdate { expr, update })
634
            },
635
        )
636
    }
637

            
638
    pub(crate) fn DataExprApplication(expr: ParseNode) -> ParseResult<Vec<DataExpr>> {
639
        match_nodes!(expr.into_children();
640
            [DataExprList(expressions)] => {
641
                Ok(expressions)
642
            },
643
        )
644
    }
645

            
646
    pub(crate) fn DataExprWhr(expr: ParseNode) -> ParseResult<Vec<Assignment>> {
647
        match_nodes!(expr.into_children();
648
            [AssignmentList(assignments)] => {
649
                Ok(assignments)
650
            },
651
        )
652
    }
653

            
654
    pub(crate) fn AssignmentList(assignments: ParseNode) -> ParseResult<Vec<Assignment>> {
655
        match_nodes!(assignments.into_children();
656
            [Assignment(assignment)] => {
657
                Ok(vec![assignment])
658
            },
659
            [Assignment(assignment)..] => {
660
                Ok(assignment.collect())
661
            },
662
        )
663
    }
664

            
665
    pub(crate) fn Assignment(assignment: ParseNode) -> ParseResult<Assignment> {
666
        match_nodes!(assignment.into_children();
667
            [IdAt(identifier), DataExpr(expr)] => {
668
                Ok(Assignment { identifier, expr })
669
            },
670
        )
671
    }
672

            
673
    pub(crate) fn DataExprSize(expr: ParseNode) -> ParseResult<DataExpr> {
674
        match_nodes!(expr.into_children();
675
            [DataExpr(expr)] => {
676
                Ok(DataExpr::Unary { op: DataExprUnaryOp::Size, expr: Box::new(expr) })
677
            },
678
        )
679
    }
680

            
681
    fn DataExprList(expr: ParseNode) -> ParseResult<Vec<DataExpr>> {
682
        match_nodes!(expr.into_children();
683
            [DataExpr(expr)] => {
684
                Ok(vec![expr])
685
            },
686
            [DataExpr(expr)..] => {
687
                Ok(expr.collect())
688
            },
689
        )
690
    }
691

            
692
    fn VarSpec(vars: ParseNode) -> ParseResult<Vec<IdDecl>> {
693
        match_nodes!(vars.into_children();
694
            [VarsDeclList(ids)..] => {
695
                Ok(ids.flatten().collect())
696
            },
697
        )
698
    }
699

            
700
    pub(crate) fn VarsDeclList(vars: ParseNode) -> ParseResult<Vec<IdDecl>> {
701
        match_nodes!(vars.into_children();
702
            [VarsDecl(decl)..] => {
703
                Ok(decl.flatten().collect())
704
            },
705
        )
706
    }
707

            
708
    fn VarsDecl(decl: ParseNode) -> ParseResult<Vec<IdDecl>> {
709
        let mut vars = Vec::new();
710

            
711
        let span = decl.as_span();
712
        match_nodes!(decl.into_children();
713
            [IdList(identifier), SortExpr(sort)] => {
714
                for id in identifier {
715
                    vars.push(IdDecl::new(id, sort.clone(), span.into()));
716
                }
717
            },
718
        );
719

            
720
        Ok(vars)
721
    }
722

            
723
    pub(crate) fn SortExpr(expr: ParseNode) -> ParseResult<SortExpression> {
724
        parse_sortexpr(expr.children().as_pairs().clone())
725
    }
726

            
727
    pub(crate) fn Id(identifier: ParseNode) -> ParseResult<String> {
728
        Ok(identifier.as_str().to_string())
729
    }
730

            
731
    pub(crate) fn IdAt(identifier: ParseNode) -> ParseResult<String> {
732
        Ok(identifier.as_str().to_string())
733
    }
734

            
735
    pub(crate) fn IdList(identifiers: ParseNode) -> ParseResult<Vec<String>> {
736
        match_nodes!(identifiers.into_children();
737
            [IdAt(ids)..] => {
738
                Ok(ids.collect())
739
            },
740
        )
741
    }
742

            
743
    fn IdInfix(identifier: ParseNode) -> ParseResult<String> {
744
        Ok(identifier.as_str().to_string())
745
    }
746

            
747
    fn IdInfixList(identifiers: ParseNode) -> ParseResult<Vec<String>> {
748
        match_nodes!(identifiers.into_children();
749
            [IdInfix(ids)..] => {
750
                Ok(ids.collect())
751
            },
752
        )
753
    }
754

            
755
    // Complex sorts
756
    pub(crate) fn SortExprList(inner: ParseNode) -> ParseResult<SortExpression> {
757
        Ok(SortExpression::Complex(
758
            ComplexSort::List,
759
            Box::new(parse_sortexpr(inner.children().as_pairs().clone())?),
760
        ))
761
    }
762

            
763
    pub(crate) fn SortExprSet(inner: ParseNode) -> ParseResult<SortExpression> {
764
        Ok(SortExpression::Complex(
765
            ComplexSort::Set,
766
            Box::new(parse_sortexpr(inner.children().as_pairs().clone())?),
767
        ))
768
    }
769

            
770
    pub(crate) fn SortExprBag(inner: ParseNode) -> ParseResult<SortExpression> {
771
        Ok(SortExpression::Complex(
772
            ComplexSort::Bag,
773
            Box::new(parse_sortexpr(inner.children().as_pairs().clone())?),
774
        ))
775
    }
776

            
777
    pub(crate) fn SortExprFSet(inner: ParseNode) -> ParseResult<SortExpression> {
778
        Ok(SortExpression::Complex(
779
            ComplexSort::FSet,
780
            Box::new(parse_sortexpr(inner.children().as_pairs().clone())?),
781
        ))
782
    }
783

            
784
    pub(crate) fn SortExprFBag(inner: ParseNode) -> ParseResult<SortExpression> {
785
        Ok(SortExpression::Complex(
786
            ComplexSort::FBag,
787
            Box::new(parse_sortexpr(inner.children().as_pairs().clone())?),
788
        ))
789
    }
790

            
791
    pub(crate) fn SortExprStruct(inner: ParseNode) -> ParseResult<SortExpression> {
792
        match_nodes!(inner.into_children();
793
            [ConstrDeclList(inner)] => {
794
                Ok(SortExpression::Struct { inner })
795
            },
796
        )
797
    }
798

            
799
    pub(crate) fn ConstrDeclList(input: ParseNode) -> ParseResult<Vec<ConstructorDecl>> {
800
        match_nodes!(input.into_children();
801
            [ConstrDecl(decl)..] => {
802
                Ok(decl.collect())
803
            },
804
        )
805
    }
806

            
807
    pub(crate) fn ProjDeclList(input: ParseNode) -> ParseResult<Vec<(Option<String>, SortExpression)>> {
808
        match_nodes!(input.into_children();
809
            [ProjDecl(decl)..] => {
810
                Ok(decl.collect())
811
            },
812
        )
813
    }
814

            
815
    pub(crate) fn ConstrDecl(input: ParseNode) -> ParseResult<ConstructorDecl> {
816
        match_nodes!(input.into_children();
817
            [IdAt(name)] => {
818
                Ok(ConstructorDecl { name, args: Vec::new(), projection: None })
819
            },
820
            [IdAt(name), IdAt(projection)] => {
821
                Ok(ConstructorDecl { name, args: Vec::new(), projection: Some(projection)  })
822
            },
823
            [IdAt(name), ProjDeclList(args)] => {
824
                Ok(ConstructorDecl { name, args, projection: None })
825
            },
826
            [IdAt(name), ProjDeclList(args), IdAt(projection)] => {
827
                Ok(ConstructorDecl { name, args, projection: Some(projection) })
828
            },
829
        )
830
    }
831

            
832
    pub(crate) fn ProjDecl(input: ParseNode) -> ParseResult<(Option<String>, SortExpression)> {
833
        match_nodes!(input.into_children();
834
            [SortExpr(sort)] => {
835
                Ok((None, sort))
836
            },
837
            [Id(name), SortExpr(sort)] => {
838
                Ok((Some(name), sort))
839
            },
840
        )
841
    }
842

            
843
    pub(crate) fn DataExprListEnum(input: ParseNode) -> ParseResult<DataExpr> {
844
        match_nodes!(input.into_children();
845
            [DataExprList(expressions)] => {
846
                Ok(DataExpr::List(expressions))
847
            },
848
        )
849
    }
850

            
851
    pub(crate) fn DataExprBagEnum(input: ParseNode) -> ParseResult<DataExpr> {
852
        match_nodes!(input.into_children();
853
            [BagEnumEltList(elements)] => {
854
                Ok(DataExpr::Bag(elements))
855
            },
856
        )
857
    }
858

            
859
    fn BagEnumEltList(input: ParseNode) -> ParseResult<Vec<BagElement>> {
860
        match_nodes!(input.into_children();
861
            [BagEnumElt(elements)..] => {
862
                Ok(elements.collect())
863
            },
864
        )
865
    }
866

            
867
    fn BagEnumElt(input: ParseNode) -> ParseResult<BagElement> {
868
        match_nodes!(input.into_children();
869
            [DataExpr(expr), DataExpr(multiplicity)] => {
870
                Ok(BagElement { expr, multiplicity })
871
            },
872
        )
873
    }
874

            
875
    pub(crate) fn DataExprSetEnum(input: ParseNode) -> ParseResult<DataExpr> {
876
        match_nodes!(input.into_children();
877
            [DataExprList(expressions)] => {
878
                Ok(DataExpr::Set(expressions))
879
            },
880
        )
881
    }
882

            
883
    pub(crate) fn DataExprSetBagComp(input: ParseNode) -> ParseResult<DataExpr> {
884
        match_nodes!(input.into_children();
885
            [VarDecl(variable), DataExpr(predicate)] => {
886
                Ok(DataExpr::SetBagComp { variable, predicate: Box::new(predicate) })
887
            },
888
        )
889
    }
890

            
891
    pub(crate) fn Number(input: ParseNode) -> ParseResult<DataExpr> {
892
        Ok(DataExpr::Number(input.as_str().into()))
893
    }
894

            
895
    fn VarDecl(decl: ParseNode) -> ParseResult<IdDecl> {
896
        let span = decl.as_span();
897
        match_nodes!(decl.into_children();
898
            [IdAt(identifier), SortExpr(sort)] => {
899
                Ok(IdDecl::new(identifier, sort, span.into()))
900
            },
901
        )
902
    }
903

            
904
    pub(crate) fn DataExprLambda(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
905
        match_nodes!(input.into_children();
906
            [VarsDeclList(vars)] => {
907
                Ok(vars)
908
            },
909
        )
910
    }
911

            
912
    pub(crate) fn DataExprForall(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
913
        match_nodes!(input.into_children();
914
            [VarsDeclList(vars)] => {
915
                Ok(vars)
916
            },
917
        )
918
    }
919

            
920
    pub(crate) fn DataExprExists(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
921
        match_nodes!(input.into_children();
922
            [VarsDeclList(vars)] => {
923
                Ok(vars)
924
            },
925
        )
926
    }
927

            
928
    pub(crate) fn ActFrm(input: ParseNode) -> ParseResult<ActFrm> {
929
        parse_actfrm(input.children().as_pairs().clone())
930
    }
931

            
932
    pub(crate) fn ActIdSet(actions: ParseNode) -> ParseResult<Vec<String>> {
933
        match_nodes!(actions.into_children();
934
            [IdList(list)] => {
935
                Ok(list)
936
            },
937
        )
938
    }
939

            
940
    fn MultActId(actions: ParseNode) -> ParseResult<MultiActionLabel> {
941
        match_nodes!(actions.into_children();
942
            [Id(action), Id(actions)..] => {
943
                Ok(MultiActionLabel { actions: iter::once(action).chain(actions).collect() })
944
            },
945
        )
946
    }
947

            
948
    fn MultActIdList(actions: ParseNode) -> ParseResult<Vec<MultiActionLabel>> {
949
        match_nodes!(actions.into_children();
950
            [MultActId(action), MultActId(actions)..] => {
951
                Ok(iter::once(action).chain(actions).collect())
952
            },
953
        )
954
    }
955

            
956
    pub(crate) fn MultActIdSet(actions: ParseNode) -> ParseResult<Vec<MultiActionLabel>> {
957
        match_nodes!(actions.into_children();
958
            [MultActIdList(list)] => {
959
                Ok(list)
960
            },
961
        )
962
    }
963

            
964
    fn ProcExpr(input: ParseNode) -> ParseResult<ProcessExpr> {
965
        parse_process_expr(input.children().as_pairs().clone())
966
    }
967

            
968
    fn ProcExprNoIf(input: ParseNode) -> ParseResult<ProcessExpr> {
969
        parse_process_expr(input.children().as_pairs().clone())
970
    }
971

            
972
    pub(crate) fn ProcExprId(input: ParseNode) -> ParseResult<ProcessExpr> {
973
        match_nodes!(input.into_children();
974
            [Id(identifier)] => {
975
                Ok(ProcessExpr::Id(identifier, Vec::new()))
976
            },
977
            [Id(identifier), AssignmentList(assignments)] => {
978
                Ok(ProcessExpr::Id(identifier, assignments))
979
            },
980
        )
981
    }
982

            
983
    pub(crate) fn ProcExprBlock(input: ParseNode) -> ParseResult<ProcessExpr> {
984
        match_nodes!(input.into_children();
985
            [ActIdSet(actions), ProcExpr(expr)] => {
986
                Ok(ProcessExpr::Block {
987
                    actions,
988
                    operand: Box::new(expr),
989
                })
990
            },
991
        )
992
    }
993

            
994
    pub(crate) fn ProcExprIf(input: ParseNode) -> ParseResult<DataExpr> {
995
        match_nodes!(input.into_children();
996
            [DataExpr(condition)] => {
997
                Ok(condition)
998
            },
999
        )
    }
    pub(crate) fn ProcExprIfThen(input: ParseNode) -> ParseResult<(DataExpr, ProcessExpr)> {
        match_nodes!(input.into_children();
            [DataExpr(condition), ProcExprNoIf(expr)] => {
                Ok((condition, expr))
            },
        )
    }
    pub(crate) fn ProcExprAllow(input: ParseNode) -> ParseResult<ProcessExpr> {
        match_nodes!(input.into_children();
            [MultActIdSet(actions), ProcExpr(expr)] => {
                Ok(ProcessExpr::Allow {
                    actions,
                    operand: Box::new(expr),
                })
            },
        )
    }
    pub(crate) fn ProcExprHide(input: ParseNode) -> ParseResult<ProcessExpr> {
        match_nodes!(input.into_children();
            [ActIdSet(actions), ProcExpr(expr)] => {
                Ok(ProcessExpr::Hide {
                    actions,
                    operand: Box::new(expr),
                })
            },
        )
    }
    fn ActionList(actions: ParseNode) -> ParseResult<Vec<Action>> {
        match_nodes!(actions.into_children();
            [Action(action), Action(actions)..] => {
                Ok(iter::once(action).chain(actions).collect())
            },
        )
    }
    fn MultiActTau(_input: ParseNode) -> ParseResult<()> {
        Ok(())
    }
    fn ProcExprDelta(_input: ParseNode) -> ParseResult<()> {
        Ok(())
    }
    pub(crate) fn MultAct(input: ParseNode) -> ParseResult<MultiAction> {
        match_nodes!(input.into_children();
            [MultiActTau(_)] => {
                Ok(MultiAction { actions: Vec::new() })
            },
            [ActionList(actions)] => {
                Ok(MultiAction { actions })
            },
        )
    }
    fn CommExpr(action: ParseNode) -> ParseResult<CommExpr> {
        match_nodes!(action.into_children();
            [Id(id), MultActId(multiact), Id(to)] => {
                let mut actions = vec![id];
                actions.extend(multiact.actions);
                Ok(CommExpr {
                    from: MultiActionLabel { actions },
                    to
                })
            },
        )
    }
    fn CommExprList(actions: ParseNode) -> ParseResult<Vec<CommExpr>> {
        match_nodes!(actions.into_children();
            [CommExpr(action), CommExpr(actions)..] => {
                Ok(iter::once(action).chain(actions).collect())
            },
        )
    }
    pub(crate) fn CommExprSet(actions: ParseNode) -> ParseResult<Vec<CommExpr>> {
        match_nodes!(actions.into_children();
            [CommExprList(list)] => {
                Ok(list)
            },
        )
    }
    pub(crate) fn ProcExprRename(input: ParseNode) -> ParseResult<ProcessExpr> {
        match_nodes!(input.into_children();
            [RenExprSet(renames), ProcExpr(expr)] => {
                Ok(ProcessExpr::Rename {
                    renames,
                    operand: Box::new(expr),
                })
            },
        )
    }
    pub(crate) fn ProcExprComm(input: ParseNode) -> ParseResult<ProcessExpr> {
        match_nodes!(input.into_children();
            [CommExprSet(comm), ProcExpr(expr)] => {
                Ok(ProcessExpr::Comm {
                    comm,
                    operand: Box::new(expr),
                })
            },
        )
    }
    pub(crate) fn Action(input: ParseNode) -> ParseResult<Action> {
        match_nodes!(input.into_children();
            [Id(id), DataExprList(args)] => {
                Ok(Action { id, args })
            },
            [Id(id)] => {
                Ok(Action { id, args: Vec::new() })
            },
        )
    }
    fn RenExprSet(renames: ParseNode) -> ParseResult<Vec<Rename>> {
        match_nodes!(renames.into_children();
            [RenExprList(renames)] => {
                Ok(renames)
            },
        )
    }
    fn RenExprList(renames: ParseNode) -> ParseResult<Vec<Rename>> {
        match_nodes!(renames.into_children();
            [RenExpr(renames)..] => {
                Ok(renames.collect())
            },
        )
    }
    fn RenExpr(renames: ParseNode) -> ParseResult<Rename> {
        match_nodes!(renames.into_children();
            [Id(from), Id(to)] => {
                Ok(Rename { from, to })
            },
        )
    }
    pub(crate) fn ProcExprSum(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn ProcExprDist(input: ParseNode) -> ParseResult<(Vec<IdDecl>, DataExpr)> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables), DataExpr(expr)] => {
                Ok((variables, expr))
            },
        )
    }
    pub(crate) fn StateFrmDelay(input: ParseNode) -> ParseResult<StateFrm> {
        match_nodes!(input.into_children();
            [DataExpr(delay)] => {
                Ok(StateFrm::Delay(delay))
            },
        )
    }
    pub(crate) fn StateFrmYaled(input: ParseNode) -> ParseResult<StateFrm> {
        match_nodes!(input.into_children();
            [DataExpr(delay)] => {
                Ok(StateFrm::Yaled(delay))
            },
        )
    }
    pub(crate) fn StateFrmNegation(input: ParseNode) -> ParseResult<StateFrm> {
        match_nodes!(input.into_children();
            [StateFrm(state)] => {
                Ok(StateFrm::Unary { op: crate::StateFrmUnaryOp::Negation, expr: Box::new(state) })
            },
        )
    }
    pub(crate) fn StateFrmLeftConstantMultiply(input: ParseNode) -> ParseResult<DataExpr> {
        match_nodes!(input.into_children();
            [DataValExpr(expr)] => {
                Ok(expr)
            },
        )
    }
    pub(crate) fn StateFrmRightConstantMultiply(input: ParseNode) -> ParseResult<DataExpr> {
        match_nodes!(input.into_children();
            [DataValExpr(expr)] => {
                Ok(expr)
            },
        )
    }
    pub(crate) fn StateFrmDiamond(input: ParseNode) -> ParseResult<RegFrm> {
        match_nodes!(input.into_children();
            [RegFrm(formula)] => {
                Ok(formula)
            },
        )
    }
    pub(crate) fn StateFrmBox(input: ParseNode) -> ParseResult<RegFrm> {
        match_nodes!(input.into_children();
            [RegFrm(formula)] => {
                Ok(formula)
            },
        )
    }
    pub(crate) fn StateFrmSpec(spec: ParseNode) -> ParseResult<UntypedStateFrmSpec> {
        let mut map_declarations = Vec::new();
        let mut equation_declarations = Vec::new();
        let mut constructor_declarations = Vec::new();
        let mut sort_declarations = Vec::new();
        let mut action_declarations = Vec::new();
        let mut form_spec = None;
        let span = spec.as_span();
        for child in spec.into_children() {
            match child.as_rule() {
                Rule::StateFrmSpecElt => {
                    let element = child
                        .into_children()
                        .next()
                        .expect("StateFrmSpecElt has exactly one child");
                    match element.as_rule() {
                        Rule::ConsSpec => {
                            constructor_declarations.append(&mut Mcrl2Parser::ConsSpec(element)?);
                        }
                        Rule::MapSpec => {
                            map_declarations.append(&mut Mcrl2Parser::MapSpec(element)?);
                        }
                        Rule::EqnSpec => {
                            equation_declarations.append(&mut Mcrl2Parser::EqnSpec(element)?);
                        }
                        Rule::SortSpec => {
                            sort_declarations.append(&mut Mcrl2Parser::SortSpec(element)?);
                        }
                        Rule::ActSpec => {
                            action_declarations.append(&mut Mcrl2Parser::ActSpec(element)?);
                        }
                        _ => {
                            unimplemented!("Unexpected rule in StateFrmSpecElt: {:?}", element.as_rule());
                        }
                    }
                }
                Rule::StateFrm => {
                    if form_spec.is_some() {
                        return Err(Error::new_from_span(
                            ErrorVariant::CustomError {
                                message: "Multiple state formula specifications are not allowed".to_string(),
                            },
                            child.as_span(),
                        ));
                    }
                    form_spec = Some(Mcrl2Parser::StateFrm(child)?);
                }
                Rule::FormSpec => {
                    if form_spec.is_some() {
                        return Err(Error::new_from_span(
                            ErrorVariant::CustomError {
                                message: "Multiple state formula specifications are not allowed".to_string(),
                            },
                            child.as_span(),
                        ));
                    }
                    form_spec = Some(Mcrl2Parser::FormSpec(child)?);
                }
                Rule::EOI => {
                    // End of input
                    break;
                }
                _ => {
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
                }
            }
        }
        let data_specification = UntypedDataSpecification {
            map_declarations,
            equation_declarations,
            constructor_declarations,
            sort_declarations,
        };
        Ok(UntypedStateFrmSpec {
            data_specification,
            action_declarations,
            formula: form_spec.ok_or(Error::new_from_span(
                ErrorVariant::CustomError {
                    message: "No state formula found in the state formula specification".to_string(),
                },
                span,
            ))?,
        })
    }
    pub(crate) fn PbesExprForall(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(vars)] => {
                Ok(vars)
            },
        )
    }
    pub(crate) fn PbesExprExists(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(vars)] => {
                Ok(vars)
            },
        )
    }
    pub(crate) fn PresExprEqinf(input: ParseNode) -> ParseResult<PresExpr> {
        match_nodes!(input.into_children();
            [PresExpr(body)] => {
                Ok(PresExpr::Equal {
                    eq: Eq::EqInf,
                    body: Box::new(body),
                })
            },
        )
    }
    pub(crate) fn PresExprEqninf(input: ParseNode) -> ParseResult<PresExpr> {
        match_nodes!(input.into_children();
            [PresExpr(body)] => {
                Ok(PresExpr::Equal {
                    eq: Eq::EqnInf,
                    body: Box::new(body),
                })
            },
        )
    }
    pub(crate) fn PresExprCondsm(input: ParseNode) -> ParseResult<PresExpr> {
        match_nodes!(input.into_children();
            [PresExpr(expr), PresExpr(then), PresExpr(else_)] => {
                Ok(PresExpr::Condition{
                    condition: Condition::Condsm,
                    lhs: Box::new(expr),
                    then: Box::new(then),
                    else_: Box::new(else_),
                })
            },
        )
    }
    pub(crate) fn PresExprCondeq(input: ParseNode) -> ParseResult<PresExpr> {
        match_nodes!(input.into_children();
            [PresExpr(expr), PresExpr(then), PresExpr(else_)] => {
                Ok(PresExpr::Condition{
                    condition: Condition::Condeq,
                    lhs: Box::new(expr),
                    then: Box::new(then),
                    else_: Box::new(else_),
                })
            },
        )
    }
    fn IdsDecl(decl: ParseNode) -> ParseResult<Vec<IdDecl>> {
        let span = decl.as_span();
        match_nodes!(decl.into_children();
            [IdInfixList(identifiers), SortExpr(sort)] => {
49406
                let id_decls = identifiers.into_iter().map(|identifier| {
49406
                    IdDecl::new(identifier, sort.clone(), span.into())
49406
                }).collect();
                Ok(id_decls)
            },
        )
    }
    fn EqnSpec(spec: ParseNode) -> ParseResult<Vec<EqnSpec>> {
        let mut ids = Vec::new();
        match_nodes!(spec.into_children();
            [VarSpec(variables), EqnDecl(decls)..] => {
                ids.push(EqnSpec { variables, equations: decls.collect() });
            },
            [EqnDecl(decls)..] => {
                ids.push(EqnSpec { variables: Vec::new(), equations: decls.collect() });
            },
        );
        Ok(ids)
    }
    fn EqnDecl(decl: ParseNode) -> ParseResult<EqnDecl> {
        let span = decl.as_span();
        match_nodes!(decl.into_children();
            [DataExpr(condition), DataExpr(lhs), DataExpr(rhs)] => {
                Ok(EqnDecl { condition: Some(condition), lhs, rhs, span: span.into() })
            },
            [DataExpr(lhs), DataExpr(rhs)] => {
                Ok(EqnDecl { condition: None, lhs, rhs, span: span.into() })
            },
        )
    }
    fn StateFrm(input: ParseNode) -> ParseResult<StateFrm> {
        parse_statefrm(input.children().as_pairs().clone())
    }
    fn RegFrm(input: ParseNode) -> ParseResult<RegFrm> {
        parse_regfrm(input.children().as_pairs().clone())
    }
    fn StateVarDecl(input: ParseNode) -> ParseResult<StateVarDecl> {
        let span = input.as_span();
        match_nodes!(input.into_children();
            [Id(identifier), StateVarAssignmentList(arguments)] => {
                Ok(StateVarDecl {
                    identifier,
                    arguments,
                    span: span.into(),
                })
            },
            [Id(identifier)] => {
                Ok(StateVarDecl {
                    identifier,
                    arguments: Vec::new(),
                    span: span.into(),
                })
            }
        )
    }
    fn StateVarAssignmentList(input: ParseNode) -> ParseResult<Vec<StateVarAssignment>> {
        match_nodes!(input.into_children();
            [StateVarAssignment(assignments)..] => {
                Ok(assignments.collect())
            }
        )
    }
    fn StateVarAssignment(input: ParseNode) -> ParseResult<StateVarAssignment> {
        match_nodes!(input.into_children();
            [Id(identifier), SortExpr(sort), DataExpr(expr)] => {
                Ok(StateVarAssignment {
                    identifier,
                    sort,
                    expr,
                })
            }
        )
    }
    fn ActionRenameRuleSpec(spec: ParseNode) -> ParseResult<Vec<ActionRenameDecl>> {
        match_nodes!(spec.into_children();
            [VarSpec(variables_specification), ActionRenameRule(renames)..] => {
                Ok(renames.map(|rename_rule| {
                    ActionRenameDecl { variables_specification: variables_specification.clone(), rename_rule }
                }).collect())
            },
            [ActionRenameRule(renames)..] => {
                Ok(renames.map(|rename_rule| {
                    ActionRenameDecl { variables_specification: Vec::new(), rename_rule }
                }).collect())
            },
        )
    }
    fn ActionRenameRule(input: ParseNode) -> ParseResult<ActionRenameRule> {
        match_nodes!(input.into_children();
            [DataExpr(condition), Action(action), ActionRenameRuleRHS(rhs)] => {
                Ok(ActionRenameRule { condition: Some(condition), action, rhs })
            },
            [Action(action), ActionRenameRuleRHS(rhs)] => {
                Ok(ActionRenameRule { condition: None, action, rhs })
            },
        )
    }
    fn ActionRenameRuleRHS(input: ParseNode) -> ParseResult<ActionRHS> {
        match_nodes!(input.into_children();
            [Action(action)] => {
                Ok(ActionRHS::Action(action))
            },
            [MultiActTau(_)] => {
                Ok(ActionRHS::Tau)
            },
            [ProcExprDelta(_)] => {
                Ok(ActionRHS::Delta)
            },
        )
    }
    fn FormSpec(input: ParseNode) -> ParseResult<StateFrm> {
        match_nodes!(input.into_children();
            [StateFrm(formula)] => {
                Ok(formula)
            },
        )
    }
    pub(crate) fn StateFrmSup(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn StateFrmInf(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn StateFrmSum(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn PresExprInf(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn PresExprSup(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn PresExprSum(input: ParseNode) -> ParseResult<Vec<IdDecl>> {
        match_nodes!(input.into_children();
            [VarsDeclList(variables)] => {
                Ok(variables)
            },
        )
    }
    pub(crate) fn PresExprLeftConstantMultiply(input: ParseNode) -> ParseResult<DataExpr> {
        match_nodes!(input.into_children();
            [DataExpr(constant)] => {
                Ok(constant)
            },
        )
    }
    pub(crate) fn PresExprRightConstMultiply(input: ParseNode) -> ParseResult<DataExpr> {
        match_nodes!(input.into_children();
            [DataExpr(constant)] => {
                Ok(constant)
            },
        )
    }
    fn EOI(_input: ParseNode) -> ParseResult<()> {
        Ok(())
    }
}