aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/grammar.ron2
-rw-r--r--src/parser/grammar/expressions.rs39
-rw-r--r--src/syntax_kinds/generated.rs4
-rw-r--r--tests/data/parser/inline/0061_struct_lit.rs5
-rw-r--r--tests/data/parser/inline/0061_struct_lit.txt94
5 files changed, 143 insertions, 1 deletions
diff --git a/src/grammar.ron b/src/grammar.ron
index 6c67db739..564249200 100644
--- a/src/grammar.ron
+++ b/src/grammar.ron
@@ -129,6 +129,8 @@ Grammar(
129 "METHOD_CALL_EXPR", 129 "METHOD_CALL_EXPR",
130 "FIELD_EXPR", 130 "FIELD_EXPR",
131 "REF_EXPR", 131 "REF_EXPR",
132 "STRUCT_LIT",
133 "STRUCT_LIT_FIELD",
132 134
133 "EXTERN_BLOCK", 135 "EXTERN_BLOCK",
134 "ENUM_VARIANT", 136 "ENUM_VARIANT",
diff --git a/src/parser/grammar/expressions.rs b/src/parser/grammar/expressions.rs
index 3956df568..5b18e2294 100644
--- a/src/parser/grammar/expressions.rs
+++ b/src/parser/grammar/expressions.rs
@@ -154,5 +154,42 @@ fn path_expr(p: &mut Parser) -> CompletedMarker {
154 assert!(paths::is_path_start(p)); 154 assert!(paths::is_path_start(p));
155 let m = p.start(); 155 let m = p.start();
156 paths::expr_path(p); 156 paths::expr_path(p);
157 m.complete(p, PATH_EXPR) 157 if p.at(L_CURLY) {
158 struct_lit(p);
159 m.complete(p, STRUCT_LIT)
160 } else {
161 m.complete(p, PATH_EXPR)
162 }
163}
164
165// test struct_lit
166// fn foo() {
167// S {};
168// S { x, y: 32, };
169// S { x, y: 32, ..Default::default() };
170// }
171fn struct_lit(p: &mut Parser) {
172 assert!(p.at(L_CURLY));
173 p.bump();
174 while !p.at(EOF) && !p.at(R_CURLY) {
175 match p.current() {
176 IDENT => {
177 let m = p.start();
178 name_ref(p);
179 if p.eat(COLON) {
180 expr(p);
181 }
182 m.complete(p, STRUCT_LIT_FIELD);
183 },
184 DOTDOT => {
185 p.bump();
186 expr(p);
187 },
188 _ => p.err_and_bump("expected identifier"),
189 }
190 if !p.at(R_CURLY) {
191 p.expect(COMMA);
192 }
193 }
194 p.expect(R_CURLY);
158} 195}
diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs
index 7c9333fc5..655764528 100644
--- a/src/syntax_kinds/generated.rs
+++ b/src/syntax_kinds/generated.rs
@@ -120,6 +120,8 @@ pub enum SyntaxKind {
120 METHOD_CALL_EXPR, 120 METHOD_CALL_EXPR,
121 FIELD_EXPR, 121 FIELD_EXPR,
122 REF_EXPR, 122 REF_EXPR,
123 STRUCT_LIT,
124 STRUCT_LIT_FIELD,
123 EXTERN_BLOCK, 125 EXTERN_BLOCK,
124 ENUM_VARIANT, 126 ENUM_VARIANT,
125 NAMED_FIELD, 127 NAMED_FIELD,
@@ -277,6 +279,8 @@ impl SyntaxKind {
277 METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" }, 279 METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" },
278 FIELD_EXPR => &SyntaxInfo { name: "FIELD_EXPR" }, 280 FIELD_EXPR => &SyntaxInfo { name: "FIELD_EXPR" },
279 REF_EXPR => &SyntaxInfo { name: "REF_EXPR" }, 281 REF_EXPR => &SyntaxInfo { name: "REF_EXPR" },
282 STRUCT_LIT => &SyntaxInfo { name: "STRUCT_LIT" },
283 STRUCT_LIT_FIELD => &SyntaxInfo { name: "STRUCT_LIT_FIELD" },
280 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, 284 EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
281 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, 285 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
282 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, 286 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
diff --git a/tests/data/parser/inline/0061_struct_lit.rs b/tests/data/parser/inline/0061_struct_lit.rs
new file mode 100644
index 000000000..eb711f68a
--- /dev/null
+++ b/tests/data/parser/inline/0061_struct_lit.rs
@@ -0,0 +1,5 @@
1fn foo() {
2 S {};
3 S { x, y: 32, };
4 S { x, y: 32, ..Default::default() };
5}
diff --git a/tests/data/parser/inline/0061_struct_lit.txt b/tests/data/parser/inline/0061_struct_lit.txt
new file mode 100644
index 000000000..9a63d8fb2
--- /dev/null
+++ b/tests/data/parser/inline/0061_struct_lit.txt
@@ -0,0 +1,94 @@
1FILE@[0; 86)
2 FN_ITEM@[0; 86)
3 FN_KW@[0; 2)
4 NAME@[2; 6)
5 WHITESPACE@[2; 3)
6 IDENT@[3; 6) "foo"
7 PARAM_LIST@[6; 9)
8 L_PAREN@[6; 7)
9 R_PAREN@[7; 8)
10 WHITESPACE@[8; 9)
11 BLOCK@[9; 86)
12 L_CURLY@[9; 10)
13 EXPR_STMT@[10; 25)
14 STRUCT_LIT@[10; 19)
15 PATH@[10; 17)
16 PATH_SEGMENT@[10; 17)
17 NAME_REF@[10; 17)
18 WHITESPACE@[10; 15)
19 IDENT@[15; 16) "S"
20 WHITESPACE@[16; 17)
21 L_CURLY@[17; 18)
22 R_CURLY@[18; 19)
23 SEMI@[19; 20)
24 WHITESPACE@[20; 25)
25 EXPR_STMT@[25; 46)
26 STRUCT_LIT@[25; 40)
27 PATH@[25; 27)
28 PATH_SEGMENT@[25; 27)
29 NAME_REF@[25; 27)
30 IDENT@[25; 26) "S"
31 WHITESPACE@[26; 27)
32 L_CURLY@[27; 28)
33 STRUCT_LIT_FIELD@[28; 30)
34 NAME_REF@[28; 30)
35 WHITESPACE@[28; 29)
36 IDENT@[29; 30) "x"
37 COMMA@[30; 31)
38 STRUCT_LIT_FIELD@[31; 37)
39 NAME_REF@[31; 33)
40 WHITESPACE@[31; 32)
41 IDENT@[32; 33) "y"
42 COLON@[33; 34)
43 LITERAL@[34; 37)
44 WHITESPACE@[34; 35)
45 INT_NUMBER@[35; 37)
46 COMMA@[37; 38)
47 WHITESPACE@[38; 39)
48 R_CURLY@[39; 40)
49 SEMI@[40; 41)
50 WHITESPACE@[41; 46)
51 EXPR_STMT@[46; 84)
52 STRUCT_LIT@[46; 82)
53 PATH@[46; 48)
54 PATH_SEGMENT@[46; 48)
55 NAME_REF@[46; 48)
56 IDENT@[46; 47) "S"
57 WHITESPACE@[47; 48)
58 L_CURLY@[48; 49)
59 STRUCT_LIT_FIELD@[49; 51)
60 NAME_REF@[49; 51)
61 WHITESPACE@[49; 50)
62 IDENT@[50; 51) "x"
63 COMMA@[51; 52)
64 STRUCT_LIT_FIELD@[52; 58)
65 NAME_REF@[52; 54)
66 WHITESPACE@[52; 53)
67 IDENT@[53; 54) "y"
68 COLON@[54; 55)
69 LITERAL@[55; 58)
70 WHITESPACE@[55; 56)
71 INT_NUMBER@[56; 58)
72 COMMA@[58; 59)
73 WHITESPACE@[59; 60)
74 DOTDOT@[60; 62)
75 CALL_EXPR@[62; 81)
76 PATH_EXPR@[62; 78)
77 PATH@[62; 78)
78 PATH@[62; 69)
79 PATH_SEGMENT@[62; 69)
80 NAME_REF@[62; 69)
81 IDENT@[62; 69) "Default"
82 COLONCOLON@[69; 71)
83 PATH_SEGMENT@[71; 78)
84 NAME_REF@[71; 78)
85 IDENT@[71; 78) "default"
86 ARG_LIST@[78; 81)
87 L_PAREN@[78; 79)
88 R_PAREN@[79; 80)
89 WHITESPACE@[80; 81)
90 R_CURLY@[81; 82)
91 SEMI@[82; 83)
92 WHITESPACE@[83; 84)
93 R_CURLY@[84; 85)
94 WHITESPACE@[85; 86)