From 1257f0d55f578ae63462bb1697850816c6eba6c0 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 16 Feb 2022 17:41:32 +0100 Subject: [PATCH] parser binary: fail on syntax error --- parser/examples/parser.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/parser/examples/parser.rs b/parser/examples/parser.rs index 801646f27f..1a18f74dd8 100644 --- a/parser/examples/parser.rs +++ b/parser/examples/parser.rs @@ -15,25 +15,29 @@ // along with the Leo library. If not, see . use leo_ast::Ast; -use leo_errors::{emitter::Handler, Result}; +use leo_errors::emitter::Handler; use leo_span::symbol::create_session_if_not_set_then; use std::{env, fs, path::Path}; -fn to_leo_tree(filepath: &Path) -> Result { +fn to_leo_tree(filepath: &Path) -> Result { // Loads the Leo code as a string from the given file path. let program_filepath = filepath.to_path_buf(); - let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file"); + let code = fs::read_to_string(&program_filepath).expect("failed to open input file"); // Parses the Leo file constructing an ast which is then serialized. create_session_if_not_set_then(|_| { - let handler = Handler::default(); - let ast = leo_parser::parse_ast(&handler, filepath.to_str().unwrap(), &program_string)?; - Ok(Ast::to_json_string(&ast).expect("serialization failed")) + Handler::with(|h| { + let ast = leo_parser::parse_ast(&h, filepath.to_str().unwrap(), &code)?; + let json = Ast::to_json_string(&ast)?; + println!("{}", json); + Ok(json) + }) + .map_err(|b| b.to_string()) }) } -fn main() -> Result<()> { +fn main() -> Result<(), String> { // Parse the command-line arguments as strings. let cli_arguments = env::args().collect::>(); @@ -51,7 +55,6 @@ fn main() -> Result<()> { // Construct the serialized syntax tree. let serialized_leo_tree = to_leo_tree(input_filepath)?; - println!("{}", serialized_leo_tree); // Determine the output directory. let output_directory = match cli_arguments.len() == 3 {