2013-03-02 03:57:05 +00:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
2013-08-08 17:28:06 +00:00
|
|
|
#[macro_escape];
|
|
|
|
|
2013-03-14 02:25:28 +00:00
|
|
|
use abi;
|
|
|
|
use abi::AbiSet;
|
|
|
|
use ast::{Sigil, BorrowedSigil, ManagedSigil, OwnedSigil};
|
2013-08-03 03:20:22 +00:00
|
|
|
use ast::{CallSugar, NoSugar, DoSugar};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{BareFnTy, ClosureTy};
|
2013-02-01 01:12:29 +00:00
|
|
|
use ast::{RegionTyParamBound, TraitTyParamBound};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{Provided, Public, Purity};
|
|
|
|
use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{BiBitAnd, BiBitOr, BiBitXor, Block};
|
|
|
|
use ast::{BlockCheckMode, UnBox};
|
|
|
|
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};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{ExprBreak, ExprCall, ExprCast, ExprDoBody};
|
|
|
|
use ast::{ExprField, ExprFnBlock, ExprIf, ExprIndex};
|
2013-08-28 06:12:05 +00:00
|
|
|
use ast::{ExprLit, ExprLogLevel, ExprLoop, ExprMac};
|
2013-10-28 22:22:49 +00:00
|
|
|
use ast::{ExprMethodCall, ExprParen, ExprPath, ExprProc, ExprRepeat};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{ExprRet, ExprSelf, ExprStruct, ExprTup, ExprUnary};
|
2013-12-31 20:55:39 +00:00
|
|
|
use ast::{ExprVec, ExprVstore, ExprVstoreSlice, ExprVstoreBox};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{ExprVstoreMutSlice, ExprWhile, ExprForLoop, ExternFn, Field, FnDecl};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{ExprVstoreUniq, Onceness, Once, Many};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod};
|
|
|
|
use ast::{Ident, ImpureFn, Inherited, Item, Item_, ItemStatic};
|
|
|
|
use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl};
|
|
|
|
use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy, Lit, Lit_};
|
|
|
|
use ast::{LitBool, LitFloat, LitFloatUnsuffixed, LitInt, LitChar};
|
|
|
|
use ast::{LitIntUnsuffixed, LitNil, LitStr, LitUint, Local};
|
|
|
|
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, Matcher, MatchNonterminal};
|
|
|
|
use ast::{MatchSeq, MatchTok, Method, MutTy, BiMul, Mutability};
|
|
|
|
use ast::{NamedField, UnNeg, NoReturn, UnNot, P, Pat, PatBox, PatEnum};
|
2013-09-02 01:45:37 +00:00
|
|
|
use ast::{PatIdent, PatLit, PatRange, PatRegion, PatStruct};
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{PatTup, PatUniq, PatWild, PatWildMulti, Private};
|
|
|
|
use ast::{BiRem, Required};
|
|
|
|
use ast::{RetStyle, Return, BiShl, BiShr, Stmt, StmtDecl};
|
|
|
|
use ast::{StmtExpr, StmtSemi, StmtMac, StructDef, StructField};
|
|
|
|
use ast::{StructVariantKind, BiSub};
|
2013-10-08 00:49:10 +00:00
|
|
|
use ast::StrStyle;
|
2014-01-09 13:05:33 +00:00
|
|
|
use ast::{SelfBox, SelfRegion, SelfStatic, SelfUniq, SelfValue};
|
|
|
|
use ast::{TokenTree, TraitMethod, TraitRef, TTDelim, TTSeq, TTTok};
|
|
|
|
use ast::{TTNonterminal, TupleVariantKind, Ty, Ty_, TyBot, TyBox};
|
|
|
|
use ast::{TypeField, TyFixedLengthVec, TyClosure, TyBareFn, TyTypeof};
|
|
|
|
use ast::{TyInfer, TypeMethod};
|
|
|
|
use ast::{TyNil, TyParam, TyParamBound, TyPath, TyPtr, TyRptr};
|
|
|
|
use ast::{TyTup, TyU32, TyUniq, TyVec, UnUniq};
|
|
|
|
use ast::{UnnamedField, UnsafeBlock, UnsafeFn, ViewItem};
|
|
|
|
use ast::{ViewItem_, ViewItemExternMod, ViewItemUse};
|
|
|
|
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
|
|
|
|
use ast::Visibility;
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2013-06-25 05:13:22 +00:00
|
|
|
use ast_util::{as_prec, operator_prec};
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast_util;
|
2013-08-31 16:13:04 +00:00
|
|
|
use codemap::{Span, BytePos, Spanned, spanned, mk_sp};
|
2012-12-23 22:41:37 +00:00
|
|
|
use codemap;
|
2014-01-09 13:05:33 +00:00
|
|
|
use parse::attr::ParserAttr;
|
2013-03-01 18:44:43 +00:00
|
|
|
use parse::classify;
|
2013-06-15 01:21:47 +00:00
|
|
|
use parse::common::{SeqSep, seq_sep_none};
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed};
|
2014-01-09 13:05:33 +00:00
|
|
|
use parse::lexer::Reader;
|
2013-01-30 17:56:33 +00:00
|
|
|
use parse::lexer::TokenAndSpan;
|
2013-08-09 08:25:24 +00:00
|
|
|
use parse::obsolete::*;
|
2013-06-07 22:47:05 +00:00
|
|
|
use parse::token::{can_begin_expr, get_ident_interner, ident_to_str, is_ident};
|
|
|
|
use parse::token::{is_ident_or_path};
|
|
|
|
use parse::token::{is_plain_ident, INTERPOLATED, keywords, special_idents};
|
|
|
|
use parse::token::{token_to_binop};
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse::token;
|
2013-09-07 02:11:55 +00:00
|
|
|
use parse::{new_sub_parser_from_file, ParseSess};
|
2013-02-15 05:50:03 +00:00
|
|
|
use opt_vec;
|
|
|
|
use opt_vec::OptVec;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2013-12-28 00:11:33 +00:00
|
|
|
use std::cell::Cell;
|
2013-06-25 00:40:33 +00:00
|
|
|
use std::hashmap::HashSet;
|
2013-07-02 19:47:32 +00:00
|
|
|
use std::util;
|
2013-06-25 00:40:33 +00:00
|
|
|
use std::vec;
|
2010-08-18 22:41:13 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
2013-03-20 15:52:45 +00:00
|
|
|
#[deriving(Eq)]
|
2012-01-19 22:24:03 +00:00
|
|
|
enum restriction {
|
2012-01-20 01:56:05 +00:00
|
|
|
UNRESTRICTED,
|
|
|
|
RESTRICT_STMT_EXPR,
|
|
|
|
RESTRICT_NO_BAR_OP,
|
2012-06-30 01:09:56 +00:00
|
|
|
RESTRICT_NO_BAR_OR_DOUBLEBAR_OP,
|
2011-12-21 04:12:52 +00:00
|
|
|
}
|
2011-01-24 23:26:10 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
type ItemInfo = (Ident, Item_, Option<~[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.
|
|
|
|
#[deriving(Eq)]
|
|
|
|
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,
|
|
|
|
/// A path with a lifetime and type parameters with bounds before the last
|
2013-12-10 07:16:18 +00:00
|
|
|
/// set of type parameters only; e.g. `foo::bar<'a>::Baz:X+Y<T>` This
|
2013-08-07 16:47:28 +00:00
|
|
|
/// form does not use extra double colons.
|
|
|
|
LifetimeAndTypesAndBounds,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A pair of a path segment and group of type parameter bounds. (See `ast.rs`
|
|
|
|
/// for the definition of a path segment.)
|
|
|
|
struct PathSegmentAndBoundSet {
|
|
|
|
segment: ast::PathSegment,
|
|
|
|
bound_set: Option<OptVec<TyParamBound>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A path paired with optional type bounds.
|
|
|
|
struct PathAndBounds {
|
|
|
|
path: ast::Path,
|
|
|
|
bounds: Option<OptVec<TyParamBound>>,
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
enum ItemOrViewItem {
|
2013-07-02 19:47:32 +00:00
|
|
|
// Indicates a failure to parse any kind of item. The attributes are
|
|
|
|
// returned.
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviNone(~[Attribute]),
|
|
|
|
IoviItem(@Item),
|
|
|
|
IoviForeignItem(@ForeignItem),
|
|
|
|
IoviViewItem(ViewItem)
|
2012-08-14 00:11:52 +00:00
|
|
|
}
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2012-07-26 17:14:01 +00:00
|
|
|
/* The expr situation is not as complex as I thought it would be.
|
|
|
|
The important thing is to make sure that lookahead doesn't balk
|
2012-07-28 02:14:46 +00:00
|
|
|
at 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
|
|
|
{
|
|
|
|
// This horrible convolution is brought to you by
|
|
|
|
// @mut, have a terrible day
|
2013-12-30 23:09:41 +00:00
|
|
|
let mut maybe_path = match ($p).token {
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(token::NtPath(ref pt)) => Some((**pt).clone()),
|
2013-12-30 23:09:41 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
let ret = match ($p).token {
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(token::NtExpr(e)) => {
|
2013-07-05 10:15:21 +00:00
|
|
|
Some(e)
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(token::NtPath(_)) => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let pt = maybe_path.take_unwrap();
|
|
|
|
Some($p.mk_expr(($p).span.lo, ($p).span.hi, ExprPath(pt)))
|
2013-07-05 10:15:21 +00:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match ret {
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-07-02 19:47:32 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
|
|
|
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) => (
|
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-11-30 22:00:39 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2013-03-02 21:02:27 +00:00
|
|
|
(deref $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-07-02 19:47:32 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
|
|
|
return (*x).clone()
|
|
|
|
}
|
|
|
|
_ => {}
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(Some $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-07-02 19:47:32 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
|
|
|
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
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-07-02 19:47:32 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(x.clone())
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(pair_empty $p:expr, $constructor:ident) => (
|
2013-07-02 19:47:32 +00:00
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let __found__ = match ($p).token {
|
2013-07-02 19:47:32 +00:00
|
|
|
INTERPOLATED(token::$constructor(_)) => {
|
|
|
|
Some(($p).bump_and_get())
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
match __found__ {
|
2013-11-30 22:00:39 +00:00
|
|
|
Some(INTERPOLATED(token::$constructor(x))) => {
|
|
|
|
return (~[], x)
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
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
|
|
|
|
2013-07-19 11:51:37 +00:00
|
|
|
fn maybe_append(lhs: ~[Attribute], rhs: Option<~[Attribute]>)
|
|
|
|
-> ~[Attribute] {
|
2012-08-14 18:07:41 +00:00
|
|
|
match rhs {
|
2012-08-20 19:23:37 +00:00
|
|
|
None => lhs,
|
2012-12-04 18:50:00 +00:00
|
|
|
Some(ref attrs) => vec::append(lhs, (*attrs))
|
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 {
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs_remaining: ~[Attribute],
|
2014-01-09 13:05:33 +00:00
|
|
|
view_items: ~[ViewItem],
|
|
|
|
items: ~[@Item],
|
|
|
|
foreign_items: ~[@ForeignItem]
|
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-01-09 13:05:33 +00:00
|
|
|
pub fn Parser(sess: @ParseSess, cfg: ast::CrateConfig, rdr: @Reader)
|
2013-12-27 19:56:29 +00:00
|
|
|
-> Parser {
|
2013-07-02 19:47:32 +00:00
|
|
|
let tok0 = rdr.next_token();
|
2013-05-17 17:18:35 +00:00
|
|
|
let interner = get_ident_interner();
|
2013-07-02 19:47:32 +00:00
|
|
|
let span = tok0.sp;
|
|
|
|
let placeholder = TokenAndSpan {
|
|
|
|
tok: token::UNDERSCORE,
|
|
|
|
sp: span,
|
|
|
|
};
|
2012-09-05 22:58:43 +00:00
|
|
|
|
2012-10-15 21:56:42 +00:00
|
|
|
Parser {
|
2013-02-15 09:15:53 +00:00
|
|
|
reader: rdr,
|
|
|
|
interner: interner,
|
2012-09-05 22:58:43 +00:00
|
|
|
sess: sess,
|
|
|
|
cfg: cfg,
|
2013-12-30 23:09:41 +00:00
|
|
|
token: tok0.tok,
|
2013-12-30 23:17:53 +00:00
|
|
|
span: span,
|
2013-12-30 23:30:14 +00:00
|
|
|
last_span: span,
|
2013-12-30 23:30:56 +00:00
|
|
|
last_token: None,
|
2013-12-30 23:31:40 +00:00
|
|
|
buffer: [
|
2013-07-10 23:00:11 +00:00
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
|
|
|
placeholder.clone(),
|
2013-12-30 23:31:40 +00:00
|
|
|
],
|
2013-12-30 23:32:56 +00:00
|
|
|
buffer_start: 0,
|
|
|
|
buffer_end: 0,
|
2013-12-30 23:33:39 +00:00
|
|
|
tokens_consumed: 0,
|
2013-12-30 23:34:28 +00:00
|
|
|
restriction: UNRESTRICTED,
|
2013-12-30 22:40:31 +00:00
|
|
|
quote_depth: 0,
|
2013-12-30 23:36:23 +00:00
|
|
|
obsolete_set: HashSet::new(),
|
2013-12-30 23:38:02 +00:00
|
|
|
mod_path_stack: ~[],
|
2013-12-30 23:41:11 +00:00
|
|
|
open_braces: ~[],
|
2013-10-29 09:53:56 +00:00
|
|
|
non_copyable: util::NonCopyable
|
2012-09-05 22:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:54:06 +00:00
|
|
|
pub struct Parser {
|
2013-12-27 19:56:29 +00:00
|
|
|
sess: @ParseSess,
|
2013-07-19 05:38:55 +00:00
|
|
|
cfg: CrateConfig,
|
2013-04-15 23:13:42 +00:00
|
|
|
// the current token:
|
2013-12-30 23:09:41 +00:00
|
|
|
token: token::Token,
|
2013-04-15 23:13:42 +00:00
|
|
|
// the span of the current token:
|
2013-12-30 23:17:53 +00:00
|
|
|
span: Span,
|
2013-04-15 23:13:42 +00:00
|
|
|
// the span of the prior token:
|
2013-12-30 23:30:14 +00:00
|
|
|
last_span: Span,
|
2013-08-05 20:18:29 +00:00
|
|
|
// the previous token or None (only stashed sometimes).
|
2013-12-30 23:30:56 +00:00
|
|
|
last_token: Option<~token::Token>,
|
2013-12-30 23:31:40 +00:00
|
|
|
buffer: [TokenAndSpan, ..4],
|
2013-12-30 23:32:56 +00:00
|
|
|
buffer_start: int,
|
|
|
|
buffer_end: int,
|
2013-12-30 23:33:39 +00:00
|
|
|
tokens_consumed: uint,
|
2013-12-30 23:34:28 +00:00
|
|
|
restriction: restriction,
|
2013-12-30 22:40:31 +00:00
|
|
|
quote_depth: uint, // not (yet) related to the quasiquoter
|
2014-01-09 13:05:33 +00:00
|
|
|
reader: @Reader,
|
|
|
|
interner: @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
|
2013-12-30 23:36:23 +00:00
|
|
|
obsolete_set: HashSet<ObsoleteSyntax>,
|
2012-12-11 20:20:27 +00:00
|
|
|
/// Used to determine the path to externally loaded source files
|
2013-12-30 23:38:02 +00:00
|
|
|
mod_path_stack: ~[@str],
|
2013-10-07 19:43:42 +00:00
|
|
|
/// Stack of spans of open delimiters. Used for error message.
|
2013-12-30 23:41:11 +00:00
|
|
|
open_braces: ~[Span],
|
2013-02-28 00:13:53 +00:00
|
|
|
/* do not copy the parser; its state is tied to outside state */
|
2013-10-29 00:10:32 +00:00
|
|
|
priv non_copyable: util::NonCopyable
|
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 {
|
2013-09-03 09:15:41 +00:00
|
|
|
is_plain_ident(t) || *t == token::UNDERSCORE
|
2013-09-02 23:58:12 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl Parser {
|
2013-06-15 01:21:47 +00:00
|
|
|
// convert a token to a string using self's reader
|
2013-12-30 23:09:41 +00:00
|
|
|
pub fn token_to_str(token: &token::Token) -> ~str {
|
2013-06-15 01:21:47 +00:00
|
|
|
token::to_str(get_ident_interner(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the current token to a string using self's reader
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn this_token_to_str(&mut self) -> ~str {
|
2013-12-30 23:09:41 +00:00
|
|
|
Parser::token_to_str(&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) -> ! {
|
2013-12-30 23:09:41 +00:00
|
|
|
let token_str = Parser::token_to_str(t);
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_fatal(self.last_span, format!("unexpected token: `{}`",
|
|
|
|
token_str));
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn unexpected(&mut self) -> ! {
|
|
|
|
let this_token = self.this_token_to_str();
|
|
|
|
self.fatal(format!("unexpected token: `{}`", this_token));
|
2013-06-15 01:21:47 +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) {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == *t {
|
2013-06-15 01:21:47 +00:00
|
|
|
self.bump();
|
|
|
|
} else {
|
2013-12-30 23:09:41 +00:00
|
|
|
let token_str = Parser::token_to_str(t);
|
2013-12-30 22:04:00 +00:00
|
|
|
let this_token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `{}` but found `{}`",
|
|
|
|
token_str,
|
|
|
|
this_token_str))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:18:29 +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]) {
|
2013-12-30 23:09:41 +00:00
|
|
|
fn tokens_to_str(tokens: &[token::Token]) -> ~str {
|
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.
|
2013-12-06 18:51:10 +00:00
|
|
|
let b = i.next().map_or(~"", |t| Parser::token_to_str(t));
|
2013-12-30 23:09:41 +00:00
|
|
|
i.fold(b, |b,a| b + "`, `" + Parser::token_to_str(a))
|
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 {
|
|
|
|
let expected = vec::append(edible.to_owned(), inedible);
|
2013-12-30 23:09:41 +00:00
|
|
|
let expect = tokens_to_str(expected);
|
2013-08-05 20:18:29 +00:00
|
|
|
let actual = self.this_token_to_str();
|
|
|
|
self.fatal(
|
|
|
|
if expected.len() != 1 {
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("expected one of `{}` but found `{}`", expect, actual)
|
2013-08-05 20:18:29 +00:00
|
|
|
} else {
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("expected `{}` but found `{}`", expect, actual)
|
2013-08-05 20:18:29 +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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::LBRACE
|
2013-08-05 20:18:29 +00:00
|
|
|
&& expected.iter().all(|t| *t != token::LBRACE)
|
|
|
|
&& self.look_ahead(1, |t| *t == token::RBRACE) {
|
|
|
|
// matched; signal non-fatal error and recover.
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_err(self.span,
|
2013-08-05 20:18:29 +00:00
|
|
|
"Unit-like struct construction is written with no trailing `{ }`");
|
|
|
|
self.eat(&token::LBRACE);
|
|
|
|
self.eat(&token::RBRACE);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn commit_expr(&mut self, e: @Expr, edible: &[token::Token], inedible: &[token::Token]) {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("commit_expr {:?}", e);
|
2013-08-05 20:18:29 +00:00
|
|
|
match e.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprPath(..) => {
|
2013-08-05 20:18:29 +00:00
|
|
|
// might be unit-struct construction; check for recoverableinput error.
|
|
|
|
let expected = vec::append(edible.to_owned(), inedible);
|
|
|
|
self.check_for_erroneous_unit_struct_expecting(expected);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
self.expect_one_of(edible, inedible)
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +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], &[])
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn commit_stmt(&mut self, s: @Stmt, edible: &[token::Token], inedible: &[token::Token]) {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("commit_stmt {:?}", s);
|
2013-08-05 20:18:29 +00:00
|
|
|
let _s = s; // unused, but future checks might want to inspect `s`.
|
2013-12-06 18:51:10 +00:00
|
|
|
if self.last_token.as_ref().map_or(false, |t| is_ident_or_path(*t)) {
|
2013-08-05 20:18:29 +00:00
|
|
|
let expected = vec::append(edible.to_owned(), inedible);
|
|
|
|
self.check_for_erroneous_unit_struct_expecting(expected);
|
|
|
|
}
|
|
|
|
self.expect_one_of(edible, inedible)
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn commit_stmt_expecting(&mut self, s: @Stmt, edible: token::Token) {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_stmt(s, &[edible], &[])
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-06-15 01:21:47 +00:00
|
|
|
token::IDENT(i, _) => {
|
|
|
|
self.bump();
|
|
|
|
i
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
token::INTERPOLATED(token::NtIdent(..)) => {
|
2013-06-15 01:21:47 +00:00
|
|
|
self.bug("ident interpolation not converted to real token");
|
|
|
|
}
|
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!( "expected ident, found `{}`", token_str))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_path_list_ident(&mut self) -> ast::PathListIdent {
|
2013-06-15 01:21:47 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let ident = self.parse_ident();
|
|
|
|
let hi = self.last_span.hi;
|
2014-01-09 13:05:33 +00:00
|
|
|
spanned(lo, hi, ast::PathListIdent_ { name: ident,
|
|
|
|
id: ast::DUMMY_NODE_ID })
|
2013-06-15 01:21:47 +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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
let is_present = self.token == *tok;
|
2013-07-18 03:04:37 +00:00
|
|
|
if is_present { self.bump() }
|
|
|
|
is_present
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn is_keyword(&mut self, kw: keywords::Keyword) -> bool {
|
2013-12-30 23:09:41 +00:00
|
|
|
token::is_keyword(kw, &self.token)
|
2013-06-15 01:21:47 +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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
let is_kw = match self.token {
|
2013-06-15 01:21:47 +00:00
|
|
|
token::IDENT(sid, false) => kw.to_ident().name == sid.name,
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
if is_kw { self.bump() }
|
|
|
|
is_kw
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2013-12-30 22:04:00 +00:00
|
|
|
let id_str = self.id_to_str(kw.to_ident()).to_str();
|
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
id_str,
|
|
|
|
token_str))
|
2013-06-15 01:21:47 +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) {
|
2013-12-30 23:09:41 +00:00
|
|
|
if token::is_strict_keyword(&self.token) {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_err(self.span,
|
2013-12-30 22:04:00 +00:00
|
|
|
format!("found `{}` in ident position", token_str));
|
2013-06-15 01:21:47 +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) {
|
2013-12-30 23:09:41 +00:00
|
|
|
if token::is_reserved_keyword(&self.token) {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("`{}` is a reserved keyword", token_str))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:06:13 +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 {
|
2013-10-29 22:06:13 +00:00
|
|
|
token::BINOP(token::OR) => self.bump(),
|
|
|
|
token::OROR => {
|
2013-12-30 23:17:53 +00:00
|
|
|
let lo = self.span.lo + BytePos(1);
|
|
|
|
self.replace_token(token::BINOP(token::OR), lo, self.span.hi)
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
2013-12-30 23:09:41 +00:00
|
|
|
let found_token =
|
|
|
|
Parser::token_to_str(&token::BINOP(token::OR));
|
2013-10-29 22:06:13 +00:00
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
found_token,
|
2013-12-30 22:04:00 +00:00
|
|
|
token_str))
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_seq_to_before_or<T>(
|
|
|
|
&mut self,
|
|
|
|
sep: &token::Token,
|
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> ~[T] {
|
2013-10-29 22:06:13 +00:00
|
|
|
let mut first = true;
|
|
|
|
let mut vector = ~[];
|
2013-12-30 23:09:41 +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
|
|
|
|
}
|
|
|
|
|
2013-06-15 01:21:47 +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 {
|
2013-07-18 03:04:37 +00:00
|
|
|
token::GT => self.bump(),
|
2013-12-30 23:17:53 +00:00
|
|
|
token::BINOP(token::SHR) => {
|
|
|
|
let lo = self.span.lo + BytePos(1);
|
|
|
|
self.replace_token(token::GT, lo, self.span.hi)
|
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
_ => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let gt_str = Parser::token_to_str(&token::GT);
|
2013-12-30 22:04:00 +00:00
|
|
|
let this_token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `{}`, found `{}`",
|
|
|
|
gt_str,
|
|
|
|
this_token_str))
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a sequence bracketed by '<' and '>', stopping
|
|
|
|
// before the '>'.
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_seq_to_before_gt<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
sep: Option<token::Token>,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> OptVec<T> {
|
2013-06-15 01:21:47 +00:00
|
|
|
let mut first = true;
|
|
|
|
let mut v = opt_vec::Empty;
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::GT
|
|
|
|
&& self.token != token::BINOP(token::SHR) {
|
2013-06-15 01:21:47 +00:00
|
|
|
match sep {
|
|
|
|
Some(ref t) => {
|
|
|
|
if first { first = false; }
|
|
|
|
else { self.expect(t); }
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
v.push(f(self));
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_seq_to_gt<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
sep: Option<token::Token>,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> OptVec<T> {
|
2013-06-15 01:21:47 +00:00
|
|
|
let v = self.parse_seq_to_before_gt(sep, f);
|
|
|
|
self.expect_gt();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a sequence, including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_seq_to_end<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> ~[T] {
|
2013-06-15 01:21:47 +00:00
|
|
|
let val = self.parse_seq_to_before_end(ket, sep, f);
|
|
|
|
self.bump();
|
|
|
|
val
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a sequence, not including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_seq_to_before_end<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> ~[T] {
|
2013-06-15 01:21:47 +00:00
|
|
|
let mut first: bool = true;
|
|
|
|
let mut v: ~[T] = ~[];
|
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); }
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
if sep.trailing_sep_allowed && self.token == *ket { break; }
|
2013-06-15 01:21:47 +00:00
|
|
|
v.push(f(self));
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a sequence, including the closing delimiter. The function
|
|
|
|
// f must consume tokens until reaching the next separator or
|
|
|
|
// closing bracket.
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_unspanned_seq<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
bra: &token::Token,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> ~[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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: Do not use this function unless you actually plan to place the
|
|
|
|
// spanned list in the AST.
|
2013-11-19 20:21:21 +00:00
|
|
|
pub fn parse_seq<T>(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
bra: &token::Token,
|
|
|
|
ket: &token::Token,
|
|
|
|
sep: SeqSep,
|
2013-12-30 22:04:00 +00:00
|
|
|
f: |&mut Parser| -> T)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> Spanned<~[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)
|
|
|
|
}
|
|
|
|
|
2013-01-30 17:56:33 +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).
|
2013-12-30 23:30:56 +00:00
|
|
|
self.last_token = if is_ident_or_path(&self.token) {
|
2013-12-30 23:09:41 +00:00
|
|
|
Some(~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 {
|
2012-06-15 16:32:17 +00:00
|
|
|
self.reader.next_token()
|
2012-01-13 08:56:53 +00:00
|
|
|
} else {
|
2013-07-02 19:47:32 +00:00
|
|
|
// Avoid token copies with `util::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 {
|
|
|
|
tok: token::UNDERSCORE,
|
2013-12-30 23:17:53 +00:00
|
|
|
sp: self.span,
|
2013-07-02 19:47:32 +00:00
|
|
|
};
|
|
|
|
util::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;
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
let old_token = util::replace(&mut self.token, token::UNDERSCORE);
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
old_token
|
|
|
|
}
|
|
|
|
|
2013-01-30 17:56:33 +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) {
|
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
|
|
|
}
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn look_ahead<R>(&mut self, distance: uint, f: |&token::Token| -> R)
|
2013-11-19 20:21:21 +00:00
|
|
|
-> R {
|
2012-06-09 00:17:31 +00:00
|
|
|
let dist = distance as int;
|
|
|
|
while self.buffer_length() < dist {
|
2013-12-30 23:32:56 +00:00
|
|
|
self.buffer[self.buffer_end] = self.reader.next_token();
|
|
|
|
self.buffer_end = (self.buffer_end + 1) & 3;
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-12-30 23:32:56 +00:00
|
|
|
f(&self.buffer[(self.buffer_start + dist - 1) & 3].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
|
|
|
}
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
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)
|
|
|
|
}
|
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
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn id_to_str(&mut self, id: Ident) -> @str {
|
2013-06-04 19:34:25 +00:00
|
|
|
get_ident_interner().get(id.name)
|
2013-03-02 21:02:27 +00:00
|
|
|
}
|
2012-07-18 23:18:02 +00:00
|
|
|
|
2013-10-29 22:06:13 +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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
if token::is_keyword(keywords::Fn, &self.token) {
|
2013-10-29 22:06:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if token::is_keyword(keywords::Unsafe, &self.token) ||
|
|
|
|
token::is_keyword(keywords::Once, &self.token) {
|
2013-10-29 22:06:13 +00:00
|
|
|
return self.look_ahead(1, |t| token::is_keyword(keywords::Fn, t))
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
token::is_keyword(keywords::Unsafe, &self.token) ||
|
|
|
|
token::is_keyword(keywords::Once, &self.token)
|
2013-10-29 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is the current token one of the keywords that signals an old-style
|
|
|
|
// closure type (with explicit sigil)?
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn token_is_old_style_closure_keyword(&mut self) -> bool {
|
2013-12-30 23:09:41 +00:00
|
|
|
token::is_keyword(keywords::Unsafe, &self.token) ||
|
|
|
|
token::is_keyword(keywords::Once, &self.token) ||
|
|
|
|
token::is_keyword(keywords::Fn, &self.token)
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn token_is_lifetime(tok: &token::Token) -> bool {
|
2013-04-26 23:19:26 +00:00
|
|
|
match *tok {
|
2013-11-28 20:22:53 +00:00
|
|
|
token::LIFETIME(..) => true,
|
2013-04-26 23:19:26 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
pub fn get_lifetime(&mut self) -> ast::Ident {
|
|
|
|
match self.token {
|
2013-06-28 00:41:35 +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-01-09 13:05:33 +00:00
|
|
|
// parse a TyBareFn type:
|
|
|
|
pub fn parse_ty_bare_fn(&mut self) -> Ty_ {
|
2012-11-05 04:41:00 +00:00
|
|
|
/*
|
|
|
|
|
2013-10-29 22:06:13 +00:00
|
|
|
[extern "ABI"] [unsafe] fn <'lt> (S) -> T
|
|
|
|
^~~~^ ^~~~~~~^ ^~~~^ ^~^ ^
|
|
|
|
| | | | |
|
|
|
|
| | | | Return type
|
|
|
|
| | | Argument types
|
|
|
|
| | Lifetimes
|
|
|
|
| |
|
|
|
|
| Purity
|
|
|
|
ABI
|
2013-02-01 01:12:29 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-03-14 02:25:28 +00:00
|
|
|
let opt_abis = self.parse_opt_abis();
|
2013-09-10 02:57:08 +00:00
|
|
|
let abis = opt_abis.unwrap_or(AbiSet::Rust());
|
2013-04-19 22:48:02 +00:00
|
|
|
let purity = self.parse_unsafety();
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2013-10-25 05:56:34 +00:00
|
|
|
let (decl, lifetimes) = self.parse_ty_fn_decl(true);
|
2014-01-09 13:05:33 +00:00
|
|
|
return TyBareFn(@BareFnTy {
|
2013-03-14 02:25:28 +00:00
|
|
|
abis: abis,
|
2013-02-01 01:12:29 +00:00
|
|
|
purity: purity,
|
2013-03-27 16:55:18 +00:00
|
|
|
lifetimes: lifetimes,
|
2013-03-25 20:21:04 +00:00
|
|
|
decl: decl
|
2013-02-01 01:12:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-28 22:22:49 +00:00
|
|
|
// Parses a procedure type (`proc`). The initial `proc` keyword must
|
|
|
|
// already have been parsed.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_proc_type(&mut self) -> Ty_ {
|
2013-10-25 05:56:34 +00:00
|
|
|
let (decl, lifetimes) = self.parse_ty_fn_decl(false);
|
2014-01-09 13:05:33 +00:00
|
|
|
TyClosure(@ClosureTy {
|
2013-10-28 22:22:49 +00:00
|
|
|
sigil: OwnedSigil,
|
|
|
|
region: None,
|
2014-01-09 13:05:33 +00:00
|
|
|
purity: ImpureFn,
|
2013-10-28 22:22:49 +00:00
|
|
|
onceness: Once,
|
|
|
|
bounds: None,
|
|
|
|
decl: decl,
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
// parse a TyClosure type
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_ty_closure(&mut self,
|
2013-10-29 22:06:13 +00:00
|
|
|
opt_sigil: Option<ast::Sigil>,
|
|
|
|
mut region: Option<ast::Lifetime>)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> Ty_ {
|
2013-02-01 01:12:29 +00:00
|
|
|
/*
|
|
|
|
|
2013-10-03 09:53:46 +00:00
|
|
|
(&|~|@) ['r] [unsafe] [once] fn [:Bounds] <'lt> (S) -> T
|
|
|
|
^~~~~~^ ^~~^ ^~~~~~~^ ^~~~~^ ^~~~~~~~^ ^~~~^ ^~^ ^
|
|
|
|
| | | | | | | |
|
|
|
|
| | | | | | | Return type
|
|
|
|
| | | | | | Argument types
|
|
|
|
| | | | | Lifetimes
|
|
|
|
| | | | Closure bounds
|
|
|
|
| | | Once-ness (a.k.a., affine)
|
2012-11-05 04:41:00 +00:00
|
|
|
| | Purity
|
|
|
|
| Lifetime bound
|
|
|
|
Allocation type
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// At this point, the allocation type and lifetime bound have been
|
|
|
|
// parsed.
|
|
|
|
|
2013-04-19 22:48:02 +00:00
|
|
|
let purity = self.parse_unsafety();
|
2013-03-02 21:02:27 +00:00
|
|
|
let onceness = parse_onceness(self);
|
2013-03-02 00:59:46 +00:00
|
|
|
|
2013-10-29 22:06:13 +00:00
|
|
|
let (sigil, decl, lifetimes, bounds) = match opt_sigil {
|
|
|
|
Some(sigil) => {
|
|
|
|
// Old-style closure syntax (`fn(A)->B`).
|
|
|
|
self.expect_keyword(keywords::Fn);
|
|
|
|
let bounds = self.parse_optional_ty_param_bounds();
|
2013-10-25 05:56:34 +00:00
|
|
|
let (decl, lifetimes) = self.parse_ty_fn_decl(false);
|
2013-10-29 22:06:13 +00:00
|
|
|
(sigil, decl, lifetimes, bounds)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// New-style closure syntax (`<'lt>|A|:K -> B`).
|
|
|
|
let lifetimes = if self.eat(&token::LT) {
|
|
|
|
let lifetimes = self.parse_lifetimes();
|
|
|
|
self.expect_gt();
|
|
|
|
|
|
|
|
// Re-parse the region here. What a hack.
|
|
|
|
if region.is_some() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span,
|
2013-10-29 22:06:13 +00:00
|
|
|
"lifetime declarations must precede \
|
|
|
|
the lifetime associated with a \
|
|
|
|
closure");
|
|
|
|
}
|
|
|
|
region = self.parse_opt_lifetime();
|
|
|
|
|
|
|
|
lifetimes
|
|
|
|
} else {
|
|
|
|
opt_vec::Empty
|
|
|
|
};
|
|
|
|
|
|
|
|
let inputs = if self.eat(&token::OROR) {
|
|
|
|
~[]
|
|
|
|
} else {
|
|
|
|
self.expect_or();
|
|
|
|
let inputs = self.parse_seq_to_before_or(
|
|
|
|
&token::COMMA,
|
|
|
|
|p| p.parse_arg_general(false));
|
|
|
|
self.expect_or();
|
|
|
|
inputs
|
|
|
|
};
|
|
|
|
|
|
|
|
let bounds = self.parse_optional_ty_param_bounds();
|
|
|
|
|
|
|
|
let (return_style, output) = self.parse_ret_ty();
|
2014-01-09 13:05:33 +00:00
|
|
|
let decl = P(FnDecl {
|
2013-10-29 22:06:13 +00:00
|
|
|
inputs: inputs,
|
|
|
|
output: output,
|
|
|
|
cf: return_style,
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
(BorrowedSigil, decl, lifetimes, bounds)
|
|
|
|
}
|
|
|
|
};
|
2013-03-25 20:21:04 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
return TyClosure(@ClosureTy {
|
2013-02-01 01:12:29 +00:00
|
|
|
sigil: sigil,
|
2012-11-05 04:41:00 +00:00
|
|
|
region: region,
|
|
|
|
purity: purity,
|
|
|
|
onceness: onceness,
|
2013-05-10 19:57:27 +00:00
|
|
|
bounds: bounds,
|
2013-03-25 20:21:04 +00:00
|
|
|
decl: decl,
|
|
|
|
lifetimes: lifetimes,
|
2012-11-05 04:41:00 +00:00
|
|
|
});
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_onceness(this: &mut Parser) -> Onceness {
|
2013-05-25 15:45:45 +00:00
|
|
|
if this.eat_keyword(keywords::Once) {
|
2013-05-10 22:15:06 +00:00
|
|
|
Once
|
|
|
|
} else {
|
|
|
|
Many
|
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
2012-05-25 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_unsafety(&mut self) -> Purity {
|
2013-10-03 09:53:46 +00:00
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
2014-01-09 13:05:33 +00:00
|
|
|
return UnsafeFn;
|
2013-02-01 01:12:29 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
return ImpureFn;
|
2013-02-01 01:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
|
2013-03-08 18:19:19 +00:00
|
|
|
// parse a function type (following the 'fn')
|
2013-12-31 23:49:11 +00:00
|
|
|
pub fn parse_ty_fn_decl(&mut self, allow_variadic: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> (P<FnDecl>, OptVec<ast::Lifetime>) {
|
2013-02-12 23:01:29 +00:00
|
|
|
/*
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2013-02-12 23:01:29 +00:00
|
|
|
(fn) <'lt> (S) -> T
|
|
|
|
^~~~^ ^~^ ^
|
|
|
|
| | |
|
|
|
|
| | Return type
|
|
|
|
| Argument types
|
|
|
|
Lifetimes
|
|
|
|
|
|
|
|
*/
|
2013-03-25 20:21:04 +00:00
|
|
|
let lifetimes = if self.eat(&token::LT) {
|
|
|
|
let lifetimes = self.parse_lifetimes();
|
2013-02-28 00:41:02 +00:00
|
|
|
self.expect_gt();
|
2013-03-25 20:21:04 +00:00
|
|
|
lifetimes
|
|
|
|
} else {
|
|
|
|
opt_vec::Empty
|
|
|
|
};
|
|
|
|
|
2013-10-25 05:56:34 +00:00
|
|
|
let (inputs, variadic) = self.parse_fn_args(false, allow_variadic);
|
2012-05-23 22:06:11 +00:00
|
|
|
let (ret_style, ret_ty) = self.parse_ret_ty();
|
2014-01-09 13:05:33 +00:00
|
|
|
let decl = P(FnDecl {
|
2013-03-25 20:21:04 +00:00
|
|
|
inputs: inputs,
|
|
|
|
output: ret_ty,
|
2013-10-25 05:56:34 +00:00
|
|
|
cf: ret_style,
|
|
|
|
variadic: variadic
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2013-03-25 20:21:04 +00:00
|
|
|
(decl, lifetimes)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse the methods in a trait declaration
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_trait_methods(&mut self) -> ~[TraitMethod] {
|
2013-11-21 00:23:04 +00:00
|
|
|
self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LBRACE,
|
|
|
|
&token::RBRACE,
|
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();
|
2012-07-13 22:27:21 +00:00
|
|
|
let lo = p.span.lo;
|
2012-08-02 23:01:38 +00:00
|
|
|
|
2013-12-30 23:17:53 +00:00
|
|
|
let vis_span = p.span;
|
2013-08-07 07:11:34 +00:00
|
|
|
let vis = p.parse_visibility();
|
2012-05-23 22:06:11 +00:00
|
|
|
let pur = p.parse_fn_purity();
|
2012-07-13 22:27:21 +00:00
|
|
|
// NB: at the moment, trait methods are public by default; this
|
|
|
|
// could change.
|
2013-03-06 17:30:54 +00:00
|
|
|
let ident = p.parse_ident();
|
2012-08-17 22:25:35 +00:00
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = p.parse_generics();
|
2012-08-17 22:25:35 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
let (explicit_self, d) = p.parse_fn_decl_with_self(|p| {
|
2012-08-17 22:25:35 +00:00
|
|
|
// This is somewhat dubious; We don't want to allow argument
|
|
|
|
// names to be left off if there is a definition...
|
2013-09-14 02:07:43 +00:00
|
|
|
p.parse_arg_general(false)
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2013-03-13 02:32:14 +00:00
|
|
|
|
2012-07-13 22:27:21 +00:00
|
|
|
let hi = p.last_span.hi;
|
2013-12-30 23:09:41 +00:00
|
|
|
match p.token {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::SEMI => {
|
2012-07-13 22:27:21 +00:00
|
|
|
p.bump();
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("parse_trait_methods(): parsing required method");
|
2012-07-13 22:27:21 +00:00
|
|
|
// NB: at the moment, visibility annotations on required
|
|
|
|
// methods are ignored; this could change.
|
2014-01-09 13:05:33 +00:00
|
|
|
if vis != ast::Inherited {
|
2013-12-30 22:04:00 +00:00
|
|
|
p.obsolete(vis_span, ObsoleteTraitFuncVisibility);
|
2013-08-09 08:25:24 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
Required(TypeMethod {
|
2013-01-15 23:03:49 +00:00
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
|
|
|
purity: pur,
|
|
|
|
decl: d,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: generics,
|
2013-04-30 15:49:48 +00:00
|
|
|
explicit_self: explicit_self,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-15 23:03:49 +00:00
|
|
|
span: mk_sp(lo, hi)
|
|
|
|
})
|
2012-07-10 20:44:20 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::LBRACE => {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("parse_trait_methods(): parsing provided method");
|
2012-07-10 20:44:20 +00:00
|
|
|
let (inner_attrs, body) =
|
2013-04-06 00:31:52 +00:00
|
|
|
p.parse_inner_attrs_and_block();
|
2012-07-10 20:44:20 +00:00
|
|
|
let attrs = vec::append(attrs, inner_attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
Provided(@ast::Method {
|
2013-01-16 00:05:20 +00:00
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: generics,
|
2013-04-30 15:49:48 +00:00
|
|
|
explicit_self: explicit_self,
|
2013-01-16 00:05:20 +00:00
|
|
|
purity: pur,
|
|
|
|
decl: d,
|
|
|
|
body: body,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-01-16 00:05:20 +00:00
|
|
|
span: mk_sp(lo, hi),
|
2013-09-07 02:11:55 +00:00
|
|
|
self_id: ast::DUMMY_NODE_ID,
|
2013-01-16 00:05:20 +00:00
|
|
|
vis: vis,
|
|
|
|
})
|
2012-07-10 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
2013-02-25 02:32:02 +00:00
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = p.this_token_to_str();
|
|
|
|
p.fatal(format!("expected `;` or `\\{` but found `{}`",
|
|
|
|
token_str))
|
|
|
|
}
|
2012-07-10 20:44:20 +00:00
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 18:19:19 +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();
|
2013-11-30 22:00:39 +00:00
|
|
|
let t = self.parse_ty(false);
|
2014-01-09 13:05:33 +00:00
|
|
|
MutTy { ty: t, mutbl: mutbl }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 18:19:19 +00:00
|
|
|
// parse [mut/const/imm] ID : TY
|
2013-04-02 23:44:01 +00:00
|
|
|
// 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();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::COLON);
|
2013-11-30 22:00:39 +00:00
|
|
|
let ty = self.parse_ty(false);
|
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
|
|
|
}
|
|
|
|
|
2013-03-08 18:19:19 +00:00
|
|
|
// parse optional return type [ -> TY ] in function decl
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_ret_ty(&mut self) -> (RetStyle, P<Ty>) {
|
2013-02-24 17:54:41 +00:00
|
|
|
return if self.eat(&token::RARROW) {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::NOT) {
|
2013-01-15 22:59:39 +00:00
|
|
|
(
|
2014-01-09 13:05:33 +00:00
|
|
|
NoReturn,
|
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: TyBot,
|
2013-01-15 22:59:39 +00:00
|
|
|
span: mk_sp(lo, self.last_span.hi)
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2013-01-15 22:59:39 +00:00
|
|
|
)
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
(Return, self.parse_ty(false))
|
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;
|
2013-01-15 22:59:39 +00:00
|
|
|
(
|
2014-01-09 13:05:33 +00:00
|
|
|
Return,
|
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: TyNil,
|
2013-01-15 22:59:39 +00:00
|
|
|
span: mk_sp(pos, pos),
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2013-01-15 22:59:39 +00:00
|
|
|
)
|
2012-03-12 23:26:31 +00:00
|
|
|
}
|
2011-05-15 02:02:30 +00:00
|
|
|
}
|
2012-04-10 00:32:49 +00:00
|
|
|
|
2013-03-08 18:19:19 +00:00
|
|
|
// parse a type.
|
2013-03-20 20:24:09 +00:00
|
|
|
// Useless second parameter for compatibility with quasiquote macros.
|
|
|
|
// Bleh!
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_ty(&mut self, _: bool) -> 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;
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let t = if self.token == token::LPAREN {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RPAREN {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2014-01-09 13:05:33 +00:00
|
|
|
TyNil
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2013-02-19 01:45:56 +00:00
|
|
|
// (t) is a parenthesized ty
|
|
|
|
// (t,) is the type of a tuple with only one field,
|
|
|
|
// of type t
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut ts = ~[self.parse_ty(false)];
|
2013-02-19 01:45:56 +00:00
|
|
|
let mut one_tuple = false;
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::COMMA {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token != token::RPAREN {
|
2013-02-19 01:45:56 +00:00
|
|
|
ts.push(self.parse_ty(false));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
one_tuple = true;
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
|
|
|
|
if ts.len() == 1 && !one_tuple {
|
|
|
|
self.expect(&token::RPAREN);
|
|
|
|
return ts[0]
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
let t = TyTup(ts);
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RPAREN);
|
2012-05-23 22:06:11 +00:00
|
|
|
t
|
2011-08-15 10:18:27 +00:00
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::AT {
|
2013-03-08 18:19:19 +00:00
|
|
|
// MANAGED POINTER
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-12-16 18:01:40 +00:00
|
|
|
self.parse_box_or_uniq_pointee(ManagedSigil)
|
2013-12-30 23:09:41 +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();
|
2013-12-16 18:01:40 +00:00
|
|
|
self.parse_box_or_uniq_pointee(OwnedSigil)
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == 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-01-09 13:05:33 +00:00
|
|
|
TyPtr(self.parse_mt())
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LBRACKET {
|
2013-03-08 18:19:19 +00:00
|
|
|
// VECTOR
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACKET);
|
2013-12-16 18:01:40 +00:00
|
|
|
let t = self.parse_ty(false);
|
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
|
|
|
};
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACKET);
|
2012-02-06 14:29:56 +00:00
|
|
|
t
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::BINOP(token::AND) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// BORROWED POINTER
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-11-05 04:41:00 +00:00
|
|
|
self.parse_borrowed_pointee()
|
2013-10-29 22:06:13 +00:00
|
|
|
} else if self.is_keyword(keywords::Extern) ||
|
|
|
|
self.token_is_bare_fn_keyword() {
|
|
|
|
// BARE FUNCTION
|
2013-02-01 01:12:29 +00:00
|
|
|
self.parse_ty_bare_fn()
|
2013-10-29 22:06:13 +00:00
|
|
|
} else if self.token_is_closure_keyword() ||
|
2013-12-30 23:09:41 +00:00
|
|
|
self.token == token::BINOP(token::OR) ||
|
|
|
|
self.token == token::OROR ||
|
|
|
|
self.token == token::LT ||
|
|
|
|
Parser::token_is_lifetime(&self.token) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// CLOSURE
|
2013-10-29 22:06:13 +00:00
|
|
|
//
|
|
|
|
// XXX(pcwalton): Eventually `token::LT` will not unambiguously
|
|
|
|
// introduce a closure, once procs can have lifetime bounds. We
|
|
|
|
// will need to refactor the grammar a little bit at that point.
|
|
|
|
|
|
|
|
let lifetime = self.parse_opt_lifetime();
|
|
|
|
let result = self.parse_ty_closure(None, lifetime);
|
2013-03-07 23:44:21 +00:00
|
|
|
result
|
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.
|
|
|
|
self.expect(&token::LPAREN);
|
|
|
|
let e = self.parse_expr();
|
|
|
|
self.expect(&token::RPAREN);
|
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) {
|
|
|
|
self.parse_proc_type()
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::MOD_SEP
|
|
|
|
|| is_ident_or_path(&self.token) {
|
2013-03-08 18:19:19 +00:00
|
|
|
// NAMED TYPE
|
2013-08-07 16:47:28 +00:00
|
|
|
let PathAndBounds {
|
|
|
|
path,
|
|
|
|
bounds
|
|
|
|
} = self.parse_path(LifetimeAndTypesAndBounds);
|
2014-01-09 13:05:33 +00:00
|
|
|
TyPath(path, bounds, ast::DUMMY_NODE_ID)
|
2013-02-24 17:39:29 +00:00
|
|
|
} else {
|
2013-12-30 23:09:41 +00:00
|
|
|
let msg = format!("expected type, found token {:?}", self.token);
|
|
|
|
self.fatal(msg);
|
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
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse the type following a @ or a ~
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_box_or_uniq_pointee(&mut self,
|
2013-12-16 18:01:40 +00:00
|
|
|
sigil: ast::Sigil)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> Ty_ {
|
2013-11-19 02:25:25 +00:00
|
|
|
// ~'foo fn() or ~fn() are parsed directly as obsolete fn types:
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-11-28 20:22:53 +00:00
|
|
|
token::LIFETIME(..) => {
|
2013-07-05 12:33:52 +00:00
|
|
|
let lifetime = self.parse_lifetime();
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteBoxedClosure);
|
2013-10-29 22:06:13 +00:00
|
|
|
return self.parse_ty_closure(Some(sigil), Some(lifetime));
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 20:22:53 +00:00
|
|
|
token::IDENT(..) => {
|
2013-10-29 22:06:13 +00:00
|
|
|
if self.token_is_old_style_closure_keyword() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteBoxedClosure);
|
2013-10-29 22:06:13 +00:00
|
|
|
return self.parse_ty_closure(Some(sigil), None);
|
2012-11-05 04:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-10-03 09:53:46 +00:00
|
|
|
// other things are parsed as @/~ + a type. Note that constructs like
|
2012-11-05 04:41:00 +00:00
|
|
|
// @[] and @str will be resolved during typeck to slices and so forth,
|
|
|
|
// rather than boxed ptrs. But the special casing of str/vec is not
|
|
|
|
// reflected in the AST type.
|
2013-10-03 09:53:46 +00:00
|
|
|
if sigil == OwnedSigil {
|
2014-01-09 13:05:33 +00:00
|
|
|
TyUniq(self.parse_ty(false))
|
2013-10-03 09:53:46 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
TyBox(self.parse_ty(false))
|
2013-02-25 23:54:13 +00:00
|
|
|
}
|
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
|
|
|
|
2013-10-29 22:06:13 +00:00
|
|
|
if self.token_is_old_style_closure_keyword() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteClosureType);
|
2013-10-29 22:06:13 +00:00
|
|
|
return self.parse_ty_closure(Some(BorrowedSigil), 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
|
|
|
}
|
|
|
|
|
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 {
|
2013-07-18 03:04:37 +00:00
|
|
|
token::BINOP(token::AND) => 1,
|
|
|
|
token::ANDAND => 1,
|
2013-12-30 23:09:41 +00:00
|
|
|
_ if token::is_keyword(keywords::Mut, &self.token) => 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)
|
2013-07-02 19:47:32 +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))
|
2013-07-02 19:47:32 +00:00
|
|
|
&& self.look_ahead(offset + 1, |t| *t == token::COLON)
|
2012-09-26 08:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 22:25:35 +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() {
|
2013-10-21 20:08:31 +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
|
|
|
|
2013-02-24 18:20:24 +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
|
|
|
};
|
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
let t = self.parse_ty(false);
|
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
|
|
|
}
|
|
|
|
|
2013-04-04 21:30:11 +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
|
|
|
|
2013-04-02 23:44:01 +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();
|
2013-02-27 19:03:21 +00:00
|
|
|
let t = if self.eat(&token::COLON) {
|
2013-02-26 00:49:28 +00:00
|
|
|
self.parse_ty(false)
|
|
|
|
} 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
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn maybe_parse_fixed_vstore(&mut self) -> Option<@ast::Expr> {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::COMMA &&
|
2013-07-02 19:47:32 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:51:10 +00:00
|
|
|
// matches token_lit = LIT_INT | ...
|
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-01-09 13:05:33 +00:00
|
|
|
token::LIT_CHAR(i) => LitChar(i),
|
|
|
|
token::LIT_INT(i, it) => LitInt(i, it),
|
|
|
|
token::LIT_UINT(u, ut) => LitUint(u, ut),
|
|
|
|
token::LIT_INT_UNSUFFIXED(i) => LitIntUnsuffixed(i),
|
|
|
|
token::LIT_FLOAT(s, ft) => LitFloat(self.id_to_str(s), ft),
|
2013-02-25 04:51:56 +00:00
|
|
|
token::LIT_FLOAT_UNSUFFIXED(s) =>
|
2014-01-09 13:05:33 +00:00
|
|
|
LitFloatUnsuffixed(self.id_to_str(s)),
|
|
|
|
token::LIT_STR(s) => LitStr(self.id_to_str(s), ast::CookedStr),
|
|
|
|
token::LIT_STR_RAW(s, n) => LitStr(self.id_to_str(s), ast::RawStr(n)),
|
|
|
|
token::LPAREN => { self.expect(&token::RPAREN); LitNil },
|
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
|
|
|
|
2013-03-29 19:51:10 +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
|
|
|
|
2013-05-11 01:19:58 +00:00
|
|
|
// matches '-' lit | lit
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_literal_maybe_minus(&mut self) -> @Expr {
|
2013-05-11 01:19:58 +00:00
|
|
|
let minus_lo = self.span.lo;
|
|
|
|
let minus_present = self.eat(&token::BINOP(token::MINUS));
|
|
|
|
|
|
|
|
let lo = self.span.lo;
|
|
|
|
let literal = @self.parse_lit();
|
|
|
|
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.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_path(&mut self, mode: PathParsingMode) -> PathAndBounds {
|
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-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(token::NtPath(_)) => Some(self.bump_and_get()),
|
2013-08-07 16:47:28 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
match found {
|
2014-01-09 13:05:33 +00:00
|
|
|
Some(INTERPOLATED(token::NtPath(~path))) => {
|
2013-08-07 16:47:28 +00:00
|
|
|
return PathAndBounds {
|
|
|
|
path: path,
|
|
|
|
bounds: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-03-27 19:36:10 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let is_global = self.eat(&token::MOD_SEP);
|
|
|
|
|
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.
|
|
|
|
let mut segments = ~[];
|
2012-05-23 22:06:11 +00:00
|
|
|
loop {
|
2013-08-07 16:47:28 +00:00
|
|
|
// First, parse an identifier.
|
|
|
|
let identifier = self.parse_ident();
|
2013-03-27 19:36:10 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Next, parse a colon and bounded type parameters, if applicable.
|
|
|
|
let bound_set = if mode == LifetimeAndTypesAndBounds {
|
|
|
|
self.parse_optional_ty_param_bounds()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Parse the '::' before type parameters if it's required. If
|
|
|
|
// it is required and wasn't present, then we're done.
|
|
|
|
if mode == LifetimeAndTypesWithColons &&
|
|
|
|
!self.eat(&token::MOD_SEP) {
|
|
|
|
segments.push(PathSegmentAndBoundSet {
|
|
|
|
segment: ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: opt_vec::Empty,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: opt_vec::Empty,
|
|
|
|
},
|
|
|
|
bound_set: bound_set
|
|
|
|
});
|
|
|
|
break
|
|
|
|
}
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Parse the `<` before the lifetime and types, if applicable.
|
2013-10-29 10:03:32 +00:00
|
|
|
let (any_lifetime_or_types, lifetimes, types) = {
|
|
|
|
if mode != NoTypesAllowed && self.eat(&token::LT) {
|
|
|
|
let (lifetimes, types) =
|
|
|
|
self.parse_generic_values_after_lt();
|
|
|
|
(true, lifetimes, opt_vec::from(types))
|
|
|
|
} else {
|
|
|
|
(false, opt_vec::Empty, opt_vec::Empty)
|
2013-02-28 00:41:02 +00:00
|
|
|
}
|
2013-08-07 16:47:28 +00:00
|
|
|
};
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// Assemble and push the result.
|
|
|
|
segments.push(PathSegmentAndBoundSet {
|
|
|
|
segment: ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: lifetimes,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: types,
|
|
|
|
},
|
|
|
|
bound_set: bound_set
|
|
|
|
});
|
2013-02-12 00:33:31 +00:00
|
|
|
|
2013-08-07 16:47:28 +00:00
|
|
|
// We're done if we don't see a '::', unless the mode required
|
|
|
|
// a double colon to get here in the first place.
|
|
|
|
if !(mode == LifetimeAndTypesWithColons &&
|
|
|
|
!any_lifetime_or_types) {
|
|
|
|
if !self.eat(&token::MOD_SEP) {
|
|
|
|
break
|
2013-02-26 19:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
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 path segments.
|
|
|
|
let mut path_segments = ~[];
|
2013-06-20 22:23:25 +00:00
|
|
|
let mut bounds = None;
|
2013-08-07 16:47:28 +00:00
|
|
|
let last_segment_index = segments.len() - 1;
|
2013-08-08 18:38:10 +00:00
|
|
|
for (i, segment_and_bounds) in segments.move_iter().enumerate() {
|
2013-08-07 16:47:28 +00:00
|
|
|
let PathSegmentAndBoundSet {
|
|
|
|
segment: segment,
|
|
|
|
bound_set: bound_set
|
|
|
|
} = segment_and_bounds;
|
|
|
|
path_segments.push(segment);
|
|
|
|
|
|
|
|
if bound_set.is_some() {
|
|
|
|
if i != last_segment_index {
|
|
|
|
self.span_err(span,
|
|
|
|
"type parameter bounds are allowed only \
|
|
|
|
before the last segment in a path")
|
|
|
|
}
|
|
|
|
|
|
|
|
bounds = bound_set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble the result.
|
|
|
|
let path_and_bounds = PathAndBounds {
|
|
|
|
path: ast::Path {
|
|
|
|
span: span,
|
|
|
|
global: is_global,
|
|
|
|
segments: path_segments,
|
|
|
|
},
|
|
|
|
bounds: bounds,
|
|
|
|
};
|
|
|
|
|
|
|
|
path_and_bounds
|
2013-06-17 19:16:30 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2013-11-28 20:22:53 +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
|
2013-10-03 09:53:46 +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 {
|
2013-02-12 00:33:31 +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,
|
2013-02-28 00:41:02 +00:00
|
|
|
ident: i
|
|
|
|
};
|
|
|
|
}
|
2013-02-12 00:33:31 +00:00
|
|
|
_ => {
|
2013-09-28 04:01:58 +00:00
|
|
|
self.fatal(format!("Expected a lifetime name"));
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 23:47:47 +00:00
|
|
|
// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes )
|
|
|
|
// actually, it matches the empty one too, but putting that in there
|
|
|
|
// messes up the grammar....
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_lifetimes(&mut self) -> OptVec<ast::Lifetime> {
|
2013-02-12 00:33:31 +00:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* 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>`.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
let mut res = opt_vec::Empty;
|
2013-02-12 00:33:31 +00:00
|
|
|
loop {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-02-12 00:33:31 +00:00
|
|
|
token::LIFETIME(_) => {
|
|
|
|
res.push(self.parse_lifetime());
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-02-12 00:33:31 +00:00
|
|
|
token::COMMA => { self.bump();}
|
|
|
|
token::GT => { return res; }
|
2013-02-28 00:41:02 +00:00
|
|
|
token::BINOP(token::SHR) => { return res; }
|
2013-02-12 00:33:31 +00:00
|
|
|
_ => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let msg = format!("expected `,` or `>` after lifetime \
|
|
|
|
name, got: {:?}",
|
|
|
|
self.token);
|
|
|
|
self.fatal(msg);
|
2013-02-12 00:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn token_is_mutability(tok: &token::Token) -> bool {
|
2013-05-25 15:45:45 +00:00
|
|
|
token::is_keyword(keywords::Mut, tok) ||
|
|
|
|
token::is_keyword(keywords::Const, tok)
|
2013-03-10 00:43:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 18:19:19 +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
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Const) {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteConstPointer);
|
2013-09-02 01:45:37 +00:00
|
|
|
MutImmutable
|
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
|
|
|
|
2013-04-22 00:26:41 +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;
|
2013-04-22 00:26:41 +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
|
|
|
expr: e,
|
|
|
|
span: mk_sp(lo, e.span.hi),
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-09-28 01:25:02 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> @Expr {
|
2013-09-02 01:45:37 +00:00
|
|
|
@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),
|
|
|
|
}
|
2010-10-12 23:30:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_unary(&mut self, unop: ast::UnOp, expr: @Expr) -> ast::Expr_ {
|
2013-09-07 02:11:55 +00:00
|
|
|
ExprUnary(ast::DUMMY_NODE_ID, unop, expr)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: @Expr, rhs: @Expr) -> ast::Expr_ {
|
2013-09-07 02:11:55 +00:00
|
|
|
ExprBinary(ast::DUMMY_NODE_ID, binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_call(&mut self, f: @Expr, args: ~[@Expr], sugar: CallSugar) -> ast::Expr_ {
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprCall(f, args, sugar)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_method_call(&mut self,
|
2013-09-02 01:45:37 +00:00
|
|
|
rcvr: @Expr,
|
2013-09-02 00:50:59 +00:00
|
|
|
ident: Ident,
|
2013-11-30 22:00:39 +00:00
|
|
|
tps: ~[P<Ty>],
|
2013-09-02 01:45:37 +00:00
|
|
|
args: ~[@Expr],
|
|
|
|
sugar: CallSugar) -> ast::Expr_ {
|
2013-09-07 02:11:55 +00:00
|
|
|
ExprMethodCall(ast::DUMMY_NODE_ID, rcvr, ident, tps, args, sugar)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_index(&mut self, expr: @Expr, idx: @Expr) -> ast::Expr_ {
|
2013-09-07 02:11:55 +00:00
|
|
|
ExprIndex(ast::DUMMY_NODE_ID, expr, idx)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_field(&mut self, expr: @Expr, ident: Ident, tys: ~[P<Ty>]) -> ast::Expr_ {
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprField(expr, ident, tys)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_assign_op(&mut self, binop: ast::BinOp, lhs: @Expr, rhs: @Expr) -> ast::Expr_ {
|
2013-09-07 02:11:55 +00:00
|
|
|
ExprAssignOp(ast::DUMMY_NODE_ID, binop, lhs, rhs)
|
2013-06-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> @Expr {
|
2013-09-02 01:45:37 +00:00
|
|
|
@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),
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-12-14 23:32:13 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn mk_lit_u32(&mut self, i: u32) -> @Expr {
|
2013-12-30 23:17:53 +00:00
|
|
|
let span = &self.span;
|
2013-08-31 16:13:04 +00:00
|
|
|
let lv_lit = @codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitUint(i as u64, TyU32),
|
2013-02-22 02:12:13 +00:00
|
|
|
span: *span
|
|
|
|
};
|
2011-06-21 20:16:40 +00:00
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
@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,
|
2013-01-15 21:51:43 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-07-08 23:35:09 +00:00
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// at the bottom (top?) of the precedence hierarchy,
|
|
|
|
// parse things like parenthesized exprs,
|
|
|
|
// macros, return, etc.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_bottom_expr(&mut self) -> @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
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::LPAREN {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-02-17 23:41:47 +00:00
|
|
|
// (e) is parenthesized e
|
|
|
|
// (e,) is a tuple with only one field, e
|
2013-04-22 00:27:19 +00:00
|
|
|
let mut trailing_comma = false;
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RPAREN {
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
|
|
|
self.bump();
|
2014-01-09 13:05:33 +00:00
|
|
|
let lit = @spanned(lo, hi, LitNil);
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprLit(lit));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut es = ~[self.parse_expr()];
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr(*es.last(), &[], &[token::COMMA, token::RPAREN]);
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::COMMA {
|
2013-02-17 23:41:47 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token != token::RPAREN {
|
2013-02-17 23:41:47 +00:00
|
|
|
es.push(self.parse_expr());
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr(*es.last(), &[], &[token::COMMA, token::RPAREN]);
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-04-22 00:27:19 +00:00
|
|
|
trailing_comma = true;
|
2013-02-17 23:41:47 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
hi = self.span.hi;
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr_expecting(*es.last(), token::RPAREN);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-04-22 00:27:19 +00:00
|
|
|
return if es.len() == 1 && !trailing_comma {
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, self.span.hi, ExprParen(es[0]))
|
2012-10-28 00:14:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, hi, ExprTup(es))
|
2012-10-28 00:14:09 +00:00
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LBRACE {
|
2013-03-02 03:57:05 +00:00
|
|
|
self.bump();
|
2013-07-27 08:25:59 +00:00
|
|
|
let blk = self.parse_block_tail(lo, DefaultBlock);
|
2013-03-02 03:57:05 +00:00
|
|
|
return self.mk_expr(blk.span.lo, blk.span.hi,
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprBlock(blk));
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if token::is_bar(&self.token) {
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.parse_lambda_expr();
|
2013-10-28 22:22:49 +00:00
|
|
|
} else if self.eat_keyword(keywords::Proc) {
|
|
|
|
let decl = self.parse_proc_decl();
|
|
|
|
let body = self.parse_expr();
|
2013-11-30 22:00:39 +00:00
|
|
|
let fakeblock = P(ast::Block {
|
2013-10-28 22:22:49 +00:00
|
|
|
view_items: ~[],
|
|
|
|
stmts: ~[],
|
|
|
|
expr: Some(body),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: DefaultBlock,
|
|
|
|
span: body.span,
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2013-10-28 22:22:49 +00:00
|
|
|
|
|
|
|
return self.mk_expr(lo, body.span.hi, ExprProc(decl, fakeblock));
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Self) {
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprSelf;
|
2013-05-10 22:15:06 +00:00
|
|
|
hi = self.span.hi;
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::If) {
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.parse_if_expr();
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::For) {
|
2013-09-08 12:08:01 +00:00
|
|
|
return self.parse_for_expr(None);
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Do) {
|
2013-07-04 11:48:45 +00:00
|
|
|
return self.parse_sugary_call_expr(lo, ~"do", DoSugar,
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprDoBody);
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::While) {
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.parse_while_expr();
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if Parser::token_is_lifetime(&self.token) {
|
|
|
|
let lifetime = self.get_lifetime();
|
2013-04-26 23:19:26 +00:00
|
|
|
self.bump();
|
|
|
|
self.expect(&token::COLON);
|
2013-09-08 12:08:01 +00:00
|
|
|
if self.eat_keyword(keywords::For) {
|
|
|
|
return self.parse_for_expr(Some(lifetime))
|
|
|
|
} else if self.eat_keyword(keywords::Loop) {
|
|
|
|
return self.parse_loop_expr(Some(lifetime))
|
|
|
|
} else {
|
|
|
|
self.fatal("expected `for` or `loop` after a label")
|
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Loop) {
|
2013-04-26 23:19:26 +00:00
|
|
|
return self.parse_loop_expr(None);
|
2013-09-26 00:56:54 +00:00
|
|
|
} else if self.eat_keyword(keywords::Continue) {
|
|
|
|
let lo = self.span.lo;
|
2013-12-30 23:09:41 +00:00
|
|
|
let ex = if Parser::token_is_lifetime(&self.token) {
|
|
|
|
let lifetime = self.get_lifetime();
|
2013-09-26 00:56:54 +00:00
|
|
|
self.bump();
|
|
|
|
ExprAgain(Some(lifetime.name))
|
|
|
|
} else {
|
|
|
|
ExprAgain(None)
|
|
|
|
};
|
|
|
|
let hi = self.span.hi;
|
|
|
|
return self.mk_expr(lo, hi, ex);
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Match) {
|
2013-01-04 14:52:07 +00:00
|
|
|
return self.parse_match_expr();
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Unsafe) {
|
2013-09-06 03:50:10 +00:00
|
|
|
return self.parse_block_expr(lo, UnsafeBlock(ast::UserProvided));
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LBRACKET {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-10-03 09:53:46 +00:00
|
|
|
let mutbl = MutImmutable;
|
2013-02-26 22:50:09 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RBRACKET {
|
2012-08-04 01:01:30 +00:00
|
|
|
// Empty vector.
|
|
|
|
self.bump();
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprVec(~[], mutbl);
|
2012-08-04 01:01:30 +00:00
|
|
|
} else {
|
|
|
|
// Nonempty vector.
|
|
|
|
let first_expr = self.parse_expr();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::COMMA &&
|
2013-07-02 19:47:32 +00:00
|
|
|
self.look_ahead(1, |t| *t == token::DOTDOT) {
|
2012-08-04 01:01:30 +00:00
|
|
|
// Repeating vector syntax: [ 0, ..512 ]
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
let count = self.parse_expr();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACKET);
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprRepeat(first_expr, count, mutbl);
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::COMMA {
|
2012-08-04 01:01:30 +00:00
|
|
|
// Vector with two or more elements.
|
|
|
|
self.bump();
|
2013-02-24 23:41:54 +00:00
|
|
|
let remaining_exprs = self.parse_seq_to_end(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::RBRACKET,
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_trailing_allowed(token::COMMA),
|
|
|
|
|p| p.parse_expr()
|
2013-03-02 04:35:55 +00:00
|
|
|
);
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprVec(~[first_expr] + remaining_exprs, mutbl);
|
2012-08-04 01:01:30 +00:00
|
|
|
} else {
|
|
|
|
// Vector with one element.
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACKET);
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprVec(~[first_expr], mutbl);
|
2012-08-04 01:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 10:08:34 +00:00
|
|
|
hi = self.last_span.hi;
|
2013-08-28 06:12:05 +00:00
|
|
|
} else if self.eat_keyword(keywords::__LogLevel) {
|
|
|
|
// LOG LEVEL expression
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LPAREN);
|
2013-08-28 06:12:05 +00:00
|
|
|
ex = ExprLogLevel;
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RPAREN);
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Return) {
|
2013-04-02 23:44:01 +00:00
|
|
|
// RETURN expression
|
2013-12-30 23:09:41 +00:00
|
|
|
if can_begin_expr(&self.token) {
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_expr();
|
|
|
|
hi = e.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprRet(Some(e));
|
|
|
|
} else { ex = ExprRet(None); }
|
2013-05-25 15:45:45 +00:00
|
|
|
} else if self.eat_keyword(keywords::Break) {
|
2013-04-02 23:44:01 +00:00
|
|
|
// BREAK expression
|
2013-12-30 23:09:41 +00:00
|
|
|
if Parser::token_is_lifetime(&self.token) {
|
|
|
|
let lifetime = self.get_lifetime();
|
2013-04-26 23:19:26 +00:00
|
|
|
self.bump();
|
2013-09-10 19:01:44 +00:00
|
|
|
ex = ExprBreak(Some(lifetime.name));
|
2012-08-15 02:20:56 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprBreak(None);
|
2012-08-15 02:20:56 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::MOD_SEP ||
|
|
|
|
is_ident(&self.token) && !self.is_keyword(keywords::True) &&
|
2013-05-25 15:45:45 +00:00
|
|
|
!self.is_keyword(keywords::False) {
|
2013-08-07 16:47:28 +00:00
|
|
|
let pth = self.parse_path(LifetimeAndTypesWithColons).path;
|
2012-06-25 22:04:50 +00:00
|
|
|
|
2013-03-02 21:02:27 +00:00
|
|
|
// `!`, as an operator, is prefix, so we know this isn't that
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::NOT {
|
2013-04-02 23:44:01 +00:00
|
|
|
// MACRO INVOCATION expression
|
2012-06-25 22:04:50 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-02-25 04:51:56 +00:00
|
|
|
token::LPAREN | token::LBRACE => {}
|
2013-05-19 05:07:44 +00:00
|
|
|
_ => self.fatal("expected open delimiter")
|
2012-11-07 23:16:58 +00:00
|
|
|
};
|
2013-02-22 02:12:13 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let ket = token::flip_delimiter(&self.token);
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
|
|
|
|
let tts = self.parse_seq_to_end(&ket,
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
2012-06-25 22:04:50 +00:00
|
|
|
let hi = self.span.hi;
|
2012-07-07 01:04:28 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
return self.mk_mac_expr(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT));
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LBRACE {
|
2012-07-23 23:39:18 +00:00
|
|
|
// This might be a struct literal.
|
2013-10-23 03:44:55 +00:00
|
|
|
if self.looking_at_struct_literal() {
|
2012-07-23 23:39:18 +00:00
|
|
|
// It's a struct literal.
|
|
|
|
self.bump();
|
|
|
|
let mut fields = ~[];
|
2012-09-08 22:50:29 +00:00
|
|
|
let mut base = None;
|
2013-03-02 03:57:05 +00:00
|
|
|
|
2013-04-22 00:26:41 +00:00
|
|
|
fields.push(self.parse_field());
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr(fields.last().expr, &[token::COMMA], &[token::RBRACE]);
|
2012-09-08 22:50:29 +00:00
|
|
|
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::DOTDOT) {
|
2012-09-08 22:50:29 +00:00
|
|
|
base = Some(self.parse_expr());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RBRACE {
|
2012-07-31 20:43:20 +00:00
|
|
|
// Accept an optional trailing comma.
|
|
|
|
break;
|
2012-07-23 23:39:18 +00:00
|
|
|
}
|
2013-04-22 00:26:41 +00:00
|
|
|
fields.push(self.parse_field());
|
2012-07-23 23:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hi = pth.span.hi;
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr_expecting(fields.last().expr, token::RBRACE);
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprStruct(pth, fields, base);
|
2012-10-28 00:14:09 +00:00
|
|
|
return self.mk_expr(lo, hi, ex);
|
2012-07-23 23:39:18 +00:00
|
|
|
}
|
2012-06-25 22:04:50 +00:00
|
|
|
}
|
2012-07-23 23:39:18 +00:00
|
|
|
|
|
|
|
hi = pth.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprPath(pth);
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2013-04-02 23:44:01 +00:00
|
|
|
// other literal expression
|
2012-05-23 22:06:11 +00:00
|
|
|
let lit = self.parse_lit();
|
|
|
|
hi = lit.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = ExprLit(@lit);
|
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
|
|
|
|
2013-04-02 23:44:01 +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)
|
2013-09-02 01:45:37 +00:00
|
|
|
-> @Expr {
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACE);
|
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
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse a.b or a(13) or a[4] or just a
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_dot_or_call_expr(&mut self) -> @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
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_dot_or_call_expr_with(&mut self, e0: @Expr) -> @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
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::DOT) {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::IDENT(i, _) => {
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
|
|
|
self.bump();
|
2013-02-26 19:35:17 +00:00
|
|
|
let (_, tys) = if self.eat(&token::MOD_SEP) {
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LT);
|
2013-02-15 05:50:03 +00:00
|
|
|
self.parse_generic_values_after_lt()
|
2012-11-30 19:18:25 +00:00
|
|
|
} else {
|
2013-02-26 19:35:17 +00:00
|
|
|
(opt_vec::Empty, ~[])
|
2012-11-30 19:18:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// expr.f() method call
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-04-11 20:10:06 +00:00
|
|
|
token::LPAREN => {
|
2012-11-30 19:18:25 +00:00
|
|
|
let es = self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2012-11-30 19:18:25 +00:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_expr()
|
|
|
|
);
|
2012-11-30 19:18:25 +00:00
|
|
|
hi = self.span.hi;
|
|
|
|
|
2013-06-01 22:31:56 +00:00
|
|
|
let nd = self.mk_method_call(e, i, tys, es, NoSugar);
|
2013-02-15 09:15:53 +00:00
|
|
|
e = self.mk_expr(lo, hi, nd);
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let field = self.mk_field(e, i, tys);
|
|
|
|
e = self.mk_expr(lo, hi, field)
|
2012-11-30 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
2012-05-23 22:06:11 +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(...)
|
2013-04-11 20:10:06 +00:00
|
|
|
token::LPAREN => {
|
2012-06-20 02:34:01 +00:00
|
|
|
let es = self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2012-06-12 17:17:36 +00:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_expr()
|
|
|
|
);
|
2012-06-08 05:09:27 +00:00
|
|
|
hi = self.span.hi;
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-06-01 22:31:56 +00:00
|
|
|
let nd = self.mk_call(e, es, NoSugar);
|
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[...]
|
2012-08-04 02:59:04 +00:00
|
|
|
token::LBRACKET => {
|
2012-05-31 01:14:40 +00:00
|
|
|
self.bump();
|
|
|
|
let ix = self.parse_expr();
|
|
|
|
hi = ix.span.hi;
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr_expecting(ix, token::RBRACKET);
|
2013-12-30 22:04:00 +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
|
|
|
|
2013-02-04 21:15:17 +00:00
|
|
|
// parse an optional separator followed by a kleene-style
|
|
|
|
// repetition token (+ or *).
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_sep_and_zerok(&mut self) -> (Option<token::Token>, bool) {
|
|
|
|
fn parse_zerok(parser: &mut Parser) -> Option<bool> {
|
2013-12-30 23:09:41 +00:00
|
|
|
match parser.token {
|
2013-07-18 03:04:37 +00:00
|
|
|
token::BINOP(token::STAR) | token::BINOP(token::PLUS) => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let zerok = parser.token == token::BINOP(token::STAR);
|
2013-07-18 03:04:37 +00:00
|
|
|
parser.bump();
|
|
|
|
Some(zerok)
|
|
|
|
},
|
|
|
|
_ => None
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
2013-07-18 03:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match parse_zerok(self) {
|
|
|
|
Some(zerok) => return (None, zerok),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let separator = self.bump_and_get();
|
|
|
|
match parse_zerok(self) {
|
|
|
|
Some(zerok) => (Some(separator), zerok),
|
|
|
|
None => self.fatal("expected `*` or `+`")
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:15:17 +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-01-09 13:05:33 +00:00
|
|
|
// parses token trees but also identifies TTSeq's
|
|
|
|
// and TTNonterminal'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 {
|
2013-11-25 02:18:20 +00:00
|
|
|
token::RPAREN | token::RBRACE | token::RBRACKET => {
|
|
|
|
// 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-30 23:41:11 +00:00
|
|
|
match p.open_braces.last_opt() {
|
|
|
|
None => {}
|
|
|
|
Some(&sp) => p.span_note(sp, "unclosed delimiter"),
|
|
|
|
};
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = p.this_token_to_str();
|
|
|
|
p.fatal(format!("incorrect close delimiter: `{}`",
|
|
|
|
token_str))
|
2013-11-25 02:18:20 +00:00
|
|
|
},
|
2012-06-27 22:29:35 +00:00
|
|
|
/* we ought to allow different depths of unquotation */
|
2013-12-30 22:40:31 +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
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if p.token == token::LPAREN {
|
2013-02-24 23:41:54 +00:00
|
|
|
let seq = p.parse_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree()
|
|
|
|
);
|
2012-07-05 21:30:56 +00:00
|
|
|
let (s, z) = p.parse_sep_and_zerok();
|
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-01-09 13:05:33 +00:00
|
|
|
TTSeq(mk_sp(sp.lo, p.span.hi), @seq, s, z)
|
2012-07-05 21:30:56 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
TTNonterminal(sp, p.parse_ident())
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
2012-06-27 22:29:35 +00:00
|
|
|
}
|
2013-02-04 21:15:17 +00:00
|
|
|
_ => {
|
|
|
|
parse_any_tt_tok(p)
|
|
|
|
}
|
2012-05-21 17:45:56 +00:00
|
|
|
}
|
2013-02-04 21:15:17 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
// turn the next token into a TTTok:
|
|
|
|
fn parse_any_tt_tok(p: &mut Parser) -> TokenTree {
|
|
|
|
TTTok(p.span, p.bump_and_get())
|
2012-05-21 17:45:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-02-24 23:41:54 +00:00
|
|
|
token::EOF => {
|
2013-12-30 23:41:11 +00:00
|
|
|
let open_braces = self.open_braces.clone();
|
|
|
|
for sp in open_braces.iter() {
|
2013-10-08 01:37:36 +00:00
|
|
|
self.span_note(*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
|
|
|
|
self.fatal("This file contains an un-closed delimiter ");
|
2013-02-24 23:41:54 +00:00
|
|
|
}
|
|
|
|
token::LPAREN | token::LBRACE | token::LBRACKET => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let close_delim = token::flip_delimiter(&self.token);
|
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);
|
2013-07-16 21:54:29 +00:00
|
|
|
let mut result = ~[parse_any_tt_tok(self)];
|
|
|
|
|
|
|
|
let trees =
|
|
|
|
self.parse_seq_to_before_end(&close_delim,
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree());
|
|
|
|
result.push_all_move(trees);
|
|
|
|
|
|
|
|
// Parse the close delimiter.
|
|
|
|
result.push(parse_any_tt_tok(self));
|
2013-10-07 19:43:42 +00:00
|
|
|
self.open_braces.pop();
|
2013-07-16 21:54:29 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
TTDelim(@result)
|
2013-02-24 23:41:54 +00:00
|
|
|
}
|
2013-03-02 21:02:27 +00:00
|
|
|
_ => 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-01-09 13:05:33 +00:00
|
|
|
pub fn parse_all_token_trees(&mut self) -> ~[TokenTree] {
|
2013-02-14 13:14:59 +00:00
|
|
|
let mut tts = ~[];
|
2013-12-30 23:09:41 +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-01-09 13:05:33 +00:00
|
|
|
pub fn parse_matchers(&mut self) -> ~[Matcher] {
|
|
|
|
// unification of Matcher's and TokenTree's would vastly improve
|
|
|
|
// the interpolation of Matcher's
|
|
|
|
maybe_whole!(self, NtMatchers);
|
2013-12-28 00:11:33 +00:00
|
|
|
let name_idx = @Cell::new(0u);
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-02-24 23:41:54 +00:00
|
|
|
token::LBRACE | token::LPAREN | token::LBRACKET => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let other_delimiter = token::flip_delimiter(&self.token);
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
self.parse_matcher_subseq_upto(name_idx, &other_delimiter)
|
2013-02-24 23:41:54 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
_ => self.fatal("expected open delimiter")
|
2012-08-01 21:48:30 +00:00
|
|
|
}
|
2012-07-10 00:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
// This goofy function is necessary to correctly match parens in Matcher's.
|
|
|
|
// Otherwise, `$( ( )` would be a valid Matcher, and `$( () )` would be
|
2012-07-10 00:04:06 +00:00
|
|
|
// invalid. It's similar to common::parse_seq.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_matcher_subseq_upto(&mut self,
|
2013-12-28 00:11:33 +00:00
|
|
|
name_idx: @Cell<uint>,
|
2013-07-02 19:47:32 +00:00
|
|
|
ket: &token::Token)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ~[Matcher] {
|
2012-07-10 00:04:06 +00:00
|
|
|
let mut ret_val = ~[];
|
|
|
|
let mut lparens = 0u;
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != *ket || lparens > 0u {
|
|
|
|
if self.token == token::LPAREN { lparens += 1u; }
|
|
|
|
if self.token == token::RPAREN { lparens -= 1u; }
|
2012-09-27 00:33:34 +00:00
|
|
|
ret_val.push(self.parse_matcher(name_idx));
|
2012-07-10 00:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.bump();
|
|
|
|
|
2012-08-02 00:30:05 +00:00
|
|
|
return ret_val;
|
2012-07-06 21:48:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn parse_matcher(&mut self, name_idx: @Cell<uint>) -> Matcher {
|
2012-06-12 17:59:50 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let m = if self.token == token::DOLLAR {
|
2012-06-12 17:59:50 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::LPAREN {
|
2013-12-28 00:11:33 +00:00
|
|
|
let name_idx_lo = name_idx.get();
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
let ms = self.parse_matcher_subseq_upto(name_idx,
|
|
|
|
&token::RPAREN);
|
2012-07-05 21:30:56 +00:00
|
|
|
if ms.len() == 0u {
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("repetition body must be nonempty");
|
2012-07-05 21:30:56 +00:00
|
|
|
}
|
|
|
|
let (sep, zerok) = self.parse_sep_and_zerok();
|
2014-01-09 13:05:33 +00:00
|
|
|
MatchSeq(ms, sep, zerok, name_idx_lo, name_idx.get())
|
2012-07-05 21:30:56 +00:00
|
|
|
} else {
|
|
|
|
let bound_to = self.parse_ident();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::COLON);
|
2012-07-05 21:30:56 +00:00
|
|
|
let nt_name = self.parse_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
let m = MatchNonterminal(bound_to, nt_name, name_idx.get());
|
2013-12-28 00:11:33 +00:00
|
|
|
name_idx.set(name_idx.get() + 1u);
|
2012-07-05 21:30:56 +00:00
|
|
|
m
|
2012-06-12 17:59:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
MatchTok(self.bump_and_get())
|
2012-06-12 17:59:50 +00:00
|
|
|
};
|
2012-07-05 21:30:56 +00:00
|
|
|
|
2012-08-02 00:30:05 +00:00
|
|
|
return spanned(lo, self.span.hi, m);
|
2012-06-12 17:59:50 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse a prefix-operator expr
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_prefix_expr(&mut self) -> @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 {
|
2012-08-04 02:59:04 +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
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::BINOP(b) => {
|
2012-08-06 19:34:08 +00:00
|
|
|
match b {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::MINUS => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-10-28 00:14:09 +00:00
|
|
|
let e = self.parse_prefix_expr();
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = e.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = self.mk_unary(UnNeg, e);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::STAR => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2012-10-28 00:14:09 +00:00
|
|
|
let e = self.parse_prefix_expr();
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = e.span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
ex = self.mk_unary(UnDeref, e);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::AND => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-02-12 00:33:31 +00:00
|
|
|
let _lt = self.parse_opt_lifetime();
|
2012-05-23 22:06:11 +00:00
|
|
|
let m = self.parse_mutability();
|
2012-10-28 00:14:09 +00:00
|
|
|
let e = self.parse_prefix_expr();
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = e.span.hi;
|
2014-01-01 02:09:50 +00:00
|
|
|
// HACK: turn &[...] into a &-vec
|
2012-08-06 19:34:08 +00:00
|
|
|
ex = match e.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprVec(..) | ExprLit(@codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitStr(..), span: _
|
2013-01-29 21:54:06 +00:00
|
|
|
})
|
2013-09-02 01:45:37 +00:00
|
|
|
if m == MutImmutable => {
|
|
|
|
ExprVstore(e, ExprVstoreSlice)
|
2012-06-26 05:38:29 +00:00
|
|
|
}
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprVec(..) if m == MutMutable => {
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprVstore(e, ExprVstoreMutSlice)
|
2012-12-08 00:26:52 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
_ => ExprAddrOf(m, e)
|
2012-06-26 05:38:29 +00:00
|
|
|
};
|
2012-05-23 22:06:11 +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
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::AT => {
|
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;
|
2014-01-01 02:09:50 +00:00
|
|
|
// HACK: turn @[...] into a @-vec
|
2012-08-06 19:34:08 +00:00
|
|
|
ex = match e.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprVec(..) |
|
2014-01-09 13:05:33 +00:00
|
|
|
ExprLit(@codemap::Spanned { node: LitStr(..), span: _}) |
|
2013-12-31 20:55:39 +00:00
|
|
|
ExprRepeat(..) => ExprVstore(e, ExprVstoreBox),
|
|
|
|
_ => self.mk_unary(UnBox, e)
|
2012-06-26 05:38:29 +00:00
|
|
|
};
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::TILDE => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
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;
|
2014-01-01 02:09:50 +00:00
|
|
|
// HACK: turn ~[...] into a ~-vec
|
2012-08-06 19:34:08 +00:00
|
|
|
ex = match e.node {
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprVec(..) |
|
2014-01-09 13:05:33 +00:00
|
|
|
ExprLit(@codemap::Spanned { node: LitStr(..), span: _}) |
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprRepeat(..) => ExprVstore(e, ExprVstoreUniq),
|
2013-09-02 01:45:37 +00:00
|
|
|
_ => self.mk_unary(UnUniq, e)
|
2012-06-26 05:38:29 +00:00
|
|
|
};
|
2012-03-08 20:12:04 +00:00
|
|
|
}
|
2013-12-12 18:42:03 +00:00
|
|
|
token::IDENT(_, _) if self.is_keyword(keywords::Box) => {
|
|
|
|
self.bump();
|
|
|
|
|
2013-12-18 00:46:18 +00:00
|
|
|
// Check for a place: `box(PLACE) EXPR`.
|
|
|
|
if self.eat(&token::LPAREN) {
|
|
|
|
// Support `box() EXPR` as the default.
|
|
|
|
if !self.eat(&token::RPAREN) {
|
|
|
|
let place = self.parse_expr();
|
|
|
|
self.expect(&token::RPAREN);
|
|
|
|
let subexpression = self.parse_prefix_expr();
|
|
|
|
hi = subexpression.span.hi;
|
|
|
|
ex = ExprBox(place, subexpression);
|
|
|
|
return self.mk_expr(lo, hi, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we use the unique pointer default.
|
2013-12-12 18:42:03 +00:00
|
|
|
let subexpression = self.parse_prefix_expr();
|
|
|
|
hi = subexpression.span.hi;
|
2014-01-01 02:09:50 +00:00
|
|
|
// HACK: turn `box [...]` into a boxed-vec
|
2013-12-12 18:42:03 +00:00
|
|
|
ex = match subexpression.node {
|
|
|
|
ExprVec(..) |
|
|
|
|
ExprLit(@codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitStr(..),
|
2013-12-12 18:42:03 +00:00
|
|
|
span: _
|
|
|
|
}) |
|
|
|
|
ExprRepeat(..) => ExprVstore(subexpression, ExprVstoreUniq),
|
|
|
|
_ => self.mk_unary(UnUniq, subexpression)
|
|
|
|
};
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-01-31 18:32:57 +00:00
|
|
|
// parse an expression of binops
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_binops(&mut self) -> @Expr {
|
|
|
|
let prefix_expr = self.parse_prefix_expr();
|
|
|
|
self.parse_more_binops(prefix_expr, 0)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 18:32:57 +00:00
|
|
|
// parse an expression of binops of at least min_prec precedence
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_more_binops(&mut self, lhs: @Expr, min_prec: uint) -> @Expr {
|
2012-10-28 00:14:09 +00:00
|
|
|
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.
|
|
|
|
{
|
2013-12-30 23:09:41 +00:00
|
|
|
let token: &token::Token = &self.token;
|
2013-12-30 23:34:28 +00:00
|
|
|
let restriction: &restriction = &self.restriction;
|
2013-07-18 03:04:37 +00:00
|
|
|
match (token, restriction) {
|
|
|
|
(&token::BINOP(token::OR), &RESTRICT_NO_BAR_OP) => return lhs,
|
|
|
|
(&token::BINOP(token::OR),
|
|
|
|
&RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs,
|
|
|
|
(&token::OROR, &RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) => return lhs,
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let cur_opt = token_to_binop(&self.token);
|
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);
|
2013-12-30 22:04:00 +00:00
|
|
|
let binary = self.mk_binary(cur_op, lhs, rhs);
|
|
|
|
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) {
|
|
|
|
let rhs = self.parse_ty(true);
|
|
|
|
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
|
|
|
|
2013-01-31 18:32:57 +00:00
|
|
|
// parse an assignment expression....
|
|
|
|
// actually, this seems to be the main entry point for
|
|
|
|
// parsing an arbitrary expression.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_assign_expr(&mut self) -> @Expr {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
let lhs = self.parse_binops();
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-03-28 23:31:12 +00:00
|
|
|
token::EQ => {
|
|
|
|
self.bump();
|
|
|
|
let rhs = self.parse_expr();
|
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
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::BINOPEQ(op) => {
|
2013-01-31 18:32:57 +00:00
|
|
|
self.bump();
|
|
|
|
let rhs = self.parse_expr();
|
2013-07-18 03:04:37 +00:00
|
|
|
let aop = match op {
|
2013-09-02 01:45:37 +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
|
|
|
};
|
2013-12-30 22:04:00 +00:00
|
|
|
let assign_op = self.mk_assign_op(aop, lhs, rhs);
|
|
|
|
self.mk_expr(lo, rhs.span.hi, assign_op)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::DARROW => {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteSwap);
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-05-06 04:43:19 +00:00
|
|
|
// Ignore what we get, this is an error anyway
|
|
|
|
self.parse_expr();
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, self.span.hi, ExprBreak(None))
|
2013-01-31 18:32:57 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
lhs
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse an 'if' expression ('if' token already eaten)
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_if_expr(&mut self) -> @Expr {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
|
|
|
let cond = self.parse_expr();
|
|
|
|
let thn = self.parse_block();
|
2013-09-02 01:45:37 +00:00
|
|
|
let mut els: Option<@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();
|
2012-08-20 19:23:37 +00:00
|
|
|
els = Some(elexpr);
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = elexpr.span.hi;
|
|
|
|
}
|
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
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// `|args| { ... }` or `{ ...}` like in `do` expressions
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_lambda_block_expr(&mut self) -> @Expr {
|
2012-07-02 05:18:41 +00:00
|
|
|
self.parse_lambda_expr_(
|
2013-12-30 22:04:00 +00:00
|
|
|
|p| {
|
2013-12-30 23:09:41 +00:00
|
|
|
match p.token {
|
2014-01-09 13:05:33 +00:00
|
|
|
token::BINOP(token::OR) | token::OROR => {
|
|
|
|
p.parse_fn_block_decl()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// No argument list - `do foo {`
|
|
|
|
P(FnDecl {
|
|
|
|
inputs: ~[],
|
|
|
|
output: P(Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: TyInfer,
|
|
|
|
span: p.span
|
|
|
|
}),
|
|
|
|
cf: Return,
|
|
|
|
variadic: false
|
|
|
|
})
|
|
|
|
}
|
2012-07-02 05:18:41 +00:00
|
|
|
}
|
|
|
|
},
|
2013-12-30 22:04:00 +00:00
|
|
|
|p| {
|
|
|
|
let blk = p.parse_block();
|
|
|
|
p.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk))
|
2012-07-02 05:18:41 +00:00
|
|
|
})
|
2012-06-30 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `|args| expr`
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_lambda_expr(&mut self) -> @Expr {
|
|
|
|
self.parse_lambda_expr_(|p| p.parse_fn_block_decl(),
|
|
|
|
|p| p.parse_expr())
|
2012-06-30 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse something of the form |args| expr
|
|
|
|
// this is used both in parsing a lambda expr
|
|
|
|
// and in parsing a block expr as e.g. in for...
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_lambda_expr_(&mut self,
|
2014-01-09 13:05:33 +00:00
|
|
|
parse_decl: |&mut Parser| -> P<FnDecl>,
|
2013-12-30 22:04:00 +00:00
|
|
|
parse_body: |&mut Parser| -> @Expr)
|
2013-09-02 01:45:37 +00:00
|
|
|
-> @Expr {
|
2012-07-01 21:53:46 +00:00
|
|
|
let lo = self.last_span.lo;
|
2013-12-30 22:04:00 +00:00
|
|
|
let decl = parse_decl(self);
|
|
|
|
let body = parse_body(self);
|
2013-11-30 22:00:39 +00:00
|
|
|
let fakeblock = P(ast::Block {
|
2013-01-15 03:35:08 +00:00
|
|
|
view_items: ~[],
|
|
|
|
stmts: ~[],
|
|
|
|
expr: Some(body),
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-27 08:25:59 +00:00
|
|
|
rules: DefaultBlock,
|
2013-07-16 18:08:35 +00:00
|
|
|
span: body.span,
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2013-07-16 18:08:35 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
return self.mk_expr(lo, body.span.hi, ExprFnBlock(decl, fakeblock));
|
2012-06-30 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_else_expr(&mut self) -> @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
|
|
|
|
2013-08-03 03:20:22 +00:00
|
|
|
// parse a 'for' .. 'in' expression ('for' token already eaten)
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> @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);
|
|
|
|
let expr = self.parse_expr();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 00:55:00 +00:00
|
|
|
// parse a 'do'.
|
|
|
|
// the 'do' expression parses as a call, but looks like
|
|
|
|
// a function call followed by a closure expression.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_sugary_call_expr(&mut self,
|
2013-11-19 20:21:21 +00:00
|
|
|
lo: BytePos,
|
2013-05-31 22:17:22 +00:00
|
|
|
keyword: ~str,
|
|
|
|
sugar: CallSugar,
|
2013-11-19 20:21:21 +00:00
|
|
|
ctor: |v: @Expr| -> Expr_)
|
2013-09-02 01:45:37 +00:00
|
|
|
-> @Expr {
|
2012-06-30 01:09:56 +00:00
|
|
|
// Parse the callee `foo` in
|
2013-12-04 00:55:00 +00:00
|
|
|
// do foo || {
|
|
|
|
// do foo.bar || {
|
2012-06-30 01:09:56 +00:00
|
|
|
// etc, or the portion of the call expression before the lambda in
|
2013-12-04 00:55:00 +00:00
|
|
|
// do foo() || {
|
2012-06-30 01:09:56 +00:00
|
|
|
// or
|
2013-12-04 00:55:00 +00:00
|
|
|
// do foo.bar(a) || {
|
2012-06-30 01:09:56 +00:00
|
|
|
// Turn on the restriction to stop at | or || so we can parse
|
|
|
|
// them as the lambda arguments
|
|
|
|
let e = self.parse_expr_res(RESTRICT_NO_BAR_OR_DOUBLEBAR_OP);
|
2013-03-20 01:24:01 +00:00
|
|
|
match e.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprCall(f, ref args, NoSugar) => {
|
2013-01-24 04:27:12 +00:00
|
|
|
let block = self.parse_lambda_block_expr();
|
|
|
|
let last_arg = self.mk_expr(block.span.lo, block.span.hi,
|
|
|
|
ctor(block));
|
2013-07-02 19:47:32 +00:00
|
|
|
let args = vec::append((*args).clone(), [last_arg]);
|
2013-09-02 01:45:37 +00:00
|
|
|
self.mk_expr(lo, block.span.hi, ExprCall(f, args, sugar))
|
2013-01-24 04:27:12 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprMethodCall(_, f, i, ref tps, ref args, NoSugar) => {
|
2013-01-24 04:27:12 +00:00
|
|
|
let block = self.parse_lambda_block_expr();
|
|
|
|
let last_arg = self.mk_expr(block.span.lo, block.span.hi,
|
|
|
|
ctor(block));
|
2013-07-02 19:47:32 +00:00
|
|
|
let args = vec::append((*args).clone(), [last_arg]);
|
2013-12-30 22:04:00 +00:00
|
|
|
let method_call = self.mk_method_call(f,
|
|
|
|
i,
|
|
|
|
(*tps).clone(),
|
|
|
|
args,
|
|
|
|
sugar);
|
|
|
|
self.mk_expr(lo, block.span.hi, method_call)
|
2013-01-24 04:27:12 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprField(f, i, ref tps) => {
|
2013-01-24 04:27:12 +00:00
|
|
|
let block = self.parse_lambda_block_expr();
|
|
|
|
let last_arg = self.mk_expr(block.span.lo, block.span.hi,
|
|
|
|
ctor(block));
|
2013-12-30 22:04:00 +00:00
|
|
|
let method_call = self.mk_method_call(f,
|
|
|
|
i,
|
|
|
|
(*tps).clone(),
|
|
|
|
~[last_arg],
|
|
|
|
sugar);
|
|
|
|
self.mk_expr(lo, block.span.hi, method_call)
|
2013-01-24 04:27:12 +00:00
|
|
|
}
|
2013-11-28 20:22:53 +00:00
|
|
|
ExprPath(..) | ExprCall(..) | ExprMethodCall(..) |
|
|
|
|
ExprParen(..) => {
|
2013-01-24 04:27:12 +00:00
|
|
|
let block = self.parse_lambda_block_expr();
|
|
|
|
let last_arg = self.mk_expr(block.span.lo, block.span.hi,
|
|
|
|
ctor(block));
|
2013-12-30 22:04:00 +00:00
|
|
|
let call = self.mk_call(e, ~[last_arg], sugar);
|
|
|
|
self.mk_expr(lo, last_arg.span.hi, call)
|
2013-01-24 04:27:12 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// There may be other types of expressions that can
|
2013-12-04 00:55:00 +00:00
|
|
|
// represent the callee in `do` expressions
|
2013-01-24 04:27:12 +00:00
|
|
|
// but they aren't represented by tests
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("sugary call on {:?}", e.node);
|
2013-01-24 04:27:12 +00:00
|
|
|
self.span_fatal(
|
2013-07-04 11:48:45 +00:00
|
|
|
e.span,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("`{}` must be followed by a block call", keyword));
|
2013-01-24 04:27:12 +00:00
|
|
|
}
|
2012-06-19 00:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_while_expr(&mut self) -> @Expr {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
|
|
|
let cond = self.parse_expr();
|
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;
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprWhile(cond, body));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> @Expr {
|
2012-09-04 20:36:54 +00:00
|
|
|
// loop headers look like 'loop {' or 'loop unsafe {'
|
|
|
|
let is_loop_header =
|
2013-12-30 23:09:41 +00:00
|
|
|
self.token == token::LBRACE
|
|
|
|
|| (is_ident(&self.token)
|
2013-07-02 19:47:32 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::LBRACE));
|
2012-09-04 20:36:54 +00:00
|
|
|
|
2013-04-26 23:19:26 +00:00
|
|
|
if is_loop_header {
|
2012-09-04 20:36:54 +00:00
|
|
|
// This is a loop body
|
|
|
|
let lo = self.last_span.lo;
|
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;
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprLoop(body, opt_ident));
|
2012-08-15 02:20:56 +00:00
|
|
|
} else {
|
2013-10-03 09:53:46 +00:00
|
|
|
// This is an obsolete 'continue' expression
|
2013-04-26 23:19:26 +00:00
|
|
|
if opt_ident.is_some() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span,
|
2013-05-02 16:28:53 +00:00
|
|
|
"a label may not be used with a `loop` expression");
|
2013-04-26 23:19:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteLoopAsContinue);
|
2012-09-04 20:36:54 +00:00
|
|
|
let lo = self.span.lo;
|
2013-12-30 23:09:41 +00:00
|
|
|
let ex = if Parser::token_is_lifetime(&self.token) {
|
|
|
|
let lifetime = self.get_lifetime();
|
2013-04-26 23:19:26 +00:00
|
|
|
self.bump();
|
2013-09-10 19:01:44 +00:00
|
|
|
ExprAgain(Some(lifetime.name))
|
2012-09-04 20:36:54 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
ExprAgain(None)
|
2012-09-04 20:36:54 +00:00
|
|
|
};
|
|
|
|
let hi = self.span.hi;
|
|
|
|
return self.mk_expr(lo, hi, ex);
|
2012-08-15 02:20:56 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 03:44:55 +00:00
|
|
|
// For distingishing between struct literals and blocks
|
2013-12-30 22:04:00 +00:00
|
|
|
fn looking_at_struct_literal(&mut self) -> bool {
|
2013-12-30 23:09:41 +00:00
|
|
|
self.token == token::LBRACE &&
|
2013-10-23 03:44:55 +00:00
|
|
|
(self.look_ahead(1, |t| token::is_plain_ident(t)) &&
|
|
|
|
self.look_ahead(2, |t| *t == token::COLON))
|
2012-07-31 20:43:20 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_match_expr(&mut self) -> @Expr {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.last_span.lo;
|
|
|
|
let discriminant = self.parse_expr();
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr_expecting(discriminant, token::LBRACE);
|
2013-09-02 01:45:37 +00:00
|
|
|
let mut arms: ~[Arm] = ~[];
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
2012-05-23 22:06:11 +00:00
|
|
|
let pats = self.parse_pats();
|
2012-08-20 19:23:37 +00:00
|
|
|
let mut guard = None;
|
2013-07-02 19:47:32 +00:00
|
|
|
if self.eat_keyword(keywords::If) {
|
|
|
|
guard = Some(self.parse_expr());
|
|
|
|
}
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::FAT_ARROW);
|
2012-08-06 05:09:57 +00:00
|
|
|
let expr = self.parse_expr_res(RESTRICT_STMT_EXPR);
|
2012-08-01 23:16:53 +00:00
|
|
|
|
|
|
|
let require_comma =
|
2012-08-07 00:14:32 +00:00
|
|
|
!classify::expr_is_simple_block(expr)
|
2013-12-30 23:09:41 +00:00
|
|
|
&& self.token != token::RBRACE;
|
2012-08-01 23:16:53 +00:00
|
|
|
|
|
|
|
if require_comma {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr(expr, &[token::COMMA], &[token::RBRACE]);
|
2012-08-01 23:16:53 +00:00
|
|
|
} else {
|
2013-02-24 17:54:41 +00:00
|
|
|
self.eat(&token::COMMA);
|
2012-08-01 23:16:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 22:00:39 +00:00
|
|
|
let blk = P(ast::Block {
|
2013-07-16 18:08:35 +00:00
|
|
|
view_items: ~[],
|
|
|
|
stmts: ~[],
|
|
|
|
expr: Some(expr),
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-27 08:25:59 +00:00
|
|
|
rules: DefaultBlock,
|
2013-01-15 03:35:08 +00:00
|
|
|
span: expr.span,
|
2013-11-30 22:00:39 +00:00
|
|
|
});
|
2012-08-01 23:16:53 +00:00
|
|
|
|
2013-09-02 01:45:37 +00:00
|
|
|
arms.push(ast::Arm { pats: pats, guard: guard, body: blk });
|
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();
|
2013-09-02 01:45:37 +00:00
|
|
|
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms));
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-11-24 22:42:01 +00:00
|
|
|
|
2013-01-30 17:56:33 +00:00
|
|
|
// parse an expression
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_expr(&mut self) -> @Expr {
|
2012-08-02 00:30:05 +00:00
|
|
|
return self.parse_expr_res(UNRESTRICTED);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
2013-01-30 17:56:33 +00:00
|
|
|
// parse an expression, subject to the given restriction
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_expr_res(&mut self, r: restriction) -> @Expr {
|
2013-12-30 23:34:28 +00:00
|
|
|
let old = self.restriction;
|
|
|
|
self.restriction = r;
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_assign_expr();
|
2013-12-30 23:34:28 +00:00
|
|
|
self.restriction = 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
|
|
|
|
2013-04-15 23:13:42 +00:00
|
|
|
// parse the RHS of a local variable declaration (e.g. '= 14;')
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_initializer(&mut self) -> Option<@Expr> {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == 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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse patterns, separated by '|' s
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_pats(&mut self) -> ~[@Pat] {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut pats = ~[];
|
2012-05-23 22:06:11 +00:00
|
|
|
loop {
|
2013-05-29 23:59:33 +00:00
|
|
|
pats.push(self.parse_pat());
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == 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,
|
2013-09-02 01:45:37 +00:00
|
|
|
) -> (~[@Pat], Option<@Pat>, ~[@Pat]) {
|
2013-02-26 18:58:46 +00:00
|
|
|
let mut before = ~[];
|
|
|
|
let mut slice = None;
|
|
|
|
let mut after = ~[];
|
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
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACKET {
|
2012-12-08 20:22:43 +00:00
|
|
|
if first { first = false; }
|
2013-02-24 18:20:24 +00:00
|
|
|
else { self.expect(&token::COMMA); }
|
2012-12-08 20:22:43 +00:00
|
|
|
|
2013-02-26 18:58:46 +00:00
|
|
|
let mut is_slice = false;
|
|
|
|
if before_slice {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::DOTDOT {
|
2013-02-26 18:58:46 +00:00
|
|
|
self.bump();
|
|
|
|
is_slice = true;
|
|
|
|
before_slice = false;
|
|
|
|
}
|
2012-12-08 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 18:58:46 +00:00
|
|
|
if is_slice {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::COMMA || self.token == token::RBRACKET {
|
2013-11-08 03:25:39 +00:00
|
|
|
slice = Some(@ast::Pat {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: PatWildMulti,
|
2013-12-30 23:17:53 +00:00
|
|
|
span: self.span,
|
2013-11-08 03:25:39 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let subpat = self.parse_pat();
|
|
|
|
match subpat {
|
|
|
|
@ast::Pat { id, node: PatWild, span } => {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteVecDotDotWildcard);
|
2013-11-08 03:25:39 +00:00
|
|
|
slice = Some(@ast::Pat {
|
|
|
|
id: id,
|
|
|
|
node: PatWildMulti,
|
|
|
|
span: span
|
|
|
|
})
|
|
|
|
},
|
2013-11-28 20:22:53 +00:00
|
|
|
@ast::Pat { node: PatIdent(_, _, _), .. } => {
|
2013-11-08 03:25:39 +00:00
|
|
|
slice = Some(subpat);
|
|
|
|
}
|
2013-11-28 20:22:53 +00:00
|
|
|
@ast::Pat { span, .. } => self.span_fatal(
|
2013-11-08 03:25:39 +00:00
|
|
|
span, "expected an identifier or nothing"
|
|
|
|
)
|
|
|
|
}
|
2012-12-08 20:22:43 +00:00
|
|
|
}
|
2013-02-26 18:58:46 +00:00
|
|
|
} else {
|
2013-11-08 03:25:39 +00:00
|
|
|
let subpat = self.parse_pat();
|
2013-02-26 18:58:46 +00:00
|
|
|
if before_slice {
|
|
|
|
before.push(subpat);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse the fields of a struct-like pattern
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_pat_fields(&mut self) -> (~[ast::FieldPat], bool) {
|
2012-08-07 00:01:14 +00:00
|
|
|
let mut fields = ~[];
|
|
|
|
let mut etc = false;
|
|
|
|
let mut first = true;
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
2013-12-19 17:21:05 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
self.expect(&token::COMMA);
|
|
|
|
// accept trailing commas
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RBRACE { break }
|
2013-12-19 17:21:05 +00:00
|
|
|
}
|
2012-08-07 00:01:14 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
etc = self.token == token::UNDERSCORE || self.token == token::DOTDOT;
|
|
|
|
if self.token == token::UNDERSCORE {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteStructWildcard);
|
2013-11-02 01:02:30 +00:00
|
|
|
}
|
|
|
|
if etc {
|
2012-08-07 00:01:14 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token != token::RBRACE {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `\\}`, found `{}`",
|
|
|
|
token_str))
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
|
|
|
etc = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lo1 = self.last_span.lo;
|
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();
|
2012-08-07 00:01:14 +00:00
|
|
|
let hi1 = self.last_span.lo;
|
|
|
|
let fieldpath = ast_util::ident_to_path(mk_sp(lo1, hi1),
|
|
|
|
fieldname);
|
2013-04-12 05:10:31 +00:00
|
|
|
let subpat;
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::COLON {
|
2013-12-06 08:15:02 +00:00
|
|
|
match bind_type {
|
2013-12-30 22:04:00 +00:00
|
|
|
BindByRef(..) | BindByValue(MutMutable) => {
|
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("unexpected `{}`", token_str))
|
|
|
|
}
|
2013-12-06 08:15:02 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:01:14 +00:00
|
|
|
self.bump();
|
2013-05-29 23:59:33 +00:00
|
|
|
subpat = self.parse_pat();
|
2012-08-07 00:01:14 +00:00
|
|
|
} else {
|
2013-09-02 01:45:37 +00:00
|
|
|
subpat = @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
|
2012-08-07 00:01:14 +00:00
|
|
|
};
|
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
fields.push(ast::FieldPat { ident: fieldname, pat: subpat });
|
2012-08-07 00:01:14 +00:00
|
|
|
}
|
|
|
|
return (fields, etc);
|
|
|
|
}
|
|
|
|
|
2013-05-29 23:59:33 +00:00
|
|
|
// parse a pattern.
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_pat(&mut self) -> @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 _
|
2013-07-02 19:47:32 +00:00
|
|
|
token::UNDERSCORE => {
|
|
|
|
self.bump();
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatWild;
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// parse @pat
|
2012-08-04 02:59:04 +00:00
|
|
|
token::AT => {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-05-29 23:59:33 +00:00
|
|
|
let sub = self.parse_pat();
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = sub.span.hi;
|
2012-07-14 02:08:18 +00:00
|
|
|
// HACK: parse @"..." as a literal of a vstore @str
|
2012-08-06 19:34:08 +00:00
|
|
|
pat = match sub.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(e@@Expr {
|
|
|
|
node: ExprLit(@codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitStr(..),
|
2013-11-28 20:22:53 +00:00
|
|
|
span: _}), ..
|
2012-08-04 02:59:04 +00:00
|
|
|
}) => {
|
2013-09-02 01:45:37 +00:00
|
|
|
let vst = @Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-09-02 01:45:37 +00:00
|
|
|
node: ExprVstore(e, ExprVstoreBox),
|
2013-01-15 21:51:43 +00:00
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
};
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(vst)
|
2012-07-14 02:08:18 +00:00
|
|
|
}
|
2014-01-03 22:15:08 +00:00
|
|
|
_ => {
|
|
|
|
self.obsolete(self.span, ObsoleteManagedPattern);
|
|
|
|
PatBox(sub)
|
|
|
|
}
|
2012-07-14 02:08:18 +00:00
|
|
|
};
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +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();
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = sub.span.hi;
|
2012-07-14 02:08:18 +00:00
|
|
|
// HACK: parse ~"..." as a literal of a vstore ~str
|
2012-08-06 19:34:08 +00:00
|
|
|
pat = match sub.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(e@@Expr {
|
|
|
|
node: ExprLit(@codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitStr(..),
|
2013-11-28 20:22:53 +00:00
|
|
|
span: _}), ..
|
2012-08-04 02:59:04 +00:00
|
|
|
}) => {
|
2013-09-02 01:45:37 +00:00
|
|
|
let vst = @Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-09-02 01:45:37 +00:00
|
|
|
node: ExprVstore(e, ExprVstoreUniq),
|
2013-01-15 21:51:43 +00:00
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
};
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(vst)
|
2012-07-14 02:08:18 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
_ => PatUniq(sub)
|
2012-07-14 02:08:18 +00:00
|
|
|
};
|
2013-07-02 19:47:32 +00:00
|
|
|
hi = self.last_span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-09-08 00:07:32 +00:00
|
|
|
token::BINOP(token::AND) => {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse &pat
|
2012-09-08 00:07:32 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
self.bump();
|
2013-05-29 23:59:33 +00:00
|
|
|
let sub = self.parse_pat();
|
2012-09-08 00:07:32 +00:00
|
|
|
hi = sub.span.hi;
|
|
|
|
// HACK: parse &"..." as a literal of a borrowed str
|
|
|
|
pat = match sub.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(e@@Expr {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: ExprLit(@codemap::Spanned{ node: LitStr(..), .. }),
|
2013-11-28 20:22:53 +00:00
|
|
|
..
|
2012-09-08 00:07:32 +00:00
|
|
|
}) => {
|
2013-09-02 01:45:37 +00:00
|
|
|
let vst = @Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-09-02 01:45:37 +00:00
|
|
|
node: ExprVstore(e, ExprVstoreSlice),
|
2012-09-12 04:25:01 +00:00
|
|
|
span: mk_sp(lo, hi)
|
|
|
|
};
|
2013-09-02 01:45:37 +00:00
|
|
|
PatLit(vst)
|
2012-09-08 00:07:32 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
_ => PatRegion(sub)
|
2013-07-02 19:47:32 +00:00
|
|
|
};
|
|
|
|
hi = self.last_span.hi;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
2012-09-08 00:07:32 +00:00
|
|
|
}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::LPAREN => {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse (pat,pat,pat,...) as tuple
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RPAREN {
|
2012-05-23 22:06:11 +00:00
|
|
|
hi = self.span.hi;
|
|
|
|
self.bump();
|
2013-08-31 16:13:04 +00:00
|
|
|
let lit = @codemap::Spanned {
|
2014-01-09 13:05:33 +00:00
|
|
|
node: LitNil,
|
2013-01-30 17:56:33 +00:00
|
|
|
span: mk_sp(lo, hi)};
|
2013-09-02 01:45:37 +00:00
|
|
|
let expr = self.mk_expr(lo, hi, ExprLit(lit));
|
|
|
|
pat = PatLit(expr);
|
2011-07-27 12:19:39 +00:00
|
|
|
} else {
|
2013-05-29 23:59:33 +00:00
|
|
|
let mut fields = ~[self.parse_pat()];
|
2013-07-02 19:47:32 +00:00
|
|
|
if self.look_ahead(1, |t| *t != token::RPAREN) {
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::COMMA {
|
2013-02-17 23:41:47 +00:00
|
|
|
self.bump();
|
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
|
|
|
}
|
2013-02-24 18:20:24 +00:00
|
|
|
if fields.len() == 1 { self.expect(&token::COMMA); }
|
|
|
|
self.expect(&token::RPAREN);
|
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;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-12-08 20:22:43 +00:00
|
|
|
token::LBRACKET => {
|
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
|
|
|
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACKET);
|
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;
|
2013-09-02 01:45:37 +00:00
|
|
|
return @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)
|
|
|
|
}
|
2012-12-08 20:22:43 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if !is_ident_or_path(&self.token)
|
2013-05-25 15:45:45 +00:00
|
|
|
|| self.is_keyword(keywords::True)
|
2013-07-02 19:47:32 +00:00
|
|
|
|| self.is_keyword(keywords::False) {
|
|
|
|
// 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();
|
|
|
|
if self.eat(&token::DOTDOT) {
|
2013-12-30 23:09:41 +00:00
|
|
|
let end = if is_ident_or_path(&self.token) {
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = self.parse_path(LifetimeAndTypesWithColons)
|
|
|
|
.path;
|
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));
|
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 {
|
2012-08-07 00:01:14 +00:00
|
|
|
token::LPAREN | token::LBRACKET | token::LT |
|
2013-07-02 19:47:32 +00:00
|
|
|
token::LBRACE | token::MOD_SEP => true,
|
|
|
|
_ => 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
|
|
|
|
2013-07-02 19:47:32 +00:00
|
|
|
if self.look_ahead(1, |t| *t == token::DOTDOT) {
|
|
|
|
let start = self.parse_expr_res(RESTRICT_NO_BAR_OP);
|
|
|
|
self.eat(&token::DOTDOT);
|
|
|
|
let end = self.parse_expr_res(RESTRICT_NO_BAR_OP);
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatRange(start, end);
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if is_plain_ident(&self.token) && !can_be_enum_or_struct {
|
2013-08-07 16:47:28 +00:00
|
|
|
let name = self.parse_path(NoTypesAllowed).path;
|
2013-07-02 19:47:32 +00:00
|
|
|
let sub;
|
|
|
|
if self.eat(&token::AT) {
|
|
|
|
// parse foo @ pat
|
|
|
|
sub = Some(self.parse_pat());
|
|
|
|
} else {
|
|
|
|
// or just foo
|
|
|
|
sub = None;
|
2013-05-05 01:57:14 +00:00
|
|
|
}
|
2013-10-20 12:31:23 +00:00
|
|
|
pat = PatIdent(BindByValue(MutImmutable), name, sub);
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
|
|
|
// parse an enum pat
|
2013-08-07 16:47:28 +00:00
|
|
|
let enum_path = self.parse_path(LifetimeAndTypesWithColons)
|
|
|
|
.path;
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-07-02 19:47:32 +00:00
|
|
|
token::LBRACE => {
|
|
|
|
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
|
|
|
_ => {
|
2013-09-02 01:45:37 +00:00
|
|
|
let mut args: ~[@Pat] = ~[];
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-07-02 19:47:32 +00:00
|
|
|
token::LPAREN => {
|
2013-11-21 00:23:04 +00:00
|
|
|
let is_star = self.look_ahead(1, |t| {
|
2013-07-02 19:47:32 +00:00
|
|
|
match *t {
|
|
|
|
token::BINOP(token::STAR) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
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 {
|
|
|
|
token::DOTDOT => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2013-11-02 01:02:30 +00:00
|
|
|
if is_star | is_dotdot {
|
2013-07-02 19:47:32 +00:00
|
|
|
// This is a "top constructor only" pat
|
|
|
|
self.bump();
|
2013-11-02 01:02:30 +00:00
|
|
|
if is_star {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteEnumWildcard);
|
2013-11-02 01:02:30 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
self.expect(&token::RPAREN);
|
2013-09-02 01:45:37 +00:00
|
|
|
pat = PatEnum(enum_path, None);
|
2013-07-02 19:47:32 +00:00
|
|
|
} else {
|
|
|
|
args = self.parse_unspanned_seq(
|
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
|
|
|
|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
|
|
|
},
|
|
|
|
_ => {
|
2013-08-07 16:47:28 +00:00
|
|
|
if enum_path.segments.len() == 1 {
|
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),
|
2013-07-02 19:47:32 +00:00
|
|
|
enum_path,
|
|
|
|
None);
|
|
|
|
} 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;
|
2013-09-02 01:45:37 +00:00
|
|
|
@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),
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 20:16:02 +00:00
|
|
|
// parse ident or ident @ pat
|
2013-04-02 23:44:01 +00:00
|
|
|
// 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_ {
|
2013-12-30 23:09:41 +00:00
|
|
|
if !is_plain_ident(&self.token) {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_fatal(self.last_span,
|
2013-05-02 16:28:53 +00:00
|
|
|
"expected identifier, found path");
|
2012-08-06 14:20:23 +00:00
|
|
|
}
|
2013-04-02 23:44:01 +00:00
|
|
|
// why a path here, and not just an identifier?
|
2013-08-07 16:47:28 +00:00
|
|
|
let name = self.parse_path(NoTypesAllowed).path;
|
2013-02-24 17:54:41 +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()
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::LPAREN {
|
2012-08-06 14:20:23 +00:00
|
|
|
self.span_fatal(
|
2013-12-30 23:30:14 +00:00
|
|
|
self.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
|
|
|
}
|
|
|
|
|
2013-04-15 23:13:42 +00:00
|
|
|
// parse a local variable declaration
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_local(&mut self) -> @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
|
|
|
});
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::COLON) { ty = self.parse_ty(false); }
|
2013-04-15 23:31:57 +00:00
|
|
|
let init = self.parse_initializer();
|
2013-07-19 05:38:55 +00:00
|
|
|
@ast::Local {
|
|
|
|
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),
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse a "let" stmt
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_let(&mut self) -> @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();
|
2013-02-24 17:54:41 +00:00
|
|
|
while self.eat(&token::COMMA) {
|
2013-10-20 12:31:23 +00:00
|
|
|
let _ = self.parse_local();
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteMultipleLocalDecl);
|
2010-11-24 22:42:01 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
return @spanned(lo, self.last_span.hi, DeclLocal(local));
|
2010-11-24 22:42:01 +00:00
|
|
|
}
|
|
|
|
|
2013-04-19 17:49:02 +00:00
|
|
|
// parse a structure field
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_name_and_ty(&mut self, pr: Visibility,
|
|
|
|
attrs: ~[Attribute]) -> StructField {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2013-12-30 23:09:41 +00:00
|
|
|
if !is_plain_ident(&self.token) {
|
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();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::COLON);
|
2012-05-23 22:06:11 +00:00
|
|
|
let ty = self.parse_ty(false);
|
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
|
|
|
}
|
|
|
|
|
2013-04-30 19:02:56 +00:00
|
|
|
// parse a statement. may include decl.
|
|
|
|
// precondition: any attributes are parsed already
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_stmt(&mut self, item_attrs: ~[Attribute]) -> @Stmt {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_whole!(self, NtStmt);
|
2012-08-01 21:34:35 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn check_expected_item(p: &mut Parser, found_attrs: bool) {
|
2012-05-23 22:06:11 +00:00
|
|
|
// If we have attributes then we should have an item
|
2013-07-02 19:47:32 +00:00
|
|
|
if found_attrs {
|
2013-12-30 23:30:14 +00:00
|
|
|
p.span_err(p.last_span, "expected item after attributes");
|
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;
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.is_keyword(keywords::Let) {
|
2013-07-02 19:47:32 +00:00
|
|
|
check_expected_item(self, !item_attrs.is_empty());
|
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();
|
2013-09-07 02:11:55 +00:00
|
|
|
return @spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID));
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if is_ident(&self.token)
|
|
|
|
&& !token::is_any_keyword(&self.token)
|
2013-07-02 19:47:32 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::NOT) {
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse a macro invocation. Looks like there's serious
|
|
|
|
// overlap here; if this clause doesn't catch it (and it
|
|
|
|
// won't, for brace-delimited macros) it will fall through
|
|
|
|
// to the macro clause of parse_item_or_view_item. This
|
|
|
|
// could use some cleanup, it appears to me.
|
2012-11-22 03:38:27 +00:00
|
|
|
|
2013-05-08 22:27:29 +00:00
|
|
|
// whoops! I now have a guess: I'm guessing the "parens-only"
|
|
|
|
// rule here is deliberate, to allow macro users to use parens
|
|
|
|
// for things that should be parsed as stmt_mac, and braces
|
|
|
|
// for things that should expand into items. Tricky, and
|
|
|
|
// somewhat awkward... and probably undocumented. Of course,
|
|
|
|
// I could just be wrong.
|
|
|
|
|
2013-07-02 19:47:32 +00:00
|
|
|
check_expected_item(self, !item_attrs.is_empty());
|
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...
|
2013-08-07 16:47:28 +00:00
|
|
|
let pth = self.parse_path(NoTypesAllowed).path;
|
2012-11-14 04:45:25 +00:00
|
|
|
self.bump();
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let id = if self.token == token::LPAREN {
|
2012-11-14 04:45:25 +00:00
|
|
|
token::special_idents::invalid // no special identifier
|
|
|
|
} else {
|
|
|
|
self.parse_ident()
|
|
|
|
};
|
|
|
|
|
|
|
|
let tts = self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
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;
|
|
|
|
|
|
|
|
if id == token::special_idents::invalid {
|
2013-09-02 01:45:37 +00:00
|
|
|
return @spanned(lo, hi, StmtMac(
|
2014-01-09 13:05:33 +00:00
|
|
|
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
|
2013-09-02 01:45:37 +00:00
|
|
|
return @spanned(lo, hi, StmtDecl(
|
|
|
|
@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))),
|
|
|
|
Inherited, ~[/*no attrs*/]))),
|
2013-09-07 02:11:55 +00:00
|
|
|
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();
|
|
|
|
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;
|
2013-09-02 01:45:37 +00:00
|
|
|
let decl = @spanned(lo, hi, DeclItem(i));
|
2013-09-07 02:11:55 +00:00
|
|
|
return @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-01-09 13:05:33 +00:00
|
|
|
IoviNone(_) => { /* fallthrough */ }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-06-15 20:27:39 +00:00
|
|
|
|
2013-07-02 19:47:32 +00:00
|
|
|
check_expected_item(self, found_attrs);
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2012-05-23 22:06:11 +00:00
|
|
|
// Remainder are line-expr stmts.
|
|
|
|
let e = self.parse_expr_res(RESTRICT_STMT_EXPR);
|
2013-09-07 02:11:55 +00:00
|
|
|
return @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
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// is this expression a successfully-parsed statement?
|
2013-12-30 22:04:00 +00:00
|
|
|
fn expr_is_complete(&mut self, e: @Expr) -> bool {
|
2013-12-30 23:34:28 +00:00
|
|
|
return self.restriction == RESTRICT_STMT_EXPR &&
|
2012-10-28 00:14:09 +00:00
|
|
|
!classify::expr_requires_semi_to_be_stmt(e);
|
2010-09-21 23:22:32 +00:00
|
|
|
}
|
|
|
|
|
2013-04-06 00:31:52 +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;
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteUnsafeBlock);
|
2013-04-06 00:31:52 +00:00
|
|
|
}
|
|
|
|
self.expect(&token::LBRACE);
|
|
|
|
|
2013-07-27 08:25:59 +00:00
|
|
|
return self.parse_block_tail_(lo, DefaultBlock, ~[]);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2010-11-30 01:11:03 +00:00
|
|
|
|
2013-04-06 00:31:52 +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)
|
2013-11-30 22:00:39 +00:00
|
|
|
-> (~[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;
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Unsafe) {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.obsolete(self.span, ObsoleteUnsafeBlock);
|
2013-01-23 19:43:58 +00:00
|
|
|
}
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACE);
|
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
|
|
|
|
2012-05-23 22:06:11 +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> {
|
2012-06-29 23:26:56 +00:00
|
|
|
self.parse_block_tail_(lo, s, ~[])
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-16 01:23:59 +00:00
|
|
|
|
2013-03-29 17:35:23 +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,
|
2013-11-30 22:00:39 +00:00
|
|
|
first_item_attrs: ~[Attribute]) -> P<Block> {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut stmts = ~[];
|
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 {
|
|
|
|
attrs_remaining: attrs_remaining,
|
|
|
|
view_items: view_items,
|
|
|
|
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
|
|
|
|
2013-08-03 16:45:23 +00:00
|
|
|
for item in items.iter() {
|
2013-09-02 01:45:37 +00:00
|
|
|
let decl = @spanned(item.span.lo, item.span.hi, DeclItem(*item));
|
2012-09-27 00:33:34 +00:00
|
|
|
stmts.push(@spanned(item.span.lo, item.span.hi,
|
2013-09-07 02:11:55 +00:00
|
|
|
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
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
while (self.token != token::RBRACE) {
|
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.
|
|
|
|
attributes_box.push_all(self.parse_outer_attributes());
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-09-27 00:33:34 +00:00
|
|
|
token::SEMI => {
|
2013-06-09 01:38:47 +00:00
|
|
|
if !attributes_box.is_empty() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span, "expected item after attributes");
|
2013-04-30 19:02:56 +00:00
|
|
|
attributes_box = ~[];
|
|
|
|
}
|
2012-09-27 00:33:34 +00:00
|
|
|
self.bump(); // empty
|
|
|
|
}
|
2013-04-30 19:02:56 +00:00
|
|
|
token::RBRACE => {
|
|
|
|
// 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);
|
|
|
|
attributes_box = ~[];
|
2012-09-27 00:33:34 +00:00
|
|
|
match stmt.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
StmtExpr(e, stmt_id) => {
|
2013-04-30 19:02:56 +00:00
|
|
|
// expression without semicolon
|
2013-08-05 20:18:29 +00:00
|
|
|
if classify::stmt_ends_with_semi(stmt) {
|
|
|
|
// Just check for errors and recover; do not eat semicolon yet.
|
|
|
|
self.commit_stmt(stmt, &[], &[token::SEMI, token::RBRACE]);
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-09-27 00:33:34 +00:00
|
|
|
token::SEMI => {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.bump();
|
2013-08-31 16:13:04 +00:00
|
|
|
stmts.push(@codemap::Spanned {
|
2013-09-02 01:45:37 +00:00
|
|
|
node: StmtSemi(e, stmt_id),
|
2013-08-05 20:18:29 +00:00
|
|
|
span: stmt.span,
|
|
|
|
});
|
2012-09-27 00:33:34 +00:00
|
|
|
}
|
|
|
|
token::RBRACE => {
|
|
|
|
expr = Some(e);
|
|
|
|
}
|
2013-08-05 20:18:29 +00:00
|
|
|
_ => {
|
2012-09-27 00:33:34 +00:00
|
|
|
stmts.push(stmt);
|
|
|
|
}
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-09-02 01:45:37 +00:00
|
|
|
StmtMac(ref m, _) => {
|
2013-04-30 19:02:56 +00:00
|
|
|
// statement macro; might be an expr
|
2013-07-02 19:47:32 +00:00
|
|
|
let has_semi;
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-11-19 05:36:26 +00:00
|
|
|
token::SEMI => {
|
2013-07-02 19:47:32 +00:00
|
|
|
has_semi = true;
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
|
|
|
token::RBRACE => {
|
|
|
|
// if a block ends in `m!(arg)` without
|
|
|
|
// a `;`, it must be an expr
|
2013-07-02 19:47:32 +00:00
|
|
|
has_semi = false;
|
2012-11-19 05:36:26 +00:00
|
|
|
expr = Some(
|
|
|
|
self.mk_mac_expr(stmt.span.lo,
|
|
|
|
stmt.span.hi,
|
2013-07-02 19:47:32 +00:00
|
|
|
m.node.clone()));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
has_semi = false;
|
|
|
|
stmts.push(stmt);
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if has_semi {
|
|
|
|
self.bump();
|
2013-08-31 16:13:04 +00:00
|
|
|
stmts.push(@codemap::Spanned {
|
2013-09-02 01:45:37 +00:00
|
|
|
node: StmtMac((*m).clone(), true),
|
2013-07-02 19:47:32 +00:00
|
|
|
span: stmt.span,
|
|
|
|
});
|
2012-11-19 05:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 19:02:56 +00:00
|
|
|
_ => { // all other kinds of statements:
|
2012-09-27 00:33:34 +00:00
|
|
|
stmts.push(stmt);
|
2012-05-23 22:06:11 +00:00
|
|
|
|
2013-04-17 16:15:08 +00:00
|
|
|
if classify::stmt_ends_with_semi(stmt) {
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_stmt_expecting(stmt, token::SEMI);
|
2012-09-27 00:33:34 +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() {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span, "expected item after attributes");
|
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
|
|
|
}
|
|
|
|
|
2013-04-23 23:47:47 +00:00
|
|
|
// matches optbounds = ( ( : ( boundseq )? )? )
|
|
|
|
// where boundseq = ( bound + boundseq ) | bound
|
2013-04-01 22:50:58 +00:00
|
|
|
// and bound = 'static | ty
|
2013-06-20 22:23:25 +00:00
|
|
|
// Returns "None" if there's no colon (e.g. "T");
|
|
|
|
// Returns "Some(Empty)" if there's a colon but nothing after (e.g. "T:")
|
|
|
|
// Returns "Some(stuff)" otherwise (e.g. "T:stuff").
|
|
|
|
// NB: The None/Some distinction is important for issue #7264.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_optional_ty_param_bounds(&mut self) -> Option<OptVec<TyParamBound>> {
|
2013-02-28 15:25:31 +00:00
|
|
|
if !self.eat(&token::COLON) {
|
2013-06-20 22:23:25 +00:00
|
|
|
return None;
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
2012-09-08 22:50:29 +00:00
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
let mut result = opt_vec::Empty;
|
|
|
|
loop {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-03-22 00:29:49 +00:00
|
|
|
token::LIFETIME(lifetime) => {
|
2013-06-12 17:02:55 +00:00
|
|
|
if "static" == self.id_to_str(lifetime) {
|
2013-03-22 00:29:49 +00:00
|
|
|
result.push(RegionTyParamBound);
|
|
|
|
} else {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_err(self.span,
|
2013-05-02 16:28:53 +00:00
|
|
|
"`'static` is the only permissible region bound here");
|
2013-03-22 00:29:49 +00:00
|
|
|
}
|
|
|
|
self.bump();
|
2013-02-15 05:50:03 +00:00
|
|
|
}
|
2013-11-28 20:22:53 +00:00
|
|
|
token::MOD_SEP | token::IDENT(..) => {
|
2013-07-31 21:23:22 +00:00
|
|
|
let tref = self.parse_trait_ref();
|
|
|
|
result.push(TraitTyParamBound(tref));
|
2012-08-07 01:54:20 +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
|
|
|
|
2013-06-17 19:16:30 +00:00
|
|
|
if !self.eat(&token::BINOP(token::PLUS)) {
|
|
|
|
break;
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
|
2013-06-20 22:23:25 +00:00
|
|
|
return Some(result);
|
2012-08-07 01:54:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 23:47:47 +00:00
|
|
|
// matches typaram = IDENT optbounds
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_ty_param(&mut self) -> TyParam {
|
2012-08-07 01:54:20 +00:00
|
|
|
let ident = self.parse_ident();
|
2013-06-20 22:23:25 +00:00
|
|
|
let opt_bounds = self.parse_optional_ty_param_bounds();
|
|
|
|
// For typarams we don't care about the difference b/w "<T>" and "<T:>".
|
2013-09-10 02:32:32 +00:00
|
|
|
let bounds = opt_bounds.unwrap_or_default();
|
2013-09-07 02:11:55 +00:00
|
|
|
ast::TyParam { ident: ident, id: ast::DUMMY_NODE_ID, bounds: bounds }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-04 22:16:41 +00:00
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse a set of optional generic type parameter declarations
|
|
|
|
// 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 {
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::LT) {
|
2013-02-15 05:50:03 +00:00
|
|
|
let lifetimes = self.parse_lifetimes();
|
|
|
|
let ty_params = self.parse_seq_to_gt(
|
2013-02-12 00:33:31 +00:00
|
|
|
Some(token::COMMA),
|
2013-02-15 05:50:03 +00:00
|
|
|
|p| p.parse_ty_param());
|
2013-02-28 15:25:31 +00:00
|
|
|
ast::Generics { lifetimes: lifetimes, ty_params: ty_params }
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_generic_values_after_lt(&mut self) -> (OptVec<ast::Lifetime>, ~[P<Ty>]) {
|
2013-02-26 19:35:17 +00:00
|
|
|
let lifetimes = self.parse_lifetimes();
|
2013-02-15 05:50:03 +00:00
|
|
|
let result = self.parse_seq_to_gt(
|
|
|
|
Some(token::COMMA),
|
|
|
|
|p| p.parse_ty(false));
|
2013-02-26 19:35:17 +00:00
|
|
|
(lifetimes, opt_vec::take_vec(result))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> (~[Arg], bool) {
|
2013-12-30 23:17:53 +00:00
|
|
|
let sp = self.span;
|
2014-01-09 13:05:33 +00:00
|
|
|
let mut args: ~[Option<Arg>] =
|
2012-06-12 17:17:36 +00:00
|
|
|
self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2013-02-24 23:41:54 +00:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
2013-10-25 05:56:34 +00:00
|
|
|
|p| {
|
2013-12-30 23:09:41 +00:00
|
|
|
if p.token == token::DOTDOTDOT {
|
2013-10-25 05:56:34 +00:00
|
|
|
p.bump();
|
|
|
|
if allow_variadic {
|
2013-12-30 23:09:41 +00:00
|
|
|
if p.token != token::RPAREN {
|
2013-12-30 23:17:53 +00:00
|
|
|
p.span_fatal(p.span,
|
2013-10-25 05:56:34 +00:00
|
|
|
"`...` must be last in argument list for variadic function");
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-30 23:17:53 +00:00
|
|
|
p.span_fatal(p.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-10-25 05:56:34 +00:00
|
|
|
let variadic = match args.pop_opt() {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = args.move_iter().map(|x| x.unwrap()).collect();
|
|
|
|
|
|
|
|
(args, variadic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2012-05-23 22:06:11 +00:00
|
|
|
let (ret_style, 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,
|
|
|
|
cf: ret_style,
|
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 {
|
2013-09-05 21:14:31 +00:00
|
|
|
token::IDENT(id, false) => id.name == special_idents::self_.name,
|
|
|
|
_ => false
|
|
|
|
}
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
fn expect_self_ident(&mut self) {
|
2012-07-30 23:33:02 +00:00
|
|
|
if !self.is_self_ident() {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `self` but found `{}`", token_str))
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
|
2013-04-04 21:30:11 +00:00
|
|
|
// parse the argument list and result type of a function
|
|
|
|
// that may have a self type.
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_fn_decl_with_self(&mut self, parse_arg_fn: |&mut Parser| -> Arg)
|
|
|
|
-> (ExplicitSelf, P<FnDecl>) {
|
2013-11-19 20:21:21 +00:00
|
|
|
fn maybe_parse_explicit_self(cnstr: |v: Mutability| ->
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ExplicitSelf_,
|
2013-12-30 22:04:00 +00:00
|
|
|
p: &mut Parser)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ast::ExplicitSelf_ {
|
2013-10-03 09:53:46 +00:00
|
|
|
// We need to make sure it isn't a type
|
2013-07-02 19:47:32 +00:00
|
|
|
if p.look_ahead(1, |t| token::is_keyword(keywords::Self, t)) ||
|
|
|
|
((p.look_ahead(1, |t| token::is_keyword(keywords::Const, t)) ||
|
|
|
|
p.look_ahead(1, |t| token::is_keyword(keywords::Mut, t))) &&
|
|
|
|
p.look_ahead(2, |t| token::is_keyword(keywords::Self, t))) {
|
2012-08-17 22:25:35 +00:00
|
|
|
|
|
|
|
p.bump();
|
|
|
|
let mutability = p.parse_mutability();
|
|
|
|
p.expect_self_ident();
|
|
|
|
cnstr(mutability)
|
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfStatic
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 `&`.
|
|
|
|
|
2013-07-02 19:47:32 +00:00
|
|
|
if this.look_ahead(1, |t| token::is_keyword(keywords::Self, t)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
|
|
|
this.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfRegion(None, MutImmutable)
|
2013-12-30 22:04:00 +00:00
|
|
|
} else if this.look_ahead(1, |t| Parser::token_is_mutability(t)) &&
|
2013-07-02 19:47:32 +00:00
|
|
|
this.look_ahead(2,
|
|
|
|
|t| token::is_keyword(keywords::Self,
|
|
|
|
t)) {
|
2013-05-10 22:15:06 +00:00
|
|
|
this.bump();
|
|
|
|
let mutability = this.parse_mutability();
|
|
|
|
this.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfRegion(None, mutability)
|
2013-12-30 22:04:00 +00:00
|
|
|
} else if this.look_ahead(1, |t| Parser::token_is_lifetime(t)) &&
|
2013-07-02 19:47:32 +00:00
|
|
|
this.look_ahead(2,
|
|
|
|
|t| token::is_keyword(keywords::Self,
|
|
|
|
t)) {
|
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
|
|
|
this.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfRegion(Some(lifetime), MutImmutable)
|
2013-12-30 22:04:00 +00:00
|
|
|
} else if this.look_ahead(1, |t| Parser::token_is_lifetime(t)) &&
|
|
|
|
this.look_ahead(2, |t| {
|
|
|
|
Parser::token_is_mutability(t)
|
|
|
|
}) &&
|
2013-07-02 19:47:32 +00:00
|
|
|
this.look_ahead(3, |t| token::is_keyword(keywords::Self,
|
|
|
|
t)) {
|
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();
|
|
|
|
this.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfRegion(Some(lifetime), mutability)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LPAREN);
|
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;
|
2013-12-30 23:09:41 +00:00
|
|
|
let explicit_self = match self.token {
|
2012-08-17 22:25:35 +00:00
|
|
|
token::BINOP(token::AND) => {
|
2013-04-30 15:49:48 +00:00
|
|
|
maybe_parse_borrowed_explicit_self(self)
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
token::AT => {
|
2014-01-09 13:05:33 +00:00
|
|
|
maybe_parse_explicit_self(SelfBox, self)
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
token::TILDE => {
|
2013-06-24 04:12:17 +00:00
|
|
|
maybe_parse_explicit_self(|mutability| {
|
2013-09-02 01:45:37 +00:00
|
|
|
if mutability != MutImmutable {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span,
|
2013-10-03 09:53:46 +00:00
|
|
|
"mutability declaration not allowed here");
|
2013-06-24 04:12:17 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfUniq(MutImmutable)
|
2013-06-24 04:12:17 +00:00
|
|
|
}, self)
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
2013-11-28 20:22:53 +00:00
|
|
|
token::IDENT(..) if self.is_self_ident() => {
|
2012-08-17 22:25:35 +00:00
|
|
|
self.bump();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfValue(MutImmutable)
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
2013-08-23 00:15:09 +00:00
|
|
|
token::BINOP(token::STAR) => {
|
|
|
|
// Possibly "*self" or "*mut self" -- not supported. Try to avoid
|
|
|
|
// emitting cryptic "unexpected token" errors.
|
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
let mutability = if Parser::token_is_mutability(&self.token) {
|
2013-10-20 05:55:23 +00:00
|
|
|
self.parse_mutability()
|
|
|
|
} else { MutImmutable };
|
2013-08-23 00:15:09 +00:00
|
|
|
if self.is_self_ident() {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_err(self.span, "cannot pass self by unsafe pointer");
|
2013-08-23 00:15:09 +00:00
|
|
|
self.bump();
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfValue(mutability)
|
2013-10-20 05:55:23 +00:00
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
_ if Parser::token_is_mutability(&self.token) &&
|
2013-10-20 05:55:23 +00:00
|
|
|
self.look_ahead(1, |t| token::is_keyword(keywords::Self, t)) => {
|
|
|
|
let mutability = self.parse_mutability();
|
|
|
|
self.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfValue(mutability)
|
2013-08-23 00:15:09 +00:00
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
_ if Parser::token_is_mutability(&self.token) &&
|
2013-10-20 06:34:01 +00:00
|
|
|
self.look_ahead(1, |t| *t == token::TILDE) &&
|
|
|
|
self.look_ahead(2, |t| token::is_keyword(keywords::Self, t)) => {
|
|
|
|
let mutability = self.parse_mutability();
|
|
|
|
self.bump();
|
|
|
|
self.expect_self_ident();
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfUniq(mutability)
|
2013-10-20 06:34:01 +00:00
|
|
|
}
|
2012-08-17 22:25:35 +00:00
|
|
|
_ => {
|
2014-01-09 13:05:33 +00:00
|
|
|
SelfStatic
|
2012-08-17 22:25:35 +00:00
|
|
|
}
|
|
|
|
};
|
2012-07-30 23:33:02 +00:00
|
|
|
|
|
|
|
// If we parsed a self type, expect a comma before the argument list.
|
2013-09-14 02:07:43 +00:00
|
|
|
let fn_inputs;
|
2014-01-09 13:05:33 +00:00
|
|
|
if explicit_self != SelfStatic {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-07-30 23:33:02 +00:00
|
|
|
token::COMMA => {
|
|
|
|
self.bump();
|
|
|
|
let sep = seq_sep_trailing_disallowed(token::COMMA);
|
2013-09-14 02:07:43 +00:00
|
|
|
fn_inputs = self.parse_seq_to_before_end(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::RPAREN,
|
2013-02-24 23:41:54 +00:00
|
|
|
sep,
|
|
|
|
parse_arg_fn
|
2013-03-02 04:35:55 +00:00
|
|
|
);
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
token::RPAREN => {
|
2013-09-14 02:07:43 +00:00
|
|
|
fn_inputs = ~[];
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `,` or `)`, found `{}`",
|
|
|
|
token_str))
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let sep = seq_sep_trailing_disallowed(token::COMMA);
|
2013-12-30 22:04:00 +00:00
|
|
|
fn_inputs = self.parse_seq_to_before_end(&token::RPAREN,
|
|
|
|
sep,
|
|
|
|
parse_arg_fn);
|
2012-07-30 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RPAREN);
|
2012-07-30 23:33:02 +00:00
|
|
|
|
|
|
|
let hi = self.span.hi;
|
|
|
|
|
|
|
|
let (ret_style, ret_ty) = self.parse_ret_ty();
|
|
|
|
|
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
|
|
|
cf: ret_style,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse the |arg, arg| header on a lambda
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_fn_block_decl(&mut self) -> P<FnDecl> {
|
2012-05-23 22:06:11 +00:00
|
|
|
let inputs_captures = {
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::OROR) {
|
2012-06-29 23:26:56 +00:00
|
|
|
~[]
|
2012-05-23 22:06:11 +00:00
|
|
|
} else {
|
2012-06-12 17:17:36 +00:00
|
|
|
self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::BINOP(token::OR),
|
|
|
|
&token::BINOP(token::OR),
|
2012-06-12 17:17:36 +00:00
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_fn_block_arg()
|
|
|
|
)
|
2012-05-04 19:33:04 +00:00
|
|
|
}
|
|
|
|
};
|
2013-02-24 17:54:41 +00:00
|
|
|
let output = if self.eat(&token::RARROW) {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.parse_ty(false)
|
|
|
|
} else {
|
2013-12-30 23:17:53 +00:00
|
|
|
P(Ty {
|
|
|
|
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,
|
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
};
|
2013-01-16 00:05:20 +00:00
|
|
|
|
2014-01-09 13:05:33 +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,
|
2014-01-09 13:05:33 +00:00
|
|
|
cf: Return,
|
2013-10-25 05:56:34 +00:00
|
|
|
variadic: false
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 22:22:49 +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 =
|
|
|
|
self.parse_unspanned_seq(&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
|
|
|
seq_sep_trailing_allowed(token::COMMA),
|
|
|
|
|p| p.parse_fn_block_arg());
|
|
|
|
|
|
|
|
let output = if self.eat(&token::RARROW) {
|
|
|
|
self.parse_ty(false)
|
|
|
|
} else {
|
2013-11-30 22:00:39 +00:00
|
|
|
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,
|
2013-11-30 22:00:39 +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,
|
2014-01-09 13:05:33 +00:00
|
|
|
cf: Return,
|
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
|
|
|
}
|
|
|
|
|
2013-04-04 21:30:11 +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,
|
|
|
|
attrs: ~[Attribute]) -> @Item {
|
|
|
|
@Item {
|
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: node,
|
|
|
|
vis: vis,
|
|
|
|
span: mk_sp(lo, hi)
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 21:30:11 +00:00
|
|
|
// parse an item-position function declaration.
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_fn(&mut self, purity: Purity, abis: AbiSet) -> ItemInfo {
|
2013-02-15 05:50:03 +00:00
|
|
|
let (ident, generics) = self.parse_fn_header();
|
2013-10-25 05:56:34 +00:00
|
|
|
let decl = self.parse_fn_decl(false);
|
2013-04-06 00:31:52 +00:00
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
|
2014-01-09 13:05:33 +00:00
|
|
|
(ident, ItemFn(decl, purity, abis, generics, body), Some(inner_attrs))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:48:46 +00:00
|
|
|
// parse a method in a trait impl, starting with `attrs` attributes.
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_method(&mut self, already_parsed_attrs: Option<~[Attribute]>) -> @Method {
|
2013-11-24 00:48:46 +00:00
|
|
|
let next_attrs = self.parse_outer_attributes();
|
|
|
|
let attrs = match already_parsed_attrs {
|
|
|
|
Some(mut a) => { a.push_all_move(next_attrs); a }
|
|
|
|
None => next_attrs
|
|
|
|
};
|
|
|
|
|
2012-08-02 23:01:38 +00:00
|
|
|
let lo = self.span.lo;
|
|
|
|
|
2013-08-07 07:11:34 +00:00
|
|
|
let visa = self.parse_visibility();
|
2012-08-02 23:01:38 +00:00
|
|
|
let pur = self.parse_fn_purity();
|
2013-03-06 17:30:54 +00:00
|
|
|
let ident = self.parse_ident();
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = self.parse_generics();
|
2013-11-21 00:23:04 +00:00
|
|
|
let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
|
2012-07-30 23:33:02 +00:00
|
|
|
p.parse_arg()
|
2013-11-21 00:23:04 +00:00
|
|
|
});
|
2013-03-13 02:32:14 +00:00
|
|
|
|
2013-04-06 00:31:52 +00:00
|
|
|
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
|
2013-02-26 14:35:36 +00:00
|
|
|
let hi = body.span.hi;
|
2012-06-28 06:09:51 +00:00
|
|
|
let attrs = vec::append(attrs, inner_attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
@ast::Method {
|
2013-01-16 00:05:20 +00:00
|
|
|
ident: ident,
|
|
|
|
attrs: attrs,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: generics,
|
2013-04-30 15:49:48 +00:00
|
|
|
explicit_self: explicit_self,
|
2013-01-16 00:05:20 +00:00
|
|
|
purity: pur,
|
|
|
|
decl: decl,
|
|
|
|
body: body,
|
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),
|
2013-09-07 02:11:55 +00:00
|
|
|
self_id: ast::DUMMY_NODE_ID,
|
2013-01-16 00:05:20 +00:00
|
|
|
vis: visa,
|
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse trait Foo { ... }
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_trait(&mut self) -> ItemInfo {
|
2012-05-24 19:38:45 +00:00
|
|
|
let ident = self.parse_ident();
|
2013-02-15 05:50:03 +00:00
|
|
|
let tps = self.parse_generics();
|
2012-08-03 22:24:11 +00:00
|
|
|
|
2012-08-03 22:02:01 +00:00
|
|
|
// Parse traits, if necessary.
|
|
|
|
let traits;
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::COLON {
|
2012-08-03 22:02:01 +00:00
|
|
|
self.bump();
|
2013-02-25 02:51:04 +00:00
|
|
|
traits = self.parse_trait_ref_list(&token::LBRACE);
|
2012-08-03 22:02:01 +00:00
|
|
|
} else {
|
|
|
|
traits = ~[];
|
|
|
|
}
|
|
|
|
|
2012-07-10 20:44:20 +00:00
|
|
|
let meths = self.parse_trait_methods();
|
2014-01-09 13:05:33 +00:00
|
|
|
(ident, ItemTrait(tps, traits, meths), None)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:14:53 +00:00
|
|
|
// Parses two variants (with the region/type params always optional):
|
2013-02-27 01:12:00 +00:00
|
|
|
// impl<T> Foo { ... }
|
|
|
|
// impl<T> ToStr for ~[T] { ... }
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_impl(&mut self) -> ItemInfo {
|
2012-07-24 23:38:24 +00:00
|
|
|
// First, parse type parameters if necessary.
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = self.parse_generics();
|
2012-07-24 23:38:24 +00:00
|
|
|
|
2012-08-08 22:34:17 +00:00
|
|
|
// This is a new-style impl declaration.
|
2012-07-18 23:18:02 +00:00
|
|
|
// XXX: clownshoes
|
2012-09-14 22:20:09 +00:00
|
|
|
let ident = special_idents::clownshoes_extensions;
|
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.
|
2013-12-30 23:09:41 +00:00
|
|
|
let could_be_trait = self.token != token::LPAREN;
|
2013-03-29 01:55:35 +00:00
|
|
|
|
2013-02-15 05:17:26 +00:00
|
|
|
// Parse the trait.
|
2013-01-29 19:14:53 +00:00
|
|
|
let mut ty = self.parse_ty(false);
|
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-01-09 13:05:33 +00:00
|
|
|
TyPath(ref path, None, node_id) => {
|
|
|
|
Some(TraitRef {
|
2013-07-02 19:47:32 +00:00
|
|
|
path: /* bad */ (*path).clone(),
|
2013-01-29 19:14:53 +00:00
|
|
|
ref_id: node_id
|
|
|
|
})
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
TyPath(..) => {
|
2013-06-17 19:16:30 +00:00
|
|
|
self.span_err(ty.span,
|
|
|
|
"bounded traits are only valid in type position");
|
|
|
|
None
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ty = self.parse_ty(false);
|
|
|
|
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
|
|
|
|
2012-11-14 03:08:01 +00:00
|
|
|
let mut meths = ~[];
|
2013-11-24 00:48:46 +00:00
|
|
|
let inner_attrs = if self.eat(&token::SEMI) {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteEmptyImpl);
|
2013-11-24 00:48:46 +00:00
|
|
|
None
|
2013-09-19 19:09:52 +00:00
|
|
|
} else {
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACE);
|
2013-11-24 00:48:46 +00:00
|
|
|
let (inner_attrs, next) = self.parse_inner_attrs_and_next();
|
|
|
|
let mut method_attrs = Some(next);
|
2013-02-24 17:54:41 +00:00
|
|
|
while !self.eat(&token::RBRACE) {
|
2013-11-24 00:48:46 +00:00
|
|
|
meths.push(self.parse_method(method_attrs));
|
|
|
|
method_attrs = None;
|
2012-10-23 00:57:10 +00:00
|
|
|
}
|
2013-11-24 00:48:46 +00:00
|
|
|
Some(inner_attrs)
|
|
|
|
};
|
2012-10-23 00:57:10 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
(ident, ItemImpl(generics, opt_trait, ty, meths), inner_attrs)
|
2012-11-07 02:41:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse a::B<~str,int>
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_trait_ref(&mut self) -> TraitRef {
|
|
|
|
ast::TraitRef {
|
2013-08-07 16:47:28 +00:00
|
|
|
path: self.parse_path(LifetimeAndTypesWithoutColons).path,
|
2013-09-07 02:11:55 +00:00
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
2013-01-16 00:05:20 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse B + C<~str,int> + D
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_trait_ref_list(&mut self, ket: &token::Token) -> ~[TraitRef] {
|
2012-06-12 17:17:36 +00:00
|
|
|
self.parse_seq_to_before_end(
|
2013-02-25 02:55:24 +00:00
|
|
|
ket,
|
2013-03-05 00:11:30 +00:00
|
|
|
seq_sep_trailing_disallowed(token::BINOP(token::PLUS)),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_trait_ref()
|
2013-03-02 04:35:55 +00:00
|
|
|
)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// parse struct Foo { ... }
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_struct(&mut self) -> ItemInfo {
|
2013-03-06 17:30:54 +00:00
|
|
|
let class_name = self.parse_ident();
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = self.parse_generics();
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
let mut fields: ~[StructField];
|
2012-10-24 21:36:00 +00:00
|
|
|
let is_tuple_like;
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2013-02-24 17:54:41 +00:00
|
|
|
if self.eat(&token::LBRACE) {
|
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;
|
2012-08-16 00:10:23 +00:00
|
|
|
fields = ~[];
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
2013-10-03 09:53:46 +00:00
|
|
|
fields.push(self.parse_struct_decl_field());
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-03-28 01:17:58 +00:00
|
|
|
if fields.len() == 0 {
|
2013-09-28 04:01:58 +00:00
|
|
|
self.fatal(format!("Unit-like struct definition should be written as `struct {};`",
|
2013-06-12 17:02:55 +00:00
|
|
|
get_ident_interner().get(class_name.name)));
|
2013-03-28 01:17:58 +00:00
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
self.bump();
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LPAREN {
|
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(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
2013-11-21 00:23:04 +00:00
|
|
|
seq_sep_trailing_allowed(token::COMMA),
|
|
|
|
|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_ {
|
|
|
|
kind: UnnamedField,
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-01 03:20:08 +00:00
|
|
|
ty: p.parse_ty(false),
|
|
|
|
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
|
|
|
});
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::SEMI);
|
2013-02-24 17:54:41 +00:00
|
|
|
} 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;
|
2012-08-16 00:10:23 +00:00
|
|
|
fields = ~[];
|
|
|
|
} else {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected `\\{`, `(`, or `;` after struct \
|
|
|
|
name but found `{}`",
|
|
|
|
token_str))
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2013-09-07 02:11:55 +00:00
|
|
|
let _ = ast::DUMMY_NODE_ID; // XXX: Workaround for crazy bug.
|
|
|
|
let new_id = ast::DUMMY_NODE_ID;
|
2012-10-08 18:49:01 +00:00
|
|
|
(class_name,
|
2014-01-09 13:05:33 +00:00
|
|
|
ItemStruct(@ast::StructDef {
|
2013-01-13 20:29:36 +00:00
|
|
|
fields: fields,
|
2012-10-24 21:36:00 +00:00
|
|
|
ctor_id: if is_tuple_like { Some(new_id) } else { None }
|
2013-02-15 05:50:03 +00:00
|
|
|
}, generics),
|
2012-10-08 18:49:01 +00:00
|
|
|
None)
|
2012-02-01 03:30:40 +00:00
|
|
|
}
|
|
|
|
|
2013-04-19 17:41:59 +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,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: ~[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 {
|
2013-02-25 03:27:43 +00:00
|
|
|
token::COMMA => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
token::RBRACE => {}
|
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_fatal(self.span,
|
2013-10-03 09:53:46 +00:00
|
|
|
format!("expected `,`, or `\\}` but found `{}`",
|
2013-12-30 22:04:00 +00:00
|
|
|
token_str))
|
2013-02-25 03:27:43 +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
|
|
|
|
2013-04-19 18:19:37 +00:00
|
|
|
// parse an element of a struct definition
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_struct_decl_field(&mut self) -> 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::Priv) {
|
2014-01-09 13:05:33 +00:00
|
|
|
return self.parse_single_struct_field(Private, attrs);
|
2012-08-03 20:58:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.eat_keyword(keywords::Pub) {
|
2014-01-09 13:05:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse visiility: 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 if self.eat_keyword(keywords::Priv) { Private }
|
|
|
|
else { Inherited }
|
2012-02-23 05:47:23 +00:00
|
|
|
}
|
2013-03-22 19:56:10 +00:00
|
|
|
|
2013-02-11 21:36:24 +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,
|
2013-07-19 11:51:37 +00:00
|
|
|
first_item_attrs: ~[Attribute])
|
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 {
|
|
|
|
attrs_remaining: attrs_remaining,
|
|
|
|
view_items: view_items,
|
|
|
|
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-01-09 13:05:33 +00:00
|
|
|
let mut items: ~[@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 {
|
2013-03-20 01:24:01 +00:00
|
|
|
attrs = attrs_remaining + attrs;
|
2012-06-28 06:09:51 +00:00
|
|
|
first = false;
|
|
|
|
}
|
2013-10-21 20:08:31 +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
|
|
|
_ => {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
|
|
|
self.fatal(format!("expected item but found `{}`",
|
|
|
|
token_str))
|
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
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span, "expected item after attributes");
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-02-04 16:10:04 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::Mod { view_items: view_items, items: items }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_item_const(&mut self) -> ItemInfo {
|
2013-09-02 01:45:37 +00:00
|
|
|
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
|
2013-03-06 17:30:54 +00:00
|
|
|
let id = self.parse_ident();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::COLON);
|
2012-05-23 22:06:11 +00:00
|
|
|
let ty = self.parse_ty(false);
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::EQ);
|
2012-05-23 22:06:11 +00:00
|
|
|
let e = self.parse_expr();
|
2013-08-05 20:18:29 +00:00
|
|
|
self.commit_expr_expecting(e, token::SEMI);
|
2014-01-09 13:05:33 +00:00
|
|
|
(id, ItemStatic(ty, m, e), None)
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 17:51:11 +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();
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == 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);
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACE);
|
2013-02-21 08:16:31 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
|
|
|
let m = self.parse_mod_items(token::RBRACE, next);
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACE);
|
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]) {
|
2013-06-04 19:34:25 +00:00
|
|
|
let default_path = token::interner_get(id.name);
|
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,
|
|
|
|
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) {
|
2012-12-11 20:20:27 +00:00
|
|
|
self.mod_path_stack.pop();
|
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +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-01-09 13:05:33 +00:00
|
|
|
-> (ast::Item_, ~[ast::Attribute]) {
|
2013-12-30 23:17:53 +00:00
|
|
|
let mut prefix = Path::new(self.sess.cm.span_to_filename(self.span));
|
2013-09-27 00:21:59 +00:00
|
|
|
prefix.pop();
|
2013-12-30 23:38:02 +00:00
|
|
|
let mod_path = Path::new(".").join_many(self.mod_path_stack);
|
2013-10-08 02:16:58 +00:00
|
|
|
let dir_path = prefix.join(&mod_path);
|
2012-11-19 01:56:50 +00:00
|
|
|
let file_path = match ::attr::first_attr_value_str_by_name(
|
2013-07-02 19:47:32 +00:00
|
|
|
outer_attrs, "path") {
|
2013-10-06 02:49:32 +00:00
|
|
|
Some(d) => dir_path.join(d),
|
2013-07-20 02:11:30 +00:00
|
|
|
None => {
|
|
|
|
let mod_name = token::interner_get(id.name).to_owned();
|
|
|
|
let default_path_str = mod_name + ".rs";
|
|
|
|
let secondary_path_str = mod_name + "/mod.rs";
|
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();
|
|
|
|
match (default_exists, secondary_exists) {
|
|
|
|
(true, false) => default_path,
|
|
|
|
(false, true) => secondary_path,
|
|
|
|
(false, false) => {
|
2013-09-28 04:01:58 +00:00
|
|
|
self.span_fatal(id_sp, format!("file not found for module `{}`", mod_name));
|
2013-07-20 02:11:30 +00:00
|
|
|
}
|
|
|
|
(true, true) => {
|
|
|
|
self.span_fatal(id_sp,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("file for module `{}` found at both {} and {}",
|
2013-07-20 02:11:30 +00:00
|
|
|
mod_name, default_path_str, secondary_path_str));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 01:56:50 +00:00
|
|
|
};
|
|
|
|
|
2013-07-20 02:11:30 +00:00
|
|
|
self.eval_src_mod_from_path(file_path,
|
2013-07-02 19:47:32 +00:00
|
|
|
outer_attrs.to_owned(),
|
|
|
|
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,
|
2013-07-19 11:51:37 +00:00
|
|
|
outer_attrs: ~[ast::Attribute],
|
2014-01-09 13:05:33 +00:00
|
|
|
id_sp: Span) -> (ast::Item_, ~[ast::Attribute]) {
|
2013-12-27 19:51:18 +00:00
|
|
|
{
|
|
|
|
let mut included_mod_stack = self.sess
|
|
|
|
.included_mod_stack
|
|
|
|
.borrow_mut();
|
|
|
|
let maybe_i = included_mod_stack.get()
|
|
|
|
.iter()
|
|
|
|
.position(|p| *p == path);
|
|
|
|
match maybe_i {
|
|
|
|
Some(i) => {
|
|
|
|
let mut err = ~"circular modules: ";
|
|
|
|
let len = included_mod_stack.get().len();
|
|
|
|
for p in included_mod_stack.get().slice(i, len).iter() {
|
|
|
|
p.display().with_str(|s| err.push_str(s));
|
|
|
|
err.push_str(" -> ");
|
|
|
|
}
|
|
|
|
path.display().with_str(|s| err.push_str(s));
|
|
|
|
self.span_fatal(id_sp, err);
|
2013-07-04 17:51:11 +00:00
|
|
|
}
|
2013-12-27 19:51:18 +00:00
|
|
|
None => ()
|
2013-07-04 17:51:11 +00:00
|
|
|
}
|
2013-12-27 19:51:18 +00:00
|
|
|
included_mod_stack.get().push(path.clone());
|
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,
|
2013-07-02 19:47:32 +00:00
|
|
|
id_sp);
|
2013-02-21 08:16:31 +00:00
|
|
|
let (inner, next) = p0.parse_inner_attrs_and_next();
|
2013-05-12 04:25:31 +00:00
|
|
|
let mod_attrs = vec::append(outer_attrs, inner);
|
2013-02-21 08:16:31 +00:00
|
|
|
let first_item_outer_attrs = next;
|
2012-11-19 01:56:50 +00:00
|
|
|
let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
|
2013-12-27 19:51:18 +00:00
|
|
|
{
|
|
|
|
let mut included_mod_stack = self.sess
|
|
|
|
.included_mod_stack
|
|
|
|
.borrow_mut();
|
|
|
|
included_mod_stack.get().pop();
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
return (ast::ItemMod(m0), mod_attrs);
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +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,
|
|
|
|
attrs: ~[Attribute]) -> @ForeignItem {
|
2012-08-24 01:17:16 +00:00
|
|
|
let lo = self.span.lo;
|
2013-08-02 21:30:00 +00:00
|
|
|
|
|
|
|
// Parse obsolete purity.
|
2012-08-24 01:17:16 +00:00
|
|
|
let purity = self.parse_fn_purity();
|
2014-01-09 13:05:33 +00:00
|
|
|
if purity != ImpureFn {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteUnsafeExternFn);
|
2013-08-02 21:30:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 05:50:03 +00:00
|
|
|
let (ident, generics) = self.parse_fn_header();
|
2013-10-25 05:56:34 +00:00
|
|
|
let decl = self.parse_fn_decl(true);
|
2013-04-12 05:10:31 +00:00
|
|
|
let hi = self.span.hi;
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::SEMI);
|
2014-01-09 13:05:33 +00:00
|
|
|
@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
|
|
|
}
|
|
|
|
|
2013-10-03 09:53:46 +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,
|
|
|
|
attrs: ~[Attribute]) -> @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();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::COLON);
|
2012-08-25 22:09:33 +00:00
|
|
|
let ty = self.parse_ty(false);
|
|
|
|
let hi = self.span.hi;
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::SEMI);
|
2014-01-09 13:05:33 +00:00
|
|
|
@ast::ForeignItem { ident: ident,
|
|
|
|
attrs: attrs,
|
|
|
|
node: ForeignItemStatic(ty, mutbl),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: mk_sp(lo, hi),
|
|
|
|
vis: vis }
|
2012-08-25 22:09:33 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse safe/unsafe and fn
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_fn_purity(&mut self) -> Purity {
|
|
|
|
if self.eat_keyword(keywords::Fn) { ImpureFn }
|
2013-10-03 09:53:46 +00:00
|
|
|
else if self.eat_keyword(keywords::Unsafe) {
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Fn);
|
2014-01-09 13:05:33 +00:00
|
|
|
UnsafeFn
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-05-24 19:38:45 +00:00
|
|
|
else { self.unexpected(); }
|
2012-04-25 03:16:53 +00:00
|
|
|
}
|
2012-02-13 17:56:09 +00:00
|
|
|
|
2013-04-02 23:44:01 +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,
|
2013-04-17 16:15:08 +00:00
|
|
|
abis: AbiSet,
|
2013-07-19 11:51:37 +00:00
|
|
|
first_item_attrs: ~[Attribute])
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ForeignMod {
|
2013-02-21 08:16:31 +00:00
|
|
|
let ParsedItemsAndViewItems {
|
2013-04-30 19:02:56 +00:00
|
|
|
attrs_remaining: attrs_remaining,
|
2013-02-21 08:16:31 +00:00
|
|
|
view_items: view_items,
|
|
|
|
items: _,
|
|
|
|
foreign_items: foreign_items
|
2013-04-01 22:50:58 +00:00
|
|
|
} = self.parse_foreign_items(first_item_attrs, true);
|
2013-04-30 19:02:56 +00:00
|
|
|
if (! attrs_remaining.is_empty()) {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_err(self.last_span,
|
2013-05-23 16:39:24 +00:00
|
|
|
"expected item after attributes");
|
2013-04-30 19:02:56 +00:00
|
|
|
}
|
2013-12-30 23:09:41 +00:00
|
|
|
assert!(self.token == token::RBRACE);
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::ForeignMod {
|
2013-03-14 02:25:28 +00:00
|
|
|
abis: abis,
|
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
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse extern foo; or extern mod foo { ... } or extern { ... }
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_item_foreign_mod(&mut self,
|
2013-03-14 02:25:28 +00:00
|
|
|
lo: BytePos,
|
|
|
|
opt_abis: Option<AbiSet>,
|
2014-01-09 13:05:33 +00:00
|
|
|
visibility: Visibility,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: ~[Attribute],
|
2012-08-14 21:22:52 +00:00
|
|
|
items_allowed: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ItemOrViewItem {
|
2012-08-31 00:09:04 +00:00
|
|
|
let mut must_be_named_mod = false;
|
2013-05-25 15:45:45 +00:00
|
|
|
if self.is_keyword(keywords::Mod) {
|
2012-08-31 00:09:04 +00:00
|
|
|
must_be_named_mod = true;
|
2013-05-25 15:45:45 +00:00
|
|
|
self.expect_keyword(keywords::Mod);
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token != token::LBRACE {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_fatal(self.span,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("expected `\\{` or `mod` but found `{}`",
|
2013-12-30 22:04:00 +00:00
|
|
|
token_str))
|
2012-07-31 23:38:41 +00:00
|
|
|
}
|
2012-08-31 00:09:04 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
let (named, maybe_path, ident) = match self.token {
|
2013-11-28 20:22:53 +00:00
|
|
|
token::IDENT(..) => {
|
2013-07-31 20:47:32 +00:00
|
|
|
let the_ident = self.parse_ident();
|
2013-12-30 23:09:41 +00:00
|
|
|
let path = if self.token == token::EQ {
|
2013-07-31 20:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
Some(self.parse_str())
|
|
|
|
}
|
|
|
|
else { None };
|
2013-10-10 19:57:34 +00:00
|
|
|
(true, path, the_ident)
|
2013-07-31 20:47:32 +00:00
|
|
|
}
|
2012-08-31 00:09:04 +00:00
|
|
|
_ => {
|
|
|
|
if must_be_named_mod {
|
2013-12-30 22:04:00 +00:00
|
|
|
let token_str = self.this_token_to_str();
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_fatal(self.span,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("expected foreign module name but \
|
2013-12-30 22:04:00 +00:00
|
|
|
found `{}`",
|
|
|
|
token_str))
|
2012-08-31 00:09:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 19:57:34 +00:00
|
|
|
(false, None,
|
2012-09-14 22:20:09 +00:00
|
|
|
special_idents::clownshoes_foreign_mod)
|
2012-08-31 00:09:04 +00:00
|
|
|
}
|
2012-08-29 19:22:05 +00:00
|
|
|
};
|
2012-08-14 18:07:41 +00:00
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// extern mod foo { ... } or extern { ... }
|
2013-02-24 17:54:41 +00:00
|
|
|
if items_allowed && self.eat(&token::LBRACE) {
|
2013-05-09 21:14:42 +00:00
|
|
|
// `extern mod foo { ... }` is obsolete.
|
2013-10-10 19:57:34 +00:00
|
|
|
if named {
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteNamedExternModule);
|
2013-05-09 21:14:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 02:57:08 +00:00
|
|
|
let abis = opt_abis.unwrap_or(AbiSet::C());
|
2012-11-28 01:25:55 +00:00
|
|
|
|
2013-02-21 08:16:31 +00:00
|
|
|
let (inner, next) = self.parse_inner_attrs_and_next();
|
2013-10-10 19:57:34 +00:00
|
|
|
let m = self.parse_foreign_mod_items(abis, next);
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACE);
|
2012-11-28 01:25:55 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
ident,
|
2014-01-09 13:05:33 +00:00
|
|
|
ItemForeignMod(m),
|
2013-12-30 22:04:00 +00:00
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, Some(inner)));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2012-08-14 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 02:25:28 +00:00
|
|
|
if opt_abis.is_some() {
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span_err(self.span, "an ABI may not be specified here");
|
2012-11-28 01:25:55 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 22:54:32 +00:00
|
|
|
|
2014-01-02 20:30:58 +00:00
|
|
|
if self.token == token::LPAREN {
|
2013-11-26 22:54:32 +00:00
|
|
|
// `extern mod foo (name = "bar"[,vers = "version"]) is obsolete,
|
|
|
|
// `extern mod foo = "bar#[version]";` should be used.
|
|
|
|
// Parse obsolete options to avoid wired parser errors
|
|
|
|
self.parse_optional_meta();
|
2014-01-02 20:30:58 +00:00
|
|
|
self.obsolete(self.span, ObsoleteExternModAttributesInParens);
|
2013-11-26 22:54:32 +00:00
|
|
|
}
|
2012-08-14 18:07:41 +00:00
|
|
|
// extern mod foo;
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::SEMI);
|
2014-01-09 13:05:33 +00:00
|
|
|
IoviViewItem(ast::ViewItem {
|
|
|
|
node: ViewItemExternMod(ident, maybe_path, ast::DUMMY_NODE_ID),
|
2013-07-02 19:47:32 +00:00
|
|
|
attrs: attrs,
|
2012-08-14 18:07:41 +00:00
|
|
|
vis: visibility,
|
|
|
|
span: mk_sp(lo, self.last_span.hi)
|
2013-01-14 00:51:48 +00:00
|
|
|
})
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2011-02-01 18:40:04 +00:00
|
|
|
|
2013-03-29 17:35:23 +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();
|
2013-02-15 05:50:03 +00:00
|
|
|
let tps = self.parse_generics();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::EQ);
|
2012-05-23 22:06:11 +00:00
|
|
|
let ty = self.parse_ty(false);
|
2013-02-24 18:20:24 +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
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse a structure-like enum variant definition
|
|
|
|
// this should probably be renamed or refactored...
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_struct_def(&mut self) -> @StructDef {
|
|
|
|
let mut fields: ~[StructField] = ~[];
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
2013-10-03 09:53:46 +00:00
|
|
|
fields.push(self.parse_struct_decl_field());
|
2012-08-09 02:51:19 +00:00
|
|
|
}
|
|
|
|
self.bump();
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
return @ast::StructDef {
|
2013-01-13 20:29:36 +00:00
|
|
|
fields: fields,
|
2012-10-25 21:18:48 +00:00
|
|
|
ctor_id: None
|
2012-08-09 02:51:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +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 {
|
2013-02-28 15:25:31 +00:00
|
|
|
let mut variants = ~[];
|
2013-06-05 04:43:41 +00:00
|
|
|
let mut all_nullary = true;
|
|
|
|
let mut have_disr = false;
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token != token::RBRACE {
|
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;
|
|
|
|
let mut args = ~[];
|
|
|
|
let mut disr_expr = None;
|
2013-03-28 18:29:21 +00:00
|
|
|
ident = self.parse_ident();
|
|
|
|
if self.eat(&token::LBRACE) {
|
|
|
|
// Parse a struct variant.
|
|
|
|
all_nullary = false;
|
2014-01-09 13:05:33 +00:00
|
|
|
kind = StructVariantKind(self.parse_struct_def());
|
2013-12-30 23:09:41 +00:00
|
|
|
} else if self.token == token::LPAREN {
|
2013-03-28 18:29:21 +00:00
|
|
|
all_nullary = false;
|
|
|
|
let arg_tys = self.parse_unspanned_seq(
|
|
|
|
&token::LPAREN,
|
|
|
|
&token::RPAREN,
|
|
|
|
seq_sep_trailing_disallowed(token::COMMA),
|
|
|
|
|p| p.parse_ty(false)
|
|
|
|
);
|
2013-08-08 02:21:36 +00:00
|
|
|
for ty in arg_tys.move_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);
|
2013-03-28 18:29:21 +00:00
|
|
|
} else if self.eat(&token::EQ) {
|
|
|
|
have_disr = true;
|
|
|
|
disr_expr = Some(self.parse_expr());
|
2014-01-09 13:05:33 +00:00
|
|
|
kind = TupleVariantKind(args);
|
2013-03-28 18:29:21 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
kind = TupleVariantKind(~[]);
|
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
|
|
|
|
2013-04-19 16:45:02 +00:00
|
|
|
if !self.eat(&token::COMMA) { break; }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::RBRACE);
|
2012-05-23 22:06:11 +00:00
|
|
|
if (have_disr && !all_nullary) {
|
2013-05-19 05:07:44 +00:00
|
|
|
self.fatal("discriminator values can only be used with a c-like \
|
2012-05-23 22:06:11 +00:00
|
|
|
enum");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +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();
|
2013-02-15 05:50:03 +00:00
|
|
|
let generics = self.parse_generics();
|
2013-02-24 18:20:24 +00:00
|
|
|
self.expect(&token::LBRACE);
|
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 {
|
2012-08-04 02:59:04 +00:00
|
|
|
token::LPAREN | token::AT | token::TILDE | token::BINOP(_) => true,
|
|
|
|
_ => false
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-01-12 20:31:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 22:06:13 +00:00
|
|
|
// Parses a string as an ABI spec on an extern type or module. Consumes
|
|
|
|
// the `extern` keyword, if one is found.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_opt_abis(&mut self) -> Option<AbiSet> {
|
2013-10-29 22:06:13 +00:00
|
|
|
if !self.eat_keyword(keywords::Extern) {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2013-10-02 01:32:29 +00:00
|
|
|
token::LIT_STR(s)
|
|
|
|
| token::LIT_STR_RAW(s, _) => {
|
2013-03-14 02:25:28 +00:00
|
|
|
self.bump();
|
2013-06-04 06:00:49 +00:00
|
|
|
let the_string = ident_to_str(&s);
|
2013-03-14 02:25:28 +00:00
|
|
|
let mut abis = AbiSet::empty();
|
2013-11-23 10:18:51 +00:00
|
|
|
for word in the_string.words() {
|
2013-06-09 13:10:50 +00:00
|
|
|
match abi::lookup(word) {
|
2013-03-14 02:25:28 +00:00
|
|
|
Some(abi) => {
|
|
|
|
if abis.contains(abi) {
|
|
|
|
self.span_err(
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("ABI `{}` appears twice",
|
2013-06-09 13:10:50 +00:00
|
|
|
word));
|
2013-03-14 02:25:28 +00:00
|
|
|
} else {
|
|
|
|
abis.add(abi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
|
|
|
self.span_err(
|
2013-12-30 23:17:53 +00:00
|
|
|
self.span,
|
2013-09-28 04:01:58 +00:00
|
|
|
format!("illegal ABI: \
|
|
|
|
expected one of [{}], \
|
|
|
|
found `{}`",
|
2013-06-10 13:25:25 +00:00
|
|
|
abi::all_names().connect(", "),
|
2013-06-09 13:10:50 +00:00
|
|
|
word));
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
2013-10-08 00:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
Some(abis)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
None
|
2013-10-08 00:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2013-02-11 21:36:24 +00:00
|
|
|
// parse one of the items or view items allowed by the
|
2014-01-09 13:05:33 +00:00
|
|
|
// flags; on failure, return IoviNone.
|
2013-04-01 22:50:58 +00:00
|
|
|
// NB: this function no longer parses the items inside an
|
|
|
|
// extern mod.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_item_or_view_item(&mut self,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: ~[Attribute],
|
2013-07-02 19:47:32 +00:00
|
|
|
macros_allowed: bool)
|
2014-01-09 13:05:33 +00:00
|
|
|
-> ItemOrViewItem {
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(token::NtItem(item)) => {
|
2013-08-08 17:28:06 +00:00
|
|
|
self.bump();
|
|
|
|
let new_attrs = vec::append(attrs, item.attrs);
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(@Item {
|
|
|
|
attrs: new_attrs,
|
|
|
|
..(*item).clone()
|
|
|
|
});
|
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();
|
|
|
|
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:
|
2013-10-29 22:06:13 +00:00
|
|
|
if self.is_keyword(keywords::Extern) {
|
2013-04-01 22:50:58 +00:00
|
|
|
let opt_abis = self.parse_opt_abis();
|
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
|
2013-09-10 02:57:08 +00:00
|
|
|
let abis = opt_abis.unwrap_or(AbiSet::C());
|
2013-04-01 22:50:58 +00:00
|
|
|
let (ident, item_, extra_attrs) =
|
2014-01-09 13:05:33 +00:00
|
|
|
self.parse_item_fn(ExternFn, abis);
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
ident,
|
|
|
|
item_,
|
|
|
|
visibility,
|
|
|
|
maybe_append(attrs, extra_attrs));
|
2014-01-09 13:05:33 +00:00
|
|
|
return IoviItem(item);
|
2013-04-01 22:50:58 +00:00
|
|
|
} else {
|
2014-01-09 13:05:33 +00:00
|
|
|
// EXTERN MODULE ITEM (IoviViewItem)
|
2013-04-01 22:50:58 +00:00
|
|
|
return self.parse_item_foreign_mod(lo, opt_abis, visibility, attrs,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the rest are all guaranteed to be items:
|
2013-10-03 09:53:46 +00:00
|
|
|
if self.is_keyword(keywords::Static) {
|
|
|
|
// STATIC ITEM
|
2013-03-19 21:46:27 +00:00
|
|
|
self.bump();
|
2012-08-14 00:11:52 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_const();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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.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-01-09 13:05:33 +00:00
|
|
|
self.parse_item_fn(ImpureFn, AbiSet::Rust());
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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.is_keyword(keywords::Unsafe)
|
2013-07-02 19:47:32 +00:00
|
|
|
&& self.look_ahead(1u, |t| *t != token::LBRACE) {
|
2013-04-01 22:50:58 +00:00
|
|
|
// UNSAFE FUNCTION ITEM
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
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-01-09 13:05:33 +00:00
|
|
|
self.parse_item_fn(UnsafeFn, AbiSet::Rust());
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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
|
2013-07-02 19:47:32 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_mod(attrs);
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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
|
2012-08-14 00:11:52 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_trait();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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
|
2013-08-07 07:11:34 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_impl();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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
|
2012-12-10 21:47:54 +00:00
|
|
|
let (ident, item_, extra_attrs) = self.parse_item_struct();
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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-01-09 13:05:33 +00:00
|
|
|
// parse a foreign item; on failure, return IoviNone.
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_foreign_item(&mut self,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: ~[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
|
|
|
|
2013-10-03 09:53:46 +00:00
|
|
|
if self.is_keyword(keywords::Static) {
|
|
|
|
// 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
|
|
|
}
|
2013-10-03 09:53:46 +00:00
|
|
|
if self.is_keyword(keywords::Fn) || self.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is the fall-through for parsing items.
|
|
|
|
fn parse_macro_use_or_failure(
|
2013-12-30 22:04:00 +00:00
|
|
|
&mut self,
|
2013-07-19 11:51:37 +00:00
|
|
|
attrs: ~[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 {
|
2013-12-30 23:09:41 +00:00
|
|
|
if macros_allowed && !token::is_any_keyword(&self.token)
|
2013-07-02 19:47:32 +00:00
|
|
|
&& self.look_ahead(1, |t| *t == token::NOT)
|
|
|
|
&& (self.look_ahead(2, |t| is_plain_ident(t))
|
|
|
|
|| self.look_ahead(2, |t| *t == token::LPAREN)
|
|
|
|
|| self.look_ahead(2, |t| *t == token::LBRACE)) {
|
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.
|
2013-08-07 16:47:28 +00:00
|
|
|
let pth = self.parse_path(NoTypesAllowed).path;
|
2013-02-24 18:20:24 +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.
|
2013-12-30 23:09:41 +00:00
|
|
|
let id = if is_plain_ident(&self.token) {
|
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:
|
2013-12-30 23:09:41 +00:00
|
|
|
let tts = match self.token {
|
2013-02-24 23:41:54 +00:00
|
|
|
token::LPAREN | token::LBRACE => {
|
2013-12-30 23:09:41 +00:00
|
|
|
let ket = token::flip_delimiter(&self.token);
|
2013-07-02 19:47:32 +00:00
|
|
|
self.bump();
|
|
|
|
self.parse_seq_to_end(&ket,
|
|
|
|
seq_sep_none(),
|
|
|
|
|p| p.parse_token_tree())
|
2013-02-24 23:41:54 +00:00
|
|
|
}
|
2013-05-19 05:07:44 +00:00
|
|
|
_ => self.fatal("expected open delimiter")
|
2012-11-07 23:16:58 +00:00
|
|
|
};
|
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);
|
2013-12-30 22:04:00 +00:00
|
|
|
let item = self.mk_item(lo,
|
|
|
|
self.last_span.hi,
|
|
|
|
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-01-09 13:05:33 +00:00
|
|
|
if visibility != Inherited {
|
2013-03-20 01:00:18 +00:00
|
|
|
let mut s = ~"unmatched visibility `";
|
2014-01-09 13:05:33 +00:00
|
|
|
if visibility == Public {
|
2013-06-12 02:13:42 +00:00
|
|
|
s.push_str("pub")
|
|
|
|
} else {
|
|
|
|
s.push_str("priv")
|
|
|
|
}
|
|
|
|
s.push_char('`');
|
2013-12-30 23:30:14 +00:00
|
|
|
self.span_fatal(self.last_span, s);
|
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-01-09 13:05:33 +00:00
|
|
|
pub fn parse_item(&mut self, attrs: ~[Attribute]) -> Option<@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
|
|
|
}
|
|
|
|
|
2013-04-02 23:44:01 +00:00
|
|
|
// parse, e.g., "use a::b::{z,y}"
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_use(&mut self) -> ViewItem_ {
|
|
|
|
return ViewItemUse(self.parse_view_paths());
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:35:23 +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-01-09 13:05:33 +00:00
|
|
|
fn parse_view_path(&mut self) -> @ViewPath {
|
2012-05-23 22:06:11 +00:00
|
|
|
let lo = self.span.lo;
|
2012-08-31 18:19:07 +00:00
|
|
|
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::LBRACE {
|
2013-12-04 21:08:42 +00:00
|
|
|
// use {foo,bar}
|
|
|
|
let idents = self.parse_unspanned_seq(
|
|
|
|
&token::LBRACE, &token::RBRACE,
|
|
|
|
seq_sep_trailing_allowed(token::COMMA),
|
|
|
|
|p| p.parse_path_list_ident());
|
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
|
|
|
segments: ~[]
|
|
|
|
};
|
|
|
|
return @spanned(lo, self.span.hi,
|
2014-01-09 13:05:33 +00:00
|
|
|
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();
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut path = ~[first_ident];
|
2014-01-09 13:05:33 +00:00
|
|
|
debug!("parsed view path: {}", self.id_to_str(first_ident));
|
2013-12-30 23:09:41 +00:00
|
|
|
match self.token {
|
2012-08-04 02:59:04 +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;
|
2012-06-29 23:26:56 +00:00
|
|
|
path = ~[self.parse_ident()];
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::MOD_SEP {
|
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
|
|
|
}
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
2014-01-09 07:12:23 +00:00
|
|
|
span: mk_sp(path_lo, self.span.hi),
|
2013-08-07 16:47:28 +00:00
|
|
|
global: false,
|
2013-08-08 18:38:10 +00:00
|
|
|
segments: path.move_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: opt_vec::Empty,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: opt_vec::Empty,
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2012-08-02 00:30:05 +00:00
|
|
|
return @spanned(lo, self.span.hi,
|
2014-01-09 13:05:33 +00:00
|
|
|
ViewPathSimple(first_ident, 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
|
|
|
token::MOD_SEP => {
|
2012-05-23 22:06:11 +00:00
|
|
|
// foo::bar or foo::{a,b,c} or foo::*
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::MOD_SEP {
|
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 {
|
2012-08-04 02:59:04 +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}
|
2012-08-04 02:59:04 +00:00
|
|
|
token::LBRACE => {
|
2012-06-12 17:17:36 +00:00
|
|
|
let idents = self.parse_unspanned_seq(
|
2013-02-25 02:55:24 +00:00
|
|
|
&token::LBRACE,
|
|
|
|
&token::RBRACE,
|
2012-06-13 18:08:21 +00:00
|
|
|
seq_sep_trailing_allowed(token::COMMA),
|
2013-02-24 23:41:54 +00:00
|
|
|
|p| p.parse_path_list_ident()
|
|
|
|
);
|
2013-08-07 16:47:28 +00:00
|
|
|
let path = ast::Path {
|
|
|
|
span: mk_sp(lo, self.span.hi),
|
|
|
|
global: false,
|
2013-08-08 18:38:10 +00:00
|
|
|
segments: path.move_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: opt_vec::Empty,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: opt_vec::Empty,
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2012-08-02 00:30:05 +00:00
|
|
|
return @spanned(lo, self.span.hi,
|
2014-01-09 13:05:33 +00:00
|
|
|
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::*
|
2012-08-04 02:59:04 +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,
|
2013-08-08 18:38:10 +00:00
|
|
|
segments: path.move_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: opt_vec::Empty,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: opt_vec::Empty,
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2012-08-02 00:30:05 +00:00
|
|
|
return @spanned(lo, self.span.hi,
|
2014-01-09 13:05:33 +00:00
|
|
|
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
|
|
|
}
|
2013-05-14 09:52:12 +00:00
|
|
|
let last = 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,
|
2013-08-08 18:38:10 +00:00
|
|
|
segments: path.move_iter().map(|identifier| {
|
2013-08-07 16:47:28 +00:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: identifier,
|
2013-10-29 10:03:32 +00:00
|
|
|
lifetimes: opt_vec::Empty,
|
2013-08-07 16:47:28 +00:00
|
|
|
types: opt_vec::Empty,
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
};
|
2013-04-22 19:32:59 +00:00
|
|
|
return @spanned(lo,
|
2013-05-02 17:36:24 +00:00
|
|
|
self.last_span.hi,
|
2014-01-09 13:05:33 +00:00
|
|
|
ViewPathSimple(last, path, ast::DUMMY_NODE_ID));
|
2011-01-28 16:54:59 +00:00
|
|
|
}
|
2010-12-25 04:25:02 +00:00
|
|
|
|
2013-03-29 17:35:23 +00:00
|
|
|
// matches view_paths = view_path | view_path , view_paths
|
2014-01-09 13:05:33 +00:00
|
|
|
fn parse_view_paths(&mut self) -> ~[@ViewPath] {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut vp = ~[self.parse_view_path()];
|
2013-12-30 23:09:41 +00:00
|
|
|
while self.token == token::COMMA {
|
2012-05-23 22:06:11 +00:00
|
|
|
self.bump();
|
2013-12-30 23:30:14 +00:00
|
|
|
self.obsolete(self.last_span, ObsoleteMultipleImport);
|
2012-09-27 00:33:34 +00:00
|
|
|
vp.push(self.parse_view_path());
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-08-02 00:30:05 +00:00
|
|
|
return vp;
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-02-11 21:36:24 +00:00
|
|
|
// Parses a sequence of items. Stops when it finds program
|
|
|
|
// text that can't be parsed as an item
|
2013-04-01 22:50:58 +00:00
|
|
|
// - 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,
|
2013-07-19 11:51:37 +00:00
|
|
|
first_item_attrs: ~[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 {
|
2012-06-28 06:09:51 +00:00
|
|
|
let mut attrs = vec::append(first_item_attrs,
|
|
|
|
self.parse_outer_attributes());
|
2013-03-27 02:53:33 +00:00
|
|
|
// First, parse view items.
|
2014-01-09 13:05:33 +00:00
|
|
|
let mut view_items : ~[ast::ViewItem] = ~[];
|
2013-06-07 01:54:14 +00:00
|
|
|
let mut items = ~[];
|
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
|
|
|
|
// loop with a mutable three-state-variable (for extern mods,
|
|
|
|
// 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,
|
|
|
|
foreign_items: ~[]
|
|
|
|
}
|
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(..) => {
|
2013-04-01 22:50:58 +00:00
|
|
|
// `extern mod` must precede `use`.
|
|
|
|
extern_mod_allowed = false;
|
2013-03-27 02:53:33 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ViewItemExternMod(..) if !extern_mod_allowed => {
|
2013-04-01 22:50:58 +00:00
|
|
|
self.span_err(view_item.span,
|
2013-05-02 16:28:53 +00:00
|
|
|
"\"extern mod\" declarations are not allowed here");
|
2013-04-01 22:50:58 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ViewItemExternMod(..) => {}
|
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(_) => {
|
2013-10-21 20:08:31 +00:00
|
|
|
fail!();
|
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,
|
|
|
|
"`use` and `extern mod` declarations must precede items");
|
|
|
|
}
|
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(_) => {
|
2013-10-21 20:08:31 +00:00
|
|
|
fail!();
|
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,
|
2013-04-01 22:50:58 +00:00
|
|
|
foreign_items: ~[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses a sequence of foreign items. Stops when it finds program
|
|
|
|
// text that can't be parsed as an item
|
2013-12-30 22:04:00 +00:00
|
|
|
fn parse_foreign_items(&mut self, first_item_attrs: ~[Attribute],
|
2013-04-01 22:50:58 +00:00
|
|
|
macros_allowed: bool)
|
|
|
|
-> ParsedItemsAndViewItems {
|
|
|
|
let mut attrs = vec::append(first_item_attrs,
|
|
|
|
self.parse_outer_attributes());
|
|
|
|
let mut foreign_items = ~[];
|
|
|
|
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) => {
|
2013-12-30 23:09:41 +00:00
|
|
|
if self.token == token::RBRACE {
|
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,
|
2013-05-02 16:28:53 +00:00
|
|
|
"`use` and `extern mod` 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,
|
|
|
|
view_items: ~[],
|
|
|
|
items: ~[],
|
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
|
|
|
|
2013-04-02 23:44:01 +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:
|
2012-05-23 22:06:11 +00:00
|
|
|
let m = self.parse_mod_items(token::EOF, first_item_outer_attrs);
|
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(),
|
|
|
|
span: mk_sp(lo, self.span.lo)
|
|
|
|
}
|
2011-06-23 22:42:55 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_optional_str(&mut self) -> Option<(@str, ast::StrStyle)> {
|
2013-12-30 23:09:41 +00:00
|
|
|
let (s, style) = match self.token {
|
2013-10-08 00:49:10 +00:00
|
|
|
token::LIT_STR(s) => (s, ast::CookedStr),
|
|
|
|
token::LIT_STR_RAW(s, n) => (s, ast::RawStr(n)),
|
|
|
|
_ => return None
|
|
|
|
};
|
|
|
|
self.bump();
|
|
|
|
Some((ident_to_str(&s), style))
|
2013-07-12 21:43:57 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:04:00 +00:00
|
|
|
pub fn parse_str(&mut self) -> (@str, StrStyle) {
|
2013-07-12 21:43:57 +00:00
|
|
|
match self.parse_optional_str() {
|
|
|
|
Some(s) => { s }
|
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
|
|
|
}
|