geno 0.4.0

A cross-language schema compiler that generates type definitions and serialization code from a simple, declarative schema language.
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::{Location, ast, error::*};
use pest::{Parser as PestParser, iterators::Pair};
use pest_derive::Parser;
use std::{
    collections::HashSet,
    path::{self, Path, PathBuf},
};

// Put the Pest parser in a private module to suppress doc warnings
// See [Issue #326](https://github.com/pest-parser/pest/issues/326)
mod parser {
    use super::*;

    #[derive(Parser)]
    #[grammar = "geno.pest"]
    pub struct GenoParser;
}

use crate::ast::IntegerType;
use parser::{GenoParser, Rule};

fn remove_quotes(value: &str) -> &str {
    let mut chars = value.chars();

    chars.next(); // Consume the first character
    chars.next_back(); // Consume the last character
    chars.as_str() // Return the remaining slice
}

impl From<Pair<'_, Rule>> for ast::Ident {
    fn from(pair: Pair<'_, Rule>) -> Self {
        ast::Ident {
            name: pair.as_str().to_string(),
            location: Location::from(&pair.as_span()),
        }
    }
}

/// A Geno AST builder
pub struct GenoAstBuilder {
    file_path: PathBuf,
}

impl GenoAstBuilder {
    /// Create a new Geno AST builder from a file path.  A file path is required
    /// in order to give meaningful error messages.
    pub fn new(file_path: PathBuf) -> Result<Self, std::io::Error> {
        Ok(GenoAstBuilder {
            file_path: path::absolute(file_path.clone())?,
        })
    }

    /// Create a new Geno AST builder from a file path relative to the current file.
    pub fn from(&self, file_path: &Path) -> Self {
        let include_path = self
            .file_path
            .parent()
            .unwrap_or(Path::new("/"))
            .join(file_path);

        Self {
            file_path: include_path,
        }
    }

    /// Build and validate the AST
    pub fn build(
        &self,
        read_to_string: &impl Fn(&Path) -> std::io::Result<String>,
    ) -> Result<ast::Schema, GenoError> {
        let mut file_paths = HashSet::<PathBuf>::new();

        file_paths.insert(self.file_path.clone());

        let input = read_to_string(&self.file_path).map_err(|e| GenoError::Io {
            file_path: self.file_path.clone(),
            error: e,
        })?;

        let mut schema_pairs = match GenoParser::parse(Rule::_schema, &input) {
            Ok(pairs) => pairs,
            Err(err) => {
                return Err(GenoError::Parse {
                    content: err.line().to_string(),
                    file_path: self.file_path.clone(),
                    location: Location::from(err.line_col),
                });
            }
        };
        let metadata = self.build_meta_decl(schema_pairs.next().unwrap())?;
        let mut declarations = Vec::new();
        let mut nested_asts = Vec::new();

        while let Some(pair) = schema_pairs.next() {
            if pair.as_rule() == Rule::EOI {
                break;
            }

            let rule = pair.as_rule();
            match rule {
                Rule::enum_decl => declarations.push(self.build_enum_decl(pair)?),
                Rule::struct_decl => declarations.push(self.build_struct_decl(pair)?),
                Rule::include_stmt => {
                    let nested_file_path =
                        Path::new(remove_quotes(&pair.into_inner().next().unwrap().as_str()));
                    let builder = self.from(&nested_file_path);

                    if !file_paths.contains(&builder.file_path) {
                        file_paths.insert(builder.file_path.clone());
                        nested_asts.push(builder.build(read_to_string)?);
                    }
                }
                _ => {
                    unreachable!(); // Pest problem?
                }
            };
        }

        Ok(ast::Schema {
            metadata,
            declarations,
            nested_asts,
            file_path: self.file_path.clone(),
        })
    }

    fn build_meta_decl(
        &self,
        pair: Pair<'_, Rule>,
    ) -> Result<Vec<(ast::Ident, ast::MetadataValue)>, GenoError> {
        let mut inner_pairs = pair.into_inner();
        let inner_pair = inner_pairs.next().unwrap();
        let mut metadata: Vec<(ast::Ident, ast::MetadataValue)> = Vec::new();

        // Parse 'meta_data_entry' pairs
        for entry_pair in inner_pair.into_inner() {
            let mut inner_pairs = entry_pair.into_inner();
            let ident_pair = inner_pairs.next().unwrap();
            let ident = ast::Ident::from(ident_pair);
            let value_pair = inner_pairs.next().unwrap();
            let value = match value_pair.as_rule() {
                Rule::string_literal => {
                    ast::MetadataValue::String(remove_quotes(&value_pair.as_str()).to_string())
                }
                Rule::integer_literal => ast::MetadataValue::Integer(
                    self.build_integer_literal(IntegerType::I64, value_pair)?,
                ),
                _ => {
                    unreachable!(); // Pest problem?
                }
            };

            metadata.push((ident, value));
        }

        Ok(metadata)
    }

    fn build_integer_type(&self, pair: Pair<'_, Rule>) -> Result<ast::IntegerType, GenoError> {
        let s = pair.as_str();

        match s {
            "i8" => Ok(ast::IntegerType::I8),
            "u8" => Ok(ast::IntegerType::U8),
            "i16" => Ok(ast::IntegerType::I16),
            "u16" => Ok(ast::IntegerType::U16),
            "i32" => Ok(ast::IntegerType::I32),
            "u32" => Ok(ast::IntegerType::U32),
            "i64" => Ok(ast::IntegerType::I64),
            "u64" => Ok(ast::IntegerType::U64),
            _ => unreachable!(),
        }
    }

    fn build_integer_literal(
        &self,
        base_type: IntegerType,
        pair: Pair<'_, Rule>,
    ) -> Result<ast::IntegerValue, GenoError> {
        let s = pair.as_str();
        let radix = if s.starts_with("0b") {
            2
        } else if s.starts_with("0x") {
            16
        } else {
            10
        };
        let is_signed = matches!(
            base_type,
            IntegerType::I8 | IntegerType::I16 | IntegerType::I32 | IntegerType::I64
        );

        if is_signed && (radix == 16 || radix == 2) {
            return Err(GenoError::new_number_range(
                &pair.as_str(),
                &self.file_path,
                &Location::from(&pair.as_span()),
            ));
        }

        let digits = if radix == 2 || radix == 16 {
            &s[2..]
        } else {
            s
        };

        match base_type {
            IntegerType::U8 => {
                return Ok(ast::IntegerValue::U8(
                    u8::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::U16 => {
                return Ok(ast::IntegerValue::U16(
                    u16::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::U32 => {
                return Ok(ast::IntegerValue::U32(
                    u32::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::U64 => {
                return Ok(ast::IntegerValue::U64(
                    u64::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::I8 => {
                return Ok(ast::IntegerValue::I8(
                    i8::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::I16 => {
                return Ok(ast::IntegerValue::I16(
                    i16::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::I32 => {
                return Ok(ast::IntegerValue::I32(
                    i32::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
            IntegerType::I64 => {
                return Ok(ast::IntegerValue::I64(
                    i64::from_str_radix(digits, radix).map_err(|_| {
                        GenoError::new_number_range(
                            pair.as_str(),
                            &self.file_path,
                            &Location::from(&pair.as_span()),
                        )
                    })?,
                ));
            }
        };
    }

    fn build_enum_decl<'a>(
        &self,
        enum_decl_pair: Pair<'a, Rule>,
    ) -> Result<ast::Declaration, GenoError> {
        let mut inner_pairs = enum_decl_pair.into_inner();

        let ident_pair = inner_pairs.next().unwrap();
        let ident = ast::Ident::from(ident_pair);
        let mut next_pair = inner_pairs.next().unwrap();
        let base_type;

        if next_pair.as_rule() == Rule::integer_type {
            base_type = self.build_integer_type(next_pair)?;
            next_pair = inner_pairs.next().unwrap();
        } else {
            // No base type specified, default to i32
            base_type = ast::IntegerType::I32
        };

        // next_pair is now an 'enum_variant_list'
        let mut variants: Vec<(ast::Ident, ast::IntegerValue)> = Vec::new();

        for enum_variant_pair in next_pair.into_inner() {
            let mut variant_inner = enum_variant_pair.into_inner();
            let variant_ident_pair = variant_inner.next().unwrap();
            let variant_ident = ast::Ident::from(variant_ident_pair);
            let variant_value =
                self.build_integer_literal(base_type.clone(), variant_inner.next().unwrap())?;

            variants.push((variant_ident, variant_value));
        }

        Ok(ast::Declaration::Enum {
            ident,
            base_type,
            variants,
        })
    }

    fn build_struct_decl<'a>(
        &self,
        struct_decl_pair: Pair<'a, Rule>,
    ) -> Result<ast::Declaration, GenoError> {
        let mut inner_pairs = struct_decl_pair.into_inner();

        let ident_pair = inner_pairs.next().unwrap();
        let ident = ast::Ident::from(ident_pair);
        let next_pair = inner_pairs.next().unwrap();

        // next_pair is now a 'struct_field_list'
        let mut fields: Vec<(ast::Ident, ast::FieldType)> = Vec::new();

        for struct_field_pair in next_pair.into_inner() {
            let mut struct_field_inner = struct_field_pair.into_inner();
            let field_ident_pair = struct_field_inner.next().unwrap();
            let field_ident = ast::Ident::from(field_ident_pair);

            fields.push((
                field_ident,
                self.build_field_type(struct_field_inner.next().unwrap())?,
            ));
        }

        // Parse struct declaration
        Ok(ast::Declaration::Struct { ident, fields })
    }

    fn build_field_type<'a>(&self, pair: Pair<'a, Rule>) -> Result<ast::FieldType, GenoError> {
        let mut inner_pairs = pair.into_inner();
        let inner_pair = inner_pairs.next().unwrap();

        let nullable = if let Some(nullable_pair) = inner_pairs.peek() {
            if nullable_pair.as_rule() == Rule::nullable {
                true
            } else {
                false
            }
        } else {
            false
        };

        match inner_pair.as_rule() {
            Rule::array_type => {
                let mut inner_pairs = inner_pair.into_inner();
                let element_type_pair = inner_pairs.next().unwrap();
                let length = if let Some(length_pair) = inner_pairs.next() {
                    Some(length_pair.as_str().parse::<usize>().map_err(|_| {
                        GenoError::new_number_range(
                            length_pair.as_str(),
                            &self.file_path,
                            &Location::from(&length_pair.as_span()),
                        )
                    })?)
                } else {
                    None
                };
                Ok(ast::FieldType::Array(
                    Box::new(self.build_field_type(element_type_pair)?),
                    length,
                    nullable,
                ))
            }
            Rule::map_type => {
                let mut inner_pairs = inner_pair.into_inner();
                let key_type_pair = inner_pairs.next().unwrap();
                let value_type_pair = inner_pairs.next().unwrap();

                Ok(ast::FieldType::Map(
                    self.build_builtin_type(key_type_pair)?,
                    Box::new(self.build_field_type(value_type_pair)?),
                    nullable,
                ))
            }
            Rule::builtin_type => Ok(ast::FieldType::Builtin(
                self.build_builtin_type(inner_pair)?,
                nullable,
            )),
            Rule::identifier => Ok(ast::FieldType::UserDefined(
                ast::Ident::from(inner_pair),
                nullable,
            )),
            _ => unreachable!(),
        }
    }

    fn build_builtin_type(&self, pair: Pair<'_, Rule>) -> Result<ast::BuiltinType, GenoError> {
        let mut inner_pairs = pair.into_inner();
        let inner_pair = inner_pairs.next().unwrap();

        match inner_pair.as_rule() {
            Rule::integer_type => self
                .build_integer_type(inner_pair)
                .map(ast::BuiltinType::Integer),
            Rule::float_type => {
                let s = inner_pair.as_str();
                match s {
                    "f32" => Ok(ast::BuiltinType::Float(ast::FloatType::F32)),
                    "f64" => Ok(ast::BuiltinType::Float(ast::FloatType::F64)),
                    _ => unreachable!(),
                }
            }
            Rule::string_type => Ok(ast::BuiltinType::String),
            Rule::bool_type => Ok(ast::BuiltinType::Bool),
            _ => unreachable!(),
        }
    }
}