dasn1_notation/
lib.rs

1#[macro_use]
2extern crate log;
3
4mod codegen;
5mod parser;
6mod registry;
7mod semantics;
8
9use std::{fs, path::PathBuf};
10
11use self::{codegen::*, parser::Parser, semantics::*};
12
13pub type Result<T> = std::result::Result<T, failure::Error>;
14
15pub struct NotationCompiler {
16    path: PathBuf,
17    dependencies: Option<PathBuf>,
18}
19
20impl NotationCompiler {
21    pub fn new<I: Into<PathBuf>>(path: I) -> Self {
22        Self {
23            path: path.into(),
24            dependencies: None,
25        }
26    }
27
28    pub fn dependencies<I: Into<PathBuf>>(mut self, path: I) -> Self {
29        self.dependencies = Some(path.into());
30        self
31    }
32
33    pub fn build(self) -> Result<String> {
34        let source = fs::read_to_string(&self.path)?;
35        let ast = Parser::parse(&source)?;
36
37        let mut fixed_tree = SemanticChecker::new(ast);
38        fixed_tree.build()?;
39        let table = fixed_tree.table;
40
41        let output = Vec::new();
42
43        let codegen = CodeGenerator::<Vec<u8>, Rust>::new(table, output);
44        let output = codegen.generate()?;
45
46        Ok(String::from_utf8(output).unwrap())
47    }
48}