-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher_num.go
198 lines (137 loc) · 2.59 KB
/
matcher_num.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
package ulexer
import (
"strings"
"unicode"
)
// 数字, 无符号整形
func UInteger() Matcher {
return (*uIntegerMatcher)(nil)
}
type uIntegerMatcher struct{}
func (*uIntegerMatcher) TokenType() string {
return "UInteger"
}
func (self *uIntegerMatcher) Read(lex *Lexer) (tk *Token) {
var count int
for {
c := lex.Peek(count)
switch {
case unicode.IsDigit(c):
default:
goto ExitFor
}
count++
}
ExitFor:
if count == 0 {
return nil
}
tk = lex.NewToken(count, self)
lex.Consume(count)
return
}
// 有符号整数=正数, 0, 负数
func Integer() Matcher {
return (*integerMatcher)(nil)
}
type integerMatcher struct{}
func (*integerMatcher) TokenType() string {
return "Integer"
}
func (self *integerMatcher) Read(lex *Lexer) (tk *Token) {
var count int
for {
c := lex.Peek(count)
switch {
case unicode.IsDigit(c):
case count == 0 && c == '-':
default:
goto ExitFor
}
count++
}
ExitFor:
if count == 0 {
return nil
}
tk = lex.NewToken(count, self)
lex.Consume(count)
return
}
// 匹配正数, 负数, 浮点数
func Numeral() Matcher {
return (*numeralMatcher)(nil)
}
type numeralMatcher struct{}
func (*numeralMatcher) TokenType() string {
return "Numeral"
}
func (self *numeralMatcher) Read(lex *Lexer) (tk *Token) {
var count int
var isFloat bool
for {
c := lex.Peek(count)
switch {
case unicode.IsDigit(c):
case c == '-' && count == 0:
case c == '.' && count > 0 && !isFloat:
isFloat = true
case isFloat && (c == 'e' || c == '+'):
default:
goto ExitFor
}
count++
}
ExitFor:
if count == 0 {
return nil
}
tk = lex.NewToken(count, self)
lex.Consume(count)
return
}
// 匹配十六进制, 带0x前缀的, 自动去掉, 使用Token.Numeral(32, 16, false)获取数值
func Hex() Matcher {
return (*hexMatcher)(nil)
}
type hexMatcher struct{}
func (*hexMatcher) TokenType() string {
return "Hex"
}
func isHexNumber(c rune) bool {
if unicode.IsNumber(c) {
return true
}
switch c {
case 'a', 'b', 'c', 'd', 'e', 'f':
return true
case 'A', 'B', 'C', 'D', 'E', 'F':
return true
}
return false
}
func (self *hexMatcher) Read(lex *Lexer) (tk *Token) {
var count int
for {
c := lex.Peek(count)
switch {
case isHexNumber(c):
case c == 'x' && count == 1:
default:
goto ExitFor
}
count++
}
ExitFor:
if count == 0 {
return nil
}
str := lex.ToLiteral(count)
// 纯十六进制parse时, 在输出字符前加0x, 方便直接转换为16进制
if strings.HasPrefix(str, "0x") {
str = str[2:]
}
tk = lex.NewTokenLiteral(count, self, str)
lex.Consume(count)
return
}