-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
288 lines (268 loc) · 5.38 KB
/
parser.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package aml
import (
"fmt"
"path/filepath"
"strconv"
"github.com/Drelf2020/utils"
)
// 语法分析器
type Parser struct {
// 词法分析器
*Lexer
// 文件路径
path string
// 数组长度
length int64
// 暂存的 Sentence
sentence *Sentence
// 变量类型
Types map[string]*Sentence
// Api 字典
Output map[string]*Api
}
// 判断类型
func (p *Parser) IsType() (*Sentence, bool) {
switch p.token.Kind {
case NUM, STR, BOOL, AUTO:
return nil, true
case IDENTIFIER:
s, ok := p.Types[p.token.Value]
if ok {
return s, true
}
return nil, In(p.sentence.Args, p.token.Value)
}
return nil, false
}
// 匹配导入语句
func (p *Parser) MatchImport() (*Include, error) {
_, path := p.Done()
k, v := p.Done()
if k != IMPORT {
return nil, fmt.Errorf("导入格式 %v 错误", v)
}
kind := COMMA
types := make([]string, 0)
for kind == COMMA {
_, typ := p.Done()
kind, _ = p.Done()
types = append(types, typ)
}
return NewInclude(p.GetDir(), path, types), nil
}
// 匹配参数
func (p *Parser) MatchArgs() (args []string, kind int) {
kind, _ = p.Done()
if kind != LANGLE {
return
}
depth := 1
args = append(args, "")
for depth > 0 {
k, v := p.Done()
if depth == 1 && k == COMMA {
args = append(args, "")
} else {
if k == LANGLE {
depth++
} else if k == RANGLE {
depth--
}
if depth > 0 {
args[len(args)-1] += v
}
}
}
return
}
// 匹配定义语句
func (p *Parser) MatchType() error {
k, name := p.Done()
if k != IDENTIFIER {
return fmt.Errorf("%v 不是一个好的变量名", name)
}
var base int
var hint string
args, kind := p.MatchArgs()
if len(args) != 0 {
kind, _ = p.Done()
}
if kind == COLON {
_, hint = p.Done()
kind, _ = p.Done()
}
if kind == ASSIGNMENT {
base, _ = p.Done()
}
p.sentence = &Sentence{
"type", name, hint, "", args,
base, -1, nil,
0, nil, make([]*Sentence, 0), make(map[string]*Sentence),
}
p.sentence.SetOutput(nil)
p.Types[name] = p.sentence
return nil
}
// 匹配列表
func (p *Parser) MatchList() (err error) {
if len(p.sentence.List) == 0 {
k, v := p.Done()
if k == AUTO {
return fmt.Errorf("%v 不是一个好的类型", v)
}
p.MatchVar(v)
if p.length <= 0 {
p.sentence.Length = -1
} else {
p.sentence.Length = p.length
}
p.length = 0
} else {
p.Shift()
}
return nil
}
// 匹配类型数组长度
func (p *Parser) MatchLength() (err error) {
k, v := p.Done()
if k == NUMBER {
p.length, err = strconv.ParseInt(v, 10, 64)
k, v = p.Done()
}
if p.length <= 0 {
p.length = -1
}
if k != RBRACKET {
return fmt.Errorf("%v 不是合法的中括号", v)
}
return
}
// 匹配 Api
func (p *Parser) MatchApi(typ string) error {
_, name := p.Done()
var hint, value string
k, v := p.Done()
if k != COLON && k != ASSIGNMENT {
return fmt.Errorf("%v 不是一个好的 Api 格式", v)
}
if k == COLON {
_, hint = p.Done()
k, _ = p.Done()
}
if k == ASSIGNMENT {
_, value = p.Done()
}
var s *Sentence
p.sentence = s.Add(typ, name, hint, value, []string{}, p.Types, 0)
return nil
}
// 匹配变量
func (p *Parser) MatchVar(typ string) *Sentence {
var kind int
var args []string
var name, hint, value string
if tk, ok := p.IsType(); ok {
// 检查这个类型 typ 是否需要参数
if tk != nil && len(tk.Args) != 0 {
args, _ = p.MatchArgs()
}
} else {
typ, name = "auto", typ
}
// 父语句是列表 那就只读取 typ 和 hint
if p.sentence.base == LBRACKET {
k, _ := p.Done()
if k == COLON {
_, hint = p.Done()
p.Done()
}
return p.sentence.Add(typ, "", hint, "", args, p.Types, 0)
}
if name == "" {
kind, name = p.Done()
if kind != IDENTIFIER {
panic(fmt.Errorf("%v 不是一个好的名字", name))
}
}
kind, _ = p.Done()
if kind == COLON {
_, hint = p.Done()
kind, _ = p.Done()
}
if kind == ASSIGNMENT {
_, value = p.Done()
p.Done()
}
if p.length != 0 {
s := p.sentence.Add(typ, name, hint, value, args, p.Types, p.length)
p.length = 0
return s
}
return p.sentence.Add(typ, name, hint, value, args, p.Types, 0)
}
// 选择匹配
func (p *Parser) Match() {
switch p.token.Kind {
case FROM:
i, err := p.MatchImport()
utils.PanicErr(err)
utils.ForMap(
NewParser(i.path).Types,
func(s string, t *Sentence) { p.Types[s] = t },
func(s string, t *Sentence) bool { return i.Need(s) },
)
return
case TYPE:
err := p.MatchType()
utils.PanicErr(err)
case GET, POST:
err := p.MatchApi(p.token.Value)
utils.PanicErr(err)
case NUMBER:
p.length, _ = strconv.ParseInt(p.token.Value, 10, 64)
case RBRACKET:
err := p.MatchList()
utils.PanicErr(err)
p.sentence = p.sentence.parent
return
case RBRACE, RGROUP:
if p.sentence.IsApi() {
p.Output[p.sentence.Name] = NewApi(p.sentence)
}
p.sentence = p.sentence.parent
case LBRACKET:
err := p.MatchLength()
utils.PanicErr(err)
p.Done()
fallthrough
case NUM, STR, BOOL, AUTO, IDENTIFIER:
s := p.MatchVar(p.token.Value)
if s.IsOpen() {
p.sentence = s
}
return
}
p.Shift()
}
func (p *Parser) GetDir() string {
return filepath.Dir(p.path)
}
func (p *Parser) NewExt(ext string) string {
fullname := filepath.Base(p.path)
suffix := filepath.Ext(fullname)
return fullname[0:len(fullname)-len(suffix)] + ext
}
func NewParser(path string) *Parser {
p := Parser{
NewLexer(path),
path,
0,
new(Sentence),
make(map[string]*Sentence),
make(map[string]*Api),
}
for p.Next() {
p.Match()
}
return &p
}