tl_scheme/
tokens.rs

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use crc::{Crc, CRC_32_ISO_HDLC};
use rustc_hash::FxHashMap;

/// Parsed scheme with all types and functions.
#[derive(Debug, Clone, Default)]
pub struct Scheme<'a> {
    pub builtins: FxHashMap<&'a str, BuiltinConstructor<'a>>,
    pub types: FxHashMap<&'a str, Constructor<'a>>,
    pub boxed_types: FxHashMap<&'a str, Vec<&'a str>>,
    pub functions: FxHashMap<&'a str, Constructor<'a>>,
}

impl<'a> Scheme<'a> {
    /// Computes all TL ids in the scheme
    pub fn compute_all_ids(&self) -> FxHashMap<u32, (ConstructorKind, &Constructor<'a>)> {
        let mut ids = FxHashMap::with_capacity_and_hasher(
            self.types.len() + self.functions.len(),
            Default::default(),
        );

        for ty in self.types.values() {
            ids.insert(ty.compute_tl_id(), (ConstructorKind::Type, ty));
        }

        for func in self.functions.values() {
            ids.insert(func.compute_tl_id(), (ConstructorKind::Function, func));
        }

        ids
    }

    /// Resolves all types in the scheme
    pub fn validate(&self) -> Result<(), ValidationError> {
        for ty in self.types.values() {
            self.check_constructor(ty)?;
        }
        for func in self.functions.values() {
            self.check_constructor(func)?;
        }
        Ok(())
    }

    /// Finds a type variant or function with the given name
    pub fn find_constructor(&self, name: &str) -> Option<(ConstructorKind, &Constructor<'a>)> {
        if let Some(constructor) = self.get_type_variant(name) {
            Some((ConstructorKind::Type, constructor))
        } else {
            self.get_function(name)
                .map(|constructor| (ConstructorKind::Function, constructor))
        }
    }

    /// Finds type variant with the given name
    pub fn get_type_variant(&self, name: &str) -> Option<&Constructor<'a>> {
        self.types.get(name)
    }

    /// Finds function with the given name
    pub fn get_function(&self, name: &str) -> Option<&Constructor<'a>> {
        self.functions.get(name)
    }

    fn check_constructor(&self, decl: &Constructor<'a>) -> Result<(), ValidationError> {
        for (i, field) in decl.fields.iter().enumerate() {
            let prev_fields = &decl.fields[..i];
            self.check_type(&field.ty, &decl.type_parameters, prev_fields)?;
        }

        if !self.boxed_types.contains_key(decl.output.ty)
            && !decl
                .type_parameters
                .iter()
                .any(|field| field.name == Some(decl.output.ty))
        {
            return Err(ValidationError::UnknownType(decl.output.to_string()));
        }

        if let Some(ty_param) = &decl.output.ty_param {
            self.check_type(ty_param, &decl.type_parameters, &[])?;
        }

        Ok(())
    }

    fn check_type(
        &self,
        ty: &Type<'a>,
        type_parameters: &[Field<'a>],
        prev_fields: &[Field<'a>],
    ) -> Result<(), ValidationError> {
        match ty {
            Type::Int => {}
            Type::Named { ty } => {
                if !self.is_declared(ty)
                    && !type_parameters.iter().any(|field| field.name == Some(ty))
                {
                    return Err(ValidationError::UnknownType(ty.to_string()));
                }
            }
            Type::Generic { ty, ty_param } => {
                if !self.is_declared(ty)
                    && !type_parameters.iter().any(|field| field.name == Some(ty))
                {
                    return Err(ValidationError::UnknownType(ty.to_string()));
                }
                self.check_type(ty_param, type_parameters, &[])?;
            }
            Type::Flagged {
                flags_field, ty, ..
            } => {
                prev_fields
                    .iter()
                    .find(|field| field.name == Some(flags_field))
                    .ok_or_else(|| ValidationError::UnknownField(flags_field.to_string()))?;
                self.check_type(ty, type_parameters, prev_fields)?;
            }
            Type::Repeated { ty, .. } => {
                for field in ty {
                    self.check_type(&field.ty, type_parameters, prev_fields)?;
                }
            }
        }

        Ok(())
    }

    fn is_declared(&self, ty: &str) -> bool {
        self.types.contains_key(ty)
            || self.boxed_types.contains_key(ty)
            || self.builtins.contains_key(ty)
    }
}

/// Predefined constructor
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct BuiltinConstructor<'a> {
    pub variant: &'a str,
    pub tl_id: Option<u32>,
    pub output: &'a str,
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ConstructorKind {
    Type,
    Function,
}

impl std::fmt::Display for ConstructorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConstructorKind::Type => write!(f, "type"),
            ConstructorKind::Function => write!(f, "function"),
        }
    }
}

/// Type or function declaration
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Constructor<'a> {
    pub variant: &'a str,
    pub tl_id: Option<u32>,
    pub type_parameters: Vec<Field<'a>>,
    pub fields: Vec<Field<'a>>,
    pub output: OutputType<'a>,
}

impl Constructor<'_> {
    pub fn as_normalized(&self) -> String {
        NormalizedConstructor(self).to_string()
    }

    pub fn compute_tl_id(&self) -> u32 {
        use std::fmt::Write;

        if let Some(id) = self.tl_id {
            return id;
        }

        struct Checksum<'a>(crc::Digest<'a, u32>);

        impl Write for Checksum<'_> {
            #[inline(always)]
            fn write_str(&mut self, s: &str) -> std::fmt::Result {
                self.0.update(s.as_bytes());
                Ok(())
            }
        }

        let mut checksum = Checksum(CRC.digest());
        write!(&mut checksum, "{}", NormalizedConstructor(self)).unwrap();

        checksum.0.finalize()
    }
}

struct NormalizedConstructor<'c, 'a>(&'c Constructor<'a>);

impl std::fmt::Display for NormalizedConstructor<'_, '_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.variant.fmt(f)?;

        for ty in &self.0.type_parameters {
            f.write_fmt(format_args!(" {{{ty}}}"))?;
        }

        for field in &self.0.fields {
            f.write_fmt(format_args!(" {field}"))?
        }

        f.write_fmt(format_args!(" = {}", self.0.output))
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Field<'a> {
    pub name: Option<&'a str>,
    pub ty: Type<'a>,
}

impl std::fmt::Display for Field<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(name) = self.name {
            f.write_fmt(format_args!("{name}:{}", self.ty))
        } else {
            self.ty.fmt(f)
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OutputType<'a> {
    pub ty: &'a str,
    pub ty_param: Option<Box<Type<'a>>>,
}

impl std::fmt::Display for OutputType<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.ty_param {
            None => f.write_str(self.ty),
            Some(ty_param) => f.write_fmt(format_args!("{} {ty_param}", self.ty)),
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Type<'a> {
    /// Simple `#` (equivalent to `u32`)
    Int,
    /// Full path to identifier
    Named { ty: &'a str },
    /// Full path to identifier with type param
    Generic {
        ty: &'a str,
        ty_param: Box<Type<'a>>,
    },
    /// Conditional type
    Flagged {
        flags_field: &'a str,
        bit: u8,
        ty: Box<Type<'a>>,
    },
    /// Tuple type which can be multiplied
    Repeated {
        multiplicity: Option<u32>,
        ty: Vec<Field<'a>>,
    },
}

impl std::fmt::Display for Type<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Int => f.write_str("#"),

            Self::Named { ty } => f.write_str(ty),

            Self::Generic { ty, ty_param } => f.write_fmt(format_args!("{ty} {ty_param}")),

            Self::Flagged {
                flags_field,
                bit,
                ty,
            } => f.write_fmt(format_args!("{flags_field}.{bit}?{ty}")),

            Self::Repeated { multiplicity, ty } => {
                if let Some(multiplicity) = multiplicity {
                    f.write_fmt(format_args!("{multiplicity} * [ "))?
                } else {
                    f.write_str("[ ")?
                }

                for ty in ty {
                    f.write_fmt(format_args!("{ty} "))?
                }

                f.write_str("]")
            }
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum ValidationError {
    #[error("unknown field: {0}")]
    UnknownField(String),
    #[error("unknown type: {0}")]
    UnknownType(String),
}

static CRC: Crc<u32> = Crc::<u32>::new(&CRC_32_ISO_HDLC);