Skip to content

Commit a32891d

Browse files
committed
Fix to handle const types correctly
1 parent ce8df76 commit a32891d

File tree

7 files changed

+237
-285
lines changed

7 files changed

+237
-285
lines changed

ast/ast.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,20 @@
3232
// (PosVar, NodeVar, NodeSliceVar, and BoolVar are derived by its struct definition.)
3333
package ast
3434

35-
// This file must contain only AST definitions.
36-
// We use the following go:generate directive for generating pos.go. Thus, all AST definitions must have pos and end lines.
37-
//go:generate go run ../tools/gen-ast-pos/main.go -infile ast.go -outfile pos.go
35+
// NOTE: ast.go and ast_*.go are used for automatic generation, so these files are conventional.
36+
37+
// NOTE: This file defines AST nodes and they are used for automatic generation,
38+
// so this file is conventional.
39+
//
40+
// Conventions:
41+
//
42+
// - Each node interface (except for Node) should have isXXX method (XXX must be a name of the interface itself).
43+
// - `isXXX` methods should be defined after the interface definition
44+
// and the receiver should be the non-pointer node struct type.
45+
// - Each node struct should have pos and end comments.
46+
// - Each node struct should have template lines in its doc comment.
47+
48+
//go:generate go run ../tools/gen-ast-pos/main.go -astfile ast.go -constfile ast_const.go -outfile pos.go
3849

3950
import (
4051
"github.com/cloudspannerecosystem/memefish/token"

ast/const.go ast/ast_const.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package ast
22

3+
// NOTE: This file defines constants used in AST nodes and they are used for automatic generation,
4+
// so this file is conventional.
5+
//
6+
// Convention:
7+
//
8+
// - Each const types should be defined as a string type.
9+
// - Each value is defined as a string literal.
10+
311
// AllOrDistinct represents ALL or DISTINCT in SELECT or set operations, etc.
412
// If it is optional, it may be an empty string, so handle it according to the context.
513
type AllOrDistinct string

ast/ast_test.go

-214
This file was deleted.

tools/astcatalog/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
)
1010

1111
var (
12-
infile = flag.String("infile", "", "input filename")
12+
astfile = flag.String("astfile", "ast/ast.go", "path to ast/ast.go")
13+
constfile = flag.String("constfile", "ast/ast_const.go", "path to ast/ast_const.go")
1314
)
1415

1516
func main() {
1617
flag.Parse()
1718

18-
catalog, err := astcatalog.Load(*infile)
19+
catalog, err := astcatalog.Load(*astfile, *constfile)
1920
if err != nil {
2021
log.Fatalf("failed to load: %v", err)
2122
}

tools/gen-ast-pos/main.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ var (
3939
)
4040

4141
var (
42-
infile = flag.String("infile", "", "input filename")
43-
outfile = flag.String("outfile", "", "output filename (if it is not specified, the result is printed to stdout.)")
42+
astfile = flag.String("astfile", "ast/ast.go", "path to ast/ast.go")
43+
constfile = flag.String("constfile", "ast/ast_const.go", "path to ast/ast_const.go")
44+
outfile = flag.String("outfile", "", "output filename (if it is not specified, the result is printed to stdout.)")
4445
)
4546

4647
func main() {
@@ -51,41 +52,41 @@ func main() {
5152

5253
flag.Parse()
5354

54-
catalog, err := astcatalog.Load(*infile)
55+
catalog, err := astcatalog.Load(*astfile, *constfile)
5556
if err != nil {
5657
log.Fatal(err)
5758
}
5859

59-
nodes := make([]*astcatalog.NodeDef, 0, len(catalog))
60-
for _, node := range catalog {
61-
nodes = append(nodes, node)
60+
structs := make([]*astcatalog.NodeStructDef, 0, len(catalog.Structs))
61+
for _, structDef := range catalog.Structs {
62+
structs = append(structs, structDef)
6263
}
63-
sort.Slice(nodes, func(i, j int) bool {
64-
return nodes[i].SourcePos < nodes[j].SourcePos
64+
sort.Slice(structs, func(i, j int) bool {
65+
return structs[i].SourcePos < structs[j].SourcePos
6566
})
6667

6768
var buffer bytes.Buffer
6869
buffer.WriteString(prologue)
6970

70-
for _, node := range nodes {
71-
x := string(unicode.ToLower(rune(node.Name[0])))
71+
for _, structDef := range structs {
72+
x := string(unicode.ToLower(rune(structDef.Name[0])))
7273

73-
posExpr, err := poslang.Parse(node.Pos)
74+
posExpr, err := poslang.Parse(structDef.Pos)
7475
if err != nil {
7576
log.Fatalf("error on parsing pos: %v", err)
7677
}
7778

78-
endExpr, err := poslang.Parse(node.End)
79+
endExpr, err := poslang.Parse(structDef.End)
7980
if err != nil {
8081
log.Fatalf("error on parsing pos: %v", err)
8182
}
8283

8384
fmt.Fprintln(&buffer)
84-
fmt.Fprintf(&buffer, "func (%s *%s) Pos() token.Pos {\n", x, node.Name)
85+
fmt.Fprintf(&buffer, "func (%s *%s) Pos() token.Pos {\n", x, structDef.Name)
8586
fmt.Fprintf(&buffer, "\treturn %s\n", posExpr.PosExprToGo(x))
8687
fmt.Fprintf(&buffer, "}\n")
8788
fmt.Fprintln(&buffer)
88-
fmt.Fprintf(&buffer, "func (%s *%s) End() token.Pos {\n", x, node.Name)
89+
fmt.Fprintf(&buffer, "func (%s *%s) End() token.Pos {\n", x, structDef.Name)
8990
fmt.Fprintf(&buffer, "\treturn %s\n", endExpr.PosExprToGo(x))
9091
fmt.Fprintf(&buffer, "}\n")
9192
}

0 commit comments

Comments
 (0)