forked from vektah/goparsify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
literals.go
171 lines (148 loc) · 3.24 KB
/
literals.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
package goparsify
import (
"bytes"
"strconv"
"unicode/utf8"
)
// StringLit matches a quoted string and returns it in .Token. It may contain:
// - unicode
// - escaped characters, eg \" or \n
// - unicode sequences, eg \uBEEF
func StringLit(allowedQuotes string) Parser {
return NewParser("string literal", func(ps *State, node *Result) {
ps.WS(ps)
if !stringContainsByte(allowedQuotes, ps.Input[ps.Pos]) {
ps.ErrorHere(allowedQuotes)
return
}
quote := ps.Input[ps.Pos]
var end = ps.Pos + 1
inputLen := len(ps.Input)
var buf *bytes.Buffer
for end < inputLen {
switch ps.Input[end] {
case '\\':
if end+1 >= inputLen {
ps.ErrorHere(string(quote))
return
}
if buf == nil {
buf = bytes.NewBufferString(ps.Input[ps.Pos+1 : end])
}
c := ps.Input[end+1]
if c == 'u' {
if end+6 >= inputLen {
ps.Error.expected = "[a-f0-9]{4}"
ps.Error.pos = end + 2
return
}
r, ok := unhex(ps.Input[end+2 : end+6])
if !ok {
ps.Error.expected = "[a-f0-9]"
ps.Error.pos = end + 2
return
}
buf.WriteRune(r)
end += 6
} else {
buf.WriteByte(c)
end += 2
}
case quote:
if buf == nil {
node.Token = ps.Input[ps.Pos+1 : end]
ps.Pos = end + 1
return
}
ps.Pos = end + 1
node.Token = buf.String()
return
default:
if buf == nil {
if ps.Input[end] < 127 {
end++
} else {
_, w := utf8.DecodeRuneInString(ps.Input[end:])
end += w
}
} else {
r, w := utf8.DecodeRuneInString(ps.Input[end:])
end += w
buf.WriteRune(r)
}
}
}
ps.ErrorHere(string(quote))
})
}
// NumberLit matches a floating point or integer number and returns it as a int64 or float64 in .Result
func NumberLit() Parser {
return NewParser("number literal", func(ps *State, node *Result) {
ps.WS(ps)
end := ps.Pos
float := false
inputLen := len(ps.Input)
if end < inputLen && (ps.Input[end] == '-' || ps.Input[end] == '+') {
end++
}
for end < inputLen && ps.Input[end] >= '0' && ps.Input[end] <= '9' {
end++
}
if end < inputLen && ps.Input[end] == '.' {
float = true
end++
}
for end < inputLen && ps.Input[end] >= '0' && ps.Input[end] <= '9' {
end++
}
if end < inputLen && (ps.Input[end] == 'e' || ps.Input[end] == 'E') {
end++
float = true
if end < inputLen && (ps.Input[end] == '-' || ps.Input[end] == '+') {
end++
}
for end < inputLen && ps.Input[end] >= '0' && ps.Input[end] <= '9' {
end++
}
}
if end == ps.Pos {
ps.ErrorHere("number")
return
}
var err error
if float {
node.Result, err = strconv.ParseFloat(ps.Input[ps.Pos:end], 10)
} else {
node.Result, err = strconv.ParseInt(ps.Input[ps.Pos:end], 10, 64)
}
if err != nil {
ps.ErrorHere("number")
return
}
ps.Pos = end
})
}
func stringContainsByte(s string, b byte) bool {
for i := 0; i < len(s); i++ {
if b == s[i] {
return true
}
}
return false
}
func unhex(b string) (v rune, ok bool) {
for _, c := range b {
v <<= 4
switch {
case '0' <= c && c <= '9':
v |= c - '0'
case 'a' <= c && c <= 'f':
v |= c - 'a' + 10
case 'A' <= c && c <= 'F':
v |= c - 'A' + 10
default:
return 0, false
}
}
return v, true
}