From c3ac2c93fb446329b09882ef452fd6820290bfc5 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 12 Jan 2020 10:19:17 -0500 Subject: Allow attributes before function arguments This adds support for function calls of the form: ```rust ( #[attr(...)] 1.2, #[attr_one(...)] #[attr_two(...)] 1.5, ... etc ... ) ``` Closes https://github.com/rust-analyzer/rust-analyzer/issues/2801 --- crates/ra_parser/src/grammar/expressions.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 81d4f75f9..7caed66a0 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,11 +535,22 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } +// test arg_list +// fn assert_float(s: &str, n: f64) {} +// fn foo() { +// assert_float( +// "1.797693134862315708e+308L", +// #[allow(clippy::excessive_precision)] +// #[allow(dead_code)] +// 1.797_693_134_862_315_730_8e+308, +// ); +// } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); let m = p.start(); p.bump(T!['(']); while !p.at(T![')']) && !p.at(EOF) { + attributes::outer_attributes(p); if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); break; -- cgit v1.2.3 From a24dcd7babc7212a23eb5c8d7c73bede0d6465aa Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 15 Jan 2020 23:29:55 +0200 Subject: fix(ra_parser.typo): amend 'format language' to 'formal language' --- crates/ra_parser/src/grammar.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index 22f64a9f4..a46e11e1d 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -1,7 +1,7 @@ //! This is the actual "grammar" of the Rust language. //! //! Each function in this module and its children corresponds -//! to a production of the format grammar. Submodules roughly +//! to a production of the formal grammar. Submodules roughly //! correspond to different *areas* of the grammar. By convention, //! each submodule starts with `use super::*` import and exports //! "public" productions via `pub(super)`. -- cgit v1.2.3 From c78e34968ff24bf8b195ea02be54835a09f53abd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 15 Jan 2020 19:12:56 -0500 Subject: shrink inline tes --- crates/ra_parser/src/grammar/expressions.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 7caed66a0..d8105c382 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,15 +535,9 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } -// test arg_list -// fn assert_float(s: &str, n: f64) {} -// fn foo() { -// assert_float( -// "1.797693134862315708e+308L", -// #[allow(clippy::excessive_precision)] -// #[allow(dead_code)] -// 1.797_693_134_862_315_730_8e+308, -// ); +// test arg_with_attr +// fn main() { +// foo(#[attr] 92) // } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); -- cgit v1.2.3 From f077d5c303ecc4f38471b37be985bb0c5ab2f68c Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 16 Jan 2020 22:20:17 -0500 Subject: move inline function closer to relevant code also updates generated inline tests --- crates/ra_parser/src/grammar/expressions.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index d8105c382..95564b602 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -535,15 +535,15 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { m.complete(p, CAST_EXPR) } -// test arg_with_attr -// fn main() { -// foo(#[attr] 92) -// } fn arg_list(p: &mut Parser) { assert!(p.at(T!['('])); let m = p.start(); p.bump(T!['(']); while !p.at(T![')']) && !p.at(EOF) { + // test arg_with_attr + // fn main() { + // foo(#[attr] 92) + // } attributes::outer_attributes(p); if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); -- cgit v1.2.3 From b7c45fba57224a013fbc926abd2e8e9f8f3c77d4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Jan 2020 11:44:40 +0100 Subject: Extract expr_with_attrs --- crates/ra_parser/src/grammar/expressions.rs | 20 ++++++++++++++++++++ crates/ra_parser/src/grammar/expressions/atom.rs | 15 ++------------- 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index a31a7a08d..3cf619e38 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -19,6 +19,26 @@ pub(super) fn expr(p: &mut Parser) -> (Option, BlockLike) { expr_bp(p, r, 1) } +pub(super) fn expr_with_attrs(p: &mut Parser) -> bool { + let m = p.start(); + let has_attrs = p.at(T![#]); + attributes::outer_attributes(p); + + 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 +} + pub(super) fn expr_stmt(p: &mut Parser) -> (Option, BlockLike) { let r = Restrictions { forbid_structs: false, prefer_stmt: true }; expr_bp(p, r, 1) diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index a98a2a3ef..2cc321473 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -191,19 +191,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { // test array_attrs // const A: &[i64] = &[1, #[cfg(test)] 2]; - let m = p.start(); - let has_attrs = p.at(T![#]); - attributes::outer_attributes(p); - - let cm = expr(p).0; - - match (has_attrs, cm) { - (true, Some(cm)) => { - let kind = cm.kind(); - cm.undo_completion(p).abandon(p); - m.complete(p, kind); - } - _ => m.abandon(p), + if !expr_with_attrs(p) { + break; } if n_exprs == 1 && p.eat(T![;]) { -- cgit v1.2.3 From 3a859e587f3bcf9f49293bd1f2b2d19cdfd2be4b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Jan 2020 11:47:07 +0100 Subject: Nest attrs into exprs in function args --- crates/ra_parser/src/grammar/expressions.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 3cf619e38..06c92645e 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -564,12 +564,9 @@ fn arg_list(p: &mut Parser) { // fn main() { // foo(#[attr] 92) // } - attributes::outer_attributes(p); - if !p.at_ts(EXPR_FIRST) { - p.error("expected expression"); + if !expr_with_attrs(p) { break; } - expr(p); if !p.at(T![')']) && !p.expect(T![,]) { break; } -- cgit v1.2.3