2018-08-04 16:58:22 +03:00
|
|
|
mod atom;
|
|
|
|
|
2018-09-10 20:14:09 +02:00
|
|
|
pub(crate) use self::atom::match_arm_list;
|
2018-10-15 17:44:23 -04:00
|
|
|
pub(super) use self::atom::{literal, LITERAL_FIRST};
|
|
|
|
use super::*;
|
2018-08-04 16:58:22 +03:00
|
|
|
|
2018-08-07 14:24:03 +03:00
|
|
|
const EXPR_FIRST: TokenSet = LHS_FIRST;
|
2018-08-04 16:58:22 +03:00
|
|
|
|
2018-08-07 16:32:09 +03:00
|
|
|
pub(super) fn expr(p: &mut Parser) -> BlockLike {
|
2018-10-15 17:44:23 -04:00
|
|
|
let r = Restrictions {
|
|
|
|
forbid_structs: false,
|
|
|
|
prefer_stmt: false,
|
|
|
|
};
|
2018-08-07 17:00:45 +03:00
|
|
|
expr_bp(p, r, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn expr_stmt(p: &mut Parser) -> BlockLike {
|
2018-10-15 17:44:23 -04:00
|
|
|
let r = Restrictions {
|
|
|
|
forbid_structs: false,
|
|
|
|
prefer_stmt: true,
|
|
|
|
};
|
2018-08-04 17:12:00 +03:00
|
|
|
expr_bp(p, r, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_no_struct(p: &mut Parser) {
|
2018-10-15 17:44:23 -04:00
|
|
|
let r = Restrictions {
|
|
|
|
forbid_structs: true,
|
|
|
|
prefer_stmt: false,
|
|
|
|
};
|
2018-08-07 16:32:09 +03:00
|
|
|
expr_bp(p, r, 1);
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test block
|
|
|
|
// fn a() {}
|
|
|
|
// fn b() { let _ = 1; }
|
|
|
|
// fn c() { 1; 2; }
|
|
|
|
// fn d() { 1; 2 }
|
2018-08-25 13:17:54 +03:00
|
|
|
pub(crate) fn block(p: &mut Parser) {
|
2018-08-25 13:21:43 +03:00
|
|
|
if !p.at(L_CURLY) {
|
|
|
|
p.error("expected a block");
|
|
|
|
return;
|
|
|
|
}
|
2018-08-24 19:27:30 +03:00
|
|
|
let m = p.start();
|
|
|
|
p.bump();
|
|
|
|
while !p.at(EOF) && !p.at(R_CURLY) {
|
|
|
|
match p.current() {
|
|
|
|
LET_KW => let_stmt(p),
|
|
|
|
_ => {
|
|
|
|
// test block_items
|
|
|
|
// fn a() { fn b() {} }
|
|
|
|
let m = p.start();
|
|
|
|
match items::maybe_item(p, items::ItemFlavor::Mod) {
|
|
|
|
items::MaybeItem::Item(kind) => {
|
|
|
|
m.complete(p, kind);
|
|
|
|
}
|
|
|
|
items::MaybeItem::Modifiers => {
|
|
|
|
m.abandon(p);
|
|
|
|
p.error("expected an item");
|
|
|
|
}
|
|
|
|
// test pub_expr
|
|
|
|
// fn foo() { pub 92; } //FIXME
|
|
|
|
items::MaybeItem::None => {
|
|
|
|
let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block;
|
2018-08-25 16:04:47 +03:00
|
|
|
if p.at(R_CURLY) {
|
2018-08-24 19:27:30 +03:00
|
|
|
m.abandon(p);
|
2018-08-25 16:04:47 +03:00
|
|
|
} else {
|
|
|
|
if is_blocklike {
|
|
|
|
p.eat(SEMI);
|
|
|
|
} else {
|
|
|
|
p.expect(SEMI);
|
|
|
|
}
|
|
|
|
m.complete(p, EXPR_STMT);
|
2018-08-24 19:27:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.expect(R_CURLY);
|
|
|
|
m.complete(p, BLOCK);
|
|
|
|
|
|
|
|
// test let_stmt;
|
|
|
|
// fn foo() {
|
|
|
|
// let a;
|
|
|
|
// let b: i32;
|
|
|
|
// let c = 92;
|
|
|
|
// let d: i32 = 92;
|
|
|
|
// }
|
|
|
|
fn let_stmt(p: &mut Parser) {
|
|
|
|
assert!(p.at(LET_KW));
|
|
|
|
let m = p.start();
|
|
|
|
p.bump();
|
|
|
|
patterns::pattern(p);
|
|
|
|
if p.at(COLON) {
|
|
|
|
types::ascription(p);
|
|
|
|
}
|
|
|
|
if p.eat(EQ) {
|
|
|
|
expressions::expr(p);
|
|
|
|
}
|
|
|
|
p.expect(SEMI);
|
|
|
|
m.complete(p, LET_STMT);
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:12:00 +03:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Restrictions {
|
2018-08-07 17:00:45 +03:00
|
|
|
forbid_structs: bool,
|
|
|
|
prefer_stmt: bool,
|
2018-08-04 17:12:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:09:25 +03:00
|
|
|
enum Op {
|
|
|
|
Simple,
|
2018-08-05 16:15:40 +03:00
|
|
|
Composite(SyntaxKind, u8),
|
2018-08-05 16:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn current_op(p: &Parser) -> (u8, Op) {
|
2018-08-24 00:48:10 +03:00
|
|
|
if let Some(t) = p.next3() {
|
|
|
|
match t {
|
2018-10-15 17:44:23 -04:00
|
|
|
(L_ANGLE, L_ANGLE, EQ) => return (1, Op::Composite(SHLEQ, 3)),
|
|
|
|
(R_ANGLE, R_ANGLE, EQ) => return (1, Op::Composite(SHREQ, 3)),
|
2018-08-24 00:48:10 +03:00
|
|
|
_ => (),
|
|
|
|
}
|
2018-08-05 16:09:25 +03:00
|
|
|
}
|
2018-08-24 00:48:10 +03:00
|
|
|
|
|
|
|
if let Some(t) = p.next2() {
|
|
|
|
match t {
|
|
|
|
(PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)),
|
|
|
|
(MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)),
|
|
|
|
(STAR, EQ) => return (1, Op::Composite(STAREQ, 2)),
|
|
|
|
(SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)),
|
|
|
|
(PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)),
|
|
|
|
(AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)),
|
|
|
|
(CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)),
|
|
|
|
(PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)),
|
|
|
|
(AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)),
|
|
|
|
(L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)),
|
|
|
|
(R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)),
|
|
|
|
(L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)),
|
|
|
|
(R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)),
|
|
|
|
_ => (),
|
|
|
|
}
|
2018-08-07 14:24:03 +03:00
|
|
|
}
|
2018-08-05 16:09:25 +03:00
|
|
|
|
|
|
|
let bp = match p.current() {
|
2018-08-04 17:17:20 +03:00
|
|
|
EQ => 1,
|
2018-12-17 22:34:18 +00:00
|
|
|
DOTDOT | DOTDOTEQ => 2,
|
2018-08-07 14:28:07 +03:00
|
|
|
EQEQ | NEQ | L_ANGLE | R_ANGLE => 5,
|
2018-08-07 14:24:03 +03:00
|
|
|
PIPE => 6,
|
|
|
|
CARET => 7,
|
|
|
|
AMP => 8,
|
|
|
|
MINUS | PLUS => 10,
|
|
|
|
STAR | SLASH | PERCENT => 11,
|
2018-08-05 16:09:25 +03:00
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
(bp, Op::Simple)
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses expression with binding power of at least bp.
|
2018-08-07 16:32:09 +03:00
|
|
|
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike {
|
2018-08-05 18:07:06 +03:00
|
|
|
let mut lhs = match lhs(p, r) {
|
2018-12-19 20:02:37 +00:00
|
|
|
(Some(lhs), macro_blocklike) => {
|
2018-08-07 17:00:45 +03:00
|
|
|
// test stmt_bin_expr_ambiguity
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = {1} & 2;
|
|
|
|
// {1} &2;
|
|
|
|
// }
|
2018-12-19 20:02:37 +00:00
|
|
|
if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block))
|
|
|
|
{
|
2018-08-07 17:00:45 +03:00
|
|
|
return BlockLike::Block;
|
|
|
|
}
|
2018-08-07 16:32:09 +03:00
|
|
|
lhs
|
2018-08-07 17:00:45 +03:00
|
|
|
}
|
2018-12-19 20:02:37 +00:00
|
|
|
(None, _) => return BlockLike::NotBlock,
|
2018-08-04 16:58:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
2018-12-17 22:34:18 +00:00
|
|
|
let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ;
|
2018-08-05 16:09:25 +03:00
|
|
|
let (op_bp, op) = current_op(p);
|
2018-08-04 16:58:22 +03:00
|
|
|
if op_bp < bp {
|
|
|
|
break;
|
|
|
|
}
|
2018-08-07 15:21:15 +03:00
|
|
|
let m = lhs.precede(p);
|
2018-08-05 16:09:25 +03:00
|
|
|
match op {
|
|
|
|
Op::Simple => p.bump(),
|
|
|
|
Op::Composite(kind, n) => {
|
|
|
|
p.bump_compound(kind, n);
|
2018-08-05 16:15:40 +03:00
|
|
|
}
|
2018-08-05 16:09:25 +03:00
|
|
|
}
|
2018-08-07 15:21:15 +03:00
|
|
|
expr_bp(p, r, op_bp + 1);
|
|
|
|
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
2018-08-07 17:00:45 +03:00
|
|
|
BlockLike::NotBlock
|
2018-08-07 16:32:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test no_semi_after_block
|
|
|
|
// fn foo() {
|
|
|
|
// if true {}
|
|
|
|
// loop {}
|
|
|
|
// match () {}
|
|
|
|
// while true {}
|
|
|
|
// for _ in () {}
|
|
|
|
// {}
|
|
|
|
// {}
|
|
|
|
// }
|
|
|
|
fn is_block(kind: SyntaxKind) -> bool {
|
|
|
|
match kind {
|
|
|
|
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
|
2018-10-15 17:44:23 -04:00
|
|
|
const LHS_FIRST: TokenSet = token_set_union![
|
|
|
|
token_set![AMP, STAR, EXCL, DOTDOT, MINUS],
|
|
|
|
atom::ATOM_EXPR_FIRST,
|
|
|
|
];
|
2018-08-04 16:58:22 +03:00
|
|
|
|
2018-12-19 20:02:37 +00:00
|
|
|
fn lhs(p: &mut Parser, r: Restrictions) -> (Option<CompletedMarker>, Option<BlockLike>) {
|
2018-08-04 17:12:00 +03:00
|
|
|
let m;
|
|
|
|
let kind = match p.current() {
|
|
|
|
// test ref_expr
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = &1;
|
|
|
|
// let _ = &mut &f();
|
|
|
|
// }
|
2018-08-05 18:18:02 +03:00
|
|
|
AMP => {
|
2018-08-04 17:12:00 +03:00
|
|
|
m = p.start();
|
|
|
|
p.bump();
|
|
|
|
p.eat(MUT_KW);
|
|
|
|
REF_EXPR
|
2018-08-05 16:15:40 +03:00
|
|
|
}
|
2018-08-07 14:56:33 +03:00
|
|
|
// test unary_expr
|
2018-08-04 17:12:00 +03:00
|
|
|
// fn foo() {
|
|
|
|
// **&1;
|
|
|
|
// !!true;
|
2018-08-07 14:24:03 +03:00
|
|
|
// --1;
|
|
|
|
// }
|
2018-08-07 14:52:03 +03:00
|
|
|
STAR | EXCL | MINUS => {
|
2018-08-07 14:24:03 +03:00
|
|
|
m = p.start();
|
|
|
|
p.bump();
|
2018-08-07 14:52:03 +03:00
|
|
|
PREFIX_EXPR
|
2018-08-07 14:24:03 +03:00
|
|
|
}
|
2018-08-14 11:46:46 +03:00
|
|
|
// test full_range_expr
|
|
|
|
// fn foo() { xs[..]; }
|
2018-08-05 18:07:06 +03:00
|
|
|
DOTDOT => {
|
|
|
|
m = p.start();
|
|
|
|
p.bump();
|
2018-09-08 10:38:53 +03:00
|
|
|
if p.at_ts(EXPR_FIRST) {
|
2018-08-14 11:46:46 +03:00
|
|
|
expr_bp(p, r, 2);
|
|
|
|
}
|
2018-12-19 20:02:37 +00:00
|
|
|
return (Some(m.complete(p, RANGE_EXPR)), None);
|
2018-08-05 18:07:06 +03:00
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
_ => {
|
2018-12-19 20:02:37 +00:00
|
|
|
let (lhs_marker, macro_block_like) = atom::atom_expr(p, r);
|
|
|
|
|
|
|
|
if macro_block_like == Some(BlockLike::Block) {
|
|
|
|
return (lhs_marker, macro_block_like);
|
|
|
|
}
|
|
|
|
if let Some(lhs_marker) = lhs_marker {
|
|
|
|
return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like);
|
|
|
|
} else {
|
|
|
|
return (None, None);
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
};
|
2018-08-05 18:07:06 +03:00
|
|
|
expr_bp(p, r, 255);
|
2018-12-19 20:02:37 +00:00
|
|
|
(Some(m.complete(p, kind)), None)
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 18:12:19 +00:00
|
|
|
fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker {
|
2018-12-19 20:02:37 +00:00
|
|
|
// Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple
|
|
|
|
// E.g. `while true {break}();` is parsed as
|
|
|
|
// `while true {break}; ();`
|
2018-12-19 18:12:19 +00:00
|
|
|
let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind());
|
2018-08-04 16:58:22 +03:00
|
|
|
loop {
|
|
|
|
lhs = match p.current() {
|
2018-08-07 17:00:45 +03:00
|
|
|
// test stmt_postfix_expr_ambiguity
|
|
|
|
// fn foo() {
|
|
|
|
// match () {
|
|
|
|
// _ => {}
|
|
|
|
// () => {}
|
|
|
|
// [] => {}
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
L_PAREN if allow_calls => call_expr(p, lhs),
|
|
|
|
L_BRACK if allow_calls => index_expr(p, lhs),
|
2018-10-15 17:44:23 -04:00
|
|
|
DOT if p.nth(1) == IDENT => {
|
|
|
|
if p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON {
|
|
|
|
method_call_expr(p, lhs)
|
|
|
|
} else {
|
|
|
|
field_expr(p, lhs)
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
DOT if p.nth(1) == INT_NUMBER => field_expr(p, lhs),
|
2018-08-05 18:07:06 +03:00
|
|
|
// test postfix_range
|
|
|
|
// fn foo() { let x = 1..; }
|
|
|
|
DOTDOT if !EXPR_FIRST.contains(p.nth(1)) => {
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
|
|
|
m.complete(p, RANGE_EXPR)
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
QUESTION => try_expr(p, lhs),
|
2018-08-07 03:55:16 +03:00
|
|
|
AS_KW => cast_expr(p, lhs),
|
2018-08-04 16:58:22 +03:00
|
|
|
_ => break,
|
2018-08-07 17:00:45 +03:00
|
|
|
};
|
|
|
|
allow_calls = true
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
lhs
|
|
|
|
}
|
|
|
|
|
|
|
|
// test call_expr
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = f();
|
|
|
|
// let _ = f()(1)(1, 2,);
|
|
|
|
// }
|
|
|
|
fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
|
|
|
assert!(p.at(L_PAREN));
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
arg_list(p);
|
|
|
|
m.complete(p, CALL_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-05 17:24:44 +03:00
|
|
|
// test index_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x[1][2];
|
|
|
|
// }
|
|
|
|
fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
|
|
|
assert!(p.at(L_BRACK));
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
|
|
|
expr(p);
|
|
|
|
p.expect(R_BRACK);
|
|
|
|
m.complete(p, INDEX_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-04 16:58:22 +03:00
|
|
|
// test method_call_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.foo();
|
2018-08-05 16:15:40 +03:00
|
|
|
// y.bar::<T>(1, 2,);
|
2018-08-04 16:58:22 +03:00
|
|
|
// }
|
|
|
|
fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2018-10-15 17:44:23 -04:00
|
|
|
assert!(p.at(DOT) && p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON));
|
2018-08-04 16:58:22 +03:00
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
|
|
|
name_ref(p);
|
2018-08-24 02:14:10 +03:00
|
|
|
type_args::opt_type_arg_list(p, true);
|
2018-08-13 18:46:43 +03:00
|
|
|
if p.at(L_PAREN) {
|
|
|
|
arg_list(p);
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
m.complete(p, METHOD_CALL_EXPR)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test field_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.foo;
|
|
|
|
// x.0.bar;
|
|
|
|
// }
|
|
|
|
fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
|
|
|
assert!(p.at(DOT) && (p.nth(1) == IDENT || p.nth(1) == INT_NUMBER));
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
|
|
|
if p.at(IDENT) {
|
|
|
|
name_ref(p)
|
|
|
|
} else {
|
|
|
|
p.bump()
|
|
|
|
}
|
|
|
|
m.complete(p, FIELD_EXPR)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test try_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x?;
|
|
|
|
// }
|
|
|
|
fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
|
|
|
assert!(p.at(QUESTION));
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
|
|
|
m.complete(p, TRY_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:55:16 +03:00
|
|
|
// test cast_expr
|
|
|
|
// fn foo() {
|
|
|
|
// 82 as i32;
|
2018-12-17 17:10:05 +01:00
|
|
|
// 81 as i8 + 1;
|
|
|
|
// 79 as i16 - 1;
|
2018-08-07 03:55:16 +03:00
|
|
|
// }
|
|
|
|
fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
|
|
|
assert!(p.at(AS_KW));
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump();
|
2018-12-17 17:10:05 +01:00
|
|
|
// Use type_no_bounds(), because cast expressions are not
|
|
|
|
// allowed to have bounds.
|
|
|
|
types::type_no_bounds(p);
|
2018-08-07 03:55:16 +03:00
|
|
|
m.complete(p, CAST_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-04 16:58:22 +03:00
|
|
|
fn arg_list(p: &mut Parser) {
|
|
|
|
assert!(p.at(L_PAREN));
|
|
|
|
let m = p.start();
|
|
|
|
p.bump();
|
|
|
|
while !p.at(R_PAREN) && !p.at(EOF) {
|
2018-09-08 10:38:53 +03:00
|
|
|
if !p.at_ts(EXPR_FIRST) {
|
2018-09-08 10:13:32 +03:00
|
|
|
p.error("expected expression");
|
|
|
|
break;
|
|
|
|
}
|
2018-08-04 16:58:22 +03:00
|
|
|
expr(p);
|
|
|
|
if !p.at(R_PAREN) && !p.expect(COMMA) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.eat(R_PAREN);
|
|
|
|
m.complete(p, ARG_LIST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test path_expr
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = a;
|
|
|
|
// let _ = a::b;
|
|
|
|
// let _ = ::a::<b>;
|
2018-08-05 14:16:38 +03:00
|
|
|
// let _ = format!();
|
2018-08-04 16:58:22 +03:00
|
|
|
// }
|
2018-12-19 20:02:37 +00:00
|
|
|
fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option<BlockLike>) {
|
2018-08-13 23:54:00 +03:00
|
|
|
assert!(paths::is_path_start(p) || p.at(L_ANGLE));
|
2018-08-04 16:58:22 +03:00
|
|
|
let m = p.start();
|
|
|
|
paths::expr_path(p);
|
2018-12-19 20:02:37 +00:00
|
|
|
let res = match p.current() {
|
2018-08-05 14:16:38 +03:00
|
|
|
L_CURLY if !r.forbid_structs => {
|
2018-08-24 19:27:30 +03:00
|
|
|
named_field_list(p);
|
2018-08-05 14:16:38 +03:00
|
|
|
m.complete(p, STRUCT_LIT)
|
|
|
|
}
|
|
|
|
EXCL => {
|
2018-12-19 20:02:37 +00:00
|
|
|
let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike)
|
|
|
|
return (m.complete(p, MACRO_CALL), Some(block_like));
|
2018-08-05 14:16:38 +03:00
|
|
|
}
|
2018-10-15 17:44:23 -04:00
|
|
|
_ => m.complete(p, PATH_EXPR),
|
2018-12-19 20:02:37 +00:00
|
|
|
};
|
|
|
|
(res, None)
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test struct_lit
|
|
|
|
// fn foo() {
|
|
|
|
// S {};
|
|
|
|
// S { x, y: 32, };
|
|
|
|
// S { x, y: 32, ..Default::default() };
|
|
|
|
// }
|
2018-09-10 20:14:09 +02:00
|
|
|
pub(crate) fn named_field_list(p: &mut Parser) {
|
2018-08-04 16:58:22 +03:00
|
|
|
assert!(p.at(L_CURLY));
|
2018-08-24 19:27:30 +03:00
|
|
|
let m = p.start();
|
2018-08-04 16:58:22 +03:00
|
|
|
p.bump();
|
|
|
|
while !p.at(EOF) && !p.at(R_CURLY) {
|
|
|
|
match p.current() {
|
|
|
|
IDENT => {
|
|
|
|
let m = p.start();
|
|
|
|
name_ref(p);
|
|
|
|
if p.eat(COLON) {
|
|
|
|
expr(p);
|
|
|
|
}
|
2018-08-24 19:27:30 +03:00
|
|
|
m.complete(p, NAMED_FIELD);
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|
|
|
|
DOTDOT => {
|
|
|
|
p.bump();
|
|
|
|
expr(p);
|
|
|
|
}
|
2018-08-27 21:10:02 +03:00
|
|
|
L_CURLY => error_block(p, "expected a field"),
|
2018-08-04 16:58:22 +03:00
|
|
|
_ => p.err_and_bump("expected identifier"),
|
|
|
|
}
|
|
|
|
if !p.at(R_CURLY) {
|
|
|
|
p.expect(COMMA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.expect(R_CURLY);
|
2018-08-24 19:27:30 +03:00
|
|
|
m.complete(p, NAMED_FIELD_LIST);
|
2018-08-04 16:58:22 +03:00
|
|
|
}
|