forked from GaloisInc/goblin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/json"
"flag"
"github.com/ReconfigureIO/goblin"
"go/ast"
"go/parser"
"go/token"
"os"
)
// Assuming you build with `make`, this variable will be filled in automatically
// (leaning on -ldflags -X).
var version string = "unspecified"
func main() {
versionFlag := flag.Bool("v", false, "display goblin version")
builtinDumpFlag := flag.Bool("builtin-dump", false, "use go/ast to dump the file, not JSON")
panicFlag := flag.Bool("panic", false, "use panic() rather than JSON on error conditions")
fileFlag := flag.String("file", "", "file to parse")
stmtFlag := flag.String("stmt", "", "statement to parse")
exprFlag := flag.String("expr", "", "expression to parse")
flag.Parse()
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
if *panicFlag {
goblin.ShouldPanic = true
}
if *versionFlag {
println(version)
return
} else if *fileFlag != "" {
file, err := os.Open(*fileFlag)
if err != nil {
goblin.Perish(goblin.TOPLEVEL_POSITION, "path_error", err.Error())
}
info, err := file.Stat()
if err != nil {
goblin.Perish(goblin.TOPLEVEL_POSITION, "path_error", err.Error())
}
size := info.Size()
file.Close()
fset.AddFile(*fileFlag, -1, int(size))
f, err := parser.ParseFile(fset, *fileFlag, nil, parser.ParseComments)
if err != nil {
goblin.Perish(goblin.INVALID_POSITION, "positionless_syntax_error", err.Error())
}
if *builtinDumpFlag {
ast.Print(fset, f)
} else {
val, _ := goblin.DumpFile(f, fset)
os.Stdout.Write(val)
}
} else if *exprFlag != "" {
val, _ := json.Marshal(goblin.TestExpr(*exprFlag))
os.Stdout.Write(val)
} else if *stmtFlag != "" {
val := goblin.TestStmt(*stmtFlag)
os.Stdout.Write(val)
} else {
flag.PrintDefaults()
}
}