Skip to content

Commit

Permalink
Show parse error's line number and column number (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo authored Aug 21, 2023
1 parent f93adcb commit d0544fe
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use egglog::{CompilerPassStop, EGraph, SerializeConfig};
use egglog::{CompilerPassStop, EGraph, Error, SerializeConfig};
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;

Expand Down Expand Up @@ -91,10 +91,11 @@ fn main() {
});
let mut egraph = mk_egraph();
let already_enables = program_read.starts_with("(set-option enable_proofs 1)");
let program = if args.proofs && !already_enables {
format!("(set-option enable_proofs 1)\n{}", program_read)
let (program, program_offset) = if args.proofs && !already_enables {
let expr = "(set-option enable_proofs 1)\n";
(format!("{}{}", expr, program_read), expr.len())
} else {
program_read
(program_read, 0)
};

if args.desugar || args.resugar {
Expand All @@ -121,6 +122,40 @@ fn main() {
}
}
Err(err) => {
let err = match err {
Error::ParseError(err) => err
.map_location(|byte_offset| {
let byte_offset = byte_offset - program_offset;
let (line_num, sum_offset) = std::iter::once(0)
.chain(program.split_inclusive('\n').scan(
0,
|sum_offset, l| {
*sum_offset += l.len();

if *sum_offset > byte_offset {
None
} else {
Some(*sum_offset)
}
},
))
.enumerate()
.last()
// No panic because of the initial 0
.unwrap();
{
format!(
"{}:{}:{}",
input.display(),
line_num + 1,
// TODO: Show utf8 aware character count
byte_offset - sum_offset + 1
)
}
})
.to_string(),
err => err.to_string(),
};
log::error!("{}", err);
std::process::exit(1)
}
Expand Down

0 comments on commit d0544fe

Please sign in to comment.