-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.odin
299 lines (278 loc) · 6.31 KB
/
tokenizer.odin
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package gql
import "core:unicode/utf8"
Token :: struct {
kind : Token_Kind,
value: string,
}
Token_Kind :: enum u8 {
Invalid,
EOF,
// Punctuators
Paren_Open,
Paren_Close,
Bracket_Open,
Bracket_Close,
Brace_Open,
Brace_Close,
Colon,
Equals,
At,
Dollar,
Exclamation,
Pipe,
// Scalars
Int,
Float,
String,
String_Block,
// Identifiers
Name,
// Operators
Spread,
}
Keyword :: enum u8 {
None,
Query,
Mutation,
Subscription,
Fragment,
Directive,
Enum,
Union,
Scalar,
Type,
Input,
On,
Repeatable,
Interface,
Implements,
Extend,
Schema,
// Keyword Values
True,
False,
Null,
}
Tokenizer :: struct {
src : string,
offset_read : int,
offset_write: int,
char : rune,
last_width : int,
}
tokenizer_init :: proc "contextless" (t: ^Tokenizer, src: string) {
t.src = src
if next_char(t) == utf8.RUNE_BOM {
t.offset_write = t.offset_read
next_char(t)
}
}
@(require_results)
tokenizer_make :: proc "contextless" (src: string) -> (t: Tokenizer) {
tokenizer_init(&t, src)
return
}
make_tokenizer :: tokenizer_make
next_char :: proc "contextless" (t: ^Tokenizer) -> (char: rune, before_eof: bool) #optional_ok #no_bounds_check {
if t.offset_read >= len(t.src) {
t.char = 0
t.offset_read = len(t.src)+1
t.last_width = 1
return 0, false
}
ch, width := utf8.decode_rune_in_string(t.src[t.offset_read:])
t.char = ch
t.offset_read += width
t.last_width = width
return ch, true
}
@(require_results)
next_token :: proc "contextless" (t: ^Tokenizer) -> (token: Token, before_eof: bool) #optional_ok #no_bounds_check {
make_token :: proc "contextless" (t: ^Tokenizer, kind: Token_Kind) -> (token: Token) #no_bounds_check {
token.kind = kind
token.value = t.src[t.offset_write : t.offset_read]
t.offset_write = t.offset_read
next_char(t)
return
}
make_token_ignore_last_char :: proc "contextless" (t: ^Tokenizer, kind: Token_Kind) -> (token: Token) #no_bounds_check {
token.kind = kind
token.value = t.src[t.offset_write : t.offset_read-t.last_width]
t.offset_write = t.offset_read-t.last_width
return
}
if t.offset_read > len(t.src) {
return make_token(t, .EOF), false
}
before_eof = true
switch t.char {
// Whitespace and comma
case ',', ' ', '\t', '\n', '\r':
t.offset_write = t.offset_read
next_char(t)
return next_token(t)
// Ignore Comment
case '#':
for {
switch next_char(t) {
case 0, '\n', '\r':
t.offset_write = t.offset_read
next_char(t)
return next_token(t)
}
}
// Punctuators
case '(': token = make_token(t, .Paren_Open)
case ')': token = make_token(t, .Paren_Close)
case '[': token = make_token(t, .Bracket_Open)
case ']': token = make_token(t, .Bracket_Close)
case '{': token = make_token(t, .Brace_Open)
case '}': token = make_token(t, .Brace_Close)
case ':': token = make_token(t, .Colon)
case '=': token = make_token(t, .Equals)
case '@': token = make_token(t, .At,)
case '$': token = make_token(t, .Dollar)
case '!': token = make_token(t, .Exclamation)
case '|': token = make_token(t, .Pipe)
case '.':
if '.' == next_char(t) &&
'.' == next_char(t)
{
token = make_token(t, .Spread)
} else {
token = make_token_ignore_last_char(t, .Invalid)
}
// Int and Float
case '-':
next_char(t)
return scan_number(t)
case '0'..='9':
return scan_number(t)
// Keywords and Identifiers
case 'a'..='z', 'A'..='Z', '_':
for {
switch next_char(t) {
case 'a'..='z', 'A'..='Z', '0'..='9', '_': continue
}
break
}
token = make_token_ignore_last_char(t, .Name)
// String
case '"':
escaping := false
if '"' == next_char(t) {
// Empty String
if '"' != next_char(t) {
return make_token_ignore_last_char(t, .String), true
}
// Block String
for {
switch next_char(t) {
case 0:
return make_token_ignore_last_char(t, .Invalid), true
case '\\':
escaping = !escaping
case '"':
if !escaping &&
'"' == next_char(t) &&
'"' == next_char(t)
{
return make_token(t, .String_Block), true
}
escaping = false
case:
escaping = false
}
}
}
// String
for {
switch next_char(t) {
case 0, '\n':
return make_token_ignore_last_char(t, .Invalid), true
case '\\':
escaping = !escaping
case '"':
if !escaping {
return make_token(t, .String), true
}
escaping = false
case:
escaping = false
}
}
case:
token = make_token(t, .Invalid)
}
/*
123
123.456
0.123
0
*/
scan_number :: proc "contextless" (t: ^Tokenizer) -> (token: Token, before_eof: bool) #optional_ok {
scan_fraction :: proc "contextless" (t: ^Tokenizer) -> (token: Token, before_eof: bool) #optional_ok {
switch next_char(t) {
case '0'..='9': // continue
case: return make_token_ignore_last_char(t, .Invalid), true
}
for {
switch next_char(t) {
case '0'..='9': // continue
case 'a'..='z', 'A'..='Z', '_':
return make_token_ignore_last_char(t, .Invalid), true
case:
return make_token_ignore_last_char(t, .Float), true
}
}
}
if t.char == '0' {
switch next_char(t) {
case '.':
return scan_fraction(t)
case '0'..='9', 'a'..='z', 'A'..='Z', '_':
return make_token_ignore_last_char(t, .Invalid), true
case:
return make_token_ignore_last_char(t, .Int), true
}
}
for {
switch next_char(t) {
case '0'..='9': // continue
case '.':
return scan_fraction(t)
case 'a'..='z', 'A'..='Z', '_':
return make_token_ignore_last_char(t, .Invalid), true
case:
return make_token_ignore_last_char(t, .Int), true
}
}
}
return
}
tokenizer_next :: next_token
@(require_results)
match_keyword :: proc(str: string) -> Keyword {
switch str {
case "query": return .Query
case "mutation": return .Mutation
case "subscription":return .Subscription
case "fragment": return .Fragment
case "directive": return .Directive
case "enum": return .Enum
case "union": return .Union
case "scalar": return .Scalar
case "type": return .Type
case "input": return .Input
case "on": return .On
case "repeatable": return .Repeatable
case "interface": return .Interface
case "implements": return .Implements
case "extend": return .Extend
case "schema": return .Schema
case "true": return .True
case "false": return .False
case "null": return .Null
case: return .None
}
}