]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lexer/src/lib.rs
Rollup merge of #104121 - Lokathor:mir-opt-when-instruction-set-missing-on-callee...
[rust.git] / compiler / rustc_lexer / src / lib.rs
1 //! Low-level Rust lexer.
2 //!
3 //! The idea with `rustc_lexer` is to make a reusable library,
4 //! by separating out pure lexing and rustc-specific concerns, like spans,
5 //! error reporting, and interning.  So, rustc_lexer operates directly on `&str`,
6 //! produces simple tokens which are a pair of type-tag and a bit of original text,
7 //! and does not report errors, instead storing them as flags on the token.
8 //!
9 //! Tokens produced by this lexer are not yet ready for parsing the Rust syntax.
10 //! For that see [`rustc_parse::lexer`], which converts this basic token stream
11 //! into wide tokens used by actual parser.
12 //!
13 //! The purpose of this crate is to convert raw sources into a labeled sequence
14 //! of well-known token types, so building an actual Rust token stream will
15 //! be easier.
16 //!
17 //! The main entity of this crate is the [`TokenKind`] enum which represents common
18 //! lexeme types.
19 //!
20 //! [`rustc_parse::lexer`]: ../rustc_parse/lexer/index.html
21 #![deny(rustc::untranslatable_diagnostic)]
22 #![deny(rustc::diagnostic_outside_of_impl)]
23 // We want to be able to build this crate with a stable compiler, so no
24 // `#![feature]` attributes should be added.
25
26 mod cursor;
27 pub mod unescape;
28
29 #[cfg(test)]
30 mod tests;
31
32 pub use crate::cursor::Cursor;
33
34 use self::LiteralKind::*;
35 use self::TokenKind::*;
36 use crate::cursor::EOF_CHAR;
37 use std::convert::TryFrom;
38
39 /// Parsed token.
40 /// It doesn't contain information about data that has been parsed,
41 /// only the type of the token and its size.
42 #[derive(Debug)]
43 pub struct Token {
44     pub kind: TokenKind,
45     pub len: u32,
46 }
47
48 impl Token {
49     fn new(kind: TokenKind, len: u32) -> Token {
50         Token { kind, len }
51     }
52 }
53
54 /// Enum representing common lexeme types.
55 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
56 pub enum TokenKind {
57     // Multi-char tokens:
58     /// "// comment"
59     LineComment { doc_style: Option<DocStyle> },
60
61     /// `/* block comment */`
62     ///
63     /// Block comments can be recursive, so a sequence like `/* /* */`
64     /// will not be considered terminated and will result in a parsing error.
65     BlockComment { doc_style: Option<DocStyle>, terminated: bool },
66
67     /// Any whitespace character sequence.
68     Whitespace,
69
70     /// "ident" or "continue"
71     ///
72     /// At this step, keywords are also considered identifiers.
73     Ident,
74
75     /// Like the above, but containing invalid unicode codepoints.
76     InvalidIdent,
77
78     /// "r#ident"
79     RawIdent,
80
81     /// An unknown prefix, like `foo#`, `foo'`, `foo"`.
82     ///
83     /// Note that only the
84     /// prefix (`foo`) is included in the token, not the separator (which is
85     /// lexed as its own distinct token). In Rust 2021 and later, reserved
86     /// prefixes are reported as errors; in earlier editions, they result in a
87     /// (allowed by default) lint, and are treated as regular identifier
88     /// tokens.
89     UnknownPrefix,
90
91     /// Examples: `12u8`, `1.0e-40`, `b"123"`. Note that `_` is an invalid
92     /// suffix, but may be present here on string and float literals. Users of
93     /// this type will need to check for and reject that case.
94     ///
95     /// See [LiteralKind] for more details.
96     Literal { kind: LiteralKind, suffix_start: u32 },
97
98     /// "'a"
99     Lifetime { starts_with_number: bool },
100
101     // One-char tokens:
102     /// ";"
103     Semi,
104     /// ","
105     Comma,
106     /// "."
107     Dot,
108     /// "("
109     OpenParen,
110     /// ")"
111     CloseParen,
112     /// "{"
113     OpenBrace,
114     /// "}"
115     CloseBrace,
116     /// "["
117     OpenBracket,
118     /// "]"
119     CloseBracket,
120     /// "@"
121     At,
122     /// "#"
123     Pound,
124     /// "~"
125     Tilde,
126     /// "?"
127     Question,
128     /// ":"
129     Colon,
130     /// "$"
131     Dollar,
132     /// "="
133     Eq,
134     /// "!"
135     Bang,
136     /// "<"
137     Lt,
138     /// ">"
139     Gt,
140     /// "-"
141     Minus,
142     /// "&"
143     And,
144     /// "|"
145     Or,
146     /// "+"
147     Plus,
148     /// "*"
149     Star,
150     /// "/"
151     Slash,
152     /// "^"
153     Caret,
154     /// "%"
155     Percent,
156
157     /// Unknown token, not expected by the lexer, e.g. "№"
158     Unknown,
159
160     /// End of input.
161     Eof,
162 }
163
164 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
165 pub enum DocStyle {
166     Outer,
167     Inner,
168 }
169
170 // Note that the suffix is *not* considered when deciding the `LiteralKind` in
171 // this type. This means that float literals like `1f32` are classified by this
172 // type as `Int`. (Compare against `rustc_ast::token::LitKind` and
173 // `rustc_ast::ast::LitKind.)
174 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
175 pub enum LiteralKind {
176     /// "12_u8", "0o100", "0b120i99", "1f32".
177     Int { base: Base, empty_int: bool },
178     /// "12.34f32", "1e3", but not "1f32`.
179     Float { base: Base, empty_exponent: bool },
180     /// "'a'", "'\\'", "'''", "';"
181     Char { terminated: bool },
182     /// "b'a'", "b'\\'", "b'''", "b';"
183     Byte { terminated: bool },
184     /// ""abc"", ""abc"
185     Str { terminated: bool },
186     /// "b"abc"", "b"abc"
187     ByteStr { terminated: bool },
188     /// "r"abc"", "r#"abc"#", "r####"ab"###"c"####", "r#"a". `None` indicates
189     /// an invalid literal.
190     RawStr { n_hashes: Option<u8> },
191     /// "br"abc"", "br#"abc"#", "br####"ab"###"c"####", "br#"a". `None`
192     /// indicates an invalid literal.
193     RawByteStr { n_hashes: Option<u8> },
194 }
195
196 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
197 pub enum RawStrError {
198     /// Non `#` characters exist between `r` and `"`, e.g. `r##~"abcde"##`
199     InvalidStarter { bad_char: char },
200     /// The string was not terminated, e.g. `r###"abcde"##`.
201     /// `possible_terminator_offset` is the number of characters after `r` or
202     /// `br` where they may have intended to terminate it.
203     NoTerminator { expected: u32, found: u32, possible_terminator_offset: Option<u32> },
204     /// More than 255 `#`s exist.
205     TooManyDelimiters { found: u32 },
206 }
207
208 /// Base of numeric literal encoding according to its prefix.
209 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
210 pub enum Base {
211     /// Literal starts with "0b".
212     Binary = 2,
213     /// Literal starts with "0o".
214     Octal = 8,
215     /// Literal doesn't contain a prefix.
216     Decimal = 10,
217     /// Literal starts with "0x".
218     Hexadecimal = 16,
219 }
220
221 /// `rustc` allows files to have a shebang, e.g. "#!/usr/bin/rustrun",
222 /// but shebang isn't a part of rust syntax.
223 pub fn strip_shebang(input: &str) -> Option<usize> {
224     // Shebang must start with `#!` literally, without any preceding whitespace.
225     // For simplicity we consider any line starting with `#!` a shebang,
226     // regardless of restrictions put on shebangs by specific platforms.
227     if let Some(input_tail) = input.strip_prefix("#!") {
228         // Ok, this is a shebang but if the next non-whitespace token is `[`,
229         // then it may be valid Rust code, so consider it Rust code.
230         let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok| {
231             !matches!(
232                 tok,
233                 TokenKind::Whitespace
234                     | TokenKind::LineComment { doc_style: None }
235                     | TokenKind::BlockComment { doc_style: None, .. }
236             )
237         });
238         if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
239             // No other choice than to consider this a shebang.
240             return Some(2 + input_tail.lines().next().unwrap_or_default().len());
241         }
242     }
243     None
244 }
245
246 /// Validates a raw string literal. Used for getting more information about a
247 /// problem with a `RawStr`/`RawByteStr` with a `None` field.
248 #[inline]
249 pub fn validate_raw_str(input: &str, prefix_len: u32) -> Result<(), RawStrError> {
250     debug_assert!(!input.is_empty());
251     let mut cursor = Cursor::new(input);
252     // Move past the leading `r` or `br`.
253     for _ in 0..prefix_len {
254         cursor.bump().unwrap();
255     }
256     cursor.raw_double_quoted_string(prefix_len).map(|_| ())
257 }
258
259 /// Creates an iterator that produces tokens from the input string.
260 pub fn tokenize(input: &str) -> impl Iterator<Item = Token> + '_ {
261     let mut cursor = Cursor::new(input);
262     std::iter::from_fn(move || {
263         let token = cursor.advance_token();
264         if token.kind != TokenKind::Eof { Some(token) } else { None }
265     })
266 }
267
268 /// True if `c` is considered a whitespace according to Rust language definition.
269 /// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
270 /// for definitions of these classes.
271 pub fn is_whitespace(c: char) -> bool {
272     // This is Pattern_White_Space.
273     //
274     // Note that this set is stable (ie, it doesn't change with different
275     // Unicode versions), so it's ok to just hard-code the values.
276
277     matches!(
278         c,
279         // Usual ASCII suspects
280         '\u{0009}'   // \t
281         | '\u{000A}' // \n
282         | '\u{000B}' // vertical tab
283         | '\u{000C}' // form feed
284         | '\u{000D}' // \r
285         | '\u{0020}' // space
286
287         // NEXT LINE from latin1
288         | '\u{0085}'
289
290         // Bidi markers
291         | '\u{200E}' // LEFT-TO-RIGHT MARK
292         | '\u{200F}' // RIGHT-TO-LEFT MARK
293
294         // Dedicated whitespace characters from Unicode
295         | '\u{2028}' // LINE SEPARATOR
296         | '\u{2029}' // PARAGRAPH SEPARATOR
297     )
298 }
299
300 /// True if `c` is valid as a first character of an identifier.
301 /// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
302 /// a formal definition of valid identifier name.
303 pub fn is_id_start(c: char) -> bool {
304     // This is XID_Start OR '_' (which formally is not a XID_Start).
305     c == '_' || unicode_xid::UnicodeXID::is_xid_start(c)
306 }
307
308 /// True if `c` is valid as a non-first character of an identifier.
309 /// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
310 /// a formal definition of valid identifier name.
311 pub fn is_id_continue(c: char) -> bool {
312     unicode_xid::UnicodeXID::is_xid_continue(c)
313 }
314
315 /// The passed string is lexically an identifier.
316 pub fn is_ident(string: &str) -> bool {
317     let mut chars = string.chars();
318     if let Some(start) = chars.next() {
319         is_id_start(start) && chars.all(is_id_continue)
320     } else {
321         false
322     }
323 }
324
325 impl Cursor<'_> {
326     /// Parses a token from the input string.
327     pub fn advance_token(&mut self) -> Token {
328         let first_char = match self.bump() {
329             Some(c) => c,
330             None => return Token::new(TokenKind::Eof, 0),
331         };
332         let token_kind = match first_char {
333             // Slash, comment or block comment.
334             '/' => match self.first() {
335                 '/' => self.line_comment(),
336                 '*' => self.block_comment(),
337                 _ => Slash,
338             },
339
340             // Whitespace sequence.
341             c if is_whitespace(c) => self.whitespace(),
342
343             // Raw identifier, raw string literal or identifier.
344             'r' => match (self.first(), self.second()) {
345                 ('#', c1) if is_id_start(c1) => self.raw_ident(),
346                 ('#', _) | ('"', _) => {
347                     let res = self.raw_double_quoted_string(1);
348                     let suffix_start = self.pos_within_token();
349                     if res.is_ok() {
350                         self.eat_literal_suffix();
351                     }
352                     let kind = RawStr { n_hashes: res.ok() };
353                     Literal { kind, suffix_start }
354                 }
355                 _ => self.ident_or_unknown_prefix(),
356             },
357
358             // Byte literal, byte string literal, raw byte string literal or identifier.
359             'b' => match (self.first(), self.second()) {
360                 ('\'', _) => {
361                     self.bump();
362                     let terminated = self.single_quoted_string();
363                     let suffix_start = self.pos_within_token();
364                     if terminated {
365                         self.eat_literal_suffix();
366                     }
367                     let kind = Byte { terminated };
368                     Literal { kind, suffix_start }
369                 }
370                 ('"', _) => {
371                     self.bump();
372                     let terminated = self.double_quoted_string();
373                     let suffix_start = self.pos_within_token();
374                     if terminated {
375                         self.eat_literal_suffix();
376                     }
377                     let kind = ByteStr { terminated };
378                     Literal { kind, suffix_start }
379                 }
380                 ('r', '"') | ('r', '#') => {
381                     self.bump();
382                     let res = self.raw_double_quoted_string(2);
383                     let suffix_start = self.pos_within_token();
384                     if res.is_ok() {
385                         self.eat_literal_suffix();
386                     }
387                     let kind = RawByteStr { n_hashes: res.ok() };
388                     Literal { kind, suffix_start }
389                 }
390                 _ => self.ident_or_unknown_prefix(),
391             },
392
393             // Identifier (this should be checked after other variant that can
394             // start as identifier).
395             c if is_id_start(c) => self.ident_or_unknown_prefix(),
396
397             // Numeric literal.
398             c @ '0'..='9' => {
399                 let literal_kind = self.number(c);
400                 let suffix_start = self.pos_within_token();
401                 self.eat_literal_suffix();
402                 TokenKind::Literal { kind: literal_kind, suffix_start }
403             }
404
405             // One-symbol tokens.
406             ';' => Semi,
407             ',' => Comma,
408             '.' => Dot,
409             '(' => OpenParen,
410             ')' => CloseParen,
411             '{' => OpenBrace,
412             '}' => CloseBrace,
413             '[' => OpenBracket,
414             ']' => CloseBracket,
415             '@' => At,
416             '#' => Pound,
417             '~' => Tilde,
418             '?' => Question,
419             ':' => Colon,
420             '$' => Dollar,
421             '=' => Eq,
422             '!' => Bang,
423             '<' => Lt,
424             '>' => Gt,
425             '-' => Minus,
426             '&' => And,
427             '|' => Or,
428             '+' => Plus,
429             '*' => Star,
430             '^' => Caret,
431             '%' => Percent,
432
433             // Lifetime or character literal.
434             '\'' => self.lifetime_or_char(),
435
436             // String literal.
437             '"' => {
438                 let terminated = self.double_quoted_string();
439                 let suffix_start = self.pos_within_token();
440                 if terminated {
441                     self.eat_literal_suffix();
442                 }
443                 let kind = Str { terminated };
444                 Literal { kind, suffix_start }
445             }
446             // Identifier starting with an emoji. Only lexed for graceful error recovery.
447             c if !c.is_ascii() && unic_emoji_char::is_emoji(c) => {
448                 self.fake_ident_or_unknown_prefix()
449             }
450             _ => Unknown,
451         };
452         let res = Token::new(token_kind, self.pos_within_token());
453         self.reset_pos_within_token();
454         res
455     }
456
457     fn line_comment(&mut self) -> TokenKind {
458         debug_assert!(self.prev() == '/' && self.first() == '/');
459         self.bump();
460
461         let doc_style = match self.first() {
462             // `//!` is an inner line doc comment.
463             '!' => Some(DocStyle::Inner),
464             // `////` (more than 3 slashes) is not considered a doc comment.
465             '/' if self.second() != '/' => Some(DocStyle::Outer),
466             _ => None,
467         };
468
469         self.eat_while(|c| c != '\n');
470         LineComment { doc_style }
471     }
472
473     fn block_comment(&mut self) -> TokenKind {
474         debug_assert!(self.prev() == '/' && self.first() == '*');
475         self.bump();
476
477         let doc_style = match self.first() {
478             // `/*!` is an inner block doc comment.
479             '!' => Some(DocStyle::Inner),
480             // `/***` (more than 2 stars) is not considered a doc comment.
481             // `/**/` is not considered a doc comment.
482             '*' if !matches!(self.second(), '*' | '/') => Some(DocStyle::Outer),
483             _ => None,
484         };
485
486         let mut depth = 1usize;
487         while let Some(c) = self.bump() {
488             match c {
489                 '/' if self.first() == '*' => {
490                     self.bump();
491                     depth += 1;
492                 }
493                 '*' if self.first() == '/' => {
494                     self.bump();
495                     depth -= 1;
496                     if depth == 0 {
497                         // This block comment is closed, so for a construction like "/* */ */"
498                         // there will be a successfully parsed block comment "/* */"
499                         // and " */" will be processed separately.
500                         break;
501                     }
502                 }
503                 _ => (),
504             }
505         }
506
507         BlockComment { doc_style, terminated: depth == 0 }
508     }
509
510     fn whitespace(&mut self) -> TokenKind {
511         debug_assert!(is_whitespace(self.prev()));
512         self.eat_while(is_whitespace);
513         Whitespace
514     }
515
516     fn raw_ident(&mut self) -> TokenKind {
517         debug_assert!(self.prev() == 'r' && self.first() == '#' && is_id_start(self.second()));
518         // Eat "#" symbol.
519         self.bump();
520         // Eat the identifier part of RawIdent.
521         self.eat_identifier();
522         RawIdent
523     }
524
525     fn ident_or_unknown_prefix(&mut self) -> TokenKind {
526         debug_assert!(is_id_start(self.prev()));
527         // Start is already eaten, eat the rest of identifier.
528         self.eat_while(is_id_continue);
529         // Known prefixes must have been handled earlier. So if
530         // we see a prefix here, it is definitely an unknown prefix.
531         match self.first() {
532             '#' | '"' | '\'' => UnknownPrefix,
533             c if !c.is_ascii() && unic_emoji_char::is_emoji(c) => {
534                 self.fake_ident_or_unknown_prefix()
535             }
536             _ => Ident,
537         }
538     }
539
540     fn fake_ident_or_unknown_prefix(&mut self) -> TokenKind {
541         // Start is already eaten, eat the rest of identifier.
542         self.eat_while(|c| {
543             unicode_xid::UnicodeXID::is_xid_continue(c)
544                 || (!c.is_ascii() && unic_emoji_char::is_emoji(c))
545                 || c == '\u{200d}'
546         });
547         // Known prefixes must have been handled earlier. So if
548         // we see a prefix here, it is definitely an unknown prefix.
549         match self.first() {
550             '#' | '"' | '\'' => UnknownPrefix,
551             _ => InvalidIdent,
552         }
553     }
554
555     fn number(&mut self, first_digit: char) -> LiteralKind {
556         debug_assert!('0' <= self.prev() && self.prev() <= '9');
557         let mut base = Base::Decimal;
558         if first_digit == '0' {
559             // Attempt to parse encoding base.
560             let has_digits = match self.first() {
561                 'b' => {
562                     base = Base::Binary;
563                     self.bump();
564                     self.eat_decimal_digits()
565                 }
566                 'o' => {
567                     base = Base::Octal;
568                     self.bump();
569                     self.eat_decimal_digits()
570                 }
571                 'x' => {
572                     base = Base::Hexadecimal;
573                     self.bump();
574                     self.eat_hexadecimal_digits()
575                 }
576                 // Not a base prefix.
577                 '0'..='9' | '_' | '.' | 'e' | 'E' => {
578                     self.eat_decimal_digits();
579                     true
580                 }
581                 // Just a 0.
582                 _ => return Int { base, empty_int: false },
583             };
584             // Base prefix was provided, but there were no digits
585             // after it, e.g. "0x".
586             if !has_digits {
587                 return Int { base, empty_int: true };
588             }
589         } else {
590             // No base prefix, parse number in the usual way.
591             self.eat_decimal_digits();
592         };
593
594         match self.first() {
595             // Don't be greedy if this is actually an
596             // integer literal followed by field/method access or a range pattern
597             // (`0..2` and `12.foo()`)
598             '.' if self.second() != '.' && !is_id_start(self.second()) => {
599                 // might have stuff after the ., and if it does, it needs to start
600                 // with a number
601                 self.bump();
602                 let mut empty_exponent = false;
603                 if self.first().is_digit(10) {
604                     self.eat_decimal_digits();
605                     match self.first() {
606                         'e' | 'E' => {
607                             self.bump();
608                             empty_exponent = !self.eat_float_exponent();
609                         }
610                         _ => (),
611                     }
612                 }
613                 Float { base, empty_exponent }
614             }
615             'e' | 'E' => {
616                 self.bump();
617                 let empty_exponent = !self.eat_float_exponent();
618                 Float { base, empty_exponent }
619             }
620             _ => Int { base, empty_int: false },
621         }
622     }
623
624     fn lifetime_or_char(&mut self) -> TokenKind {
625         debug_assert!(self.prev() == '\'');
626
627         let can_be_a_lifetime = if self.second() == '\'' {
628             // It's surely not a lifetime.
629             false
630         } else {
631             // If the first symbol is valid for identifier, it can be a lifetime.
632             // Also check if it's a number for a better error reporting (so '0 will
633             // be reported as invalid lifetime and not as unterminated char literal).
634             is_id_start(self.first()) || self.first().is_digit(10)
635         };
636
637         if !can_be_a_lifetime {
638             let terminated = self.single_quoted_string();
639             let suffix_start = self.pos_within_token();
640             if terminated {
641                 self.eat_literal_suffix();
642             }
643             let kind = Char { terminated };
644             return Literal { kind, suffix_start };
645         }
646
647         // Either a lifetime or a character literal with
648         // length greater than 1.
649
650         let starts_with_number = self.first().is_digit(10);
651
652         // Skip the literal contents.
653         // First symbol can be a number (which isn't a valid identifier start),
654         // so skip it without any checks.
655         self.bump();
656         self.eat_while(is_id_continue);
657
658         // Check if after skipping literal contents we've met a closing
659         // single quote (which means that user attempted to create a
660         // string with single quotes).
661         if self.first() == '\'' {
662             self.bump();
663             let kind = Char { terminated: true };
664             Literal { kind, suffix_start: self.pos_within_token() }
665         } else {
666             Lifetime { starts_with_number }
667         }
668     }
669
670     fn single_quoted_string(&mut self) -> bool {
671         debug_assert!(self.prev() == '\'');
672         // Check if it's a one-symbol literal.
673         if self.second() == '\'' && self.first() != '\\' {
674             self.bump();
675             self.bump();
676             return true;
677         }
678
679         // Literal has more than one symbol.
680
681         // Parse until either quotes are terminated or error is detected.
682         loop {
683             match self.first() {
684                 // Quotes are terminated, finish parsing.
685                 '\'' => {
686                     self.bump();
687                     return true;
688                 }
689                 // Probably beginning of the comment, which we don't want to include
690                 // to the error report.
691                 '/' => break,
692                 // Newline without following '\'' means unclosed quote, stop parsing.
693                 '\n' if self.second() != '\'' => break,
694                 // End of file, stop parsing.
695                 EOF_CHAR if self.is_eof() => break,
696                 // Escaped slash is considered one character, so bump twice.
697                 '\\' => {
698                     self.bump();
699                     self.bump();
700                 }
701                 // Skip the character.
702                 _ => {
703                     self.bump();
704                 }
705             }
706         }
707         // String was not terminated.
708         false
709     }
710
711     /// Eats double-quoted string and returns true
712     /// if string is terminated.
713     fn double_quoted_string(&mut self) -> bool {
714         debug_assert!(self.prev() == '"');
715         while let Some(c) = self.bump() {
716             match c {
717                 '"' => {
718                     return true;
719                 }
720                 '\\' if self.first() == '\\' || self.first() == '"' => {
721                     // Bump again to skip escaped character.
722                     self.bump();
723                 }
724                 _ => (),
725             }
726         }
727         // End of file reached.
728         false
729     }
730
731     /// Eats the double-quoted string and returns `n_hashes` and an error if encountered.
732     fn raw_double_quoted_string(&mut self, prefix_len: u32) -> Result<u8, RawStrError> {
733         // Wrap the actual function to handle the error with too many hashes.
734         // This way, it eats the whole raw string.
735         let n_hashes = self.raw_string_unvalidated(prefix_len)?;
736         // Only up to 255 `#`s are allowed in raw strings
737         match u8::try_from(n_hashes) {
738             Ok(num) => Ok(num),
739             Err(_) => Err(RawStrError::TooManyDelimiters { found: n_hashes }),
740         }
741     }
742
743     fn raw_string_unvalidated(&mut self, prefix_len: u32) -> Result<u32, RawStrError> {
744         debug_assert!(self.prev() == 'r');
745         let start_pos = self.pos_within_token();
746         let mut possible_terminator_offset = None;
747         let mut max_hashes = 0;
748
749         // Count opening '#' symbols.
750         let mut eaten = 0;
751         while self.first() == '#' {
752             eaten += 1;
753             self.bump();
754         }
755         let n_start_hashes = eaten;
756
757         // Check that string is started.
758         match self.bump() {
759             Some('"') => (),
760             c => {
761                 let c = c.unwrap_or(EOF_CHAR);
762                 return Err(RawStrError::InvalidStarter { bad_char: c });
763             }
764         }
765
766         // Skip the string contents and on each '#' character met, check if this is
767         // a raw string termination.
768         loop {
769             self.eat_while(|c| c != '"');
770
771             if self.is_eof() {
772                 return Err(RawStrError::NoTerminator {
773                     expected: n_start_hashes,
774                     found: max_hashes,
775                     possible_terminator_offset,
776                 });
777             }
778
779             // Eat closing double quote.
780             self.bump();
781
782             // Check that amount of closing '#' symbols
783             // is equal to the amount of opening ones.
784             // Note that this will not consume extra trailing `#` characters:
785             // `r###"abcde"####` is lexed as a `RawStr { n_hashes: 3 }`
786             // followed by a `#` token.
787             let mut n_end_hashes = 0;
788             while self.first() == '#' && n_end_hashes < n_start_hashes {
789                 n_end_hashes += 1;
790                 self.bump();
791             }
792
793             if n_end_hashes == n_start_hashes {
794                 return Ok(n_start_hashes);
795             } else if n_end_hashes > max_hashes {
796                 // Keep track of possible terminators to give a hint about
797                 // where there might be a missing terminator
798                 possible_terminator_offset =
799                     Some(self.pos_within_token() - start_pos - n_end_hashes + prefix_len);
800                 max_hashes = n_end_hashes;
801             }
802         }
803     }
804
805     fn eat_decimal_digits(&mut self) -> bool {
806         let mut has_digits = false;
807         loop {
808             match self.first() {
809                 '_' => {
810                     self.bump();
811                 }
812                 '0'..='9' => {
813                     has_digits = true;
814                     self.bump();
815                 }
816                 _ => break,
817             }
818         }
819         has_digits
820     }
821
822     fn eat_hexadecimal_digits(&mut self) -> bool {
823         let mut has_digits = false;
824         loop {
825             match self.first() {
826                 '_' => {
827                     self.bump();
828                 }
829                 '0'..='9' | 'a'..='f' | 'A'..='F' => {
830                     has_digits = true;
831                     self.bump();
832                 }
833                 _ => break,
834             }
835         }
836         has_digits
837     }
838
839     /// Eats the float exponent. Returns true if at least one digit was met,
840     /// and returns false otherwise.
841     fn eat_float_exponent(&mut self) -> bool {
842         debug_assert!(self.prev() == 'e' || self.prev() == 'E');
843         if self.first() == '-' || self.first() == '+' {
844             self.bump();
845         }
846         self.eat_decimal_digits()
847     }
848
849     // Eats the suffix of the literal, e.g. "u8".
850     fn eat_literal_suffix(&mut self) {
851         self.eat_identifier();
852     }
853
854     // Eats the identifier. Note: succeeds on `_`, which isn't a valid
855     // identifer.
856     fn eat_identifier(&mut self) {
857         if !is_id_start(self.first()) {
858             return;
859         }
860         self.bump();
861
862         self.eat_while(is_id_continue);
863     }
864 }