From 55a3e21ac4cd24dd7979a44c37cd1e7a3d1b85fd Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Jan 2019 21:35:03 +0000 Subject: Support attributes on let statements --- crates/ra_syntax/src/grammar/expressions.rs | 50 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 1604d9b5a..0b2f7b116 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -54,6 +54,7 @@ pub(crate) fn block(p: &mut Parser) { _ => { // test block_items // fn a() { fn b() {} } + let has_attrs = p.at(POUND); let m = p.start(); match items::maybe_item(p, items::ItemFlavor::Mod) { items::MaybeItem::Item(kind) => { @@ -66,30 +67,39 @@ pub(crate) fn block(p: &mut Parser) { // test pub_expr // fn foo() { pub 92; } //FIXME items::MaybeItem::None => { - let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; - if p.at(R_CURLY) { + if has_attrs { m.abandon(p); + if p.at(LET_KW) { + let_stmt(p); + } else { + p.error("expected a let statement"); + } } else { - // test no_semi_after_block - // fn foo() { - // if true {} - // loop {} - // match () {} - // while true {} - // for _ in () {} - // {} - // {} - // macro_rules! test { - // () => {} - // } - // test!{} - // } - if is_blocklike { - p.eat(SEMI); + let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; + if p.at(R_CURLY) { + m.abandon(p); } else { - p.expect(SEMI); + // test no_semi_after_block + // fn foo() { + // if true {} + // loop {} + // match () {} + // while true {} + // for _ in () {} + // {} + // {} + // macro_rules! test { + // () => {} + // } + // test!{} + // } + if is_blocklike { + p.eat(SEMI); + } else { + p.expect(SEMI); + } + m.complete(p, EXPR_STMT); } - m.complete(p, EXPR_STMT); } } } -- cgit v1.2.3 From 7055d43c3a1edca16cf5625f1b908643fa0bf21a Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Jan 2019 22:02:23 +0000 Subject: Make attrs be a child of the let statement --- crates/ra_syntax/src/grammar/expressions.rs | 15 +++++++++------ crates/ra_syntax/src/grammar/items.rs | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 0b2f7b116..8dd9587d0 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -45,7 +45,10 @@ pub(crate) fn block(p: &mut Parser) { while !p.at(EOF) && !p.at(R_CURLY) { match p.current() { - LET_KW => let_stmt(p), + LET_KW => { + let m = p.start(); + let_stmt(p, m) + } // test nocontentexpr // fn foo(){ // ;;;some_expr();;;;{;;;};;;;Ok(()) @@ -54,8 +57,9 @@ pub(crate) fn block(p: &mut Parser) { _ => { // test block_items // fn a() { fn b() {} } - let has_attrs = p.at(POUND); let m = p.start(); + let has_attrs = p.at(POUND); + attributes::outer_attributes(p); match items::maybe_item(p, items::ItemFlavor::Mod) { items::MaybeItem::Item(kind) => { m.complete(p, kind); @@ -68,10 +72,10 @@ pub(crate) fn block(p: &mut Parser) { // fn foo() { pub 92; } //FIXME items::MaybeItem::None => { if has_attrs { - m.abandon(p); if p.at(LET_KW) { - let_stmt(p); + let_stmt(p, m); } else { + m.abandon(p); p.error("expected a let statement"); } } else { @@ -116,9 +120,8 @@ pub(crate) fn block(p: &mut Parser) { // let c = 92; // let d: i32 = 92; // } - fn let_stmt(p: &mut Parser) { + fn let_stmt(p: &mut Parser, m: Marker) { assert!(p.at(LET_KW)); - let m = p.start(); p.bump(); patterns::pattern(p); if p.at(COLON) { diff --git a/crates/ra_syntax/src/grammar/items.rs b/crates/ra_syntax/src/grammar/items.rs index 265e84570..18039cd3f 100644 --- a/crates/ra_syntax/src/grammar/items.rs +++ b/crates/ra_syntax/src/grammar/items.rs @@ -36,6 +36,7 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { let m = p.start(); + attributes::outer_attributes(p); match maybe_item(p, flavor) { MaybeItem::Item(kind) => { m.complete(p, kind); @@ -79,7 +80,6 @@ pub(super) enum MaybeItem { } pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { - attributes::outer_attributes(p); opt_visibility(p); if let Some(kind) = items_without_modifiers(p) { return MaybeItem::Item(kind); -- cgit v1.2.3 From 4d35cc387576be6645b12d24af09b1e9c3a5f65b Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 27 Jan 2019 09:00:57 +0000 Subject: Stop using let_stmt twice --- crates/ra_syntax/src/grammar/expressions.rs | 84 ++++++++++++++--------------- 1 file changed, 41 insertions(+), 43 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 8dd9587d0..d27eb8b7e 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -45,10 +45,6 @@ pub(crate) fn block(p: &mut Parser) { while !p.at(EOF) && !p.at(R_CURLY) { match p.current() { - LET_KW => { - let m = p.start(); - let_stmt(p, m) - } // test nocontentexpr // fn foo(){ // ;;;some_expr();;;;{;;;};;;;Ok(()) @@ -60,49 +56,51 @@ pub(crate) fn block(p: &mut Parser) { let m = p.start(); let has_attrs = p.at(POUND); attributes::outer_attributes(p); - 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 => { - if has_attrs { - if p.at(LET_KW) { - let_stmt(p, m); - } else { - m.abandon(p); - p.error("expected a let statement"); - } - } else { - let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; - if p.at(R_CURLY) { + if p.at(LET_KW) { + let_stmt(p, m); + } else { + 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 => { + if has_attrs { m.abandon(p); + p.error( + "expected a let statement or an item after attributes in block", + ); } else { - // test no_semi_after_block - // fn foo() { - // if true {} - // loop {} - // match () {} - // while true {} - // for _ in () {} - // {} - // {} - // macro_rules! test { - // () => {} - // } - // test!{} - // } - if is_blocklike { - p.eat(SEMI); + let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; + if p.at(R_CURLY) { + m.abandon(p); } else { - p.expect(SEMI); + // test no_semi_after_block + // fn foo() { + // if true {} + // loop {} + // match () {} + // while true {} + // for _ in () {} + // {} + // {} + // macro_rules! test { + // () => {} + // } + // test!{} + // } + if is_blocklike { + p.eat(SEMI); + } else { + p.expect(SEMI); + } + m.complete(p, EXPR_STMT); } - m.complete(p, EXPR_STMT); } } } -- cgit v1.2.3