From 58d14bcaf70662506e1a289494365d976b40fc4e Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 17 Feb 2019 22:19:21 +0200 Subject: Enable parsing attributes for generic lifetimes and type parameters --- crates/ra_syntax/src/ast/generated.rs | 2 + crates/ra_syntax/src/grammar.ron | 7 ++- crates/ra_syntax/src/grammar/type_params.rs | 22 +++++--- .../ok/0122_generic_lifetime_type_attribute.rs | 2 + .../ok/0122_generic_lifetime_type_attribute.txt | 61 ++++++++++++++++++++++ 5 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.txt diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index c7aaccc95..47107a58b 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1716,6 +1716,7 @@ impl ToOwned for LifetimeParam { } +impl ast::AttrsOwner for LifetimeParam {} impl LifetimeParam { pub fn lifetime(&self) -> Option<&Lifetime> { super::child_opt(self) @@ -4076,6 +4077,7 @@ impl ToOwned for TypeParam { impl ast::NameOwner for TypeParam {} +impl ast::AttrsOwner for TypeParam {} impl TypeParam {} // TypeParamList diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 193b563aa..b1775d0f8 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -559,8 +559,11 @@ Grammar( ["lifetime_params", "LifetimeParam" ], ] ), - "TypeParam": ( traits: ["NameOwner"] ), - "LifetimeParam": ( options: [ "Lifetime" ] ), + "TypeParam": ( traits: ["NameOwner", "AttrsOwner"] ), + "LifetimeParam": ( + options: [ "Lifetime"], + traits: ["AttrsOwner"], + ), "Lifetime": ( traits: ["AstToken"] ), "WhereClause": (), "ExprStmt": ( diff --git a/crates/ra_syntax/src/grammar/type_params.rs b/crates/ra_syntax/src/grammar/type_params.rs index 1ec813b3e..40f998682 100644 --- a/crates/ra_syntax/src/grammar/type_params.rs +++ b/crates/ra_syntax/src/grammar/type_params.rs @@ -13,10 +13,20 @@ fn type_param_list(p: &mut Parser) { p.bump(); while !p.at(EOF) && !p.at(R_ANGLE) { + let m = p.start(); + + // test generic_lifetime_type_attribute + // fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) { + // } + attributes::outer_attributes(p); + match p.current() { - LIFETIME => lifetime_param(p), - IDENT => type_param(p), - _ => p.err_and_bump("expected type parameter"), + LIFETIME => lifetime_param(p, m), + IDENT => type_param(p, m), + _ => { + m.abandon(p); + p.err_and_bump("expected type parameter") + } } if !p.at(R_ANGLE) && !p.expect(COMMA) { break; @@ -26,9 +36,8 @@ fn type_param_list(p: &mut Parser) { m.complete(p, TYPE_PARAM_LIST); } -fn lifetime_param(p: &mut Parser) { +fn lifetime_param(p: &mut Parser, m: Marker) { assert!(p.at(LIFETIME)); - let m = p.start(); p.bump(); if p.at(COLON) { lifetime_bounds(p); @@ -36,9 +45,8 @@ fn lifetime_param(p: &mut Parser) { m.complete(p, LIFETIME_PARAM); } -fn type_param(p: &mut Parser) { +fn type_param(p: &mut Parser, m: Marker) { assert!(p.at(IDENT)); - let m = p.start(); name(p); if p.at(COLON) { bounds(p); diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs new file mode 100644 index 000000000..e8fdf741f --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs @@ -0,0 +1,2 @@ +fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) { +} diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.txt new file mode 100644 index 000000000..6a6aa89e8 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0122_generic_lifetime_type_attribute.txt @@ -0,0 +1,61 @@ +SOURCE_FILE@[0; 64) + FN_DEF@[0; 63) + FN_KW@[0; 2) + WHITESPACE@[2; 3) + NAME@[3; 6) + IDENT@[3; 6) "foo" + TYPE_PARAM_LIST@[6; 49) + L_ANGLE@[6; 7) + LIFETIME_PARAM@[7; 29) + ATTR@[7; 26) + POUND@[7; 8) + TOKEN_TREE@[8; 26) + L_BRACK@[8; 9) + IDENT@[9; 15) "derive" + TOKEN_TREE@[15; 25) + L_PAREN@[15; 16) + IDENT@[16; 24) "Lifetime" + R_PAREN@[24; 25) + R_BRACK@[25; 26) + WHITESPACE@[26; 27) + LIFETIME@[27; 29) "'a" + COMMA@[29; 30) + WHITESPACE@[30; 31) + TYPE_PARAM@[31; 48) + ATTR@[31; 46) + POUND@[31; 32) + TOKEN_TREE@[32; 46) + L_BRACK@[32; 33) + IDENT@[33; 39) "derive" + TOKEN_TREE@[39; 45) + L_PAREN@[39; 40) + IDENT@[40; 44) "Type" + R_PAREN@[44; 45) + R_BRACK@[45; 46) + WHITESPACE@[46; 47) + NAME@[47; 48) + IDENT@[47; 48) "T" + R_ANGLE@[48; 49) + PARAM_LIST@[49; 59) + L_PAREN@[49; 50) + PARAM@[50; 58) + PLACEHOLDER_PAT@[50; 51) + UNDERSCORE@[50; 51) + COLON@[51; 52) + WHITESPACE@[52; 53) + REFERENCE_TYPE@[53; 58) + AMP@[53; 54) + LIFETIME@[54; 56) "'a" + WHITESPACE@[56; 57) + PATH_TYPE@[57; 58) + PATH@[57; 58) + PATH_SEGMENT@[57; 58) + NAME_REF@[57; 58) + IDENT@[57; 58) "T" + R_PAREN@[58; 59) + WHITESPACE@[59; 60) + BLOCK@[60; 63) + L_CURLY@[60; 61) + WHITESPACE@[61; 62) + R_CURLY@[62; 63) + WHITESPACE@[63; 64) -- cgit v1.2.3