-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
213 lines (195 loc) · 5.6 KB
/
lexer.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
package formula_engine
import (
"fmt"
"strings"
)
// lexer 词法分析器
type lexer struct {
FStr string
Idx int
CurrentChar uint8 // 当前的字符
}
func newLexer(fStr string) *lexer {
l := &lexer{
FStr: fStr,
Idx: -1,
CurrentChar: 0,
}
l.advance()
return l
}
// MakeTokens 获取tokens
func (l *lexer) MakeTokens() ([]*token, error) {
tokens := make([]*token, 0)
for l.CurrentChar != 0 {
switch {
case InSlice([]uint8{' ', '\t'}, l.CurrentChar):
l.advance()
case IsDigit(l.CurrentChar):
token, err := l.makeNumber()
if err != nil {
return nil, err // todo: 错误处理
}
tokens = append(tokens, token)
case l.CurrentChar == '+':
tokens = append(tokens, l.makeCharacter(TTPlus))
case l.CurrentChar == '-':
tokens = append(tokens, l.makeCharacter(TTMinus))
case l.CurrentChar == '*':
tokens = append(tokens, l.makeCharacter(TTMul))
case l.CurrentChar == '/':
tokens = append(tokens, l.makeCharacter(TTDiv))
case l.CurrentChar == '^':
tokens = append(tokens, l.makeCharacter(TTPow))
case l.CurrentChar == '(':
tokens = append(tokens, l.makeCharacter(TTLparen))
case l.CurrentChar == ')':
tokens = append(tokens, l.makeCharacter(TTRparen))
case l.CurrentChar == '&':
tokens = append(tokens, l.makeCharacter(TTAnd))
case l.CurrentChar == '|':
tokens = append(tokens, l.makeCharacter(TTOr))
case l.CurrentChar == '!':
tokens = append(tokens, l.makeNot())
case l.CurrentChar == '=':
tokens = append(tokens, l.makeCharacter(TTEq))
case l.CurrentChar == ',':
tokens = append(tokens, l.makeCharacter(TTComma))
case l.CurrentChar == '>':
tokens = append(tokens, l.makeCompare(TTGt))
case l.CurrentChar == '<':
tokens = append(tokens, l.makeCompare(TTLt))
case l.CurrentChar == '{':
token, err := l.makeIdentifier()
if err != nil {
return nil, err
}
tokens = append(tokens, token)
case IsAlpha(l.CurrentChar):
token, err := l.makeFunction()
if err != nil {
return nil, err
}
tokens = append(tokens, token)
default:
// 没有匹配到,非法字符错误
return nil, l.makeErr(illegalCharErrMsg, fmt.Sprintf("UnExpected character '%c'", l.CurrentChar))
}
}
tokens = append(tokens, newToken(TTEof, TTEof, l.Idx, l.Idx))
return tokens, nil
}
// makeCharacter 处理字符,通用组装token方法
func (l *lexer) makeCharacter(type_ TT) *token {
token := newToken(type_, string(l.CurrentChar), l.Idx, l.Idx)
l.advance()
return token
}
// makeNumber 处理数字:整数、小数
func (l *lexer) makeNumber() (*token, error) {
var numBuilder strings.Builder
dotCount := 0
start := l.Idx
//for InSlice(append(Digits, '.'), l.CurrentChar) {
for IsDigit(l.CurrentChar) || l.CurrentChar == '.' {
if l.CurrentChar == '.' {
if dotCount == 1 {
return nil, l.makeErr(illegalCharErrMsg, "UnExpected character '.', Only one '.' is allowed")
}
dotCount += 1
numBuilder.WriteByte('.')
} else {
numBuilder.WriteByte(l.CurrentChar)
}
l.advance()
}
str := numBuilder.String()
// 判断是否有错误
if !IsDigit(str[len(str)-1]) {
// 如果数字最后一位不是 0~9 ,即有可能是 .
return nil, l.makeErr(illegalCharErrMsg, fmt.Sprintf("UnExpected character '%c', expected '0'~'9'", str[len(str)-1]))
}
return newToken(TTNum, numBuilder.String(), start, l.Idx-1), nil
}
// makeNot 处理非 ! 或者不等于 !=
func (l *lexer) makeNot() *token {
var str strings.Builder
begin := l.Idx
str.WriteByte(l.CurrentChar)
l.advance()
// 判断是否是 !=
if l.CurrentChar == '=' {
str.WriteByte(l.CurrentChar)
l.advance()
return newToken(TTNeq, str.String(), begin, l.Idx-1)
}
return newToken(TTNot, str.String(), begin, l.Idx-1)
}
// makeCompare 处理大于号或者小于号 > >= < <=
func (l *lexer) makeCompare(type_ TT) *token {
var str strings.Builder
begin := l.Idx
str.WriteByte(l.CurrentChar)
l.advance()
// 判断是否是 >= 或者 <=
if l.CurrentChar == '=' {
str.WriteByte(l.CurrentChar)
if type_ == TTGt {
type_ = TTGte
} else {
type_ = TTLte
}
l.advance()
}
return newToken(type_, str.String(), begin, l.Idx-1)
}
// makeIdentifier 处理变量
func (l *lexer) makeIdentifier() (*token, error) {
var (
str strings.Builder
)
l.advance()
start := l.Idx
// 变量开头不是'字母'或者'_',报错
if !(IsAlpha(l.CurrentChar) || l.CurrentChar == '_') {
return nil, l.makeErr(illegalCharErrMsg, fmt.Sprintf("UnExpected Initial '%c', expected letter or '_'", l.CurrentChar))
}
// 字符是字母、数字或'_'
for IsAlpha(l.CurrentChar) || IsDigit(l.CurrentChar) || l.CurrentChar == '_' {
str.WriteByte(l.CurrentChar)
l.advance()
}
if l.CurrentChar != '}' {
return nil, l.makeErr(illegalCharErrMsg, fmt.Sprintf("UnExpected character '%c', expected '}' after an Identifier", l.CurrentChar))
}
l.advance()
return newToken(TTIdentifier, str.String(), start, l.Idx-2), nil
}
// makeFunction 处理函数
func (l *lexer) makeFunction() (*token, error) {
var strBuilder strings.Builder
start := l.Idx
for IsAlpha(l.CurrentChar) || l.CurrentChar == '.' {
strBuilder.WriteByte(l.CurrentChar)
l.advance()
}
str := strings.ToUpper(strBuilder.String())
_, ok := FuncMap[str]
if !ok {
return nil, l.makeErr(illegalCharErrMsg, fmt.Sprintf("UnKnow function name %s", str))
}
return newToken(TTFunction, str, start, l.Idx-1), nil
}
// advance 预读
func (l *lexer) advance() {
l.Idx += 1
if l.Idx < len(l.FStr) {
l.CurrentChar = l.FStr[l.Idx]
} else {
l.CurrentChar = 0
}
}
// makeErrWithIdx 组装错误
func (l *lexer) makeErr(errName, details string) error {
return makeStrErr(l.Idx, l.FStr, errName, details)
}