aboutsummaryrefslogtreecommitdiff
path: root/primrose/src/parser.rs
blob: 580c6b5ccf0c267664427bfb542d791b40edb64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
extern crate peg;
use peg::parser;

use std::iter::FromIterator;
use std::vec::Vec;

use crate::types::{Bounds, Name, Type, TypeVar};

pub type Id = String;

pub type Literal = String;

#[derive(Clone, Debug)]
pub enum Refinement {
    Prop(Box<Term>),
    AndProps(Box<Refinement>, Box<Refinement>),
}

#[derive(Clone, Debug)]
pub enum Term {
    LitTerm(Box<Literal>),
    VarTerm(Box<Id>),
    LambdaTerm((Box<Id>, Box<Bounds>), Box<Term>),
    AppTerm(Box<Term>, Box<Term>),
}

impl Term {
    pub fn is_quantifier(&self) -> bool {
        match self {
            Term::VarTerm(id) => id.to_string().eq("forall"),
            _ => false,
        }
    }

    pub fn require_cdr(&self) -> bool {
        match self {
            Term::VarTerm(id) => id.to_string().eq("pop"),
            _ => false,
        }
    }
}

impl ToString for Term {
    fn to_string(&self) -> String {
        match self {
            Term::LitTerm(l) => l.to_string(),
            Term::VarTerm(id) => id.to_string(),
            Term::LambdaTerm((id, bounds), t) => id.to_string(),
            Term::AppTerm(t1, t2) => t1.to_string() + &t2.to_string(),
        }
    }
}

#[derive(Clone, Debug)]
pub enum Decl {
    PropertyDecl((Box<Id>, Box<Type>), Box<Term>),
    ConTypeDecl(Box<Type>, (Box<Id>, Box<Bounds>, Box<Refinement>)),
}

impl Decl {
    pub fn is_prop_decl(&self) -> bool {
        matches!(self, Decl::PropertyDecl(_, _))
    }

    pub fn is_contype_decl(&self) -> bool {
        matches!(self, Decl::ConTypeDecl(_, _))
    }

    pub fn get_name(&self) -> String {
        match self {
            Decl::ConTypeDecl(con_ty, _) => {
                let (con, _) = con_ty.get_con_elem().unwrap();
                con
            }
            Decl::PropertyDecl((id, _), _) => id.to_string(),
        }
    }
}

pub type Spec = Vec<Decl>;
pub type Code = String;

#[derive(Clone, Debug)]
pub enum Block {
    SpecBlock(Box<Spec>, usize),
    CodeBlock(Box<Code>, usize),
}

impl Block {
    pub fn is_spec_block(&self) -> bool {
        matches!(self, Block::SpecBlock(_, _))
    }

    pub fn is_code_block(&self) -> bool {
        matches!(self, Block::CodeBlock(_, _))
    }

    pub fn extract_spec(&self) -> Spec {
        match self {
            Block::SpecBlock(spec, _) => spec.to_vec(),
            _ => Vec::new(),
        }
    }

    pub fn extract_code(&self) -> Code {
        match self {
            Block::CodeBlock(code, _) => code.to_string(),
            _ => String::new(),
        }
    }
}

pub type Prog = Vec<Block>;

parser! {
pub grammar spec() for str {
    pub rule id() -> Id
        = s:$(!keyword() ([ 'a'..='z' | 'A'..='Z' | '_' ]['a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '?' ]*))
        { s.into() }

    pub rule name() -> Name
        = s:$(!keyword() ([ 'a'..='z' | 'A'..='Z' | '_' ]['a'..='z' | 'A'..='Z' | '0'..='9' ]*))
        { s.into() }

    pub rule keyword() -> ()
        = ("crate" / "super" / "self" / "Self" / "const" / "mut" / "true" / "false" / "pub" / "in" / "from" / "with"
            / "f32"/ "i32" / "u32" / "bool" / "let" / "if" / "else" / "for" / "while" / "fn" / "do")
            ![ 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '?' ]

    pub rule literal() -> Literal
        = s:$("true" / "false")
        { s.into() }

    pub rule ty() -> Type
        = precedence! {
            n:name() "<" _ t:ty() _ ">"
            { Type::Con(Box::new(n), Box::new(t), Box::new(Bounds::from(["Container".to_string()]))) }
            --
            n:name()
            { Type::Var(TypeVar::new(n)) }
        }

    pub rule term() -> Term
        = precedence!{
            lit: literal() { Term::LitTerm(Box::new(lit)) }
            --
            v:id() { Term::VarTerm(Box::new(v)) }
            --
            "\\" v:id() _ "->" _ t:term() { Term::LambdaTerm((Box::new(v), Box::default()), Box::new(t)) }
            --
            "\\" v:id() _ "<:" _ "(" _ b:bounds() _ ")" _ "->" _ t:term() { Term::LambdaTerm((Box::new(v), Box::new(b)), Box::new(t)) }
            --
            "(" _ t1:term() __ t2:term() _ ")" { Term::AppTerm(Box::new(t1), Box::new(t2)) }
        }

    pub rule refinement() -> Refinement
        = precedence!{
            t:term() { Refinement::Prop(Box::new(t)) }
            --
            "(" _ p1:refinement() __ "and" __ p2:refinement() _ ")" { Refinement::AndProps(Box::new(p1), Box::new(p2)) }
        }

    pub rule bounds() -> Bounds
        = l: ((_ n:name() _ {n}) ++ "," ) { Bounds::from_iter(l.iter().cloned()) }

    pub rule decl() -> Decl
        = precedence! {
            _ "property" __ p:id() _ "<" _ ty:ty() _ ">" _ "{" _ t:term() _ "}" _
            {
                Decl::PropertyDecl((Box::new(p), Box::new(ty)), Box::new(t))
            }
            --
            _ "type" __ ty:ty() _ "=" _ "{" _ c:id() _ "impl" __ "(" _ b:bounds() _ ")" _ "|" _ t:refinement() _ "}" _
            {
                Decl::ConTypeDecl(Box::new(ty), (Box::new(c), Box::new(b), Box::new(t)))
            }
        }

    pub rule spec() -> Spec
        = _ "/*SPEC*" _ decls: (d:decl() { d }) ** _ _ "*ENDSPEC*/" _
        {
            decls
        }

    pub rule code() -> Code
        = _ "/*CODE*/" c:$((!"/*ENDCODE*/"!"/*SPEC*"!"*ENDSPEC*/"[_])*) "/*ENDCODE*/" _ { c.into() }

    pub rule block() -> Block
        = precedence! {
            _ p:position!() s:spec() _
            {
                Block::SpecBlock(Box::new(s), p)
            }
            --
            _ p:position!() c:code() _
            {
                Block::CodeBlock(Box::new(c), p)
            }
        }

    pub rule prog() -> Prog
        = _ blocks: (b:block() { b }) ** _ _
        {
            blocks
        }

    rule _ = quiet!{[' ' | '\n' | '\t']*}
    rule __ = quiet!{[' ' | '\n' | '\t']+}

}}