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 10:46:32 +00:00
|
|
|
use crate::token::{self, Token};
|
2022-10-31 18:30:09 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-11-04 00:09:23 +00:00
|
|
|
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2019-05-10 23:31:34 +00:00
|
|
|
use std::ascii;
|
|
|
|
|
2019-10-15 20:48:13 +00:00
|
|
|
pub enum LitError {
|
2019-05-18 14:36:30 +00:00
|
|
|
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.
|
2022-08-01 06:46:08 +00:00
|
|
|
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
|
2019-05-19 16:56:45 +00:00
|
|
|
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 => {
|
2021-12-15 03:39:23 +00:00
|
|
|
return unescape_byte(symbol.as_str())
|
2019-05-19 16:56:45 +00:00
|
|
|
.map(LitKind::Byte)
|
|
|
|
.map_err(|_| LitError::LexerError);
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2019-05-19 16:56:45 +00:00
|
|
|
token::Char => {
|
2021-12-15 03:39:23 +00:00
|
|
|
return unescape_char(symbol.as_str())
|
2019-05-19 16:56:45 +00:00
|
|
|
.map(LitKind::Char)
|
|
|
|
.map_err(|_| LitError::LexerError);
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
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();
|
2022-02-24 05:49:37 +00:00
|
|
|
let symbol = if s.contains(&['\\', '\r']) {
|
|
|
|
let mut buf = String::with_capacity(s.len());
|
|
|
|
let mut error = Ok(());
|
|
|
|
// Force-inlining here is aggressive but the closure is
|
|
|
|
// called on every char in the string, so it can be
|
|
|
|
// hot in programs with many long strings.
|
|
|
|
unescape_literal(
|
|
|
|
&s,
|
|
|
|
Mode::Str,
|
|
|
|
&mut #[inline(always)]
|
|
|
|
|_, unescaped_char| match unescaped_char {
|
|
|
|
Ok(c) => buf.push(c),
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_fatal() {
|
|
|
|
error = Err(LitError::LexerError);
|
2021-07-30 14:09:33 +00:00
|
|
|
}
|
2020-05-13 08:03:49 +00:00
|
|
|
}
|
2022-02-24 05:49:37 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
error?;
|
|
|
|
Symbol::intern(&buf)
|
|
|
|
} else {
|
|
|
|
symbol
|
|
|
|
};
|
2019-05-19 16:56:45 +00:00
|
|
|
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();
|
2020-05-13 08:03:49 +00:00
|
|
|
let symbol =
|
|
|
|
if s.contains('\r') {
|
|
|
|
let mut buf = String::with_capacity(s.len());
|
|
|
|
let mut error = Ok(());
|
|
|
|
unescape_literal(&s, Mode::RawStr, &mut |_, unescaped_char| {
|
|
|
|
match unescaped_char {
|
|
|
|
Ok(c) => buf.push(c),
|
2021-07-30 14:09:33 +00:00
|
|
|
Err(err) => {
|
|
|
|
if err.is_fatal() {
|
|
|
|
error = Err(LitError::LexerError);
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 08:03:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
error?;
|
|
|
|
Symbol::intern(&buf)
|
|
|
|
} else {
|
|
|
|
symbol
|
|
|
|
};
|
2019-05-19 16:56:45 +00:00
|
|
|
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(());
|
2022-11-04 00:09:23 +00:00
|
|
|
unescape_literal(&s, Mode::ByteStr, &mut |_, c| match c {
|
|
|
|
Ok(c) => buf.push(byte_from_char(c)),
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_fatal() {
|
|
|
|
error = Err(LitError::LexerError);
|
2021-07-30 14:09:33 +00:00
|
|
|
}
|
2020-05-13 08:03:49 +00:00
|
|
|
}
|
2019-05-10 23:31:34 +00:00
|
|
|
});
|
2019-05-19 16:56:45 +00:00
|
|
|
error?;
|
2020-09-17 01:41:22 +00:00
|
|
|
LitKind::ByteStr(buf.into())
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
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(());
|
2022-11-04 00:09:23 +00:00
|
|
|
unescape_literal(&s, Mode::RawByteStr, &mut |_, c| match c {
|
|
|
|
Ok(c) => buf.push(byte_from_char(c)),
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_fatal() {
|
|
|
|
error = Err(LitError::LexerError);
|
2021-07-30 14:09:33 +00:00
|
|
|
}
|
2020-05-13 08:03:49 +00:00
|
|
|
}
|
2019-06-09 12:43:31 +00:00
|
|
|
});
|
|
|
|
error?;
|
|
|
|
buf
|
|
|
|
} else {
|
|
|
|
symbol.to_string().into_bytes()
|
|
|
|
};
|
|
|
|
|
2020-09-17 01:41:22 +00:00
|
|
|
LitKind::ByteStr(bytes.into())
|
2019-06-09 12:43:31 +00:00
|
|
|
}
|
2022-08-22 03:27:52 +00:00
|
|
|
token::Err => LitKind::Err,
|
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).
|
2022-08-01 06:46:08 +00:00
|
|
|
pub fn to_token_lit(&self) -> token::Lit {
|
2019-05-18 22:04:26 +00:00
|
|
|
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-10-22 00:04:25 +00:00
|
|
|
let s = symbol.as_str();
|
2019-05-23 02:34:38 +00:00
|
|
|
let escaped = s.escape_default().to_string();
|
2019-10-22 00:04:25 +00:00
|
|
|
let symbol = if s == escaped { symbol } else { Symbol::intern(&escaped) };
|
2019-05-23 02:34:38 +00:00
|
|
|
(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) => {
|
2022-08-19 16:53:27 +00:00
|
|
|
let string = bytes.escape_ascii().to_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-10-28 02:46:22 +00:00
|
|
|
ast::LitIntType::Unsigned(ty) => Some(ty.name()),
|
|
|
|
ast::LitIntType::Signed(ty) => Some(ty.name()),
|
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-10-28 02:46:22 +00:00
|
|
|
let suffix = match ty {
|
|
|
|
ast::LitFloatType::Suffixed(ty) => Some(ty.name()),
|
|
|
|
ast::LitFloatType::Unsuffixed => None,
|
|
|
|
};
|
|
|
|
(token::Float, symbol, suffix)
|
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
|
|
|
}
|
2022-08-24 23:41:48 +00:00
|
|
|
// This only shows up in places like `-Zunpretty=hir` output, so we
|
|
|
|
// don't bother to produce something useful.
|
|
|
|
LitKind::Err => (token::Err, Symbol::intern("<bad-literal>"), None),
|
2019-05-18 22:04:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
2022-08-01 06:46:08 +00:00
|
|
|
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<Lit, LitError> {
|
|
|
|
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
|
2019-05-18 14:36:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 16:56:45 +00:00
|
|
|
/// Converts arbitrary token into an AST literal.
|
2020-01-30 12:02:06 +00:00
|
|
|
///
|
2020-03-16 22:36:14 +00:00
|
|
|
/// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
|
2019-10-15 20:48:13 +00:00
|
|
|
pub fn from_token(token: &Token) -> Result<Lit, LitError> {
|
2020-03-07 12:58:27 +00:00
|
|
|
let lit = match token.uninterpolate().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-12-22 22:42:04 +00:00
|
|
|
}
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Literal(lit) => lit,
|
2020-07-01 10:16:49 +00:00
|
|
|
token::Interpolated(ref nt) => {
|
2022-02-26 16:45:36 +00:00
|
|
|
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt
|
|
|
|
&& let ast::ExprKind::Lit(lit) = &expr.kind
|
|
|
|
{
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-08-01 06:46:08 +00:00
|
|
|
Lit::from_token_lit(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 {
|
2022-08-01 06:46:08 +00:00
|
|
|
Lit { token_lit: kind.to_token_lit(), kind, span }
|
2019-05-10 23:31:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 18:30:09 +00:00
|
|
|
/// Recovers an AST literal from a string of bytes produced by `include_bytes!`.
|
|
|
|
/// This requires ASCII-escaping the string, which can result in poor performance
|
|
|
|
/// for very large strings of bytes.
|
|
|
|
pub fn from_included_bytes(bytes: &Lrc<[u8]>, span: Span) -> Lit {
|
|
|
|
Self::from_lit_kind(LitKind::ByteStr(bytes.clone()), span)
|
|
|
|
}
|
|
|
|
|
2020-12-19 20:38:22 +00:00
|
|
|
/// Losslessly convert an AST literal into a token.
|
|
|
|
pub fn to_token(&self) -> Token {
|
2022-08-01 06:46:08 +00:00
|
|
|
let kind = match self.token_lit.kind {
|
|
|
|
token::Bool => token::Ident(self.token_lit.symbol, false),
|
|
|
|
_ => token::Literal(self.token_lit),
|
2019-05-10 23:31:34 +00:00
|
|
|
};
|
2020-12-19 20:38:22 +00:00
|
|
|
Token::new(kind, 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 {
|
2019-10-28 02:46:22 +00:00
|
|
|
Some(suf) => LitKind::Float(
|
|
|
|
symbol,
|
|
|
|
ast::LitFloatType::Suffixed(match suf {
|
|
|
|
sym::f32 => ast::FloatTy::F32,
|
|
|
|
sym::f64 => ast::FloatTy::F64,
|
2019-05-19 16:56:45 +00:00
|
|
|
_ => return Err(LitError::InvalidFloatSuffix),
|
2019-10-28 02:46:22 +00:00
|
|
|
}),
|
2019-12-22 22:42:04 +00:00
|
|
|
),
|
2019-10-28 02:46:22 +00:00
|
|
|
None => LitKind::Float(symbol, ast::LitFloatType::Unsuffixed),
|
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
|
|
|
})
|
|
|
|
}
|