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.
|
|
|
|
|
2014-03-22 01:05:05 +00:00
|
|
|
#![macro_escape]
|
2013-08-08 17:28:06 +00:00
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::PathParsingMode::*;
|
|
|
|
use self::ItemOrViewItem::*;
|
|
|
|
|
2013-03-14 02:25:28 +00:00
|
|
|
use abi;
|
2014-08-06 02:44:21 +00:00
|
|
|
use ast::{AssociatedType, BareFnTy, ClosureTy};
|
2014-08-28 01:46:52 +00:00
|
|
|
use ast::{RegionTyParamBound, TraitTyParamBound};
|
2014-12-09 15:36:46 +00:00
|
|
|
use ast::{ProvidedMethod, Public, Unsafety};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue};
|
2014-08-06 02:44:21 +00:00
|
|
|
use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, Block};
|
2014-09-30 21:59:56 +00:00
|
|
|
use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{Crate, CrateConfig, Decl, DeclItem};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain};
|
2013-12-18 00:46:18 +00:00
|
|
|
use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox};
|
2014-01-28 00:03:32 +00:00
|
|
|
use ast::{ExprBreak, ExprCall, ExprCast};
|
2014-11-19 16:18:17 +00:00
|
|
|
use ast::{ExprField, ExprTupField, ExprClosure, ExprIf, ExprIfLet, ExprIndex, ExprSlice};
|
2014-03-09 06:36:10 +00:00
|
|
|
use ast::{ExprLit, ExprLoop, ExprMac};
|
2014-11-26 15:07:22 +00:00
|
|
|
use ast::{ExprMethodCall, ExprParen, ExprPath};
|
2014-11-19 16:18:17 +00:00
|
|
|
use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary};
|
2014-10-03 02:45:46 +00:00
|
|
|
use ast::{ExprVec, ExprWhile, ExprWhileLet, ExprForLoop, Field, FnDecl};
|
2014-11-26 15:07:22 +00:00
|
|
|
use ast::{Many};
|
2014-07-30 05:08:39 +00:00
|
|
|
use ast::{FnUnboxedClosureKind, FnMutUnboxedClosureKind};
|
|
|
|
use ast::{FnOnceUnboxedClosureKind};
|
2014-11-09 15:14:15 +00:00
|
|
|
use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod, FunctionRetTy};
|
2014-12-09 15:36:46 +00:00
|
|
|
use ast::{Ident, Inherited, ImplItem, Item, Item_, ItemStatic};
|
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
|
|
|
use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemConst};
|
2014-09-05 19:21:02 +00:00
|
|
|
use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy};
|
|
|
|
use ast::{LifetimeDef, Lit, Lit_};
|
2014-06-18 17:44:20 +00:00
|
|
|
use ast::{LitBool, LitChar, LitByte, LitBinary};
|
2014-11-09 15:14:15 +00:00
|
|
|
use ast::{LitStr, LitInt, Local, LocalLet};
|
2014-10-06 23:18:24 +00:00
|
|
|
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchNormal};
|
|
|
|
use ast::{Method, MutTy, BiMul, Mutability};
|
2014-09-13 16:06:01 +00:00
|
|
|
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, UnNot};
|
|
|
|
use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
|
2014-08-06 15:04:44 +00:00
|
|
|
use ast::{PatTup, PatBox, PatWild, PatWildMulti, PatWildSingle};
|
2014-11-07 11:53:45 +00:00
|
|
|
use ast::{PolyTraitRef};
|
2014-08-06 02:44:21 +00:00
|
|
|
use ast::{QPath, RequiredMethod};
|
2014-11-09 15:14:15 +00:00
|
|
|
use ast::{Return, BiShl, BiShr, Stmt, StmtDecl};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{StmtExpr, StmtSemi, StmtMac, StructDef, StructField};
|
2014-11-22 15:02:49 +00:00
|
|
|
use ast::{StructVariantKind, BiSub, StrStyle};
|
2014-07-23 17:21:50 +00:00
|
|
|
use ast::{SelfExplicit, SelfRegion, SelfStatic, SelfValue};
|
2014-11-02 11:21:16 +00:00
|
|
|
use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
|
|
|
|
use ast::{TtDelimited, TtSequence, TtToken};
|
2014-11-29 04:08:30 +00:00
|
|
|
use ast::{TupleVariantKind, Ty, Ty_, TypeBinding};
|
2014-11-26 15:07:22 +00:00
|
|
|
use ast::{TypeField, TyFixedLengthVec, TyClosure, TyBareFn};
|
2014-04-09 12:33:42 +00:00
|
|
|
use ast::{TyTypeof, TyInfer, TypeMethod};
|
2014-11-09 15:14:15 +00:00
|
|
|
use ast::{TyParam, TyParamBound, TyParen, TyPath, TyPolyTraitRef, TyPtr, TyQPath};
|
|
|
|
use ast::{TyRptr, TyTup, TyU32, TyVec, UnUniq};
|
2014-08-06 02:44:21 +00:00
|
|
|
use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind};
|
2014-09-06 04:27:47 +00:00
|
|
|
use ast::{UnnamedField, UnsafeBlock};
|
2014-12-09 15:36:46 +00:00
|
|
|
use ast::{ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse};
|
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};
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2014-11-22 15:02:49 +00:00
|
|
|
use ast_util::{mod, as_prec, ident_to_path, operator_prec};
|
|
|
|
use codemap::{mod, Span, BytePos, Spanned, spanned, mk_sp};
|
2014-11-10 10:58:03 +00:00
|
|
|
use diagnostic;
|
2014-10-06 22:00:56 +00:00
|
|
|
use ext::tt::macro_parser;
|
2014-07-03 07:47:30 +00:00
|
|
|
use parse;
|
2014-01-09 13:05:33 +00:00
|
|
|
use parse::attr::ParserAttr;
|
2013-03-01 18:44:43 +00:00
|
|
|
use parse::classify;
|
2014-11-23 11:14:35 +00:00
|
|
|
use parse::common::{SeqSep, seq_sep_none, seq_sep_trailing_allowed};
|
|
|
|
use parse::lexer::{Reader, TokenAndSpan};
|
2013-08-09 08:25:24 +00:00
|
|
|
use parse::obsolete::*;
|
2014-11-23 11:14:35 +00:00
|
|
|
use parse::token::{mod, MatchNt, SubstNt, InternedString};
|
2014-10-27 12:33:30 +00:00
|
|
|
use parse::token::{keywords, special_idents};
|
2013-09-07 02:11:55 +00:00
|
|
|
use parse::{new_sub_parser_from_file, ParseSess};
|
2014-10-28 00:05:28 +00:00
|
|
|
use print::pprust;
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2014-03-19 14:52:37 +00:00
|
|
|
use owned_slice::OwnedSlice;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2014-05-30 02:03:06 +00:00
|
|
|
use std::collections::HashSet;
|
2014-09-11 05:26:41 +00:00
|
|
|
use std::io::fs::PathExtensions;
|
2014-09-13 16:06:01 +00:00
|
|
|
use std::mem;
|
2014-11-10 05:26:10 +00:00
|
|
|
use std::num::Float;
|
2014-03-27 14:40:35 +00:00
|
|
|
use std::rc::Rc;
|
2014-08-21 04:37:15 +00:00
|
|
|
use std::iter;
|
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
|
|
|
|
2014-09-16 05:22:12 +00:00
|
|
|
bitflags! {
|
|
|
|
flags Restrictions: u8 {
|
2014-10-06 23:33:44 +00:00
|
|
|
const UNRESTRICTED = 0b0000,
|
|
|
|
const RESTRICTION_STMT_EXPR = 0b0001,
|
|
|
|
const RESTRICTION_NO_BAR_OP = 0b0010,
|
|
|
|
const RESTRICTION_NO_STRUCT_LITERAL = 0b0100
|
2014-09-16 05:22:12 +00:00
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
}
|
2011-01-24 23:26:10 +00:00
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-06 01:01:33 +00:00
|
|
|
|
2014-02-28 21:09:09 +00:00
|
|
|
type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
/// How to parse a path. There are four different kinds of paths, all of which
|
|
|
|
/// are parsed somewhat differently.
|
2014-05-30 00:45:07 +00:00
|
|
|
#[deriving(PartialEq)]
|
2013-08-07 16:47:28 +00:00
|
|
|
pub enum PathParsingMode {
|
|
|
|
/// A path with no type parameters; e.g. `foo::bar::Baz`
|
|
|
|
NoTypesAllowed,
|
|
|
|
/// A path with a lifetime and type parameters, with no double colons
|
2013-12-10 07:16:18 +00:00
|
|
|
/// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`
|
2013-08-07 16:47:28 +00:00
|
|
|
LifetimeAndTypesWithoutColons,
|
|
|
|
/// A path with a lifetime and type parameters with double colons before
|
2013-12-10 07:16:18 +00:00
|
|
|
/// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`
|
2013-08-07 16:47:28 +00:00
|
|
|
LifetimeAndTypesWithColons,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-06 01:01:33 +00:00
|
|
|
impl Copy for PathParsingMode {}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
enum ItemOrViewItem {
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Indicates a failure to parse any kind of item. The attributes are
|
|
|
|
/// returned.
|
2014-05-16 07:16:13 +00:00
|
|
|
IoviNone(Vec<Attribute>),
|
2014-09-13 16:06:01 +00:00
|
|
|
IoviItem(P<Item>),
|
|
|
|
IoviForeignItem(P<ForeignItem>),
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(ViewItem)
|
2012-08-14 00:11:52 +00:00
|
|
|
}
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2014-05-11 22:55:01 +00:00
|
|
|
|
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.
|
2012-08-23 00:24:52 +00:00
|
|
|
macro_rules! maybe_whole_expr (
|
2013-02-21 05:04:05 +00:00
|
|
|
($p:expr) => (
|
2013-07-05 10:15:21 +00:00
|
|
|
{
|
2014-05-26 01:33:52 +00:00
|
|
|
let found = match $p.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtExpr(ref e)) => {
|
2014-09-13 16:06:01 +00:00
|
|
|
Some((*e).clone())
|
2013-07-05 10:15:21 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtPath(_)) => {
|
2014-05-26 01:33:52 +00:00
|
|
|
// FIXME: The following avoids an issue with lexical borrowck scopes,
|
|
|
|
// but the clone is unfortunate.
|
|
|
|
let pt = match $p.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtPath(ref pt)) => (**pt).clone(),
|
2014-05-26 01:33:52 +00:00
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = $p.span;
|
|
|
|
Some($p.mk_expr(span.lo, span.hi, ExprPath(pt)))
|
2014-05-26 01:33:52 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtBlock(_)) => {
|
2014-09-13 16:06:01 +00:00
|
|
|
// FIXME: The following avoids an issue with lexical borrowck scopes,
|
|
|
|
// but the clone is unfortunate.
|
|
|
|
let b = match $p.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtBlock(ref b)) => (*b).clone(),
|
2014-09-13 16:06:01 +00:00
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = $p.span;
|
|
|
|
Some($p.mk_expr(span.lo, span.hi, ExprBlock(b)))
|
2013-07-05 10:15:21 +00:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-05-11 22:58:58 +00:00
|
|
|
match found {
|
2013-07-05 10:15:21 +00:00
|
|
|
Some(e) => {
|
|
|
|
$p.bump();
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
None => ()
|
2013-02-21 05:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2012-08-23 00:24:52 +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
|
2012-08-23 00:24:52 +00:00
|
|
|
macro_rules! maybe_whole (
|
2013-03-02 21:02:27 +00:00
|
|
|
($p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return x.clone();
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2013-03-02 21:02:27 +00:00
|
|
|
);
|
2013-11-30 22:00:39 +00:00
|
|
|
(no_clone $p:expr, $constructor:ident) => (
|
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-11-30 22:00:39 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return x;
|
2013-11-30 22:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2013-03-02 21:02:27 +00:00
|
|
|
(deref $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return (*x).clone();
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(Some $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return Some(x.clone());
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(iovi $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return IoviItem(x.clone());
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(pair_empty $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2014-05-11 22:58:58 +00:00
|
|
|
let found = match ($p).token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::$constructor(_)) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::$constructor(x))) = found {
|
|
|
|
return (Vec::new(), x);
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2012-08-23 00:24:52 +00:00
|
|
|
)
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2012-07-26 17:14:01 +00:00
|
|
|
|
2014-10-15 06:05:01 +00:00
|
|
|
fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
|
|
|
|
-> Vec<Attribute> {
|
2012-08-14 18:07:41 +00:00
|
|
|
match rhs {
|
2014-10-15 06:05:01 +00:00
|
|
|
Some(ref attrs) => lhs.extend(attrs.iter().map(|a| a.clone())),
|
|
|
|
None => {}
|
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
|
|
|
}
|
|
|
|
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2013-02-21 08:16:31 +00:00
|
|
|
struct ParsedItemsAndViewItems {
|
2014-05-16 07:16:13 +00:00
|
|
|
attrs_remaining: Vec<Attribute>,
|
|
|
|
view_items: Vec<ViewItem>,
|
2014-09-13 16:06:01 +00:00
|
|
|
items: Vec<P<Item>> ,
|
|
|
|
foreign_items: Vec<P<ForeignItem>>
|
2014-05-25 23:27:36 +00:00
|
|
|
}
|
2013-02-21 08:16:31 +00:00
|
|
|
|
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,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// the span of the prior token:
|
2014-03-27 22:39:48 +00:00
|
|
|
pub last_span: Span,
|
|
|
|
pub cfg: CrateConfig,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// the previous token or None (only stashed sometimes).
|
2014-05-06 01:56:44 +00:00
|
|
|
pub last_token: Option<Box<token::Token>>,
|
2014-03-27 22:39:48 +00:00
|
|
|
pub buffer: [TokenAndSpan, ..4],
|
|
|
|
pub buffer_start: int,
|
|
|
|
pub buffer_end: int,
|
|
|
|
pub tokens_consumed: uint,
|
2014-09-16 05:22:12 +00:00
|
|
|
pub restrictions: Restrictions,
|
2014-03-27 22:39:48 +00:00
|
|
|
pub quote_depth: uint, // not (yet) related to the quasiquoter
|
2014-08-28 01:46:52 +00:00
|
|
|
pub reader: Box<Reader+'a>,
|
2014-03-27 22:39:48 +00:00
|
|
|
pub interner: Rc<token::IdentInterner>,
|
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
|
2014-03-27 22:39:48 +00:00
|
|
|
pub mod_path_stack: Vec<InternedString>,
|
2013-10-07 19:43:42 +00:00
|
|
|
/// Stack of spans of open delimiters. Used for error message.
|
2014-03-27 22:39:48 +00:00
|
|
|
pub open_braces: Vec<Span>,
|
2014-05-16 21:23:04 +00:00
|
|
|
/// Flag if this parser "owns" the directory that it is currently parsing
|
|
|
|
/// in. This will affect how nested files are looked up.
|
|
|
|
pub owns_directory: bool,
|
|
|
|
/// 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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(PartialEq, Eq, Clone)]
|
|
|
|
pub enum TokenType {
|
|
|
|
Token(token::Token),
|
|
|
|
Operator,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenType {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
|
|
|
|
TokenType::Operator => "an operator".into_string(),
|
|
|
|
}
|
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-09-02 23:58:12 +00:00
|
|
|
fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
|
2014-10-27 12:33:30 +00:00
|
|
|
t.is_plain_ident() || *t == token::Underscore
|
2013-09-02 23:58:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:54:34 +00:00
|
|
|
impl<'a> Parser<'a> {
|
2014-08-28 01:46:52 +00:00
|
|
|
pub fn new(sess: &'a ParseSess,
|
|
|
|
cfg: ast::CrateConfig,
|
|
|
|
mut rdr: Box<Reader+'a>)
|
|
|
|
-> Parser<'a>
|
|
|
|
{
|
2014-11-03 04:52:00 +00:00
|
|
|
let tok0 = rdr.real_token();
|
2014-05-25 23:27:36 +00:00
|
|
|
let span = tok0.sp;
|
|
|
|
let placeholder = TokenAndSpan {
|
2014-10-27 08:22:52 +00:00
|
|
|
tok: token::Underscore,
|
2014-05-25 23:27:36 +00:00
|
|
|
sp: span,
|
|
|
|
};
|
|
|
|
|
|
|
|
Parser {
|
|
|
|
reader: rdr,
|
|
|
|
interner: token::get_ident_interner(),
|
|
|
|
sess: sess,
|
|
|
|
cfg: cfg,
|
|
|
|
token: tok0.tok,
|
|
|
|
span: span,
|
|
|
|
last_span: span,
|
|
|
|
last_token: None,
|
|
|
|
buffer: [
|
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
|
|
|
],
|
|
|
|
buffer_start: 0,
|
|
|
|
buffer_end: 0,
|
|
|
|
tokens_consumed: 0,
|
2014-10-06 03:08:35 +00:00
|
|
|
restrictions: UNRESTRICTED,
|
2014-05-25 23:27:36 +00:00
|
|
|
quote_depth: 0,
|
|
|
|
obsolete_set: HashSet::new(),
|
|
|
|
mod_path_stack: Vec::new(),
|
|
|
|
open_braces: Vec::new(),
|
|
|
|
owns_directory: true,
|
|
|
|
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(),
|
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
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn this_token_to_string(&mut self) -> String {
|
|
|
|
Parser::token_to_string(&self.token)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn unexpected_last(&mut self, t: &token::Token) -> ! {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = Parser::token_to_string(t);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_fatal(last_span, format!("unexpected token: `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice());
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn unexpected(&mut self) -> ! {
|
2014-06-21 10:39:03 +00:00
|
|
|
let this_token = self.this_token_to_string();
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(format!("unexpected token: `{}`", this_token).as_slice());
|
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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn expect(&mut self, t: &token::Token) {
|
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 {
|
|
|
|
self.bump();
|
|
|
|
} else {
|
|
|
|
let token_str = Parser::token_to_string(t);
|
|
|
|
let this_token_str = self.this_token_to_string();
|
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
token_str,
|
|
|
|
this_token_str).as_slice())
|
|
|
|
}
|
2013-06-15 01:21:47 +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
|
|
|
self.expect_one_of(slice::ref_slice(t), &[]);
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Expect next token to be edible or inedible token. If edible,
|
|
|
|
/// then consume it; if inedible, then return without consuming
|
|
|
|
/// anything. Signal a fatal error if next token is unexpected.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn expect_one_of(&mut self,
|
|
|
|
edible: &[token::Token],
|
|
|
|
inedible: &[token::Token]) {
|
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()
|
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
|
|
|
.map_or("".into_string(), |t| t.to_string());
|
|
|
|
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(", ");
|
|
|
|
}
|
|
|
|
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) {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.bump();
|
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
|
|
|
|
} 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
|
|
|
let mut expected = edible.iter().map(|x| TokenType::Token(x.clone()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
expected.extend(inedible.iter().map(|x| TokenType::Token(x.clone())));
|
|
|
|
expected.push_all(&*self.expected_tokens);
|
|
|
|
expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
|
|
|
|
expected.dedup();
|
2014-06-21 10:39:03 +00:00
|
|
|
let expect = tokens_to_string(expected.as_slice());
|
|
|
|
let actual = self.this_token_to_string();
|
2013-08-05 20:18:29 +00:00
|
|
|
self.fatal(
|
2014-05-16 17:45:16 +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))
|
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))
|
|
|
|
}).as_slice()
|
2013-08-05 20:18:29 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Check for erroneous `ident { }`; if matches, signal error and
|
|
|
|
/// recover (without consuming any expected input token). Returns
|
|
|
|
/// true if and only if input was consumed for recovery.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn check_for_erroneous_unit_struct_expecting(&mut self, expected: &[token::Token]) -> bool {
|
2014-10-29 10:37:54 +00:00
|
|
|
if self.token == token::OpenDelim(token::Brace)
|
|
|
|
&& expected.iter().all(|t| *t != token::OpenDelim(token::Brace))
|
|
|
|
&& self.look_ahead(1, |t| *t == token::CloseDelim(token::Brace)) {
|
2013-08-05 20:18:29 +00:00
|
|
|
// matched; signal non-fatal error and recover.
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
self.span_err(span,
|
2014-02-06 09:38:08 +00:00
|
|
|
"unit-like struct construction is written with no trailing `{ }`");
|
2014-10-29 10:37:54 +00:00
|
|
|
self.eat(&token::OpenDelim(token::Brace));
|
|
|
|
self.eat(&token::CloseDelim(token::Brace));
|
2013-08-05 20:18:29 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Commit to parsing a complete expression `e` expected to be
|
|
|
|
/// followed by some token from the set edible + inedible. Recover
|
|
|
|
/// from anticipated input errors, discarding erroneous characters.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[token::Token]) {
|
2014-10-15 01:59:41 +00:00
|
|
|
debug!("commit_expr {}", e);
|
2014-11-29 21:41:21 +00:00
|
|
|
if let ExprPath(..) = e.node {
|
|
|
|
// might be unit-struct construction; check for recoverableinput error.
|
|
|
|
let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
|
|
|
|
expected.push_all(inedible);
|
|
|
|
self.check_for_erroneous_unit_struct_expecting(expected.as_slice());
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
self.expect_one_of(edible, inedible)
|
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr(e, &[edible], &[])
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Commit to parsing a complete statement `s`, which expects to be
|
|
|
|
/// followed by some token from the set edible + inedible. Check
|
|
|
|
/// for recoverable input errors, discarding erroneous characters.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token]) {
|
2014-07-07 23:35:15 +00:00
|
|
|
if self.last_token
|
|
|
|
.as_ref()
|
2014-10-27 12:33:30 +00:00
|
|
|
.map_or(false, |t| t.is_ident() || t.is_path()) {
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
|
|
|
|
expected.push_all(inedible.as_slice());
|
2014-02-28 20:54:01 +00:00
|
|
|
self.check_for_erroneous_unit_struct_expecting(
|
|
|
|
expected.as_slice());
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
self.expect_one_of(edible, inedible)
|
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn commit_stmt_expecting(&mut self, edible: token::Token) {
|
|
|
|
self.commit_stmt(&[edible], &[])
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_ident(&mut self) -> ast::Ident {
|
2013-06-15 01:21:47 +00:00
|
|
|
self.check_strict_keywords();
|
|
|
|
self.check_reserved_keywords();
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(i, _) => {
|
2013-06-15 01:21:47 +00:00
|
|
|
self.bump();
|
|
|
|
i
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtIdent(..)) => {
|
2013-06-15 01:21:47 +00:00
|
|
|
self.bug("ident interpolation not converted to real token");
|
|
|
|
}
|
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal((format!("expected ident, found `{}`",
|
|
|
|
token_str)).as_slice())
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 22:56:56 +00:00
|
|
|
pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
|
2013-06-15 01:21:47 +00:00
|
|
|
let lo = self.span.lo;
|
2014-07-17 22:56:56 +00:00
|
|
|
let node = if self.eat_keyword(keywords::Mod) {
|
|
|
|
ast::PathListMod { id: ast::DUMMY_NODE_ID }
|
|
|
|
} else {
|
|
|
|
let ident = self.parse_ident();
|
|
|
|
ast::PathListIdent { name: ident, id: ast::DUMMY_NODE_ID }
|
|
|
|
};
|
2013-06-15 01:21:47 +00:00
|
|
|
let hi = self.last_span.hi;
|
2014-07-17 22:56:56 +00:00
|
|
|
spanned(lo, hi, node)
|
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.
|
|
|
|
///
|
|
|
|
/// This method is will automatically add `tok` to `expected_tokens` if `tok` is not
|
|
|
|
/// 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.
|
2013-12-30 22:04:00 +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);
|
2013-07-18 03:04:37 +00:00
|
|
|
if is_present { self.bump() }
|
|
|
|
is_present
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// If the next token is the given keyword, eat it and return
|
|
|
|
/// true. Otherwise, return false.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(kw) {
|
2014-08-25 01:04:29 +00:00
|
|
|
self.bump();
|
|
|
|
true
|
2014-08-28 04:34:03 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2013-06-15 01:21:47 +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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn expect_keyword(&mut self, kw: keywords::Keyword) {
|
2013-06-15 01:21:47 +00:00
|
|
|
if !self.eat_keyword(kw) {
|
2014-07-06 22:19:29 +00:00
|
|
|
let id_interned_str = token::get_name(kw.to_name());
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2013-12-30 22:04:00 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
id_interned_str, token_str).as_slice())
|
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,
|
2014-08-29 07:18:05 +00:00
|
|
|
format!("expected identifier, found keyword `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice());
|
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
|
2013-12-30 22:04:00 +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();
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(format!("`{}` is a reserved keyword",
|
|
|
|
token_str).as_slice())
|
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.
|
2014-04-17 08:35:31 +00:00
|
|
|
fn expect_and(&mut self) {
|
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::And) => self.bump(),
|
|
|
|
token::AndAnd => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
let lo = span.lo + BytePos(1);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(token::BinOp(token::And), lo, span.hi)
|
2014-04-17 08:35:31 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-04-17 08:35:31 +00:00
|
|
|
let found_token =
|
2014-10-27 08:22:52 +00:00
|
|
|
Parser::token_to_string(&token::BinOp(token::And));
|
2014-04-17 08:35:31 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
found_token,
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2014-04-17 08:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Expect and consume a `|`. If `||` is seen, replace it with a single
|
|
|
|
/// `|` and continue. If a `|` is not seen, signal an error.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn expect_or(&mut self) {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Or) => self.bump(),
|
|
|
|
token::OrOr => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
let lo = span.lo + BytePos(1);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(token::BinOp(token::Or), lo, span.hi)
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let found_token = self.this_token_to_string();
|
2014-05-11 04:27:44 +00:00
|
|
|
let token_str =
|
2014-10-27 08:22:52 +00:00
|
|
|
Parser::token_to_string(&token::BinOp(token::Or));
|
2013-10-29 22:06:13 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str,
|
|
|
|
found_token).as_slice())
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 04:48:38 +00:00
|
|
|
pub fn expect_no_suffix(&mut self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
|
|
|
|
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
|
|
|
}
|
2014-11-19 09:22:54 +00:00
|
|
|
self.span_err(sp, &*format!("{} with a suffix is illegal", 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
|
|
|
|
/// starting token. The `force` parameter is used to forcefully break up a
|
|
|
|
/// `<<` token. If `force` is false, then `<<` is only broken when a lifetime
|
|
|
|
/// shows up next. For example, consider the expression:
|
|
|
|
///
|
|
|
|
/// foo as bar << test
|
|
|
|
///
|
|
|
|
/// The parser needs to know if `bar <<` is the start of a generic path or if
|
|
|
|
/// it's a left-shift token. If `test` were a lifetime, then it's impossible
|
|
|
|
/// for the token to be a left-shift, but if it's not a lifetime, then it's
|
|
|
|
/// considered a left-shift.
|
|
|
|
///
|
|
|
|
/// The reason for this is that the only current ambiguity with `<<` is when
|
|
|
|
/// parsing closure types:
|
|
|
|
///
|
|
|
|
/// foo::<<'a> ||>();
|
|
|
|
/// impl Foo<<'a> ||>() { ... }
|
2014-05-11 04:27:44 +00:00
|
|
|
fn eat_lt(&mut self, force: bool) -> bool {
|
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lt => { self.bump(); true }
|
|
|
|
token::BinOp(token::Shl) => {
|
2014-05-11 04:27:44 +00:00
|
|
|
let next_lifetime = self.look_ahead(1, |t| match *t {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(..) => true,
|
2014-05-11 04:27:44 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
if force || next_lifetime {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
let lo = span.lo + BytePos(1);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(token::Lt, lo, span.hi);
|
2014-05-11 04:27:44 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expect_lt(&mut self) {
|
|
|
|
if !self.eat_lt(true) {
|
2014-06-21 10:39:03 +00:00
|
|
|
let found_token = self.this_token_to_string();
|
2014-10-27 08:22:52 +00:00
|
|
|
let token_str = Parser::token_to_string(&token::Lt);
|
2014-05-11 04:27:44 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str,
|
|
|
|
found_token).as_slice())
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
|
2014-12-08 18:28:32 +00:00
|
|
|
fn parse_seq_to_before_or<T, F>(&mut self,
|
|
|
|
sep: &token::Token,
|
|
|
|
mut f: F)
|
|
|
|
-> Vec<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-10-29 22:06:13 +00:00
|
|
|
let mut first = true;
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut vector = Vec::new();
|
2014-10-27 08:22:52 +00:00
|
|
|
while self.token != token::BinOp(token::Or) &&
|
|
|
|
self.token != token::OrOr {
|
2013-10-29 22:06:13 +00:00
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
self.expect(sep)
|
|
|
|
}
|
|
|
|
|
|
|
|
vector.push(f(self))
|
|
|
|
}
|
|
|
|
vector
|
|
|
|
}
|
|
|
|
|
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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn expect_gt(&mut self) {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Gt => self.bump(),
|
|
|
|
token::BinOp(token::Shr) => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
let lo = span.lo + BytePos(1);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(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);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(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);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.replace_token(token::Eq, lo, span.hi)
|
2014-06-20 16:53:12 +00:00
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
_ => {
|
2014-10-27 08:22:52 +00:00
|
|
|
let gt_str = Parser::token_to_string(&token::Gt);
|
2014-06-21 10:39:03 +00:00
|
|
|
let this_token_str = self.this_token_to_string();
|
2013-12-30 22:04:00 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
gt_str,
|
2014-05-16 17:45:16 +00:00
|
|
|
this_token_str).as_slice())
|
2013-12-30 22:04:00 +00:00
|
|
|
}
|
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)
|
|
|
|
-> (OwnedSlice<T>, bool) where
|
|
|
|
F: FnMut(&mut Parser) -> Option<T>,
|
|
|
|
{
|
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.
|
|
|
|
for i in iter::count(0u, 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
|
|
|
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 {
|
2014-11-29 04:08:30 +00:00
|
|
|
match f(self) {
|
|
|
|
Some(result) => v.push(result),
|
|
|
|
None => return (OwnedSlice::from_vec(v), true)
|
|
|
|
}
|
2014-08-21 04:37:15 +00:00
|
|
|
} else {
|
|
|
|
sep.as_ref().map(|t| self.expect(t));
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 04:08:30 +00:00
|
|
|
return (OwnedSlice::from_vec(v), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
-> OwnedSlice<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2014-11-29 04:08:30 +00:00
|
|
|
let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, |p| Some(f(p)));
|
|
|
|
assert!(!returned);
|
|
|
|
return 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)
|
|
|
|
-> OwnedSlice<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
let v = self.parse_seq_to_before_gt(sep, f);
|
|
|
|
self.expect_gt();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
-> (OwnedSlice<T>, bool) where
|
|
|
|
F: FnMut(&mut Parser) -> Option<T>,
|
|
|
|
{
|
2014-11-29 04:08:30 +00:00
|
|
|
let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f);
|
|
|
|
if !returned {
|
|
|
|
self.expect_gt();
|
|
|
|
}
|
|
|
|
return (v, returned);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
-> Vec<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
let val = self.parse_seq_to_before_end(ket, sep, f);
|
|
|
|
self.bump();
|
|
|
|
val
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
mut f: F)
|
|
|
|
-> Vec<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
let mut first: bool = true;
|
2014-03-19 12:16:56 +00:00
|
|
|
let mut v = vec!();
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != *ket {
|
2013-06-15 01:21:47 +00:00
|
|
|
match sep.sep {
|
|
|
|
Some(ref t) => {
|
|
|
|
if first { first = false; }
|
|
|
|
else { self.expect(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
|
|
|
if sep.trailing_sep_allowed && self.check(ket) { break; }
|
2013-06-15 01:21:47 +00:00
|
|
|
v.push(f(self));
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
-> Vec<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
self.expect(bra);
|
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
|
|
|
self.bump();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a sequence parameter of enum variant. For consistency purposes,
|
|
|
|
/// these should not be empty.
|
2014-12-08 18:28:32 +00:00
|
|
|
pub fn parse_enum_variant_seq<T, F>(&mut self,
|
|
|
|
bra: &token::Token,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
|
|
|
f: F)
|
|
|
|
-> Vec<T> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2014-03-16 15:07:39 +00:00
|
|
|
let result = self.parse_unspanned_seq(bra, ket, sep, f);
|
|
|
|
if result.is_empty() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
2014-03-16 15:07:39 +00:00
|
|
|
"nullary enum variants are written with no trailing `( )`");
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
-> Spanned<Vec<T>> where
|
|
|
|
F: FnMut(&mut Parser) -> T,
|
|
|
|
{
|
2013-06-15 01:21:47 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
self.expect(bra);
|
|
|
|
let result = self.parse_seq_to_before_end(ket, sep, f);
|
|
|
|
let hi = self.span.hi;
|
|
|
|
self.bump();
|
|
|
|
spanned(lo, hi, result)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Advance the parser by one token
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn bump(&mut self) {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.last_span = self.span;
|
2013-08-05 20:18:29 +00:00
|
|
|
// Stash token for error recovery (sometimes; clone is not necessarily cheap).
|
2014-10-27 12:33:30 +00:00
|
|
|
self.last_token = if self.token.is_ident() || self.token.is_path() {
|
2014-04-25 08:08:02 +00:00
|
|
|
Some(box self.token.clone())
|
2013-08-05 20:18:29 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2013-12-30 23:32:56 +00:00
|
|
|
let next = if self.buffer_start == self.buffer_end {
|
2014-11-03 04:52:00 +00:00
|
|
|
self.reader.real_token()
|
2012-01-13 08:56:53 +00:00
|
|
|
} else {
|
2014-01-31 20:35:36 +00:00
|
|
|
// Avoid token copies with `replace`.
|
2013-12-30 23:32:56 +00:00
|
|
|
let buffer_start = self.buffer_start as uint;
|
2013-07-02 19:47:32 +00:00
|
|
|
let next_index = (buffer_start + 1) & 3 as uint;
|
2013-12-30 23:32:56 +00:00
|
|
|
self.buffer_start = next_index as int;
|
2013-07-02 19:47:32 +00:00
|
|
|
|
|
|
|
let placeholder = TokenAndSpan {
|
2014-10-27 08:22:52 +00:00
|
|
|
tok: token::Underscore,
|
2013-12-30 23:17:53 +00:00
|
|
|
sp: self.span,
|
2013-07-02 19:47:32 +00:00
|
|
|
};
|
2014-11-23 11:14:35 +00:00
|
|
|
mem::replace(&mut self.buffer[buffer_start], placeholder)
|
2012-06-15 16:32:17 +00:00
|
|
|
};
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span = next.sp;
|
2013-12-30 23:09:41 +00:00
|
|
|
self.token = next.tok;
|
2013-12-30 23:33:39 +00:00
|
|
|
self.tokens_consumed += 1u;
|
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();
|
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.
|
2013-12-30 22:04:00 +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);
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
old_token
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// EFFECT: replace the current token and span with the given one
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn replace_token(&mut self,
|
2013-05-31 22:17:22 +00:00
|
|
|
next: token::Token,
|
|
|
|
lo: BytePos,
|
|
|
|
hi: BytePos) {
|
2014-03-12 19:20:18 +00:00
|
|
|
self.last_span = mk_sp(self.span.lo, lo);
|
2013-12-30 23:09:41 +00:00
|
|
|
self.token = next;
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span = mk_sp(lo, hi);
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn buffer_length(&mut self) -> int {
|
2013-12-30 23:32:56 +00:00
|
|
|
if self.buffer_start <= self.buffer_end {
|
|
|
|
return self.buffer_end - self.buffer_start;
|
2012-06-09 00:17:31 +00:00
|
|
|
}
|
2013-12-30 23:32:56 +00:00
|
|
|
return (4 - self.buffer_start) + self.buffer_end;
|
2012-06-09 00:17:31 +00:00
|
|
|
}
|
2014-12-08 18:28:32 +00:00
|
|
|
pub fn look_ahead<R, F>(&mut self, distance: uint, f: F) -> R where
|
|
|
|
F: FnOnce(&token::Token) -> R,
|
|
|
|
{
|
2012-06-09 00:17:31 +00:00
|
|
|
let dist = distance as int;
|
|
|
|
while self.buffer_length() < dist {
|
2014-11-03 04:52:00 +00:00
|
|
|
self.buffer[self.buffer_end as uint] = self.reader.real_token();
|
2013-12-30 23:32:56 +00:00
|
|
|
self.buffer_end = (self.buffer_end + 1) & 3;
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2014-04-02 03:39:26 +00:00
|
|
|
f(&self.buffer[((self.buffer_start + dist - 1) & 3) as uint].tok)
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn fatal(&mut self, m: &str) -> ! {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.sess.span_diagnostic.span_fatal(self.span, m)
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn span_fatal(&mut self, sp: Span, m: &str) -> ! {
|
2012-01-25 05:42:54 +00:00
|
|
|
self.sess.span_diagnostic.span_fatal(sp, m)
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2014-11-10 10:58:03 +00:00
|
|
|
pub fn span_fatal_help(&mut self, sp: Span, m: &str, help: &str) -> ! {
|
|
|
|
self.span_err(sp, m);
|
|
|
|
self.span_help(sp, help);
|
|
|
|
panic!(diagnostic::FatalError);
|
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn span_note(&mut self, sp: Span, m: &str) {
|
2012-08-07 00:44:07 +00:00
|
|
|
self.sess.span_diagnostic.span_note(sp, m)
|
|
|
|
}
|
2014-08-29 06:55:35 +00:00
|
|
|
pub fn span_help(&mut self, sp: Span, m: &str) {
|
|
|
|
self.sess.span_diagnostic.span_help(sp, m)
|
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn bug(&mut 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
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn warn(&mut 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
|
|
|
}
|
2014-03-22 01:05:05 +00:00
|
|
|
pub fn span_warn(&mut self, sp: Span, m: &str) {
|
|
|
|
self.sess.span_diagnostic.span_warn(sp, m)
|
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn span_err(&mut self, sp: Span, m: &str) {
|
2012-09-08 22:50:29 +00:00
|
|
|
self.sess.span_diagnostic.span_err(sp, m)
|
|
|
|
}
|
2014-11-19 04:48:38 +00:00
|
|
|
pub fn span_bug(&mut self, sp: Span, m: &str) -> ! {
|
|
|
|
self.sess.span_diagnostic.span_bug(sp, m)
|
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn abort_if_errors(&mut self) {
|
2012-09-08 22:50:29 +00:00
|
|
|
self.sess.span_diagnostic.handler().abort_if_errors();
|
|
|
|
}
|
2011-03-18 00:39:47 +00:00
|
|
|
|
2014-01-08 18:35:15 +00:00
|
|
|
pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString {
|
2014-02-14 05:07:09 +00:00
|
|
|
token::get_ident(id)
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-11-07 11:53:45 +00:00
|
|
|
self.token.is_keyword(keywords::Fn) ||
|
|
|
|
self.token.is_keyword(keywords::Unsafe) ||
|
|
|
|
self.token.is_keyword(keywords::Extern)
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Is the current token one of the keywords that signals a closure type?
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn token_is_closure_keyword(&mut self) -> bool {
|
2014-11-07 11:53:45 +00:00
|
|
|
self.token.is_keyword(keywords::Unsafe)
|
2013-04-26 23:19:26 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
pub fn parse_for_in_type(&mut self) -> Ty_ {
|
|
|
|
/*
|
|
|
|
Parses whatever can come after a `for` keyword in a type.
|
|
|
|
The `for` has already been consumed.
|
|
|
|
|
|
|
|
Deprecated:
|
|
|
|
|
|
|
|
- for <'lt> |S| -> T
|
|
|
|
|
|
|
|
Eventually:
|
|
|
|
|
|
|
|
- for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
|
|
|
|
- for <'lt> path::foo(a, b)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// parse <'lt>
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs();
|
|
|
|
|
|
|
|
// examine next token to decide to do
|
|
|
|
if self.eat_keyword(keywords::Proc) {
|
|
|
|
self.parse_proc_type(lifetime_defs)
|
|
|
|
} else if self.token_is_bare_fn_keyword() || self.token_is_closure_keyword() {
|
|
|
|
self.parse_ty_bare_fn_or_ty_closure(lifetime_defs)
|
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::ModSep) ||
|
2014-11-07 11:53:45 +00:00
|
|
|
self.token.is_ident() ||
|
2014-11-15 21:55:27 +00:00
|
|
|
self.token.is_path()
|
|
|
|
{
|
2014-11-07 11:53:45 +00:00
|
|
|
let trait_ref = self.parse_trait_ref();
|
2014-11-15 21:55:27 +00:00
|
|
|
let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
|
|
|
|
trait_ref: trait_ref };
|
|
|
|
let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
|
|
|
|
self.parse_ty_param_bounds()
|
|
|
|
} else {
|
|
|
|
OwnedSlice::empty()
|
|
|
|
};
|
|
|
|
let all_bounds =
|
|
|
|
Some(TraitTyParamBound(poly_trait_ref)).into_iter()
|
|
|
|
.chain(other_bounds.into_vec().into_iter())
|
|
|
|
.collect();
|
|
|
|
ast::TyPolyTraitRef(all_bounds)
|
2014-11-07 11:53:45 +00:00
|
|
|
} else {
|
|
|
|
self.parse_ty_closure(lifetime_defs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 20:05:29 +00:00
|
|
|
pub fn parse_ty_path(&mut self) -> Ty_ {
|
|
|
|
let path = self.parse_path(LifetimeAndTypesWithoutColons);
|
|
|
|
TyPath(path, ast::DUMMY_NODE_ID)
|
2014-11-07 11:53:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// parse a TyBareFn type:
|
2014-11-07 11:53:45 +00:00
|
|
|
pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
|
2012-11-05 04:41:00 +00:00
|
|
|
/*
|
|
|
|
|
2014-05-07 01:43:56 +00:00
|
|
|
[unsafe] [extern "ABI"] fn <'lt> (S) -> T
|
|
|
|
^~~~^ ^~~~^ ^~~~^ ^~^ ^
|
|
|
|
| | | | |
|
|
|
|
| | | | Return type
|
|
|
|
| | | Argument types
|
|
|
|
| | Lifetimes
|
|
|
|
| ABI
|
|
|
|
Function Style
|
2013-02-01 01:12:29 +00:00
|
|
|
*/
|
|
|
|
|
2014-12-09 15:36:46 +00:00
|
|
|
let unsafety = self.parse_unsafety();
|
2014-04-02 08:19:41 +00:00
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
|
|
|
self.parse_opt_abi().unwrap_or(abi::C)
|
2014-02-14 04:23:01 +00:00
|
|
|
} else {
|
2014-04-02 08:19:41 +00:00
|
|
|
abi::Rust
|
2014-02-14 04:23:01 +00:00
|
|
|
};
|
2014-02-02 22:52:06 +00:00
|
|
|
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2014-11-07 11:53:45 +00:00
|
|
|
let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
|
|
|
|
let (inputs, variadic) = self.parse_fn_args(false, true);
|
2014-11-09 15:14:15 +00:00
|
|
|
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
|
|
|
|
});
|
2014-09-13 16:06:01 +00:00
|
|
|
TyBareFn(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
|
2014-09-13 16:06:01 +00:00
|
|
|
}))
|
2013-02-01 01:12:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses a procedure type (`proc`). The initial `proc` keyword must
|
|
|
|
/// already have been parsed.
|
2014-11-07 11:53:45 +00:00
|
|
|
pub fn parse_proc_type(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
|
2014-04-02 16:47:11 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
proc <'lt> (S) [:Bounds] -> T
|
|
|
|
^~~^ ^~~~^ ^ ^~~~~~~~^ ^
|
|
|
|
| | | | |
|
|
|
|
| | | | Return type
|
|
|
|
| | | Bounds
|
|
|
|
| | Argument types
|
2014-11-07 11:53:45 +00:00
|
|
|
| Legacy lifetimes
|
2014-11-26 15:07:22 +00:00
|
|
|
the `proc` keyword (already consumed)
|
2014-04-02 16:47:11 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-11-26 15:07:22 +00:00
|
|
|
let proc_span = self.last_span;
|
|
|
|
|
|
|
|
// To be helpful, parse the proc as ever
|
|
|
|
let _ = self.parse_legacy_lifetime_defs(lifetime_defs);
|
|
|
|
let _ = self.parse_fn_args(false, false);
|
|
|
|
let _ = self.parse_colon_then_ty_param_bounds();
|
|
|
|
let _ = self.parse_ret_ty();
|
|
|
|
|
|
|
|
self.obsolete(proc_span, ObsoleteProcType);
|
|
|
|
|
|
|
|
TyInfer
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 05:08:39 +00:00
|
|
|
/// Parses an optional unboxed closure kind (`&:`, `&mut:`, or `:`).
|
|
|
|
pub fn parse_optional_unboxed_closure_kind(&mut self)
|
|
|
|
-> Option<UnboxedClosureKind> {
|
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::BinOp(token::And)) &&
|
2014-10-27 12:33:30 +00:00
|
|
|
self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
|
|
|
|
self.look_ahead(2, |t| *t == token::Colon) {
|
2014-07-30 05:08:39 +00:00
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
return Some(FnMutUnboxedClosureKind)
|
|
|
|
}
|
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.token == token::BinOp(token::And) &&
|
|
|
|
self.look_ahead(1, |t| *t == token::Colon) {
|
2014-07-30 05:08:39 +00:00
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
return Some(FnUnboxedClosureKind)
|
|
|
|
}
|
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2014-07-30 05:08:39 +00:00
|
|
|
return Some(FnOnceUnboxedClosureKind)
|
|
|
|
}
|
|
|
|
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
pub fn parse_ty_bare_fn_or_ty_closure(&mut self, lifetime_defs: Vec<LifetimeDef>) -> Ty_ {
|
|
|
|
// Both bare fns and closures can begin with stuff like unsafe
|
|
|
|
// and extern. So we just scan ahead a few tokens to see if we see
|
|
|
|
// a `fn`.
|
|
|
|
//
|
|
|
|
// Closure: [unsafe] <'lt> |S| [:Bounds] -> T
|
|
|
|
// Fn: [unsafe] [extern "ABI"] fn <'lt> (S) -> T
|
|
|
|
|
|
|
|
if self.token.is_keyword(keywords::Fn) {
|
|
|
|
self.parse_ty_bare_fn(lifetime_defs)
|
|
|
|
} else if self.token.is_keyword(keywords::Extern) {
|
|
|
|
self.parse_ty_bare_fn(lifetime_defs)
|
|
|
|
} else if self.token.is_keyword(keywords::Unsafe) {
|
|
|
|
if self.look_ahead(1, |t| t.is_keyword(keywords::Fn) ||
|
|
|
|
t.is_keyword(keywords::Extern)) {
|
|
|
|
self.parse_ty_bare_fn(lifetime_defs)
|
|
|
|
} else {
|
|
|
|
self.parse_ty_closure(lifetime_defs)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.parse_ty_closure(lifetime_defs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a TyClosure type
|
2014-11-07 11:53:45 +00:00
|
|
|
pub fn parse_ty_closure(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
|
2013-02-01 01:12:29 +00:00
|
|
|
/*
|
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
[unsafe] <'lt> |S| [:Bounds] -> T
|
|
|
|
^~~~~~~^ ^~~~^ ^ ^~~~~~~~^ ^
|
|
|
|
| | | | |
|
|
|
|
| | | | Return type
|
|
|
|
| | | Closure bounds
|
|
|
|
| | Argument types
|
|
|
|
| Deprecated lifetime defs
|
|
|
|
|
|
2014-04-07 01:04:40 +00:00
|
|
|
Function Style
|
2012-11-05 04:41:00 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-12-09 15:36:46 +00:00
|
|
|
let unsafety = self.parse_unsafety();
|
2013-03-02 00:59:46 +00:00
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2014-11-04 21:25:15 +00:00
|
|
|
let inputs = if self.eat(&token::OrOr) {
|
|
|
|
Vec::new()
|
2014-04-02 16:47:11 +00:00
|
|
|
} else {
|
|
|
|
self.expect_or();
|
2014-06-02 01:41:46 +00:00
|
|
|
|
2014-04-02 16:47:11 +00:00
|
|
|
let inputs = self.parse_seq_to_before_or(
|
2014-10-27 08:22:52 +00:00
|
|
|
&token::Comma,
|
2014-04-02 16:47:11 +00:00
|
|
|
|p| p.parse_arg_general(false));
|
|
|
|
self.expect_or();
|
2014-11-04 21:25:15 +00:00
|
|
|
inputs
|
2014-04-02 16:47:11 +00:00
|
|
|
};
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
let bounds = self.parse_colon_then_ty_param_bounds();
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2014-11-09 15:14:15 +00:00
|
|
|
let output = self.parse_ret_ty();
|
2014-04-02 16:47:11 +00:00
|
|
|
let decl = P(FnDecl {
|
|
|
|
inputs: inputs,
|
|
|
|
output: output,
|
|
|
|
variadic: false
|
|
|
|
});
|
2013-03-25 20:21:04 +00:00
|
|
|
|
2014-11-04 21:25:15 +00:00
|
|
|
TyClosure(P(ClosureTy {
|
2014-12-09 15:36:46 +00:00
|
|
|
unsafety: unsafety,
|
2014-11-07 11:53:45 +00:00
|
|
|
onceness: Many,
|
2014-11-04 21:25:15 +00:00
|
|
|
bounds: bounds,
|
|
|
|
decl: decl,
|
|
|
|
lifetimes: lifetime_defs,
|
|
|
|
}))
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 15:36:46 +00:00
|
|
|
pub fn parse_unsafety(&mut self) -> Unsafety {
|
2013-10-03 09:53:46 +00:00
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
2014-12-09 15:36:46 +00:00
|
|
|
return Unsafety::Unsafe;
|
2013-02-01 01:12:29 +00:00
|
|
|
} else {
|
2014-12-09 15:36:46 +00:00
|
|
|
return Unsafety::Normal;
|
2013-02-01 01:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
/// Parses `[ 'for' '<' lifetime_defs '>' ]'
|
|
|
|
fn parse_legacy_lifetime_defs(&mut self,
|
|
|
|
lifetime_defs: Vec<ast::LifetimeDef>)
|
|
|
|
-> Vec<ast::LifetimeDef>
|
|
|
|
{
|
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.token == token::Lt {
|
|
|
|
self.bump();
|
2014-11-07 11:53:45 +00:00
|
|
|
if lifetime_defs.is_empty() {
|
2014-11-17 04:01:37 +00:00
|
|
|
self.warn("deprecated syntax; use the `for` keyword now \
|
|
|
|
(e.g. change `fn<'a>` to `for<'a> fn`)");
|
2014-11-07 11:53:45 +00:00
|
|
|
let lifetime_defs = self.parse_lifetime_defs();
|
|
|
|
self.expect_gt();
|
|
|
|
lifetime_defs
|
|
|
|
} else {
|
|
|
|
self.fatal("cannot use new `for` keyword and older syntax together");
|
|
|
|
}
|
2013-03-25 20:21:04 +00:00
|
|
|
} else {
|
2014-11-07 11:53:45 +00:00
|
|
|
lifetime_defs
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
/// Parses `type Foo;` in a trait declaration only. The `type` keyword has
|
|
|
|
/// already been parsed.
|
|
|
|
fn parse_associated_type(&mut self, attrs: Vec<Attribute>)
|
2014-10-28 18:48:52 +00:00
|
|
|
-> AssociatedType
|
|
|
|
{
|
|
|
|
let ty_param = self.parse_ty_param();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-08-06 02:44:21 +00:00
|
|
|
AssociatedType {
|
|
|
|
attrs: attrs,
|
2014-10-28 18:48:52 +00:00
|
|
|
ty_param: ty_param,
|
2014-08-06 02:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses `type Foo = TYPE;` in an implementation declaration only. The
|
|
|
|
/// `type` keyword has already been parsed.
|
|
|
|
fn parse_typedef(&mut self, attrs: Vec<Attribute>, vis: Visibility)
|
|
|
|
-> Typedef {
|
|
|
|
let lo = self.span.lo;
|
|
|
|
let ident = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Eq);
|
2014-11-20 20:05:29 +00:00
|
|
|
let typ = self.parse_ty_sum();
|
2014-08-06 02:44:21 +00:00
|
|
|
let hi = self.span.hi;
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-08-06 02:44:21 +00:00
|
|
|
Typedef {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
ident: ident,
|
|
|
|
vis: vis,
|
|
|
|
attrs: attrs,
|
|
|
|
typ: typ,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse the items in a trait declaration
|
|
|
|
pub fn parse_trait_items(&mut self) -> Vec<TraitItem> {
|
2013-11-21 00:23:04 +00:00
|
|
|
self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Brace),
|
|
|
|
&token::CloseDelim(token::Brace),
|
2013-11-21 00:23:04 +00:00
|
|
|
seq_sep_none(),
|
|
|
|
|p| {
|
2012-05-24 20:44:42 +00:00
|
|
|
let attrs = p.parse_outer_attributes();
|
2014-08-06 02:44:21 +00:00
|
|
|
|
|
|
|
if p.eat_keyword(keywords::Type) {
|
|
|
|
TypeTraitItem(P(p.parse_associated_type(attrs)))
|
2014-05-29 05:26:56 +00:00
|
|
|
} else {
|
2014-08-06 02:44:21 +00:00
|
|
|
let lo = p.span.lo;
|
2012-08-17 22:25:35 +00:00
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
let vis = p.parse_visibility();
|
2014-12-09 15:36:46 +00:00
|
|
|
let style = p.parse_unsafety();
|
2014-08-06 02:44:21 +00:00
|
|
|
let abi = if p.eat_keyword(keywords::Extern) {
|
|
|
|
p.parse_opt_abi().unwrap_or(abi::C)
|
|
|
|
} else {
|
|
|
|
abi::Rust
|
|
|
|
};
|
2014-11-30 08:33:04 +00:00
|
|
|
p.expect_keyword(keywords::Fn);
|
2012-08-17 22:25:35 +00:00
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
let ident = p.parse_ident();
|
|
|
|
let mut generics = p.parse_generics();
|
2013-03-13 02:32:14 +00:00
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
let (explicit_self, d) = p.parse_fn_decl_with_self(|p| {
|
|
|
|
// 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-11 16:32:26 +00:00
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
p.parse_where_clause(&mut generics);
|
|
|
|
|
|
|
|
let hi = p.last_span.hi;
|
|
|
|
match p.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Semi => {
|
2014-08-06 02:44:21 +00:00
|
|
|
p.bump();
|
|
|
|
debug!("parse_trait_methods(): parsing required method");
|
|
|
|
RequiredMethod(TypeMethod {
|
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
2014-12-09 15:36:46 +00:00
|
|
|
unsafety: style,
|
2014-08-06 02:44:21 +00:00
|
|
|
decl: d,
|
|
|
|
generics: generics,
|
|
|
|
abi: abi,
|
|
|
|
explicit_self: explicit_self,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
vis: vis,
|
|
|
|
})
|
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Brace) => {
|
2014-08-06 02:44:21 +00:00
|
|
|
debug!("parse_trait_methods(): parsing provided method");
|
|
|
|
let (inner_attrs, body) =
|
|
|
|
p.parse_inner_attrs_and_block();
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut attrs = attrs;
|
|
|
|
attrs.push_all(inner_attrs.as_slice());
|
2014-08-06 02:44:21 +00:00
|
|
|
ProvidedMethod(P(ast::Method {
|
|
|
|
attrs: attrs,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
node: ast::MethDecl(ident,
|
|
|
|
generics,
|
|
|
|
abi,
|
|
|
|
explicit_self,
|
|
|
|
style,
|
|
|
|
d,
|
|
|
|
body,
|
|
|
|
vis)
|
|
|
|
}))
|
|
|
|
}
|
2012-07-10 20:44:20 +00:00
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
_ => {
|
|
|
|
let token_str = p.this_token_to_string();
|
|
|
|
p.fatal((format!("expected `;` or `{{`, found `{}`",
|
|
|
|
token_str)).as_slice())
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 20:44:20 +00:00
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a possibly mutable type
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_mt(&mut self) -> MutTy {
|
2012-05-23 22:06:11 +00:00
|
|
|
let mutbl = self.parse_mutability();
|
2014-11-20 20:05:29 +00:00
|
|
|
let t = self.parse_ty();
|
2014-01-09 13:05:33 +00:00
|
|
|
MutTy { ty: t, mutbl: mutbl }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse [mut/const/imm] ID : TY
|
|
|
|
/// now used only by obsolete record syntax parser...
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_ty_field(&mut self) -> TypeField {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let mutbl = self.parse_mutability();
|
2012-05-24 19:38:45 +00:00
|
|
|
let id = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2013-07-27 08:25:59 +00:00
|
|
|
let hi = ty.span.hi;
|
|
|
|
ast::TypeField {
|
|
|
|
ident: id,
|
2014-01-09 13:05:33 +00:00
|
|
|
mt: MutTy { ty: ty, mutbl: mutbl },
|
2013-07-27 08:25:59 +00:00
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse optional return type [ -> TY ] in function decl
|
2014-11-09 15:14:15 +00:00
|
|
|
pub fn parse_ret_ty(&mut self) -> FunctionRetTy {
|
|
|
|
if self.eat(&token::RArrow) {
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Not) {
|
2014-11-09 15:14:15 +00:00
|
|
|
NoReturn(self.span)
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2014-11-20 20:05:29 +00:00
|
|
|
let t = self.parse_ty();
|
|
|
|
|
|
|
|
// We used to allow `fn foo() -> &T + U`, but don't
|
|
|
|
// anymore. If we see it, report a useful error. This
|
|
|
|
// only makes sense because `parse_ret_ty` is only
|
|
|
|
// used in fn *declarations*, not fn types or where
|
|
|
|
// clauses (i.e., not when parsing something like
|
|
|
|
// `FnMut() -> T + Send`, where the `+` is legal).
|
|
|
|
if self.token == token::BinOp(token::Plus) {
|
|
|
|
self.warn("deprecated syntax: `()` are required, see RFC 248 for details");
|
|
|
|
}
|
|
|
|
|
|
|
|
Return(t)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-03-12 23:26:31 +00:00
|
|
|
} else {
|
2012-05-23 22:06:11 +00:00
|
|
|
let pos = self.span.lo;
|
2014-11-09 15:14:15 +00:00
|
|
|
Return(P(Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: TyTup(vec![]),
|
|
|
|
span: 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
|
|
|
|
2014-11-20 20:05:29 +00:00
|
|
|
/// Parse a type in a context where `T1+T2` is allowed.
|
|
|
|
pub fn parse_ty_sum(&mut self) -> P<Ty> {
|
|
|
|
let lo = self.span.lo;
|
|
|
|
let lhs = self.parse_ty();
|
|
|
|
|
|
|
|
if !self.eat(&token::BinOp(token::Plus)) {
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bounds = self.parse_ty_param_bounds();
|
|
|
|
|
|
|
|
// In type grammar, `+` is treated like a binary operator,
|
|
|
|
// and hence both L and R side are required.
|
|
|
|
if bounds.len() == 0 {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
|
|
|
"at least one type parameter bound \
|
|
|
|
must be specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
let sp = mk_sp(lo, self.last_span.hi);
|
|
|
|
let sum = ast::TyObjectSum(lhs, bounds);
|
|
|
|
P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})
|
|
|
|
}
|
|
|
|
|
2014-06-11 19:14:38 +00:00
|
|
|
/// Parse a type.
|
|
|
|
///
|
|
|
|
/// The second parameter specifies whether the `+` binary operator is
|
|
|
|
/// allowed in the type grammar.
|
2014-11-20 20:05:29 +00:00
|
|
|
pub fn parse_ty(&mut self) -> P<Ty> {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(no_clone self, NtTy);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
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 t = if self.check(&token::OpenDelim(token::Paren)) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-07-02 19:47:32 +00:00
|
|
|
|
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) {
|
2014-11-20 20:05:29 +00:00
|
|
|
ts.push(self.parse_ty_sum());
|
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-11-09 15:14:15 +00:00
|
|
|
last_comma = true;
|
|
|
|
self.bump();
|
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
|
|
|
|
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
|
|
|
if ts.len() == 1 && !last_comma {
|
|
|
|
TyParen(ts.into_iter().nth(0).unwrap())
|
|
|
|
} else {
|
|
|
|
TyTup(ts)
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
} else if self.token == token::Tilde {
|
2013-03-08 18:19:19 +00:00
|
|
|
// OWNED POINTER
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-08-28 03:30:41 +00:00
|
|
|
let last_span = self.last_span;
|
2014-05-06 01:56:44 +00:00
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Bracket) => self.obsolete(last_span, ObsoleteOwnedVector),
|
2014-08-28 03:30:41 +00:00
|
|
|
_ => self.obsolete(last_span, ObsoleteOwnedType)
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
}
|
2014-11-20 20:05:29 +00:00
|
|
|
TyTup(vec![self.parse_ty()])
|
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::Star)) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// STAR POINTER (bare pointer?)
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-06-16 23:58:17 +00:00
|
|
|
TyPtr(self.parse_ptr())
|
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::Bracket)) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// VECTOR
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Bracket));
|
2014-11-20 20:05:29 +00:00
|
|
|
let t = self.parse_ty_sum();
|
2012-08-14 02:59:32 +00:00
|
|
|
|
2013-03-19 22:40:04 +00:00
|
|
|
// Parse the `, ..e` in `[ int, ..e ]`
|
2013-03-05 02:03:21 +00:00
|
|
|
// where `e` is a const expression
|
2013-03-19 22:40:04 +00:00
|
|
|
let t = match self.maybe_parse_fixed_vstore() {
|
2014-01-09 13:05:33 +00:00
|
|
|
None => TyVec(t),
|
|
|
|
Some(suffix) => TyFixedLengthVec(t, suffix)
|
2012-11-05 04:41:00 +00:00
|
|
|
};
|
2014-10-29 10:37:54 +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)) ||
|
2014-11-07 11:53:45 +00:00
|
|
|
self.token == token::AndAnd {
|
2013-03-08 18:19:19 +00:00
|
|
|
// BORROWED POINTER
|
2014-04-17 08:35:31 +00:00
|
|
|
self.expect_and();
|
2012-11-05 04:41:00 +00:00
|
|
|
self.parse_borrowed_pointee()
|
2014-11-07 11:53:45 +00:00
|
|
|
} else if self.token.is_keyword(keywords::For) {
|
|
|
|
self.parse_for_in_type()
|
|
|
|
} else if self.token_is_bare_fn_keyword() ||
|
|
|
|
self.token_is_closure_keyword() {
|
|
|
|
// BARE FUNCTION OR CLOSURE
|
|
|
|
self.parse_ty_bare_fn_or_ty_closure(Vec::new())
|
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::Or)) ||
|
2014-11-07 11:53:45 +00:00
|
|
|
self.token == token::OrOr ||
|
|
|
|
(self.token == token::Lt &&
|
|
|
|
self.look_ahead(1, |t| {
|
|
|
|
*t == token::Gt || t.is_lifetime()
|
|
|
|
})) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// CLOSURE
|
2014-11-07 11:53:45 +00:00
|
|
|
self.parse_ty_closure(Vec::new())
|
2013-08-22 21:00:02 +00:00
|
|
|
} else if self.eat_keyword(keywords::Typeof) {
|
|
|
|
// TYPEOF
|
|
|
|
// In order to not be ambiguous, the type must be surrounded by parens.
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Paren));
|
2013-08-22 21:00:02 +00:00
|
|
|
let e = self.parse_expr();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
2014-01-09 13:05:33 +00:00
|
|
|
TyTypeof(e)
|
2013-10-28 22:22:49 +00:00
|
|
|
} else if self.eat_keyword(keywords::Proc) {
|
2014-11-07 11:53:45 +00:00
|
|
|
self.parse_proc_type(Vec::new())
|
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::Lt) {
|
2014-11-08 11:59:10 +00:00
|
|
|
// QUALIFIED PATH `<TYPE as TRAIT_REF>::item`
|
2014-08-06 02:44:21 +00:00
|
|
|
self.bump();
|
2014-11-20 20:05:29 +00:00
|
|
|
let self_type = self.parse_ty_sum();
|
2014-08-06 02:44:21 +00:00
|
|
|
self.expect_keyword(keywords::As);
|
2014-11-08 11:59:10 +00:00
|
|
|
let trait_ref = self.parse_trait_ref();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Gt);
|
|
|
|
self.expect(&token::ModSep);
|
2014-08-06 02:44:21 +00:00
|
|
|
let item_name = self.parse_ident();
|
|
|
|
TyQPath(P(QPath {
|
2014-11-08 11:59:10 +00:00
|
|
|
self_type: self_type,
|
|
|
|
trait_ref: P(trait_ref),
|
2014-08-06 02:44:21 +00:00
|
|
|
item_name: item_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
|
|
|
} else if self.check(&token::ModSep) ||
|
2014-11-04 02:52:52 +00:00
|
|
|
self.token.is_ident() ||
|
|
|
|
self.token.is_path() {
|
2013-03-08 18:19:19 +00:00
|
|
|
// NAMED TYPE
|
2014-11-20 20:05:29 +00:00
|
|
|
self.parse_ty_path()
|
2014-10-27 08:22:52 +00:00
|
|
|
} else if self.eat(&token::Underscore) {
|
2014-03-10 23:17:46 +00:00
|
|
|
// TYPE TO BE INFERRED
|
|
|
|
TyInfer
|
2013-02-24 17:39: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
|
|
|
let this_token_str = self.this_token_to_string();
|
|
|
|
let msg = format!("expected type, found `{}`", this_token_str);
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(msg.as_slice());
|
2013-02-24 17:39:29 +00:00
|
|
|
};
|
2012-05-23 22:06:11 +00:00
|
|
|
|
|
|
|
let sp = mk_sp(lo, self.last_span.hi);
|
2013-11-30 22:00:39 +00:00
|
|
|
P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp})
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_borrowed_pointee(&mut self) -> Ty_ {
|
2013-03-14 18:22:51 +00:00
|
|
|
// look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
|
2013-02-28 00:41:02 +00:00
|
|
|
let opt_lifetime = self.parse_opt_lifetime();
|
2012-11-05 04:41:00 +00:00
|
|
|
|
|
|
|
let mt = self.parse_mt();
|
2014-01-09 13:05:33 +00:00
|
|
|
return TyRptr(opt_lifetime, mt);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 23:58:17 +00:00
|
|
|
pub fn parse_ptr(&mut self) -> MutTy {
|
|
|
|
let mutbl = if self.eat_keyword(keywords::Mut) {
|
|
|
|
MutMutable
|
|
|
|
} else if self.eat_keyword(keywords::Const) {
|
|
|
|
MutImmutable
|
|
|
|
} else {
|
2014-06-25 19:00:27 +00:00
|
|
|
let span = self.last_span;
|
|
|
|
self.span_err(span,
|
|
|
|
"bare raw pointers are no longer allowed, you should \
|
|
|
|
likely use `*mut T`, but otherwise `*T` is now \
|
|
|
|
known as `*const T`");
|
2014-06-16 23:58:17 +00:00
|
|
|
MutImmutable
|
|
|
|
};
|
2014-11-20 20:05:29 +00:00
|
|
|
let t = self.parse_ty();
|
2014-06-16 23:58:17 +00:00
|
|
|
MutTy { ty: t, mutbl: mutbl }
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
is_plain_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 {
|
2013-09-02 23:58:12 +00:00
|
|
|
self.look_ahead(offset, |t| is_plain_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.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_arg_general(&mut self, require_name: bool) -> Arg {
|
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);
|
2013-05-29 23:59:33 +00:00
|
|
|
let pat = self.parse_pat();
|
2013-06-07 01:54:14 +00:00
|
|
|
|
2014-10-27 08:22:52 +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");
|
2013-09-07 02:11:55 +00:00
|
|
|
ast_util::ident_to_pat(ast::DUMMY_NODE_ID,
|
2013-12-30 23:30:14 +00:00
|
|
|
self.last_span,
|
2012-11-07 02:41:06 +00:00
|
|
|
special_idents::invalid)
|
2012-08-17 22:25:35 +00:00
|
|
|
};
|
|
|
|
|
2014-11-20 20:05:29 +00:00
|
|
|
let t = self.parse_ty_sum();
|
2012-08-17 22:25:35 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
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,
|
2013-04-24 08:29:46 +00:00
|
|
|
}
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a single function argument
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_arg(&mut self) -> 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|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_fn_block_arg(&mut self) -> Arg {
|
2013-05-29 23:59:33 +00:00
|
|
|
let pat = self.parse_pat();
|
2014-10-27 08:22:52 +00:00
|
|
|
let t = if self.eat(&token::Colon) {
|
2014-11-20 20:05:29 +00:00
|
|
|
self.parse_ty_sum()
|
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,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: TyInfer,
|
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
|
|
|
};
|
2014-01-09 13:05:33 +00:00
|
|
|
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
|
2013-09-14 02:07:43 +00:00
|
|
|
}
|
2012-05-04 19:33:04 +00:00
|
|
|
}
|
2010-09-21 23:22:32 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn maybe_parse_fixed_vstore(&mut self) -> Option<P<ast::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::Comma) &&
|
2014-10-27 08:22:52 +00:00
|
|
|
self.look_ahead(1, |t| *t == token::DotDot) {
|
2013-03-19 22:40:04 +00:00
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2013-03-05 02:03:21 +00:00
|
|
|
Some(self.parse_expr())
|
2012-08-14 02:59:32 +00:00
|
|
|
} else {
|
2012-08-20 19:23:37 +00:00
|
|
|
None
|
2012-08-14 02:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 17:44:20 +00:00
|
|
|
/// Matches token_lit = LIT_INTEGER | ...
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ {
|
2013-02-25 04:51:56 +00:00
|
|
|
match *tok {
|
2014-11-30 14:51:15 +00:00
|
|
|
token::Interpolated(token::NtExpr(ref v)) => {
|
|
|
|
match v.node {
|
|
|
|
ExprLit(ref lit) => { lit.node.clone() }
|
|
|
|
_ => { self.unexpected_last(tok); }
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 04:48:38 +00:00
|
|
|
token::Literal(lit, suf) => {
|
|
|
|
let (suffix_illegal, out) = match lit {
|
2014-12-09 17:17:24 +00:00
|
|
|
token::Byte(i) => (true, LitByte(parse::byte_lit(i.as_str()).0)),
|
|
|
|
token::Char(i) => (true, LitChar(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) => {
|
|
|
|
(false, parse::integer_lit(s.as_str(),
|
|
|
|
suf.as_ref().map(|s| s.as_str()),
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
self.last_span))
|
|
|
|
}
|
|
|
|
token::Float(s) => {
|
|
|
|
(false, parse::float_lit(s.as_str(),
|
|
|
|
suf.as_ref().map(|s| s.as_str()),
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
self.last_span))
|
|
|
|
}
|
|
|
|
|
2014-11-19 04:48:38 +00:00
|
|
|
token::Str_(s) => {
|
|
|
|
(true,
|
|
|
|
LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str()).as_slice()),
|
|
|
|
ast::CookedStr))
|
|
|
|
}
|
|
|
|
token::StrRaw(s, n) => {
|
|
|
|
(true,
|
|
|
|
LitStr(
|
|
|
|
token::intern_and_get_ident(
|
|
|
|
parse::raw_str_lit(s.as_str()).as_slice()),
|
|
|
|
ast::RawStr(n)))
|
|
|
|
}
|
|
|
|
token::Binary(i) =>
|
|
|
|
(true, LitBinary(parse::binary_lit(i.as_str()))),
|
|
|
|
token::BinaryRaw(i, _) =>
|
|
|
|
(true,
|
|
|
|
LitBinary(Rc::new(i.as_str().as_bytes().iter().map(|&x| x).collect()))),
|
|
|
|
};
|
|
|
|
|
|
|
|
if suffix_illegal {
|
|
|
|
let sp = self.last_span;
|
|
|
|
self.expect_no_suffix(sp, &*format!("{} literal", lit.short_name()), suf)
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
2014-01-10 22:02:36 +00:00
|
|
|
}
|
2013-02-25 05:20:50 +00:00
|
|
|
_ => { self.unexpected_last(tok); }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
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
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_lit(&mut self) -> Lit {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-05-25 15:45:45 +00:00
|
|
|
let lit = if self.eat_keyword(keywords::True) {
|
2014-01-09 13:05:33 +00:00
|
|
|
LitBool(true)
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::False) {
|
2014-01-09 13:05:33 +00:00
|
|
|
LitBool(false)
|
2012-04-27 19:22:42 +00:00
|
|
|
} else {
|
2013-07-02 19:47:32 +00:00
|
|
|
let token = self.bump_and_get();
|
|
|
|
let lit = self.lit_from_token(&token);
|
|
|
|
lit
|
2012-05-23 22:06:11 +00:00
|
|
|
};
|
2013-08-31 16:13:04 +00:00
|
|
|
codemap::Spanned { node: lit, span: mk_sp(lo, self.last_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
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_literal_maybe_minus(&mut self) -> P<Expr> {
|
2013-05-11 01:19:58 +00:00
|
|
|
let minus_lo = self.span.lo;
|
2014-10-27 08:22:52 +00:00
|
|
|
let minus_present = self.eat(&token::BinOp(token::Minus));
|
2013-05-11 01:19:58 +00:00
|
|
|
|
|
|
|
let lo = self.span.lo;
|
2014-09-13 16:06:01 +00:00
|
|
|
let literal = P(self.parse_lit());
|
2013-05-11 01:19:58 +00:00
|
|
|
let hi = self.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
let expr = self.mk_expr(lo, hi, ExprLit(literal));
|
2013-05-11 01:19:58 +00:00
|
|
|
|
|
|
|
if minus_present {
|
|
|
|
let minus_hi = self.span.hi;
|
2013-12-30 22:04:00 +00:00
|
|
|
let unary = self.mk_unary(UnNeg, expr);
|
|
|
|
self.mk_expr(minus_lo, minus_hi, unary)
|
2013-05-11 01:19:58 +00:00
|
|
|
} else {
|
|
|
|
expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2014-11-20 20:05:29 +00:00
|
|
|
pub fn parse_path(&mut self, mode: PathParsingMode) -> ast::Path {
|
2013-08-07 16:47:28 +00:00
|
|
|
// Check for a whole path...
|
2013-12-30 23:09:41 +00:00
|
|
|
let found = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtPath(_)) => Some(self.bump_and_get()),
|
2013-08-07 16:47:28 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
2014-11-29 21:41:21 +00:00
|
|
|
if let Some(token::Interpolated(token::NtPath(box path))) = found {
|
|
|
|
return path;
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:36:10 +00:00
|
|
|
let lo = self.span.lo;
|
2014-10-27 08:22:52 +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.
|
2014-10-29 22:03:40 +00:00
|
|
|
let segments = match mode {
|
2014-11-20 20:05:29 +00:00
|
|
|
LifetimeAndTypesWithoutColons => {
|
2014-10-29 22:03:40 +00:00
|
|
|
self.parse_path_segments_without_colons()
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
2014-10-29 22:03:40 +00:00
|
|
|
LifetimeAndTypesWithColons => {
|
|
|
|
self.parse_path_segments_with_colons()
|
2013-02-26 19:35:17 +00:00
|
|
|
}
|
2014-10-29 22:03:40 +00:00
|
|
|
NoTypesAllowed => {
|
|
|
|
self.parse_path_segments_without_types()
|
|
|
|
}
|
|
|
|
};
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Assemble the span.
|
|
|
|
let span = mk_sp(lo, self.last_span.hi);
|
2013-06-17 19:16:30 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Assemble the result.
|
2014-11-20 20:05:29 +00:00
|
|
|
ast::Path {
|
|
|
|
span: span,
|
|
|
|
global: is_global,
|
|
|
|
segments: segments,
|
2014-03-22 16:20:22 +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)`
|
|
|
|
pub fn parse_path_segments_without_colons(&mut self) -> Vec<ast::PathSegment> {
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
loop {
|
|
|
|
// First, parse an identifier.
|
|
|
|
let identifier = self.parse_ident();
|
|
|
|
|
|
|
|
// Parse types, optionally.
|
2014-11-04 02:52:52 +00:00
|
|
|
let parameters = if self.eat_lt(false) {
|
2014-11-29 04:08:30 +00:00
|
|
|
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
|
2014-11-04 02:52:52 +00:00
|
|
|
|
|
|
|
ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
types: OwnedSlice::from_vec(types),
|
2014-11-29 04:08:30 +00:00
|
|
|
bindings: OwnedSlice::from_vec(bindings),
|
2014-11-04 02:52:52 +00:00
|
|
|
})
|
|
|
|
} else if self.eat(&token::OpenDelim(token::Paren)) {
|
|
|
|
let inputs = self.parse_seq_to_end(
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-29 22:03:40 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2014-11-20 20:05:29 +00:00
|
|
|
|p| p.parse_ty_sum());
|
2014-10-29 22:03:40 +00:00
|
|
|
|
2014-11-04 02:52:52 +00:00
|
|
|
let output_ty = if self.eat(&token::RArrow) {
|
2014-11-20 20:05:29 +00:00
|
|
|
Some(self.parse_ty())
|
2014-11-04 02:52:52 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2014-10-29 22:03:40 +00:00
|
|
|
|
2014-11-04 02:52:52 +00:00
|
|
|
ast::ParenthesizedParameters(ast::ParenthesizedParameterData {
|
|
|
|
inputs: inputs,
|
|
|
|
output: output_ty
|
|
|
|
})
|
2014-10-29 22:03:40 +00:00
|
|
|
} else {
|
2014-11-04 02:52:52 +00:00
|
|
|
ast::PathParameters::none()
|
2014-10-29 22:03:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Assemble and push the result.
|
|
|
|
segments.push(ast::PathSegment { identifier: identifier,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: parameters });
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
// Continue only if we see a `::`
|
|
|
|
if !self.eat(&token::ModSep) {
|
|
|
|
return segments;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Examples:
|
|
|
|
/// - `a::b::<T,U>::c`
|
|
|
|
pub fn parse_path_segments_with_colons(&mut self) -> Vec<ast::PathSegment> {
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
loop {
|
|
|
|
// First, parse an identifier.
|
|
|
|
let identifier = self.parse_ident();
|
|
|
|
|
|
|
|
// If we do not see a `::`, stop.
|
|
|
|
if !self.eat(&token::ModSep) {
|
2014-11-04 02:52:52 +00:00
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
identifier: identifier,
|
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: Vec::new(),
|
|
|
|
types: OwnedSlice::empty(),
|
2014-11-29 04:08:30 +00:00
|
|
|
bindings: OwnedSlice::empty(),
|
2014-11-04 02:52:52 +00:00
|
|
|
})
|
|
|
|
});
|
2014-10-29 22:03:40 +00:00
|
|
|
return segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a type segment.
|
|
|
|
if self.eat_lt(false) {
|
|
|
|
// Consumed `a::b::<`, go look for types
|
2014-11-29 04:08:30 +00:00
|
|
|
let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
|
2014-11-04 02:52:52 +00:00
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
identifier: identifier,
|
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
types: OwnedSlice::from_vec(types),
|
2014-11-29 04:08:30 +00:00
|
|
|
bindings: OwnedSlice::from_vec(bindings),
|
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
|
|
|
|
if !self.eat(&token::ModSep) {
|
|
|
|
return segments;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Consumed `a::`, go look for `b`
|
2014-11-04 02:52:52 +00:00
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
identifier: identifier,
|
|
|
|
parameters: ast::PathParameters::none(),
|
|
|
|
});
|
2014-10-29 22:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Examples:
|
|
|
|
/// - `a::b::c`
|
|
|
|
pub fn parse_path_segments_without_types(&mut self) -> Vec<ast::PathSegment> {
|
|
|
|
let mut segments = Vec::new();
|
|
|
|
loop {
|
|
|
|
// First, parse an identifier.
|
|
|
|
let identifier = self.parse_ident();
|
|
|
|
|
|
|
|
// Assemble and push the result.
|
2014-11-04 02:52:52 +00:00
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
identifier: identifier,
|
|
|
|
parameters: ast::PathParameters::none()
|
|
|
|
});
|
2014-10-29 22:03:40 +00:00
|
|
|
|
|
|
|
// If we do not see a `::`, stop.
|
|
|
|
if !self.eat(&token::ModSep) {
|
|
|
|
return segments;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
/// parses 0 or 1 lifetime
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_opt_lifetime(&mut self) -> Option<ast::Lifetime> {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(..) => {
|
2013-07-05 12:33:52 +00:00
|
|
|
Some(self.parse_lifetime())
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
/// Parses a single lifetime
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Matches lifetime = LIFETIME
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_lifetime(&mut self) -> ast::Lifetime {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(i) => {
|
2013-12-30 23:17:53 +00:00
|
|
|
let span = self.span;
|
2013-02-28 00:41:02 +00:00
|
|
|
self.bump();
|
|
|
|
return ast::Lifetime {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-10-29 10:03:32 +00:00
|
|
|
span: span,
|
2014-03-07 02:10:52 +00:00
|
|
|
name: i.name
|
2013-02-28 00:41:02 +00:00
|
|
|
};
|
|
|
|
}
|
2013-02-12 00:33:31 +00:00
|
|
|
_ => {
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(format!("expected a lifetime name").as_slice());
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 02:17:11 +00:00
|
|
|
/// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
|
|
|
|
/// lifetime [':' lifetimes]`
|
2014-08-06 02:59:24 +00:00
|
|
|
pub fn parse_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
|
2013-02-12 00:33:31 +00:00
|
|
|
|
2014-03-07 15:50:40 +00:00
|
|
|
let mut res = Vec::new();
|
2013-02-12 00:33:31 +00:00
|
|
|
loop {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(_) => {
|
2014-08-06 02:59:24 +00:00
|
|
|
let lifetime = self.parse_lifetime();
|
|
|
|
let bounds =
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
|
|
|
self.parse_lifetimes(token::BinOp(token::Plus))
|
2014-08-06 02:59:24 +00:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
res.push(ast::LifetimeDef { lifetime: lifetime,
|
|
|
|
bounds: bounds });
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
2014-08-06 02:59:24 +00:00
|
|
|
|
2013-02-12 00:33:31 +00:00
|
|
|
_ => {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Comma => { self.bump(); }
|
|
|
|
token::Gt => { return res; }
|
|
|
|
token::BinOp(token::Shr) => { return res; }
|
2013-02-12 00:33:31 +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
|
|
|
let this_token_str = self.this_token_to_string();
|
2013-12-30 23:09:41 +00:00
|
|
|
let msg = format!("expected `,` or `>` after lifetime \
|
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
|
|
|
name, found `{}`",
|
|
|
|
this_token_str);
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(msg.as_slice());
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 02:17:11 +00:00
|
|
|
/// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
|
|
|
|
/// one too, but putting that in there messes up the grammar....
|
|
|
|
///
|
|
|
|
/// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
|
|
|
|
/// either a comma or `>`. Used when parsing type parameter lists, where we expect something
|
|
|
|
/// like `<'a, 'b, T>`.
|
2014-08-06 02:59:24 +00:00
|
|
|
pub fn parse_lifetimes(&mut self, sep: token::Token) -> Vec<ast::Lifetime> {
|
|
|
|
|
|
|
|
let mut res = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(_) => {
|
2014-08-06 02:59:24 +00:00
|
|
|
res.push(self.parse_lifetime());
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.token != sep {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse mutability declaration (mut/const/imm)
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_mutability(&mut self) -> Mutability {
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Mut) {
|
2013-09-02 01:45:37 +00:00
|
|
|
MutMutable
|
2012-04-24 22:52:52 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
MutImmutable
|
2012-04-24 22:52:52 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse ident COLON expr
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_field(&mut self) -> Field {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2012-05-24 19:38:45 +00:00
|
|
|
let i = self.parse_ident();
|
2013-10-29 02:22:42 +00:00
|
|
|
let hi = self.last_span.hi;
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_expr();
|
2013-07-19 14:24:22 +00:00
|
|
|
ast::Field {
|
2013-10-29 02:22:42 +00:00
|
|
|
ident: spanned(lo, hi, i),
|
2013-07-19 14:24:22 +00:00
|
|
|
span: mk_sp(lo, e.span.hi),
|
2014-09-13 16:06:01 +00:00
|
|
|
expr: e,
|
2013-07-19 14:24:22 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-09-28 01:25:02 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr> {
|
|
|
|
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),
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2010-10-12 23:30:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::Expr_ {
|
2014-02-26 14:06:45 +00:00
|
|
|
ExprUnary(unop, expr)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
|
2014-02-26 14:06:45 +00:00
|
|
|
ExprBinary(binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::Expr_ {
|
2014-02-14 08:28:32 +00:00
|
|
|
ExprCall(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>>)
|
2014-04-23 21:19:23 +00:00
|
|
|
-> ast::Expr_ {
|
2014-02-26 14:06:45 +00:00
|
|
|
ExprMethodCall(ident, tps, args)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::Expr_ {
|
2014-02-26 14:06:45 +00:00
|
|
|
ExprIndex(expr, idx)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 08:48:58 +00:00
|
|
|
pub fn mk_slice(&mut self, expr: P<Expr>,
|
|
|
|
start: Option<P<Expr>>,
|
|
|
|
end: Option<P<Expr>>,
|
|
|
|
mutbl: Mutability)
|
|
|
|
-> ast::Expr_ {
|
|
|
|
ExprSlice(expr, start, end, mutbl)
|
|
|
|
}
|
|
|
|
|
2014-11-23 11:14:35 +00:00
|
|
|
pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::Expr_ {
|
|
|
|
ExprField(expr, ident)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 11:14:35 +00:00
|
|
|
pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<uint>) -> ast::Expr_ {
|
|
|
|
ExprTupField(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,
|
2014-09-13 16:06:01 +00:00
|
|
|
lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
|
2014-02-26 14:06:45 +00:00
|
|
|
ExprAssignOp(binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr> {
|
|
|
|
P(Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-09-02 01:45:37 +00:00
|
|
|
node: ExprMac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
|
2013-01-15 21:51:43 +00:00
|
|
|
span: mk_sp(lo, hi),
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-12-14 23:32:13 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn mk_lit_u32(&mut self, i: u32) -> 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 {
|
2014-08-05 07:59:03 +00:00
|
|
|
node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)),
|
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,
|
2013-09-02 01:45:37 +00:00
|
|
|
node: ExprLit(lv_lit),
|
2013-02-22 02:12:13 +00:00
|
|
|
span: *span,
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
|
2014-10-29 14:47:53 +00:00
|
|
|
fn expect_open_delim(&mut self) -> token::DelimToken {
|
|
|
|
match self.token {
|
|
|
|
token::OpenDelim(delim) => {
|
|
|
|
self.bump();
|
|
|
|
delim
|
|
|
|
},
|
|
|
|
_ => self.fatal("expected open delimiter"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// At the bottom (top?) of the precedence hierarchy,
|
|
|
|
/// parse things like parenthesized exprs,
|
|
|
|
/// macros, return, etc.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_bottom_expr(&mut self) -> P<Expr> {
|
2012-08-23 00:24:52 +00:00
|
|
|
maybe_whole_expr!(self);
|
2013-03-02 21:02:27 +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
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
let ex: Expr_;
|
2012-01-04 06:03:07 +00:00
|
|
|
|
2014-07-06 21:29:29 +00:00
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2013-02-17 23:41:47 +00:00
|
|
|
self.bump();
|
2014-11-09 15:14:15 +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) {
|
|
|
|
es.push(self.parse_expr());
|
|
|
|
self.commit_expr(&**es.last().unwrap(), &[],
|
|
|
|
&[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
|
|
|
|
|
|
|
self.bump();
|
|
|
|
} else {
|
|
|
|
trailing_comma = false;
|
|
|
|
break;
|
2014-07-06 21:29:29 +00:00
|
|
|
}
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
2014-11-09 15:14:15 +00:00
|
|
|
self.bump();
|
2013-10-28 22:22:49 +00:00
|
|
|
|
2014-11-09 15:14:15 +00:00
|
|
|
hi = self.span.hi;
|
2014-07-06 21:29:29 +00:00
|
|
|
return if es.len() == 1 && !trailing_comma {
|
2014-11-09 15:14:15 +00:00
|
|
|
self.mk_expr(lo, hi, ExprParen(es.into_iter().nth(0).unwrap()))
|
2014-09-13 16:06:01 +00:00
|
|
|
} else {
|
2014-07-06 21:29:29 +00:00
|
|
|
self.mk_expr(lo, hi, ExprTup(es))
|
|
|
|
}
|
|
|
|
},
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Brace) => {
|
2014-07-06 21:29:29 +00:00
|
|
|
self.bump();
|
|
|
|
let blk = self.parse_block_tail(lo, DefaultBlock);
|
|
|
|
return self.mk_expr(blk.span.lo, blk.span.hi,
|
|
|
|
ExprBlock(blk));
|
|
|
|
},
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Or) | token::OrOr => {
|
2014-09-24 17:58:53 +00:00
|
|
|
return self.parse_lambda_expr(CaptureByRef);
|
2014-07-06 21:29:29 +00:00
|
|
|
},
|
2014-07-06 08:17:59 +00:00
|
|
|
// FIXME #13626: Should be able to stick in
|
|
|
|
// token::SELF_KEYWORD_NAME
|
2014-10-27 15:01:44 +00:00
|
|
|
token::Ident(id @ ast::Ident {
|
|
|
|
name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
|
|
|
|
ctxt: _
|
|
|
|
}, token::Plain) => {
|
2014-07-06 22:11:44 +00:00
|
|
|
self.bump();
|
|
|
|
let path = ast_util::ident_to_path(mk_sp(lo, hi), id);
|
2014-07-06 21:29:29 +00:00
|
|
|
ex = ExprPath(path);
|
|
|
|
hi = self.last_span.hi;
|
2013-09-08 12:08:01 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Bracket) => {
|
2014-07-06 21:29:29 +00:00
|
|
|
self.bump();
|
2014-07-06 22:11:44 +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.
|
2012-08-04 01:01:30 +00:00
|
|
|
self.bump();
|
2014-07-06 21:29:29 +00:00
|
|
|
ex = ExprVec(Vec::new());
|
|
|
|
} else {
|
|
|
|
// Nonempty vector.
|
|
|
|
let first_expr = self.parse_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::Comma) &&
|
2014-10-27 08:22:52 +00:00
|
|
|
self.look_ahead(1, |t| *t == token::DotDot) {
|
2014-07-06 21:29:29 +00:00
|
|
|
// Repeating vector syntax: [ 0, ..512 ]
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
let count = self.parse_expr();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket));
|
2014-07-06 21:29:29 +00:00
|
|
|
ex = ExprRepeat(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.
|
|
|
|
self.bump();
|
|
|
|
let remaining_exprs = self.parse_seq_to_end(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::CloseDelim(token::Bracket),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2014-07-06 21:29:29 +00:00
|
|
|
|p| p.parse_expr()
|
|
|
|
);
|
|
|
|
let mut exprs = vec!(first_expr);
|
2014-10-15 06:05:01 +00:00
|
|
|
exprs.extend(remaining_exprs.into_iter());
|
2014-07-06 21:29:29 +00:00
|
|
|
ex = ExprVec(exprs);
|
|
|
|
} else {
|
|
|
|
// Vector with one element.
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket));
|
2014-07-06 21:29:29 +00:00
|
|
|
ex = ExprVec(vec!(first_expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hi = self.last_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
|
|
|
_ => {
|
2014-09-24 17:58:53 +00:00
|
|
|
if self.eat_keyword(keywords::Move) {
|
|
|
|
return self.parse_lambda_expr(CaptureByValue);
|
2014-07-23 19:43:29 +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
|
|
|
if self.eat_keyword(keywords::Proc) {
|
2014-11-26 15:07:22 +00:00
|
|
|
let span = self.last_span;
|
|
|
|
let _ = self.parse_proc_decl();
|
|
|
|
let _ = self.parse_expr();
|
|
|
|
return self.obsolete_expr(span, ObsoleteProcExpr);
|
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
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::If) {
|
|
|
|
return self.parse_if_expr();
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::For) {
|
|
|
|
return self.parse_for_expr(None);
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::While) {
|
2014-07-26 00:12:51 +00:00
|
|
|
return self.parse_while_expr(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
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_lifetime() {
|
2014-07-06 21:29:29 +00:00
|
|
|
let lifetime = self.get_lifetime();
|
2012-08-04 01:01:30 +00:00
|
|
|
self.bump();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2014-07-26 00:12:51 +00:00
|
|
|
if self.eat_keyword(keywords::While) {
|
|
|
|
return self.parse_while_expr(Some(lifetime))
|
|
|
|
}
|
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
|
|
|
if self.eat_keyword(keywords::For) {
|
|
|
|
return self.parse_for_expr(Some(lifetime))
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::Loop) {
|
|
|
|
return self.parse_loop_expr(Some(lifetime))
|
|
|
|
}
|
2014-07-26 00:12:51 +00:00
|
|
|
self.fatal("expected `while`, `for`, or `loop` after a label")
|
2012-08-04 01:01:30 +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
|
|
|
if self.eat_keyword(keywords::Loop) {
|
|
|
|
return self.parse_loop_expr(None);
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::Continue) {
|
|
|
|
let lo = self.span.lo;
|
2014-10-27 12:33:30 +00:00
|
|
|
let ex = if self.token.is_lifetime() {
|
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
|
|
|
let lifetime = self.get_lifetime();
|
|
|
|
self.bump();
|
|
|
|
ExprAgain(Some(lifetime))
|
|
|
|
} else {
|
|
|
|
ExprAgain(None)
|
|
|
|
};
|
2014-07-06 21:29:29 +00:00
|
|
|
let hi = self.span.hi;
|
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.mk_expr(lo, hi, ex);
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::Match) {
|
|
|
|
return self.parse_match_expr();
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
|
|
|
return self.parse_block_expr(
|
|
|
|
lo,
|
|
|
|
UnsafeBlock(ast::UserProvided));
|
|
|
|
}
|
|
|
|
if self.eat_keyword(keywords::Return) {
|
|
|
|
// RETURN expression
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.can_begin_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
|
|
|
let e = self.parse_expr();
|
|
|
|
hi = e.span.hi;
|
|
|
|
ex = ExprRet(Some(e));
|
|
|
|
} else {
|
|
|
|
ex = ExprRet(None);
|
|
|
|
}
|
|
|
|
} else if self.eat_keyword(keywords::Break) {
|
|
|
|
// BREAK expression
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_lifetime() {
|
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
|
|
|
let lifetime = self.get_lifetime();
|
|
|
|
self.bump();
|
|
|
|
ex = ExprBreak(Some(lifetime));
|
|
|
|
} else {
|
|
|
|
ex = ExprBreak(None);
|
|
|
|
}
|
|
|
|
hi = self.span.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
|
|
|
} else if self.check(&token::ModSep) ||
|
2014-10-27 12:33:30 +00:00
|
|
|
self.token.is_ident() &&
|
|
|
|
!self.token.is_keyword(keywords::True) &&
|
|
|
|
!self.token.is_keyword(keywords::False) {
|
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
|
|
|
let pth =
|
2014-11-20 20:05:29 +00:00
|
|
|
self.parse_path(LifetimeAndTypesWithColons);
|
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
|
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::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
|
|
|
|
self.bump();
|
2014-07-06 21:29:29 +00:00
|
|
|
|
2014-10-29 14:47:53 +00:00
|
|
|
let delim = self.expect_open_delim();
|
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
|
|
|
let tts = self.parse_seq_to_end(
|
2014-10-29 14:47:53 +00:00
|
|
|
&token::CloseDelim(delim),
|
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
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
|
|
|
return self.mk_mac_expr(lo,
|
|
|
|
hi,
|
|
|
|
MacInvocTT(pth,
|
|
|
|
tts,
|
|
|
|
EMPTY_CTXT));
|
|
|
|
}
|
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.
|
2014-10-06 03:08:35 +00:00
|
|
|
if !self.restrictions.contains(RESTRICTION_NO_STRUCT_LITERAL) {
|
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
|
|
|
// It's a struct literal.
|
|
|
|
self.bump();
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
let mut base = None;
|
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::DotDot) {
|
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
|
|
|
base = Some(self.parse_expr());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fields.push(self.parse_field());
|
2014-09-13 16:06:01 +00:00
|
|
|
self.commit_expr(&*fields.last().unwrap().expr,
|
2014-10-27 08:22:52 +00:00
|
|
|
&[token::Comma],
|
2014-10-29 10:37:54 +00:00
|
|
|
&[token::CloseDelim(token::Brace)]);
|
2014-07-06 21:29:29 +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
|
|
|
if fields.len() == 0 && base.is_none() {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
|
|
|
"structure literal must either \
|
|
|
|
have at least one field or use \
|
|
|
|
functional structure update \
|
|
|
|
syntax");
|
|
|
|
}
|
2012-09-08 22:50:29 +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 = self.span.hi;
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(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
|
|
|
ex = ExprStruct(pth, fields, base);
|
|
|
|
return self.mk_expr(lo, hi, ex);
|
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;
|
|
|
|
ex = ExprPath(pth);
|
|
|
|
} else {
|
|
|
|
// other literal expression
|
|
|
|
let lit = self.parse_lit();
|
|
|
|
hi = lit.span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
ex = ExprLit(P(lit));
|
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
|
|
|
}
|
|
|
|
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.mk_expr(lo, hi, ex);
|
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
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode)
|
2014-09-13 16:06:01 +00:00
|
|
|
-> P<Expr> {
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2012-05-23 22:06:11 +00:00
|
|
|
let blk = self.parse_block_tail(lo, blk_mode);
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
|
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
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_dot_or_call_expr(&mut self) -> P<Expr> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let b = self.parse_bottom_expr();
|
|
|
|
self.parse_dot_or_call_expr_with(b)
|
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let mut e = e0;
|
|
|
|
let lo = e.span.lo;
|
2012-05-24 20:35:57 +00:00
|
|
|
let mut hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
loop {
|
|
|
|
// expr.f
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Dot) {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(i, _) => {
|
2014-04-23 21:19:23 +00:00
|
|
|
let dot = self.last_span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
|
|
|
self.bump();
|
2014-11-29 04:08:30 +00:00
|
|
|
let (_, tys, bindings) = if self.eat(&token::ModSep) {
|
2014-05-11 04:27:44 +00:00
|
|
|
self.expect_lt();
|
2013-02-15 05:50:03 +00:00
|
|
|
self.parse_generic_values_after_lt()
|
2012-11-30 19:18:25 +00:00
|
|
|
} else {
|
2014-11-29 04:08:30 +00:00
|
|
|
(Vec::new(), Vec::new(), Vec::new())
|
2012-11-30 19:18:25 +00:00
|
|
|
};
|
|
|
|
|
2014-11-29 04:08:30 +00:00
|
|
|
if bindings.len() > 0 {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span, "type bindings are only permitted on trait paths");
|
|
|
|
}
|
|
|
|
|
2012-11-30 19:18:25 +00:00
|
|
|
// expr.f() method call
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2014-01-27 12:18:36 +00:00
|
|
|
let mut es = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_expr()
|
|
|
|
);
|
2014-02-07 10:52:12 +00:00
|
|
|
hi = self.last_span.hi;
|
2012-11-30 19:18:25 +00:00
|
|
|
|
2014-10-15 06:05:01 +00:00
|
|
|
es.insert(0, e);
|
2014-04-23 21:19:23 +00:00
|
|
|
let id = spanned(dot, hi, i);
|
|
|
|
let nd = self.mk_method_call(id, tys, es);
|
2013-02-15 09:15:53 +00:00
|
|
|
e = self.mk_expr(lo, hi, nd);
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2014-11-11 18:45:59 +00:00
|
|
|
if !tys.is_empty() {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
|
|
|
"field expressions may not \
|
|
|
|
have type parameters");
|
|
|
|
}
|
|
|
|
|
2014-06-13 21:56:42 +00:00
|
|
|
let id = spanned(dot, hi, i);
|
2014-11-23 11:14:35 +00:00
|
|
|
let field = self.mk_field(e, id);
|
2014-11-11 18:45:59 +00:00
|
|
|
e = self.mk_expr(lo, hi, field);
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
|
2014-08-10 03:54:33 +00:00
|
|
|
let dot = self.last_span.hi;
|
|
|
|
hi = self.span.hi;
|
|
|
|
self.bump();
|
|
|
|
|
2014-11-22 15:02:49 +00:00
|
|
|
let index = from_str::<uint>(n.as_str());
|
|
|
|
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);
|
2014-08-10 03:54:33 +00:00
|
|
|
e = self.mk_expr(lo, hi, field);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span, "invalid tuple or tuple struct index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 04:48:38 +00:00
|
|
|
token::Literal(token::Float(n), _suf) => {
|
2014-08-10 03:54:33 +00:00
|
|
|
self.bump();
|
|
|
|
let last_span = self.last_span;
|
2014-10-18 02:39:44 +00:00
|
|
|
let fstr = n.as_str();
|
2014-08-10 03:54:33 +00:00
|
|
|
self.span_err(last_span,
|
|
|
|
format!("unexpected token: `{}`", n.as_str()).as_slice());
|
2014-10-18 02:39:44 +00:00
|
|
|
if fstr.chars().all(|x| "0123456789.".contains_char(x)) {
|
|
|
|
let float = match from_str::<f64>(fstr) {
|
|
|
|
Some(f) => f,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
self.span_help(last_span,
|
|
|
|
format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
|
|
|
|
float.trunc() as uint,
|
|
|
|
float.fract().to_string()[1..]).as_slice());
|
|
|
|
}
|
2014-08-10 03:54:33 +00:00
|
|
|
self.abort_if_errors();
|
|
|
|
|
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => self.unexpected()
|
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
|
|
|
}
|
2014-09-13 16:06:01 +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) => {
|
2012-06-20 02:34:01 +00:00
|
|
|
let es = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_expr()
|
|
|
|
);
|
2014-01-16 13:45:01 +00:00
|
|
|
hi = self.last_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);
|
2012-10-28 00:14:09 +00:00
|
|
|
e = self.mk_expr(lo, hi, nd);
|
2011-12-21 04:12:52 +00:00
|
|
|
}
|
2012-05-31 01:14:40 +00:00
|
|
|
|
|
|
|
// expr[...]
|
2014-09-15 08:48:58 +00:00
|
|
|
// Could be either an index expression or a slicing expression.
|
|
|
|
// Any slicing non-terminal can have a mutable version with `mut`
|
|
|
|
// after the opening square bracket.
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Bracket) => {
|
2012-05-31 01:14:40 +00:00
|
|
|
self.bump();
|
2014-09-15 08:48:58 +00:00
|
|
|
let mutbl = if self.eat_keyword(keywords::Mut) {
|
|
|
|
MutMutable
|
|
|
|
} else {
|
|
|
|
MutImmutable
|
|
|
|
};
|
|
|
|
match self.token {
|
|
|
|
// e[]
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Bracket) => {
|
2014-09-15 08:48:58 +00:00
|
|
|
self.bump();
|
|
|
|
hi = self.span.hi;
|
|
|
|
let slice = self.mk_slice(e, None, None, mutbl);
|
|
|
|
e = self.mk_expr(lo, hi, slice)
|
|
|
|
}
|
|
|
|
// e[..e]
|
2014-10-27 08:22:52 +00:00
|
|
|
token::DotDot => {
|
2014-09-15 08:48:58 +00:00
|
|
|
self.bump();
|
|
|
|
match self.token {
|
|
|
|
// e[..]
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Bracket) => {
|
2014-09-15 08:48:58 +00:00
|
|
|
self.bump();
|
|
|
|
hi = self.span.hi;
|
|
|
|
let slice = self.mk_slice(e, None, None, mutbl);
|
|
|
|
e = self.mk_expr(lo, hi, slice);
|
|
|
|
|
|
|
|
self.span_err(e.span, "incorrect slicing expression: `[..]`");
|
|
|
|
self.span_note(e.span,
|
|
|
|
"use `expr[]` to construct a slice of the whole of expr");
|
|
|
|
}
|
|
|
|
// e[..e]
|
|
|
|
_ => {
|
|
|
|
hi = self.span.hi;
|
|
|
|
let e2 = self.parse_expr();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.commit_expr_expecting(&*e2, token::CloseDelim(token::Bracket));
|
2014-09-15 08:48:58 +00:00
|
|
|
let slice = self.mk_slice(e, None, Some(e2), mutbl);
|
|
|
|
e = self.mk_expr(lo, hi, slice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// e[e] | e[e..] | e[e..e]
|
|
|
|
_ => {
|
|
|
|
let ix = self.parse_expr();
|
|
|
|
match self.token {
|
|
|
|
// e[e..] | e[e..e]
|
2014-10-27 08:22:52 +00:00
|
|
|
token::DotDot => {
|
2014-09-15 08:48:58 +00:00
|
|
|
self.bump();
|
|
|
|
let e2 = match self.token {
|
|
|
|
// e[e..]
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Bracket) => {
|
2014-09-15 08:48:58 +00:00
|
|
|
self.bump();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
// e[e..e]
|
|
|
|
_ => {
|
|
|
|
let e2 = self.parse_expr();
|
2014-10-29 21:44:41 +00:00
|
|
|
self.commit_expr_expecting(&*e2,
|
|
|
|
token::CloseDelim(token::Bracket));
|
2014-09-15 08:48:58 +00:00
|
|
|
Some(e2)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
hi = self.span.hi;
|
|
|
|
let slice = self.mk_slice(e, Some(ix), e2, mutbl);
|
|
|
|
e = self.mk_expr(lo, hi, slice)
|
|
|
|
}
|
|
|
|
// e[e]
|
|
|
|
_ => {
|
|
|
|
if mutbl == ast::MutMutable {
|
|
|
|
self.span_err(e.span,
|
|
|
|
"`mut` keyword is invalid in index expressions");
|
|
|
|
}
|
|
|
|
hi = self.span.hi;
|
2014-10-29 10:37:54 +00:00
|
|
|
self.commit_expr_expecting(&*ix, token::CloseDelim(token::Bracket));
|
2014-09-15 08:48:58 +00:00
|
|
|
let index = self.mk_index(e, ix);
|
|
|
|
e = self.mk_expr(lo, hi, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-31 01:14:40 +00:00
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => return e
|
2012-05-31 01:14:40 +00:00
|
|
|
}
|
2010-09-28 17:30:34 +00:00
|
|
|
}
|
2012-08-02 00:30:05 +00:00
|
|
|
return e;
|
2010-09-28 17:30:34 +00:00
|
|
|
}
|
2010-09-28 01:25:02 +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 *).
|
2014-10-23 00:24:20 +00:00
|
|
|
pub fn parse_sep_and_kleene_op(&mut self) -> (Option<token::Token>, ast::KleeneOp) {
|
|
|
|
fn parse_kleene_op(parser: &mut Parser) -> Option<ast::KleeneOp> {
|
2013-12-30 23:09:41 +00:00
|
|
|
match parser.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Star) => {
|
2013-07-18 03:04:37 +00:00
|
|
|
parser.bump();
|
2014-10-23 00:24:20 +00:00
|
|
|
Some(ast::ZeroOrMore)
|
|
|
|
},
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Plus) => {
|
2014-10-23 00:24:20 +00:00
|
|
|
parser.bump();
|
|
|
|
Some(ast::OneOrMore)
|
2013-07-18 03:04:37 +00:00
|
|
|
},
|
|
|
|
_ => None
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
2013-07-18 03:04:37 +00:00
|
|
|
};
|
|
|
|
|
2014-10-23 00:24:20 +00:00
|
|
|
match parse_kleene_op(self) {
|
|
|
|
Some(kleene_op) => return (None, kleene_op),
|
2013-07-18 03:04:37 +00:00
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let separator = self.bump_and_get();
|
2014-10-23 00:24:20 +00:00
|
|
|
match parse_kleene_op(self) {
|
2013-07-18 03:04:37 +00:00
|
|
|
Some(zerok) => (Some(separator), zerok),
|
|
|
|
None => 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.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_token_tree(&mut self) -> TokenTree {
|
2013-09-24 19:31:24 +00:00
|
|
|
// FIXME #6994: currently, this is too eager. It
|
2014-10-22 17:39:58 +00:00
|
|
|
// parses token trees but also identifies TtSequence'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-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(deref self, NtTT);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2013-04-15 23:13:42 +00:00
|
|
|
// this is the fall-through for the 'match' below.
|
|
|
|
// invariants: the current token is not a left-delimiter,
|
|
|
|
// not an EOF, and not the desired right-delimiter (if
|
|
|
|
// it were, parse_seq_to_before_end would have prevented
|
|
|
|
// reaching this point.
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree {
|
|
|
|
maybe_whole!(deref p, NtTT);
|
2013-12-30 23:09:41 +00:00
|
|
|
match p.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(_) => {
|
2013-11-25 02:18:20 +00:00
|
|
|
// This is a conservative error: only report the last unclosed delimiter. The
|
|
|
|
// previous unclosed delimiters could actually be closed! The parser just hasn't
|
|
|
|
// gotten to them yet.
|
2013-12-23 14:08:23 +00:00
|
|
|
match p.open_braces.last() {
|
2013-12-30 23:41:11 +00:00
|
|
|
None => {}
|
|
|
|
Some(&sp) => p.span_note(sp, "unclosed delimiter"),
|
|
|
|
};
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = p.this_token_to_string();
|
2013-12-30 22:04:00 +00:00
|
|
|
p.fatal(format!("incorrect close delimiter: `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2013-11-25 02:18:20 +00:00
|
|
|
},
|
2012-06-27 22:29:35 +00:00
|
|
|
/* we ought to allow different depths of unquotation */
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Dollar if p.quote_depth > 0u => {
|
2012-06-27 22:29:35 +00:00
|
|
|
p.bump();
|
2013-12-30 23:17:53 +00:00
|
|
|
let sp = p.span;
|
2012-07-05 21:30:56 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
if p.token == token::OpenDelim(token::Paren) {
|
2013-02-24 23:41:54 +00:00
|
|
|
let seq = p.parse_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree()
|
|
|
|
);
|
2014-10-23 00:24:20 +00:00
|
|
|
let (sep, repeat) = p.parse_sep_and_kleene_op();
|
2013-05-12 04:25:31 +00:00
|
|
|
let seq = match seq {
|
2013-11-28 20:22:53 +00:00
|
|
|
Spanned { node, .. } => node,
|
2013-05-12 04:25:31 +00:00
|
|
|
};
|
2014-10-06 22:00:56 +00:00
|
|
|
let name_num = macro_parser::count_names(seq.as_slice());
|
2014-11-02 11:21:16 +00:00
|
|
|
TtSequence(mk_sp(sp.lo, p.span.hi),
|
|
|
|
Rc::new(SequenceRepetition {
|
|
|
|
tts: seq,
|
|
|
|
separator: sep,
|
|
|
|
op: repeat,
|
|
|
|
num_captures: name_num
|
|
|
|
}))
|
2012-07-05 21:30:56 +00:00
|
|
|
} else {
|
2014-10-06 22:00:56 +00:00
|
|
|
// A nonterminal that matches or not
|
|
|
|
let namep = match p.token { token::Ident(_, p) => p, _ => token::Plain };
|
|
|
|
let name = p.parse_ident();
|
|
|
|
if p.token == token::Colon && p.look_ahead(1, |t| t.is_ident()) {
|
|
|
|
p.bump();
|
|
|
|
let kindp = match p.token { token::Ident(_, p) => p, _ => token::Plain };
|
|
|
|
let nt_kind = p.parse_ident();
|
|
|
|
let m = TtToken(sp, MatchNt(name, nt_kind, namep, kindp));
|
|
|
|
m
|
|
|
|
} else {
|
|
|
|
TtToken(sp, SubstNt(name, namep))
|
|
|
|
}
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
2012-06-27 22:29:35 +00:00
|
|
|
}
|
2013-02-04 21:15:17 +00:00
|
|
|
_ => {
|
2014-10-22 17:39:58 +00:00
|
|
|
TtToken(p.span, p.bump_and_get())
|
2013-02-04 21:15:17 +00:00
|
|
|
}
|
2012-05-21 17:45:56 +00:00
|
|
|
}
|
2013-02-04 21:15:17 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
match self.token {
|
|
|
|
token::Eof => {
|
2013-12-30 23:41:11 +00:00
|
|
|
let open_braces = self.open_braces.clone();
|
|
|
|
for sp in open_braces.iter() {
|
2014-10-18 02:39:44 +00:00
|
|
|
self.span_help(*sp, "did you mean to close this delimiter?");
|
2013-10-07 19:43:42 +00:00
|
|
|
}
|
|
|
|
// There shouldn't really be a span, but it's easier for the test runner
|
|
|
|
// if we give it one
|
2014-02-06 09:38:08 +00:00
|
|
|
self.fatal("this file contains an un-closed delimiter ");
|
2014-10-29 10:37:54 +00:00
|
|
|
},
|
|
|
|
token::OpenDelim(delim) => {
|
2014-10-22 05:37:20 +00:00
|
|
|
// The span for beginning of the delimited section
|
|
|
|
let pre_span = self.span;
|
|
|
|
|
2013-07-16 21:54:29 +00:00
|
|
|
// Parse the open delimiter.
|
2013-12-30 23:41:11 +00:00
|
|
|
self.open_braces.push(self.span);
|
2014-10-29 10:37:54 +00:00
|
|
|
let open_span = self.span;
|
|
|
|
self.bump();
|
2013-07-16 21:54:29 +00:00
|
|
|
|
2014-10-22 05:37:20 +00:00
|
|
|
// Parse the token trees within the delimeters
|
|
|
|
let tts = self.parse_seq_to_before_end(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::CloseDelim(delim),
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree()
|
2014-10-22 05:37:20 +00:00
|
|
|
);
|
2013-07-16 21:54:29 +00:00
|
|
|
|
|
|
|
// Parse the close delimiter.
|
2014-10-29 10:37:54 +00:00
|
|
|
let close_span = self.span;
|
|
|
|
self.bump();
|
2013-12-23 15:20:52 +00:00
|
|
|
self.open_braces.pop().unwrap();
|
2013-07-16 21:54:29 +00:00
|
|
|
|
2014-10-22 05:37:20 +00:00
|
|
|
// Expand to cover the entire delimited token tree
|
|
|
|
let span = Span { hi: self.span.hi, ..pre_span };
|
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
TtDelimited(span, Rc::new(Delimited {
|
|
|
|
delim: delim,
|
|
|
|
open_span: open_span,
|
|
|
|
tts: tts,
|
|
|
|
close_span: close_span,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
_ => parse_non_delim_tt_tok(self),
|
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.
|
2014-02-28 21:09:09 +00:00
|
|
|
pub fn parse_all_token_trees(&mut self) -> Vec<TokenTree> {
|
|
|
|
let mut tts = Vec::new();
|
2014-10-27 08:22:52 +00:00
|
|
|
while self.token != token::Eof {
|
2012-11-21 00:07:57 +00:00
|
|
|
tts.push(self.parse_token_tree());
|
|
|
|
}
|
2013-02-14 13:14:59 +00:00
|
|
|
tts
|
2012-11-21 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a prefix-operator expr
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_prefix_expr(&mut self) -> P<Expr> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-04-12 05:10:31 +00:00
|
|
|
let ex;
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Not => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-10-28 00:14:09 +00:00
|
|
|
let e = self.parse_prefix_expr();
|
2011-04-08 16:44:20 +00:00
|
|
|
hi = e.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = self.mk_unary(UnNot, e);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Minus) => {
|
2014-04-17 08:35:31 +00:00
|
|
|
self.bump();
|
|
|
|
let e = self.parse_prefix_expr();
|
|
|
|
hi = e.span.hi;
|
|
|
|
ex = self.mk_unary(UnNeg, e);
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Star) => {
|
2014-04-17 08:35:31 +00:00
|
|
|
self.bump();
|
|
|
|
let e = self.parse_prefix_expr();
|
|
|
|
hi = e.span.hi;
|
|
|
|
ex = self.mk_unary(UnDeref, e);
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::And) | token::AndAnd => {
|
2014-04-17 08:35:31 +00:00
|
|
|
self.expect_and();
|
|
|
|
let m = self.parse_mutability();
|
|
|
|
let e = self.parse_prefix_expr();
|
|
|
|
hi = e.span.hi;
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
ex = ExprAddrOf(m, e);
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Tilde => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-08-28 03:30:41 +00:00
|
|
|
let last_span = self.last_span;
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
match self.token {
|
2014-10-29 21:44:41 +00:00
|
|
|
token::OpenDelim(token::Bracket) => {
|
|
|
|
self.obsolete(last_span, ObsoleteOwnedVector)
|
|
|
|
},
|
2014-08-28 03:30:41 +00:00
|
|
|
_ => self.obsolete(last_span, ObsoleteOwnedExpr)
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
}
|
2013-02-26 19:32:00 +00:00
|
|
|
|
2012-10-28 00:14:09 +00:00
|
|
|
let e = self.parse_prefix_expr();
|
2012-03-08 20:12:04 +00:00
|
|
|
hi = e.span.hi;
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
ex = self.mk_unary(UnUniq, e);
|
2012-03-08 20:12:04 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(_, _) => {
|
2014-10-27 12:33:30 +00:00
|
|
|
if !self.token.is_keyword(keywords::Box) {
|
2014-08-06 09:59:40 +00:00
|
|
|
return self.parse_dot_or_call_expr();
|
|
|
|
}
|
2013-12-12 18:42:03 +00:00
|
|
|
|
2014-10-25 23:07:54 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
2014-08-06 09:59:40 +00:00
|
|
|
self.bump();
|
|
|
|
|
|
|
|
// Check for a place: `box(PLACE) EXPR`.
|
2014-10-29 10:37:54 +00:00
|
|
|
if self.eat(&token::OpenDelim(token::Paren)) {
|
2014-08-06 09:59:40 +00:00
|
|
|
// Support `box() EXPR` as the default.
|
2014-10-29 10:37:54 +00:00
|
|
|
if !self.eat(&token::CloseDelim(token::Paren)) {
|
2014-08-06 09:59:40 +00:00
|
|
|
let place = self.parse_expr();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
2014-10-25 23:07:54 +00:00
|
|
|
// Give a suggestion to use `box()` when a parenthesised expression is used
|
|
|
|
if !self.token.can_begin_expr() {
|
|
|
|
let span = self.span;
|
|
|
|
let this_token_to_string = self.this_token_to_string();
|
|
|
|
self.span_err(span,
|
|
|
|
format!("expected expression, found `{}`",
|
|
|
|
this_token_to_string).as_slice());
|
|
|
|
let box_span = mk_sp(lo, self.last_span.hi);
|
|
|
|
self.span_help(box_span,
|
|
|
|
"perhaps you meant `box() (foo)` instead?");
|
|
|
|
self.abort_if_errors();
|
|
|
|
}
|
2014-08-06 09:59:40 +00:00
|
|
|
let subexpression = self.parse_prefix_expr();
|
|
|
|
hi = subexpression.span.hi;
|
2014-12-16 13:30:30 +00:00
|
|
|
ex = ExprBox(Some(place), subexpression);
|
2014-08-06 09:59:40 +00:00
|
|
|
return self.mk_expr(lo, hi, ex);
|
2013-12-18 00:46:18 +00:00
|
|
|
}
|
2014-08-06 09:59:40 +00:00
|
|
|
}
|
2013-12-18 00:46:18 +00:00
|
|
|
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
// Otherwise, we use the unique pointer default.
|
|
|
|
let subexpression = self.parse_prefix_expr();
|
|
|
|
hi = subexpression.span.hi;
|
2014-12-16 13:30:30 +00:00
|
|
|
// FIXME (pnkfelix): After working out kinks with box
|
|
|
|
// desugaring, should be `ExprBox(None, subexpression)`
|
|
|
|
// instead.
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
|
|
|
ex = self.mk_unary(UnUniq, subexpression);
|
2013-12-12 18:42:03 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => return self.parse_dot_or_call_expr()
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.mk_expr(lo, hi, ex);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse an expression of binops
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_binops(&mut self) -> P<Expr> {
|
2013-12-30 22:04:00 +00:00
|
|
|
let prefix_expr = self.parse_prefix_expr();
|
|
|
|
self.parse_more_binops(prefix_expr, 0)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse an expression of binops of at least min_prec precedence
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: uint) -> P<Expr> {
|
|
|
|
if self.expr_is_complete(&*lhs) { return lhs; }
|
2013-07-18 03:04:37 +00:00
|
|
|
|
|
|
|
// Prevent dynamic borrow errors later on by limiting the
|
|
|
|
// scope of the borrows.
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.token == token::BinOp(token::Or) &&
|
2014-10-06 03:08:35 +00:00
|
|
|
self.restrictions.contains(RESTRICTION_NO_BAR_OP) {
|
2014-09-16 05:22:12 +00:00
|
|
|
return lhs;
|
2013-07-18 03:04:37 +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
|
|
|
self.expected_tokens.push(TokenType::Operator);
|
2013-07-18 03:04:37 +00:00
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
let cur_opt = self.token.to_binop();
|
2013-07-18 03:04:37 +00:00
|
|
|
match cur_opt {
|
|
|
|
Some(cur_op) => {
|
|
|
|
let cur_prec = operator_prec(cur_op);
|
|
|
|
if cur_prec > min_prec {
|
|
|
|
self.bump();
|
|
|
|
let expr = self.parse_prefix_expr();
|
|
|
|
let rhs = self.parse_more_binops(expr, cur_prec);
|
2014-09-13 16:06:01 +00:00
|
|
|
let lhs_span = lhs.span;
|
|
|
|
let rhs_span = rhs.span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let binary = self.mk_binary(cur_op, lhs, rhs);
|
2014-09-13 16:06:01 +00:00
|
|
|
let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary);
|
2013-07-18 03:04:37 +00:00
|
|
|
self.parse_more_binops(bin, min_prec)
|
|
|
|
} else {
|
|
|
|
lhs
|
2013-01-31 18:32:57 +00:00
|
|
|
}
|
2013-07-18 03:04:37 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if as_prec > min_prec && self.eat_keyword(keywords::As) {
|
2014-11-20 20:05:29 +00:00
|
|
|
let rhs = self.parse_ty();
|
2013-07-18 03:04:37 +00:00
|
|
|
let _as = self.mk_expr(lhs.span.lo,
|
|
|
|
rhs.span.hi,
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprCast(lhs, rhs));
|
2013-07-18 03:04:37 +00:00
|
|
|
self.parse_more_binops(_as, min_prec)
|
|
|
|
} else {
|
|
|
|
lhs
|
2013-01-31 18:32:57 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-28 17:30:34 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse an assignment expression....
|
|
|
|
/// actually, this seems to be the main entry point for
|
|
|
|
/// parsing an arbitrary expression.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_assign_expr(&mut self) -> P<Expr> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let lhs = self.parse_binops();
|
2014-10-06 03:08:35 +00:00
|
|
|
let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL;
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Eq => {
|
2013-03-28 23:31:12 +00:00
|
|
|
self.bump();
|
2014-09-16 02:06:27 +00:00
|
|
|
let rhs = self.parse_expr_res(restrictions);
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOpEq(op) => {
|
2013-01-31 18:32:57 +00:00
|
|
|
self.bump();
|
2014-09-16 02:06:27 +00:00
|
|
|
let rhs = self.parse_expr_res(restrictions);
|
2013-07-18 03:04:37 +00:00
|
|
|
let aop = match op {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Plus => BiAdd,
|
|
|
|
token::Minus => BiSub,
|
|
|
|
token::Star => BiMul,
|
|
|
|
token::Slash => BiDiv,
|
|
|
|
token::Percent => BiRem,
|
|
|
|
token::Caret => BiBitXor,
|
|
|
|
token::And => BiBitAnd,
|
|
|
|
token::Or => BiBitOr,
|
|
|
|
token::Shl => BiShl,
|
|
|
|
token::Shr => BiShr
|
2013-07-18 03:04:37 +00:00
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
let rhs_span = rhs.span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let assign_op = self.mk_assign_op(aop, lhs, rhs);
|
2014-09-13 16:06:01 +00:00
|
|
|
self.mk_expr(lo, rhs_span.hi, assign_op)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-01-31 18:32:57 +00:00
|
|
|
_ => {
|
|
|
|
lhs
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 01:04:29 +00:00
|
|
|
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_if_expr(&mut self) -> P<Expr> {
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Let) {
|
2014-08-25 01:04:29 +00:00
|
|
|
return self.parse_if_let_expr();
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
2014-10-06 03:08:35 +00:00
|
|
|
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
2012-05-23 22:06:11 +00:00
|
|
|
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;
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Else) {
|
2012-05-23 22:06:11 +00:00
|
|
|
let elexpr = self.parse_else_expr();
|
|
|
|
hi = elexpr.span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
els = Some(elexpr);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, hi, ExprIf(cond, thn, els))
|
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)
|
|
|
|
pub fn parse_if_let_expr(&mut self) -> P<Expr> {
|
|
|
|
let lo = self.last_span.lo;
|
|
|
|
self.expect_keyword(keywords::Let);
|
|
|
|
let pat = self.parse_pat();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Eq);
|
2014-10-06 03:08:35 +00:00
|
|
|
let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
2014-08-25 01:04:29 +00:00
|
|
|
let thn = self.parse_block();
|
|
|
|
let (hi, els) = if self.eat_keyword(keywords::Else) {
|
|
|
|
let expr = self.parse_else_expr();
|
|
|
|
(expr.span.hi, Some(expr))
|
|
|
|
} else {
|
|
|
|
(thn.span.hi, None)
|
|
|
|
};
|
|
|
|
self.mk_expr(lo, hi, ExprIfLet(pat, expr, thn, els))
|
|
|
|
}
|
|
|
|
|
2014-05-29 05:26:56 +00:00
|
|
|
// `|args| expr`
|
2014-07-23 19:43:29 +00:00
|
|
|
pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
|
2014-11-19 16:18:17 +00:00
|
|
|
-> P<Expr>
|
|
|
|
{
|
2014-02-07 10:52:12 +00:00
|
|
|
let lo = self.span.lo;
|
2014-07-30 05:08:39 +00:00
|
|
|
let (decl, optional_unboxed_closure_kind) =
|
|
|
|
self.parse_fn_block_decl();
|
2014-05-29 05:26:56 +00:00
|
|
|
let body = self.parse_expr();
|
2013-11-30 22:00:39 +00:00
|
|
|
let fakeblock = P(ast::Block {
|
2014-09-13 16:06:01 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-02-28 21:09:09 +00:00
|
|
|
view_items: Vec::new(),
|
|
|
|
stmts: Vec::new(),
|
2014-09-13 16:06:01 +00:00
|
|
|
span: body.span,
|
2013-01-15 03:35:08 +00:00
|
|
|
expr: Some(body),
|
2013-07-27 08:25:59 +00:00
|
|
|
rules: DefaultBlock,
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2013-07-16 18:08:35 +00:00
|
|
|
|
2014-11-19 16:18:17 +00:00
|
|
|
self.mk_expr(
|
|
|
|
lo,
|
|
|
|
fakeblock.span.hi,
|
|
|
|
ExprClosure(capture_clause, optional_unboxed_closure_kind, decl, fakeblock))
|
2012-06-30 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_else_expr(&mut self) -> P<Expr> {
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::If) {
|
2012-08-02 00:30:05 +00:00
|
|
|
return self.parse_if_expr();
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
|
|
|
let blk = self.parse_block();
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
|
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)
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> 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
|
|
|
|
|
|
|
let lo = self.last_span.lo;
|
|
|
|
let pat = self.parse_pat();
|
|
|
|
self.expect_keyword(keywords::In);
|
2014-10-06 03:08:35 +00:00
|
|
|
let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
2013-07-30 00:25:00 +00:00
|
|
|
let loop_block = self.parse_block();
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
2013-09-08 12:08:01 +00:00
|
|
|
self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident))
|
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)
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Let) {
|
2014-10-03 02:45:46 +00:00
|
|
|
return self.parse_while_let_expr(opt_ident);
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
2014-10-06 03:08:35 +00:00
|
|
|
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
2013-04-06 00:31:52 +00:00
|
|
|
let body = self.parse_block();
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = body.span.hi;
|
2014-07-26 00:12:51 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
|
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)
|
|
|
|
pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
|
|
|
|
let lo = self.last_span.lo;
|
|
|
|
self.expect_keyword(keywords::Let);
|
|
|
|
let pat = self.parse_pat();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Eq);
|
2014-10-03 02:45:46 +00:00
|
|
|
let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
|
|
|
let body = self.parse_block();
|
|
|
|
let hi = body.span.hi;
|
|
|
|
return self.mk_expr(lo, hi, ExprWhileLet(pat, expr, body, opt_ident));
|
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
|
2014-05-22 17:49:26 +00:00
|
|
|
let lo = self.last_span.lo;
|
|
|
|
let body = self.parse_block();
|
|
|
|
let hi = body.span.hi;
|
|
|
|
self.mk_expr(lo, hi, ExprLoop(body, opt_ident))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_match_expr(&mut self) -> P<Expr> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
2014-10-06 03:08:35 +00:00
|
|
|
let discriminant = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
|
2014-10-29 10:37:54 +00:00
|
|
|
self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace));
|
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) {
|
2014-07-29 00:32:51 +00:00
|
|
|
arms.push(self.parse_arm());
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-08-25 21:55:00 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchNormal));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-11-24 22:42:01 +00:00
|
|
|
|
2014-07-29 00:32:51 +00:00
|
|
|
pub fn parse_arm(&mut self) -> Arm {
|
|
|
|
let attrs = self.parse_outer_attributes();
|
|
|
|
let pats = self.parse_pats();
|
|
|
|
let mut guard = None;
|
|
|
|
if self.eat_keyword(keywords::If) {
|
|
|
|
guard = Some(self.parse_expr());
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::FatArrow);
|
2014-10-06 03:08:35 +00:00
|
|
|
let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR);
|
2014-07-29 00:32:51 +00:00
|
|
|
|
|
|
|
let require_comma =
|
2014-09-13 16:06:01 +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 {
|
2014-10-29 10:37:54 +00:00
|
|
|
self.commit_expr(&*expr, &[token::Comma], &[token::CloseDelim(token::Brace)]);
|
2014-07-29 00:32:51 +00:00
|
|
|
} else {
|
2014-10-27 08:22:52 +00:00
|
|
|
self.eat(&token::Comma);
|
2014-07-29 00:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ast::Arm {
|
|
|
|
attrs: attrs,
|
|
|
|
pats: pats,
|
|
|
|
guard: guard,
|
|
|
|
body: expr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse an expression
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_expr(&mut self) -> P<Expr> {
|
2014-10-06 03:08:35 +00:00
|
|
|
return self.parse_expr_res(UNRESTRICTED);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
2014-09-16 05:22:12 +00:00
|
|
|
/// Parse an expression, subject to the given restrictions
|
|
|
|
pub fn parse_expr_res(&mut self, r: Restrictions) -> P<Expr> {
|
|
|
|
let old = self.restrictions;
|
|
|
|
self.restrictions = r;
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_assign_expr();
|
2014-09-16 05:22:12 +00:00
|
|
|
self.restrictions = old;
|
2012-08-02 00:30:05 +00:00
|
|
|
return e;
|
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;')
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_initializer(&mut self) -> 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) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-10-03 09:53:46 +00:00
|
|
|
Some(self.parse_expr())
|
|
|
|
} else {
|
|
|
|
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
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_pats(&mut self) -> 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 {
|
2013-05-29 23:59:33 +00:00
|
|
|
pats.push(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 self.check(&token::BinOp(token::Or)) { self.bump(); }
|
2012-08-02 00:30:05 +00:00
|
|
|
else { return pats; }
|
2012-05-23 22:06:11 +00:00
|
|
|
};
|
|
|
|
}
|
2011-07-08 14:27:55 +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,
|
2014-09-13 16:06:01 +00:00
|
|
|
) -> (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 {
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Comma);
|
2014-11-30 04:39:50 +00:00
|
|
|
|
|
|
|
if self.token == token::CloseDelim(token::Bracket)
|
|
|
|
&& (before_slice || after.len() != 0) {
|
|
|
|
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) {
|
2013-02-26 18:58:46 +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,
|
|
|
|
node: PatWild(PatWildMulti),
|
|
|
|
span: self.span,
|
2014-09-13 16:06:01 +00:00
|
|
|
}));
|
2014-09-06 22:23:55 +00:00
|
|
|
before_slice = false;
|
|
|
|
} else {
|
|
|
|
let _ = self.parse_pat();
|
|
|
|
let span = self.span;
|
|
|
|
self.obsolete(span, ObsoleteSubsliceMatch);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-09-06 22:23:55 +00:00
|
|
|
self.bump();
|
|
|
|
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
|
|
|
|
|
|
|
(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
|
2014-10-06 00:36:53 +00:00
|
|
|
fn parse_pat_fields(&mut self) -> (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 {
|
2014-10-27 08:22:52 +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
|
|
|
|
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) {
|
2012-08-07 00:01:14 +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();
|
2014-05-28 16:24:28 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`", "}",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
|
|
|
etc = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-06 08:15:02 +00:00
|
|
|
let bind_type = if self.eat_keyword(keywords::Mut) {
|
|
|
|
BindByValue(MutMutable)
|
|
|
|
} else if self.eat_keyword(keywords::Ref) {
|
|
|
|
BindByRef(self.parse_mutability())
|
|
|
|
} else {
|
|
|
|
BindByValue(MutImmutable)
|
|
|
|
};
|
|
|
|
|
2013-03-06 17:30:54 +00:00
|
|
|
let fieldname = self.parse_ident();
|
2014-03-12 22:51:20 +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
|
|
|
let (subpat, is_shorthand) = if self.check(&token::Colon) {
|
2013-12-06 08:15:02 +00:00
|
|
|
match bind_type {
|
2013-12-30 22:04:00 +00:00
|
|
|
BindByRef(..) | BindByValue(MutMutable) => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(format!("unexpected `{}`",
|
|
|
|
token_str).as_slice())
|
2013-12-30 22:04:00 +00:00
|
|
|
}
|
2013-12-06 08:15:02 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:01:14 +00:00
|
|
|
self.bump();
|
2014-10-06 00:36:53 +00:00
|
|
|
let pat = self.parse_pat();
|
|
|
|
hi = pat.span.hi;
|
|
|
|
(pat, false)
|
2012-08-07 00:01:14 +00:00
|
|
|
} else {
|
2014-10-06 00:36:53 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-07-01 01:02:14 +00:00
|
|
|
let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname};
|
2014-10-06 00:36:53 +00:00
|
|
|
(P(ast::Pat {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-12-06 08:15:02 +00:00
|
|
|
node: PatIdent(bind_type, fieldpath, None),
|
2013-12-30 23:30:14 +00:00
|
|
|
span: self.last_span
|
2014-10-06 00:36:53 +00:00
|
|
|
}), true)
|
2014-03-12 22:51:20 +00:00
|
|
|
};
|
2014-10-06 00:36:53 +00:00
|
|
|
fields.push(codemap::Spanned { span: mk_sp(lo, hi),
|
|
|
|
node: ast::FieldPat { ident: fieldname,
|
|
|
|
pat: subpat,
|
|
|
|
is_shorthand: is_shorthand }});
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
|
|
|
return (fields, etc);
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a pattern.
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_pat(&mut self) -> P<Pat> {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(self, NtPat);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-07-02 19:47:32 +00:00
|
|
|
let mut hi;
|
2013-04-12 05:10:31 +00:00
|
|
|
let pat;
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse _
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Underscore => {
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
2014-08-06 15:04:44 +00:00
|
|
|
pat = PatWild(PatWildSingle);
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
return 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)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Tilde => {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse ~pat
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-05-29 23:59:33 +00:00
|
|
|
let sub = self.parse_pat();
|
2014-05-27 00:55:54 +00:00
|
|
|
pat = PatBox(sub);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
hi = last_span.hi;
|
|
|
|
self.obsolete(last_span, ObsoleteOwnedPattern);
|
2014-09-13 16:06:01 +00:00
|
|
|
return 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)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::And) | token::AndAnd => {
|
2014-04-30 23:49:12 +00:00
|
|
|
// parse &pat
|
|
|
|
let lo = self.span.lo;
|
|
|
|
self.expect_and();
|
|
|
|
let sub = self.parse_pat();
|
|
|
|
pat = PatRegion(sub);
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
return 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)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-09-08 00:07:32 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse (pat,pat,pat,...) as tuple
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
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::Paren)) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-11-09 15:14:15 +00:00
|
|
|
pat = PatTup(vec![]);
|
2011-07-27 12:19:39 +00:00
|
|
|
} else {
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut fields = vec!(self.parse_pat());
|
2014-10-29 10:37:54 +00:00
|
|
|
if self.look_ahead(1, |t| *t != 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
|
|
|
while self.check(&token::Comma) {
|
2013-02-17 23:41:47 +00:00
|
|
|
self.bump();
|
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::Paren)) { break; }
|
2013-05-29 23:59:33 +00:00
|
|
|
fields.push(self.parse_pat());
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
if fields.len() == 1 { self.expect(&token::Comma); }
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatTup(fields);
|
2011-08-15 11:15:19 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
return 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)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Bracket) => {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse [pat,pat,...] as vector pattern
|
2012-12-08 20:22:43 +00:00
|
|
|
self.bump();
|
2013-02-26 18:58:46 +00:00
|
|
|
let (before, slice, after) =
|
2013-05-29 23:59:33 +00:00
|
|
|
self.parse_pat_vec_elements();
|
2013-07-18 03:04:37 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Bracket));
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = ast::PatVec(before, slice, after);
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
return 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)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-12-08 20:22:43 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2014-07-01 01:02:14 +00:00
|
|
|
// at this point, token != _, ~, &, &&, (, [
|
2013-07-02 19:47:32 +00:00
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
if (!(self.token.is_ident() || self.token.is_path())
|
|
|
|
&& self.token != token::ModSep)
|
|
|
|
|| self.token.is_keyword(keywords::True)
|
|
|
|
|| self.token.is_keyword(keywords::False) {
|
2013-07-02 19:47:32 +00:00
|
|
|
// Parse an expression pattern or exp .. exp.
|
|
|
|
//
|
|
|
|
// These expressions are limited to literals (possibly
|
|
|
|
// preceded by unary-minus) or identifiers.
|
|
|
|
let val = self.parse_literal_maybe_minus();
|
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::DotDotDot)) &&
|
2014-09-06 22:23:55 +00:00
|
|
|
self.look_ahead(1, |t| {
|
2014-10-29 10:37:54 +00:00
|
|
|
*t != token::Comma && *t != token::CloseDelim(token::Bracket)
|
2014-09-06 22:23:55 +00:00
|
|
|
}) {
|
|
|
|
self.bump();
|
2014-10-27 12:33:30 +00:00
|
|
|
let end = if self.token.is_ident() || self.token.is_path() {
|
2014-11-20 20:05:29 +00:00
|
|
|
let path = self.parse_path(LifetimeAndTypesWithColons);
|
2013-07-02 19:47:32 +00:00
|
|
|
let hi = self.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, hi, ExprPath(path))
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2013-07-02 19:47:32 +00:00
|
|
|
self.parse_literal_maybe_minus()
|
|
|
|
};
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatRange(val, end);
|
2011-09-28 19:07:33 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatLit(val);
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2013-10-20 12:31:23 +00:00
|
|
|
} else if self.eat_keyword(keywords::Mut) {
|
|
|
|
pat = self.parse_pat_ident(BindByValue(MutMutable));
|
2013-07-02 19:47:32 +00:00
|
|
|
} else if self.eat_keyword(keywords::Ref) {
|
|
|
|
// parse ref pat
|
|
|
|
let mutbl = self.parse_mutability();
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = self.parse_pat_ident(BindByRef(mutbl));
|
2014-05-03 00:21:36 +00:00
|
|
|
} else if self.eat_keyword(keywords::Box) {
|
|
|
|
// `box PAT`
|
|
|
|
//
|
|
|
|
// FIXME(#13910): Rename to `PatBox` and extend to full DST
|
|
|
|
// support.
|
|
|
|
let sub = self.parse_pat();
|
2014-05-27 00:55:54 +00:00
|
|
|
pat = PatBox(sub);
|
2014-05-03 00:21:36 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
return P(ast::Pat {
|
2014-05-03 00:21:36 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: pat,
|
|
|
|
span: mk_sp(lo, hi)
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
2013-11-21 00:23:04 +00:00
|
|
|
let can_be_enum_or_struct = self.look_ahead(1, |t| {
|
2013-07-02 19:47:32 +00:00
|
|
|
match *t {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(_) | token::Lt | token::ModSep => true,
|
2013-07-02 19:47:32 +00:00
|
|
|
_ => false,
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2012-08-07 00:01:14 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.look_ahead(1, |t| *t == token::DotDotDot) &&
|
2014-09-06 22:23:55 +00:00
|
|
|
self.look_ahead(2, |t| {
|
2014-10-29 10:37:54 +00:00
|
|
|
*t != token::Comma && *t != token::CloseDelim(token::Bracket)
|
2014-09-06 22:23:55 +00:00
|
|
|
}) {
|
2014-10-06 03:08:35 +00:00
|
|
|
let start = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.eat(&token::DotDotDot);
|
2014-10-06 03:08:35 +00:00
|
|
|
let end = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatRange(start, end);
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if self.token.is_plain_ident() && !can_be_enum_or_struct {
|
2014-07-01 01:02:14 +00:00
|
|
|
let id = self.parse_ident();
|
|
|
|
let id_span = self.last_span;
|
|
|
|
let pth1 = codemap::Spanned{span:id_span, node: id};
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Not) {
|
2014-05-19 22:03:48 +00:00
|
|
|
// macro invocation
|
2014-10-29 14:47:53 +00:00
|
|
|
let delim = self.expect_open_delim();
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
2014-05-19 22:03:48 +00:00
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
|
|
|
|
2014-07-01 01:02:14 +00:00
|
|
|
let mac = MacInvocTT(ident_to_path(id_span,id), tts, EMPTY_CTXT);
|
2014-05-19 22:03:48 +00:00
|
|
|
pat = ast::PatMac(codemap::Spanned {node: mac, span: self.span});
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
2014-10-27 08:22:52 +00:00
|
|
|
let sub = if self.eat(&token::At) {
|
2014-05-19 22:03:48 +00:00
|
|
|
// parse foo @ pat
|
|
|
|
Some(self.parse_pat())
|
|
|
|
} else {
|
|
|
|
// or just foo
|
|
|
|
None
|
|
|
|
};
|
2014-07-01 01:02:14 +00:00
|
|
|
pat = PatIdent(BindByValue(MutImmutable), pth1, sub);
|
2013-05-05 01:57:14 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
|
|
|
// parse an enum pat
|
2014-11-20 20:05:29 +00:00
|
|
|
let enum_path = self.parse_path(LifetimeAndTypesWithColons);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Brace) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
let (fields, etc) =
|
|
|
|
self.parse_pat_fields();
|
|
|
|
self.bump();
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatStruct(enum_path, fields, etc);
|
2013-05-05 01:57:14 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
_ => {
|
2014-09-13 16:06:01 +00:00
|
|
|
let mut args: Vec<P<Pat>> = Vec::new();
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) => {
|
2013-11-21 00:23:04 +00:00
|
|
|
let is_dotdot = self.look_ahead(1, |t| {
|
2013-11-02 01:02:30 +00:00
|
|
|
match *t {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::DotDot => true,
|
2013-11-02 01:02:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2014-05-22 17:49:26 +00:00
|
|
|
if is_dotdot {
|
2013-07-02 19:47:32 +00:00
|
|
|
// This is a "top constructor only" pat
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatEnum(enum_path, None);
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
2014-03-16 15:07:39 +00:00
|
|
|
args = self.parse_enum_variant_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-07-02 19:47:32 +00:00
|
|
|
|p| p.parse_pat()
|
|
|
|
);
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatEnum(enum_path, Some(args));
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2014-08-12 17:33:16 +00:00
|
|
|
if !enum_path.global &&
|
2014-11-04 02:52:52 +00:00
|
|
|
enum_path.segments.len() == 1 &&
|
|
|
|
enum_path.segments[0].parameters.is_empty()
|
|
|
|
{
|
2013-07-02 19:47:32 +00:00
|
|
|
// it could still be either an enum
|
|
|
|
// or an identifier pattern, resolve
|
|
|
|
// will sort it out:
|
2013-10-20 12:31:23 +00:00
|
|
|
pat = PatIdent(BindByValue(MutImmutable),
|
2014-07-01 01:02:14 +00:00
|
|
|
codemap::Spanned{
|
|
|
|
span: enum_path.span,
|
2014-10-15 06:05:01 +00:00
|
|
|
node: enum_path.segments[0]
|
2014-07-01 01:02:14 +00:00
|
|
|
.identifier},
|
|
|
|
None);
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatEnum(enum_path, Some(args));
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-20 07:54:42 +00:00
|
|
|
}
|
2012-01-15 00:05:07 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
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),
|
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 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)
|
|
|
|
-> ast::Pat_ {
|
2014-10-27 12:33:30 +00:00
|
|
|
if !self.token.is_plain_ident() {
|
2014-09-13 02:09:22 +00:00
|
|
|
let span = self.span;
|
|
|
|
let tok_str = self.this_token_to_string();
|
|
|
|
self.span_fatal(span,
|
|
|
|
format!("expected identifier, found `{}`", tok_str).as_slice());
|
2012-08-06 14:20:23 +00:00
|
|
|
}
|
2014-07-04 20:46:39 +00:00
|
|
|
let ident = self.parse_ident();
|
|
|
|
let last_span = self.last_span;
|
|
|
|
let name = codemap::Spanned{span: last_span, node: ident};
|
2014-10-27 08:22:52 +00:00
|
|
|
let sub = if self.eat(&token::At) {
|
2013-05-29 23:59:33 +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) {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2012-08-06 14:20:23 +00:00
|
|
|
self.span_fatal(
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span,
|
2013-05-02 16:28:53 +00:00
|
|
|
"expected identifier, found enum pattern");
|
2012-08-06 14:20:23 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
PatIdent(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
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_local(&mut self) -> P<Local> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-05-29 23:59:33 +00:00
|
|
|
let pat = self.parse_pat();
|
2013-06-07 01:54:14 +00:00
|
|
|
|
2013-11-30 22:00:39 +00:00
|
|
|
let mut ty = P(Ty {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: TyInfer,
|
2013-01-15 22:59:39 +00:00
|
|
|
span: mk_sp(lo, lo),
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2014-11-20 20:05:29 +00:00
|
|
|
ty = self.parse_ty_sum();
|
2014-06-11 19:14:38 +00:00
|
|
|
}
|
2013-04-15 23:31:57 +00:00
|
|
|
let init = self.parse_initializer();
|
2014-09-13 16:06:01 +00:00
|
|
|
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,
|
2013-07-19 05:38:55 +00:00
|
|
|
span: mk_sp(lo, self.last_span.hi),
|
2014-05-26 12:00:08 +00:00
|
|
|
source: LocalLet,
|
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 a "let" stmt
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_let(&mut self) -> P<Decl> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-10-20 12:31:23 +00:00
|
|
|
let local = self.parse_local();
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, self.last_span.hi, DeclLocal(local)))
|
2010-11-24 22:42:01 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a structure field
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_name_and_ty(&mut self, pr: Visibility,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> ) -> StructField {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2014-10-27 12:33:30 +00:00
|
|
|
if !self.token.is_plain_ident() {
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("expected ident");
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-05-24 19:38:45 +00:00
|
|
|
let name = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2014-01-09 13:05:33 +00:00
|
|
|
spanned(lo, self.last_span.hi, ast::StructField_ {
|
|
|
|
kind: NamedField(name, pr),
|
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,
|
2013-01-13 23:28:49 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 01:07:07 +00:00
|
|
|
/// Get an expected item after attributes error message.
|
|
|
|
fn expected_item_err(attrs: &[Attribute]) -> &'static str {
|
|
|
|
match attrs.last() {
|
|
|
|
Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
|
|
|
|
"expected item after doc comment"
|
|
|
|
}
|
|
|
|
_ => "expected item after attributes",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a statement. may include decl.
|
|
|
|
/// Precondition: any attributes are parsed already
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> P<Stmt> {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(self, NtStmt);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2014-09-12 01:07:07 +00:00
|
|
|
fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) {
|
2012-05-23 22:06:11 +00:00
|
|
|
// If we have attributes then we should have an item
|
2014-09-12 01:07:07 +00:00
|
|
|
if !attrs.is_empty() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = p.last_span;
|
2014-09-12 01:07:07 +00:00
|
|
|
p.span_err(last_span, Parser::expected_item_err(attrs));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-19 01:19:16 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Let) {
|
2014-09-12 01:07:07 +00:00
|
|
|
check_expected_item(self, item_attrs.as_slice());
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Let);
|
2012-05-23 22:06:11 +00:00
|
|
|
let decl = self.parse_let();
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if self.token.is_ident()
|
|
|
|
&& !self.token.is_any_keyword()
|
2014-10-27 08:22:52 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::Not) {
|
2014-07-07 22:15:31 +00:00
|
|
|
// it's a macro invocation:
|
2013-05-08 22:27:29 +00:00
|
|
|
|
2014-09-12 01:07:07 +00:00
|
|
|
check_expected_item(self, item_attrs.as_slice());
|
2012-11-22 03:38:27 +00:00
|
|
|
|
2012-11-14 04:45:25 +00:00
|
|
|
// Potential trouble: if we allow macros with paths instead of
|
|
|
|
// idents, we'd need to look ahead past the whole path here...
|
2014-11-20 20:05:29 +00:00
|
|
|
let pth = self.parse_path(NoTypesAllowed);
|
2012-11-14 04:45:25 +00:00
|
|
|
self.bump();
|
|
|
|
|
2014-10-29 14:47:53 +00:00
|
|
|
let id = match self.token {
|
|
|
|
token::OpenDelim(_) => token::special_idents::invalid, // no special identifier
|
|
|
|
_ => 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.
|
2014-07-02 05:11:47 +00:00
|
|
|
let ident_str = if id.name == token::special_idents::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();
|
2014-08-23 10:41:32 +00:00
|
|
|
self.fatal(format!("expected {}`(` or `{{`, found `{}`",
|
2014-05-28 16:24:28 +00:00
|
|
|
ident_str,
|
|
|
|
tok_str).as_slice())
|
2014-10-29 14:47:53 +00:00
|
|
|
},
|
2014-02-23 13:53:59 +00:00
|
|
|
};
|
|
|
|
|
2012-11-14 04:45:25 +00:00
|
|
|
let tts = self.parse_unspanned_seq(
|
2014-10-29 14:47:53 +00:00
|
|
|
&token::OpenDelim(delim),
|
|
|
|
&token::CloseDelim(delim),
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree()
|
|
|
|
);
|
2012-11-14 04:45:25 +00:00
|
|
|
let hi = self.span.hi;
|
|
|
|
|
2014-07-02 05:11:47 +00:00
|
|
|
if id.name == token::special_idents::invalid.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
|
|
|
if self.check(&token::Dot) {
|
2014-10-27 04:43:38 +00:00
|
|
|
let span = self.span;
|
|
|
|
let token_string = self.this_token_to_string();
|
|
|
|
self.span_err(span,
|
|
|
|
format!("expected statement, found `{}`",
|
|
|
|
token_string).as_slice());
|
|
|
|
let mac_span = mk_sp(lo, hi);
|
|
|
|
self.span_help(mac_span, "try parenthesizing this macro invocation");
|
|
|
|
self.abort_if_errors();
|
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, hi, StmtMac(
|
|
|
|
spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)))
|
2012-11-14 04:45:25 +00:00
|
|
|
} else {
|
|
|
|
// if it has a special ident, it's definitely an item
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, hi, StmtDecl(
|
|
|
|
P(spanned(lo, hi, DeclItem(
|
2012-11-14 04:45:25 +00:00
|
|
|
self.mk_item(
|
|
|
|
lo, hi, id /*id is good here*/,
|
2014-01-09 13:05:33 +00:00
|
|
|
ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))),
|
2014-09-13 16:06:01 +00:00
|
|
|
Inherited, Vec::new(/*no attrs*/))))),
|
|
|
|
ast::DUMMY_NODE_ID)))
|
2012-11-14 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2013-07-02 19:47:32 +00:00
|
|
|
let found_attrs = !item_attrs.is_empty();
|
2014-09-12 01:07:07 +00:00
|
|
|
let item_err = Parser::expected_item_err(item_attrs.as_slice());
|
2013-07-02 19:47:32 +00:00
|
|
|
match self.parse_item_or_view_item(item_attrs, false) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(i) => {
|
2013-05-02 16:28:53 +00:00
|
|
|
let hi = i.span.hi;
|
2014-09-13 16:06:01 +00:00
|
|
|
let decl = P(spanned(lo, hi, DeclItem(i)));
|
|
|
|
P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
|
2013-05-02 16:28:53 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(vi) => {
|
2013-05-02 16:28:53 +00:00
|
|
|
self.span_fatal(vi.span,
|
|
|
|
"view items must be declared at the top of the block");
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviForeignItem(_) => {
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("foreign items are not allowed here");
|
2013-05-02 16:28:53 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
IoviNone(_) => {
|
2014-09-12 01:07:07 +00:00
|
|
|
if found_attrs {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span, item_err);
|
|
|
|
}
|
2011-06-15 20:27:39 +00:00
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
// Remainder are line-expr stmts.
|
2014-10-06 03:08:35 +00:00
|
|
|
let e = self.parse_expr_res(RESTRICTION_STMT_EXPR);
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
|
|
|
|
}
|
|
|
|
}
|
2010-10-04 22:55:12 +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 {
|
2014-10-06 03:08:35 +00:00
|
|
|
self.restrictions.contains(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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_block(&mut self) -> P<Block> {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(no_clone self, NtBlock);
|
2013-04-06 00:31:52 +00:00
|
|
|
|
|
|
|
let lo = self.span.lo;
|
2014-11-10 10:58:03 +00:00
|
|
|
|
|
|
|
if !self.eat(&token::OpenDelim(token::Brace)) {
|
|
|
|
let sp = self.span;
|
|
|
|
let tok = self.this_token_to_string();
|
|
|
|
self.span_fatal_help(sp,
|
|
|
|
format!("expected `{{`, found `{}`", tok).as_slice(),
|
|
|
|
"place this code inside a block");
|
|
|
|
}
|
2013-04-06 00:31:52 +00:00
|
|
|
|
2014-02-28 21:09:09 +00:00
|
|
|
return self.parse_block_tail_(lo, DefaultBlock, Vec::new());
|
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.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_inner_attrs_and_block(&mut self)
|
2014-02-28 21:09:09 +00:00
|
|
|
-> (Vec<Attribute> , P<Block>) {
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(pair_empty self, NtBlock);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2013-04-06 00:31:52 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
2013-02-25 04:51:56 +00:00
|
|
|
|
2013-07-27 08:25:59 +00:00
|
|
|
(inner, self.parse_block_tail_(lo, DefaultBlock, next))
|
2012-01-16 01:23:59 +00:00
|
|
|
}
|
2013-04-02 23:44:01 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Precondition: already parsed the '{' or '#{'
|
|
|
|
/// I guess that also means "already parsed the 'impure'" if
|
|
|
|
/// necessary, and this should take a qualifier.
|
|
|
|
/// Some blocks start with "#{"...
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> P<Block> {
|
2014-02-28 21:09:09 +00:00
|
|
|
self.parse_block_tail_(lo, s, Vec::new())
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse the rest of a block expression or function body
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode,
|
2014-02-28 21:09:09 +00:00
|
|
|
first_item_attrs: Vec<Attribute> ) -> P<Block> {
|
|
|
|
let mut stmts = Vec::new();
|
2012-08-20 19:23:37 +00:00
|
|
|
let mut expr = None;
|
2012-08-14 21:22:52 +00:00
|
|
|
|
2013-04-30 19:02:56 +00:00
|
|
|
// wouldn't it be more uniform to parse view items only, here?
|
2013-02-21 08:16:31 +00:00
|
|
|
let ParsedItemsAndViewItems {
|
2014-10-06 00:36:53 +00:00
|
|
|
attrs_remaining,
|
|
|
|
view_items,
|
|
|
|
items,
|
2013-11-28 20:22:53 +00:00
|
|
|
..
|
2013-02-21 08:16:31 +00:00
|
|
|
} = self.parse_items_and_view_items(first_item_attrs,
|
2013-04-01 22:50:58 +00:00
|
|
|
false, false);
|
2012-08-14 21:22:52 +00:00
|
|
|
|
2014-09-15 03:27:36 +00:00
|
|
|
for item in items.into_iter() {
|
2014-09-13 16:06:01 +00:00
|
|
|
let span = item.span;
|
|
|
|
let decl = P(spanned(span.lo, span.hi, DeclItem(item)));
|
|
|
|
stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))));
|
2012-08-14 21:22:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 19:02:56 +00:00
|
|
|
let mut attributes_box = attrs_remaining;
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2013-04-30 19:02:56 +00:00
|
|
|
// parsing items even when they're not allowed lets us give
|
|
|
|
// better error messages and recover more gracefully.
|
2014-02-28 20:54:01 +00:00
|
|
|
attributes_box.push_all(self.parse_outer_attributes().as_slice());
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Semi => {
|
2013-06-09 01:38:47 +00:00
|
|
|
if !attributes_box.is_empty() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2014-09-12 01:07:07 +00:00
|
|
|
self.span_err(last_span,
|
|
|
|
Parser::expected_item_err(attributes_box.as_slice()));
|
2014-02-28 21:09:09 +00:00
|
|
|
attributes_box = Vec::new();
|
2013-04-30 19:02:56 +00:00
|
|
|
}
|
2012-09-27 00:33:34 +00:00
|
|
|
self.bump(); // empty
|
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Brace) => {
|
2013-04-30 19:02:56 +00:00
|
|
|
// fall through and out.
|
|
|
|
}
|
2012-09-27 00:33:34 +00:00
|
|
|
_ => {
|
2013-04-30 19:02:56 +00:00
|
|
|
let stmt = self.parse_stmt(attributes_box);
|
2014-02-28 21:09:09 +00:00
|
|
|
attributes_box = Vec::new();
|
2014-09-13 16:06:01 +00:00
|
|
|
stmt.and_then(|Spanned {node, span}| match node {
|
2013-09-02 01:45:37 +00:00
|
|
|
StmtExpr(e, stmt_id) => {
|
2013-04-30 19:02:56 +00:00
|
|
|
// expression without semicolon
|
2014-09-13 16:06:01 +00:00
|
|
|
if classify::expr_requires_semi_to_be_stmt(&*e) {
|
2013-08-05 20:18:29 +00:00
|
|
|
// Just check for errors and recover; do not eat semicolon yet.
|
2014-10-29 21:44:41 +00:00
|
|
|
self.commit_stmt(&[], &[token::Semi,
|
|
|
|
token::CloseDelim(token::Brace)]);
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Semi => {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.bump();
|
2014-04-09 20:42:25 +00:00
|
|
|
let span_with_semi = Span {
|
2014-09-13 16:06:01 +00:00
|
|
|
lo: span.lo,
|
2014-04-09 20:42:25 +00:00
|
|
|
hi: self.last_span.hi,
|
2014-09-17 16:01:33 +00:00
|
|
|
expn_id: span.expn_id,
|
2014-04-09 20:42:25 +00:00
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
stmts.push(P(Spanned {
|
2013-09-02 01:45:37 +00:00
|
|
|
node: StmtSemi(e, stmt_id),
|
2014-04-09 20:42:25 +00:00
|
|
|
span: span_with_semi,
|
2014-09-13 16:06:01 +00:00
|
|
|
}));
|
2012-09-27 00:33:34 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Brace) => {
|
2012-09-27 00:33:34 +00:00
|
|
|
expr = Some(e);
|
|
|
|
}
|
2013-08-05 20:18:29 +00:00
|
|
|
_ => {
|
2014-09-13 16:06:01 +00:00
|
|
|
stmts.push(P(Spanned {
|
|
|
|
node: StmtExpr(e, stmt_id),
|
|
|
|
span: span
|
|
|
|
}));
|
2012-09-27 00:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
StmtMac(m, semi) => {
|
2013-04-30 19:02:56 +00:00
|
|
|
// statement macro; might be an expr
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Semi => {
|
2014-09-13 16:06:01 +00:00
|
|
|
stmts.push(P(Spanned {
|
|
|
|
node: StmtMac(m, true),
|
|
|
|
span: span,
|
|
|
|
}));
|
2014-04-09 20:42:25 +00:00
|
|
|
self.bump();
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Brace) => {
|
2012-11-19 05:36:26 +00:00
|
|
|
// if a block ends in `m!(arg)` without
|
|
|
|
// a `;`, it must be an expr
|
|
|
|
expr = Some(
|
2014-09-13 16:06:01 +00:00
|
|
|
self.mk_mac_expr(span.lo,
|
|
|
|
span.hi,
|
|
|
|
m.node));
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2014-09-13 16:06:01 +00:00
|
|
|
stmts.push(P(Spanned {
|
|
|
|
node: StmtMac(m, semi),
|
|
|
|
span: span
|
|
|
|
}));
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
2013-04-30 19:02:56 +00:00
|
|
|
_ => { // all other kinds of statements:
|
2014-09-13 16:06:01 +00:00
|
|
|
if classify::stmt_ends_with_semi(&node) {
|
2014-10-27 08:22:52 +00:00
|
|
|
self.commit_stmt_expecting(token::Semi);
|
2012-09-27 00:33:34 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
|
|
|
|
stmts.push(P(Spanned {
|
|
|
|
node: node,
|
|
|
|
span: span
|
|
|
|
}));
|
2012-09-27 00:33:34 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2010-11-30 01:11:03 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 19:02:56 +00:00
|
|
|
|
2013-06-09 01:38:47 +00:00
|
|
|
if !attributes_box.is_empty() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2014-09-12 01:07:07 +00:00
|
|
|
self.span_err(last_span,
|
|
|
|
Parser::expected_item_err(attributes_box.as_slice()));
|
2013-04-30 19:02:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-11-30 22:00:39 +00:00
|
|
|
P(ast::Block {
|
2013-01-15 03:35:08 +00:00
|
|
|
view_items: view_items,
|
|
|
|
stmts: stmts,
|
|
|
|
expr: expr,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-15 03:35:08 +00:00
|
|
|
rules: s,
|
2013-07-16 18:08:35 +00:00
|
|
|
span: mk_sp(lo, hi),
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
// Parses a sequence of bounds if a `:` is found,
|
|
|
|
// otherwise returns empty list.
|
|
|
|
fn parse_colon_then_ty_param_bounds(&mut self)
|
|
|
|
-> OwnedSlice<TyParamBound>
|
|
|
|
{
|
2014-10-27 08:22:52 +00:00
|
|
|
if !self.eat(&token::Colon) {
|
2014-08-28 01:46:52 +00:00
|
|
|
OwnedSlice::empty()
|
|
|
|
} else {
|
|
|
|
self.parse_ty_param_bounds()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// matches bounds = ( boundseq )?
|
2014-11-07 11:53:45 +00:00
|
|
|
// where boundseq = ( polybound + boundseq ) | polybound
|
|
|
|
// and polybound = ( 'for' '<' 'region '>' )? bound
|
|
|
|
// and bound = 'region | trait_ref
|
2014-08-28 01:46:52 +00:00
|
|
|
// NB: The None/Some distinction is important for issue #7264.
|
|
|
|
fn parse_ty_param_bounds(&mut self)
|
|
|
|
-> OwnedSlice<TyParamBound>
|
|
|
|
{
|
2014-03-19 12:16:56 +00:00
|
|
|
let mut result = vec!();
|
2013-02-15 05:50:03 +00:00
|
|
|
loop {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Lifetime(lifetime) => {
|
2014-08-28 01:46:52 +00:00
|
|
|
result.push(RegionTyParamBound(ast::Lifetime {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: self.span,
|
|
|
|
name: lifetime.name
|
|
|
|
}));
|
2013-03-22 00:29:49 +00:00
|
|
|
self.bump();
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::ModSep | token::Ident(..) => {
|
2014-11-07 11:53:45 +00:00
|
|
|
let poly_trait_ref = self.parse_poly_trait_ref();
|
|
|
|
result.push(TraitTyParamBound(poly_trait_ref))
|
2014-06-02 01:41:46 +00:00
|
|
|
}
|
2013-03-22 00:29:49 +00:00
|
|
|
_ => break,
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
2013-01-22 22:37:32 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if !self.eat(&token::BinOp(token::Plus)) {
|
2013-06-17 19:16:30 +00:00
|
|
|
break;
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
return OwnedSlice::from_vec(result);
|
2012-08-07 01:54:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
fn trait_ref_from_ident(ident: Ident, span: Span) -> TraitRef {
|
2014-07-08 02:26:02 +00:00
|
|
|
let segment = ast::PathSegment {
|
|
|
|
identifier: ident,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: ast::PathParameters::none()
|
2014-07-08 02:26:02 +00:00
|
|
|
};
|
|
|
|
let path = ast::Path {
|
|
|
|
span: span,
|
|
|
|
global: false,
|
|
|
|
segments: vec![segment],
|
|
|
|
};
|
|
|
|
ast::TraitRef {
|
|
|
|
path: path,
|
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Matches typaram = (unbound`?`)? IDENT optbounds ( EQ ty )?
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_ty_param(&mut self) -> TyParam {
|
2014-07-08 02:26:02 +00:00
|
|
|
// This is a bit hacky. Currently we are only interested in a single
|
|
|
|
// unbound, and it may only be `Sized`. To avoid backtracking and other
|
|
|
|
// complications, we parse an ident, then check for `?`. If we find it,
|
|
|
|
// we use the ident as the unbound, otherwise, we use it as the name of
|
|
|
|
// type param.
|
|
|
|
let mut span = self.span;
|
|
|
|
let mut ident = self.parse_ident();
|
|
|
|
let mut unbound = None;
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Question) {
|
2014-07-08 02:26:02 +00:00
|
|
|
let tref = Parser::trait_ref_from_ident(ident, span);
|
2014-11-07 11:53:45 +00:00
|
|
|
unbound = Some(tref);
|
2014-07-08 02:26:02 +00:00
|
|
|
span = self.span;
|
|
|
|
ident = self.parse_ident();
|
|
|
|
}
|
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
let bounds = self.parse_colon_then_ty_param_bounds();
|
2014-01-30 17:28:02 +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
|
|
|
let default = if self.check(&token::Eq) {
|
2014-01-30 17:28:02 +00:00
|
|
|
self.bump();
|
2014-11-20 20:05:29 +00:00
|
|
|
Some(self.parse_ty_sum())
|
2014-01-30 17:28:02 +00:00
|
|
|
}
|
|
|
|
else { None };
|
|
|
|
|
|
|
|
TyParam {
|
|
|
|
ident: ident,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
bounds: bounds,
|
2014-07-08 02:26:02 +00:00
|
|
|
unbound: unbound,
|
2014-04-03 00:53:57 +00:00
|
|
|
default: default,
|
|
|
|
span: span,
|
2014-01-30 17:28:02 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-04 22:16:41 +00:00
|
|
|
|
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 )
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_generics(&mut self) -> ast::Generics {
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Lt) {
|
2014-08-06 02:59:24 +00:00
|
|
|
let lifetime_defs = self.parse_lifetime_defs();
|
2014-01-30 17:28:02 +00:00
|
|
|
let mut seen_default = false;
|
2014-10-27 08:22:52 +00:00
|
|
|
let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
|
2014-05-23 19:51:21 +00:00
|
|
|
p.forbid_lifetime();
|
2014-01-30 17:28:02 +00:00
|
|
|
let ty_param = p.parse_ty_param();
|
|
|
|
if ty_param.default.is_some() {
|
|
|
|
seen_default = true;
|
|
|
|
} else if seen_default {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = p.last_span;
|
|
|
|
p.span_err(last_span,
|
2014-01-30 17:28:02 +00:00
|
|
|
"type parameters with a default must be trailing");
|
|
|
|
}
|
|
|
|
ty_param
|
|
|
|
});
|
2014-08-11 16:32:26 +00:00
|
|
|
ast::Generics {
|
|
|
|
lifetimes: lifetime_defs,
|
|
|
|
ty_params: ty_params,
|
|
|
|
where_clause: WhereClause {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
} else {
|
2013-02-28 15:25:31 +00:00
|
|
|
ast_util::empty_generics()
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:08:30 +00:00
|
|
|
fn parse_generic_values_after_lt(&mut self)
|
|
|
|
-> (Vec<ast::Lifetime>, Vec<P<Ty>>, Vec<P<TypeBinding>>) {
|
2014-10-27 08:22:52 +00:00
|
|
|
let lifetimes = self.parse_lifetimes(token::Comma);
|
2014-11-29 04:08:30 +00:00
|
|
|
|
|
|
|
// First parse types.
|
|
|
|
let (types, returned) = self.parse_seq_to_gt_or_return(
|
2014-10-27 08:22:52 +00:00
|
|
|
Some(token::Comma),
|
2014-05-23 19:51:21 +00:00
|
|
|
|p| {
|
|
|
|
p.forbid_lifetime();
|
2014-11-29 04:08:30 +00:00
|
|
|
if p.look_ahead(1, |t| t == &token::Eq) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(p.parse_ty_sum())
|
|
|
|
}
|
2014-05-23 19:51:21 +00:00
|
|
|
}
|
|
|
|
);
|
2014-11-29 04:08:30 +00:00
|
|
|
|
|
|
|
// If we found the `>`, don't continue.
|
|
|
|
if !returned {
|
|
|
|
return (lifetimes, types.into_vec(), Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then parse type bindings.
|
|
|
|
let bindings = self.parse_seq_to_gt(
|
|
|
|
Some(token::Comma),
|
|
|
|
|p| {
|
|
|
|
p.forbid_lifetime();
|
|
|
|
let lo = p.span.lo;
|
|
|
|
let ident = p.parse_ident();
|
|
|
|
let found_eq = p.eat(&token::Eq);
|
|
|
|
if !found_eq {
|
|
|
|
let span = p.span;
|
|
|
|
p.span_warn(span, "whoops, no =?");
|
|
|
|
}
|
|
|
|
let ty = p.parse_ty();
|
|
|
|
let hi = p.span.hi;
|
|
|
|
let span = mk_sp(lo, hi);
|
|
|
|
return P(TypeBinding{id: ast::DUMMY_NODE_ID,
|
|
|
|
ident: ident,
|
|
|
|
ty: ty,
|
|
|
|
span: span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(lifetimes, types.into_vec(), bindings.into_vec())
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
|
2014-05-23 19:51:21 +00:00
|
|
|
fn forbid_lifetime(&mut self) {
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_lifetime() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
self.span_fatal(span, "lifetime parameters must be declared \
|
2014-05-23 19:51:21 +00:00
|
|
|
prior to type parameters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
/// Parses an optional `where` clause and places it in `generics`.
|
|
|
|
fn parse_where_clause(&mut self, generics: &mut ast::Generics) {
|
|
|
|
if !self.eat_keyword(keywords::Where) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parsed_something = false;
|
|
|
|
loop {
|
|
|
|
let lo = self.span.lo;
|
2014-11-29 04:08:30 +00:00
|
|
|
let path = match self.token {
|
|
|
|
token::Ident(..) => self.parse_path(NoTypesAllowed),
|
2014-08-11 16:32:26 +00:00
|
|
|
_ => break,
|
|
|
|
};
|
|
|
|
|
2014-11-29 04:08:30 +00:00
|
|
|
if self.eat(&token::Colon) {
|
|
|
|
let bounds = self.parse_ty_param_bounds();
|
|
|
|
let hi = self.span.hi;
|
|
|
|
let span = mk_sp(lo, hi);
|
2014-08-11 16:32:26 +00:00
|
|
|
|
2014-11-29 04:08:30 +00:00
|
|
|
if bounds.len() == 0 {
|
|
|
|
self.span_err(span,
|
|
|
|
"each predicate in a `where` clause must have \
|
|
|
|
at least one bound in it");
|
|
|
|
}
|
2014-08-11 16:32:26 +00:00
|
|
|
|
2014-11-29 04:08:30 +00:00
|
|
|
let ident = match ast_util::path_to_ident(&path) {
|
|
|
|
Some(ident) => ident,
|
|
|
|
None => {
|
|
|
|
self.span_err(path.span, "expected a single identifier \
|
|
|
|
in bound where clause");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
generics.where_clause.predicates.push(
|
|
|
|
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: span,
|
|
|
|
ident: ident,
|
|
|
|
bounds: bounds,
|
|
|
|
}));
|
|
|
|
parsed_something = true;
|
|
|
|
} else if self.eat(&token::Eq) {
|
|
|
|
let ty = self.parse_ty();
|
|
|
|
let hi = self.span.hi;
|
|
|
|
let span = mk_sp(lo, hi);
|
|
|
|
generics.where_clause.predicates.push(
|
|
|
|
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: span,
|
|
|
|
path: path,
|
|
|
|
ty: ty,
|
|
|
|
}));
|
|
|
|
parsed_something = true;
|
|
|
|
// FIXME(#18433)
|
|
|
|
self.span_err(span, "equality constraints are not yet supported in where clauses");
|
|
|
|
} else {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
|
|
|
"unexpected token in `where` clause");
|
|
|
|
}
|
2014-08-11 16:32:26 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if !self.eat(&token::Comma) {
|
2014-08-11 16:32:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parsed_something {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
|
|
|
"a `where` clause must have at least one predicate \
|
|
|
|
in it");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
|
2014-02-28 21:09:09 +00:00
|
|
|
-> (Vec<Arg> , bool) {
|
2013-12-30 23:17:53 +00:00
|
|
|
let sp = self.span;
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut args: Vec<Option<Arg>> =
|
2012-06-12 17:17:36 +00:00
|
|
|
self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_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 {
|
2013-10-25 05:56:34 +00:00
|
|
|
p.bump();
|
|
|
|
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;
|
|
|
|
p.span_fatal(span,
|
2013-10-25 05:56:34 +00:00
|
|
|
"`...` must be last in argument list for variadic function");
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = p.span;
|
|
|
|
p.span_fatal(span,
|
2013-10-25 05:56:34 +00:00
|
|
|
"only foreign functions are allowed to be variadic");
|
|
|
|
}
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(p.parse_arg_general(named_args))
|
|
|
|
}
|
|
|
|
}
|
2013-02-24 23:41:54 +00:00
|
|
|
);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-12-23 15:20:52 +00:00
|
|
|
let variadic = match args.pop() {
|
2013-10-25 05:56:34 +00:00
|
|
|
Some(None) => true,
|
|
|
|
Some(x) => {
|
|
|
|
// Need to put back that last arg
|
|
|
|
args.push(x);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
None => false
|
|
|
|
};
|
|
|
|
|
|
|
|
if variadic && args.is_empty() {
|
|
|
|
self.span_err(sp,
|
|
|
|
"variadic function must be declared with at least one named argument");
|
|
|
|
}
|
|
|
|
|
2014-09-15 03:27:36 +00:00
|
|
|
let args = args.into_iter().map(|x| x.unwrap()).collect();
|
2013-10-25 05:56:34 +00:00
|
|
|
|
|
|
|
(args, variadic)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse the argument list and result type of a function declaration
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> P<FnDecl> {
|
2013-10-25 05:56:34 +00:00
|
|
|
|
|
|
|
let (args, variadic) = self.parse_fn_args(true, allow_variadic);
|
2014-11-09 15:14:15 +00:00
|
|
|
let ret_ty = self.parse_ret_ty();
|
2013-10-25 05:56:34 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
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
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn is_self_ident(&mut self) -> bool {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 15:01:44 +00:00
|
|
|
token::Ident(id, token::Plain) => id.name == special_idents::self_.name,
|
2013-09-05 21:14:31 +00:00
|
|
|
_ => false
|
|
|
|
}
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 22:10:57 +00:00
|
|
|
fn expect_self_ident(&mut self) -> ast::Ident {
|
|
|
|
match self.token {
|
2014-10-27 15:01:44 +00:00
|
|
|
token::Ident(id, token::Plain) if id.name == special_idents::self_.name => {
|
2014-07-06 22:10:57 +00:00
|
|
|
self.bump();
|
|
|
|
id
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let token_str = self.this_token_to_string();
|
2014-08-23 10:41:32 +00:00
|
|
|
self.fatal(format!("expected `self`, found `{}`",
|
2014-07-06 22:10:57 +00:00
|
|
|
token_str).as_slice())
|
|
|
|
}
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse the argument list and result type of a function
|
|
|
|
/// that may have a self type.
|
2014-12-08 18:28:32 +00:00
|
|
|
fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
|
|
|
|
F: FnMut(&mut Parser) -> Arg,
|
|
|
|
{
|
2013-12-30 22:04:00 +00:00
|
|
|
fn maybe_parse_borrowed_explicit_self(this: &mut Parser)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ast::ExplicitSelf_ {
|
2013-03-10 00:43:53 +00:00
|
|
|
// The following things are possible to see here:
|
|
|
|
//
|
2013-12-30 22:04:00 +00:00
|
|
|
// fn(&mut self)
|
2013-03-10 00:43:53 +00:00
|
|
|
// fn(&mut self)
|
|
|
|
// fn(&'lt self)
|
|
|
|
// fn(&'lt mut self)
|
|
|
|
//
|
|
|
|
// We already know that the current token is `&`.
|
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
2014-07-06 22:10:57 +00:00
|
|
|
SelfRegion(None, MutImmutable, this.expect_self_ident())
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if this.look_ahead(1, |t| t.is_mutability()) &&
|
|
|
|
this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
|
|
|
let mutability = this.parse_mutability();
|
2014-07-06 22:10:57 +00:00
|
|
|
SelfRegion(None, mutability, this.expect_self_ident())
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if this.look_ahead(1, |t| t.is_lifetime()) &&
|
|
|
|
this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
2013-07-05 12:33:52 +00:00
|
|
|
let lifetime = this.parse_lifetime();
|
2014-07-06 22:10:57 +00:00
|
|
|
SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if this.look_ahead(1, |t| t.is_lifetime()) &&
|
|
|
|
this.look_ahead(2, |t| t.is_mutability()) &&
|
|
|
|
this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
2013-07-05 12:33:52 +00:00
|
|
|
let lifetime = this.parse_lifetime();
|
2013-05-10 22:15:06 +00:00
|
|
|
let mutability = this.parse_mutability();
|
2014-07-06 22:10:57 +00:00
|
|
|
SelfRegion(Some(lifetime), mutability, this.expect_self_ident())
|
2013-03-10 00:43:53 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfStatic
|
2013-03-10 00:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Paren));
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2013-06-06 07:38:41 +00:00
|
|
|
// A bit of complexity and lookahead is needed here in order to be
|
2012-07-30 23:33:02 +00:00
|
|
|
// backwards compatible.
|
|
|
|
let lo = self.span.lo;
|
2014-10-15 17:04:29 +00:00
|
|
|
let mut self_ident_lo = self.span.lo;
|
|
|
|
let mut self_ident_hi = self.span.hi;
|
|
|
|
|
2014-01-27 12:18:36 +00:00
|
|
|
let mut mutbl_self = MutImmutable;
|
2013-12-30 23:09:41 +00:00
|
|
|
let explicit_self = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::And) => {
|
2014-10-15 17:04:29 +00:00
|
|
|
let eself = maybe_parse_borrowed_explicit_self(self);
|
|
|
|
self_ident_lo = self.last_span.lo;
|
|
|
|
self_ident_hi = self.last_span.hi;
|
|
|
|
eself
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Tilde => {
|
2014-02-06 22:38:33 +00:00
|
|
|
// We need to make sure it isn't a type
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
|
2014-02-06 22:38:33 +00:00
|
|
|
self.bump();
|
2014-07-23 17:21:50 +00:00
|
|
|
drop(self.expect_self_ident());
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.obsolete(last_span, ObsoleteOwnedSelf)
|
2014-02-06 22:38:33 +00:00
|
|
|
}
|
2014-07-23 17:21:50 +00:00
|
|
|
SelfStatic
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Star) => {
|
2014-01-12 00:25:51 +00:00
|
|
|
// Possibly "*self" or "*mut self" -- not supported. Try to avoid
|
|
|
|
// emitting cryptic "unexpected token" errors.
|
|
|
|
self.bump();
|
2014-10-27 12:33:30 +00:00
|
|
|
let _mutability = if self.token.is_mutability() {
|
2014-01-12 00:25:51 +00:00
|
|
|
self.parse_mutability()
|
2014-05-06 23:37:32 +00:00
|
|
|
} else {
|
|
|
|
MutImmutable
|
|
|
|
};
|
2014-01-12 00:25:51 +00:00
|
|
|
if self.is_self_ident() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
|
|
|
self.span_err(span, "cannot pass self by unsafe pointer");
|
2014-01-12 00:25:51 +00:00
|
|
|
self.bump();
|
2013-06-24 04:12:17 +00:00
|
|
|
}
|
2014-07-06 22:10:57 +00:00
|
|
|
// error case, making bogus self ident:
|
|
|
|
SelfValue(special_idents::self_)
|
2014-01-12 00:25:51 +00:00
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
token::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
|
|
|
if self.is_self_ident() {
|
|
|
|
let self_ident = self.expect_self_ident();
|
2014-05-06 23:37:32 +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
|
|
|
// Determine whether this is the fully explicit form, `self:
|
|
|
|
// TYPE`.
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2014-11-20 20:05:29 +00:00
|
|
|
SelfExplicit(self.parse_ty_sum(), self_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 {
|
|
|
|
SelfValue(self_ident)
|
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if self.token.is_mutability() &&
|
|
|
|
self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
|
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
|
|
|
mutbl_self = self.parse_mutability();
|
|
|
|
let self_ident = self.expect_self_ident();
|
|
|
|
|
|
|
|
// Determine whether this is the fully explicit form,
|
|
|
|
// `self: TYPE`.
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2014-11-20 20:05:29 +00:00
|
|
|
SelfExplicit(self.parse_ty_sum(), self_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 {
|
|
|
|
SelfValue(self_ident)
|
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
} else if self.token.is_mutability() &&
|
2014-10-27 08:22:52 +00:00
|
|
|
self.look_ahead(1, |t| *t == token::Tilde) &&
|
2014-10-27 12:33:30 +00:00
|
|
|
self.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
|
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
|
|
|
mutbl_self = self.parse_mutability();
|
|
|
|
self.bump();
|
|
|
|
drop(self.expect_self_ident());
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.obsolete(last_span, ObsoleteOwnedSelf);
|
|
|
|
SelfStatic
|
2014-05-06 23:37:32 +00:00
|
|
|
} else {
|
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
|
|
|
SelfStatic
|
2014-05-06 23:37:32 +00:00
|
|
|
}
|
2014-01-12 00:25:51 +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
|
|
|
_ => SelfStatic,
|
2012-08-17 22:25:35 +00:00
|
|
|
};
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2014-10-15 17:04:29 +00:00
|
|
|
let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
|
2014-01-27 12:18:36 +00:00
|
|
|
|
2014-07-06 22:10:57 +00:00
|
|
|
// shared fall-through for the three cases below. borrowing prevents simply
|
|
|
|
// writing this as a closure
|
|
|
|
macro_rules! parse_remaining_arguments {
|
|
|
|
($self_id:ident) =>
|
|
|
|
{
|
|
|
|
// If we parsed a self type, expect a comma before the argument list.
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Comma => {
|
2012-07-30 23:33:02 +00:00
|
|
|
self.bump();
|
2014-10-27 08:22:52 +00:00
|
|
|
let sep = seq_sep_trailing_allowed(token::Comma);
|
2014-01-27 12:18:36 +00:00
|
|
|
let mut fn_inputs = self.parse_seq_to_before_end(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::CloseDelim(token::Paren),
|
2013-02-24 23:41:54 +00:00
|
|
|
sep,
|
|
|
|
parse_arg_fn
|
2013-03-02 04:35:55 +00:00
|
|
|
);
|
2014-10-15 06:05:01 +00:00
|
|
|
fn_inputs.insert(0, Arg::new_self(explicit_self_sp, mutbl_self, $self_id));
|
2014-01-27 12:18:36 +00:00
|
|
|
fn_inputs
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Paren) => {
|
2014-07-06 22:10:57 +00:00
|
|
|
vec!(Arg::new_self(explicit_self_sp, mutbl_self, $self_id))
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2013-12-30 22:04:00 +00:00
|
|
|
self.fatal(format!("expected `,` or `)`, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-06 22:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let fn_inputs = match explicit_self {
|
|
|
|
SelfStatic => {
|
2014-10-27 08:22:52 +00:00
|
|
|
let sep = seq_sep_trailing_allowed(token::Comma);
|
2014-10-29 10:37:54 +00:00
|
|
|
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
|
2014-07-06 22:10:57 +00:00
|
|
|
}
|
|
|
|
SelfValue(id) => parse_remaining_arguments!(id),
|
|
|
|
SelfRegion(_,_,id) => parse_remaining_arguments!(id),
|
2014-05-06 23:37:32 +00:00
|
|
|
SelfExplicit(_,id) => parse_remaining_arguments!(id),
|
2014-01-27 12:18:36 +00:00
|
|
|
};
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2014-07-06 22:10:57 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Paren));
|
2012-07-30 23:33:02 +00:00
|
|
|
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
2014-11-09 15:14:15 +00:00
|
|
|
let ret_ty = self.parse_ret_ty();
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
let fn_decl = P(FnDecl {
|
2013-09-14 02:07:43 +00:00
|
|
|
inputs: fn_inputs,
|
2012-07-30 23:33:02 +00:00
|
|
|
output: ret_ty,
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2012-07-30 23:33:02 +00:00
|
|
|
|
2013-04-30 15:49:48 +00:00
|
|
|
(spanned(lo, hi, explicit_self), fn_decl)
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 05:26:56 +00:00
|
|
|
// parse the |arg, arg| header on a lambda
|
2014-07-30 05:08:39 +00:00
|
|
|
fn parse_fn_block_decl(&mut self)
|
|
|
|
-> (P<FnDecl>, Option<UnboxedClosureKind>) {
|
|
|
|
let (optional_unboxed_closure_kind, inputs_captures) = {
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::OrOr) {
|
2014-07-30 05:08:39 +00:00
|
|
|
(None, Vec::new())
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::BinOp(token::Or));
|
2014-07-30 05:08:39 +00:00
|
|
|
let optional_unboxed_closure_kind =
|
|
|
|
self.parse_optional_unboxed_closure_kind();
|
2014-05-29 05:26:56 +00:00
|
|
|
let args = self.parse_seq_to_before_end(
|
2014-10-27 08:22:52 +00:00
|
|
|
&token::BinOp(token::Or),
|
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_fn_block_arg()
|
2014-05-29 05:26:56 +00:00
|
|
|
);
|
|
|
|
self.bump();
|
2014-07-30 05:08:39 +00:00
|
|
|
(optional_unboxed_closure_kind, args)
|
2012-05-04 19:33:04 +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
|
|
|
let output = if self.check(&token::RArrow) {
|
2014-08-29 05:16:23 +00:00
|
|
|
self.parse_ret_ty()
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2014-11-09 15:14:15 +00:00
|
|
|
Return(P(Ty {
|
2013-12-30 23:17:53 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: TyInfer,
|
2013-12-30 23:17:53 +00:00
|
|
|
span: self.span,
|
2014-08-29 05:16:23 +00:00
|
|
|
}))
|
2012-05-23 22:06:11 +00:00
|
|
|
};
|
2013-01-16 00:05:20 +00:00
|
|
|
|
2014-05-29 05:26:56 +00:00
|
|
|
(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
|
2014-07-30 05:08:39 +00:00
|
|
|
}), optional_unboxed_closure_kind)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses the `(arg, arg) -> return_type` header on a procedure.
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_proc_decl(&mut self) -> P<FnDecl> {
|
2013-10-28 22:22:49 +00:00
|
|
|
let inputs =
|
2014-10-29 10:37:54 +00:00
|
|
|
self.parse_unspanned_seq(&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-10-28 22:22:49 +00:00
|
|
|
|p| p.parse_fn_block_arg());
|
|
|
|
|
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 output = if self.check(&token::RArrow) {
|
2014-08-29 05:16:23 +00:00
|
|
|
self.parse_ret_ty()
|
2013-10-28 22:22:49 +00:00
|
|
|
} else {
|
2014-11-09 15:14:15 +00:00
|
|
|
Return(P(Ty {
|
2013-10-28 22:22:49 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: TyInfer,
|
2013-12-30 23:17:53 +00:00
|
|
|
span: self.span,
|
2014-08-29 05:16:23 +00:00
|
|
|
}))
|
2013-10-28 22:22:49 +00:00
|
|
|
};
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
P(FnDecl {
|
2013-10-28 22:22:49 +00:00
|
|
|
inputs: inputs,
|
|
|
|
output: output,
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse the name and optional generic types of a function header.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_fn_header(&mut self) -> (Ident, ast::Generics) {
|
2013-03-06 17:30:54 +00:00
|
|
|
let id = self.parse_ident();
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = self.parse_generics();
|
|
|
|
(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,
|
2014-01-09 13:05:33 +00:00
|
|
|
node: Item_, 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.
|
2014-12-09 15:36:46 +00:00
|
|
|
fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> ItemInfo {
|
2014-08-11 16:32:26 +00:00
|
|
|
let (ident, mut generics) = self.parse_fn_header();
|
2013-10-25 05:56:34 +00:00
|
|
|
let decl = self.parse_fn_decl(false);
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut generics);
|
2013-04-06 00:31:52 +00:00
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
|
2014-12-09 15:36:46 +00:00
|
|
|
(ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 14:53:05 +00:00
|
|
|
/// Parse a method in a trait impl
|
|
|
|
pub fn parse_method_with_outer_attributes(&mut self) -> P<Method> {
|
|
|
|
let attrs = self.parse_outer_attributes();
|
|
|
|
let visa = self.parse_visibility();
|
|
|
|
self.parse_method(attrs, visa)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a method in a trait impl, starting with `attrs` attributes.
|
2014-07-07 22:15:31 +00:00
|
|
|
pub fn parse_method(&mut self,
|
2014-08-06 02:44:21 +00:00
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
visa: Visibility)
|
2014-09-13 16:06:01 +00:00
|
|
|
-> P<Method> {
|
2012-08-02 23:01:38 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
2014-07-07 22:15:31 +00:00
|
|
|
// code copied from parse_macro_use_or_failure... abstraction!
|
|
|
|
let (method_, hi, new_attrs) = {
|
2014-10-27 12:33:30 +00:00
|
|
|
if !self.token.is_any_keyword()
|
2014-10-27 08:22:52 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::Not)
|
2014-10-29 10:37:54 +00:00
|
|
|
&& (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
|
|
|
|
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
|
2014-07-07 22:15:31 +00:00
|
|
|
// method macro.
|
2014-11-20 20:05:29 +00:00
|
|
|
let pth = self.parse_path(NoTypesAllowed);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Not);
|
2014-07-07 22:15:31 +00:00
|
|
|
|
|
|
|
// eat a matched-delimiter token tree:
|
2014-10-29 14:47:53 +00:00
|
|
|
let delim = self.expect_open_delim();
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
2014-07-07 22:15:31 +00:00
|
|
|
let m_ = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
|
|
|
|
let m: ast::Mac = codemap::Spanned { node: m_,
|
|
|
|
span: mk_sp(self.span.lo,
|
|
|
|
self.span.hi) };
|
|
|
|
(ast::MethMac(m), self.span.hi, attrs)
|
|
|
|
} else {
|
2014-12-09 15:36:46 +00:00
|
|
|
let unsafety = self.parse_unsafety();
|
2014-05-29 05:26:56 +00:00
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
|
|
|
self.parse_opt_abi().unwrap_or(abi::C)
|
|
|
|
} else {
|
|
|
|
abi::Rust
|
|
|
|
};
|
2014-11-30 08:33:04 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2014-07-07 22:15:31 +00:00
|
|
|
let ident = self.parse_ident();
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut generics = self.parse_generics();
|
2014-07-07 22:15:31 +00:00
|
|
|
let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
|
|
|
|
p.parse_arg()
|
|
|
|
});
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut generics);
|
2014-07-07 22:15:31 +00:00
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
|
2014-09-13 16:06:01 +00:00
|
|
|
let body_span = body.span;
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut new_attrs = attrs;
|
|
|
|
new_attrs.push_all(inner_attrs.as_slice());
|
2014-05-29 05:26:56 +00:00
|
|
|
(ast::MethDecl(ident,
|
|
|
|
generics,
|
|
|
|
abi,
|
|
|
|
explicit_self,
|
2014-12-09 15:36:46 +00:00
|
|
|
unsafety,
|
2014-05-29 05:26:56 +00:00
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
visa),
|
2014-09-13 16:06:01 +00:00
|
|
|
body_span.hi, new_attrs)
|
2014-07-07 22:15:31 +00:00
|
|
|
}
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
P(ast::Method {
|
2014-07-07 22:15:31 +00:00
|
|
|
attrs: new_attrs,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-02-26 14:35:36 +00:00
|
|
|
span: mk_sp(lo, hi),
|
2014-07-07 22:15:31 +00:00
|
|
|
node: method_,
|
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 trait Foo { ... }
|
2014-12-10 00:59:20 +00:00
|
|
|
fn parse_item_trait(&mut self, unsafety: Unsafety) -> ItemInfo {
|
2012-05-24 19:38:45 +00:00
|
|
|
let ident = self.parse_ident();
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut tps = self.parse_generics();
|
2014-04-03 00:38:45 +00:00
|
|
|
let sized = self.parse_for_sized();
|
2012-08-03 22:24:11 +00:00
|
|
|
|
2014-08-28 01:46:52 +00:00
|
|
|
// Parse supertrait bounds.
|
|
|
|
let bounds = self.parse_colon_then_ty_param_bounds();
|
2012-08-03 22:02:01 +00:00
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut tps);
|
|
|
|
|
2014-08-06 02:44:21 +00:00
|
|
|
let meths = self.parse_trait_items();
|
2014-12-10 00:59:20 +00:00
|
|
|
(ident, ItemTrait(unsafety, tps, sized, bounds, meths), None)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 20:56:56 +00:00
|
|
|
fn parse_impl_items(&mut self) -> (Vec<ImplItem>, Vec<Attribute>) {
|
|
|
|
let mut impl_items = Vec::new();
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2014-08-06 02:44:21 +00:00
|
|
|
let (inner_attrs, mut method_attrs) =
|
|
|
|
self.parse_inner_attrs_and_next();
|
2014-10-29 10:37:54 +00:00
|
|
|
while !self.eat(&token::CloseDelim(token::Brace)) {
|
2014-10-15 06:05:01 +00:00
|
|
|
method_attrs.extend(self.parse_outer_attributes().into_iter());
|
2014-08-06 02:44:21 +00:00
|
|
|
let vis = self.parse_visibility();
|
|
|
|
if self.eat_keyword(keywords::Type) {
|
|
|
|
impl_items.push(TypeImplItem(P(self.parse_typedef(
|
|
|
|
method_attrs,
|
|
|
|
vis))))
|
|
|
|
} else {
|
|
|
|
impl_items.push(MethodImplItem(self.parse_method(
|
|
|
|
method_attrs,
|
|
|
|
vis)));
|
|
|
|
}
|
|
|
|
method_attrs = self.parse_outer_attributes();
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
|
|
|
(impl_items, inner_attrs)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses two variants (with the region/type params always optional):
|
|
|
|
/// impl<T> Foo { ... }
|
|
|
|
/// impl<T> ToString for ~[T] { ... }
|
2014-12-10 11:15:06 +00:00
|
|
|
fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> ItemInfo {
|
2012-07-24 23:38:24 +00:00
|
|
|
// First, parse type parameters if necessary.
|
2014-08-11 16:32:26 +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
|
|
|
|
2013-02-15 05:17:26 +00:00
|
|
|
// Parse the trait.
|
2014-11-20 20:05:29 +00:00
|
|
|
let mut ty = self.parse_ty_sum();
|
2012-07-18 23:18:02 +00:00
|
|
|
|
2012-08-08 22:34:17 +00:00
|
|
|
// Parse traits, if necessary.
|
2013-05-25 15:45:45 +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.
|
|
|
|
let opt_trait_ref = match ty.node {
|
2014-11-20 20:05:29 +00:00
|
|
|
TyPath(ref path, node_id) => {
|
2014-09-05 19:21:02 +00:00
|
|
|
Some(TraitRef {
|
|
|
|
path: (*path).clone(),
|
|
|
|
ref_id: node_id,
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-20 20:05:29 +00:00
|
|
|
ty = self.parse_ty_sum();
|
2013-01-29 19:14:53 +00:00
|
|
|
opt_trait_ref
|
2012-08-08 22:34:17 +00:00
|
|
|
} else {
|
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
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut generics);
|
2014-08-04 20:56:56 +00:00
|
|
|
let (impl_items, attrs) = self.parse_impl_items();
|
2012-10-23 00:57:10 +00:00
|
|
|
|
2014-05-16 07:16:13 +00:00
|
|
|
let ident = ast_util::impl_pretty_name(&opt_trait, &*ty);
|
2014-02-14 05:07:09 +00:00
|
|
|
|
2014-08-04 20:56:56 +00:00
|
|
|
(ident,
|
2014-12-10 11:15:06 +00:00
|
|
|
ItemImpl(unsafety, generics, opt_trait, ty, impl_items),
|
2014-08-04 20:56:56 +00:00
|
|
|
Some(attrs))
|
2012-11-07 02:41:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
/// Parse a::B<String,int>
|
|
|
|
fn parse_trait_ref(&mut self) -> TraitRef {
|
|
|
|
ast::TraitRef {
|
2014-11-20 20:05:29 +00:00
|
|
|
path: self.parse_path(LifetimeAndTypesWithoutColons),
|
2014-11-07 11:53:45 +00:00
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_late_bound_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
|
|
|
|
if self.eat_keyword(keywords::For) {
|
|
|
|
self.expect(&token::Lt);
|
|
|
|
let lifetime_defs = self.parse_lifetime_defs();
|
|
|
|
self.expect_gt();
|
|
|
|
lifetime_defs
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse for<'l> a::B<String,int>
|
|
|
|
fn parse_poly_trait_ref(&mut self) -> PolyTraitRef {
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs();
|
|
|
|
|
|
|
|
ast::PolyTraitRef {
|
|
|
|
bound_lifetimes: lifetime_defs,
|
|
|
|
trait_ref: self.parse_trait_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse struct Foo { ... }
|
2014-10-11 17:24:58 +00:00
|
|
|
fn parse_item_struct(&mut self) -> ItemInfo {
|
2013-03-06 17:30:54 +00:00
|
|
|
let class_name = self.parse_ident();
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut generics = self.parse_generics();
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2014-10-11 17:24:58 +00:00
|
|
|
self.span_err(ty.span, "`virtual` structs have been removed from the language");
|
|
|
|
}
|
2014-02-24 07:17:02 +00:00
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut generics);
|
|
|
|
|
2014-02-24 07:17:02 +00:00
|
|
|
let mut fields: Vec<StructField>;
|
2012-10-24 21:36:00 +00:00
|
|
|
let is_tuple_like;
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
if self.eat(&token::OpenDelim(token::Brace)) {
|
2012-08-16 00:10:23 +00:00
|
|
|
// It's a record-like struct.
|
2012-10-24 21:36:00 +00:00
|
|
|
is_tuple_like = false;
|
2014-02-28 21:09:09 +00:00
|
|
|
fields = Vec::new();
|
2014-10-29 10:37:54 +00:00
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2014-11-16 01:57:54 +00:00
|
|
|
fields.push(self.parse_struct_decl_field(true));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-03-28 01:17:58 +00:00
|
|
|
if fields.len() == 0 {
|
2014-05-16 17:45:16 +00:00
|
|
|
self.fatal(format!("unit-like struct definition should be \
|
|
|
|
written as `struct {};`",
|
|
|
|
token::get_ident(class_name)).as_slice());
|
2013-03-28 01:17:58 +00:00
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
self.bump();
|
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)) {
|
2012-08-16 00:10:23 +00:00
|
|
|
// It's a tuple-like struct.
|
2012-10-24 21:36:00 +00:00
|
|
|
is_tuple_like = true;
|
2013-11-21 00:23:04 +00:00
|
|
|
fields = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2013-11-21 00:23:04 +00:00
|
|
|
|p| {
|
2013-12-30 22:04:00 +00:00
|
|
|
let attrs = p.parse_outer_attributes();
|
2012-08-16 00:10:23 +00:00
|
|
|
let lo = p.span.lo;
|
2014-01-09 13:05:33 +00:00
|
|
|
let struct_field_ = ast::StructField_ {
|
2014-03-25 23:53:52 +00:00
|
|
|
kind: UnnamedField(p.parse_visibility()),
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-11-20 20:05:29 +00:00
|
|
|
ty: p.parse_ty_sum(),
|
2013-05-01 03:20:08 +00:00
|
|
|
attrs: attrs,
|
2012-08-16 00:10:23 +00:00
|
|
|
};
|
2013-11-30 16:27:25 +00:00
|
|
|
spanned(lo, p.span.hi, struct_field_)
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2014-06-22 17:53:56 +00:00
|
|
|
if fields.len() == 0 {
|
|
|
|
self.fatal(format!("unit-like struct definition should be \
|
|
|
|
written as `struct {};`",
|
|
|
|
token::get_ident(class_name)).as_slice());
|
|
|
|
}
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
|
|
|
} else if self.eat(&token::Semi) {
|
2012-08-16 00:10:23 +00:00
|
|
|
// It's a unit-like struct.
|
2012-10-24 21:36:00 +00:00
|
|
|
is_tuple_like = true;
|
2014-02-28 21:09:09 +00:00
|
|
|
fields = Vec::new();
|
2012-08-16 00:10:23 +00:00
|
|
|
} else {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-05-28 16:24:28 +00:00
|
|
|
self.fatal(format!("expected `{}`, `(`, or `;` after struct \
|
2014-08-23 10:41:32 +00:00
|
|
|
name, found `{}`", "{",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2014-01-26 08:43:42 +00:00
|
|
|
let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug.
|
2013-09-07 02:11:55 +00:00
|
|
|
let new_id = ast::DUMMY_NODE_ID;
|
2012-10-08 18:49:01 +00:00
|
|
|
(class_name,
|
2014-09-13 16:06:01 +00:00
|
|
|
ItemStruct(P(ast::StructDef {
|
2013-01-13 20:29:36 +00:00
|
|
|
fields: fields,
|
2014-02-24 07:17:02 +00:00
|
|
|
ctor_id: if is_tuple_like { Some(new_id) } else { None },
|
2014-09-13 16:06:01 +00:00
|
|
|
}), generics),
|
2012-10-08 18:49:01 +00:00
|
|
|
None)
|
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,
|
2014-01-09 13:05:33 +00:00
|
|
|
vis: Visibility,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> )
|
2014-01-09 13:05:33 +00:00
|
|
|
-> StructField {
|
2013-05-01 03:20:08 +00:00
|
|
|
let a_var = self.parse_name_and_ty(vis, attrs);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Comma => {
|
2013-02-25 03:27:43 +00:00
|
|
|
self.bump();
|
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::CloseDelim(token::Brace) => {}
|
2014-05-28 16:24:28 +00:00
|
|
|
_ => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-11-10 10:58:03 +00:00
|
|
|
self.span_fatal_help(span,
|
|
|
|
format!("expected `,`, or `}}`, found `{}`",
|
|
|
|
token_str).as_slice(),
|
|
|
|
"struct fields should be separated by commas")
|
2014-05-28 16:24:28 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-02-09 13:00:55 +00:00
|
|
|
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
|
2014-11-16 01:57:54 +00:00
|
|
|
fn parse_struct_decl_field(&mut self, allow_pub: bool) -> StructField {
|
2012-09-11 01:56:07 +00:00
|
|
|
|
2013-02-09 13:00:55 +00:00
|
|
|
let attrs = self.parse_outer_attributes();
|
|
|
|
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Pub) {
|
2014-11-16 01:57:54 +00:00
|
|
|
if !allow_pub {
|
|
|
|
let span = self.last_span;
|
|
|
|
self.span_err(span, "`pub` is not allowed here");
|
|
|
|
}
|
|
|
|
return self.parse_single_struct_field(Public, attrs);
|
2012-05-31 01:14:40 +00:00
|
|
|
}
|
2012-07-17 02:16:19 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
return self.parse_single_struct_field(Inherited, attrs);
|
2012-02-01 03:30:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 23:40:21 +00:00
|
|
|
/// Parse visibility: PUB, PRIV, or nothing
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_visibility(&mut self) -> Visibility {
|
|
|
|
if self.eat_keyword(keywords::Pub) { Public }
|
|
|
|
else { Inherited }
|
2012-02-23 05:47:23 +00:00
|
|
|
}
|
2013-03-22 19:56:10 +00:00
|
|
|
|
2014-11-07 11:53:45 +00:00
|
|
|
fn parse_for_sized(&mut self) -> Option<ast::TraitRef> {
|
2014-04-03 00:38:45 +00:00
|
|
|
if self.eat_keyword(keywords::For) {
|
2014-07-08 02:26:02 +00:00
|
|
|
let span = self.span;
|
|
|
|
let ident = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
if !self.eat(&token::Question) {
|
2014-07-08 02:26:02 +00:00
|
|
|
self.span_err(span,
|
|
|
|
"expected 'Sized?' after `for` in trait item");
|
|
|
|
return None;
|
2014-04-03 00:38:45 +00:00
|
|
|
}
|
2014-07-08 02:26:02 +00:00
|
|
|
let tref = Parser::trait_ref_from_ident(ident, span);
|
2014-11-07 11:53:45 +00:00
|
|
|
Some(tref)
|
2014-04-03 00:38:45 +00:00
|
|
|
} else {
|
2014-07-08 02:26:02 +00:00
|
|
|
None
|
2014-04-03 00:38:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Given a termination token and a vector of already-parsed
|
|
|
|
/// attributes (of length 0 or 1), parse all of the items in a module
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_mod_items(&mut self,
|
2013-07-02 19:47:32 +00:00
|
|
|
term: token::Token,
|
2014-04-26 20:05:45 +00:00
|
|
|
first_item_attrs: Vec<Attribute>,
|
|
|
|
inner_lo: BytePos)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> Mod {
|
2013-02-11 21:36:24 +00:00
|
|
|
// parse all of the items up to closing or an attribute.
|
|
|
|
// view items are legal here.
|
2013-02-21 08:16:31 +00:00
|
|
|
let ParsedItemsAndViewItems {
|
2014-10-06 00:36:53 +00:00
|
|
|
attrs_remaining,
|
|
|
|
view_items,
|
2013-02-21 08:16:31 +00:00
|
|
|
items: starting_items,
|
2013-11-28 20:22:53 +00:00
|
|
|
..
|
2013-07-02 19:47:32 +00:00
|
|
|
} = self.parse_items_and_view_items(first_item_attrs, true, true);
|
2014-09-13 16:06:01 +00:00
|
|
|
let mut items: Vec<P<Item>> = starting_items;
|
2013-03-20 01:24:01 +00:00
|
|
|
let attrs_remaining_len = attrs_remaining.len();
|
2012-08-14 21:22:52 +00:00
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// don't think this other loop is even necessary....
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let mut first = true;
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != term {
|
2012-05-24 20:44:42 +00:00
|
|
|
let mut attrs = self.parse_outer_attributes();
|
2012-06-28 06:09:51 +00:00
|
|
|
if first {
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut tmp = attrs_remaining.clone();
|
|
|
|
tmp.push_all(attrs.as_slice());
|
|
|
|
attrs = tmp;
|
2012-06-28 06:09:51 +00:00
|
|
|
first = false;
|
|
|
|
}
|
2014-10-15 01:59:41 +00:00
|
|
|
debug!("parse_mod_items: parse_item_or_view_item(attrs={})",
|
2012-08-14 00:11:52 +00:00
|
|
|
attrs);
|
2013-07-02 19:47:32 +00:00
|
|
|
match self.parse_item_or_view_item(attrs,
|
|
|
|
true /* macros allowed */) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(item) => items.push(item),
|
|
|
|
IoviViewItem(view_item) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
self.span_fatal(view_item.span,
|
|
|
|
"view items must be declared at the top of \
|
|
|
|
the module");
|
2012-08-14 00:11:52 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => {
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-08-23 10:41:32 +00:00
|
|
|
self.fatal(format!("expected item, found `{}`",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice())
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-23 20:15:51 +00:00
|
|
|
|
2013-03-20 01:24:01 +00:00
|
|
|
if first && attrs_remaining_len > 0u {
|
2012-05-23 22:06:11 +00:00
|
|
|
// We parsed attributes for the first item but didn't find it
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2014-09-12 01:07:07 +00:00
|
|
|
self.span_err(last_span,
|
|
|
|
Parser::expected_item_err(attrs_remaining.as_slice()));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-02-04 16:10:04 +00:00
|
|
|
|
2014-04-26 20:05:45 +00:00
|
|
|
ast::Mod {
|
|
|
|
inner: mk_sp(inner_lo, self.span.lo),
|
|
|
|
view_items: view_items,
|
|
|
|
items: items
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
fn parse_item_const(&mut self, m: Option<Mutability>) -> ItemInfo {
|
2013-03-06 17:30:54 +00:00
|
|
|
let id = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Eq);
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_expr();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.commit_expr_expecting(&*e, 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 {
|
|
|
|
Some(m) => ItemStatic(ty, m, e),
|
|
|
|
None => ItemConst(ty, e),
|
|
|
|
};
|
|
|
|
(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
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> ItemInfo {
|
2013-12-30 23:17:53 +00:00
|
|
|
let id_span = self.span;
|
2012-05-24 19:38:45 +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) {
|
2012-11-10 00:31:44 +00:00
|
|
|
self.bump();
|
|
|
|
// This mod is in an external file. Let's go get it!
|
2012-11-19 01:56:50 +00:00
|
|
|
let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
|
2013-02-15 09:15:53 +00:00
|
|
|
(id, m, Some(attrs))
|
2012-11-10 00:31:44 +00:00
|
|
|
} else {
|
2012-12-11 20:20:27 +00:00
|
|
|
self.push_mod_path(id, outer_attrs);
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2014-04-26 20:05:45 +00:00
|
|
|
let mod_inner_lo = self.span.lo;
|
2014-05-16 21:23:04 +00:00
|
|
|
let old_owns_directory = self.owns_directory;
|
|
|
|
self.owns_directory = true;
|
2013-02-21 08:16:31 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
2014-10-29 10:37:54 +00:00
|
|
|
let m = self.parse_mod_items(token::CloseDelim(token::Brace), next, mod_inner_lo);
|
|
|
|
self.expect(&token::CloseDelim(token::Brace));
|
2014-05-16 21:23:04 +00:00
|
|
|
self.owns_directory = old_owns_directory;
|
2012-12-11 20:20:27 +00:00
|
|
|
self.pop_mod_path();
|
2014-01-09 13:05:33 +00:00
|
|
|
(id, ItemMod(m), Some(inner))
|
2012-11-10 00:31:44 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
|
2014-01-10 22:02:36 +00:00
|
|
|
let default_path = self.id_to_interned_str(id);
|
2013-07-19 11:51:37 +00:00
|
|
|
let file_path = match ::attr::first_attr_value_str_by_name(attrs,
|
|
|
|
"path") {
|
2013-06-12 17:02:55 +00:00
|
|
|
Some(d) => d,
|
2014-01-10 22:02:36 +00:00
|
|
|
None => default_path,
|
2012-12-11 20:20:27 +00:00
|
|
|
};
|
|
|
|
self.mod_path_stack.push(file_path)
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn pop_mod_path(&mut self) {
|
2013-12-23 15:20:52 +00:00
|
|
|
self.mod_path_stack.pop().unwrap();
|
2012-12-11 20:20:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Read a module from a source file.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn eval_src_mod(&mut self,
|
2013-09-02 00:50:59 +00:00
|
|
|
id: ast::Ident,
|
2013-07-19 11:51:37 +00:00
|
|
|
outer_attrs: &[ast::Attribute],
|
2013-08-31 16:13:04 +00:00
|
|
|
id_sp: Span)
|
2014-02-28 21:09:09 +00:00
|
|
|
-> (ast::Item_, Vec<ast::Attribute> ) {
|
2014-03-16 18:56:24 +00:00
|
|
|
let mut prefix = Path::new(self.sess.span_diagnostic.cm.span_to_filename(self.span));
|
2013-09-27 00:21:59 +00:00
|
|
|
prefix.pop();
|
2014-02-28 20:54:01 +00:00
|
|
|
let mod_path = Path::new(".").join_many(self.mod_path_stack.as_slice());
|
2013-10-08 02:16:58 +00:00
|
|
|
let dir_path = prefix.join(&mod_path);
|
2014-05-16 21:23:04 +00:00
|
|
|
let mod_string = token::get_ident(id);
|
|
|
|
let (file_path, owns_directory) = match ::attr::first_attr_value_str_by_name(
|
2013-07-02 19:47:32 +00:00
|
|
|
outer_attrs, "path") {
|
2014-05-16 21:23:04 +00:00
|
|
|
Some(d) => (dir_path.join(d), true),
|
2013-07-20 02:11:30 +00:00
|
|
|
None => {
|
2014-05-25 10:10:11 +00:00
|
|
|
let mod_name = mod_string.get().to_string();
|
2014-05-20 00:23:26 +00:00
|
|
|
let default_path_str = format!("{}.rs", mod_name);
|
|
|
|
let secondary_path_str = format!("{}/mod.rs", mod_name);
|
2013-10-06 02:49:32 +00:00
|
|
|
let default_path = dir_path.join(default_path_str.as_slice());
|
|
|
|
let secondary_path = dir_path.join(secondary_path_str.as_slice());
|
2013-07-20 02:11:30 +00:00
|
|
|
let default_exists = default_path.exists();
|
|
|
|
let secondary_exists = secondary_path.exists();
|
2014-05-16 21:23:04 +00:00
|
|
|
|
|
|
|
if !self.owns_directory {
|
|
|
|
self.span_err(id_sp,
|
|
|
|
"cannot declare a new module at this location");
|
|
|
|
let this_module = match self.mod_path_stack.last() {
|
2014-05-25 10:17:19 +00:00
|
|
|
Some(name) => name.get().to_string(),
|
2014-10-15 06:05:01 +00:00
|
|
|
None => self.root_module_name.as_ref().unwrap().clone(),
|
2014-05-16 21:23:04 +00:00
|
|
|
};
|
|
|
|
self.span_note(id_sp,
|
|
|
|
format!("maybe move this module `{0}` \
|
|
|
|
to its own directory via \
|
2014-05-20 06:19:56 +00:00
|
|
|
`{0}/mod.rs`",
|
|
|
|
this_module).as_slice());
|
2014-05-16 21:23:04 +00:00
|
|
|
if default_exists || secondary_exists {
|
|
|
|
self.span_note(id_sp,
|
|
|
|
format!("... or maybe `use` the module \
|
|
|
|
`{}` instead of possibly \
|
2014-05-20 06:19:56 +00:00
|
|
|
redeclaring it",
|
|
|
|
mod_name).as_slice());
|
2014-05-16 21:23:04 +00:00
|
|
|
}
|
|
|
|
self.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2013-07-20 02:11:30 +00:00
|
|
|
match (default_exists, secondary_exists) {
|
2014-05-16 21:23:04 +00:00
|
|
|
(true, false) => (default_path, false),
|
|
|
|
(false, true) => (secondary_path, true),
|
2013-07-20 02:11:30 +00:00
|
|
|
(false, false) => {
|
2014-11-10 10:58:03 +00:00
|
|
|
self.span_fatal_help(id_sp,
|
|
|
|
format!("file not found for module `{}`",
|
|
|
|
mod_name).as_slice(),
|
|
|
|
format!("name the file either {} or {} inside \
|
|
|
|
the directory {}",
|
|
|
|
default_path_str,
|
|
|
|
secondary_path_str,
|
|
|
|
dir_path.display()).as_slice());
|
2013-07-20 02:11:30 +00:00
|
|
|
}
|
|
|
|
(true, true) => {
|
2014-11-10 10:58:03 +00:00
|
|
|
self.span_fatal_help(
|
2014-05-16 17:45:16 +00:00
|
|
|
id_sp,
|
|
|
|
format!("file for module `{}` found at both {} \
|
|
|
|
and {}",
|
|
|
|
mod_name,
|
|
|
|
default_path_str,
|
2014-11-10 10:58:03 +00:00
|
|
|
secondary_path_str).as_slice(),
|
|
|
|
"delete or rename one of them to remove the ambiguity");
|
2013-07-20 02:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 01:56:50 +00:00
|
|
|
};
|
|
|
|
|
2014-05-16 21:23:04 +00:00
|
|
|
self.eval_src_mod_from_path(file_path, owns_directory,
|
2014-05-25 10:17:19 +00:00
|
|
|
mod_string.get().to_string(), id_sp)
|
2012-11-19 01:56:50 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn eval_src_mod_from_path(&mut self,
|
2013-07-02 19:47:32 +00:00
|
|
|
path: Path,
|
2014-05-16 21:23:04 +00:00
|
|
|
owns_directory: bool,
|
2014-05-22 23:57:53 +00:00
|
|
|
name: String,
|
2014-02-28 21:09:09 +00:00
|
|
|
id_sp: Span) -> (ast::Item_, Vec<ast::Attribute> ) {
|
2014-03-20 22:05:37 +00:00
|
|
|
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
|
|
|
|
match included_mod_stack.iter().position(|p| *p == path) {
|
|
|
|
Some(i) => {
|
2014-05-22 23:57:53 +00:00
|
|
|
let mut err = String::from_str("circular modules: ");
|
2014-03-20 22:05:37 +00:00
|
|
|
let len = included_mod_stack.len();
|
|
|
|
for p in included_mod_stack.slice(i, len).iter() {
|
2014-11-21 22:10:42 +00:00
|
|
|
err.push_str(p.display().as_cow().as_slice());
|
2014-03-20 22:05:37 +00:00
|
|
|
err.push_str(" -> ");
|
2013-07-04 17:51:11 +00:00
|
|
|
}
|
2014-11-21 22:10:42 +00:00
|
|
|
err.push_str(path.display().as_cow().as_slice());
|
2014-05-20 00:23:26 +00:00
|
|
|
self.span_fatal(id_sp, err.as_slice());
|
2013-07-04 17:51:11 +00:00
|
|
|
}
|
2014-03-20 22:05:37 +00:00
|
|
|
None => ()
|
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
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
let mut p0 =
|
2013-07-02 19:47:32 +00:00
|
|
|
new_sub_parser_from_file(self.sess,
|
|
|
|
self.cfg.clone(),
|
2013-09-27 00:21:59 +00:00
|
|
|
&path,
|
2014-05-16 21:23:04 +00:00
|
|
|
owns_directory,
|
|
|
|
Some(name),
|
2013-07-02 19:47:32 +00:00
|
|
|
id_sp);
|
2014-04-26 20:05:45 +00:00
|
|
|
let mod_inner_lo = p0.span.lo;
|
2014-05-02 23:39:08 +00:00
|
|
|
let (mod_attrs, next) = p0.parse_inner_attrs_and_next();
|
2013-02-21 08:16:31 +00:00
|
|
|
let first_item_outer_attrs = next;
|
2014-10-27 08:22:52 +00:00
|
|
|
let m0 = p0.parse_mod_items(token::Eof, first_item_outer_attrs, mod_inner_lo);
|
2014-03-20 22:05:37 +00:00
|
|
|
self.sess.included_mod_stack.borrow_mut().pop();
|
2014-01-09 13:05:33 +00:00
|
|
|
return (ast::ItemMod(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
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_foreign_fn(&mut self, vis: ast::Visibility,
|
2014-09-13 16:06:01 +00:00
|
|
|
attrs: Vec<Attribute>) -> P<ForeignItem> {
|
2012-08-24 01:17:16 +00:00
|
|
|
let lo = self.span.lo;
|
2014-05-22 17:49:26 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2013-08-02 21:30:00 +00:00
|
|
|
|
2014-08-11 16:32:26 +00:00
|
|
|
let (ident, mut generics) = self.parse_fn_header();
|
2013-10-25 05:56:34 +00:00
|
|
|
let decl = self.parse_fn_decl(true);
|
2014-08-11 16:32:26 +00:00
|
|
|
self.parse_where_clause(&mut generics);
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-09-13 16:06:01 +00:00
|
|
|
P(ast::ForeignItem {
|
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
|
|
|
node: ForeignItemFn(decl, generics),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
vis: vis
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a static item from a foreign module
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_foreign_static(&mut self, vis: ast::Visibility,
|
2014-09-13 16:06:01 +00:00
|
|
|
attrs: Vec<Attribute>) -> P<ForeignItem> {
|
2012-08-25 22:09:33 +00:00
|
|
|
let lo = self.span.lo;
|
2013-03-19 21:46:27 +00:00
|
|
|
|
2013-10-03 09:53:46 +00:00
|
|
|
self.expect_keyword(keywords::Static);
|
2013-06-22 05:46:27 +00:00
|
|
|
let mutbl = self.eat_keyword(keywords::Mut);
|
2013-03-19 21:46:27 +00:00
|
|
|
|
2012-08-25 22:09:33 +00:00
|
|
|
let ident = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Colon);
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2012-08-25 22:09:33 +00:00
|
|
|
let hi = self.span.hi;
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-09-13 16:06:01 +00:00
|
|
|
P(ForeignItem {
|
2014-05-16 17:45:16 +00:00
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
|
|
|
node: ForeignItemStatic(ty, mutbl),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
2014-09-13 16:06:01 +00:00
|
|
|
vis: vis
|
|
|
|
})
|
2012-08-25 22:09:33 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// At this point, this is essentially a wrapper for
|
|
|
|
/// parse_foreign_items.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_foreign_mod_items(&mut self,
|
2014-04-02 08:19:41 +00:00
|
|
|
abi: abi::Abi,
|
2014-02-28 21:09:09 +00:00
|
|
|
first_item_attrs: Vec<Attribute> )
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ForeignMod {
|
2013-02-21 08:16:31 +00:00
|
|
|
let ParsedItemsAndViewItems {
|
2014-10-06 00:36:53 +00:00
|
|
|
attrs_remaining,
|
|
|
|
view_items,
|
2013-02-21 08:16:31 +00:00
|
|
|
items: _,
|
2014-10-06 00:36:53 +00:00
|
|
|
foreign_items,
|
2013-04-01 22:50:58 +00:00
|
|
|
} = self.parse_foreign_items(first_item_attrs, true);
|
2014-09-12 01:07:07 +00:00
|
|
|
if !attrs_remaining.is_empty() {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(last_span,
|
2014-09-12 01:07:07 +00:00
|
|
|
Parser::expected_item_err(attrs_remaining.as_slice()));
|
2013-04-30 19:02:56 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +00:00
|
|
|
assert!(self.token == token::CloseDelim(token::Brace));
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ForeignMod {
|
2014-04-02 08:19:41 +00:00
|
|
|
abi: abi,
|
2012-11-28 01:25:55 +00:00
|
|
|
view_items: view_items,
|
2013-04-01 22:50:58 +00:00
|
|
|
items: foreign_items
|
2013-01-16 00:05:20 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
|
2014-02-02 22:52:06 +00:00
|
|
|
/// Parse extern crate links
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-03-14 18:16:10 +00:00
|
|
|
/// extern crate url;
|
2014-08-23 04:02:00 +00:00
|
|
|
/// extern crate foo = "bar"; //deprecated
|
|
|
|
/// extern crate "bar" as foo;
|
2014-02-02 22:52:06 +00:00
|
|
|
fn parse_item_extern_crate(&mut self,
|
|
|
|
lo: BytePos,
|
|
|
|
visibility: Visibility,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> )
|
2014-02-02 22:52:06 +00:00
|
|
|
-> ItemOrViewItem {
|
2012-08-31 00:09:04 +00:00
|
|
|
|
2014-10-04 00:16:38 +00:00
|
|
|
let span = self.span;
|
2014-02-02 22:52:06 +00:00
|
|
|
let (maybe_path, ident) = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(..) => {
|
2013-07-31 20:47:32 +00:00
|
|
|
let the_ident = 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
|
|
|
let path = if self.token == token::Eq {
|
|
|
|
self.bump();
|
2014-09-01 15:59:23 +00:00
|
|
|
let path = self.parse_str();
|
|
|
|
let span = self.span;
|
2014-09-06 18:54:11 +00:00
|
|
|
self.obsolete(span, ObsoleteExternCrateRenaming);
|
2014-09-01 15:59:23 +00:00
|
|
|
Some(path)
|
2014-10-04 00:16:38 +00:00
|
|
|
} else if self.eat_keyword(keywords::As) {
|
|
|
|
// skip the ident if there is one
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_ident() { self.bump(); }
|
2014-10-04 00:16:38 +00:00
|
|
|
|
2014-11-10 10:58:03 +00:00
|
|
|
self.span_err(span, "expected `;`, found `as`");
|
|
|
|
self.span_help(span,
|
|
|
|
format!("perhaps you meant to enclose the crate name `{}` in \
|
|
|
|
a string?",
|
2014-10-04 00:16:38 +00:00
|
|
|
the_ident.as_str()).as_slice());
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-02-02 22:52:06 +00:00
|
|
|
(path, the_ident)
|
2014-08-23 04:02:00 +00:00
|
|
|
},
|
2014-11-19 04:48:38 +00:00
|
|
|
token::Literal(token::Str_(..), suf) | token::Literal(token::StrRaw(..), suf) => {
|
|
|
|
let sp = self.span;
|
|
|
|
self.expect_no_suffix(sp, "extern crate name", suf);
|
|
|
|
// forgo the internal suffix check of `parse_str` to
|
|
|
|
// avoid repeats (this unwrap will always succeed due
|
|
|
|
// to the restriction of the `match`)
|
|
|
|
let (s, style, _) = self.parse_optional_str().unwrap();
|
2014-08-23 04:02:00 +00:00
|
|
|
self.expect_keyword(keywords::As);
|
|
|
|
let the_ident = self.parse_ident();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-11-19 04:48:38 +00:00
|
|
|
(Some((s, style)), the_ident)
|
2014-08-23 04:02:00 +00:00
|
|
|
},
|
2012-08-31 00:09:04 +00:00
|
|
|
_ => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-06-14 03:48:09 +00:00
|
|
|
self.span_fatal(span,
|
2014-05-16 17:45:16 +00:00
|
|
|
format!("expected extern crate name but \
|
|
|
|
found `{}`",
|
|
|
|
token_str).as_slice());
|
2012-08-31 00:09:04 +00:00
|
|
|
}
|
2012-08-29 19:22:05 +00:00
|
|
|
};
|
2012-08-14 18:07:41 +00:00
|
|
|
|
2014-02-02 22:52:06 +00:00
|
|
|
IoviViewItem(ast::ViewItem {
|
2014-03-07 07:57:45 +00:00
|
|
|
node: ViewItemExternCrate(ident, maybe_path, ast::DUMMY_NODE_ID),
|
2014-02-02 22:52:06 +00:00
|
|
|
attrs: attrs,
|
|
|
|
vis: visibility,
|
|
|
|
span: mk_sp(lo, self.last_span.hi)
|
|
|
|
})
|
|
|
|
}
|
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,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> )
|
2014-02-02 22:52:06 +00:00
|
|
|
-> ItemOrViewItem {
|
2012-11-28 01:25:55 +00:00
|
|
|
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2012-08-14 18:07:41 +00:00
|
|
|
|
2014-04-02 08:19:41 +00:00
|
|
|
let abi = opt_abi.unwrap_or(abi::C);
|
2012-11-28 01:25:55 +00:00
|
|
|
|
2014-02-02 22:52:06 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
2014-04-02 08:19:41 +00:00
|
|
|
let m = self.parse_foreign_mod_items(abi, next);
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::CloseDelim(token::Brace));
|
2013-11-26 22:54:32 +00:00
|
|
|
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2014-02-02 22:52:06 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2014-02-14 05:07:09 +00:00
|
|
|
special_idents::invalid,
|
2014-02-02 22:52:06 +00:00
|
|
|
ItemForeignMod(m),
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, Some(inner)));
|
|
|
|
return IoviItem(item);
|
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;
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_type(&mut self) -> ItemInfo {
|
2013-03-28 22:45:09 +00:00
|
|
|
let ident = self.parse_ident();
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut tps = self.parse_generics();
|
|
|
|
self.parse_where_clause(&mut tps);
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Eq);
|
2014-11-20 20:05:29 +00:00
|
|
|
let ty = self.parse_ty_sum();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-01-09 13:05:33 +00:00
|
|
|
(ident, ItemTy(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 a structure-like enum variant definition
|
|
|
|
/// this should probably be renamed or refactored...
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_struct_def(&mut self) -> P<StructDef> {
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut fields: Vec<StructField> = Vec::new();
|
2014-10-29 10:37:54 +00:00
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
2014-11-16 01:57:54 +00:00
|
|
|
fields.push(self.parse_struct_decl_field(false));
|
2012-08-09 02:51:19 +00:00
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
P(StructDef {
|
2013-01-13 20:29:36 +00:00
|
|
|
fields: fields,
|
2014-02-24 07:17:02 +00:00
|
|
|
ctor_id: None,
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2012-08-09 02:51:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse the part of an "enum" decl following the '{'
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_enum_def(&mut self, _generics: &ast::Generics) -> 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) {
|
2012-05-24 20:44:42 +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
|
|
|
|
2012-08-03 20:58:14 +00:00
|
|
|
let vis = self.parse_visibility();
|
2012-08-15 17:58:35 +00:00
|
|
|
|
2013-06-05 04:43:41 +00:00
|
|
|
let ident;
|
|
|
|
let kind;
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut args = Vec::new();
|
2013-06-05 04:43:41 +00:00
|
|
|
let mut disr_expr = None;
|
2013-03-28 18:29:21 +00:00
|
|
|
ident = self.parse_ident();
|
2014-10-29 10:37:54 +00:00
|
|
|
if self.eat(&token::OpenDelim(token::Brace)) {
|
2013-03-28 18:29:21 +00:00
|
|
|
// Parse a struct variant.
|
|
|
|
all_nullary = false;
|
2014-11-17 02:13:21 +00:00
|
|
|
let start_span = self.span;
|
|
|
|
let struct_def = self.parse_struct_def();
|
|
|
|
if struct_def.fields.len() == 0 {
|
|
|
|
self.span_err(start_span,
|
|
|
|
format!("unit-like struct variant should be written \
|
|
|
|
without braces, as `{},`",
|
|
|
|
token::get_ident(ident)).as_slice());
|
|
|
|
}
|
|
|
|
kind = StructVariantKind(struct_def);
|
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;
|
2014-03-16 15:07:39 +00:00
|
|
|
let arg_tys = self.parse_enum_variant_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Paren),
|
|
|
|
&token::CloseDelim(token::Paren),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2014-11-20 20:05:29 +00:00
|
|
|
|p| p.parse_ty_sum()
|
2013-03-28 18:29:21 +00:00
|
|
|
);
|
2014-09-15 03:27:36 +00:00
|
|
|
for ty in arg_tys.into_iter() {
|
2014-01-09 13:05:33 +00:00
|
|
|
args.push(ast::VariantArg {
|
2013-07-06 04:57:11 +00:00
|
|
|
ty: ty,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-03-28 18:29:21 +00:00
|
|
|
});
|
2012-08-15 17:58:35 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
kind = TupleVariantKind(args);
|
2014-10-27 08:22:52 +00:00
|
|
|
} else if self.eat(&token::Eq) {
|
2013-03-28 18:29:21 +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);
|
2014-01-09 13:05:33 +00:00
|
|
|
kind = TupleVariantKind(args);
|
2013-03-28 18:29:21 +00:00
|
|
|
} else {
|
2014-02-28 21:09:09 +00:00
|
|
|
kind = TupleVariantKind(Vec::new());
|
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,
|
|
|
|
kind: kind,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-16 00:05:20 +00:00
|
|
|
disr_expr: disr_expr,
|
|
|
|
vis: vis,
|
|
|
|
};
|
2013-11-30 22:00:39 +00:00
|
|
|
variants.push(P(spanned(vlo, self.last_span.hi, vr)));
|
2012-01-25 13:10:33 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if !self.eat(&token::Comma) { break; }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-10-29 10:37:54 +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
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::EnumDef { variants: variants }
|
2012-08-08 21:17:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse an "enum" declaration
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_enum(&mut self) -> ItemInfo {
|
2012-08-08 21:17:52 +00:00
|
|
|
let id = self.parse_ident();
|
2014-08-11 16:32:26 +00:00
|
|
|
let mut generics = self.parse_generics();
|
|
|
|
self.parse_where_clause(&mut generics);
|
2014-10-29 10:37:54 +00:00
|
|
|
self.expect(&token::OpenDelim(token::Brace));
|
2012-08-08 21:17:52 +00:00
|
|
|
|
2013-02-28 15:25:31 +00:00
|
|
|
let enum_definition = self.parse_enum_def(&generics);
|
2014-01-09 13:05:33 +00:00
|
|
|
(id, ItemEnum(enum_definition, generics), None)
|
2012-01-10 21:50:40 +00:00
|
|
|
}
|
2010-11-24 19:36:35 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn fn_expr_lookahead(tok: &token::Token) -> bool {
|
2013-07-02 19:47:32 +00:00
|
|
|
match *tok {
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Paren) | token::At | token::Tilde | token::BinOp(_) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => false
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-12 20:31:45 +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.
|
2014-04-02 08:19:41 +00:00
|
|
|
fn parse_opt_abi(&mut self) -> 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);
|
2013-03-14 02:25:28 +00:00
|
|
|
self.bump();
|
2014-07-06 08:17:59 +00:00
|
|
|
let the_string = s.as_str();
|
2014-04-02 08:19:41 +00:00
|
|
|
match abi::lookup(the_string) {
|
|
|
|
Some(abi) => Some(abi),
|
|
|
|
None => {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2014-04-02 08:19:41 +00:00
|
|
|
self.span_err(
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span,
|
2014-05-16 17:45:16 +00:00
|
|
|
format!("illegal ABI: expected one of [{}], \
|
|
|
|
found `{}`",
|
|
|
|
abi::all_names().connect(", "),
|
|
|
|
the_string).as_slice());
|
2014-04-02 08:19:41 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 08:19:41 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse one of the items or view items allowed by the
|
|
|
|
/// flags; on failure, return IoviNone.
|
|
|
|
/// NB: this function no longer parses the items inside an
|
|
|
|
/// extern crate.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_item_or_view_item(&mut self,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> ,
|
2013-07-02 19:47:32 +00:00
|
|
|
macros_allowed: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ItemOrViewItem {
|
2014-09-13 16:06:01 +00:00
|
|
|
let nt_item = match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Interpolated(token::NtItem(ref item)) => {
|
2014-09-13 16:06:01 +00:00
|
|
|
Some((**item).clone())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match nt_item {
|
|
|
|
Some(mut item) => {
|
2013-08-08 17:28:06 +00:00
|
|
|
self.bump();
|
2014-09-13 16:06:01 +00:00
|
|
|
let mut attrs = attrs;
|
|
|
|
mem::swap(&mut item.attrs, &mut attrs);
|
2014-09-15 03:27:36 +00:00
|
|
|
item.attrs.extend(attrs.into_iter());
|
2014-09-13 16:06:01 +00:00
|
|
|
return IoviItem(P(item));
|
2013-08-08 17:28:06 +00:00
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
None => {}
|
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
|
|
|
|
2013-08-07 07:11:34 +00:00
|
|
|
let visibility = self.parse_visibility();
|
2013-04-01 22:50:58 +00:00
|
|
|
|
|
|
|
// must be a view item:
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Use) {
|
2014-01-09 13:05:33 +00:00
|
|
|
// USE ITEM (IoviViewItem)
|
2013-04-01 22:50:58 +00:00
|
|
|
let view_item = self.parse_use();
|
2014-10-27 08:22:52 +00:00
|
|
|
self.expect(&token::Semi);
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviViewItem(ast::ViewItem {
|
2013-04-01 22:50:58 +00:00
|
|
|
node: view_item,
|
|
|
|
attrs: attrs,
|
|
|
|
vis: visibility,
|
|
|
|
span: mk_sp(lo, self.last_span.hi)
|
|
|
|
});
|
2012-08-03 20:58:14 +00:00
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
// either a view item or an item:
|
2014-02-02 22:52:06 +00:00
|
|
|
if self.eat_keyword(keywords::Extern) {
|
|
|
|
let next_is_mod = self.eat_keyword(keywords::Mod);
|
|
|
|
|
|
|
|
if next_is_mod || self.eat_keyword(keywords::Crate) {
|
2014-02-14 18:03:53 +00:00
|
|
|
if next_is_mod {
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_err(mk_sp(lo, last_span.hi),
|
2014-02-14 18:10:06 +00:00
|
|
|
format!("`extern mod` is obsolete, use \
|
|
|
|
`extern crate` instead \
|
2014-05-16 17:45:16 +00:00
|
|
|
to refer to external \
|
|
|
|
crates.").as_slice())
|
2014-02-14 18:03:53 +00:00
|
|
|
}
|
2014-02-02 22:52:06 +00:00
|
|
|
return self.parse_item_extern_crate(lo, visibility, attrs);
|
|
|
|
}
|
|
|
|
|
2014-04-02 08:19:41 +00:00
|
|
|
let opt_abi = self.parse_opt_abi();
|
2012-08-03 20:58:14 +00:00
|
|
|
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Fn) {
|
2013-04-01 22:50:58 +00:00
|
|
|
// EXTERN FUNCTION ITEM
|
2014-04-02 08:19:41 +00:00
|
|
|
let abi = opt_abi.unwrap_or(abi::C);
|
2013-04-01 22:50:58 +00:00
|
|
|
let (ident, item_, extra_attrs) =
|
2014-12-09 15:36:46 +00:00
|
|
|
self.parse_item_fn(Unsafety::Normal, abi);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(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)) {
|
2014-04-02 08:19:41 +00:00
|
|
|
return 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
|
|
|
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2014-06-21 10:39:03 +00:00
|
|
|
let token_str = self.this_token_to_string();
|
2014-06-14 03:48:09 +00:00
|
|
|
self.span_fatal(span,
|
2014-08-23 10:41:32 +00:00
|
|
|
format!("expected `{}` or `fn`, found `{}`", "{",
|
2014-05-16 17:45:16 +00:00
|
|
|
token_str).as_slice());
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-02-02 22:52:06 +00:00
|
|
|
|
2014-10-11 17:24:58 +00:00
|
|
|
if self.eat_keyword(keywords::Virtual) {
|
2014-06-14 03:48:09 +00:00
|
|
|
let span = self.span;
|
2014-10-11 17:24:58 +00:00
|
|
|
self.span_err(span, "`virtual` structs have been removed from the language");
|
2014-02-24 07:17:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 22:50:58 +00:00
|
|
|
// the rest are all guaranteed to be items:
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Static) {
|
2013-10-03 09:53:46 +00:00
|
|
|
// STATIC ITEM
|
2013-03-19 21:46:27 +00:00
|
|
|
self.bump();
|
2014-10-02 22:06:08 +00:00
|
|
|
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
|
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 (ident, item_, extra_attrs) = self.parse_item_const(Some(m));
|
2014-10-02 22:06:08 +00:00
|
|
|
let last_span = self.last_span;
|
|
|
|
let item = self.mk_item(lo,
|
|
|
|
last_span.hi,
|
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
|
|
|
return IoviItem(item);
|
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Const) {
|
2014-10-02 22:06:08 +00:00
|
|
|
// CONST ITEM
|
|
|
|
self.bump();
|
|
|
|
if self.eat_keyword(keywords::Mut) {
|
|
|
|
let last_span = self.last_span;
|
2014-10-18 02:39:44 +00:00
|
|
|
self.span_err(last_span, "const globals cannot be mutable");
|
|
|
|
self.span_help(last_span, "did you mean to declare a static?");
|
2014-10-02 22:06:08 +00:00
|
|
|
}
|
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 (ident, item_, extra_attrs) = self.parse_item_const(None);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2014-12-10 00:59:20 +00:00
|
|
|
if self.token.is_keyword(keywords::Unsafe) &&
|
|
|
|
self.look_ahead(1u, |t| t.is_keyword(keywords::Trait))
|
|
|
|
{
|
|
|
|
// UNSAFE TRAIT ITEM
|
|
|
|
self.expect_keyword(keywords::Unsafe);
|
|
|
|
self.expect_keyword(keywords::Trait);
|
|
|
|
let (ident, item_, extra_attrs) =
|
|
|
|
self.parse_item_trait(ast::Unsafety::Unsafe);
|
|
|
|
let last_span = self.last_span;
|
|
|
|
let item = self.mk_item(lo,
|
|
|
|
last_span.hi,
|
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
|
|
|
return IoviItem(item);
|
|
|
|
}
|
2014-12-10 11:15:06 +00:00
|
|
|
if self.token.is_keyword(keywords::Unsafe) &&
|
|
|
|
self.look_ahead(1u, |t| t.is_keyword(keywords::Impl))
|
|
|
|
{
|
|
|
|
// IMPL ITEM
|
|
|
|
self.expect_keyword(keywords::Unsafe);
|
|
|
|
self.expect_keyword(keywords::Impl);
|
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe);
|
|
|
|
let last_span = self.last_span;
|
|
|
|
let item = self.mk_item(lo,
|
|
|
|
last_span.hi,
|
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
|
|
|
return IoviItem(item);
|
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Fn) &&
|
2013-12-30 22:04:00 +00:00
|
|
|
self.look_ahead(1, |f| !Parser::fn_expr_lookahead(f)) {
|
2013-03-29 17:35:23 +00:00
|
|
|
// FUNCTION ITEM
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-03-14 02:25:28 +00:00
|
|
|
let (ident, item_, extra_attrs) =
|
2014-12-09 15:36:46 +00:00
|
|
|
self.parse_item_fn(Unsafety::Normal, abi::Rust);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Unsafe)
|
2014-10-29 10:37:54 +00:00
|
|
|
&& self.look_ahead(1u, |t| *t != token::OpenDelim(token::Brace)) {
|
2013-04-01 22:50:58 +00:00
|
|
|
// UNSAFE FUNCTION ITEM
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-05-07 01:43:56 +00:00
|
|
|
let abi = if self.eat_keyword(keywords::Extern) {
|
|
|
|
self.parse_opt_abi().unwrap_or(abi::C)
|
|
|
|
} else {
|
|
|
|
abi::Rust
|
|
|
|
};
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2013-03-14 02:25:28 +00:00
|
|
|
let (ident, item_, extra_attrs) =
|
2014-12-09 15:36:46 +00:00
|
|
|
self.parse_item_fn(Unsafety::Unsafe, abi);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +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) =
|
|
|
|
self.parse_item_mod(attrs.as_slice());
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Type) {
|
2013-02-11 21:36:24 +00:00
|
|
|
// TYPE ITEM
|
2012-08-14 00:11:52 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_type();
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Enum) {
|
2013-02-11 21:36:24 +00:00
|
|
|
// ENUM ITEM
|
2012-08-14 00:11:52 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_enum();
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +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) =
|
|
|
|
self.parse_item_trait(ast::Unsafety::Normal);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Impl) {
|
2013-02-11 21:36:24 +00:00
|
|
|
// IMPL ITEM
|
2014-12-10 11:15:06 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Struct) {
|
2013-02-11 21:36:24 +00:00
|
|
|
// STRUCT ITEM
|
2014-10-11 17:24:58 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_struct();
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse a foreign item; on failure, return IoviNone.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_foreign_item(&mut self,
|
2014-02-28 21:09:09 +00:00
|
|
|
attrs: Vec<Attribute> ,
|
2013-07-02 19:47:32 +00:00
|
|
|
macros_allowed: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ItemOrViewItem {
|
|
|
|
maybe_whole!(iovi self, NtItem);
|
2013-04-01 22:50:58 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
2013-08-07 07:11:34 +00:00
|
|
|
let visibility = self.parse_visibility();
|
2013-04-01 22:50:58 +00:00
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Static) {
|
2013-10-03 09:53:46 +00:00
|
|
|
// FOREIGN STATIC ITEM
|
|
|
|
let item = self.parse_item_foreign_static(visibility, attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviForeignItem(item);
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-10-27 12:33:30 +00:00
|
|
|
if self.token.is_keyword(keywords::Fn) || self.token.is_keyword(keywords::Unsafe) {
|
2013-04-01 22:50:58 +00:00
|
|
|
// FOREIGN FUNCTION ITEM
|
2013-09-26 18:57:25 +00:00
|
|
|
let item = self.parse_item_foreign_fn(visibility, attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviForeignItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
|
|
|
|
}
|
|
|
|
|
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,
|
2014-01-09 13:05:33 +00:00
|
|
|
lo: BytePos,
|
|
|
|
visibility: Visibility
|
|
|
|
) -> ItemOrViewItem {
|
2014-10-27 12:33:30 +00:00
|
|
|
if macros_allowed && !self.token.is_any_keyword()
|
2014-10-27 08:22:52 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::Not)
|
2014-10-27 12:33:30 +00:00
|
|
|
&& (self.look_ahead(2, |t| t.is_plain_ident())
|
2014-10-29 10:37:54 +00:00
|
|
|
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
|
|
|
|
|| self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
|
2013-02-11 21:36:24 +00:00
|
|
|
// MACRO INVOCATION ITEM
|
2012-11-22 03:38:27 +00:00
|
|
|
|
2012-07-05 19:10:33 +00:00
|
|
|
// item macro.
|
2014-11-20 20:05:29 +00:00
|
|
|
let pth = self.parse_path(NoTypesAllowed);
|
2014-10-27 08:22:52 +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.
|
2014-10-27 12:33:30 +00:00
|
|
|
let id = if self.token.is_plain_ident() {
|
2012-11-09 04:12:45 +00:00
|
|
|
self.parse_ident()
|
2012-11-21 19:49:19 +00:00
|
|
|
} else {
|
|
|
|
token::special_idents::invalid // 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:
|
2014-10-29 14:47:53 +00:00
|
|
|
let delim = self.expect_open_delim();
|
|
|
|
let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
2013-02-11 21:36:24 +00:00
|
|
|
// single-variant-enum... :
|
2014-01-09 13:05:33 +00:00
|
|
|
let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
|
|
|
|
let m: ast::Mac = codemap::Spanned { node: m,
|
2013-01-29 21:54:06 +00:00
|
|
|
span: mk_sp(self.span.lo,
|
|
|
|
self.span.hi) };
|
2014-01-09 13:05:33 +00:00
|
|
|
let item_ = ItemMac(m);
|
2014-06-14 03:48:09 +00:00
|
|
|
let last_span = self.last_span;
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
2014-06-14 03:48:09 +00:00
|
|
|
last_span.hi,
|
2013-12-30 22:04:00 +00:00
|
|
|
id,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FAILURE TO PARSE ITEM
|
2014-11-10 10:58:03 +00:00
|
|
|
match visibility {
|
|
|
|
Inherited => {}
|
|
|
|
Public => {
|
|
|
|
let last_span = self.last_span;
|
|
|
|
self.span_fatal(last_span, "unmatched visibility `pub`");
|
2013-06-12 02:13:42 +00:00
|
|
|
}
|
2013-03-20 01:00:18 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviNone(attrs);
|
2012-08-14 00:11:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_item_with_outer_attributes(&mut self) -> Option<P<Item>> {
|
2014-06-12 23:40:10 +00:00
|
|
|
let attrs = self.parse_outer_attributes();
|
|
|
|
self.parse_item(attrs)
|
|
|
|
}
|
|
|
|
|
2014-09-13 16:06:01 +00:00
|
|
|
pub fn parse_item(&mut self, attrs: Vec<Attribute>) -> Option<P<Item>> {
|
2013-04-01 22:50:58 +00:00
|
|
|
match self.parse_item_or_view_item(attrs, true) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviNone(_) => None,
|
|
|
|
IoviViewItem(_) =>
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("view items are not allowed here"),
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviForeignItem(_) =>
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("foreign items are not allowed here"),
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(item) => Some(item)
|
2012-08-14 00:11:52 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 21:36:27 +00:00
|
|
|
/// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo`
|
|
|
|
pub fn parse_view_item(&mut self, attrs: Vec<Attribute>) -> ViewItem {
|
|
|
|
match self.parse_item_or_view_item(attrs, false) {
|
|
|
|
IoviViewItem(vi) => vi,
|
|
|
|
_ => self.fatal("expected `use` or `extern crate`"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parse, e.g., "use a::b::{z,y}"
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_use(&mut self) -> ViewItem_ {
|
2014-05-22 17:49:26 +00:00
|
|
|
return ViewItemUse(self.parse_view_path());
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Matches view_path : MOD? IDENT EQ non_global_path
|
|
|
|
/// | MOD? non_global_path MOD_SEP LBRACE RBRACE
|
|
|
|
/// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE
|
|
|
|
/// | MOD? non_global_path MOD_SEP STAR
|
|
|
|
/// | MOD? non_global_path
|
2014-09-13 16:06:01 +00:00
|
|
|
fn parse_view_path(&mut self) -> P<ViewPath> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2012-08-31 18:19:07 +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)) {
|
2013-12-04 21:08:42 +00:00
|
|
|
// use {foo,bar}
|
|
|
|
let idents = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Brace),
|
|
|
|
&token::CloseDelim(token::Brace),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2014-07-17 22:56:56 +00:00
|
|
|
|p| p.parse_path_list_item());
|
2013-12-04 21:08:42 +00:00
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
2014-02-28 21:09:09 +00:00
|
|
|
segments: Vec::new()
|
2013-12-04 21:08:42 +00:00
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
return P(spanned(lo, self.span.hi,
|
|
|
|
ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
|
2013-12-04 21:08:42 +00:00
|
|
|
}
|
|
|
|
|
2012-05-24 19:38:45 +00:00
|
|
|
let first_ident = self.parse_ident();
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut path = vec!(first_ident);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Eq => {
|
2012-05-23 22:06:11 +00:00
|
|
|
// x = foo::bar
|
|
|
|
self.bump();
|
2014-01-09 07:12:23 +00:00
|
|
|
let path_lo = self.span.lo;
|
2014-02-28 21:09:09 +00:00
|
|
|
path = vec!(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
|
|
|
while self.check(&token::ModSep) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-05-24 19:38:45 +00:00
|
|
|
let id = self.parse_ident();
|
2012-09-27 00:33:34 +00:00
|
|
|
path.push(id);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2014-08-18 15:29:44 +00:00
|
|
|
let span = mk_sp(path_lo, self.span.hi);
|
|
|
|
self.obsolete(span, ObsoleteImportRenaming);
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
2014-08-18 15:29:44 +00:00
|
|
|
span: span,
|
2013-08-07 16:47:28 +00:00
|
|
|
global: false,
|
2014-09-15 03:27:36 +00:00
|
|
|
segments: path.into_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: ast::PathParameters::none(),
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
return P(spanned(lo, self.span.hi,
|
|
|
|
ViewPathSimple(first_ident, path,
|
|
|
|
ast::DUMMY_NODE_ID)));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-09-12 10:39:38 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
token::ModSep => {
|
2012-05-23 22:06:11 +00:00
|
|
|
// foo::bar or foo::{a,b,c} or foo::*
|
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
|
|
|
while self.check(&token::ModSep) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2011-08-16 22:21:30 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-10-27 08:22:52 +00:00
|
|
|
token::Ident(i, _) => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-09-27 00:33:34 +00:00
|
|
|
path.push(i);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-09-02 22:34:58 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
// foo::bar::{a,b,c}
|
2014-10-29 10:37:54 +00:00
|
|
|
token::OpenDelim(token::Brace) => {
|
2012-06-12 17:17:36 +00:00
|
|
|
let idents = self.parse_unspanned_seq(
|
2014-10-29 10:37:54 +00:00
|
|
|
&token::OpenDelim(token::Brace),
|
|
|
|
&token::CloseDelim(token::Brace),
|
2014-10-27 08:22:52 +00:00
|
|
|
seq_sep_trailing_allowed(token::Comma),
|
2014-07-17 22:56:56 +00:00
|
|
|
|p| p.parse_path_list_item()
|
2013-02-24 23:41:54 +00:00
|
|
|
);
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
2014-09-15 03:27:36 +00:00
|
|
|
segments: path.into_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: ast::PathParameters::none(),
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
return P(spanned(lo, self.span.hi,
|
|
|
|
ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-09-12 09:27:30 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
// foo::bar::*
|
2014-10-27 08:22:52 +00:00
|
|
|
token::BinOp(token::Star) => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
2014-09-15 03:27:36 +00:00
|
|
|
segments: path.into_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: ast::PathParameters::none(),
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
return P(spanned(lo, self.span.hi,
|
|
|
|
ViewPathGlob(path, ast::DUMMY_NODE_ID)));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-09-12 10:39:38 +00:00
|
|
|
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => break
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-08-16 22:21:30 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => ()
|
2011-01-28 16:54:59 +00:00
|
|
|
}
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut rename_to = path[path.len() - 1u];
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
2014-09-15 03:27:36 +00:00
|
|
|
segments: path.into_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2014-11-04 02:52:52 +00:00
|
|
|
parameters: ast::PathParameters::none(),
|
2013-08-07 16:47:28 +00:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2014-08-13 02:25:05 +00:00
|
|
|
if self.eat_keyword(keywords::As) {
|
|
|
|
rename_to = self.parse_ident()
|
|
|
|
}
|
2014-09-13 16:06:01 +00:00
|
|
|
P(spanned(lo, self.last_span.hi,
|
|
|
|
ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID)))
|
2011-01-28 16:54:59 +00:00
|
|
|
}
|
2010-12-25 04:25:02 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses a sequence of items. Stops when it finds program
|
|
|
|
/// text that can't be parsed as an item
|
|
|
|
/// - mod_items uses extern_mod_allowed = true
|
|
|
|
/// - block_tail_ uses extern_mod_allowed = false
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_items_and_view_items(&mut self,
|
2014-02-28 21:09:09 +00:00
|
|
|
first_item_attrs: Vec<Attribute> ,
|
2013-04-01 22:50:58 +00:00
|
|
|
mut extern_mod_allowed: bool,
|
2012-11-21 19:49:19 +00:00
|
|
|
macros_allowed: bool)
|
2013-04-24 08:29:46 +00:00
|
|
|
-> ParsedItemsAndViewItems {
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut attrs = first_item_attrs;
|
|
|
|
attrs.push_all(self.parse_outer_attributes().as_slice());
|
2013-03-27 02:53:33 +00:00
|
|
|
// First, parse view items.
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut view_items : Vec<ast::ViewItem> = Vec::new();
|
|
|
|
let mut items = Vec::new();
|
2013-07-18 03:04:37 +00:00
|
|
|
|
2013-04-01 22:50:58 +00:00
|
|
|
// I think this code would probably read better as a single
|
2014-02-14 18:10:06 +00:00
|
|
|
// loop with a mutable three-state-variable (for extern crates,
|
2013-04-01 22:50:58 +00:00
|
|
|
// view items, and regular items) ... except that because
|
|
|
|
// of macros, I'd like to delay that entire check until later.
|
|
|
|
loop {
|
2013-07-02 19:47:32 +00:00
|
|
|
match self.parse_item_or_view_item(attrs, macros_allowed) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviNone(attrs) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
return ParsedItemsAndViewItems {
|
|
|
|
attrs_remaining: attrs,
|
|
|
|
view_items: view_items,
|
|
|
|
items: items,
|
2014-02-28 21:09:09 +00:00
|
|
|
foreign_items: Vec::new()
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2013-03-27 02:53:33 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(view_item) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
match view_item.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ViewItemUse(..) => {
|
2014-02-14 18:10:06 +00:00
|
|
|
// `extern crate` must precede `use`.
|
2013-04-01 22:50:58 +00:00
|
|
|
extern_mod_allowed = false;
|
2013-03-27 02:53:33 +00:00
|
|
|
}
|
2014-03-07 07:57:45 +00:00
|
|
|
ViewItemExternCrate(..) if !extern_mod_allowed => {
|
2013-04-01 22:50:58 +00:00
|
|
|
self.span_err(view_item.span,
|
2014-05-16 17:45:16 +00:00
|
|
|
"\"extern crate\" declarations are \
|
|
|
|
not allowed here");
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-03-07 07:57:45 +00:00
|
|
|
ViewItemExternCrate(..) => {}
|
2012-08-14 21:22:52 +00:00
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
view_items.push(view_item);
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(item) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
items.push(item);
|
|
|
|
attrs = self.parse_outer_attributes();
|
|
|
|
break;
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviForeignItem(_) => {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!();
|
2012-08-14 21:22:52 +00:00
|
|
|
}
|
2013-03-27 02:53:33 +00:00
|
|
|
}
|
2013-04-01 22:50:58 +00:00
|
|
|
attrs = self.parse_outer_attributes();
|
2013-03-27 02:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, parse items.
|
2013-07-02 19:47:32 +00:00
|
|
|
loop {
|
|
|
|
match self.parse_item_or_view_item(attrs, macros_allowed) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviNone(returned_attrs) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
attrs = returned_attrs;
|
|
|
|
break
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(view_item) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
attrs = self.parse_outer_attributes();
|
|
|
|
self.span_err(view_item.span,
|
2014-02-14 18:10:06 +00:00
|
|
|
"`use` and `extern crate` declarations must precede items");
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(item) => {
|
2013-07-02 19:47:32 +00:00
|
|
|
attrs = self.parse_outer_attributes();
|
|
|
|
items.push(item)
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviForeignItem(_) => {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!();
|
2012-10-09 09:59:03 +00:00
|
|
|
}
|
2012-08-14 21:22:52 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-14 21:22:52 +00:00
|
|
|
|
2013-02-21 08:16:31 +00:00
|
|
|
ParsedItemsAndViewItems {
|
|
|
|
attrs_remaining: attrs,
|
|
|
|
view_items: view_items,
|
|
|
|
items: items,
|
2014-02-28 21:09:09 +00:00
|
|
|
foreign_items: Vec::new()
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Parses a sequence of foreign items. Stops when it finds program
|
|
|
|
/// text that can't be parsed as an item
|
2014-02-28 21:09:09 +00:00
|
|
|
fn parse_foreign_items(&mut self, first_item_attrs: Vec<Attribute> ,
|
2013-04-01 22:50:58 +00:00
|
|
|
macros_allowed: bool)
|
|
|
|
-> ParsedItemsAndViewItems {
|
2014-10-15 06:05:01 +00:00
|
|
|
let mut attrs = first_item_attrs;
|
|
|
|
attrs.push_all(self.parse_outer_attributes().as_slice());
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut foreign_items = Vec::new();
|
2013-04-01 22:50:58 +00:00
|
|
|
loop {
|
2013-07-02 19:47:32 +00:00
|
|
|
match self.parse_foreign_item(attrs, macros_allowed) {
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviNone(returned_attrs) => {
|
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)) {
|
2013-07-02 19:47:32 +00:00
|
|
|
attrs = returned_attrs;
|
2013-05-22 07:11:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
self.unexpected();
|
|
|
|
},
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(view_item) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
// I think this can't occur:
|
|
|
|
self.span_err(view_item.span,
|
2014-02-14 18:10:06 +00:00
|
|
|
"`use` and `extern crate` declarations must precede items");
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviItem(item) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
// FIXME #5668: this will occur for a macro invocation:
|
2013-06-06 23:28:38 +00:00
|
|
|
self.span_fatal(item.span, "macros cannot expand to foreign items");
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviForeignItem(foreign_item) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
foreign_items.push(foreign_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attrs = self.parse_outer_attributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedItemsAndViewItems {
|
|
|
|
attrs_remaining: attrs,
|
2014-02-28 21:09:09 +00:00
|
|
|
view_items: Vec::new(),
|
|
|
|
items: Vec::new(),
|
2013-02-21 08:16:31 +00:00
|
|
|
foreign_items: foreign_items
|
|
|
|
}
|
2012-01-21 03:44:38 +00:00
|
|
|
}
|
2012-01-16 00:25:31 +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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_crate_mod(&mut self) -> Crate {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-02-11 21:36:24 +00:00
|
|
|
// parse the crate's inner attrs, maybe (oops) one
|
|
|
|
// of the attrs of an item:
|
2013-02-21 08:16:31 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
|
|
|
let first_item_outer_attrs = next;
|
2013-02-11 21:36:24 +00:00
|
|
|
// parse the items inside the crate:
|
2014-10-27 08:22:52 +00:00
|
|
|
let m = self.parse_mod_items(token::Eof, first_item_outer_attrs, lo);
|
2013-07-19 05:38:55 +00:00
|
|
|
|
2013-09-28 02:46:09 +00:00
|
|
|
ast::Crate {
|
2013-07-19 05:38:55 +00:00
|
|
|
module: m,
|
|
|
|
attrs: inner,
|
|
|
|
config: self.cfg.clone(),
|
2014-07-10 22:41:11 +00:00
|
|
|
span: mk_sp(lo, self.span.lo),
|
|
|
|
exported_macros: Vec::new(),
|
2013-07-19 05:38:55 +00:00
|
|
|
}
|
2011-06-23 22:42:55 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-01-16 02:30:40 +00:00
|
|
|
pub fn parse_optional_str(&mut self)
|
2014-11-19 04:48:38 +00:00
|
|
|
-> Option<(InternedString, ast::StrStyle, Option<ast::Name>)> {
|
|
|
|
let ret = match self.token {
|
|
|
|
token::Literal(token::Str_(s), suf) => {
|
|
|
|
(self.id_to_interned_str(s.ident()), ast::CookedStr, suf)
|
|
|
|
}
|
|
|
|
token::Literal(token::StrRaw(s, n), suf) => {
|
|
|
|
(self.id_to_interned_str(s.ident()), ast::RawStr(n), suf)
|
2014-01-16 02:30:40 +00:00
|
|
|
}
|
2013-10-08 00:49:10 +00:00
|
|
|
_ => return None
|
|
|
|
};
|
|
|
|
self.bump();
|
2014-11-19 04:48:38 +00:00
|
|
|
Some(ret)
|
2013-07-12 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 02:30:40 +00:00
|
|
|
pub fn parse_str(&mut self) -> (InternedString, StrStyle) {
|
2013-07-12 21:43:57 +00:00
|
|
|
match self.parse_optional_str() {
|
2014-11-19 04:48:38 +00:00
|
|
|
Some((s, style, suf)) => {
|
|
|
|
let sp = self.last_span;
|
|
|
|
self.expect_no_suffix(sp, "str literal", suf);
|
|
|
|
(s, style)
|
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
_ => 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
|
|
|
}
|