@@ -12,6 +12,7 @@ use {ast, attr};
1212use syntax_pos:: { Span , DUMMY_SP } ;
1313use ext:: base:: { DummyResult , ExtCtxt , MacEager , MacResult , SyntaxExtension } ;
1414use ext:: base:: { IdentMacroExpander , NormalTT , TTMacroExpander } ;
15+ use ext:: expand:: { Expansion , ExpansionKind } ;
1516use ext:: placeholders;
1617use ext:: tt:: macro_parser:: { Success , Error , Failure } ;
1718use ext:: tt:: macro_parser:: { MatchedSeq , MatchedNonterminal } ;
@@ -22,18 +23,14 @@ use parse::parser::{Parser, Restrictions};
2223use parse:: token:: { self , gensym_ident, NtTT , Token } ;
2324use parse:: token:: Token :: * ;
2425use print;
25- use ptr:: P ;
2626use tokenstream:: { self , TokenTree } ;
2727
28- use util:: small_vector:: SmallVector ;
29-
30- use std:: cell:: RefCell ;
3128use std:: collections:: { HashMap } ;
3229use std:: collections:: hash_map:: { Entry } ;
3330use std:: rc:: Rc ;
3431
35- struct ParserAnyMacro < ' a > {
36- parser : RefCell < Parser < ' a > > ,
32+ pub struct ParserAnyMacro < ' a > {
33+ parser : Parser < ' a > ,
3734
3835 /// Span of the expansion site of the macro this parser is for
3936 site_span : Span ,
@@ -48,8 +45,8 @@ impl<'a> ParserAnyMacro<'a> {
4845 /// about e.g. the semicolon in `macro_rules! kapow { () => {
4946 /// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
5047 /// allowed to be there.
51- fn ensure_complete_parse ( & self , allow_semi : bool , context : & str ) {
52- let mut parser = self . parser . borrow_mut ( ) ;
48+ fn ensure_complete_parse ( & mut self , allow_semi : bool , context : & str ) {
49+ let ParserAnyMacro { site_span , macro_ident , ref mut parser } = * self ;
5350 parser. ensure_complete_parse ( allow_semi, |parser| {
5451 let token_str = parser. this_token_to_string ( ) ;
5552 let msg = format ! ( "macro expansion ignores token `{}` and any \
@@ -59,89 +56,16 @@ impl<'a> ParserAnyMacro<'a> {
5956 let mut err = parser. diagnostic ( ) . struct_span_err ( span, & msg) ;
6057 let msg = format ! ( "caused by the macro expansion here; the usage \
6158 of `{}!` is likely invalid in {} context",
62- self . macro_ident, context) ;
63- err. span_note ( self . site_span , & msg)
59+ macro_ident, context) ;
60+ err. span_note ( site_span, & msg)
6461 . emit ( ) ;
6562 } ) ;
6663 }
67- }
68-
69- impl < ' a > MacResult for ParserAnyMacro < ' a > {
70- fn make_expr ( self : Box < ParserAnyMacro < ' a > > ) -> Option < P < ast:: Expr > > {
71- let ret = panictry ! ( self . parser. borrow_mut( ) . parse_expr( ) ) ;
72- self . ensure_complete_parse ( true , "expression" ) ;
73- Some ( ret)
74- }
75- fn make_pat ( self : Box < ParserAnyMacro < ' a > > ) -> Option < P < ast:: Pat > > {
76- let ret = panictry ! ( self . parser. borrow_mut( ) . parse_pat( ) ) ;
77- self . ensure_complete_parse ( false , "pattern" ) ;
78- Some ( ret)
79- }
80- fn make_items ( self : Box < ParserAnyMacro < ' a > > ) -> Option < SmallVector < P < ast:: Item > > > {
81- let mut ret = SmallVector :: zero ( ) ;
82- while let Some ( item) = panictry ! ( self . parser. borrow_mut( ) . parse_item( ) ) {
83- ret. push ( item) ;
84- }
85- self . ensure_complete_parse ( false , "item" ) ;
86- Some ( ret)
87- }
88-
89- fn make_impl_items ( self : Box < ParserAnyMacro < ' a > > )
90- -> Option < SmallVector < ast:: ImplItem > > {
91- let mut ret = SmallVector :: zero ( ) ;
92- loop {
93- let mut parser = self . parser . borrow_mut ( ) ;
94- match parser. token {
95- token:: Eof => break ,
96- _ => ret. push ( panictry ! ( parser. parse_impl_item( ) ) )
97- }
98- }
99- self . ensure_complete_parse ( false , "item" ) ;
100- Some ( ret)
101- }
102-
103- fn make_trait_items ( self : Box < ParserAnyMacro < ' a > > )
104- -> Option < SmallVector < ast:: TraitItem > > {
105- let mut ret = SmallVector :: zero ( ) ;
106- loop {
107- let mut parser = self . parser . borrow_mut ( ) ;
108- match parser. token {
109- token:: Eof => break ,
110- _ => ret. push ( panictry ! ( parser. parse_trait_item( ) ) )
111- }
112- }
113- self . ensure_complete_parse ( false , "item" ) ;
114- Some ( ret)
115- }
116-
117-
118- fn make_stmts ( self : Box < ParserAnyMacro < ' a > > )
119- -> Option < SmallVector < ast:: Stmt > > {
120- let mut ret = SmallVector :: zero ( ) ;
121- loop {
122- let mut parser = self . parser . borrow_mut ( ) ;
123- match parser. token {
124- token:: Eof => break ,
125- _ => match parser. parse_full_stmt ( true ) {
126- Ok ( maybe_stmt) => match maybe_stmt {
127- Some ( stmt) => ret. push ( stmt) ,
128- None => ( ) ,
129- } ,
130- Err ( mut e) => {
131- e. emit ( ) ;
132- break ;
133- }
134- }
135- }
136- }
137- self . ensure_complete_parse ( false , "statement" ) ;
138- Some ( ret)
139- }
14064
141- fn make_ty ( self : Box < ParserAnyMacro < ' a > > ) -> Option < P < ast :: Ty > > {
142- let ret = panictry ! ( self . parser. borrow_mut ( ) . parse_ty ( ) ) ;
143- self . ensure_complete_parse ( false , "type" ) ;
144- Some ( ret )
65+ pub fn make ( mut self : Box < ParserAnyMacro < ' a > > , kind : ExpansionKind ) -> Expansion {
66+ let expansion = panictry ! ( self . parser. parse_expansion ( kind ) ) ;
67+ self . ensure_complete_parse ( kind == ExpansionKind :: Expr , kind . name ( ) ) ;
68+ expansion
14569 }
14670}
14771
@@ -219,7 +143,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
219143 // Let the context choose how to interpret the result.
220144 // Weird, but useful for X-macros.
221145 return Box :: new ( ParserAnyMacro {
222- parser : RefCell :: new ( p ) ,
146+ parser : p ,
223147
224148 // Pass along the original expansion site and the name of the macro
225149 // so we can print a useful error message if the parse of the expanded
0 commit comments