Skip to content

nested_in() + repeated() produce incorrect error spans. #577

@ktsimpso

Description

@ktsimpso

Using 1.0.0.alpha6 using a combination of nested_in + repeated() if there is a parse error in the inner parser, the error is reported with a span relative to the start of the chunk that the outer parser parsed rather than the start of the entire input. (This makes sense from the inner parser's perspective as it does not know about the other input). Though my expectation would be for the combinator to reframe error spans to be consistent with the outer span context.

Example:

let blank_line = text::newline::<&str, extra::Err<Rich<'_, char>>>()
    .repeated()
    .exactly(2)
    .ignored();
let chunker = any()
    .and_is(blank_line.not())
    .repeated()
    .at_least(1)
    .to_slice()
    .then_ignore(blank_line.or(end()));
let numbers = text::int(10)
    .separated_by(text::newline())
    .allow_trailing()
    .collect::<Vec<_>>();
let parser = numbers.nested_in(chunker).repeated().collect::<Vec<_>>();

let result = parser.parse("1\n2\n\n3\n4\n\n").into_result();
assert_eq!(result, Ok(vec![vec!["1", "2"], vec!["3", "4"]]));

let result = parser.parse("1\na\n\n3\n4\n\n").into_result();
assert_eq!(
    result.map_err(|e| *e.first().unwrap().span()),
    Err(SimpleSpan::new(2usize, 3))
);

let result = parser.parse("1\n2\n\n3\na\n\n").into_result();
assert_eq!(
    result.map_err(|e| *e.first().unwrap().span()),
    Err(SimpleSpan::new(7usize, 8))
); // fails with left: Err(2..3)

Alternatively is there a nice way for me to manually map the span to the correct outer context?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions