Skip to content

Commit 665ad9c

Browse files
committed
Move token-to-string functions into print::pprust
1 parent cd04959 commit 665ad9c

File tree

5 files changed

+108
-106
lines changed

5 files changed

+108
-106
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use parse::attr::ParserAttr;
8787
use parse::parser::{LifetimeAndTypesWithoutColons, Parser};
8888
use parse::token::{Token, Nonterminal};
8989
use parse::token;
90+
use print::pprust;
9091
use ptr::P;
9192

9293
use std::rc::Rc;
@@ -402,7 +403,7 @@ pub fn parse(sess: &ParseSess,
402403
nts, next_eis.len()).to_string());
403404
} else if bb_eis.len() == 0u && next_eis.len() == 0u {
404405
return Failure(sp, format!("no rules expected the token `{}`",
405-
token::to_string(&tok)).to_string());
406+
pprust::token_to_string(&tok)).to_string());
406407
} else if next_eis.len() > 0u {
407408
/* Now process the next token */
408409
while next_eis.len() > 0u {
@@ -449,7 +450,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal {
449450
"ident" => match p.token {
450451
token::Ident(sn,b) => { p.bump(); token::NtIdent(box sn,b) }
451452
_ => {
452-
let token_str = token::to_string(&p.token);
453+
let token_str = pprust::token_to_string(&p.token);
453454
p.fatal((format!("expected ident, found {}",
454455
token_str.as_slice())).as_slice())
455456
}

src/libsyntax/parse/lexer/comments.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use parse::lexer::{is_whitespace, Reader};
1515
use parse::lexer::{StringReader, TokenAndSpan};
1616
use parse::lexer::is_block_doc_comment;
1717
use parse::lexer;
18-
use parse::token;
18+
use print::pprust;
1919

2020
use std::io;
2121
use std::str;
@@ -373,7 +373,7 @@ pub fn gather_comments_and_literals(span_diagnostic: &diagnostic::SpanHandler,
373373
literals.push(Literal {lit: s.to_string(), pos: sp.lo});
374374
})
375375
} else {
376-
debug!("tok: {}", token::to_string(&tok));
376+
debug!("tok: {}", pprust::token_to_string(&tok));
377377
}
378378
first_read = false;
379379
}

src/libsyntax/parse/parser.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use parse::token::InternedString;
7878
use parse::token::{keywords, special_idents};
7979
use parse::token;
8080
use parse::{new_sub_parser_from_file, ParseSess};
81+
use print::pprust;
8182
use ptr::P;
8283
use owned_slice::OwnedSlice;
8384

@@ -394,7 +395,7 @@ impl<'a> Parser<'a> {
394395

395396
/// Convert a token to a string using self's reader
396397
pub fn token_to_string(token: &token::Token) -> String {
397-
token::to_string(token)
398+
pprust::token_to_string(token)
398399
}
399400

400401
/// Convert the current token to a string using self's reader

src/libsyntax/parse/token.rs

-95
Original file line numberDiff line numberDiff line change
@@ -431,101 +431,6 @@ impl fmt::Show for Nonterminal {
431431
}
432432
}
433433

434-
pub fn binop_to_string(o: BinOpToken) -> &'static str {
435-
match o {
436-
Plus => "+",
437-
Minus => "-",
438-
Star => "*",
439-
Slash => "/",
440-
Percent => "%",
441-
Caret => "^",
442-
And => "&",
443-
Or => "|",
444-
Shl => "<<",
445-
Shr => ">>",
446-
}
447-
}
448-
449-
pub fn to_string(t: &Token) -> String {
450-
match *t {
451-
Eq => "=".into_string(),
452-
Lt => "<".into_string(),
453-
Le => "<=".into_string(),
454-
EqEq => "==".into_string(),
455-
Ne => "!=".into_string(),
456-
Ge => ">=".into_string(),
457-
Gt => ">".into_string(),
458-
Not => "!".into_string(),
459-
Tilde => "~".into_string(),
460-
OrOr => "||".into_string(),
461-
AndAnd => "&&".into_string(),
462-
BinOp(op) => binop_to_string(op).into_string(),
463-
BinOpEq(op) => format!("{}=", binop_to_string(op)),
464-
465-
/* Structural symbols */
466-
At => "@".into_string(),
467-
Dot => ".".into_string(),
468-
DotDot => "..".into_string(),
469-
DotDotDot => "...".into_string(),
470-
Comma => ",".into_string(),
471-
Semi => ";".into_string(),
472-
Colon => ":".into_string(),
473-
ModSep => "::".into_string(),
474-
RArrow => "->".into_string(),
475-
LArrow => "<-".into_string(),
476-
FatArrow => "=>".into_string(),
477-
LParen => "(".into_string(),
478-
RParen => ")".into_string(),
479-
LBracket => "[".into_string(),
480-
RBracket => "]".into_string(),
481-
LBrace => "{".into_string(),
482-
RBrace => "}".into_string(),
483-
Pound => "#".into_string(),
484-
Dollar => "$".into_string(),
485-
Question => "?".into_string(),
486-
487-
/* Literals */
488-
LitByte(b) => format!("b'{}'", b.as_str()),
489-
LitChar(c) => format!("'{}'", c.as_str()),
490-
LitFloat(c) => c.as_str().into_string(),
491-
LitInteger(c) => c.as_str().into_string(),
492-
LitStr(s) => format!("\"{}\"", s.as_str()),
493-
LitStrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
494-
delim="#".repeat(n),
495-
string=s.as_str()),
496-
LitBinary(v) => format!("b\"{}\"", v.as_str()),
497-
LitBinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
498-
delim="#".repeat(n),
499-
string=s.as_str()),
500-
501-
/* Name components */
502-
Ident(s, _) => get_ident(s).get().into_string(),
503-
Lifetime(s) => format!("{}", get_ident(s)),
504-
Underscore => "_".into_string(),
505-
506-
/* Other */
507-
DocComment(s) => s.as_str().into_string(),
508-
Eof => "<eof>".into_string(),
509-
Whitespace => " ".into_string(),
510-
Comment => "/* */".into_string(),
511-
Shebang(s) => format!("/* shebang: {}*/", s.as_str()),
512-
513-
Interpolated(ref nt) => match *nt {
514-
NtExpr(ref e) => ::print::pprust::expr_to_string(&**e),
515-
NtMeta(ref e) => ::print::pprust::meta_item_to_string(&**e),
516-
NtTy(ref e) => ::print::pprust::ty_to_string(&**e),
517-
NtPath(ref e) => ::print::pprust::path_to_string(&**e),
518-
NtItem(..) => "an interpolated item".into_string(),
519-
NtBlock(..) => "an interpolated block".into_string(),
520-
NtStmt(..) => "an interpolated statement".into_string(),
521-
NtPat(..) => "an interpolated pattern".into_string(),
522-
NtIdent(..) => "an interpolated identifier".into_string(),
523-
NtTT(..) => "an interpolated tt".into_string(),
524-
NtMatchers(..) => "an interpolated matcher sequence".into_string(),
525-
}
526-
}
527-
}
528-
529434
// Get the first "argument"
530435
macro_rules! first {
531436
( $first:expr, $( $remainder:expr, )* ) => ( $first )

src/libsyntax/print/pprust.rs

+101-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use attr::{AttrMetaMethods, AttributeMethods};
2121
use codemap::{CodeMap, BytePos};
2222
use codemap;
2323
use diagnostic;
24+
use parse::token::{BinOpToken, Token};
2425
use parse::token;
2526
use parse::lexer::comments;
2627
use parse;
@@ -181,6 +182,101 @@ pub fn to_string(f: |&mut State| -> IoResult<()>) -> String {
181182
}
182183
}
183184

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+
184280
// FIXME (Issue #16472): the thing_to_string_impls macro should go away
185281
// after we revise the syntax::ext::quote::ToToken impls to go directly
186282
// to token-trees instead of thing -> string -> token-trees.
@@ -1026,14 +1122,14 @@ impl<'a> State<'a> {
10261122
match *tt {
10271123
ast::TtDelimited(_, ref delimed) => {
10281124
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()));
10301126
try!(space(&mut self.s));
10311127
try!(self.print_tts(tts.as_slice()));
10321128
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())
10341130
},
10351131
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()));
10371133
match *tk {
10381134
parse::token::DocComment(..) => {
10391135
hardbreak(&mut self.s)
@@ -1049,10 +1145,9 @@ impl<'a> State<'a> {
10491145
try!(word(&mut self.s, ")"));
10501146
match *separator {
10511147
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()));
10541149
}
1055-
None => ()
1150+
None => {},
10561151
}
10571152
match kleene_op {
10581153
ast::ZeroOrMore => word(&mut self.s, "*"),

0 commit comments

Comments
 (0)