2014-04-17 08:35:31 +00:00
|
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 23:44:02 +00:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-02-05 12:13:36 +00:00
|
|
|
|
use abi::{self, Abi};
|
2015-03-28 09:23:20 +00:00
|
|
|
|
use ast::BareFnTy;
|
2014-12-24 06:38:10 +00:00
|
|
|
|
use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
|
2016-02-09 16:54:11 +00:00
|
|
|
|
use ast::Unsafety;
|
|
|
|
|
use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind};
|
2016-02-08 12:16:12 +00:00
|
|
|
|
use ast::Block;
|
2016-02-08 14:27:08 +00:00
|
|
|
|
use ast::{BlockCheckMode, CaptureBy};
|
2016-10-27 06:36:56 +00:00
|
|
|
|
use ast::{Constness, Crate};
|
2016-06-17 02:30:01 +00:00
|
|
|
|
use ast::Defaultness;
|
2016-05-24 11:38:39 +00:00
|
|
|
|
use ast::EnumDef;
|
2016-01-13 06:23:31 +00:00
|
|
|
|
use ast::{Expr, ExprKind, RangeLimits};
|
2016-02-08 15:05:05 +00:00
|
|
|
|
use ast::{Field, FnDecl};
|
2016-02-09 10:31:19 +00:00
|
|
|
|
use ast::{ForeignItem, ForeignItemKind, FunctionRetTy};
|
2016-02-09 16:57:49 +00:00
|
|
|
|
use ast::{Ident, ImplItem, Item, ItemKind};
|
2017-01-17 18:18:29 +00:00
|
|
|
|
use ast::{Lifetime, LifetimeDef, Lit, LitKind, UintTy};
|
2016-02-08 16:06:20 +00:00
|
|
|
|
use ast::Local;
|
2016-02-09 10:56:59 +00:00
|
|
|
|
use ast::MacStmtStyle;
|
2016-02-09 16:44:47 +00:00
|
|
|
|
use ast::Mac_;
|
2016-02-08 12:16:12 +00:00
|
|
|
|
use ast::{MutTy, Mutability};
|
2016-02-11 18:16:33 +00:00
|
|
|
|
use ast::{Pat, PatKind};
|
2015-03-11 21:38:58 +00:00
|
|
|
|
use ast::{PolyTraitRef, QSelf};
|
2016-02-08 16:23:13 +00:00
|
|
|
|
use ast::{Stmt, StmtKind};
|
|
|
|
|
use ast::{VariantData, StructField};
|
2016-02-08 12:16:12 +00:00
|
|
|
|
use ast::StrStyle;
|
2016-02-08 14:43:56 +00:00
|
|
|
|
use ast::SelfKind;
|
2016-06-20 15:49:33 +00:00
|
|
|
|
use ast::{TraitItem, TraitRef};
|
2016-02-08 15:53:21 +00:00
|
|
|
|
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
|
2014-01-09 13:05:33 +00:00
|
|
|
|
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
|
2014-11-29 04:08:30 +00:00
|
|
|
|
use ast::{Visibility, WhereClause};
|
2016-02-08 12:16:12 +00:00
|
|
|
|
use ast::{BinOpKind, UnOp};
|
2016-11-14 09:31:03 +00:00
|
|
|
|
use {ast, attr};
|
2016-08-10 23:20:12 +00:00
|
|
|
|
use codemap::{self, CodeMap, Spanned, spanned, respan};
|
2017-01-16 20:33:45 +00:00
|
|
|
|
use syntax_pos::{self, Span, Pos, BytePos, mk_sp};
|
2015-12-20 21:00:43 +00:00
|
|
|
|
use errors::{self, DiagnosticBuilder};
|
2014-10-06 22:00:56 +00:00
|
|
|
|
use ext::tt::macro_parser;
|
2014-07-03 07:47:30 +00:00
|
|
|
|
use parse;
|
2013-03-01 18:44:43 +00:00
|
|
|
|
use parse::classify;
|
2016-02-23 04:24:42 +00:00
|
|
|
|
use parse::common::SeqSep;
|
2017-01-13 04:49:20 +00:00
|
|
|
|
use parse::lexer::TokenAndSpan;
|
2016-08-26 16:23:42 +00:00
|
|
|
|
use parse::obsolete::ObsoleteSyntax;
|
2016-11-16 08:21:52 +00:00
|
|
|
|
use parse::token::{self, MatchNt, SubstNt};
|
2016-11-05 04:16:26 +00:00
|
|
|
|
use parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership};
|
2015-10-15 12:51:30 +00:00
|
|
|
|
use util::parser::{AssocOp, Fixity};
|
2014-10-28 00:05:28 +00:00
|
|
|
|
use print::pprust;
|
2014-09-13 16:06:01 +00:00
|
|
|
|
use ptr::P;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
use parse::PResult;
|
2016-06-20 15:49:33 +00:00
|
|
|
|
use tokenstream::{self, Delimited, SequenceRepetition, TokenTree};
|
2016-11-16 10:52:37 +00:00
|
|
|
|
use symbol::{Symbol, keywords};
|
2016-06-18 04:01:57 +00:00
|
|
|
|
use util::ThinVec;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
|
2014-05-30 02:03:06 +00:00
|
|
|
|
use std::collections::HashSet;
|
2014-09-13 16:06:01 +00:00
|
|
|
|
use std::mem;
|
2015-02-27 05:00:43 +00:00
|
|
|
|
use std::path::{Path, PathBuf};
|
2014-03-27 14:40:35 +00:00
|
|
|
|
use std::rc::Rc;
|
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
|
|
|
|
use std::slice;
|
2010-08-18 22:41:13 +00:00
|
|
|
|
|
2016-08-23 00:56:52 +00:00
|
|
|
|
use rustc_i128::u128;
|
|
|
|
|
|
2014-09-16 05:22:12 +00:00
|
|
|
|
bitflags! {
|
|
|
|
|
flags Restrictions: u8 {
|
2015-04-28 23:36:22 +00:00
|
|
|
|
const RESTRICTION_STMT_EXPR = 1 << 0,
|
|
|
|
|
const RESTRICTION_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
|
|
|
|
|
2016-02-09 10:36:51 +00:00
|
|
|
|
type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute> >);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
2016-04-18 21:42:18 +00:00
|
|
|
|
/// How to parse a path. There are three different kinds of paths, all of which
|
2013-08-07 16:47:28 +00:00
|
|
|
|
/// are parsed somewhat differently.
|
2015-03-30 13:38:59 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2016-04-18 21:42:18 +00:00
|
|
|
|
pub enum PathStyle {
|
|
|
|
|
/// A path with no type parameters, e.g. `foo::bar::Baz`, used in imports or visibilities.
|
|
|
|
|
Mod,
|
2013-08-07 16:47:28 +00:00
|
|
|
|
/// A path with a lifetime and type parameters, with no double colons
|
2016-04-18 21:42:18 +00:00
|
|
|
|
/// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`, used in types.
|
|
|
|
|
/// Paths using this style can be passed into macros expecting `path` nonterminals.
|
|
|
|
|
Type,
|
2013-08-07 16:47:28 +00:00
|
|
|
|
/// A path with a lifetime and type parameters with double colons before
|
2016-04-18 21:42:18 +00:00
|
|
|
|
/// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`, used in expressions or patterns.
|
|
|
|
|
Expr,
|
2013-08-07 16:47:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 03:11:27 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
|
pub enum SemiColonMode {
|
|
|
|
|
Break,
|
|
|
|
|
Ignore,
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
|
/// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
|
|
|
|
|
/// dropped into the token stream, which happens while parsing the result of
|
|
|
|
|
/// macro expansion). Placement of these is not as complex as I feared it would
|
|
|
|
|
/// be. The important thing is to make sure that lookahead doesn't balk at
|
|
|
|
|
/// `token::Interpolated` tokens.
|
2014-11-14 17:18:10 +00:00
|
|
|
|
macro_rules! maybe_whole_expr {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
($p:expr) => {
|
|
|
|
|
if let token::Interpolated(nt) = $p.token.clone() {
|
|
|
|
|
match *nt {
|
|
|
|
|
token::NtExpr(ref e) => {
|
|
|
|
|
$p.bump();
|
|
|
|
|
return Ok((*e).clone());
|
2013-07-05 10:15:21 +00:00
|
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
|
token::NtPath(ref path) => {
|
|
|
|
|
$p.bump();
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = $p.span;
|
2016-11-02 03:03:55 +00:00
|
|
|
|
let kind = ExprKind::Path(None, (*path).clone());
|
|
|
|
|
return Ok($p.mk_expr(span.lo, span.hi, kind, ThinVec::new()));
|
2014-05-26 01:33:52 +00:00
|
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
|
token::NtBlock(ref block) => {
|
|
|
|
|
$p.bump();
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = $p.span;
|
2016-11-02 03:03:55 +00:00
|
|
|
|
let kind = ExprKind::Block((*block).clone());
|
|
|
|
|
return Ok($p.mk_expr(span.lo, span.hi, kind, ThinVec::new()));
|
2013-07-05 10:15:21 +00:00
|
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
|
_ => {},
|
2013-07-05 10:15:21 +00:00
|
|
|
|
};
|
2013-02-21 05:04:05 +00:00
|
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
|
}
|
2014-11-14 17:18:10 +00:00
|
|
|
|
}
|
2012-07-10 23:37:44 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// As maybe_whole_expr, but for things other than expressions
|
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) => {
|
|
|
|
|
if let token::Interpolated(nt) = $p.token.clone() {
|
|
|
|
|
if let token::$constructor($x) = (*nt).clone() {
|
|
|
|
|
$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
|
|
|
|
|
2014-10-15 06:05:01 +00:00
|
|
|
|
fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
|
|
|
|
|
-> Vec<Attribute> {
|
2015-02-13 07:33:44 +00:00
|
|
|
|
if let Some(ref attrs) = rhs {
|
|
|
|
|
lhs.extend(attrs.iter().cloned())
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 05:46:40 +00:00
|
|
|
|
#[derive(PartialEq)]
|
2016-09-21 02:16:28 +00:00
|
|
|
|
enum PrevTokenKind {
|
2016-09-16 05:46:40 +00:00
|
|
|
|
DocComment,
|
|
|
|
|
Comma,
|
|
|
|
|
Interpolated,
|
|
|
|
|
Eof,
|
|
|
|
|
Other,
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-01 21:34:35 +00:00
|
|
|
|
/* ident is handled by common.rs */
|
2012-07-26 17:14:01 +00:00
|
|
|
|
|
2014-03-09 14:54:34 +00:00
|
|
|
|
pub struct Parser<'a> {
|
2014-03-27 22:39:48 +00:00
|
|
|
|
pub sess: &'a ParseSess,
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// the current token:
|
2014-03-27 22:39:48 +00:00
|
|
|
|
pub token: token::Token,
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// the span of the current token:
|
2014-03-27 22:39:48 +00:00
|
|
|
|
pub span: Span,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
/// the span of the previous token:
|
|
|
|
|
pub prev_span: Span,
|
2016-09-16 05:46:40 +00:00
|
|
|
|
/// the previous token kind
|
2016-09-21 02:16:28 +00:00
|
|
|
|
prev_token_kind: PrevTokenKind,
|
2014-09-16 05:22:12 +00:00
|
|
|
|
pub restrictions: Restrictions,
|
2015-01-17 23:33:05 +00:00
|
|
|
|
pub quote_depth: usize, // not (yet) related to the quasiquoter
|
2016-07-19 20:01:54 +00:00
|
|
|
|
parsing_token_tree: bool,
|
2012-09-08 22:50:29 +00:00
|
|
|
|
/// The set of seen errors about obsolete syntax. Used to suppress
|
|
|
|
|
/// extra detail when the same error is seen twice
|
2014-03-27 22:39:48 +00:00
|
|
|
|
pub obsolete_set: HashSet<ObsoleteSyntax>,
|
2012-12-11 20:20:27 +00:00
|
|
|
|
/// Used to determine the path to externally loaded source files
|
2016-11-05 04:16:26 +00:00
|
|
|
|
pub directory: Directory,
|
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>,
|
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
|
|
|
|
pub expected_tokens: Vec<TokenType>,
|
2016-11-03 07:43:29 +00:00
|
|
|
|
pub tts: Vec<(TokenTree, usize)>,
|
2016-11-03 10:44:25 +00:00
|
|
|
|
pub desugar_doc_comments: bool,
|
2017-01-18 00:13:36 +00:00
|
|
|
|
/// Whether we should configure out of line modules as we parse.
|
|
|
|
|
pub cfg_mods: 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
|
|
|
|
}
|
|
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Clone)]
|
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
|
|
|
|
pub enum TokenType {
|
|
|
|
|
Token(token::Token),
|
2015-01-16 03:04:28 +00:00
|
|
|
|
Keyword(keywords::Keyword),
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TokenType {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
match *self {
|
|
|
|
|
TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
|
2014-12-11 03:46:38 +00:00
|
|
|
|
TokenType::Operator => "an operator".to_string(),
|
2016-04-18 21:42:18 +00:00
|
|
|
|
TokenType::Keyword(kw) => format!("`{}`", kw.name()),
|
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
|
|
|
|
|
2016-04-16 01:10:59 +00:00
|
|
|
|
fn is_ident_or_underscore(t: &token::Token) -> bool {
|
|
|
|
|
t.is_ident() || *t == token::Underscore
|
2013-09-02 23:58:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 00:50:18 +00:00
|
|
|
|
/// Information about the path to a module.
|
|
|
|
|
pub struct ModulePath {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub path_exists: bool,
|
|
|
|
|
pub result: Result<ModulePathSuccess, ModulePathError>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ModulePathSuccess {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
pub path: PathBuf,
|
|
|
|
|
pub directory_ownership: DirectoryOwnership,
|
2016-11-14 09:31:03 +00:00
|
|
|
|
warn: bool,
|
2015-07-03 00:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ModulePathError {
|
|
|
|
|
pub err_msg: String,
|
|
|
|
|
pub help_msg: String,
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub enum LhsExpr {
|
|
|
|
|
NotYetParsed,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
AttributesParsed(ThinVec<Attribute>),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
AlreadyParsed(P<Expr>),
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
impl From<Option<ThinVec<Attribute>>> for LhsExpr {
|
|
|
|
|
fn from(o: Option<ThinVec<Attribute>>) -> Self {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
if let Some(attrs) = o {
|
|
|
|
|
LhsExpr::AttributesParsed(attrs)
|
|
|
|
|
} else {
|
|
|
|
|
LhsExpr::NotYetParsed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<P<Expr>> for LhsExpr {
|
|
|
|
|
fn from(expr: P<Expr>) -> Self {
|
|
|
|
|
LhsExpr::AlreadyParsed(expr)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-03 00:50:18 +00:00
|
|
|
|
|
2014-03-09 14:54:34 +00:00
|
|
|
|
impl<'a> Parser<'a> {
|
2016-12-07 00:28:51 +00:00
|
|
|
|
pub fn new(sess: &'a ParseSess,
|
2017-01-13 04:49:20 +00:00
|
|
|
|
tokens: Vec<TokenTree>,
|
2016-12-07 00:28:51 +00:00
|
|
|
|
directory: Option<Directory>,
|
|
|
|
|
desugar_doc_comments: bool)
|
|
|
|
|
-> Self {
|
2017-01-13 04:49:20 +00:00
|
|
|
|
let tt = TokenTree::Delimited(syntax_pos::DUMMY_SP, Rc::new(Delimited {
|
|
|
|
|
delim: token::NoDelim,
|
|
|
|
|
tts: tokens,
|
|
|
|
|
}));
|
2016-11-03 07:43:29 +00:00
|
|
|
|
let mut parser = Parser {
|
2014-05-25 23:27:36 +00:00
|
|
|
|
sess: sess,
|
2016-11-03 07:43:29 +00:00
|
|
|
|
token: token::Underscore,
|
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
|
prev_span: syntax_pos::DUMMY_SP,
|
2016-09-21 02:16:28 +00:00
|
|
|
|
prev_token_kind: PrevTokenKind::Other,
|
2015-04-29 21:58:43 +00:00
|
|
|
|
restrictions: Restrictions::empty(),
|
2014-05-25 23:27:36 +00:00
|
|
|
|
quote_depth: 0,
|
2016-07-19 20:01:54 +00:00
|
|
|
|
parsing_token_tree: false,
|
2014-05-25 23:27:36 +00:00
|
|
|
|
obsolete_set: HashSet::new(),
|
2016-11-05 04:16:26 +00:00
|
|
|
|
directory: Directory { path: PathBuf::new(), ownership: DirectoryOwnership::Owned },
|
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-01-13 04:49:20 +00:00
|
|
|
|
tts: if tt.len() > 0 { vec![(tt, 0)] } else { Vec::new() },
|
2016-11-03 10:44:25 +00:00
|
|
|
|
desugar_doc_comments: desugar_doc_comments,
|
2017-01-18 00:13:36 +00:00
|
|
|
|
cfg_mods: true,
|
2016-11-03 07:43:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tok = parser.next_tok();
|
|
|
|
|
parser.token = tok.tok;
|
|
|
|
|
parser.span = tok.sp;
|
2016-12-07 00:28:51 +00:00
|
|
|
|
if let Some(directory) = directory {
|
|
|
|
|
parser.directory = directory;
|
|
|
|
|
} else if parser.span != syntax_pos::DUMMY_SP {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
parser.directory.path = PathBuf::from(sess.codemap().span_to_filename(parser.span));
|
|
|
|
|
parser.directory.path.pop();
|
2016-11-03 07:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
parser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next_tok(&mut self) -> TokenAndSpan {
|
2017-01-14 12:42:00 +00:00
|
|
|
|
loop {
|
|
|
|
|
let tok = if let Some((tts, i)) = self.tts.pop() {
|
2016-11-03 07:43:29 +00:00
|
|
|
|
let tt = tts.get_tt(i);
|
|
|
|
|
if i + 1 < tts.len() {
|
|
|
|
|
self.tts.push((tts, i + 1));
|
|
|
|
|
}
|
|
|
|
|
if let TokenTree::Token(sp, tok) = tt {
|
|
|
|
|
TokenAndSpan { tok: tok, sp: sp }
|
|
|
|
|
} else {
|
|
|
|
|
self.tts.push((tt, 0));
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-01-13 04:49:20 +00:00
|
|
|
|
TokenAndSpan { tok: token::Eof, sp: self.span }
|
2016-11-03 07:43:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-14 12:42:00 +00:00
|
|
|
|
match tok.tok {
|
|
|
|
|
token::DocComment(name) if self.desugar_doc_comments => {
|
|
|
|
|
self.tts.push((TokenTree::Token(tok.sp, token::DocComment(name)), 0));
|
2016-11-03 07:43:29 +00:00
|
|
|
|
}
|
2017-01-14 12:42:00 +00:00
|
|
|
|
_ => return tok,
|
2016-11-03 07:43:29 +00:00
|
|
|
|
}
|
2014-05-25 23:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-09 20:12:30 +00:00
|
|
|
|
|
|
|
|
|
/// Convert a token to a string using self's reader
|
2014-06-21 10:39:03 +00:00
|
|
|
|
pub fn token_to_string(token: &token::Token) -> String {
|
2014-10-28 00:05:28 +00:00
|
|
|
|
pprust::token_to_string(token)
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Convert 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 {
|
2014-06-21 10:39:03 +00:00
|
|
|
|
Parser::token_to_string(&self.token)
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 23:03:29 +00:00
|
|
|
|
pub fn this_token_descr(&self) -> String {
|
|
|
|
|
let s = self.this_token_to_string();
|
|
|
|
|
if self.token.is_strict_keyword() {
|
|
|
|
|
format!("keyword `{}`", s)
|
|
|
|
|
} else if self.token.is_reserved_keyword() {
|
|
|
|
|
format!("reserved keyword `{}`", s)
|
|
|
|
|
} else {
|
|
|
|
|
format!("`{}`", s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn unexpected_last<T>(&self, t: &token::Token) -> PResult<'a, T> {
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let token_str = Parser::token_to_string(t);
|
2016-09-21 02:09:22 +00:00
|
|
|
|
Err(self.span_fatal(self.prev_span, &format!("unexpected token: `{}`", token_str)))
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub 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
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Expect and consume the token t. Signal an error if
|
|
|
|
|
/// the next token is not t.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn expect(&mut self, t: &token::Token) -> PResult<'a, ()> {
|
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();
|
|
|
|
|
Ok(())
|
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 {
|
|
|
|
|
let token_str = Parser::token_to_string(t);
|
|
|
|
|
let this_token_str = self.this_token_to_string();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Err(self.fatal(&format!("expected `{}`, found `{}`",
|
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
|
|
|
|
token_str,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
this_token_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
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
|
} else {
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 23:28:45 +00:00
|
|
|
|
self.expect_one_of(unsafe { slice::from_raw_parts(t, 1) }, &[])
|
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.
|
2013-12-30 22:04:00 +00:00
|
|
|
|
pub fn expect_one_of(&mut self,
|
|
|
|
|
edible: &[token::Token],
|
2015-12-20 21:00:43 +00:00
|
|
|
|
inedible: &[token::Token]) -> PResult<'a, ()>{
|
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
|
|
|
|
fn tokens_to_string(tokens: &[TokenType]) -> String {
|
2013-08-05 20:18:29 +00:00
|
|
|
|
let mut i = tokens.iter();
|
|
|
|
|
// This might be a sign we need a connect method on Iterator.
|
2014-05-07 23:33:43 +00:00
|
|
|
|
let b = i.next()
|
2014-12-11 03:46:38 +00:00
|
|
|
|
.map_or("".to_string(), |t| t.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
|
|
|
|
i.enumerate().fold(b, |mut b, (i, ref a)| {
|
|
|
|
|
if tokens.len() > 2 && i == tokens.len() - 2 {
|
|
|
|
|
b.push_str(", or ");
|
|
|
|
|
} else if tokens.len() == 2 && i == tokens.len() - 2 {
|
|
|
|
|
b.push_str(" or ");
|
|
|
|
|
} else {
|
|
|
|
|
b.push_str(", ");
|
|
|
|
|
}
|
2016-02-08 22:55:55 +00:00
|
|
|
|
b.push_str(&a.to_string());
|
2014-05-07 23:33:43 +00:00
|
|
|
|
b
|
|
|
|
|
})
|
2013-08-05 20:18:29 +00:00
|
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
|
if edible.contains(&self.token) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
Ok(())
|
2013-12-30 23:09:41 +00:00
|
|
|
|
} else if inedible.contains(&self.token) {
|
2013-08-05 20:18:29 +00:00
|
|
|
|
// leave it in the input
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(())
|
2013-08-05 20:18:29 +00:00
|
|
|
|
} else {
|
2015-04-17 04:57:38 +00:00
|
|
|
|
let mut expected = edible.iter()
|
|
|
|
|
.map(|x| TokenType::Token(x.clone()))
|
|
|
|
|
.chain(inedible.iter().map(|x| TokenType::Token(x.clone())))
|
|
|
|
|
.chain(self.expected_tokens.iter().cloned())
|
|
|
|
|
.collect::<Vec<_>>();
|
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.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
|
|
|
|
|
expected.dedup();
|
2015-02-18 19:48:57 +00:00
|
|
|
|
let expect = tokens_to_string(&expected[..]);
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let actual = self.this_token_to_string();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Err(self.fatal(
|
2015-01-16 03:04:28 +00:00
|
|
|
|
&(if expected.len() > 1 {
|
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
|
|
|
|
(format!("expected one of {}, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
|
expect,
|
|
|
|
|
actual))
|
2015-03-24 23:53:34 +00:00
|
|
|
|
} else if expected.is_empty() {
|
2015-01-16 03:04:28 +00:00
|
|
|
|
(format!("unexpected token: `{}`",
|
|
|
|
|
actual))
|
2013-08-05 20:18:29 +00:00
|
|
|
|
} else {
|
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
|
|
|
|
(format!("expected {}, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
|
expect,
|
|
|
|
|
actual))
|
2015-02-18 19:48:57 +00:00
|
|
|
|
})[..]
|
2015-03-28 21:58:51 +00:00
|
|
|
|
))
|
2013-08-05 20:18:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 09:47:33 +00:00
|
|
|
|
/// returns the span of expr, if it was not interpolated or the span of the interpolated token
|
2016-01-27 10:26:38 +00:00
|
|
|
|
fn interpolated_or_expr_span(&self,
|
|
|
|
|
expr: PResult<'a, P<Expr>>)
|
|
|
|
|
-> PResult<'a, (Span, P<Expr>)> {
|
2016-01-27 09:47:33 +00:00
|
|
|
|
expr.map(|e| {
|
2016-09-21 02:16:28 +00:00
|
|
|
|
if self.prev_token_kind == PrevTokenKind::Interpolated {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
(self.prev_span, e)
|
2016-01-27 09:47:33 +00:00
|
|
|
|
} else {
|
|
|
|
|
(e.span, e)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
self.check_strict_keywords();
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.check_reserved_keywords();
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
token::Ident(i) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(i)
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
2016-09-21 02:16:28 +00:00
|
|
|
|
Err(if self.prev_token_kind == PrevTokenKind::DocComment {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.span_fatal_help(self.prev_span,
|
2016-05-28 02:05:22 +00:00
|
|
|
|
"found a documentation comment that doesn't document anything",
|
|
|
|
|
"doc comments must come before what they document, maybe a comment was \
|
2016-09-16 05:46:40 +00:00
|
|
|
|
intended with `//`?")
|
|
|
|
|
} else {
|
2016-05-28 02:05:22 +00:00
|
|
|
|
let mut err = self.fatal(&format!("expected identifier, found `{}`",
|
|
|
|
|
self.this_token_to_string()));
|
|
|
|
|
if self.token == token::Underscore {
|
|
|
|
|
err.note("`_` is a wildcard pattern, not an identifier");
|
|
|
|
|
}
|
|
|
|
|
err
|
2016-09-16 05:46:40 +00:00
|
|
|
|
})
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
/// Check if the next token is `tok`, and return `true` if so.
|
|
|
|
|
///
|
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.
|
|
|
|
|
pub fn check(&mut self, tok: &token::Token) -> bool {
|
|
|
|
|
let is_present = self.token == *tok;
|
|
|
|
|
if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
|
|
|
|
|
is_present
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Consume token 'tok' if it exists. Returns true if the given
|
|
|
|
|
/// token was present, false otherwise.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn eat(&mut self, tok: &token::Token) -> 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
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 03:04:28 +00:00
|
|
|
|
pub fn check_keyword(&mut self, kw: keywords::Keyword) -> bool {
|
|
|
|
|
self.expected_tokens.push(TokenType::Keyword(kw));
|
|
|
|
|
self.token.is_keyword(kw)
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// If the next token is the given keyword, eat it and return
|
|
|
|
|
/// true. Otherwise, return false.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> 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
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 22:38:28 +00:00
|
|
|
|
pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::Ident(ident)));
|
|
|
|
|
if let token::Ident(ref cur_ident) = self.token {
|
2015-12-18 22:38:28 +00:00
|
|
|
|
cur_ident.name == ident.name
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 00:25:18 +00:00
|
|
|
|
pub fn eat_contextual_keyword(&mut self, ident: Ident) -> bool {
|
2015-12-18 22:38:28 +00:00
|
|
|
|
if self.check_contextual_keyword(ident) {
|
2016-01-30 00:25:18 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
true
|
2015-12-18 22:38:28 +00:00
|
|
|
|
} else {
|
2016-01-30 00:25:18 +00:00
|
|
|
|
false
|
2015-12-18 22:38:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// If the given word is not a keyword, signal an error.
|
|
|
|
|
/// If the next token is not the given word, signal an error.
|
|
|
|
|
/// Otherwise, eat it.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn expect_keyword(&mut self, kw: keywords::Keyword) -> 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Signal an error if the given string is a strict keyword
|
2013-12-30 22:04:00 +00:00
|
|
|
|
pub fn check_strict_keywords(&mut self) {
|
2014-10-27 12:33:30 +00:00
|
|
|
|
if self.token.is_strict_keyword() {
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let token_str = self.this_token_to_string();
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
self.span_err(span,
|
2015-01-07 16:58:31 +00:00
|
|
|
|
&format!("expected identifier, found keyword `{}`",
|
2015-02-18 19:48:57 +00:00
|
|
|
|
token_str));
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Signal an error if the current token is a reserved keyword
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn check_reserved_keywords(&mut self) {
|
2014-10-27 12:33:30 +00:00
|
|
|
|
if self.token.is_reserved_keyword() {
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let token_str = self.this_token_to_string();
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.fatal(&format!("`{}` is a reserved keyword", token_str)).emit()
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Expect and consume an `&`. If `&&` is seen, replace it with a single
|
|
|
|
|
/// `&` and continue. If an `&` is not seen, signal 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)));
|
2014-04-17 08:35:31 +00:00
|
|
|
|
match self.token {
|
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 => {
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
let lo = span.lo + BytePos(1);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
Ok(self.bump_with(token::BinOp(token::And), lo, span.hi))
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
|
2014-11-19 04:48:38 +00:00
|
|
|
|
match suffix {
|
|
|
|
|
None => {/* everything ok */}
|
|
|
|
|
Some(suf) => {
|
|
|
|
|
let text = suf.as_str();
|
|
|
|
|
if text.is_empty() {
|
2014-11-19 09:22:54 +00:00
|
|
|
|
self.span_bug(sp, "found empty literal suffix in Some")
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
2016-02-08 22:55:55 +00:00
|
|
|
|
self.span_err(sp, &format!("{} with a suffix is invalid", kind));
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Attempt to consume a `<`. If `<<` is seen, replace it with a single
|
|
|
|
|
/// `<` and continue. If a `<` is not seen, return false.
|
|
|
|
|
///
|
|
|
|
|
/// 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));
|
2014-05-11 04:27:44 +00:00
|
|
|
|
match self.token {
|
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) => {
|
2014-12-23 00:13:49 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
let lo = span.lo + BytePos(1);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
self.bump_with(token::Lt, lo, span.hi);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
true
|
2014-05-11 04:27:44 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
_ => false,
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Expect and consume a GT. if a >> is seen, replace it
|
|
|
|
|
/// with a single > and continue. If a GT is not seen,
|
|
|
|
|
/// signal an error.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn expect_gt(&mut self) -> PResult<'a, ()> {
|
2015-01-16 03:04:28 +00:00
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::Gt));
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
token::Gt => {
|
|
|
|
|
self.bump();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::Shr) => {
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
let lo = span.lo + BytePos(1);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
Ok(self.bump_with(token::Gt, lo, span.hi))
|
2013-12-30 23:17:53 +00:00
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOpEq(token::Shr) => {
|
2014-06-20 16:53:12 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
let lo = span.lo + BytePos(1);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
Ok(self.bump_with(token::Ge, lo, span.hi))
|
2014-06-20 16:53:12 +00:00
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::Ge => {
|
2014-06-20 16:53:12 +00:00
|
|
|
|
let span = self.span;
|
|
|
|
|
let lo = span.lo + BytePos(1);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
Ok(self.bump_with(token::Eq, lo, span.hi))
|
2014-06-20 16:53:12 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
_ => self.unexpected()
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
|
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
|
mut f: F)
|
2017-01-16 22:54:59 +00:00
|
|
|
|
-> PResult<'a, (Vec<T>, bool)>
|
2015-12-20 21:00:43 +00:00
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2014-03-19 12:16:56 +00:00
|
|
|
|
let mut v = Vec::new();
|
2014-08-21 04:37:15 +00:00
|
|
|
|
// This loop works by alternating back and forth between parsing types
|
|
|
|
|
// and commas. For example, given a string `A, B,>`, the parser would
|
|
|
|
|
// first parse `A`, then a comma, then `B`, then a comma. After that it
|
|
|
|
|
// would encounter a `>` and stop. This lets the parser handle trailing
|
|
|
|
|
// commas in generic parameters, because it can stop either after
|
|
|
|
|
// parsing a type or after parsing a comma.
|
2015-03-13 18:35:53 +00:00
|
|
|
|
for i in 0.. {
|
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.check(&token::Gt)
|
2014-10-27 08:22:52 +00:00
|
|
|
|
|| self.token == token::BinOp(token::Shr)
|
|
|
|
|
|| self.token == token::Ge
|
|
|
|
|
|| self.token == token::BinOpEq(token::Shr) {
|
2014-08-21 04:37:15 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if i % 2 == 0 {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
match f(self)? {
|
2014-11-29 04:08:30 +00:00
|
|
|
|
Some(result) => v.push(result),
|
2017-01-16 22:54:59 +00:00
|
|
|
|
None => return Ok((v, true))
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
2014-08-21 04:37:15 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
if let Some(t) = sep.as_ref() {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(t)?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-16 22:54:59 +00:00
|
|
|
|
return Ok((v, false));
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse a sequence bracketed by '<' and '>', stopping
|
|
|
|
|
/// before the '>'.
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_before_gt<T, F>(&mut self,
|
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
|
mut f: F)
|
2017-01-16 22:54:59 +00:00
|
|
|
|
-> PResult<'a, Vec<T>> where
|
2015-12-20 21:00:43 +00:00
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (result, returned) = self.parse_seq_to_before_gt_or_return(sep,
|
2016-03-22 22:58:45 +00:00
|
|
|
|
|p| Ok(Some(f(p)?)))?;
|
2014-11-29 04:08:30 +00:00
|
|
|
|
assert!(!returned);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(result);
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_gt<T, F>(&mut self,
|
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
|
f: F)
|
2017-01-16 22:54:59 +00:00
|
|
|
|
-> PResult<'a, Vec<T>> where
|
2015-12-20 21:00:43 +00:00
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let v = self.parse_seq_to_before_gt(sep, f)?;
|
|
|
|
|
self.expect_gt()?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(v);
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
|
|
|
|
|
sep: Option<token::Token>,
|
|
|
|
|
f: F)
|
2017-01-16 22:54:59 +00:00
|
|
|
|
-> PResult<'a, (Vec<T>, bool)> where
|
2015-12-20 21:00:43 +00:00
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?;
|
2014-11-29 04:08:30 +00:00
|
|
|
|
if !returned {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_gt()?;
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok((v, returned));
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 19:39:50 +00:00
|
|
|
|
/// Eat and discard tokens until one of `kets` is encountered. Respects token trees,
|
|
|
|
|
/// passes through any errors encountered. Used for error recovery.
|
|
|
|
|
pub fn eat_to_tokens(&mut self, kets: &[&token::Token]) {
|
2016-09-15 19:34:21 +00:00
|
|
|
|
let handler = self.diagnostic();
|
|
|
|
|
|
2016-02-10 03:11:27 +00:00
|
|
|
|
self.parse_seq_to_before_tokens(kets,
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::none(),
|
2016-02-10 03:11:27 +00:00
|
|
|
|
|p| p.parse_token_tree(),
|
2016-09-15 19:34:21 +00:00
|
|
|
|
|mut e| handler.cancel(&mut e));
|
2016-01-31 19:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a sequence, including the closing delimiter. The function
|
|
|
|
|
/// f must consume tokens until reaching the next separator or
|
|
|
|
|
/// closing bracket.
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_end<T, F>(&mut self,
|
|
|
|
|
ket: &token::Token,
|
|
|
|
|
sep: SeqSep,
|
|
|
|
|
f: F)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, Vec<T>> where
|
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2016-01-29 04:49:59 +00:00
|
|
|
|
let val = self.parse_seq_to_before_end(ket, sep, f);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(val)
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a sequence, not including the closing delimiter. The function
|
|
|
|
|
/// f must consume tokens until reaching the next separator or
|
|
|
|
|
/// closing bracket.
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq_to_before_end<T, F>(&mut self,
|
|
|
|
|
ket: &token::Token,
|
|
|
|
|
sep: SeqSep,
|
2016-01-31 19:39:50 +00:00
|
|
|
|
f: F)
|
2016-01-29 04:49:59 +00:00
|
|
|
|
-> Vec<T>
|
2016-03-22 03:02:09 +00:00
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>
|
2016-01-31 19:39:50 +00:00
|
|
|
|
{
|
2016-02-10 03:11:27 +00:00
|
|
|
|
self.parse_seq_to_before_tokens(&[ket], sep, f, |mut e| e.emit())
|
2016-01-31 19:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 03:11:27 +00:00
|
|
|
|
// `fe` is an error handler.
|
|
|
|
|
fn parse_seq_to_before_tokens<T, F, Fe>(&mut self,
|
2016-01-31 19:39:50 +00:00
|
|
|
|
kets: &[&token::Token],
|
|
|
|
|
sep: SeqSep,
|
2016-02-10 03:11:27 +00:00
|
|
|
|
mut f: F,
|
|
|
|
|
mut fe: Fe)
|
2016-01-31 19:39:50 +00:00
|
|
|
|
-> Vec<T>
|
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2016-02-10 03:11:27 +00:00
|
|
|
|
Fe: FnMut(DiagnosticBuilder)
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
|
let mut first: bool = true;
|
2016-10-29 21:54:04 +00:00
|
|
|
|
let mut v = vec![];
|
2016-01-31 19:39:50 +00:00
|
|
|
|
while !kets.contains(&&self.token) {
|
2013-06-15 01:21:47 +00:00
|
|
|
|
match sep.sep {
|
2016-01-29 04:49:59 +00:00
|
|
|
|
Some(ref t) => {
|
|
|
|
|
if first {
|
|
|
|
|
first = false;
|
|
|
|
|
} else {
|
2016-02-10 03:11:27 +00:00
|
|
|
|
if let Err(e) = self.expect(t) {
|
|
|
|
|
fe(e);
|
2016-01-29 04:49:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => ()
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
2016-01-31 19:39:50 +00:00
|
|
|
|
if sep.trailing_sep_allowed && kets.iter().any(|k| self.check(k)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
|
|
|
|
|
|
match f(self) {
|
|
|
|
|
Ok(t) => v.push(t),
|
2016-02-10 03:11:27 +00:00
|
|
|
|
Err(e) => {
|
|
|
|
|
fe(e);
|
2016-01-29 04:49:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
|
|
|
|
|
|
v
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a sequence, including the closing delimiter. The function
|
|
|
|
|
/// f must consume tokens until reaching the next separator or
|
|
|
|
|
/// closing bracket.
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_unspanned_seq<T, F>(&mut self,
|
|
|
|
|
bra: &token::Token,
|
|
|
|
|
ket: &token::Token,
|
|
|
|
|
sep: SeqSep,
|
|
|
|
|
f: F)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, Vec<T>> where
|
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(bra)?;
|
2016-01-29 04:49:59 +00:00
|
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
2016-03-26 19:37:53 +00:00
|
|
|
|
if self.token == *ket {
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(result)
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NB: Do not use this function unless you actually plan to place the
|
|
|
|
|
// spanned list in the AST.
|
2014-12-08 18:28:32 +00:00
|
|
|
|
pub fn parse_seq<T, F>(&mut self,
|
|
|
|
|
bra: &token::Token,
|
|
|
|
|
ket: &token::Token,
|
|
|
|
|
sep: SeqSep,
|
|
|
|
|
f: F)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, Spanned<Vec<T>>> where
|
|
|
|
|
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2014-12-08 18:28:32 +00:00
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(bra)?;
|
2016-01-29 04:49:59 +00:00
|
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
2013-06-15 01:21:47 +00:00
|
|
|
|
let hi = self.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(spanned(lo, hi, result))
|
2013-06-15 01:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
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)");
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.prev_span = self.span;
|
2016-09-16 05:46:40 +00:00
|
|
|
|
|
|
|
|
|
// Record last token kind for possible error recovery.
|
2016-09-21 02:16:28 +00:00
|
|
|
|
self.prev_token_kind = match self.token {
|
|
|
|
|
token::DocComment(..) => PrevTokenKind::DocComment,
|
|
|
|
|
token::Comma => PrevTokenKind::Comma,
|
|
|
|
|
token::Interpolated(..) => PrevTokenKind::Interpolated,
|
|
|
|
|
token::Eof => PrevTokenKind::Eof,
|
|
|
|
|
_ => PrevTokenKind::Other,
|
2013-08-05 20:18:29 +00:00
|
|
|
|
};
|
2016-09-16 05:46:40 +00:00
|
|
|
|
|
2017-01-14 11:13:45 +00:00
|
|
|
|
let next = self.next_tok();
|
2013-12-30 23:17:53 +00:00
|
|
|
|
self.span = next.sp;
|
2013-12-30 23:09:41 +00:00
|
|
|
|
self.token = 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
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.check_unknown_macro_variable();
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Advance the parser by one token and return the bumped token.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn bump_and_get(&mut self) -> token::Token {
|
2014-11-23 11:14:35 +00:00
|
|
|
|
let old_token = mem::replace(&mut self.token, token::Underscore);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
old_token
|
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 `<<`.
|
|
|
|
|
pub fn bump_with(&mut self,
|
|
|
|
|
next: token::Token,
|
|
|
|
|
lo: BytePos,
|
|
|
|
|
hi: BytePos) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.prev_span = mk_sp(self.span.lo, 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;
|
2013-12-30 23:17:53 +00:00
|
|
|
|
self.span = mk_sp(lo, hi);
|
2016-02-06 17:42:17 +00:00
|
|
|
|
self.token = next;
|
|
|
|
|
self.expected_tokens.clear();
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2016-02-06 17:42:17 +00:00
|
|
|
|
|
2016-10-19 20:33:41 +00:00
|
|
|
|
pub fn look_ahead<R, F>(&mut self, dist: usize, f: F) -> R where
|
2014-12-08 18:28:32 +00:00
|
|
|
|
F: FnOnce(&token::Token) -> R,
|
|
|
|
|
{
|
2016-10-19 20:33:41 +00:00
|
|
|
|
if dist == 0 {
|
2017-01-14 11:13:45 +00:00
|
|
|
|
return f(&self.token);
|
|
|
|
|
}
|
|
|
|
|
let mut tok = token::Eof;
|
|
|
|
|
if let Some(&(ref tts, mut i)) = self.tts.last() {
|
|
|
|
|
i += dist - 1;
|
|
|
|
|
if i < tts.len() {
|
|
|
|
|
tok = match tts.get_tt(i) {
|
|
|
|
|
TokenTree::Token(_, tok) => tok,
|
|
|
|
|
TokenTree::Delimited(_, delimited) => token::OpenDelim(delimited.delim),
|
|
|
|
|
TokenTree::Sequence(..) => token::Dollar,
|
|
|
|
|
};
|
2016-10-19 20:33:41 +00:00
|
|
|
|
}
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2017-01-14 11:13:45 +00:00
|
|
|
|
f(&tok)
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> {
|
|
|
|
|
self.sess.span_diagnostic.struct_span_fatal(self.span, m)
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
|
|
|
|
|
self.sess.span_diagnostic.struct_span_fatal(sp, m)
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
|
|
|
|
|
let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
|
2016-04-20 18:49:16 +00:00
|
|
|
|
err.help(help);
|
2015-12-20 21:00:43 +00:00
|
|
|
|
err
|
2015-02-24 14:07:54 +00:00
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn bug(&self, m: &str) -> ! {
|
2013-12-30 23:17:53 +00:00
|
|
|
|
self.sess.span_diagnostic.span_bug(self.span, m)
|
2012-04-19 23:44:24 +00:00
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn warn(&self, m: &str) {
|
2013-12-30 23:17:53 +00:00
|
|
|
|
self.sess.span_diagnostic.span_warn(self.span, m)
|
2012-01-13 08:56:53 +00:00
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn span_warn(&self, sp: Span, m: &str) {
|
2014-03-22 01:05:05 +00:00
|
|
|
|
self.sess.span_diagnostic.span_warn(sp, m)
|
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn span_err(&self, sp: Span, m: &str) {
|
2012-09-08 22:50:29 +00:00
|
|
|
|
self.sess.span_diagnostic.span_err(sp, m)
|
|
|
|
|
}
|
2016-05-28 02:05:22 +00:00
|
|
|
|
pub fn span_err_help(&self, sp: Span, m: &str, h: &str) {
|
|
|
|
|
let mut err = self.sess.span_diagnostic.mut_span_err(sp, m);
|
|
|
|
|
err.help(h);
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn span_bug(&self, sp: Span, m: &str) -> ! {
|
2014-11-19 04:48:38 +00:00
|
|
|
|
self.sess.span_diagnostic.span_bug(sp, m)
|
|
|
|
|
}
|
2015-01-21 11:44:49 +00:00
|
|
|
|
pub fn abort_if_errors(&self) {
|
2015-12-13 22:17:55 +00:00
|
|
|
|
self.sess.span_diagnostic.abort_if_errors();
|
2012-09-08 22:50:29 +00:00
|
|
|
|
}
|
2011-03-18 00:39:47 +00:00
|
|
|
|
|
2016-09-15 19:34:21 +00:00
|
|
|
|
fn cancel(&self, err: &mut DiagnosticBuilder) {
|
|
|
|
|
self.sess.span_diagnostic.cancel(err)
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn diagnostic(&self) -> &'a errors::Handler {
|
|
|
|
|
&self.sess.span_diagnostic
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Is the current token one of the keywords that signals a bare function
|
|
|
|
|
/// type?
|
2013-12-30 22:04:00 +00:00
|
|
|
|
pub fn token_is_bare_fn_keyword(&mut self) -> bool {
|
2015-01-16 03:04:28 +00:00
|
|
|
|
self.check_keyword(keywords::Fn) ||
|
|
|
|
|
self.check_keyword(keywords::Unsafe) ||
|
|
|
|
|
self.check_keyword(keywords::Extern)
|
2013-10-29 22:06:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
|
pub fn get_lifetime(&mut self) -> ast::Ident {
|
|
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::Lifetime(ref ident) => *ident,
|
2013-05-19 05:07:44 +00:00
|
|
|
|
_ => self.bug("not a lifetime"),
|
2013-04-26 23:19:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:53:21 +00:00
|
|
|
|
pub fn parse_for_in_type(&mut self) -> PResult<'a, TyKind> {
|
2014-11-07 11:53:45 +00:00
|
|
|
|
/*
|
|
|
|
|
Parses whatever can come after a `for` keyword in a type.
|
2016-08-01 01:25:32 +00:00
|
|
|
|
The `for` hasn't been consumed.
|
2014-11-07 11:53:45 +00:00
|
|
|
|
|
|
|
|
|
- for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
|
2017-01-17 18:18:29 +00:00
|
|
|
|
- for <'lt> path::foo(a, b) + Trait + 'a
|
2014-11-07 11:53:45 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2015-02-05 08:46:01 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
2014-11-07 11:53:45 +00:00
|
|
|
|
|
|
|
|
|
// examine next token to decide to do
|
2015-03-18 18:54:06 +00:00
|
|
|
|
if self.token_is_bare_fn_keyword() {
|
|
|
|
|
self.parse_ty_bare_fn(lifetime_defs)
|
|
|
|
|
} else {
|
2015-02-05 08:46:01 +00:00
|
|
|
|
let hi = self.span.hi;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let trait_ref = self.parse_trait_ref()?;
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let poly_trait_ref = PolyTraitRef { bound_lifetimes: lifetime_defs,
|
|
|
|
|
trait_ref: trait_ref,
|
|
|
|
|
span: mk_sp(lo, hi)};
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
|
2016-10-22 00:33:36 +00:00
|
|
|
|
self.parse_ty_param_bounds()?
|
2014-11-15 21:55:27 +00:00
|
|
|
|
} else {
|
2017-01-16 22:54:59 +00:00
|
|
|
|
Vec::new()
|
2014-11-15 21:55:27 +00:00
|
|
|
|
};
|
|
|
|
|
let all_bounds =
|
2014-12-24 06:38:10 +00:00
|
|
|
|
Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
|
2017-01-16 22:54:59 +00:00
|
|
|
|
.chain(other_bounds)
|
2014-11-15 21:55:27 +00:00
|
|
|
|
.collect();
|
2017-01-17 07:41:44 +00:00
|
|
|
|
Ok(ast::TyKind::TraitObject(all_bounds))
|
2014-11-07 11:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-01 01:25:32 +00:00
|
|
|
|
pub fn parse_impl_trait_type(&mut self) -> PResult<'a, TyKind> {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// Parses whatever can come after a `impl` keyword in a type.
|
|
|
|
|
// The `impl` has already been consumed.
|
|
|
|
|
Ok(ast::TyKind::ImplTrait(self.parse_ty_param_bounds()?))
|
2016-08-01 01:25:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:53:21 +00:00
|
|
|
|
pub fn parse_ty_path(&mut self) -> PResult<'a, TyKind> {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
Ok(TyKind::Path(None, self.parse_path(PathStyle::Type)?))
|
2014-11-07 11:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:53:21 +00:00
|
|
|
|
/// parse a TyKind::BareFn type:
|
2017-01-17 18:18:29 +00:00
|
|
|
|
pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<LifetimeDef>)
|
2016-02-08 15:53:21 +00:00
|
|
|
|
-> PResult<'a, TyKind> {
|
2012-11-05 04:41:00 +00:00
|
|
|
|
/*
|
|
|
|
|
|
2016-04-04 14:24:57 +00:00
|
|
|
|
[unsafe] [extern "ABI"] fn (S) -> T
|
|
|
|
|
^~~~^ ^~~~^ ^~^ ^
|
|
|
|
|
| | | |
|
|
|
|
|
| | | Return type
|
|
|
|
|
| | Argument types
|
|
|
|
|
| |
|
2014-05-07 01:43:56 +00:00
|
|
|
|
| ABI
|
|
|
|
|
Function Style
|
2013-02-01 01:12:29 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let unsafety = self.parse_unsafety()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_opt_abi()?.unwrap_or(Abi::C)
|
2014-02-14 04:23:01 +00:00
|
|
|
|
} else {
|
2016-02-05 12:13:36 +00:00
|
|
|
|
Abi::Rust
|
2014-02-14 04:23:01 +00:00
|
|
|
|
};
|
2014-02-02 22:52:06 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Fn)?;
|
|
|
|
|
let (inputs, variadic) = self.parse_fn_args(false, true)?;
|
|
|
|
|
let ret_ty = self.parse_ret_ty()?;
|
2014-11-07 11:53:45 +00:00
|
|
|
|
let decl = P(FnDecl {
|
|
|
|
|
inputs: inputs,
|
|
|
|
|
output: ret_ty,
|
|
|
|
|
variadic: variadic
|
|
|
|
|
});
|
2016-02-08 15:53:21 +00:00
|
|
|
|
Ok(TyKind::BareFn(P(BareFnTy {
|
2014-04-02 08:19:41 +00:00
|
|
|
|
abi: abi,
|
2014-12-09 15:36:46 +00:00
|
|
|
|
unsafety: unsafety,
|
2014-11-07 11:53:45 +00:00
|
|
|
|
lifetimes: lifetime_defs,
|
2013-03-25 20:21:04 +00:00
|
|
|
|
decl: decl
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})))
|
2013-02-01 01:12:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_unsafety(&mut self) -> PResult<'a, Unsafety> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Unsafety::Unsafe);
|
2013-02-01 01:12:29 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Unsafety::Normal);
|
2013-02-01 01:12:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
|
/// Parse the items in a trait declaration
|
2016-06-11 01:00:07 +00:00
|
|
|
|
pub fn parse_trait_item(&mut self) -> PResult<'a, TraitItem> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtTraitItem, |x| x);
|
2016-06-11 01:00:07 +00:00
|
|
|
|
let mut attrs = self.parse_outer_attributes()?;
|
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
|
|
|
|
|
let (name, node) = if self.eat_keyword(keywords::Type) {
|
2016-05-17 16:51:45 +00:00
|
|
|
|
let TyParam {ident, bounds, default, ..} = self.parse_ty_param(vec![])?;
|
2016-06-11 01:00:07 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
|
|
|
|
(ident, TraitItemKind::Type(bounds, default))
|
|
|
|
|
} else if self.is_const_item() {
|
|
|
|
|
self.expect_keyword(keywords::Const)?;
|
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Colon)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-06-11 01:00:07 +00:00
|
|
|
|
let default = if self.check(&token::Eq) {
|
|
|
|
|
self.bump();
|
|
|
|
|
let expr = self.parse_expr()?;
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2016-06-11 01:00:07 +00:00
|
|
|
|
Some(expr)
|
2014-05-29 05:26:56 +00:00
|
|
|
|
} else {
|
2016-06-11 01:00:07 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
(ident, TraitItemKind::Const(ty, default))
|
2016-09-22 07:05:05 +00:00
|
|
|
|
} else if self.token.is_path_start() {
|
2016-09-22 22:44:59 +00:00
|
|
|
|
// trait item macro.
|
|
|
|
|
// code copied from parse_macro_use_or_failure... abstraction!
|
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
let pth = self.parse_path(PathStyle::Mod)?;
|
|
|
|
|
self.expect(&token::Not)?;
|
2016-06-11 01:00:07 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
// eat a matched-delimiter token tree:
|
|
|
|
|
let delim = self.expect_open_delim()?;
|
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|pp| pp.parse_token_tree())?;
|
|
|
|
|
if delim != token::Brace {
|
|
|
|
|
self.expect(&token::Semi)?
|
|
|
|
|
}
|
2016-09-22 22:26:35 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let mac = spanned(lo, self.prev_span.hi, Mac_ { path: pth, tts: tts });
|
2016-09-22 22:44:59 +00:00
|
|
|
|
(keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac))
|
|
|
|
|
} else {
|
|
|
|
|
let (constness, unsafety, abi) = match self.parse_fn_front_matter() {
|
|
|
|
|
Ok(cua) => cua,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
loop {
|
|
|
|
|
match self.token {
|
|
|
|
|
token::Eof => break,
|
|
|
|
|
token::CloseDelim(token::Brace) |
|
|
|
|
|
token::Semi => {
|
|
|
|
|
self.bump();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Brace) => {
|
|
|
|
|
self.parse_token_tree()?;
|
|
|
|
|
break;
|
2016-01-31 19:39:50 +00:00
|
|
|
|
}
|
2016-09-22 22:44:59 +00:00
|
|
|
|
_ => self.bump(),
|
2016-01-31 19:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-17 22:25:35 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-03-13 02:32:14 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
2014-08-11 16:32:26 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
|
|
|
|
|
// This is somewhat dubious; We don't want to allow
|
|
|
|
|
// argument names to be left off if there is a
|
|
|
|
|
// definition...
|
|
|
|
|
p.parse_arg_general(false)
|
|
|
|
|
})?;
|
2014-08-06 02:44:21 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
let sig = ast::MethodSig {
|
|
|
|
|
unsafety: unsafety,
|
|
|
|
|
constness: constness,
|
|
|
|
|
decl: d,
|
|
|
|
|
generics: generics,
|
|
|
|
|
abi: abi,
|
|
|
|
|
};
|
2012-07-10 20:44:20 +00:00
|
|
|
|
|
2016-09-22 22:44:59 +00:00
|
|
|
|
let body = match self.token {
|
|
|
|
|
token::Semi => {
|
|
|
|
|
self.bump();
|
|
|
|
|
debug!("parse_trait_methods(): parsing required method");
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Brace) => {
|
|
|
|
|
debug!("parse_trait_methods(): parsing provided method");
|
|
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
|
attrs.extend(inner_attrs.iter().cloned());
|
|
|
|
|
Some(body)
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
let token_str = self.this_token_to_string();
|
|
|
|
|
return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
|
|
|
|
|
}
|
2015-03-13 09:34:51 +00:00
|
|
|
|
};
|
2016-09-22 22:44:59 +00:00
|
|
|
|
(ident, ast::TraitItemKind::Method(sig, body))
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-11 01:00:07 +00:00
|
|
|
|
Ok(TraitItem {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
ident: name,
|
|
|
|
|
attrs: attrs,
|
|
|
|
|
node: node,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2016-06-11 01:00:07 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2015-03-10 10:28:44 +00:00
|
|
|
|
|
2016-06-11 01:00:07 +00:00
|
|
|
|
|
|
|
|
|
/// Parse the items in a trait declaration
|
|
|
|
|
pub fn parse_trait_items(&mut self) -> PResult<'a, Vec<TraitItem>> {
|
|
|
|
|
self.parse_unspanned_seq(
|
|
|
|
|
&token::OpenDelim(token::Brace),
|
|
|
|
|
&token::CloseDelim(token::Brace),
|
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| -> PResult<'a, TraitItem> {
|
|
|
|
|
p.parse_trait_item()
|
2016-02-11 20:33:09 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse optional return type [ -> TY ] in function decl
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::RArrow) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
|
2012-03-12 23:26:31 +00:00
|
|
|
|
} else {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let pos = self.span.lo;
|
2016-02-08 14:04:11 +00:00
|
|
|
|
Ok(FunctionRetTy::Default(mk_sp(pos, pos)))
|
2012-03-12 23:26:31 +00:00
|
|
|
|
}
|
2011-05-15 02:02:30 +00:00
|
|
|
|
}
|
2012-04-10 00:32:49 +00:00
|
|
|
|
|
2017-01-16 23:13:41 +00:00
|
|
|
|
/// Parse a type.
|
|
|
|
|
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
|
2014-11-20 20:05:29 +00:00
|
|
|
|
let lo = self.span.lo;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let lhs = self.parse_ty_no_plus()?;
|
2014-11-20 20:05:29 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::BinOp(token::Plus)) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(lhs);
|
2014-11-20 20:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 22:54:59 +00:00
|
|
|
|
let mut bounds = self.parse_ty_param_bounds()?;
|
2014-11-20 20:05:29 +00:00
|
|
|
|
|
|
|
|
|
// In type grammar, `+` is treated like a binary operator,
|
|
|
|
|
// and hence both L and R side are required.
|
2015-03-24 23:53:34 +00:00
|
|
|
|
if bounds.is_empty() {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.span_err(prev_span,
|
2014-11-20 20:05:29 +00:00
|
|
|
|
"at least one type parameter bound \
|
|
|
|
|
must be specified");
|
|
|
|
|
}
|
2017-01-19 10:28:45 +00:00
|
|
|
|
|
|
|
|
|
let mut lhs = lhs.unwrap();
|
|
|
|
|
if let TyKind::Paren(ty) = lhs.node {
|
|
|
|
|
// We have to accept the first bound in parens for backward compatibility.
|
|
|
|
|
// Example: `(Bound) + Bound + Bound`
|
|
|
|
|
lhs = ty.unwrap();
|
|
|
|
|
}
|
|
|
|
|
if let TyKind::Path(None, path) = lhs.node {
|
2017-01-16 20:33:45 +00:00
|
|
|
|
let poly_trait_ref = PolyTraitRef {
|
|
|
|
|
bound_lifetimes: Vec::new(),
|
2017-01-19 10:28:45 +00:00
|
|
|
|
trait_ref: TraitRef { path: path, ref_id: lhs.id },
|
2017-01-16 20:33:45 +00:00
|
|
|
|
span: lhs.span,
|
|
|
|
|
};
|
|
|
|
|
let poly_trait_ref = TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None);
|
|
|
|
|
bounds.insert(0, poly_trait_ref);
|
|
|
|
|
} else {
|
|
|
|
|
let mut err = struct_span_err!(self.sess.span_diagnostic, lhs.span, E0178,
|
|
|
|
|
"expected a path on the left-hand side \
|
|
|
|
|
of `+`, not `{}`",
|
|
|
|
|
pprust::ty_to_string(&lhs));
|
|
|
|
|
err.span_label(lhs.span, &format!("expected a path"));
|
|
|
|
|
let hi = bounds.iter().map(|x| match *x {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
TraitTyParamBound(ref tr, _) => tr.span.hi,
|
|
|
|
|
RegionTyParamBound(ref r) => r.span.hi,
|
2017-01-16 20:33:45 +00:00
|
|
|
|
}).max_by_key(|x| x.to_usize());
|
|
|
|
|
let full_span = hi.map(|hi| Span {
|
|
|
|
|
lo: lhs.span.lo,
|
|
|
|
|
hi: hi,
|
|
|
|
|
expn_id: lhs.span.expn_id,
|
|
|
|
|
});
|
|
|
|
|
match (&lhs.node, full_span) {
|
|
|
|
|
(&TyKind::Rptr(ref lifetime, ref mut_ty), Some(full_span)) => {
|
|
|
|
|
let ty_str = pprust::to_string(|s| {
|
|
|
|
|
use print::pp::word;
|
|
|
|
|
use print::pprust::PrintState;
|
|
|
|
|
|
|
|
|
|
word(&mut s.s, "&")?;
|
|
|
|
|
s.print_opt_lifetime(lifetime)?;
|
|
|
|
|
s.print_mutability(mut_ty.mutbl)?;
|
|
|
|
|
s.popen()?;
|
|
|
|
|
s.print_type(&mut_ty.ty)?;
|
|
|
|
|
s.print_bounds(" +", &bounds)?;
|
|
|
|
|
s.pclose()
|
|
|
|
|
});
|
|
|
|
|
err.span_suggestion(full_span, "try adding parentheses (per RFC 438):",
|
|
|
|
|
ty_str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
help!(&mut err,
|
|
|
|
|
"perhaps you forgot parentheses? (per RFC 438)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
2014-11-20 20:05:29 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let sp = mk_sp(lo, self.prev_span.hi);
|
2017-01-17 07:41:44 +00:00
|
|
|
|
let sum = TyKind::TraitObject(bounds);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp}))
|
2014-11-20 20:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 23:13:41 +00:00
|
|
|
|
/// Parse a type in restricted contexts where `+` is not permitted.
|
|
|
|
|
/// Example 1: `&'a TYPE`
|
|
|
|
|
/// `+` is prohibited to maintain operator priority (P(+) < P(&)).
|
|
|
|
|
/// Example 2: `value1 as TYPE + value2`
|
|
|
|
|
/// `+` is prohibited to avoid interactions with expression grammar.
|
|
|
|
|
pub fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtTy, |x| x);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let t = if self.eat(&token::OpenDelim(token::Paren)) {
|
2014-11-09 15:14:15 +00:00
|
|
|
|
// (t) is a parenthesized ty
|
|
|
|
|
// (t,) is the type of a tuple with only one field,
|
|
|
|
|
// of type t
|
|
|
|
|
let mut ts = vec![];
|
|
|
|
|
let mut last_comma = false;
|
|
|
|
|
while self.token != token::CloseDelim(token::Paren) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
ts.push(self.parse_ty()?);
|
2017-01-17 18:18:29 +00:00
|
|
|
|
if self.eat(&token::Comma) {
|
2014-11-09 15:14:15 +00:00
|
|
|
|
last_comma = true;
|
2014-06-11 19:14:38 +00:00
|
|
|
|
} else {
|
2014-11-09 15:14:15 +00:00
|
|
|
|
last_comma = false;
|
|
|
|
|
break;
|
2013-07-02 19:47:32 +00:00
|
|
|
|
}
|
2011-08-15 10:18:27 +00:00
|
|
|
|
}
|
2014-11-09 15:14:15 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
2014-11-09 15:14:15 +00:00
|
|
|
|
if ts.len() == 1 && !last_comma {
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Paren(ts.into_iter().nth(0).unwrap())
|
2014-11-09 15:14:15 +00:00
|
|
|
|
} else {
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Tup(ts)
|
2014-11-09 15:14:15 +00:00
|
|
|
|
}
|
2016-06-22 06:02:26 +00:00
|
|
|
|
} else if self.eat(&token::Not) {
|
2016-08-02 07:56:20 +00:00
|
|
|
|
TyKind::Never
|
2017-01-17 18:18:29 +00:00
|
|
|
|
} else if self.eat(&token::BinOp(token::Star)) {
|
2013-03-08 18:19:19 +00:00
|
|
|
|
// STAR POINTER (bare pointer?)
|
2016-03-23 03:01:37 +00:00
|
|
|
|
TyKind::Ptr(self.parse_ptr()?)
|
2017-01-17 18:18:29 +00:00
|
|
|
|
} else if self.eat(&token::OpenDelim(token::Bracket)) {
|
2013-03-08 18:19:19 +00:00
|
|
|
|
// VECTOR
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let t = self.parse_ty()?;
|
2012-08-14 02:59:32 +00:00
|
|
|
|
|
2015-01-17 23:55:21 +00:00
|
|
|
|
// Parse the `; e` in `[ i32; e ]`
|
2013-03-05 02:03:21 +00:00
|
|
|
|
// where `e` is a const expression
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let t = match self.maybe_parse_fixed_length_of_vec()? {
|
2016-09-20 14:54:24 +00:00
|
|
|
|
None => TyKind::Slice(t),
|
|
|
|
|
Some(suffix) => TyKind::Array(t, suffix)
|
2012-11-05 04:41:00 +00:00
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2012-02-06 14:29:56 +00:00
|
|
|
|
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
|
|
|
|
} else if self.check(&token::BinOp(token::And)) ||
|
2017-01-17 18:18:29 +00:00
|
|
|
|
self.check(&token::AndAnd) {
|
2013-03-08 18:19:19 +00:00
|
|
|
|
// BORROWED POINTER
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_and()?;
|
|
|
|
|
self.parse_borrowed_pointee()?
|
2015-01-16 03:04:28 +00:00
|
|
|
|
} else if self.check_keyword(keywords::For) {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// FIXME plus priority
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_for_in_type()?
|
2016-08-01 01:25:32 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::Impl) {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// FIXME plus priority
|
2016-08-01 01:25:32 +00:00
|
|
|
|
self.parse_impl_trait_type()?
|
2015-03-18 18:54:06 +00:00
|
|
|
|
} else if self.token_is_bare_fn_keyword() {
|
|
|
|
|
// BARE FUNCTION
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_ty_bare_fn(Vec::new())?
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword_noexpect(keywords::Typeof) {
|
2013-08-22 21:00:02 +00:00
|
|
|
|
// TYPEOF
|
|
|
|
|
// In order to not be ambiguous, the type must be surrounded by parens.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
|
let e = self.parse_expr()?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Typeof(e)
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_lt() {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let (qself, path) = self.parse_qualified_path(PathStyle::Type)?;
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Path(Some(qself), path)
|
2016-04-20 23:03:29 +00:00
|
|
|
|
} else if self.token.is_path_start() {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
let path = self.parse_path(PathStyle::Type)?;
|
2016-09-22 22:26:35 +00:00
|
|
|
|
if self.eat(&token::Not) {
|
2015-07-26 04:40:57 +00:00
|
|
|
|
// MACRO INVOCATION
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let delim = self.expect_open_delim()?;
|
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
2016-03-22 22:58:45 +00:00
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree())?;
|
2015-07-26 04:40:57 +00:00
|
|
|
|
let hi = self.span.hi;
|
2016-05-24 11:38:39 +00:00
|
|
|
|
TyKind::Mac(spanned(lo, hi, Mac_ { path: path, tts: tts }))
|
2015-07-26 04:40:57 +00:00
|
|
|
|
} else {
|
|
|
|
|
// NAMED TYPE
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Path(None, path)
|
2015-07-26 04:40:57 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat(&token::Underscore) {
|
2014-03-10 23:17:46 +00:00
|
|
|
|
// TYPE TO BE INFERRED
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Infer
|
2013-02-24 17:39:29 +00:00
|
|
|
|
} else {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
let msg = format!("expected type, found {}", self.this_token_descr());
|
|
|
|
|
return Err(self.fatal(&msg));
|
2013-02-24 17:39:29 +00:00
|
|
|
|
};
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let sp = mk_sp(lo, self.prev_span.hi);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp}))
|
2012-11-05 04:41:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:53:21 +00:00
|
|
|
|
pub fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
|
2013-03-14 18:22:51 +00:00
|
|
|
|
// look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let opt_lifetime = self.eat_lifetime();
|
|
|
|
|
let mutbl = self.parse_mutability()?;
|
|
|
|
|
let ty = self.parse_ty_no_plus()?;
|
|
|
|
|
return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty: ty, mutbl: mutbl }));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let mutbl = if self.eat_keyword(keywords::Mut) {
|
2016-02-09 16:44:47 +00:00
|
|
|
|
Mutability::Mutable
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::Const) {
|
2016-02-09 16:44:47 +00:00
|
|
|
|
Mutability::Immutable
|
2014-06-16 23:58:17 +00:00
|
|
|
|
} else {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let span = self.prev_span;
|
2014-06-25 19:00:27 +00:00
|
|
|
|
self.span_err(span,
|
2016-04-14 16:46:27 +00:00
|
|
|
|
"expected mut or const in raw pointer type (use \
|
|
|
|
|
`*mut T` or `*const T` as appropriate)");
|
2016-02-09 16:44:47 +00:00
|
|
|
|
Mutability::Immutable
|
2014-06-16 23:58:17 +00:00
|
|
|
|
};
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let t = self.parse_ty_no_plus()?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(MutTy { ty: t, mutbl: mutbl })
|
2014-06-16 23:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
|
pub fn is_named_argument(&mut self) -> bool {
|
2013-12-30 23:09:41 +00:00
|
|
|
|
let offset = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::And) => 1,
|
|
|
|
|
token::AndAnd => 1,
|
2014-10-27 12:33:30 +00:00
|
|
|
|
_ if self.token.is_keyword(keywords::Mut) => 1,
|
2013-07-18 03:04:37 +00:00
|
|
|
|
_ => 0
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
|
debug!("parser is_named_argument offset:{}", offset);
|
2013-09-02 23:58:12 +00:00
|
|
|
|
|
2012-09-26 08:47:21 +00:00
|
|
|
|
if offset == 0 {
|
2016-04-16 01:10:59 +00:00
|
|
|
|
is_ident_or_underscore(&self.token)
|
2014-10-27 08:22:52 +00:00
|
|
|
|
&& self.look_ahead(1, |t| *t == token::Colon)
|
2012-09-26 08:47:21 +00:00
|
|
|
|
} else {
|
2016-04-16 01:10:59 +00:00
|
|
|
|
self.look_ahead(offset, |t| is_ident_or_underscore(t))
|
2014-10-27 08:22:52 +00:00
|
|
|
|
&& self.look_ahead(offset + 1, |t| *t == token::Colon)
|
2012-09-26 08:47:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// This version of parse arg doesn't necessarily require
|
|
|
|
|
/// identifier names.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtArg, |x| x);
|
2015-11-11 20:19:01 +00:00
|
|
|
|
|
2012-11-07 02:41:06 +00:00
|
|
|
|
let pat = if require_name || self.is_named_argument() {
|
2014-10-15 01:59:41 +00:00
|
|
|
|
debug!("parse_arg_general parse_pat (require_name:{})",
|
2013-09-02 23:58:12 +00:00
|
|
|
|
require_name);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let pat = self.parse_pat()?;
|
2013-06-07 01:54:14 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Colon)?;
|
2012-11-07 02:41:06 +00:00
|
|
|
|
pat
|
2012-08-17 22:25:35 +00:00
|
|
|
|
} else {
|
2013-10-21 20:08:31 +00:00
|
|
|
|
debug!("parse_arg_general ident_to_pat");
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let sp = self.prev_span;
|
2016-04-18 21:42:18 +00:00
|
|
|
|
let spanned = Spanned { span: sp, node: keywords::Invalid.ident() };
|
2016-03-29 09:12:01 +00:00
|
|
|
|
P(Pat {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable),
|
|
|
|
|
spanned, None),
|
|
|
|
|
span: sp
|
|
|
|
|
})
|
2012-08-17 22:25:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let t = self.parse_ty()?;
|
2012-08-17 22:25:35 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(Arg {
|
2013-04-24 08:29:46 +00:00
|
|
|
|
ty: t,
|
|
|
|
|
pat: pat,
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2012-08-17 22:25:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a single function argument
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_arg(&mut self) -> PResult<'a, Arg> {
|
2013-09-14 02:07:43 +00:00
|
|
|
|
self.parse_arg_general(true)
|
2012-05-04 19:33:04 +00:00
|
|
|
|
}
|
2011-08-12 19:58:37 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse an argument in a lambda header e.g. |arg, arg|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let pat = self.parse_pat()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let t = if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
self.parse_ty()?
|
2013-02-26 00:49:28 +00:00
|
|
|
|
} else {
|
2013-11-30 22:00:39 +00:00
|
|
|
|
P(Ty {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-08 15:53:21 +00:00
|
|
|
|
node: TyKind::Infer,
|
2013-02-26 00:49:28 +00:00
|
|
|
|
span: mk_sp(self.span.lo, self.span.hi),
|
2013-11-30 22:00:39 +00:00
|
|
|
|
})
|
2013-02-26 00:49:28 +00:00
|
|
|
|
};
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(Arg {
|
2013-02-26 00:49:28 +00:00
|
|
|
|
ty: t,
|
|
|
|
|
pat: pat,
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2012-05-04 19:33:04 +00:00
|
|
|
|
}
|
2010-09-21 23:22:32 +00:00
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
if self.eat(&token::Semi) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
Ok(Some(self.parse_expr()?))
|
2012-08-14 02:59:32 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(None)
|
2012-08-14 02:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-18 17:44:20 +00:00
|
|
|
|
/// Matches token_lit = LIT_INTEGER | ...
|
2016-04-20 23:03:29 +00:00
|
|
|
|
pub fn parse_lit_token(&mut self) -> PResult<'a, LitKind> {
|
|
|
|
|
let out = match self.token {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
token::Interpolated(ref nt) => match **nt {
|
|
|
|
|
token::NtExpr(ref v) => match v.node {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
ExprKind::Lit(ref lit) => { lit.node.clone() }
|
|
|
|
|
_ => { return self.unexpected_last(&self.token); }
|
2016-11-02 03:03:55 +00:00
|
|
|
|
},
|
|
|
|
|
_ => { return self.unexpected_last(&self.token); }
|
|
|
|
|
},
|
2014-11-19 04:48:38 +00:00
|
|
|
|
token::Literal(lit, suf) => {
|
|
|
|
|
let (suffix_illegal, out) = match lit {
|
2016-02-08 16:06:20 +00:00
|
|
|
|
token::Byte(i) => (true, LitKind::Byte(parse::byte_lit(&i.as_str()).0)),
|
|
|
|
|
token::Char(i) => (true, LitKind::Char(parse::char_lit(&i.as_str()).0)),
|
2014-11-19 09:22:54 +00:00
|
|
|
|
|
|
|
|
|
// there are some valid suffixes for integer and
|
|
|
|
|
// float literals, so all the handling is done
|
|
|
|
|
// internally.
|
|
|
|
|
token::Integer(s) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
let diag = &self.sess.span_diagnostic;
|
|
|
|
|
(false, parse::integer_lit(&s.as_str(), suf, diag, self.span))
|
2014-11-19 09:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
token::Float(s) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
let diag = &self.sess.span_diagnostic;
|
|
|
|
|
(false, parse::float_lit(&s.as_str(), suf, diag, self.span))
|
2014-11-19 09:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 04:48:38 +00:00
|
|
|
|
token::Str_(s) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
let s = Symbol::intern(&parse::str_lit(&s.as_str()));
|
|
|
|
|
(true, LitKind::Str(s, ast::StrStyle::Cooked))
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
token::StrRaw(s, n) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
let s = Symbol::intern(&parse::raw_str_lit(&s.as_str()));
|
|
|
|
|
(true, LitKind::Str(s, ast::StrStyle::Raw(n)))
|
|
|
|
|
}
|
|
|
|
|
token::ByteStr(i) => {
|
|
|
|
|
(true, LitKind::ByteStr(parse::byte_str_lit(&i.as_str())))
|
|
|
|
|
}
|
|
|
|
|
token::ByteStrRaw(i, _) => {
|
|
|
|
|
(true, LitKind::ByteStr(Rc::new(i.to_string().into_bytes())))
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if suffix_illegal {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
let sp = self.span;
|
2016-02-08 22:55:55 +00:00
|
|
|
|
self.expect_no_suffix(sp, &format!("{} literal", lit.short_name()), suf)
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 23:03:29 +00:00
|
|
|
|
out
|
2014-01-10 22:02:36 +00:00
|
|
|
|
}
|
2016-04-20 23:03:29 +00:00
|
|
|
|
_ => { return self.unexpected_last(&self.token); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
|
Ok(out)
|
2011-10-07 14:22:53 +00:00
|
|
|
|
}
|
2011-07-06 00:57:34 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Matches lit = true | false | token_lit
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_lit(&mut self) -> PResult<'a, Lit> {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let lit = if self.eat_keyword(keywords::True) {
|
2016-02-08 16:06:20 +00:00
|
|
|
|
LitKind::Bool(true)
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::False) {
|
2016-02-08 16:06:20 +00:00
|
|
|
|
LitKind::Bool(false)
|
2012-04-27 19:22:42 +00:00
|
|
|
|
} else {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
let lit = self.parse_lit_token()?;
|
2013-07-02 19:47:32 +00:00
|
|
|
|
lit
|
2012-05-23 22:06:11 +00:00
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
Ok(codemap::Spanned { node: lit, span: mk_sp(lo, self.prev_span.hi) })
|
2012-04-27 19:22:42 +00:00
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// matches '-' lit | lit
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_pat_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
|
2013-05-11 01:19:58 +00:00
|
|
|
|
let minus_lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let minus_present = self.eat(&token::BinOp(token::Minus));
|
2013-05-11 01:19:58 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let literal = P(self.parse_lit()?);
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let expr = self.mk_expr(lo, hi, ExprKind::Lit(literal), ThinVec::new());
|
2013-05-11 01:19:58 +00:00
|
|
|
|
|
|
|
|
|
if minus_present {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let minus_hi = self.prev_span.hi;
|
2016-02-08 12:21:29 +00:00
|
|
|
|
let unary = self.mk_unary(UnOp::Neg, expr);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
Ok(self.mk_expr(minus_lo, minus_hi, unary, ThinVec::new()))
|
2013-05-11 01:19:58 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(expr)
|
2013-05-11 01:19:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 01:10:59 +00:00
|
|
|
|
pub fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> {
|
|
|
|
|
match self.token {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
token::Ident(sid) if self.token.is_path_segment_keyword() => {
|
2016-04-16 01:10:59 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
Ok(sid)
|
|
|
|
|
}
|
|
|
|
|
_ => self.parse_ident(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-22 23:17:03 +00:00
|
|
|
|
/// Parses qualified path.
|
|
|
|
|
///
|
|
|
|
|
/// Assumes that the leading `<` has been parsed already.
|
|
|
|
|
///
|
|
|
|
|
/// Qualifed paths are a part of the universal function call
|
|
|
|
|
/// syntax (UFCS).
|
|
|
|
|
///
|
|
|
|
|
/// `qualified_path = <type [as trait_ref]>::path`
|
|
|
|
|
///
|
|
|
|
|
/// See `parse_path` for `mode` meaning.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples:
|
|
|
|
|
///
|
|
|
|
|
/// `<T as U>::a`
|
|
|
|
|
/// `<T as U>::F::a::<S>`
|
2016-04-18 21:42:18 +00:00
|
|
|
|
pub fn parse_qualified_path(&mut self, mode: PathStyle)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, (QSelf, ast::Path)> {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let span = self.prev_span;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let self_type = self.parse_ty()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let mut path = if self.eat_keyword(keywords::As) {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
self.parse_path(PathStyle::Type)?
|
2015-03-25 16:53:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
ast::Path {
|
2015-07-08 05:47:27 +00:00
|
|
|
|
span: span,
|
2015-03-25 16:53:28 +00:00
|
|
|
|
segments: vec![]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let qself = QSelf {
|
|
|
|
|
ty: self_type,
|
|
|
|
|
position: path.segments.len()
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Gt)?;
|
|
|
|
|
self.expect(&token::ModSep)?;
|
2015-03-25 16:53:28 +00:00
|
|
|
|
|
2015-05-07 07:52:38 +00:00
|
|
|
|
let segments = match mode {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Type => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_path_segments_without_colons()?
|
2015-05-07 07:52:38 +00:00
|
|
|
|
}
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Expr => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_path_segments_with_colons()?
|
2015-05-07 07:52:38 +00:00
|
|
|
|
}
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Mod => {
|
|
|
|
|
self.parse_path_segments_without_types()?
|
2015-03-25 16:53:28 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2015-05-07 07:52:38 +00:00
|
|
|
|
path.segments.extend(segments);
|
2015-03-25 16:53:28 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
path.span.hi = self.prev_span.hi;
|
2015-03-25 16:53:28 +00:00
|
|
|
|
|
|
|
|
|
Ok((qself, path))
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
|
/// Parses a path and optional type parameter bounds, depending on the
|
|
|
|
|
/// mode. The `mode` parameter determines whether lifetimes, types, and/or
|
|
|
|
|
/// bounds are permitted and whether `::` must precede type parameter
|
|
|
|
|
/// groups.
|
2016-04-18 21:42:18 +00:00
|
|
|
|
pub fn parse_path(&mut self, mode: PathStyle) -> PResult<'a, ast::Path> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtPath, |x| x);
|
2013-08-07 16:47:28 +00:00
|
|
|
|
|
2013-03-27 19:36:10 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let is_global = self.eat(&token::ModSep);
|
2013-03-27 19:36:10 +00:00
|
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
|
// Parse any number of segments and bound sets. A segment is an
|
|
|
|
|
// identifier followed by an optional lifetime and a set of types.
|
|
|
|
|
// A bound set is a set of type parameter bounds.
|
2016-12-05 03:51:11 +00:00
|
|
|
|
let mut segments = match mode {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Type => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_path_segments_without_colons()?
|
2013-08-07 16:47:28 +00:00
|
|
|
|
}
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Expr => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_path_segments_with_colons()?
|
2013-02-26 19:35:17 +00:00
|
|
|
|
}
|
2016-04-18 21:42:18 +00:00
|
|
|
|
PathStyle::Mod => {
|
|
|
|
|
self.parse_path_segments_without_types()?
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
|
if is_global {
|
|
|
|
|
segments.insert(0, ast::PathSegment::crate_root());
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
|
// Assemble the span.
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let span = mk_sp(lo, self.prev_span.hi);
|
2013-06-17 19:16:30 +00:00
|
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
|
// Assemble the result.
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Path {
|
2014-11-20 20:05:29 +00:00
|
|
|
|
span: span,
|
|
|
|
|
segments: segments,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2013-06-17 19:16:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 22:03:40 +00:00
|
|
|
|
/// Examples:
|
|
|
|
|
/// - `a::b<T,U>::c<V,W>`
|
|
|
|
|
/// - `a::b<T,U>::c(V) -> W`
|
|
|
|
|
/// - `a::b<T,U>::c(V)`
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_path_segments_without_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
|
2014-10-29 22:03:40 +00:00
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
// First, parse an identifier.
|
2016-04-16 01:10:59 +00:00
|
|
|
|
let identifier = self.parse_path_segment_ident()?;
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
2016-09-01 23:58:44 +00:00
|
|
|
|
if self.check(&token::ModSep) && self.look_ahead(1, |t| *t == token::Lt) {
|
|
|
|
|
self.bump();
|
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
|
|
|
|
|
let mut err = self.diagnostic().struct_span_err(prev_span,
|
|
|
|
|
"unexpected token: `::`");
|
|
|
|
|
err.help(
|
|
|
|
|
"use `<...>` instead of `::<...>` if you meant to specify type arguments");
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 22:03:40 +00:00
|
|
|
|
// Parse types, optionally.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let parameters = if self.eat_lt() {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let (lifetimes, types, bindings) = self.parse_generic_args()?;
|
|
|
|
|
self.expect_gt()?;
|
2016-12-10 06:45:58 +00:00
|
|
|
|
ast::AngleBracketedParameterData {
|
2014-11-04 02:52:52 +00:00
|
|
|
|
lifetimes: lifetimes,
|
2017-01-16 22:54:59 +00:00
|
|
|
|
types: types,
|
|
|
|
|
bindings: bindings,
|
2016-12-10 06:45:58 +00:00
|
|
|
|
}.into()
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat(&token::OpenDelim(token::Paren)) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2015-01-10 16:54:15 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let inputs = self.parse_seq_to_end(
|
2014-11-04 02:52:52 +00:00
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2017-01-16 23:13:41 +00:00
|
|
|
|
|p| p.parse_ty())?;
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let output_ty = if self.eat(&token::RArrow) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
Some(self.parse_ty_no_plus()?)
|
2014-11-04 02:52:52 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2015-01-10 16:54:15 +00:00
|
|
|
|
|
2016-12-10 06:45:58 +00:00
|
|
|
|
Some(P(ast::PathParameters::Parenthesized(ast::ParenthesizedParameterData {
|
2015-01-10 16:54:15 +00:00
|
|
|
|
span: mk_sp(lo, hi),
|
2014-11-04 02:52:52 +00:00
|
|
|
|
inputs: inputs,
|
2015-01-10 16:54:15 +00:00
|
|
|
|
output: output_ty,
|
2016-12-10 06:45:58 +00:00
|
|
|
|
})))
|
2014-10-29 22:03:40 +00:00
|
|
|
|
} else {
|
2016-12-10 06:45:58 +00:00
|
|
|
|
None
|
2014-10-29 22:03:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Assemble and push the result.
|
2016-12-10 06:45:58 +00:00
|
|
|
|
segments.push(ast::PathSegment { identifier: identifier, parameters: parameters });
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
|
|
// Continue only if we see a `::`
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::ModSep) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(segments);
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Examples:
|
|
|
|
|
/// - `a::b::<T,U>::c`
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_path_segments_with_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
|
2014-10-29 22:03:40 +00:00
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
// First, parse an identifier.
|
2016-04-16 01:10:59 +00:00
|
|
|
|
let identifier = self.parse_path_segment_ident()?;
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
|
|
// If we do not see a `::`, stop.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::ModSep) {
|
2016-12-10 06:45:58 +00:00
|
|
|
|
segments.push(identifier.into());
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(segments);
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for a type segment.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_lt() {
|
2014-10-29 22:03:40 +00:00
|
|
|
|
// Consumed `a::b::<`, go look for types
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let (lifetimes, types, bindings) = self.parse_generic_args()?;
|
|
|
|
|
self.expect_gt()?;
|
2014-11-04 02:52:52 +00:00
|
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
|
identifier: identifier,
|
2016-12-10 06:45:58 +00:00
|
|
|
|
parameters: ast::AngleBracketedParameterData {
|
|
|
|
|
lifetimes: lifetimes,
|
2017-01-16 22:54:59 +00:00
|
|
|
|
types: types,
|
|
|
|
|
bindings: bindings,
|
2016-12-10 06:45:58 +00:00
|
|
|
|
}.into(),
|
2014-11-04 02:52:52 +00:00
|
|
|
|
});
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
|
|
// Consumed `a::b::<T,U>`, check for `::` before proceeding
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::ModSep) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(segments);
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Consumed `a::`, go look for `b`
|
2016-12-10 06:45:58 +00:00
|
|
|
|
segments.push(identifier.into());
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Examples:
|
|
|
|
|
/// - `a::b::c`
|
2016-04-18 21:42:18 +00:00
|
|
|
|
pub fn parse_path_segments_without_types(&mut self)
|
2016-04-17 00:48:40 +00:00
|
|
|
|
-> PResult<'a, Vec<ast::PathSegment>> {
|
2014-10-29 22:03:40 +00:00
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
// First, parse an identifier.
|
2016-04-16 01:10:59 +00:00
|
|
|
|
let identifier = self.parse_path_segment_ident()?;
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
|
|
// Assemble and push the result.
|
2016-12-10 06:45:58 +00:00
|
|
|
|
segments.push(identifier.into());
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
2016-04-17 00:48:40 +00:00
|
|
|
|
// If we do not see a `::` or see `::{`/`::*`, stop.
|
2016-04-18 21:42:18 +00:00
|
|
|
|
if !self.check(&token::ModSep) || self.is_import_coupler() {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(segments);
|
2016-04-17 00:48:40 +00:00
|
|
|
|
} else {
|
|
|
|
|
self.bump();
|
2014-10-29 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
/// Parse single lifetime 'a or nothing.
|
|
|
|
|
pub fn eat_lifetime(&mut self) -> Option<Lifetime> {
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
token::Lifetime(ident) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2017-01-17 18:18:29 +00:00
|
|
|
|
Some(Lifetime {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-01-17 18:18:29 +00:00
|
|
|
|
span: self.prev_span,
|
|
|
|
|
name: ident.name
|
|
|
|
|
})
|
2014-08-06 02:59:24 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
_ => None
|
2014-08-06 02:59:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 18:18:21 +00:00
|
|
|
|
/// Parse mutability (`mut` or nothing).
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_mutability(&mut self) -> PResult<'a, Mutability> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Mut) {
|
2016-02-09 16:44:47 +00:00
|
|
|
|
Ok(Mutability::Mutable)
|
2012-04-24 22:52:52 +00:00
|
|
|
|
} else {
|
2016-02-09 16:44:47 +00:00
|
|
|
|
Ok(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
|
|
|
|
|
2016-07-29 20:47:55 +00:00
|
|
|
|
pub fn parse_field_name(&mut self) -> PResult<'a, Ident> {
|
|
|
|
|
if let token::Literal(token::Integer(name), None) = self.token {
|
|
|
|
|
self.bump();
|
|
|
|
|
Ok(Ident::with_empty_ctxt(name))
|
|
|
|
|
} else {
|
|
|
|
|
self.parse_ident()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 00:15:13 +00:00
|
|
|
|
/// Parse ident (COLON expr)?
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_field(&mut self) -> PResult<'a, Field> {
|
2017-01-04 03:13:01 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-10-27 00:15:13 +00:00
|
|
|
|
let hi;
|
|
|
|
|
|
|
|
|
|
// Check if a colon exists one ahead. This means we're parsing a fieldname.
|
|
|
|
|
let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
|
|
|
|
|
let fieldname = self.parse_field_name()?;
|
|
|
|
|
self.bump();
|
|
|
|
|
hi = self.prev_span.hi;
|
|
|
|
|
(fieldname, self.parse_expr()?, false)
|
|
|
|
|
} else {
|
|
|
|
|
let fieldname = self.parse_ident()?;
|
|
|
|
|
hi = self.prev_span.hi;
|
|
|
|
|
|
|
|
|
|
// Mimic `x: x` for the `x` field shorthand.
|
|
|
|
|
let path = ast::Path::from_ident(mk_sp(lo, hi), fieldname);
|
|
|
|
|
(fieldname, self.mk_expr(lo, hi, ExprKind::Path(None, path), ThinVec::new()), true)
|
|
|
|
|
};
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Field {
|
2016-10-27 00:15:13 +00:00
|
|
|
|
ident: spanned(lo, hi, fieldname),
|
|
|
|
|
span: mk_sp(lo, expr.span.hi),
|
|
|
|
|
expr: expr,
|
|
|
|
|
is_shorthand: is_shorthand,
|
2017-01-04 03:13:01 +00:00
|
|
|
|
attrs: attrs.into(),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-09-28 01:25:02 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: ExprKind, attrs: ThinVec<Attribute>)
|
|
|
|
|
-> P<Expr> {
|
2014-09-13 16:06:01 +00:00
|
|
|
|
P(Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-15 21:51:43 +00:00
|
|
|
|
node: node,
|
|
|
|
|
span: mk_sp(lo, hi),
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs: attrs.into(),
|
2014-09-13 16:06:01 +00:00
|
|
|
|
})
|
2010-10-12 23:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::Unary(unop, expr)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::Binary(binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::Call(f, args)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 21:19:23 +00:00
|
|
|
|
fn mk_method_call(&mut self,
|
|
|
|
|
ident: ast::SpannedIdent,
|
|
|
|
|
tps: Vec<P<Ty>>,
|
2014-09-13 16:06:01 +00:00
|
|
|
|
args: Vec<P<Expr>>)
|
2016-02-08 15:05:05 +00:00
|
|
|
|
-> ast::ExprKind {
|
|
|
|
|
ExprKind::MethodCall(ident, tps, args)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::Index(expr, idx)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-13 05:41:02 +00:00
|
|
|
|
pub fn mk_range(&mut self,
|
2014-12-30 07:07:25 +00:00
|
|
|
|
start: Option<P<Expr>>,
|
2016-01-13 06:23:31 +00:00
|
|
|
|
end: Option<P<Expr>>,
|
|
|
|
|
limits: RangeLimits)
|
2016-03-17 01:35:36 +00:00
|
|
|
|
-> PResult<'a, ast::ExprKind> {
|
|
|
|
|
if end.is_none() && limits == RangeLimits::Closed {
|
|
|
|
|
Err(self.span_fatal_help(self.span,
|
|
|
|
|
"inclusive range with no end",
|
2016-03-18 23:04:43 +00:00
|
|
|
|
"inclusive ranges must be bounded at the end \
|
|
|
|
|
(`...b` or `a...b`)"))
|
2016-03-17 01:35:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
Ok(ExprKind::Range(start, end, limits))
|
|
|
|
|
}
|
2014-12-13 05:41:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::ExprKind {
|
|
|
|
|
ExprKind::Field(expr, ident)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<usize>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::TupField(expr, idx)
|
2014-08-10 03:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 07:16:13 +00:00
|
|
|
|
pub fn mk_assign_op(&mut self, binop: ast::BinOp,
|
2016-02-08 15:05:05 +00:00
|
|
|
|
lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
|
|
|
|
|
ExprKind::AssignOp(binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
m: Mac_, attrs: ThinVec<Attribute>) -> P<Expr> {
|
2014-09-13 16:06:01 +00:00
|
|
|
|
P(Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-08 15:05:05 +00:00
|
|
|
|
node: ExprKind::Mac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
|
2013-01-15 21:51:43 +00:00
|
|
|
|
span: mk_sp(lo, hi),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs: attrs,
|
2014-09-13 16:06:01 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-12-14 23:32:13 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
pub fn mk_lit_u32(&mut self, i: u32, attrs: ThinVec<Attribute>) -> P<Expr> {
|
2013-12-30 23:17:53 +00:00
|
|
|
|
let span = &self.span;
|
2014-09-13 16:06:01 +00:00
|
|
|
|
let lv_lit = P(codemap::Spanned {
|
2016-08-23 00:56:52 +00:00
|
|
|
|
node: LitKind::Int(i as u128, ast::LitIntType::Unsigned(UintTy::U32)),
|
2013-02-22 02:12:13 +00:00
|
|
|
|
span: *span
|
2014-09-13 16:06:01 +00:00
|
|
|
|
});
|
2011-06-21 20:16:40 +00:00
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
|
P(Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-08 15:05:05 +00:00
|
|
|
|
node: ExprKind::Lit(lv_lit),
|
2013-02-22 02:12:13 +00:00
|
|
|
|
span: *span,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs: attrs,
|
2014-09-13 16:06:01 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn expect_open_delim(&mut self) -> PResult<'a, token::DelimToken> {
|
2015-01-16 03:04:28 +00:00
|
|
|
|
self.expected_tokens.push(TokenType::Token(token::Gt));
|
2014-10-29 14:47:53 +00:00
|
|
|
|
match self.token {
|
|
|
|
|
token::OpenDelim(delim) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(delim)
|
2014-10-29 14:47:53 +00:00
|
|
|
|
},
|
2015-03-28 21:58:51 +00:00
|
|
|
|
_ => Err(self.fatal("expected open delimiter")),
|
2014-10-29 14:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// At the bottom (top?) of the precedence hierarchy,
|
|
|
|
|
/// parse things like parenthesized exprs,
|
|
|
|
|
/// macros, return, etc.
|
2015-11-03 16:39:51 +00:00
|
|
|
|
///
|
|
|
|
|
/// NB: This does not parse outer attributes,
|
|
|
|
|
/// and is private because it only works
|
|
|
|
|
/// correctly if called from parse_dot_or_call_expr().
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
|
2012-08-23 00:24:52 +00:00
|
|
|
|
maybe_whole_expr!(self);
|
2013-03-02 21:02:27 +00:00
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// Outer attributes are already parsed and will be
|
|
|
|
|
// added to the return value after the fact.
|
|
|
|
|
//
|
|
|
|
|
// Therefore, prevent sub-parser from parsing
|
|
|
|
|
// attributes by giving them a empty "already parsed" list.
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let mut attrs = ThinVec::new();
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
let mut hi = self.span.hi;
|
2012-01-04 06:03:07 +00:00
|
|
|
|
|
2016-02-08 15:05:05 +00:00
|
|
|
|
let ex: ExprKind;
|
2012-01-04 06:03:07 +00:00
|
|
|
|
|
2015-01-10 23:14:03 +00:00
|
|
|
|
// Note: when adding new syntax here, don't forget to adjust Token::can_begin_expr().
|
2014-07-06 21:29:29 +00:00
|
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::OpenDelim(token::Paren) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-11-09 15:14:15 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2014-07-06 21:29:29 +00:00
|
|
|
|
// (e) is parenthesized e
|
|
|
|
|
// (e,) is a tuple with only one field, e
|
2014-11-09 15:14:15 +00:00
|
|
|
|
let mut es = vec![];
|
2014-07-06 21:29:29 +00:00
|
|
|
|
let mut trailing_comma = false;
|
2014-11-09 15:14:15 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Paren) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
es.push(self.parse_expr()?);
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect_one_of(&[], &[token::Comma, token::CloseDelim(token::Paren)])?;
|
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.check(&token::Comma) {
|
2014-07-06 21:29:29 +00:00
|
|
|
|
trailing_comma = true;
|
2014-11-09 15:14:15 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-11-09 15:14:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
trailing_comma = false;
|
|
|
|
|
break;
|
2014-07-06 21:29:29 +00:00
|
|
|
|
}
|
2013-02-17 23:41:47 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2013-10-28 22:22:49 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
hi = self.prev_span.hi;
|
2014-07-06 21:29:29 +00:00
|
|
|
|
return if es.len() == 1 && !trailing_comma {
|
2016-02-08 15:05:05 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, ExprKind::Paren(es.into_iter().nth(0).unwrap()), attrs))
|
2014-09-13 16:06:01 +00:00
|
|
|
|
} else {
|
2016-02-08 15:05:05 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, ExprKind::Tup(es), attrs))
|
2014-07-06 21:29:29 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::OpenDelim(token::Brace) => {
|
2016-02-08 11:44:45 +00:00
|
|
|
|
return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
|
2014-07-06 21:29:29 +00:00
|
|
|
|
},
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::Or) | token::OrOr => {
|
2015-05-15 23:24:06 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-02-08 14:27:08 +00:00
|
|
|
|
return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
|
2014-07-06 21:29:29 +00:00
|
|
|
|
},
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::OpenDelim(token::Bracket) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-07-06 22:11:44 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
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.check(&token::CloseDelim(token::Bracket)) {
|
2014-07-06 21:29:29 +00:00
|
|
|
|
// Empty vector.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2017-01-16 07:36:10 +00:00
|
|
|
|
ex = ExprKind::Array(Vec::new());
|
2014-07-06 21:29:29 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Nonempty vector.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let first_expr = self.parse_expr()?;
|
2014-12-30 06:55:06 +00:00
|
|
|
|
if self.check(&token::Semi) {
|
2015-08-11 15:35:22 +00:00
|
|
|
|
// Repeating array syntax: [ 0; 512 ]
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let count = self.parse_expr()?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ex = ExprKind::Repeat(first_expr, count);
|
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 if self.check(&token::Comma) {
|
2014-07-06 21:29:29 +00:00
|
|
|
|
// Vector with two or more elements.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let remaining_exprs = self.parse_seq_to_end(
|
2014-10-29 10:37:54 +00:00
|
|
|
|
&token::CloseDelim(token::Bracket),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2016-03-23 03:01:37 +00:00
|
|
|
|
|p| Ok(p.parse_expr()?)
|
2016-03-22 22:58:45 +00:00
|
|
|
|
)?;
|
2016-10-29 21:54:04 +00:00
|
|
|
|
let mut exprs = vec![first_expr];
|
2015-06-10 16:22:20 +00:00
|
|
|
|
exprs.extend(remaining_exprs);
|
2017-01-16 07:36:10 +00:00
|
|
|
|
ex = ExprKind::Array(exprs);
|
2014-07-06 21:29:29 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Vector with one element.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2017-01-16 07:36:10 +00:00
|
|
|
|
ex = ExprKind::Array(vec![first_expr]);
|
2014-07-06 21:29:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-21 02:09:22 +00:00
|
|
|
|
hi = self.prev_span.hi;
|
2014-08-06 02:44:21 +00:00
|
|
|
|
}
|
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
|
|
|
|
_ => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_lt() {
|
2015-03-25 16:53:28 +00:00
|
|
|
|
let (qself, path) =
|
2016-04-18 21:42:18 +00:00
|
|
|
|
self.parse_qualified_path(PathStyle::Expr)?;
|
2015-06-03 21:51:51 +00:00
|
|
|
|
hi = path.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ExprKind::Path(Some(qself), path), attrs));
|
2015-01-13 04:03:12 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Move) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2016-02-08 14:27:08 +00:00
|
|
|
|
return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
|
2014-07-23 19:43:29 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::If) {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_if_expr(attrs);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::For) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_for_expr(None, lo, attrs);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::While) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_while_expr(None, lo, attrs);
|
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
|
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
|
if self.token.is_lifetime() {
|
2016-05-02 16:22:03 +00:00
|
|
|
|
let label = Spanned { node: self.get_lifetime(),
|
|
|
|
|
span: self.span };
|
2015-07-16 07:22:57 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Colon)?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::While) {
|
2016-05-02 16:22:03 +00:00
|
|
|
|
return self.parse_while_expr(Some(label), lo, attrs)
|
2014-07-26 00:12:51 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::For) {
|
2016-05-02 16:22:03 +00:00
|
|
|
|
return self.parse_for_expr(Some(label), lo, attrs)
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Loop) {
|
2016-05-02 16:22:03 +00:00
|
|
|
|
return self.parse_loop_expr(Some(label), lo, attrs)
|
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
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.fatal("expected `while`, `for`, or `loop` after a label"))
|
2012-08-04 01:01:30 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Loop) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_loop_expr(None, lo, attrs);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Continue) {
|
2014-10-27 12:33:30 +00:00
|
|
|
|
let ex = if self.token.is_lifetime() {
|
2016-06-17 02:34:18 +00:00
|
|
|
|
let ex = ExprKind::Continue(Some(Spanned{
|
2015-09-02 19:29:41 +00:00
|
|
|
|
node: self.get_lifetime(),
|
|
|
|
|
span: self.span
|
|
|
|
|
}));
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-09-02 19:29:41 +00:00
|
|
|
|
ex
|
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 {
|
2016-06-17 02:34:18 +00:00
|
|
|
|
ExprKind::Continue(None)
|
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
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ex, attrs));
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Match) {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_match_expr(attrs);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
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
|
|
|
|
return self.parse_block_expr(
|
|
|
|
|
lo,
|
2016-02-08 11:44:45 +00:00
|
|
|
|
BlockCheckMode::Unsafe(ast::UserProvided),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Return) {
|
2014-10-27 12:33:30 +00:00
|
|
|
|
if self.token.can_begin_expr() {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let e = self.parse_expr()?;
|
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
|
|
|
|
hi = e.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ex = ExprKind::Ret(Some(e));
|
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 {
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ex = ExprKind::Ret(None);
|
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
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::Break) {
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
|
let lt = if self.token.is_lifetime() {
|
|
|
|
|
let spanned_lt = Spanned {
|
2015-09-02 19:29:41 +00:00
|
|
|
|
node: self.get_lifetime(),
|
|
|
|
|
span: self.span
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
|
};
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
|
Some(spanned_lt)
|
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 {
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
let e = if self.token.can_begin_expr()
|
|
|
|
|
&& !(self.token == token::OpenDelim(token::Brace)
|
|
|
|
|
&& self.restrictions.contains(
|
|
|
|
|
Restrictions::RESTRICTION_NO_STRUCT_LITERAL)) {
|
|
|
|
|
Some(self.parse_expr()?)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
ex = ExprKind::Break(lt, e);
|
2016-09-21 02:09:22 +00:00
|
|
|
|
hi = self.prev_span.hi;
|
2016-01-14 15:52:24 +00:00
|
|
|
|
} else if self.token.is_keyword(keywords::Let) {
|
|
|
|
|
// Catch this syntax error here, instead of in `check_strict_keywords`, so
|
|
|
|
|
// that we can explicitly mention that let is not to be used as an expression
|
2016-01-26 08:23:28 +00:00
|
|
|
|
let mut db = self.fatal("expected expression, found statement (`let`)");
|
|
|
|
|
db.note("variable declaration using `let` is a statement");
|
|
|
|
|
return Err(db);
|
2016-04-20 23:03:29 +00:00
|
|
|
|
} else if self.token.is_path_start() {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
let pth = self.parse_path(PathStyle::Expr)?;
|
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
|
|
|
|
|
|
|
|
|
// `!`, as an operator, is prefix, so we know this isn't that
|
2016-09-22 22:26:35 +00:00
|
|
|
|
if self.eat(&token::Not) {
|
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
|
|
|
|
// MACRO INVOCATION expression
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let delim = self.expect_open_delim()?;
|
2016-09-22 22:26:35 +00:00
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree())?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-09-22 22:26:35 +00:00
|
|
|
|
return Ok(self.mk_mac_expr(lo, hi, Mac_ { path: pth, tts: tts }, attrs));
|
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
|
|
|
|
}
|
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.check(&token::OpenDelim(token::Brace)) {
|
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
|
|
|
|
// This is a struct literal, unless we're prohibited
|
|
|
|
|
// from parsing struct literals here.
|
2015-04-28 23:36:22 +00:00
|
|
|
|
let prohibited = self.restrictions.contains(
|
|
|
|
|
Restrictions::RESTRICTION_NO_STRUCT_LITERAL
|
|
|
|
|
);
|
|
|
|
|
if !prohibited {
|
2016-09-22 04:45:29 +00:00
|
|
|
|
return self.parse_struct_expr(lo, pth, attrs);
|
2014-07-06 21:29:29 +00:00
|
|
|
|
}
|
2014-06-14 02:09:12 +00:00
|
|
|
|
}
|
2012-07-23 23:39:18 +00:00
|
|
|
|
|
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
|
|
|
|
hi = pth.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ex = ExprKind::Path(None, pth);
|
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 {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
match self.parse_lit() {
|
|
|
|
|
Ok(lit) => {
|
|
|
|
|
hi = lit.span.hi;
|
|
|
|
|
ex = ExprKind::Lit(P(lit));
|
|
|
|
|
}
|
|
|
|
|
Err(mut err) => {
|
2016-09-15 19:34:21 +00:00
|
|
|
|
self.cancel(&mut err);
|
2016-04-20 23:03:29 +00:00
|
|
|
|
let msg = format!("expected expression, found {}",
|
|
|
|
|
self.this_token_descr());
|
|
|
|
|
return Err(self.fatal(&msg));
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
2014-07-06 21:29:29 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ex, attrs));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 04:45:29 +00:00
|
|
|
|
fn parse_struct_expr(&mut self, lo: BytePos, pth: ast::Path, mut attrs: ThinVec<Attribute>)
|
|
|
|
|
-> PResult<'a, P<Expr>> {
|
|
|
|
|
self.bump();
|
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
|
let mut base = None;
|
|
|
|
|
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
|
|
|
|
|
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
|
|
|
|
if self.eat(&token::DotDot) {
|
|
|
|
|
match self.parse_expr() {
|
|
|
|
|
Ok(e) => {
|
|
|
|
|
base = Some(e);
|
|
|
|
|
}
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match self.parse_field() {
|
|
|
|
|
Ok(f) => fields.push(f),
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match self.expect_one_of(&[token::Comma],
|
|
|
|
|
&[token::CloseDelim(token::Brace)]) {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Brace))?;
|
|
|
|
|
return Ok(self.mk_expr(lo, hi, ExprKind::Struct(pth, fields, base), attrs));
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
fn parse_or_use_outer_attributes(&mut self,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
|
|
|
|
-> PResult<'a, ThinVec<Attribute>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
if let Some(attrs) = already_parsed_attrs {
|
|
|
|
|
Ok(attrs)
|
|
|
|
|
} else {
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.parse_outer_attributes().map(|a| a.into())
|
2015-11-03 16:39:51 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-09-28 01:25:02 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a block or unsafe block
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
outer_attrs: ThinVec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let mut attrs = outer_attrs;
|
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let blk = self.parse_block_tail(lo, blk_mode)?;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), attrs));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-10-06 23:42:27 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// parse a.b or a(13) or a[4] or just a
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn parse_dot_or_call_expr(&mut self,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let b = self.parse_bottom_expr();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, b) = self.interpolated_or_expr_span(b)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
self.parse_dot_or_call_expr_with(b, span.lo, attrs)
|
2015-11-03 16:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse_dot_or_call_expr_with(&mut self,
|
|
|
|
|
e0: P<Expr>,
|
2016-01-23 23:52:43 +00:00
|
|
|
|
lo: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
mut attrs: ThinVec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// Stitch the list of outer attributes onto the return value.
|
|
|
|
|
// A little bit ugly, but the best way given the current code
|
|
|
|
|
// structure
|
2016-01-23 23:52:43 +00:00
|
|
|
|
self.parse_dot_or_call_expr_with_(e0, lo)
|
2015-11-03 16:39:51 +00:00
|
|
|
|
.map(|expr|
|
|
|
|
|
expr.map(|mut expr| {
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend::<Vec<_>>(expr.attrs.into());
|
|
|
|
|
expr.attrs = attrs;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
match expr.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::If(..) | ExprKind::IfLet(..) => {
|
2016-06-18 04:01:57 +00:00
|
|
|
|
if !expr.attrs.is_empty() {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// Just point to the first attribute in there...
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let span = expr.attrs[0].span;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
|
|
|
|
self.span_err(span,
|
|
|
|
|
"attributes are not yet allowed on `if` \
|
|
|
|
|
expressions");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
expr
|
|
|
|
|
})
|
|
|
|
|
)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
|
|
2016-01-20 04:15:03 +00:00
|
|
|
|
// Assuming we have just parsed `.foo` (i.e., a dot and an ident), continue
|
|
|
|
|
// parsing into an expression.
|
2016-01-20 09:07:33 +00:00
|
|
|
|
fn parse_dot_suffix(&mut self,
|
|
|
|
|
ident: Ident,
|
|
|
|
|
ident_span: Span,
|
2016-01-26 10:34:32 +00:00
|
|
|
|
self_value: P<Expr>,
|
|
|
|
|
lo: BytePos)
|
2016-01-20 09:07:33 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2016-01-20 04:15:03 +00:00
|
|
|
|
let (_, tys, bindings) = if self.eat(&token::ModSep) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_lt()?;
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let args = self.parse_generic_args()?;
|
|
|
|
|
self.expect_gt()?;
|
|
|
|
|
args
|
2016-01-20 04:15:03 +00:00
|
|
|
|
} else {
|
|
|
|
|
(Vec::new(), Vec::new(), Vec::new())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !bindings.is_empty() {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.span_err(prev_span, "type bindings are only permitted on trait paths");
|
2016-01-20 04:15:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(match self.token {
|
|
|
|
|
// expr.f() method call.
|
|
|
|
|
token::OpenDelim(token::Paren) => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let mut es = self.parse_unspanned_seq(
|
2016-01-20 04:15:03 +00:00
|
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2016-03-23 03:01:37 +00:00
|
|
|
|
|p| Ok(p.parse_expr()?)
|
|
|
|
|
)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-01-20 04:15:03 +00:00
|
|
|
|
|
|
|
|
|
es.insert(0, self_value);
|
|
|
|
|
let id = spanned(ident_span.lo, ident_span.hi, ident);
|
|
|
|
|
let nd = self.mk_method_call(id, tys, es);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, hi, nd, ThinVec::new())
|
2016-01-20 04:15:03 +00:00
|
|
|
|
}
|
|
|
|
|
// Field access.
|
|
|
|
|
_ => {
|
|
|
|
|
if !tys.is_empty() {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.span_err(prev_span,
|
2016-01-20 04:15:03 +00:00
|
|
|
|
"field expressions may not \
|
|
|
|
|
have type parameters");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let id = spanned(ident_span.lo, ident_span.hi, ident);
|
|
|
|
|
let field = self.mk_field(self_value, id);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, ident_span.hi, field, ThinVec::new())
|
2016-01-20 04:15:03 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 23:52:43 +00:00
|
|
|
|
fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: BytePos) -> PResult<'a, P<Expr>> {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let mut e = e0;
|
2012-05-24 20:35:57 +00:00
|
|
|
|
let mut hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
loop {
|
2016-02-28 22:38:48 +00:00
|
|
|
|
// expr?
|
|
|
|
|
while self.eat(&token::Question) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
e = self.mk_expr(lo, hi, ExprKind::Try(e), ThinVec::new());
|
2016-02-28 22:38:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
// expr.f
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::Dot) {
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
token::Ident(i) => {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let dot_pos = self.prev_span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
hi = self.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-11-29 04:08:30 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
e = self.parse_dot_suffix(i, mk_sp(dot_pos, hi), e, lo)?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2014-11-19 04:48:38 +00:00
|
|
|
|
token::Literal(token::Integer(n), suf) => {
|
|
|
|
|
let sp = self.span;
|
2014-11-22 15:02:49 +00:00
|
|
|
|
|
|
|
|
|
// A tuple index may not have a suffix
|
2014-11-19 04:48:38 +00:00
|
|
|
|
self.expect_no_suffix(sp, "tuple index", suf);
|
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let dot = self.prev_span.hi;
|
2014-08-10 03:54:33 +00:00
|
|
|
|
hi = self.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-08-10 03:54:33 +00:00
|
|
|
|
|
2015-01-28 06:52:32 +00:00
|
|
|
|
let index = n.as_str().parse::<usize>().ok();
|
2014-11-22 15:02:49 +00:00
|
|
|
|
match index {
|
2014-08-10 03:54:33 +00:00
|
|
|
|
Some(n) => {
|
|
|
|
|
let id = spanned(dot, hi, n);
|
2014-11-23 11:14:35 +00:00
|
|
|
|
let field = self.mk_tup_field(e, id);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
e = self.mk_expr(lo, hi, field, ThinVec::new());
|
2014-08-10 03:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
None => {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.span_err(prev_span, "invalid tuple or tuple struct index");
|
2014-08-10 03:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-19 04:48:38 +00:00
|
|
|
|
token::Literal(token::Float(n), _suf) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2014-10-18 02:39:44 +00:00
|
|
|
|
let fstr = n.as_str();
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let mut err = self.diagnostic().struct_span_err(prev_span,
|
2016-11-17 14:04:20 +00:00
|
|
|
|
&format!("unexpected token: `{}`", n));
|
2015-02-19 13:36:58 +00:00
|
|
|
|
if fstr.chars().all(|x| "0123456789.".contains(x)) {
|
2015-01-28 06:52:32 +00:00
|
|
|
|
let float = match fstr.parse::<f64>().ok() {
|
2014-10-18 02:39:44 +00:00
|
|
|
|
Some(f) => f,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
2016-04-20 18:49:16 +00:00
|
|
|
|
err.help(&format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
|
|
|
|
|
float.trunc() as usize,
|
|
|
|
|
format!(".{}", fstr.splitn(2, ".").last().unwrap())));
|
2014-10-18 02:39:44 +00:00
|
|
|
|
}
|
2016-01-27 06:01:01 +00:00
|
|
|
|
return Err(err);
|
2014-08-10 03:54:33 +00:00
|
|
|
|
|
|
|
|
|
}
|
2016-01-20 04:15:03 +00:00
|
|
|
|
_ => {
|
|
|
|
|
// FIXME Could factor this out into non_fatal_unexpected or something.
|
|
|
|
|
let actual = self.this_token_to_string();
|
|
|
|
|
self.span_err(self.span, &format!("unexpected token: `{}`", actual));
|
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let dot_pos = self.prev_span.hi;
|
2016-04-18 21:42:18 +00:00
|
|
|
|
e = self.parse_dot_suffix(keywords::Invalid.ident(),
|
2016-03-22 22:58:45 +00:00
|
|
|
|
mk_sp(dot_pos, dot_pos),
|
|
|
|
|
e, lo)?;
|
2016-01-20 04:15:03 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2013-10-01 21:31:03 +00:00
|
|
|
|
continue;
|
2012-01-31 12:31:02 +00:00
|
|
|
|
}
|
2016-02-08 22:55:55 +00:00
|
|
|
|
if self.expr_is_complete(&e) { break; }
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
// expr(...)
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::OpenDelim(token::Paren) => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let es = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2016-03-23 03:01:37 +00:00
|
|
|
|
|p| Ok(p.parse_expr()?)
|
|
|
|
|
)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
hi = self.prev_span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
2014-02-14 08:28:32 +00:00
|
|
|
|
let nd = self.mk_call(e, es);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
e = self.mk_expr(lo, hi, nd, ThinVec::new());
|
2011-12-21 04:12:52 +00:00
|
|
|
|
}
|
2012-05-31 01:14:40 +00:00
|
|
|
|
|
|
|
|
|
// expr[...]
|
2015-01-07 19:30:33 +00:00
|
|
|
|
// Could be either an index expression or a slicing expression.
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::OpenDelim(token::Bracket) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ix = self.parse_expr()?;
|
2015-03-26 16:57:58 +00:00
|
|
|
|
hi = self.span.hi;
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2015-03-26 16:57:58 +00:00
|
|
|
|
let index = self.mk_index(e, ix);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
e = self.mk_expr(lo, hi, index, ThinVec::new())
|
2012-05-31 01:14:40 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
_ => return Ok(e)
|
2012-05-31 01:14:40 +00:00
|
|
|
|
}
|
2010-09-28 17:30:34 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(e);
|
2010-09-28 17:30:34 +00:00
|
|
|
|
}
|
2010-09-28 01:25:02 +00:00
|
|
|
|
|
2015-01-02 22:00:06 +00:00
|
|
|
|
// Parse unquoted tokens after a `$` in a token tree
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_unquoted(&mut self) -> PResult<'a, TokenTree> {
|
2015-01-02 22:00:06 +00:00
|
|
|
|
let mut sp = self.span;
|
2016-04-16 01:12:02 +00:00
|
|
|
|
let name = match self.token {
|
2015-01-02 22:00:06 +00:00
|
|
|
|
token::Dollar => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-01-02 22:00:06 +00:00
|
|
|
|
|
|
|
|
|
if self.token == token::OpenDelim(token::Paren) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let Spanned { node: seq, span: seq_span } = self.parse_seq(
|
2015-01-02 22:00:06 +00:00
|
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::none(),
|
2015-01-02 22:00:06 +00:00
|
|
|
|
|p| p.parse_token_tree()
|
2016-03-23 03:01:37 +00:00
|
|
|
|
)?;
|
|
|
|
|
let (sep, repeat) = self.parse_sep_and_kleene_op()?;
|
2015-02-02 02:53:25 +00:00
|
|
|
|
let name_num = macro_parser::count_names(&seq);
|
2016-07-04 10:25:50 +00:00
|
|
|
|
return Ok(TokenTree::Sequence(mk_sp(sp.lo, seq_span.hi),
|
|
|
|
|
Rc::new(SequenceRepetition {
|
|
|
|
|
tts: seq,
|
|
|
|
|
separator: sep,
|
|
|
|
|
op: repeat,
|
|
|
|
|
num_captures: name_num
|
|
|
|
|
})));
|
2016-04-16 01:10:59 +00:00
|
|
|
|
} else if self.token.is_keyword(keywords::Crate) {
|
2016-10-16 03:39:52 +00:00
|
|
|
|
let ident = match self.token {
|
2016-11-16 08:21:52 +00:00
|
|
|
|
token::Ident(id) => ast::Ident { name: Symbol::intern("$crate"), ..id },
|
2016-10-16 03:39:52 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-10-16 03:39:52 +00:00
|
|
|
|
return Ok(TokenTree::Token(sp, token::Ident(ident)));
|
2015-01-02 22:00:06 +00:00
|
|
|
|
} else {
|
|
|
|
|
sp = mk_sp(sp.lo, self.span.hi);
|
2016-05-25 20:05:47 +00:00
|
|
|
|
self.parse_ident().unwrap_or_else(|mut e| {
|
|
|
|
|
e.emit();
|
|
|
|
|
keywords::Invalid.ident()
|
|
|
|
|
})
|
2015-01-02 22:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-16 01:12:02 +00:00
|
|
|
|
token::SubstNt(name) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-04-16 01:12:02 +00:00
|
|
|
|
name
|
2015-01-02 22:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
_ => unreachable!()
|
|
|
|
|
};
|
|
|
|
|
// continue by trying to parse the `:ident` after `$name`
|
2016-04-18 21:42:18 +00:00
|
|
|
|
if self.token == token::Colon &&
|
|
|
|
|
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword()) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-01-02 22:00:06 +00:00
|
|
|
|
sp = mk_sp(sp.lo, self.span.hi);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let nt_kind = self.parse_ident()?;
|
2016-04-16 01:12:02 +00:00
|
|
|
|
Ok(TokenTree::Token(sp, MatchNt(name, nt_kind)))
|
2015-01-02 22:00:06 +00:00
|
|
|
|
} else {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
Ok(TokenTree::Token(sp, SubstNt(name)))
|
2015-01-02 22:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
pub fn check_unknown_macro_variable(&mut self) {
|
2016-07-19 20:01:54 +00:00
|
|
|
|
if self.quote_depth == 0 && !self.parsing_token_tree {
|
2015-01-02 22:00:06 +00:00
|
|
|
|
match self.token {
|
2016-04-16 01:12:02 +00:00
|
|
|
|
token::SubstNt(name) =>
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.fatal(&format!("unknown macro variable `{}`", name)).emit(),
|
2015-01-02 22:00:06 +00:00
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 00:24:20 +00:00
|
|
|
|
/// Parse an optional separator followed by a Kleene-style
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// repetition token (+ or *).
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_sep_and_kleene_op(&mut self)
|
2016-06-20 15:49:33 +00:00
|
|
|
|
-> PResult<'a, (Option<token::Token>, tokenstream::KleeneOp)> {
|
|
|
|
|
fn parse_kleene_op<'a>(parser: &mut Parser<'a>) ->
|
|
|
|
|
PResult<'a, Option<tokenstream::KleeneOp>> {
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match parser.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::Star) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
parser.bump();
|
2016-06-20 15:49:33 +00:00
|
|
|
|
Ok(Some(tokenstream::KleeneOp::ZeroOrMore))
|
2014-10-23 00:24:20 +00:00
|
|
|
|
},
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::Plus) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
parser.bump();
|
2016-06-20 15:49:33 +00:00
|
|
|
|
Ok(Some(tokenstream::KleeneOp::OneOrMore))
|
2013-07-18 03:04:37 +00:00
|
|
|
|
},
|
2015-03-28 21:58:51 +00:00
|
|
|
|
_ => Ok(None)
|
2012-07-05 21:30:56 +00:00
|
|
|
|
}
|
2013-07-18 03:04:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-03 21:38:37 +00:00
|
|
|
|
if let Some(kleene_op) = parse_kleene_op(self)? {
|
|
|
|
|
return Ok((None, kleene_op));
|
2013-07-18 03:04:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let separator = self.bump_and_get();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
match parse_kleene_op(self)? {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Some(zerok) => Ok((Some(separator), zerok)),
|
|
|
|
|
None => return Err(self.fatal("expected `*` or `+`"))
|
2012-07-05 21:30:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// parse a single token tree from the input.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> {
|
2013-09-24 19:31:24 +00:00
|
|
|
|
// FIXME #6994: currently, this is too eager. It
|
2015-11-06 13:52:02 +00:00
|
|
|
|
// parses token trees but also identifies TokenType::Sequence's
|
2014-10-06 22:00:56 +00:00
|
|
|
|
// and token::SubstNt's; it's too early to know yet
|
2013-09-24 19:31:24 +00:00
|
|
|
|
// whether something will be a nonterminal or a seq
|
|
|
|
|
// yet.
|
2014-10-29 10:37:54 +00:00
|
|
|
|
match self.token {
|
|
|
|
|
token::OpenDelim(delim) => {
|
2017-01-13 04:49:20 +00:00
|
|
|
|
if self.quote_depth == 0 && self.tts.last().map(|&(_, i)| i == 1).unwrap_or(false) {
|
2016-11-03 07:43:29 +00:00
|
|
|
|
let tt = self.tts.pop().unwrap().0;
|
|
|
|
|
self.bump();
|
2017-01-13 04:49:20 +00:00
|
|
|
|
return Ok(tt);
|
2016-11-03 07:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 20:01:54 +00:00
|
|
|
|
let parsing_token_tree = ::std::mem::replace(&mut self.parsing_token_tree, true);
|
2017-01-23 04:58:15 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-22 03:02:09 +00:00
|
|
|
|
let tts = self.parse_seq_to_before_tokens(&[&token::CloseDelim(token::Brace),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Bracket)],
|
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree(),
|
|
|
|
|
|mut e| e.emit());
|
2017-01-13 04:49:20 +00:00
|
|
|
|
self.parsing_token_tree = parsing_token_tree;
|
|
|
|
|
self.bump();
|
2016-03-22 03:02:09 +00:00
|
|
|
|
|
2017-01-23 04:58:15 +00:00
|
|
|
|
Ok(TokenTree::Delimited(Span { lo: lo, ..self.prev_span }, Rc::new(Delimited {
|
2014-10-29 10:37:54 +00:00
|
|
|
|
delim: delim,
|
|
|
|
|
tts: tts,
|
2016-07-04 10:25:50 +00:00
|
|
|
|
})))
|
2014-10-29 10:37:54 +00:00
|
|
|
|
},
|
2017-01-13 04:49:20 +00:00
|
|
|
|
token::CloseDelim(_) | token::Eof => unreachable!(),
|
|
|
|
|
token::Dollar | token::SubstNt(..) if self.quote_depth > 0 => self.parse_unquoted(),
|
|
|
|
|
_ => Ok(TokenTree::Token(self.span, self.bump_and_get())),
|
2013-02-04 21:15:17 +00:00
|
|
|
|
}
|
2012-05-21 17:45:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
|
// parse a stream of tokens into a list of TokenTree's,
|
2013-04-15 23:13:42 +00:00
|
|
|
|
// up to EOF.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut tts = Vec::new();
|
2014-10-27 08:22:52 +00:00
|
|
|
|
while self.token != token::Eof {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
tts.push(self.parse_token_tree()?);
|
2012-11-21 00:07:57 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(tts)
|
2012-11-21 00:07:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 12:51:30 +00:00
|
|
|
|
/// Parse a prefix-unary-operator expr
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn parse_prefix_expr(&mut self,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2013-04-12 05:10:31 +00:00
|
|
|
|
let hi;
|
2015-01-10 23:14:03 +00:00
|
|
|
|
// Note: when adding new unary operators, don't forget to adjust Token::can_begin_expr()
|
2015-10-15 18:37:21 +00:00
|
|
|
|
let ex = match self.token {
|
|
|
|
|
token::Not => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let e = self.parse_prefix_expr(None);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, e) = self.interpolated_or_expr_span(e)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
hi = span.hi;
|
2016-02-08 12:21:29 +00:00
|
|
|
|
self.mk_unary(UnOp::Not, e)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
token::BinOp(token::Minus) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let e = self.parse_prefix_expr(None);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, e) = self.interpolated_or_expr_span(e)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
hi = span.hi;
|
2016-02-08 12:21:29 +00:00
|
|
|
|
self.mk_unary(UnOp::Neg, e)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
token::BinOp(token::Star) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let e = self.parse_prefix_expr(None);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, e) = self.interpolated_or_expr_span(e)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
hi = span.hi;
|
2016-02-08 12:21:29 +00:00
|
|
|
|
self.mk_unary(UnOp::Deref, e)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
token::BinOp(token::And) | token::AndAnd => {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_and()?;
|
|
|
|
|
let m = self.parse_mutability()?;
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let e = self.parse_prefix_expr(None);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, e) = self.interpolated_or_expr_span(e)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
hi = span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::AddrOf(m, e)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
token::Ident(..) if self.token.is_keyword(keywords::In) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let place = self.parse_expr_res(
|
2015-11-03 16:39:51 +00:00
|
|
|
|
Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
|
|
|
|
|
None,
|
2016-03-23 03:01:37 +00:00
|
|
|
|
)?;
|
|
|
|
|
let blk = self.parse_block()?;
|
2015-10-15 18:37:21 +00:00
|
|
|
|
let span = blk.span;
|
|
|
|
|
hi = span.hi;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let blk_expr = self.mk_expr(span.lo, hi, ExprKind::Block(blk), ThinVec::new());
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::InPlace(place, blk_expr)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
token::Ident(..) if self.token.is_keyword(keywords::Box) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-01-27 09:47:33 +00:00
|
|
|
|
let e = self.parse_prefix_expr(None);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (span, e) = self.interpolated_or_expr_span(e)?;
|
2016-01-26 10:34:32 +00:00
|
|
|
|
hi = span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::Box(e)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
|
_ => return self.parse_dot_or_call_expr(Some(attrs))
|
2015-10-15 18:37:21 +00:00
|
|
|
|
};
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ex, attrs));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 12:51:30 +00:00
|
|
|
|
/// Parse an associative expression
|
|
|
|
|
///
|
|
|
|
|
/// This parses an expression accounting for associativity and precedence of the operators in
|
|
|
|
|
/// the expression.
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn parse_assoc_expr(&mut self,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.parse_assoc_expr_with(0, already_parsed_attrs.into())
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 12:51:30 +00:00
|
|
|
|
/// Parse an associative expression with operators of at least `min_prec` precedence
|
2015-10-16 21:06:25 +00:00
|
|
|
|
pub fn parse_assoc_expr_with(&mut self,
|
|
|
|
|
min_prec: usize,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
lhs: LhsExpr)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
|
|
|
|
|
expr
|
2015-10-15 12:51:30 +00:00
|
|
|
|
} else {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
let attrs = match lhs {
|
|
|
|
|
LhsExpr::AttributesParsed(attrs) => Some(attrs),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2016-01-13 06:23:31 +00:00
|
|
|
|
if self.token == token::DotDot || self.token == token::DotDotDot {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_prefix_range_expr(attrs);
|
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_prefix_expr(attrs)?
|
2015-11-03 16:39:51 +00:00
|
|
|
|
}
|
2015-10-15 12:51:30 +00:00
|
|
|
|
};
|
2016-01-24 21:46:39 +00:00
|
|
|
|
|
2016-02-08 22:55:55 +00:00
|
|
|
|
if self.expr_is_complete(&lhs) {
|
2015-10-15 12:51:30 +00:00
|
|
|
|
// Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
|
|
|
|
|
return Ok(lhs);
|
|
|
|
|
}
|
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.push(TokenType::Operator);
|
2015-10-15 12:51:30 +00:00
|
|
|
|
while let Some(op) = AssocOp::from_token(&self.token) {
|
2016-01-24 21:46:39 +00:00
|
|
|
|
|
2016-09-21 02:16:28 +00:00
|
|
|
|
let lhs_span = if self.prev_token_kind == PrevTokenKind::Interpolated {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.prev_span
|
2016-01-27 10:26:38 +00:00
|
|
|
|
} else {
|
|
|
|
|
lhs.span
|
2016-01-24 21:46:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-12 20:34:41 +00:00
|
|
|
|
let cur_op_span = self.span;
|
2015-10-16 19:42:06 +00:00
|
|
|
|
let restrictions = if op.is_assign_like() {
|
|
|
|
|
self.restrictions & Restrictions::RESTRICTION_NO_STRUCT_LITERAL
|
|
|
|
|
} else {
|
|
|
|
|
self.restrictions
|
|
|
|
|
};
|
2015-10-15 12:51:30 +00:00
|
|
|
|
if op.precedence() < min_prec {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-10-15 12:51:30 +00:00
|
|
|
|
if op.is_comparison() {
|
2016-02-08 22:55:55 +00:00
|
|
|
|
self.check_no_chained_comparison(&lhs, &op);
|
2015-10-15 12:51:30 +00:00
|
|
|
|
}
|
|
|
|
|
// Special cases:
|
|
|
|
|
if op == AssocOp::As {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let rhs = self.parse_ty_no_plus()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let (lo, hi) = (lhs_span.lo, rhs.span.hi);
|
|
|
|
|
lhs = self.mk_expr(lo, hi, ExprKind::Cast(lhs, rhs), ThinVec::new());
|
2015-10-15 12:51:30 +00:00
|
|
|
|
continue
|
2015-12-03 02:37:48 +00:00
|
|
|
|
} else if op == AssocOp::Colon {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let rhs = self.parse_ty_no_plus()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let (lo, hi) = (lhs_span.lo, rhs.span.hi);
|
|
|
|
|
lhs = self.mk_expr(lo, hi, ExprKind::Type(lhs, rhs), ThinVec::new());
|
2015-12-03 02:37:48 +00:00
|
|
|
|
continue
|
2016-03-17 01:35:36 +00:00
|
|
|
|
} else if op == AssocOp::DotDot || op == AssocOp::DotDotDot {
|
|
|
|
|
// If we didn’t have to handle `x..`/`x...`, it would be pretty easy to
|
|
|
|
|
// generalise it to the Fixity::None code.
|
2016-01-13 06:23:31 +00:00
|
|
|
|
//
|
2016-03-17 01:35:36 +00:00
|
|
|
|
// We have 2 alternatives here: `x..y`/`x...y` and `x..`/`x...` The other
|
|
|
|
|
// two variants are handled with `parse_prefix_range_expr` call above.
|
2016-01-13 06:23:31 +00:00
|
|
|
|
let rhs = if self.is_at_start_of_range_notation_rhs() {
|
2016-05-01 16:43:27 +00:00
|
|
|
|
Some(self.parse_assoc_expr_with(op.precedence() + 1,
|
|
|
|
|
LhsExpr::NotYetParsed)?)
|
2016-01-13 06:23:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
let (lhs_span, rhs_span) = (lhs.span, if let Some(ref x) = rhs {
|
|
|
|
|
x.span
|
|
|
|
|
} else {
|
|
|
|
|
cur_op_span
|
|
|
|
|
});
|
2016-03-17 01:35:36 +00:00
|
|
|
|
let limits = if op == AssocOp::DotDot {
|
|
|
|
|
RangeLimits::HalfOpen
|
|
|
|
|
} else {
|
|
|
|
|
RangeLimits::Closed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let r = try!(self.mk_range(Some(lhs), rhs, limits));
|
2016-06-18 04:01:57 +00:00
|
|
|
|
lhs = self.mk_expr(lhs_span.lo, rhs_span.hi, r, ThinVec::new());
|
2016-01-13 06:23:31 +00:00
|
|
|
|
break
|
2015-10-15 12:51:30 +00:00
|
|
|
|
}
|
2013-07-18 03:04:37 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let rhs = match op.fixity() {
|
2015-12-14 20:32:16 +00:00
|
|
|
|
Fixity::Right => self.with_res(
|
2015-12-16 10:53:36 +00:00
|
|
|
|
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
|
|
|
|
|
|this| {
|
2015-12-14 20:32:16 +00:00
|
|
|
|
this.parse_assoc_expr_with(op.precedence(),
|
2015-12-16 10:53:36 +00:00
|
|
|
|
LhsExpr::NotYetParsed)
|
2015-10-16 19:42:06 +00:00
|
|
|
|
}),
|
2015-12-14 20:32:16 +00:00
|
|
|
|
Fixity::Left => self.with_res(
|
2015-12-16 10:53:36 +00:00
|
|
|
|
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
|
|
|
|
|
|this| {
|
2015-12-14 20:32:16 +00:00
|
|
|
|
this.parse_assoc_expr_with(op.precedence() + 1,
|
|
|
|
|
LhsExpr::NotYetParsed)
|
2015-10-16 19:42:06 +00:00
|
|
|
|
}),
|
2016-03-18 23:04:43 +00:00
|
|
|
|
// We currently have no non-associative operators that are not handled above by
|
|
|
|
|
// the special cases. The code is here only for future convenience.
|
2015-12-14 20:32:16 +00:00
|
|
|
|
Fixity::None => self.with_res(
|
2015-12-16 10:53:36 +00:00
|
|
|
|
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
|
|
|
|
|
|this| {
|
2015-12-14 20:32:16 +00:00
|
|
|
|
this.parse_assoc_expr_with(op.precedence() + 1,
|
|
|
|
|
LhsExpr::NotYetParsed)
|
2015-10-16 19:42:06 +00:00
|
|
|
|
}),
|
2016-03-23 03:01:37 +00:00
|
|
|
|
}?;
|
2015-10-15 12:51:30 +00:00
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let (lo, hi) = (lhs_span.lo, rhs.span.hi);
|
2015-10-15 12:51:30 +00:00
|
|
|
|
lhs = match op {
|
|
|
|
|
AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide |
|
|
|
|
|
AssocOp::Modulus | AssocOp::LAnd | AssocOp::LOr | AssocOp::BitXor |
|
|
|
|
|
AssocOp::BitAnd | AssocOp::BitOr | AssocOp::ShiftLeft | AssocOp::ShiftRight |
|
|
|
|
|
AssocOp::Equal | AssocOp::Less | AssocOp::LessEqual | AssocOp::NotEqual |
|
|
|
|
|
AssocOp::Greater | AssocOp::GreaterEqual => {
|
|
|
|
|
let ast_op = op.to_ast_binop().unwrap();
|
|
|
|
|
let binary = self.mk_binary(codemap::respan(cur_op_span, ast_op), lhs, rhs);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, hi, binary, ThinVec::new())
|
2013-01-31 18:32:57 +00:00
|
|
|
|
}
|
2015-10-15 12:51:30 +00:00
|
|
|
|
AssocOp::Assign =>
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, hi, ExprKind::Assign(lhs, rhs), ThinVec::new()),
|
2015-10-15 12:51:30 +00:00
|
|
|
|
AssocOp::Inplace =>
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, hi, ExprKind::InPlace(lhs, rhs), ThinVec::new()),
|
2015-10-15 12:51:30 +00:00
|
|
|
|
AssocOp::AssignOp(k) => {
|
|
|
|
|
let aop = match k {
|
2016-02-08 12:16:12 +00:00
|
|
|
|
token::Plus => BinOpKind::Add,
|
|
|
|
|
token::Minus => BinOpKind::Sub,
|
|
|
|
|
token::Star => BinOpKind::Mul,
|
|
|
|
|
token::Slash => BinOpKind::Div,
|
|
|
|
|
token::Percent => BinOpKind::Rem,
|
|
|
|
|
token::Caret => BinOpKind::BitXor,
|
|
|
|
|
token::And => BinOpKind::BitAnd,
|
|
|
|
|
token::Or => BinOpKind::BitOr,
|
|
|
|
|
token::Shl => BinOpKind::Shl,
|
|
|
|
|
token::Shr => BinOpKind::Shr,
|
2015-10-15 12:51:30 +00:00
|
|
|
|
};
|
|
|
|
|
let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
|
2016-06-18 04:01:57 +00:00
|
|
|
|
self.mk_expr(lo, hi, aopexpr, ThinVec::new())
|
2013-01-31 18:32:57 +00:00
|
|
|
|
}
|
2016-03-17 01:35:36 +00:00
|
|
|
|
AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotDot => {
|
|
|
|
|
self.bug("As, Colon, DotDot or DotDotDot branch reached")
|
2015-12-03 02:37:48 +00:00
|
|
|
|
}
|
2015-10-15 12:51:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if op.fixity() == Fixity::None { break }
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2015-10-15 12:51:30 +00:00
|
|
|
|
Ok(lhs)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-09-28 17:30:34 +00:00
|
|
|
|
|
2015-01-08 00:44:01 +00:00
|
|
|
|
/// Produce an error if comparison operators are chained (RFC #558).
|
|
|
|
|
/// We only need to check lhs, not rhs, because all comparison ops
|
|
|
|
|
/// have same precedence and are left-associative
|
2015-10-15 12:51:30 +00:00
|
|
|
|
fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: &AssocOp) {
|
|
|
|
|
debug_assert!(outer_op.is_comparison());
|
2015-01-08 00:44:01 +00:00
|
|
|
|
match lhs.node {
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
|
2015-01-13 04:18:55 +00:00
|
|
|
|
// respan to include both operators
|
|
|
|
|
let op_span = mk_sp(op.span.lo, self.span.hi);
|
2015-12-20 21:00:43 +00:00
|
|
|
|
let mut err = self.diagnostic().struct_span_err(op_span,
|
2015-01-13 04:18:55 +00:00
|
|
|
|
"chained comparison operators require parentheses");
|
2016-02-08 12:16:12 +00:00
|
|
|
|
if op.node == BinOpKind::Lt && *outer_op == AssocOp::Greater {
|
2016-04-20 18:49:16 +00:00
|
|
|
|
err.help(
|
2015-01-13 04:18:55 +00:00
|
|
|
|
"use `::<...>` instead of `<...>` if you meant to specify type arguments");
|
2015-01-08 00:44:01 +00:00
|
|
|
|
}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
err.emit();
|
2015-01-08 00:44:01 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 06:23:31 +00:00
|
|
|
|
/// Parse prefix-forms of range notation: `..expr`, `..`, `...expr`
|
2015-11-03 16:39:51 +00:00
|
|
|
|
fn parse_prefix_range_expr(&mut self,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2016-01-13 06:23:31 +00:00
|
|
|
|
debug_assert!(self.token == token::DotDot || self.token == token::DotDotDot);
|
|
|
|
|
let tok = self.token.clone();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
|
2015-10-15 18:37:21 +00:00
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
let mut hi = self.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-10-15 18:37:21 +00:00
|
|
|
|
let opt_end = if self.is_at_start_of_range_notation_rhs() {
|
2016-01-13 06:23:31 +00:00
|
|
|
|
// RHS must be parsed with more associativity than the dots.
|
|
|
|
|
let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
Some(self.parse_assoc_expr_with(next_prec,
|
2016-03-17 01:35:36 +00:00
|
|
|
|
LhsExpr::NotYetParsed)
|
|
|
|
|
.map(|x|{
|
|
|
|
|
hi = x.span.hi;
|
|
|
|
|
x
|
|
|
|
|
})?)
|
2015-10-15 18:37:21 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2016-03-17 01:35:36 +00:00
|
|
|
|
let limits = if tok == token::DotDot {
|
|
|
|
|
RangeLimits::HalfOpen
|
|
|
|
|
} else {
|
|
|
|
|
RangeLimits::Closed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let r = try!(self.mk_range(None,
|
|
|
|
|
opt_end,
|
|
|
|
|
limits));
|
2015-11-03 16:39:51 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, r, attrs))
|
2015-10-15 18:37:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 20:43:03 +00:00
|
|
|
|
fn is_at_start_of_range_notation_rhs(&self) -> bool {
|
|
|
|
|
if self.token.can_begin_expr() {
|
|
|
|
|
// parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
|
|
|
|
|
if self.token == token::OpenDelim(token::Brace) {
|
2015-04-28 23:36:22 +00:00
|
|
|
|
return !self.restrictions.contains(Restrictions::RESTRICTION_NO_STRUCT_LITERAL);
|
2015-01-18 20:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 01:04:29 +00:00
|
|
|
|
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
|
2016-06-18 04:01:57 +00:00
|
|
|
|
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Let) {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_if_let_expr(attrs);
|
2014-08-25 01:04:29 +00:00
|
|
|
|
}
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
let thn = self.parse_block()?;
|
2014-09-13 16:06:01 +00:00
|
|
|
|
let mut els: Option<P<Expr>> = None;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let mut hi = thn.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Else) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let elexpr = self.parse_else_expr()?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
hi = elexpr.span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
|
els = Some(elexpr);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2016-02-08 15:05:05 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, ExprKind::If(cond, thn, els), attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-12-08 19:47:01 +00:00
|
|
|
|
|
2014-08-25 01:04:29 +00:00
|
|
|
|
/// Parse an 'if let' expression ('if' token already eaten)
|
2016-06-18 04:01:57 +00:00
|
|
|
|
pub fn parse_if_let_expr(&mut self, attrs: ThinVec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let lo = self.prev_span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Let)?;
|
|
|
|
|
let pat = self.parse_pat()?;
|
|
|
|
|
self.expect(&token::Eq)?;
|
|
|
|
|
let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
let thn = self.parse_block()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let (hi, els) = if self.eat_keyword(keywords::Else) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let expr = self.parse_else_expr()?;
|
2014-08-25 01:04:29 +00:00
|
|
|
|
(expr.span.hi, Some(expr))
|
|
|
|
|
} else {
|
|
|
|
|
(thn.span.hi, None)
|
|
|
|
|
};
|
2016-02-08 15:05:05 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, ExprKind::IfLet(pat, expr, thn, els), attrs))
|
2014-08-25 01:04:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 18:44:07 +00:00
|
|
|
|
// `move |args| expr`
|
|
|
|
|
pub fn parse_lambda_expr(&mut self,
|
|
|
|
|
lo: BytePos,
|
2016-02-08 14:27:08 +00:00
|
|
|
|
capture_clause: CaptureBy,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs: ThinVec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>>
|
2014-11-19 16:18:17 +00:00
|
|
|
|
{
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let decl = self.parse_fn_block_decl()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let decl_hi = self.prev_span.hi;
|
2015-03-18 13:22:38 +00:00
|
|
|
|
let body = match decl.output {
|
2016-10-25 23:17:29 +00:00
|
|
|
|
FunctionRetTy::Default(_) => self.parse_expr()?,
|
2015-03-18 13:22:38 +00:00
|
|
|
|
_ => {
|
|
|
|
|
// If an explicit return type is given, require a
|
|
|
|
|
// block to appear (RFC 968).
|
2016-10-25 23:17:29 +00:00
|
|
|
|
let body_lo = self.span.lo;
|
|
|
|
|
self.parse_block_expr(body_lo, BlockCheckMode::Default, ThinVec::new())?
|
2015-03-18 13:22:38 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2013-07-16 18:08:35 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(self.mk_expr(
|
2014-11-19 16:18:17 +00:00
|
|
|
|
lo,
|
2015-03-18 13:22:38 +00:00
|
|
|
|
body.span.hi,
|
2016-04-20 18:44:07 +00:00
|
|
|
|
ExprKind::Closure(capture_clause, decl, body, mk_sp(lo, decl_hi)),
|
|
|
|
|
attrs))
|
2012-06-30 01:09:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// `else` token already eaten
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::If) {
|
2016-06-18 04:01:57 +00:00
|
|
|
|
return self.parse_if_expr(ThinVec::new());
|
2012-05-23 22:06:11 +00:00
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let blk = self.parse_block()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), ThinVec::new()));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-11-03 18:05:15 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a 'for' .. 'in' expression ('for' token already eaten)
|
2016-05-02 16:22:03 +00:00
|
|
|
|
pub fn parse_for_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
span_lo: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2013-08-03 03:20:22 +00:00
|
|
|
|
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
|
2013-07-30 00:25:00 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let pat = self.parse_pat()?;
|
|
|
|
|
self.expect_keyword(keywords::In)?;
|
|
|
|
|
let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(iattrs);
|
2015-11-03 16:39:51 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2013-07-30 00:25:00 +00:00
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
Ok(self.mk_expr(span_lo, hi,
|
2016-02-08 15:05:05 +00:00
|
|
|
|
ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs))
|
2013-07-30 00:25:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 02:45:46 +00:00
|
|
|
|
/// Parse a 'while' or 'while let' expression ('while' token already eaten)
|
2016-05-02 16:22:03 +00:00
|
|
|
|
pub fn parse_while_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
span_lo: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2014-10-27 12:33:30 +00:00
|
|
|
|
if self.token.is_keyword(keywords::Let) {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
return self.parse_while_let_expr(opt_ident, span_lo, attrs);
|
2014-10-03 02:45:46 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(iattrs);
|
2013-04-12 05:10:31 +00:00
|
|
|
|
let hi = body.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
return Ok(self.mk_expr(span_lo, hi, ExprKind::While(cond, body, opt_ident),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 02:45:46 +00:00
|
|
|
|
/// Parse a 'while let' expression ('while' token already eaten)
|
2016-05-02 16:22:03 +00:00
|
|
|
|
pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
span_lo: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Let)?;
|
|
|
|
|
let pat = self.parse_pat()?;
|
|
|
|
|
self.expect(&token::Eq)?;
|
|
|
|
|
let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(iattrs);
|
2014-10-03 02:45:46 +00:00
|
|
|
|
let hi = body.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
return Ok(self.mk_expr(span_lo, hi, ExprKind::WhileLet(pat, expr, body, opt_ident), attrs));
|
2014-10-03 02:45:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// parse `loop {...}`, `loop` token already eaten
|
2016-05-02 16:22:03 +00:00
|
|
|
|
pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
span_lo: BytePos,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(iattrs);
|
2014-05-22 17:49:26 +00:00
|
|
|
|
let hi = body.span.hi;
|
2016-02-08 15:05:05 +00:00
|
|
|
|
Ok(self.mk_expr(span_lo, hi, ExprKind::Loop(body, opt_ident), attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// `match` token already eaten
|
2016-06-18 04:01:57 +00:00
|
|
|
|
fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let match_span = self.prev_span;
|
|
|
|
|
let lo = self.prev_span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let discriminant = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
|
2016-03-22 22:58:45 +00:00
|
|
|
|
None)?;
|
2016-07-01 23:40:45 +00:00
|
|
|
|
if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) {
|
2015-10-27 13:41:55 +00:00
|
|
|
|
if self.token == token::Token::Semi {
|
2015-12-20 21:00:43 +00:00
|
|
|
|
e.span_note(match_span, "did you mean to remove this `match` keyword?");
|
2015-10-27 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
return Err(e)
|
|
|
|
|
}
|
2016-06-18 04:01:57 +00:00
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
|
|
|
|
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut arms: Vec<Arm> = Vec::new();
|
2014-10-29 10:37:54 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2016-02-10 03:11:27 +00:00
|
|
|
|
match self.parse_arm() {
|
|
|
|
|
Ok(arm) => arms.push(arm),
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
// Recover by skipping to the end of the block.
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
if self.token == token::CloseDelim(token::Brace) {
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
2016-02-15 00:14:31 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
|
2016-02-10 03:11:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2013-04-12 05:10:31 +00:00
|
|
|
|
let hi = self.span.hi;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-02-08 15:05:05 +00:00
|
|
|
|
return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-11-24 22:42:01 +00:00
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_arm(&mut self) -> PResult<'a, Arm> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtArm, |x| x);
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
|
let pats = self.parse_pats()?;
|
2014-07-29 00:32:51 +00:00
|
|
|
|
let mut guard = None;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::If) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
guard = Some(self.parse_expr()?);
|
2014-07-29 00:32:51 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::FatArrow)?;
|
|
|
|
|
let expr = self.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR, None)?;
|
2014-07-29 00:32:51 +00:00
|
|
|
|
|
|
|
|
|
let require_comma =
|
2016-02-08 22:55:55 +00:00
|
|
|
|
!classify::expr_is_simple_block(&expr)
|
2014-10-29 10:37:54 +00:00
|
|
|
|
&& self.token != token::CloseDelim(token::Brace);
|
2014-07-29 00:32:51 +00:00
|
|
|
|
|
|
|
|
|
if require_comma {
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])?;
|
2014-07-29 00:32:51 +00:00
|
|
|
|
} else {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.eat(&token::Comma);
|
2014-07-29 00:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Arm {
|
2014-07-29 00:32:51 +00:00
|
|
|
|
attrs: attrs,
|
|
|
|
|
pats: pats,
|
|
|
|
|
guard: guard,
|
|
|
|
|
body: expr,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2014-07-29 00:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse an expression
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.parse_expr_res(Restrictions::empty(), None)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
|
2015-10-16 19:42:06 +00:00
|
|
|
|
/// Evaluate the closure with restrictions in place.
|
|
|
|
|
///
|
|
|
|
|
/// After the closure is evaluated, restrictions are reset.
|
2016-02-11 00:52:44 +00:00
|
|
|
|
pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
|
|
|
|
|
where F: FnOnce(&mut Self) -> T
|
2015-12-20 21:00:43 +00:00
|
|
|
|
{
|
2014-09-16 05:22:12 +00:00
|
|
|
|
let old = self.restrictions;
|
|
|
|
|
self.restrictions = r;
|
2015-10-16 19:42:06 +00:00
|
|
|
|
let r = f(self);
|
2014-09-16 05:22:12 +00:00
|
|
|
|
self.restrictions = old;
|
2015-10-16 19:42:06 +00:00
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse an expression, subject to the given restrictions
|
2015-11-03 16:39:51 +00:00
|
|
|
|
pub fn parse_expr_res(&mut self, r: Restrictions,
|
2016-06-18 04:01:57 +00:00
|
|
|
|
already_parsed_attrs: Option<ThinVec<Attribute>>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Expr>> {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse the RHS of a local variable declaration (e.g. '= 14;')
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_initializer(&mut self) -> PResult<'a, Option<P<Expr>>> {
|
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.check(&token::Eq) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
Ok(Some(self.parse_expr()?))
|
2013-10-03 09:53:46 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(None)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-10-12 01:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse patterns, separated by '|' s
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut pats = Vec::new();
|
2012-05-23 22:06:11 +00:00
|
|
|
|
loop {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
pats.push(self.parse_pat()?);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.check(&token::BinOp(token::Or)) { self.bump();}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
else { return Ok(pats); }
|
2012-05-23 22:06:11 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2011-07-08 14:27:55 +00:00
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
|
fn parse_pat_tuple_elements(&mut self, unary_needs_comma: bool)
|
|
|
|
|
-> PResult<'a, (Vec<P<Pat>>, Option<usize>)> {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
let mut fields = vec![];
|
2016-03-06 12:54:44 +00:00
|
|
|
|
let mut ddpos = None;
|
|
|
|
|
|
|
|
|
|
while !self.check(&token::CloseDelim(token::Paren)) {
|
|
|
|
|
if ddpos.is_none() && self.eat(&token::DotDot) {
|
|
|
|
|
ddpos = Some(fields.len());
|
|
|
|
|
if self.eat(&token::Comma) {
|
|
|
|
|
// `..` needs to be followed by `)` or `, pat`, `..,)` is disallowed.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
fields.push(self.parse_pat()?);
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
|
} else if ddpos.is_some() && self.eat(&token::DotDot) {
|
|
|
|
|
// Emit a friendly error, ignore `..` and continue parsing
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.span_err(self.prev_span, "`..` can only be used once per \
|
2016-03-06 12:54:44 +00:00
|
|
|
|
tuple or tuple struct pattern");
|
2016-03-06 12:54:44 +00:00
|
|
|
|
} else {
|
|
|
|
|
fields.push(self.parse_pat()?);
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
|
|
|
|
|
|
if !self.check(&token::CloseDelim(token::Paren)) ||
|
|
|
|
|
(unary_needs_comma && fields.len() == 1 && ddpos.is_none()) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Comma)?;
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
|
|
|
|
|
|
Ok((fields, ddpos))
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 21:02:27 +00:00
|
|
|
|
fn parse_pat_vec_elements(
|
2013-12-30 22:04:00 +00:00
|
|
|
|
&mut self,
|
2015-12-20 21:00:43 +00:00
|
|
|
|
) -> PResult<'a, (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>)> {
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut before = Vec::new();
|
2013-02-26 18:58:46 +00:00
|
|
|
|
let mut slice = None;
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut after = Vec::new();
|
2012-12-08 20:22:43 +00:00
|
|
|
|
let mut first = true;
|
2013-02-26 18:58:46 +00:00
|
|
|
|
let mut before_slice = true;
|
2012-12-08 20:22:43 +00:00
|
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Bracket) {
|
2014-09-06 22:23:55 +00:00
|
|
|
|
if first {
|
|
|
|
|
first = false;
|
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Comma)?;
|
2014-11-30 04:39:50 +00:00
|
|
|
|
|
|
|
|
|
if self.token == token::CloseDelim(token::Bracket)
|
2015-03-24 23:54:09 +00:00
|
|
|
|
&& (before_slice || !after.is_empty()) {
|
2014-11-30 04:39:50 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2014-09-06 22:23:55 +00:00
|
|
|
|
}
|
2012-12-08 20:22:43 +00:00
|
|
|
|
|
2013-02-26 18:58:46 +00:00
|
|
|
|
if before_slice {
|
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.check(&token::DotDot) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2012-12-08 20:22:43 +00:00
|
|
|
|
|
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.check(&token::Comma) ||
|
|
|
|
|
self.check(&token::CloseDelim(token::Bracket)) {
|
2014-09-13 16:06:01 +00:00
|
|
|
|
slice = Some(P(ast::Pat {
|
2014-09-06 22:23:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-11 18:16:33 +00:00
|
|
|
|
node: PatKind::Wild,
|
2014-09-06 22:23:55 +00:00
|
|
|
|
span: self.span,
|
2014-09-13 16:06:01 +00:00
|
|
|
|
}));
|
2014-09-06 22:23:55 +00:00
|
|
|
|
before_slice = false;
|
2013-11-08 03:25:39 +00:00
|
|
|
|
}
|
2014-09-06 22:23:55 +00:00
|
|
|
|
continue
|
2012-12-08 20:22:43 +00:00
|
|
|
|
}
|
2014-09-06 22:23:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let subpat = self.parse_pat()?;
|
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 before_slice && self.check(&token::DotDot) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-09-06 22:23:55 +00:00
|
|
|
|
slice = Some(subpat);
|
|
|
|
|
before_slice = false;
|
|
|
|
|
} else if before_slice {
|
|
|
|
|
before.push(subpat);
|
2013-02-26 18:58:46 +00:00
|
|
|
|
} else {
|
2014-09-06 22:23:55 +00:00
|
|
|
|
after.push(subpat);
|
2012-12-08 20:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 18:58:46 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok((before, slice, after))
|
2012-12-08 20:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse the fields of a struct-like pattern
|
2016-06-21 22:08:13 +00:00
|
|
|
|
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<codemap::Spanned<ast::FieldPat>>, bool)> {
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut fields = Vec::new();
|
2012-08-07 00:01:14 +00:00
|
|
|
|
let mut etc = false;
|
|
|
|
|
let mut first = true;
|
2014-10-29 10:37:54 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2013-12-19 17:21:05 +00:00
|
|
|
|
if first {
|
|
|
|
|
first = false;
|
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Comma)?;
|
2013-12-19 17:21:05 +00:00
|
|
|
|
// accept trailing commas
|
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.check(&token::CloseDelim(token::Brace)) { break }
|
2013-12-19 17:21:05 +00:00
|
|
|
|
}
|
2012-08-07 00:01:14 +00:00
|
|
|
|
|
2017-01-04 03:13:01 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2014-10-06 00:36:53 +00:00
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
let hi;
|
|
|
|
|
|
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.check(&token::DotDot) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2014-10-29 10:37:54 +00:00
|
|
|
|
if self.token != token::CloseDelim(token::Brace) {
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let token_str = self.this_token_to_string();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.fatal(&format!("expected `{}`, found `{}`", "}",
|
|
|
|
|
token_str)))
|
2012-08-07 00:01:14 +00:00
|
|
|
|
}
|
|
|
|
|
etc = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 16:58:01 +00:00
|
|
|
|
// Check if a colon exists one ahead. This means we're parsing a fieldname.
|
|
|
|
|
let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
|
|
|
|
|
// Parsing a pattern of the form "fieldname: pat"
|
2016-07-29 20:47:55 +00:00
|
|
|
|
let fieldname = self.parse_field_name()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let pat = self.parse_pat()?;
|
2014-10-06 00:36:53 +00:00
|
|
|
|
hi = pat.span.hi;
|
2015-01-19 16:58:01 +00:00
|
|
|
|
(pat, fieldname, false)
|
2012-08-07 00:01:14 +00:00
|
|
|
|
} else {
|
2015-01-19 16:58:01 +00:00
|
|
|
|
// Parsing a pattern of the form "(box) (ref) (mut) fieldname"
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let is_box = self.eat_keyword(keywords::Box);
|
2015-01-19 16:58:01 +00:00
|
|
|
|
let boxed_span_lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let is_ref = self.eat_keyword(keywords::Ref);
|
|
|
|
|
let is_mut = self.eat_keyword(keywords::Mut);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let fieldname = self.parse_ident()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
hi = self.prev_span.hi;
|
2015-01-19 16:58:01 +00:00
|
|
|
|
|
|
|
|
|
let bind_type = match (is_ref, is_mut) {
|
2016-02-09 16:44:47 +00:00
|
|
|
|
(true, true) => BindingMode::ByRef(Mutability::Mutable),
|
|
|
|
|
(true, false) => BindingMode::ByRef(Mutability::Immutable),
|
|
|
|
|
(false, true) => BindingMode::ByValue(Mutability::Mutable),
|
|
|
|
|
(false, false) => BindingMode::ByValue(Mutability::Immutable),
|
2015-01-19 16:58:01 +00:00
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let fieldpath = codemap::Spanned{span:self.prev_span, node:fieldname};
|
2015-01-19 16:58:01 +00:00
|
|
|
|
let fieldpat = P(ast::Pat{
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-11 18:16:33 +00:00
|
|
|
|
node: PatKind::Ident(bind_type, fieldpath, None),
|
2015-01-19 16:58:01 +00:00
|
|
|
|
span: mk_sp(boxed_span_lo, hi),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let subpat = if is_box {
|
|
|
|
|
P(ast::Pat{
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-11 18:16:33 +00:00
|
|
|
|
node: PatKind::Box(fieldpat),
|
2015-01-19 16:58:01 +00:00
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
fieldpat
|
|
|
|
|
};
|
|
|
|
|
(subpat, fieldname, true)
|
2014-03-12 22:51:20 +00:00
|
|
|
|
};
|
2015-01-19 16:58:01 +00:00
|
|
|
|
|
2014-10-06 00:36:53 +00:00
|
|
|
|
fields.push(codemap::Spanned { span: mk_sp(lo, hi),
|
2017-01-04 03:13:01 +00:00
|
|
|
|
node: ast::FieldPat {
|
|
|
|
|
ident: fieldname,
|
|
|
|
|
pat: subpat,
|
|
|
|
|
is_shorthand: is_shorthand,
|
|
|
|
|
attrs: attrs.into(),
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-08-07 00:01:14 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok((fields, etc));
|
2012-08-07 00:01:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
|
2016-04-20 23:03:29 +00:00
|
|
|
|
if self.token.is_path_start() {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let (qself, path) = if self.eat_lt() {
|
2015-03-25 16:53:28 +00:00
|
|
|
|
// Parse a qualified path
|
|
|
|
|
let (qself, path) =
|
2016-04-18 21:42:18 +00:00
|
|
|
|
self.parse_qualified_path(PathStyle::Expr)?;
|
2015-03-25 16:53:28 +00:00
|
|
|
|
(Some(qself), path)
|
|
|
|
|
} else {
|
|
|
|
|
// Parse an unqualified path
|
2016-04-18 21:42:18 +00:00
|
|
|
|
(None, self.parse_path(PathStyle::Expr)?)
|
2015-03-25 16:53:28 +00:00
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
Ok(self.mk_expr(lo, hi, ExprKind::Path(qself, path), ThinVec::new()))
|
2015-03-31 06:10:11 +00:00
|
|
|
|
} else {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.parse_pat_literal_maybe_minus()
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a pattern.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtPat, |x| x);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2013-04-12 05:10:31 +00:00
|
|
|
|
let pat;
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2016-09-22 22:44:59 +00:00
|
|
|
|
token::Underscore => {
|
|
|
|
|
// Parse _
|
|
|
|
|
self.bump();
|
|
|
|
|
pat = PatKind::Wild;
|
|
|
|
|
}
|
|
|
|
|
token::BinOp(token::And) | token::AndAnd => {
|
|
|
|
|
// Parse &pat / &mut pat
|
|
|
|
|
self.expect_and()?;
|
|
|
|
|
let mutbl = self.parse_mutability()?;
|
|
|
|
|
if let token::Lifetime(ident) = self.token {
|
|
|
|
|
return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
|
|
|
|
|
}
|
|
|
|
|
let subpat = self.parse_pat()?;
|
|
|
|
|
pat = PatKind::Ref(subpat, mutbl);
|
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Paren) => {
|
|
|
|
|
// Parse (pat,pat,pat,...) as tuple pattern
|
|
|
|
|
self.bump();
|
|
|
|
|
let (fields, ddpos) = self.parse_pat_tuple_elements(true)?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
|
pat = PatKind::Tuple(fields, ddpos);
|
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Bracket) => {
|
|
|
|
|
// Parse [pat,pat,...] as slice pattern
|
|
|
|
|
self.bump();
|
|
|
|
|
let (before, slice, after) = self.parse_pat_vec_elements()?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
2016-09-20 14:54:24 +00:00
|
|
|
|
pat = PatKind::Slice(before, slice, after);
|
2015-10-24 23:57:42 +00:00
|
|
|
|
}
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// At this point, token != _, &, &&, (, [
|
2016-09-22 22:44:59 +00:00
|
|
|
|
_ => if self.eat_keyword(keywords::Mut) {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse mut ident @ pat
|
2016-03-23 03:01:37 +00:00
|
|
|
|
pat = self.parse_pat_ident(BindingMode::ByValue(Mutability::Mutable))?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::Ref) {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse ref ident @ pat / ref mut ident @ pat
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let mutbl = self.parse_mutability()?;
|
|
|
|
|
pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat_keyword(keywords::Box) {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse box pat
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let subpat = self.parse_pat()?;
|
2016-02-11 18:16:33 +00:00
|
|
|
|
pat = PatKind::Box(subpat);
|
2016-10-19 20:33:41 +00:00
|
|
|
|
} else if self.token.is_ident() && !self.token.is_any_keyword() &&
|
2016-09-22 22:44:59 +00:00
|
|
|
|
self.look_ahead(1, |t| match *t {
|
|
|
|
|
token::OpenDelim(token::Paren) | token::OpenDelim(token::Brace) |
|
|
|
|
|
token::DotDotDot | token::ModSep | token::Not => false,
|
|
|
|
|
_ => true,
|
|
|
|
|
}) {
|
|
|
|
|
// Parse ident @ pat
|
|
|
|
|
// This can give false positives and parse nullary enums,
|
|
|
|
|
// they are dealt with later in resolve
|
|
|
|
|
let binding_mode = BindingMode::ByValue(Mutability::Immutable);
|
|
|
|
|
pat = self.parse_pat_ident(binding_mode)?;
|
2016-04-20 23:03:29 +00:00
|
|
|
|
} else if self.token.is_path_start() {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse pattern starting with a path
|
2016-09-22 22:44:59 +00:00
|
|
|
|
let (qself, path) = if self.eat_lt() {
|
|
|
|
|
// Parse a qualified path
|
|
|
|
|
let (qself, path) = self.parse_qualified_path(PathStyle::Expr)?;
|
|
|
|
|
(Some(qself), path)
|
2015-03-31 06:10:11 +00:00
|
|
|
|
} else {
|
2016-09-22 22:44:59 +00:00
|
|
|
|
// Parse an unqualified path
|
|
|
|
|
(None, self.parse_path(PathStyle::Expr)?)
|
|
|
|
|
};
|
|
|
|
|
match self.token {
|
|
|
|
|
token::Not if qself.is_none() => {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse macro invocation
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let delim = self.expect_open_delim()?;
|
2016-09-22 22:44:59 +00:00
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree())?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let mac = spanned(lo, self.prev_span.hi, Mac_ { path: path, tts: tts });
|
2016-09-22 22:44:59 +00:00
|
|
|
|
pat = PatKind::Mac(mac);
|
2013-05-05 01:57:14 +00:00
|
|
|
|
}
|
2016-09-22 22:44:59 +00:00
|
|
|
|
token::DotDotDot => {
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse range
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-06-18 04:01:57 +00:00
|
|
|
|
let begin =
|
|
|
|
|
self.mk_expr(lo, hi, ExprKind::Path(qself, path), ThinVec::new());
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let end = self.parse_pat_range_end()?;
|
2016-02-11 18:16:33 +00:00
|
|
|
|
pat = PatKind::Range(begin, end);
|
2016-09-22 22:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Brace) => {
|
|
|
|
|
if qself.is_some() {
|
2015-10-25 00:02:08 +00:00
|
|
|
|
return Err(self.fatal("unexpected `{` after qualified path"));
|
2015-03-25 16:53:28 +00:00
|
|
|
|
}
|
2015-10-25 00:02:08 +00:00
|
|
|
|
// Parse struct pattern
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-02-10 03:11:27 +00:00
|
|
|
|
let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
(vec![], false)
|
|
|
|
|
});
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-02-11 18:16:33 +00:00
|
|
|
|
pat = PatKind::Struct(path, fields, etc);
|
2016-09-22 22:44:59 +00:00
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::Paren) => {
|
2015-03-25 16:53:28 +00:00
|
|
|
|
if qself.is_some() {
|
2015-10-25 00:02:08 +00:00
|
|
|
|
return Err(self.fatal("unexpected `(` after qualified path"));
|
2015-03-25 16:53:28 +00:00
|
|
|
|
}
|
2015-03-31 06:10:11 +00:00
|
|
|
|
// Parse tuple struct or enum pattern
|
2016-03-06 12:54:44 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
let (fields, ddpos) = self.parse_pat_tuple_elements(false)?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
|
pat = PatKind::TupleStruct(path, fields, ddpos)
|
2012-08-07 00:01:14 +00:00
|
|
|
|
}
|
2016-09-22 22:44:59 +00:00
|
|
|
|
_ => pat = PatKind::Path(qself, path),
|
2012-04-20 07:54:42 +00:00
|
|
|
|
}
|
2015-03-31 06:10:11 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Try to parse everything else as literal with optional minus
|
2016-04-20 23:03:29 +00:00
|
|
|
|
match self.parse_pat_literal_maybe_minus() {
|
|
|
|
|
Ok(begin) => {
|
|
|
|
|
if self.eat(&token::DotDotDot) {
|
|
|
|
|
let end = self.parse_pat_range_end()?;
|
|
|
|
|
pat = PatKind::Range(begin, end);
|
|
|
|
|
} else {
|
|
|
|
|
pat = PatKind::Lit(begin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(mut err) => {
|
2016-09-15 19:34:21 +00:00
|
|
|
|
self.cancel(&mut err);
|
2016-04-20 23:03:29 +00:00
|
|
|
|
let msg = format!("expected pattern, found {}", self.this_token_descr());
|
|
|
|
|
return Err(self.fatal(&msg));
|
|
|
|
|
}
|
2015-03-31 06:10:11 +00:00
|
|
|
|
}
|
2012-01-15 00:05:07 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2015-03-31 06:10:11 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(ast::Pat {
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-02 19:47:32 +00:00
|
|
|
|
node: pat,
|
|
|
|
|
span: mk_sp(lo, hi),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse ident or ident @ pat
|
|
|
|
|
/// used by the copy foo and ref foo patterns to give a good
|
|
|
|
|
/// error message when parsing mistakes like ref foo(a,b)
|
2013-12-30 22:04:00 +00:00
|
|
|
|
fn parse_pat_ident(&mut self,
|
2013-09-02 01:45:37 +00:00
|
|
|
|
binding_mode: ast::BindingMode)
|
2016-02-11 18:16:33 +00:00
|
|
|
|
-> PResult<'a, PatKind> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
let name = codemap::Spanned{span: prev_span, node: ident};
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let sub = if self.eat(&token::At) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
Some(self.parse_pat()?)
|
2013-04-24 08:29:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2012-08-06 14:20:23 +00:00
|
|
|
|
|
|
|
|
|
// just to be friendly, if they write something like
|
2012-08-20 19:23:37 +00:00
|
|
|
|
// ref Some(i)
|
2012-08-06 14:20:23 +00:00
|
|
|
|
// we end up here with ( as the current token. This shortly
|
|
|
|
|
// leads to a parse error. Note that if there is no explicit
|
|
|
|
|
// binding mode then we do not end up here, because the lookahead
|
|
|
|
|
// will direct us over to parse_enum_variant()
|
2014-10-29 10:37:54 +00:00
|
|
|
|
if self.token == token::OpenDelim(token::Paren) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.span_fatal(
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.prev_span,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
"expected identifier, found enum pattern"))
|
2012-08-06 14:20:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 18:16:33 +00:00
|
|
|
|
Ok(PatKind::Ident(binding_mode, name, sub))
|
2012-08-06 14:20:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a local variable declaration
|
2016-06-18 04:01:57 +00:00
|
|
|
|
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let pat = self.parse_pat()?;
|
2013-06-07 01:54:14 +00:00
|
|
|
|
|
2015-01-02 11:55:31 +00:00
|
|
|
|
let mut ty = None;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
ty = Some(self.parse_ty()?);
|
2014-06-11 19:14:38 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let init = self.parse_initializer()?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(ast::Local {
|
2013-07-19 05:38:55 +00:00
|
|
|
|
ty: ty,
|
|
|
|
|
pat: pat,
|
|
|
|
|
init: init,
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attrs: attrs,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a structure field
|
2016-08-08 12:35:15 +00:00
|
|
|
|
fn parse_name_and_ty(&mut self,
|
|
|
|
|
lo: BytePos,
|
|
|
|
|
vis: Visibility,
|
|
|
|
|
attrs: Vec<Attribute>)
|
|
|
|
|
-> PResult<'a, StructField> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let name = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Colon)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-04-06 08:19:10 +00:00
|
|
|
|
Ok(StructField {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2016-04-02 13:47:53 +00:00
|
|
|
|
ident: Some(name),
|
2016-08-08 12:35:15 +00:00
|
|
|
|
vis: vis,
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-01 03:20:08 +00:00
|
|
|
|
ty: ty,
|
|
|
|
|
attrs: attrs,
|
2016-04-06 08:19:10 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Emit an expected item after attributes error.
|
|
|
|
|
fn expected_item_err(&self, attrs: &[Attribute]) {
|
|
|
|
|
let message = match attrs.last() {
|
2016-11-14 12:00:25 +00:00
|
|
|
|
Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment",
|
2014-09-12 01:07:07 +00:00
|
|
|
|
_ => "expected item after attributes",
|
2015-03-13 09:34:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.span_err(self.prev_span, message);
|
2014-09-12 01:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 09:01:21 +00:00
|
|
|
|
/// Parse a statement. This stops just before trailing semicolons on everything but items.
|
|
|
|
|
/// e.g. a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
|
2016-02-11 20:33:09 +00:00
|
|
|
|
pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
|
2016-07-16 20:41:43 +00:00
|
|
|
|
Ok(self.parse_stmt_(true))
|
2016-02-10 03:11:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Eat tokens until we can be relatively sure we reached the end of the
|
|
|
|
|
// statement. This is something of a best-effort heuristic.
|
|
|
|
|
//
|
|
|
|
|
// We terminate when we find an unmatched `}` (without consuming it).
|
|
|
|
|
fn recover_stmt(&mut self) {
|
|
|
|
|
self.recover_stmt_(SemiColonMode::Ignore)
|
|
|
|
|
}
|
|
|
|
|
// If `break_on_semi` is `Break`, then we will stop consuming tokens after
|
|
|
|
|
// finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
|
|
|
|
|
// approximate - it can mean we break too early due to macros, but that
|
|
|
|
|
// shoud only lead to sub-optimal recovery, not inaccurate parsing).
|
|
|
|
|
fn recover_stmt_(&mut self, break_on_semi: SemiColonMode) {
|
|
|
|
|
let mut brace_depth = 0;
|
|
|
|
|
let mut bracket_depth = 0;
|
2016-03-18 02:49:12 +00:00
|
|
|
|
debug!("recover_stmt_ enter loop");
|
2016-02-10 03:11:27 +00:00
|
|
|
|
loop {
|
2016-03-18 02:49:12 +00:00
|
|
|
|
debug!("recover_stmt_ loop {:?}", self.token);
|
2016-02-10 03:11:27 +00:00
|
|
|
|
match self.token {
|
|
|
|
|
token::OpenDelim(token::DelimToken::Brace) => {
|
|
|
|
|
brace_depth += 1;
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
|
|
|
|
token::OpenDelim(token::DelimToken::Bracket) => {
|
|
|
|
|
bracket_depth += 1;
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
|
|
|
|
token::CloseDelim(token::DelimToken::Brace) => {
|
|
|
|
|
if brace_depth == 0 {
|
2016-03-18 02:49:12 +00:00
|
|
|
|
debug!("recover_stmt_ return - close delim {:?}", self.token);
|
2016-02-10 03:11:27 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
brace_depth -= 1;
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
|
|
|
|
token::CloseDelim(token::DelimToken::Bracket) => {
|
|
|
|
|
bracket_depth -= 1;
|
|
|
|
|
if bracket_depth < 0 {
|
|
|
|
|
bracket_depth = 0;
|
|
|
|
|
}
|
|
|
|
|
self.bump();
|
|
|
|
|
}
|
2016-03-18 02:49:12 +00:00
|
|
|
|
token::Eof => {
|
|
|
|
|
debug!("recover_stmt_ return - Eof");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-02-10 03:11:27 +00:00
|
|
|
|
token::Semi => {
|
|
|
|
|
self.bump();
|
|
|
|
|
if break_on_semi == SemiColonMode::Break &&
|
|
|
|
|
brace_depth == 0 &&
|
|
|
|
|
bracket_depth == 0 {
|
2016-03-18 02:49:12 +00:00
|
|
|
|
debug!("recover_stmt_ return - Semi");
|
2016-02-10 03:11:27 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
self.bump()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 05:57:58 +00:00
|
|
|
|
fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option<Stmt> {
|
|
|
|
|
self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| {
|
2016-02-10 03:11:27 +00:00
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt_(SemiColonMode::Break);
|
|
|
|
|
None
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 20:33:41 +00:00
|
|
|
|
fn is_union_item(&mut self) -> bool {
|
|
|
|
|
self.token.is_keyword(keywords::Union) &&
|
|
|
|
|
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 05:57:58 +00:00
|
|
|
|
fn parse_stmt_without_recovery(&mut self,
|
|
|
|
|
macro_legacy_warnings: bool)
|
|
|
|
|
-> PResult<'a, Option<Stmt>> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtStmt, |x| Some(x));
|
2012-08-01 21:34:35 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2015-04-23 20:45:32 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
2016-06-17 02:30:01 +00:00
|
|
|
|
Ok(Some(if self.eat_keyword(keywords::Let) {
|
|
|
|
|
Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-06-26 02:18:04 +00:00
|
|
|
|
node: StmtKind::Local(self.parse_local(attrs.into())?),
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2016-06-17 02:30:01 +00:00
|
|
|
|
}
|
2016-10-19 20:33:41 +00:00
|
|
|
|
// Starts like a simple path, but not a union item.
|
|
|
|
|
} else if self.token.is_path_start() &&
|
|
|
|
|
!self.token.is_qpath_start() &&
|
|
|
|
|
!self.is_union_item() {
|
2016-09-22 05:10:16 +00:00
|
|
|
|
let pth = self.parse_path(PathStyle::Expr)?;
|
2013-05-08 22:27:29 +00:00
|
|
|
|
|
2016-09-22 05:10:16 +00:00
|
|
|
|
if !self.eat(&token::Not) {
|
|
|
|
|
let expr = if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
|
self.parse_struct_expr(lo, pth, ThinVec::new())?
|
|
|
|
|
} else {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-09-22 05:10:16 +00:00
|
|
|
|
self.mk_expr(lo, hi, ExprKind::Path(None, pth), ThinVec::new())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let expr = self.with_res(Restrictions::RESTRICTION_STMT_EXPR, |this| {
|
|
|
|
|
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
|
|
|
|
|
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
|
|
|
|
|
})?;
|
2012-11-14 04:45:25 +00:00
|
|
|
|
|
2016-09-22 05:10:16 +00:00
|
|
|
|
return Ok(Some(Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
node: StmtKind::Expr(expr),
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2016-09-22 05:10:16 +00:00
|
|
|
|
}));
|
|
|
|
|
}
|
2012-11-14 04:45:25 +00:00
|
|
|
|
|
2016-09-22 05:10:16 +00:00
|
|
|
|
// it's a macro invocation
|
2014-10-29 14:47:53 +00:00
|
|
|
|
let id = match self.token {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
token::OpenDelim(_) => keywords::Invalid.ident(), // no special identifier
|
2016-03-23 03:01:37 +00:00
|
|
|
|
_ => self.parse_ident()?,
|
2012-11-14 04:45:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-02-23 13:53:59 +00:00
|
|
|
|
// check that we're pointing at delimiters (need to check
|
|
|
|
|
// again after the `if`, because of `parse_ident`
|
|
|
|
|
// consuming more tokens).
|
2014-10-29 14:47:53 +00:00
|
|
|
|
let delim = match self.token {
|
|
|
|
|
token::OpenDelim(delim) => delim,
|
|
|
|
|
_ => {
|
2014-05-28 16:24:28 +00:00
|
|
|
|
// we only expect an ident if we didn't parse one
|
|
|
|
|
// above.
|
2016-04-18 21:42:18 +00:00
|
|
|
|
let ident_str = if id.name == keywords::Invalid.name() {
|
2014-05-28 16:24:28 +00:00
|
|
|
|
"identifier, "
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
};
|
2014-06-21 10:39:03 +00:00
|
|
|
|
let tok_str = self.this_token_to_string();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
|
2014-05-28 16:24:28 +00:00
|
|
|
|
ident_str,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
tok_str)))
|
2014-10-29 14:47:53 +00:00
|
|
|
|
},
|
2014-02-23 13:53:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let tts = self.parse_unspanned_seq(
|
2014-10-29 14:47:53 +00:00
|
|
|
|
&token::OpenDelim(delim),
|
|
|
|
|
&token::CloseDelim(delim),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::none(),
|
2013-02-24 23:41:54 +00:00
|
|
|
|
|p| p.parse_token_tree()
|
2016-03-23 03:01:37 +00:00
|
|
|
|
)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2012-11-14 04:45:25 +00:00
|
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
|
let style = if delim == token::Brace {
|
2016-02-09 10:56:59 +00:00
|
|
|
|
MacStmtStyle::Braces
|
2014-11-14 17:18:10 +00:00
|
|
|
|
} else {
|
2016-02-09 10:56:59 +00:00
|
|
|
|
MacStmtStyle::NoBraces
|
2014-11-14 17:18:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-04-18 21:42:18 +00:00
|
|
|
|
if id.name == keywords::Invalid.name() {
|
2016-06-26 02:16:55 +00:00
|
|
|
|
let mac = spanned(lo, hi, Mac_ { path: pth, tts: tts });
|
2016-07-16 20:41:43 +00:00
|
|
|
|
let node = if delim == token::Brace ||
|
|
|
|
|
self.token == token::Semi || self.token == token::Eof {
|
|
|
|
|
StmtKind::Mac(P((mac, style, attrs.into())))
|
|
|
|
|
}
|
|
|
|
|
// We used to incorrectly stop parsing macro-expanded statements here.
|
|
|
|
|
// If the next token will be an error anyway but could have parsed with the
|
|
|
|
|
// earlier behavior, stop parsing here and emit a warning to avoid breakage.
|
2016-09-06 05:57:58 +00:00
|
|
|
|
else if macro_legacy_warnings && self.token.can_begin_expr() && match self.token {
|
2016-07-16 20:41:43 +00:00
|
|
|
|
// These can continue an expression, so we can't stop parsing and warn.
|
|
|
|
|
token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
|
|
|
|
|
token::BinOp(token::Minus) | token::BinOp(token::Star) |
|
|
|
|
|
token::BinOp(token::And) | token::BinOp(token::Or) |
|
|
|
|
|
token::AndAnd | token::OrOr |
|
|
|
|
|
token::DotDot | token::DotDotDot => false,
|
|
|
|
|
_ => true,
|
|
|
|
|
} {
|
|
|
|
|
self.warn_missing_semicolon();
|
|
|
|
|
StmtKind::Mac(P((mac, style, attrs.into())))
|
|
|
|
|
} else {
|
|
|
|
|
let e = self.mk_mac_expr(lo, hi, mac.node, ThinVec::new());
|
|
|
|
|
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
|
|
|
|
|
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
|
|
|
|
|
StmtKind::Expr(e)
|
|
|
|
|
};
|
2016-06-17 02:30:01 +00:00
|
|
|
|
Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, hi),
|
2016-07-16 20:41:43 +00:00
|
|
|
|
node: node,
|
2016-06-17 02:30:01 +00:00
|
|
|
|
}
|
2012-11-14 04:45:25 +00:00
|
|
|
|
} else {
|
|
|
|
|
// if it has a special ident, it's definitely an item
|
2014-11-14 17:18:10 +00:00
|
|
|
|
//
|
|
|
|
|
// Require a semicolon or braces.
|
2016-02-09 10:56:59 +00:00
|
|
|
|
if style != MacStmtStyle::Braces {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::Semi) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.span_err(self.prev_span,
|
2014-11-14 17:18:10 +00:00
|
|
|
|
"macros that expand to items must \
|
|
|
|
|
either be surrounded with braces or \
|
|
|
|
|
followed by a semicolon");
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-17 02:30:01 +00:00
|
|
|
|
Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
|
node: StmtKind::Item({
|
2012-11-14 04:45:25 +00:00
|
|
|
|
self.mk_item(
|
|
|
|
|
lo, hi, id /*id is good here*/,
|
2016-06-26 02:16:55 +00:00
|
|
|
|
ItemKind::Mac(spanned(lo, hi, Mac_ { path: pth, tts: tts })),
|
|
|
|
|
Visibility::Inherited,
|
|
|
|
|
attrs)
|
2016-06-17 02:30:01 +00:00
|
|
|
|
}),
|
|
|
|
|
}
|
2012-11-14 04:45:25 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
} else {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
// FIXME: Bad copy of attrs
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let old_directory_ownership =
|
|
|
|
|
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
|
|
|
|
|
let item = self.parse_item_(attrs.clone(), false, true)?;
|
|
|
|
|
self.directory.ownership = old_directory_ownership;
|
|
|
|
|
match item {
|
2016-06-17 02:30:01 +00:00
|
|
|
|
Some(i) => Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, i.span.hi),
|
|
|
|
|
node: StmtKind::Item(i),
|
|
|
|
|
},
|
2015-03-13 09:34:51 +00:00
|
|
|
|
None => {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
let unused_attrs = |attrs: &[_], s: &mut Self| {
|
|
|
|
|
if attrs.len() > 0 {
|
2016-09-21 02:16:28 +00:00
|
|
|
|
if s.prev_token_kind == PrevTokenKind::DocComment {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
s.span_err_help(s.prev_span,
|
2016-05-28 02:05:22 +00:00
|
|
|
|
"found a documentation comment that doesn't document anything",
|
|
|
|
|
"doc comments must come before what they document, maybe a \
|
2016-09-16 05:46:40 +00:00
|
|
|
|
comment was intended with `//`?");
|
|
|
|
|
} else {
|
|
|
|
|
s.span_err(s.span, "expected statement after outer attribute");
|
2016-05-28 02:05:22 +00:00
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
// Do not attempt to parse an expression if we're done here.
|
|
|
|
|
if self.token == token::Semi {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
unused_attrs(&attrs, self);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(None);
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.token == token::CloseDelim(token::Brace) {
|
2015-11-03 16:39:51 +00:00
|
|
|
|
unused_attrs(&attrs, self);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(None);
|
2014-09-12 01:07:07 +00:00
|
|
|
|
}
|
2011-06-15 20:27:39 +00:00
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
|
// Remainder are line-expr stmts.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let e = self.parse_expr_res(
|
2016-06-18 04:01:57 +00:00
|
|
|
|
Restrictions::RESTRICTION_STMT_EXPR, Some(attrs.into()))?;
|
2016-06-17 02:30:01 +00:00
|
|
|
|
Stmt {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, e.span.hi),
|
|
|
|
|
node: StmtKind::Expr(e),
|
|
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Is this expression a successfully-parsed statement?
|
2014-09-13 16:06:01 +00:00
|
|
|
|
fn expr_is_complete(&mut self, e: &Expr) -> bool {
|
2015-04-28 23:36:22 +00:00
|
|
|
|
self.restrictions.contains(Restrictions::RESTRICTION_STMT_EXPR) &&
|
2014-09-13 16:06:01 +00:00
|
|
|
|
!classify::expr_requires_semi_to_be_stmt(e)
|
2010-09-21 23:22:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a block. No inner attrs are allowed.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtBlock, |x| x);
|
2013-04-06 00:31:52 +00:00
|
|
|
|
|
|
|
|
|
let lo = self.span.lo;
|
2014-11-10 10:58:03 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::OpenDelim(token::Brace)) {
|
2014-11-10 10:58:03 +00:00
|
|
|
|
let sp = self.span;
|
|
|
|
|
let tok = self.this_token_to_string();
|
2016-08-23 06:23:31 +00:00
|
|
|
|
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
|
|
|
|
|
|
|
|
|
|
// Check to see if the user has written something like
|
|
|
|
|
//
|
|
|
|
|
// if (cond)
|
|
|
|
|
// bar;
|
|
|
|
|
//
|
|
|
|
|
// Which is valid in other languages, but not Rust.
|
|
|
|
|
match self.parse_stmt_without_recovery(false) {
|
|
|
|
|
Ok(Some(stmt)) => {
|
|
|
|
|
let mut stmt_span = stmt.span;
|
|
|
|
|
// expand the span to include the semicolon, if it exists
|
|
|
|
|
if self.eat(&token::Semi) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
stmt_span.hi = self.prev_span.hi;
|
2016-08-23 06:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
e.span_help(stmt_span, "try placing this code inside a block");
|
|
|
|
|
}
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
self.recover_stmt_(SemiColonMode::Break);
|
2016-09-15 19:34:21 +00:00
|
|
|
|
self.cancel(&mut e);
|
2016-08-23 06:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
_ => ()
|
|
|
|
|
}
|
|
|
|
|
return Err(e);
|
2014-11-10 10:58:03 +00:00
|
|
|
|
}
|
2013-04-06 00:31:52 +00:00
|
|
|
|
|
2016-02-08 11:44:45 +00:00
|
|
|
|
self.parse_block_tail(lo, BlockCheckMode::Default)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-11-30 01:11:03 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a block. Inner attrs are allowed.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
|
2012-08-01 21:34:35 +00:00
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
|
|
|
|
Ok((self.parse_inner_attributes()?,
|
|
|
|
|
self.parse_block_tail(lo, BlockCheckMode::Default)?))
|
2012-01-16 01:23:59 +00:00
|
|
|
|
}
|
2013-04-02 23:44:01 +00:00
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Parse the rest of a block expression or function body
|
2014-12-23 13:07:30 +00:00
|
|
|
|
/// Precondition: already parsed the '{'.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> PResult<'a, P<Block>> {
|
2014-12-23 13:07:30 +00:00
|
|
|
|
let mut stmts = vec![];
|
2012-01-16 01:23:59 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
while !self.eat(&token::CloseDelim(token::Brace)) {
|
2016-07-12 03:56:19 +00:00
|
|
|
|
if let Some(stmt) = self.parse_full_stmt(false)? {
|
2016-07-07 04:12:20 +00:00
|
|
|
|
stmts.push(stmt);
|
2016-03-18 02:49:12 +00:00
|
|
|
|
} else if self.token == token::Eof {
|
|
|
|
|
break;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Found only `;` or `}`.
|
|
|
|
|
continue;
|
|
|
|
|
};
|
2013-04-30 19:02:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(ast::Block {
|
2013-01-15 03:35:08 +00:00
|
|
|
|
stmts: stmts,
|
2013-09-07 02:11:55 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-15 03:35:08 +00:00
|
|
|
|
rules: s,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 04:12:20 +00:00
|
|
|
|
/// Parse a statement, including the trailing semicolon.
|
2016-09-06 05:57:58 +00:00
|
|
|
|
pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option<Stmt>> {
|
|
|
|
|
let mut stmt = match self.parse_stmt_(macro_legacy_warnings) {
|
2016-07-07 04:12:20 +00:00
|
|
|
|
Some(stmt) => stmt,
|
|
|
|
|
None => return Ok(None),
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-02 09:01:21 +00:00
|
|
|
|
match stmt.node {
|
2016-07-02 09:32:23 +00:00
|
|
|
|
StmtKind::Expr(ref expr) if self.token != token::Eof => {
|
2016-07-02 09:01:21 +00:00
|
|
|
|
// expression without semicolon
|
|
|
|
|
if classify::expr_requires_semi_to_be_stmt(expr) {
|
|
|
|
|
// Just check for errors and recover; do not eat semicolon yet.
|
|
|
|
|
if let Err(mut e) =
|
|
|
|
|
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
|
|
|
|
|
{
|
|
|
|
|
e.emit();
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-10 03:11:27 +00:00
|
|
|
|
}
|
2016-07-02 09:01:21 +00:00
|
|
|
|
StmtKind::Local(..) => {
|
2016-07-12 03:56:19 +00:00
|
|
|
|
// We used to incorrectly allow a macro-expanded let statement to lack a semicolon.
|
2016-09-06 05:57:58 +00:00
|
|
|
|
if macro_legacy_warnings && self.token != token::Semi {
|
2016-07-12 03:56:19 +00:00
|
|
|
|
self.warn_missing_semicolon();
|
|
|
|
|
} else {
|
|
|
|
|
self.expect_one_of(&[token::Semi], &[])?;
|
|
|
|
|
}
|
2016-07-02 09:01:21 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2014-11-14 17:18:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 09:01:21 +00:00
|
|
|
|
if self.eat(&token::Semi) {
|
|
|
|
|
stmt = stmt.add_trailing_semicolon();
|
2014-11-14 17:18:10 +00:00
|
|
|
|
}
|
2016-07-02 09:01:21 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
stmt.span.hi = self.prev_span.hi;
|
2016-07-16 20:41:43 +00:00
|
|
|
|
Ok(Some(stmt))
|
2014-11-14 17:18:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 03:56:19 +00:00
|
|
|
|
fn warn_missing_semicolon(&self) {
|
|
|
|
|
self.diagnostic().struct_span_warn(self.span, {
|
|
|
|
|
&format!("expected `;`, found `{}`", self.this_token_to_string())
|
|
|
|
|
}).note({
|
|
|
|
|
"This was erroneously allowed and will become a hard error in a future release"
|
|
|
|
|
}).emit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// Parse bounds of a type parameter `BOUND + BOUND + BOUND` without trailing `+`.
|
|
|
|
|
// BOUND = TY_BOUND | LT_BOUND
|
|
|
|
|
// LT_BOUND = LIFETIME (e.g. `'a`)
|
|
|
|
|
// TY_BOUND = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
|
2016-10-22 00:33:36 +00:00
|
|
|
|
fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
|
2014-08-28 01:46:52 +00:00
|
|
|
|
{
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let mut bounds = Vec::new();
|
2013-02-15 05:50:03 +00:00
|
|
|
|
loop {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
|
|
|
|
|
if let Some(lifetime) = self.eat_lifetime() {
|
|
|
|
|
if let Some(question_span) = question {
|
|
|
|
|
self.span_err(question_span,
|
|
|
|
|
"`?` may only modify trait bounds, not lifetime bounds");
|
2014-06-02 01:41:46 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
bounds.push(RegionTyParamBound(lifetime));
|
|
|
|
|
} else if self.token.is_keyword(keywords::For) || self.token.is_path_start() {
|
|
|
|
|
let poly_trait_ref = self.parse_poly_trait_ref()?;
|
|
|
|
|
let modifier = if question.is_some() {
|
|
|
|
|
TraitBoundModifier::Maybe
|
|
|
|
|
} else {
|
|
|
|
|
TraitBoundModifier::None
|
|
|
|
|
};
|
|
|
|
|
bounds.push(TraitTyParamBound(poly_trait_ref, modifier));
|
|
|
|
|
} else {
|
|
|
|
|
break
|
2013-02-15 05:50:03 +00:00
|
|
|
|
}
|
2013-01-22 22:37:32 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// Trailing plus is not allowed for now and we have to detect it.
|
|
|
|
|
let is_bound_start = |token: &token::Token| {
|
|
|
|
|
token == &token::Question || token.is_lifetime() ||
|
|
|
|
|
token.is_keyword(keywords::For) || token.is_path_start()
|
|
|
|
|
};
|
|
|
|
|
if self.check(&token::BinOp(token::Plus)) && self.look_ahead(1, is_bound_start) {
|
|
|
|
|
self.bump();
|
|
|
|
|
} else {
|
|
|
|
|
break
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
return Ok(bounds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse bounds of a type parameter `BOUND + BOUND + BOUND` without trailing `+`.
|
|
|
|
|
// BOUND = LT_BOUND (e.g. `'a`)
|
|
|
|
|
fn parse_lt_param_bounds(&mut self) -> Vec<Lifetime> {
|
|
|
|
|
let mut lifetimes = Vec::new();
|
|
|
|
|
while let Some(lifetime) = self.eat_lifetime() {
|
|
|
|
|
lifetimes.push(lifetime);
|
|
|
|
|
if self.check(&token::BinOp(token::Plus)) && self.look_ahead(1, |t| t.is_lifetime()) {
|
|
|
|
|
self.bump();
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lifetimes
|
2012-08-07 01:54:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 18:54:06 +00:00
|
|
|
|
/// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
|
2017-01-17 18:18:29 +00:00
|
|
|
|
fn parse_ty_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, TyParam> {
|
2015-03-18 18:54:06 +00:00
|
|
|
|
let span = self.span;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
2014-07-08 02:26:02 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// Parse optional colon and param bounds.
|
|
|
|
|
let bounds = if self.eat(&token::Colon) {
|
|
|
|
|
self.parse_ty_param_bounds()?
|
|
|
|
|
} else {
|
|
|
|
|
Vec::new()
|
|
|
|
|
};
|
2014-01-30 17:28:02 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let default = if self.eat(&token::Eq) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
Some(self.parse_ty()?)
|
2015-03-18 18:54:06 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2014-01-30 17:28:02 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(TyParam {
|
2016-05-17 16:51:45 +00:00
|
|
|
|
attrs: preceding_attrs.into(),
|
2014-01-30 17:28:02 +00:00
|
|
|
|
ident: ident,
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
bounds: bounds,
|
2014-04-03 00:53:57 +00:00
|
|
|
|
default: default,
|
|
|
|
|
span: span,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2012-01-04 22:16:41 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
/// Parses (possibly empty) list of lifetime and type parameters, possibly including
|
|
|
|
|
/// trailing comma and erroneous trailing attributes.
|
|
|
|
|
pub fn parse_generic_params(&mut self) -> PResult<'a, (Vec<LifetimeDef>, Vec<TyParam>)> {
|
|
|
|
|
let mut lifetime_defs = Vec::new();
|
|
|
|
|
let mut ty_params = Vec::new();
|
|
|
|
|
let mut seen_ty_param = false;
|
|
|
|
|
loop {
|
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
|
if let Some(lifetime) = self.eat_lifetime() {
|
|
|
|
|
// Parse lifetime parameter.
|
|
|
|
|
let bounds = if self.eat(&token::Colon) {
|
|
|
|
|
self.parse_lt_param_bounds()
|
|
|
|
|
} else {
|
|
|
|
|
Vec::new()
|
|
|
|
|
};
|
|
|
|
|
lifetime_defs.push(LifetimeDef {
|
|
|
|
|
attrs: attrs.into(),
|
|
|
|
|
lifetime: lifetime,
|
|
|
|
|
bounds: bounds,
|
|
|
|
|
});
|
|
|
|
|
if seen_ty_param {
|
|
|
|
|
self.span_err(self.prev_span,
|
|
|
|
|
"lifetime parameters must be declared prior to type parameters");
|
|
|
|
|
}
|
|
|
|
|
} else if self.token.is_ident() {
|
|
|
|
|
// Parse type parameter.
|
|
|
|
|
ty_params.push(self.parse_ty_param(attrs)?);
|
|
|
|
|
seen_ty_param = true;
|
|
|
|
|
} else {
|
|
|
|
|
// Check for trailing attributes and stop parsing.
|
|
|
|
|
if !attrs.is_empty() {
|
|
|
|
|
let param_kind = if seen_ty_param { "type" } else { "lifetime" };
|
|
|
|
|
self.span_err(attrs[0].span,
|
|
|
|
|
&format!("trailing attribute after {} parameters", param_kind));
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !self.eat(&token::Comma) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok((lifetime_defs, ty_params))
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
|
/// Parse a set of optional generic type parameter declarations. Where
|
|
|
|
|
/// clauses are not parsed here, and must be added later via
|
|
|
|
|
/// `parse_where_clause()`.
|
|
|
|
|
///
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
|
|
|
|
|
/// | ( < lifetimes , typaramseq ( , )? > )
|
|
|
|
|
/// where typaramseq = ( typaram ) | ( typaram , typaramseq )
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtGenerics, |x| x);
|
2015-05-02 17:55:41 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let span_lo = self.span.lo;
|
|
|
|
|
if self.eat_lt() {
|
|
|
|
|
let (lifetime_defs, ty_params) = self.parse_generic_params()?;
|
|
|
|
|
self.expect_gt()?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Generics {
|
2014-08-11 16:32:26 +00:00
|
|
|
|
lifetimes: lifetime_defs,
|
|
|
|
|
ty_params: ty_params,
|
|
|
|
|
where_clause: WhereClause {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
predicates: Vec::new(),
|
2016-08-10 17:39:12 +00:00
|
|
|
|
},
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(span_lo, self.prev_span.hi),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2013-02-15 05:50:03 +00:00
|
|
|
|
} else {
|
2015-11-28 19:02:07 +00:00
|
|
|
|
Ok(ast::Generics::default())
|
2013-02-15 05:50:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
/// Parses (possibly empty) list of lifetime and type arguments and associated type bindings,
|
|
|
|
|
/// possibly including trailing comma.
|
|
|
|
|
fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<TypeBinding>)> {
|
|
|
|
|
let mut lifetimes = Vec::new();
|
|
|
|
|
let mut types = Vec::new();
|
|
|
|
|
let mut bindings = Vec::new();
|
|
|
|
|
let mut seen_type = false;
|
|
|
|
|
let mut seen_binding = false;
|
|
|
|
|
loop {
|
|
|
|
|
let eq_is_next = self.look_ahead(1, |t| t == &token::Eq); // borrowck workaround
|
|
|
|
|
if let Some(lifetime) = self.eat_lifetime() {
|
|
|
|
|
// Parse lifetime argument.
|
|
|
|
|
lifetimes.push(lifetime);
|
|
|
|
|
if seen_type || seen_binding {
|
|
|
|
|
self.span_err(self.prev_span,
|
|
|
|
|
"lifetime parameters must be declared prior to type parameters");
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
} else if self.token.is_ident() && eq_is_next {
|
|
|
|
|
// Parse associated type binding.
|
|
|
|
|
let lo = self.span.lo;
|
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
self.bump();
|
|
|
|
|
let ty = self.parse_ty()?;
|
|
|
|
|
bindings.push(TypeBinding {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-11-29 04:08:30 +00:00
|
|
|
|
ident: ident,
|
|
|
|
|
ty: ty,
|
2017-01-17 18:18:29 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2016-02-11 20:33:09 +00:00
|
|
|
|
});
|
2017-01-17 18:18:29 +00:00
|
|
|
|
seen_binding = true;
|
|
|
|
|
} else if self.token.can_begin_type() {
|
|
|
|
|
// Parse type argument.
|
|
|
|
|
types.push(self.parse_ty()?);
|
|
|
|
|
if seen_binding {
|
|
|
|
|
self.span_err(types[types.len() - 1].span,
|
|
|
|
|
"type parameters must be declared prior to associated type bindings");
|
|
|
|
|
}
|
|
|
|
|
seen_type = true;
|
|
|
|
|
} else {
|
|
|
|
|
break
|
2014-11-29 04:08:30 +00:00
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
if !self.eat(&token::Comma) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2014-05-23 19:51:21 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
Ok((lifetimes, types, bindings))
|
2014-05-23 19:51:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
|
/// Parses an optional `where` clause and places it in `generics`.
|
2014-12-20 10:29:19 +00:00
|
|
|
|
///
|
2015-11-03 16:34:11 +00:00
|
|
|
|
/// ```ignore
|
2014-12-20 10:29:19 +00:00
|
|
|
|
/// where T : Trait<U, V> + 'b, 'a : 'b
|
|
|
|
|
/// ```
|
2017-01-17 18:18:29 +00:00
|
|
|
|
pub fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtWhereClause, |x| x);
|
2015-05-02 17:55:41 +00:00
|
|
|
|
|
2015-03-14 05:22:04 +00:00
|
|
|
|
let mut where_clause = WhereClause {
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
predicates: Vec::new(),
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat_keyword(keywords::Where) {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(where_clause);
|
2014-08-11 16:32:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// This is a temporary future proofing.
|
2016-12-09 18:54:05 +00:00
|
|
|
|
//
|
|
|
|
|
// We are considering adding generics to the `where` keyword as an alternative higher-rank
|
|
|
|
|
// parameter syntax (as in `where<'a>` or `where<T>`. To avoid that being a breaking
|
|
|
|
|
// change, for now we refuse to parse `where < (ident | lifetime) (> | , | :)`.
|
|
|
|
|
if token::Lt == self.token {
|
|
|
|
|
let ident_or_lifetime = self.look_ahead(1, |t| t.is_ident() || t.is_lifetime());
|
|
|
|
|
if ident_or_lifetime {
|
|
|
|
|
let gt_comma_or_colon = self.look_ahead(2, |t| {
|
|
|
|
|
*t == token::Gt || *t == token::Comma || *t == token::Colon
|
|
|
|
|
});
|
|
|
|
|
if gt_comma_or_colon {
|
2016-12-10 05:17:58 +00:00
|
|
|
|
self.span_err(self.span, "syntax `where<T>` is reserved for future use");
|
2016-12-09 18:54:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
|
loop {
|
|
|
|
|
let lo = self.span.lo;
|
2017-01-17 18:18:29 +00:00
|
|
|
|
if let Some(lifetime) = self.eat_lifetime() {
|
|
|
|
|
// Bounds starting with a colon are mandatory, but possibly empty.
|
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
|
let bounds = self.parse_lt_param_bounds();
|
|
|
|
|
where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
|
|
|
|
|
ast::WhereRegionPredicate {
|
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
|
|
|
|
lifetime: lifetime,
|
|
|
|
|
bounds: bounds,
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
} else if self.token.can_begin_type() {
|
|
|
|
|
// Parse optional `for<'a, 'b>`.
|
|
|
|
|
// This `for` is parsed greedily and applies to the whole predicate,
|
|
|
|
|
// the bounded type can have its own `for` applying only to it.
|
|
|
|
|
// Example 1: for<'a> Trait1<'a>: Trait2<'a /*ok*/>
|
|
|
|
|
// Example 2: (for<'a> Trait1<'a>): Trait2<'a /*not ok*/>
|
|
|
|
|
// Example 3: for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /*ok*/, 'b /*not ok*/>
|
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
|
|
|
|
|
|
|
|
|
// Parse type with mandatory colon and (possibly empty) bounds,
|
|
|
|
|
// or with mandatory equality sign and the second type.
|
|
|
|
|
let ty = self.parse_ty()?;
|
|
|
|
|
if self.eat(&token::Colon) {
|
|
|
|
|
let bounds = self.parse_ty_param_bounds()?;
|
|
|
|
|
where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
|
|
|
|
|
ast::WhereBoundPredicate {
|
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
|
|
|
|
bound_lifetimes: lifetime_defs,
|
|
|
|
|
bounded_ty: ty,
|
|
|
|
|
bounds: bounds,
|
2014-12-20 10:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
));
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// FIXME: Decide what should be used here, `=` or `==`.
|
|
|
|
|
} else if self.eat(&token::Eq) || self.eat(&token::EqEq) {
|
|
|
|
|
let rhs_ty = self.parse_ty()?;
|
|
|
|
|
where_clause.predicates.push(ast::WherePredicate::EqPredicate(
|
|
|
|
|
ast::WhereEqPredicate {
|
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
|
|
|
|
lhs_ty: ty,
|
|
|
|
|
rhs_ty: rhs_ty,
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-12-20 10:29:19 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
return self.unexpected();
|
2014-12-20 10:29:19 +00:00
|
|
|
|
}
|
2017-01-17 18:18:29 +00:00
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
2014-08-11 16:32:26 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::Comma) {
|
2014-08-11 16:32:26 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(where_clause)
|
2014-08-11 16:32:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
|
fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, (Vec<Arg> , bool)> {
|
2013-12-30 23:17:53 +00:00
|
|
|
|
let sp = self.span;
|
2016-01-31 19:39:50 +00:00
|
|
|
|
let mut variadic = false;
|
|
|
|
|
let args: Vec<Option<Arg>> =
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2013-10-25 05:56:34 +00:00
|
|
|
|
|p| {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
if p.token == token::DotDotDot {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
p.bump();
|
2013-10-25 05:56:34 +00:00
|
|
|
|
if allow_variadic {
|
2014-10-29 10:37:54 +00:00
|
|
|
|
if p.token != token::CloseDelim(token::Paren) {
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = p.span;
|
2016-01-31 19:39:50 +00:00
|
|
|
|
p.span_err(span,
|
|
|
|
|
"`...` must be last in argument list for variadic function");
|
2013-10-25 05:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-06-14 03:48:09 +00:00
|
|
|
|
let span = p.span;
|
2016-01-31 19:39:50 +00:00
|
|
|
|
p.span_err(span,
|
|
|
|
|
"only foreign functions are allowed to be variadic");
|
2013-10-25 05:56:34 +00:00
|
|
|
|
}
|
2016-01-31 19:39:50 +00:00
|
|
|
|
variadic = true;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(None)
|
2013-10-25 05:56:34 +00:00
|
|
|
|
} else {
|
2016-01-31 19:39:50 +00:00
|
|
|
|
match p.parse_arg_general(named_args) {
|
|
|
|
|
Ok(arg) => Ok(Some(arg)),
|
|
|
|
|
Err(mut e) => {
|
|
|
|
|
e.emit();
|
|
|
|
|
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-25 05:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
)?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
2016-02-10 03:11:27 +00:00
|
|
|
|
let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
|
|
|
|
|
|
2013-10-25 05:56:34 +00:00
|
|
|
|
if variadic && args.is_empty() {
|
|
|
|
|
self.span_err(sp,
|
|
|
|
|
"variadic function must be declared with at least one named argument");
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok((args, variadic))
|
2013-10-25 05:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse the argument list and result type of a function declaration
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> PResult<'a, P<FnDecl>> {
|
2013-10-25 05:56:34 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (args, variadic) = self.parse_fn_args(true, allow_variadic)?;
|
|
|
|
|
let ret_ty = self.parse_ret_ty()?;
|
2013-10-25 05:56:34 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(FnDecl {
|
2013-09-14 02:07:43 +00:00
|
|
|
|
inputs: args,
|
2013-01-10 18:59:58 +00:00
|
|
|
|
output: ret_ty,
|
2013-10-25 05:56:34 +00:00
|
|
|
|
variadic: variadic
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
|
/// Returns the parsed optional self argument and whether a self shortcut was used.
|
2016-03-06 12:54:44 +00:00
|
|
|
|
fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
let expect_ident = |this: &mut Self| match this.token {
|
2016-03-06 12:54:44 +00:00
|
|
|
|
// Preserve hygienic context.
|
2016-09-21 02:09:22 +00:00
|
|
|
|
token::Ident(ident) => { this.bump(); codemap::respan(this.prev_span, ident) }
|
2016-05-08 18:18:21 +00:00
|
|
|
|
_ => unreachable!()
|
|
|
|
|
};
|
2016-10-19 20:33:41 +00:00
|
|
|
|
let isolated_self = |this: &mut Self, n| {
|
|
|
|
|
this.look_ahead(n, |t| t.is_keyword(keywords::SelfValue)) &&
|
|
|
|
|
this.look_ahead(n + 1, |t| t != &token::ModSep)
|
|
|
|
|
};
|
2013-03-10 00:43:53 +00:00
|
|
|
|
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// Parse optional self parameter of a method.
|
|
|
|
|
// Only a limited set of initial token sequences is considered self parameters, anything
|
|
|
|
|
// else is parsed as a normal function parameter list, so some lookahead is required.
|
|
|
|
|
let eself_lo = self.span.lo;
|
2016-03-06 12:54:44 +00:00
|
|
|
|
let (eself, eself_ident) = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::And) => {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// &self
|
|
|
|
|
// &mut self
|
|
|
|
|
// &'lt self
|
|
|
|
|
// &'lt mut self
|
|
|
|
|
// ¬_self
|
2016-10-19 20:33:41 +00:00
|
|
|
|
if isolated_self(self, 1) {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
self.bump();
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Region(None, Mutability::Immutable), expect_ident(self))
|
2016-05-08 18:18:21 +00:00
|
|
|
|
} else if self.look_ahead(1, |t| t.is_keyword(keywords::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();
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Region(None, Mutability::Mutable), expect_ident(self))
|
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-01-17 18:18:29 +00:00
|
|
|
|
let lt = self.eat_lifetime().expect("not a lifetime");
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Region(Some(lt), Mutability::Immutable), expect_ident(self))
|
2016-05-08 18:18:21 +00:00
|
|
|
|
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
|
|
|
|
|
self.look_ahead(2, |t| t.is_keyword(keywords::Mut)) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
|
isolated_self(self, 3) {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
self.bump();
|
2017-01-17 18:18:29 +00:00
|
|
|
|
let lt = self.eat_lifetime().expect("not a lifetime");
|
2016-05-08 18:18:21 +00:00
|
|
|
|
self.bump();
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Region(Some(lt), Mutability::Mutable), expect_ident(self))
|
2016-05-08 18:18:21 +00:00
|
|
|
|
} else {
|
2016-03-06 12:54:44 +00:00
|
|
|
|
return Ok(None);
|
2016-05-08 18:18:21 +00:00
|
|
|
|
}
|
2014-01-12 00:25:51 +00:00
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::BinOp(token::Star) => {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// *self
|
|
|
|
|
// *const self
|
|
|
|
|
// *mut self
|
|
|
|
|
// *not_self
|
|
|
|
|
// Emit special error for `self` cases.
|
2016-10-19 20:33:41 +00:00
|
|
|
|
if isolated_self(self, 1) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-05-08 18:18:21 +00:00
|
|
|
|
self.span_err(self.span, "cannot pass `self` by raw pointer");
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Value(Mutability::Immutable), expect_ident(self))
|
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();
|
|
|
|
|
self.span_err(self.span, "cannot pass `self` by raw pointer");
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Value(Mutability::Immutable), expect_ident(self))
|
2016-05-08 18:18:21 +00:00
|
|
|
|
} else {
|
2016-03-06 12:54:44 +00:00
|
|
|
|
return Ok(None);
|
2013-06-24 04:12:17 +00:00
|
|
|
|
}
|
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) {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// self
|
|
|
|
|
// self: TYPE
|
|
|
|
|
let eself_ident = expect_ident(self);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
|
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 {
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Value(Mutability::Immutable), eself_ident)
|
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
|
|
|
|
}
|
2016-05-08 18:18:21 +00:00
|
|
|
|
} else if self.token.is_keyword(keywords::Mut) &&
|
2016-10-19 20:33:41 +00:00
|
|
|
|
isolated_self(self, 1) {
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// mut self
|
|
|
|
|
// mut self: TYPE
|
|
|
|
|
self.bump();
|
|
|
|
|
let eself_ident = expect_ident(self);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::Colon) {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
|
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 {
|
2016-03-06 12:54:44 +00:00
|
|
|
|
(SelfKind::Value(Mutability::Mutable), eself_ident)
|
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
|
|
|
|
}
|
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
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let eself = codemap::respan(mk_sp(eself_lo, self.prev_span.hi), eself);
|
2016-03-06 12:54:44 +00:00
|
|
|
|
Ok(Some(Arg::from_self(eself, eself_ident)))
|
2016-03-06 12:54:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse 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))?;
|
|
|
|
|
|
|
|
|
|
// Parse optional self argument
|
2016-03-06 12:54:44 +00:00
|
|
|
|
let self_arg = self.parse_self_arg()?;
|
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);
|
2016-03-06 12:54:44 +00:00
|
|
|
|
let fn_inputs = if let Some(self_arg) = self_arg {
|
|
|
|
|
if self.check(&token::CloseDelim(token::Paren)) {
|
|
|
|
|
vec![self_arg]
|
|
|
|
|
} else if self.eat(&token::Comma) {
|
|
|
|
|
let mut fn_inputs = vec![self_arg];
|
|
|
|
|
fn_inputs.append(&mut self.parse_seq_to_before_end(
|
|
|
|
|
&token::CloseDelim(token::Paren), sep, parse_arg_fn)
|
|
|
|
|
);
|
|
|
|
|
fn_inputs
|
|
|
|
|
} else {
|
|
|
|
|
return self.unexpected();
|
2012-07-30 23:33:02 +00:00
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
|
} else {
|
|
|
|
|
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
|
2014-01-27 12:18:36 +00:00
|
|
|
|
};
|
2012-07-30 23:33:02 +00:00
|
|
|
|
|
2016-05-08 18:18:21 +00:00
|
|
|
|
// Parse closing paren and return type.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
2016-03-06 12:54:44 +00:00
|
|
|
|
Ok(P(FnDecl {
|
2013-09-14 02:07:43 +00:00
|
|
|
|
inputs: fn_inputs,
|
2016-05-08 18:18:21 +00:00
|
|
|
|
output: self.parse_ret_ty()?,
|
2013-10-25 05:56:34 +00:00
|
|
|
|
variadic: false
|
2016-03-06 12:54:44 +00:00
|
|
|
|
}))
|
2012-07-30 23:33:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 05:26:56 +00:00
|
|
|
|
// parse the |arg, arg| header on a lambda
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
|
2015-02-03 16:34:05 +00:00
|
|
|
|
let inputs_captures = {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::OrOr) {
|
2015-02-03 16:34:05 +00:00
|
|
|
|
Vec::new()
|
2012-05-23 22:06:11 +00:00
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::BinOp(token::Or))?;
|
2016-01-29 04:49:59 +00:00
|
|
|
|
let args = self.parse_seq_to_before_end(
|
2014-10-27 08:22:52 +00:00
|
|
|
|
&token::BinOp(token::Or),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2013-02-24 23:41:54 +00:00
|
|
|
|
|p| p.parse_fn_block_arg()
|
2016-01-29 04:49:59 +00:00
|
|
|
|
);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-02-03 16:34:05 +00:00
|
|
|
|
args
|
2012-05-04 19:33:04 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let output = self.parse_ret_ty()?;
|
2013-01-16 00:05:20 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(P(FnDecl {
|
2013-09-14 02:07:43 +00:00
|
|
|
|
inputs: inputs_captures,
|
2013-01-10 18:59:58 +00:00
|
|
|
|
output: output,
|
2013-10-25 05:56:34 +00:00
|
|
|
|
variadic: false
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse the name and optional generic types of a function header.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let id = self.parse_ident()?;
|
|
|
|
|
let generics = self.parse_generics()?;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok((id, generics))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
|
fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
|
2016-02-09 10:36:51 +00:00
|
|
|
|
node: ItemKind, vis: Visibility,
|
2014-09-13 16:06:01 +00:00
|
|
|
|
attrs: Vec<Attribute>) -> P<Item> {
|
|
|
|
|
P(Item {
|
2014-01-09 13:05:33 +00:00
|
|
|
|
ident: ident,
|
|
|
|
|
attrs: attrs,
|
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
node: node,
|
|
|
|
|
vis: vis,
|
|
|
|
|
span: mk_sp(lo, hi)
|
2014-09-13 16:06:01 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse an item-position function declaration.
|
2015-02-25 20:05:07 +00:00
|
|
|
|
fn parse_item_fn(&mut self,
|
|
|
|
|
unsafety: Unsafety,
|
2016-08-10 23:20:12 +00:00
|
|
|
|
constness: Spanned<Constness>,
|
2015-02-25 20:05:07 +00:00
|
|
|
|
abi: abi::Abi)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, mut generics) = self.parse_fn_header()?;
|
|
|
|
|
let decl = self.parse_fn_decl(false)?;
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((ident, ItemKind::Fn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs)))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 12:47:04 +00:00
|
|
|
|
/// true if we are looking at `const ID`, false for things like `const fn` etc
|
|
|
|
|
pub fn is_const_item(&mut self) -> bool {
|
|
|
|
|
self.token.is_keyword(keywords::Const) &&
|
2015-10-24 08:52:07 +00:00
|
|
|
|
!self.look_ahead(1, |t| t.is_keyword(keywords::Fn)) &&
|
|
|
|
|
!self.look_ahead(1, |t| t.is_keyword(keywords::Unsafe))
|
2015-05-05 12:47:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// parses all the "front matter" for a `fn` declaration, up to
|
|
|
|
|
/// and including the `fn` keyword:
|
|
|
|
|
///
|
|
|
|
|
/// - `const fn`
|
|
|
|
|
/// - `unsafe fn`
|
2015-10-24 08:52:07 +00:00
|
|
|
|
/// - `const unsafe fn`
|
2015-05-05 12:47:04 +00:00
|
|
|
|
/// - `extern fn`
|
|
|
|
|
/// - etc
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_fn_front_matter(&mut self)
|
2016-08-10 23:20:12 +00:00
|
|
|
|
-> PResult<'a, (Spanned<ast::Constness>,
|
|
|
|
|
ast::Unsafety,
|
|
|
|
|
abi::Abi)> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let is_const_fn = self.eat_keyword(keywords::Const);
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let const_span = self.prev_span;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let unsafety = self.parse_unsafety()?;
|
2015-05-05 12:47:04 +00:00
|
|
|
|
let (constness, unsafety, abi) = if is_const_fn {
|
2016-08-10 23:20:12 +00:00
|
|
|
|
(respan(const_span, Constness::Const), unsafety, Abi::Rust)
|
2015-05-05 12:47:04 +00:00
|
|
|
|
} else {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_opt_abi()?.unwrap_or(Abi::C)
|
2015-05-05 12:47:04 +00:00
|
|
|
|
} else {
|
2016-02-05 12:13:36 +00:00
|
|
|
|
Abi::Rust
|
2015-05-05 12:47:04 +00:00
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
(respan(self.prev_span, Constness::NotConst), unsafety, abi)
|
2015-05-05 12:47:04 +00:00
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Fn)?;
|
2015-05-05 12:47:04 +00:00
|
|
|
|
Ok((constness, unsafety, abi))
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 21:38:58 +00:00
|
|
|
|
/// Parse an impl item.
|
2016-02-11 20:33:09 +00:00
|
|
|
|
pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtImplItem, |x| x);
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let mut attrs = self.parse_outer_attributes()?;
|
2015-04-23 20:45:32 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-04-11 00:39:35 +00:00
|
|
|
|
let vis = self.parse_visibility(true)?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let defaultness = self.parse_defaultness()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let (name, node) = if self.eat_keyword(keywords::Type) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let name = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Eq)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let typ = self.parse_ty()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2015-11-13 13:15:04 +00:00
|
|
|
|
(name, ast::ImplItemKind::Type(typ))
|
2015-05-05 12:47:04 +00:00
|
|
|
|
} else if self.is_const_item() {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Const)?;
|
|
|
|
|
let name = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Colon)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let typ = self.parse_ty()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Eq)?;
|
|
|
|
|
let expr = self.parse_expr()?;
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2015-11-13 13:15:04 +00:00
|
|
|
|
(name, ast::ImplItemKind::Const(typ, expr))
|
2015-03-11 21:38:58 +00:00
|
|
|
|
} else {
|
2016-03-23 10:17:34 +00:00
|
|
|
|
let (name, inner_attrs, node) = self.parse_impl_method(&vis)?;
|
2015-06-10 16:22:20 +00:00
|
|
|
|
attrs.extend(inner_attrs);
|
2015-03-13 09:34:51 +00:00
|
|
|
|
(name, node)
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-11 20:33:09 +00:00
|
|
|
|
Ok(ImplItem {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2015-03-13 09:34:51 +00:00
|
|
|
|
ident: name,
|
|
|
|
|
vis: vis,
|
2015-12-18 22:38:28 +00:00
|
|
|
|
defaultness: defaultness,
|
2015-03-13 09:34:51 +00:00
|
|
|
|
attrs: attrs,
|
|
|
|
|
node: node
|
2016-02-11 20:33:09 +00:00
|
|
|
|
})
|
2014-10-06 14:53:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 10:17:34 +00:00
|
|
|
|
fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
|
|
|
|
|
match *visa {
|
2016-03-31 19:10:38 +00:00
|
|
|
|
Visibility::Inherited => (),
|
|
|
|
|
_ => {
|
2015-12-29 12:59:19 +00:00
|
|
|
|
let is_macro_rules: bool = match self.token {
|
2016-11-16 08:21:52 +00:00
|
|
|
|
token::Ident(sid) => sid.name == Symbol::intern("macro_rules"),
|
2015-12-28 04:31:11 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
if is_macro_rules {
|
2015-12-30 15:27:55 +00:00
|
|
|
|
self.diagnostic().struct_span_err(span, "can't qualify macro_rules \
|
|
|
|
|
invocation with `pub`")
|
2016-04-20 18:49:16 +00:00
|
|
|
|
.help("did you mean #[macro_export]?")
|
2015-12-28 04:31:11 +00:00
|
|
|
|
.emit();
|
|
|
|
|
} else {
|
2015-12-30 15:27:55 +00:00
|
|
|
|
self.diagnostic().struct_span_err(span, "can't qualify macro \
|
|
|
|
|
invocation with `pub`")
|
2016-04-20 18:49:16 +00:00
|
|
|
|
.help("try adjusting the macro to put `pub` \
|
|
|
|
|
inside the invocation")
|
2015-12-28 04:31:11 +00:00
|
|
|
|
.emit();
|
|
|
|
|
}
|
2015-02-13 04:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Parse a method or a macro invocation in a trait impl.
|
2016-03-23 10:17:34 +00:00
|
|
|
|
fn parse_impl_method(&mut self, vis: &Visibility)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
|
2014-07-07 22:15:31 +00:00
|
|
|
|
// code copied from parse_macro_use_or_failure... abstraction!
|
2016-09-22 07:05:05 +00:00
|
|
|
|
if self.token.is_path_start() {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
// method macro.
|
2015-02-13 04:43:57 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.complain_if_pub_macro(&vis, prev_span);
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
2015-11-26 19:14:10 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-09-22 07:05:05 +00:00
|
|
|
|
let pth = self.parse_path(PathStyle::Mod)?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Not)?;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
|
|
|
|
// eat a matched-delimiter token tree:
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let delim = self.expect_open_delim()?;
|
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
2016-03-22 22:58:45 +00:00
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree())?;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
if delim != token::Brace {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?
|
2014-07-07 22:15:31 +00:00
|
|
|
|
}
|
2016-09-22 22:26:35 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let mac = spanned(lo, self.prev_span.hi, Mac_ { path: pth, tts: tts });
|
2016-09-22 22:26:35 +00:00
|
|
|
|
Ok((keywords::Invalid.ident(), vec![], ast::ImplItemKind::Macro(mac)))
|
2015-03-13 09:34:51 +00:00
|
|
|
|
} else {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
|
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
2016-03-06 12:54:44 +00:00
|
|
|
|
let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
|
2015-11-13 13:15:04 +00:00
|
|
|
|
Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
generics: generics,
|
|
|
|
|
abi: abi,
|
|
|
|
|
unsafety: unsafety,
|
2015-05-05 12:47:04 +00:00
|
|
|
|
constness: constness,
|
2015-03-13 09:34:51 +00:00
|
|
|
|
decl: decl
|
2015-03-28 21:58:51 +00:00
|
|
|
|
}, body)))
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse trait Foo { ... }
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_trait(&mut self, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
let mut tps = self.parse_generics()?;
|
2012-08-03 22:24:11 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
// Parse optional colon and supertrait bounds.
|
|
|
|
|
let bounds = if self.eat(&token::Colon) {
|
|
|
|
|
self.parse_ty_param_bounds()?
|
|
|
|
|
} else {
|
|
|
|
|
Vec::new()
|
|
|
|
|
};
|
2012-08-03 22:02:01 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
tps.where_clause = self.parse_where_clause()?;
|
2014-08-11 16:32:26 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let meths = self.parse_trait_items()?;
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((ident, ItemKind::Trait(unsafety, tps, bounds, meths), None))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 21:14:52 +00:00
|
|
|
|
/// Parses items implementations variants
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// impl<T> Foo { ... }
|
2015-01-22 21:14:52 +00:00
|
|
|
|
/// impl<T> ToString for &'static T { ... }
|
|
|
|
|
/// impl Send for .. {}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> PResult<'a, ItemInfo> {
|
2015-01-22 21:14:52 +00:00
|
|
|
|
let impl_span = self.span;
|
|
|
|
|
|
2012-07-24 23:38:24 +00:00
|
|
|
|
// First, parse type parameters if necessary.
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let mut generics = self.parse_generics()?;
|
2012-07-24 23:38:24 +00:00
|
|
|
|
|
2013-03-29 01:55:35 +00:00
|
|
|
|
// Special case: if the next identifier that follows is '(', don't
|
|
|
|
|
// allow this to be parsed as a trait.
|
2014-10-29 10:37:54 +00:00
|
|
|
|
let could_be_trait = self.token != token::OpenDelim(token::Paren);
|
2013-03-29 01:55:35 +00:00
|
|
|
|
|
2014-12-28 22:33:18 +00:00
|
|
|
|
let neg_span = self.span;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let polarity = if self.eat(&token::Not) {
|
2014-12-28 22:33:18 +00:00
|
|
|
|
ast::ImplPolarity::Negative
|
|
|
|
|
} else {
|
|
|
|
|
ast::ImplPolarity::Positive
|
|
|
|
|
};
|
|
|
|
|
|
2013-02-15 05:17:26 +00:00
|
|
|
|
// Parse the trait.
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let mut ty = self.parse_ty()?;
|
2012-07-18 23:18:02 +00:00
|
|
|
|
|
2012-08-08 22:34:17 +00:00
|
|
|
|
// Parse traits, if necessary.
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
|
2013-01-29 19:14:53 +00:00
|
|
|
|
// New-style trait. Reinterpret the type as a trait.
|
2015-01-22 21:14:52 +00:00
|
|
|
|
match ty.node {
|
2016-02-08 15:53:21 +00:00
|
|
|
|
TyKind::Path(None, ref path) => {
|
2014-09-05 19:21:02 +00:00
|
|
|
|
Some(TraitRef {
|
|
|
|
|
path: (*path).clone(),
|
2015-01-29 19:18:17 +00:00
|
|
|
|
ref_id: ty.id,
|
2014-09-05 19:21:02 +00:00
|
|
|
|
})
|
2013-01-29 19:14:53 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
2013-06-17 19:16:30 +00:00
|
|
|
|
self.span_err(ty.span, "not a trait");
|
2013-01-29 19:14:53 +00:00
|
|
|
|
None
|
|
|
|
|
}
|
2015-01-22 21:14:52 +00:00
|
|
|
|
}
|
2012-08-08 22:34:17 +00:00
|
|
|
|
} else {
|
2014-12-28 22:33:18 +00:00
|
|
|
|
match polarity {
|
|
|
|
|
ast::ImplPolarity::Negative => {
|
|
|
|
|
// This is a negated type implementation
|
|
|
|
|
// `impl !MyType {}`, which is not allowed.
|
|
|
|
|
self.span_err(neg_span, "inherent implementation can't be negated");
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2012-09-07 22:11:26 +00:00
|
|
|
|
None
|
2012-08-08 22:34:17 +00:00
|
|
|
|
};
|
2012-07-24 23:38:24 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if opt_trait.is_some() && self.eat(&token::DotDot) {
|
2015-01-22 21:14:52 +00:00
|
|
|
|
if generics.is_parameterized() {
|
|
|
|
|
self.span_err(impl_span, "default trait implementations are not \
|
2015-05-21 06:17:50 +00:00
|
|
|
|
allowed to have generics");
|
2015-01-22 21:14:52 +00:00
|
|
|
|
}
|
2012-10-23 00:57:10 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
|
|
|
|
self.expect(&token::CloseDelim(token::Brace))?;
|
2016-04-18 21:42:18 +00:00
|
|
|
|
Ok((keywords::Invalid.ident(),
|
2016-02-09 10:36:51 +00:00
|
|
|
|
ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
|
2015-01-22 21:14:52 +00:00
|
|
|
|
} else {
|
|
|
|
|
if opt_trait.is_some() {
|
2017-01-16 23:13:41 +00:00
|
|
|
|
ty = self.parse_ty()?;
|
2015-01-22 21:14:52 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
|
|
|
|
let attrs = self.parse_inner_attributes()?;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
|
|
|
|
let mut impl_items = vec![];
|
2015-12-30 23:11:53 +00:00
|
|
|
|
while !self.eat(&token::CloseDelim(token::Brace)) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
impl_items.push(self.parse_impl_item()?);
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
2014-02-14 05:07:09 +00:00
|
|
|
|
|
2016-04-18 21:42:18 +00:00
|
|
|
|
Ok((keywords::Invalid.ident(),
|
2016-02-09 10:36:51 +00:00
|
|
|
|
ItemKind::Impl(unsafety, polarity, generics, opt_trait, ty, impl_items),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Some(attrs)))
|
2015-01-22 21:14:52 +00:00
|
|
|
|
}
|
2012-11-07 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 23:55:21 +00:00
|
|
|
|
/// Parse a::B<String,i32>
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_trait_ref(&mut self) -> PResult<'a, TraitRef> {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
Ok(TraitRef {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
path: self.parse_path(PathStyle::Type)?,
|
2014-11-07 11:53:45 +00:00
|
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2014-11-07 11:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<LifetimeDef>> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::For) {
|
2017-01-17 18:18:29 +00:00
|
|
|
|
self.expect_lt()?;
|
|
|
|
|
let (lifetime_defs, ty_params) = self.parse_generic_params()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_gt()?;
|
2017-01-17 18:18:29 +00:00
|
|
|
|
if !ty_params.is_empty() {
|
|
|
|
|
self.span_err(ty_params[0].span,
|
|
|
|
|
"only lifetime parameters can be used in this context");
|
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(lifetime_defs)
|
2014-11-07 11:53:45 +00:00
|
|
|
|
} else {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(Vec::new())
|
2014-11-07 11:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 23:55:21 +00:00
|
|
|
|
/// Parse for<'l> a::B<String,i32>
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_poly_trait_ref(&mut self) -> PResult<'a, PolyTraitRef> {
|
2015-02-05 08:46:01 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
2014-11-07 11:53:45 +00:00
|
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
|
Ok(PolyTraitRef {
|
2014-11-07 11:53:45 +00:00
|
|
|
|
bound_lifetimes: lifetime_defs,
|
2016-03-23 03:01:37 +00:00
|
|
|
|
trait_ref: self.parse_trait_ref()?,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
span: mk_sp(lo, self.prev_span.hi),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2014-11-07 11:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse struct Foo { ... }
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let class_name = self.parse_ident()?;
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
2012-08-16 00:10:23 +00:00
|
|
|
|
|
2015-01-02 12:02:50 +00:00
|
|
|
|
// There is a special case worth noting here, as reported in issue #17904.
|
|
|
|
|
// If we are parsing a tuple struct it is the case that the where clause
|
|
|
|
|
// should follow the field list. Like so:
|
|
|
|
|
//
|
|
|
|
|
// struct Foo<T>(T) where T: Copy;
|
|
|
|
|
//
|
|
|
|
|
// If we are parsing a normal record-style struct it is the case
|
|
|
|
|
// that the where clause comes before the body, and after the generics.
|
|
|
|
|
// So if we look ahead and see a brace or a where-clause we begin
|
|
|
|
|
// parsing a record style struct.
|
|
|
|
|
//
|
|
|
|
|
// Otherwise if we look ahead and see a paren we parse a tuple-style
|
|
|
|
|
// struct.
|
|
|
|
|
|
2015-10-10 00:28:40 +00:00
|
|
|
|
let vdata = if self.token.is_keyword(keywords::Where) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::Semi) {
|
2015-01-04 10:35:14 +00:00
|
|
|
|
// If we see a: `struct Foo<T> where T: Copy;` style decl.
|
2015-10-10 00:28:40 +00:00
|
|
|
|
VariantData::Unit(ast::DUMMY_NODE_ID)
|
2015-01-04 10:35:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
// If we see: `struct Foo<T> where T: Copy { ... }`
|
2016-03-23 03:01:37 +00:00
|
|
|
|
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
|
2015-01-04 10:35:14 +00:00
|
|
|
|
}
|
|
|
|
|
// No `where` so: `struct Foo<T>;`
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat(&token::Semi) {
|
2015-10-10 00:28:40 +00:00
|
|
|
|
VariantData::Unit(ast::DUMMY_NODE_ID)
|
2015-01-04 10:35:14 +00:00
|
|
|
|
// Record-style struct definition
|
|
|
|
|
} else if self.token == token::OpenDelim(token::Brace) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
|
2015-01-04 10:35:14 +00:00
|
|
|
|
// Tuple-style struct definition with optional where-clause.
|
2015-09-07 19:15:36 +00:00
|
|
|
|
} else if self.token == token::OpenDelim(token::Paren) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID);
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
self.expect(&token::Semi)?;
|
2015-11-09 15:43:32 +00:00
|
|
|
|
body
|
2015-09-07 19:15:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
let token_str = self.this_token_to_string();
|
2015-09-10 12:14:24 +00:00
|
|
|
|
return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
|
|
|
|
|
name, found `{}`", token_str)))
|
2015-01-04 10:35:14 +00:00
|
|
|
|
};
|
2014-08-11 16:32:26 +00:00
|
|
|
|
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((class_name, ItemKind::Struct(vdata, generics), None))
|
2015-01-04 10:35:14 +00:00
|
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
|
|
2016-08-08 22:18:47 +00:00
|
|
|
|
/// Parse union Foo { ... }
|
|
|
|
|
fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
|
|
|
|
|
let class_name = self.parse_ident()?;
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
|
|
|
|
|
|
let vdata = if self.token.is_keyword(keywords::Where) {
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
|
|
|
|
|
} else if self.token == token::OpenDelim(token::Brace) {
|
|
|
|
|
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
|
|
|
|
|
} else {
|
|
|
|
|
let token_str = self.this_token_to_string();
|
|
|
|
|
return Err(self.fatal(&format!("expected `where` or `{{` after union \
|
|
|
|
|
name, found `{}`", token_str)))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((class_name, ItemKind::Union(vdata, generics), None))
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:58:47 +00:00
|
|
|
|
pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
2015-01-04 10:35:14 +00:00
|
|
|
|
let mut fields = Vec::new();
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat(&token::OpenDelim(token::Brace)) {
|
2014-10-29 10:37:54 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2016-10-18 04:47:58 +00:00
|
|
|
|
fields.push(self.parse_struct_decl_field().map_err(|e| {
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
self.eat(&token::CloseDelim(token::Brace));
|
|
|
|
|
e
|
|
|
|
|
})?);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2015-01-04 10:35:14 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-01-04 10:35:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
let token_str = self.this_token_to_string();
|
2015-09-10 12:14:24 +00:00
|
|
|
|
return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
|
|
|
|
|
name, found `{}`",
|
2015-03-28 21:58:51 +00:00
|
|
|
|
token_str)));
|
2015-01-04 10:35:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(fields)
|
2015-01-04 10:35:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:58:47 +00:00
|
|
|
|
pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
2015-01-04 10:35:14 +00:00
|
|
|
|
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
|
2015-09-07 17:08:57 +00:00
|
|
|
|
// Unit like structs are handled in parse_item_struct function
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let fields = self.parse_unspanned_seq(
|
2015-09-07 17:08:57 +00:00
|
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
|
&token::CloseDelim(token::Paren),
|
2016-02-23 04:24:42 +00:00
|
|
|
|
SeqSep::trailing_allowed(token::Comma),
|
2015-09-07 17:08:57 +00:00
|
|
|
|
|p| {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = p.parse_outer_attributes()?;
|
2015-09-07 17:08:57 +00:00
|
|
|
|
let lo = p.span.lo;
|
2016-04-23 05:40:55 +00:00
|
|
|
|
let mut vis = p.parse_visibility(false)?;
|
|
|
|
|
let ty_is_interpolated =
|
|
|
|
|
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let mut ty = p.parse_ty()?;
|
2016-04-23 05:40:55 +00:00
|
|
|
|
|
|
|
|
|
// Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
|
|
|
|
|
if vis == Visibility::Public && !ty_is_interpolated &&
|
|
|
|
|
p.token != token::Comma && p.token != token::CloseDelim(token::Paren) {
|
|
|
|
|
ty = if let TyKind::Paren(ref path_ty) = ty.node {
|
|
|
|
|
if let TyKind::Path(None, ref path) = path_ty.node {
|
|
|
|
|
vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
|
2017-01-16 23:13:41 +00:00
|
|
|
|
Some(p.parse_ty()?)
|
2016-04-23 05:40:55 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}.unwrap_or(ty);
|
|
|
|
|
}
|
2016-04-06 08:19:10 +00:00
|
|
|
|
Ok(StructField {
|
|
|
|
|
span: mk_sp(lo, p.span.hi),
|
|
|
|
|
vis: vis,
|
2016-04-02 13:47:53 +00:00
|
|
|
|
ident: None,
|
2015-09-07 17:08:57 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-04-06 08:19:10 +00:00
|
|
|
|
ty: ty,
|
2015-09-07 17:08:57 +00:00
|
|
|
|
attrs: attrs,
|
2016-04-06 08:19:10 +00:00
|
|
|
|
})
|
2016-03-23 03:01:37 +00:00
|
|
|
|
})?;
|
2015-01-04 10:35:14 +00:00
|
|
|
|
|
2015-09-07 17:08:57 +00:00
|
|
|
|
Ok(fields)
|
2012-02-01 03:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a structure field declaration
|
2013-12-30 22:04:00 +00:00
|
|
|
|
pub fn parse_single_struct_field(&mut self,
|
2016-08-08 12:35:15 +00:00
|
|
|
|
lo: BytePos,
|
2014-01-09 13:05:33 +00:00
|
|
|
|
vis: Visibility,
|
2014-02-28 21:09:09 +00:00
|
|
|
|
attrs: Vec<Attribute> )
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, StructField> {
|
2016-08-08 12:35:15 +00:00
|
|
|
|
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
|
token::Comma => {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2013-02-25 03:27:43 +00:00
|
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
|
token::CloseDelim(token::Brace) => {}
|
2016-05-28 02:05:22 +00:00
|
|
|
|
token::DocComment(_) => return Err(self.span_fatal_help(self.span,
|
|
|
|
|
"found a documentation comment that doesn't document anything",
|
|
|
|
|
"doc comments must come before what they document, maybe a comment was \
|
|
|
|
|
intended with `//`?")),
|
|
|
|
|
_ => return Err(self.span_fatal_help(self.span,
|
|
|
|
|
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
|
|
|
|
|
"struct fields should be separated by commas")),
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(a_var)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2012-03-29 01:50:33 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse an element of a struct definition
|
2016-02-28 15:58:47 +00:00
|
|
|
|
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2016-08-08 12:35:15 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-04-11 00:39:35 +00:00
|
|
|
|
let vis = self.parse_visibility(true)?;
|
2016-08-08 12:35:15 +00:00
|
|
|
|
self.parse_single_struct_field(lo, vis, attrs)
|
2012-02-01 03:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 05:40:55 +00:00
|
|
|
|
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
|
|
|
|
|
fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
|
|
|
|
|
let pub_crate = |this: &mut Self| {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let span = this.prev_span;
|
2016-04-23 05:40:55 +00:00
|
|
|
|
this.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
|
Ok(Visibility::Crate(span))
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-11 00:39:35 +00:00
|
|
|
|
if !self.eat_keyword(keywords::Pub) {
|
|
|
|
|
Ok(Visibility::Inherited)
|
2016-04-23 05:40:55 +00:00
|
|
|
|
} else if !allow_path {
|
|
|
|
|
// Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
|
|
|
|
|
if self.token == token::OpenDelim(token::Paren) &&
|
|
|
|
|
self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
|
|
|
|
|
self.bump(); self.bump();
|
|
|
|
|
pub_crate(self)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Visibility::Public)
|
|
|
|
|
}
|
|
|
|
|
} else if !self.eat(&token::OpenDelim(token::Paren)) {
|
2016-04-11 00:39:35 +00:00
|
|
|
|
Ok(Visibility::Public)
|
|
|
|
|
} else if self.eat_keyword(keywords::Crate) {
|
2016-04-23 05:40:55 +00:00
|
|
|
|
pub_crate(self)
|
2016-04-11 00:39:35 +00:00
|
|
|
|
} else {
|
2016-12-05 03:51:11 +00:00
|
|
|
|
let path = self.parse_path(PathStyle::Mod)?.default_to_global();
|
2016-04-11 00:39:35 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
|
Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
|
|
|
|
|
}
|
2012-02-23 05:47:23 +00:00
|
|
|
|
}
|
2013-03-22 19:56:10 +00:00
|
|
|
|
|
2015-12-18 22:38:28 +00:00
|
|
|
|
/// Parse defaultness: DEFAULT or nothing
|
2016-01-30 00:25:18 +00:00
|
|
|
|
fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
if self.eat_contextual_keyword(keywords::Default.ident()) {
|
2015-12-18 22:38:28 +00:00
|
|
|
|
Ok(Defaultness::Default)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Defaultness::Final)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Given a termination token, parse all of the items in a module
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_mod_items(&mut self, term: &token::Token, inner_lo: BytePos) -> PResult<'a, Mod> {
|
2014-12-23 13:07:30 +00:00
|
|
|
|
let mut items = vec![];
|
2016-03-23 03:01:37 +00:00
|
|
|
|
while let Some(item) = self.parse_item()? {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
items.push(item);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2010-09-23 20:15:51 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(term) {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
let token_str = self.this_token_to_string();
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-02-04 16:10:04 +00:00
|
|
|
|
|
2016-06-21 22:08:13 +00:00
|
|
|
|
let hi = if self.span == syntax_pos::DUMMY_SP {
|
2015-07-06 02:13:19 +00:00
|
|
|
|
inner_lo
|
|
|
|
|
} else {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
self.prev_span.hi
|
2015-07-06 02:13:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Mod {
|
2015-07-06 02:13:19 +00:00
|
|
|
|
inner: mk_sp(inner_lo, hi),
|
2014-04-26 20:05:45 +00:00
|
|
|
|
items: items
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let id = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Colon)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Eq)?;
|
|
|
|
|
let e = self.parse_expr()?;
|
2016-07-01 23:40:45 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
|
|
|
|
let item = match m {
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Some(m) => ItemKind::Static(ty, m, e),
|
|
|
|
|
None => ItemKind::Const(ty, e),
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
|
|
|
|
};
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok((id, item, None))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a `mod <foo> { ... }` or `mod <foo>;` item
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
|
2016-09-14 22:36:42 +00:00
|
|
|
|
let (in_cfg, outer_attrs) = {
|
|
|
|
|
let mut strip_unconfigured = ::config::StripUnconfigured {
|
|
|
|
|
sess: self.sess,
|
|
|
|
|
should_test: false, // irrelevant
|
|
|
|
|
features: None, // don't perform gated feature checking
|
|
|
|
|
};
|
|
|
|
|
let outer_attrs = strip_unconfigured.process_cfg_attrs(outer_attrs.to_owned());
|
2017-01-18 00:13:36 +00:00
|
|
|
|
(!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
|
2016-09-14 22:36:42 +00:00
|
|
|
|
};
|
2016-06-29 09:28:50 +00:00
|
|
|
|
|
2013-12-30 23:17:53 +00:00
|
|
|
|
let id_span = self.span;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let id = self.parse_ident()?;
|
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.check(&token::Semi) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-09-14 22:36:42 +00:00
|
|
|
|
if in_cfg {
|
|
|
|
|
// This mod is in an external file. Let's go get it!
|
2016-11-14 09:31:03 +00:00
|
|
|
|
let ModulePathSuccess { path, directory_ownership, warn } =
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.submod_path(id, &outer_attrs, id_span)?;
|
2016-11-14 09:31:03 +00:00
|
|
|
|
let (module, mut attrs) =
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
|
2016-11-14 09:31:03 +00:00
|
|
|
|
if warn {
|
|
|
|
|
let attr = ast::Attribute {
|
|
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
|
style: ast::AttrStyle::Outer,
|
|
|
|
|
value: ast::MetaItem {
|
|
|
|
|
name: Symbol::intern("warn_directory_ownership"),
|
|
|
|
|
node: ast::MetaItemKind::Word,
|
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
|
},
|
|
|
|
|
is_sugared_doc: false,
|
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
|
};
|
|
|
|
|
attr::mark_known(&attr);
|
|
|
|
|
attrs.push(attr);
|
|
|
|
|
}
|
2016-11-05 04:16:26 +00:00
|
|
|
|
Ok((id, module, Some(attrs)))
|
2016-09-14 22:36:42 +00:00
|
|
|
|
} else {
|
|
|
|
|
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
|
|
|
|
|
Ok((id, ItemKind::Mod(placeholder), None))
|
|
|
|
|
}
|
2012-11-10 00:31:44 +00:00
|
|
|
|
} else {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let old_directory = self.directory.clone();
|
|
|
|
|
self.push_directory(id, &outer_attrs);
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
2014-04-26 20:05:45 +00:00
|
|
|
|
let mod_inner_lo = self.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_inner_attributes()?;
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
|
|
|
|
|
self.directory = old_directory;
|
|
|
|
|
Ok((id, ItemKind::Mod(module), Some(attrs)))
|
2012-11-10 00:31:44 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
|
2016-11-14 09:31:03 +00:00
|
|
|
|
if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.directory.path.push(&*path.as_str());
|
|
|
|
|
self.directory.ownership = DirectoryOwnership::Owned;
|
2016-09-27 21:14:45 +00:00
|
|
|
|
} else {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.directory.path.push(&*id.name.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
|
}
|
2012-12-11 20:20:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 00:50:18 +00:00
|
|
|
|
pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
|
2016-11-14 09:31:03 +00:00
|
|
|
|
attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&*d.as_str()))
|
2015-07-03 00:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns either a path to a module, or .
|
|
|
|
|
pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath
|
|
|
|
|
{
|
2015-07-28 16:07:20 +00:00
|
|
|
|
let mod_name = id.to_string();
|
2015-07-03 00:50:18 +00:00
|
|
|
|
let default_path_str = format!("{}.rs", mod_name);
|
|
|
|
|
let secondary_path_str = format!("{}/mod.rs", mod_name);
|
|
|
|
|
let default_path = dir_path.join(&default_path_str);
|
|
|
|
|
let secondary_path = dir_path.join(&secondary_path_str);
|
|
|
|
|
let default_exists = codemap.file_exists(&default_path);
|
|
|
|
|
let secondary_exists = codemap.file_exists(&secondary_path);
|
|
|
|
|
|
|
|
|
|
let result = match (default_exists, secondary_exists) {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
(true, false) => Ok(ModulePathSuccess {
|
|
|
|
|
path: default_path,
|
2016-11-14 09:31:03 +00:00
|
|
|
|
directory_ownership: DirectoryOwnership::UnownedViaMod(false),
|
|
|
|
|
warn: false,
|
2016-11-05 04:16:26 +00:00
|
|
|
|
}),
|
|
|
|
|
(false, true) => Ok(ModulePathSuccess {
|
|
|
|
|
path: secondary_path,
|
|
|
|
|
directory_ownership: DirectoryOwnership::Owned,
|
2016-11-14 09:31:03 +00:00
|
|
|
|
warn: false,
|
2016-11-05 04:16:26 +00:00
|
|
|
|
}),
|
2015-07-03 00:50:18 +00:00
|
|
|
|
(false, false) => Err(ModulePathError {
|
|
|
|
|
err_msg: format!("file not found for module `{}`", mod_name),
|
|
|
|
|
help_msg: format!("name the file either {} or {} inside the directory {:?}",
|
|
|
|
|
default_path_str,
|
|
|
|
|
secondary_path_str,
|
|
|
|
|
dir_path.display()),
|
|
|
|
|
}),
|
|
|
|
|
(true, true) => Err(ModulePathError {
|
|
|
|
|
err_msg: format!("file for module `{}` found at both {} and {}",
|
|
|
|
|
mod_name,
|
|
|
|
|
default_path_str,
|
|
|
|
|
secondary_path_str),
|
|
|
|
|
help_msg: "delete or rename one of them to remove the ambiguity".to_owned(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ModulePath {
|
|
|
|
|
name: mod_name,
|
|
|
|
|
path_exists: default_exists || secondary_exists,
|
|
|
|
|
result: result,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn submod_path(&mut self,
|
|
|
|
|
id: ast::Ident,
|
|
|
|
|
outer_attrs: &[ast::Attribute],
|
2015-12-20 21:00:43 +00:00
|
|
|
|
id_sp: Span) -> PResult<'a, ModulePathSuccess> {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) {
|
|
|
|
|
return Ok(ModulePathSuccess {
|
|
|
|
|
directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
|
|
|
|
|
Some("mod.rs") => DirectoryOwnership::Owned,
|
2016-11-14 09:31:03 +00:00
|
|
|
|
_ => DirectoryOwnership::UnownedViaMod(true),
|
2016-11-05 04:16:26 +00:00
|
|
|
|
},
|
|
|
|
|
path: path,
|
2016-11-14 09:31:03 +00:00
|
|
|
|
warn: false,
|
2016-11-05 04:16:26 +00:00
|
|
|
|
});
|
2015-07-03 00:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let paths = Parser::default_submod_path(id, &self.directory.path, self.sess.codemap());
|
2015-07-03 00:50:18 +00:00
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
if let DirectoryOwnership::UnownedViaBlock = self.directory.ownership {
|
2016-02-11 00:52:44 +00:00
|
|
|
|
let msg =
|
|
|
|
|
"Cannot declare a non-inline module inside a block unless it has a path attribute";
|
|
|
|
|
let mut err = self.diagnostic().struct_span_err(id_sp, msg);
|
|
|
|
|
if paths.path_exists {
|
|
|
|
|
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
|
|
|
|
|
paths.name);
|
|
|
|
|
err.span_note(id_sp, &msg);
|
|
|
|
|
}
|
|
|
|
|
return Err(err);
|
2016-11-14 09:31:03 +00:00
|
|
|
|
} else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
|
|
|
|
|
if warn {
|
|
|
|
|
if let Ok(result) = paths.result {
|
|
|
|
|
return Ok(ModulePathSuccess { warn: true, ..result });
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-20 21:00:43 +00:00
|
|
|
|
let mut err = self.diagnostic().struct_span_err(id_sp,
|
|
|
|
|
"cannot declare a new module at this location");
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let this_module = match self.directory.path.file_name() {
|
2016-08-31 09:02:45 +00:00
|
|
|
|
Some(file_name) => file_name.to_str().unwrap().to_owned(),
|
2015-07-03 00:50:18 +00:00
|
|
|
|
None => self.root_module_name.as_ref().unwrap().clone(),
|
|
|
|
|
};
|
2015-12-20 21:00:43 +00:00
|
|
|
|
err.span_note(id_sp,
|
|
|
|
|
&format!("maybe move this module `{0}` to its own directory \
|
2015-07-03 00:50:18 +00:00
|
|
|
|
via `{0}/mod.rs`",
|
|
|
|
|
this_module));
|
|
|
|
|
if paths.path_exists {
|
2015-12-20 21:00:43 +00:00
|
|
|
|
err.span_note(id_sp,
|
|
|
|
|
&format!("... or maybe `use` the module `{}` instead \
|
|
|
|
|
of possibly redeclaring it",
|
|
|
|
|
paths.name));
|
2016-11-14 09:31:03 +00:00
|
|
|
|
return Err(err);
|
|
|
|
|
} else {
|
|
|
|
|
return Err(err);
|
|
|
|
|
};
|
2015-07-03 00:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match paths.result {
|
|
|
|
|
Ok(succ) => Ok(succ),
|
|
|
|
|
Err(err) => Err(self.span_fatal_help(id_sp, &err.err_msg, &err.help_msg)),
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-19 01:56:50 +00:00
|
|
|
|
|
2015-07-03 00:50:18 +00:00
|
|
|
|
/// Read a module from a source file.
|
|
|
|
|
fn eval_src_mod(&mut self,
|
2016-11-05 04:16:26 +00:00
|
|
|
|
path: PathBuf,
|
|
|
|
|
directory_ownership: DirectoryOwnership,
|
|
|
|
|
name: String,
|
2015-07-03 00:50:18 +00:00
|
|
|
|
id_sp: Span)
|
2016-02-09 10:36:51 +00:00
|
|
|
|
-> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
|
2014-03-20 22:05:37 +00:00
|
|
|
|
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
|
2016-06-14 05:43:30 +00:00
|
|
|
|
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
|
|
|
|
|
let mut err = String::from("circular modules: ");
|
|
|
|
|
let len = included_mod_stack.len();
|
|
|
|
|
for p in &included_mod_stack[i.. len] {
|
|
|
|
|
err.push_str(&p.to_string_lossy());
|
|
|
|
|
err.push_str(" -> ");
|
2013-07-04 17:51:11 +00:00
|
|
|
|
}
|
2016-06-14 05:43:30 +00:00
|
|
|
|
err.push_str(&path.to_string_lossy());
|
|
|
|
|
return Err(self.span_fatal(id_sp, &err[..]));
|
2013-07-04 17:51:11 +00:00
|
|
|
|
}
|
2014-03-20 22:05:37 +00:00
|
|
|
|
included_mod_stack.push(path.clone());
|
|
|
|
|
drop(included_mod_stack);
|
2013-07-04 17:51:11 +00:00
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let mut p0 =
|
|
|
|
|
new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp);
|
2017-01-18 00:13:36 +00:00
|
|
|
|
p0.cfg_mods = self.cfg_mods;
|
2014-04-26 20:05:45 +00:00
|
|
|
|
let mod_inner_lo = p0.span.lo;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let mod_attrs = p0.parse_inner_attributes()?;
|
|
|
|
|
let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
|
2014-03-20 22:05:37 +00:00
|
|
|
|
self.sess.included_mod_stack.borrow_mut().pop();
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((ast::ItemKind::Mod(m0), mod_attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a function declaration from a foreign module
|
2015-09-18 22:27:51 +00:00
|
|
|
|
fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: BytePos,
|
2016-02-11 20:33:09 +00:00
|
|
|
|
attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Fn)?;
|
2013-08-02 21:30:00 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, mut generics) = self.parse_fn_header()?;
|
|
|
|
|
let decl = self.parse_fn_decl(true)?;
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
2013-04-12 05:10:31 +00:00
|
|
|
|
let hi = self.span.hi;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2016-02-11 20:33:09 +00:00
|
|
|
|
Ok(ast::ForeignItem {
|
2014-09-13 16:06:01 +00:00
|
|
|
|
ident: ident,
|
|
|
|
|
attrs: attrs,
|
2016-02-09 10:31:19 +00:00
|
|
|
|
node: ForeignItemKind::Fn(decl, generics),
|
2014-09-13 16:06:01 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
|
vis: vis
|
2016-02-11 20:33:09 +00:00
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse a static item from a foreign module
|
2015-09-18 22:27:51 +00:00
|
|
|
|
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: BytePos,
|
2016-02-11 20:33:09 +00:00
|
|
|
|
attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Static)?;
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let mutbl = self.eat_keyword(keywords::Mut);
|
2013-03-19 21:46:27 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
self.expect(&token::Colon)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2012-08-25 22:09:33 +00:00
|
|
|
|
let hi = self.span.hi;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2016-02-11 20:33:09 +00:00
|
|
|
|
Ok(ForeignItem {
|
2014-05-16 17:45:16 +00:00
|
|
|
|
ident: ident,
|
|
|
|
|
attrs: attrs,
|
2016-02-09 10:31:19 +00:00
|
|
|
|
node: ForeignItemKind::Static(ty, mutbl),
|
2014-05-16 17:45:16 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
|
span: mk_sp(lo, hi),
|
2014-09-13 16:06:01 +00:00
|
|
|
|
vis: vis
|
2016-02-11 20:33:09 +00:00
|
|
|
|
})
|
2012-08-25 22:09:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 22:52:06 +00:00
|
|
|
|
/// Parse extern crate links
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2014-02-02 22:52:06 +00:00
|
|
|
|
///
|
2015-03-27 00:35:13 +00:00
|
|
|
|
/// extern crate foo;
|
|
|
|
|
/// extern crate bar as foo;
|
2014-02-02 22:52:06 +00:00
|
|
|
|
fn parse_item_extern_crate(&mut self,
|
2015-03-27 00:35:13 +00:00
|
|
|
|
lo: BytePos,
|
|
|
|
|
visibility: Visibility,
|
|
|
|
|
attrs: Vec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Item>> {
|
2012-08-31 00:09:04 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let crate_name = self.parse_ident()?;
|
|
|
|
|
let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
|
2015-08-01 05:20:25 +00:00
|
|
|
|
(Some(crate_name.name), ident)
|
2015-03-27 00:35:13 +00:00
|
|
|
|
} else {
|
|
|
|
|
(None, crate_name)
|
2012-08-29 19:22:05 +00:00
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2012-08-14 18:07:41 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2015-09-18 06:07:05 +00:00
|
|
|
|
ident,
|
2016-02-09 10:36:51 +00:00
|
|
|
|
ItemKind::ExternCrate(maybe_path),
|
2015-09-18 06:07:05 +00:00
|
|
|
|
visibility,
|
|
|
|
|
attrs))
|
2014-02-02 22:52:06 +00:00
|
|
|
|
}
|
2012-11-28 01:25:55 +00:00
|
|
|
|
|
2014-02-02 22:52:06 +00:00
|
|
|
|
/// Parse `extern` for foreign ABIs
|
|
|
|
|
/// modules.
|
|
|
|
|
///
|
|
|
|
|
/// `extern` is expected to have been
|
|
|
|
|
/// consumed before calling this method
|
|
|
|
|
///
|
|
|
|
|
/// # Examples:
|
|
|
|
|
///
|
|
|
|
|
/// extern "C" {}
|
|
|
|
|
/// extern {}
|
|
|
|
|
fn parse_item_foreign_mod(&mut self,
|
|
|
|
|
lo: BytePos,
|
2014-04-02 08:19:41 +00:00
|
|
|
|
opt_abi: Option<abi::Abi>,
|
2014-02-02 22:52:06 +00:00
|
|
|
|
visibility: Visibility,
|
2015-03-13 09:34:51 +00:00
|
|
|
|
mut attrs: Vec<Attribute>)
|
2015-12-20 21:00:43 +00:00
|
|
|
|
-> PResult<'a, P<Item>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
2012-08-14 18:07:41 +00:00
|
|
|
|
|
2016-02-05 12:13:36 +00:00
|
|
|
|
let abi = opt_abi.unwrap_or(Abi::C);
|
2012-11-28 01:25:55 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
attrs.extend(self.parse_inner_attributes()?);
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
|
|
|
|
let mut foreign_items = vec![];
|
2016-03-23 03:01:37 +00:00
|
|
|
|
while let Some(item) = self.parse_foreign_item()? {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
foreign_items.push(item);
|
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Brace))?;
|
2013-11-26 22:54:32 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2015-03-13 09:34:51 +00:00
|
|
|
|
let m = ast::ForeignMod {
|
|
|
|
|
abi: abi,
|
|
|
|
|
items: foreign_items
|
|
|
|
|
};
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2016-04-18 21:42:18 +00:00
|
|
|
|
keywords::Invalid.ident(),
|
2016-02-09 10:36:51 +00:00
|
|
|
|
ItemKind::ForeignMod(m),
|
2014-12-23 13:07:30 +00:00
|
|
|
|
visibility,
|
2015-03-28 21:58:51 +00:00
|
|
|
|
attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-02-01 18:40:04 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse type Foo = Bar;
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_type(&mut self) -> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
let mut tps = self.parse_generics()?;
|
|
|
|
|
tps.where_clause = self.parse_where_clause()?;
|
|
|
|
|
self.expect(&token::Eq)?;
|
2017-01-16 23:13:41 +00:00
|
|
|
|
let ty = self.parse_ty()?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Semi)?;
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((ident, ItemKind::Ty(ty, tps), None))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2012-04-19 04:26:25 +00:00
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse the part of an "enum" decl following the '{'
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> {
|
2014-02-28 21:09:09 +00:00
|
|
|
|
let mut variants = Vec::new();
|
2013-06-05 04:43:41 +00:00
|
|
|
|
let mut all_nullary = true;
|
2014-09-20 20:29:26 +00:00
|
|
|
|
let mut any_disr = None;
|
2014-10-29 10:37:54 +00:00
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let variant_attrs = self.parse_outer_attributes()?;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let vlo = self.span.lo;
|
2012-08-09 02:51:19 +00:00
|
|
|
|
|
2015-10-01 15:47:27 +00:00
|
|
|
|
let struct_def;
|
2013-06-05 04:43:41 +00:00
|
|
|
|
let mut disr_expr = None;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let ident = self.parse_ident()?;
|
2015-11-09 15:43:32 +00:00
|
|
|
|
if self.check(&token::OpenDelim(token::Brace)) {
|
2013-03-28 18:29:21 +00:00
|
|
|
|
// Parse a struct variant.
|
|
|
|
|
all_nullary = false;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
struct_def = VariantData::Struct(self.parse_record_struct_body()?,
|
2015-11-09 15:43:32 +00:00
|
|
|
|
ast::DUMMY_NODE_ID);
|
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 if self.check(&token::OpenDelim(token::Paren)) {
|
2013-03-28 18:29:21 +00:00
|
|
|
|
all_nullary = false;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
struct_def = VariantData::Tuple(self.parse_tuple_struct_body()?,
|
2015-11-09 15:43:32 +00:00
|
|
|
|
ast::DUMMY_NODE_ID);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
} else if self.eat(&token::Eq) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
disr_expr = Some(self.parse_expr()?);
|
2014-09-20 20:29:26 +00:00
|
|
|
|
any_disr = disr_expr.as_ref().map(|expr| expr.span);
|
2015-11-09 15:43:32 +00:00
|
|
|
|
struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
|
2013-03-28 18:29:21 +00:00
|
|
|
|
} else {
|
2015-11-09 15:43:32 +00:00
|
|
|
|
struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2012-01-25 13:10:33 +00:00
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
|
let vr = ast::Variant_ {
|
2013-01-16 00:05:20 +00:00
|
|
|
|
name: ident,
|
|
|
|
|
attrs: variant_attrs,
|
2015-10-08 00:20:57 +00:00
|
|
|
|
data: struct_def,
|
2013-01-16 00:05:20 +00:00
|
|
|
|
disr_expr: disr_expr,
|
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
variants.push(spanned(vlo, self.prev_span.hi, vr));
|
2012-01-25 13:10:33 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::Comma) { break; }
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::CloseDelim(token::Brace))?;
|
2014-09-20 20:29:26 +00:00
|
|
|
|
match any_disr {
|
|
|
|
|
Some(disr_span) if !all_nullary =>
|
|
|
|
|
self.span_err(disr_span,
|
|
|
|
|
"discriminator values can only be used with a c-like enum"),
|
|
|
|
|
_ => ()
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2012-08-08 21:17:52 +00:00
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::EnumDef { variants: variants })
|
2012-08-08 21:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parse an "enum" declaration
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let id = self.parse_ident()?;
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
2012-08-08 21:17:52 +00:00
|
|
|
|
|
2016-10-18 04:47:58 +00:00
|
|
|
|
let enum_definition = self.parse_enum_def(&generics).map_err(|e| {
|
|
|
|
|
self.recover_stmt();
|
|
|
|
|
self.eat(&token::CloseDelim(token::Brace));
|
|
|
|
|
e
|
|
|
|
|
})?;
|
2016-02-09 10:36:51 +00:00
|
|
|
|
Ok((id, ItemKind::Enum(enum_definition, generics), None))
|
2012-01-10 21:50:40 +00:00
|
|
|
|
}
|
2010-11-24 19:36:35 +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.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_opt_abi(&mut self) -> PResult<'a, Option<abi::Abi>> {
|
2013-12-30 23:09:41 +00:00
|
|
|
|
match self.token {
|
2014-11-19 04:48:38 +00:00
|
|
|
|
token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
|
|
|
|
|
let sp = self.span;
|
|
|
|
|
self.expect_no_suffix(sp, "ABI spec", suf);
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-07-28 16:07:20 +00:00
|
|
|
|
match abi::lookup(&s.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;
|
2014-04-02 08:19:41 +00:00
|
|
|
|
self.span_err(
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span,
|
2015-07-27 00:49:38 +00:00
|
|
|
|
&format!("invalid ABI: expected one of [{}], \
|
2014-05-16 17:45:16 +00:00
|
|
|
|
found `{}`",
|
2015-07-10 12:19:21 +00:00
|
|
|
|
abi::all_names().join(", "),
|
2015-07-28 16:07:20 +00:00
|
|
|
|
s));
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Parse one of the items allowed by the flags.
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// NB: this function no longer parses the items inside an
|
|
|
|
|
/// extern crate.
|
2014-12-23 13:07:30 +00:00
|
|
|
|
fn parse_item_(&mut self, attrs: Vec<Attribute>,
|
2015-12-20 21:00:43 +00:00
|
|
|
|
macros_allowed: bool, attributes_allowed: bool) -> PResult<'a, Option<P<Item>>> {
|
2016-11-02 03:03:55 +00:00
|
|
|
|
maybe_whole!(self, NtItem, |item| {
|
|
|
|
|
let mut item = item.unwrap();
|
2016-07-03 21:38:37 +00:00
|
|
|
|
let mut attrs = attrs;
|
|
|
|
|
mem::swap(&mut item.attrs, &mut attrs);
|
|
|
|
|
item.attrs.extend(attrs);
|
2016-11-02 03:03:55 +00:00
|
|
|
|
Some(P(item))
|
|
|
|
|
});
|
2013-08-08 17:28:06 +00:00
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2012-08-03 20:58:14 +00:00
|
|
|
|
|
2016-04-11 00:39:35 +00:00
|
|
|
|
let visibility = self.parse_visibility(true)?;
|
2013-04-01 22:50:58 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Use) {
|
2014-12-23 13:07:30 +00:00
|
|
|
|
// USE ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let item_ = ItemKind::Use(self.parse_view_path()?);
|
|
|
|
|
self.expect(&token::Semi)?;
|
2014-12-23 13:07:30 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2014-12-23 13:07:30 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2016-04-18 21:42:18 +00:00
|
|
|
|
keywords::Invalid.ident(),
|
2014-12-23 13:07:30 +00:00
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
attrs);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2012-08-03 20:58:14 +00:00
|
|
|
|
}
|
2014-12-23 13:07:30 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Extern) {
|
|
|
|
|
if self.eat_keyword(keywords::Crate) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?));
|
2014-02-02 22:52:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let opt_abi = self.parse_opt_abi()?;
|
2012-08-03 20:58:14 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Fn) {
|
2013-04-01 22:50:58 +00:00
|
|
|
|
// EXTERN FUNCTION ITEM
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let fn_span = self.prev_span;
|
2016-02-05 12:13:36 +00:00
|
|
|
|
let abi = opt_abi.unwrap_or(Abi::C);
|
2013-04-01 22:50:58 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-08-10 23:20:12 +00:00
|
|
|
|
self.parse_item_fn(Unsafety::Normal,
|
|
|
|
|
respan(fn_span, Constness::NotConst),
|
|
|
|
|
abi)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
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 if self.check(&token::OpenDelim(token::Brace)) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
return Ok(Some(self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs)?));
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
2014-02-02 22:52:06 +00:00
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.unexpected()?;
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
2014-02-02 22:52:06 +00:00
|
|
|
|
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Static) {
|
2013-10-03 09:53:46 +00:00
|
|
|
|
// STATIC ITEM
|
2016-02-09 16:44:47 +00:00
|
|
|
|
let m = if self.eat_keyword(keywords::Mut) {
|
|
|
|
|
Mutability::Mutable
|
|
|
|
|
} else {
|
|
|
|
|
Mutability::Immutable
|
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2014-10-02 22:06:08 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2014-10-02 22:06:08 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2014-10-02 22:06:08 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Const) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let const_span = self.prev_span;
|
2015-10-24 08:52:07 +00:00
|
|
|
|
if self.check_keyword(keywords::Fn)
|
|
|
|
|
|| (self.check_keyword(keywords::Unsafe)
|
|
|
|
|
&& self.look_ahead(1, |t| t.is_keyword(keywords::Fn))) {
|
2015-02-25 20:05:07 +00:00
|
|
|
|
// CONST FUNCTION ITEM
|
2015-12-30 23:11:53 +00:00
|
|
|
|
let unsafety = if self.eat_keyword(keywords::Unsafe) {
|
2015-10-24 08:52:07 +00:00
|
|
|
|
Unsafety::Unsafe
|
|
|
|
|
} else {
|
|
|
|
|
Unsafety::Normal
|
|
|
|
|
};
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2015-02-25 20:05:07 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-08-10 23:20:12 +00:00
|
|
|
|
self.parse_item_fn(unsafety,
|
|
|
|
|
respan(const_span, Constness::Const),
|
|
|
|
|
Abi::Rust)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2015-02-25 20:05:07 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2015-02-25 20:05:07 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-05-05 12:47:04 +00:00
|
|
|
|
return Ok(Some(item));
|
2015-02-25 20:05:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 22:06:08 +00:00
|
|
|
|
// CONST ITEM
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Mut) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.diagnostic().struct_span_err(prev_span, "const globals cannot be mutable")
|
2016-04-20 18:49:16 +00:00
|
|
|
|
.help("did you mean to declare a static?")
|
2015-12-20 21:00:43 +00:00
|
|
|
|
.emit();
|
2014-10-02 22:06:08 +00:00
|
|
|
|
}
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_const(None)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Unsafe) &&
|
2015-01-28 01:01:48 +00:00
|
|
|
|
self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
|
2014-12-10 00:59:20 +00:00
|
|
|
|
{
|
|
|
|
|
// UNSAFE TRAIT ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Unsafe)?;
|
|
|
|
|
self.expect_keyword(keywords::Trait)?;
|
2014-12-10 00:59:20 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_item_trait(ast::Unsafety::Unsafe)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2014-12-10 00:59:20 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2014-12-10 00:59:20 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2014-12-10 00:59:20 +00:00
|
|
|
|
}
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Unsafe) &&
|
2015-01-28 01:01:48 +00:00
|
|
|
|
self.look_ahead(1, |t| t.is_keyword(keywords::Impl))
|
2014-12-10 11:15:06 +00:00
|
|
|
|
{
|
|
|
|
|
// IMPL ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Unsafe)?;
|
|
|
|
|
self.expect_keyword(keywords::Impl)?;
|
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2014-12-10 11:15:06 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2014-12-10 11:15:06 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2014-12-10 11:15:06 +00:00
|
|
|
|
}
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Fn) {
|
2013-03-29 17:35:23 +00:00
|
|
|
|
// FUNCTION ITEM
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let fn_span = self.prev_span;
|
2013-03-14 02:25:28 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-08-10 23:20:12 +00:00
|
|
|
|
self.parse_item_fn(Unsafety::Normal,
|
|
|
|
|
respan(fn_span, Constness::NotConst),
|
|
|
|
|
Abi::Rust)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Unsafe)
|
2015-01-28 01:01:48 +00:00
|
|
|
|
&& self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) {
|
2013-04-01 22:50:58 +00:00
|
|
|
|
// UNSAFE FUNCTION ITEM
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
|
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_opt_abi()?.unwrap_or(Abi::C)
|
2014-05-07 01:43:56 +00:00
|
|
|
|
} else {
|
2016-02-05 12:13:36 +00:00
|
|
|
|
Abi::Rust
|
2014-05-07 01:43:56 +00:00
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect_keyword(keywords::Fn)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let fn_span = self.prev_span;
|
2013-03-14 02:25:28 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-08-10 23:20:12 +00:00
|
|
|
|
self.parse_item_fn(Unsafety::Unsafe,
|
|
|
|
|
respan(fn_span, Constness::NotConst),
|
|
|
|
|
abi)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Mod) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// MODULE ITEM
|
2014-02-28 20:54:01 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_item_mod(&attrs[..])?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Type) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// TYPE ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_type()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Enum) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// ENUM ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_enum()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Trait) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// TRAIT ITEM
|
2014-12-10 00:59:20 +00:00
|
|
|
|
let (ident, item_, extra_attrs) =
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_item_trait(ast::Unsafety::Normal)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Impl) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// IMPL ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal)?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::Struct) {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// STRUCT ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_struct()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2016-08-08 22:18:47 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2016-08-08 22:18:47 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
|
|
|
|
return Ok(Some(item));
|
|
|
|
|
}
|
2016-10-19 20:33:41 +00:00
|
|
|
|
if self.is_union_item() {
|
2016-08-08 22:18:47 +00:00
|
|
|
|
// UNION ITEM
|
2016-08-18 15:31:47 +00:00
|
|
|
|
self.bump();
|
2016-08-08 22:18:47 +00:00
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_union()?;
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
|
let item = self.mk_item(lo,
|
2016-09-21 02:09:22 +00:00
|
|
|
|
prev_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
|
ident,
|
|
|
|
|
item_,
|
|
|
|
|
visibility,
|
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 09:34:51 +00:00
|
|
|
|
/// Parse a foreign item.
|
2016-02-11 20:33:09 +00:00
|
|
|
|
fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2015-04-23 20:45:32 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-04-11 00:39:35 +00:00
|
|
|
|
let visibility = self.parse_visibility(true)?;
|
2013-04-01 22:50:58 +00:00
|
|
|
|
|
2015-01-16 03:04:28 +00:00
|
|
|
|
if self.check_keyword(keywords::Static) {
|
2013-10-03 09:53:46 +00:00
|
|
|
|
// FOREIGN STATIC ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
2016-05-02 06:45:38 +00:00
|
|
|
|
if self.check_keyword(keywords::Fn) {
|
2013-04-01 22:50:58 +00:00
|
|
|
|
// FOREIGN FUNCTION ITEM
|
2016-03-23 03:01:37 +00:00
|
|
|
|
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2014-12-23 13:07:30 +00:00
|
|
|
|
|
|
|
|
|
// FIXME #5668: this will occur for a macro invocation:
|
2016-03-23 03:01:37 +00:00
|
|
|
|
match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
Some(item) => {
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Err(self.span_fatal(item.span, "macros cannot expand to foreign items"));
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
None => Ok(None)
|
2015-03-13 09:34:51 +00:00
|
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// This is the fall-through for parsing items.
|
2013-04-01 22:50:58 +00:00
|
|
|
|
fn parse_macro_use_or_failure(
|
2013-12-30 22:04:00 +00:00
|
|
|
|
&mut self,
|
2014-02-28 21:09:09 +00:00
|
|
|
|
attrs: Vec<Attribute> ,
|
2013-04-01 22:50:58 +00:00
|
|
|
|
macros_allowed: bool,
|
2015-11-03 16:39:51 +00:00
|
|
|
|
attributes_allowed: bool,
|
2014-01-09 13:05:33 +00:00
|
|
|
|
lo: BytePos,
|
|
|
|
|
visibility: Visibility
|
2015-12-20 21:00:43 +00:00
|
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
2016-09-22 07:05:05 +00:00
|
|
|
|
if macros_allowed && self.token.is_path_start() {
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// MACRO INVOCATION ITEM
|
2012-11-22 03:38:27 +00:00
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.complain_if_pub_macro(&visibility, prev_span);
|
2015-02-13 04:43:57 +00:00
|
|
|
|
|
2015-11-26 19:14:10 +00:00
|
|
|
|
let mac_lo = self.span.lo;
|
|
|
|
|
|
2012-07-05 19:10:33 +00:00
|
|
|
|
// item macro.
|
2016-09-22 07:05:05 +00:00
|
|
|
|
let pth = self.parse_path(PathStyle::Mod)?;
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.expect(&token::Not)?;
|
2012-11-19 05:36:26 +00:00
|
|
|
|
|
|
|
|
|
// a 'special' identifier (like what `macro_rules!` uses)
|
|
|
|
|
// is optional. We should eventually unify invoc syntax
|
|
|
|
|
// and remove this.
|
2016-04-16 01:10:59 +00:00
|
|
|
|
let id = if self.token.is_ident() {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
self.parse_ident()?
|
2012-11-21 19:49:19 +00:00
|
|
|
|
} else {
|
2016-04-18 21:42:18 +00:00
|
|
|
|
keywords::Invalid.ident() // no special identifier
|
2012-11-09 04:12:45 +00:00
|
|
|
|
};
|
2013-02-11 21:36:24 +00:00
|
|
|
|
// eat a matched-delimiter token tree:
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let delim = self.expect_open_delim()?;
|
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
2016-03-22 22:58:45 +00:00
|
|
|
|
SeqSep::none(),
|
|
|
|
|
|p| p.parse_token_tree())?;
|
2014-11-14 17:18:10 +00:00
|
|
|
|
if delim != token::Brace {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if !self.eat(&token::Semi) {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
self.span_err(prev_span,
|
2014-11-14 17:18:10 +00:00
|
|
|
|
"macros that expand to items must either \
|
|
|
|
|
be surrounded with braces or followed by \
|
|
|
|
|
a semicolon");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = self.prev_span.hi;
|
2016-09-22 22:26:35 +00:00
|
|
|
|
let mac = spanned(mac_lo, hi, Mac_ { path: pth, tts: tts });
|
|
|
|
|
let item = self.mk_item(lo, hi, id, ItemKind::Mac(mac), visibility, attrs);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
return Ok(Some(item));
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FAILURE TO PARSE ITEM
|
2014-11-10 10:58:03 +00:00
|
|
|
|
match visibility {
|
2016-02-09 16:57:49 +00:00
|
|
|
|
Visibility::Inherited => {}
|
2016-03-31 19:10:38 +00:00
|
|
|
|
_ => {
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
|
return Err(self.span_fatal(prev_span, "unmatched visibility `pub`"));
|
2013-06-12 02:13:42 +00:00
|
|
|
|
}
|
2013-03-20 01:00:18 +00:00
|
|
|
|
}
|
2015-03-13 09:34:51 +00:00
|
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
|
if !attributes_allowed && !attrs.is_empty() {
|
2015-03-13 09:34:51 +00:00
|
|
|
|
self.expected_item_err(&attrs);
|
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(None)
|
2012-08-14 00:11:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2015-11-03 16:39:51 +00:00
|
|
|
|
self.parse_item_(attrs, true, false)
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-17 00:48:40 +00:00
|
|
|
|
fn parse_path_list_items(&mut self) -> PResult<'a, Vec<ast::PathListItem>> {
|
|
|
|
|
self.parse_unspanned_seq(&token::OpenDelim(token::Brace),
|
|
|
|
|
&token::CloseDelim(token::Brace),
|
|
|
|
|
SeqSep::trailing_allowed(token::Comma), |this| {
|
|
|
|
|
let lo = this.span.lo;
|
2016-08-12 09:01:22 +00:00
|
|
|
|
let ident = if this.eat_keyword(keywords::SelfValue) {
|
|
|
|
|
keywords::SelfValue.ident()
|
2016-04-17 00:48:40 +00:00
|
|
|
|
} else {
|
2016-08-12 09:01:22 +00:00
|
|
|
|
this.parse_ident()?
|
|
|
|
|
};
|
|
|
|
|
let rename = this.parse_rename()?;
|
|
|
|
|
let node = ast::PathListItem_ {
|
|
|
|
|
name: ident,
|
|
|
|
|
rename: rename,
|
|
|
|
|
id: ast::DUMMY_NODE_ID
|
2016-04-17 00:48:40 +00:00
|
|
|
|
};
|
2016-09-21 02:09:22 +00:00
|
|
|
|
let hi = this.prev_span.hi;
|
2016-04-17 00:48:40 +00:00
|
|
|
|
Ok(spanned(lo, hi, node))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `::{` 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))
|
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
|
2016-04-17 00:48:40 +00:00
|
|
|
|
/// Matches ViewPath:
|
|
|
|
|
/// MOD_SEP? non_global_path
|
|
|
|
|
/// MOD_SEP? non_global_path as IDENT
|
|
|
|
|
/// MOD_SEP? non_global_path MOD_SEP STAR
|
|
|
|
|
/// MOD_SEP? non_global_path MOD_SEP LBRACE item_seq RBRACE
|
|
|
|
|
/// MOD_SEP? LBRACE item_seq RBRACE
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_view_path(&mut self) -> PResult<'a, P<ViewPath>> {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2016-10-23 21:43:41 +00:00
|
|
|
|
if self.check(&token::OpenDelim(token::Brace)) || self.check(&token::BinOp(token::Star)) ||
|
|
|
|
|
self.is_import_coupler() {
|
|
|
|
|
// `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`.
|
2016-12-05 03:51:11 +00:00
|
|
|
|
self.eat(&token::ModSep);
|
2016-04-17 00:48:40 +00:00
|
|
|
|
let prefix = ast::Path {
|
2016-12-05 03:51:11 +00:00
|
|
|
|
segments: vec![ast::PathSegment::crate_root()],
|
2013-12-04 21:08:42 +00:00
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
|
};
|
2016-10-23 21:43:41 +00:00
|
|
|
|
let view_path_kind = if self.eat(&token::BinOp(token::Star)) {
|
|
|
|
|
ViewPathGlob(prefix)
|
|
|
|
|
} else {
|
|
|
|
|
ViewPathList(prefix, self.parse_path_list_items()?)
|
|
|
|
|
};
|
|
|
|
|
Ok(P(spanned(lo, self.span.hi, view_path_kind)))
|
2016-04-17 00:48:40 +00:00
|
|
|
|
} else {
|
2016-12-05 03:51:11 +00:00
|
|
|
|
let prefix = self.parse_path(PathStyle::Mod)?.default_to_global();
|
2016-04-17 00:48:40 +00:00
|
|
|
|
if self.is_import_coupler() {
|
|
|
|
|
// `foo::bar::{a, b}` or `foo::bar::*`
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-04-17 00:48:40 +00:00
|
|
|
|
if self.check(&token::BinOp(token::Star)) {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
self.bump();
|
2016-04-17 00:48:40 +00:00
|
|
|
|
Ok(P(spanned(lo, self.span.hi, ViewPathGlob(prefix))))
|
|
|
|
|
} else {
|
|
|
|
|
let items = self.parse_path_list_items()?;
|
|
|
|
|
Ok(P(spanned(lo, self.span.hi, ViewPathList(prefix, items))))
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2016-04-17 00:48:40 +00:00
|
|
|
|
} else {
|
|
|
|
|
// `foo::bar` or `foo::bar as baz`
|
|
|
|
|
let rename = self.parse_rename()?.
|
|
|
|
|
unwrap_or(prefix.segments.last().unwrap().identifier);
|
2016-09-21 02:09:22 +00:00
|
|
|
|
Ok(P(spanned(lo, self.prev_span.hi, ViewPathSimple(rename, prefix))))
|
2011-08-16 22:21:30 +00:00
|
|
|
|
}
|
2011-01-28 16:54:59 +00:00
|
|
|
|
}
|
2015-08-01 05:20:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 21:00:43 +00:00
|
|
|
|
fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
|
2015-12-30 23:11:53 +00:00
|
|
|
|
if self.eat_keyword(keywords::As) {
|
2015-08-01 05:20:25 +00:00
|
|
|
|
self.parse_ident().map(Some)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
2014-08-13 02:25:05 +00:00
|
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
|
/// Parses a source module as a crate. This is the main
|
|
|
|
|
/// entry point for the parser.
|
2015-12-20 21:00:43 +00:00
|
|
|
|
pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> {
|
2012-05-23 22:06:11 +00:00
|
|
|
|
let lo = self.span.lo;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok(ast::Crate {
|
2016-03-23 03:01:37 +00:00
|
|
|
|
attrs: self.parse_inner_attributes()?,
|
|
|
|
|
module: self.parse_mod_items(&token::Eof, lo)?,
|
2014-07-10 22:41:11 +00:00
|
|
|
|
span: mk_sp(lo, self.span.lo),
|
|
|
|
|
exported_macros: Vec::new(),
|
2015-03-28 21:58:51 +00:00
|
|
|
|
})
|
2011-06-23 22:42:55 +00:00
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
|
pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
|
2014-11-19 04:48:38 +00:00
|
|
|
|
let ret = match self.token {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
token::Literal(token::Str_(s), suf) => (s, ast::StrStyle::Cooked, suf),
|
|
|
|
|
token::Literal(token::StrRaw(s, n), suf) => (s, ast::StrStyle::Raw(n), suf),
|
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;
|
2015-09-03 07:54:53 +00:00
|
|
|
|
self.expect_no_suffix(sp, "string literal", suf);
|
2015-03-28 21:58:51 +00:00
|
|
|
|
Ok((s, style))
|
2014-11-19 04:48:38 +00:00
|
|
|
|
}
|
2015-03-28 21:58:51 +00:00
|
|
|
|
_ => Err(self.fatal("expected string literal"))
|
2011-02-25 01:00:24 +00:00
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
}
|
2011-01-11 02:18:16 +00:00
|
|
|
|
}
|