1
use pest_derive::Parser;
2

            
3
20833145
#[derive(Parser)]
4
#[grammar = "mcrl2_grammar.pest"]
5
pub struct Mcrl2Parser;
6

            
7

            
8
#[cfg(test)]
9
mod tests {
10
    use indoc::indoc;
11
    use pest::Parser;
12

            
13
    use super::*;
14

            
15
    #[test]
16
1
    fn test_parse_term() {
17
1
        let term = "f(a, b)";
18
1

            
19
1
        let result = Mcrl2Parser::parse(Rule::TermSpec, term).unwrap();
20
1
        print!("{}", result);
21
1
    }
22

            
23
    #[test]
24
1
    fn test_parse_ifthen() {
25
1
        let expr = "init a -> b -> c <> delta;";
26
1

            
27
1
        let result = Mcrl2Parser::parse(Rule::MCRL2Spec, expr).unwrap();
28
1
        print!("{}", result);
29
1
    }
30

            
31
    #[test]
32
1
    fn test_parse_keywords() {
33
1
        let expr = "map or : Boolean # Boolean -> Boolean ;";
34
1

            
35
1
        let result = Mcrl2Parser::parse(Rule::MCRL2Spec, expr).unwrap();
36
1
        print!("{}", result);
37
1
    }
38

            
39
    #[test]
40
1
    fn test_parse_sort_spec() {
41
1
        let sort_spec = indoc! {"
42
1
            sort D = Bool -> Int -> Bool;
43
1
            
44
1

            
45
1
            % Test
46
1
            F     = struct d1 | d2;
47
1
            Error = struct e;
48
1
        "};
49
1

            
50
1
        let result = Mcrl2Parser::parse(Rule::MCRL2Spec, sort_spec).unwrap();
51
1
        print!("{}", result);
52
1
    }
53

            
54
    #[test]
55
1
    fn test_parse_regular_expression() {
56
1
        let spec = "[true++false]true";
57

            
58
1
        if let Err(y) = Mcrl2Parser::parse(Rule::StateFrmSpec, spec) {
59
            panic!("{}", y);
60
1
        }
61
1
    }
62

            
63
    #[test]
64
1
    fn test_parse_abp() {
65
1
        let abp_spec = indoc! {"
66
1
            % This file contains the alternating bit protocol, as described 
67
1
            % J.F. Groote and M.R. Mousavi. Modeling and analysis of communicating
68
1
            % systems. The MIT Press, 2014.
69
1
            %
70
1
            % The only exception is that the domain D consists of two data elements to
71
1
            % facilitate simulation.
72
1

            
73
1
            sort
74
1
            D     = struct d1 | d2;
75
1
            Error = struct e;
76
1

            
77
1
            act
78
1
            r1,s4: D;
79
1
            s2,r2,c2: D # Bool;
80
1
            s3,r3,c3: D # Bool;
81
1
            s3,r3,c3: Error;
82
1
            s5,r5,c5: Bool;
83
1
            s6,r6,c6: Bool;
84
1
            s6,r6,c6: Error;
85
1
            i;
86
1

            
87
1
            proc
88
1
            S(b:Bool)     = sum d:D. r1(d).T(d,b);
89
1
            T(d:D,b:Bool) = s2(d,b).(r6(b).S(!b)+(r6(!b)+r6(e)).T(d,b));
90
1

            
91
1
            R(b:Bool)     = sum d:D. r3(d,b).s4(d).s5(b).R(!b)+
92
1
                            (sum d:D.r3(d,!b)+r3(e)).s5(!b).R(b);
93
1

            
94
1
            K             = sum d:D,b:Bool. r2(d,b).(i.s3(d,b)+i.s3(e)).K;
95
1

            
96
1
            L             = sum b:Bool. r5(b).(i.s6(b)+i.s6(e)).L;
97
1

            
98
1
            init
99
1
            allow({r1,s4,c2,c3,c5,c6,i},
100
1
                comm({r2|s2->c2, r3|s3->c3, r5|s5->c5, r6|s6->c6},
101
1
                    S(true) || K || L || R(true)
102
1
                )
103
1
            );
104
1
        "};
105
1

            
106
1
        match Mcrl2Parser::parse(Rule::MCRL2Spec, abp_spec) {
107
1
            Ok(x) => {
108
1
                print!("{}", x);
109
1
            }
110
            Err(y) => {
111
                panic!("{}", y);
112
            }
113
        }
114
1
    }
115
}