diff --git a/Cargo.lock b/Cargo.lock index 1e1d68f778..2aa776b508 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1270,6 +1270,7 @@ dependencies = [ "edition", "expect-test", "ra-ap-rustc_lexer", + "rustc-literal-escaper", "stdx", "tracing", ] @@ -1743,6 +1744,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +[[package]] +name = "rustc-literal-escaper" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfdd0fcb1409d38cb2d940400497e2384a4a04b8685ee92a0a7a8986ccd72115" + [[package]] name = "rustc-stable-hash" version = "0.1.1" @@ -1982,6 +1989,7 @@ dependencies = [ "rayon", "rowan", "rustc-hash 2.0.0", + "rustc-literal-escaper", "rustc_apfloat", "smol_str", "stdx", diff --git a/crates/parser/Cargo.toml b/crates/parser/Cargo.toml index a36a39dbee..292b7de255 100644 --- a/crates/parser/Cargo.toml +++ b/crates/parser/Cargo.toml @@ -14,6 +14,7 @@ rust-version.workspace = true [dependencies] drop_bomb = "0.1.5" ra-ap-rustc_lexer.workspace = true +rustc-literal-escaper = "0.0.1" tracing = { workspace = true, optional = true } edition.workspace = true diff --git a/crates/parser/src/lexed_str.rs b/crates/parser/src/lexed_str.rs index c97596d509..b0bbc2fa5f 100644 --- a/crates/parser/src/lexed_str.rs +++ b/crates/parser/src/lexed_str.rs @@ -10,7 +10,7 @@ use std::ops; -use rustc_lexer::unescape::{EscapeError, Mode}; +use rustc_literal_escaper::{EscapeError, Mode, unescape_byte, unescape_char, unescape_mixed, unescape_unicode}; use crate::{ Edition, @@ -282,7 +282,7 @@ impl<'a> Converter<'a> { let text = &self.res.text[self.offset + 1..][..len - 1]; let i = text.rfind('\'').unwrap(); let text = &text[..i]; - if let Err(e) = rustc_lexer::unescape::unescape_char(text) { + if let Err(e) = unescape_char(text) { err = error_to_diagnostic_message(e, Mode::Char); } } @@ -295,7 +295,7 @@ impl<'a> Converter<'a> { let text = &self.res.text[self.offset + 2..][..len - 2]; let i = text.rfind('\'').unwrap(); let text = &text[..i]; - if let Err(e) = rustc_lexer::unescape::unescape_byte(text) { + if let Err(e) = unescape_byte(text) { err = error_to_diagnostic_message(e, Mode::Byte); } } @@ -402,14 +402,14 @@ fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str { let mut error_message = ""; match mode { Mode::CStr => { - rustc_lexer::unescape::unescape_mixed(text, mode, &mut |_, res| { + unescape_mixed(text, mode, &mut |_, res| { if let Err(e) = res { error_message = error_to_diagnostic_message(e, mode); } }); } Mode::ByteStr | Mode::Str => { - rustc_lexer::unescape::unescape_unicode(text, mode, &mut |_, res| { + unescape_unicode(text, mode, &mut |_, res| { if let Err(e) = res { error_message = error_to_diagnostic_message(e, mode); } diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index 3fe6e01dc3..4fb628685a 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -17,6 +17,7 @@ either.workspace = true itertools.workspace = true rowan = "=0.15.15" rustc-hash.workspace = true +rustc-literal-escaper = "0.0.1" indexmap.workspace = true smol_str.workspace = true triomphe.workspace = true diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index df851ab5b2..08bffb9e3a 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, num::ParseIntError}; -use rustc_lexer::unescape::{ +use rustc_literal_escaper::{ unescape_byte, unescape_char, unescape_mixed, unescape_unicode, EscapeError, MixedUnit, Mode, }; use stdx::always; diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index c9e9f468dc..21f1ea5f91 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -19,13 +19,6 @@ //! [RFC]: //! [Swift]: -#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))] - -#[cfg(not(feature = "in-rust-tree"))] -extern crate ra_ap_rustc_lexer as rustc_lexer; -#[cfg(feature = "in-rust-tree")] -extern crate rustc_lexer; - mod parsing; mod ptr; mod syntax_error; @@ -64,7 +57,7 @@ pub use rowan::{ api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset, WalkEvent, }; -pub use rustc_lexer::unescape; +pub use rustc_literal_escaper as unescape; pub use smol_str::{format_smolstr, SmolStr, SmolStrBuilder, ToSmolStr}; /// `Parse` is the result of the parsing: a syntax tree and a collection of diff --git a/crates/syntax/src/validation.rs b/crates/syntax/src/validation.rs index 85eefac734..71c5f9a946 100644 --- a/crates/syntax/src/validation.rs +++ b/crates/syntax/src/validation.rs @@ -5,7 +5,7 @@ mod block; use rowan::Direction; -use rustc_lexer::unescape::{self, unescape_mixed, unescape_unicode, Mode}; +use rustc_literal_escaper::{unescape_mixed, unescape_unicode, EscapeError, Mode}; use crate::{ algo, @@ -44,8 +44,8 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec) { } } -fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> (&'static str, bool) { - use unescape::EscapeError as EE; +fn rustc_unescape_error_to_string(err: EscapeError) -> (&'static str, bool) { + use rustc_literal_escaper::EscapeError as EE; #[rustfmt::skip] let err_message = match err { @@ -127,7 +127,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { let text = token.text(); // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-lang/rust-analyzer/pull/2834#discussion_r366199205) - let mut push_err = |prefix_len, off, err: unescape::EscapeError| { + let mut push_err = |prefix_len, off, err: EscapeError| { let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap(); let (message, is_err) = rustc_unescape_error_to_string(err); // FIXME: Emit lexer warnings