Skip to content

Commit 3f064ca

Browse files
committed
Move literal parsing code into a separate file
Remove some dead code
1 parent 8739668 commit 3f064ca

13 files changed

+521
-537
lines changed

src/libsyntax/attr/mod.rs

-99
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ use crate::ThinVec;
2727
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
2828
use crate::GLOBALS;
2929

30-
use errors::Handler;
3130
use log::debug;
3231
use syntax_pos::{FileName, Span};
3332

34-
use std::ascii;
3533
use std::iter;
3634
use std::ops::DerefMut;
3735

@@ -620,103 +618,6 @@ impl NestedMetaItem {
620618
}
621619
}
622620

623-
impl Lit {
624-
crate fn tokens(&self) -> TokenStream {
625-
let token = match self.token {
626-
token::Bool(symbol) => Token::Ident(Ident::with_empty_ctxt(symbol), false),
627-
token => Token::Literal(token, self.suffix),
628-
};
629-
TokenTree::Token(self.span, token).into()
630-
}
631-
}
632-
633-
impl LitKind {
634-
/// Attempts to recover a token from semantic literal.
635-
/// This function is used when the original token doesn't exist (e.g. the literal is created
636-
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
637-
pub fn to_lit_token(&self) -> (token::Lit, Option<Symbol>) {
638-
match *self {
639-
LitKind::Str(string, ast::StrStyle::Cooked) => {
640-
let escaped = string.as_str().escape_default().to_string();
641-
(token::Lit::Str_(Symbol::intern(&escaped)), None)
642-
}
643-
LitKind::Str(string, ast::StrStyle::Raw(n)) => {
644-
(token::Lit::StrRaw(string, n), None)
645-
}
646-
LitKind::ByteStr(ref bytes) => {
647-
let string = bytes.iter().cloned().flat_map(ascii::escape_default)
648-
.map(Into::<char>::into).collect::<String>();
649-
(token::Lit::ByteStr(Symbol::intern(&string)), None)
650-
}
651-
LitKind::Byte(byte) => {
652-
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
653-
(token::Lit::Byte(Symbol::intern(&string)), None)
654-
}
655-
LitKind::Char(ch) => {
656-
let string: String = ch.escape_default().map(Into::<char>::into).collect();
657-
(token::Lit::Char(Symbol::intern(&string)), None)
658-
}
659-
LitKind::Int(n, ty) => {
660-
let suffix = match ty {
661-
ast::LitIntType::Unsigned(ty) => Some(Symbol::intern(ty.ty_to_string())),
662-
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
663-
ast::LitIntType::Unsuffixed => None,
664-
};
665-
(token::Lit::Integer(Symbol::intern(&n.to_string())), suffix)
666-
}
667-
LitKind::Float(symbol, ty) => {
668-
(token::Lit::Float(symbol), Some(Symbol::intern(ty.ty_to_string())))
669-
}
670-
LitKind::FloatUnsuffixed(symbol) => (token::Lit::Float(symbol), None),
671-
LitKind::Bool(value) => {
672-
let kw = if value { keywords::True } else { keywords::False };
673-
(token::Lit::Bool(kw.name()), None)
674-
}
675-
LitKind::Err(val) => (token::Lit::Err(val), None),
676-
}
677-
}
678-
}
679-
680-
impl Lit {
681-
/// Converts literal token with a suffix into an AST literal.
682-
/// Works speculatively and may return `None` is diagnostic handler is not passed.
683-
/// If diagnostic handler is passed, may return `Some`,
684-
/// possibly after reporting non-fatal errors and recovery, or `None` for irrecoverable errors.
685-
crate fn from_token(
686-
token: &token::Token,
687-
span: Span,
688-
diag: Option<(Span, &Handler)>,
689-
) -> Option<Lit> {
690-
let (token, suffix) = match *token {
691-
token::Ident(ident, false) if ident.name == keywords::True.name() ||
692-
ident.name == keywords::False.name() =>
693-
(token::Bool(ident.name), None),
694-
token::Literal(token, suffix) =>
695-
(token, suffix),
696-
token::Interpolated(ref nt) => {
697-
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt {
698-
if let ast::ExprKind::Lit(lit) = &expr.node {
699-
return Some(lit.clone());
700-
}
701-
}
702-
return None;
703-
}
704-
_ => return None,
705-
};
706-
707-
let node = LitKind::from_lit_token(token, suffix, diag)?;
708-
Some(Lit { node, token, suffix, span })
709-
}
710-
711-
/// Attempts to recover an AST literal from semantic literal.
712-
/// This function is used when the original token doesn't exist (e.g. the literal is created
713-
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
714-
pub fn from_lit_kind(node: LitKind, span: Span) -> Lit {
715-
let (token, suffix) = node.to_lit_token();
716-
Lit { node, token, suffix, span }
717-
}
718-
}
719-
720621
pub trait HasAttrs: Sized {
721622
fn attrs(&self) -> &[ast::Attribute];
722623
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F);

src/libsyntax/parse/classify.rs

-13
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,3 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
2525
_ => true,
2626
}
2727
}
28-
29-
/// this statement requires a semicolon after it.
30-
/// note that in one case (`stmt_semi`), we've already
31-
/// seen the semicolon, and thus don't need another.
32-
pub fn stmt_ends_with_semi(stmt: &ast::StmtKind) -> bool {
33-
match *stmt {
34-
ast::StmtKind::Local(_) => true,
35-
ast::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(e),
36-
ast::StmtKind::Item(_) |
37-
ast::StmtKind::Semi(..) |
38-
ast::StmtKind::Mac(..) => false,
39-
}
40-
}

src/libsyntax/parse/lexer/mod.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,6 @@ impl<'a> StringReader<'a> {
262262
}
263263
}
264264

265-
pub fn new(sess: &'a ParseSess,
266-
source_file: Lrc<syntax_pos::SourceFile>,
267-
override_span: Option<Span>) -> Self {
268-
let mut sr = StringReader::new_raw(sess, source_file, override_span);
269-
if sr.advance_token().is_err() {
270-
sr.emit_fatal_errors();
271-
FatalError.raise();
272-
}
273-
274-
sr
275-
}
276-
277265
pub fn new_or_buffered_errs(sess: &'a ParseSess,
278266
source_file: Lrc<syntax_pos::SourceFile>,
279267
override_span: Option<Span>) -> Result<Self, Vec<Diagnostic>> {
@@ -1627,7 +1615,12 @@ mod tests {
16271615
teststr: String)
16281616
-> StringReader<'a> {
16291617
let sf = sm.new_source_file(PathBuf::from(teststr.clone()).into(), teststr);
1630-
StringReader::new(sess, sf, None)
1618+
let mut sr = StringReader::new_raw(sess, sf, None);
1619+
if sr.advance_token().is_err() {
1620+
sr.emit_fatal_errors();
1621+
FatalError.raise();
1622+
}
1623+
sr
16311624
}
16321625

16331626
#[test]

0 commit comments

Comments
 (0)