From e5d7b003afb24dda070bc150e268f009ac557529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Fri, 31 Jul 2020 19:30:37 +0300 Subject: Rename test modules --- crates/ra_hir_expand/src/proc_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_expand/src/proc_macro.rs') diff --git a/crates/ra_hir_expand/src/proc_macro.rs b/crates/ra_hir_expand/src/proc_macro.rs index 04c026004..2c0ec41d2 100644 --- a/crates/ra_hir_expand/src/proc_macro.rs +++ b/crates/ra_hir_expand/src/proc_macro.rs @@ -101,7 +101,7 @@ fn remove_derive_attrs(tt: &tt::Subtree) -> Option { } #[cfg(test)] -mod test { +mod tests { use super::*; use test_utils::assert_eq_text; -- cgit v1.2.3 From ed20a857f485a471369cd99b843af19a4d875ad0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:25:38 +0200 Subject: Rename ra_db -> base_db --- crates/ra_hir_expand/src/proc_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_expand/src/proc_macro.rs') diff --git a/crates/ra_hir_expand/src/proc_macro.rs b/crates/ra_hir_expand/src/proc_macro.rs index 2c0ec41d2..80255ea32 100644 --- a/crates/ra_hir_expand/src/proc_macro.rs +++ b/crates/ra_hir_expand/src/proc_macro.rs @@ -1,7 +1,7 @@ //! Proc Macro Expander stub use crate::{db::AstDatabase, LazyMacroId}; -use ra_db::{CrateId, ProcMacroId}; +use base_db::{CrateId, ProcMacroId}; use tt::buffer::{Cursor, TokenBuffer}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -- cgit v1.2.3 From b7aa4898e0841ab8199643f89a0caa967b698ca8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:26:29 +0200 Subject: Rename ra_hir_expand -> hir_expand --- crates/ra_hir_expand/src/proc_macro.rs | 143 --------------------------------- 1 file changed, 143 deletions(-) delete mode 100644 crates/ra_hir_expand/src/proc_macro.rs (limited to 'crates/ra_hir_expand/src/proc_macro.rs') diff --git a/crates/ra_hir_expand/src/proc_macro.rs b/crates/ra_hir_expand/src/proc_macro.rs deleted file mode 100644 index 80255ea32..000000000 --- a/crates/ra_hir_expand/src/proc_macro.rs +++ /dev/null @@ -1,143 +0,0 @@ -//! Proc Macro Expander stub - -use crate::{db::AstDatabase, LazyMacroId}; -use base_db::{CrateId, ProcMacroId}; -use tt::buffer::{Cursor, TokenBuffer}; - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub struct ProcMacroExpander { - krate: CrateId, - proc_macro_id: ProcMacroId, -} - -macro_rules! err { - ($fmt:literal, $($tt:tt),*) => { - mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*))) - }; - ($fmt:literal) => { - mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string())) - } -} - -impl ProcMacroExpander { - pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander { - ProcMacroExpander { krate, proc_macro_id } - } - - pub fn expand( - self, - db: &dyn AstDatabase, - _id: LazyMacroId, - tt: &tt::Subtree, - ) -> Result { - let krate_graph = db.crate_graph(); - let proc_macro = krate_graph[self.krate] - .proc_macro - .get(self.proc_macro_id.0 as usize) - .clone() - .ok_or_else(|| err!("No derive macro found."))?; - - let tt = remove_derive_attrs(tt) - .ok_or_else(|| err!("Fail to remove derive for custom derive"))?; - - proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from) - } -} - -fn eat_punct(cursor: &mut Cursor, c: char) -> bool { - if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = cursor.token_tree() { - if punct.char == c { - *cursor = cursor.bump(); - return true; - } - } - false -} - -fn eat_subtree(cursor: &mut Cursor, kind: tt::DelimiterKind) -> bool { - if let Some(tt::TokenTree::Subtree(subtree)) = cursor.token_tree() { - if Some(kind) == subtree.delimiter_kind() { - *cursor = cursor.bump_subtree(); - return true; - } - } - false -} - -fn eat_ident(cursor: &mut Cursor, t: &str) -> bool { - if let Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) = cursor.token_tree() { - if t == ident.text.as_str() { - *cursor = cursor.bump(); - return true; - } - } - false -} - -fn remove_derive_attrs(tt: &tt::Subtree) -> Option { - let buffer = TokenBuffer::new(&tt.token_trees); - let mut p = buffer.begin(); - let mut result = tt::Subtree::default(); - - while !p.eof() { - let curr = p; - - if eat_punct(&mut p, '#') { - eat_punct(&mut p, '!'); - let parent = p; - if eat_subtree(&mut p, tt::DelimiterKind::Bracket) { - if eat_ident(&mut p, "derive") { - p = parent.bump(); - continue; - } - } - } - - result.token_trees.push(curr.token_tree()?.clone()); - p = curr.bump(); - } - - Some(result) -} - -#[cfg(test)] -mod tests { - use super::*; - use test_utils::assert_eq_text; - - #[test] - fn test_remove_derive_attrs() { - let tt = mbe::parse_to_token_tree( - r#" - #[allow(unused)] - #[derive(Copy)] - #[derive(Hello)] - struct A { - bar: u32 - } -"#, - ) - .unwrap() - .0; - let result = format!("{:#?}", remove_derive_attrs(&tt).unwrap()); - - assert_eq_text!( - &result, - r#" -SUBTREE $ - PUNCH # [alone] 0 - SUBTREE [] 1 - IDENT allow 2 - SUBTREE () 3 - IDENT unused 4 - IDENT struct 15 - IDENT A 16 - SUBTREE {} 17 - IDENT bar 18 - PUNCH : [alone] 19 - IDENT u32 20 -"# - .trim() - ); - } -} -- cgit v1.2.3