1
Fork 0

Some code cleanup and tidy/test fixes

This commit is contained in:
Vadim Petrochenkov 2019-06-05 14:17:56 +03:00
parent 738e14565d
commit ff40e37b98
17 changed files with 109 additions and 101 deletions

View file

@ -56,7 +56,7 @@ extern crate syntax_pos;
extern crate rustc; extern crate rustc;
extern crate rustc_plugin; extern crate rustc_plugin;
use syntax::parse::token; use syntax::parse::token::{self, Token};
use syntax::tokenstream::TokenTree; use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager}; use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder; // A trait for expr_usize. use syntax::ext::build::AstBuilder; // A trait for expr_usize.
@ -64,7 +64,7 @@ use syntax_pos::Span;
use rustc_plugin::Registry; use rustc_plugin::Registry;
fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
-> Box<MacResult + 'static> { -> Box<dyn MacResult + 'static> {
static NUMERALS: &'static [(&'static str, usize)] = &[ static NUMERALS: &'static [(&'static str, usize)] = &[
("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("M", 1000), ("CM", 900), ("D", 500), ("CD", 400),
@ -80,7 +80,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
} }
let text = match args[0] { let text = match args[0] {
TokenTree::Token(_, token::Ident(s)) => s.to_string(), TokenTree::Token(Token { kind: token::Ident(s, _), .. }) => s.to_string(),
_ => { _ => {
cx.span_err(sp, "argument should be a single identifier"); cx.span_err(sp, "argument should be a single identifier");
return DummyResult::any(sp); return DummyResult::any(sp);

View file

@ -1414,15 +1414,9 @@ impl KeywordIdents {
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: TokenStream) { fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: TokenStream) {
for tt in tokens.into_trees() { for tt in tokens.into_trees() {
match tt { match tt {
TokenTree::Token(token) => match token.ident() { // Only report non-raw idents.
// only report non-raw idents TokenTree::Token(token) => if let Some((ident, false)) = token.ident() {
Some((ident, false)) => { self.check_ident_token(cx, UnderMacro(true), ident);
self.check_ident_token(cx, UnderMacro(true), ast::Ident {
span: token.span.substitute_dummy(ident.span),
..ident
});
}
_ => {},
} }
TokenTree::Delimited(_, _, tts) => { TokenTree::Delimited(_, _, tts) => {
self.check_tokens(cx, tts) self.check_tokens(cx, tts)

View file

@ -483,7 +483,8 @@ impl MetaItem {
Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span })) | Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span })) |
Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: { Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: {
let mut segments = if let token::Ident(name, _) = kind { let mut segments = if let token::Ident(name, _) = kind {
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek() { if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }))
= tokens.peek() {
tokens.next(); tokens.next();
vec![PathSegment::from_ident(Ident::new(name, span))] vec![PathSegment::from_ident(Ident::new(name, span))]
} else { } else {
@ -493,12 +494,14 @@ impl MetaItem {
vec![PathSegment::path_root(span)] vec![PathSegment::path_root(span)]
}; };
loop { loop {
if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) = tokens.next() { if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }))
= tokens.next() {
segments.push(PathSegment::from_ident(Ident::new(name, span))); segments.push(PathSegment::from_ident(Ident::new(name, span)));
} else { } else {
return None; return None;
} }
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek() { if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }))
= tokens.peek() {
tokens.next(); tokens.next();
} else { } else {
break; break;

View file

@ -77,7 +77,9 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
}, },
(3, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })), (3, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })),
Some(&TokenTree::Token(Token { kind: token::Comma, .. })), Some(&TokenTree::Token(Token { kind: token::Comma, .. })),
Some(&TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), .. }))) => { Some(&TokenTree::Token(Token {
kind: token::Literal(token::Lit { symbol, .. }), ..
}))) => {
(code, Some(symbol)) (code, Some(symbol))
} }
_ => unreachable!() _ => unreachable!()

View file

@ -3,7 +3,7 @@
//! Since we cannot have a dependency on `librustc`, we implement some types here that are somewhat //! Since we cannot have a dependency on `librustc`, we implement some types here that are somewhat
//! redundant. Later, these types can be converted to types for use by the rest of the compiler. //! redundant. Later, these types can be converted to types for use by the rest of the compiler.
use crate::syntax::ast::NodeId; use crate::ast::NodeId;
use syntax_pos::MultiSpan; use syntax_pos::MultiSpan;
/// Since we cannot import `LintId`s from `rustc::lint`, we define some Ids here which can later be /// Since we cannot import `LintId`s from `rustc::lint`, we define some Ids here which can later be

View file

@ -24,12 +24,12 @@ pub struct Delimited {
impl Delimited { impl Delimited {
/// Returns the opening delimiter (possibly `NoDelim`). /// Returns the opening delimiter (possibly `NoDelim`).
pub fn open_token(&self) -> token::TokenKind { pub fn open_token(&self) -> TokenKind {
token::OpenDelim(self.delim) token::OpenDelim(self.delim)
} }
/// Returns the closing delimiter (possibly `NoDelim`). /// Returns the closing delimiter (possibly `NoDelim`).
pub fn close_token(&self) -> token::TokenKind { pub fn close_token(&self) -> TokenKind {
token::CloseDelim(self.delim) token::CloseDelim(self.delim)
} }
@ -59,7 +59,7 @@ pub struct SequenceRepetition {
/// The sequence of token trees /// The sequence of token trees
pub tts: Vec<TokenTree>, pub tts: Vec<TokenTree>,
/// The optional separator /// The optional separator
pub separator: Option<token::TokenKind>, pub separator: Option<TokenKind>,
/// Whether the sequence can be repeated zero (*), or one or more times (+) /// Whether the sequence can be repeated zero (*), or one or more times (+)
pub op: KleeneOp, pub op: KleeneOp,
/// The number of `Match`s that appear in the sequence (and subsequences) /// The number of `Match`s that appear in the sequence (and subsequences)
@ -210,20 +210,21 @@ pub fn parse(
match tree { match tree {
TokenTree::MetaVar(start_sp, ident) if expect_matchers => { TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
let span = match trees.next() { let span = match trees.next() {
Some(tokenstream::TokenTree::Token(Token { kind: token::Colon, span })) => match trees.next() { Some(tokenstream::TokenTree::Token(Token { kind: token::Colon, span })) =>
Some(tokenstream::TokenTree::Token(token)) => match token.ident() { match trees.next() {
Some((kind, _)) => { Some(tokenstream::TokenTree::Token(token)) => match token.ident() {
let span = token.span.with_lo(start_sp.lo()); Some((kind, _)) => {
result.push(TokenTree::MetaVarDecl(span, ident, kind)); let span = token.span.with_lo(start_sp.lo());
continue; result.push(TokenTree::MetaVarDecl(span, ident, kind));
} continue;
_ => token.span, }
_ => token.span,
},
tree => tree
.as_ref()
.map(tokenstream::TokenTree::span)
.unwrap_or(span),
}, },
tree => tree
.as_ref()
.map(tokenstream::TokenTree::span)
.unwrap_or(span),
},
tree => tree tree => tree
.as_ref() .as_ref()
.map(tokenstream::TokenTree::span) .map(tokenstream::TokenTree::span)
@ -370,7 +371,7 @@ where
/// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return /// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return
/// `None`. /// `None`.
fn kleene_op(token: &token::TokenKind) -> Option<KleeneOp> { fn kleene_op(token: &TokenKind) -> Option<KleeneOp> {
match *token { match *token {
token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore), token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore),
token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore), token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore),
@ -423,7 +424,7 @@ fn parse_sep_and_kleene_op<I>(
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
edition: Edition, edition: Edition,
macro_node_id: NodeId, macro_node_id: NodeId,
) -> (Option<token::TokenKind>, KleeneOp) ) -> (Option<TokenKind>, KleeneOp)
where where
I: Iterator<Item = tokenstream::TokenTree>, I: Iterator<Item = tokenstream::TokenTree>,
{ {
@ -448,7 +449,7 @@ fn parse_sep_and_kleene_op_2015<I>(
_features: &Features, _features: &Features,
_attrs: &[ast::Attribute], _attrs: &[ast::Attribute],
macro_node_id: NodeId, macro_node_id: NodeId,
) -> (Option<token::TokenKind>, KleeneOp) ) -> (Option<TokenKind>, KleeneOp)
where where
I: Iterator<Item = tokenstream::TokenTree>, I: Iterator<Item = tokenstream::TokenTree>,
{ {
@ -566,7 +567,7 @@ fn parse_sep_and_kleene_op_2018<I>(
sess: &ParseSess, sess: &ParseSess,
_features: &Features, _features: &Features,
_attrs: &[ast::Attribute], _attrs: &[ast::Attribute],
) -> (Option<token::TokenKind>, KleeneOp) ) -> (Option<TokenKind>, KleeneOp)
where where
I: Iterator<Item = tokenstream::TokenTree>, I: Iterator<Item = tokenstream::TokenTree>,
{ {

View file

@ -242,7 +242,7 @@ pub fn transcribe(
Ident::new(ident.name, ident.span.apply_mark(cx.current_expansion.mark)); Ident::new(ident.name, ident.span.apply_mark(cx.current_expansion.mark));
sp = sp.apply_mark(cx.current_expansion.mark); sp = sp.apply_mark(cx.current_expansion.mark);
result.push(TokenTree::token(token::Dollar, sp).into()); result.push(TokenTree::token(token::Dollar, sp).into());
result.push(TokenTree::token(token::TokenKind::from_ast_ident(ident), sp).into()); result.push(TokenTree::token(TokenKind::from_ast_ident(ident), sp).into());
} }
} }

View file

@ -137,12 +137,6 @@ pub mod util {
pub mod json; pub mod json;
pub mod syntax {
pub use crate::ext;
pub use crate::parse;
pub use crate::ast;
}
pub mod ast; pub mod ast;
pub mod attr; pub mod attr;
pub mod source_map; pub mod source_map;

View file

@ -2,8 +2,9 @@ use crate::ast::{
self, Arg, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Ident, Item, ItemKind, self, Arg, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Ident, Item, ItemKind,
Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind, VariantData, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind, VariantData,
}; };
use crate::parse::{SeqSep, token, PResult, Parser}; use crate::parse::{SeqSep, PResult, Parser};
use crate::parse::parser::{BlockMode, PathStyle, SemiColonMode, TokenType, TokenExpectType}; use crate::parse::parser::{BlockMode, PathStyle, SemiColonMode, TokenType, TokenExpectType};
use crate::parse::token::{self, TokenKind};
use crate::print::pprust; use crate::print::pprust;
use crate::ptr::P; use crate::ptr::P;
use crate::source_map::Spanned; use crate::source_map::Spanned;
@ -229,8 +230,8 @@ impl<'a> Parser<'a> {
pub fn expected_one_of_not_found( pub fn expected_one_of_not_found(
&mut self, &mut self,
edible: &[token::TokenKind], edible: &[TokenKind],
inedible: &[token::TokenKind], inedible: &[TokenKind],
) -> PResult<'a, bool /* recovered */> { ) -> PResult<'a, bool /* recovered */> {
fn tokens_to_string(tokens: &[TokenType]) -> String { fn tokens_to_string(tokens: &[TokenType]) -> String {
let mut i = tokens.iter(); let mut i = tokens.iter();
@ -368,7 +369,7 @@ impl<'a> Parser<'a> {
/// Eats and discards tokens until one of `kets` is encountered. Respects token trees, /// Eats and discards tokens until one of `kets` is encountered. Respects token trees,
/// passes through any errors encountered. Used for error recovery. /// passes through any errors encountered. Used for error recovery.
crate fn eat_to_tokens(&mut self, kets: &[&token::TokenKind]) { crate fn eat_to_tokens(&mut self, kets: &[&TokenKind]) {
let handler = self.diagnostic(); let handler = self.diagnostic();
if let Err(ref mut err) = self.parse_seq_to_before_tokens( if let Err(ref mut err) = self.parse_seq_to_before_tokens(
@ -388,7 +389,7 @@ impl<'a> Parser<'a> {
/// let _ = vec![1, 2, 3].into_iter().collect::<Vec<usize>>>>(); /// let _ = vec![1, 2, 3].into_iter().collect::<Vec<usize>>>>();
/// ^^ help: remove extra angle brackets /// ^^ help: remove extra angle brackets
/// ``` /// ```
crate fn check_trailing_angle_brackets(&mut self, segment: &PathSegment, end: token::TokenKind) { crate fn check_trailing_angle_brackets(&mut self, segment: &PathSegment, end: TokenKind) {
// This function is intended to be invoked after parsing a path segment where there are two // This function is intended to be invoked after parsing a path segment where there are two
// cases: // cases:
// //
@ -726,7 +727,7 @@ impl<'a> Parser<'a> {
/// closing delimiter. /// closing delimiter.
pub fn unexpected_try_recover( pub fn unexpected_try_recover(
&mut self, &mut self,
t: &token::TokenKind, t: &TokenKind,
) -> PResult<'a, bool /* recovered */> { ) -> PResult<'a, bool /* recovered */> {
let token_str = pprust::token_to_string(t); let token_str = pprust::token_to_string(t);
let this_token_str = self.this_token_descr(); let this_token_str = self.this_token_descr();
@ -903,7 +904,7 @@ impl<'a> Parser<'a> {
crate fn recover_closing_delimiter( crate fn recover_closing_delimiter(
&mut self, &mut self,
tokens: &[token::TokenKind], tokens: &[TokenKind],
mut err: DiagnosticBuilder<'a>, mut err: DiagnosticBuilder<'a>,
) -> PResult<'a, bool> { ) -> PResult<'a, bool> {
let mut pos = None; let mut pos = None;

View file

@ -272,7 +272,8 @@ impl<'a> Parser<'a> {
if self.token == token::Dot { if self.token == token::Dot {
// Attempt to recover `.4` as `0.4`. // Attempt to recover `.4` as `0.4`.
recovered = self.look_ahead(1, |t| { recovered = self.look_ahead(1, |t| {
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = t.kind { if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix })
= t.kind {
let next_span = self.look_ahead_span(1); let next_span = self.look_ahead_span(1);
if self.span.hi() == next_span.lo() { if self.span.hi() == next_span.lo() {
let s = String::from("0.") + &symbol.as_str(); let s = String::from("0.") + &symbol.as_str();

View file

@ -5,7 +5,8 @@ use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
use crate::source_map::{SourceMap, FilePathMapping}; use crate::source_map::{SourceMap, FilePathMapping};
use crate::feature_gate::UnstableFeatures; use crate::feature_gate::UnstableFeatures;
use crate::parse::parser::Parser; use crate::parse::parser::Parser;
use crate::syntax::parse::parser::emit_unclosed_delims; use crate::parse::parser::emit_unclosed_delims;
use crate::parse::token::TokenKind;
use crate::tokenstream::{TokenStream, TokenTree}; use crate::tokenstream::{TokenStream, TokenTree};
use crate::diagnostics::plugin::ErrorMap; use crate::diagnostics::plugin::ErrorMap;
use crate::print::pprust::token_to_string; use crate::print::pprust::token_to_string;
@ -358,13 +359,13 @@ pub fn stream_to_parser_with_base_dir<'a>(
/// A sequence separator. /// A sequence separator.
pub struct SeqSep { pub struct SeqSep {
/// The seperator token. /// The seperator token.
pub sep: Option<token::TokenKind>, pub sep: Option<TokenKind>,
/// `true` if a trailing separator is allowed. /// `true` if a trailing separator is allowed.
pub trailing_sep_allowed: bool, pub trailing_sep_allowed: bool,
} }
impl SeqSep { impl SeqSep {
pub fn trailing_allowed(t: token::TokenKind) -> SeqSep { pub fn trailing_allowed(t: TokenKind) -> SeqSep {
SeqSep { SeqSep {
sep: Some(t), sep: Some(t),
trailing_sep_allowed: true, trailing_sep_allowed: true,
@ -426,7 +427,9 @@ mod tests {
match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) { match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) {
( (
4, 4,
Some(&TokenTree::Token(Token { kind: token::Ident(name_macro_rules, false), .. })), Some(&TokenTree::Token(Token {
kind: token::Ident(name_macro_rules, false), ..
})),
Some(&TokenTree::Token(Token { kind: token::Not, .. })), Some(&TokenTree::Token(Token { kind: token::Not, .. })),
Some(&TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. })), Some(&TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. })),
Some(&TokenTree::Delimited(_, macro_delim, ref macro_tts)), Some(&TokenTree::Delimited(_, macro_delim, ref macro_tts)),
@ -446,7 +449,9 @@ mod tests {
( (
2, 2,
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })), Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
Some(&TokenTree::Token(Token { kind: token::Ident(name, false), .. })), Some(&TokenTree::Token(Token {
kind: token::Ident(name, false), ..
})),
) )
if first_delim == token::Paren && name.as_str() == "a" => {}, if first_delim == token::Paren && name.as_str() == "a" => {},
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts), _ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
@ -456,7 +461,9 @@ mod tests {
( (
2, 2,
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })), Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
Some(&TokenTree::Token(Token { kind: token::Ident(name, false), .. })), Some(&TokenTree::Token(Token {
kind: token::Ident(name, false), ..
})),
) )
if second_delim == token::Paren && name.as_str() == "a" => {}, if second_delim == token::Paren && name.as_str() == "a" => {},
_ => panic!("value 4: {:?} {:?}", second_delim, second_tts), _ => panic!("value 4: {:?} {:?}", second_delim, second_tts),

View file

@ -38,7 +38,7 @@ use crate::source_map::{self, SourceMap, Spanned, respan};
use crate::parse::{SeqSep, classify, literal, token}; use crate::parse::{SeqSep, classify, literal, token};
use crate::parse::lexer::UnmatchedBrace; use crate::parse::lexer::UnmatchedBrace;
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::token::{Token, DelimToken}; use crate::parse::token::{Token, TokenKind, DelimToken};
use crate::parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership}; use crate::parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership};
use crate::util::parser::{AssocOp, Fixity}; use crate::util::parser::{AssocOp, Fixity};
use crate::print::pprust; use crate::print::pprust;
@ -337,8 +337,8 @@ impl TokenCursor {
} }
fn next_desugared(&mut self) -> Token { fn next_desugared(&mut self) -> Token {
let (sp, name) = match self.next() { let (name, sp) = match self.next() {
Token { span, kind: token::DocComment(name) } => (span, name), Token { kind: token::DocComment(name), span } => (name, span),
tok => return tok, tok => return tok,
}; };
@ -364,7 +364,7 @@ impl TokenCursor {
[ [
TokenTree::token(token::Ident(sym::doc, false), sp), TokenTree::token(token::Ident(sym::doc, false), sp),
TokenTree::token(token::Eq, sp), TokenTree::token(token::Eq, sp),
TokenTree::token(token::TokenKind::lit( TokenTree::token(TokenKind::lit(
token::StrRaw(num_of_hashes), Symbol::intern(&stripped), None token::StrRaw(num_of_hashes), Symbol::intern(&stripped), None
), sp), ), sp),
] ]
@ -389,7 +389,7 @@ impl TokenCursor {
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
crate enum TokenType { crate enum TokenType {
Token(token::TokenKind), Token(TokenKind),
Keyword(Symbol), Keyword(Symbol),
Operator, Operator,
Lifetime, Lifetime,
@ -419,7 +419,7 @@ impl TokenType {
/// ///
/// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes /// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes
/// that `IDENT` is not the ident of a fn trait. /// that `IDENT` is not the ident of a fn trait.
fn can_continue_type_after_non_fn_ident(t: &token::TokenKind) -> bool { fn can_continue_type_after_non_fn_ident(t: &TokenKind) -> bool {
t == &token::ModSep || t == &token::Lt || t == &token::ModSep || t == &token::Lt ||
t == &token::BinOp(token::Shl) t == &token::BinOp(token::Shl)
} }
@ -565,7 +565,7 @@ impl<'a> Parser<'a> {
} }
/// Expects and consumes the token `t`. Signals an error if the next token is not `t`. /// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
pub fn expect(&mut self, t: &token::TokenKind) -> PResult<'a, bool /* recovered */> { pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, bool /* recovered */> {
if self.expected_tokens.is_empty() { if self.expected_tokens.is_empty() {
if self.token == *t { if self.token == *t {
self.bump(); self.bump();
@ -583,8 +583,8 @@ impl<'a> Parser<'a> {
/// anything. Signal a fatal error if next token is unexpected. /// anything. Signal a fatal error if next token is unexpected.
pub fn expect_one_of( pub fn expect_one_of(
&mut self, &mut self,
edible: &[token::TokenKind], edible: &[TokenKind],
inedible: &[token::TokenKind], inedible: &[TokenKind],
) -> PResult<'a, bool /* recovered */> { ) -> PResult<'a, bool /* recovered */> {
if edible.contains(&self.token) { if edible.contains(&self.token) {
self.bump(); self.bump();
@ -646,14 +646,14 @@ impl<'a> Parser<'a> {
/// ///
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
/// encountered. /// encountered.
crate fn check(&mut self, tok: &token::TokenKind) -> bool { crate fn check(&mut self, tok: &TokenKind) -> bool {
let is_present = self.token == *tok; let is_present = self.token == *tok;
if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); } if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
is_present is_present
} }
/// Consumes a token 'tok' if it exists. Returns whether the given token was present. /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
pub fn eat(&mut self, tok: &token::TokenKind) -> bool { pub fn eat(&mut self, tok: &TokenKind) -> bool {
let is_present = self.check(tok); let is_present = self.check(tok);
if is_present { self.bump() } if is_present { self.bump() }
is_present is_present
@ -889,7 +889,7 @@ impl<'a> Parser<'a> {
/// `f` must consume tokens until reaching the next separator or /// `f` must consume tokens until reaching the next separator or
/// closing bracket. /// closing bracket.
pub fn parse_seq_to_end<T, F>(&mut self, pub fn parse_seq_to_end<T, F>(&mut self,
ket: &token::TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: F) f: F)
-> PResult<'a, Vec<T>> where -> PResult<'a, Vec<T>> where
@ -907,7 +907,7 @@ impl<'a> Parser<'a> {
/// closing bracket. /// closing bracket.
pub fn parse_seq_to_before_end<T, F>( pub fn parse_seq_to_before_end<T, F>(
&mut self, &mut self,
ket: &token::TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: F, f: F,
) -> PResult<'a, (Vec<T>, bool)> ) -> PResult<'a, (Vec<T>, bool)>
@ -918,7 +918,7 @@ impl<'a> Parser<'a> {
crate fn parse_seq_to_before_tokens<T, F>( crate fn parse_seq_to_before_tokens<T, F>(
&mut self, &mut self,
kets: &[&token::TokenKind], kets: &[&TokenKind],
sep: SeqSep, sep: SeqSep,
expect: TokenExpectType, expect: TokenExpectType,
mut f: F, mut f: F,
@ -992,8 +992,8 @@ impl<'a> Parser<'a> {
/// closing bracket. /// closing bracket.
fn parse_unspanned_seq<T, F>( fn parse_unspanned_seq<T, F>(
&mut self, &mut self,
bra: &token::TokenKind, bra: &TokenKind,
ket: &token::TokenKind, ket: &TokenKind,
sep: SeqSep, sep: SeqSep,
f: F, f: F,
) -> PResult<'a, Vec<T>> where ) -> PResult<'a, Vec<T>> where
@ -1036,7 +1036,7 @@ impl<'a> Parser<'a> {
/// Advance the parser using provided token as a next one. Use this when /// Advance the parser using provided token as a next one. Use this when
/// consuming a part of a token. For example a single `<` from `<<`. /// consuming a part of a token. For example a single `<` from `<<`.
fn bump_with(&mut self, next: token::TokenKind, span: Span) { fn bump_with(&mut self, next: TokenKind, span: Span) {
self.prev_span = self.span.with_hi(span.lo()); self.prev_span = self.span.with_hi(span.lo());
// It would be incorrect to record the kind of the current token, but // It would be incorrect to record the kind of the current token, but
// fortunately for tokens currently using `bump_with`, the // fortunately for tokens currently using `bump_with`, the
@ -1050,7 +1050,6 @@ impl<'a> Parser<'a> {
F: FnOnce(&Token) -> R, F: FnOnce(&Token) -> R,
{ {
if dist == 0 { if dist == 0 {
// FIXME: Avoid cloning here.
return f(&self.token); return f(&self.token);
} }
@ -1058,7 +1057,8 @@ impl<'a> Parser<'a> {
f(&match frame.tree_cursor.look_ahead(dist - 1) { f(&match frame.tree_cursor.look_ahead(dist - 1) {
Some(tree) => match tree { Some(tree) => match tree {
TokenTree::Token(token) => token, TokenTree::Token(token) => token,
TokenTree::Delimited(dspan, delim, _) => Token::new(token::OpenDelim(delim), dspan.open), TokenTree::Delimited(dspan, delim, _) =>
Token::new(token::OpenDelim(delim), dspan.open),
} }
None => Token::new(token::CloseDelim(frame.delim), frame.span.close) None => Token::new(token::CloseDelim(frame.delim), frame.span.close)
}) })
@ -1768,7 +1768,7 @@ impl<'a> Parser<'a> {
fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> { fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> {
let ident = self.parse_path_segment_ident()?; let ident = self.parse_path_segment_ident()?;
let is_args_start = |token: &token::TokenKind| match *token { let is_args_start = |token: &TokenKind| match *token {
token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren) token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren)
| token::LArrow => true, | token::LArrow => true,
_ => false, _ => false,
@ -1864,7 +1864,8 @@ impl<'a> Parser<'a> {
} }
fn parse_field_name(&mut self) -> PResult<'a, Ident> { fn parse_field_name(&mut self) -> PResult<'a, Ident> {
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind { if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
self.token.kind {
self.expect_no_suffix(self.span, "a tuple index", suffix); self.expect_no_suffix(self.span, "a tuple index", suffix);
self.bump(); self.bump();
Ok(Ident::new(symbol, self.prev_span)) Ok(Ident::new(symbol, self.prev_span))
@ -2649,8 +2650,10 @@ impl<'a> Parser<'a> {
// Interpolated identifier and lifetime tokens are replaced with usual identifier // Interpolated identifier and lifetime tokens are replaced with usual identifier
// and lifetime tokens, so the former are never encountered during normal parsing. // and lifetime tokens, so the former are never encountered during normal parsing.
match **nt { match **nt {
token::NtIdent(ident, is_raw) => Token::new(token::Ident(ident.name, is_raw), ident.span), token::NtIdent(ident, is_raw) =>
token::NtLifetime(ident) => Token::new(token::Lifetime(ident.name), ident.span), Token::new(token::Ident(ident.name, is_raw), ident.span),
token::NtLifetime(ident) =>
Token::new(token::Lifetime(ident.name), ident.span),
_ => return, _ => return,
} }
} }
@ -4481,7 +4484,9 @@ impl<'a> Parser<'a> {
// We used to incorrectly stop parsing macro-expanded statements here. // We used to incorrectly stop parsing macro-expanded statements here.
// If the next token will be an error anyway but could have parsed with the // If the next token will be an error anyway but could have parsed with the
// earlier behavior, stop parsing here and emit a warning to avoid breakage. // earlier behavior, stop parsing here and emit a warning to avoid breakage.
else if macro_legacy_warnings && self.token.can_begin_expr() && match self.token.kind { else if macro_legacy_warnings &&
self.token.can_begin_expr() &&
match self.token.kind {
// These can continue an expression, so we can't stop parsing and warn. // These can continue an expression, so we can't stop parsing and warn.
token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) | token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
token::BinOp(token::Minus) | token::BinOp(token::Star) | token::BinOp(token::Minus) | token::BinOp(token::Star) |
@ -6409,7 +6414,7 @@ impl<'a> Parser<'a> {
} }
/// Given a termination token, parses all of the items in a module. /// Given a termination token, parses all of the items in a module.
fn parse_mod_items(&mut self, term: &token::TokenKind, inner_lo: Span) -> PResult<'a, Mod> { fn parse_mod_items(&mut self, term: &TokenKind, inner_lo: Span) -> PResult<'a, Mod> {
let mut items = vec![]; let mut items = vec![];
while let Some(item) = self.parse_item()? { while let Some(item) = self.parse_item()? {
items.push(item); items.push(item);

View file

@ -5,11 +5,10 @@ pub use LitKind::*;
pub use TokenKind::*; pub use TokenKind::*;
use crate::ast::{self}; use crate::ast::{self};
use crate::parse::ParseSess; use crate::parse::{parse_stream_from_source_str, ParseSess};
use crate::print::pprust; use crate::print::pprust;
use crate::ptr::P; use crate::ptr::P;
use crate::symbol::kw; use crate::symbol::kw;
use crate::syntax::parse::parse_stream_from_source_str;
use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree}; use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
use syntax_pos::symbol::Symbol; use syntax_pos::symbol::Symbol;

View file

@ -203,7 +203,8 @@ impl TokenStream {
if let Some((_, next)) = iter.peek() { if let Some((_, next)) = iter.peek() {
let sp = match (&ts, &next) { let sp = match (&ts, &next) {
(_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue, (_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
((TokenTree::Token(token_left), NonJoint), (TokenTree::Token(token_right), _)) ((TokenTree::Token(token_left), NonJoint),
(TokenTree::Token(token_right), _))
if ((token_left.is_ident() && !token_left.is_reserved_ident()) if ((token_left.is_ident() && !token_left.is_reserved_ident())
|| token_left.is_lit()) && || token_left.is_lit()) &&
((token_right.is_ident() && !token_right.is_reserved_ident()) ((token_right.is_ident() && !token_right.is_reserved_ident())
@ -575,7 +576,7 @@ impl DelimSpan {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::syntax::ast::Name; use crate::ast::Name;
use crate::with_default_globals; use crate::with_default_globals;
use crate::util::parser_testing::string_to_stream; use crate::util::parser_testing::string_to_stream;
use syntax_pos::{Span, BytePos, NO_EXPANSION}; use syntax_pos::{Span, BytePos, NO_EXPANSION};

View file

@ -103,7 +103,8 @@ fn parse_assert<'a>(
// //
// Parse this as an actual message, and suggest inserting a comma. Eventually, this should be // Parse this as an actual message, and suggest inserting a comma. Eventually, this should be
// turned into an error. // turned into an error.
let custom_message = if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind { let custom_message = if let token::Literal(token::Lit { kind: token::Str, .. })
= parser.token.kind {
let mut err = cx.struct_span_warn(parser.span, "unexpected string literal"); let mut err = cx.struct_span_warn(parser.span, "unexpected string literal");
let comma_span = cx.source_map().next_point(parser.prev_span); let comma_span = cx.source_map().next_point(parser.prev_span);
err.span_suggestion_short( err.span_suggestion_short(

View file

@ -921,10 +921,9 @@ pub struct Interner {
impl Interner { impl Interner {
fn prefill(init: &[&'static str]) -> Self { fn prefill(init: &[&'static str]) -> Self {
let symbols = (0 .. init.len() as u32).map(Symbol::new);
Interner { Interner {
strings: init.to_vec(), strings: init.into(),
names: init.iter().copied().zip(symbols).collect(), names: init.iter().copied().zip((0..).map(Symbol::new)).collect(),
..Default::default() ..Default::default()
} }
} }

View file

@ -1,3 +1,9 @@
// WARNING WARNING WARNING WARNING WARNING
// =======================================
//
// This code also appears in src/doc/unstable-book/src/language-features/plugin.md.
// Please keep the two copies in sync! FIXME: have rustdoc read this file
// force-host // force-host
#![crate_type="dylib"] #![crate_type="dylib"]
@ -8,21 +14,15 @@ extern crate syntax_pos;
extern crate rustc; extern crate rustc;
extern crate rustc_plugin; extern crate rustc_plugin;
use syntax::parse::token; use syntax::parse::token::{self, Token};
use syntax::tokenstream::TokenTree; use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager}; use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder; // trait for expr_usize use syntax::ext::build::AstBuilder; // A trait for expr_usize.
use syntax_pos::Span; use syntax_pos::Span;
use rustc_plugin::Registry; use rustc_plugin::Registry;
// WARNING WARNING WARNING WARNING WARNING
// =======================================
//
// This code also appears in src/doc/unstable-book/src/language-features/plugin.md.
// Please keep the two copies in sync! FIXME: have rustdoc read this file
fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
-> Box<MacResult + 'static> { -> Box<dyn MacResult + 'static> {
static NUMERALS: &'static [(&'static str, usize)] = &[ static NUMERALS: &'static [(&'static str, usize)] = &[
("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("M", 1000), ("CM", 900), ("D", 500), ("CD", 400),
@ -38,7 +38,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
} }
let text = match args[0] { let text = match args[0] {
TokenTree::Token(_, token::Ident(s, _)) => s.to_string(), TokenTree::Token(Token { kind: token::Ident(s, _), .. }) => s.to_string(),
_ => { _ => {
cx.span_err(sp, "argument should be a single identifier"); cx.span_err(sp, "argument should be a single identifier");
return DummyResult::any(sp); return DummyResult::any(sp);