better parsing error reporting

This commit is contained in:
brevalferrari 2025-06-06 01:21:03 +02:00
parent f55a8c7e47
commit 7460a14bc1
2 changed files with 16 additions and 4 deletions

View file

@ -110,7 +110,19 @@ fn parse_and_compile(opts: &PlayOpts) -> anyhow::Result<Vec<f64>> {
info!("parsing tokens"); info!("parsing tokens");
let tokens = parser let tokens = parser
.parse_all(&input) .parse_all(&input)
.map_err(|e| anyhow!("{e}")) .map_err(|nom::error::Error { input, code }| {
anyhow!(
"{code:?} line {line} column {column} (at \"{at}\")",
code = code,
line = input.location_line(),
column = input.get_utf8_column(),
at = input
.chars()
.chain("...".chars())
.take(10)
.collect::<String>()
)
})
.context("Failed to parse input")?; .context("Failed to parse input")?;
info!("found {} tokens", tokens.as_ref().len()); info!("found {} tokens", tokens.as_ref().len());
if tokens.as_ref().is_empty() { if tokens.as_ref().is_empty() {

View file

@ -11,7 +11,7 @@ use nom::{
complete::{char, space1, usize}, complete::{char, space1, usize},
streaming::one_of, streaming::one_of,
}, },
combinator::{opt, value}, combinator::{all_consuming, opt, value},
error::{Error, ParseError}, error::{Error, ParseError},
multi::many0, multi::many0,
sequence::{delimited, preceded}, sequence::{delimited, preceded},
@ -81,7 +81,7 @@ where
many0(value((), char('#').and(take_till(|c| c == '\n'))).or(value((), space1))), many0(value((), char('#').and(take_till(|c| c == '\n'))).or(value((), space1))),
) )
}; };
many0(delimited( all_consuming(many0(delimited(
space_or_comment(), space_or_comment(),
alt(( alt((
Silence::parser().map(into_box), Silence::parser().map(into_box),
@ -93,7 +93,7 @@ where
Slope::parser(parser).map(into_box), Slope::parser(parser).map(into_box),
)), )),
space_or_comment(), space_or_comment(),
)) )))
.map(Into::into) .map(Into::into)
} }