1 //! Low-level Rust lexer.
3 //! The idea with `librustc_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.
9 //! Tokens produced by this lexer are not yet ready for parsing the Rust syntax.
10 //! For that see [`librustc_parse::lexer`], which converts this basic token stream
11 //! into wide tokens used by actual parser.
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
17 //! The main entity of this crate is the [`TokenKind`] enum which represents common
20 //! [`librustc_parse::lexer`]: ../rustc_parse/lexer/index.html
21 // We want to be able to build this crate with a stable compiler, so no
22 // `#![feature]` attributes should be added.
30 use self::LiteralKind::*;
31 use self::TokenKind::*;
32 use crate::cursor::{Cursor, EOF_CHAR};
33 use std::convert::TryFrom;
36 /// It doesn't contain information about data that has been parsed,
37 /// only the type of the token and its size.
45 fn new(kind: TokenKind, len: usize) -> Token {
50 /// Enum representing common lexeme types.
51 // perf note: Changing all `usize` to `u32` doesn't change performance. See #77629
52 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
56 LineComment { doc_style: Option<DocStyle> },
57 /// `/* block comment */`
59 /// Block comments can be recursive, so the sequence like `/* /* */`
60 /// will not be considered terminated and will result in a parsing error.
61 BlockComment { doc_style: Option<DocStyle>, terminated: bool },
62 /// Any whitespace characters sequence.
64 /// "ident" or "continue"
65 /// At this step keywords are also considered identifiers.
69 /// "12_u8", "1.0e-40", "b"123"". See `LiteralKind` for more details.
70 Literal { kind: LiteralKind, suffix_start: usize },
72 Lifetime { starts_with_number: bool },
130 /// Unknown token, not expected by the lexer, e.g. "№"
134 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
140 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
141 pub enum LiteralKind {
142 /// "12_u8", "0o100", "0b120i99"
143 Int { base: Base, empty_int: bool },
144 /// "12.34f32", "0b100.100"
145 Float { base: Base, empty_exponent: bool },
146 /// "'a'", "'\\'", "'''", "';"
147 Char { terminated: bool },
148 /// "b'a'", "b'\\'", "b'''", "b';"
149 Byte { terminated: bool },
151 Str { terminated: bool },
152 /// "b"abc"", "b"abc"
153 ByteStr { terminated: bool },
154 /// "r"abc"", "r#"abc"#", "r####"ab"###"c"####", "r#"a"
155 RawStr { n_hashes: u16, err: Option<RawStrError> },
156 /// "br"abc"", "br#"abc"#", "br####"ab"###"c"####", "br#"a"
157 RawByteStr { n_hashes: u16, err: Option<RawStrError> },
160 /// Error produced validating a raw string. Represents cases like:
161 /// - `r##~"abcde"##`: `InvalidStarter`
162 /// - `r###"abcde"##`: `NoTerminator { expected: 3, found: 2, possible_terminator_offset: Some(11)`
163 /// - Too many `#`s (>65535): `TooManyDelimiters`
164 // perf note: It doesn't matter that this makes `Token` 36 bytes bigger. See #77629
165 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
166 pub enum RawStrError {
167 /// Non `#` characters exist between `r` and `"` eg. `r#~"..`
168 InvalidStarter { bad_char: char },
169 /// The string was never terminated. `possible_terminator_offset` is the number of characters after `r` or `br` where they
170 /// may have intended to terminate it.
171 NoTerminator { expected: usize, found: usize, possible_terminator_offset: Option<usize> },
172 /// More than 65535 `#`s exist.
173 TooManyDelimiters { found: usize },
176 /// Base of numeric literal encoding according to its prefix.
177 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
179 /// Literal starts with "0b".
181 /// Literal starts with "0o".
183 /// Literal starts with "0x".
185 /// Literal doesn't contain a prefix.
189 /// `rustc` allows files to have a shebang, e.g. "#!/usr/bin/rustrun",
190 /// but shebang isn't a part of rust syntax.
191 pub fn strip_shebang(input: &str) -> Option<usize> {
192 // Shebang must start with `#!` literally, without any preceding whitespace.
193 // For simplicity we consider any line starting with `#!` a shebang,
194 // regardless of restrictions put on shebangs by specific platforms.
195 if let Some(input_tail) = input.strip_prefix("#!") {
196 // Ok, this is a shebang but if the next non-whitespace token is `[`,
197 // then it may be valid Rust code, so consider it Rust code.
198 let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok| {
201 TokenKind::Whitespace
202 | TokenKind::LineComment { doc_style: None }
203 | TokenKind::BlockComment { doc_style: None, .. }
206 if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
207 // No other choice than to consider this a shebang.
208 return Some(2 + input_tail.lines().next().unwrap_or_default().len());
214 /// Parses the first token from the provided input string.
215 pub fn first_token(input: &str) -> Token {
216 debug_assert!(!input.is_empty());
217 Cursor::new(input).advance_token()
220 /// Creates an iterator that produces tokens from the input string.
221 pub fn tokenize(mut input: &str) -> impl Iterator<Item = Token> + '_ {
222 std::iter::from_fn(move || {
223 if input.is_empty() {
226 let token = first_token(input);
227 input = &input[token.len..];
232 /// True if `c` is considered a whitespace according to Rust language definition.
233 /// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
234 /// for definitions of these classes.
235 pub fn is_whitespace(c: char) -> bool {
236 // This is Pattern_White_Space.
238 // Note that this set is stable (ie, it doesn't change with different
239 // Unicode versions), so it's ok to just hard-code the values.
243 // Usual ASCII suspects
246 | '\u{000B}' // vertical tab
247 | '\u{000C}' // form feed
249 | '\u{0020}' // space
251 // NEXT LINE from latin1
255 | '\u{200E}' // LEFT-TO-RIGHT MARK
256 | '\u{200F}' // RIGHT-TO-LEFT MARK
258 // Dedicated whitespace characters from Unicode
259 | '\u{2028}' // LINE SEPARATOR
260 | '\u{2029}' // PARAGRAPH SEPARATOR
264 /// True if `c` is valid as a first character of an identifier.
265 /// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
266 /// a formal definition of valid identifier name.
267 pub fn is_id_start(c: char) -> bool {
268 // This is XID_Start OR '_' (which formally is not a XID_Start).
269 // We also add fast-path for ascii idents
270 ('a'..='z').contains(&c)
271 || ('A'..='Z').contains(&c)
273 || (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_start(c))
276 /// True if `c` is valid as a non-first character of an identifier.
277 /// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
278 /// a formal definition of valid identifier name.
279 pub fn is_id_continue(c: char) -> bool {
280 // This is exactly XID_Continue.
281 // We also add fast-path for ascii idents
282 ('a'..='z').contains(&c)
283 || ('A'..='Z').contains(&c)
284 || ('0'..='9').contains(&c)
286 || (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_continue(c))
289 /// The passed string is lexically an identifier.
290 pub fn is_ident(string: &str) -> bool {
291 let mut chars = string.chars();
292 if let Some(start) = chars.next() {
293 is_id_start(start) && chars.all(is_id_continue)
300 /// Parses a token from the input string.
301 fn advance_token(&mut self) -> Token {
302 let first_char = self.bump().unwrap();
303 let token_kind = match first_char {
304 // Slash, comment or block comment.
305 '/' => match self.first() {
306 '/' => self.line_comment(),
307 '*' => self.block_comment(),
311 // Whitespace sequence.
312 c if is_whitespace(c) => self.whitespace(),
314 // Raw identifier, raw string literal or identifier.
315 'r' => match (self.first(), self.second()) {
316 ('#', c1) if is_id_start(c1) => self.raw_ident(),
317 ('#', _) | ('"', _) => {
318 let (n_hashes, err) = self.raw_double_quoted_string(1);
319 let suffix_start = self.len_consumed();
321 self.eat_literal_suffix();
323 let kind = RawStr { n_hashes, err };
324 Literal { kind, suffix_start }
329 // Byte literal, byte string literal, raw byte string literal or identifier.
330 'b' => match (self.first(), self.second()) {
333 let terminated = self.single_quoted_string();
334 let suffix_start = self.len_consumed();
336 self.eat_literal_suffix();
338 let kind = Byte { terminated };
339 Literal { kind, suffix_start }
343 let terminated = self.double_quoted_string();
344 let suffix_start = self.len_consumed();
346 self.eat_literal_suffix();
348 let kind = ByteStr { terminated };
349 Literal { kind, suffix_start }
351 ('r', '"') | ('r', '#') => {
353 let (n_hashes, err) = self.raw_double_quoted_string(2);
354 let suffix_start = self.len_consumed();
356 self.eat_literal_suffix();
358 let kind = RawByteStr { n_hashes, err };
359 Literal { kind, suffix_start }
364 // Identifier (this should be checked after other variant that can
365 // start as identifier).
366 c if is_id_start(c) => self.ident(),
370 let literal_kind = self.number(c);
371 let suffix_start = self.len_consumed();
372 self.eat_literal_suffix();
373 TokenKind::Literal { kind: literal_kind, suffix_start }
376 // One-symbol tokens.
404 // Lifetime or character literal.
405 '\'' => self.lifetime_or_char(),
409 let terminated = self.double_quoted_string();
410 let suffix_start = self.len_consumed();
412 self.eat_literal_suffix();
414 let kind = Str { terminated };
415 Literal { kind, suffix_start }
419 Token::new(token_kind, self.len_consumed())
422 fn line_comment(&mut self) -> TokenKind {
423 debug_assert!(self.prev() == '/' && self.first() == '/');
426 let doc_style = match self.first() {
427 // `//!` is an inner line doc comment.
428 '!' => Some(DocStyle::Inner),
429 // `////` (more than 3 slashes) is not considered a doc comment.
430 '/' if self.second() != '/' => Some(DocStyle::Outer),
434 self.eat_while(|c| c != '\n');
435 LineComment { doc_style }
438 fn block_comment(&mut self) -> TokenKind {
439 debug_assert!(self.prev() == '/' && self.first() == '*');
442 let doc_style = match self.first() {
443 // `/*!` is an inner block doc comment.
444 '!' => Some(DocStyle::Inner),
445 // `/***` (more than 2 stars) is not considered a doc comment.
446 // `/**/` is not considered a doc comment.
447 '*' if !matches!(self.second(), '*' | '/') => Some(DocStyle::Outer),
451 let mut depth = 1usize;
452 while let Some(c) = self.bump() {
454 '/' if self.first() == '*' => {
458 '*' if self.first() == '/' => {
462 // This block comment is closed, so for a construction like "/* */ */"
463 // there will be a successfully parsed block comment "/* */"
464 // and " */" will be processed separately.
472 BlockComment { doc_style, terminated: depth == 0 }
475 fn whitespace(&mut self) -> TokenKind {
476 debug_assert!(is_whitespace(self.prev()));
477 self.eat_while(is_whitespace);
481 fn raw_ident(&mut self) -> TokenKind {
482 debug_assert!(self.prev() == 'r' && self.first() == '#' && is_id_start(self.second()));
485 // Eat the identifier part of RawIdent.
486 self.eat_identifier();
490 fn ident(&mut self) -> TokenKind {
491 debug_assert!(is_id_start(self.prev()));
492 // Start is already eaten, eat the rest of identifier.
493 self.eat_while(is_id_continue);
497 fn number(&mut self, first_digit: char) -> LiteralKind {
498 debug_assert!('0' <= self.prev() && self.prev() <= '9');
499 let mut base = Base::Decimal;
500 if first_digit == '0' {
501 // Attempt to parse encoding base.
502 let has_digits = match self.first() {
506 self.eat_decimal_digits()
511 self.eat_decimal_digits()
514 base = Base::Hexadecimal;
516 self.eat_hexadecimal_digits()
518 // Not a base prefix.
519 '0'..='9' | '_' | '.' | 'e' | 'E' => {
520 self.eat_decimal_digits();
524 _ => return Int { base, empty_int: false },
526 // Base prefix was provided, but there were no digits
527 // after it, e.g. "0x".
529 return Int { base, empty_int: true };
532 // No base prefix, parse number in the usual way.
533 self.eat_decimal_digits();
537 // Don't be greedy if this is actually an
538 // integer literal followed by field/method access or a range pattern
539 // (`0..2` and `12.foo()`)
540 '.' if self.second() != '.' && !is_id_start(self.second()) => {
541 // might have stuff after the ., and if it does, it needs to start
544 let mut empty_exponent = false;
545 if self.first().is_digit(10) {
546 self.eat_decimal_digits();
550 empty_exponent = !self.eat_float_exponent();
555 Float { base, empty_exponent }
559 let empty_exponent = !self.eat_float_exponent();
560 Float { base, empty_exponent }
562 _ => Int { base, empty_int: false },
566 fn lifetime_or_char(&mut self) -> TokenKind {
567 debug_assert!(self.prev() == '\'');
569 let can_be_a_lifetime = if self.second() == '\'' {
570 // It's surely not a lifetime.
573 // If the first symbol is valid for identifier, it can be a lifetime.
574 // Also check if it's a number for a better error reporting (so '0 will
575 // be reported as invalid lifetime and not as unterminated char literal).
576 is_id_start(self.first()) || self.first().is_digit(10)
579 if !can_be_a_lifetime {
580 let terminated = self.single_quoted_string();
581 let suffix_start = self.len_consumed();
583 self.eat_literal_suffix();
585 let kind = Char { terminated };
586 return Literal { kind, suffix_start };
589 // Either a lifetime or a character literal with
590 // length greater than 1.
592 let starts_with_number = self.first().is_digit(10);
594 // Skip the literal contents.
595 // First symbol can be a number (which isn't a valid identifier start),
596 // so skip it without any checks.
598 self.eat_while(is_id_continue);
600 // Check if after skipping literal contents we've met a closing
601 // single quote (which means that user attempted to create a
602 // string with single quotes).
603 if self.first() == '\'' {
605 let kind = Char { terminated: true };
606 Literal { kind, suffix_start: self.len_consumed() }
608 Lifetime { starts_with_number }
612 fn single_quoted_string(&mut self) -> bool {
613 debug_assert!(self.prev() == '\'');
614 // Check if it's a one-symbol literal.
615 if self.second() == '\'' && self.first() != '\\' {
621 // Literal has more than one symbol.
623 // Parse until either quotes are terminated or error is detected.
626 // Quotes are terminated, finish parsing.
631 // Probably beginning of the comment, which we don't want to include
632 // to the error report.
634 // Newline without following '\'' means unclosed quote, stop parsing.
635 '\n' if self.second() != '\'' => break,
636 // End of file, stop parsing.
637 EOF_CHAR if self.is_eof() => break,
638 // Escaped slash is considered one character, so bump twice.
643 // Skip the character.
649 // String was not terminated.
653 /// Eats double-quoted string and returns true
654 /// if string is terminated.
655 fn double_quoted_string(&mut self) -> bool {
656 debug_assert!(self.prev() == '"');
657 while let Some(c) = self.bump() {
662 '\\' if self.first() == '\\' || self.first() == '"' => {
663 // Bump again to skip escaped character.
669 // End of file reached.
673 /// Eats the double-quoted string and returns `n_hashes` and an error if encountered.
674 fn raw_double_quoted_string(&mut self, prefix_len: usize) -> (u16, Option<RawStrError>) {
675 // Wrap the actual function to handle the error with too many hashes.
676 // This way, it eats the whole raw string.
677 let (n_hashes, err) = self.raw_string_unvalidated(prefix_len);
678 // Only up to 65535 `#`s are allowed in raw strings
679 match u16::try_from(n_hashes) {
680 Ok(num) => (num, err),
681 // We lie about the number of hashes here :P
682 Err(_) => (0, Some(RawStrError::TooManyDelimiters { found: n_hashes })),
686 fn raw_string_unvalidated(&mut self, prefix_len: usize) -> (usize, Option<RawStrError>) {
687 debug_assert!(self.prev() == 'r');
688 let start_pos = self.len_consumed();
689 let mut possible_terminator_offset = None;
690 let mut max_hashes = 0;
692 // Count opening '#' symbols.
694 while self.first() == '#' {
698 let n_start_hashes = eaten;
700 // Check that string is started.
704 let c = c.unwrap_or(EOF_CHAR);
705 return (n_start_hashes, Some(RawStrError::InvalidStarter { bad_char: c }));
709 // Skip the string contents and on each '#' character met, check if this is
710 // a raw string termination.
712 self.eat_while(|c| c != '"');
717 Some(RawStrError::NoTerminator {
718 expected: n_start_hashes,
720 possible_terminator_offset,
725 // Eat closing double quote.
728 // Check that amount of closing '#' symbols
729 // is equal to the amount of opening ones.
730 // Note that this will not consume extra trailing `#` characters:
731 // `r###"abcde"####` is lexed as a `RawStr { n_hashes: 3 }`
732 // followed by a `#` token.
733 let mut n_end_hashes = 0;
734 while self.first() == '#' && n_end_hashes < n_start_hashes {
739 if n_end_hashes == n_start_hashes {
740 return (n_start_hashes, None);
741 } else if n_end_hashes > max_hashes {
742 // Keep track of possible terminators to give a hint about
743 // where there might be a missing terminator
744 possible_terminator_offset =
745 Some(self.len_consumed() - start_pos - n_end_hashes + prefix_len);
746 max_hashes = n_end_hashes;
751 fn eat_decimal_digits(&mut self) -> bool {
752 let mut has_digits = false;
768 fn eat_hexadecimal_digits(&mut self) -> bool {
769 let mut has_digits = false;
775 '0'..='9' | 'a'..='f' | 'A'..='F' => {
785 /// Eats the float exponent. Returns true if at least one digit was met,
786 /// and returns false otherwise.
787 fn eat_float_exponent(&mut self) -> bool {
788 debug_assert!(self.prev() == 'e' || self.prev() == 'E');
789 if self.first() == '-' || self.first() == '+' {
792 self.eat_decimal_digits()
795 // Eats the suffix of the literal, e.g. "_u8".
796 fn eat_literal_suffix(&mut self) {
797 self.eat_identifier();
800 // Eats the identifier.
801 fn eat_identifier(&mut self) {
802 if !is_id_start(self.first()) {
807 self.eat_while(is_id_continue);
810 /// Eats symbols while predicate returns true or until the end of file is reached.
811 fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
812 while predicate(self.first()) && !self.is_eof() {