package uci import ( "os" "github.com/alecthomas/participle/v2" "github.com/alecthomas/participle/v2/lexer" "github.com/pkg/errors" ) var ( uciLexer = lexer.MustSimple([]lexer.SimpleRule{ {Name: `Ident`, Pattern: `[a-zA-Z][a-zA-Z\d_\-]*`}, {Name: `String`, Pattern: `'([^'])*'`}, {Name: "whitespace", Pattern: `\s+`}, }) parser = participle.MustBuild[UCI]( participle.Lexer(uciLexer), participle.Unquote("String"), ) ) type Parser struct{} func ParseFile(filename string) (*UCI, error) { file, err := os.Open(filename) if err != nil { return nil, errors.WithStack(err) } defer func() { if err := file.Close(); err != nil { panic(errors.WithStack(err)) } }() uci, err := parser.Parse(filename, file, participle.Trace(os.Stdout)) if err != nil { return nil, errors.WithStack(err) } return uci, nil } func Parse(data []byte) (*UCI, error) { uci, err := parser.ParseBytes("", data) if err != nil { return nil, errors.WithStack(err) } return uci, nil }