diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-04-11 18:37:02 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-04-11 18:37:02 +0100 |
commit | 1a1c09ed3e3a34c0a8750f98ece9ad85595395d2 (patch) | |
tree | 44136a4ce738d388bb9b0ce9f5569cf68465cbe2 /crates/ra_syntax | |
parent | 11d400b63b07d3cffbe8d1363b802a2d52f5d786 (diff) | |
parent | 0aece75cdd40daa4d48484103cfcd36ba13ba076 (diff) |
Merge #3951
3951: Simplify records grammar r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
5 files changed, 97 insertions, 4 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 76b7655e6..63e272fbf 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -187,6 +187,36 @@ impl ast::StructDef { | |||
187 | } | 187 | } |
188 | } | 188 | } |
189 | 189 | ||
190 | impl ast::RecordField { | ||
191 | pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordField> { | ||
192 | let candidate = | ||
193 | field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| { | ||
194 | field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast) | ||
195 | })?; | ||
196 | if candidate.field_name().as_ref() == Some(field_name) { | ||
197 | Some(candidate) | ||
198 | } else { | ||
199 | None | ||
200 | } | ||
201 | } | ||
202 | |||
203 | /// Deals with field init shorthand | ||
204 | pub fn field_name(&self) -> Option<ast::NameRef> { | ||
205 | if let Some(name_ref) = self.name_ref() { | ||
206 | return Some(name_ref); | ||
207 | } | ||
208 | if let Some(ast::Expr::PathExpr(expr)) = self.expr() { | ||
209 | let path = expr.path()?; | ||
210 | let segment = path.segment()?; | ||
211 | let name_ref = segment.name_ref()?; | ||
212 | if path.qualifier().is_none() { | ||
213 | return Some(name_ref); | ||
214 | } | ||
215 | } | ||
216 | None | ||
217 | } | ||
218 | } | ||
219 | |||
190 | impl ast::EnumVariant { | 220 | impl ast::EnumVariant { |
191 | pub fn parent_enum(&self) -> ast::EnumDef { | 221 | pub fn parent_enum(&self) -> ast::EnumDef { |
192 | self.syntax() | 222 | self.syntax() |
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 @@ | |||
19 | //! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> | 19 | //! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> |
20 | //! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> | 20 | //! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> |
21 | 21 | ||
22 | #[allow(unused)] | ||
23 | macro_rules! eprintln { | ||
24 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; | ||
25 | } | ||
26 | |||
22 | mod syntax_node; | 27 | mod syntax_node; |
23 | mod syntax_error; | 28 | mod syntax_error; |
24 | mod parsing; | 29 | mod parsing; |
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 @@ | |||
1 | SOURCE_FILE@[0; 45) | ||
2 | FN_DEF@[0; 44) | ||
3 | FN_KW@[0; 2) "fn" | ||
4 | WHITESPACE@[2; 3) " " | ||
5 | NAME@[3; 7) | ||
6 | IDENT@[3; 7) "main" | ||
7 | PARAM_LIST@[7; 9) | ||
8 | L_PAREN@[7; 8) "(" | ||
9 | R_PAREN@[8; 9) ")" | ||
10 | WHITESPACE@[9; 10) " " | ||
11 | BLOCK_EXPR@[10; 44) | ||
12 | BLOCK@[10; 44) | ||
13 | L_CURLY@[10; 11) "{" | ||
14 | WHITESPACE@[11; 16) "\n " | ||
15 | RECORD_LIT@[16; 42) | ||
16 | PATH@[16; 17) | ||
17 | PATH_SEGMENT@[16; 17) | ||
18 | NAME_REF@[16; 17) | ||
19 | IDENT@[16; 17) "S" | ||
20 | WHITESPACE@[17; 18) " " | ||
21 | RECORD_FIELD_LIST@[18; 42) | ||
22 | L_CURLY@[18; 19) "{" | ||
23 | WHITESPACE@[19; 20) " " | ||
24 | RECORD_FIELD@[20; 40) | ||
25 | NAME_REF@[20; 25) | ||
26 | IDENT@[20; 25) "field" | ||
27 | WHITESPACE@[25; 26) " " | ||
28 | RANGE_EXPR@[26; 40) | ||
29 | DOT2@[26; 28) ".." | ||
30 | CALL_EXPR@[28; 40) | ||
31 | PATH_EXPR@[28; 38) | ||
32 | PATH@[28; 38) | ||
33 | PATH@[28; 29) | ||
34 | PATH_SEGMENT@[28; 29) | ||
35 | NAME_REF@[28; 29) | ||
36 | IDENT@[28; 29) "S" | ||
37 | COLON2@[29; 31) "::" | ||
38 | PATH_SEGMENT@[31; 38) | ||
39 | NAME_REF@[31; 38) | ||
40 | IDENT@[31; 38) "default" | ||
41 | ARG_LIST@[38; 40) | ||
42 | L_PAREN@[38; 39) "(" | ||
43 | R_PAREN@[39; 40) ")" | ||
44 | WHITESPACE@[40; 41) " " | ||
45 | R_CURLY@[41; 42) "}" | ||
46 | WHITESPACE@[42; 43) "\n" | ||
47 | R_CURLY@[43; 44) "}" | ||
48 | WHITESPACE@[44; 45) "\n" | ||
49 | 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 @@ | |||
1 | fn main() { | ||
2 | S { field ..S::default() } | ||
3 | } | ||
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) | |||
35 | L_CURLY@[27; 28) "{" | 35 | L_CURLY@[27; 28) "{" |
36 | WHITESPACE@[28; 29) " " | 36 | WHITESPACE@[28; 29) " " |
37 | RECORD_FIELD@[29; 30) | 37 | RECORD_FIELD@[29; 30) |
38 | NAME_REF@[29; 30) | 38 | PATH_EXPR@[29; 30) |
39 | IDENT@[29; 30) "x" | 39 | PATH@[29; 30) |
40 | PATH_SEGMENT@[29; 30) | ||
41 | NAME_REF@[29; 30) | ||
42 | IDENT@[29; 30) "x" | ||
40 | COMMA@[30; 31) "," | 43 | COMMA@[30; 31) "," |
41 | WHITESPACE@[31; 32) " " | 44 | WHITESPACE@[31; 32) " " |
42 | RECORD_FIELD@[32; 37) | 45 | RECORD_FIELD@[32; 37) |
@@ -62,8 +65,11 @@ SOURCE_FILE@[0; 112) | |||
62 | L_CURLY@[48; 49) "{" | 65 | L_CURLY@[48; 49) "{" |
63 | WHITESPACE@[49; 50) " " | 66 | WHITESPACE@[49; 50) " " |
64 | RECORD_FIELD@[50; 51) | 67 | RECORD_FIELD@[50; 51) |
65 | NAME_REF@[50; 51) | 68 | PATH_EXPR@[50; 51) |
66 | IDENT@[50; 51) "x" | 69 | PATH@[50; 51) |
70 | PATH_SEGMENT@[50; 51) | ||
71 | NAME_REF@[50; 51) | ||
72 | IDENT@[50; 51) "x" | ||
67 | COMMA@[51; 52) "," | 73 | COMMA@[51; 52) "," |
68 | WHITESPACE@[52; 53) " " | 74 | WHITESPACE@[52; 53) " " |
69 | RECORD_FIELD@[53; 58) | 75 | RECORD_FIELD@[53; 58) |