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

Missing Position when Capture errors #179

Open
mhr3 opened this issue Sep 10, 2021 · 1 comment
Open

Missing Position when Capture errors #179

mhr3 opened this issue Sep 10, 2021 · 1 comment
Assignees
Labels

Comments

@mhr3
Copy link

mhr3 commented Sep 10, 2021

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)

@alecthomas
Copy link
Owner

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.

@alecthomas alecthomas self-assigned this Sep 13, 2021
@alecthomas alecthomas added the bug label Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants