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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Provides [Scanner] to parse Oracle specific SQL into a lazy
//! iterator over individual tokens.
//!
//! Usage example:
//!
//! ```rust
//! use orql::scanner::*;
//!
//! let mut scan = Scanner::new("select * from dual;");
//! assert_eq!(scan.next(), Some(Ok(Token {
//! ttype: TokenType::Keyword(Keyword::SELECT),
//! loc: (1, 1).into(),
//! })));
//! assert_eq!(scan.next(), Some(Ok(Token {
//! ttype: TokenType::Star,
//! loc: (1, 8).into(),
//! })));
//! // ...
//! ```
use std::{
borrow::Cow,
iter::Peekable,
ops::Range,
str::{Chars, FromStr},
};
mod error;
mod keywords;
mod text;
mod tokens;
pub use error::*;
pub(crate) use keywords::BuiltIn;
pub use keywords::{Keyword, Reserved};
pub use text::*;
pub use tokens::*;
#[cfg(test)]
mod tests;
/// An iterator over tokens from a lazily parsed SQL string.
pub struct Scanner<'s> {
source: &'s str,
/// a stream of characters from `source`
chars: Peekable<Chars<'s>>,
/// location of the next character, ie. `self.chars.next()`
next_loc: Location,
/// offset into `source` of the next character, ie. `self.chars.next()`,
/// `>= source.len()` if reached EOF
next_pos: usize,
/// a pushed back state and token to be served by `self.chars.next()`;
/// note: while `self.pushed_back.is_none()`, the `chars` iterator might
/// be out of sync with the scanner's direct state
pushed_back: Option<PushedBack<'s>>,
}
/// Temporary state pushed back and to be restored later
struct PushedBack<'s> {
token: Option<Result<Token<'s>>>,
next_loc: Location,
}
// ----------------------------------------------------------------------------
impl<'s> Scanner<'s> {
pub fn new(source: &'s str) -> Self {
Self {
source,
chars: source.chars().peekable(),
next_loc: Location { line: 1, col: 1 },
next_pos: 0,
pushed_back: None,
}
}
/// Retrieves the underlying source string being processed.
pub(crate) fn source(&self) -> &'s str {
self.source
}
/// Retrieves the current position within the underlying `source` text
/// (where `position` is a zero based index into `source`.) This is the
/// position where the scanner will start finding the next token. The
/// initial position is zero. After hitting the "end of the source" the
/// position will be equal to the source's length.
pub(crate) fn position(&self) -> usize {
self.next_pos
}
/// Retrieves the location of the scanner is its `source` effectively
/// telling the approximate location of the next token.
pub fn location(&self) -> Location {
self.next_loc
}
/// Turns this scanner into one that can "peek" at the next token (without
/// consuming it), just like with [std::iter::Peekable]. The returned
/// "peekable", however, will allow to access the underlying scanner.
pub fn peekable(self) -> PeekableScanner<'s> {
PeekableScanner {
inner: self,
peeked: None,
}
}
}
impl<'s> Iterator for Scanner<'s> {
type Item = Result<Token<'s>>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(state) = self.pushed_back.take() {
self.next_loc = state.next_loc;
return state.token;
}
loop {
let token_pos = self.next_pos;
let token_loc = self.next_loc;
macro_rules! emit_token {
($ttype:expr) => {
return Some(Ok(Token {
ttype: $ttype,
loc: token_loc,
}))
};
}
macro_rules! expect_char {
($char:literal, $ttype:expr) => {
// ~ do not consume the peeked char upon an error;
// maybe it's the start of a valid, next token
match self.peek_char() {
Some($char) => {
self.consume_char_and_col();
emit_token!($ttype)
}
Some(c) => {
return Some(Err(Error {
etype: ErrorType::InvalidChar {
char: c,
expected: Some(concat!("a '", $char, "'")),
},
loc: self.next_loc,
}));
}
None => {
return Some(Err(Error {
etype: ErrorType::UnexpectedEof {
expected: Some(concat!("a '", $char, "'")),
},
loc: self.next_loc,
}));
}
}
};
}
match self.next_char()? {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
c => {
self.next_loc.col += 1;
match c {
' ' | '\t' => {}
'(' => emit_token!(TokenType::LeftParen),
')' => emit_token!(TokenType::RightParen),
'.' => {
if matches!(self.peek_char(), Some('0'..='9')) {
return Some(self.consume_fraction(token_loc, token_pos));
} else {
emit_token!(TokenType::Dot);
};
}
'0'..='9' => return Some(self.consume_number(token_loc, token_pos)),
',' => emit_token!(TokenType::Comma),
';' => emit_token!(TokenType::Semicolon),
'+' => emit_token!(TokenType::Plus),
'*' => emit_token!(TokenType::Star),
'|' => expect_char!('|', TokenType::PipePipe),
'?' => emit_token!(TokenType::QuestionMark),
':' => return Some(self.consume_placeholder(token_loc)),
'@' => emit_token!(TokenType::At),
'=' => {
emit_token!(match self.peek_char() {
Some('>') => {
self.consume_char_and_col();
TokenType::EqualGreater
}
_ => TokenType::Equal,
})
}
'<' => {
emit_token!(match self.peek_char() {
Some('=') => {
self.consume_char_and_col();
TokenType::LessEqual
}
Some('>') => {
self.consume_char_and_col();
TokenType::LessGreater
}
_ => TokenType::Less,
});
}
'!' => expect_char!('=', TokenType::BangEqual),
'>' => {
emit_token!(if matches!(self.peek_char(), Some('=')) {
self.consume_char_and_col();
TokenType::GreaterEqual
} else {
TokenType::Greater
});
}
'/' => {
if matches!(self.peek_char(), Some('*')) {
self.consume_char_and_col();
return Some(
self.consume_block_comment(self.next_loc.with_cols_removed(2)),
);
} else {
emit_token!(TokenType::Slash)
}
}
'-' => {
if matches!(self.peek_char(), Some('-')) {
self.consume_char();
return Some(Ok(self.consume_line_comment(token_loc)));
} else {
emit_token!(TokenType::Minus)
}
}
'^' => expect_char!('=', TokenType::CaretEqual),
'\'' => return Some(self.consume_text(token_loc, NationalStyle::None)),
'"' => return Some(self.consume_quoted_ident(token_loc).map(Into::into)),
'n' | 'N' => match self.peek_char() {
Some('\'') => {
self.consume_char_and_col();
return Some(self.consume_text(token_loc, NationalStyle::National));
}
Some('q') | Some('Q') => {
self.consume_char_and_col();
return Some(if matches!(self.peek_char(), Some('\'')) {
self.consume_char_and_col();
self.consume_quoted_text(token_loc, NationalStyle::National)
} else {
self.consume_ident_or_keyword(token_loc, token_pos)
.map(Into::into)
});
}
_ => {
return Some(
self.consume_ident_or_keyword(token_loc, token_pos)
.map(Into::into),
);
}
},
'q' | 'Q' => {
return Some(if matches!(self.peek_char(), Some('\'')) {
self.consume_char_and_col();
self.consume_quoted_text(token_loc, NationalStyle::None)
} else {
self.consume_ident_or_keyword(token_loc, token_pos)
.map(Into::into)
});
}
c if c.is_alphabetic() => {
return Some(
self.consume_ident_or_keyword(token_loc, token_pos)
.map(Into::into),
);
}
_ => {
return Some(Err(Error {
etype: ErrorType::InvalidChar {
char: c,
expected: None,
},
loc: self.next_loc,
}));
}
}
}
}
}
}
}
impl<'s> Scanner<'s> {
fn next_char(&mut self) -> Option<char> {
let c = self.chars.next()?;
self.next_pos += c.len_utf8();
Some(c)
}
fn peek_char(&mut self) -> Option<char> {
self.chars.peek().copied()
}
/// Consumes the next char
fn consume_char(&mut self) {
_ = self.next_char();
}
/// Consume the next char assuming it is not a newline; increments `self.next_loc.col`.
fn consume_char_and_col(&mut self) {
_ = self.next_char();
self.next_loc.col += 1;
}
fn curr_lexem(&self, pos: usize) -> &'s str {
&self.source[pos..self.next_pos]
}
/// Scans a number assuming a digit has been identified (and consumed) at
/// `token_pos` / `token_loc`.
fn consume_number(&mut self, token_loc: Location, token_pos: usize) -> Result<Token<'s>> {
while matches!(self.peek_char(), Some('0'..='9')) {
self.consume_char_and_col();
}
if matches!(self.peek_char(), Some('.')) {
self.consume_char_and_col();
self.consume_fraction(token_loc, token_pos)
} else {
Ok(Token {
ttype: TokenType::Integer(self.curr_lexem(token_pos)),
loc: token_loc,
})
}
}
fn consume_fraction(&mut self, token_loc: Location, token_pos: usize) -> Result<Token<'s>> {
// ~ optional
while matches!(self.peek_char(), Some('0'..='9')) {
self.consume_char_and_col();
}
// ~ optional
if matches!(self.peek_char(), Some('e') | Some('E')) {
self.consume_char_and_col();
// ~ optional
if matches!(self.peek_char(), Some('+') | Some('-')) {
self.consume_char_and_col();
}
// ~ at least one digit expected
match self.peek_char() {
None => {
return Err(Error {
etype: ErrorType::UnexpectedEof {
expected: Some("a digit"),
},
loc: self.next_loc,
});
}
Some('0'..='9') => {
self.consume_char_and_col();
}
Some(c) => {
return Err(Error {
etype: ErrorType::InvalidChar {
char: c,
expected: Some("a digit"),
},
loc: self.next_loc,
});
}
}
while matches!(self.peek_char(), Some('0'..='9')) {
self.consume_char_and_col();
}
};
// ~ optional
match self.peek_char() {
Some('f') | Some('F') => {
self.consume_char_and_col();
}
Some('d') | Some('D') => {
self.consume_char_and_col();
}
_ => {}
}
Ok(Token {
ttype: TokenType::Float(self.curr_lexem(token_pos)),
loc: token_loc,
})
}
/// Reads until "*/"
fn consume_block_comment(&mut self, token_loc: Location) -> Result<Token<'s>> {
let start_pos = self.next_pos;
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
c => {
self.next_loc.col += 1;
if c == '*' && matches!(self.peek_char(), Some('/')) {
self.consume_char_and_col();
return Ok(Token {
ttype: TokenType::Comment(Comment(
// ~ do not include the terminating "*/"
&self.source[start_pos..self.next_pos - 2],
CommentStyle::Block,
)),
loc: token_loc,
});
}
}
}
}
Err(Error {
etype: ErrorType::UnexpectedEof { expected: None },
loc: self.next_loc,
})
}
/// Reads until a newline or end-of-file
fn consume_line_comment(&mut self, token_loc: Location) -> Token<'s> {
let (start_pos, mut newline_len) = (self.next_pos, 0);
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
newline_len = 2;
} else {
newline_len = 1;
}
self.next_loc.line += 1;
self.next_loc.col = 1;
break;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
newline_len = 1;
break;
}
_ => {
self.next_loc.col += 1;
}
}
}
Token {
ttype: TokenType::Comment(Comment(
&self.source[start_pos..self.next_pos - newline_len],
CommentStyle::Line,
)),
loc: token_loc,
}
}
/// Reads a "plain" string literal, assuming the opening quote is already consumed.
fn consume_text(&mut self, token_loc: Location, encoding: NationalStyle) -> Result<Token<'s>> {
// first: loop until hitting the end of the string and then _return_
// the identified slice; however, when hitting a `''` _break_, make a
// copy of the the so-far identified string part and continue
// accumlating into a buffer with `''` replaced by one `'`.
let start_pos = self.next_pos;
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
c => {
self.next_loc.col += 1;
if c == '\'' {
if matches!(self.peek_char(), Some('\'')) {
self.consume_char_and_col();
break;
} else {
let s = self.curr_lexem(start_pos); // ~ includes the consumed `'`
return Ok(Token {
ttype: TokenType::Text(
Text::Regular(Cow::Borrowed(&s[..s.len() - 1])),
encoding,
),
loc: token_loc,
});
}
}
}
}
}
// ~ at this point the last consumed part of the string was a "''"
// which we need to turn into a single "'" ... make a copy of the
// consumed part (without the second quote) and continue accumulating
// while producing only one quote for every "''".
let mut accum = String::new();
{
let s = self.curr_lexem(start_pos);
accum.push_str(&s[..s.len() - 1]);
};
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
accum.push(c);
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
accum.push(c);
}
c => {
self.next_loc.col += 1;
match c {
'\'' => {
if matches!(self.peek_char(), Some('\'')) {
self.consume_char_and_col();
accum.push('\'');
} else {
return Ok(Token {
ttype: TokenType::Text(
Text::Regular(Cow::Owned(accum)),
encoding,
),
loc: token_loc,
});
}
}
c => accum.push(c),
}
}
}
}
Err(Error {
etype: ErrorType::UnterminatedString,
loc: token_loc,
})
}
/// Reads a "quote delimited" string literal, assuming the opening quote
/// is already consumed.
///
/// See <https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Literals.html>
fn consume_quoted_text(
&mut self,
token_loc: Location,
encoding: NationalStyle,
) -> Result<Token<'s>> {
let start_pos = self.next_pos;
let error_loc = self.next_loc;
match self.next_char() {
Some(c @ ' ') | Some(c @ '\t') | Some(c @ '\r') | Some(c @ '\n') => {
// ~ consume until the closing "'" or "{c}'" (whichever comes
// first) to allow consuming further tokens after having
// reported the error
let mut q = c;
loop {
match q {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
nc => {
self.next_loc.col += 1;
if nc == '\'' {
break;
}
}
}
match self.next_char() {
Some(c) => q = c,
None => break,
};
}
Err(Error {
etype: ErrorType::InvalidChar {
char: c,
expected: Some("a non-whitespace character"),
},
loc: error_loc,
})
}
Some(start_delim) => {
self.next_loc.col += 1;
let end_delim = match start_delim {
'{' => '}',
'[' => ']',
'<' => '>',
'(' => ')',
c => c,
};
// ~ no escaping for quote delimited strings
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
c => {
self.next_loc.col += 1;
if c == end_delim && matches!(self.peek_char(), Some('\'')) {
let s = self.curr_lexem(start_pos);
self.consume_char_and_col(); // the peeked "'"
return Ok(Token {
ttype: TokenType::Text(
Text::Quoted(QuotedText::new_unchecked(s)),
encoding,
),
loc: token_loc,
});
}
}
}
}
Err(Error {
etype: ErrorType::UnterminatedString,
loc: token_loc,
})
}
None => Err(Error {
etype: ErrorType::UnexpectedEof {
expected: Some("a non-whitespace quote delimiter character"),
},
loc: error_loc,
}),
}
}
/// Reads a quoted identifier (ie. until encountering a double quote)
/// assuming the initial double quote has been consumed.
fn consume_quoted_ident(&mut self, token_loc: Location) -> Result<ScannedIdent<'s>> {
let start_pos = self.next_pos;
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\0' => {
let error_loc = self.next_loc;
self.next_loc.col += 1;
// ~ consume until the next double quote so that we can
// continue serving more tokens
while let Some(c) = self.next_char() {
match c {
'\r' => {
if matches!(self.peek_char(), Some('\n')) {
self.consume_char();
}
self.next_loc.line += 1;
self.next_loc.col = 1;
}
'\n' => {
self.next_loc.line += 1;
self.next_loc.col = 1;
}
c => {
self.next_loc.col += 1;
if c == '"' {
break;
}
}
}
}
return Err(Error {
etype: ErrorType::InvalidChar {
char: '\0',
expected: None,
},
loc: error_loc,
});
}
'"' => {
self.next_loc.col += 1;
let s = &self.source[start_pos..self.next_pos - 1];
if s.is_empty() {
return Err(Error {
etype: ErrorType::EmptyIdent,
loc: token_loc,
});
} else {
return Ok(ScannedIdent(Ident(s, QuoteStyle::Quoted), token_loc));
}
}
_ => {
self.next_loc.col += 1;
}
}
}
Err(Error {
etype: ErrorType::UnterminatedIdent,
loc: token_loc,
})
}
/// Reads a plain / non-quoted identifier (or keyword) starting at `token_pos` assuming the
/// character at `token_pos` is alphabetic, hence a valid identifier start.
fn consume_ident_or_keyword(
&mut self,
token_loc: Location,
token_pos: usize,
) -> Result<IdentOrKeyword<'s>> {
while let Some(c) = self.peek_char() {
if c.is_alphanumeric() || c == '_' || c == '$' || c == '#' {
self.consume_char_and_col();
} else {
break;
}
}
let ident = self.curr_lexem(token_pos);
Ok(match Keyword::from_str(ident) {
Ok(kw) => IdentOrKeyword::Keyword(kw, token_loc),
Err(_) => {
IdentOrKeyword::Ident(ScannedIdent(Ident(ident, QuoteStyle::None), token_loc))
}
})
}
/// Reads a (mandatory) placeholder identifier. The leading ':' or '?' is assumed to be consumed already.
///
/// See <https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Placeholder-Expressions.html>
fn consume_placeholder(&mut self, token_loc: Location) -> Result<Token<'s>> {
let start_pos = self.next_pos;
let start_loc = self.next_loc;
let ident = match self.peek_char() {
Some(c) if c.is_alphabetic() => {
self.consume_char_and_col();
self.consume_ident_or_keyword(token_loc, start_pos)?
}
Some('"') => {
self.consume_char_and_col();
IdentOrKeyword::Ident(self.consume_quoted_ident(token_loc)?)
}
Some(c) => {
return Err(Error {
etype: ErrorType::InvalidChar {
char: c,
expected: Some("an identifier starting with an alphabetic character"),
},
loc: self.next_loc,
});
}
None => {
return Err(Error {
etype: ErrorType::UnexpectedEof {
expected: Some("an identifier starting with an alphabetic character"),
},
loc: self.next_loc,
});
}
};
Ok(match ident {
IdentOrKeyword::Ident(ident) => Token {
ttype: TokenType::Placeholder(ident.0),
loc: ident.1,
},
IdentOrKeyword::Keyword(kw, _) => {
debug_assert!(self.pushed_back.is_none());
// ~ keywords are not allowed as placeholders, so we push back
// the identified keyword for the next call to self.next() and
// serve an error right now for the invalid placeholder,
// essentially consuming the placeholders beginninig
// discriminator
let _ = self.pushed_back.insert(PushedBack {
token: Some(Ok(Token {
ttype: TokenType::Keyword(kw),
loc: start_loc,
})),
next_loc: self.next_loc,
});
// we need to restore `self.next_loc` for correct usage with
// tokens ends; the current
self.next_loc = start_loc;
return Err(Error {
etype: ErrorType::InvalidPlaceholder {
details: "placeholder name must not be a keyword",
},
loc: token_loc,
});
}
})
}
}
struct ScannedIdent<'s>(Ident<'s>, Location);
impl<'s> From<ScannedIdent<'s>> for Token<'s> {
fn from(ScannedIdent(ident, location): ScannedIdent<'s>) -> Self {
// XXX could we merge this lookup with the lookup for keywords? both have distinct variants.
let reserved = Reserved::lookup_ident(&ident).ok();
Self {
ttype: TokenType::Identifier(ident, reserved),
loc: location,
}
}
}
enum IdentOrKeyword<'s> {
Ident(ScannedIdent<'s>),
Keyword(Keyword, Location),
}
impl<'s> From<IdentOrKeyword<'s>> for Token<'s> {
fn from(value: IdentOrKeyword<'s>) -> Self {
match value {
IdentOrKeyword::Ident(ident) => ident.into(),
IdentOrKeyword::Keyword(keyword, location) => Self {
ttype: TokenType::Keyword(keyword),
loc: location,
},
}
}
}
// ----------------------------------------------------------------------------
/// A [Scanner] wrapper able to hold up to one look-ahead token allowing
/// temporary access to the wrapped scanner at the same time.
///
/// See [`Scanner::peekable`]
pub struct PeekableScanner<'s> {
inner: Scanner<'s>,
peeked: Option<Option<Result<Token<'s>>>>,
}
impl<'s> PeekableScanner<'s> {
#[inline]
pub fn inner(&self) -> &Scanner<'s> {
&self.inner
}
/// A convenience shortcut for `self.inner().location()` deliver the
/// underlying scanner's current location in its `source`. This might
/// represent a point past the current [Self::peek]ed token.
pub fn location(&self) -> Location {
self.inner.location()
}
/// Extracts a slice from the underlying source.
pub(crate) fn source_range(&self, range: Range<usize>) -> &'s str {
let s = self.source();
if range.start >= range.end || range.start >= s.len() {
""
} else {
&s[range.start..range.end.min(s.len())]
}
}
/// Retrieves the underlying source string being processed.
pub(crate) fn source(&self) -> &'s str {
self.inner.source()
}
/// Retrieves the underlying scanner's current position - a zero based
/// index into the underlying source slice.
///
/// Mind that the returned position will be already beyond the peeked
/// token, possibly at the start of the next token following the peeked
/// one.
pub(crate) fn position(&self) -> usize {
self.inner.position()
}
#[inline]
pub fn peek(&mut self) -> Option<&Result<Token<'s>>> {
self.peeked
.get_or_insert_with(|| self.inner.next())
.as_ref()
}
}
impl<'s> Iterator for PeekableScanner<'s> {
type Item = Result<Token<'s>>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.peeked.take() {
Some(v) => v,
None => self.inner.next(),
}
}
}