/
baseparser.go
227 lines (199 loc) · 5.24 KB
/
baseparser.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package api
import (
"fmt"
"net/http"
"regexp"
"strings"
"unicode"
"github.com/zeromicro/antlr"
)
const (
versionRegex = `(?m)"v[1-9][0-9]*"`
importValueRegex = `(?m)"(/?[a-zA-Z0-9_#-])+\.api"`
tagRegex = `(?m)\x60[a-z]+:".+"\x60`
)
var (
holder = struct{}{}
kind = map[string]struct{}{
"bool": holder,
"int": holder,
"int8": holder,
"int16": holder,
"int32": holder,
"int64": holder,
"uint": holder,
"uint8": holder,
"uint16": holder,
"uint32": holder,
"uint64": holder,
"uintptr": holder,
"float32": holder,
"float64": holder,
"complex64": holder,
"complex128": holder,
"string": holder,
"byte": holder,
"rune": holder,
}
)
func match(p *ApiParserParser, text string) {
v := getCurrentTokenText(p)
if v != text {
notifyErrorListeners(p, expecting(text, v))
}
}
func checkVersion(p *ApiParserParser) {
v := getCurrentTokenText(p)
if !matchRegex(v, versionRegex) {
notifyErrorListeners(p, mismatched("version", v))
}
}
func checkImportValue(p *ApiParserParser) {
v := getCurrentTokenText(p)
if !matchRegex(v, importValueRegex) {
notifyErrorListeners(p, mismatched("import value", v))
}
}
func checkKeyValue(p *ApiParserParser) {
v := getCurrentTokenText(p)
if !strings.HasPrefix(v, ":") {
notifyErrorListeners(p, mismatched(":", v))
}
v = strings.TrimPrefix(v, ":")
v = strings.TrimFunc(v, func(r rune) bool {
return unicode.IsSpace(r)
})
setCurrentTokenText(p, v)
}
func checkHTTPMethod(p *ApiParserParser) {
method := getCurrentTokenText(p)
uppler := strings.ToUpper(method)
switch uppler {
case http.MethodPost, http.MethodGet, http.MethodHead,
http.MethodPut, http.MethodPatch, http.MethodDelete,
http.MethodConnect, http.MethodOptions, http.MethodTrace:
if method != strings.ToLower(method) {
notifyErrorListeners(p, expecting("http method lower case", method))
}
default:
notifyErrorListeners(p, expecting("http method", method))
}
}
func checkKeyword(p *ApiParserParser) {
v := getCurrentTokenText(p)
if IsGolangKeyWord(v) {
notifyErrorListeners(p, fmt.Sprintf("expecting ID, found golang keyword: '%s'", v))
}
}
func checkKey(p *ApiParserParser) {
v := getCurrentTokenText(p)
if IsGolangKeyWord(v) {
notifyErrorListeners(p, fmt.Sprintf("expecting ID, found golang keyword: '%s'", v))
}
if _, ok := kind[v]; !ok {
notifyErrorListeners(p, fmt.Sprintf("expecting golang basic type, found : '%s'", v))
}
}
// IsBasicType returns true if the input argument is basic golang type
func IsBasicType(text string) bool {
_, ok := kind[text]
return ok
}
// IsGolangKeyWord returns true if input argument is golang keyword, but it will be ignored which in excepts
func IsGolangKeyWord(text string, excepts ...string) bool {
for _, each := range excepts {
if text == each {
return false
}
}
switch text {
case "var", "const", "package", "import", "func", "return",
"defer", "go", "select", "interface", "struct", "break", "case",
"continue", "for", "fallthrough", "else", "if", "switch", "goto",
"default", "chan", "type", "map", "range":
return true
default:
return false
}
}
func isNormal(p *ApiParserParser) bool {
ct := p.GetTokenStream().(*antlr.CommonTokenStream)
line := p.GetCurrentToken().GetLine()
tokens := ct.GetAllTokens()
var list []string
for _, token := range tokens {
if token.GetLine() == line {
text := token.GetText()
if strings.HasPrefix(text, "//") {
continue
}
if strings.HasPrefix(text, "/*") {
continue
}
if text == "<EOF>" {
continue
}
if strings.TrimSpace(text) == "" {
continue
}
list = append(list, text)
}
}
if len(list) == 1 {
t := strings.TrimPrefix(list[0], "*")
if IsGolangKeyWord(t) {
notifyErrorListeners(p, fmt.Sprintf("expecting ID, found golang keyword: '%s'", t))
}
}
if len(list) > 1 {
if list[0] == "*" {
t := strings.TrimPrefix(list[1], "*")
if IsGolangKeyWord(t) {
notifyErrorListeners(p, fmt.Sprintf("expecting ID, found golang keyword: '%s'", t))
}
return false
}
}
return len(list) > 1
}
// MatchTag returns a Boolean value, which returns true if it does matched, otherwise returns fase
func MatchTag(v string) bool {
return matchRegex(v, tagRegex)
}
func isInterface(p *ApiParserParser) {
v := getCurrentTokenText(p)
if IsGolangKeyWord(v) {
notifyErrorListeners(p, fmt.Sprintf("expecting ID, found golang keyword: '%s'", v))
}
}
func getCurrentTokenText(p *ApiParserParser) string {
token := p.GetCurrentToken()
if token == nil {
return ""
}
return token.GetText()
}
func setCurrentTokenText(p *ApiParserParser, text string) {
token := p.GetCurrentToken()
if token == nil {
return
}
token.SetText(text)
}
func notifyErrorListeners(p *ApiParserParser, msg string) {
p.NotifyErrorListeners(msg, nil, nil)
}
func matchRegex(text, str string) bool {
re := regexp.MustCompile(str)
v := re.FindString(text)
text = strings.TrimFunc(text, func(r rune) bool {
return unicode.IsSpace(r)
})
return v == text
}
func expecting(expecting, found string) string {
return fmt.Sprintf(`expecting '%s', found input '%s'`, expecting, found)
}
func mismatched(expecting, found string) string {
return fmt.Sprintf(`mismatched '%s', found input '%s'`, expecting, found)
}