@@ -12,6 +12,7 @@ use {ast, attr};
12
12
use syntax_pos:: { Span , DUMMY_SP } ;
13
13
use ext:: base:: { DummyResult , ExtCtxt , MacEager , MacResult , SyntaxExtension } ;
14
14
use ext:: base:: { IdentMacroExpander , NormalTT , TTMacroExpander } ;
15
+ use ext:: expand:: { Expansion , ExpansionKind } ;
15
16
use ext:: placeholders;
16
17
use ext:: tt:: macro_parser:: { Success , Error , Failure } ;
17
18
use ext:: tt:: macro_parser:: { MatchedSeq , MatchedNonterminal } ;
@@ -22,18 +23,14 @@ use parse::parser::{Parser, Restrictions};
22
23
use parse:: token:: { self , gensym_ident, NtTT , Token } ;
23
24
use parse:: token:: Token :: * ;
24
25
use print;
25
- use ptr:: P ;
26
26
use tokenstream:: { self , TokenTree } ;
27
27
28
- use util:: small_vector:: SmallVector ;
29
-
30
- use std:: cell:: RefCell ;
31
28
use std:: collections:: { HashMap } ;
32
29
use std:: collections:: hash_map:: { Entry } ;
33
30
use std:: rc:: Rc ;
34
31
35
- struct ParserAnyMacro < ' a > {
36
- parser : RefCell < Parser < ' a > > ,
32
+ pub struct ParserAnyMacro < ' a > {
33
+ parser : Parser < ' a > ,
37
34
38
35
/// Span of the expansion site of the macro this parser is for
39
36
site_span : Span ,
@@ -48,8 +45,8 @@ impl<'a> ParserAnyMacro<'a> {
48
45
/// about e.g. the semicolon in `macro_rules! kapow { () => {
49
46
/// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
50
47
/// 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 ;
53
50
parser. ensure_complete_parse ( allow_semi, |parser| {
54
51
let token_str = parser. this_token_to_string ( ) ;
55
52
let msg = format ! ( "macro expansion ignores token `{}` and any \
@@ -59,89 +56,16 @@ impl<'a> ParserAnyMacro<'a> {
59
56
let mut err = parser. diagnostic ( ) . struct_span_err ( span, & msg) ;
60
57
let msg = format ! ( "caused by the macro expansion here; the usage \
61
58
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)
64
61
. emit ( ) ;
65
62
} ) ;
66
63
}
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
- }
140
64
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
145
69
}
146
70
}
147
71
@@ -219,7 +143,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
219
143
// Let the context choose how to interpret the result.
220
144
// Weird, but useful for X-macros.
221
145
return Box :: new ( ParserAnyMacro {
222
- parser : RefCell :: new ( p ) ,
146
+ parser : p ,
223
147
224
148
// Pass along the original expansion site and the name of the macro
225
149
// so we can print a useful error message if the parse of the expanded
0 commit comments