2019-05-11 02:31:34 +03:00
|
|
|
//! Code related to parsing literals.
|
|
|
|
|
|
2022-11-29 13:35:44 +11:00
|
|
|
use crate::ast::{self, LitKind, MetaItemLit, StrStyle};
|
2019-10-11 12:46:32 +02:00
|
|
|
use crate::token::{self, Token};
|
2023-03-05 15:03:22 +00:00
|
|
|
use rustc_lexer::unescape::{
|
|
|
|
|
byte_from_char, unescape_byte, unescape_c_string, unescape_char, unescape_literal, CStrUnit,
|
|
|
|
|
Mode,
|
|
|
|
|
};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2022-12-05 16:09:45 +11:00
|
|
|
use std::{ascii, fmt, str};
|
2019-05-11 02:31:34 +03:00
|
|
|
|
2022-12-05 14:16:41 +11:00
|
|
|
// Escapes a string, represented as a symbol. Reuses the original symbol,
|
|
|
|
|
// avoiding interning, if no changes are required.
|
|
|
|
|
pub fn escape_string_symbol(symbol: Symbol) -> Symbol {
|
|
|
|
|
let s = symbol.as_str();
|
|
|
|
|
let escaped = s.escape_default().to_string();
|
|
|
|
|
if s == escaped { symbol } else { Symbol::intern(&escaped) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Escapes a char.
|
|
|
|
|
pub fn escape_char_symbol(ch: char) -> Symbol {
|
|
|
|
|
let s: String = ch.escape_default().map(Into::<char>::into).collect();
|
|
|
|
|
Symbol::intern(&s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Escapes a byte string.
|
|
|
|
|
pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
|
|
|
|
|
let s = bytes.escape_ascii().to_string();
|
|
|
|
|
Symbol::intern(&s)
|
|
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
|
2022-10-10 13:40:56 +11:00
|
|
|
#[derive(Debug)]
|
2019-10-15 22:48:13 +02:00
|
|
|
pub enum LitError {
|
2019-05-18 17:36:30 +03:00
|
|
|
LexerError,
|
|
|
|
|
InvalidSuffix,
|
|
|
|
|
InvalidIntSuffix,
|
|
|
|
|
InvalidFloatSuffix,
|
2019-05-19 19:56:45 +03:00
|
|
|
NonDecimalFloat(u32),
|
2023-01-02 05:07:02 +00:00
|
|
|
IntTooLarge(u32),
|
2019-05-18 17:36:30 +03:00
|
|
|
}
|
|
|
|
|
|
2019-05-11 02:31:34 +03:00
|
|
|
impl LitKind {
|
2019-05-19 19:56:45 +03:00
|
|
|
/// Converts literal token into a semantic literal.
|
2022-08-01 16:46:08 +10:00
|
|
|
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
|
2019-05-19 19:56:45 +03:00
|
|
|
let token::Lit { kind, symbol, suffix } = lit;
|
2019-05-19 01:04:26 +03:00
|
|
|
if suffix.is_some() && !kind.may_have_suffix() {
|
2019-05-18 17:36:30 +03:00
|
|
|
return Err(LitError::InvalidSuffix);
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
|
2019-05-19 01:04:26 +03:00
|
|
|
Ok(match kind {
|
|
|
|
|
token::Bool => {
|
2019-08-27 10:21:41 +02:00
|
|
|
assert!(symbol.is_bool_lit());
|
2019-05-19 01:04:26 +03:00
|
|
|
LitKind::Bool(symbol == kw::True)
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2019-05-19 19:56:45 +03:00
|
|
|
token::Byte => {
|
2021-12-15 14:39:23 +11:00
|
|
|
return unescape_byte(symbol.as_str())
|
2019-05-19 19:56:45 +03:00
|
|
|
.map(LitKind::Byte)
|
|
|
|
|
.map_err(|_| LitError::LexerError);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-19 19:56:45 +03:00
|
|
|
token::Char => {
|
2021-12-15 14:39:23 +11:00
|
|
|
return unescape_char(symbol.as_str())
|
2019-05-19 19:56:45 +03:00
|
|
|
.map(LitKind::Char)
|
|
|
|
|
.map_err(|_| LitError::LexerError);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
|
|
|
|
|
// There are some valid suffixes for integer and float literals,
|
|
|
|
|
// so all the handling is done internally.
|
2019-05-19 01:04:26 +03:00
|
|
|
token::Integer => return integer_lit(symbol, suffix),
|
|
|
|
|
token::Float => return float_lit(symbol, suffix),
|
2019-05-11 02:31:34 +03:00
|
|
|
|
2019-05-19 01:04:26 +03:00
|
|
|
token::Str => {
|
2019-05-11 02:31:34 +03:00
|
|
|
// If there are no characters requiring special treatment we can
|
2019-05-18 22:45:24 +03:00
|
|
|
// reuse the symbol from the token. Otherwise, we must generate a
|
2019-05-11 02:31:34 +03:00
|
|
|
// new symbol because the string in the LitKind is different to the
|
2019-05-18 22:45:24 +03:00
|
|
|
// string in the token.
|
2019-05-19 19:56:45 +03:00
|
|
|
let s = symbol.as_str();
|
2022-11-29 11:01:17 +00:00
|
|
|
let symbol = if s.contains(['\\', '\r']) {
|
2022-02-24 16:49:37 +11:00
|
|
|
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(
|
2022-11-29 11:01:17 +00:00
|
|
|
s,
|
2022-02-24 16:49:37 +11:00
|
|
|
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 16:09:33 +02:00
|
|
|
}
|
2020-05-13 10:03:49 +02:00
|
|
|
}
|
2022-02-24 16:49:37 +11:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
error?;
|
|
|
|
|
Symbol::intern(&buf)
|
|
|
|
|
} else {
|
|
|
|
|
symbol
|
|
|
|
|
};
|
2019-05-19 19:56:45 +03:00
|
|
|
LitKind::Str(symbol, ast::StrStyle::Cooked)
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2019-05-19 01:04:26 +03:00
|
|
|
token::StrRaw(n) => {
|
2019-05-11 02:31:34 +03:00
|
|
|
// Ditto.
|
2019-05-19 19:56:45 +03:00
|
|
|
let s = symbol.as_str();
|
2020-05-13 10:03:49 +02:00
|
|
|
let symbol =
|
|
|
|
|
if s.contains('\r') {
|
|
|
|
|
let mut buf = String::with_capacity(s.len());
|
|
|
|
|
let mut error = Ok(());
|
2022-11-29 11:01:17 +00:00
|
|
|
unescape_literal(s, Mode::RawStr, &mut |_, unescaped_char| {
|
2020-05-13 10:03:49 +02:00
|
|
|
match unescaped_char {
|
|
|
|
|
Ok(c) => buf.push(c),
|
2021-07-30 16:09:33 +02:00
|
|
|
Err(err) => {
|
|
|
|
|
if err.is_fatal() {
|
|
|
|
|
error = Err(LitError::LexerError);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-13 10:03:49 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
error?;
|
|
|
|
|
Symbol::intern(&buf)
|
|
|
|
|
} else {
|
|
|
|
|
symbol
|
|
|
|
|
};
|
2019-05-19 19:56:45 +03:00
|
|
|
LitKind::Str(symbol, ast::StrStyle::Raw(n))
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2019-05-19 01:04:26 +03:00
|
|
|
token::ByteStr => {
|
2019-05-19 19:56:45 +03:00
|
|
|
let s = symbol.as_str();
|
2019-05-11 02:31:34 +03:00
|
|
|
let mut buf = Vec::with_capacity(s.len());
|
2019-05-19 19:56:45 +03:00
|
|
|
let mut error = Ok(());
|
2022-11-29 11:01:17 +00:00
|
|
|
unescape_literal(s, Mode::ByteStr, &mut |_, c| match c {
|
2022-11-04 11:09:23 +11:00
|
|
|
Ok(c) => buf.push(byte_from_char(c)),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if err.is_fatal() {
|
|
|
|
|
error = Err(LitError::LexerError);
|
2021-07-30 16:09:33 +02:00
|
|
|
}
|
2020-05-13 10:03:49 +02:00
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
});
|
2019-05-19 19:56:45 +03:00
|
|
|
error?;
|
2022-11-29 13:35:44 +11:00
|
|
|
LitKind::ByteStr(buf.into(), StrStyle::Cooked)
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2022-11-29 13:35:44 +11:00
|
|
|
token::ByteStrRaw(n) => {
|
2019-06-09 14:43:31 +02:00
|
|
|
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-29 11:01:17 +00:00
|
|
|
unescape_literal(s, Mode::RawByteStr, &mut |_, c| match c {
|
2022-11-04 11:09:23 +11:00
|
|
|
Ok(c) => buf.push(byte_from_char(c)),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if err.is_fatal() {
|
|
|
|
|
error = Err(LitError::LexerError);
|
2021-07-30 16:09:33 +02:00
|
|
|
}
|
2020-05-13 10:03:49 +02:00
|
|
|
}
|
2019-06-09 14:43:31 +02:00
|
|
|
});
|
|
|
|
|
error?;
|
|
|
|
|
buf
|
|
|
|
|
} else {
|
|
|
|
|
symbol.to_string().into_bytes()
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-29 13:35:44 +11:00
|
|
|
LitKind::ByteStr(bytes.into(), StrStyle::Raw(n))
|
2019-06-09 14:43:31 +02:00
|
|
|
}
|
2023-03-05 15:03:22 +00:00
|
|
|
token::CStr => {
|
|
|
|
|
let s = symbol.as_str();
|
|
|
|
|
let mut buf = Vec::with_capacity(s.len());
|
|
|
|
|
let mut error = Ok(());
|
|
|
|
|
unescape_c_string(s, Mode::CStr, &mut |span, c| match c {
|
|
|
|
|
Ok(CStrUnit::Byte(0) | CStrUnit::Char('\0')) => {
|
|
|
|
|
error = Err(LitError::NulInCStr(span));
|
|
|
|
|
}
|
|
|
|
|
Ok(CStrUnit::Byte(b)) => buf.push(b),
|
|
|
|
|
Ok(CStrUnit::Char(c)) if c.len_utf8() == 1 => buf.push(c as u8),
|
|
|
|
|
Ok(CStrUnit::Char(c)) => {
|
|
|
|
|
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if err.is_fatal() {
|
|
|
|
|
error = Err(LitError::LexerError);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
error?;
|
2023-03-06 07:10:23 +00:00
|
|
|
buf.push(0);
|
2023-03-05 15:03:22 +00:00
|
|
|
LitKind::CStr(buf.into(), StrStyle::Cooked)
|
|
|
|
|
}
|
|
|
|
|
token::CStrRaw(n) => {
|
|
|
|
|
let s = symbol.as_str();
|
|
|
|
|
let mut buf = Vec::with_capacity(s.len());
|
|
|
|
|
let mut error = Ok(());
|
|
|
|
|
unescape_c_string(s, Mode::RawCStr, &mut |span, c| match c {
|
|
|
|
|
Ok(CStrUnit::Byte(0) | CStrUnit::Char('\0')) => {
|
|
|
|
|
error = Err(LitError::NulInCStr(span));
|
|
|
|
|
}
|
|
|
|
|
Ok(CStrUnit::Byte(b)) => buf.push(b),
|
|
|
|
|
Ok(CStrUnit::Char(c)) if c.len_utf8() == 1 => buf.push(c as u8),
|
|
|
|
|
Ok(CStrUnit::Char(c)) => {
|
|
|
|
|
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if err.is_fatal() {
|
|
|
|
|
error = Err(LitError::LexerError);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
error?;
|
2023-03-06 07:10:23 +00:00
|
|
|
buf.push(0);
|
2023-03-05 15:03:22 +00:00
|
|
|
LitKind::CStr(buf.into(), StrStyle::Raw(n))
|
|
|
|
|
}
|
2022-08-22 13:27:52 +10:00
|
|
|
token::Err => LitKind::Err,
|
2019-05-11 02:31:34 +03:00
|
|
|
})
|
|
|
|
|
}
|
2022-12-05 16:09:45 +11:00
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
|
2022-12-05 16:09:45 +11:00
|
|
|
impl fmt::Display for LitKind {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {
|
|
|
|
|
LitKind::Byte(b) => {
|
|
|
|
|
let b: String = ascii::escape_default(b).map(Into::<char>::into).collect();
|
2022-12-19 10:31:55 +01:00
|
|
|
write!(f, "b'{b}'")?;
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2022-12-05 16:09:45 +11:00
|
|
|
LitKind::Char(ch) => write!(f, "'{}'", escape_char_symbol(ch))?,
|
|
|
|
|
LitKind::Str(sym, StrStyle::Cooked) => write!(f, "\"{}\"", escape_string_symbol(sym))?,
|
|
|
|
|
LitKind::Str(sym, StrStyle::Raw(n)) => write!(
|
|
|
|
|
f,
|
|
|
|
|
"r{delim}\"{string}\"{delim}",
|
|
|
|
|
delim = "#".repeat(n as usize),
|
|
|
|
|
string = sym
|
|
|
|
|
)?,
|
|
|
|
|
LitKind::ByteStr(ref bytes, StrStyle::Cooked) => {
|
|
|
|
|
write!(f, "b\"{}\"", escape_byte_str_symbol(bytes))?
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2022-12-05 16:09:45 +11:00
|
|
|
LitKind::ByteStr(ref bytes, StrStyle::Raw(n)) => {
|
2022-11-29 13:35:44 +11:00
|
|
|
// Unwrap because raw byte string literals can only contain ASCII.
|
2022-12-05 16:09:45 +11:00
|
|
|
let symbol = str::from_utf8(bytes).unwrap();
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"br{delim}\"{string}\"{delim}",
|
|
|
|
|
delim = "#".repeat(n as usize),
|
|
|
|
|
string = symbol
|
|
|
|
|
)?;
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2023-03-06 07:42:04 +00:00
|
|
|
LitKind::CStr(ref bytes, StrStyle::Cooked) => {
|
|
|
|
|
write!(f, "c\"{}\"", escape_byte_str_symbol(bytes))?
|
|
|
|
|
}
|
|
|
|
|
LitKind::CStr(ref bytes, StrStyle::Raw(n)) => {
|
|
|
|
|
// This can only be valid UTF-8.
|
|
|
|
|
let symbol = str::from_utf8(bytes).unwrap();
|
|
|
|
|
write!(f, "cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize),)?;
|
|
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
LitKind::Int(n, ty) => {
|
2022-12-19 10:31:55 +01:00
|
|
|
write!(f, "{n}")?;
|
2022-12-05 16:09:45 +11:00
|
|
|
match ty {
|
|
|
|
|
ast::LitIntType::Unsigned(ty) => write!(f, "{}", ty.name())?,
|
|
|
|
|
ast::LitIntType::Signed(ty) => write!(f, "{}", ty.name())?,
|
|
|
|
|
ast::LitIntType::Unsuffixed => {}
|
|
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
LitKind::Float(symbol, ty) => {
|
2022-12-19 10:31:55 +01:00
|
|
|
write!(f, "{symbol}")?;
|
2022-12-05 16:09:45 +11:00
|
|
|
match ty {
|
|
|
|
|
ast::LitFloatType::Suffixed(ty) => write!(f, "{}", ty.name())?,
|
|
|
|
|
ast::LitFloatType::Unsuffixed => {}
|
|
|
|
|
}
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2022-12-05 16:09:45 +11:00
|
|
|
LitKind::Bool(b) => write!(f, "{}", if b { "true" } else { "false" })?,
|
|
|
|
|
LitKind::Err => {
|
|
|
|
|
// This only shows up in places like `-Zunpretty=hir` output, so we
|
|
|
|
|
// don't bother to produce something useful.
|
|
|
|
|
write!(f, "<bad-literal>")?;
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
2022-12-05 16:09:45 +11:00
|
|
|
}
|
2019-05-19 01:04:26 +03:00
|
|
|
|
2022-12-05 16:09:45 +11:00
|
|
|
Ok(())
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 15:39:42 +11:00
|
|
|
impl MetaItemLit {
|
2022-11-29 13:36:00 +11:00
|
|
|
/// Converts a token literal into a meta item literal.
|
2022-11-23 15:39:42 +11:00
|
|
|
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<MetaItemLit, LitError> {
|
2022-11-29 13:36:00 +11:00
|
|
|
Ok(MetaItemLit {
|
|
|
|
|
symbol: token_lit.symbol,
|
|
|
|
|
suffix: token_lit.suffix,
|
|
|
|
|
kind: LitKind::from_token_lit(token_lit)?,
|
|
|
|
|
span,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Cheaply converts a meta item literal into a token literal.
|
|
|
|
|
pub fn as_token_lit(&self) -> token::Lit {
|
|
|
|
|
let kind = match self.kind {
|
|
|
|
|
LitKind::Bool(_) => token::Bool,
|
|
|
|
|
LitKind::Str(_, ast::StrStyle::Cooked) => token::Str,
|
|
|
|
|
LitKind::Str(_, ast::StrStyle::Raw(n)) => token::StrRaw(n),
|
|
|
|
|
LitKind::ByteStr(_, ast::StrStyle::Cooked) => token::ByteStr,
|
|
|
|
|
LitKind::ByteStr(_, ast::StrStyle::Raw(n)) => token::ByteStrRaw(n),
|
2023-03-05 15:03:22 +00:00
|
|
|
LitKind::CStr(_, ast::StrStyle::Cooked) => token::CStr,
|
|
|
|
|
LitKind::CStr(_, ast::StrStyle::Raw(n)) => token::CStrRaw(n),
|
2022-11-29 13:36:00 +11:00
|
|
|
LitKind::Byte(_) => token::Byte,
|
|
|
|
|
LitKind::Char(_) => token::Char,
|
|
|
|
|
LitKind::Int(..) => token::Integer,
|
|
|
|
|
LitKind::Float(..) => token::Float,
|
|
|
|
|
LitKind::Err => token::Err,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
token::Lit::new(kind, self.symbol, self.suffix)
|
2019-05-18 17:36:30 +03:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 15:39:42 +11:00
|
|
|
/// Converts an arbitrary token into meta item literal.
|
|
|
|
|
pub fn from_token(token: &Token) -> Option<MetaItemLit> {
|
2022-10-10 13:40:56 +11:00
|
|
|
token::Lit::from_token(token)
|
2022-11-23 15:39:42 +11:00
|
|
|
.and_then(|token_lit| MetaItemLit::from_token_lit(token_lit, token.span).ok())
|
2019-05-11 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 19:56:45 +03: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-11 02:31:34 +03:00
|
|
|
|
2019-05-19 19:56:45 +03: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 03:46:22 +01:00
|
|
|
Some(suf) => LitKind::Float(
|
|
|
|
|
symbol,
|
|
|
|
|
ast::LitFloatType::Suffixed(match suf {
|
|
|
|
|
sym::f32 => ast::FloatTy::F32,
|
|
|
|
|
sym::f64 => ast::FloatTy::F64,
|
2019-05-19 19:56:45 +03:00
|
|
|
_ => return Err(LitError::InvalidFloatSuffix),
|
2019-10-28 03:46:22 +01:00
|
|
|
}),
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
2019-10-28 03:46:22 +01:00
|
|
|
None => LitKind::Float(symbol, ast::LitFloatType::Unsuffixed),
|
2019-05-11 02:31:34 +03:00
|
|
|
})
|
|
|
|
|
}
|
2019-05-18 17:36:30 +03:00
|
|
|
|
2019-05-19 19:56:45 +03: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-11 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
|
2019-05-19 19:56:45 +03: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-11 02:31:34 +03:00
|
|
|
|
2019-10-08 10:59:05 +02: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-11 02:31:34 +03:00
|
|
|
|
2019-05-19 19:56:45 +03: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 17:36:30 +03:00
|
|
|
_ => return Err(LitError::InvalidIntSuffix),
|
2019-05-11 02:31:34 +03:00
|
|
|
},
|
2019-05-19 19:56:45 +03:00
|
|
|
_ => ast::LitIntType::Unsuffixed,
|
|
|
|
|
};
|
2019-05-11 02:31:34 +03:00
|
|
|
|
2019-05-19 19:56:45 +03: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));
|
2023-01-02 05:07:02 +00:00
|
|
|
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge(base) }
|
2019-05-11 02:31:34 +03:00
|
|
|
})
|
|
|
|
|
}
|