@@ -21,6 +21,7 @@ use attr::{AttrMetaMethods, AttributeMethods};
21
21
use codemap:: { CodeMap , BytePos } ;
22
22
use codemap;
23
23
use diagnostic;
24
+ use parse:: token:: { BinOpToken , Token } ;
24
25
use parse:: token;
25
26
use parse:: lexer:: comments;
26
27
use parse;
@@ -181,6 +182,101 @@ pub fn to_string(f: |&mut State| -> IoResult<()>) -> String {
181
182
}
182
183
}
183
184
185
+ pub fn binop_to_string ( op : BinOpToken ) -> & ' static str {
186
+ match op {
187
+ token:: Plus => "+" ,
188
+ token:: Minus => "-" ,
189
+ token:: Star => "*" ,
190
+ token:: Slash => "/" ,
191
+ token:: Percent => "%" ,
192
+ token:: Caret => "^" ,
193
+ token:: And => "&" ,
194
+ token:: Or => "|" ,
195
+ token:: Shl => "<<" ,
196
+ token:: Shr => ">>" ,
197
+ }
198
+ }
199
+
200
+ pub fn token_to_string ( tok : & Token ) -> String {
201
+ match * tok {
202
+ token:: Eq => "=" . into_string ( ) ,
203
+ token:: Lt => "<" . into_string ( ) ,
204
+ token:: Le => "<=" . into_string ( ) ,
205
+ token:: EqEq => "==" . into_string ( ) ,
206
+ token:: Ne => "!=" . into_string ( ) ,
207
+ token:: Ge => ">=" . into_string ( ) ,
208
+ token:: Gt => ">" . into_string ( ) ,
209
+ token:: Not => "!" . into_string ( ) ,
210
+ token:: Tilde => "~" . into_string ( ) ,
211
+ token:: OrOr => "||" . into_string ( ) ,
212
+ token:: AndAnd => "&&" . into_string ( ) ,
213
+ token:: BinOp ( op) => binop_to_string ( op) . into_string ( ) ,
214
+ token:: BinOpEq ( op) => format ! ( "{}=" , binop_to_string( op) ) ,
215
+
216
+ /* Structural symbols */
217
+ token:: At => "@" . into_string ( ) ,
218
+ token:: Dot => "." . into_string ( ) ,
219
+ token:: DotDot => ".." . into_string ( ) ,
220
+ token:: DotDotDot => "..." . into_string ( ) ,
221
+ token:: Comma => "," . into_string ( ) ,
222
+ token:: Semi => ";" . into_string ( ) ,
223
+ token:: Colon => ":" . into_string ( ) ,
224
+ token:: ModSep => "::" . into_string ( ) ,
225
+ token:: RArrow => "->" . into_string ( ) ,
226
+ token:: LArrow => "<-" . into_string ( ) ,
227
+ token:: FatArrow => "=>" . into_string ( ) ,
228
+ token:: LParen => "(" . into_string ( ) ,
229
+ token:: RParen => ")" . into_string ( ) ,
230
+ token:: LBracket => "[" . into_string ( ) ,
231
+ token:: RBracket => "]" . into_string ( ) ,
232
+ token:: LBrace => "{" . into_string ( ) ,
233
+ token:: RBrace => "}" . into_string ( ) ,
234
+ token:: Pound => "#" . into_string ( ) ,
235
+ token:: Dollar => "$" . into_string ( ) ,
236
+ token:: Question => "?" . into_string ( ) ,
237
+
238
+ /* Literals */
239
+ token:: LitByte ( b) => format ! ( "b'{}'" , b. as_str( ) ) ,
240
+ token:: LitChar ( c) => format ! ( "'{}'" , c. as_str( ) ) ,
241
+ token:: LitFloat ( c) => c. as_str ( ) . into_string ( ) ,
242
+ token:: LitInteger ( c) => c. as_str ( ) . into_string ( ) ,
243
+ token:: LitStr ( s) => format ! ( "\" {}\" " , s. as_str( ) ) ,
244
+ token:: LitStrRaw ( s, n) => format ! ( "r{delim}\" {string}\" {delim}" ,
245
+ delim="#" . repeat( n) ,
246
+ string=s. as_str( ) ) ,
247
+ token:: LitBinary ( v) => format ! ( "b\" {}\" " , v. as_str( ) ) ,
248
+ token:: LitBinaryRaw ( s, n) => format ! ( "br{delim}\" {string}\" {delim}" ,
249
+ delim="#" . repeat( n) ,
250
+ string=s. as_str( ) ) ,
251
+
252
+ /* Name components */
253
+ token:: Ident ( s, _) => token:: get_ident ( s) . get ( ) . into_string ( ) ,
254
+ token:: Lifetime ( s) => format ! ( "{}" , token:: get_ident( s) ) ,
255
+ token:: Underscore => "_" . into_string ( ) ,
256
+
257
+ /* Other */
258
+ token:: DocComment ( s) => s. as_str ( ) . into_string ( ) ,
259
+ token:: Eof => "<eof>" . into_string ( ) ,
260
+ token:: Whitespace => " " . into_string ( ) ,
261
+ token:: Comment => "/* */" . into_string ( ) ,
262
+ token:: Shebang ( s) => format ! ( "/* shebang: {}*/" , s. as_str( ) ) ,
263
+
264
+ token:: Interpolated ( ref nt) => match * nt {
265
+ token:: NtExpr ( ref e) => expr_to_string ( & * * e) ,
266
+ token:: NtMeta ( ref e) => meta_item_to_string ( & * * e) ,
267
+ token:: NtTy ( ref e) => ty_to_string ( & * * e) ,
268
+ token:: NtPath ( ref e) => path_to_string ( & * * e) ,
269
+ token:: NtItem ( ..) => "an interpolated item" . into_string ( ) ,
270
+ token:: NtBlock ( ..) => "an interpolated block" . into_string ( ) ,
271
+ token:: NtStmt ( ..) => "an interpolated statement" . into_string ( ) ,
272
+ token:: NtPat ( ..) => "an interpolated pattern" . into_string ( ) ,
273
+ token:: NtIdent ( ..) => "an interpolated identifier" . into_string ( ) ,
274
+ token:: NtTT ( ..) => "an interpolated tt" . into_string ( ) ,
275
+ token:: NtMatchers ( ..) => "an interpolated matcher sequence" . into_string ( ) ,
276
+ }
277
+ }
278
+ }
279
+
184
280
// FIXME (Issue #16472): the thing_to_string_impls macro should go away
185
281
// after we revise the syntax::ext::quote::ToToken impls to go directly
186
282
// to token-trees instead of thing -> string -> token-trees.
@@ -1026,14 +1122,14 @@ impl<'a> State<'a> {
1026
1122
match * tt {
1027
1123
ast:: TtDelimited ( _, ref delimed) => {
1028
1124
let ( ref open, ref tts, ref close) = * * delimed;
1029
- try!( word ( & mut self . s , parse :: token :: to_string ( & open. token ) . as_slice ( ) ) ) ;
1125
+ try!( word ( & mut self . s , token_to_string ( & open. token ) . as_slice ( ) ) ) ;
1030
1126
try!( space ( & mut self . s ) ) ;
1031
1127
try!( self . print_tts ( tts. as_slice ( ) ) ) ;
1032
1128
try!( space ( & mut self . s ) ) ;
1033
- word ( & mut self . s , parse :: token :: to_string ( & close. token ) . as_slice ( ) )
1129
+ word ( & mut self . s , token_to_string ( & close. token ) . as_slice ( ) )
1034
1130
} ,
1035
1131
ast:: TtToken ( _, ref tk) => {
1036
- try!( word ( & mut self . s , parse :: token :: to_string ( tk) . as_slice ( ) ) ) ;
1132
+ try!( word ( & mut self . s , token_to_string ( tk) . as_slice ( ) ) ) ;
1037
1133
match * tk {
1038
1134
parse:: token:: DocComment ( ..) => {
1039
1135
hardbreak ( & mut self . s )
@@ -1049,10 +1145,9 @@ impl<'a> State<'a> {
1049
1145
try!( word ( & mut self . s , ")" ) ) ;
1050
1146
match * separator {
1051
1147
Some ( ref tk) => {
1052
- try!( word ( & mut self . s ,
1053
- parse:: token:: to_string ( tk) . as_slice ( ) ) ) ;
1148
+ try!( word ( & mut self . s , token_to_string ( tk) . as_slice ( ) ) ) ;
1054
1149
}
1055
- None => ( )
1150
+ None => { } ,
1056
1151
}
1057
1152
match kleene_op {
1058
1153
ast:: ZeroOrMore => word ( & mut self . s , "*" ) ,
0 commit comments