-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtext.go
226 lines (204 loc) · 5.79 KB
/
text.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
package goP2
import (
"fmt"
"unicode"
)
// Chr 判断下一个字符是否与给定值相等
func Chr(val rune) P {
return func(state State) (interface{}, error) {
x, err := state.Next()
if err != nil {
return nil, err
}
if c, ok := x.(int32); ok {
if c == val {
return c, nil
}
return nil, state.Trap("Expect '%v' but '%v'", string([]rune{val}), string([]rune{c}))
}
return nil, state.Trap("Expect a rune '%s' but x is %v", string([]rune{val}), x)
}
}
// NChr 判断下一个字符是否与给定值不相等
func NChr(val rune) P {
return func(state State) (interface{}, error) {
x, err := state.Next()
if err != nil {
return nil, err
}
if c, ok := x.(int32); ok {
if c == val {
return nil, state.Trap("Expect not '%v' but '%v'", string([]rune{val}), string([]rune{c}))
}
return c, nil
}
return nil, state.Trap("Expect a rune '%s' but x is %v", string([]rune{val}), x)
}
}
// RuneOf 检查后续的字符是否是给定值中的某一个
func RuneOf(str string) P {
data := []rune(str)
return func(state State) (interface{}, error) {
x, err := state.Next()
if err != nil {
return nil, err
}
if c, ok := x.(int32); ok {
for _, r := range data {
if c == r {
return c, nil
}
}
return nil, state.Trap("Expect rune in '%s' but '%s'", str, string([]rune{c}))
}
return nil, state.Trap("Expect rune in '%s' but x=%v is %t", str, x, x)
}
}
// RuneNone 检查后续的字符是否不是给定值中的任一个
func RuneNone(str string) P {
data := []rune(str)
return func(state State) (interface{}, error) {
x, err := state.Next()
if err != nil {
return nil, err
}
if c, ok := x.(int32); ok {
for _, r := range data {
if c == r {
return nil, state.Trap("Expect rune none of '%s' but '%s'", str, string([]rune{c}))
}
}
return c, nil
}
return nil, state.Trap("Expect rune none of '%s' but x=%v is %t", str, x, x)
}
}
// Str 判断后续的字符串是否匹配给定的串
func Str(str string) P {
data := []rune(str)
return func(state State) (interface{}, error) {
for _, r := range data {
_, err := Chr(r).Parse(state)
if err != nil {
return nil, err
}
}
return str, nil
}
}
// RuneP 通过一个谓词参数,提供通用的 rune 算子生成判断
func RuneP(name string, pred func(r rune) bool) P {
return func(state State) (interface{}, error) {
x, err := state.Next()
if err != nil {
return nil, err
}
if c, ok := x.(int32); ok {
r := rune(c)
if pred(r) {
return c, nil
}
return nil, state.Trap("Expect %s but '%v'", name, string([]rune{r}))
}
return nil, state.Trap("Expect %s but x=%v is %t", name, x, x)
}
}
// Space 构造一个空格校验算子
func Space(state State) (interface{}, error) {
return RuneP("space", unicode.IsSpace)(state)
}
// Whitespace 构造一个空白校验算子
func Whitespace(state State) (interface{}, error) {
return RuneP("whitespace", func(r rune) bool {
return unicode.In(r, unicode.White_Space)
})(state)
}
// Newline 构造一个 newline 校验算子
func Newline(state State) (interface{}, error) {
return RuneOf("\n")(state)
}
// Crlf 构造一个 \n\r 校验算子
func Crlf(state State) (interface{}, error) {
return Str("\n\r")(state)
}
// EndOfLine 匹配 \n\r 或 \n 。
func EndOfLine(state State) (interface{}, error) {
_, err := Chr('\n')(state)
if err != nil {
return nil, err
}
_, err = Chr('\r')(state)
if err != nil {
return "\n", err
}
return "\n\r", err
}
// Letter 构造一个字母校验算子
func Letter(state State) (interface{}, error) {
return RuneP("letter", unicode.IsLetter)(state)
}
// Number 构造一个 Number 校验算子
func Number(state State) (interface{}, error) {
return RuneP("number", unicode.IsNumber)(state)
}
// Digit 构造一个数字字符校验算子
func Digit(state State) (interface{}, error) {
return RuneP("digit", unicode.IsDigit)(state)
}
// UInt 返回一个无符号整型的解析算子
func UInt(state State) (interface{}, error) {
return Do(func(st State) interface{} {
digits := Many1(Digit).Exec(st)
buffer := digits.([]interface{})
data := make([]rune, 0, len(buffer))
for _, value := range buffer {
data = append(data, value.(rune))
}
return string(data)
})(state)
}
// Int 返回一个有符号整型的解析算子
func Int(state State) (interface{}, error) {
binder := func(value interface{}) P {
return Return(fmt.Sprintf("-%s", value.(string)))
}
return Choice(Try(Chr('-').Then(UInt).Bind(binder)), UInt)(state)
}
// UFloat 返回一个无符号实数的解析算子
func UFloat(state State) (interface{}, error) {
return Do(func(state State) interface{} {
left := Choice(Try(P(UInt).Over(Chr('.'))), Chr('.').Then(Return("0"))).Exec(state)
right := P(UInt).Exec(state)
return fmt.Sprintf("%s.%s", left, right)
})(state)
}
// Float 返回一个有符号实数的解析算子
func Float(state State) (interface{}, error) {
binder := func(value interface{}) P {
return Return("-" + value.(string))
}
return Choice(Try(Chr('-').Then(UFloat).Bind(binder)), UFloat)(state)
}
// ToString 将封装为 interface{} 的 []interface{} 转成 string,如果输入数据与前面提到的规范不符,会 panic
func ToString(input interface{}) string {
data := input.([]interface{})
l := len(data)
buffer := make([]rune, l)
for index, item := range data {
buffer[index] = item.(rune)
}
return string(buffer)
}
// ToInterfaces 将string 转为 []interface{} ,如果输入数据与前面提到的规范不符,会 panic
func ToInterfaces(input string) []interface{} {
buf := []rune(input)
re := make([]interface{}, 0, len(buf))
for r := range buf {
re = append(re, r)
}
return re
}
// ReturnString 用 Return 包装 ToString,使其适用于组合子表达式。
func ReturnString(input interface{}) P {
return Return(ToString(input))
}