rscel 1.0.8

Cel interpreter in rust
Documentation
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
use super::{
    source_location::SourceLocation,
    source_range::SourceRange,
    string_scanner::StringScanner,
    syntax_error::SyntaxError,
    tokenizer::{TokenWithLoc, Tokenizer},
    tokens::{FStringSegment, Token},
};

pub struct StringTokenizer<'l> {
    scanner: StringScanner<'l>,

    current: Option<TokenWithLoc>,

    eof: bool,
}

impl<'l> StringTokenizer<'l> {
    pub fn with_input(input: &'l str) -> StringTokenizer<'l> {
        StringTokenizer {
            scanner: StringScanner::from_input(input),
            current: None,
            eof: false,
        }
    }

    fn collect_next_token(&mut self) -> Result<Option<TokenWithLoc>, SyntaxError> {
        let mut tmp = [0; 4];
        let mut token_start = self.location();
        let mut curr_char = self.scanner.next();

        if self.eof {
            return Ok(None);
        }

        'outer: loop {
            match curr_char {
                Some(' ') | Some('\t') | Some('\n') => {
                    token_start = self.location();
                    curr_char = self.scanner.next();
                }
                _ => break 'outer,
            };
        }

        let res = if let Some(input_char) = curr_char {
            match input_char {
                '?' => Ok(Some(Token::Question)),
                ':' => Ok(Some(Token::Colon)),
                '+' => Ok(Some(Token::Add)),
                '-' => Ok(Some(Token::Minus)),
                '*' => Ok(Some(Token::Multiply)),
                '/' => Ok(Some(Token::Divide)),
                '%' => Ok(Some(Token::Mod)),
                '!' => match self.scanner.peek() {
                    Some('=') => {
                        self.scanner.next();
                        Ok(Some(Token::NotEqual))
                    }
                    _ => Ok(Some(Token::Not)),
                },
                '.' => {
                    if let Some(v) = self.scanner.peek() {
                        if v >= '0' && v <= '9' {
                            self.parse_number_or_token(input_char.encode_utf8(&mut tmp))
                        } else {
                            Ok(Some(Token::Dot))
                        }
                    } else {
                        Ok(Some(Token::Dot))
                    }
                }
                ',' => Ok(Some(Token::Comma)),
                '[' => Ok(Some(Token::LBracket)),
                ']' => Ok(Some(Token::RBracket)),
                '{' => Ok(Some(Token::LBrace)),
                '}' => Ok(Some(Token::RBrace)),
                '(' => Ok(Some(Token::LParen)),
                ')' => Ok(Some(Token::RParen)),
                '<' => match self.scanner.peek() {
                    Some('=') => {
                        self.scanner.next();
                        Ok(Some(Token::LessEqual))
                    }
                    _ => Ok(Some(Token::LessThan)),
                },
                '>' => match self.scanner.peek() {
                    Some('=') => {
                        self.scanner.next();
                        Ok(Some(Token::GreaterEqual))
                    }
                    _ => Ok(Some(Token::GreaterThan)),
                },
                '=' => match self.scanner.peek() {
                    Some('=') => {
                        self.scanner.next();
                        Ok(Some(Token::EqualEqual))
                    }
                    _ => Err(SyntaxError::from_location(self.scanner.location())
                        .with_message("Token = is not supported".to_string())),
                },
                '|' => match self.scanner.peek() {
                    Some('|') => {
                        self.scanner.next();
                        Ok(Some(Token::OrOr))
                    }
                    _ => Err(SyntaxError::from_location(self.scanner.location())
                        .with_message("Token | is not supported".to_string())),
                },
                '&' => match self.scanner.peek() {
                    Some('&') => {
                        self.scanner.next();
                        Ok(Some(Token::AndAnd))
                    }
                    _ => Err(SyntaxError::from_location(self.scanner.location())
                        .with_message("Token & is not supported".to_string())),
                },
                'b' => {
                    if let Some('\'') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_bytes_literal('\'')
                    } else if let Some('"') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_bytes_literal('"')
                    } else {
                        self.parse_keywords_or_ident("b", &[])
                    }
                }
                'c' => self.parse_keywords_or_ident("c", &[("case", Token::Case)]),
                'f' => {
                    if let Some('\'') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_string_literal('\'', false, true)
                    } else if let Some('"') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_string_literal('"', false, true)
                    } else {
                        self.parse_keywords_or_ident("f", &[("false", Token::BoolLit(false))])
                    }
                }
                'i' => self.parse_keywords_or_ident("i", &[("in", Token::In)]),
                'm' => self.parse_keywords_or_ident("m", &[("match", Token::Match)]),
                'n' => self.parse_keywords_or_ident("n", &[("null", Token::Null)]),
                'r' => {
                    if let Some('\'') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_string_literal('\'', true, false)
                    } else if let Some('"') = self.scanner.peek() {
                        self.scanner.next();
                        self.parse_string_literal('"', true, false)
                    } else {
                        self.parse_keywords_or_ident("r", &[])
                    }
                }
                't' => self.parse_keywords_or_ident("t", &[("true", Token::BoolLit(true))]),
                '0'..='9' => self.parse_number_or_token(input_char.encode_utf8(&mut tmp)),
                '\'' | '"' => self.parse_string_literal(input_char, false, false),
                '_' | 'A'..='Z' | 'a'..='z' => {
                    self.parse_keywords_or_ident(&input_char.to_string(), &[])
                }
                other => {
                    return Err(SyntaxError::from_location(self.scanner.location())
                        .with_message(format!("Unexpected symbol: '{}'", other)));
                }
            }
        } else {
            self.eof = true;
            Ok(None)
        };

        #[cfg(feature = "debug_output")]
        {
            if let Ok(Some(ref val)) = res {
                println!("[tokenizer]: collect {:?}", val);
            } else if let Ok(None) = res {
                println!("[tokenizer]: EOF");
            } else if let Err(ref err) = res {
                println!("[tokenizer]: {:?}", err);
            }
        }

        res.map(|o| o.map(|t| TokenWithLoc::new(t, SourceRange::new(token_start, self.location()))))
    }

    fn parse_bytes_literal(&mut self, starting: char) -> Result<Option<Token>, SyntaxError> {
        let mut buf = [0u8; 4];
        let mut working = Vec::new();

        'outer: loop {
            let curr = if let Some(curr) = self.scanner.next() {
                curr
            } else {
                return Err(SyntaxError::from_location(self.scanner.location()));
            };

            if curr == starting {
                break 'outer;
            } else if curr == '\\' {
                let escaped = if let Some(curr) = self.scanner.next() {
                    curr
                } else {
                    return Err(SyntaxError::from_location(self.scanner.location()));
                };

                match escaped {
                    'a' => working.push(0x07u8),
                    'b' => working.push(0x08u8),
                    'f' => working.push(0x0cu8),
                    'n' => working.push('\n' as u8),
                    'r' => working.push('\r' as u8),
                    't' => working.push('\t' as u8),
                    'v' => working.push(0x0bu8),
                    'x' => working.push(self.extract_hex_val(2)? as u8),
                    'X' => working.push(self.extract_hex_val(2)? as u8),
                    '\\' => working.push('\\' as u8),
                    '\'' => working.push('\'' as u8),
                    '"' => working.push('"' as u8),
                    '0'..='9' => {
                        let mut oct: String = [escaped].into_iter().collect();
                        for _ in 0..2 {
                            match self.scanner.next() {
                                Some(c) => oct.push(c),
                                None => {
                                    return Err(SyntaxError::from_location(self.scanner.location())
                                        .with_message(format!("Octal number requires 3 digits")))
                                }
                            }
                        }

                        let val = match u8::from_str_radix(&oct, 8) {
                            Ok(v) => v,
                            Err(_) => {
                                return Err(SyntaxError::from_location(self.scanner.location())
                                    .with_message(format!("{} is not a valid octal number", oct)))
                            }
                        };

                        working.push(val)
                    }
                    other => {
                        other.encode_utf8(&mut buf);
                        working.extend_from_slice(&buf[..other.len_utf8()]);
                    }
                }
            } else {
                curr.encode_utf8(&mut buf);
                working.extend_from_slice(&buf[..curr.len_utf8()]);
            }
        }

        Ok(Some(Token::ByteStringLit(working.into())))
    }

    fn parse_string_literal(
        &mut self,
        starting: char,
        is_raw: bool,
        is_format: bool,
    ) -> Result<Option<Token>, SyntaxError> {
        let mut working = String::new();
        let mut segments = Vec::new();

        'outer: loop {
            let curr = if let Some(curr) = self.scanner.next() {
                curr
            } else {
                return Err(SyntaxError::from_location(self.scanner.location()));
            };

            if curr == starting {
                break 'outer;
            } else if curr == '\\' && !is_raw {
                let escaped = if let Some(curr) = self.scanner.next() {
                    curr
                } else {
                    return Err(SyntaxError::from_location(self.scanner.location()));
                };

                match escaped {
                    'a' => working.push(0x07 as char),
                    'b' => working.push(0x08 as char),
                    'f' => working.push(0x0c as char),
                    'n' => working.push('\n'),
                    'r' => working.push('\r'),
                    't' => working.push('\t'),
                    'u' => working.push(self.extract_hex_val(4)?),
                    'U' => working.push(self.extract_hex_val(8)?),
                    'v' => working.push(0x0b as char),
                    'x' => working.push(self.extract_hex_val(2)?),
                    'X' => working.push(self.extract_hex_val(2)?),
                    '\\' => working.push('\\'),
                    '\'' => working.push('\''),
                    '"' => working.push('"'),
                    '0'..='9' => {
                        let mut oct: String = [escaped].into_iter().collect();
                        for _ in 0..2 {
                            match self.scanner.next() {
                                Some(c) => oct.push(c),
                                None => {
                                    return Err(SyntaxError::from_location(self.scanner.location())
                                        .with_message(format!("Octal number requires 3 digits")))
                                }
                            }
                        }

                        let val = match u32::from_str_radix(&oct, 8) {
                            Ok(v) => v,
                            Err(_) => {
                                return Err(SyntaxError::from_location(self.scanner.location())
                                    .with_message(format!("{} is not a valid octal number", oct)))
                            }
                        };

                        working.push(match char::from_u32(val) {
                            Some(c) => c,
                            None => {
                                return Err(SyntaxError::from_location(self.scanner.location())
                                    .with_message(format!("Invalid code point {}", val)))
                            }
                        })
                    }
                    other => working.push(other),
                }
            } else if curr == '{' && is_format {
                let escaped = if let Some(curr) = self.scanner.next() {
                    curr
                } else {
                    return Err(SyntaxError::from_location(self.scanner.location()));
                };

                match escaped {
                    '{' => working.push('{'),
                    c => {
                        if !working.is_empty() {
                            segments.push(FStringSegment::Lit(working));
                            working = String::new();
                        }

                        if c == '}' {
                            return Err(SyntaxError::from_location(self.scanner.location())
                                .with_message("Empty format specifier".to_string()));
                        }

                        working.push(c);
                        let mut bracket_count = 1;
                        while bracket_count > 0 {
                            if let Some(c) = self.scanner.next() {
                                match c {
                                    '}' => {
                                        bracket_count -= 1;
                                        if bracket_count > 0 {
                                            working.push('}');
                                        }
                                    }
                                    '{' => {
                                        bracket_count += 1;
                                        working.push('{');
                                    }
                                    other => working.push(other),
                                }
                            } else {
                                return Err(SyntaxError::from_location(self.scanner.location()));
                            }
                        }

                        if working.is_empty() {
                            return Err(SyntaxError::from_location(self.scanner.location()));
                        }

                        segments.push(FStringSegment::Expr(working));
                        working = String::new();
                    }
                }
            } else if curr == '}' && is_format {
                let escaped = if let Some(curr) = self.scanner.next() {
                    curr
                } else {
                    return Err(SyntaxError::from_location(self.scanner.location()));
                };

                if escaped == '}' {
                    working.push('}');
                } else {
                    return Err(SyntaxError::from_location(self.scanner.location())
                        .with_message("Single } not allowed".to_string()));
                }
            } else {
                working.push(curr);
            }
        }

        if segments.is_empty() {
            Ok(Some(Token::StringLit(working)))
        } else {
            if !working.is_empty() {
                segments.push(FStringSegment::Lit(working))
            }
            Ok(Some(Token::FStringLit(segments)))
        }
    }

    fn parse_keywords_or_ident(
        &mut self,
        starting: &str,
        options: &[(&str, Token)],
    ) -> Result<Option<Token>, SyntaxError> {
        let mut working = starting.to_owned();

        'outer: loop {
            if let Some(next) = self.scanner.peek() {
                match next {
                    'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {
                        working.push(next);
                        self.scanner.next();
                    }
                    _ => break 'outer,
                }
            } else {
                break 'outer;
            }
        }

        if let Some(ident) = options.iter().find(|x| x.0 == working) {
            return Ok(Some(ident.1.clone()));
        } else {
            return Ok(Some(Token::Ident(working)));
        }
    }

    fn parse_number_or_token(&mut self, starting: &str) -> Result<Option<Token>, SyntaxError> {
        let mut working = starting.to_owned();
        let mut is_float = starting.contains(".");
        let mut is_exp = false;
        let mut is_unsigned = false;
        let mut base = 10;

        'outer: loop {
            if let Some(next) = self.scanner.peek() {
                match next {
                    '0' => {
                        working.push(next);
                        self.scanner.next();
                    }
                    '1'..='9' => {
                        working.push(next);
                        self.scanner.next();
                    }
                    'e' | 'E' | '.' => {
                        if next == '.' && is_float {
                            break 'outer;
                        } else if is_exp {
                            break 'outer;
                        }

                        is_float = true;

                        if next == 'e' || next == 'E' {
                            is_exp = true;
                        }
                        working.push(next);

                        self.scanner.next();
                        if let Some(p) = self.scanner.peek() {
                            match p {
                                '+' | '-' => {
                                    self.scanner.next();
                                    working.push(p);
                                }
                                _ => {
                                    continue 'outer;
                                }
                            }
                        }
                    }
                    'u' | 'U' => {
                        if is_float {
                            break 'outer;
                        }

                        is_unsigned = true;
                        self.scanner.next();
                        break 'outer;
                    }
                    'x' | 'X' => {
                        if working == "0" && base == 10 {
                            working.push('x');
                            self.scanner.next();
                            base = 16;
                        } else {
                            break 'outer;
                        }
                    }
                    _ => break 'outer,
                };
            } else {
                break 'outer;
            }
        }

        let orig = working.clone();
        let fixedup_str = match base {
            10 => &working,
            16 => working.trim_start_matches("0x"),
            _ => return Err(SyntaxError::from_location(self.scanner.location())),
        };

        if is_unsigned {
            // u64 is always positive and doesn't have a neg rep that cannot be represeted in a u64
            match u64::from_str_radix(fixedup_str, base) {
                Ok(val) => Ok(Some(Token::UIntLit(val))),
                Err(_) => Err(SyntaxError::from_location(self.scanner.location())
                    .with_message(format!("Failed to parse unsigned int {}", orig))),
            }
        } else if is_float {
            match working.parse::<f64>() {
                Ok(v) => Ok(Some(Token::FloatLit(v))),
                Err(_) => Err(SyntaxError::from_location(self.scanner.location())
                    .with_message(format!("Failed to parse float {}", orig))),
            }
        } else {
            match u64::from_str_radix(fixedup_str, base) {
                Ok(val) => Ok(Some(Token::IntLit(val))),
                Err(_) => Err(SyntaxError::from_location(self.scanner.location())
                    .with_message(format!("Failed to parse unsigned int {}", orig))),
            }
        }
    }

    fn extract_hex_val(&mut self, len: usize) -> Result<char, SyntaxError> {
        let mut code_str = String::new();
        for _ in 0..len {
            code_str.push(match self.scanner.next() {
                Some(c) => {
                    if c.is_digit(16) {
                        c
                    } else {
                        return Err(SyntaxError::from_location(self.scanner.location())
                            .with_message(format!(
                                "{} is not a valid unicode code point value",
                                c
                            )));
                    }
                }
                None => {
                    return Err(SyntaxError::from_location(self.scanner.location())
                        .with_message(format!("Expected {} hex digits after unicode escape", len)))
                }
            });
        }

        let unicode_value: u32 = u32::from_str_radix(&code_str, 16).unwrap();

        match char::from_u32(unicode_value) {
            Some(c) => Ok(c),
            None => Err(
                SyntaxError::from_location(self.scanner.location()).with_message(format!(
                    "{:x} is not a valid unicode code point",
                    unicode_value
                )),
            ),
        }
    }
}

impl Tokenizer for StringTokenizer<'_> {
    fn peek(&mut self) -> Result<Option<&TokenWithLoc>, SyntaxError> {
        if self.current.is_none() {
            match self.collect_next_token() {
                Ok(token) => self.current = token,
                Err(err) => return Err(err),
            };
        }
        Ok(self.current.as_ref())
    }

    fn next(&mut self) -> Result<Option<TokenWithLoc>, SyntaxError> {
        if self.current.is_none() {
            self.collect_next_token()
        } else {
            let tmp = std::mem::replace(&mut self.current, None);
            Ok(tmp)
        }
    }

    fn source<'a>(&'a self) -> &'a str {
        self.scanner.input()
    }

    fn location(&self) -> SourceLocation {
        self.scanner.location()
    }
}

#[cfg(test)]
mod test {
    use crate::{
        compiler::{
            source_location::SourceLocation, source_range::SourceRange, tokenizer::TokenWithLoc,
            tokens::Token,
        },
        StringTokenizer, Tokenizer,
    };

    #[test]
    fn tokens_locations() {
        let src = "foo + 3";

        let mut tokenizer = StringTokenizer::with_input(src);

        let t = tokenizer.next().unwrap().unwrap();
        assert_eq!(
            t,
            TokenWithLoc {
                token: Token::Ident("foo".to_owned()),
                loc: SourceRange::new(SourceLocation::new(0, 0), SourceLocation::new(0, 3))
            }
        );

        assert_eq!(&src[t.loc.start().col()..t.loc.end().col()], "foo");
        assert_eq!(
            tokenizer.next().unwrap().unwrap(),
            TokenWithLoc {
                token: Token::Add,
                loc: SourceRange::new(SourceLocation::new(0, 4), SourceLocation::new(0, 5))
            }
        );

        assert_eq!(
            tokenizer.next().unwrap().unwrap(),
            TokenWithLoc {
                token: Token::IntLit(3),
                loc: SourceRange::new(SourceLocation::new(0, 6), SourceLocation::new(0, 7))
            }
        )
    }

    #[test]
    fn string_literal_loc() {
        let src = "\"this is a test string\"";

        let mut tokenozer = StringTokenizer::with_input(src);

        assert_eq!(
            tokenozer.next().unwrap().unwrap(),
            TokenWithLoc {
                token: Token::StringLit("this is a test string".to_owned()),
                loc: SourceRange::new(SourceLocation::new(0, 0), SourceLocation::new(0, src.len()))
            }
        );
    }
}