@@ -28,10 +28,8 @@ static TOKEN_LIMIT: Limit = Limit::new(524_288);
2828
2929#[ derive( Debug , Clone , Eq , PartialEq ) ]
3030pub enum TokenExpander {
31- /// Old-style `macro_rules`.
32- MacroRules { mac : mbe:: MacroRules , def_site_token_map : mbe:: TokenMap } ,
33- /// AKA macros 2.0.
34- MacroDef { mac : mbe:: MacroDef , def_site_token_map : mbe:: TokenMap } ,
31+ /// Old-style `macro_rules` or the new macros 2.0
32+ DeclarativeMacro { mac : mbe:: DeclarativeMacro , def_site_token_map : mbe:: TokenMap } ,
3533 /// Stuff like `line!` and `file!`.
3634 Builtin ( BuiltinFnLikeExpander ) ,
3735 /// `global_allocator` and such.
@@ -50,8 +48,7 @@ impl TokenExpander {
5048 tt : & tt:: Subtree ,
5149 ) -> mbe:: ExpandResult < tt:: Subtree > {
5250 match self {
53- TokenExpander :: MacroRules { mac, .. } => mac. expand ( tt) ,
54- TokenExpander :: MacroDef { mac, .. } => mac. expand ( tt) ,
51+ TokenExpander :: DeclarativeMacro { mac, .. } => mac. expand ( tt) ,
5552 TokenExpander :: Builtin ( it) => it. expand ( db, id, tt) ,
5653 TokenExpander :: BuiltinAttr ( it) => it. expand ( db, id, tt) ,
5754 TokenExpander :: BuiltinDerive ( it) => it. expand ( db, id, tt) ,
@@ -66,8 +63,7 @@ impl TokenExpander {
6663
6764 pub ( crate ) fn map_id_down ( & self , id : tt:: TokenId ) -> tt:: TokenId {
6865 match self {
69- TokenExpander :: MacroRules { mac, .. } => mac. map_id_down ( id) ,
70- TokenExpander :: MacroDef { mac, .. } => mac. map_id_down ( id) ,
66+ TokenExpander :: DeclarativeMacro { mac, .. } => mac. map_id_down ( id) ,
7167 TokenExpander :: Builtin ( ..)
7268 | TokenExpander :: BuiltinAttr ( ..)
7369 | TokenExpander :: BuiltinDerive ( ..)
@@ -77,8 +73,7 @@ impl TokenExpander {
7773
7874 pub ( crate ) fn map_id_up ( & self , id : tt:: TokenId ) -> ( tt:: TokenId , mbe:: Origin ) {
7975 match self {
80- TokenExpander :: MacroRules { mac, .. } => mac. map_id_up ( id) ,
81- TokenExpander :: MacroDef { mac, .. } => mac. map_id_up ( id) ,
76+ TokenExpander :: DeclarativeMacro { mac, .. } => mac. map_id_up ( id) ,
8277 TokenExpander :: Builtin ( ..)
8378 | TokenExpander :: BuiltinAttr ( ..)
8479 | TokenExpander :: BuiltinDerive ( ..)
@@ -368,24 +363,27 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
368363
369364fn macro_def ( db : & dyn AstDatabase , id : MacroDefId ) -> Result < Arc < TokenExpander > , mbe:: ParseError > {
370365 match id. kind {
371- MacroDefKind :: Declarative ( ast_id) => match ast_id. to_node ( db) {
372- ast:: Macro :: MacroRules ( macro_rules) => {
373- let arg = macro_rules
374- . token_tree ( )
375- . ok_or_else ( || mbe:: ParseError :: Expected ( "expected a token tree" . into ( ) ) ) ?;
376- let ( tt, def_site_token_map) = mbe:: syntax_node_to_token_tree ( arg. syntax ( ) ) ;
377- let mac = mbe:: MacroRules :: parse ( & tt) ?;
378- Ok ( Arc :: new ( TokenExpander :: MacroRules { mac, def_site_token_map } ) )
379- }
380- ast:: Macro :: MacroDef ( macro_def) => {
381- let arg = macro_def
382- . body ( )
383- . ok_or_else ( || mbe:: ParseError :: Expected ( "expected a token tree" . into ( ) ) ) ?;
384- let ( tt, def_site_token_map) = mbe:: syntax_node_to_token_tree ( arg. syntax ( ) ) ;
385- let mac = mbe:: MacroDef :: parse ( & tt) ?;
386- Ok ( Arc :: new ( TokenExpander :: MacroDef { mac, def_site_token_map } ) )
387- }
388- } ,
366+ MacroDefKind :: Declarative ( ast_id) => {
367+ let ( mac, def_site_token_map) = match ast_id. to_node ( db) {
368+ ast:: Macro :: MacroRules ( macro_rules) => {
369+ let arg = macro_rules
370+ . token_tree ( )
371+ . ok_or_else ( || mbe:: ParseError :: Expected ( "expected a token tree" . into ( ) ) ) ?;
372+ let ( tt, def_site_token_map) = mbe:: syntax_node_to_token_tree ( arg. syntax ( ) ) ;
373+ let mac = mbe:: DeclarativeMacro :: parse_macro_rules ( & tt) ?;
374+ ( mac, def_site_token_map)
375+ }
376+ ast:: Macro :: MacroDef ( macro_def) => {
377+ let arg = macro_def
378+ . body ( )
379+ . ok_or_else ( || mbe:: ParseError :: Expected ( "expected a token tree" . into ( ) ) ) ?;
380+ let ( tt, def_site_token_map) = mbe:: syntax_node_to_token_tree ( arg. syntax ( ) ) ;
381+ let mac = mbe:: DeclarativeMacro :: parse_macro2 ( & tt) ?;
382+ ( mac, def_site_token_map)
383+ }
384+ } ;
385+ Ok ( Arc :: new ( TokenExpander :: DeclarativeMacro { mac, def_site_token_map } ) )
386+ }
389387 MacroDefKind :: BuiltIn ( expander, _) => Ok ( Arc :: new ( TokenExpander :: Builtin ( expander) ) ) ,
390388 MacroDefKind :: BuiltInAttr ( expander, _) => {
391389 Ok ( Arc :: new ( TokenExpander :: BuiltinAttr ( expander) ) )
0 commit comments