2019-10-26 11:12:58 -05:00
|
|
|
//! Low-level Rust lexer.
|
|
|
|
//!
|
|
|
|
//! Tokens produced by this lexer are not yet ready for parsing the Rust syntax,
|
|
|
|
//! for that see `libsyntax::parse::lexer`, which converts this basic token stream
|
|
|
|
//! into wide tokens used by actual parser.
|
|
|
|
//!
|
|
|
|
//! The purpose of this crate is to convert raw sources into a labeled sequence
|
|
|
|
//! of well-known token types, so building an actual Rust token stream will
|
|
|
|
//! be easier.
|
|
|
|
//!
|
|
|
|
//! Main entity of this crate is [`TokenKind`] enum which represents common
|
|
|
|
//! lexeme types.
|
|
|
|
|
2019-07-21 06:50:39 -05:00
|
|
|
// We want to be able to build this crate with a stable compiler, so no
|
|
|
|
// `#![feature]` attributes should be added.
|
2019-05-06 03:53:40 -05:00
|
|
|
|
|
|
|
mod cursor;
|
2019-07-21 08:46:11 -05:00
|
|
|
pub mod unescape;
|
2019-05-06 03:53:40 -05:00
|
|
|
|
|
|
|
use crate::cursor::{Cursor, EOF_CHAR};
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Parsed token.
|
|
|
|
/// It doesn't contain information about data that has been parsed,
|
|
|
|
/// only the type of the token and its size.
|
2019-05-06 03:53:40 -05:00
|
|
|
pub struct Token {
|
|
|
|
pub kind: TokenKind,
|
|
|
|
pub len: usize,
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
impl Token {
|
|
|
|
fn new(kind: TokenKind, len: usize) -> Token {
|
|
|
|
Token { kind, len }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enum represening common lexeme types.
|
2019-05-06 03:53:40 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum TokenKind {
|
2019-10-26 11:12:58 -05:00
|
|
|
// Multi-char tokens:
|
|
|
|
|
|
|
|
/// "// comment"
|
2019-05-06 03:53:40 -05:00
|
|
|
LineComment,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "/* block comment */"
|
|
|
|
/// Block comments can be recursive, so the sequence like "/* /* */"
|
|
|
|
/// will not be considered terminated and will result in a parsing error.
|
2019-05-06 03:53:40 -05:00
|
|
|
BlockComment { terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Any whitespace characters sequence.
|
2019-05-06 03:53:40 -05:00
|
|
|
Whitespace,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "ident" or "continue"
|
|
|
|
/// At this step keywords are also considered identifiers.
|
2019-05-06 03:53:40 -05:00
|
|
|
Ident,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "r#ident"
|
2019-05-06 03:53:40 -05:00
|
|
|
RawIdent,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "12_u8", "1.0e-40", "b"123"". See `LiteralKind` for more details.
|
2019-05-06 03:53:40 -05:00
|
|
|
Literal { kind: LiteralKind, suffix_start: usize },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "'a"
|
2019-05-06 03:53:40 -05:00
|
|
|
Lifetime { starts_with_number: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// One-char tokens:
|
|
|
|
|
|
|
|
/// ";"
|
2019-05-06 03:53:40 -05:00
|
|
|
Semi,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// ","
|
2019-05-06 03:53:40 -05:00
|
|
|
Comma,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "."
|
2019-05-06 03:53:40 -05:00
|
|
|
Dot,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "("
|
2019-05-06 03:53:40 -05:00
|
|
|
OpenParen,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// ")"
|
2019-05-06 03:53:40 -05:00
|
|
|
CloseParen,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "{"
|
2019-05-06 03:53:40 -05:00
|
|
|
OpenBrace,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "}"
|
2019-05-06 03:53:40 -05:00
|
|
|
CloseBrace,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "["
|
2019-05-06 03:53:40 -05:00
|
|
|
OpenBracket,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "]"
|
2019-05-06 03:53:40 -05:00
|
|
|
CloseBracket,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "@"
|
2019-05-06 03:53:40 -05:00
|
|
|
At,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "#"
|
2019-05-06 03:53:40 -05:00
|
|
|
Pound,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "~"
|
2019-05-06 03:53:40 -05:00
|
|
|
Tilde,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "?"
|
2019-05-06 03:53:40 -05:00
|
|
|
Question,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// ":"
|
2019-05-06 03:53:40 -05:00
|
|
|
Colon,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "$"
|
2019-05-06 03:53:40 -05:00
|
|
|
Dollar,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "="
|
2019-05-06 03:53:40 -05:00
|
|
|
Eq,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "!"
|
2019-05-06 03:53:40 -05:00
|
|
|
Not,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "<"
|
2019-05-06 03:53:40 -05:00
|
|
|
Lt,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// ">"
|
2019-05-06 03:53:40 -05:00
|
|
|
Gt,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "-"
|
2019-05-06 03:53:40 -05:00
|
|
|
Minus,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "&"
|
2019-05-06 03:53:40 -05:00
|
|
|
And,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "|"
|
2019-05-06 03:53:40 -05:00
|
|
|
Or,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "+"
|
2019-05-06 03:53:40 -05:00
|
|
|
Plus,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "*"
|
2019-05-06 03:53:40 -05:00
|
|
|
Star,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "/"
|
2019-05-06 03:53:40 -05:00
|
|
|
Slash,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "^"
|
2019-05-06 03:53:40 -05:00
|
|
|
Caret,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "%"
|
2019-05-06 03:53:40 -05:00
|
|
|
Percent,
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
/// Unknown token, not expected by the lexer, e.g. "№"
|
2019-05-06 03:53:40 -05:00
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
use self::TokenKind::*;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum LiteralKind {
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "12_u8", "0o100", "0b120i99"
|
2019-05-06 03:53:40 -05:00
|
|
|
Int { base: Base, empty_int: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "12.34f32", "0b100.100"
|
2019-05-06 03:53:40 -05:00
|
|
|
Float { base: Base, empty_exponent: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "'a'", "'\\'", "'''", "';"
|
2019-05-06 03:53:40 -05:00
|
|
|
Char { terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "b'a'", "b'\\'", "b'''", "b';"
|
2019-05-06 03:53:40 -05:00
|
|
|
Byte { terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// ""abc"", ""abc"
|
2019-05-06 03:53:40 -05:00
|
|
|
Str { terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "b"abc"", "b"abc"
|
2019-05-06 03:53:40 -05:00
|
|
|
ByteStr { terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "r"abc"", "r#"abc"#", "r####"ab"###"c"####", "r#"a"
|
2019-05-06 03:53:40 -05:00
|
|
|
RawStr { n_hashes: usize, started: bool, terminated: bool },
|
2019-10-26 11:12:58 -05:00
|
|
|
/// "br"abc"", "br#"abc"#", "br####"ab"###"c"####", "br#"a"
|
2019-05-06 03:53:40 -05:00
|
|
|
RawByteStr { n_hashes: usize, started: bool, terminated: bool },
|
|
|
|
}
|
|
|
|
use self::LiteralKind::*;
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Base of numeric literal encoding according to its prefix.
|
2019-05-06 03:53:40 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum Base {
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Literal starts with "0b".
|
2019-05-06 03:53:40 -05:00
|
|
|
Binary,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Literal starts with "0o".
|
2019-05-06 03:53:40 -05:00
|
|
|
Octal,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Literal starts with "0x".
|
2019-05-06 03:53:40 -05:00
|
|
|
Hexadecimal,
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Literal doesn't contain a prefix.
|
2019-05-06 03:53:40 -05:00
|
|
|
Decimal,
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// `rustc` allows files to have a shebang, e.g. "#!/usr/bin/rustrun",
|
|
|
|
/// but shebang isn't a part of rust syntax, so this function
|
|
|
|
/// skips the line if it starts with a shebang ("#!").
|
|
|
|
/// Line won't be skipped if it represents a valid Rust syntax
|
|
|
|
/// (e.g. "#![deny(missing_docs)]").
|
2019-05-06 03:53:40 -05:00
|
|
|
pub fn strip_shebang(input: &str) -> Option<usize> {
|
|
|
|
debug_assert!(!input.is_empty());
|
|
|
|
if !input.starts_with("#!") || input.starts_with("#![") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(input.find('\n').unwrap_or(input.len()))
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Parses the first token from the provided input string.
|
2019-05-06 03:53:40 -05:00
|
|
|
pub fn first_token(input: &str) -> Token {
|
|
|
|
debug_assert!(!input.is_empty());
|
|
|
|
Cursor::new(input).advance_token()
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Creates an iterator that produces tokens from the input string.
|
2019-05-06 03:53:40 -05:00
|
|
|
pub fn tokenize(mut input: &str) -> impl Iterator<Item = Token> + '_ {
|
|
|
|
std::iter::from_fn(move || {
|
|
|
|
if input.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let token = first_token(input);
|
|
|
|
input = &input[token.len..];
|
|
|
|
Some(token)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-04 05:16:36 -05:00
|
|
|
/// True if `c` is considered a whitespace according to Rust language definition.
|
2019-10-26 11:12:58 -05:00
|
|
|
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
|
|
|
|
/// for definitions of these classes.
|
2019-09-04 05:16:36 -05:00
|
|
|
pub fn is_whitespace(c: char) -> bool {
|
|
|
|
// This is Pattern_White_Space.
|
|
|
|
//
|
|
|
|
// Note that this set is stable (ie, it doesn't change with different
|
|
|
|
// Unicode versions), so it's ok to just hard-code the values.
|
|
|
|
|
|
|
|
match c {
|
|
|
|
// Usual ASCII suspects
|
|
|
|
| '\u{0009}' // \t
|
|
|
|
| '\u{000A}' // \n
|
|
|
|
| '\u{000B}' // vertical tab
|
|
|
|
| '\u{000C}' // form feed
|
|
|
|
| '\u{000D}' // \r
|
|
|
|
| '\u{0020}' // space
|
|
|
|
|
|
|
|
// NEXT LINE from latin1
|
|
|
|
| '\u{0085}'
|
|
|
|
|
|
|
|
// Bidi markers
|
|
|
|
| '\u{200E}' // LEFT-TO-RIGHT MARK
|
|
|
|
| '\u{200F}' // RIGHT-TO-LEFT MARK
|
|
|
|
|
|
|
|
// Dedicated whitespace characters from Unicode
|
|
|
|
| '\u{2028}' // LINE SEPARATOR
|
|
|
|
| '\u{2029}' // PARAGRAPH SEPARATOR
|
|
|
|
=> true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// True if `c` is valid as a first character of an identifier.
|
2019-10-26 11:12:58 -05:00
|
|
|
/// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
|
|
|
|
/// a formal definition of valid identifier name.
|
2019-09-04 05:16:36 -05:00
|
|
|
pub fn is_id_start(c: char) -> bool {
|
|
|
|
// This is XID_Start OR '_' (which formally is not a XID_Start).
|
|
|
|
// We also add fast-path for ascii idents
|
|
|
|
('a' <= c && c <= 'z')
|
|
|
|
|| ('A' <= c && c <= 'Z')
|
|
|
|
|| c == '_'
|
|
|
|
|| (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_start(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// True if `c` is valid as a non-first character of an identifier.
|
2019-10-26 11:12:58 -05:00
|
|
|
/// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
|
|
|
|
/// a formal definition of valid identifier name.
|
2019-09-04 05:16:36 -05:00
|
|
|
pub fn is_id_continue(c: char) -> bool {
|
|
|
|
// This is exactly XID_Continue.
|
|
|
|
// We also add fast-path for ascii idents
|
|
|
|
('a' <= c && c <= 'z')
|
|
|
|
|| ('A' <= c && c <= 'Z')
|
|
|
|
|| ('0' <= c && c <= '9')
|
|
|
|
|| c == '_'
|
|
|
|
|| (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_continue(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-06 03:53:40 -05:00
|
|
|
impl Cursor<'_> {
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Parses a token from the input string.
|
2019-05-06 03:53:40 -05:00
|
|
|
fn advance_token(&mut self) -> Token {
|
|
|
|
let first_char = self.bump().unwrap();
|
|
|
|
let token_kind = match first_char {
|
2019-10-26 11:12:58 -05:00
|
|
|
// Slash, comment or block comment.
|
2019-05-06 03:53:40 -05:00
|
|
|
'/' => match self.nth_char(0) {
|
|
|
|
'/' => self.line_comment(),
|
|
|
|
'*' => self.block_comment(),
|
2019-08-19 11:00:24 -05:00
|
|
|
_ => Slash,
|
2019-05-06 03:53:40 -05:00
|
|
|
},
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Whitespace sequence.
|
2019-09-04 05:16:36 -05:00
|
|
|
c if is_whitespace(c) => self.whitespace(),
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Raw string literal or identifier.
|
2019-05-06 03:53:40 -05:00
|
|
|
'r' => match (self.nth_char(0), self.nth_char(1)) {
|
2019-09-04 05:16:36 -05:00
|
|
|
('#', c1) if is_id_start(c1) => self.raw_ident(),
|
2019-05-06 03:53:40 -05:00
|
|
|
('#', _) | ('"', _) => {
|
|
|
|
let (n_hashes, started, terminated) = self.raw_double_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = RawStr { n_hashes, started, terminated };
|
|
|
|
Literal { kind, suffix_start }
|
|
|
|
}
|
|
|
|
_ => self.ident(),
|
|
|
|
},
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Byte literal, byte string literal, raw byte string literal or identifier.
|
2019-05-06 03:53:40 -05:00
|
|
|
'b' => match (self.nth_char(0), self.nth_char(1)) {
|
|
|
|
('\'', _) => {
|
|
|
|
self.bump();
|
|
|
|
let terminated = self.single_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = Byte { terminated };
|
|
|
|
Literal { kind, suffix_start }
|
|
|
|
}
|
|
|
|
('"', _) => {
|
|
|
|
self.bump();
|
|
|
|
let terminated = self.double_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = ByteStr { terminated };
|
|
|
|
Literal { kind, suffix_start }
|
|
|
|
}
|
|
|
|
('r', '"') | ('r', '#') => {
|
|
|
|
self.bump();
|
|
|
|
let (n_hashes, started, terminated) = self.raw_double_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = RawByteStr { n_hashes, started, terminated };
|
|
|
|
Literal { kind, suffix_start }
|
|
|
|
}
|
|
|
|
_ => self.ident(),
|
|
|
|
},
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Identifier (this should be checked after other variant that can
|
|
|
|
// start as identifier).
|
2019-09-04 05:16:36 -05:00
|
|
|
c if is_id_start(c) => self.ident(),
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Numeric literal.
|
2019-05-06 03:53:40 -05:00
|
|
|
c @ '0'..='9' => {
|
|
|
|
let literal_kind = self.number(c);
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
TokenKind::Literal { kind: literal_kind, suffix_start }
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// One-symbol tokens.
|
2019-05-06 03:53:40 -05:00
|
|
|
';' => Semi,
|
|
|
|
',' => Comma,
|
2019-08-19 11:00:24 -05:00
|
|
|
'.' => Dot,
|
2019-05-06 03:53:40 -05:00
|
|
|
'(' => OpenParen,
|
|
|
|
')' => CloseParen,
|
|
|
|
'{' => OpenBrace,
|
|
|
|
'}' => CloseBrace,
|
|
|
|
'[' => OpenBracket,
|
|
|
|
']' => CloseBracket,
|
|
|
|
'@' => At,
|
|
|
|
'#' => Pound,
|
|
|
|
'~' => Tilde,
|
|
|
|
'?' => Question,
|
2019-08-19 11:00:24 -05:00
|
|
|
':' => Colon,
|
2019-05-06 03:53:40 -05:00
|
|
|
'$' => Dollar,
|
2019-08-19 11:00:24 -05:00
|
|
|
'=' => Eq,
|
|
|
|
'!' => Not,
|
|
|
|
'<' => Lt,
|
|
|
|
'>' => Gt,
|
|
|
|
'-' => Minus,
|
|
|
|
'&' => And,
|
|
|
|
'|' => Or,
|
|
|
|
'+' => Plus,
|
|
|
|
'*' => Star,
|
|
|
|
'^' => Caret,
|
|
|
|
'%' => Percent,
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Lifetime or character literal.
|
2019-05-06 03:53:40 -05:00
|
|
|
'\'' => self.lifetime_or_char(),
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// String literal.
|
2019-05-06 03:53:40 -05:00
|
|
|
'"' => {
|
|
|
|
let terminated = self.double_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = Str { terminated };
|
|
|
|
Literal { kind, suffix_start }
|
|
|
|
}
|
|
|
|
_ => Unknown,
|
|
|
|
};
|
|
|
|
Token::new(token_kind, self.len_consumed())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_comment(&mut self) -> TokenKind {
|
|
|
|
debug_assert!(self.prev() == '/' && self.nth_char(0) == '/');
|
|
|
|
self.bump();
|
|
|
|
loop {
|
|
|
|
match self.nth_char(0) {
|
|
|
|
'\n' => break,
|
|
|
|
EOF_CHAR if self.is_eof() => break,
|
|
|
|
_ => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LineComment
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_comment(&mut self) -> TokenKind {
|
|
|
|
debug_assert!(self.prev() == '/' && self.nth_char(0) == '*');
|
|
|
|
self.bump();
|
|
|
|
let mut depth = 1usize;
|
|
|
|
while let Some(c) = self.bump() {
|
|
|
|
match c {
|
|
|
|
'/' if self.nth_char(0) == '*' => {
|
|
|
|
self.bump();
|
|
|
|
depth += 1;
|
|
|
|
}
|
|
|
|
'*' if self.nth_char(0) == '/' => {
|
|
|
|
self.bump();
|
|
|
|
depth -= 1;
|
|
|
|
if depth == 0 {
|
2019-10-26 11:12:58 -05:00
|
|
|
// This block comment is closed, so for a construction like "/* */ */"
|
|
|
|
// there will be a successfully parsed block comment "/* */"
|
|
|
|
// and " */" will be processed separately.
|
2019-05-06 03:53:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockComment { terminated: depth == 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn whitespace(&mut self) -> TokenKind {
|
2019-09-04 05:16:36 -05:00
|
|
|
debug_assert!(is_whitespace(self.prev()));
|
|
|
|
while is_whitespace(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
Whitespace
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw_ident(&mut self) -> TokenKind {
|
|
|
|
debug_assert!(
|
|
|
|
self.prev() == 'r'
|
|
|
|
&& self.nth_char(0) == '#'
|
2019-09-04 05:16:36 -05:00
|
|
|
&& is_id_start(self.nth_char(1))
|
2019-05-06 03:53:40 -05:00
|
|
|
);
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2019-09-04 05:16:36 -05:00
|
|
|
while is_id_continue(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
RawIdent
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident(&mut self) -> TokenKind {
|
2019-09-04 05:16:36 -05:00
|
|
|
debug_assert!(is_id_start(self.prev()));
|
|
|
|
while is_id_continue(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
Ident
|
|
|
|
}
|
|
|
|
|
|
|
|
fn number(&mut self, first_digit: char) -> LiteralKind {
|
|
|
|
debug_assert!('0' <= self.prev() && self.prev() <= '9');
|
|
|
|
let mut base = Base::Decimal;
|
|
|
|
if first_digit == '0' {
|
2019-10-26 11:12:58 -05:00
|
|
|
// Attempt to parse encoding base.
|
2019-05-06 03:53:40 -05:00
|
|
|
let has_digits = match self.nth_char(0) {
|
|
|
|
'b' => {
|
|
|
|
base = Base::Binary;
|
|
|
|
self.bump();
|
|
|
|
self.eat_decimal_digits()
|
|
|
|
}
|
|
|
|
'o' => {
|
|
|
|
base = Base::Octal;
|
|
|
|
self.bump();
|
|
|
|
self.eat_decimal_digits()
|
|
|
|
}
|
|
|
|
'x' => {
|
|
|
|
base = Base::Hexadecimal;
|
|
|
|
self.bump();
|
|
|
|
self.eat_hexadecimal_digits()
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
// Not a base prefix.
|
2019-05-06 03:53:40 -05:00
|
|
|
'0'..='9' | '_' | '.' | 'e' | 'E' => {
|
|
|
|
self.eat_decimal_digits();
|
|
|
|
true
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
// Just a 0.
|
2019-05-06 03:53:40 -05:00
|
|
|
_ => return Int { base, empty_int: false },
|
|
|
|
};
|
2019-10-26 11:12:58 -05:00
|
|
|
// Base prefix was provided, but there were no digits
|
|
|
|
// after it, e.g. "0x".
|
2019-05-06 03:53:40 -05:00
|
|
|
if !has_digits {
|
|
|
|
return Int { base, empty_int: true };
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-26 11:12:58 -05:00
|
|
|
// No base prefix, parse number in the usual way.
|
2019-05-06 03:53:40 -05:00
|
|
|
self.eat_decimal_digits();
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.nth_char(0) {
|
|
|
|
// Don't be greedy if this is actually an
|
|
|
|
// integer literal followed by field/method access or a range pattern
|
|
|
|
// (`0..2` and `12.foo()`)
|
|
|
|
'.' if self.nth_char(1) != '.'
|
2019-09-04 05:16:36 -05:00
|
|
|
&& !is_id_start(self.nth_char(1)) =>
|
2019-05-06 03:53:40 -05:00
|
|
|
{
|
|
|
|
// might have stuff after the ., and if it does, it needs to start
|
|
|
|
// with a number
|
|
|
|
self.bump();
|
|
|
|
let mut empty_exponent = false;
|
|
|
|
if self.nth_char(0).is_digit(10) {
|
|
|
|
self.eat_decimal_digits();
|
|
|
|
match self.nth_char(0) {
|
|
|
|
'e' | 'E' => {
|
|
|
|
self.bump();
|
|
|
|
empty_exponent = self.float_exponent().is_err()
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Float { base, empty_exponent }
|
|
|
|
}
|
|
|
|
'e' | 'E' => {
|
|
|
|
self.bump();
|
|
|
|
let empty_exponent = self.float_exponent().is_err();
|
|
|
|
Float { base, empty_exponent }
|
|
|
|
}
|
|
|
|
_ => Int { base, empty_int: false },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lifetime_or_char(&mut self) -> TokenKind {
|
|
|
|
debug_assert!(self.prev() == '\'');
|
|
|
|
let mut starts_with_number = false;
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Check if the first symbol after '\'' is a valid identifier
|
|
|
|
// character or a number (not a digit followed by '\'').
|
2019-09-04 05:16:36 -05:00
|
|
|
if (is_id_start(self.nth_char(0))
|
2019-05-06 03:53:40 -05:00
|
|
|
|| self.nth_char(0).is_digit(10) && {
|
|
|
|
starts_with_number = true;
|
|
|
|
true
|
|
|
|
})
|
|
|
|
&& self.nth_char(1) != '\''
|
|
|
|
{
|
|
|
|
self.bump();
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// Skip the identifier.
|
2019-09-04 05:16:36 -05:00
|
|
|
while is_id_continue(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
|
|
|
|
return if self.nth_char(0) == '\'' {
|
|
|
|
self.bump();
|
|
|
|
let kind = Char { terminated: true };
|
|
|
|
Literal { kind, suffix_start: self.len_consumed() }
|
|
|
|
} else {
|
|
|
|
Lifetime { starts_with_number }
|
|
|
|
};
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
|
|
|
|
// This is not a lifetime (checked above), parse a char literal.
|
2019-05-06 03:53:40 -05:00
|
|
|
let terminated = self.single_quoted_string();
|
|
|
|
let suffix_start = self.len_consumed();
|
|
|
|
if terminated {
|
|
|
|
self.eat_literal_suffix();
|
|
|
|
}
|
|
|
|
let kind = Char { terminated };
|
|
|
|
return Literal { kind, suffix_start };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn single_quoted_string(&mut self) -> bool {
|
|
|
|
debug_assert!(self.prev() == '\'');
|
2019-10-26 11:12:58 -05:00
|
|
|
// Parse `'''` as a single char literal.
|
2019-05-06 03:53:40 -05:00
|
|
|
if self.nth_char(0) == '\'' && self.nth_char(1) == '\'' {
|
|
|
|
self.bump();
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
// Parse until either quotes are terminated or error is detected.
|
2019-05-06 03:53:40 -05:00
|
|
|
let mut first = true;
|
|
|
|
loop {
|
|
|
|
match self.nth_char(0) {
|
2019-10-26 11:12:58 -05:00
|
|
|
// Probably beginning of the comment, which we don't want to include
|
|
|
|
// to the error report.
|
2019-05-06 03:53:40 -05:00
|
|
|
'/' if !first => break,
|
2019-10-26 11:12:58 -05:00
|
|
|
// Newline without following '\'' means unclosed quote, stop parsing.
|
2019-05-06 03:53:40 -05:00
|
|
|
'\n' if self.nth_char(1) != '\'' => break,
|
2019-10-26 11:12:58 -05:00
|
|
|
// End of file, stop parsing.
|
2019-05-06 03:53:40 -05:00
|
|
|
EOF_CHAR if self.is_eof() => break,
|
2019-10-26 11:12:58 -05:00
|
|
|
// Quotes are terminated, finish parsing.
|
2019-05-06 03:53:40 -05:00
|
|
|
'\'' => {
|
|
|
|
self.bump();
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
// Escaped slash is considered one character, so bump twice.
|
2019-05-06 03:53:40 -05:00
|
|
|
'\\' => {
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
}
|
2019-10-26 11:12:58 -05:00
|
|
|
// Skip the character.
|
2019-05-06 03:53:40 -05:00
|
|
|
_ => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Eats double-quoted string and returns true
|
|
|
|
/// if string is terminated.
|
2019-05-06 03:53:40 -05:00
|
|
|
fn double_quoted_string(&mut self) -> bool {
|
|
|
|
debug_assert!(self.prev() == '"');
|
|
|
|
loop {
|
|
|
|
match self.nth_char(0) {
|
|
|
|
'"' => {
|
|
|
|
self.bump();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EOF_CHAR if self.is_eof() => return false,
|
|
|
|
'\\' if self.nth_char(1) == '\\' || self.nth_char(1) == '"' => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
/// Eats the double-quoted string and returns a tuple of
|
|
|
|
/// (amount of the '#' symbols, raw string started, raw string terminated)
|
2019-05-06 03:53:40 -05:00
|
|
|
fn raw_double_quoted_string(&mut self) -> (usize, bool, bool) {
|
|
|
|
debug_assert!(self.prev() == 'r');
|
2019-10-26 11:12:58 -05:00
|
|
|
// Count opening '#' symbols.
|
2019-05-06 03:53:40 -05:00
|
|
|
let n_hashes = {
|
|
|
|
let mut acc: usize = 0;
|
|
|
|
loop {
|
|
|
|
match self.bump() {
|
|
|
|
Some('#') => acc += 1,
|
|
|
|
Some('"') => break acc,
|
|
|
|
None | Some(_) => return (acc, false, false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
// Skip the string itself and check that amount of closing '#'
|
|
|
|
// symbols is equal to the amount of opening ones.
|
2019-05-06 03:53:40 -05:00
|
|
|
loop {
|
|
|
|
match self.bump() {
|
|
|
|
Some('"') => {
|
|
|
|
let mut acc = n_hashes;
|
|
|
|
while self.nth_char(0) == '#' && acc > 0 {
|
|
|
|
self.bump();
|
|
|
|
acc -= 1;
|
|
|
|
}
|
|
|
|
if acc == 0 {
|
|
|
|
return (n_hashes, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => (),
|
|
|
|
None => return (n_hashes, true, false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_decimal_digits(&mut self) -> bool {
|
|
|
|
let mut has_digits = false;
|
|
|
|
loop {
|
|
|
|
match self.nth_char(0) {
|
|
|
|
'_' => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
'0'..='9' => {
|
|
|
|
has_digits = true;
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
has_digits
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_hexadecimal_digits(&mut self) -> bool {
|
|
|
|
let mut has_digits = false;
|
|
|
|
loop {
|
|
|
|
match self.nth_char(0) {
|
|
|
|
'_' => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
'0'..='9' | 'a'..='f' | 'A'..='F' => {
|
|
|
|
has_digits = true;
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
has_digits
|
|
|
|
}
|
|
|
|
|
|
|
|
fn float_exponent(&mut self) -> Result<(), ()> {
|
|
|
|
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
|
|
|
|
if self.nth_char(0) == '-' || self.nth_char(0) == '+' {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
if self.eat_decimal_digits() { Ok(()) } else { Err(()) }
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:12:58 -05:00
|
|
|
// Eats the suffix if it's an identifier.
|
2019-05-06 03:53:40 -05:00
|
|
|
fn eat_literal_suffix(&mut self) {
|
2019-09-04 05:16:36 -05:00
|
|
|
if !is_id_start(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
|
2019-09-04 05:16:36 -05:00
|
|
|
while is_id_continue(self.nth_char(0)) {
|
2019-05-06 03:53:40 -05:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|