-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (49 loc) · 861 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
52
53
54
55
56
57
58
package main
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
)
func main() {
filename, src, err := getInput()
if err != nil {
panic(err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, src, parser.Mode(0))
if err != nil {
fmt.Println(err)
}
ast.Print(fset, f)
}
func getInput() (string, []byte, error) {
filename := "input.go"
file := os.Stdin
if !hasStdin() {
if len(os.Args) != 2 {
return "", nil, errors.New("needs 1 argument: file to process")
}
filename = os.Args[1]
var err error
file, err = os.Open(filename)
if err != nil {
return "", nil, err
}
}
buf, err := io.ReadAll(file)
if err != nil {
return "", nil, err
}
return filename, buf, nil
}
func hasStdin() bool {
fi, err := os.Stdin.Stat()
if err != nil {
return false
}
return fi.Size() > 0
}