1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use crate::types::Label;
use crate::Result;
use pretty::RcDoc;

#[derive(Debug, Clone)]
pub enum IDLType {
    PrimT(PrimType),
    VarT(String),
    FuncT(FuncType),
    OptT(Box<IDLType>),
    VecT(Box<IDLType>),
    RecordT(Vec<TypeField>),
    VariantT(Vec<TypeField>),
    ServT(Vec<Binding>),
    ClassT(Vec<IDLType>, Box<IDLType>),
    PrincipalT,
}

#[derive(Debug, Clone)]
pub struct IDLTypes {
    pub args: Vec<IDLType>,
}

macro_rules! enum_to_doc {
    (pub enum $name:ident {
        $($variant:ident),*,
    }) => {
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        pub enum $name {
            $($variant),*
        }
        impl $name {
            pub(crate) fn to_doc(&self) -> RcDoc<()> {
                match self {
                    $($name::$variant => RcDoc::text(stringify!($variant).to_lowercase())),*
                }
            }
            pub fn str_to_enum(str: &str) -> Option<Self> {
                $(if str == stringify!($variant).to_lowercase() {
                    return Some($name::$variant);
                });*
                return None;
            }
        }
    };
}

enum_to_doc! {
pub enum PrimType {
    Nat,
    Nat8,
    Nat16,
    Nat32,
    Nat64,
    Int,
    Int8,
    Int16,
    Int32,
    Int64,
    Float32,
    Float64,
    Bool,
    Text,
    Null,
    Reserved,
    Empty,
}}

enum_to_doc! {
pub enum FuncMode {
    Oneway,
    Query,
}}

#[derive(Debug, Clone)]
pub struct FuncType {
    pub modes: Vec<FuncMode>,
    pub args: Vec<IDLType>,
    pub rets: Vec<IDLType>,
}

impl FuncType {
    pub fn is_query(&self) -> bool {
        for m in self.modes.iter() {
            if let FuncMode::Query = m {
                return true;
            }
        }
        false
    }
}

#[derive(Debug, Clone)]
pub struct TypeField {
    pub label: Label,
    pub typ: IDLType,
}

#[derive(Debug)]
pub enum Dec {
    TypD(Binding),
    ImportD(String),
}

#[derive(Debug, Clone)]
pub struct Binding {
    pub id: String,
    pub typ: IDLType,
}

#[derive(Debug)]
pub struct IDLProg {
    pub decs: Vec<Dec>,
    pub actor: Option<IDLType>,
}

impl std::str::FromStr for IDLProg {
    type Err = crate::Error;
    fn from_str(str: &str) -> Result<Self> {
        let lexer = super::token::Tokenizer::new(str);
        Ok(super::grammar::IDLProgParser::new().parse(lexer)?)
    }
}

impl std::str::FromStr for IDLType {
    type Err = crate::Error;
    fn from_str(str: &str) -> Result<Self> {
        let lexer = super::token::Tokenizer::new(str);
        Ok(super::grammar::TypParser::new().parse(lexer)?)
    }
}

impl std::str::FromStr for IDLTypes {
    type Err = crate::Error;
    fn from_str(str: &str) -> Result<Self> {
        let lexer = super::token::Tokenizer::new(str);
        Ok(super::grammar::TypsParser::new().parse(lexer)?)
    }
}

// Pretty printing

pub trait ToDoc {
    fn to_doc(&self) -> RcDoc<()>;
}

pub fn to_pretty<T>(doc: &T, width: usize) -> String
where
    T: ToDoc,
{
    let mut w = Vec::new();
    doc.to_doc().render(width, &mut w).unwrap();
    String::from_utf8(w).unwrap()
}

impl ToDoc for IDLProg {
    fn to_doc(&self) -> RcDoc<()> {
        let doc = RcDoc::concat(
            self.decs
                .iter()
                .map(|d| d.to_doc().append(RcDoc::text(";").append(RcDoc::line()))),
        );
        if self.actor.is_some() {
            let actor = self.actor.as_ref().unwrap();
            let doc = doc.append(RcDoc::text("service : "));
            match actor {
                IDLType::VarT(ref var) => doc.append(RcDoc::text(var.to_string())),
                IDLType::ServT(ref meths) => doc.append(meths_to_doc(meths)),
                _ => unreachable!(),
            }
        } else {
            doc
        }
    }
}

impl ToDoc for Dec {
    fn to_doc(&self) -> RcDoc<()> {
        match *self {
            Dec::TypD(ref b) => RcDoc::text("type ").append(b.to_doc()),
            Dec::ImportD(ref file) => RcDoc::text(format!("import \"{}\"", file)),
        }
    }
}

impl ToDoc for Binding {
    fn to_doc(&self) -> RcDoc<()> {
        RcDoc::text(format!("{} =", self.id))
            .append(RcDoc::space())
            .append(self.typ.to_doc())
            .nest(2)
            .group()
    }
}

impl ToDoc for IDLType {
    fn to_doc(&self) -> RcDoc<()> {
        match self {
            IDLType::PrimT(p) => p.to_doc(),
            IDLType::VarT(var) => RcDoc::text(var),
            IDLType::FuncT(func) => RcDoc::text("func")
                .append(RcDoc::space())
                .append(func.to_doc()),
            IDLType::OptT(ref t) => RcDoc::text("opt").append(RcDoc::space()).append(t.to_doc()),
            IDLType::VecT(ref t) => RcDoc::text("vec").append(RcDoc::space()).append(t.to_doc()),
            IDLType::RecordT(ref fs) => RcDoc::text("record ").append(fields_to_doc(fs)),
            IDLType::VariantT(ref fs) => RcDoc::text("variant ").append(fields_to_doc(fs)),
            IDLType::ServT(ref serv) => RcDoc::text("service ").append(meths_to_doc(serv)),
            IDLType::ClassT(_, _) => unreachable!(),
            IDLType::PrincipalT => RcDoc::text("principal"),
        }
        .nest(2)
        .group()
    }
}

impl ToDoc for FuncType {
    fn to_doc(&self) -> RcDoc<()> {
        args_to_doc(&self.args)
            .append(RcDoc::space())
            .append(RcDoc::text("-> "))
            .append(args_to_doc(&self.rets))
            .append(RcDoc::concat(
                self.modes.iter().map(|m| RcDoc::space().append(m.to_doc())),
            ))
    }
}

impl ToDoc for TypeField {
    fn to_doc(&self) -> RcDoc<()> {
        let colon = RcDoc::text(":").append(RcDoc::space());
        let doc = match &self.label {
            Label::Id(n) => RcDoc::as_string(n).append(colon),
            Label::Named(name) => RcDoc::text(name).append(colon),
            Label::Unnamed(_) => RcDoc::nil(),
        };
        doc.append(self.typ.to_doc()).nest(2).group()
    }
}

fn fields_to_doc(fields: &[TypeField]) -> RcDoc<()> {
    RcDoc::text("{")
        .append(
            RcDoc::concat(
                fields
                    .iter()
                    .map(|f| RcDoc::space().append(f.to_doc()).append(RcDoc::text(";"))),
            )
            .nest(2)
            .group(),
        )
        .append(RcDoc::space())
        .append(RcDoc::text("}"))
}

fn meths_to_doc(meths: &[Binding]) -> RcDoc<()> {
    RcDoc::text("{")
        .append(RcDoc::concat(meths.iter().map(|meth| {
            let doc = RcDoc::line().append(RcDoc::text(format!("{}:", meth.id)));
            let doc = match meth.typ {
                IDLType::VarT(ref var) => doc.append(RcDoc::space().append(RcDoc::text(var))),
                IDLType::FuncT(ref func) => {
                    doc.append(RcDoc::space().append(func.to_doc()).nest(2))
                }
                _ => unreachable!(),
            }
            .nest(2)
            .group();
            doc.append(RcDoc::text(";"))
        })))
        .append(RcDoc::space())
        .append(RcDoc::text("}"))
}

fn args_to_doc(args: &[IDLType]) -> RcDoc<()> {
    RcDoc::text("(")
        .append(
            RcDoc::intersperse(
                args.iter().map(|arg| arg.to_doc()),
                RcDoc::text(",").append(RcDoc::space()),
            )
            .nest(1)
            .group(),
        )
        .append(")")
}