2019-05-10 23:31:34 +00:00
|
|
|
//! Code related to parsing literals.
|
|
|
|
|
2019-06-05 08:56:06 +00:00
|
|
|
use crate::ast::{self, Lit, LitKind};
|
2019-10-11 16:40:56 +00:00
|
|
|
use crate::parse::token::{self, Token};
|
2019-05-19 16:56:45 +00:00
|
|
|
use crate::symbol::{kw, sym, Symbol};
|
2019-10-14 00:24:46 +00:00
|
|
|
use crate::tokenstream::TokenTree;
|
2019-05-10 23:31:34 +00:00
|
|
|
|
|
|
|
use log::debug;
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
|
|
|
use syntax_pos::Span;
|
2019-07-21 13:46:11 +00:00
|
|
|
use rustc_lexer::unescape::{unescape_char, unescape_byte};
|
|
|
|
use rustc_lexer::unescape::{unescape_str, unescape_byte_str};
|
|
|
|
use rustc_lexer::unescape::{unescape_raw_str, unescape_raw_byte_str};
|
2019-05-10 23:31:34 +00:00
|
|
|
|
|
|
|
use std::ascii;
|
|
|
|
|
2019-05-18 14:36:30 +00:00
|
|
|
crate enum LitError {
|
|
|
|
NotLiteral,
|
|
|
|
LexerError,
|
|
|
|
InvalidSuffix,
|
|
|
|
InvalidIntSuffix,
|
|
|
|
InvalidFloatSuffix,
|
2019-05-19 16:56:45 +00:00
|
|
|
NonDecimalFloat(u32),
|
2019-05-18 14:36:30 +00:00
|
|
|
IntTooLarge,
|
|
|
|
}
|
|
|
|
|
2019-05-10 23:31:34 +00:00
|
|
|
impl LitKind {
|
2019-05-19 16:56:45 +00:00
|
|
|
/// Converts literal token into a semantic literal.
|
|
|
|
fn from_lit_token(lit: token::Lit) -> Result<LitKind, LitError> {
|
|
|
|
let token::Lit { kind, symbol, suffix } = lit;
|
2019-05-18 22:04:26 +00:00
|
|
|
if suffix.is_some() && !kind.may_have_suffix() {
|
2019-05-18 14:36:30 +00:00
|
|
|
return Err(LitError::InvalidSuffix);
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 22:04:26 +00:00
|
|
|
Ok(match kind {
|
|
|
|
token::Bool => {
|
2019-08-27 08:21:41 +00:00
|
|
|
assert!(symbol.is_bool_lit());
|
2019-05-18 22:04:26 +00:00
|
|
|
LitKind::Bool(symbol == kw::True)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-19 16:56:45 +00:00
|
|
|
token::Byte => return unescape_byte(&symbol.as_str())
|
|
|
|
.map(LitKind::Byte).map_err(|_| LitError::LexerError),
|
|
|
|
token::Char => return unescape_char(&symbol.as_str())
|
|
|
|
.map(LitKind::Char).map_err(|_| LitError::LexerError),
|
2019-05-10 23:31:34 +00:00
|
|
|
|
|
|
|
// There are some valid suffixes for integer and float literals,
|
|
|
|
// so all the handling is done internally.
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Integer => return integer_lit(symbol, suffix),
|
|
|
|
token::Float => return float_lit(symbol, suffix),
|
2019-05-10 23:31:34 +00:00
|
|
|
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Str => {
|
2019-05-10 23:31:34 +00:00
|
|
|
// If there are no characters requiring special treatment we can
|
2019-05-18 19:45:24 +00:00
|
|
|
// reuse the symbol from the token. Otherwise, we must generate a
|
2019-05-10 23:31:34 +00:00
|
|
|
// new symbol because the string in the LitKind is different to the
|
2019-05-18 19:45:24 +00:00
|
|
|
// string in the token.
|
2019-05-19 16:56:45 +00:00
|
|
|
let s = symbol.as_str();
|
|
|
|
let symbol = if s.contains(&['\\', '\r'][..]) {
|
2019-05-10 23:31:34 +00:00
|
|
|
let mut buf = String::with_capacity(s.len());
|
2019-05-19 16:56:45 +00:00
|
|
|
let mut error = Ok(());
|
|
|
|
unescape_str(&s, &mut |_, unescaped_char| {
|
2019-05-10 23:31:34 +00:00
|
|
|
match unescaped_char {
|
|
|
|
Ok(c) => buf.push(c),
|
2019-05-19 16:56:45 +00:00
|
|
|
Err(_) => error = Err(LitError::LexerError),
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
});
|
2019-05-19 16:56:45 +00:00
|
|
|
error?;
|
|
|
|
Symbol::intern(&buf)
|
|
|
|
} else {
|
|
|
|
symbol
|
|
|
|
};
|
|
|
|
LitKind::Str(symbol, ast::StrStyle::Cooked)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-18 22:04:26 +00:00
|
|
|
token::StrRaw(n) => {
|
2019-05-10 23:31:34 +00:00
|
|
|
// Ditto.
|
2019-05-19 16:56:45 +00:00
|
|
|
let s = symbol.as_str();
|
|
|
|
let symbol = if s.contains('\r') {
|
2019-05-13 17:52:55 +00:00
|
|
|
let mut buf = String::with_capacity(s.len());
|
|
|
|
let mut error = Ok(());
|
2019-06-09 12:43:31 +00:00
|
|
|
unescape_raw_str(&s, &mut |_, unescaped_char| {
|
2019-05-13 17:52:55 +00:00
|
|
|
match unescaped_char {
|
|
|
|
Ok(c) => buf.push(c),
|
|
|
|
Err(_) => error = Err(LitError::LexerError),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
error?;
|
|
|
|
buf.shrink_to_fit();
|
|
|
|
Symbol::intern(&buf)
|
2019-05-19 16:56:45 +00:00
|
|
|
} else {
|
|
|
|
symbol
|
|
|
|
};
|
|
|
|
LitKind::Str(symbol, ast::StrStyle::Raw(n))
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-18 22:04:26 +00:00
|
|
|
token::ByteStr => {
|
2019-05-19 16:56:45 +00:00
|
|
|
let s = symbol.as_str();
|
2019-05-10 23:31:34 +00:00
|
|
|
let mut buf = Vec::with_capacity(s.len());
|
2019-05-19 16:56:45 +00:00
|
|
|
let mut error = Ok(());
|
|
|
|
unescape_byte_str(&s, &mut |_, unescaped_byte| {
|
2019-05-10 23:31:34 +00:00
|
|
|
match unescaped_byte {
|
|
|
|
Ok(c) => buf.push(c),
|
2019-05-19 16:56:45 +00:00
|
|
|
Err(_) => error = Err(LitError::LexerError),
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
});
|
2019-05-19 16:56:45 +00:00
|
|
|
error?;
|
2019-05-10 23:31:34 +00:00
|
|
|
buf.shrink_to_fit();
|
|
|
|
LitKind::ByteStr(Lrc::new(buf))
|
|
|
|
}
|
2019-06-09 12:43:31 +00:00
|
|
|
token::ByteStrRaw(_) => {
|
|
|
|
let s = symbol.as_str();
|
|
|
|
let bytes = if s.contains('\r') {
|
|
|
|
let mut buf = Vec::with_capacity(s.len());
|
|
|
|
let mut error = Ok(());
|
|
|
|
unescape_raw_byte_str(&s, &mut |_, unescaped_byte| {
|
|
|
|
match unescaped_byte {
|
|
|
|
Ok(c) => buf.push(c),
|
|
|
|
Err(_) => error = Err(LitError::LexerError),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
error?;
|
|
|
|
buf.shrink_to_fit();
|
|
|
|
buf
|
|
|
|
} else {
|
|
|
|
symbol.to_string().into_bytes()
|
|
|
|
};
|
|
|
|
|
|
|
|
LitKind::ByteStr(Lrc::new(bytes))
|
|
|
|
},
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Err => LitKind::Err(symbol),
|
2019-05-10 23:31:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to recover a token from semantic literal.
|
|
|
|
/// This function is used when the original token doesn't exist (e.g. the literal is created
|
|
|
|
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
|
2019-05-18 22:04:26 +00:00
|
|
|
pub fn to_lit_token(&self) -> token::Lit {
|
|
|
|
let (kind, symbol, suffix) = match *self {
|
2019-05-23 02:34:38 +00:00
|
|
|
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
|
|
|
|
// Don't re-intern unless the escaped string is different.
|
2019-09-05 05:52:47 +00:00
|
|
|
let s: &str = &symbol.as_str();
|
2019-05-23 02:34:38 +00:00
|
|
|
let escaped = s.escape_default().to_string();
|
|
|
|
let symbol = if escaped == *s { symbol } else { Symbol::intern(&escaped) };
|
|
|
|
(token::Str, symbol, None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-23 02:34:38 +00:00
|
|
|
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => {
|
|
|
|
(token::StrRaw(n), symbol, None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::ByteStr(ref bytes) => {
|
|
|
|
let string = bytes.iter().cloned().flat_map(ascii::escape_default)
|
|
|
|
.map(Into::<char>::into).collect::<String>();
|
2019-05-18 22:04:26 +00:00
|
|
|
(token::ByteStr, Symbol::intern(&string), None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::Byte(byte) => {
|
|
|
|
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
|
2019-05-18 22:04:26 +00:00
|
|
|
(token::Byte, Symbol::intern(&string), None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::Char(ch) => {
|
|
|
|
let string: String = ch.escape_default().map(Into::<char>::into).collect();
|
2019-05-18 22:04:26 +00:00
|
|
|
(token::Char, Symbol::intern(&string), None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::Int(n, ty) => {
|
|
|
|
let suffix = match ty {
|
2019-05-23 02:22:43 +00:00
|
|
|
ast::LitIntType::Unsigned(ty) => Some(ty.to_symbol()),
|
|
|
|
ast::LitIntType::Signed(ty) => Some(ty.to_symbol()),
|
2019-05-10 23:31:34 +00:00
|
|
|
ast::LitIntType::Unsuffixed => None,
|
|
|
|
};
|
2019-05-22 09:25:39 +00:00
|
|
|
(token::Integer, sym::integer(n), suffix)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::Float(symbol, ty) => {
|
2019-05-23 02:22:43 +00:00
|
|
|
(token::Float, symbol, Some(ty.to_symbol()))
|
2019-05-18 22:04:26 +00:00
|
|
|
}
|
|
|
|
LitKind::FloatUnsuffixed(symbol) => {
|
|
|
|
(token::Float, symbol, None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
LitKind::Bool(value) => {
|
2019-05-18 22:04:26 +00:00
|
|
|
let symbol = if value { kw::True } else { kw::False };
|
|
|
|
(token::Bool, symbol, None)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-18 22:04:26 +00:00
|
|
|
LitKind::Err(symbol) => {
|
|
|
|
(token::Err, symbol, None)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
token::Lit::new(kind, symbol, suffix)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lit {
|
2019-05-19 16:56:45 +00:00
|
|
|
/// Converts literal token into an AST literal.
|
2019-10-11 16:40:56 +00:00
|
|
|
crate fn from_lit_token(token: token::Lit, span: Span) -> Result<Lit, LitError> {
|
2019-09-26 15:56:53 +00:00
|
|
|
Ok(Lit { token, kind: LitKind::from_lit_token(token)?, span })
|
2019-05-18 14:36:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
/// Converts arbitrary token into an AST literal.
|
2019-06-05 10:24:54 +00:00
|
|
|
crate fn from_token(token: &Token) -> Result<Lit, LitError> {
|
|
|
|
let lit = match token.kind {
|
2019-08-27 08:21:41 +00:00
|
|
|
token::Ident(name, false) if name.is_bool_lit() =>
|
2019-06-05 08:56:06 +00:00
|
|
|
token::Lit::new(token::Bool, name, None),
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Literal(lit) =>
|
|
|
|
lit,
|
2019-05-10 23:31:34 +00:00
|
|
|
token::Interpolated(ref nt) => {
|
|
|
|
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt {
|
2019-09-26 13:39:48 +00:00
|
|
|
if let ast::ExprKind::Lit(lit) = &expr.kind {
|
2019-05-18 14:36:30 +00:00
|
|
|
return Ok(lit.clone());
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 14:36:30 +00:00
|
|
|
return Err(LitError::NotLiteral);
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-18 14:36:30 +00:00
|
|
|
_ => return Err(LitError::NotLiteral)
|
2019-05-10 23:31:34 +00:00
|
|
|
};
|
|
|
|
|
2019-06-05 10:24:54 +00:00
|
|
|
Lit::from_lit_token(lit, token.span)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to recover an AST literal from semantic literal.
|
|
|
|
/// This function is used when the original token doesn't exist (e.g. the literal is created
|
|
|
|
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
|
2019-09-26 15:56:53 +00:00
|
|
|
pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
|
|
|
|
Lit { token: kind.to_lit_token(), kind, span }
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 00:24:46 +00:00
|
|
|
/// Losslessly convert an AST literal into a token tree.
|
|
|
|
crate fn token_tree(&self) -> TokenTree {
|
2019-05-18 22:04:26 +00:00
|
|
|
let token = match self.token.kind {
|
2019-06-05 08:56:06 +00:00
|
|
|
token::Bool => token::Ident(self.token.symbol, false),
|
2019-05-18 22:04:26 +00:00
|
|
|
_ => token::Literal(self.token),
|
2019-05-10 23:31:34 +00:00
|
|
|
};
|
2019-10-14 00:24:46 +00:00
|
|
|
TokenTree::token(token, self.span)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
fn strip_underscores(symbol: Symbol) -> Symbol {
|
|
|
|
// Do not allocate a new string unless necessary.
|
|
|
|
let s = symbol.as_str();
|
|
|
|
if s.contains('_') {
|
|
|
|
let mut s = s.to_string();
|
|
|
|
s.retain(|c| c != '_');
|
|
|
|
return Symbol::intern(&s);
|
|
|
|
}
|
|
|
|
symbol
|
|
|
|
}
|
2019-05-10 23:31:34 +00:00
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
fn filtered_float_lit(symbol: Symbol, suffix: Option<Symbol>, base: u32)
|
|
|
|
-> Result<LitKind, LitError> {
|
|
|
|
debug!("filtered_float_lit: {:?}, {:?}, {:?}", symbol, suffix, base);
|
|
|
|
if base != 10 {
|
|
|
|
return Err(LitError::NonDecimalFloat(base));
|
|
|
|
}
|
|
|
|
Ok(match suffix {
|
|
|
|
Some(suf) => match suf {
|
|
|
|
sym::f32 => LitKind::Float(symbol, ast::FloatTy::F32),
|
|
|
|
sym::f64 => LitKind::Float(symbol, ast::FloatTy::F64),
|
|
|
|
_ => return Err(LitError::InvalidFloatSuffix),
|
|
|
|
}
|
|
|
|
None => LitKind::FloatUnsuffixed(symbol)
|
2019-05-10 23:31:34 +00:00
|
|
|
})
|
|
|
|
}
|
2019-05-18 14:36:30 +00:00
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
fn float_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitError> {
|
|
|
|
debug!("float_lit: {:?}, {:?}", symbol, suffix);
|
|
|
|
filtered_float_lit(strip_underscores(symbol), suffix, 10)
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitError> {
|
|
|
|
debug!("integer_lit: {:?}, {:?}", symbol, suffix);
|
|
|
|
let symbol = strip_underscores(symbol);
|
|
|
|
let s = symbol.as_str();
|
2019-05-10 23:31:34 +00:00
|
|
|
|
2019-10-08 08:59:05 +00:00
|
|
|
let base = match s.as_bytes() {
|
|
|
|
[b'0', b'x', ..] => 16,
|
|
|
|
[b'0', b'o', ..] => 8,
|
|
|
|
[b'0', b'b', ..] => 2,
|
|
|
|
_ => 10,
|
|
|
|
};
|
2019-05-10 23:31:34 +00:00
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
let ty = match suffix {
|
|
|
|
Some(suf) => match suf {
|
|
|
|
sym::isize => ast::LitIntType::Signed(ast::IntTy::Isize),
|
|
|
|
sym::i8 => ast::LitIntType::Signed(ast::IntTy::I8),
|
|
|
|
sym::i16 => ast::LitIntType::Signed(ast::IntTy::I16),
|
|
|
|
sym::i32 => ast::LitIntType::Signed(ast::IntTy::I32),
|
|
|
|
sym::i64 => ast::LitIntType::Signed(ast::IntTy::I64),
|
|
|
|
sym::i128 => ast::LitIntType::Signed(ast::IntTy::I128),
|
|
|
|
sym::usize => ast::LitIntType::Unsigned(ast::UintTy::Usize),
|
|
|
|
sym::u8 => ast::LitIntType::Unsigned(ast::UintTy::U8),
|
|
|
|
sym::u16 => ast::LitIntType::Unsigned(ast::UintTy::U16),
|
|
|
|
sym::u32 => ast::LitIntType::Unsigned(ast::UintTy::U32),
|
|
|
|
sym::u64 => ast::LitIntType::Unsigned(ast::UintTy::U64),
|
|
|
|
sym::u128 => ast::LitIntType::Unsigned(ast::UintTy::U128),
|
|
|
|
// `1f64` and `2f32` etc. are valid float literals, and
|
|
|
|
// `fxxx` looks more like an invalid float literal than invalid integer literal.
|
|
|
|
_ if suf.as_str().starts_with('f') => return filtered_float_lit(symbol, suffix, base),
|
2019-05-18 14:36:30 +00:00
|
|
|
_ => return Err(LitError::InvalidIntSuffix),
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
2019-05-19 16:56:45 +00:00
|
|
|
_ => ast::LitIntType::Unsuffixed
|
|
|
|
};
|
2019-05-10 23:31:34 +00:00
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
let s = &s[if base != 10 { 2 } else { 0 } ..];
|
|
|
|
u128::from_str_radix(s, base).map(|i| LitKind::Int(i, ty)).map_err(|_| {
|
|
|
|
// Small bases are lexed as if they were base 10, e.g, the string
|
|
|
|
// might be `0b10201`. This will cause the conversion above to fail,
|
|
|
|
// but these kinds of errors are already reported by the lexer.
|
|
|
|
let from_lexer =
|
|
|
|
base < 10 && s.chars().any(|c| c.to_digit(10).map_or(false, |d| d >= base));
|
|
|
|
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge }
|
2019-05-10 23:31:34 +00:00
|
|
|
})
|
|
|
|
}
|