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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! A tokenizer for use in LALRPOP itself.
use std::str::CharIndices;
use unicode_xid::UnicodeXID;
use self::ErrorCode::*;
use self::Tok::*;
#[cfg(test)]
mod test;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub location: usize,
pub code: ErrorCode
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorCode {
UnrecognizedToken,
UnterminatedEscape,
UnterminatedStringLiteral,
UnterminatedCode,
}
fn error<T>(c: ErrorCode, l: usize) -> Result<T,Error> {
Err(Error { location: l, code: c })
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Tok<'input> {
// Keywords;
Enum,
Extern,
Grammar,
If,
Mut,
Pub,
Type,
// Special keywords: these are accompanied by a series of
// uninterpreted strings representing imports and stuff.
Use(&'input str),
Where(Vec<&'input str>),
// Identifiers of various kinds:
Escape(&'input str),
Id(&'input str),
MacroId(&'input str), // identifier followed immediately by `<`
Lifetime(&'input str), // includes the `'`
StringLiteral(&'input str), // excludes the `"`
// Symbols:
Ampersand,
BangEquals,
BangTilde,
Colon,
ColonColon,
Comma,
DotDot,
Equals,
EqualsEquals,
EqualsGreaterThanCode(&'input str),
EqualsGreaterThanQuestionCode(&'input str),
EqualsGreaterThanLookahead,
EqualsGreaterThanLookbehind,
Hash,
GreaterThan,
LeftBrace,
LeftBracket,
LeftParen,
LessThan,
Lookahead, // @L
Lookbehind, // @R
Plus,
Question,
RightBrace,
RightBracket,
RightParen,
Semi,
Star,
TildeTilde,
Underscore,
}
pub struct Tokenizer<'input> {
text: &'input str,
chars: CharIndices<'input>,
lookahead: Option<(usize, char)>,
shift: usize,
}
macro_rules! eof {
($x:expr) => {
match $x { Some(v) => v, None => { return None; } }
}
}
pub type Spanned<T> = (usize, T, usize);
const KEYWORDS: &'static [(&'static str, Tok<'static>)] = &[
("enum", Enum),
("extern", Extern),
("grammar", Grammar),
("if", If),
("mut", Mut),
("pub", Pub),
("type", Type),
];
impl<'input> Tokenizer<'input> {
pub fn new(text: &'input str, shift: usize) -> Tokenizer<'input> {
let mut t = Tokenizer {
text: text,
chars: text.char_indices(),
lookahead: None,
shift: shift,
};
t.bump();
t
}
fn next_unshifted(&mut self) -> Option<Result<Spanned<Tok<'input>>, Error>> {
loop {
return match self.lookahead {
Some((idx0, '&')) => {
self.bump();
Some(Ok((idx0, Ampersand, idx0+1)))
}
Some((idx0, '!')) => {
match self.bump() {
Some((idx1, '=')) => {
self.bump();
Some(Ok((idx0, BangEquals, idx1+1)))
}
Some((idx1, '~')) => {
self.bump();
Some(Ok((idx0, BangTilde, idx1+1)))
}
_ => {
Some(error(UnrecognizedToken, idx0))
}
}
}
Some((idx0, ':')) => {
match self.bump() {
Some((idx1, ':')) => {
self.bump();
Some(Ok((idx0, ColonColon, idx1+1)))
}
_ => {
Some(Ok((idx0, Colon, idx0+1)))
}
}
}
Some((idx0, ',')) => {
self.bump();
Some(Ok((idx0, Comma, idx0+1)))
}
Some((idx0, '.')) => {
match self.bump() {
Some((idx1, '.')) => {
self.bump();
Some(Ok((idx0, DotDot, idx1+1)))
}
_ => {
Some(error(UnrecognizedToken, idx0))
}
}
}
Some((idx0, '=')) => {
match self.bump() {
Some((idx1, '=')) => {
self.bump();
Some(Ok((idx0, EqualsEquals, idx1+1)))
}
Some((_, '>')) => {
self.bump();
Some(self.right_arrow(idx0))
}
_ => {
Some(Ok((idx0, Equals, idx0+1)))
}
}
}
Some((idx0, '#')) => {
self.bump();
Some(Ok((idx0, Hash, idx0+1)))
}
Some((idx0, '>')) => {
self.bump();
Some(Ok((idx0, GreaterThan, idx0+1)))
}
Some((idx0, '{')) => {
self.bump();
Some(Ok((idx0, LeftBrace, idx0+1)))
}
Some((idx0, '[')) => {
self.bump();
Some(Ok((idx0, LeftBracket, idx0+1)))
}
Some((idx0, '(')) => {
self.bump();
Some(Ok((idx0, LeftParen, idx0+1)))
}
Some((idx0, '<')) => {
self.bump();
Some(Ok((idx0, LessThan, idx0+1)))
}
Some((idx0, '@')) => {
match self.bump() {
Some((idx1, 'L')) => {
self.bump();
Some(Ok((idx0, Lookahead, idx1+1)))
}
Some((idx1, 'R')) => {
self.bump();
Some(Ok((idx0, Lookbehind, idx1+1)))
}
_ => {
Some(error(UnrecognizedToken, idx0))
}
}
}
Some((idx0, '+')) => {
self.bump();
Some(Ok((idx0, Plus, idx0+1)))
}
Some((idx0, '?')) => {
self.bump();
Some(Ok((idx0, Question, idx0+1)))
}
Some((idx0, '}')) => {
self.bump();
Some(Ok((idx0, RightBrace, idx0+1)))
}
Some((idx0, ']')) => {
self.bump();
Some(Ok((idx0, RightBracket, idx0+1)))
}
Some((idx0, ')')) => {
self.bump();
Some(Ok((idx0, RightParen, idx0+1)))
}
Some((idx0, ';')) => {
self.bump();
Some(Ok((idx0, Semi, idx0+1)))
}
Some((idx0, '*')) => {
self.bump();
Some(Ok((idx0, Star, idx0+1)))
}
Some((idx0, '~')) => {
match self.bump() {
Some((idx1, '~')) => {
self.bump();
Some(Ok((idx0, TildeTilde, idx1+1)))
}
_ => {
Some(error(UnrecognizedToken, idx0))
}
}
}
Some((idx0, '_')) => {
self.bump();
Some(Ok((idx0, Underscore, idx0+1)))
}
Some((idx0, '`')) => {
self.bump();
Some(self.escape(idx0))
}
Some((idx0, '\'')) => {
self.bump();
Some(Ok(self.lifetime(idx0)))
}
Some((idx0, '"')) => {
self.bump();
Some(self.string_literal(idx0))
}
Some((idx0, '/')) => {
match self.bump() {
Some((_, '/')) => {
self.take_until(|c| c == '\n');
continue;
}
_ => {
Some(error(UnrecognizedToken, idx0))
}
}
}
Some((idx0, c)) if is_identifier_start(c) => {
Some(self.identifierish(idx0))
}
Some((_, c)) if c.is_whitespace() => {
self.bump();
continue;
}
Some((idx, _)) => {
Some(error(UnrecognizedToken, idx))
}
None => {
None
}
};
}
}
fn bump(&mut self) -> Option<(usize, char)> {
self.lookahead = self.chars.next();
self.lookahead
}
fn right_arrow(&mut self, idx0: usize) -> Result<Spanned<Tok<'input>>, Error> {
// we've seen =>, now we have to choose between:
//
// => code
// =>? code
// =>@L
// =>@R
match self.lookahead {
Some((_, '@')) => {
match self.bump() {
Some((idx2, 'L')) => {
self.bump();
Ok((idx0, EqualsGreaterThanLookahead, idx2+1))
}
Some((idx2, 'R')) => {
self.bump();
Ok((idx0, EqualsGreaterThanLookbehind, idx2+1))
}
_ => {
error(UnrecognizedToken, idx0)
}
}
}
Some((idx1, '?')) => {
self.bump();
let idx2 = try!(self.code(idx0, "([{", "}])"));
let code = &self.text[idx1+1..idx2];
Ok((idx0, EqualsGreaterThanQuestionCode(code), idx2))
}
Some((idx1, _)) => {
let idx2 = try!(self.code(idx0, "([{", "}])"));
let code = &self.text[idx1..idx2];
Ok((idx0, EqualsGreaterThanCode(code), idx2))
}
None => {
error(UnterminatedCode, idx0)
}
}
}
fn code(&mut self, idx0: usize, open_delims: &str, close_delims: &str) -> Result<usize, Error> {
// This is the interesting case. To find the end of the code,
// we have to scan ahead, matching (), [], and {}, and looking
// for a suitable terminator: `,`, `;`, `]`, `}`, or `)`.
let mut balance = 0; // number of unclosed `(` etc
loop {
if let Some((idx, c)) = self.lookahead {
if open_delims.find(c).is_some() {
balance += 1;
} else if balance > 0 {
if close_delims.find(c).is_some() {
balance -= 1;
}
} else {
debug_assert!(balance == 0);
if c == ',' || c == ';' || close_delims.find(c).is_some() {
// Note: we do not consume the
// terminator. The code is everything *up
// to but not including* the terminating
// `,`, `;`, etc.
return Ok(idx);
}
}
} else if balance > 0 {
// the input should not end with an
// unbalanced number of `{` etc!
return error(UnterminatedCode, idx0);
} else {
debug_assert!(balance == 0);
return Ok(self.text.len());
}
self.bump();
}
}
fn escape(&mut self, idx0: usize) -> Result<Spanned<Tok<'input>>, Error> {
match self.take_until(|c| c == '`') {
Some(idx1) => {
self.bump(); // consume the '`'
let text: &'input str = &self.text[idx0+1..idx1]; // do not include the `` in the str
Ok((idx0, Escape(text), idx1+1))
}
None => {
error(UnterminatedEscape, idx0)
}
}
}
fn string_literal(&mut self, idx0: usize) -> Result<Spanned<Tok<'input>>, Error> {
let mut escape = false;
let terminate = |c: char| {
if escape {
escape = false;
false
} else if c == '\\' {
escape = true;
false
} else if c == '"' {
true
} else {
false
}
};
match self.take_until(terminate) {
Some(idx1) => {
self.bump(); // consume the '"'
let text = &self.text[idx0+1..idx1]; // do not include the "" in the str
Ok((idx0, StringLiteral(text), idx1+1))
}
None => {
error(UnterminatedStringLiteral, idx0)
}
}
}
fn lifetime(&mut self, idx0: usize) -> Spanned<Tok<'input>> {
let (start, word, end) = self.word(idx0);
(start, Lifetime(word), end)
}
fn identifierish(&mut self, idx0: usize) -> Result<Spanned<Tok<'input>>, Error> {
let (start, word, end) = self.word(idx0);
if word == "use" {
let code_end = try!(self.code(idx0, "([{", "}])"));
let code = &self.text[end..code_end];
return Ok((start, Tok::Use(code), code_end));
}
if word == "where" {
let mut wcs = vec![];
let mut wc_start = end;
let mut wc_end;
loop {
// Note: do not include `{` as a delimeter here, as
// that is not legal in the trait/where-clause syntax,
// and in fact signals start of the fn body. But do
// include `<`.
wc_end = try!(self.code(wc_start, "([<", ">])"));
let wc = &self.text[wc_start..wc_end];
wcs.push(wc);
// if this ended in a comma, maybe expect another where-clause
if let Some((_, ',')) = self.lookahead {
self.bump();
wc_start = wc_end + 1;
} else {
break;
}
}
return Ok((start, Tok::Where(wcs), wc_end));
}
let tok =
// search for a keyword first; if none are found, this is
// either a MacroId or an Id, depending on whether there
// is a `<` immediately afterwards
KEYWORDS.iter()
.filter(|&&(w, _)| w == word)
.map(|&(_, ref t)| t.clone())
.next()
.unwrap_or_else(|| {
match self.lookahead {
Some((_, '<')) => MacroId(word),
_ => Id(word),
}
});
Ok((start, tok, end))
}
fn word(&mut self, idx0: usize) -> Spanned<&'input str> {
match self.take_while(is_identifier_continue) {
Some(end) => (idx0, &self.text[idx0..end], end),
None => (idx0, &self.text[idx0..], self.text.len()),
}
}
fn take_while<F>(&mut self, mut keep_going: F) -> Option<usize>
where F: FnMut(char) -> bool
{
self.take_until(|c| !keep_going(c))
}
fn take_until<F>(&mut self, mut terminate: F) -> Option<usize>
where F: FnMut(char) -> bool
{
loop {
match self.lookahead {
None => {
return None;
}
Some((idx1, c)) => {
if terminate(c) {
return Some(idx1);
} else {
self.bump();
}
}
}
}
}
}
impl<'input> Iterator for Tokenizer<'input> {
type Item = Result<Spanned<Tok<'input>>, Error>;
fn next(&mut self) -> Option<Result<Spanned<Tok<'input>>, Error>> {
match self.next_unshifted() {
None =>
None,
Some(Ok((l, t, r))) =>
Some(Ok((l+self.shift, t, r+self.shift))),
Some(Err(Error { location, code })) =>
Some(Err(Error { location: location+self.shift, code: code })),
}
}
}
fn is_identifier_start(c: char) -> bool {
UnicodeXID::is_xid_start(c)
}
fn is_identifier_continue(c: char) -> bool {
UnicodeXID::is_xid_continue(c)
}