Skip to content

Commit

Permalink
Merge pull request #24 from haya14busa/json-arg
Browse files Browse the repository at this point in the history
JSON arg
  • Loading branch information
haya14busa committed Dec 3, 2016
2 parents 47c0128 + 8d49e05 commit d1662cb
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/vimlparser/main.go
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
Expand All @@ -11,14 +12,15 @@ import (
)

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

func main() {
flag.Parse()

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

if len(flag.Args()) == 0 {
if err := parseFile("", os.Stdin, os.Stdout, opt); err != nil {
if err := parseFile("", os.Stdin, os.Stdout, opt, *usejson); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand All @@ -37,7 +39,7 @@ func main() {
exitCode = 1
continue
}
if err := parseFile(f.Name(), f, os.Stdout, opt); err != nil {
if err := parseFile(f.Name(), f, os.Stdout, opt, *usejson); err != nil {
fmt.Fprintln(os.Stderr, err)
exitCode = 1
}
Expand All @@ -47,12 +49,17 @@ func main() {
}

// filename is empty string if r is os.Stdin
func parseFile(filename string, r io.ReadCloser, w io.Writer, opt *vimlparser.ParseOption) error {
func parseFile(filename string, r io.ReadCloser, w io.Writer, opt *vimlparser.ParseOption, usejson bool) error {
defer r.Close()
node, err := vimlparser.ParseFile(r, filename, opt)
if err != nil {
return err
}
if usejson {
e := json.NewEncoder(w)
e.SetIndent("", " ")
return e.Encode(node)
}
c := &compiler.Compiler{Config: compiler.Config{Indent: " "}}
if err := c.Compile(w, node); err != nil {
return err
Expand Down

0 comments on commit d1662cb

Please sign in to comment.