/
ast.go
157 lines (125 loc) · 3.15 KB
/
ast.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
package main
import (
"strings"
)
type token struct {
value string
typ string
}
type param struct {
name string
baseType string
isArray bool // describe on 'param'
arrayFlag string // e.g. `[3]`/`[][]`
isPointer bool // describe on 'base type', e.g. `[]*int` also consider as pointer
}
const (
TokenType_VariableName = "variable_name"
TokenType_VariableType = "variable_type"
)
const (
ScanStatus_ReadingName = iota
ScanStatus_ReadingType
)
func parseInputParams(str string) []*param {
return parseTokens(scan(str))
}
// scan FSM rule: e.g. ` a [3]string , b []*int ` -> {a, name}, {[3]string, type}, {b, name}, {[]*int, type}
// ' ':
// 0: index < 0: skip
// index >= 0: [index, i) is 'name', status -> 1, index = -1
// 1: index < 0: skip
// index >= 0: [index, i) is 'type', status -> 0, index = -1
// ',':
// 0: skip
// 1: index >= 0: [index, i) is 'type', status -> 0, index = -1 (simulate 'str' is valid, index will >= 0 here)
// other chars:
// 0: index < 0: index = i
// index >= 0: skip
// 1: index < 0: index = i
// index >= 0: skip
func scan(str string) []*token {
str += " " // for last 'type', todo: if any program skill to handle this situation?
tokenList := make([]*token, 0, strings.Count(str, ",")+1)
status := ScanStatus_ReadingName
index := -1
for i := 0; i < len(str); i++ {
switch str[i] {
case ' ':
if index < 0 {
continue
}
tokenItem := &token{
value: str[index:i],
}
switch status {
case ScanStatus_ReadingName:
tokenItem.typ = TokenType_VariableName
status = ScanStatus_ReadingType
case ScanStatus_ReadingType:
tokenItem.typ = TokenType_VariableType
status = ScanStatus_ReadingName
}
tokenList = append(tokenList, tokenItem)
index = -1
case ',':
if status != ScanStatus_ReadingType || index < 0 {
continue
}
tokenList = append(tokenList, &token{
value: str[index:i],
typ: TokenType_VariableType,
})
status = ScanStatus_ReadingName
index = -1
default:
if index < 0 {
index = i
}
}
}
return tokenList
}
func parseTokens(tokens []*token) []*param {
if len(tokens)%2 != 0 {
return nil
}
paramList := make([]*param, 0, len(tokens)/2)
for i := 0; i+1 < len(tokens); i += 2 {
paramItem := ¶m{
name: tokens[i].value,
}
variableType := tokens[i+1].value
if strings.HasPrefix(variableType, "[") {
index := strings.LastIndex(variableType, "]")
paramItem.isArray = true
paramItem.arrayFlag = variableType[:index+1]
variableType = variableType[index+1:]
}
if strings.HasPrefix(variableType, "*") {
paramItem.isPointer = true
variableType = variableType[1:]
}
paramItem.baseType = variableType
paramList = append(paramList, paramItem)
}
return paramList
}
// string return the param like '[name] [type]'
func (p *param) string() string {
res := []byte(p.name + " ")
res = append(res, p.typ()...)
return string(res)
}
// typ return the type of param
func (p *param) typ() string {
var res []byte
if p.isArray {
res = append(res, p.arrayFlag...)
}
if p.isPointer {
res = append(res, '*')
}
res = append(res, p.baseType...)
return string(res)
}