Skip to content

Commit 8d49e05

Browse files
committed
add -json arg to output AST in json
1 parent 47c0128 commit 8d49e05

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cmd/vimlparser/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"io"
@@ -11,14 +12,15 @@ import (
1112
)
1213

1314
var neovim = flag.Bool("neovim", false, "use neovim parser")
15+
var usejson = flag.Bool("json", false, "output json")
1416

1517
func main() {
1618
flag.Parse()
1719

1820
opt := &vimlparser.ParseOption{Neovim: *neovim}
1921

2022
if len(flag.Args()) == 0 {
21-
if err := parseFile("", os.Stdin, os.Stdout, opt); err != nil {
23+
if err := parseFile("", os.Stdin, os.Stdout, opt, *usejson); err != nil {
2224
fmt.Fprintln(os.Stderr, err)
2325
os.Exit(1)
2426
}
@@ -37,7 +39,7 @@ func main() {
3739
exitCode = 1
3840
continue
3941
}
40-
if err := parseFile(f.Name(), f, os.Stdout, opt); err != nil {
42+
if err := parseFile(f.Name(), f, os.Stdout, opt, *usejson); err != nil {
4143
fmt.Fprintln(os.Stderr, err)
4244
exitCode = 1
4345
}
@@ -47,12 +49,17 @@ func main() {
4749
}
4850

4951
// filename is empty string if r is os.Stdin
50-
func parseFile(filename string, r io.ReadCloser, w io.Writer, opt *vimlparser.ParseOption) error {
52+
func parseFile(filename string, r io.ReadCloser, w io.Writer, opt *vimlparser.ParseOption, usejson bool) error {
5153
defer r.Close()
5254
node, err := vimlparser.ParseFile(r, filename, opt)
5355
if err != nil {
5456
return err
5557
}
58+
if usejson {
59+
e := json.NewEncoder(w)
60+
e.SetIndent("", " ")
61+
return e.Encode(node)
62+
}
5663
c := &compiler.Compiler{Config: compiler.Config{Indent: " "}}
5764
if err := c.Compile(w, node); err != nil {
5865
return err

0 commit comments

Comments
 (0)