-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (43 loc) · 772 Bytes
/
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
package main
import (
"bufio"
"fmt"
"io"
"io/fs"
"os"
)
func handleError(err error) {
if err == nil {
return
}
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
func main() {
var r io.Reader
var query string = "."
if len(os.Args) == 1 {
stat, err := os.Stdin.Stat()
if (stat.Mode()&fs.ModeNamedPipe) == 0 || err != nil {
fmt.Println("Usage:\n\tgojson [FILE] [PATH]")
os.Exit(0)
}
r = bufio.NewReader(os.Stdin)
} else if len(os.Args) >= 2 {
f, err := os.Open(os.Args[1])
if err != nil {
handleError(err)
}
r = bufio.NewReader(f)
if len(os.Args) >= 3 {
query = os.Args[2]
}
}
l := NewLexer(r)
p := NewParser(l)
err := p.Parse()
handleError(err)
out, err := p.Get(query)
handleError(err)
fmt.Println(out)
}