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
//! A lexical analyzer for JavaScript source code.
//!
//! The Lexer splits its input source code into a sequence of input elements called tokens, represented by the [Token](../ast/token/struct.Token.html) structure.
//! It also removes whitespace and comments and attaches them to the next token.

#[cfg(test)]
mod tests;

use crate::builtins::BigInt;
use crate::{
    syntax::ast::{
        token::{NumericLiteral, Token, TokenKind},
        Position, Punctuator, Span,
    },
    BoaProfiler,
};
use std::{
    char::{decode_utf16, from_u32},
    error, fmt,
    iter::Peekable,
    str::{Chars, FromStr},
};

/// `vop` tests the next token to see if we're on an assign operation of just a plain binary operation.
///
/// If the next value is not an assignment operation it will pattern match  the provided values and return the corresponding token.
macro_rules! vop {
    ($this:ident, $assign_op:expr, $op:expr) => ({
        let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
        match preview {
            '=' => {
                $this.next();
                $this.next_column();
                $assign_op
            }
            _ => $op,
        }
    });
    ($this:ident, $assign_op:expr, $op:expr, {$($case:pat => $block:expr), +}) => ({
        let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
        match preview {
            '=' => {
                $this.next();
                $this.next_column();
                $assign_op
            },
            $($case => {
                $this.next();
                $this.next_column();
                $block
            })+,
            _ => $op
        }
    });
    ($this:ident, $op:expr, {$($case:pat => $block:expr),+}) => {
        let preview = $this.preview_next().ok_or_else(|| LexerError::new("could not preview next value"))?;
        match preview {
            $($case => {
                $this.next()?;
                $this.next_column();
                $block
            })+,
            _ => $op
        }
    }
}

/// The `op` macro handles binary operations or assignment operations and converts them into tokens.
macro_rules! op {
    ($this:ident, $start_pos:expr, $assign_op:expr, $op:expr) => ({
        let punc = vop!($this, $assign_op, $op);
        $this.push_punc(punc, $start_pos);
    });
    ($this:ident, $start_pos:expr, $assign_op:expr, $op:expr, {$($case:pat => $block:expr),+}) => ({
        let punc = vop!($this, $assign_op, $op, {$($case => $block),+});
        $this.push_punc(punc, $start_pos);
    });
}

/// An error that occurred during lexing or compiling of the source input.
///
/// [LexerError] implements [fmt::Display] so you just display this value as an error
#[derive(Debug, Clone)]
pub struct LexerError {
    /// details will be displayed when a LexerError occurs.
    details: String,
}

impl LexerError {
    /// Create a new LexerError struct
    ///
    /// * `msg` - The message to show when LexerError is displayed
    pub(crate) fn new<M>(msg: M) -> Self
    where
        M: Into<String>,
    {
        Self {
            details: msg.into(),
        }
    }
}

impl fmt::Display for LexerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.details)
    }
}

impl error::Error for LexerError {
    fn description(&self) -> &str {
        &self.details
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

/// A lexical analyzer for JavaScript source code.
#[derive(Debug)]
pub struct Lexer<'a> {
    /// The list of tokens generated so far.
    ///
    /// This field is public so you can use them once lexing has finished.
    pub tokens: Vec<Token>,
    /// The current position in the source code.
    position: Position,
    /// The full Peekable buffer, an array of [Char]s
    buffer: Peekable<Chars<'a>>,
}

impl<'a> Lexer<'a> {
    /// Returns a Lexer with a buffer inside
    ///
    /// The buffer needs to have a lifetime as long as the Lexer instance itself
    pub fn new(buffer: &'a str) -> Lexer<'a> {
        Lexer {
            tokens: Vec::new(),
            position: Position::new(1, 1),
            buffer: buffer.chars().peekable(),
        }
    }

    /// Push a token onto the token queue.
    fn push_token(&mut self, tk: TokenKind, start: Position) {
        let end = if let TokenKind::LineTerminator = tk {
            self.position
        } else {
            Position::new(
                self.position.line_number(),
                self.position.column_number() - 1,
            )
        };
        self.tokens.push(Token::new(tk, Span::new(start, end)))
    }

    /// Push a punctuation token
    fn push_punc(&mut self, punc: Punctuator, start: Position) {
        self.push_token(TokenKind::Punctuator(punc), start);
    }

    /// Changes the current position by advancing to the next column.
    fn next_column(&mut self) {
        let pos = Position::new(
            self.position.line_number(),
            self.position.column_number() + 1,
        );
        self.position = pos;
    }

    /// Changes the current position by advancing the given number of columns.
    fn move_columns(&mut self, columns: u32) {
        let pos = Position::new(
            self.position.line_number(),
            self.position.column_number() + columns,
        );
        self.position = pos;
    }

    fn carriage_return(&mut self) {
        let pos = Position::new(self.position.line_number(), 1);
        self.position = pos;
    }

    /// Changes the current position by advancing to the next line.
    fn next_line(&mut self) {
        let pos = Position::new(self.position.line_number() + 1, 1);
        self.position = pos;
    }

    /// Changes the current position by advancing the given number of lines.
    fn move_lines(&mut self, lines: u32) {
        let pos = Position::new(self.position.line_number() + lines, 1);
        self.position = pos;
    }

    /// next fetches the next token and return it, or a LexerError if there are no more.
    fn next(&mut self) -> char {
        self.buffer.next().expect(
            "No more more characters to consume from input stream, \
             use preview_next() first to check before calling next()",
        )
    }

    /// Preview the next character but don't actually increment
    fn preview_next(&mut self) -> Option<char> {
        self.buffer.peek().copied()
    }

    /// Preview a char x indexes further in buf, without incrementing
    fn preview_multiple_next(&mut self, nb_next: usize) -> Option<char> {
        let mut next_peek = None;

        for (i, x) in self.buffer.clone().enumerate() {
            if i >= nb_next {
                break;
            }

            next_peek = Some(x);
        }

        next_peek
    }

    /// Utility Function, while ``f(char)`` is true, read chars and move curser.
    /// All chars are returned as a string
    fn take_char_while<F>(&mut self, mut f: F) -> Result<String, LexerError>
    where
        F: FnMut(char) -> bool,
    {
        let mut s = String::new();
        while self.buffer.peek().is_some()
            && f(self.preview_next().expect("Could not preview next value"))
        {
            s.push(self.next());
        }

        Ok(s)
    }

    /// Compares the character passed in to the next character, if they match true is returned and the buffer is incremented
    fn next_is(&mut self, peek: char) -> bool {
        let result = self.preview_next() == Some(peek);
        if result {
            self.next_column();
            self.buffer.next();
        }
        result
    }

    /// Utility function for checkint the NumericLiteral is not followed by an `IdentifierStart` or `DecimalDigit` character.
    ///
    /// More information:
    ///  - [ECMAScript Specification][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-literals-numeric-literals
    fn check_after_numeric_literal(&mut self) -> Result<(), LexerError> {
        match self.preview_next() {
            Some(ch)
                if ch.is_ascii_alphabetic() || ch == '$' || ch == '_' || ch.is_ascii_digit() =>
            {
                Err(LexerError::new("NumericLiteral token must not be followed by IdentifierStart nor DecimalDigit characters"))
            }
            Some(_) => Ok(()),
            None => Ok(())
        }
    }

    /// Lexes a numerical literal.
    ///
    /// More information:
    ///  - [ECMAScript Specification][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-literals-numeric-literals
    fn reed_numerical_literal(&mut self, ch: char) -> Result<(), LexerError> {
        /// This is a helper structure
        ///
        /// This structure helps with identifying what numerical type it is and what base is it.
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        enum NumericKind {
            Rational,
            Integer(u8),
            BigInt(u8),
        }

        impl NumericKind {
            /// Get the base of the number kind.
            fn base(self) -> u32 {
                match self {
                    Self::Rational => 10,
                    Self::Integer(base) => base as u32,
                    Self::BigInt(base) => base as u32,
                }
            }

            /// Converts `self` to BigInt kind.
            fn to_bigint(self) -> Self {
                match self {
                    Self::Rational => unreachable!("can not convert rational number to BigInt"),
                    Self::Integer(base) => Self::BigInt(base),
                    Self::BigInt(base) => Self::BigInt(base),
                }
            }
        }

        // TODO: Setup strict mode.
        let strict_mode = false;

        let mut buf = ch.to_string();
        let mut kind = NumericKind::Integer(10);
        let start_pos = self.position;
        if ch == '0' {
            match self.preview_next() {
                None => {
                    self.next_column();
                    self.push_token(
                        TokenKind::NumericLiteral(NumericLiteral::Integer(0)),
                        start_pos,
                    );
                    return Ok(());
                }
                Some('x') | Some('X') => {
                    self.next();
                    self.next_column();
                    kind = NumericKind::Integer(16);
                }
                Some('o') | Some('O') => {
                    self.next();
                    self.next_column();
                    kind = NumericKind::Integer(8);
                }
                Some('b') | Some('B') => {
                    self.next();
                    self.next_column();
                    kind = NumericKind::Integer(2);
                }
                Some(ch) if ch.is_ascii_digit() => {
                    let mut is_implicit_octal = true;
                    while let Some(ch) = self.preview_next() {
                        if !ch.is_ascii_digit() {
                            break;
                        } else if !ch.is_digit(8) {
                            is_implicit_octal = false;
                        }
                        buf.push(self.next());
                    }
                    if !strict_mode {
                        if is_implicit_octal {
                            kind = NumericKind::Integer(8);
                        }
                    } else {
                        return Err(if is_implicit_octal {
                            LexerError::new(
                                "Implicit octal literals are not allowed in strict mode.",
                            )
                        } else {
                            LexerError::new(
                                "Decimals with leading zeros are not allowed in strict mode.",
                            )
                        });
                    }
                }
                Some(_) => {}
            }
        }

        while let Some(ch) = self.preview_next() {
            if !ch.is_digit(kind.base()) {
                break;
            }
            buf.push(self.next());
        }

        if self.next_is('n') {
            kind = kind.to_bigint();
        }

        if let NumericKind::Integer(10) = kind {
            'digitloop: while let Some(ch) = self.preview_next() {
                match ch {
                    '.' => loop {
                        kind = NumericKind::Rational;
                        buf.push(self.next());

                        let c = match self.preview_next() {
                            Some(ch) => ch,
                            None => break,
                        };

                        match c {
                            'e' | 'E' => {
                                match self
                                    .preview_multiple_next(2)
                                    .unwrap_or_default()
                                    .to_digit(10)
                                {
                                    Some(0..=9) | None => {
                                        buf.push(self.next());
                                    }
                                    _ => {
                                        break 'digitloop;
                                    }
                                }
                            }
                            _ => {
                                if !c.is_digit(10) {
                                    break 'digitloop;
                                }
                            }
                        }
                    },
                    'e' | 'E' => {
                        kind = NumericKind::Rational;
                        match self
                            .preview_multiple_next(2)
                            .unwrap_or_default()
                            .to_digit(10)
                        {
                            Some(0..=9) | None => {
                                buf.push(self.next());
                            }
                            _ => {
                                break;
                            }
                        }
                        buf.push(self.next());
                    }
                    '+' | '-' => {
                        break;
                    }
                    _ if ch.is_digit(10) => {
                        buf.push(self.next());
                    }
                    _ => break,
                }
            }
        }

        self.check_after_numeric_literal()?;

        let num = match kind {
                NumericKind::BigInt(base) => {
                    NumericLiteral::BigInt(
                        BigInt::from_string_radix(&buf, base as u32).expect("Could not conver to BigInt")
                        )
                }
                NumericKind::Rational /* base: 10 */ => {
                    NumericLiteral::Rational(
                        f64::from_str(&buf)
                            .map_err(|_| LexerError::new("Could not convert value to f64"))?,
                    )
                }
                NumericKind::Integer(base) => {
                    if let Ok(num) = i32::from_str_radix(&buf, base as u32) {
                        NumericLiteral::Integer(
                            num
                        )
                    } else {
                        let b = f64::from(base);
                        let mut result = 0.0_f64;
                        for c in buf.chars() {
                            let digit = f64::from(c.to_digit(base as u32).unwrap());
                            result = result * b + digit;
                        }

                        NumericLiteral::Rational(result)
                    }

                }
            };

        self.move_columns(buf.len() as u32);
        self.push_token(TokenKind::NumericLiteral(num), start_pos);

        Ok(())
    }

    /// Runs the lexer until completion, returning a [LexerError] if there's a syntax issue, or an empty unit result
    ///
    /// # Example
    ///
    /// ```
    /// # use boa::syntax::lexer::{LexerError, Lexer};
    /// fn main() -> Result<(), LexerError> {
    ///     let buffer = String::from("Hello World");
    ///     let mut lexer = Lexer::new(&buffer);
    ///     lexer.lex()
    /// }
    /// ```
    pub fn lex(&mut self) -> Result<(), LexerError> {
        let _timer = BoaProfiler::global().start_event("lex", "lexing");
        loop {
            // Check if we've reached the end
            if self.preview_next().is_none() {
                return Ok(());
            }
            let start_pos = self.position;
            self.next_column();
            let ch = self.next();
            match ch {
                 // StringLiteral
                '"' | '\'' => {
                    let mut buf = String::new();
                    loop {
                        if self.preview_next().is_none() {
                            return Err(LexerError::new("Unterminated String"));
                        }
                        match self.next() {
                            '\'' if ch == '\'' => {
                                break;
                            }
                            '"' if ch == '"' => {
                                break;
                            }
                            '\\' => {
                                if self.preview_next().is_none() {
                                    return Err(LexerError::new("Unterminated String"));
                                }
                                let escape_pos = self.position;
                                let escape = self.next();
                                if escape != '\n' {
                                    let escaped_ch = match escape {
                                        'n' => '\n',
                                        'r' => '\r',
                                        't' => '\t',
                                        'b' => '\x08',
                                        'f' => '\x0c',
                                        '0' => '\0',
                                        'x' => {
                                            let mut nums = String::with_capacity(2);
                                            for _ in 0_u8..2 {
                                                if self.preview_next().is_none() {
                                                    return Err(LexerError::new("Unterminated String"));
                                                }
                                                nums.push(self.next());
                                            }
                                            self.move_columns(2);
                                            let as_num = match u64::from_str_radix(&nums, 16) {
                                                Ok(v) => v,
                                                Err(_) => 0,
                                            };
                                            match from_u32(as_num as u32) {
                                                Some(v) => v,
                                                None => panic!(
                                                    "{}: {} is not a valid unicode scalar value",
                                                    self.position, as_num
                                                ),
                                            }
                                        }
                                        'u' => {
                                            // There are 2 types of codepoints. Surragate codepoints and unicode codepoints.
                                            // UTF-16 could be surrogate codepoints, "\uXXXX\uXXXX" which make up a single unicode codepoint.
                                            // We will need to loop to make sure we catch all UTF-16 codepoints
                                            // Example Test: https://github.com/tc39/test262/blob/ee3715ee56744ccc8aeb22a921f442e98090b3c1/implementation-contributed/v8/mjsunit/es6/unicode-escapes.js#L39-L44

                                            // Support \u{X..X} (Unicode Codepoint)
                                            if self.next_is('{') {
                                                let s = self
                                                    .take_char_while(char::is_alphanumeric)
                                                    .expect("Could not read chars");

                                                // We know this is a single unicode codepoint, convert to u32
                                                let as_num = match u32::from_str_radix(&s, 16) {
                                                    Ok(v) => v,
                                                    Err(_) => 0,
                                                };
                                                let c = from_u32(as_num).ok_or_else(|| LexerError::new("Invalid Unicode escape sequence"))?;

                                                if self.preview_next().is_none() {
                                                    return Err(LexerError::new("Unterminated String"));
                                                }
                                                self.next(); // '}'
                                                self.move_columns(s.len() as u32);
                                                c
                                            } else {
                                                let mut codepoints: Vec<u16> = vec![];
                                                loop {
                                                    // Collect each character after \u e.g \uD83D will give "D83D"
                                                    let s = self
                                                        .take_char_while(char::is_alphanumeric)
                                                        .expect("Could not read chars");

                                                    // Convert to u16
                                                    let as_num = match u16::from_str_radix(&s, 16) {
                                                        Ok(v) => v,
                                                        Err(_) => 0,
                                                    };

                                                    codepoints.push(as_num);
                                                    self.move_columns(s.len() as u32);

                                                    // Check for another UTF-16 codepoint
                                                    if self.next_is('\\') && self.next_is('u') {
                                                        continue;
                                                    }
                                                    break;
                                                }

                                                // codepoints length should either be 1 (unicode codepoint) or 2 (surrogate codepoint).
                                                // Rust's decode_utf16 will deal with it regardless
                                                decode_utf16(codepoints.iter().cloned())
                                                    .next()
                                                    .expect("Could not get next codepoint")
                                                    .expect("Could not get next codepoint")
                                            }
                                        }
                                        '\'' | '"' | '\\' => escape,
                                        ch => {
                                            let details = format!("invalid escape sequence `{}` at line {}, column {}", escape_pos.line_number(), escape_pos.column_number(), ch);
                                            return Err(LexerError { details });
                                        }
                                    };
                                    buf.push(escaped_ch);
                                }
                            }
                            next_ch => buf.push(next_ch),
                        }
                    }
                    let str_length = buf.len() as u32;
                    // Why +1? Quotation marks are not included,
                    // So technically it would be +2, (for both " ") but we want to be 1 less
                    // to compensate for the incrementing at the top
                    self.move_columns( str_length.wrapping_add(1));
                    self.push_token(TokenKind::string_literal(buf), start_pos);
                }
                // TemplateLiteral
                '`' => {
                    let mut buf = String::new();
                    loop {
                        if self.preview_next().is_none() {
                            return Err(LexerError::new("Unterminated template literal"));
                        }
                        match self.next() {
                            '`' => {
                                break;
                            }
                            next_ch => buf.push(next_ch),
                            // TODO when there is an expression inside the literal
                        }
                    }
                    let str_length = buf.len() as u32;
                    // Why +1? Quotation marks are not included,
                    // So technically it would be +2, (for both " ") but we want to be 1 less
                    // to compensate for the incrementing at the top
                    self.move_columns( str_length.wrapping_add(1));
                    self.push_token(TokenKind::template_literal(buf), start_pos);
                }
                _ if ch.is_digit(10) => self.reed_numerical_literal(ch)?,
                _ if ch.is_alphabetic() || ch == '$' || ch == '_' => {
                    let mut buf = ch.to_string();
                    while let Some(ch) = self.preview_next() {
                        if ch.is_alphabetic() || ch.is_digit(10) || ch == '_' {
                            buf.push(self.next());
                        } else {
                            break;
                        }
                    }
                    let tk = match buf.as_str() {
                        "true" => TokenKind::BooleanLiteral(true),
                        "false" => TokenKind::BooleanLiteral(false),
                        "null" => TokenKind::NullLiteral,
                        slice => {
                            if let Ok(keyword) = FromStr::from_str(slice) {
                                TokenKind::Keyword(keyword)
                            } else {
                                TokenKind::identifier(slice)
                            }
                        }
                    };

                    // Move position forward the length of the token
                    self.move_columns( (buf.len().wrapping_sub(1)) as u32);

                    self.push_token(tk, start_pos);
                }
                ';' => self.push_punc(Punctuator::Semicolon, start_pos),
                ':' => self.push_punc(Punctuator::Colon, start_pos),
                '.' => {
                    // . or ...
                    if self.next_is('.') {
                        if self.next_is('.') {
                            self.push_punc(Punctuator::Spread, start_pos);
                        } else {
                            return Err(LexerError::new("Expecting Token ."));
                        }
                    } else {
                        self.push_punc(Punctuator::Dot, start_pos);
                    };
                }
                '(' => self.push_punc(Punctuator::OpenParen, start_pos),
                ')' => self.push_punc(Punctuator::CloseParen, start_pos),
                ',' => self.push_punc(Punctuator::Comma, start_pos),
                '{' => self.push_punc(Punctuator::OpenBlock, start_pos),
                '}' => self.push_punc(Punctuator::CloseBlock, start_pos),
                '[' => self.push_punc(Punctuator::OpenBracket, start_pos),
                ']' => self.push_punc(Punctuator::CloseBracket, start_pos),
                '?' => self.push_punc(Punctuator::Question, start_pos),
                // Comments
                '/' => {
                    if let Some(ch) = self.preview_next() {
                        match ch {
                            // line comment
                            '/' => {
                                while self.preview_next().is_some() {
                                    if self.next() == '\n' {
                                        break;
                                    }
                                }
                                self.next_line()
                            }
                            // block comment
                            '*' => {
                                let mut lines = 0;
                                loop {
                                    if self.preview_next().is_none() {
                                        return Err(LexerError::new("unterminated multiline comment"));
                                    }
                                    match self.next() {
                                        '*' => {
                                            if self.next_is('/') {
                                                break;
                                            }
                                        }
                                        next_ch => {
                                            if next_ch == '\n' {
                                                lines += 1;
                                            }
                                        },
                                    }
                                }
                                self.move_lines(lines);
                            }
                            // division, assigndiv or regex literal
                            _ => {
                                // if we fail to parse a regex literal, store a copy of the current
                                // buffer to restore later on
                                let original_buffer = self.buffer.clone();
                                let original_pos = self.position;
                                // first, try to parse a regex literal
                                let mut body = String::new();
                                let mut regex = false;
                                loop {
                                    self.next_column();
                                    match self.buffer.next() {
                                        // end of body
                                        Some('/') => {
                                            regex = true;
                                            break;
                                        }
                                        // newline/eof not allowed in regex literal
                                        n @ Some('\n') | n @ Some('\r') | n @ Some('\u{2028}')
                                        | n @ Some('\u{2029}') => {
                                            self.carriage_return();
                                            if n != Some('\r') {
                                                self.next_line();
                                            }
                                            break
                                        },
                                        None => {
                                            self.position = Position::new(self.position.line_number(), self.position.column_number()-1);
                                            break
                                        }
                                        // escape sequence
                                        Some('\\') => {
                                            body.push('\\');
                                            if self.preview_next().is_none() {
                                                break;
                                            }
                                            match self.next() {
                                                // newline not allowed in regex literal
                                                '\n' | '\r' | '\u{2028}' | '\u{2029}' => break,
                                                ch => body.push(ch),
                                            }
                                        }
                                        Some(ch) => body.push(ch),
                                    }
                                }
                                if regex {
                                    // body was parsed, now look for flags
                                    let flags = self.take_char_while(char::is_alphabetic)?;
                                    self.move_columns(body.len() as u32 + 1 + flags.len() as u32);
                                    self.push_token(TokenKind::regular_expression_literal(
                                        body, flags.parse()?,
                                    ), start_pos);
                                } else {
                                    // failed to parse regex, restore original buffer position and
                                    // parse either div or assigndiv
                                    self.buffer = original_buffer;
                                    self.position = original_pos;
                                    if self.next_is('=') {
                                        self.push_token(TokenKind::Punctuator(
                                            Punctuator::AssignDiv,
                                        ), start_pos);
                                    } else {
                                        self.push_token(TokenKind::Punctuator(Punctuator::Div), start_pos);
                                    }
                                }
                            }
                        }
                    } else {
                        return Err(LexerError::new("Expecting Token /,*,= or regex"));
                    }
                }
                '*' => op!(self, start_pos, Punctuator::AssignMul, Punctuator::Mul, {
                    '*' => vop!(self, Punctuator::AssignPow, Punctuator::Exp)
                }),
                '+' => op!(self, start_pos, Punctuator::AssignAdd, Punctuator::Add, {
                    '+' => Punctuator::Inc
                }),
                '-' => op!(self, start_pos, Punctuator::AssignSub, Punctuator::Sub, {
                    '-' => {
                        Punctuator::Dec
                    }
                }),
                '%' => op!(self, start_pos, Punctuator::AssignMod, Punctuator::Mod),
                '|' => op!(self, start_pos, Punctuator::AssignOr, Punctuator::Or, {
                    '|' => Punctuator::BoolOr
                }),
                '&' => op!(self, start_pos, Punctuator::AssignAnd, Punctuator::And, {
                    '&' => Punctuator::BoolAnd
                }),
                '^' => op!(self, start_pos, Punctuator::AssignXor, Punctuator::Xor),
                '=' => op!(self, start_pos, if self.next_is('=') {
                    Punctuator::StrictEq
                } else {
                    Punctuator::Eq
                }, Punctuator::Assign, {
                    '>' => {
                        Punctuator::Arrow
                    }
                }),
                '<' => op!(self, start_pos, Punctuator::LessThanOrEq, Punctuator::LessThan, {
                    '<' => vop!(self, Punctuator::AssignLeftSh, Punctuator::LeftSh)
                }),
                '>' => op!(self, start_pos, Punctuator::GreaterThanOrEq, Punctuator::GreaterThan, {
                    '>' => vop!(self, Punctuator::AssignRightSh, Punctuator::RightSh, {
                        '>' => vop!(self, Punctuator::AssignURightSh, Punctuator::URightSh)
                    })
                }),
                '!' => op!(
                    self,
                    start_pos,
                    vop!(self, Punctuator::StrictNotEq, Punctuator::NotEq),
                    Punctuator::Not
                ),
                '~' => self.push_punc(Punctuator::Neg, start_pos),
                '\n' | '\u{2028}' | '\u{2029}' => {
                    self.next_line();
                    self.push_token(TokenKind::LineTerminator, start_pos);
                }
                '\r' => {
                    self.carriage_return();
                }
                // The rust char::is_whitespace function and the ecma standard use different sets
                // of characters as whitespaces:
                //  * Rust uses \p{White_Space},
                //  * ecma standard uses \{Space_Separator} + \u{0009}, \u{000B}, \u{000C}, \u{FEFF}
                //
                // Explicit whitespace: see https://tc39.es/ecma262/#table-32
                '\u{0020}' | '\u{0009}' | '\u{000B}' | '\u{000C}' | '\u{00A0}' | '\u{FEFF}' |
                // Unicode Space_Seperator category (minus \u{0020} and \u{00A0} which are allready stated above)
                '\u{1680}' | '\u{2000}'..='\u{200A}' | '\u{202F}' | '\u{205F}' | '\u{3000}' => (),
                _ => {
                    let details = format!("Unexpected '{}' at line {}, column {}", start_pos.line_number(), start_pos.column_number(), ch);
                    return Err(LexerError { details });
                },
            }
        }
    }
}