Skip to content

Commit be1f438

Browse files
committed
Implement --output in tools/parser
1 parent 221a831 commit be1f438

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

tools/parse/main.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
"unicode"
1212

1313
"github.com/MakeNowJust/heredoc/v2"
14+
"github.com/k0kubun/pp/v3"
15+
1416
"github.com/cloudspannerecosystem/memefish"
1517
"github.com/cloudspannerecosystem/memefish/ast"
1618
"github.com/cloudspannerecosystem/memefish/token"
1719
"github.com/cloudspannerecosystem/memefish/tools/util/poslang"
18-
"github.com/k0kubun/pp/v3"
1920
)
2021

2122
var usage = heredoc.Doc(`
@@ -29,6 +30,10 @@ var usage = heredoc.Doc(`
2930
Show the parse result of "SELECT 1 AS X".
3031
$ go run ./tools/parse/main.go -mode expr "(SELECT 1) + 2"
3132
Parse "(SELECT 1) + 2" on the expression mode.
33+
$ go run ./tools/parse/main.go -mode expr --output unparse "(SELECT 1) + 2"
34+
Parse "(SELECT 1) + 2" on the expression mode and show only unparsed SQL.
35+
$ go run ./tools/parse/main.go -mode expr --output ast "(SELECT 1) + 2"
36+
Parse "(SELECT 1) + 2" on the expression mode and show only AST.
3237
$ go run ./tools/parse/main.go -pos "Query.end" "SELECT 1 AS x"
3338
Evaluate the POS expression "Query.end" on "SELECT 1 AS x"
3439
$ go run ./tools/parse/main.go -pos "As.end" -dig "Query.Results.0" "SELECT 1 AS x"
@@ -42,6 +47,7 @@ var (
4247
logging = flag.Bool("logging", false, "enable log (default: false)")
4348
dig = flag.String("dig", "", "digging the result node before printing")
4449
pos = flag.String("pos", "", "POS expression")
50+
output = flag.String("output", "ast,unparse", "comma separated outputs: {ast|unparse}")
4551
)
4652

4753
func main() {
@@ -124,16 +130,22 @@ func main() {
124130
}
125131
}
126132

127-
fmt.Println("--- AST")
128-
pprinter := pp.New()
129-
pprinter.SetOmitEmpty(true)
130-
_, _ = pprinter.Println(node)
131-
fmt.Println()
132-
133-
fmt.Println("--- SQL")
134-
fmt.Println(node.SQL())
133+
for _, elem := range strings.Split(*output, ",") {
134+
switch elem {
135+
case "ast":
136+
fmt.Println("--- AST")
137+
pprinter := pp.New()
138+
pprinter.SetOmitEmpty(true)
139+
_, _ = pprinter.Println(node)
140+
case "unparse":
141+
fmt.Println("--- SQL")
142+
fmt.Println(node.SQL())
143+
}
144+
}
135145

136146
if *pos != "" {
147+
fmt.Println()
148+
137149
fmt.Println("--- POS")
138150

139151
expr, err := poslang.Parse(*pos)

0 commit comments

Comments
 (0)