2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2018-08-04 08:58:22 -05:00
|
|
|
mod atom;
|
|
|
|
|
2020-05-02 07:34:39 -05:00
|
|
|
pub(crate) use self::atom::{block_expr, match_arm_list};
|
2018-10-15 16:44:23 -05:00
|
|
|
pub(super) use self::atom::{literal, LITERAL_FIRST};
|
|
|
|
use super::*;
|
2018-08-04 08:58:22 -05:00
|
|
|
|
2019-04-18 14:49:56 -05:00
|
|
|
pub(super) enum StmtWithSemi {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
Optional,
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:24:03 -05:00
|
|
|
const EXPR_FIRST: TokenSet = LHS_FIRST;
|
2018-08-04 08:58:22 -05:00
|
|
|
|
2020-01-16 09:37:43 -06:00
|
|
|
pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
|
2019-02-08 05:49:43 -06:00
|
|
|
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
|
2020-01-16 09:37:43 -06:00
|
|
|
expr_bp(p, r, 1)
|
2018-08-07 09:00:45 -05:00
|
|
|
}
|
|
|
|
|
2020-01-17 04:44:40 -06:00
|
|
|
pub(super) fn expr_with_attrs(p: &mut Parser) -> bool {
|
|
|
|
let m = p.start();
|
|
|
|
let has_attrs = p.at(T![#]);
|
2020-08-13 10:58:35 -05:00
|
|
|
attributes::outer_attrs(p);
|
2020-01-17 04:44:40 -06:00
|
|
|
|
|
|
|
let (cm, _block_like) = expr(p);
|
|
|
|
let success = cm.is_some();
|
|
|
|
|
|
|
|
match (has_attrs, cm) {
|
|
|
|
(true, Some(cm)) => {
|
|
|
|
let kind = cm.kind();
|
|
|
|
cm.undo_completion(p).abandon(p);
|
|
|
|
m.complete(p, kind);
|
|
|
|
}
|
|
|
|
_ => m.abandon(p),
|
|
|
|
}
|
|
|
|
|
|
|
|
success
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:24:02 -05:00
|
|
|
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
|
2019-02-08 05:49:43 -06:00
|
|
|
let r = Restrictions { forbid_structs: false, prefer_stmt: true };
|
2019-09-10 13:28:27 -05:00
|
|
|
expr_bp(p, r, 1)
|
2018-08-04 09:12:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_no_struct(p: &mut Parser) {
|
2019-02-08 05:49:43 -06:00
|
|
|
let r = Restrictions { forbid_structs: true, prefer_stmt: false };
|
2019-09-10 13:28:27 -05:00
|
|
|
expr_bp(p, r, 1);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:37:08 -05:00
|
|
|
fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool {
|
2020-06-15 03:59:05 -05:00
|
|
|
let forbid = matches!(kind, BIN_EXPR | RANGE_EXPR);
|
|
|
|
!forbid
|
2019-03-19 03:37:08 -05:00
|
|
|
}
|
|
|
|
|
2021-03-16 00:44:50 -05:00
|
|
|
pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) {
|
2019-04-16 23:34:43 -05:00
|
|
|
let m = p.start();
|
|
|
|
// test attr_on_expr_stmt
|
|
|
|
// fn foo() {
|
|
|
|
// #[A] foo();
|
|
|
|
// #[B] bar!{}
|
|
|
|
// #[C] #[D] {}
|
|
|
|
// #[D] return ();
|
|
|
|
// }
|
2019-05-15 07:35:47 -05:00
|
|
|
let has_attrs = p.at(T![#]);
|
2020-08-13 10:58:35 -05:00
|
|
|
attributes::outer_attrs(p);
|
2018-12-20 12:39:38 -06:00
|
|
|
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.at(T![let]) {
|
2019-04-16 23:34:43 -05:00
|
|
|
let_stmt(p, m, with_semi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:44:06 -05:00
|
|
|
// test block_items
|
|
|
|
// fn a() { fn b() {} }
|
2020-08-12 07:26:36 -05:00
|
|
|
let m = match items::maybe_item(p, m) {
|
2019-04-16 23:34:43 -05:00
|
|
|
Ok(()) => return,
|
|
|
|
Err(m) => m,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (cm, blocklike) = expr_stmt(p);
|
|
|
|
let kind = cm.as_ref().map(|cm| cm.kind()).unwrap_or(ERROR);
|
2019-03-17 05:14:17 -05:00
|
|
|
|
2019-04-16 23:34:43 -05:00
|
|
|
if has_attrs && !is_expr_stmt_attr_allowed(kind) {
|
|
|
|
// test_err attr_on_expr_not_allowed
|
2019-03-19 03:24:02 -05:00
|
|
|
// fn foo() {
|
2019-04-16 23:34:43 -05:00
|
|
|
// #[A] 1 + 2;
|
|
|
|
// #[B] if true {};
|
2019-03-19 03:24:02 -05:00
|
|
|
// }
|
2019-04-16 23:34:43 -05:00
|
|
|
p.error(format!("attributes are not allowed on {:?}", kind));
|
|
|
|
}
|
2019-03-19 03:24:02 -05:00
|
|
|
|
2021-03-16 00:44:50 -05:00
|
|
|
if p.at(T!['}']) || (prefer_expr && p.at(EOF)) {
|
2019-04-16 23:34:43 -05:00
|
|
|
// test attr_on_last_expr_in_block
|
|
|
|
// fn foo() {
|
|
|
|
// { #[A] bar!()? }
|
|
|
|
// #[B] &()
|
|
|
|
// }
|
|
|
|
if let Some(cm) = cm {
|
|
|
|
cm.undo_completion(p).abandon(p);
|
|
|
|
m.complete(p, kind);
|
2019-03-19 03:24:02 -05:00
|
|
|
} else {
|
2019-04-16 23:34:43 -05:00
|
|
|
m.abandon(p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// test no_semi_after_block
|
|
|
|
// fn foo() {
|
|
|
|
// if true {}
|
|
|
|
// loop {}
|
|
|
|
// match () {}
|
|
|
|
// while true {}
|
|
|
|
// for _ in () {}
|
|
|
|
// {}
|
|
|
|
// {}
|
|
|
|
// macro_rules! test {
|
|
|
|
// () => {}
|
|
|
|
// }
|
|
|
|
// test!{}
|
|
|
|
// }
|
2019-04-18 14:49:56 -05:00
|
|
|
|
|
|
|
match with_semi {
|
|
|
|
StmtWithSemi::Yes => {
|
|
|
|
if blocklike.is_block() {
|
2019-05-15 07:35:47 -05:00
|
|
|
p.eat(T![;]);
|
2019-04-18 14:49:56 -05:00
|
|
|
} else {
|
2019-05-15 07:35:47 -05:00
|
|
|
p.expect(T![;]);
|
2019-04-18 14:49:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
StmtWithSemi::No => {}
|
|
|
|
StmtWithSemi::Optional => {
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.at(T![;]) {
|
|
|
|
p.eat(T![;]);
|
2019-04-18 14:49:56 -05:00
|
|
|
}
|
2018-08-24 11:27:30 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 14:49:56 -05:00
|
|
|
|
2019-04-16 23:34:43 -05:00
|
|
|
m.complete(p, EXPR_STMT);
|
2018-08-24 11:27:30 -05:00
|
|
|
}
|
|
|
|
|
2019-06-07 06:49:49 -05:00
|
|
|
// test let_stmt
|
2018-08-24 11:27:30 -05:00
|
|
|
// fn foo() {
|
|
|
|
// let a;
|
|
|
|
// let b: i32;
|
|
|
|
// let c = 92;
|
|
|
|
// let d: i32 = 92;
|
2019-06-07 06:49:49 -05:00
|
|
|
// let e: !;
|
|
|
|
// let _: ! = {};
|
2020-02-27 23:08:47 -06:00
|
|
|
// let f = #[attr]||{};
|
2018-08-24 11:27:30 -05:00
|
|
|
// }
|
2019-04-18 14:49:56 -05:00
|
|
|
fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T![let]));
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![let]);
|
2018-08-24 11:27:30 -05:00
|
|
|
patterns::pattern(p);
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.at(T![:]) {
|
2018-08-24 11:27:30 -05:00
|
|
|
types::ascription(p);
|
|
|
|
}
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.eat(T![=]) {
|
2020-02-27 23:08:47 -06:00
|
|
|
expressions::expr_with_attrs(p);
|
2018-08-24 11:27:30 -05:00
|
|
|
}
|
2019-04-16 23:34:43 -05:00
|
|
|
|
2019-04-18 14:49:56 -05:00
|
|
|
match with_semi {
|
|
|
|
StmtWithSemi::Yes => {
|
2019-05-15 07:35:47 -05:00
|
|
|
p.expect(T![;]);
|
2019-04-18 14:49:56 -05:00
|
|
|
}
|
|
|
|
StmtWithSemi::No => {}
|
|
|
|
StmtWithSemi::Optional => {
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.at(T![;]) {
|
|
|
|
p.eat(T![;]);
|
2019-04-18 14:49:56 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 23:34:43 -05:00
|
|
|
}
|
2018-08-24 11:27:30 -05:00
|
|
|
m.complete(p, LET_STMT);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:18:19 -05:00
|
|
|
pub(super) fn expr_block_contents(p: &mut Parser) {
|
2019-04-16 23:34:43 -05:00
|
|
|
// This is checked by a validator
|
2020-08-13 10:58:35 -05:00
|
|
|
attributes::inner_attrs(p);
|
2019-04-16 23:34:43 -05:00
|
|
|
|
2019-05-15 07:35:47 -05:00
|
|
|
while !p.at(EOF) && !p.at(T!['}']) {
|
2019-04-16 23:34:43 -05:00
|
|
|
// test nocontentexpr
|
|
|
|
// fn foo(){
|
|
|
|
// ;;;some_expr();;;;{;;;};;;;Ok(())
|
|
|
|
// }
|
2019-06-25 22:36:14 -05:00
|
|
|
|
|
|
|
// test nocontentexpr_after_item
|
|
|
|
// fn simple_function() {
|
|
|
|
// enum LocalEnum {
|
|
|
|
// One,
|
|
|
|
// Two,
|
|
|
|
// };
|
|
|
|
// fn f() {};
|
|
|
|
// struct S {};
|
|
|
|
// }
|
|
|
|
|
2019-09-09 05:23:37 -05:00
|
|
|
if p.at(T![;]) {
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![;]);
|
2019-04-16 23:34:43 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-16 00:44:50 -05:00
|
|
|
stmt(p, StmtWithSemi::Yes, false)
|
2019-04-16 23:34:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 09:12:00 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Restrictions {
|
2018-08-07 09:00:45 -05:00
|
|
|
forbid_structs: bool,
|
|
|
|
prefer_stmt: bool,
|
2018-08-04 09:12:00 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 06:52:31 -05:00
|
|
|
/// Binding powers of operators for a Pratt parser.
|
|
|
|
///
|
|
|
|
/// See https://www.oilshell.org/blog/2016/11/03.html
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn current_op(p: &Parser) -> (u8, SyntaxKind) {
|
|
|
|
const NOT_AN_OP: (u8, SyntaxKind) = (0, T![@]);
|
|
|
|
match p.current() {
|
|
|
|
T![|] if p.at(T![||]) => (3, T![||]),
|
|
|
|
T![|] if p.at(T![|=]) => (1, T![|=]),
|
|
|
|
T![|] => (6, T![|]),
|
|
|
|
T![>] if p.at(T![>>=]) => (1, T![>>=]),
|
|
|
|
T![>] if p.at(T![>>]) => (9, T![>>]),
|
|
|
|
T![>] if p.at(T![>=]) => (5, T![>=]),
|
|
|
|
T![>] => (5, T![>]),
|
|
|
|
T![=] if p.at(T![=>]) => NOT_AN_OP,
|
|
|
|
T![=] if p.at(T![==]) => (5, T![==]),
|
|
|
|
T![=] => (1, T![=]),
|
|
|
|
T![<] if p.at(T![<=]) => (5, T![<=]),
|
|
|
|
T![<] if p.at(T![<<=]) => (1, T![<<=]),
|
|
|
|
T![<] if p.at(T![<<]) => (9, T![<<]),
|
|
|
|
T![<] => (5, T![<]),
|
|
|
|
T![+] if p.at(T![+=]) => (1, T![+=]),
|
|
|
|
T![+] => (10, T![+]),
|
|
|
|
T![^] if p.at(T![^=]) => (1, T![^=]),
|
|
|
|
T![^] => (7, T![^]),
|
|
|
|
T![%] if p.at(T![%=]) => (1, T![%=]),
|
|
|
|
T![%] => (11, T![%]),
|
|
|
|
T![&] if p.at(T![&=]) => (1, T![&=]),
|
|
|
|
T![&] if p.at(T![&&]) => (4, T![&&]),
|
|
|
|
T![&] => (8, T![&]),
|
|
|
|
T![/] if p.at(T![/=]) => (1, T![/=]),
|
|
|
|
T![/] => (11, T![/]),
|
|
|
|
T![*] if p.at(T![*=]) => (1, T![*=]),
|
|
|
|
T![*] => (11, T![*]),
|
|
|
|
T![.] if p.at(T![..=]) => (2, T![..=]),
|
|
|
|
T![.] if p.at(T![..]) => (2, T![..]),
|
|
|
|
T![!] if p.at(T![!=]) => (5, T![!=]),
|
|
|
|
T![-] if p.at(T![-=]) => (1, T![-=]),
|
|
|
|
T![-] => (10, T![-]),
|
2019-10-03 02:10:58 -05:00
|
|
|
T![as] => (12, T![as]),
|
2019-09-09 06:52:31 -05:00
|
|
|
|
|
|
|
_ => NOT_AN_OP
|
2018-08-07 06:24:03 -05:00
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses expression with binding power of at least bp.
|
2020-03-11 06:40:38 -05:00
|
|
|
fn expr_bp(p: &mut Parser, mut r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) {
|
2019-09-10 13:28:27 -05:00
|
|
|
let mut lhs = match lhs(p, r) {
|
2018-12-19 15:19:32 -06:00
|
|
|
Some((lhs, blocklike)) => {
|
2018-08-07 09:00:45 -05:00
|
|
|
// test stmt_bin_expr_ambiguity
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = {1} & 2;
|
|
|
|
// {1} &2;
|
|
|
|
// }
|
2018-12-19 15:19:32 -06:00
|
|
|
if r.prefer_stmt && blocklike.is_block() {
|
2019-03-19 03:24:02 -05:00
|
|
|
return (Some(lhs), BlockLike::Block);
|
2018-08-07 09:00:45 -05:00
|
|
|
}
|
2018-08-07 08:32:09 -05:00
|
|
|
lhs
|
2018-08-07 09:00:45 -05:00
|
|
|
}
|
2019-03-19 03:24:02 -05:00
|
|
|
None => return (None, BlockLike::NotBlock),
|
2018-08-04 08:58:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
2019-09-09 05:23:37 -05:00
|
|
|
let is_range = p.at(T![..]) || p.at(T![..=]);
|
2018-08-05 08:09:25 -05:00
|
|
|
let (op_bp, op) = current_op(p);
|
2018-08-04 08:58:22 -05:00
|
|
|
if op_bp < bp {
|
|
|
|
break;
|
|
|
|
}
|
2019-10-05 18:30:10 -05:00
|
|
|
// test as_precedence
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = &1 as *const i32;
|
|
|
|
// }
|
2019-10-03 02:10:58 -05:00
|
|
|
if p.at(T![as]) {
|
|
|
|
lhs = cast_expr(p, lhs);
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-07 07:21:15 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-09 06:52:31 -05:00
|
|
|
p.bump(op);
|
2019-04-13 05:38:31 -05:00
|
|
|
|
2020-03-11 06:40:38 -05:00
|
|
|
// test binop_resets_statementness
|
|
|
|
// fn foo() {
|
|
|
|
// v = {1}&2;
|
|
|
|
// }
|
|
|
|
r = Restrictions { prefer_stmt: false, ..r };
|
|
|
|
|
2019-11-15 02:08:43 -06:00
|
|
|
if is_range {
|
|
|
|
// test postfix_range
|
|
|
|
// fn foo() {
|
|
|
|
// let x = 1..;
|
|
|
|
// match 1.. { _ => () };
|
|
|
|
// match a.b()..S { _ => () };
|
|
|
|
// }
|
|
|
|
let has_trailing_expression =
|
|
|
|
p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(T!['{']));
|
|
|
|
if !has_trailing_expression {
|
|
|
|
// no RHS
|
|
|
|
lhs = m.complete(p, RANGE_EXPR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 06:40:38 -05:00
|
|
|
expr_bp(p, Restrictions { prefer_stmt: false, ..r }, op_bp + 1);
|
2018-08-07 07:21:15 -05:00
|
|
|
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
2019-03-19 03:24:02 -05:00
|
|
|
(Some(lhs), BlockLike::NotBlock)
|
2018-08-07 08:32:09 -05:00
|
|
|
}
|
|
|
|
|
2020-04-10 10:06:57 -05:00
|
|
|
const LHS_FIRST: TokenSet =
|
2020-08-27 11:11:33 -05:00
|
|
|
atom::ATOM_EXPR_FIRST.union(TokenSet::new(&[T![&], T![*], T![!], T![.], T![-]]));
|
2018-08-04 08:58:22 -05:00
|
|
|
|
2019-09-10 13:28:27 -05:00
|
|
|
fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
|
2018-08-04 09:12:00 -05:00
|
|
|
let m;
|
|
|
|
let kind = match p.current() {
|
|
|
|
// test ref_expr
|
|
|
|
// fn foo() {
|
2020-05-28 14:42:22 -05:00
|
|
|
// // reference operator
|
2018-08-04 09:12:00 -05:00
|
|
|
// let _ = &1;
|
|
|
|
// let _ = &mut &f();
|
2020-05-28 14:42:22 -05:00
|
|
|
// let _ = &raw;
|
|
|
|
// let _ = &raw.0;
|
|
|
|
// // raw reference operator
|
|
|
|
// let _ = &raw mut foo;
|
|
|
|
// let _ = &raw const foo;
|
2018-08-04 09:12:00 -05:00
|
|
|
// }
|
2019-05-15 07:35:47 -05:00
|
|
|
T![&] => {
|
2018-08-04 09:12:00 -05:00
|
|
|
m = p.start();
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![&]);
|
2020-05-28 14:42:22 -05:00
|
|
|
if p.at(IDENT)
|
|
|
|
&& p.at_contextual_kw("raw")
|
|
|
|
&& (p.nth_at(1, T![mut]) || p.nth_at(1, T![const]))
|
|
|
|
{
|
|
|
|
p.bump_remap(T![raw]);
|
|
|
|
p.bump_any();
|
|
|
|
} else {
|
|
|
|
p.eat(T![mut]);
|
|
|
|
}
|
2018-08-04 09:12:00 -05:00
|
|
|
REF_EXPR
|
2018-08-05 08:15:40 -05:00
|
|
|
}
|
2018-08-07 06:56:33 -05:00
|
|
|
// test unary_expr
|
2018-08-04 09:12:00 -05:00
|
|
|
// fn foo() {
|
|
|
|
// **&1;
|
|
|
|
// !!true;
|
2018-08-07 06:24:03 -05:00
|
|
|
// --1;
|
|
|
|
// }
|
2019-05-15 07:35:47 -05:00
|
|
|
T![*] | T![!] | T![-] => {
|
2018-08-07 06:24:03 -05:00
|
|
|
m = p.start();
|
2019-09-09 16:59:29 -05:00
|
|
|
p.bump_any();
|
2018-08-07 06:52:03 -05:00
|
|
|
PREFIX_EXPR
|
2018-08-07 06:24:03 -05:00
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
_ => {
|
2019-09-09 06:52:31 -05:00
|
|
|
// test full_range_expr
|
|
|
|
// fn foo() { xs[..]; }
|
|
|
|
for &op in [T![..=], T![..]].iter() {
|
|
|
|
if p.at(op) {
|
|
|
|
m = p.start();
|
|
|
|
p.bump(op);
|
2019-11-15 02:08:43 -06:00
|
|
|
if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(T!['{'])) {
|
2019-09-10 13:28:27 -05:00
|
|
|
expr_bp(p, r, 2);
|
2019-09-09 06:52:31 -05:00
|
|
|
}
|
|
|
|
return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 05:17:10 -05:00
|
|
|
// test expression_after_block
|
|
|
|
// fn foo() {
|
|
|
|
// let mut p = F{x: 5};
|
|
|
|
// {p}.x = 10;
|
|
|
|
// }
|
|
|
|
//
|
2018-12-19 14:55:24 -06:00
|
|
|
let (lhs, blocklike) = atom::atom_expr(p, r)?;
|
2019-11-15 02:08:43 -06:00
|
|
|
return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block())));
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
};
|
2019-10-03 02:10:58 -05:00
|
|
|
// parse the interior of the unary expression
|
2019-09-10 13:28:27 -05:00
|
|
|
expr_bp(p, r, 255);
|
2018-12-19 14:55:24 -06:00
|
|
|
Some((m.complete(p, kind), BlockLike::NotBlock))
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
|
2018-12-19 14:55:24 -06:00
|
|
|
fn postfix_expr(
|
|
|
|
p: &mut Parser,
|
|
|
|
mut lhs: CompletedMarker,
|
2018-12-19 14:02:37 -06: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}; ();`
|
2019-08-03 06:57:07 -05:00
|
|
|
mut block_like: BlockLike,
|
2018-12-19 14:55:24 -06:00
|
|
|
mut allow_calls: bool,
|
2019-08-03 06:57:07 -05:00
|
|
|
) -> (CompletedMarker, BlockLike) {
|
2018-08-04 08:58:22 -05:00
|
|
|
loop {
|
|
|
|
lhs = match p.current() {
|
2018-08-07 09:00:45 -05:00
|
|
|
// test stmt_postfix_expr_ambiguity
|
|
|
|
// fn foo() {
|
|
|
|
// match () {
|
|
|
|
// _ => {}
|
|
|
|
// () => {}
|
|
|
|
// [] => {}
|
|
|
|
// }
|
|
|
|
// }
|
2019-05-15 07:35:47 -05:00
|
|
|
T!['('] if allow_calls => call_expr(p, lhs),
|
|
|
|
T!['['] if allow_calls => index_expr(p, lhs),
|
2019-11-15 02:08:43 -06:00
|
|
|
T![.] => match postfix_dot_expr(p, lhs) {
|
2019-09-09 06:52:31 -05:00
|
|
|
Ok(it) => it,
|
|
|
|
Err(it) => {
|
|
|
|
lhs = it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2019-05-15 07:35:47 -05:00
|
|
|
T![?] => try_expr(p, lhs),
|
2018-08-04 08:58:22 -05:00
|
|
|
_ => break,
|
2018-08-07 09:00:45 -05:00
|
|
|
};
|
2019-08-03 06:57:07 -05:00
|
|
|
allow_calls = true;
|
|
|
|
block_like = BlockLike::NotBlock;
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
2019-09-09 06:52:31 -05:00
|
|
|
return (lhs, block_like);
|
|
|
|
|
|
|
|
fn postfix_dot_expr(
|
|
|
|
p: &mut Parser,
|
|
|
|
lhs: CompletedMarker,
|
|
|
|
) -> Result<CompletedMarker, CompletedMarker> {
|
|
|
|
assert!(p.at(T![.]));
|
|
|
|
if p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth_at(2, T![::])) {
|
|
|
|
return Ok(method_call_expr(p, lhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// test await_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.await;
|
|
|
|
// x.0.await;
|
|
|
|
// x.0().await?.hello();
|
|
|
|
// }
|
|
|
|
if p.nth(1) == T![await] {
|
|
|
|
let m = lhs.precede(p);
|
|
|
|
p.bump(T![.]);
|
|
|
|
p.bump(T![await]);
|
|
|
|
return Ok(m.complete(p, AWAIT_EXPR));
|
|
|
|
}
|
|
|
|
|
2019-11-15 02:08:43 -06:00
|
|
|
if p.at(T![..=]) || p.at(T![..]) {
|
|
|
|
return Err(lhs);
|
2019-09-09 06:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(field_expr(p, lhs))
|
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// test call_expr
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = f();
|
|
|
|
// let _ = f()(1)(1, 2,);
|
2019-01-24 15:19:16 -06:00
|
|
|
// let _ = f(<Foo>::func());
|
|
|
|
// f(<Foo as Trait>::func());
|
2018-08-04 08:58:22 -05:00
|
|
|
// }
|
|
|
|
fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T!['(']));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = lhs.precede(p);
|
|
|
|
arg_list(p);
|
|
|
|
m.complete(p, CALL_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-05 09:24:44 -05:00
|
|
|
// test index_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x[1][2];
|
|
|
|
// }
|
|
|
|
fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T!['[']));
|
2018-08-05 09:24:44 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T!['[']);
|
2018-08-05 09:24:44 -05:00
|
|
|
expr(p);
|
2019-05-15 07:35:47 -05:00
|
|
|
p.expect(T![']']);
|
2018-08-05 09:24:44 -05:00
|
|
|
m.complete(p, INDEX_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:58:22 -05:00
|
|
|
// test method_call_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.foo();
|
2018-08-05 08:15:40 -05:00
|
|
|
// y.bar::<T>(1, 2,);
|
2018-08-04 08:58:22 -05:00
|
|
|
// }
|
|
|
|
fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-09-09 06:52:31 -05:00
|
|
|
assert!(p.at(T![.]) && p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth_at(2, T![::])));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-09 16:59:29 -05:00
|
|
|
p.bump_any();
|
2019-08-09 05:16:47 -05:00
|
|
|
name_ref(p);
|
2020-08-13 10:58:35 -05:00
|
|
|
type_args::opt_generic_arg_list(p, true);
|
2019-05-15 07:35:47 -05:00
|
|
|
if p.at(T!['(']) {
|
2018-08-13 10:46:43 -05:00
|
|
|
arg_list(p);
|
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
m.complete(p, METHOD_CALL_EXPR)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test field_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.foo;
|
|
|
|
// x.0.bar;
|
2019-04-05 15:34:45 -05:00
|
|
|
// x.0();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// test_err bad_tuple_index_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x.0.;
|
|
|
|
// x.1i32;
|
|
|
|
// x.0x01;
|
2018-08-04 08:58:22 -05:00
|
|
|
// }
|
|
|
|
fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T![.]));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![.]);
|
2019-08-09 05:16:47 -05:00
|
|
|
if p.at(IDENT) || p.at(INT_NUMBER) {
|
|
|
|
name_ref_or_index(p)
|
2019-04-05 15:34:45 -05:00
|
|
|
} else if p.at(FLOAT_NUMBER) {
|
2019-05-15 07:35:47 -05:00
|
|
|
// FIXME: How to recover and instead parse INT + T![.]?
|
2019-09-09 16:59:29 -05:00
|
|
|
p.bump_any();
|
2018-12-25 07:48:54 -06:00
|
|
|
} else {
|
|
|
|
p.error("expected field name or number")
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
m.complete(p, FIELD_EXPR)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test try_expr
|
|
|
|
// fn foo() {
|
|
|
|
// x?;
|
|
|
|
// }
|
|
|
|
fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T![?]));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![?]);
|
2018-08-04 08:58:22 -05:00
|
|
|
m.complete(p, TRY_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:55:16 -05:00
|
|
|
// test cast_expr
|
|
|
|
// fn foo() {
|
|
|
|
// 82 as i32;
|
2018-12-17 10:10:05 -06:00
|
|
|
// 81 as i8 + 1;
|
|
|
|
// 79 as i16 - 1;
|
2019-03-30 06:44:58 -05:00
|
|
|
// 0x36 as u8 <= 0x37;
|
2018-08-06 19:55:16 -05:00
|
|
|
// }
|
|
|
|
fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T![as]));
|
2018-08-06 19:55:16 -05:00
|
|
|
let m = lhs.precede(p);
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T![as]);
|
2018-12-17 10:10:05 -06:00
|
|
|
// Use type_no_bounds(), because cast expressions are not
|
|
|
|
// allowed to have bounds.
|
|
|
|
types::type_no_bounds(p);
|
2018-08-06 19:55:16 -05:00
|
|
|
m.complete(p, CAST_EXPR)
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:58:22 -05:00
|
|
|
fn arg_list(p: &mut Parser) {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T!['(']));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = p.start();
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T!['(']);
|
2019-05-15 07:35:47 -05:00
|
|
|
while !p.at(T![')']) && !p.at(EOF) {
|
2020-01-16 21:20:17 -06:00
|
|
|
// test arg_with_attr
|
|
|
|
// fn main() {
|
|
|
|
// foo(#[attr] 92)
|
|
|
|
// }
|
2020-01-17 04:47:07 -06:00
|
|
|
if !expr_with_attrs(p) {
|
2018-09-08 02:13:32 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-05-15 07:35:47 -05:00
|
|
|
if !p.at(T![')']) && !p.expect(T![,]) {
|
2018-08-04 08:58:22 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 07:35:47 -05:00
|
|
|
p.eat(T![')']);
|
2018-08-04 08:58:22 -05:00
|
|
|
m.complete(p, ARG_LIST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test path_expr
|
|
|
|
// fn foo() {
|
|
|
|
// let _ = a;
|
|
|
|
// let _ = a::b;
|
|
|
|
// let _ = ::a::<b>;
|
2018-08-05 06:16:38 -05:00
|
|
|
// let _ = format!();
|
2018-08-04 08:58:22 -05:00
|
|
|
// }
|
2018-12-19 14:55:24 -06:00
|
|
|
fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) {
|
2019-08-13 10:36:01 -05:00
|
|
|
assert!(paths::is_path_start(p));
|
2018-08-04 08:58:22 -05:00
|
|
|
let m = p.start();
|
|
|
|
paths::expr_path(p);
|
2018-12-19 14:55:24 -06:00
|
|
|
match p.current() {
|
2019-05-15 07:35:47 -05:00
|
|
|
T!['{'] if !r.forbid_structs => {
|
2020-08-13 10:58:35 -05:00
|
|
|
record_expr_field_list(p);
|
2020-07-30 09:21:30 -05:00
|
|
|
(m.complete(p, RECORD_EXPR), BlockLike::NotBlock)
|
2018-08-05 06:16:38 -05:00
|
|
|
}
|
2019-09-09 06:52:31 -05:00
|
|
|
T![!] if !p.at(T![!=]) => {
|
2018-12-19 14:55:24 -06:00
|
|
|
let block_like = items::macro_call_after_excl(p);
|
2019-04-26 10:42:56 -05:00
|
|
|
(m.complete(p, MACRO_CALL), block_like)
|
2018-08-05 06:16:38 -05:00
|
|
|
}
|
2018-12-19 14:55:24 -06:00
|
|
|
_ => (m.complete(p, PATH_EXPR), BlockLike::NotBlock),
|
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
|
2019-08-23 07:55:21 -05:00
|
|
|
// test record_lit
|
2018-08-04 08:58:22 -05:00
|
|
|
// fn foo() {
|
|
|
|
// S {};
|
|
|
|
// S { x, y: 32, };
|
|
|
|
// S { x, y: 32, ..Default::default() };
|
2019-08-09 03:38:52 -05:00
|
|
|
// TupleStruct { 0: 1 };
|
2018-08-04 08:58:22 -05:00
|
|
|
// }
|
2020-08-13 10:58:35 -05:00
|
|
|
pub(crate) fn record_expr_field_list(p: &mut Parser) {
|
2019-05-15 07:35:47 -05:00
|
|
|
assert!(p.at(T!['{']));
|
2018-08-24 11:27:30 -05:00
|
|
|
let m = p.start();
|
2019-09-19 14:51:46 -05:00
|
|
|
p.bump(T!['{']);
|
2019-05-15 07:35:47 -05:00
|
|
|
while !p.at(EOF) && !p.at(T!['}']) {
|
2020-04-11 09:42:24 -05:00
|
|
|
let m = p.start();
|
|
|
|
// test record_literal_field_with_attr
|
|
|
|
// fn main() {
|
|
|
|
// S { #[cfg(test)] field: 1 }
|
|
|
|
// }
|
2020-08-13 10:58:35 -05:00
|
|
|
attributes::outer_attrs(p);
|
2020-04-11 09:42:24 -05:00
|
|
|
|
2018-08-04 08:58:22 -05:00
|
|
|
match p.current() {
|
2020-04-11 09:42:24 -05:00
|
|
|
IDENT | INT_NUMBER => {
|
|
|
|
// test_err record_literal_before_ellipsis_recovery
|
|
|
|
// fn main() {
|
|
|
|
// S { field ..S::default() }
|
|
|
|
// }
|
|
|
|
if p.nth_at(1, T![:]) || p.nth_at(1, T![..]) {
|
|
|
|
name_ref_or_index(p);
|
|
|
|
p.expect(T![:]);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
2020-04-11 09:42:24 -05:00
|
|
|
expr(p);
|
2020-07-30 09:21:30 -05:00
|
|
|
m.complete(p, RECORD_EXPR_FIELD);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
2019-09-09 06:52:31 -05:00
|
|
|
T![.] if p.at(T![..]) => {
|
2020-04-11 09:42:24 -05:00
|
|
|
m.abandon(p);
|
2019-09-09 06:52:31 -05:00
|
|
|
p.bump(T![..]);
|
2018-08-04 08:58:22 -05:00
|
|
|
expr(p);
|
|
|
|
}
|
2020-04-11 09:42:24 -05:00
|
|
|
T!['{'] => {
|
|
|
|
error_block(p, "expected a field");
|
|
|
|
m.abandon(p);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
p.err_and_bump("expected identifier");
|
|
|
|
m.abandon(p);
|
|
|
|
}
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
2019-05-15 07:35:47 -05:00
|
|
|
if !p.at(T!['}']) {
|
|
|
|
p.expect(T![,]);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 07:35:47 -05:00
|
|
|
p.expect(T!['}']);
|
2020-07-30 09:21:30 -05:00
|
|
|
m.complete(p, RECORD_EXPR_FIELD_LIST);
|
2018-08-04 08:58:22 -05:00
|
|
|
}
|