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
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate pest_derive;
#[macro_use]
extern crate thiserror;
mod ast;
pub mod access;
pub mod annotations;
pub mod circuits;
pub mod common;
pub mod console;
pub mod definitions;
pub mod expressions;
pub mod files;
pub mod functions;
pub mod imports;
pub mod operations;
pub mod statements;
pub mod types;
pub mod values;
pub mod errors;
pub use errors::*;
pub(crate) mod span;
pub(crate) use span::*;
use from_pest::FromPest;
use std::{fs, path::PathBuf};
pub struct LeoAst<'ast> {
ast: files::File<'ast>,
}
impl<'ast> LeoAst<'ast> {
pub fn new(file_path: &'ast PathBuf, program_string: &'ast str) -> Result<Self, ParserError> {
let file = &mut ast::parse(&program_string)
.map_err(|error| ParserError::from(error.with_path(file_path.to_str().unwrap())))?;
let ast = files::File::<'ast>::from_pest(file).map_err(|_| ParserError::SyntaxTreeError)?;
tracing::debug!("{:#?}", ast);
Ok(Self { ast })
}
pub fn load_file(file_path: &'ast PathBuf) -> Result<String, ParserError> {
Ok(fs::read_to_string(file_path).map_err(|_| ParserError::FileReadError(file_path.clone()))?)
}
pub fn as_repr(&self) -> &files::File<'ast> {
&self.ast
}
pub fn to_json_string(&self) -> Result<String, ParserError> {
Ok(serde_json::to_string_pretty(&self.ast)?)
}
}