mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
Introduces LeoParser, removes pest for leo-compiler
This commit is contained in:
parent
81407ac182
commit
85ccf1b423
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -514,12 +514,10 @@ dependencies = [
|
||||
name = "leo-compiler"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"from-pest",
|
||||
"hex",
|
||||
"leo-ast",
|
||||
"leo-types",
|
||||
"log",
|
||||
"pest",
|
||||
"rand",
|
||||
"sha2",
|
||||
"snarkos-algorithms",
|
||||
|
@ -1,2 +1,5 @@
|
||||
pub mod parser;
|
||||
pub use parser::*;
|
||||
|
||||
pub mod syntax;
|
||||
pub use syntax::*;
|
||||
|
22
ast/src/errors/parser.rs
Normal file
22
ast/src/errors/parser.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use crate::{ast::Rule, errors::SyntaxError};
|
||||
|
||||
use pest::error::Error;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ParserError {
|
||||
#[error("Cannot read from the provided file path - {:?}", _0)]
|
||||
FileReadError(PathBuf),
|
||||
|
||||
#[error("{}", _0)]
|
||||
SyntaxError(#[from] SyntaxError),
|
||||
|
||||
#[error("Unable to construct abstract syntax tree")]
|
||||
SyntaxTreeError,
|
||||
}
|
||||
|
||||
impl From<Error<Rule>> for ParserError {
|
||||
fn from(error: Error<Rule>) -> Self {
|
||||
ParserError::SyntaxError(SyntaxError::from(error))
|
||||
}
|
||||
}
|
@ -20,3 +20,29 @@ pub mod operations;
|
||||
pub mod statements;
|
||||
pub mod values;
|
||||
pub mod types;
|
||||
|
||||
use from_pest::FromPest;
|
||||
use std::{path::PathBuf, fs};
|
||||
|
||||
pub struct LeoParser;
|
||||
|
||||
impl LeoParser {
|
||||
/// Reads in the given file path into a string.
|
||||
pub fn load_file(file_path: &PathBuf) -> Result<String, ParserError> {
|
||||
Ok(fs::read_to_string(file_path).map_err(|_| ParserError::FileReadError(file_path.clone()))?)
|
||||
}
|
||||
|
||||
/// Parses the input file and constructs a syntax tree.
|
||||
pub fn parse_file<'a>(file_path: &'a PathBuf, input_file: &'a str) -> Result<files::File<'a>, ParserError> {
|
||||
// Parse the file using leo.pest
|
||||
let mut file = ast::parse(input_file).map_err(|error| {
|
||||
ParserError::from(error.with_path(file_path.to_str().unwrap()))
|
||||
})?;
|
||||
|
||||
// Build the abstract syntax tree
|
||||
let syntax_tree = files::File::from_pest(&mut file).map_err(|_| ParserError::SyntaxTreeError)?;
|
||||
log::debug!("{:#?}", syntax_tree);
|
||||
|
||||
Ok(syntax_tree)
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,8 @@ snarkos-gadgets = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "
|
||||
snarkos-models = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
snarkos-utilities = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
|
||||
from-pest = { version = "0.3.1" }
|
||||
hex = { version = "0.4.2" }
|
||||
log = { version = "0.4" }
|
||||
pest = { version = "2.0" }
|
||||
rand = { version = "0.7" }
|
||||
sha2 = { version = "0.8" }
|
||||
thiserror = { version = "1.0" }
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
errors::CompilerError,
|
||||
GroupType,
|
||||
};
|
||||
use leo_ast::{ast, files::File};
|
||||
use leo_ast::LeoParser;
|
||||
use leo_types::{InputValue, Program};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
@ -14,7 +14,6 @@ use snarkos_models::{
|
||||
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem, TestConstraintSystem},
|
||||
};
|
||||
|
||||
use from_pest::FromPest;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{fs, marker::PhantomData, path::PathBuf};
|
||||
|
||||
@ -76,36 +75,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
generate_test_constraints::<F, G>(cs, self.program)
|
||||
}
|
||||
|
||||
// pub fn compile(&self) -> Result<File, CompilerError> {
|
||||
// // Read in the main file as string
|
||||
// let unparsed_file = fs::read_to_string(&self.main_file_path).map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
||||
//
|
||||
// // Parse the file using leo.pest
|
||||
// let mut file = ast::parse(&unparsed_file).map_err(|_| CompilerError::FileParsingError)?;
|
||||
//
|
||||
// // Build the abstract syntax tree
|
||||
// let syntax_tree = File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
// log::debug!("{:#?}", syntax_tree);
|
||||
//
|
||||
// Ok(syntax_tree)
|
||||
// }
|
||||
|
||||
fn parse_program(&mut self) -> Result<(), CompilerError> {
|
||||
// Read in the main file as string
|
||||
let unparsed_file = fs::read_to_string(&self.main_file_path)
|
||||
.map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
||||
// Build the program syntax tree
|
||||
let file_path = &self.main_file_path;
|
||||
let input_file = &LeoParser::load_file(file_path)?;
|
||||
let syntax_tree = LeoParser::parse_file(file_path, input_file)?;
|
||||
|
||||
// Parse the file using leo.pest
|
||||
let mut file = ast::parse(&unparsed_file).map_err(|error| {
|
||||
CompilerError::from(error.with_path(&self.main_file_path.to_str().unwrap()))
|
||||
})?;
|
||||
|
||||
// Build the abstract syntax tree
|
||||
let syntax_tree =
|
||||
File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
log::debug!("{:#?}", syntax_tree);
|
||||
|
||||
// Build program from abstract syntax tree
|
||||
// Build program from syntax tree
|
||||
let package_name = self.package_name.clone();
|
||||
|
||||
self.program = Program::from(syntax_tree, package_name);
|
||||
|
@ -4,15 +4,14 @@ use crate::{
|
||||
new_scope,
|
||||
GroupType,
|
||||
};
|
||||
use leo_ast::{ast, files::File};
|
||||
use leo_ast::LeoParser;
|
||||
use leo_types::{Import, Program};
|
||||
|
||||
use from_pest::FromPest;
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
gadgets::r1cs::ConstraintSystem,
|
||||
};
|
||||
use std::{env::current_dir, fs};
|
||||
use std::env::current_dir;
|
||||
|
||||
impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
||||
pub fn enforce_import(
|
||||
@ -35,16 +34,12 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
|
||||
println!("Compiling import - {:?}", main_file_path);
|
||||
|
||||
// Resolve program file path
|
||||
let unparsed_file = fs::read_to_string(main_file_path.clone())
|
||||
.map_err(|_| ImportError::FileReadError(main_file_path))?;
|
||||
let mut file = ast::parse(&unparsed_file).map_err(|_| ImportError::FileParsingError)?;
|
||||
// Build the abstract syntax tree
|
||||
let file_path = &main_file_path;
|
||||
let input_file = &LeoParser::load_file(file_path)?;
|
||||
let syntax_tree = LeoParser::parse_file(file_path, input_file)?;
|
||||
|
||||
// generate ast from file
|
||||
let syntax_tree =
|
||||
File::from_pest(&mut file).map_err(|_| ImportError::SyntaxTreeError)?;
|
||||
|
||||
// generate aleo program from file
|
||||
// Generate aleo program from file
|
||||
let mut program = Program::from(syntax_tree, import.path_string.clone());
|
||||
|
||||
// Use same namespace as calling function for imported symbols
|
||||
|
@ -1,8 +1,7 @@
|
||||
use crate::errors::{FunctionError, ImportError};
|
||||
use leo_ast::{ast::Rule, SyntaxError};
|
||||
use leo_ast::{SyntaxError, ParserError};
|
||||
use leo_types::IntegerError;
|
||||
|
||||
use pest::error::Error;
|
||||
use std::{io, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -34,6 +33,9 @@ pub enum CompilerError {
|
||||
#[error("Main must be a function")]
|
||||
NoMainFunction,
|
||||
|
||||
#[error("{}", _0)]
|
||||
ParserError(#[from] ParserError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
SyntaxError(#[from] SyntaxError),
|
||||
|
||||
@ -43,9 +45,3 @@ pub enum CompilerError {
|
||||
#[error("writing: {}", _0)]
|
||||
Writing(io::Error),
|
||||
}
|
||||
|
||||
impl From<Error<Rule>> for CompilerError {
|
||||
fn from(error: Error<Rule>) -> Self {
|
||||
CompilerError::SyntaxError(SyntaxError::from(error))
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use leo_ast::ParserError;
|
||||
|
||||
use std::{io, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -11,6 +13,9 @@ pub enum ImportError {
|
||||
#[error("Cannot read from the provided file path - {:?}", _0)]
|
||||
FileReadError(PathBuf),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ParserError(#[from] ParserError),
|
||||
|
||||
#[error("Unable to construct abstract syntax tree")]
|
||||
SyntaxTreeError,
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ fn test_semicolon() {
|
||||
.unwrap();
|
||||
|
||||
match error {
|
||||
CompilerError::SyntaxError(_) => {}
|
||||
_ => panic!("wrong error"),
|
||||
CompilerError::ParserError(_) => {}
|
||||
_ => panic!("test_semicolon failed the wrong expected error, should be a ParserError"),
|
||||
}
|
||||
}
|
||||
|
@ -277,7 +277,6 @@ impl<'ast> From<Assignee<'ast>> for Expression {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'ast> From<BinaryExpression<'ast>> for Expression {
|
||||
fn from(expression: BinaryExpression<'ast>) -> Self {
|
||||
match expression.operation {
|
||||
|
Loading…
Reference in New Issue
Block a user