ast_parser.go
3.9 KB
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
package astexpr
import (
"errors"
"fmt"
"strings"
)
var precedence = map[string]int{
"+": 20, "-": 20, "*": 40, "/": 40, "%": 40, "^": 60,
"=": 10, ">": 10, "<": 10, "<=": 10, ">=": 10, "&": 40,
}
const (
// Identifier 标识符 e.g.函数名、表字段
Identifier = iota
// Literal 文字 e.g. 50
Literal
// Operator 计算操作 e.g. + - * /
Operator
// COMMA 命令, e.g. (
COMMA
// CompareOperator 比较操作 e.g. < = >
CompareOperator
// StringArgs 字符串参数
StringArgs
)
type Token struct {
// raw characters
Tok string
// type with Literal/Operator
Type,
Flag int
Offset int
}
type Parser struct {
Source string
SourceRunes []rune
ch rune
offset int
err error
}
func ParseToken(s string) ([]*Token, error) {
p := &Parser{
Source: s,
SourceRunes: []rune(s),
err: nil,
//ch: s[0],
}
p.ch = p.SourceRunes[0]
toks := p.parse()
if p.err != nil {
return nil, p.err
}
return toks, nil
}
func (p *Parser) parse() []*Token {
toks := make([]*Token, 0)
for {
tok := p.nextTok()
if tok == nil {
break
}
toks = append(toks, tok)
}
return toks
}
func (p *Parser) nextTok() *Token {
if p.offset >= len(p.SourceRunes) || p.err != nil {
return nil
}
var err error
for p.isWhitespace(p.ch) && err == nil {
err = p.nextCh()
}
start := p.offset
var tok *Token
switch p.ch {
case
'(',
')',
'+',
'-',
'*',
'/',
'^',
'%',
'&':
tok = &Token{
Tok: string(p.ch),
Type: Operator,
}
tok.Offset = start
err = p.nextCh()
case
'>',
'<',
'=':
if p.isCompareWordChar(p.ch) {
for p.isCompareWordChar(p.ch) && p.nextCh() == nil {
}
tok = &Token{
Tok: string(p.SourceRunes[start:p.offset]),
Type: CompareOperator,
}
tok.Offset = start
} else if p.ch != ' ' {
s := fmt.Sprintf("symbol error: unknown '%v', pos [%v:]\n%s",
string(p.ch),
start,
ErrPos(p.Source, start))
p.err = errors.New(s)
}
case
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9':
for p.isDigitNum(p.ch) && p.nextCh() == nil {
if (p.ch == '-' || p.ch == '+') && p.SourceRunes[p.offset-1] != 'e' {
break
}
}
tok = &Token{
Tok: strings.ReplaceAll(string(p.SourceRunes[start:p.offset]), "_", ""),
Type: Literal,
}
tok.Offset = start
case '"':
for (p.isDigitNum(p.ch) || p.isChar(p.ch) || p.isCompareWordChar(p.ch) || p.ch == '*') && p.nextCh() == nil {
if p.ch == '"' {
break
}
}
err = p.nextCh()
tok = &Token{
Tok: string(p.SourceRunes[start:p.offset]),
Type: StringArgs,
}
tok.Offset = start
case ',':
tok = &Token{
Tok: string(p.ch),
Type: COMMA,
}
tok.Offset = start
err = p.nextCh()
default:
if p.isChar(p.ch) {
for p.isWordChar(p.ch) && p.nextCh() == nil {
}
tok = &Token{
Tok: string(p.SourceRunes[start:p.offset]),
Type: Identifier,
}
tok.Offset = start
} else if p.ch != ' ' {
s := fmt.Sprintf("symbol error: unknown '%v', pos [%v:]\n%s",
string(p.ch),
start,
ErrPos(p.Source, start))
p.err = errors.New(s)
}
}
return tok
}
func (p *Parser) nextCh() error {
p.offset++
if p.offset < len(p.SourceRunes) {
p.ch = p.SourceRunes[p.offset]
return nil
}
return errors.New("EOF")
}
func (p *Parser) isWhitespace(c rune) bool {
return c == ' ' ||
c == '\t' ||
c == '\n' ||
c == '\v' ||
c == '\f' ||
c == '\r'
}
func (p *Parser) isDigitNum(c rune) bool {
return '0' <= c && c <= '9' || c == '.' || c == '_' || c == 'e' || c == '-' || c == '+'
}
func (p *Parser) isChar(c rune) bool {
return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '.' || c == '"' || isChineseCharacter(c) || c == '_' //|| p.isDigitNum(c)
//判断是汉字
}
func (p *Parser) isWordChar(c rune) bool {
return p.isChar(c) || '0' <= c && c <= '9'
}
func (p *Parser) isCompareWordChar(c rune) bool {
return c == '=' || c == '<' || c == '>'
}
func isChineseCharacter(c rune) bool {
return len([]byte(string(c))) > 2
}