diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-05-19 01:04:26 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-05-23 12:46:24 +0300 |
| commit | ca2a50fad7439f02a9e99f8107ffed8460fc8c44 (patch) | |
| tree | 4552fd5c786238aa51f716f983804f8c101dc1fd /src/libsyntax/parse/token.rs | |
| parent | 558559e70f648ff518da5ada726da2f04b617197 (diff) | |
| download | rust-ca2a50fad7439f02a9e99f8107ffed8460fc8c44.tar.gz rust-ca2a50fad7439f02a9e99f8107ffed8460fc8c44.zip | |
syntax: Turn `token::Lit` into a struct
Diffstat (limited to 'src/libsyntax/parse/token.rs')
| -rw-r--r-- | src/libsyntax/parse/token.rs | 95 |
1 files changed, 51 insertions, 44 deletions
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fbc27d1999d..4711a156ab1 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -1,7 +1,7 @@ pub use BinOpToken::*; pub use Nonterminal::*; pub use DelimToken::*; -pub use Lit::*; +pub use LitKind::*; pub use Token::*; use crate::ast::{self}; @@ -59,59 +59,62 @@ impl DelimToken { } } -#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] -pub enum Lit { - Bool(ast::Name), // AST only, must never appear in a `Token` - Byte(ast::Name), - Char(ast::Name), - Err(ast::Name), - Integer(ast::Name), - Float(ast::Name), - Str_(ast::Name), - StrRaw(ast::Name, u16), /* raw str delimited by n hash symbols */ - ByteStr(ast::Name), - ByteStrRaw(ast::Name, u16), /* raw byte str delimited by n hash symbols */ +#[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)] +pub enum LitKind { + Bool, // AST only, must never appear in a `Token` + Byte, + Char, + Integer, + Float, + Str, + StrRaw(u16), // raw string delimited by `n` hash symbols + ByteStr, + ByteStrRaw(u16), // raw byte string delimited by `n` hash symbols + Err, } -#[cfg(target_arch = "x86_64")] -static_assert_size!(Lit, 8); - -impl Lit { - crate fn symbol(&self) -> Symbol { - match *self { - Bool(s) | Byte(s) | Char(s) | Integer(s) | Float(s) | Err(s) | - Str_(s) | StrRaw(s, _) | ByteStr(s) | ByteStrRaw(s, _) => s - } - } +#[derive(Clone, Copy, PartialEq, RustcEncodable, RustcDecodable, Debug)] +pub struct Lit { + pub kind: LitKind, + pub symbol: Symbol, + pub suffix: Option<Symbol>, +} - crate fn article(&self) -> &'static str { - match *self { - Integer(_) | Err(_) => "an", +impl LitKind { + crate fn article(self) -> &'static str { + match self { + Integer | Err => "an", _ => "a", } } - crate fn descr(&self) -> &'static str { - match *self { - Bool(_) => panic!("literal token contains `Lit::Bool`"), - Byte(_) => "byte literal", - Char(_) => "char literal", - Err(_) => "invalid literal", - Integer(_) => "integer literal", - Float(_) => "float literal", - Str_(_) | StrRaw(..) => "string literal", - ByteStr(_) | ByteStrRaw(..) => "byte string literal" + crate fn descr(self) -> &'static str { + match self { + Bool => panic!("literal token contains `Lit::Bool`"), + Byte => "byte literal", + Char => "char literal", + Integer => "integer literal", + Float => "float literal", + Str | StrRaw(..) => "string literal", + ByteStr | ByteStrRaw(..) => "byte string literal", + Err => "invalid literal", } } - crate fn may_have_suffix(&self) -> bool { - match *self { - Integer(..) | Float(..) | Err(..) => true, + crate fn may_have_suffix(self) -> bool { + match self { + Integer | Float | Err => true, _ => false, } } } +impl Lit { + pub fn new(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Lit { + Lit { kind, symbol, suffix } + } +} + pub(crate) fn ident_can_begin_expr(ident: ast::Ident, is_raw: bool) -> bool { let ident_token: Token = Ident(ident, is_raw); @@ -201,7 +204,7 @@ pub enum Token { CloseDelim(DelimToken), /* Literals */ - Literal(Lit, Option<ast::Name>), + Literal(Lit), /* Name components */ Ident(ast::Ident, /* is_raw */ bool), @@ -318,6 +321,10 @@ impl Token { self == &Question || self == &OpenDelim(Paren) } + pub fn lit(kind: LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Token { + Literal(Lit::new(kind, symbol, suffix)) + } + /// Returns `true` if the token is any literal crate fn is_lit(&self) -> bool { match *self { @@ -326,9 +333,9 @@ impl Token { } } - crate fn expect_lit(&self) -> (Lit, Option<Symbol>) { + crate fn expect_lit(&self) -> Lit { match *self { - Literal(lit, suf) => (lit, suf), + Literal(lit) => lit, _=> panic!("`expect_lit` called on non-literal"), } } @@ -579,13 +586,13 @@ impl Token { (&DocComment(a), &DocComment(b)) | (&Shebang(a), &Shebang(b)) => a == b, + (&Literal(a), &Literal(b)) => a == b, + (&Lifetime(a), &Lifetime(b)) => a.name == b.name, (&Ident(a, b), &Ident(c, d)) => b == d && (a.name == c.name || a.name == kw::DollarCrate || c.name == kw::DollarCrate), - (&Literal(a, b), &Literal(c, d)) => b == d && a == c, - (&Interpolated(_), &Interpolated(_)) => false, _ => panic!("forgot to add a token?"), |
