aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
authorPiotr Szpetkowski <[email protected]>2019-11-22 06:56:50 +0000
committerPiotr Szpetkowski <[email protected]>2019-11-22 06:56:50 +0000
commit8ae5d6faf6006e873695bef6fd05cba69e027f63 (patch)
treec1410008cd523a6ab61402a1279c61744313968f /crates/ra_hir_expand
parentc9273828b3c44fba62d1b989480c287d923839d2 (diff)
Add support for Stringify builtin macro
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs27
-rw-r--r--crates/ra_hir_expand/src/name.rs1
2 files changed, 28 insertions, 0 deletions
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;
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum BuiltinExpander { 12pub enum BuiltinExpander {
13 Line, 13 Line,
14 Stringify,
14} 15}
15 16
16impl BuiltinExpander { 17impl BuiltinExpander {
@@ -22,6 +23,7 @@ impl BuiltinExpander {
22 ) -> Result<tt::Subtree, mbe::ExpandError> { 23 ) -> Result<tt::Subtree, mbe::ExpandError> {
23 match self { 24 match self {
24 BuiltinExpander::Line => line_expand(db, id, tt), 25 BuiltinExpander::Line => line_expand(db, id, tt),
26 BuiltinExpander::Stringify => stringify_expand(db, id, tt),
25 } 27 }
26 } 28 }
27} 29}
@@ -34,6 +36,8 @@ pub fn find_builtin_macro(
34 // FIXME: Better registering method 36 // FIXME: Better registering method
35 if ident == &name::LINE_MACRO { 37 if ident == &name::LINE_MACRO {
36 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) 38 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
39 } else if ident == &name::STRINGIFY_MACRO {
40 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
37 } else { 41 } else {
38 None 42 None
39 } 43 }
@@ -78,3 +82,26 @@ fn line_expand(
78 82
79 Ok(expanded) 83 Ok(expanded)
80} 84}
85
86fn stringify_expand(
87 db: &dyn AstDatabase,
88 id: MacroCallId,
89 _tt: &tt::Subtree,
90) -> Result<tt::Subtree, mbe::ExpandError> {
91 let loc = db.lookup_intern_macro(id);
92 let macro_call = loc.ast_id.to_node(db);
93
94 let macro_content = {
95 let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
96 let macro_args = arg.syntax().clone();
97 let text = macro_args.text();
98 let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
99 text.slice(without_parens).to_string()
100 };
101
102 let expanded = quote! {
103 #macro_content
104 };
105
106 Ok(expanded)
107}
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index 1bf17d12b..c3f7e77a5 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -143,3 +143,4 @@ pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143 143
144// Builtin Macros 144// Builtin Macros
145pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); 145pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
146pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");