From 7460a14bc135f7724fc442e1560fcf10dc58deb1 Mon Sep 17 00:00:00 2001 From: brevalferrari Date: Fri, 6 Jun 2025 01:21:03 +0200 Subject: [PATCH] better parsing error reporting --- src/cli/main.rs | 14 +++++++++++++- src/parser.rs | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cli/main.rs b/src/cli/main.rs index f77ad39..f1c2e57 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -110,7 +110,19 @@ fn parse_and_compile(opts: &PlayOpts) -> anyhow::Result> { info!("parsing tokens"); let tokens = parser .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::() + ) + }) .context("Failed to parse input")?; info!("found {} tokens", tokens.as_ref().len()); if tokens.as_ref().is_empty() { diff --git a/src/parser.rs b/src/parser.rs index b7ef122..82f40b1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,7 @@ use nom::{ complete::{char, space1, usize}, streaming::one_of, }, - combinator::{opt, value}, + combinator::{all_consuming, opt, value}, error::{Error, ParseError}, multi::many0, sequence::{delimited, preceded}, @@ -81,7 +81,7 @@ where many0(value((), char('#').and(take_till(|c| c == '\n'))).or(value((), space1))), ) }; - many0(delimited( + all_consuming(many0(delimited( space_or_comment(), alt(( Silence::parser().map(into_box), @@ -93,7 +93,7 @@ where Slope::parser(parser).map(into_box), )), space_or_comment(), - )) + ))) .map(Into::into) }