Print parse errors in a somewhat nicer way

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,
	}
This commit is contained in:
jn 2023-02-01 23:23:03 +01:00
parent 15f8a12307
commit 2c932cef4b

View File

@ -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<AssembledInstruction> = 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<Rule>) -> AstNode {
fn parse(source: &str) -> Result<Vec<AstNode>, Error<Rule>> {
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() {