From 2c932cef4b0a59ce1e19f43f1184c62fcff09c43 Mon Sep 17 00:00:00 2001 From: jn Date: Wed, 1 Feb 2023 23:23:03 +0100 Subject: [PATCH] Print parse errors in a somewhat nicer way MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: fox32asm 0.3.0 (94dd0fe) Generating raw binary Parsing includes... Parsing file... thread 'main' panicked at 'parse was unsuccessful: Error { variant: ParsingError { positives: [EOI, origin, include_bin, data, constant, label_kind, size, instruction], negatives: [] }, location: Pos(4), line_col: Pos((1, 5)), path: None, line: "nop 42␊", continued_line: None }', src/main.rs:529:60 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace After: fox32asm 0.3.0 (2abba95) Generating raw binary Parsing includes... Parsing file... Error { variant: ParsingError { positives: [ EOI, origin, include_bin, data, constant, label_kind, size, instruction, ], negatives: [], }, location: Pos( 4, ), line_col: Pos( ( 1, 5, ), ), path: None, line: "nop 42␊", continued_line: None, } --- src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ee37e74..de293dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -351,13 +351,19 @@ fn main() { } println!("Parsing file..."); - let ast = parse(&input_file); + let ast = match parse(&input_file) { + Ok(x) => x, + Err(x) => { + print!("{:#?}\n", x); + std::process::exit(1); + }, + }; let mut instructions: Vec = Vec::new(); let mut current_address: u32 = 0; println!("Assembling..."); - for node in ast.unwrap() { + for node in ast { if let AstNode::LabelDefine {name, ..} = node { let mut address_table = LABEL_ADDRESSES.lock().unwrap(); if let Some(_) = address_table.get(&name) { @@ -525,7 +531,7 @@ fn include_binary_file(pair: pest::iterators::Pair) -> AstNode { fn parse(source: &str) -> Result, Error> { let mut ast = vec![]; - let pairs = Fox32Parser::parse(Rule::assembly, source).expect("parse was unsuccessful"); + let pairs = Fox32Parser::parse(Rule::assembly, source)?; for pair in pairs.peek().unwrap().into_inner() { match pair.as_rule() {