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

example not working? #388

Closed
Diliz opened this issue Feb 16, 2024 · 2 comments
Closed

example not working? #388

Diliz opened this issue Feb 16, 2024 · 2 comments

Comments

@Diliz
Copy link

Diliz commented Feb 16, 2024

Hello there!

Thanks for your work!

I'm currently trying to use participle to make a custom parser, I understood the basics (I think), I first tried to use the ini example and iterate other it (changing ini format a little and try to make a new format that looks like ini but is not the same, to see if I can parse it anyway).

I first had no @int defined at first, had the following error: panic: Number: unknown token type "Int"

I declared the int type in the inilexer, then used what was the in the test file, but no success, got the following error:
panic: duplicated key '@@*' in struct cmd.INI [recovered]

Do you have this issue as well? Am I missing something here?

package cmd

import (
	"path/filepath"

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

type INI struct {
	Properties []*Property `@@*`
	Sections   []*Section  `@@*`
}

type Section struct {
	Identifier string      `"["@Ident"]"`
	Properties []*Property `@@*`
}

type Property struct {
	Key   string `@Ident "="`
	Value Value  `@@`
}

type Value interface{ value() }

type String struct {
	String string `@String`
}

func (String) value() {}

type Number struct {
	// Number float64 `@Float | @Int`
	Number float64 `@Float`
}

func (Number) value() {}

func iniParser(fileLocationPath string) map[string]interface{} {
	iniLexer := lexer.MustSimple([]lexer.SimpleRule{
		{`Ident`, `[a-zA-Z][a-zA-Z_\d]*`},
		{`String`, `"(?:\\.|[^"])*"`},
		{`Float`, `\d+(?:\.\d+)?`},
		{`Punct`, `[][=]`},
		{"comment", `[#;][^\n]*`},
		{"whitespace", `\s+`},
	})
	parser := participle.MustBuild[INI](
		participle.Lexer(iniLexer),
		participle.Unquote("String"),
		participle.Union[Value](String{}, Number{}),
	)
	iniContent, err := parser.ParseString("", `
global = 1

[section]
value = "str"
`)
	if err != nil {
		log.Panicf("error: %s", err.Error())
	}
	log.Infof("error: %s", err.Error())
}
@alecthomas
Copy link
Owner

I can't quite decide if you're saying the INI example doesn't work, or your code doesn't work? The INI example does work so I assume it's the latter.

@alecthomas
Copy link
Owner

Your example code seems to work fine for me also: https://go.dev/play/p/DfcUaaEh976

If you can, paste a play.golang.org link with the reproduction and reopen.

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