Turn ast::Lit
into a struct
This commit is contained in:
parent
b8e0d0a2aa
commit
28b125b83d
5 changed files with 24 additions and 17 deletions
|
@ -162,7 +162,11 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
|
||||||
Unsuffixed
|
Unsuffixed
|
||||||
});
|
});
|
||||||
|
|
||||||
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
|
impl_stable_hash_for!(struct ::syntax::ast::Lit {
|
||||||
|
node,
|
||||||
|
span
|
||||||
|
});
|
||||||
|
|
||||||
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
|
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
|
||||||
Str(value, style),
|
Str(value, style),
|
||||||
Err(value),
|
Err(value),
|
||||||
|
|
|
@ -1351,7 +1351,11 @@ pub enum StrStyle {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A literal.
|
/// A literal.
|
||||||
pub type Lit = Spanned<LitKind>;
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)]
|
||||||
|
pub struct Lit {
|
||||||
|
pub node: LitKind,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq)]
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq)]
|
||||||
pub enum LitIntType {
|
pub enum LitIntType {
|
||||||
|
|
|
@ -16,7 +16,7 @@ use crate::ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
|
||||||
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
|
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
|
||||||
use crate::ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
|
use crate::ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
|
||||||
use crate::mut_visit::visit_clobber;
|
use crate::mut_visit::visit_clobber;
|
||||||
use crate::source_map::{BytePos, Spanned, respan, dummy_spanned};
|
use crate::source_map::{BytePos, Spanned, dummy_spanned};
|
||||||
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
||||||
use crate::parse::parser::Parser;
|
use crate::parse::parser::Parser;
|
||||||
use crate::parse::{self, ParseSess, PResult};
|
use crate::parse::{self, ParseSess, PResult};
|
||||||
|
@ -350,11 +350,11 @@ impl Attribute {
|
||||||
/* Constructors */
|
/* Constructors */
|
||||||
|
|
||||||
pub fn mk_name_value_item_str(ident: Ident, value: Spanned<Symbol>) -> MetaItem {
|
pub fn mk_name_value_item_str(ident: Ident, value: Spanned<Symbol>) -> MetaItem {
|
||||||
let value = respan(value.span, LitKind::Str(value.node, ast::StrStyle::Cooked));
|
let value = Lit { node: LitKind::Str(value.node, ast::StrStyle::Cooked), span: value.span };
|
||||||
mk_name_value_item(ident.span.to(value.span), ident, value)
|
mk_name_value_item(ident.span.to(value.span), ident, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_name_value_item(span: Span, ident: Ident, value: ast::Lit) -> MetaItem {
|
pub fn mk_name_value_item(span: Span, ident: Ident, value: Lit) -> MetaItem {
|
||||||
MetaItem { path: Path::from_ident(ident), span, node: MetaItemKind::NameValue(value) }
|
MetaItem { path: Path::from_ident(ident), span, node: MetaItemKind::NameValue(value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -417,7 +417,7 @@ pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute
|
||||||
|
|
||||||
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
|
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
|
||||||
let style = doc_comment_style(&text.as_str());
|
let style = doc_comment_style(&text.as_str());
|
||||||
let lit = respan(span, LitKind::Str(text, ast::StrStyle::Cooked));
|
let lit = Lit { node: LitKind::Str(text, ast::StrStyle::Cooked), span };
|
||||||
Attribute {
|
Attribute {
|
||||||
id,
|
id,
|
||||||
style,
|
style,
|
||||||
|
@ -562,7 +562,7 @@ impl MetaItemKind {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
|
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
|
||||||
LitKind::from_token(token)
|
LitKind::from_token(token)
|
||||||
.map(|lit| MetaItemKind::NameValue(Spanned { node: lit, span: span }))
|
.map(|node| MetaItemKind::NameValue(Lit { node, span }))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -609,7 +609,7 @@ impl NestedMetaItem {
|
||||||
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
|
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
|
||||||
if let Some(node) = LitKind::from_token(token) {
|
if let Some(node) = LitKind::from_token(token) {
|
||||||
tokens.next();
|
tokens.next();
|
||||||
return Some(NestedMetaItem::Literal(respan(span, node)));
|
return Some(NestedMetaItem::Literal(Lit { node, span }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -697,8 +697,8 @@ impl<'a> AstBuilder for 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, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
|
fn expr_lit(&self, span: Span, node: ast::LitKind) -> P<ast::Expr> {
|
||||||
self.expr(sp, ast::ExprKind::Lit(respan(sp, lit)))
|
self.expr(span, ast::ExprKind::Lit(ast::Lit { node, span }))
|
||||||
}
|
}
|
||||||
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
|
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
|
||||||
self.expr_lit(span, ast::LitKind::Int(i as u128,
|
self.expr_lit(span, ast::LitKind::Int(i as u128,
|
||||||
|
@ -1164,10 +1164,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||||
attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
|
attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
|
fn meta_name_value(&self, span: Span, name: ast::Name, node: ast::LitKind)
|
||||||
-> ast::MetaItem {
|
-> ast::MetaItem {
|
||||||
attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp),
|
attr::mk_name_value_item(span, Ident::with_empty_ctxt(name).with_span_pos(span),
|
||||||
respan(sp, value))
|
ast::Lit { node, span })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn item_use(&self, sp: Span,
|
fn item_use(&self, sp: Span,
|
||||||
|
|
|
@ -2140,15 +2140,14 @@ impl<'a> Parser<'a> {
|
||||||
/// Matches `lit = true | false | token_lit`.
|
/// Matches `lit = true | false | token_lit`.
|
||||||
crate fn parse_lit(&mut self) -> PResult<'a, Lit> {
|
crate fn parse_lit(&mut self) -> PResult<'a, Lit> {
|
||||||
let lo = self.span;
|
let lo = self.span;
|
||||||
let lit = if self.eat_keyword(keywords::True) {
|
let node = if self.eat_keyword(keywords::True) {
|
||||||
LitKind::Bool(true)
|
LitKind::Bool(true)
|
||||||
} else if self.eat_keyword(keywords::False) {
|
} else if self.eat_keyword(keywords::False) {
|
||||||
LitKind::Bool(false)
|
LitKind::Bool(false)
|
||||||
} else {
|
} else {
|
||||||
let lit = self.parse_lit_token()?;
|
self.parse_lit_token()?
|
||||||
lit
|
|
||||||
};
|
};
|
||||||
Ok(source_map::Spanned { node: lit, span: lo.to(self.prev_span) })
|
Ok(Lit { node, span: lo.to(self.prev_span) })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
|
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue