Skip to content

Commit 9aaa7c7

Browse files
committed
syntax: Move some Token methods around
1 parent 25b0514 commit 9aaa7c7

File tree

1 file changed

+40
-48
lines changed

1 file changed

+40
-48
lines changed

src/libsyntax/parse/token.rs

+40-48
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,51 @@ pub struct Token {
240240
pub span: Span,
241241
}
242242

243+
impl TokenKind {
244+
pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> TokenKind {
245+
Literal(Lit::new(kind, symbol, suffix))
246+
}
247+
248+
/// Returns tokens that are likely to be typed accidentally instead of the current token.
249+
/// Enables better error recovery when the wrong token is found.
250+
crate fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
251+
match *self {
252+
Comma => Some(vec![Dot, Lt, Semi]),
253+
Semi => Some(vec![Colon, Comma]),
254+
_ => None
255+
}
256+
}
257+
}
258+
243259
impl Token {
260+
crate fn new(kind: TokenKind, span: Span) -> Self {
261+
Token { kind, span }
262+
}
263+
264+
/// Some token that will be thrown away later.
265+
crate fn dummy() -> Self {
266+
Token::new(TokenKind::Whitespace, DUMMY_SP)
267+
}
268+
244269
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
245-
crate fn from_ast_ident(ident: ast::Ident) -> Token {
270+
crate fn from_ast_ident(ident: ast::Ident) -> Self {
246271
Token::new(Ident(ident.name, ident.is_raw_guess()), ident.span)
247272
}
248273

274+
/// Return this token by value and leave a dummy token in its place.
275+
crate fn take(&mut self) -> Self {
276+
mem::replace(self, Token::dummy())
277+
}
278+
279+
crate fn is_op(&self) -> bool {
280+
match self.kind {
281+
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
282+
Ident(..) | Lifetime(..) | Interpolated(..) |
283+
Whitespace | Comment | Shebang(..) | Eof => false,
284+
_ => true,
285+
}
286+
}
287+
249288
crate fn is_like_plus(&self) -> bool {
250289
match self.kind {
251290
BinOp(Plus) | BinOpEq(Plus) => true,
@@ -327,15 +366,7 @@ impl Token {
327366
self.is_path_start() || self.is_lifetime() || self.is_keyword(kw::For) ||
328367
self == &Question || self == &OpenDelim(Paren)
329368
}
330-
}
331-
332-
impl TokenKind {
333-
pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> TokenKind {
334-
Literal(Lit::new(kind, symbol, suffix))
335-
}
336-
}
337369

338-
impl Token {
339370
/// Returns `true` if the token is any literal
340371
crate fn is_lit(&self) -> bool {
341372
match self.kind {
@@ -535,21 +566,7 @@ impl Token {
535566

536567
Some(Token::new(kind, self.span.to(joint.span)))
537568
}
538-
}
539-
540-
impl TokenKind {
541-
/// Returns tokens that are likely to be typed accidentally instead of the current token.
542-
/// Enables better error recovery when the wrong token is found.
543-
crate fn similar_tokens(&self) -> Option<Vec<TokenKind>> {
544-
match *self {
545-
Comma => Some(vec![Dot, Lt, Semi]),
546-
Semi => Some(vec![Colon, Comma]),
547-
_ => None
548-
}
549-
}
550-
}
551569

552-
impl Token {
553570
// See comments in `Nonterminal::to_tokenstream` for why we care about
554571
// *probably* equal here rather than actual equality
555572
crate fn probably_equal_for_proc_macro(&self, other: &Token) -> bool {
@@ -608,20 +625,6 @@ impl Token {
608625
_ => panic!("forgot to add a token?"),
609626
}
610627
}
611-
612-
crate fn new(kind: TokenKind, span: Span) -> Self {
613-
Token { kind, span }
614-
}
615-
616-
/// Some token that will be thrown away later.
617-
crate fn dummy() -> Self {
618-
Token::new(TokenKind::Whitespace, DUMMY_SP)
619-
}
620-
621-
/// Return this token by value and leave a dummy token in its place.
622-
crate fn take(&mut self) -> Self {
623-
mem::replace(self, Token::dummy())
624-
}
625628
}
626629

627630
impl PartialEq<TokenKind> for Token {
@@ -769,17 +772,6 @@ impl Nonterminal {
769772
}
770773
}
771774

772-
impl Token {
773-
crate fn is_op(&self) -> bool {
774-
match self.kind {
775-
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
776-
Ident(..) | Lifetime(..) | Interpolated(..) |
777-
Whitespace | Comment | Shebang(..) | Eof => false,
778-
_ => true,
779-
}
780-
}
781-
}
782-
783775
fn prepend_attrs(sess: &ParseSess,
784776
attrs: &[ast::Attribute],
785777
tokens: Option<&tokenstream::TokenStream>,

0 commit comments

Comments
 (0)