@@ -11,6 +11,7 @@ use crate::quote;
11
11
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
12
12
pub enum BuiltinExpander {
13
13
Line ,
14
+ Stringify ,
14
15
}
15
16
16
17
impl BuiltinExpander {
@@ -22,6 +23,7 @@ impl BuiltinExpander {
22
23
) -> Result < tt:: Subtree , mbe:: ExpandError > {
23
24
match self {
24
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
36
// FIXME: Better registering method
35
37
if ident == & name:: LINE_MACRO {
36
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
41
} else {
38
42
None
39
43
}
@@ -78,3 +82,26 @@ fn line_expand(
78
82
79
83
Ok ( expanded)
80
84
}
85
+
86
+ fn 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
+ }
0 commit comments