leo_input/
lib.rs

1// Copyright (C) 2019-2021 Aleo Systems Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16#[macro_use]
17extern crate pest_derive;
18#[macro_use]
19extern crate thiserror;
20
21pub mod errors;
22pub use errors::*;
23
24mod ast;
25pub mod common;
26pub mod definitions;
27pub mod expressions;
28pub mod files;
29pub mod parameters;
30pub mod sections;
31pub mod tables;
32pub mod types;
33pub mod values;
34
35use from_pest::FromPest;
36use std::{fs, path::Path};
37
38pub struct LeoInputParser;
39
40impl LeoInputParser {
41    /// Reads in the given file path into a string.
42    pub fn load_file(file_path: &Path) -> Result<String, InputParserError> {
43        fs::read_to_string(file_path).map_err(|_| InputParserError::FileReadError(file_path.to_owned()))
44    }
45
46    /// Parses the input file and constructs a syntax tree.
47    pub fn parse_file(input_file: &str) -> Result<files::File, InputParserError> {
48        // Parse the file using leo-input.pest
49        let mut file = ast::parse(input_file)?;
50
51        // Build the abstract syntax tree
52        let syntax_tree = files::File::from_pest(&mut file).map_err(|_| InputParserError::SyntaxTreeError)?;
53
54        Ok(syntax_tree)
55    }
56}