From 8ae5d6faf6006e873695bef6fd05cba69e027f63 Mon Sep 17 00:00:00 2001 From: Piotr Szpetkowski Date: Fri, 22 Nov 2019 07:56:50 +0100 Subject: Add support for Stringify builtin macro --- crates/ra_hir_expand/src/builtin_macro.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'crates/ra_hir_expand/src/builtin_macro.rs') diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 97fb0cb55..9628666d4 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -11,6 +11,7 @@ use crate::quote; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum BuiltinExpander { Line, + Stringify, } impl BuiltinExpander { @@ -22,6 +23,7 @@ impl BuiltinExpander { ) -> Result { match self { BuiltinExpander::Line => line_expand(db, id, tt), + BuiltinExpander::Stringify => stringify_expand(db, id, tt), } } } @@ -34,6 +36,8 @@ pub fn find_builtin_macro( // FIXME: Better registering method if ident == &name::LINE_MACRO { Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) + } else if ident == &name::STRINGIFY_MACRO { + Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) }) } else { None } @@ -78,3 +82,26 @@ fn line_expand( Ok(expanded) } + +fn stringify_expand( + db: &dyn AstDatabase, + id: MacroCallId, + _tt: &tt::Subtree, +) -> Result { + let loc = db.lookup_intern_macro(id); + let macro_call = loc.ast_id.to_node(db); + + let macro_content = { + let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; + let macro_args = arg.syntax().clone(); + let text = macro_args.text(); + let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')'); + text.slice(without_parens).to_string() + }; + + let expanded = quote! { + #macro_content + }; + + Ok(expanded) +} -- cgit v1.2.3