Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More information in errors #290

Closed
ricochet1k opened this issue Dec 19, 2022 · 5 comments
Closed

More information in errors #290

ricochet1k opened this issue Dec 19, 2022 · 5 comments

Comments

@ricochet1k
Copy link

I would really like the ability to introspect more into the parser's state when it fails in order to generate better error messages.

For example, if I have a String grammar that looks something like

type String struct {
	Fragments []Fragment `parser:"\"\\\"\" @@* \"\\\"\""` // because '\"' doesn't work :/
}

if I leave off the final " the error I get is unexpected token "<EOF>" (expected "\"") and there appears to be no way of looking at a partial parse state to find where the first " was. I'd love if the parser could somehow return a stack of incomplete productions that could be inspected from the returned error. Or even better, let a production include a Error participle.Error field, which if not-nil signifies that the enclosing production is incomplete somehow, and then it would be returned from .Parse instead of an empty value.

@alecthomas
Copy link
Owner

When constructing a parser, Participle will return the stack of types leading up to the failed structure. It doesn't currently narrow it down to the field though.

eg. https://go.dev/play/p/zvJv_w4YfJx gives

panic: Fragments: <input>:1:11: literal not terminated

I think in this case it's just missing the inner struct in the trace.

@ricochet1k
Copy link
Author

Sorry, that comment was misleading, and actually wasn't what I was talking about.

What I'm referring to is using a correct and working parser definition to parse a file where the contents are an incomplete string: https://go.dev/play/p/bVqcFvYlHkT

@alecthomas
Copy link
Owner

Ah I see. In that case you want the Trace() option: https://go.dev/play/p/rI4Iz1mdWIn

@alecthomas
Copy link
Owner

Also, in the case of partial failure, the returned structure will be populated up to the point of the last successful match. I've found this useful. This is covered in the docs BTW.

@alecthomas
Copy link
Owner

Here's an example of what I mean: https://go.dev/play/p/MB73BT2KR8D

&main.String{
  Pos: Position{Filename: "", Offset: 0, Line: 1, Column: 1},
  Fragments: []main.TextFragment{
    {
      Pos: Position{Filename: "", Offset: 1, Line: 1, Column: 2},
      Token: Token@1:2{-4, "asdf"},
    },
  },
}
1:6: unexpected token "<EOF>" (expected "\"")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants