From 7a39bc3ba29351feabcd4a16e12568a9e12818ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 16:42:24 +0200 Subject: Make records grammar more orthogonal We used name [: expr] grammar before, now it is [name :] expr which makes things simpler --- crates/ra_syntax/src/ast/extensions.rs | 32 ++++++++++++++ ...14_record_literal_before_ellipsis_recovery.rast | 49 ++++++++++++++++++++++ ...0014_record_literal_before_ellipsis_recovery.rs | 3 ++ .../parser/inline/ok/0061_record_lit.rast | 14 +++++-- 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast create mode 100644 crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 76b7655e6..7dc024991 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -187,6 +187,38 @@ impl ast::StructDef { } } +impl ast::RecordField { + pub fn for_field_name(field_name: &ast::NameRef) -> Option { + eprintln!("field_name = {}", field_name); + dbg!(field_name.syntax().ancestors().nth(6)); + let candidate = + field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| { + field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast) + })?; + if candidate.field_name().as_ref() == Some(field_name) { + Some(candidate) + } else { + None + } + } + + /// Deals with field init shorthand + pub fn field_name(&self) -> Option { + if let Some(name_ref) = self.name_ref() { + return Some(name_ref); + } + if let Some(ast::Expr::PathExpr(expr)) = self.expr() { + let path = expr.path()?; + let segment = path.segment()?; + let name_ref = segment.name_ref()?; + if path.qualifier().is_none() { + return Some(name_ref); + } + } + None + } +} + impl ast::EnumVariant { pub fn parent_enum(&self) -> ast::EnumDef { self.syntax() diff --git a/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast new file mode 100644 index 000000000..75043c9c0 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rast @@ -0,0 +1,49 @@ +SOURCE_FILE@[0; 45) + FN_DEF@[0; 44) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 7) + IDENT@[3; 7) "main" + PARAM_LIST@[7; 9) + L_PAREN@[7; 8) "(" + R_PAREN@[8; 9) ")" + WHITESPACE@[9; 10) " " + BLOCK_EXPR@[10; 44) + BLOCK@[10; 44) + L_CURLY@[10; 11) "{" + WHITESPACE@[11; 16) "\n " + RECORD_LIT@[16; 42) + PATH@[16; 17) + PATH_SEGMENT@[16; 17) + NAME_REF@[16; 17) + IDENT@[16; 17) "S" + WHITESPACE@[17; 18) " " + RECORD_FIELD_LIST@[18; 42) + L_CURLY@[18; 19) "{" + WHITESPACE@[19; 20) " " + RECORD_FIELD@[20; 40) + NAME_REF@[20; 25) + IDENT@[20; 25) "field" + WHITESPACE@[25; 26) " " + RANGE_EXPR@[26; 40) + DOT2@[26; 28) ".." + CALL_EXPR@[28; 40) + PATH_EXPR@[28; 38) + PATH@[28; 38) + PATH@[28; 29) + PATH_SEGMENT@[28; 29) + NAME_REF@[28; 29) + IDENT@[28; 29) "S" + COLON2@[29; 31) "::" + PATH_SEGMENT@[31; 38) + NAME_REF@[31; 38) + IDENT@[31; 38) "default" + ARG_LIST@[38; 40) + L_PAREN@[38; 39) "(" + R_PAREN@[39; 40) ")" + WHITESPACE@[40; 41) " " + R_CURLY@[41; 42) "}" + WHITESPACE@[42; 43) "\n" + R_CURLY@[43; 44) "}" + WHITESPACE@[44; 45) "\n" +error [25; 25): expected COLON diff --git a/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs new file mode 100644 index 000000000..a4e5b2f69 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/err/0014_record_literal_before_ellipsis_recovery.rs @@ -0,0 +1,3 @@ +fn main() { + S { field ..S::default() } +} diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast b/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast index f4206858b..89a611799 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast +++ b/crates/ra_syntax/test_data/parser/inline/ok/0061_record_lit.rast @@ -35,8 +35,11 @@ SOURCE_FILE@[0; 112) L_CURLY@[27; 28) "{" WHITESPACE@[28; 29) " " RECORD_FIELD@[29; 30) - NAME_REF@[29; 30) - IDENT@[29; 30) "x" + PATH_EXPR@[29; 30) + PATH@[29; 30) + PATH_SEGMENT@[29; 30) + NAME_REF@[29; 30) + IDENT@[29; 30) "x" COMMA@[30; 31) "," WHITESPACE@[31; 32) " " RECORD_FIELD@[32; 37) @@ -62,8 +65,11 @@ SOURCE_FILE@[0; 112) L_CURLY@[48; 49) "{" WHITESPACE@[49; 50) " " RECORD_FIELD@[50; 51) - NAME_REF@[50; 51) - IDENT@[50; 51) "x" + PATH_EXPR@[50; 51) + PATH@[50; 51) + PATH_SEGMENT@[50; 51) + NAME_REF@[50; 51) + IDENT@[50; 51) "x" COMMA@[51; 52) "," WHITESPACE@[52; 53) " " RECORD_FIELD@[53; 58) -- cgit v1.2.3 From 0aece75cdd40daa4d48484103cfcd36ba13ba076 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 19:25:33 +0200 Subject: Remove dead code --- crates/ra_syntax/src/ast/extensions.rs | 2 -- crates/ra_syntax/src/lib.rs | 5 +++++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 7dc024991..63e272fbf 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -189,8 +189,6 @@ impl ast::StructDef { impl ast::RecordField { pub fn for_field_name(field_name: &ast::NameRef) -> Option { - eprintln!("field_name = {}", field_name); - dbg!(field_name.syntax().ancestors().nth(6)); let candidate = field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| { field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast) diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index f0e16dc2b..a796e78b1 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -19,6 +19,11 @@ //! [RFC]: //! [Swift]: +#[allow(unused)] +macro_rules! eprintln { + ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; +} + mod syntax_node; mod syntax_error; mod parsing; -- cgit v1.2.3