Remove ExtCtxt::expr_lit
.
This commit is contained in:
parent
7d30472180
commit
4ae956f600
@ -8,6 +8,26 @@ use rustc_span::Span;
|
|||||||
use std::ascii;
|
use std::ascii;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LitError {
|
pub enum LitError {
|
||||||
LexerError,
|
LexerError,
|
||||||
@ -149,16 +169,11 @@ impl LitKind {
|
|||||||
pub fn synthesize_token_lit(&self) -> token::Lit {
|
pub fn synthesize_token_lit(&self) -> token::Lit {
|
||||||
let (kind, symbol, suffix) = match *self {
|
let (kind, symbol, suffix) = match *self {
|
||||||
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
|
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
|
||||||
// Don't re-intern unless the escaped string is different.
|
(token::Str, escape_string_symbol(symbol), None)
|
||||||
let s = symbol.as_str();
|
|
||||||
let escaped = s.escape_default().to_string();
|
|
||||||
let symbol = if s == escaped { symbol } else { Symbol::intern(&escaped) };
|
|
||||||
(token::Str, symbol, None)
|
|
||||||
}
|
}
|
||||||
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
|
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
|
||||||
LitKind::ByteStr(ref bytes, ast::StrStyle::Cooked) => {
|
LitKind::ByteStr(ref bytes, ast::StrStyle::Cooked) => {
|
||||||
let string = bytes.escape_ascii().to_string();
|
(token::ByteStr, escape_byte_str_symbol(bytes), None)
|
||||||
(token::ByteStr, Symbol::intern(&string), None)
|
|
||||||
}
|
}
|
||||||
LitKind::ByteStr(ref bytes, ast::StrStyle::Raw(n)) => {
|
LitKind::ByteStr(ref bytes, ast::StrStyle::Raw(n)) => {
|
||||||
// Unwrap because raw byte string literals can only contain ASCII.
|
// Unwrap because raw byte string literals can only contain ASCII.
|
||||||
@ -169,10 +184,7 @@ impl LitKind {
|
|||||||
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
|
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
|
||||||
(token::Byte, Symbol::intern(&string), None)
|
(token::Byte, Symbol::intern(&string), None)
|
||||||
}
|
}
|
||||||
LitKind::Char(ch) => {
|
LitKind::Char(ch) => (token::Char, escape_char_symbol(ch), None),
|
||||||
let string: String = ch.escape_default().map(Into::<char>::into).collect();
|
|
||||||
(token::Char, Symbol::intern(&string), None)
|
|
||||||
}
|
|
||||||
LitKind::Int(n, ty) => {
|
LitKind::Int(n, ty) => {
|
||||||
let suffix = match ty {
|
let suffix = match ty {
|
||||||
ast::LitIntType::Unsigned(ty) => Some(ty.name()),
|
ast::LitIntType::Unsigned(ty) => Some(ty.name()),
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use crate::base::ExtCtxt;
|
use crate::base::ExtCtxt;
|
||||||
use rustc_ast::attr;
|
|
||||||
use rustc_ast::ptr::P;
|
use rustc_ast::ptr::P;
|
||||||
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
|
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_ast::{attr, token, util::literal};
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
@ -332,36 +331,36 @@ impl<'a> ExtCtxt<'a> {
|
|||||||
self.expr_struct(span, self.path_ident(span, id), fields)
|
self.expr_struct(span, self.path_ident(span, id), fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
|
pub fn expr_usize(&self, span: Span, n: usize) -> P<ast::Expr> {
|
||||||
let token_lit = lit_kind.synthesize_token_lit();
|
let suffix = Some(ast::UintTy::Usize.name());
|
||||||
self.expr(span, ast::ExprKind::Lit(token_lit))
|
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
|
||||||
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
|
pub fn expr_u32(&self, span: Span, n: u32) -> P<ast::Expr> {
|
||||||
self.expr_lit(
|
let suffix = Some(ast::UintTy::U32.name());
|
||||||
span,
|
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
|
||||||
ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
|
pub fn expr_bool(&self, span: Span, value: bool) -> P<ast::Expr> {
|
||||||
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
|
let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None);
|
||||||
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
|
pub fn expr_str(&self, span: Span, s: Symbol) -> P<ast::Expr> {
|
||||||
self.expr_lit(sp, ast::LitKind::Bool(value))
|
let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None);
|
||||||
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
|
pub fn expr_char(&self, span: Span, ch: char) -> P<ast::Expr> {
|
||||||
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
|
let lit = token::Lit::new(token::Char, literal::escape_char_symbol(ch), None);
|
||||||
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_char(&self, sp: Span, ch: char) -> P<ast::Expr> {
|
pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> {
|
||||||
self.expr_lit(sp, ast::LitKind::Char(ch))
|
let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
|
||||||
}
|
self.expr(span, ast::ExprKind::Lit(lit))
|
||||||
|
|
||||||
pub fn expr_byte_str(&self, sp: Span, bytes: Vec<u8>) -> P<ast::Expr> {
|
|
||||||
self.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(bytes), ast::StrStyle::Cooked))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `[expr1, expr2, ...]`
|
/// `[expr1, expr2, ...]`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user