// Copyright (c) 2026, Daniel Martí // See LICENSE for licensing information //go:build ignore // gen_token_parse emits tokens_parse.go with an [encoding.TextUnmarshaler] // implementation per exported operator type declared in tokens.go. package main import ( "bytes" "go/ast" "go/format" "go/parser" "go/token" "log" "os" "strings" "text/template" ) func main() { fset := token.NewFileSet() file, err := parser.ParseFile(fset, "tokens.go", nil, parser.ParseComments) if err != nil { log.Fatal(err) } operatorTypes := map[string]bool{} for _, decl := range file.Decls { gd, ok := decl.(*ast.GenDecl) if !ok || gd.Tok != token.TYPE { continue } for _, spec := range gd.Specs { ts := spec.(*ast.TypeSpec) id, ok := ts.Type.(*ast.Ident) if ok && id.Name == "token" && ts.Name.IsExported() { operatorTypes[ts.Name.Name] = true } } } type entry struct { Name string Str string } type group struct { Name string Entries []entry } var groups []group for _, decl := range file.Decls { gd, ok := decl.(*ast.GenDecl) if !ok || gd.Tok != token.CONST { continue } first := gd.Specs[0].(*ast.ValueSpec) if len(first.Values) == 0 { continue } typeName := operatorExprType(first.Values[0]) if !operatorTypes[typeName] { continue } g := group{Name: typeName} for _, spec := range gd.Specs { vs := spec.(*ast.ValueSpec) if vs.Doc != nil && strings.Contains(vs.Doc.Text(), "Deprecated:") { continue } str := lineComment(vs) if str == "" { log.Fatalf("no line comment for %s", vs.Names[0].Name) } for _, name := range vs.Names { g.Entries = append(g.Entries, entry{Name: name.Name, Str: str}) } } if len(g.Entries) > 0 { groups = append(groups, g) } } var buf bytes.Buffer if err := outputTmpl.Execute(&buf, groups); err != nil { log.Fatal(err) } out, err := format.Source(buf.Bytes()) if err != nil { log.Fatalf("format: %v\n%s", err, buf.Bytes()) } if err := os.WriteFile("tokens_parse.go", out, 0o644); err != nil { log.Fatal(err) } } // operatorExprType returns the operator type name in expressions of the form // `Type(name)` or `Type(name) + iota`, or "" otherwise (e.g. plain aliases). func operatorExprType(expr ast.Expr) string { if be, ok := expr.(*ast.BinaryExpr); ok { return operatorExprType(be.X) } if ce, ok := expr.(*ast.CallExpr); ok { if id, ok := ce.Fun.(*ast.Ident); ok { return id.Name } } return "" } // lineComment returns the first whitespace-separated word of the trailing // line comment of vs, which is the operator's source form. Some comments // have trailing documentation like "// :# with [LangZsh]"; we want ":#". func lineComment(vs *ast.ValueSpec) string { if vs.Comment == nil { return "" } fields := strings.Fields(vs.Comment.Text()) if len(fields) == 0 { return "" } return fields[0] } var outputTmpl = template.Must(template.New("").Parse(` // Code generated by gen_token_parse.go; DO NOT EDIT. package syntax import "fmt" {{range .}} func (o *{{.Name}}) UnmarshalText(text []byte) error { switch string(text) { {{- range .Entries}} case {{printf "%q" .Str}}: *o = {{.Name}} {{- end}} default: return fmt.Errorf("invalid {{.Name}}: %q", text) } return nil } {{end}} `[1:]))