pest icon indicating copy to clipboard operation
pest copied to clipboard

Feature Request: *Optionally* add some more information while reporting errors

Open oxcrow opened this issue 10 months ago • 3 comments

Hi,

I absolutely love how pest can report errors in a simple manner.

I have fixed many bugs within my grammar because of pest's error reports.

However sometimes some extra information is needed, and simple error reports are not enough.

Such as this error report.

Error:   --> 22:5
   |
22 |     // Error: We used a comment // not a /// documentation
   |     ^---
   |
   = expected value

This is generated because I used // instead of /// within this code I'm trying to parse.

/// Documentation of this struct
struct MyStruct {
    /// Documentation of these values
    x,y,z* float
    // Error: We used a comment // not a /// documentation
    a,b,c* usize
}

As shown in this pest grammar a value was expected thus the error was thrown.

value = { doc* ~ ids  ~ scope? ~ ":"? ~ type ~ ","? }

However the issue is, the error happened because we used a // comment instead of a /// documentation comment.

The error was thrown because pest could not find the doc* node within the code.

Thus although the error reported by pest is correct, it can also unintentionally mislead anyone who uses the compiler., because the user most likely does not know that pest expected a doc* node but the code contains something else.

Thus to me at least it would be better if pest also printed out on which node of the grammar pest failed to parse.

Maybe the error could be reported as this:

Error:   --> 22:5
   |
22 |     // Error: We used a comment // not a /// documentation
   |     ^---
   |
   = expected value
      expected doc* but received comment
      where,  value = { doc* ~ ids  ~ scope? ~ ":"? ~ type ~ ","? }
      failed to parse at doc* node of value = { doc* ~ ids  ~ scope? ~ ":"? ~ type ~ ","? }
                                                  ^--- Error 

I do not know if this will be difficult to implement. However if it is implemented it will allow us to debug pest grammar in an easier way.

Thank you.

oxcrow avatar Feb 16 '25 12:02 oxcrow

@oxcrow did you try this function: https://docs.rs/pest/latest/pest/fn.set_error_detail.html pest::set_error_detail(true); ?

tomtau avatar Feb 16 '25 13:02 tomtau

I did,

/// Parse the source cdode and create an AST
pub fn parse_string(source: &str) -> Result<()> {
	pest::set_error_detail(true);
	let parse_tree = IxParser::parse(Rule::file, source)?;
	Ok(())
}

We get the same error as before.

oxcrow avatar Feb 16 '25 14:02 oxcrow

@oxcrow you can check if the returned ErrorVariant contains that extra info, but it's just not printed

tomtau avatar Feb 16 '25 23:02 tomtau