Skip to content

Commit 3592b31

Browse files
committed
add multiple error printing
1 parent 03b3596 commit 3592b31

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub extern crate simplicity;
2929
pub use simplicity::elements;
3030

3131
use crate::debug::DebugSymbols;
32-
use crate::error::WithFile;
33-
use crate::parse::ParseFromStr;
32+
use crate::error::{ErrorHandler, WithFile};
33+
use crate::parse::ParseFromStrWithErrors;
3434
pub use crate::types::ResolvedType;
3535
pub use crate::value::Value;
3636
pub use crate::witness::{Arguments, Parameters, WitnessTypes, WitnessValues};
@@ -52,12 +52,17 @@ impl TemplateProgram {
5252
/// The string is not a valid SimplicityHL program.
5353
pub fn new<Str: Into<Arc<str>>>(s: Str) -> Result<Self, String> {
5454
let file = s.into();
55-
let parse_program = parse::Program::parse_from_str(&file)?;
56-
let ast_program = ast::Program::analyze(&parse_program).with_file(Arc::clone(&file))?;
57-
Ok(Self {
58-
simfony: ast_program,
59-
file,
60-
})
55+
let mut error_handler = ErrorHandler::new(file.clone());
56+
let parse_program = parse::Program::parse_from_str_with_errors(&file, &mut error_handler);
57+
if let Some(program) = parse_program {
58+
let ast_program = ast::Program::analyze(&program).with_file(Arc::clone(&file))?;
59+
Ok(Self {
60+
simfony: ast_program,
61+
file,
62+
})
63+
} else {
64+
Err(error_handler)?
65+
}
6166
}
6267

6368
/// Access the parameters of the program.
@@ -265,6 +270,7 @@ pub trait ArbitraryOfType: Sized {
265270

266271
#[cfg(test)]
267272
pub(crate) mod tests {
273+
use crate::parse::ParseFromStr;
268274
use base64::display::Base64Display;
269275
use base64::engine::general_purpose::STANDARD;
270276
use simplicity::BitMachine;

src/main.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6969
let include_debug_symbols = matches.get_flag("debug");
7070
let output_json = matches.get_flag("json");
7171

72-
let compiled = CompiledProgram::new(prog_text, Arguments::default(), include_debug_symbols)?;
72+
let compiled =
73+
match CompiledProgram::new(prog_text, Arguments::default(), include_debug_symbols) {
74+
Ok(program) => program,
75+
Err(e) => {
76+
eprintln!("{}", e);
77+
std::process::exit(1);
78+
}
79+
};
7380

7481
#[cfg(feature = "serde")]
7582
let witness_opt = matches

0 commit comments

Comments
 (0)