alecthomas / participle

A parser library for Go
MIT License
3.45k stars 186 forks source link

avoid returning errors when one-or-more groups have no matches #390

Closed gordallott closed 7 months ago

gordallott commented 7 months ago

We had an issue using participle when using a structure like the following

type Union struct {
    Float float64 `( @Float` 
    String string ` | @String+`
    Bool bool     ` | @Boolean )` 
}

The code works as intended, but when you use a @String+ as a part of your union types your error messages get a lot less helpful. Instead of getting errors like

1:1: unexpected token "102"

most of your errors turn into

1:1: sub-expression + must match at least once

This ends up this way because the group node returns an error when @Foo+ doesn't match the minimum quantity which then overrides any other useful error reporting and failthroughs

This small change makes it so that the error is only reported if there is at least one match in a group (and adds a test to enforce it)

alecthomas commented 7 months ago

Nice one, thanks!