Move token-to-string functions into print::pprust

This commit is contained in:
Brendan Zabarauskas 2014-10-28 11:05:28 +11:00
parent cd049591a2
commit 665ad9c175
5 changed files with 108 additions and 106 deletions

View file

@ -431,101 +431,6 @@ impl fmt::Show for Nonterminal {
}
}
pub fn binop_to_string(o: BinOpToken) -> &'static str {
match o {
Plus => "+",
Minus => "-",
Star => "*",
Slash => "/",
Percent => "%",
Caret => "^",
And => "&",
Or => "|",
Shl => "<<",
Shr => ">>",
}
}
pub fn to_string(t: &Token) -> String {
match *t {
Eq => "=".into_string(),
Lt => "<".into_string(),
Le => "<=".into_string(),
EqEq => "==".into_string(),
Ne => "!=".into_string(),
Ge => ">=".into_string(),
Gt => ">".into_string(),
Not => "!".into_string(),
Tilde => "~".into_string(),
OrOr => "||".into_string(),
AndAnd => "&&".into_string(),
BinOp(op) => binop_to_string(op).into_string(),
BinOpEq(op) => format!("{}=", binop_to_string(op)),
/* Structural symbols */
At => "@".into_string(),
Dot => ".".into_string(),
DotDot => "..".into_string(),
DotDotDot => "...".into_string(),
Comma => ",".into_string(),
Semi => ";".into_string(),
Colon => ":".into_string(),
ModSep => "::".into_string(),
RArrow => "->".into_string(),
LArrow => "<-".into_string(),
FatArrow => "=>".into_string(),
LParen => "(".into_string(),
RParen => ")".into_string(),
LBracket => "[".into_string(),
RBracket => "]".into_string(),
LBrace => "{".into_string(),
RBrace => "}".into_string(),
Pound => "#".into_string(),
Dollar => "$".into_string(),
Question => "?".into_string(),
/* Literals */
LitByte(b) => format!("b'{}'", b.as_str()),
LitChar(c) => format!("'{}'", c.as_str()),
LitFloat(c) => c.as_str().into_string(),
LitInteger(c) => c.as_str().into_string(),
LitStr(s) => format!("\"{}\"", s.as_str()),
LitStrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
delim="#".repeat(n),
string=s.as_str()),
LitBinary(v) => format!("b\"{}\"", v.as_str()),
LitBinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
delim="#".repeat(n),
string=s.as_str()),
/* Name components */
Ident(s, _) => get_ident(s).get().into_string(),
Lifetime(s) => format!("{}", get_ident(s)),
Underscore => "_".into_string(),
/* Other */
DocComment(s) => s.as_str().into_string(),
Eof => "<eof>".into_string(),
Whitespace => " ".into_string(),
Comment => "/* */".into_string(),
Shebang(s) => format!("/* shebang: {}*/", s.as_str()),
Interpolated(ref nt) => match *nt {
NtExpr(ref e) => ::print::pprust::expr_to_string(&**e),
NtMeta(ref e) => ::print::pprust::meta_item_to_string(&**e),
NtTy(ref e) => ::print::pprust::ty_to_string(&**e),
NtPath(ref e) => ::print::pprust::path_to_string(&**e),
NtItem(..) => "an interpolated item".into_string(),
NtBlock(..) => "an interpolated block".into_string(),
NtStmt(..) => "an interpolated statement".into_string(),
NtPat(..) => "an interpolated pattern".into_string(),
NtIdent(..) => "an interpolated identifier".into_string(),
NtTT(..) => "an interpolated tt".into_string(),
NtMatchers(..) => "an interpolated matcher sequence".into_string(),
}
}
}
// Get the first "argument"
macro_rules! first {
( $first:expr, $( $remainder:expr, )* ) => ( $first )