diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-05-09 02:17:32 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-05-11 14:24:21 +0300 |
| commit | f2834a403abe78f56d750a302807eab5206bb2c5 (patch) | |
| tree | 40282168119247e0e033d1d5dca4caa47e47feb6 /src/libsyntax/parse | |
| parent | 28b125b83d9db4094a08b512a956c187bd29a51f (diff) | |
| download | rust-f2834a403abe78f56d750a302807eab5206bb2c5.tar.gz rust-f2834a403abe78f56d750a302807eab5206bb2c5.zip | |
Keep the original token in `ast::Lit`
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 30 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 5 |
3 files changed, 21 insertions, 15 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index be44b964ba5..4d4e99009a9 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -376,6 +376,7 @@ crate fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Ha use ast::LitKind; match lit { + token::Bool(_) => panic!("literal token contains `Lit::Bool`"), token::Byte(i) => { let lit_kind = match unescape_byte(&i.as_str()) { Ok(c) => LitKind::Byte(c), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2b30d2db95e..b988cb1447d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2070,11 +2070,11 @@ impl<'a> Parser<'a> { } /// Matches `token_lit = LIT_INTEGER | ...`. - fn parse_lit_token(&mut self) -> PResult<'a, LitKind> { + fn parse_lit_token(&mut self) -> PResult<'a, (LitKind, token::Lit, Option<Symbol>)> { let out = match self.token { token::Interpolated(ref nt) => match **nt { token::NtExpr(ref v) | token::NtLiteral(ref v) => match v.node { - ExprKind::Lit(ref lit) => { lit.node.clone() } + ExprKind::Lit(ref lit) => { (lit.node.clone(), lit.token, lit.suffix) } _ => { return self.unexpected_last(&self.token); } }, _ => { return self.unexpected_last(&self.token); } @@ -2088,19 +2088,19 @@ impl<'a> Parser<'a> { self.expect_no_suffix(sp, &format!("a {}", lit.literal_name()), suf) } - result.unwrap() + (result.unwrap(), lit, suf) } token::Dot if self.look_ahead(1, |t| match t { - token::Literal(parse::token::Lit::Integer(_) , _) => true, + token::Literal(token::Lit::Integer(_) , _) => true, _ => false, }) => { // recover from `let x = .4;` let lo = self.span; self.bump(); if let token::Literal( - parse::token::Lit::Integer(val), + token::Lit::Integer(val), suffix, ) = self.token { - let suffix = suffix.and_then(|s| { + let float_suffix = suffix.and_then(|s| { let s = s.as_str(); if s == "f32" { Some("f32") @@ -2117,14 +2117,14 @@ impl<'a> Parser<'a> { err.span_suggestion( sp, "must have an integer part", - format!("0.{}{}", val, suffix), + format!("0.{}{}", val, float_suffix), Applicability::MachineApplicable, ); err.emit(); - return Ok(match suffix { - "f32" => ast::LitKind::Float(val, ast::FloatTy::F32), - "f64" => ast::LitKind::Float(val, ast::FloatTy::F64), - _ => ast::LitKind::FloatUnsuffixed(val), + return Ok(match float_suffix { + "f32" => (ast::LitKind::Float(val, ast::FloatTy::F32), token::Float(val), suffix), + "f64" => (ast::LitKind::Float(val, ast::FloatTy::F64), token::Float(val), suffix), + _ => (ast::LitKind::FloatUnsuffixed(val), token::Float(val), suffix), }); } else { unreachable!(); @@ -2140,14 +2140,14 @@ impl<'a> Parser<'a> { /// Matches `lit = true | false | token_lit`. crate fn parse_lit(&mut self) -> PResult<'a, Lit> { let lo = self.span; - let node = if self.eat_keyword(keywords::True) { - LitKind::Bool(true) + let (node, token, suffix) = if self.eat_keyword(keywords::True) { + (LitKind::Bool(true), token::Bool(keywords::True.name()), None) } else if self.eat_keyword(keywords::False) { - LitKind::Bool(false) + (LitKind::Bool(false), token::Bool(keywords::False.name()), None) } else { self.parse_lit_token()? }; - Ok(Lit { node, span: lo.to(self.prev_span) }) + Ok(Lit { node, token, suffix, span: lo.to(self.prev_span) }) } /// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`). diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fd7a39c576d..48a949257ff 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -61,6 +61,7 @@ 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), @@ -72,9 +73,13 @@ pub enum Lit { ByteStrRaw(ast::Name, u16), /* raw byte str delimited by n hash symbols */ } +#[cfg(target_arch = "x86_64")] +static_assert!(MEM_SIZE_OF_LIT: mem::size_of::<Lit>() == 8); + impl Lit { crate fn literal_name(&self) -> &'static str { match *self { + Bool(_) => panic!("literal token contains `Lit::Bool`"), Byte(_) => "byte literal", Char(_) => "char literal", Err(_) => "invalid literal", |
