alecthomas / participle

A parser library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help me to understand, please

alexeyco opened this issue · comments

import (
	"log"

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

type Type struct {
	IsOptional bool        `("Optional<" @@ ">")?`
	Definition *Definition `@@`
}

type Definition struct {
	Bool bool `@("Bool")?`
}

func main() {
	lex := lexer.MustSimple([]lexer.SimpleRule{
		{`Keyword`, `(?i)\b(Optional|Bool)\b`},
		{`Ident`, `[a-zA-Z_][a-zA-Z0-9_]*`},
		{`Punct`, `[:<,>]`},
		{`String`, `'[^']*'|"[^"]*"`},
		{"whitespace", `\s+`},
	})

	parser := participle.MustBuild(
		&Type{},
		participle.Lexer(lex),
		participle.Unquote("String"),
		participle.CaseInsensitive("Keyword"),
	)

	t := Type{}
	if err := parser.ParseString("", "Optional<Bool>", &t); err != nil {
		log.Fatalln(err)
	}
}

I'm trying to understand how to parse thees strings:

  • Bool
  • Optional<Bool>

But I got a panic "IsOptional: bool should be a struct or should implement the Parseable interface". What am I doing wrong?