-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
230 lines (199 loc) · 4.63 KB
/
scanner.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
227
228
229
230
/*
* Copyright (c) 2022, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package schema
import (
"github.com/dburkart/fossil/pkg/common/parse"
"strings"
"unicode"
"unicode/utf8"
)
type Scanner struct {
Input string
Start int
Pos int
RuneWidth int
LastWidth int
}
// MatchKey returns the length of the schema key
//
// Grammar:
//
// key = DQUOTE 1*( ALPHA / DIGIT / "_" / "-" ) DQUOTE
func (s *Scanner) MatchKey() int {
pos := s.Pos
r, width := utf8.DecodeRuneInString(s.Input[pos:])
if r != '"' && r != '\'' {
return 0
}
matchRune := r
pos += width
r, width = utf8.DecodeRuneInString(s.Input[pos:])
for r != matchRune {
if pos >= len(s.Input) {
return 0
}
if !unicode.IsLetter(r) && !unicode.IsDigit(r) &&
r != '_' && r != '-' {
return 0
}
pos += width
r, width = utf8.DecodeRuneInString(s.Input[pos:])
}
pos += width
return pos - s.Pos
}
func (s *Scanner) MatchNumber() int {
r, width := utf8.DecodeRuneInString(s.Input[s.Pos:])
size := 0
for i := s.Pos; unicode.IsDigit(r); {
size += width
i += width
r, width = utf8.DecodeRuneInString(s.Input[i:])
}
return size
}
// Emit the next Token found on Scanner.Input
func (s *Scanner) Emit() parse.Token {
var t parse.Token
oldStart := s.Start
for {
r, width := utf8.DecodeRuneInString(s.Input[s.Pos:])
s.Start = s.Pos
found := true
skip := 0
switch {
case unicode.IsSpace(r):
skip = width
found = false
case r == '{':
t.Type = TOK_CURLY_O
skip = width
case r == '}':
t.Type = TOK_CURLY_X
skip = width
case r == '[':
t.Type = TOK_BRACKET_O
skip = width
case r == ']':
t.Type = TOK_BRACKET_X
skip = width
case r == ':':
t.Type = TOK_COLON
skip = width
case r == ',':
t.Type = TOK_COMMA
skip = width
case unicode.IsDigit(r):
skip = s.MatchNumber()
if skip == 0 {
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
}
t.Type = TOK_NUMBER
case r == '"' || r == '\'':
skip = s.MatchKey()
if skip == 0 {
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
}
t.Type = TOK_KEY
case r == 'b':
if strings.HasPrefix(s.Input[s.Pos:], "binary") {
t.Type = TOK_TYPE
skip = len("binary")
break
}
if strings.HasPrefix(s.Input[s.Pos:], "boolean") {
t.Type = TOK_TYPE
skip = len("boolean")
break
}
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
case r == 'f':
if strings.HasPrefix(s.Input[s.Pos:], "float") {
if strings.HasPrefix(s.Input[s.Pos+5:], "32") ||
strings.HasPrefix(s.Input[s.Pos+5:], "64") {
t.Type = TOK_TYPE
skip = len("float32")
break
}
}
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
case r == 'i':
if strings.HasPrefix(s.Input[s.Pos:], "int") {
if strings.HasPrefix(s.Input[s.Pos+3:], "8") {
t.Type = TOK_TYPE
skip = len("int8")
break
}
if strings.HasPrefix(s.Input[s.Pos+3:], "16") ||
strings.HasPrefix(s.Input[s.Pos+3:], "32") ||
strings.HasPrefix(s.Input[s.Pos+3:], "64") {
t.Type = TOK_TYPE
skip = len("int16")
break
}
}
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
case r == 's':
if strings.HasPrefix(s.Input[s.Pos:], "string") {
t.Type = TOK_TYPE
skip = len("string")
break
}
t.Type = TOK_INVALID
skip = s.SkipToBoundary(isDelimiter)
case r == 'u':
if strings.HasPrefix(s.Input[s.Pos:], "uint") {
if strings.HasPrefix(s.Input[s.Pos+4:], "8") {
t.Type = TOK_TYPE
skip = len("uint8")
break
}
if strings.HasPrefix(s.Input[s.Pos+4:], "16") ||
strings.HasPrefix(s.Input[s.Pos+4:], "32") ||
strings.HasPrefix(s.Input[s.Pos+4:], "64") {
t.Type = TOK_TYPE
skip = len("uint16")
break
}
}
}
s.Pos = s.Start + skip
if found {
break
}
}
t.Lexeme = s.Input[s.Start:s.Pos]
t.Location = parse.Location{Start: s.Start, End: s.Pos}
s.Start = s.Pos
s.LastWidth = s.Start - oldStart
return t
}
// Rewind the last read token
func (s *Scanner) Rewind() {
s.Start -= s.LastWidth
s.Pos = s.Start
s.LastWidth = 0
}
type boundaryFunc func(rune) bool
func isDelimiter(r rune) bool {
return unicode.IsSpace(r) || r == ':' || r == ',' || r == '"' || r == '}'
}
// SkipToBoundary returns the number of bytes until the next delimiter.
// This is useful for skipping over invalid tokens.
func (s *Scanner) SkipToBoundary(boundary boundaryFunc) int {
r, width := utf8.DecodeRuneInString(s.Input[s.Pos:])
size := 0
for !boundary(r) && s.Pos+size < len(s.Input) {
size += width
r, width = utf8.DecodeRuneInString(s.Input[s.Pos+size:])
}
return size
}