-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.go
61 lines (54 loc) · 1.19 KB
/
string.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
package pjson
import p "github.com/Dwarfartisan/goparsec2"
// Rune 是 rune 的简单封装
type Rune rune
// EscapeChars 用于string
var EscapeChars = p.Do(func(st p.State) interface{} {
p.Chr('\\').Exec(st)
r := p.RuneOf("nrt\"\\").Exec(st)
ru := r.(rune)
switch ru {
case 'r':
return '\r'
case 'n':
return '\n'
case '"':
return '"'
case '\\':
return '\\'
case 't':
return '\t'
default:
panic(st.Trap("Unknown escape sequence \\%c", r))
}
})
//用于rune
var EscapeCharr = p.Do(func(st p.State) interface{} {
p.Chr('\\').Exec(st)
r := p.RuneOf("nrt'\\").Exec(st)
ru := r.(rune)
switch ru {
case 'r':
return '\r'
case 'n':
return '\n'
case '\'':
return '\''
case '\\':
return '\\'
case 't':
return '\t'
default:
panic(st.Trap("Unknown escape sequence \\%c", r))
}
})
// RuneParser 实现 rune 的解析
var RuneParser = p.Do(func(state p.State) interface{} {
p.Chr('\'').Exec(state)
c := p.Choice(p.Try(EscapeCharr), p.NChr('\'')).Exec(state)
p.Chr('\'').Exec(state)
return Rune(c.(rune))
})
// StringParser 实现字符串解析
var StringParser = p.Between(p.Chr('"'), p.Chr('"'),
p.Many(p.Choice(p.Try(EscapeChars), p.NChr('"')))).Bind(p.ReturnString)