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
pub extern crate num_bigint;
pub extern crate num_rational;
extern crate num_traits;
extern crate pest;
extern crate pest_derive;
mod ast;
use crate::parser::{DsdlParser, Rule};
use pest::Parser;
pub use crate::ast::types::*;
use pest::error::ErrorVariant;
pub use pest::Span;
mod parser {
use pest_derive::Parser;
#[derive(Parser)]
#[grammar = "dsdl.pest"]
pub struct DsdlParser;
}
#[derive(Debug, Clone)]
pub struct Error(Box<pest::error::Error<Rule>>);
pub fn parse(dsdl: &str) -> Result<Definition<'_>, Error> {
let parse_tree = DsdlParser::parse(Rule::definition, dsdl).map_err(|e| Error(Box::new(e)))?;
ast::parse_to_ast(parse_tree)
}
pub fn make_error<S>(message: S, span: Span<'_>) -> Error
where
S: Into<String>,
{
Error(Box::new(pest::error::Error::new_from_span(
ErrorVariant::CustomError {
message: message.into(),
},
span,
)))
}
mod error_impl {
use super::Error;
use std::fmt;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
}
}
}