diff options
4 files changed, 161 insertions, 7 deletions
diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 8c19aa179..2567313ab 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs | |||
@@ -181,7 +181,16 @@ fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> { | |||
181 | MODULE | 181 | MODULE |
182 | } | 182 | } |
183 | STRUCT_KW => { | 183 | STRUCT_KW => { |
184 | nominal::struct_def(p); | 184 | // test struct_items |
185 | // struct Foo; | ||
186 | // struct Foo {} | ||
187 | // struct Foo(); | ||
188 | // struct Foo(String, usize); | ||
189 | // struct Foo { | ||
190 | // a: i32, | ||
191 | // b: f32, | ||
192 | // } | ||
193 | nominal::struct_def(p, STRUCT_KW); | ||
185 | if p.at(SEMI) { | 194 | if p.at(SEMI) { |
186 | p.err_and_bump( | 195 | p.err_and_bump( |
187 | "expected item, found `;`\n\ | 196 | "expected item, found `;`\n\ |
@@ -190,6 +199,16 @@ fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> { | |||
190 | } | 199 | } |
191 | STRUCT_DEF | 200 | STRUCT_DEF |
192 | } | 201 | } |
202 | IDENT if p.at_contextual_kw("union") => { | ||
203 | // test union_items | ||
204 | // union Foo {} | ||
205 | // union Foo { | ||
206 | // a: i32, | ||
207 | // b: f32, | ||
208 | // } | ||
209 | nominal::struct_def(p, UNION_KW); | ||
210 | STRUCT_DEF | ||
211 | } | ||
193 | ENUM_KW => { | 212 | ENUM_KW => { |
194 | nominal::enum_def(p); | 213 | nominal::enum_def(p); |
195 | ENUM_DEF | 214 | ENUM_DEF |
diff --git a/crates/libsyntax2/src/grammar/items/nominal.rs b/crates/libsyntax2/src/grammar/items/nominal.rs index 11c43e371..8d02ad555 100644 --- a/crates/libsyntax2/src/grammar/items/nominal.rs +++ b/crates/libsyntax2/src/grammar/items/nominal.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | 2 | ||
3 | pub(super) fn struct_def(p: &mut Parser) { | 3 | pub(super) fn struct_def(p: &mut Parser, kind: SyntaxKind) { |
4 | assert!(p.at(STRUCT_KW)); | 4 | assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union")); |
5 | p.bump(); | 5 | p.bump_remap(kind); |
6 | 6 | ||
7 | name_r(p, ITEM_RECOVERY_SET); | 7 | name_r(p, ITEM_RECOVERY_SET); |
8 | type_params::opt_type_param_list(p); | 8 | type_params::opt_type_param_list(p); |
@@ -22,19 +22,23 @@ pub(super) fn struct_def(p: &mut Parser) { | |||
22 | } | 22 | } |
23 | } | 23 | } |
24 | } | 24 | } |
25 | SEMI => { | 25 | SEMI if kind == STRUCT_KW => { |
26 | p.bump(); | 26 | p.bump(); |
27 | return; | 27 | return; |
28 | } | 28 | } |
29 | L_CURLY => named_field_def_list(p), | 29 | L_CURLY => named_field_def_list(p), |
30 | L_PAREN => { | 30 | L_PAREN if kind == STRUCT_KW => { |
31 | pos_field_list(p); | 31 | pos_field_list(p); |
32 | p.expect(SEMI); | 32 | p.expect(SEMI); |
33 | } | 33 | } |
34 | _ => { | 34 | _ if kind == STRUCT_KW => { |
35 | p.error("expected `;`, `{`, or `(`"); | 35 | p.error("expected `;`, `{`, or `(`"); |
36 | return; | 36 | return; |
37 | } | 37 | } |
38 | _ => { | ||
39 | p.error("expected `{`"); | ||
40 | return; | ||
41 | } | ||
38 | } | 42 | } |
39 | } | 43 | } |
40 | 44 | ||
diff --git a/crates/libsyntax2/tests/data/parser/inline/0109_struct_items.txt b/crates/libsyntax2/tests/data/parser/inline/0109_struct_items.txt new file mode 100644 index 000000000..ea016d393 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/inline/0109_struct_items.txt | |||
@@ -0,0 +1,86 @@ | |||
1 | ROOT@[0; 105) | ||
2 | STRUCT_DEF@[0; 11) | ||
3 | STRUCT_KW@[0; 6) | ||
4 | WHITESPACE@[6; 7) | ||
5 | NAME@[7; 10) | ||
6 | IDENT@[7; 10) "Foo" | ||
7 | SEMI@[10; 11) | ||
8 | WHITESPACE@[11; 12) | ||
9 | STRUCT_DEF@[12; 25) | ||
10 | STRUCT_KW@[12; 18) | ||
11 | WHITESPACE@[18; 19) | ||
12 | NAME@[19; 22) | ||
13 | IDENT@[19; 22) "Foo" | ||
14 | WHITESPACE@[22; 23) | ||
15 | NAMED_FIELD_DEF_LIST@[23; 25) | ||
16 | L_CURLY@[23; 24) | ||
17 | R_CURLY@[24; 25) | ||
18 | WHITESPACE@[25; 26) | ||
19 | STRUCT_DEF@[26; 39) | ||
20 | STRUCT_KW@[26; 32) | ||
21 | WHITESPACE@[32; 33) | ||
22 | NAME@[33; 36) | ||
23 | IDENT@[33; 36) "Foo" | ||
24 | POS_FIELD_LIST@[36; 38) | ||
25 | L_PAREN@[36; 37) | ||
26 | R_PAREN@[37; 38) | ||
27 | SEMI@[38; 39) | ||
28 | WHITESPACE@[39; 40) | ||
29 | STRUCT_DEF@[40; 66) | ||
30 | STRUCT_KW@[40; 46) | ||
31 | WHITESPACE@[46; 47) | ||
32 | NAME@[47; 50) | ||
33 | IDENT@[47; 50) "Foo" | ||
34 | POS_FIELD_LIST@[50; 65) | ||
35 | L_PAREN@[50; 51) | ||
36 | POS_FIELD@[51; 57) | ||
37 | PATH_TYPE@[51; 57) | ||
38 | PATH@[51; 57) | ||
39 | PATH_SEGMENT@[51; 57) | ||
40 | NAME_REF@[51; 57) | ||
41 | IDENT@[51; 57) "String" | ||
42 | COMMA@[57; 58) | ||
43 | WHITESPACE@[58; 59) | ||
44 | POS_FIELD@[59; 64) | ||
45 | PATH_TYPE@[59; 64) | ||
46 | PATH@[59; 64) | ||
47 | PATH_SEGMENT@[59; 64) | ||
48 | NAME_REF@[59; 64) | ||
49 | IDENT@[59; 64) "usize" | ||
50 | R_PAREN@[64; 65) | ||
51 | SEMI@[65; 66) | ||
52 | WHITESPACE@[66; 67) | ||
53 | STRUCT_DEF@[67; 105) | ||
54 | STRUCT_KW@[67; 73) | ||
55 | WHITESPACE@[73; 74) | ||
56 | NAME@[74; 77) | ||
57 | IDENT@[74; 77) "Foo" | ||
58 | WHITESPACE@[77; 78) | ||
59 | NAMED_FIELD_DEF_LIST@[78; 105) | ||
60 | L_CURLY@[78; 79) | ||
61 | WHITESPACE@[79; 84) | ||
62 | NAMED_FIELD_DEF@[84; 90) | ||
63 | NAME@[84; 85) | ||
64 | IDENT@[84; 85) "a" | ||
65 | COLON@[85; 86) | ||
66 | WHITESPACE@[86; 87) | ||
67 | PATH_TYPE@[87; 90) | ||
68 | PATH@[87; 90) | ||
69 | PATH_SEGMENT@[87; 90) | ||
70 | NAME_REF@[87; 90) | ||
71 | IDENT@[87; 90) "i32" | ||
72 | COMMA@[90; 91) | ||
73 | WHITESPACE@[91; 96) | ||
74 | NAMED_FIELD_DEF@[96; 102) | ||
75 | NAME@[96; 97) | ||
76 | IDENT@[96; 97) "b" | ||
77 | COLON@[97; 98) | ||
78 | WHITESPACE@[98; 99) | ||
79 | PATH_TYPE@[99; 102) | ||
80 | PATH@[99; 102) | ||
81 | PATH_SEGMENT@[99; 102) | ||
82 | NAME_REF@[99; 102) | ||
83 | IDENT@[99; 102) "f32" | ||
84 | COMMA@[102; 103) | ||
85 | WHITESPACE@[103; 104) | ||
86 | R_CURLY@[104; 105) | ||
diff --git a/crates/libsyntax2/tests/data/parser/inline/0110_union_items.txt b/crates/libsyntax2/tests/data/parser/inline/0110_union_items.txt new file mode 100644 index 000000000..93e8a4e8d --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/inline/0110_union_items.txt | |||
@@ -0,0 +1,45 @@ | |||
1 | ROOT@[0; 50) | ||
2 | STRUCT_DEF@[0; 12) | ||
3 | UNION_KW@[0; 5) | ||
4 | WHITESPACE@[5; 6) | ||
5 | NAME@[6; 9) | ||
6 | IDENT@[6; 9) "Foo" | ||
7 | WHITESPACE@[9; 10) | ||
8 | NAMED_FIELD_DEF_LIST@[10; 12) | ||
9 | L_CURLY@[10; 11) | ||
10 | R_CURLY@[11; 12) | ||
11 | WHITESPACE@[12; 13) | ||
12 | STRUCT_DEF@[13; 50) | ||
13 | UNION_KW@[13; 18) | ||
14 | WHITESPACE@[18; 19) | ||
15 | NAME@[19; 22) | ||
16 | IDENT@[19; 22) "Foo" | ||
17 | WHITESPACE@[22; 23) | ||
18 | NAMED_FIELD_DEF_LIST@[23; 50) | ||
19 | L_CURLY@[23; 24) | ||
20 | WHITESPACE@[24; 29) | ||
21 | NAMED_FIELD_DEF@[29; 35) | ||
22 | NAME@[29; 30) | ||
23 | IDENT@[29; 30) "a" | ||
24 | COLON@[30; 31) | ||
25 | WHITESPACE@[31; 32) | ||
26 | PATH_TYPE@[32; 35) | ||
27 | PATH@[32; 35) | ||
28 | PATH_SEGMENT@[32; 35) | ||
29 | NAME_REF@[32; 35) | ||
30 | IDENT@[32; 35) "i32" | ||
31 | COMMA@[35; 36) | ||
32 | WHITESPACE@[36; 41) | ||
33 | NAMED_FIELD_DEF@[41; 47) | ||
34 | NAME@[41; 42) | ||
35 | IDENT@[41; 42) "b" | ||
36 | COLON@[42; 43) | ||
37 | WHITESPACE@[43; 44) | ||
38 | PATH_TYPE@[44; 47) | ||
39 | PATH@[44; 47) | ||
40 | PATH_SEGMENT@[44; 47) | ||
41 | NAME_REF@[44; 47) | ||
42 | IDENT@[44; 47) "f32" | ||
43 | COMMA@[47; 48) | ||
44 | WHITESPACE@[48; 49) | ||
45 | R_CURLY@[49; 50) | ||