@@ -475,7 +475,7 @@ impl<'src> Lexer<'src> {
475
475
match start {
476
476
' ' | '\t' => self . lex_whitespace ( ) ,
477
477
'!' if self . rest ( ) . starts_with ( "!include" ) => Err ( self . error ( Include ) ) ,
478
- '!' => self . lex_digraph ( '!' , '=' , BangEquals ) ,
478
+ '!' => self . lex_choices ( '!' , & [ ( '=' , BangEquals ) , ( '~' , BangTilde ) ] , None ) ,
479
479
'#' => self . lex_comment ( ) ,
480
480
'$' => self . lex_single ( Dollar ) ,
481
481
'&' => self . lex_digraph ( '&' , '&' , AmpersandAmpersand ) ,
@@ -486,7 +486,11 @@ impl<'src> Lexer<'src> {
486
486
',' => self . lex_single ( Comma ) ,
487
487
'/' => self . lex_single ( Slash ) ,
488
488
':' => self . lex_colon ( ) ,
489
- '=' => self . lex_choices ( '=' , & [ ( '=' , EqualsEquals ) , ( '~' , EqualsTilde ) ] , Equals ) ,
489
+ '=' => self . lex_choices (
490
+ '=' ,
491
+ & [ ( '=' , EqualsEquals ) , ( '~' , EqualsTilde ) ] ,
492
+ Some ( Equals ) ,
493
+ ) ,
490
494
'?' => self . lex_single ( QuestionMark ) ,
491
495
'@' => self . lex_single ( At ) ,
492
496
'[' => self . lex_delimiter ( BracketL ) ,
@@ -618,7 +622,7 @@ impl<'src> Lexer<'src> {
618
622
& mut self ,
619
623
first : char ,
620
624
choices : & [ ( char , TokenKind ) ] ,
621
- otherwise : TokenKind ,
625
+ otherwise : Option < TokenKind > ,
622
626
) -> CompileResult < ' src > {
623
627
self . presume ( first) ?;
624
628
@@ -629,7 +633,20 @@ impl<'src> Lexer<'src> {
629
633
}
630
634
}
631
635
632
- self . token ( otherwise) ;
636
+ if let Some ( token) = otherwise {
637
+ self . token ( token) ;
638
+ } else {
639
+ // Emit an unspecified token to consume the current character,
640
+ self . token ( Unspecified ) ;
641
+
642
+ // …and advance past another character,
643
+ self . advance ( ) ?;
644
+
645
+ // …so that the error we produce highlights the unexpected character.
646
+ return Err ( self . error ( UnexpectedCharacter {
647
+ expected : choices. iter ( ) . map ( |choice| choice. 0 ) . collect ( ) ,
648
+ } ) ) ;
649
+ }
633
650
634
651
Ok ( ( ) )
635
652
}
@@ -700,7 +717,9 @@ impl<'src> Lexer<'src> {
700
717
self . advance ( ) ?;
701
718
702
719
// …so that the error we produce highlights the unexpected character.
703
- Err ( self . error ( UnexpectedCharacter { expected : right } ) )
720
+ Err ( self . error ( UnexpectedCharacter {
721
+ expected : vec ! [ right] ,
722
+ } ) )
704
723
}
705
724
}
706
725
@@ -949,6 +968,7 @@ mod tests {
949
968
Asterisk => "*" ,
950
969
At => "@" ,
951
970
BangEquals => "!=" ,
971
+ BangTilde => "!~" ,
952
972
BarBar => "||" ,
953
973
BraceL => "{" ,
954
974
BraceR => "}" ,
@@ -2272,7 +2292,7 @@ mod tests {
2272
2292
column: 1 ,
2273
2293
width: 1 ,
2274
2294
kind: UnexpectedCharacter {
2275
- expected: '&' ,
2295
+ expected: vec! [ '&' ] ,
2276
2296
} ,
2277
2297
}
2278
2298
0 commit comments