From deba1fedfaac49804896d238e16aeb32ea8a2d97 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Aug 2019 19:16:57 +0300 Subject: :arrow_up: rustc_lexer --- Cargo.lock | 6 +- crates/ra_syntax/Cargo.toml | 2 +- crates/ra_syntax/src/parsing/lexer.rs | 136 ++++++++++++---------------------- crates/ra_syntax/src/validation.rs | 54 +++++++------- 4 files changed, 78 insertions(+), 120 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db1943477..85db6d179 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1256,7 +1256,7 @@ dependencies = [ [[package]] name = "ra_rustc_lexer" -version = "0.1.0-pre.2" +version = "0.1.0-pre.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1268,7 +1268,7 @@ version = "0.1.0" dependencies = [ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "ra_parser 0.1.0", - "ra_rustc_lexer 0.1.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)", + "ra_rustc_lexer 0.1.0-pre.3 (registry+https://github.com/rust-lang/crates.io-index)", "ra_text_edit 0.1.0", "rowan 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2134,7 +2134,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" -"checksum ra_rustc_lexer 0.1.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6baccda91574dfadd7f8a0bc8f9f110f874b6b484289b2536d3dbf4f0d5d97bb" +"checksum ra_rustc_lexer 0.1.0-pre.3 (registry+https://github.com/rust-lang/crates.io-index)" = "04371af481820ff8d35c7d12b503eb09cf9e1bd246269bf4a33e3d8c54fa3a4a" "checksum ra_vfs 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6918c38f6ab45101f1ddd6110eda831a735b5e9ca6c96d1ceedb7d13ecaeb0f4" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c" diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 5f8585878..0ead277b2 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://github.com/rust-analyzer/rust-analyzer" [dependencies] itertools = "0.8.0" rowan = "0.6.1" -ra_rustc_lexer = { version = "0.1.0-pre.2" } +ra_rustc_lexer = { version = "0.1.0-pre.3", features = ["unicode-xid"] } # ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here # to reduce number of compilations diff --git a/crates/ra_syntax/src/parsing/lexer.rs b/crates/ra_syntax/src/parsing/lexer.rs index 06822ea91..bdb01d40b 100644 --- a/crates/ra_syntax/src/parsing/lexer.rs +++ b/crates/ra_syntax/src/parsing/lexer.rs @@ -12,16 +12,16 @@ pub struct Token { pub len: TextUnit, } -fn match_literal_kind(kind: ra_rustc_lexer::LiteralKind) -> SyntaxKind { +fn match_literal_kind(kind: rustc_lexer::LiteralKind) -> SyntaxKind { match kind { - ra_rustc_lexer::LiteralKind::Int { .. } => INT_NUMBER, - ra_rustc_lexer::LiteralKind::Float { .. } => FLOAT_NUMBER, - ra_rustc_lexer::LiteralKind::Char { .. } => CHAR, - ra_rustc_lexer::LiteralKind::Byte { .. } => BYTE, - ra_rustc_lexer::LiteralKind::Str { .. } => STRING, - ra_rustc_lexer::LiteralKind::ByteStr { .. } => BYTE_STRING, - ra_rustc_lexer::LiteralKind::RawStr { .. } => RAW_STRING, - ra_rustc_lexer::LiteralKind::RawByteStr { .. } => RAW_BYTE_STRING, + rustc_lexer::LiteralKind::Int { .. } => INT_NUMBER, + rustc_lexer::LiteralKind::Float { .. } => FLOAT_NUMBER, + rustc_lexer::LiteralKind::Char { .. } => CHAR, + rustc_lexer::LiteralKind::Byte { .. } => BYTE, + rustc_lexer::LiteralKind::Str { .. } => STRING, + rustc_lexer::LiteralKind::ByteStr { .. } => BYTE_STRING, + rustc_lexer::LiteralKind::RawStr { .. } => RAW_STRING, + rustc_lexer::LiteralKind::RawByteStr { .. } => RAW_BYTE_STRING, } } @@ -32,32 +32,17 @@ pub fn tokenize(text: &str) -> Vec { } let mut text = text; let mut acc = Vec::new(); - if let Some(len) = ra_rustc_lexer::strip_shebang(text) { + if let Some(len) = rustc_lexer::strip_shebang(text) { acc.push(Token { kind: SHEBANG, len: TextUnit::from_usize(len) }); text = &text[len..]; } while !text.is_empty() { - let rustc_token = ra_rustc_lexer::first_token(text); - macro_rules! decompose { - ($t1:expr, $t2:expr) => {{ - acc.push(Token { kind: $t1, len: 1.into() }); - acc.push(Token { kind: $t2, len: 1.into() }); - text = &text[2..]; - continue; - }}; - ($t1:expr, $t2:expr, $t3:expr) => {{ - acc.push(Token { kind: $t1, len: 1.into() }); - acc.push(Token { kind: $t2, len: 1.into() }); - acc.push(Token { kind: $t3, len: 1.into() }); - text = &text[3..]; - continue; - }}; - } + let rustc_token = rustc_lexer::first_token(text); let kind = match rustc_token.kind { - ra_rustc_lexer::TokenKind::LineComment => COMMENT, - ra_rustc_lexer::TokenKind::BlockComment { .. } => COMMENT, - ra_rustc_lexer::TokenKind::Whitespace => WHITESPACE, - ra_rustc_lexer::TokenKind::Ident => { + rustc_lexer::TokenKind::LineComment => COMMENT, + rustc_lexer::TokenKind::BlockComment { .. } => COMMENT, + rustc_lexer::TokenKind::Whitespace => WHITESPACE, + rustc_lexer::TokenKind::Ident => { let token_text = &text[..rustc_token.len]; if token_text == "_" { UNDERSCORE @@ -65,62 +50,37 @@ pub fn tokenize(text: &str) -> Vec { SyntaxKind::from_keyword(&text[..rustc_token.len]).unwrap_or(IDENT) } } - ra_rustc_lexer::TokenKind::RawIdent => IDENT, - ra_rustc_lexer::TokenKind::Literal { kind, .. } => match_literal_kind(kind), - ra_rustc_lexer::TokenKind::Lifetime { .. } => LIFETIME, - ra_rustc_lexer::TokenKind::Semi => SEMI, - ra_rustc_lexer::TokenKind::Comma => COMMA, - ra_rustc_lexer::TokenKind::DotDotDot => decompose!(DOT, DOT, DOT), - ra_rustc_lexer::TokenKind::DotDotEq => decompose!(DOT, DOT, EQ), - ra_rustc_lexer::TokenKind::DotDot => decompose!(DOT, DOT), - ra_rustc_lexer::TokenKind::Dot => DOT, - ra_rustc_lexer::TokenKind::OpenParen => L_PAREN, - ra_rustc_lexer::TokenKind::CloseParen => R_PAREN, - ra_rustc_lexer::TokenKind::OpenBrace => L_CURLY, - ra_rustc_lexer::TokenKind::CloseBrace => R_CURLY, - ra_rustc_lexer::TokenKind::OpenBracket => L_BRACK, - ra_rustc_lexer::TokenKind::CloseBracket => R_BRACK, - ra_rustc_lexer::TokenKind::At => AT, - ra_rustc_lexer::TokenKind::Pound => POUND, - ra_rustc_lexer::TokenKind::Tilde => TILDE, - ra_rustc_lexer::TokenKind::Question => QUESTION, - ra_rustc_lexer::TokenKind::ColonColon => decompose!(COLON, COLON), - ra_rustc_lexer::TokenKind::Colon => COLON, - ra_rustc_lexer::TokenKind::Dollar => DOLLAR, - ra_rustc_lexer::TokenKind::EqEq => decompose!(EQ, EQ), - ra_rustc_lexer::TokenKind::Eq => EQ, - ra_rustc_lexer::TokenKind::FatArrow => decompose!(EQ, R_ANGLE), - ra_rustc_lexer::TokenKind::Ne => decompose!(EXCL, EQ), - ra_rustc_lexer::TokenKind::Not => EXCL, - ra_rustc_lexer::TokenKind::Le => decompose!(L_ANGLE, EQ), - ra_rustc_lexer::TokenKind::LArrow => decompose!(COLON, MINUS), - ra_rustc_lexer::TokenKind::Lt => L_ANGLE, - ra_rustc_lexer::TokenKind::ShlEq => decompose!(L_ANGLE, L_ANGLE, EQ), - ra_rustc_lexer::TokenKind::Shl => decompose!(L_ANGLE, L_ANGLE), - ra_rustc_lexer::TokenKind::Ge => decompose!(R_ANGLE, EQ), - ra_rustc_lexer::TokenKind::Gt => R_ANGLE, - ra_rustc_lexer::TokenKind::ShrEq => decompose!(R_ANGLE, R_ANGLE, EQ), - ra_rustc_lexer::TokenKind::Shr => decompose!(R_ANGLE, R_ANGLE), - ra_rustc_lexer::TokenKind::RArrow => decompose!(MINUS, R_ANGLE), - ra_rustc_lexer::TokenKind::Minus => MINUS, - ra_rustc_lexer::TokenKind::MinusEq => decompose!(MINUS, EQ), - ra_rustc_lexer::TokenKind::And => AMP, - ra_rustc_lexer::TokenKind::AndAnd => decompose!(AMP, AMP), - ra_rustc_lexer::TokenKind::AndEq => decompose!(AMP, EQ), - ra_rustc_lexer::TokenKind::Or => PIPE, - ra_rustc_lexer::TokenKind::OrOr => decompose!(PIPE, PIPE), - ra_rustc_lexer::TokenKind::OrEq => decompose!(PIPE, EQ), - ra_rustc_lexer::TokenKind::PlusEq => decompose!(PLUS, EQ), - ra_rustc_lexer::TokenKind::Plus => PLUS, - ra_rustc_lexer::TokenKind::StarEq => decompose!(STAR, EQ), - ra_rustc_lexer::TokenKind::Star => STAR, - ra_rustc_lexer::TokenKind::SlashEq => decompose!(SLASH, EQ), - ra_rustc_lexer::TokenKind::Slash => SLASH, - ra_rustc_lexer::TokenKind::CaretEq => decompose!(CARET, EQ), - ra_rustc_lexer::TokenKind::Caret => CARET, - ra_rustc_lexer::TokenKind::PercentEq => decompose!(PERCENT, EQ), - ra_rustc_lexer::TokenKind::Percent => PERCENT, - ra_rustc_lexer::TokenKind::Unknown => ERROR, + rustc_lexer::TokenKind::RawIdent => IDENT, + rustc_lexer::TokenKind::Literal { kind, .. } => match_literal_kind(kind), + rustc_lexer::TokenKind::Lifetime { .. } => LIFETIME, + rustc_lexer::TokenKind::Semi => SEMI, + rustc_lexer::TokenKind::Comma => COMMA, + rustc_lexer::TokenKind::Dot => DOT, + rustc_lexer::TokenKind::OpenParen => L_PAREN, + rustc_lexer::TokenKind::CloseParen => R_PAREN, + rustc_lexer::TokenKind::OpenBrace => L_CURLY, + rustc_lexer::TokenKind::CloseBrace => R_CURLY, + rustc_lexer::TokenKind::OpenBracket => L_BRACK, + rustc_lexer::TokenKind::CloseBracket => R_BRACK, + rustc_lexer::TokenKind::At => AT, + rustc_lexer::TokenKind::Pound => POUND, + rustc_lexer::TokenKind::Tilde => TILDE, + rustc_lexer::TokenKind::Question => QUESTION, + rustc_lexer::TokenKind::Colon => COLON, + rustc_lexer::TokenKind::Dollar => DOLLAR, + rustc_lexer::TokenKind::Eq => EQ, + rustc_lexer::TokenKind::Not => EXCL, + rustc_lexer::TokenKind::Lt => L_ANGLE, + rustc_lexer::TokenKind::Gt => R_ANGLE, + rustc_lexer::TokenKind::Minus => MINUS, + rustc_lexer::TokenKind::And => AMP, + rustc_lexer::TokenKind::Or => PIPE, + rustc_lexer::TokenKind::Plus => PLUS, + rustc_lexer::TokenKind::Star => STAR, + rustc_lexer::TokenKind::Slash => SLASH, + rustc_lexer::TokenKind::Caret => CARET, + rustc_lexer::TokenKind::Percent => PERCENT, + rustc_lexer::TokenKind::Unknown => ERROR, }; let token = Token { kind, len: TextUnit::from_usize(rustc_token.len) }; acc.push(token); @@ -130,12 +90,12 @@ pub fn tokenize(text: &str) -> Vec { } pub fn classify_literal(text: &str) -> Option { - let t = ra_rustc_lexer::first_token(text); + let t = rustc_lexer::first_token(text); if t.len != text.len() { return None; } let kind = match t.kind { - ra_rustc_lexer::TokenKind::Literal { kind, .. } => match_literal_kind(kind), + rustc_lexer::TokenKind::Literal { kind, .. } => match_literal_kind(kind), _ => return None, }; Some(Token { kind, len: TextUnit::from_usize(t.len) }) diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 2bb3c0a03..a8c789e0c 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -1,6 +1,6 @@ mod block; -use ra_rustc_lexer::unescape; +use rustc_lexer::unescape; use crate::{ algo::visit::{visitor_ctx, VisitorCtx}, @@ -32,64 +32,62 @@ pub enum EscapeError { NonAsciiCharInByte, } -impl From for EscapeError { - fn from(err: ra_rustc_lexer::unescape::EscapeError) -> Self { +impl From for EscapeError { + fn from(err: rustc_lexer::unescape::EscapeError) -> Self { match err { - ra_rustc_lexer::unescape::EscapeError::ZeroChars => EscapeError::ZeroChars, - ra_rustc_lexer::unescape::EscapeError::MoreThanOneChar => EscapeError::MoreThanOneChar, - ra_rustc_lexer::unescape::EscapeError::LoneSlash => EscapeError::LoneSlash, - ra_rustc_lexer::unescape::EscapeError::InvalidEscape => EscapeError::InvalidEscape, - ra_rustc_lexer::unescape::EscapeError::BareCarriageReturn - | ra_rustc_lexer::unescape::EscapeError::BareCarriageReturnInRawString => { + rustc_lexer::unescape::EscapeError::ZeroChars => EscapeError::ZeroChars, + rustc_lexer::unescape::EscapeError::MoreThanOneChar => EscapeError::MoreThanOneChar, + rustc_lexer::unescape::EscapeError::LoneSlash => EscapeError::LoneSlash, + rustc_lexer::unescape::EscapeError::InvalidEscape => EscapeError::InvalidEscape, + rustc_lexer::unescape::EscapeError::BareCarriageReturn + | rustc_lexer::unescape::EscapeError::BareCarriageReturnInRawString => { EscapeError::BareCarriageReturn } - ra_rustc_lexer::unescape::EscapeError::EscapeOnlyChar => EscapeError::EscapeOnlyChar, - ra_rustc_lexer::unescape::EscapeError::TooShortHexEscape => { - EscapeError::TooShortHexEscape - } - ra_rustc_lexer::unescape::EscapeError::InvalidCharInHexEscape => { + rustc_lexer::unescape::EscapeError::EscapeOnlyChar => EscapeError::EscapeOnlyChar, + rustc_lexer::unescape::EscapeError::TooShortHexEscape => EscapeError::TooShortHexEscape, + rustc_lexer::unescape::EscapeError::InvalidCharInHexEscape => { EscapeError::InvalidCharInHexEscape } - ra_rustc_lexer::unescape::EscapeError::OutOfRangeHexEscape => { + rustc_lexer::unescape::EscapeError::OutOfRangeHexEscape => { EscapeError::OutOfRangeHexEscape } - ra_rustc_lexer::unescape::EscapeError::NoBraceInUnicodeEscape => { + rustc_lexer::unescape::EscapeError::NoBraceInUnicodeEscape => { EscapeError::NoBraceInUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::InvalidCharInUnicodeEscape => { + rustc_lexer::unescape::EscapeError::InvalidCharInUnicodeEscape => { EscapeError::InvalidCharInUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::EmptyUnicodeEscape => { + rustc_lexer::unescape::EscapeError::EmptyUnicodeEscape => { EscapeError::EmptyUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::UnclosedUnicodeEscape => { + rustc_lexer::unescape::EscapeError::UnclosedUnicodeEscape => { EscapeError::UnclosedUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::LeadingUnderscoreUnicodeEscape => { + rustc_lexer::unescape::EscapeError::LeadingUnderscoreUnicodeEscape => { EscapeError::LeadingUnderscoreUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::OverlongUnicodeEscape => { + rustc_lexer::unescape::EscapeError::OverlongUnicodeEscape => { EscapeError::OverlongUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::LoneSurrogateUnicodeEscape => { + rustc_lexer::unescape::EscapeError::LoneSurrogateUnicodeEscape => { EscapeError::LoneSurrogateUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::OutOfRangeUnicodeEscape => { + rustc_lexer::unescape::EscapeError::OutOfRangeUnicodeEscape => { EscapeError::OutOfRangeUnicodeEscape } - ra_rustc_lexer::unescape::EscapeError::UnicodeEscapeInByte => { + rustc_lexer::unescape::EscapeError::UnicodeEscapeInByte => { EscapeError::UnicodeEscapeInByte } - ra_rustc_lexer::unescape::EscapeError::NonAsciiCharInByte - | ra_rustc_lexer::unescape::EscapeError::NonAsciiCharInByteString => { + rustc_lexer::unescape::EscapeError::NonAsciiCharInByte + | rustc_lexer::unescape::EscapeError::NonAsciiCharInByteString => { EscapeError::NonAsciiCharInByte } } } } -impl From for SyntaxErrorKind { - fn from(err: ra_rustc_lexer::unescape::EscapeError) -> Self { +impl From for SyntaxErrorKind { + fn from(err: rustc_lexer::unescape::EscapeError) -> Self { SyntaxErrorKind::EscapeError(err.into()) } } -- cgit v1.2.3