2019-08-11 11:14:30 +00:00
|
|
|
mod expr;
|
2019-08-11 13:24:37 +00:00
|
|
|
mod pat;
|
2019-08-11 16:34:42 +00:00
|
|
|
mod item;
|
|
|
|
pub use item::AliasKind;
|
|
|
|
mod module;
|
|
|
|
pub use module::{ModulePath, ModulePathSuccess};
|
2019-08-11 17:59:27 +00:00
|
|
|
mod ty;
|
|
|
|
mod path;
|
|
|
|
pub use path::PathStyle;
|
2019-08-11 18:32:29 +00:00
|
|
|
mod stmt;
|
2019-08-11 18:44:09 +00:00
|
|
|
mod generics;
|
2019-08-11 16:34:42 +00:00
|
|
|
|
2019-08-11 18:32:29 +00:00
|
|
|
use crate::ast::{self, AttrStyle, Attribute, Arg, BindingMode, StrStyle, SelfKind};
|
2019-08-11 18:46:34 +00:00
|
|
|
use crate::ast::{FnDecl, Ident, IsAsync, MacDelimiter, Mutability, TyKind};
|
2019-08-11 16:34:42 +00:00
|
|
|
use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar};
|
2019-06-07 10:31:13 +00:00
|
|
|
use crate::ext::hygiene::SyntaxContext;
|
2019-08-11 16:34:42 +00:00
|
|
|
use crate::source_map::{self, respan};
|
2019-08-11 18:32:29 +00:00
|
|
|
use crate::parse::{SeqSep, literal, token};
|
2019-06-04 15:48:40 +00:00
|
|
|
use crate::parse::lexer::UnmatchedBrace;
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
2019-06-05 11:17:56 +00:00
|
|
|
use crate::parse::token::{Token, TokenKind, DelimToken};
|
2019-08-11 16:34:42 +00:00
|
|
|
use crate::parse::{ParseSess, Directory, DirectoryOwnership};
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::print::pprust;
|
|
|
|
use crate::ptr::P;
|
|
|
|
use crate::parse::PResult;
|
|
|
|
use crate::ThinVec;
|
|
|
|
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
|
2019-05-11 14:41:37 +00:00
|
|
|
use crate::symbol::{kw, sym, Symbol};
|
2019-05-31 01:19:48 +00:00
|
|
|
use crate::parse::diagnostics::{Error, dummy_arg};
|
2019-02-06 17:33:01 +00:00
|
|
|
|
2019-08-11 16:34:42 +00:00
|
|
|
use errors::{Applicability, DiagnosticId, FatalError};
|
2018-04-25 16:30:39 +00:00
|
|
|
use rustc_target::spec::abi::{self, Abi};
|
2019-05-29 01:10:49 +00:00
|
|
|
use syntax_pos::{Span, BytePos, DUMMY_SP, FileName};
|
2019-05-23 19:55:26 +00:00
|
|
|
use log::debug;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2018-05-18 06:19:35 +00:00
|
|
|
use std::borrow::Cow;
|
2019-08-11 16:34:42 +00:00
|
|
|
use std::{cmp, mem, slice};
|
|
|
|
use std::path::PathBuf;
|
2018-07-03 17:38:14 +00:00
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
bitflags::bitflags! {
|
2018-05-31 22:53:30 +00:00
|
|
|
struct Restrictions: u8 {
|
2017-09-08 19:08:01 +00:00
|
|
|
const STMT_EXPR = 1 << 0;
|
|
|
|
const NO_STRUCT_LITERAL = 1 << 1;
|
2014-09-16 05:22:12 +00:00
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
}
|
2011-01-24 23:26:10 +00:00
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2019-05-16 21:31:07 +00:00
|
|
|
crate enum SemiColonMode {
|
2016-02-10 03:11:27 +00:00
|
|
|
Break,
|
|
|
|
Ignore,
|
2019-01-20 08:37:06 +00:00
|
|
|
Comma,
|
2016-02-10 03:11:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2019-05-16 21:31:07 +00:00
|
|
|
crate enum BlockMode {
|
2017-04-13 19:37:05 +00:00
|
|
|
Break,
|
|
|
|
Ignore,
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// As maybe_whole_expr, but for things other than expressions
|
2019-08-11 13:24:37 +00:00
|
|
|
#[macro_export]
|
2014-11-14 17:18:10 +00:00
|
|
|
macro_rules! maybe_whole {
|
2016-11-02 03:03:55 +00:00
|
|
|
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
|
2019-06-04 22:17:07 +00:00
|
|
|
if let token::Interpolated(nt) = &$p.token.kind {
|
2019-03-10 08:53:16 +00:00
|
|
|
if let token::$constructor(x) = &**nt {
|
|
|
|
let $x = x.clone();
|
2016-11-02 03:03:55 +00:00
|
|
|
$p.bump();
|
|
|
|
return Ok($e);
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
};
|
2014-11-14 17:18:10 +00:00
|
|
|
}
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2019-03-09 14:41:01 +00:00
|
|
|
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
|
2019-08-11 11:14:30 +00:00
|
|
|
#[macro_export]
|
2019-03-09 14:41:01 +00:00
|
|
|
macro_rules! maybe_recover_from_interpolated_ty_qpath {
|
|
|
|
($self: expr, $allow_qpath_recovery: expr) => {
|
|
|
|
if $allow_qpath_recovery && $self.look_ahead(1, |t| t == &token::ModSep) {
|
2019-06-04 22:17:07 +00:00
|
|
|
if let token::Interpolated(nt) = &$self.token.kind {
|
2019-03-09 14:41:01 +00:00
|
|
|
if let token::NtTy(ty) = &**nt {
|
|
|
|
let ty = ty.clone();
|
|
|
|
$self.bump();
|
|
|
|
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_span, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 19:15:03 +00:00
|
|
|
fn maybe_append(mut lhs: Vec<Attribute>, mut rhs: Option<Vec<Attribute>>) -> Vec<Attribute> {
|
|
|
|
if let Some(ref mut rhs) = rhs {
|
|
|
|
lhs.append(rhs);
|
2012-08-14 18:07:41 +00:00
|
|
|
}
|
2014-10-15 06:05:01 +00:00
|
|
|
lhs
|
2012-08-14 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:54:01 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2016-09-21 02:16:28 +00:00
|
|
|
enum PrevTokenKind {
|
2016-09-16 05:46:40 +00:00
|
|
|
DocComment,
|
|
|
|
Comma,
|
2017-04-04 22:12:53 +00:00
|
|
|
Plus,
|
2016-09-16 05:46:40 +00:00
|
|
|
Interpolated,
|
|
|
|
Eof,
|
2017-07-04 14:04:34 +00:00
|
|
|
Ident,
|
2019-04-23 02:37:23 +00:00
|
|
|
BitOr,
|
2016-09-16 05:46:40 +00:00
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// NOTE: `Ident`s are handled by `common.rs`.
|
2012-07-26 17:14:01 +00:00
|
|
|
|
2017-06-15 03:42:24 +00:00
|
|
|
#[derive(Clone)]
|
2014-03-09 14:54:34 +00:00
|
|
|
pub struct Parser<'a> {
|
2014-03-27 22:39:48 +00:00
|
|
|
pub sess: &'a ParseSess,
|
2019-06-05 19:04:52 +00:00
|
|
|
/// The current normalized token.
|
|
|
|
/// "Normalized" means that some interpolated tokens
|
|
|
|
/// (`$i: ident` and `$l: lifetime` meta-variables) are replaced
|
|
|
|
/// with non-interpolated identifier and lifetime tokens they refer to.
|
|
|
|
/// Perhaps the normalized / non-normalized setup can be simplified somehow.
|
2019-06-04 22:17:07 +00:00
|
|
|
pub token: Token,
|
2019-06-05 19:04:52 +00:00
|
|
|
/// Span of the current non-normalized token.
|
2018-05-31 22:53:30 +00:00
|
|
|
meta_var_span: Option<Span>,
|
2019-06-05 19:04:52 +00:00
|
|
|
/// Span of the previous non-normalized token.
|
2016-09-21 02:09:22 +00:00
|
|
|
pub prev_span: Span,
|
2019-06-05 19:04:52 +00:00
|
|
|
/// Kind of the previous normalized token (in simplified form).
|
2016-09-21 02:16:28 +00:00
|
|
|
prev_token_kind: PrevTokenKind,
|
2018-05-31 22:53:30 +00:00
|
|
|
restrictions: Restrictions,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Used to determine the path to externally loaded source files.
|
2018-05-31 22:53:30 +00:00
|
|
|
crate directory: Directory<'a>,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// `true` to parse sub-modules in other files.
|
2017-05-17 22:37:24 +00:00
|
|
|
pub recurse_into_file_modules: bool,
|
2014-05-16 21:23:04 +00:00
|
|
|
/// Name of the root module this parser originated from. If `None`, then the
|
|
|
|
/// name is not known. This does not change while the parser is descending
|
|
|
|
/// into modules, and sub-parsers have new values for this name.
|
2014-05-22 23:57:53 +00:00
|
|
|
pub root_module_name: Option<String>,
|
2018-05-31 22:53:30 +00:00
|
|
|
crate expected_tokens: Vec<TokenType>,
|
2019-05-23 19:55:26 +00:00
|
|
|
crate token_cursor: TokenCursor,
|
2018-05-31 22:53:30 +00:00
|
|
|
desugar_doc_comments: bool,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// `true` we should configure out of line modules as we parse.
|
2017-01-18 00:13:36 +00:00
|
|
|
pub cfg_mods: bool,
|
2019-01-23 01:35:13 +00:00
|
|
|
/// This field is used to keep track of how many left angle brackets we have seen. This is
|
|
|
|
/// required in order to detect extra leading left angle brackets (`<` characters) and error
|
|
|
|
/// appropriately.
|
|
|
|
///
|
|
|
|
/// See the comments in the `parse_path_segment` function for more details.
|
|
|
|
crate unmatched_angle_bracket_count: u32,
|
2019-01-28 05:04:50 +00:00
|
|
|
crate max_angle_bracket_count: u32,
|
2019-02-06 10:24:07 +00:00
|
|
|
/// List of all unclosed delimiters found by the lexer. If an entry is used for error recovery
|
|
|
|
/// it gets removed from here. Every entry left at the end gets emitted as an independent
|
|
|
|
/// error.
|
2019-01-28 05:04:50 +00:00
|
|
|
crate unclosed_delims: Vec<UnmatchedBrace>,
|
2019-05-23 19:55:26 +00:00
|
|
|
crate last_unexpected_token_span: Option<Span>,
|
2019-07-17 18:40:36 +00:00
|
|
|
crate last_type_ascription: Option<(Span, bool /* likely path typo */)>,
|
2019-05-22 05:17:53 +00:00
|
|
|
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
|
|
|
|
crate subparser_name: Option<&'static str>,
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 19:13:19 +00:00
|
|
|
impl<'a> Drop for Parser<'a> {
|
|
|
|
fn drop(&mut self) {
|
2019-03-04 20:59:43 +00:00
|
|
|
let diag = self.diagnostic();
|
|
|
|
emit_unclosed_delims(&mut self.unclosed_delims, diag);
|
2019-03-03 19:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 22:37:24 +00:00
|
|
|
|
2017-06-10 03:30:33 +00:00
|
|
|
#[derive(Clone)]
|
2019-05-23 19:55:26 +00:00
|
|
|
crate struct TokenCursor {
|
|
|
|
crate frame: TokenCursorFrame,
|
|
|
|
crate stack: Vec<TokenCursorFrame>,
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 03:30:33 +00:00
|
|
|
#[derive(Clone)]
|
2019-05-23 19:55:26 +00:00
|
|
|
crate struct TokenCursorFrame {
|
|
|
|
crate delim: token::DelimToken,
|
|
|
|
crate span: DelimSpan,
|
|
|
|
crate open_delim: bool,
|
|
|
|
crate tree_cursor: tokenstream::Cursor,
|
|
|
|
crate close_delim: bool,
|
|
|
|
crate last_token: LastToken,
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is used in `TokenCursorFrame` above to track tokens that are consumed
|
|
|
|
/// by the parser, and then that's transitively used to record the tokens that
|
|
|
|
/// each parse AST item is created with.
|
|
|
|
///
|
|
|
|
/// Right now this has two states, either collecting tokens or not collecting
|
|
|
|
/// tokens. If we're collecting tokens we just save everything off into a local
|
|
|
|
/// `Vec`. This should eventually though likely save tokens from the original
|
|
|
|
/// token stream and just use slicing of token streams to avoid creation of a
|
|
|
|
/// whole new vector.
|
|
|
|
///
|
|
|
|
/// The second state is where we're passively not recording tokens, but the last
|
|
|
|
/// token is still tracked for when we want to start recording tokens. This
|
|
|
|
/// "last token" means that when we start recording tokens we'll want to ensure
|
|
|
|
/// that this, the first token, is included in the output.
|
|
|
|
///
|
|
|
|
/// You can find some more example usage of this in the `collect_tokens` method
|
|
|
|
/// on the parser.
|
|
|
|
#[derive(Clone)]
|
2019-05-23 19:55:26 +00:00
|
|
|
crate enum LastToken {
|
2019-01-10 00:58:38 +00:00
|
|
|
Collecting(Vec<TreeAndJoint>),
|
|
|
|
Was(Option<TreeAndJoint>),
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenCursorFrame {
|
2019-06-25 21:22:45 +00:00
|
|
|
fn new(span: DelimSpan, delim: DelimToken, tts: &TokenStream) -> Self {
|
2017-02-20 05:44:06 +00:00
|
|
|
TokenCursorFrame {
|
2019-06-25 21:22:45 +00:00
|
|
|
delim,
|
|
|
|
span,
|
2018-11-29 23:02:04 +00:00
|
|
|
open_delim: delim == token::NoDelim,
|
2019-01-09 05:53:14 +00:00
|
|
|
tree_cursor: tts.clone().into_trees(),
|
2018-11-29 23:02:04 +00:00
|
|
|
close_delim: delim == token::NoDelim,
|
2017-07-12 16:50:05 +00:00
|
|
|
last_token: LastToken::Was(None),
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenCursor {
|
2019-06-04 15:48:40 +00:00
|
|
|
fn next(&mut self) -> Token {
|
2017-02-20 05:44:06 +00:00
|
|
|
loop {
|
|
|
|
let tree = if !self.frame.open_delim {
|
|
|
|
self.frame.open_delim = true;
|
2018-11-29 23:02:04 +00:00
|
|
|
TokenTree::open_tt(self.frame.span.open, self.frame.delim)
|
2017-02-20 05:44:06 +00:00
|
|
|
} else if let Some(tree) = self.frame.tree_cursor.next() {
|
|
|
|
tree
|
|
|
|
} else if !self.frame.close_delim {
|
|
|
|
self.frame.close_delim = true;
|
2018-11-29 23:02:04 +00:00
|
|
|
TokenTree::close_tt(self.frame.span.close, self.frame.delim)
|
2017-02-20 05:44:06 +00:00
|
|
|
} else if let Some(frame) = self.stack.pop() {
|
|
|
|
self.frame = frame;
|
|
|
|
continue
|
|
|
|
} else {
|
2019-06-05 06:39:34 +00:00
|
|
|
return Token::new(token::Eof, DUMMY_SP);
|
2017-02-20 05:44:06 +00:00
|
|
|
};
|
|
|
|
|
2017-07-12 16:50:05 +00:00
|
|
|
match self.frame.last_token {
|
2018-07-22 15:48:29 +00:00
|
|
|
LastToken::Collecting(ref mut v) => v.push(tree.clone().into()),
|
|
|
|
LastToken::Was(ref mut t) => *t = Some(tree.clone().into()),
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 05:44:06 +00:00
|
|
|
match tree {
|
2019-06-04 17:42:43 +00:00
|
|
|
TokenTree::Token(token) => return token,
|
2018-11-29 23:02:04 +00:00
|
|
|
TokenTree::Delimited(sp, delim, tts) => {
|
|
|
|
let frame = TokenCursorFrame::new(sp, delim, &tts);
|
2017-02-20 05:44:06 +00:00
|
|
|
self.stack.push(mem::replace(&mut self.frame, frame));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:48:40 +00:00
|
|
|
fn next_desugared(&mut self) -> Token {
|
2019-06-05 11:17:56 +00:00
|
|
|
let (name, sp) = match self.next() {
|
|
|
|
Token { kind: token::DocComment(name), span } => (name, span),
|
2017-05-12 18:05:39 +00:00
|
|
|
tok => return tok,
|
2017-02-20 05:44:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let stripped = strip_doc_comment_decoration(&name.as_str());
|
|
|
|
|
|
|
|
// Searches for the occurrences of `"#*` and returns the minimum number of `#`s
|
|
|
|
// required to wrap the text.
|
|
|
|
let mut num_of_hashes = 0;
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in stripped.chars() {
|
|
|
|
count = match ch {
|
|
|
|
'"' => 1,
|
|
|
|
'#' if count > 0 => count + 1,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
num_of_hashes = cmp::max(num_of_hashes, count);
|
|
|
|
}
|
|
|
|
|
2018-09-09 01:07:02 +00:00
|
|
|
let delim_span = DelimSpan::from_single(sp);
|
2018-11-29 23:02:04 +00:00
|
|
|
let body = TokenTree::Delimited(
|
|
|
|
delim_span,
|
|
|
|
token::Bracket,
|
2019-05-18 22:04:26 +00:00
|
|
|
[
|
2019-06-05 10:25:26 +00:00
|
|
|
TokenTree::token(token::Ident(sym::doc, false), sp),
|
|
|
|
TokenTree::token(token::Eq, sp),
|
2019-06-05 11:17:56 +00:00
|
|
|
TokenTree::token(TokenKind::lit(
|
2019-05-18 22:04:26 +00:00
|
|
|
token::StrRaw(num_of_hashes), Symbol::intern(&stripped), None
|
2019-06-05 10:25:26 +00:00
|
|
|
), sp),
|
2018-11-29 23:02:04 +00:00
|
|
|
]
|
|
|
|
.iter().cloned().collect::<TokenStream>().into(),
|
|
|
|
);
|
2017-02-20 05:44:06 +00:00
|
|
|
|
2018-11-29 23:02:04 +00:00
|
|
|
self.stack.push(mem::replace(&mut self.frame, TokenCursorFrame::new(
|
|
|
|
delim_span,
|
|
|
|
token::NoDelim,
|
|
|
|
&if doc_comment_style(&name.as_str()) == AttrStyle::Inner {
|
2019-06-05 10:25:26 +00:00
|
|
|
[TokenTree::token(token::Pound, sp), TokenTree::token(token::Not, sp), body]
|
2017-02-21 05:05:59 +00:00
|
|
|
.iter().cloned().collect::<TokenStream>().into()
|
2017-02-20 05:44:06 +00:00
|
|
|
} else {
|
2019-06-05 10:25:26 +00:00
|
|
|
[TokenTree::token(token::Pound, sp), body]
|
2017-02-21 05:05:59 +00:00
|
|
|
.iter().cloned().collect::<TokenStream>().into()
|
2017-02-20 05:44:06 +00:00
|
|
|
},
|
2018-11-29 23:02:04 +00:00
|
|
|
)));
|
2017-02-20 05:44:06 +00:00
|
|
|
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
2018-05-31 22:53:30 +00:00
|
|
|
crate enum TokenType {
|
2019-06-05 11:17:56 +00:00
|
|
|
Token(TokenKind),
|
2019-05-11 14:41:37 +00:00
|
|
|
Keyword(Symbol),
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
Operator,
|
2017-01-18 16:01:04 +00:00
|
|
|
Lifetime,
|
|
|
|
Ident,
|
|
|
|
Path,
|
|
|
|
Type,
|
2019-02-05 15:49:38 +00:00
|
|
|
Const,
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenType {
|
2019-05-16 21:31:07 +00:00
|
|
|
crate fn to_string(&self) -> String {
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
match *self {
|
2019-06-08 19:38:23 +00:00
|
|
|
TokenType::Token(ref t) => format!("`{}`", pprust::token_kind_to_string(t)),
|
2019-05-11 14:41:37 +00:00
|
|
|
TokenType::Keyword(kw) => format!("`{}`", kw),
|
2017-01-18 16:01:04 +00:00
|
|
|
TokenType::Operator => "an operator".to_string(),
|
|
|
|
TokenType::Lifetime => "lifetime".to_string(),
|
|
|
|
TokenType::Ident => "identifier".to_string(),
|
|
|
|
TokenType::Path => "path".to_string(),
|
|
|
|
TokenType::Type => "type".to_string(),
|
2019-02-05 15:49:38 +00:00
|
|
|
TokenType::Const => "const".to_string(),
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-05-23 19:55:26 +00:00
|
|
|
crate enum TokenExpectType {
|
2017-09-07 06:07:49 +00:00
|
|
|
Expect,
|
|
|
|
NoExpect,
|
|
|
|
}
|
|
|
|
|
2014-03-09 14:54:34 +00:00
|
|
|
impl<'a> Parser<'a> {
|
2019-05-22 00:47:23 +00:00
|
|
|
pub fn new(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
tokens: TokenStream,
|
|
|
|
directory: Option<Directory<'a>>,
|
|
|
|
recurse_into_file_modules: bool,
|
|
|
|
desugar_doc_comments: bool,
|
2019-05-22 05:17:53 +00:00
|
|
|
subparser_name: Option<&'static str>,
|
2019-05-22 00:47:23 +00:00
|
|
|
) -> Self {
|
2016-11-03 07:43:29 +00:00
|
|
|
let mut parser = Parser {
|
2017-08-07 05:54:09 +00:00
|
|
|
sess,
|
2019-06-05 06:39:34 +00:00
|
|
|
token: Token::dummy(),
|
2019-05-22 00:47:23 +00:00
|
|
|
prev_span: DUMMY_SP,
|
2017-03-29 07:17:18 +00:00
|
|
|
meta_var_span: None,
|
2016-09-21 02:16:28 +00:00
|
|
|
prev_token_kind: PrevTokenKind::Other,
|
2015-04-29 21:58:43 +00:00
|
|
|
restrictions: Restrictions::empty(),
|
2017-08-07 05:54:09 +00:00
|
|
|
recurse_into_file_modules,
|
2017-11-28 02:14:24 +00:00
|
|
|
directory: Directory {
|
2018-05-18 06:19:35 +00:00
|
|
|
path: Cow::from(PathBuf::new()),
|
2017-11-28 02:14:24 +00:00
|
|
|
ownership: DirectoryOwnership::Owned { relative: None }
|
|
|
|
},
|
2014-05-25 23:27:36 +00:00
|
|
|
root_module_name: None,
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
expected_tokens: Vec::new(),
|
2017-02-20 05:44:06 +00:00
|
|
|
token_cursor: TokenCursor {
|
2018-11-29 23:02:04 +00:00
|
|
|
frame: TokenCursorFrame::new(
|
|
|
|
DelimSpan::dummy(),
|
|
|
|
token::NoDelim,
|
|
|
|
&tokens.into(),
|
|
|
|
),
|
2017-02-20 05:44:06 +00:00
|
|
|
stack: Vec::new(),
|
|
|
|
},
|
2017-08-07 05:54:09 +00:00
|
|
|
desugar_doc_comments,
|
2017-01-18 00:13:36 +00:00
|
|
|
cfg_mods: true,
|
2019-01-23 01:35:13 +00:00
|
|
|
unmatched_angle_bracket_count: 0,
|
2019-01-28 05:04:50 +00:00
|
|
|
max_angle_bracket_count: 0,
|
|
|
|
unclosed_delims: Vec::new(),
|
2019-03-02 05:47:06 +00:00
|
|
|
last_unexpected_token_span: None,
|
2019-07-17 18:40:36 +00:00
|
|
|
last_type_ascription: None,
|
2019-05-22 05:17:53 +00:00
|
|
|
subparser_name,
|
2016-11-03 07:43:29 +00:00
|
|
|
};
|
|
|
|
|
2019-06-04 22:17:07 +00:00
|
|
|
parser.token = parser.next_tok();
|
2017-05-17 22:37:24 +00:00
|
|
|
|
2016-12-07 00:28:51 +00:00
|
|
|
if let Some(directory) = directory {
|
|
|
|
parser.directory = directory;
|
2019-06-07 10:31:13 +00:00
|
|
|
} else if !parser.token.span.is_dummy() {
|
|
|
|
if let FileName::Real(mut path) =
|
|
|
|
sess.source_map().span_to_unmapped_path(parser.token.span) {
|
2018-05-18 06:19:35 +00:00
|
|
|
path.pop();
|
|
|
|
parser.directory.path = Cow::from(path);
|
2017-12-14 07:09:19 +00:00
|
|
|
}
|
2016-11-03 07:43:29 +00:00
|
|
|
}
|
2017-05-17 22:37:24 +00:00
|
|
|
|
2017-03-29 07:17:18 +00:00
|
|
|
parser.process_potential_macro_variable();
|
2016-11-03 07:43:29 +00:00
|
|
|
parser
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:48:40 +00:00
|
|
|
fn next_tok(&mut self) -> Token {
|
2017-05-12 18:05:39 +00:00
|
|
|
let mut next = if self.desugar_doc_comments {
|
|
|
|
self.token_cursor.next_desugared()
|
|
|
|
} else {
|
|
|
|
self.token_cursor.next()
|
2017-02-21 12:04:45 +00:00
|
|
|
};
|
2019-06-04 15:48:40 +00:00
|
|
|
if next.span.is_dummy() {
|
2018-04-22 01:10:15 +00:00
|
|
|
// Tweak the location for better diagnostics, but keep syntactic context intact.
|
2019-06-04 15:48:40 +00:00
|
|
|
next.span = self.prev_span.with_ctxt(next.span.ctxt());
|
2014-05-25 23:27:36 +00:00
|
|
|
}
|
2017-02-21 12:04:45 +00:00
|
|
|
next
|
2014-05-25 23:27:36 +00:00
|
|
|
}
|
2014-06-09 20:12:30 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Converts the current token to a string using `self`'s reader.
|
2015-01-21 11:44:49 +00:00
|
|
|
pub fn this_token_to_string(&self) -> String {
|
2018-05-31 22:53:30 +00:00
|
|
|
pprust::token_to_string(&self.token)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 19:55:26 +00:00
|
|
|
crate fn token_descr(&self) -> Option<&'static str> {
|
2019-06-04 22:17:07 +00:00
|
|
|
Some(match &self.token.kind {
|
2019-06-05 08:56:06 +00:00
|
|
|
_ if self.token.is_special_ident() => "reserved identifier",
|
|
|
|
_ if self.token.is_used_keyword() => "keyword",
|
|
|
|
_ if self.token.is_unused_keyword() => "reserved keyword",
|
2018-10-28 23:05:07 +00:00
|
|
|
token::DocComment(..) => "doc comment",
|
2018-01-06 22:43:20 +00:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-10 23:31:34 +00:00
|
|
|
crate fn this_token_descr(&self) -> String {
|
2018-01-06 22:43:20 +00:00
|
|
|
if let Some(prefix) = self.token_descr() {
|
|
|
|
format!("{} `{}`", prefix, self.this_token_to_string())
|
|
|
|
} else {
|
|
|
|
format!("`{}`", self.this_token_to_string())
|
|
|
|
}
|
2016-04-20 23:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
crate fn unexpected<T>(&mut self) -> PResult<'a, T> {
|
2015-03-28 21:58:51 +00:00
|
|
|
match self.expect_one_of(&[], &[]) {
|
2015-12-30 23:11:53 +00:00
|
|
|
Err(e) => Err(e),
|
|
|
|
Ok(_) => unreachable!(),
|
2015-03-28 21:58:51 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
|
2019-06-05 11:17:56 +00:00
|
|
|
pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, bool /* recovered */> {
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
if self.expected_tokens.is_empty() {
|
|
|
|
if self.token == *t {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-01-28 05:04:50 +00:00
|
|
|
Ok(false)
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
} else {
|
2019-05-22 05:17:53 +00:00
|
|
|
self.unexpected_try_recover(t)
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
} else {
|
2018-06-01 14:05:46 +00:00
|
|
|
self.expect_one_of(slice::from_ref(t), &[])
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Expect next token to be edible or inedible token. If edible,
|
|
|
|
/// then consume it; if inedible, then return without consuming
|
|
|
|
/// anything. Signal a fatal error if next token is unexpected.
|
2019-01-28 05:04:50 +00:00
|
|
|
pub fn expect_one_of(
|
|
|
|
&mut self,
|
2019-06-05 11:17:56 +00:00
|
|
|
edible: &[TokenKind],
|
|
|
|
inedible: &[TokenKind],
|
2019-01-28 05:04:50 +00:00
|
|
|
) -> PResult<'a, bool /* recovered */> {
|
2019-06-08 19:38:23 +00:00
|
|
|
if edible.contains(&self.token.kind) {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-01-28 05:04:50 +00:00
|
|
|
Ok(false)
|
2019-06-08 19:38:23 +00:00
|
|
|
} else if inedible.contains(&self.token.kind) {
|
2013-08-05 20:18:29 +00:00
|
|
|
// leave it in the input
|
2019-01-28 05:04:50 +00:00
|
|
|
Ok(false)
|
2019-06-07 10:31:13 +00:00
|
|
|
} else if self.last_unexpected_token_span == Some(self.token.span) {
|
2019-03-02 05:47:06 +00:00
|
|
|
FatalError.raise();
|
2013-08-05 20:18:29 +00:00
|
|
|
} else {
|
2019-05-23 20:10:24 +00:00
|
|
|
self.expected_one_of_not_found(edible, inedible)
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:04:11 +00:00
|
|
|
pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
|
2018-01-06 22:43:20 +00:00
|
|
|
self.parse_ident_common(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
|
2019-06-04 22:17:07 +00:00
|
|
|
match self.token.kind {
|
2019-06-05 08:56:06 +00:00
|
|
|
token::Ident(name, _) => {
|
2017-06-29 10:16:35 +00:00
|
|
|
if self.token.is_reserved_ident() {
|
2018-01-23 03:03:51 +00:00
|
|
|
let mut err = self.expected_ident_found();
|
2018-01-06 22:43:20 +00:00
|
|
|
if recover {
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
|
|
|
}
|
2017-06-29 10:16:35 +00:00
|
|
|
}
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span;
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-06-05 08:56:06 +00:00
|
|
|
Ok(Ident::new(name, span))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2016-09-21 02:16:28 +00:00
|
|
|
Err(if self.prev_token_kind == PrevTokenKind::DocComment {
|
2019-06-03 19:06:49 +00:00
|
|
|
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
|
|
|
|
} else {
|
|
|
|
self.expected_ident_found()
|
|
|
|
})
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks if the next token is `tok`, and returns `true` if so.
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
///
|
2016-01-31 19:39:50 +00:00
|
|
|
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
/// encountered.
|
2019-06-05 11:17:56 +00:00
|
|
|
crate fn check(&mut self, tok: &TokenKind) -> bool {
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
let is_present = self.token == *tok;
|
|
|
|
if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
|
|
|
|
is_present
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
|
2019-06-05 11:17:56 +00:00
|
|
|
pub fn eat(&mut self, tok: &TokenKind) -> bool {
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
let is_present = self.check(tok);
|
2015-12-30 23:11:53 +00:00
|
|
|
if is_present { self.bump() }
|
|
|
|
is_present
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 14:41:37 +00:00
|
|
|
fn check_keyword(&mut self, kw: Symbol) -> bool {
|
2015-01-16 03:04:28 +00:00
|
|
|
self.expected_tokens.push(TokenType::Keyword(kw));
|
|
|
|
self.token.is_keyword(kw)
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// If the next token is the given keyword, eats it and returns
|
|
|
|
/// `true`. Otherwise, returns `false`.
|
2019-05-11 14:41:37 +00:00
|
|
|
pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
|
2015-01-16 03:04:28 +00:00
|
|
|
if self.check_keyword(kw) {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
|
|
|
true
|
2015-01-16 03:04:28 +00:00
|
|
|
} else {
|
2015-12-30 23:11:53 +00:00
|
|
|
false
|
2015-01-16 03:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 14:41:37 +00:00
|
|
|
fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(kw) {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
|
|
|
true
|
2014-08-28 04:34:03 +00:00
|
|
|
} else {
|
2015-12-30 23:11:53 +00:00
|
|
|
false
|
2014-08-28 04:34:03 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// If the given word is not a keyword, signals an error.
|
|
|
|
/// If the next token is not the given word, signals an error.
|
|
|
|
/// Otherwise, eats it.
|
2019-05-11 14:41:37 +00:00
|
|
|
fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
|
2015-12-30 23:11:53 +00:00
|
|
|
if !self.eat_keyword(kw) {
|
|
|
|
self.unexpected()
|
2015-03-28 21:58:51 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:55:26 +00:00
|
|
|
crate fn check_ident(&mut self) -> bool {
|
2017-01-18 16:01:04 +00:00
|
|
|
if self.token.is_ident() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.expected_tokens.push(TokenType::Ident);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_path(&mut self) -> bool {
|
|
|
|
if self.token.is_path_start() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.expected_tokens.push(TokenType::Path);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_type(&mut self) -> bool {
|
|
|
|
if self.token.can_begin_type() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.expected_tokens.push(TokenType::Type);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:49:38 +00:00
|
|
|
fn check_const_arg(&mut self) -> bool {
|
|
|
|
if self.token.can_begin_const_arg() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.expected_tokens.push(TokenType::Const);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes a `+`. if `+=` is seen, replaces it with a `=`
|
|
|
|
/// and continues. If a `+` is not seen, returns `false`.
|
2018-05-25 20:40:16 +00:00
|
|
|
///
|
2019-02-08 13:53:55 +00:00
|
|
|
/// This is used when token-splitting `+=` into `+`.
|
|
|
|
/// See issue #47856 for an example of when this may occur.
|
2018-05-25 20:40:16 +00:00
|
|
|
fn eat_plus(&mut self) -> bool {
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
|
2019-06-04 22:17:07 +00:00
|
|
|
match self.token.kind {
|
2018-05-25 20:40:16 +00:00
|
|
|
token::BinOp(token::Plus) => {
|
|
|
|
self.bump();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
token::BinOpEq(token::Plus) => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2018-05-25 20:40:16 +00:00
|
|
|
self.bump_with(token::Eq, span);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 21:36:23 +00:00
|
|
|
|
2018-05-25 21:09:32 +00:00
|
|
|
/// Checks to see if the next token is either `+` or `+=`.
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Otherwise returns `false`.
|
2018-05-25 21:09:32 +00:00
|
|
|
fn check_plus(&mut self) -> bool {
|
|
|
|
if self.token.is_like_plus() {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else {
|
2018-06-01 13:52:51 +00:00
|
|
|
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
|
|
|
|
false
|
2018-05-25 21:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 16:01:04 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes an `&`. If `&&` is seen, replaces it with a single
|
|
|
|
/// `&` and continues. If an `&` is not seen, signals an error.
|
2015-12-20 21:00:43 +00:00
|
|
|
fn expect_and(&mut self) -> PResult<'a, ()> {
|
2015-01-16 03:04:28 +00:00
|
|
|
self.expected_tokens.push(TokenType::Token(token::BinOp(token::And)));
|
2019-06-04 22:17:07 +00:00
|
|
|
match self.token.kind {
|
2015-12-30 23:11:53 +00:00
|
|
|
token::BinOp(token::And) => {
|
|
|
|
self.bump();
|
|
|
|
Ok(())
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::AndAnd => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2017-07-31 20:04:34 +00:00
|
|
|
Ok(self.bump_with(token::BinOp(token::And), span))
|
2014-04-17 08:35:31 +00:00
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
_ => self.unexpected()
|
2014-04-17 08:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes an `|`. If `||` is seen, replaces it with a single
|
|
|
|
/// `|` and continues. If an `|` is not seen, signals an error.
|
2017-09-07 06:07:49 +00:00
|
|
|
fn expect_or(&mut self) -> PResult<'a, ()> {
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Or)));
|
2019-06-04 22:17:07 +00:00
|
|
|
match self.token.kind {
|
2017-09-07 06:07:49 +00:00
|
|
|
token::BinOp(token::Or) => {
|
|
|
|
self.bump();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
token::OrOr => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2017-09-07 06:07:49 +00:00
|
|
|
Ok(self.bump_with(token::BinOp(token::Or), span))
|
|
|
|
}
|
|
|
|
_ => self.unexpected()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
|
2019-05-18 14:36:30 +00:00
|
|
|
literal::expect_no_suffix(&self.sess.span_diagnostic, sp, kind, suffix)
|
2014-11-19 04:48:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Attempts to consume a `<`. If `<<` is seen, replaces it with a single
|
|
|
|
/// `<` and continue. If `<-` is seen, replaces it with a single `<`
|
|
|
|
/// and continue. If a `<` is not seen, returns false.
|
2014-06-09 20:12:30 +00:00
|
|
|
///
|
|
|
|
/// This is meant to be used when parsing generics on a path to get the
|
2014-12-23 00:13:49 +00:00
|
|
|
/// starting token.
|
2015-12-30 23:11:53 +00:00
|
|
|
fn eat_lt(&mut self) -> bool {
|
2015-01-16 03:04:28 +00:00
|
|
|
self.expected_tokens.push(TokenType::Token(token::Lt));
|
2019-06-04 22:17:07 +00:00
|
|
|
let ate = match self.token.kind {
|
2015-12-30 23:11:53 +00:00
|
|
|
token::Lt => {
|
|
|
|
self.bump();
|
|
|
|
true
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Shl) => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2017-07-31 20:04:34 +00:00
|
|
|
self.bump_with(token::Lt, span);
|
2015-12-30 23:11:53 +00:00
|
|
|
true
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
2019-02-07 09:10:11 +00:00
|
|
|
token::LArrow => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2019-02-07 09:10:11 +00:00
|
|
|
self.bump_with(token::BinOp(token::Minus), span);
|
|
|
|
true
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
_ => false,
|
2019-01-23 01:35:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ate {
|
|
|
|
// See doc comment for `unmatched_angle_bracket_count`.
|
|
|
|
self.unmatched_angle_bracket_count += 1;
|
2019-01-28 05:04:50 +00:00
|
|
|
self.max_angle_bracket_count += 1;
|
2019-01-23 01:35:13 +00:00
|
|
|
debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
2019-01-23 01:35:13 +00:00
|
|
|
|
|
|
|
ate
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
fn expect_lt(&mut self) -> PResult<'a, ()> {
|
2015-12-30 23:11:53 +00:00
|
|
|
if !self.eat_lt() {
|
|
|
|
self.unexpected()
|
2015-03-28 21:58:51 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes a single `>` token. if a `>>` is seen, replaces it
|
|
|
|
/// with a single `>` and continues. If a `>` is not seen, signals an error.
|
2018-05-31 22:53:30 +00:00
|
|
|
fn expect_gt(&mut self) -> PResult<'a, ()> {
|
2015-01-16 03:04:28 +00:00
|
|
|
self.expected_tokens.push(TokenType::Token(token::Gt));
|
2019-06-04 22:17:07 +00:00
|
|
|
let ate = match self.token.kind {
|
2015-12-30 23:11:53 +00:00
|
|
|
token::Gt => {
|
|
|
|
self.bump();
|
2019-01-23 01:35:13 +00:00
|
|
|
Some(())
|
2015-12-30 23:11:53 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Shr) => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2019-01-23 01:35:13 +00:00
|
|
|
Some(self.bump_with(token::Gt, span))
|
2013-12-30 23:17:53 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOpEq(token::Shr) => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2019-01-23 01:35:13 +00:00
|
|
|
Some(self.bump_with(token::Ge, span))
|
2014-06-20 16:53:12 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ge => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
|
2019-01-23 01:35:13 +00:00
|
|
|
Some(self.bump_with(token::Eq, span))
|
2014-06-20 16:53:12 +00:00
|
|
|
}
|
2019-01-23 01:35:13 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match ate {
|
2019-01-28 05:04:50 +00:00
|
|
|
Some(_) => {
|
2019-01-23 01:35:13 +00:00
|
|
|
// See doc comment for `unmatched_angle_bracket_count`.
|
2019-02-22 19:17:30 +00:00
|
|
|
if self.unmatched_angle_bracket_count > 0 {
|
|
|
|
self.unmatched_angle_bracket_count -= 1;
|
|
|
|
debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
|
|
|
|
}
|
2019-01-23 01:35:13 +00:00
|
|
|
|
2019-01-28 05:04:50 +00:00
|
|
|
Ok(())
|
2019-01-23 01:35:13 +00:00
|
|
|
},
|
|
|
|
None => self.unexpected(),
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses a sequence, including the closing delimiter. The function
|
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
2014-06-09 20:12:30 +00:00
|
|
|
/// closing bracket.
|
2019-07-09 08:27:07 +00:00
|
|
|
pub fn parse_seq_to_end<T>(
|
|
|
|
&mut self,
|
|
|
|
ket: &TokenKind,
|
|
|
|
sep: SeqSep,
|
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, Vec<T>> {
|
|
|
|
let (val, _, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
|
2019-01-28 05:04:50 +00:00
|
|
|
if !recovered {
|
|
|
|
self.bump();
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
Ok(val)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses a sequence, not including the closing delimiter. The function
|
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
2014-06-09 20:12:30 +00:00
|
|
|
/// closing bracket.
|
2019-07-09 08:27:07 +00:00
|
|
|
pub fn parse_seq_to_before_end<T>(
|
2019-01-28 05:04:50 +00:00
|
|
|
&mut self,
|
2019-06-05 11:17:56 +00:00
|
|
|
ket: &TokenKind,
|
2019-01-28 05:04:50 +00:00
|
|
|
sep: SeqSep,
|
2019-07-09 08:27:07 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, (Vec<T>, bool, bool)> {
|
2017-10-22 16:19:30 +00:00
|
|
|
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
|
2016-01-31 19:39:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:27:07 +00:00
|
|
|
fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
|
|
|
|
kets.iter().any(|k| {
|
|
|
|
match expect {
|
|
|
|
TokenExpectType::Expect => self.check(k),
|
|
|
|
TokenExpectType::NoExpect => self.token == **k,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn parse_seq_to_before_tokens<T>(
|
2018-08-20 23:16:17 +00:00
|
|
|
&mut self,
|
2019-06-05 11:17:56 +00:00
|
|
|
kets: &[&TokenKind],
|
2018-08-20 23:16:17 +00:00
|
|
|
sep: SeqSep,
|
|
|
|
expect: TokenExpectType,
|
2019-07-09 08:27:07 +00:00
|
|
|
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, (Vec<T>, bool /* trailing */, bool /* recovered */)> {
|
2019-01-28 05:04:50 +00:00
|
|
|
let mut first = true;
|
|
|
|
let mut recovered = false;
|
2019-07-09 08:27:07 +00:00
|
|
|
let mut trailing = false;
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut v = vec![];
|
2019-07-09 08:27:07 +00:00
|
|
|
while !self.expect_any_with_type(kets, expect) {
|
|
|
|
if let token::CloseDelim(..) | token::Eof = self.token.kind {
|
|
|
|
break
|
|
|
|
}
|
2017-05-12 18:05:39 +00:00
|
|
|
if let Some(ref t) = sep.sep {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2019-01-28 05:04:50 +00:00
|
|
|
match self.expect(t) {
|
|
|
|
Ok(false) => {}
|
|
|
|
Ok(true) => {
|
|
|
|
recovered = true;
|
|
|
|
break;
|
2017-10-24 13:04:01 +00:00
|
|
|
}
|
2019-01-28 05:04:50 +00:00
|
|
|
Err(mut e) => {
|
|
|
|
// Attempt to keep parsing if it was a similar separator
|
|
|
|
if let Some(ref tokens) = t.similar_tokens() {
|
2019-06-08 19:38:23 +00:00
|
|
|
if tokens.contains(&self.token.kind) {
|
2019-01-28 05:04:50 +00:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.emit();
|
|
|
|
// Attempt to keep parsing if it was an omitted separator
|
|
|
|
match f(self) {
|
|
|
|
Ok(t) => {
|
|
|
|
v.push(t);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
break;
|
|
|
|
}
|
2017-10-24 13:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2019-07-09 08:27:07 +00:00
|
|
|
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
|
|
|
|
trailing = true;
|
2016-01-31 19:39:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
|
2017-10-22 16:19:30 +00:00
|
|
|
let t = f(self)?;
|
|
|
|
v.push(t);
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
|
2019-07-09 08:27:07 +00:00
|
|
|
Ok((v, trailing, recovered))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses a sequence, including the closing delimiter. The function
|
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
2014-06-09 20:12:30 +00:00
|
|
|
/// closing bracket.
|
2019-07-09 08:27:07 +00:00
|
|
|
fn parse_unspanned_seq<T>(
|
2019-01-28 05:04:50 +00:00
|
|
|
&mut self,
|
2019-06-05 11:17:56 +00:00
|
|
|
bra: &TokenKind,
|
|
|
|
ket: &TokenKind,
|
2019-01-28 05:04:50 +00:00
|
|
|
sep: SeqSep,
|
2019-07-09 08:27:07 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, (Vec<T>, bool)> {
|
2016-03-23 03:01:37 +00:00
|
|
|
self.expect(bra)?;
|
2019-07-09 08:27:07 +00:00
|
|
|
let (result, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
|
2019-01-28 05:04:50 +00:00
|
|
|
if !recovered {
|
|
|
|
self.eat(ket);
|
|
|
|
}
|
2019-07-09 08:27:07 +00:00
|
|
|
Ok((result, trailing))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:31:24 +00:00
|
|
|
fn parse_delim_comma_seq<T>(
|
|
|
|
&mut self,
|
|
|
|
delim: DelimToken,
|
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, (Vec<T>, bool)> {
|
|
|
|
self.parse_unspanned_seq(
|
|
|
|
&token::OpenDelim(delim),
|
|
|
|
&token::CloseDelim(delim),
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
|
|
|
f,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_paren_comma_seq<T>(
|
|
|
|
&mut self,
|
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, (Vec<T>, bool)> {
|
|
|
|
self.parse_delim_comma_seq(token::Paren, f)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Advance the parser by one token
|
2015-12-30 23:11:53 +00:00
|
|
|
pub fn bump(&mut self) {
|
2016-09-21 02:16:28 +00:00
|
|
|
if self.prev_token_kind == PrevTokenKind::Eof {
|
2016-03-25 22:13:54 +00:00
|
|
|
// Bumping after EOF is a bad sign, usually an infinite loop.
|
|
|
|
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:31:13 +00:00
|
|
|
self.prev_span = self.meta_var_span.take().unwrap_or(self.token.span);
|
2016-09-16 05:46:40 +00:00
|
|
|
|
|
|
|
// Record last token kind for possible error recovery.
|
2019-06-04 22:17:07 +00:00
|
|
|
self.prev_token_kind = match self.token.kind {
|
2016-09-21 02:16:28 +00:00
|
|
|
token::DocComment(..) => PrevTokenKind::DocComment,
|
|
|
|
token::Comma => PrevTokenKind::Comma,
|
2017-04-04 22:12:53 +00:00
|
|
|
token::BinOp(token::Plus) => PrevTokenKind::Plus,
|
2019-04-23 02:37:23 +00:00
|
|
|
token::BinOp(token::Or) => PrevTokenKind::BitOr,
|
2016-09-21 02:16:28 +00:00
|
|
|
token::Interpolated(..) => PrevTokenKind::Interpolated,
|
|
|
|
token::Eof => PrevTokenKind::Eof,
|
2017-07-04 14:04:34 +00:00
|
|
|
token::Ident(..) => PrevTokenKind::Ident,
|
2016-09-21 02:16:28 +00:00
|
|
|
_ => PrevTokenKind::Other,
|
2013-08-05 20:18:29 +00:00
|
|
|
};
|
2016-09-16 05:46:40 +00:00
|
|
|
|
2019-06-04 22:17:07 +00:00
|
|
|
self.token = self.next_tok();
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
self.expected_tokens.clear();
|
2015-01-02 22:00:06 +00:00
|
|
|
// check after each token
|
2017-03-29 07:17:18 +00:00
|
|
|
self.process_potential_macro_variable();
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
|
2016-02-06 17:42:17 +00:00
|
|
|
/// Advance the parser using provided token as a next one. Use this when
|
|
|
|
/// consuming a part of a token. For example a single `<` from `<<`.
|
2019-06-05 11:17:56 +00:00
|
|
|
fn bump_with(&mut self, next: TokenKind, span: Span) {
|
2019-06-07 10:31:13 +00:00
|
|
|
self.prev_span = self.token.span.with_hi(span.lo());
|
2016-09-16 05:46:40 +00:00
|
|
|
// It would be incorrect to record the kind of the current token, but
|
|
|
|
// fortunately for tokens currently using `bump_with`, the
|
2016-09-21 02:16:28 +00:00
|
|
|
// prev_token_kind will be of no use anyway.
|
|
|
|
self.prev_token_kind = PrevTokenKind::Other;
|
2019-06-05 06:39:34 +00:00
|
|
|
self.token = Token::new(next, span);
|
2016-02-06 17:42:17 +00:00
|
|
|
self.expected_tokens.clear();
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2016-02-06 17:42:17 +00:00
|
|
|
|
2019-05-23 20:10:24 +00:00
|
|
|
pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
|
2019-06-05 06:39:34 +00:00
|
|
|
F: FnOnce(&Token) -> R,
|
2019-05-23 20:10:24 +00:00
|
|
|
{
|
|
|
|
if dist == 0 {
|
2019-06-04 22:17:07 +00:00
|
|
|
return f(&self.token);
|
2019-05-23 20:10:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 22:17:07 +00:00
|
|
|
let frame = &self.token_cursor.frame;
|
|
|
|
f(&match frame.tree_cursor.look_ahead(dist - 1) {
|
2019-05-23 20:10:24 +00:00
|
|
|
Some(tree) => match tree {
|
2019-06-04 22:17:07 +00:00
|
|
|
TokenTree::Token(token) => token,
|
2019-06-05 11:17:56 +00:00
|
|
|
TokenTree::Delimited(dspan, delim, _) =>
|
|
|
|
Token::new(token::OpenDelim(delim), dspan.open),
|
2019-06-04 22:17:07 +00:00
|
|
|
}
|
2019-06-05 06:39:34 +00:00
|
|
|
None => Token::new(token::CloseDelim(frame.delim), frame.span.close)
|
2019-05-23 20:10:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:58:44 +00:00
|
|
|
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
|
|
|
|
fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
|
|
|
|
self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses asyncness: `async` or nothing.
|
2018-06-19 04:18:10 +00:00
|
|
|
fn parse_asyncness(&mut self) -> IsAsync {
|
2019-05-11 14:41:37 +00:00
|
|
|
if self.eat_keyword(kw::Async) {
|
2018-06-26 09:56:24 +00:00
|
|
|
IsAsync::Async {
|
|
|
|
closure_id: ast::DUMMY_NODE_ID,
|
|
|
|
return_impl_trait_id: ast::DUMMY_NODE_ID,
|
|
|
|
}
|
2018-06-19 04:18:10 +00:00
|
|
|
} else {
|
|
|
|
IsAsync::NotAsync
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses unsafety: `unsafe` or nothing.
|
2017-12-02 19:15:03 +00:00
|
|
|
fn parse_unsafety(&mut self) -> Unsafety {
|
2019-05-11 14:41:37 +00:00
|
|
|
if self.eat_keyword(kw::Unsafe) {
|
2017-12-02 19:15:03 +00:00
|
|
|
Unsafety::Unsafe
|
2013-02-01 01:12:29 +00:00
|
|
|
} else {
|
2017-12-02 19:15:03 +00:00
|
|
|
Unsafety::Normal
|
2013-02-01 01:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
|
2019-05-12 00:00:06 +00:00
|
|
|
fn is_named_argument(&self) -> bool {
|
2019-06-04 22:17:07 +00:00
|
|
|
let offset = match self.token.kind {
|
2019-02-14 22:10:02 +00:00
|
|
|
token::Interpolated(ref nt) => match **nt {
|
2017-11-05 01:22:18 +00:00
|
|
|
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
token::BinOp(token::And) | token::AndAnd => 1,
|
2019-05-11 14:41:37 +00:00
|
|
|
_ if self.token.is_keyword(kw::Mut) => 1,
|
2017-11-05 01:22:18 +00:00
|
|
|
_ => 0,
|
2013-07-18 03:04:37 +00:00
|
|
|
};
|
|
|
|
|
2018-03-08 11:27:23 +00:00
|
|
|
self.look_ahead(offset, |t| t.is_ident()) &&
|
2017-11-05 01:22:18 +00:00
|
|
|
self.look_ahead(offset + 1, |t| t == &token::Colon)
|
2012-09-26 08:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Skips unexpected attributes and doc comments in this position and emits an appropriate
|
|
|
|
/// error.
|
|
|
|
/// This version of parse arg doesn't necessarily require identifier names.
|
2019-06-09 10:58:40 +00:00
|
|
|
fn parse_arg_general<F>(
|
2019-05-23 19:54:27 +00:00
|
|
|
&mut self,
|
|
|
|
is_trait_item: bool,
|
|
|
|
allow_c_variadic: bool,
|
2019-06-09 10:58:40 +00:00
|
|
|
is_name_required: F,
|
|
|
|
) -> PResult<'a, Arg>
|
|
|
|
where
|
|
|
|
F: Fn(&token::Token) -> bool
|
|
|
|
{
|
2019-07-26 22:52:37 +00:00
|
|
|
let lo = self.token.span;
|
2019-06-09 10:58:40 +00:00
|
|
|
let attrs = self.parse_arg_attributes()?;
|
2019-07-14 00:34:06 +00:00
|
|
|
if let Some(mut arg) = self.parse_self_arg()? {
|
2019-06-09 10:58:40 +00:00
|
|
|
arg.attrs = attrs.into();
|
2019-05-23 19:54:27 +00:00
|
|
|
return self.recover_bad_self_arg(arg, is_trait_item);
|
2018-11-16 18:27:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
let is_name_required = is_name_required(&self.token);
|
|
|
|
let (pat, ty) = if is_name_required || self.is_named_argument() {
|
|
|
|
debug!("parse_arg_general parse_pat (is_name_required:{})", is_name_required);
|
2018-08-09 22:23:08 +00:00
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
let pat = self.parse_pat(Some("argument name"))?;
|
2018-10-04 02:21:05 +00:00
|
|
|
if let Err(mut err) = self.expect(&token::Colon) {
|
2019-05-29 22:25:46 +00:00
|
|
|
if let Some(ident) = self.argument_without_type(
|
|
|
|
&mut err,
|
|
|
|
pat,
|
2019-06-09 10:58:40 +00:00
|
|
|
is_name_required,
|
2019-05-29 22:25:46 +00:00
|
|
|
is_trait_item,
|
|
|
|
) {
|
|
|
|
err.emit();
|
|
|
|
return Ok(dummy_arg(ident));
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
|
|
|
}
|
2018-10-04 02:21:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
self.eat_incorrect_doc_comment_for_arg_type();
|
2019-02-08 17:30:42 +00:00
|
|
|
(pat, self.parse_ty_common(true, true, allow_c_variadic)?)
|
2018-08-09 22:23:08 +00:00
|
|
|
} else {
|
|
|
|
debug!("parse_arg_general ident_to_pat");
|
2018-09-21 01:26:36 +00:00
|
|
|
let parser_snapshot_before_ty = self.clone();
|
2019-06-09 10:58:40 +00:00
|
|
|
self.eat_incorrect_doc_comment_for_arg_type();
|
2019-02-08 17:30:42 +00:00
|
|
|
let mut ty = self.parse_ty_common(true, true, allow_c_variadic);
|
2019-01-01 16:14:00 +00:00
|
|
|
if ty.is_ok() && self.token != token::Comma &&
|
|
|
|
self.token != token::CloseDelim(token::Paren) {
|
2018-09-21 01:26:36 +00:00
|
|
|
// This wasn't actually a type, but a pattern looking like a type,
|
|
|
|
// so we are going to rollback and re-parse for recovery.
|
|
|
|
ty = self.unexpected();
|
2018-08-20 00:51:02 +00:00
|
|
|
}
|
2018-09-21 01:26:36 +00:00
|
|
|
match ty {
|
|
|
|
Ok(ty) => {
|
2019-05-11 14:41:37 +00:00
|
|
|
let ident = Ident::new(kw::Invalid, self.prev_span);
|
2019-08-11 13:24:37 +00:00
|
|
|
let bm = BindingMode::ByValue(Mutability::Immutable);
|
|
|
|
let pat = self.mk_pat_ident(ty.span, bm, ident);
|
2018-09-21 01:26:36 +00:00
|
|
|
(pat, ty)
|
|
|
|
}
|
|
|
|
Err(mut err) => {
|
2019-02-08 17:30:42 +00:00
|
|
|
// If this is a C-variadic argument and we hit an error, return the
|
2018-11-30 15:53:44 +00:00
|
|
|
// error.
|
|
|
|
if self.token == token::DotDotDot {
|
|
|
|
return Err(err);
|
|
|
|
}
|
2018-09-21 01:26:36 +00:00
|
|
|
// Recover from attempting to parse the argument as a type without pattern.
|
|
|
|
err.cancel();
|
|
|
|
mem::replace(self, parser_snapshot_before_ty);
|
2019-05-23 19:55:26 +00:00
|
|
|
self.recover_arg_parse()?
|
2018-08-09 22:23:08 +00:00
|
|
|
}
|
2018-08-04 01:23:21 +00:00
|
|
|
}
|
2018-08-09 22:23:08 +00:00
|
|
|
};
|
|
|
|
|
2019-07-26 22:52:37 +00:00
|
|
|
let span = lo.to(self.token.span);
|
|
|
|
|
|
|
|
Ok(Arg { attrs: attrs.into(), id: ast::DUMMY_NODE_ID, pat, span, ty })
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses mutability (`mut` or nothing).
|
2017-03-16 21:47:32 +00:00
|
|
|
fn parse_mutability(&mut self) -> Mutability {
|
2019-05-11 14:41:37 +00:00
|
|
|
if self.eat_keyword(kw::Mut) {
|
2017-03-16 21:47:32 +00:00
|
|
|
Mutability::Mutable
|
2012-04-24 22:52:52 +00:00
|
|
|
} else {
|
2017-03-16 21:47:32 +00:00
|
|
|
Mutability::Immutable
|
2012-04-24 22:52:52 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
fn parse_field_name(&mut self) -> PResult<'a, Ident> {
|
2019-06-05 11:17:56 +00:00
|
|
|
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
|
|
|
|
self.token.kind {
|
2019-06-07 10:31:13 +00:00
|
|
|
self.expect_no_suffix(self.token.span, "a tuple index", suffix);
|
2016-07-29 20:47:55 +00:00
|
|
|
self.bump();
|
2019-05-18 22:04:26 +00:00
|
|
|
Ok(Ident::new(symbol, self.prev_span))
|
2016-07-29 20:47:55 +00:00
|
|
|
} else {
|
2018-01-06 22:43:20 +00:00
|
|
|
self.parse_ident_common(false)
|
2016-07-29 20:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 05:53:14 +00:00
|
|
|
fn expect_delimited_token_tree(&mut self) -> PResult<'a, (MacDelimiter, TokenStream)> {
|
2019-06-04 22:17:07 +00:00
|
|
|
let delim = match self.token.kind {
|
2018-05-22 15:01:21 +00:00
|
|
|
token::OpenDelim(delim) => delim,
|
2018-02-19 07:08:23 +00:00
|
|
|
_ => {
|
|
|
|
let msg = "expected open delimiter";
|
|
|
|
let mut err = self.fatal(msg);
|
2019-06-07 10:31:13 +00:00
|
|
|
err.span_label(self.token.span, msg);
|
2018-05-22 15:01:21 +00:00
|
|
|
return Err(err)
|
2018-02-19 07:08:23 +00:00
|
|
|
}
|
2018-05-22 15:01:21 +00:00
|
|
|
};
|
2018-11-29 23:02:04 +00:00
|
|
|
let tts = match self.parse_token_tree() {
|
|
|
|
TokenTree::Delimited(_, _, tts) => tts,
|
2018-05-22 15:01:21 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let delim = match delim {
|
|
|
|
token::Paren => MacDelimiter::Parenthesis,
|
|
|
|
token::Bracket => MacDelimiter::Bracket,
|
|
|
|
token::Brace => MacDelimiter::Brace,
|
|
|
|
token::NoDelim => self.bug("unexpected no delimiter"),
|
|
|
|
};
|
2019-01-09 05:53:14 +00:00
|
|
|
Ok((delim, tts.into()))
|
2014-10-29 14:47:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
fn parse_or_use_outer_attributes(&mut self,
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
|
|
|
-> PResult<'a, ThinVec<Attribute>> {
|
|
|
|
if let Some(attrs) = already_parsed_attrs {
|
|
|
|
Ok(attrs)
|
|
|
|
} else {
|
|
|
|
self.parse_outer_attributes().map(|a| a.into())
|
2019-06-07 02:08:38 +00:00
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
}
|
2019-06-07 02:08:38 +00:00
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
crate fn process_potential_macro_variable(&mut self) {
|
|
|
|
self.token = match self.token.kind {
|
|
|
|
token::Dollar if self.token.span.ctxt() != SyntaxContext::empty() &&
|
|
|
|
self.look_ahead(1, |t| t.is_ident()) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-08-11 11:14:30 +00:00
|
|
|
let name = match self.token.kind {
|
|
|
|
token::Ident(name, _) => name,
|
|
|
|
_ => unreachable!()
|
2017-12-16 22:53:11 +00:00
|
|
|
};
|
2019-08-11 11:14:30 +00:00
|
|
|
let span = self.prev_span.to(self.token.span);
|
|
|
|
self.diagnostic()
|
|
|
|
.struct_span_fatal(span, &format!("unknown macro variable `{}`", name))
|
|
|
|
.span_label(span, "unknown macro variable")
|
|
|
|
.emit();
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-08-11 11:14:30 +00:00
|
|
|
return
|
2014-08-06 02:44:21 +00:00
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
token::Interpolated(ref nt) => {
|
|
|
|
self.meta_var_span = Some(self.token.span);
|
|
|
|
// Interpolated identifier and lifetime tokens are replaced with usual identifier
|
|
|
|
// and lifetime tokens, so the former are never encountered during normal parsing.
|
|
|
|
match **nt {
|
|
|
|
token::NtIdent(ident, is_raw) =>
|
|
|
|
Token::new(token::Ident(ident.name, is_raw), ident.span),
|
|
|
|
token::NtLifetime(ident) =>
|
|
|
|
Token::new(token::Lifetime(ident.name), ident.span),
|
|
|
|
_ => return,
|
2017-02-17 23:12:47 +00:00
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
}
|
2019-06-03 05:34:54 +00:00
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
/// Parses a single token tree from the input.
|
|
|
|
crate fn parse_token_tree(&mut self) -> TokenTree {
|
|
|
|
match self.token.kind {
|
|
|
|
token::OpenDelim(..) => {
|
|
|
|
let frame = mem::replace(&mut self.token_cursor.frame,
|
|
|
|
self.token_cursor.stack.pop().unwrap());
|
|
|
|
self.token.span = frame.span.entire();
|
|
|
|
self.bump();
|
|
|
|
TokenTree::Delimited(
|
|
|
|
frame.span,
|
|
|
|
frame.delim,
|
|
|
|
frame.tree_cursor.stream.into(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
token::CloseDelim(_) | token::Eof => unreachable!(),
|
|
|
|
_ => {
|
|
|
|
let token = self.token.take();
|
|
|
|
self.bump();
|
|
|
|
TokenTree::Token(token)
|
2014-07-06 21:29:29 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
/// Parses a stream of tokens into a list of `TokenTree`s, up to EOF.
|
|
|
|
pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
|
|
|
|
let mut tts = Vec::new();
|
|
|
|
while self.token != token::Eof {
|
|
|
|
tts.push(self.parse_token_tree());
|
|
|
|
}
|
|
|
|
Ok(tts)
|
|
|
|
}
|
2019-04-18 20:58:57 +00:00
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
pub fn parse_tokens(&mut self) -> TokenStream {
|
|
|
|
let mut result = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.token.kind {
|
|
|
|
token::Eof | token::CloseDelim(..) => break,
|
|
|
|
_ => result.push(self.parse_token_tree().into()),
|
2019-04-15 00:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
TokenStream::new(result)
|
2019-04-15 00:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
/// Evaluates the closure with restrictions in place.
|
|
|
|
///
|
|
|
|
/// Afters the closure is evaluated, restrictions are reset.
|
|
|
|
fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
|
|
|
|
where F: FnOnce(&mut Self) -> T
|
|
|
|
{
|
|
|
|
let old = self.restrictions;
|
|
|
|
self.restrictions = r;
|
|
|
|
let r = f(self);
|
|
|
|
self.restrictions = old;
|
|
|
|
return r;
|
2016-09-22 04:45:29 +00:00
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
2019-02-08 17:30:42 +00:00
|
|
|
fn parse_fn_args(&mut self, named_args: bool, allow_c_variadic: bool)
|
2015-12-20 21:00:43 +00:00
|
|
|
-> PResult<'a, (Vec<Arg> , bool)> {
|
2019-06-07 10:31:13 +00:00
|
|
|
let sp = self.token.span;
|
2019-02-08 17:30:42 +00:00
|
|
|
let mut c_variadic = false;
|
2019-07-09 08:34:53 +00:00
|
|
|
let (args, _): (Vec<Option<Arg>>, _) = self.parse_paren_comma_seq(|p| {
|
|
|
|
let do_not_enforce_named_arguments_for_c_variadic =
|
|
|
|
|token: &token::Token| -> bool {
|
|
|
|
if token == &token::DotDotDot {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
named_args
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match p.parse_arg_general(
|
|
|
|
false,
|
|
|
|
allow_c_variadic,
|
|
|
|
do_not_enforce_named_arguments_for_c_variadic
|
|
|
|
) {
|
|
|
|
Ok(arg) => {
|
|
|
|
if let TyKind::CVarArgs = arg.ty.node {
|
|
|
|
c_variadic = true;
|
|
|
|
if p.token != token::CloseDelim(token::Paren) {
|
|
|
|
let span = p.token.span;
|
|
|
|
p.span_err(span,
|
|
|
|
"`...` must be the last argument of a C-variadic function");
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Ok(Some(arg))
|
2016-01-31 19:39:50 +00:00
|
|
|
}
|
2019-07-09 08:34:53 +00:00
|
|
|
} else {
|
|
|
|
Ok(Some(arg))
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
2019-07-09 08:34:53 +00:00
|
|
|
},
|
|
|
|
Err(mut e) => {
|
|
|
|
e.emit();
|
|
|
|
let lo = p.prev_span;
|
|
|
|
// Skip every token until next possible arg or end.
|
|
|
|
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
|
|
|
|
// Create a placeholder argument for proper arg count (issue #34264).
|
|
|
|
let span = lo.to(p.prev_span);
|
|
|
|
Ok(Some(dummy_arg(Ident::new(kw::Invalid, span))))
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
2019-07-09 08:34:53 +00:00
|
|
|
}
|
|
|
|
})?;
|
2018-11-16 12:54:49 +00:00
|
|
|
|
2016-02-10 03:11:27 +00:00
|
|
|
let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
|
|
|
|
|
2019-08-11 05:25:30 +00:00
|
|
|
if c_variadic && args.len() <= 1 {
|
2013-10-25 05:56:34 +00:00
|
|
|
self.span_err(sp,
|
2019-02-08 17:30:42 +00:00
|
|
|
"C-variadic function must be declared with at least one named argument");
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 17:30:42 +00:00
|
|
|
Ok((args, c_variadic))
|
2013-10-25 05:56:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
/// Returns the parsed optional self argument and whether a self shortcut was used.
|
2019-06-09 10:58:40 +00:00
|
|
|
///
|
|
|
|
/// See `parse_self_arg_with_attrs` to collect attributes.
|
2016-03-06 12:54:44 +00:00
|
|
|
fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
|
2019-06-04 22:17:07 +00:00
|
|
|
let expect_ident = |this: &mut Self| match this.token.kind {
|
2016-03-06 12:54:44 +00:00
|
|
|
// Preserve hygienic context.
|
2019-06-05 08:56:06 +00:00
|
|
|
token::Ident(name, _) =>
|
2019-06-07 10:31:13 +00:00
|
|
|
{ let span = this.token.span; this.bump(); Ident::new(name, span) }
|
2016-05-08 18:18:21 +00:00
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2016-10-19 20:33:41 +00:00
|
|
|
let isolated_self = |this: &mut Self, n| {
|
2019-05-11 14:41:37 +00:00
|
|
|
this.look_ahead(n, |t| t.is_keyword(kw::SelfLower)) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
this.look_ahead(n + 1, |t| t != &token::ModSep)
|
|
|
|
};
|
2013-03-10 00:43:53 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Parse optional `self` parameter of a method.
|
|
|
|
// Only a limited set of initial token sequences is considered `self` parameters; anything
|
2016-05-08 18:18:21 +00:00
|
|
|
// else is parsed as a normal function parameter list, so some lookahead is required.
|
2019-06-07 10:31:13 +00:00
|
|
|
let eself_lo = self.token.span;
|
2019-06-04 22:17:07 +00:00
|
|
|
let (eself, eself_ident, eself_hi) = match self.token.kind {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::And) => {
|
2019-02-28 22:43:53 +00:00
|
|
|
// `&self`
|
|
|
|
// `&mut self`
|
|
|
|
// `&'lt self`
|
|
|
|
// `&'lt mut self`
|
|
|
|
// `¬_self`
|
2018-05-25 18:33:15 +00:00
|
|
|
(if isolated_self(self, 1) {
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Region(None, Mutability::Immutable)
|
2019-05-29 15:58:44 +00:00
|
|
|
} else if self.is_keyword_ahead(1, &[kw::Mut]) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
isolated_self(self, 2) {
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Region(None, Mutability::Mutable)
|
2016-05-08 18:18:21 +00:00
|
|
|
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
isolated_self(self, 2) {
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
2017-03-16 21:47:32 +00:00
|
|
|
let lt = self.expect_lifetime();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Region(Some(lt), Mutability::Immutable)
|
2016-05-08 18:18:21 +00:00
|
|
|
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
|
2019-05-29 15:58:44 +00:00
|
|
|
self.is_keyword_ahead(2, &[kw::Mut]) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
isolated_self(self, 3) {
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
2017-03-16 21:47:32 +00:00
|
|
|
let lt = self.expect_lifetime();
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Region(Some(lt), Mutability::Mutable)
|
2016-05-08 18:18:21 +00:00
|
|
|
} else {
|
2016-03-06 12:54:44 +00:00
|
|
|
return Ok(None);
|
2018-05-25 18:33:15 +00:00
|
|
|
}, expect_ident(self), self.prev_span)
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Star) => {
|
2019-02-28 22:43:53 +00:00
|
|
|
// `*self`
|
|
|
|
// `*const self`
|
|
|
|
// `*mut self`
|
|
|
|
// `*not_self`
|
2016-05-08 18:18:21 +00:00
|
|
|
// Emit special error for `self` cases.
|
2019-01-12 06:04:54 +00:00
|
|
|
let msg = "cannot pass `self` by raw pointer";
|
2018-05-25 18:33:15 +00:00
|
|
|
(if isolated_self(self, 1) {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-06-07 10:31:13 +00:00
|
|
|
self.struct_span_err(self.token.span, msg)
|
|
|
|
.span_label(self.token.span, msg)
|
2019-01-12 06:04:54 +00:00
|
|
|
.emit();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Value(Mutability::Immutable)
|
2016-05-08 18:18:21 +00:00
|
|
|
} else if self.look_ahead(1, |t| t.is_mutability()) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
isolated_self(self, 2) {
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2019-06-07 10:31:13 +00:00
|
|
|
self.struct_span_err(self.token.span, msg)
|
|
|
|
.span_label(self.token.span, msg)
|
2019-01-12 06:04:54 +00:00
|
|
|
.emit();
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Value(Mutability::Immutable)
|
2016-05-08 18:18:21 +00:00
|
|
|
} else {
|
2016-03-06 12:54:44 +00:00
|
|
|
return Ok(None);
|
2018-05-25 18:33:15 +00:00
|
|
|
}, expect_ident(self), self.prev_span)
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(..) => {
|
2016-10-19 20:33:41 +00:00
|
|
|
if isolated_self(self, 0) {
|
2019-02-28 22:43:53 +00:00
|
|
|
// `self`
|
|
|
|
// `self: TYPE`
|
2016-05-08 18:18:21 +00:00
|
|
|
let eself_ident = expect_ident(self);
|
2018-05-25 18:33:15 +00:00
|
|
|
let eself_hi = self.prev_span;
|
|
|
|
(if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
let ty = self.parse_ty()?;
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Explicit(ty, Mutability::Immutable)
|
librustc: Disallow mutation and assignment in pattern guards, and modify
the CFG for match statements.
There were two bugs in issue #14684. One was simply that the borrow
check didn't know about the correct CFG for match statements: the
pattern must be a predecessor of the guard. This disallows the bad
behavior if there are bindings in the pattern. But it isn't enough to
prevent the memory safety problem, because of wildcards; thus, this
patch introduces a more restrictive rule, which disallows assignments
and mutable borrows inside guards outright.
I discussed this with Niko and we decided this was the best plan of
action.
This breaks code that performs mutable borrows in pattern guards. Most
commonly, the code looks like this:
impl Foo {
fn f(&mut self, ...) {}
fn g(&mut self, ...) {
match bar {
Baz if self.f(...) => { ... }
_ => { ... }
}
}
}
Change this code to not use a guard. For example:
impl Foo {
fn f(&mut self, ...) {}
fn g(&mut self, ...) {
match bar {
Baz => {
if self.f(...) {
...
} else {
...
}
}
_ => { ... }
}
}
}
Sometimes this can result in code duplication, but often it illustrates
a hidden memory safety problem.
Closes #14684.
[breaking-change]
2014-07-25 22:18:19 +00:00
|
|
|
} else {
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Value(Mutability::Immutable)
|
|
|
|
}, eself_ident, eself_hi)
|
2019-05-11 14:41:37 +00:00
|
|
|
} else if self.token.is_keyword(kw::Mut) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
isolated_self(self, 1) {
|
2019-02-28 22:43:53 +00:00
|
|
|
// `mut self`
|
|
|
|
// `mut self: TYPE`
|
2016-05-08 18:18:21 +00:00
|
|
|
self.bump();
|
|
|
|
let eself_ident = expect_ident(self);
|
2018-05-25 18:33:15 +00:00
|
|
|
let eself_hi = self.prev_span;
|
|
|
|
(if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
let ty = self.parse_ty()?;
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Explicit(ty, Mutability::Mutable)
|
librustc: Disallow mutation and assignment in pattern guards, and modify
the CFG for match statements.
There were two bugs in issue #14684. One was simply that the borrow
check didn't know about the correct CFG for match statements: the
pattern must be a predecessor of the guard. This disallows the bad
behavior if there are bindings in the pattern. But it isn't enough to
prevent the memory safety problem, because of wildcards; thus, this
patch introduces a more restrictive rule, which disallows assignments
and mutable borrows inside guards outright.
I discussed this with Niko and we decided this was the best plan of
action.
This breaks code that performs mutable borrows in pattern guards. Most
commonly, the code looks like this:
impl Foo {
fn f(&mut self, ...) {}
fn g(&mut self, ...) {
match bar {
Baz if self.f(...) => { ... }
_ => { ... }
}
}
}
Change this code to not use a guard. For example:
impl Foo {
fn f(&mut self, ...) {}
fn g(&mut self, ...) {
match bar {
Baz => {
if self.f(...) {
...
} else {
...
}
}
_ => { ... }
}
}
}
Sometimes this can result in code duplication, but often it illustrates
a hidden memory safety problem.
Closes #14684.
[breaking-change]
2014-07-25 22:18:19 +00:00
|
|
|
} else {
|
2018-05-25 18:33:15 +00:00
|
|
|
SelfKind::Value(Mutability::Mutable)
|
|
|
|
}, eself_ident, eself_hi)
|
2014-05-06 23:37:32 +00:00
|
|
|
} else {
|
2016-03-06 12:54:44 +00:00
|
|
|
return Ok(None);
|
2014-05-06 23:37:32 +00:00
|
|
|
}
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
_ => return Ok(None),
|
2012-08-17 22:25:35 +00:00
|
|
|
};
|
2016-03-06 12:54:44 +00:00
|
|
|
|
2018-08-18 10:14:03 +00:00
|
|
|
let eself = source_map::respan(eself_lo.to(eself_hi), eself);
|
2019-06-09 10:58:40 +00:00
|
|
|
Ok(Some(Arg::from_self(ThinVec::default(), eself, eself_ident)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the parsed optional self argument with attributes and whether a self
|
|
|
|
/// shortcut was used.
|
|
|
|
fn parse_self_arg_with_attrs(&mut self) -> PResult<'a, Option<Arg>> {
|
|
|
|
let attrs = self.parse_arg_attributes()?;
|
|
|
|
let arg_opt = self.parse_self_arg()?;
|
|
|
|
Ok(arg_opt.map(|mut arg| {
|
|
|
|
arg.attrs = attrs.into();
|
|
|
|
arg
|
|
|
|
}))
|
2016-03-06 12:54:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Parses the parameter list and result type of a function that may have a `self` parameter.
|
2016-03-06 12:54:44 +00:00
|
|
|
fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
|
2016-03-06 12:54:44 +00:00
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
|
|
|
|
{
|
|
|
|
self.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Parse optional self argument.
|
2019-06-09 10:58:40 +00:00
|
|
|
let self_arg = self.parse_self_arg_with_attrs()?;
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2016-05-08 18:18:21 +00:00
|
|
|
// Parse the rest of the function parameter list.
|
|
|
|
let sep = SeqSep::trailing_allowed(token::Comma);
|
2019-05-29 22:25:46 +00:00
|
|
|
let (mut fn_inputs, recovered) = if let Some(self_arg) = self_arg {
|
2016-03-06 12:54:44 +00:00
|
|
|
if self.check(&token::CloseDelim(token::Paren)) {
|
2019-01-28 05:04:50 +00:00
|
|
|
(vec![self_arg], false)
|
2016-03-06 12:54:44 +00:00
|
|
|
} else if self.eat(&token::Comma) {
|
|
|
|
let mut fn_inputs = vec![self_arg];
|
2019-07-09 08:34:53 +00:00
|
|
|
let (mut input, _, recovered) = self.parse_seq_to_before_end(
|
2019-01-28 05:04:50 +00:00
|
|
|
&token::CloseDelim(token::Paren), sep, parse_arg_fn)?;
|
|
|
|
fn_inputs.append(&mut input);
|
|
|
|
(fn_inputs, recovered)
|
2016-03-06 12:54:44 +00:00
|
|
|
} else {
|
2019-03-02 05:47:06 +00:00
|
|
|
match self.expect_one_of(&[], &[]) {
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
Ok(recovered) => (vec![self_arg], recovered),
|
|
|
|
}
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
} else {
|
2019-07-09 08:34:53 +00:00
|
|
|
let (input, _, recovered) =
|
|
|
|
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)?;
|
|
|
|
(input, recovered)
|
2014-01-27 12:18:36 +00:00
|
|
|
};
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2019-01-28 05:04:50 +00:00
|
|
|
if !recovered {
|
|
|
|
// Parse closing paren and return type.
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
}
|
2019-05-29 22:25:46 +00:00
|
|
|
// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
|
2019-05-31 01:19:48 +00:00
|
|
|
self.deduplicate_recovered_arg_names(&mut fn_inputs);
|
2019-05-29 22:25:46 +00:00
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
Ok(P(FnDecl {
|
2013-09-14 02:07:43 +00:00
|
|
|
inputs: fn_inputs,
|
2018-01-18 17:59:28 +00:00
|
|
|
output: self.parse_ret_ty(true)?,
|
2019-02-08 17:30:42 +00:00
|
|
|
c_variadic: false
|
2016-03-06 12:54:44 +00:00
|
|
|
}))
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:32:29 +00:00
|
|
|
fn is_crate_vis(&self) -> bool {
|
|
|
|
self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:34:42 +00:00
|
|
|
/// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `crate` for `pub(crate)`,
|
|
|
|
/// `pub(self)` for `pub(in self)` and `pub(super)` for `pub(in super)`.
|
|
|
|
/// If the following element can't be a tuple (i.e., it's a function definition), then
|
|
|
|
/// it's not a tuple struct field), and the contents within the parentheses isn't valid,
|
|
|
|
/// so emit a proper diagnostic.
|
|
|
|
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
|
|
|
|
maybe_whole!(self, NtVis, |x| x);
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2019-08-11 16:34:42 +00:00
|
|
|
self.expected_tokens.push(TokenType::Keyword(kw::Crate));
|
|
|
|
if self.is_crate_vis() {
|
|
|
|
self.bump(); // `crate`
|
|
|
|
return Ok(respan(self.prev_span, VisibilityKind::Crate(CrateSugar::JustCrate)));
|
|
|
|
}
|
2014-08-11 16:32:26 +00:00
|
|
|
|
2019-08-11 16:34:42 +00:00
|
|
|
if !self.eat_keyword(kw::Pub) {
|
|
|
|
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
|
|
|
|
// keyword to grab a span from for inherited visibility; an empty span at the
|
|
|
|
// beginning of the current token would seem to be the "Schelling span".
|
|
|
|
return Ok(respan(self.token.span.shrink_to_lo(), VisibilityKind::Inherited))
|
|
|
|
}
|
|
|
|
let lo = self.prev_span;
|
2017-03-07 23:50:13 +00:00
|
|
|
|
|
|
|
if self.check(&token::OpenDelim(token::Paren)) {
|
2017-03-18 04:13:00 +00:00
|
|
|
// We don't `self.bump()` the `(` yet because this might be a struct definition where
|
|
|
|
// `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
|
|
|
|
// Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
|
|
|
|
// by the following tokens.
|
2019-05-29 15:58:44 +00:00
|
|
|
if self.is_keyword_ahead(1, &[kw::Crate]) &&
|
2019-05-01 00:48:18 +00:00
|
|
|
self.look_ahead(2, |t| t != &token::ModSep) // account for `pub(crate::foo)`
|
|
|
|
{
|
2017-03-07 23:50:13 +00:00
|
|
|
// `pub(crate)`
|
|
|
|
self.bump(); // `(`
|
|
|
|
self.bump(); // `crate`
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
2018-01-29 05:12:09 +00:00
|
|
|
let vis = respan(
|
|
|
|
lo.to(self.prev_span),
|
|
|
|
VisibilityKind::Crate(CrateSugar::PubCrate),
|
|
|
|
);
|
2017-03-07 23:50:13 +00:00
|
|
|
return Ok(vis)
|
2019-05-29 15:58:44 +00:00
|
|
|
} else if self.is_keyword_ahead(1, &[kw::In]) {
|
2017-03-07 23:50:13 +00:00
|
|
|
// `pub(in path)`
|
|
|
|
self.bump(); // `(`
|
|
|
|
self.bump(); // `in`
|
2018-03-09 23:02:39 +00:00
|
|
|
let path = self.parse_path(PathStyle::Mod)?; // `path`
|
2017-03-07 23:50:13 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
2018-01-29 05:12:09 +00:00
|
|
|
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
|
2018-01-27 07:15:00 +00:00
|
|
|
path: P(path),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-01-29 05:12:09 +00:00
|
|
|
});
|
2017-03-07 23:50:13 +00:00
|
|
|
return Ok(vis)
|
|
|
|
} else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren)) &&
|
2019-05-29 15:58:44 +00:00
|
|
|
self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower])
|
2018-01-27 07:13:50 +00:00
|
|
|
{
|
2017-03-07 23:50:13 +00:00
|
|
|
// `pub(self)` or `pub(super)`
|
|
|
|
self.bump(); // `(`
|
2018-03-09 23:02:39 +00:00
|
|
|
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
|
2017-03-07 23:50:13 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
2018-01-29 05:12:09 +00:00
|
|
|
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
|
2018-01-27 07:15:00 +00:00
|
|
|
path: P(path),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-01-29 05:12:09 +00:00
|
|
|
});
|
2017-03-07 23:50:13 +00:00
|
|
|
return Ok(vis)
|
2017-03-18 04:13:00 +00:00
|
|
|
} else if !can_take_tuple { // Provide this diagnostic if this is not a tuple struct
|
|
|
|
// `pub(something) fn ...` or `struct X { pub(something) y: Z }`
|
|
|
|
self.bump(); // `(`
|
|
|
|
let msg = "incorrect visibility restriction";
|
|
|
|
let suggestion = r##"some possible visibility restrictions are:
|
|
|
|
`pub(crate)`: visible only on the current crate
|
|
|
|
`pub(super)`: visible only in the current module's parent
|
|
|
|
`pub(in path::to::module)`: visible only on the specified path"##;
|
|
|
|
let path = self.parse_path(PathStyle::Mod)?;
|
2019-05-01 00:48:18 +00:00
|
|
|
let sp = path.span;
|
2017-05-16 13:12:24 +00:00
|
|
|
let help_msg = format!("make this visible only to module `{}` with `in`", path);
|
2017-03-18 04:13:00 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
2019-07-24 08:51:20 +00:00
|
|
|
struct_span_err!(self.sess.span_diagnostic, sp, E0704, "{}", msg)
|
|
|
|
.help(suggestion)
|
|
|
|
.span_suggestion(
|
|
|
|
sp,
|
|
|
|
&help_msg,
|
|
|
|
format!("in {}", path),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit(); // emit diagnostic, but continue with public visibility
|
2016-04-23 05:40:55 +00:00
|
|
|
}
|
2016-04-11 00:39:35 +00:00
|
|
|
}
|
2017-03-07 23:50:13 +00:00
|
|
|
|
2018-01-29 05:12:09 +00:00
|
|
|
Ok(respan(lo, VisibilityKind::Public))
|
2012-02-23 05:47:23 +00:00
|
|
|
}
|
2013-03-22 19:56:10 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses a string as an ABI spec on an extern type or module. Consumes
|
|
|
|
/// the `extern` keyword, if one is found.
|
2017-12-02 19:15:03 +00:00
|
|
|
fn parse_opt_abi(&mut self) -> PResult<'a, Option<Abi>> {
|
2019-06-04 22:17:07 +00:00
|
|
|
match self.token.kind {
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) |
|
|
|
|
token::Literal(token::Lit { kind: token::StrRaw(..), symbol, suffix }) => {
|
2019-06-07 10:31:13 +00:00
|
|
|
let sp = self.token.span;
|
2019-05-18 22:04:26 +00:00
|
|
|
self.expect_no_suffix(sp, "an ABI spec", suffix);
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2019-05-18 22:04:26 +00:00
|
|
|
match abi::lookup(&symbol.as_str()) {
|
2015-03-28 21:58:51 +00:00
|
|
|
Some(abi) => Ok(Some(abi)),
|
2014-04-02 08:19:41 +00:00
|
|
|
None => {
|
2016-09-21 02:09:22 +00:00
|
|
|
let prev_span = self.prev_span;
|
2019-07-24 08:51:20 +00:00
|
|
|
struct_span_err!(
|
2018-06-09 22:44:32 +00:00
|
|
|
self.sess.span_diagnostic,
|
2016-09-21 02:09:22 +00:00
|
|
|
prev_span,
|
2018-06-19 23:22:37 +00:00
|
|
|
E0703,
|
2018-06-09 22:44:32 +00:00
|
|
|
"invalid ABI: found `{}`",
|
2019-07-24 08:51:20 +00:00
|
|
|
symbol
|
|
|
|
)
|
|
|
|
.span_label(prev_span, "invalid ABI")
|
|
|
|
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
|
|
|
|
.emit();
|
2015-03-28 21:58:51 +00:00
|
|
|
Ok(None)
|
2014-04-02 08:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
_ => Ok(None),
|
2014-04-02 08:19:41 +00:00
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 19:38:26 +00:00
|
|
|
/// We are parsing `async fn`. If we are on Rust 2015, emit an error.
|
|
|
|
fn ban_async_in_2015(&self, async_span: Span) {
|
|
|
|
if async_span.rust_2015() {
|
|
|
|
self.diagnostic()
|
|
|
|
.struct_span_err_with_code(
|
|
|
|
async_span,
|
|
|
|
"`async fn` is not permitted in the 2015 edition",
|
|
|
|
DiagnosticId::Error("E0670".into())
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 16:50:05 +00:00
|
|
|
fn collect_tokens<F, R>(&mut self, f: F) -> PResult<'a, (R, TokenStream)>
|
|
|
|
where F: FnOnce(&mut Self) -> PResult<'a, R>
|
|
|
|
{
|
|
|
|
// Record all tokens we parse when parsing this item.
|
|
|
|
let mut tokens = Vec::new();
|
2018-07-22 15:48:29 +00:00
|
|
|
let prev_collecting = match self.token_cursor.frame.last_token {
|
|
|
|
LastToken::Collecting(ref mut list) => {
|
2019-06-30 18:30:01 +00:00
|
|
|
Some(mem::take(list))
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
2018-07-22 15:48:29 +00:00
|
|
|
LastToken::Was(ref mut last) => {
|
|
|
|
tokens.extend(last.take());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2017-07-12 16:50:05 +00:00
|
|
|
self.token_cursor.frame.last_token = LastToken::Collecting(tokens);
|
|
|
|
let prev = self.token_cursor.stack.len();
|
|
|
|
let ret = f(self);
|
|
|
|
let last_token = if self.token_cursor.stack.len() == prev {
|
|
|
|
&mut self.token_cursor.frame.last_token
|
2019-07-23 18:19:13 +00:00
|
|
|
} else if self.token_cursor.stack.get(prev).is_none() {
|
|
|
|
// This can happen due to a bad interaction of two unrelated recovery mechanisms with
|
2019-07-23 19:51:34 +00:00
|
|
|
// mismatched delimiters *and* recovery lookahead on the likely typo `pub ident(`
|
|
|
|
// (#62881).
|
2019-07-23 01:29:49 +00:00
|
|
|
return Ok((ret?, TokenStream::new(vec![])));
|
2017-07-12 16:50:05 +00:00
|
|
|
} else {
|
|
|
|
&mut self.token_cursor.stack[prev].last_token
|
|
|
|
};
|
2018-07-22 15:48:29 +00:00
|
|
|
|
2019-01-10 00:58:38 +00:00
|
|
|
// Pull out the tokens that we've collected from the call to `f` above.
|
2018-07-22 15:48:29 +00:00
|
|
|
let mut collected_tokens = match *last_token {
|
2019-06-30 18:30:01 +00:00
|
|
|
LastToken::Collecting(ref mut v) => mem::take(v),
|
2019-07-23 18:19:13 +00:00
|
|
|
LastToken::Was(ref was) => {
|
|
|
|
let msg = format!("our vector went away? - found Was({:?})", was);
|
|
|
|
debug!("collect_tokens: {}", msg);
|
|
|
|
self.sess.span_diagnostic.delay_span_bug(self.token.span, &msg);
|
|
|
|
// This can happen due to a bad interaction of two unrelated recovery mechanisms
|
2019-07-23 19:51:34 +00:00
|
|
|
// with mismatched delimiters *and* recovery lookahead on the likely typo
|
|
|
|
// `pub ident(` (#62895, different but similar to the case above).
|
2019-07-23 18:19:13 +00:00
|
|
|
return Ok((ret?, TokenStream::new(vec![])));
|
|
|
|
}
|
2017-07-12 16:50:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we're not at EOF our current token wasn't actually consumed by
|
|
|
|
// `f`, but it'll still be in our list that we pulled out. In that case
|
|
|
|
// put it back.
|
2018-07-22 15:48:29 +00:00
|
|
|
let extra_token = if self.token != token::Eof {
|
|
|
|
collected_tokens.pop()
|
2017-07-12 16:50:05 +00:00
|
|
|
} else {
|
2018-07-22 15:48:29 +00:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we were previously collecting tokens, then this was a recursive
|
|
|
|
// call. In that case we need to record all the tokens we collected in
|
|
|
|
// our parent list as well. To do that we push a clone of our stream
|
|
|
|
// onto the previous list.
|
|
|
|
match prev_collecting {
|
|
|
|
Some(mut list) => {
|
2019-01-10 00:58:38 +00:00
|
|
|
list.extend(collected_tokens.iter().cloned());
|
2018-07-22 15:48:29 +00:00
|
|
|
list.extend(extra_token);
|
|
|
|
*last_token = LastToken::Collecting(list);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
*last_token = LastToken::Was(extra_token);
|
|
|
|
}
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 00:58:38 +00:00
|
|
|
Ok((ret?, TokenStream::new(collected_tokens)))
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 15:44:44 +00:00
|
|
|
/// `::{` or `::*`
|
|
|
|
fn is_import_coupler(&mut self) -> bool {
|
|
|
|
self.check(&token::ModSep) &&
|
|
|
|
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
|
|
|
|
*t == token::BinOp(token::Star))
|
2016-04-17 00:48:40 +00:00
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
2018-06-20 18:19:05 +00:00
|
|
|
pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
|
2019-06-04 22:17:07 +00:00
|
|
|
let ret = match self.token.kind {
|
2019-05-18 22:04:26 +00:00
|
|
|
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) =>
|
|
|
|
(symbol, ast::StrStyle::Cooked, suffix),
|
|
|
|
token::Literal(token::Lit { kind: token::StrRaw(n), symbol, suffix }) =>
|
|
|
|
(symbol, ast::StrStyle::Raw(n), suffix),
|
2015-12-30 23:11:53 +00:00
|
|
|
_ => return None
|
2013-10-08 00:49:10 +00:00
|
|
|
};
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
|
|
|
Some(ret)
|
2013-07-12 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn parse_str(&mut self) -> PResult<'a, (Symbol, StrStyle)> {
|
2015-12-30 23:11:53 +00:00
|
|
|
match self.parse_optional_str() {
|
2014-11-19 04:48:38 +00:00
|
|
|
Some((s, style, suf)) => {
|
2016-09-21 02:09:22 +00:00
|
|
|
let sp = self.prev_span;
|
2019-03-26 19:09:13 +00:00
|
|
|
self.expect_no_suffix(sp, "a string literal", suf);
|
2015-03-28 21:58:51 +00:00
|
|
|
Ok((s, style))
|
2014-11-19 04:48:38 +00:00
|
|
|
}
|
2018-02-19 07:08:23 +00:00
|
|
|
_ => {
|
|
|
|
let msg = "expected string literal";
|
|
|
|
let mut err = self.fatal(msg);
|
2019-06-07 10:31:13 +00:00
|
|
|
err.span_label(self.token.span, msg);
|
2018-02-19 07:08:23 +00:00
|
|
|
Err(err)
|
|
|
|
}
|
2011-02-25 01:00:24 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2019-03-11 01:01:53 +00:00
|
|
|
|
|
|
|
fn report_invalid_macro_expansion_item(&self) {
|
|
|
|
self.struct_span_err(
|
|
|
|
self.prev_span,
|
|
|
|
"macros that expand to items must be delimited with braces or followed by a semicolon",
|
|
|
|
).multipart_suggestion(
|
|
|
|
"change the delimiters to curly braces",
|
|
|
|
vec![
|
|
|
|
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), String::from(" {")),
|
|
|
|
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
|
|
|
|
],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
).span_suggestion(
|
|
|
|
self.sess.source_map.next_point(self.prev_span),
|
|
|
|
"add a semicolon",
|
|
|
|
';'.to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
).emit();
|
|
|
|
}
|
2011-01-11 02:18:16 +00:00
|
|
|
}
|
2019-02-05 09:35:25 +00:00
|
|
|
|
2019-03-03 20:14:25 +00:00
|
|
|
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {
|
|
|
|
for unmatched in unclosed_delims.iter() {
|
2019-02-05 09:35:25 +00:00
|
|
|
let mut err = handler.struct_span_err(unmatched.found_span, &format!(
|
|
|
|
"incorrect close delimiter: `{}`",
|
2019-06-08 19:38:23 +00:00
|
|
|
pprust::token_kind_to_string(&token::CloseDelim(unmatched.found_delim)),
|
2019-02-05 09:35:25 +00:00
|
|
|
));
|
|
|
|
err.span_label(unmatched.found_span, "incorrect close delimiter");
|
|
|
|
if let Some(sp) = unmatched.candidate_span {
|
|
|
|
err.span_label(sp, "close delimiter possibly meant for this");
|
|
|
|
}
|
|
|
|
if let Some(sp) = unmatched.unclosed_span {
|
|
|
|
err.span_label(sp, "un-closed delimiter");
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-03-03 20:14:25 +00:00
|
|
|
unclosed_delims.clear();
|
2019-02-05 10:26:26 +00:00
|
|
|
}
|