participle icon indicating copy to clipboard operation
participle copied to clipboard

i've read all docs, but still fail at simplest example.

Open glebarez opened this issue 4 years ago • 4 comments

Here's is my tiny-testing code

main.go

package main

import (
	"github.com/alecthomas/participle/v2"
	"github.com/alecthomas/participle/v2/lexer"
)

// A custom lexer
var CondLexer = lexer.MustSimple([]lexer.Rule{
	{`Int`, `\d+`, nil}, // int
	{"whitespace", `\s+`, nil},
})



type ComponentSpec struct {
	Idx int `"[" @Int "]"`
}



var parser = participle.MustBuild(&ComponentSpec{},
	participle.Lexer(CondLexer),
)

main_test.go

package main

import (
	"testing"

	"github.com/alecthomas/repr"
	"github.com/stretchr/testify/require"
)

func Test_ComponentSpec(t *testing.T) {
	spec := &ComponentSpec{}
	err := parser.ParseString("test", "[1]", spec)
	require.NoError(t, err)
	repr.Println(spec)
}

What I get:

$ go test 
--- FAIL: Test_ComponentSpec (0.00s)
    main_test.go:13: 
                Error Trace:    main_test.go:13
                Error:          Received unexpected error:
                                test:1:1: invalid input text "[1]"
                Test:           Test_ComponentSpec
FAIL
exit status 1
FAIL    github.com/alecthomas/participle/v2/_examples/own       0.198s

What is wrong here?

glebarez avatar Sep 10 '21 10:09 glebarez

I appreciate that you have read the docs 🙂

To answer your question, your lexer must produce a token for every value captured by your grammar. In this case your lexer does not produce "[" or "]".

alecthomas avatar Sep 10 '21 10:09 alecthomas

@alecthomas thank you!

PS: Great Project! waiting for parser-codegen feature 🙂

glebarez avatar Sep 10 '21 10:09 glebarez

I've been working on this a bit. I have codegen for lexing included already, and the start of parser codegen in a branch.

alecthomas avatar Sep 12 '21 23:09 alecthomas

I appreciate that you have read the docs slightly_smiling_face

To answer your question, your lexer must produce a token for every value captured by your grammar. In this case your lexer does not produce "[" or "]".

I ran into the exact same issue trying to parse a C header file. Thanks for the pointer!

pcting avatar Nov 01 '21 17:11 pcting