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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//! Lua 5.4 lexer. Operates on bytes: Lua source and strings are byte
//! sequences, not necessarily valid UTF-8.
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
// literals
Name(Box<str>),
Int(i64),
Float(f64),
Str(Box<[u8]>),
// keywords
And,
Break,
Do,
Else,
Elseif,
End,
False,
For,
Function,
Goto,
If,
In,
Local,
Nil,
Not,
Or,
Repeat,
Return,
Then,
True,
Until,
While,
// symbols
Plus,
Minus,
Star,
Slash,
DoubleSlash,
Percent,
Caret,
Hash,
Amp,
Tilde,
Pipe,
Shl,
Shr,
Eq,
Ne,
Le,
Ge,
Lt,
Gt,
Assign,
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
DoubleColon,
Semi,
Colon,
Comma,
Dot,
Concat,
Ellipsis,
Eof,
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Token::Name(n) => write!(f, "{n}"),
Token::Int(i) => write!(f, "{i}"),
Token::Float(x) => write!(f, "{x}"),
Token::Str(_) => write!(f, "string literal"),
Token::Eof => write!(f, "<eof>"),
t => {
let s = match t {
Token::And => "and",
Token::Break => "break",
Token::Do => "do",
Token::Else => "else",
Token::Elseif => "elseif",
Token::End => "end",
Token::False => "false",
Token::For => "for",
Token::Function => "function",
Token::Goto => "goto",
Token::If => "if",
Token::In => "in",
Token::Local => "local",
Token::Nil => "nil",
Token::Not => "not",
Token::Or => "or",
Token::Repeat => "repeat",
Token::Return => "return",
Token::Then => "then",
Token::True => "true",
Token::Until => "until",
Token::While => "while",
Token::Plus => "+",
Token::Minus => "-",
Token::Star => "*",
Token::Slash => "/",
Token::DoubleSlash => "//",
Token::Percent => "%",
Token::Caret => "^",
Token::Hash => "#",
Token::Amp => "&",
Token::Tilde => "~",
Token::Pipe => "|",
Token::Shl => "<<",
Token::Shr => ">>",
Token::Eq => "==",
Token::Ne => "~=",
Token::Le => "<=",
Token::Ge => ">=",
Token::Lt => "<",
Token::Gt => ">",
Token::Assign => "=",
Token::LParen => "(",
Token::RParen => ")",
Token::LBrace => "{",
Token::RBrace => "}",
Token::LBracket => "[",
Token::RBracket => "]",
Token::DoubleColon => "::",
Token::Semi => ";",
Token::Colon => ":",
Token::Comma => ",",
Token::Dot => ".",
Token::Concat => "..",
Token::Ellipsis => "...",
_ => unreachable!(),
};
write!(f, "{s}")
}
}
}
}
#[derive(Debug, Clone)]
pub struct LexError {
pub message: String,
pub line: u32,
}
impl fmt::Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "lex error at line {}: {}", self.line, self.message)
}
}
#[derive(Clone)]
pub struct Lexer<'a> {
src: &'a [u8],
pos: usize,
pub line: u32,
}
fn keyword(s: &str) -> Option<Token> {
Some(match s {
"and" => Token::And,
"break" => Token::Break,
"do" => Token::Do,
"else" => Token::Else,
"elseif" => Token::Elseif,
"end" => Token::End,
"false" => Token::False,
"for" => Token::For,
"function" => Token::Function,
"goto" => Token::Goto,
"if" => Token::If,
"in" => Token::In,
"local" => Token::Local,
"nil" => Token::Nil,
"not" => Token::Not,
"or" => Token::Or,
"repeat" => Token::Repeat,
"return" => Token::Return,
"then" => Token::Then,
"true" => Token::True,
"until" => Token::Until,
"while" => Token::While,
_ => return None,
})
}
impl<'a> Lexer<'a> {
#[must_use]
pub fn new(src: &'a [u8]) -> Self {
Lexer {
src,
pos: 0,
line: 1,
}
}
fn err<T>(&self, message: impl Into<String>) -> Result<T, LexError> {
Err(LexError {
message: message.into(),
line: self.line,
})
}
fn peek(&self) -> Option<u8> {
self.src.get(self.pos).copied()
}
fn peek2(&self) -> Option<u8> {
self.src.get(self.pos + 1).copied()
}
fn bump(&mut self) -> Option<u8> {
let b = self.peek()?;
self.pos += 1;
Some(b)
}
/// Consumes a newline (\n, \r, \r\n or \n\r), incrementing the line count once.
fn newline(&mut self) {
let first = self.bump().unwrap();
if let Some(b) = self.peek()
&& (b == b'\n' || b == b'\r')
&& b != first
{
self.pos += 1;
}
self.line += 1;
}
fn skip_whitespace_and_comments(&mut self) -> Result<(), LexError> {
loop {
match self.peek() {
Some(b' ' | b'\t' | 0x0b | 0x0c) => {
self.pos += 1;
}
Some(b'\n' | b'\r') => self.newline(),
Some(b'-') if self.peek2() == Some(b'-') => {
self.pos += 2;
// long comment?
if self.peek() == Some(b'[')
&& let Some(level) = self.long_bracket_level()
{
self.read_long_string(level)?;
continue;
}
// line comment
while let Some(b) = self.peek() {
if b == b'\n' || b == b'\r' {
break;
}
self.pos += 1;
}
}
_ => return Ok(()),
}
}
}
/// If positioned at `[`, checks for a long-bracket opener `[=*[`.
/// On match, consumes it and returns the level; otherwise consumes nothing.
fn long_bracket_level(&mut self) -> Option<usize> {
debug_assert_eq!(self.peek(), Some(b'['));
let mut i = self.pos + 1;
let mut level = 0;
while self.src.get(i) == Some(&b'=') {
level += 1;
i += 1;
}
if self.src.get(i) == Some(&b'[') {
self.pos = i + 1;
Some(level)
} else {
None
}
}
/// Reads a long string body up to and including the closing `]=*]`.
/// The opening bracket must already be consumed.
fn read_long_string(&mut self, level: usize) -> Result<Box<[u8]>, LexError> {
// first newline is skipped
if let Some(b'\n' | b'\r') = self.peek() {
self.newline();
}
let mut out = Vec::new();
loop {
match self.peek() {
None => return self.err_eof("unfinished long string/comment"),
Some(b']') => {
let mut i = self.pos + 1;
let mut l = 0;
while self.src.get(i) == Some(&b'=') {
l += 1;
i += 1;
}
if l == level && self.src.get(i) == Some(&b']') {
self.pos = i + 1;
return Ok(out.into_boxed_slice());
}
out.push(b']');
self.pos += 1;
}
Some(b'\n' | b'\r') => {
self.newline();
out.push(b'\n');
}
Some(b) => {
out.push(b);
self.pos += 1;
}
}
}
}
/// Raw source text of the token starting at `start` up to `end`, used for
/// PUC Lua's `near '<token text>'` error suffix.
fn near_text(&self, start: usize, end: usize) -> String {
let end = end.min(self.src.len());
String::from_utf8_lossy(&self.src[start..end]).into_owned()
}
/// Errors with a PUC-style `near '<token text>'` suffix.
fn err_near<T>(
&self,
start: usize,
end: usize,
message: impl Into<String>,
) -> Result<T, LexError> {
Err(LexError {
message: format!("{} near '{}'", message.into(), self.near_text(start, end)),
line: self.line,
})
}
/// Errors at end of input, where PUC reports the token as `<eof>`.
fn err_eof<T>(&self, message: impl Into<String>) -> Result<T, LexError> {
self.err(format!("{} near <eof>", message.into()))
}
fn read_short_string(&mut self, quote: u8, start: usize) -> Result<Box<[u8]>, LexError> {
let mut out = Vec::new();
loop {
let Some(b) = self.bump() else {
return self.err_eof("unfinished string");
};
match b {
b'\n' | b'\r' => return self.err_near(start, self.pos - 1, "unfinished string"),
b'\\' => {
let Some(e) = self.bump() else {
return self.err_eof("unfinished string");
};
match e {
b'a' => out.push(7),
b'b' => out.push(8),
b'f' => out.push(12),
b'n' => out.push(b'\n'),
b'r' => out.push(b'\r'),
b't' => out.push(b'\t'),
b'v' => out.push(11),
b'\\' => out.push(b'\\'),
b'"' => out.push(b'"'),
b'\'' => out.push(b'\''),
b'\n' | b'\r' => {
self.pos -= 1;
self.newline();
out.push(b'\n');
}
b'x' => {
let mut v: u32 = 0;
for _ in 0..2 {
match self.bump().and_then(|c| (c as char).to_digit(16)) {
Some(d) => v = v * 16 + d,
None => {
return self.err_near(
start,
self.pos,
"hexadecimal digit expected",
);
}
}
}
out.push(v as u8);
}
b'0'..=b'9' => {
let mut v: u32 = (e - b'0') as u32;
for _ in 0..2 {
match self.peek() {
Some(c @ b'0'..=b'9') => {
v = v * 10 + (c - b'0') as u32;
self.pos += 1;
}
_ => break,
}
}
if v > 255 {
// PUC folds the character that ended the digit run
// into the `near` text.
if self.peek().is_some() {
self.pos += 1;
}
return self.err_near(start, self.pos, "decimal escape too large");
}
out.push(v as u8);
}
b'z' => loop {
match self.peek() {
Some(b' ' | b'\t' | 0x0b | 0x0c) => self.pos += 1,
Some(b'\n' | b'\r') => self.newline(),
_ => break,
}
},
b'u' => {
if self.bump() != Some(b'{') {
return self.err_near(start, self.pos, "missing '{'");
}
let mut v: u64 = 0;
let mut any = false;
while let Some(d) = self.peek().and_then(|c| (c as char).to_digit(16)) {
v = v * 16 + d as u64;
self.pos += 1;
if v > 0x7FFF_FFFF {
return self.err_near(start, self.pos, "UTF-8 value too large");
}
any = true;
}
if !any {
// Include the offending character in `near`.
if self.peek().is_some() {
self.pos += 1;
}
return self.err_near(
start,
self.pos,
"hexadecimal digit expected",
);
}
if self.bump() != Some(b'}') {
return self.err_near(start, self.pos, "missing '}'");
}
push_utf8(&mut out, v as u32);
}
_ => return self.err_near(start, self.pos, "invalid escape sequence"),
}
}
_ if b == quote => return Ok(out.into_boxed_slice()),
_ => out.push(b),
}
}
}
fn read_number(&mut self) -> Result<Token, LexError> {
let start = self.pos;
let hex = self.peek() == Some(b'0')
&& matches!(self.peek2(), Some(b'x' | b'X'))
&& self
.src
.get(self.pos + 2)
.is_some_and(|c| c.is_ascii_hexdigit() || *c == b'.');
if hex {
self.pos += 2;
let mut is_float = false;
while let Some(b) = self.peek() {
match b {
_ if b.is_ascii_hexdigit() => self.pos += 1,
b'.' => {
is_float = true;
self.pos += 1;
}
b'p' | b'P' => {
is_float = true;
self.pos += 1;
if matches!(self.peek(), Some(b'+' | b'-')) {
self.pos += 1;
}
}
_ => break,
}
}
let text = std::str::from_utf8(&self.src[start..self.pos]).unwrap();
if is_float {
match parse_hex_float(&text[2..]) {
Some(f) => Ok(Token::Float(f)),
None => self.err("malformed number"),
}
} else {
// hex integer constants wrap around (Lua 5.4)
let mut v: u64 = 0;
for c in text[2..].bytes() {
v = v
.wrapping_mul(16)
.wrapping_add((c as char).to_digit(16).unwrap() as u64);
}
Ok(Token::Int(v as i64))
}
} else {
let mut is_float = false;
while let Some(b) = self.peek() {
match b {
b'0'..=b'9' => self.pos += 1,
b'.' => {
is_float = true;
self.pos += 1;
}
b'e' | b'E' => {
is_float = true;
self.pos += 1;
if matches!(self.peek(), Some(b'+' | b'-')) {
self.pos += 1;
}
}
_ => break,
}
}
if self
.peek()
.is_some_and(|b| b.is_ascii_alphanumeric() || b == b'_')
{
return self.err("malformed number");
}
let text = std::str::from_utf8(&self.src[start..self.pos]).unwrap();
if is_float {
text.parse::<f64>()
.map(Token::Float)
.or_else(|_| self.err("malformed number"))
} else {
// decimal integer constants that overflow become floats (Lua 5.4)
match text.parse::<i64>() {
Ok(i) => Ok(Token::Int(i)),
Err(_) => text
.parse::<f64>()
.map(Token::Float)
.or_else(|_| self.err("malformed number")),
}
}
}
}
/// Lexes the next token, returning it with the line it started on.
///
/// # Errors
///
/// Returns a [`LexError`] on malformed input, e.g. an unterminated string
/// or long bracket, or a malformed number or escape sequence.
///
/// # Panics
///
/// Panics if an identifier byte range is not valid UTF-8; the range is
/// built only from ASCII alphanumerics and `_`, so it always is.
pub fn next_token(&mut self) -> Result<(Token, u32), LexError> {
self.skip_whitespace_and_comments()?;
let line = self.line;
let Some(b) = self.peek() else {
return Ok((Token::Eof, line));
};
let tok = match b {
b'A'..=b'Z' | b'a'..=b'z' | b'_' => {
let start = self.pos;
while self
.peek()
.is_some_and(|c| c.is_ascii_alphanumeric() || c == b'_')
{
self.pos += 1;
}
let s = std::str::from_utf8(&self.src[start..self.pos]).unwrap();
keyword(s).unwrap_or_else(|| Token::Name(s.into()))
}
b'0'..=b'9' => self.read_number()?,
b'.' if self.peek2().is_some_and(|c| c.is_ascii_digit()) => self.read_number()?,
b'"' | b'\'' => {
let start = self.pos;
self.pos += 1;
Token::Str(self.read_short_string(b, start)?)
}
b'[' => {
if let Some(level) = self.long_bracket_level() {
Token::Str(self.read_long_string(level)?)
} else {
self.pos += 1;
Token::LBracket
}
}
_ => {
self.pos += 1;
match b {
b'+' => Token::Plus,
b'-' => Token::Minus,
b'*' => Token::Star,
b'/' => {
if self.peek() == Some(b'/') {
self.pos += 1;
Token::DoubleSlash
} else {
Token::Slash
}
}
b'%' => Token::Percent,
b'^' => Token::Caret,
b'#' => Token::Hash,
b'&' => Token::Amp,
b'~' => {
if self.peek() == Some(b'=') {
self.pos += 1;
Token::Ne
} else {
Token::Tilde
}
}
b'|' => Token::Pipe,
b'<' => match self.peek() {
Some(b'<') => {
self.pos += 1;
Token::Shl
}
Some(b'=') => {
self.pos += 1;
Token::Le
}
_ => Token::Lt,
},
b'>' => match self.peek() {
Some(b'>') => {
self.pos += 1;
Token::Shr
}
Some(b'=') => {
self.pos += 1;
Token::Ge
}
_ => Token::Gt,
},
b'=' => {
if self.peek() == Some(b'=') {
self.pos += 1;
Token::Eq
} else {
Token::Assign
}
}
b'(' => Token::LParen,
b')' => Token::RParen,
b'{' => Token::LBrace,
b'}' => Token::RBrace,
b']' => Token::RBracket,
b';' => Token::Semi,
b':' => {
if self.peek() == Some(b':') {
self.pos += 1;
Token::DoubleColon
} else {
Token::Colon
}
}
b',' => Token::Comma,
b'.' => {
if self.peek() == Some(b'.') {
self.pos += 1;
if self.peek() == Some(b'.') {
self.pos += 1;
Token::Ellipsis
} else {
Token::Concat
}
} else {
Token::Dot
}
}
_ => return self.err(format!("unexpected symbol near '{}'", b as char)),
}
}
};
Ok((tok, line))
}
}
/// Lua's extended UTF-8: encodes values up to 0x7FFFFFFF, up to 6 bytes.
fn push_utf8(out: &mut Vec<u8>, v: u32) {
if v < 0x80 {
out.push(v as u8);
return;
}
let mut buf = [0u8; 6];
let mut n = 0; // continuation bytes written (reversed)
let mut mfs: u32 = 0x3F; // max value that fits in the first byte for n continuations
let mut v = v;
loop {
buf[n] = 0x80 | (v & 0x3F) as u8;
n += 1;
v >>= 6;
mfs >>= 1;
if v <= mfs {
break;
}
}
// first byte: n+1 leading ones
let first = (!mfs << 1) as u8 | v as u8;
out.push(first);
for i in (0..n).rev() {
out.push(buf[i]);
}
}
/// Parses a hex float (without the `0x` prefix): hex digits, optional `.`,
/// optional binary exponent `p±d`.
fn parse_hex_float(s: &str) -> Option<f64> {
let mut mantissa: f64 = 0.0;
let mut exp: i32 = 0;
let mut seen_digit = false;
let mut seen_dot = false;
let mut chars = s.bytes().peekable();
while let Some(&c) = chars.peek() {
match c {
b'.' => {
if seen_dot {
return None;
}
seen_dot = true;
chars.next();
}
_ if c.is_ascii_hexdigit() => {
mantissa = mantissa * 16.0 + (c as char).to_digit(16).unwrap() as f64;
if seen_dot {
exp -= 4;
}
seen_digit = true;
chars.next();
}
_ => break,
}
}
if !seen_digit {
return None;
}
match chars.next() {
None => {}
Some(b'p' | b'P') => {
let mut sign = 1i32;
match chars.peek() {
Some(b'+') => {
chars.next();
}
Some(b'-') => {
sign = -1;
chars.next();
}
_ => {}
}
let mut e: i32 = 0;
let mut any = false;
for c in chars {
if !c.is_ascii_digit() {
return None;
}
e = e.saturating_mul(10).saturating_add((c - b'0') as i32);
any = true;
}
if !any {
return None;
}
exp += sign * e;
}
Some(_) => return None,
}
Some(mantissa * (exp as f64).exp2())
}
#[cfg(test)]
mod tests {
use super::*;
fn lex(src: &str) -> Vec<Token> {
let mut l = Lexer::new(src.as_bytes());
let mut out = Vec::new();
loop {
let (t, _) = l.next_token().unwrap();
if t == Token::Eof {
return out;
}
out.push(t);
}
}
/// Lexes `src` expecting a failure and returns the error message.
fn lex_err(src: &str) -> String {
let mut l = Lexer::new(src.as_bytes());
loop {
match l.next_token() {
Ok((Token::Eof, _)) => panic!("expected lex error: {src:?}"),
Ok(_) => {}
Err(e) => return e.message,
}
}
}
#[test]
fn names_keywords_symbols() {
assert_eq!(
lex("local x = y0 // 2"),
vec![
Token::Local,
Token::Name("x".into()),
Token::Assign,
Token::Name("y0".into()),
Token::DoubleSlash,
Token::Int(2),
]
);
assert_eq!(
lex(".. ... . :: : ~= ~ << <= <"),
vec![
Token::Concat,
Token::Ellipsis,
Token::Dot,
Token::DoubleColon,
Token::Colon,
Token::Ne,
Token::Tilde,
Token::Shl,
Token::Le,
Token::Lt,
]
);
}
#[test]
#[expect(clippy::approx_constant)] // 3.1416 is the Lua manual's example
fn numbers() {
assert_eq!(lex("3"), vec![Token::Int(3)]);
assert_eq!(lex("345"), vec![Token::Int(345)]);
assert_eq!(lex("0xff"), vec![Token::Int(255)]);
assert_eq!(lex("0xBEBADA"), vec![Token::Int(0x00BE_BADA)]);
assert_eq!(lex("3.0"), vec![Token::Float(3.0)]);
assert_eq!(lex("3.1416"), vec![Token::Float(3.1416)]);
assert_eq!(lex("314.16e-2"), vec![Token::Float(3.1416)]);
assert_eq!(lex("0.31416E1"), vec![Token::Float(3.1416)]);
assert_eq!(lex("34e1"), vec![Token::Float(340.0)]);
assert_eq!(lex("0x0.1E"), vec![Token::Float(0.117_187_5)]);
assert_eq!(lex("0xA23p-4"), vec![Token::Float(162.1875)]);
assert_eq!(
lex("0X1.921FB54442D18P+1"),
vec![Token::Float(std::f64::consts::PI)]
);
// decimal overflow -> float; hex overflow -> wraps
assert_eq!(
lex("9223372036854775808"),
vec![Token::Float(9.223_372_036_854_776e18)]
);
assert_eq!(lex("0xFFFFFFFFFFFFFFFF"), vec![Token::Int(-1)]);
assert_eq!(lex(".5"), vec![Token::Float(0.5)]);
}
#[test]
fn strings() {
assert_eq!(
lex(r#""hello""#),
vec![Token::Str(b"hello".to_vec().into())]
);
assert_eq!(
lex(r"'a\n\t\\\'b'"),
vec![Token::Str(b"a\n\t\\'b".to_vec().into())]
);
assert_eq!(
lex(r#""\x41\65\66""#),
vec![Token::Str(b"AAB".to_vec().into())]
);
assert_eq!(
lex(r#""\u{48}\u{65}""#),
vec![Token::Str(b"He".to_vec().into())]
);
assert_eq!(
lex(r#""\u{20AC}""#),
vec![Token::Str("€".as_bytes().to_vec().into())]
);
assert_eq!(
lex("\"a\\z \n b\""),
vec![Token::Str(b"ab".to_vec().into())]
);
assert_eq!(
lex("[[long\nstring]]"),
vec![Token::Str(b"long\nstring".to_vec().into())]
);
assert_eq!(
lex("[==[a]=]b]==]"),
vec![Token::Str(b"a]=]b".to_vec().into())]
);
assert_eq!(
lex("[[\nskipped]]"),
vec![Token::Str(b"skipped".to_vec().into())]
);
}
#[test]
fn comments() {
assert_eq!(
lex("a -- comment\nb"),
vec![Token::Name("a".into()), Token::Name("b".into())]
);
assert_eq!(
lex("a --[==[ long\ncomment ]==] b"),
vec![Token::Name("a".into()), Token::Name("b".into())]
);
}
#[test]
fn line_tracking() {
let mut l = Lexer::new(b"a\nb\r\nc");
assert_eq!(l.next_token().unwrap().1, 1);
assert_eq!(l.next_token().unwrap().1, 2);
assert_eq!(l.next_token().unwrap().1, 3);
}
#[test]
fn errors() {
let mut l = Lexer::new(b"\"unfinished");
assert!(l.next_token().is_err());
let mut l = Lexer::new(b"[[unfinished");
assert!(l.next_token().is_err());
let mut l = Lexer::new(b"3a");
assert!(l.next_token().is_err());
}
#[test]
fn malformed_escapes_report_puc_near_text() {
// PUC appends `near '<source text>'` naming the malformed token.
assert_eq!(
lex_err(r#""\x""#),
r#"hexadecimal digit expected near '"\x"'"#
);
assert_eq!(
lex_err(r#""\xG""#),
r#"hexadecimal digit expected near '"\xG'"#
);
// \u{...} diagnostics
assert_eq!(lex_err(r#""\u""#), r#"missing '{' near '"\u"'"#);
assert_eq!(
lex_err(r#""\u{}""#),
r#"hexadecimal digit expected near '"\u{}'"#
);
assert_eq!(lex_err(r#""\u{48""#), r#"missing '}' near '"\u{48"'"#);
assert_eq!(
lex_err(r#""\u{110000000}""#),
r#"UTF-8 value too large near '"\u{110000000'"#
);
// oversized decimal escapes
assert_eq!(
lex_err(r#""\256""#),
r#"decimal escape too large near '"\256"'"#
);
assert_eq!(
lex_err(r#""\999""#),
r#"decimal escape too large near '"\999"'"#
);
// an unknown escape names the offending character
assert_eq!(lex_err(r#""\q""#), r#"invalid escape sequence near '"\q'"#);
}
#[test]
fn unfinished_strings_report_near_text_or_eof() {
// Hitting a newline reports the text consumed so far.
assert_eq!(lex_err("\"a\nb\""), "unfinished string near '\"a'");
assert_eq!(lex_err("'x\rY'"), "unfinished string near ''x'");
// Running off the end of input reports `<eof>`.
assert_eq!(lex_err("\"abc"), "unfinished string near <eof>");
assert_eq!(lex_err("\"abc\\"), "unfinished string near <eof>");
assert_eq!(
lex_err("[[abc"),
"unfinished long string/comment near <eof>"
);
}
}