participle
participle copied to clipboard
Missing Position when Capture errors
Running this code produces a ParseError (as expected), but the error doesn't have position information:
package main
import (
"fmt"
"time"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Expression struct {
Alias string `@Ident "="`
Timestamp *Timestamp `@Number`
}
type Timestamp int64
func (ts *Timestamp) Capture(values []string) error {
t, err := time.Parse("2006", values[0])
if err != nil {
return err
}
*ts = Timestamp(t.Unix())
return nil
}
var exprLexer = lexer.MustSimple([]lexer.Rule{
{"Whitespace", `\s+`, nil},
{"String", `"(\\"|[^"])*"`, nil},
{"Number", `[-+]?(\d*\.)?\d+`, nil},
{"Ident", `[a-zA-Z_]\w*`, nil},
{"Punct", `[-[!@#$%^&*()+_={}\|:;"'<,>.?/]|]`, nil},
})
func main() {
parser := participle.MustBuild(&Expression{}, participle.Lexer(exprLexer), participle.Unquote(), participle.Elide("Whitespace"))
expr := &Expression{}
err := parser.ParseString("", "foo = 03", expr)
if err != nil {
parseErr := err.(participle.Error)
fmt.Println("error at position", parseErr.Position(), parseErr)
return
}
fmt.Println("success:", expr.Alias, "=", *expr.Timestamp)
}
The output is:
error at position 0:0 Expression.Timestamp: parsing time "03" as "2006": cannot parse "03" as "2006"
(using github.com/alecthomas/participle/v2 v2.0.0-alpha7)
I did a bit of digging and this is definitely a bug. Unfortunately the fix is non-trivial so it might take a little while.