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::Comm;
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
1518
                Ok(identifiers.iter().map(|name| ActDecl { identifier: name.clone(), args: Vec::new(), span: span.into() }).collect())
344
            },
345
            [IdList(identifiers), SortProduct(args)] => {
346
10440
                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 => rename_declarations.append(&mut Mcrl2Parser::ActionRenameRuleSpec(child)?),
443
                Rule::EOI => {
444
                    // End of input
445
                    break;
446
                }
447
                _ => {
448
                    unimplemented!("Unexpected rule: {:?}", child.as_rule());
449
                }
450
            }
451
        }
452

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

            
460
        Ok(UntypedActionRenameSpec {
461
            data_specification,
462
            action_declarations,
463
            rename_declarations,
464
        })
465
    }
466

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
718
        Ok(vars)
719
    }
720

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
992
    pub(crate) fn ProcExprIf(input: ParseNode) -> ParseResult<DataExpr> {
993
        match_nodes!(input.into_children();
994
            [DataExpr(condition)] => {
995
                Ok(condition)
996
            },
997
        )
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<Comm> {
        match_nodes!(action.into_children();
            [Id(id), MultActId(multiact), Id(to)] => {
                let mut actions = vec![id];
                actions.extend(multiact.actions);
                Ok(Comm {
                    from: MultiActionLabel { actions },
                    to
                })
            },
        )
    }
    fn CommExprList(actions: ParseNode) -> ParseResult<Vec<Comm>> {
        match_nodes!(actions.into_children();
            [CommExpr(action), CommExpr(actions)..] => {
                Ok(iter::once(action).chain(actions).collect())
            },
        )
    }
    fn CommExprSet(actions: ParseNode) -> ParseResult<Vec<Comm>> {
        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)] => {
42348
                let id_decls = identifiers.into_iter().map(|identifier| {
42348
                    IdDecl::new(identifier, sort.clone(), span.into())
42348
                }).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(())
    }
}