Skip to content

Commit

Permalink
Fix to handle const types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Jan 7, 2025
1 parent ce8df76 commit a32891d
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 285 deletions.
17 changes: 14 additions & 3 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@
// (PosVar, NodeVar, NodeSliceVar, and BoolVar are derived by its struct definition.)
package ast

// This file must contain only AST definitions.
// We use the following go:generate directive for generating pos.go. Thus, all AST definitions must have pos and end lines.
//go:generate go run ../tools/gen-ast-pos/main.go -infile ast.go -outfile pos.go
// NOTE: ast.go and ast_*.go are used for automatic generation, so these files are conventional.

// NOTE: This file defines AST nodes and they are used for automatic generation,
// so this file is conventional.
//
// Conventions:
//
// - Each node interface (except for Node) should have isXXX method (XXX must be a name of the interface itself).
// - `isXXX` methods should be defined after the interface definition
// and the receiver should be the non-pointer node struct type.
// - Each node struct should have pos and end comments.
// - Each node struct should have template lines in its doc comment.

//go:generate go run ../tools/gen-ast-pos/main.go -astfile ast.go -constfile ast_const.go -outfile pos.go

import (
"github.com/cloudspannerecosystem/memefish/token"
Expand Down
8 changes: 8 additions & 0 deletions ast/const.go → ast/ast_const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ast

// NOTE: This file defines constants used in AST nodes and they are used for automatic generation,
// so this file is conventional.
//
// Convention:
//
// - Each const types should be defined as a string type.
// - Each value is defined as a string literal.

// AllOrDistinct represents ALL or DISTINCT in SELECT or set operations, etc.
// If it is optional, it may be an empty string, so handle it according to the context.
type AllOrDistinct string
Expand Down
214 changes: 0 additions & 214 deletions ast/ast_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions tools/astcatalog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

var (
infile = flag.String("infile", "", "input filename")
astfile = flag.String("astfile", "ast/ast.go", "path to ast/ast.go")
constfile = flag.String("constfile", "ast/ast_const.go", "path to ast/ast_const.go")
)

func main() {
flag.Parse()

catalog, err := astcatalog.Load(*infile)
catalog, err := astcatalog.Load(*astfile, *constfile)
if err != nil {
log.Fatalf("failed to load: %v", err)
}
Expand Down
29 changes: 15 additions & 14 deletions tools/gen-ast-pos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ var (
)

var (
infile = flag.String("infile", "", "input filename")
outfile = flag.String("outfile", "", "output filename (if it is not specified, the result is printed to stdout.)")
astfile = flag.String("astfile", "ast/ast.go", "path to ast/ast.go")
constfile = flag.String("constfile", "ast/ast_const.go", "path to ast/ast_const.go")
outfile = flag.String("outfile", "", "output filename (if it is not specified, the result is printed to stdout.)")
)

func main() {
Expand All @@ -51,41 +52,41 @@ func main() {

flag.Parse()

catalog, err := astcatalog.Load(*infile)
catalog, err := astcatalog.Load(*astfile, *constfile)
if err != nil {
log.Fatal(err)
}

nodes := make([]*astcatalog.NodeDef, 0, len(catalog))
for _, node := range catalog {
nodes = append(nodes, node)
structs := make([]*astcatalog.NodeStructDef, 0, len(catalog.Structs))
for _, structDef := range catalog.Structs {
structs = append(structs, structDef)
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].SourcePos < nodes[j].SourcePos
sort.Slice(structs, func(i, j int) bool {
return structs[i].SourcePos < structs[j].SourcePos
})

var buffer bytes.Buffer
buffer.WriteString(prologue)

for _, node := range nodes {
x := string(unicode.ToLower(rune(node.Name[0])))
for _, structDef := range structs {
x := string(unicode.ToLower(rune(structDef.Name[0])))

posExpr, err := poslang.Parse(node.Pos)
posExpr, err := poslang.Parse(structDef.Pos)
if err != nil {
log.Fatalf("error on parsing pos: %v", err)
}

endExpr, err := poslang.Parse(node.End)
endExpr, err := poslang.Parse(structDef.End)
if err != nil {
log.Fatalf("error on parsing pos: %v", err)
}

fmt.Fprintln(&buffer)
fmt.Fprintf(&buffer, "func (%s *%s) Pos() token.Pos {\n", x, node.Name)
fmt.Fprintf(&buffer, "func (%s *%s) Pos() token.Pos {\n", x, structDef.Name)
fmt.Fprintf(&buffer, "\treturn %s\n", posExpr.PosExprToGo(x))
fmt.Fprintf(&buffer, "}\n")
fmt.Fprintln(&buffer)
fmt.Fprintf(&buffer, "func (%s *%s) End() token.Pos {\n", x, node.Name)
fmt.Fprintf(&buffer, "func (%s *%s) End() token.Pos {\n", x, structDef.Name)
fmt.Fprintf(&buffer, "\treturn %s\n", endExpr.PosExprToGo(x))
fmt.Fprintf(&buffer, "}\n")
}
Expand Down
Loading

0 comments on commit a32891d

Please sign in to comment.