/
main.go
117 lines (89 loc) · 2.27 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"github.com/nevet/parser/component"
"github.com/nevet/parser/utils"
)
const (
Filename = "test-file1.go"
StateIdle = 0 // starting state, waiting for package name
StateInPackage = 1 // after package name, before EOF, outside any funcs
)
var (
noFormatCheck, noErrorCheck *bool
boundMapper = map[string]string{
"(": ")",
"{": "}",
}
)
func parseFlag() {
noFormatCheck = flag.Bool("nofmt", false, "Skip code format checking")
noErrorCheck = flag.Bool("noerr", false, "Skip code error checking")
flag.Parse()
}
func parseImport(buf *bufio.Scanner, lineTokens []string) (packageNames []string) {
if lineTokens[0] != "(" {
packageNames = append(packageNames, lineTokens[0])
} else {
for buf.Scan(); buf.Text() != ")"; buf.Scan() {
lineTokens = getLineToken(buf.Text())
if len(lineTokens[0]) > 0 {
packageNames = append(packageNames, lineTokens[0])
}
}
}
return
}
func parseFunc(buf *bufio.Scanner, lineTokens []string) (function *Function) {
}
func parseVar(buf *bufio.Scanner, lineTokens []string) *Definition {
definition := make(Definition)
return &definition
}
func Parse(buf *bufio.Scanner) (*GoFile, error) {
state := StateIdle
goFile := &GoFile{}
for tokens, hasNext := utils.GetNextLine(buf); hasNext; tokens, hasNext := utils.GetNextLine(buf) {
switch state {
case StateIdle:
goFile.Package = tokens[1]
state = StateInPackage
case StateInPackage:
switch tokens[0] {
case "import":
goFile.Import = append(goFile.Import, parseImport(buf, tokens[1:])...)
// case "const":
// append(goFile.Const, parseOneOrMoreLine(buf, tokens))
case "var":
goFile.Var.Append(parseVar(buf, tokens[1:]))
case "func":
goFile.Functions = append(goFile.Functions, parseFunc(buf, tokens[1:]))
// case "type":
// append(goFile.Type, parseOneOrMoreLine(buf, tokens))
// }
}
}
}
return goFile, nil
}
func main() {
parseFlag()
// fmt, lint and vet the file before parsing
if err := utils.PreCheck(Filename, *noFormatCheck, *noErrorCheck); err != nil {
log.Fatal(err)
}
buf, err := utils.GetScanner(Filename)
if err != nil {
log.Fatal(err)
}
parsedGoFile, err := Parse(buf)
if err != nil {
log.Fatal(err)
}
parsedGoFile.Dump()
}
//