Unnamed repository; edit this file 'description' to name the repository.
19 files changed, 206 insertions, 187 deletions
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index af83ba126a..6405a508ab 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -245,29 +245,9 @@ fn opt_item_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { T![mod] => mod_item(p, m), T![type] => type_alias(p, m), - - T![struct] => { - // test struct_items - // struct Foo; - // struct Foo {} - // struct Foo(); - // struct Foo(String, usize); - // struct Foo { - // a: i32, - // b: f32, - // } - adt::strukt(p, m); - } + T![struct] => adt::strukt(p, m), T![enum] => adt::enum_(p, m), - IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { - // test union_items - // union Foo {} - // union Foo { - // a: i32, - // b: f32, - // } - adt::union(p, m); - } + IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => adt::union(p, m), // test pub_macro_def // pub macro m($:ident) {} @@ -324,6 +304,31 @@ pub(crate) fn mod_item(p: &mut Parser, m: Marker) { m.complete(p, MODULE); } +// test type_alias +// type Foo = Bar; +fn type_alias(p: &mut Parser, m: Marker) { + p.bump(T![type]); + + name(p); + + // test type_item_type_params + // type Result<T> = (); + type_params::opt_generic_param_list(p); + + if p.at(T![:]) { + type_params::bounds(p); + } + + // test type_item_where_clause + // type Foo where Foo: Copy = (); + type_params::opt_where_clause(p); + if p.eat(T![=]) { + types::type_(p); + } + p.expect(T![;]); + m.complete(p, TYPE_ALIAS); +} + pub(crate) fn item_list(p: &mut Parser) { assert!(p.at(T!['{'])); let m = p.start(); @@ -374,32 +379,6 @@ fn fn_(p: &mut Parser) { } } -// test type_item -// type Foo = Bar; -fn type_alias(p: &mut Parser, m: Marker) { - assert!(p.at(T![type])); - p.bump(T![type]); - - name(p); - - // test type_item_type_params - // type Result<T> = (); - type_params::opt_generic_param_list(p); - - if p.at(T![:]) { - type_params::bounds(p); - } - - // test type_item_where_clause - // type Foo where Foo: Copy = (); - type_params::opt_where_clause(p); - if p.eat(T![=]) { - types::type_(p); - } - p.expect(T![;]); - m.complete(p, TYPE_ALIAS); -} - fn macro_rules(p: &mut Parser, m: Marker) { assert!(p.at_contextual_kw("macro_rules")); p.bump_remap(T![macro_rules]); diff --git a/crates/parser/src/grammar/items/adt.rs b/crates/parser/src/grammar/items/adt.rs index 386d3806c3..eaf16bc1a1 100644 --- a/crates/parser/src/grammar/items/adt.rs +++ b/crates/parser/src/grammar/items/adt.rs @@ -1,18 +1,21 @@ use super::*; +// test struct_item +// struct S {} pub(super) fn strukt(p: &mut Parser, m: Marker) { - assert!(p.at(T![struct])); p.bump(T![struct]); - struct_or_union(p, m, T![struct], STRUCT); + struct_or_union(p, m, true); } +// test union_item +// struct U { i: i32, f: f32 } pub(super) fn union(p: &mut Parser, m: Marker) { assert!(p.at_contextual_kw("union")); p.bump_remap(T![union]); - struct_or_union(p, m, T![union], UNION); + struct_or_union(p, m, false); } -fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { +fn struct_or_union(p: &mut Parser, m: Marker, is_struct: bool) { name_r(p, ITEM_RECOVERY_SET); type_params::opt_generic_param_list(p); match p.current() { @@ -29,26 +32,24 @@ fn struct_or_union(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { } } } - T![;] if kw == T![struct] => { + T!['{'] => record_field_list(p), + // test unit_struct + // struct S; + T![;] if is_struct => { p.bump(T![;]); } - T!['{'] => record_field_list(p), - T!['('] if kw == T![struct] => { + // test tuple_struct + // struct S(String, usize); + T!['('] if is_struct => { tuple_field_list(p); // test tuple_struct_where - // struct Test<T>(T) where T: Clone; - // struct Test<T>(T); + // struct S<T>(T) where T: Clone; type_params::opt_where_clause(p); p.expect(T![;]); } - _ if kw == T![struct] => { - p.error("expected `;`, `{`, or `(`"); - } - _ => { - p.error("expected `{`"); - } + _ => p.error(if is_struct { "expected `;`, `{`, or `(`" } else { "expected `{`" }), } - m.complete(p, def); + m.complete(p, if is_struct { STRUCT } else { UNION }); } pub(super) fn enum_(p: &mut Parser, m: Marker) { @@ -102,6 +103,8 @@ pub(crate) fn variant_list(p: &mut Parser) { m.complete(p, VARIANT_LIST); } +// test record_field_list +// struct S { a: i32, b: f32 } pub(crate) fn record_field_list(p: &mut Parser) { assert!(p.at(T!['{'])); let m = p.start(); diff --git a/crates/syntax/test_data/parser/inline/ok/0068_union_items.rast b/crates/syntax/test_data/parser/inline/ok/0068_union_items.rast deleted file mode 100644 index 6589e47951..0000000000 --- a/crates/syntax/test_data/parser/inline/ok/0068_union_items.rast +++ /dev/null @@ -1,46 +0,0 @@ - [email protected] "union" - [email protected] " " - [email protected] "Foo" - [email protected] " " - [email protected] "{" - [email protected] "}" - [email protected] "\n" - [email protected] "union" - [email protected] " " - [email protected] "Foo" - [email protected] " " - [email protected] "{" - [email protected] "\n " - [email protected] "a" - [email protected] ":" - [email protected] " " - [email protected] "i32" - [email protected] "," - [email protected] "\n " - [email protected] "b" - [email protected] ":" - [email protected] " " - [email protected] "f32" - [email protected] "," - [email protected] "\n" - [email protected] "}" - [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0068_union_items.rs b/crates/syntax/test_data/parser/inline/ok/0068_union_items.rs deleted file mode 100644 index b7dd610d80..0000000000 --- a/crates/syntax/test_data/parser/inline/ok/0068_union_items.rs +++ /dev/null @@ -1,5 +0,0 @@ -union Foo {} -union Foo { - a: i32, - b: f32, -} diff --git a/crates/syntax/test_data/parser/inline/ok/0078_type_item.rast b/crates/syntax/test_data/parser/inline/ok/0078_type_alias.rast index 2befc8388e..2befc8388e 100644 --- a/crates/syntax/test_data/parser/inline/ok/0078_type_item.rast +++ b/crates/syntax/test_data/parser/inline/ok/0078_type_alias.rast diff --git a/crates/syntax/test_data/parser/inline/ok/0078_type_item.rs b/crates/syntax/test_data/parser/inline/ok/0078_type_alias.rs index 04c0344fa3..04c0344fa3 100644 --- a/crates/syntax/test_data/parser/inline/ok/0078_type_item.rs +++ b/crates/syntax/test_data/parser/inline/ok/0078_type_alias.rs diff --git a/crates/syntax/test_data/parser/inline/ok/0083_struct_items.rs b/crates/syntax/test_data/parser/inline/ok/0083_struct_items.rs deleted file mode 100644 index 693e3f3ee1..0000000000 --- a/crates/syntax/test_data/parser/inline/ok/0083_struct_items.rs +++ /dev/null @@ -1,8 +0,0 @@ -struct Foo; -struct Foo {} -struct Foo(); -struct Foo(String, usize); -struct Foo { - a: i32, - b: f32, -} diff --git a/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rast b/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rast index 0e1594dc48..3196111363 100644 --- a/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rast +++ b/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rast @@ -1,64 +1,42 @@ [email protected] "struct" - [email protected] "Test" - [email protected] "<" - [email protected] "T" - [email protected] ">" - [email protected] "(" - [email protected] "T" - [email protected] ")" - [email protected] " " - [email protected] "where" - [email protected] " " - [email protected] "T" - [email protected] ":" - [email protected] " " - [email protected] "Clone" - [email protected] ";" - [email protected] "\n" - [email protected] "struct" - [email protected] " " - [email protected] "Test" - [email protected] "<" - [email protected] "T" - [email protected] ">" - [email protected] "(" - [email protected] "T" - [email protected] ")" - [email protected] ";" - [email protected] "\n" + [email protected] "S" + [email protected] "<" + [email protected] "T" + [email protected] ">" + [email protected] "(" + [email protected] "T" + [email protected] ")" + [email protected] " " + [email protected] "where" + [email protected] " " + [email protected] "T" + [email protected] ":" + [email protected] " " + [email protected] "Clone" + [email protected] ";" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rs b/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rs index ddd59016dc..a602e00182 100644 --- a/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rs +++ b/crates/syntax/test_data/parser/inline/ok/0114_tuple_struct_where.rs @@ -1,2 +1 @@ -struct Test<T>(T) where T: Clone; -struct Test<T>(T); +struct S<T>(T) where T: Clone; diff --git a/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rast b/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rast new file mode 100644 index 0000000000..935fd6e07b --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rast @@ -0,0 +1,25 @@ + [email protected] "struct" + [email protected] " " + [email protected] "S" + [email protected] "(" + [email protected] "String" + [email protected] "," + [email protected] " " + [email protected] "usize" + [email protected] ")" + [email protected] ";" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rs b/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rs new file mode 100644 index 0000000000..b4e05717ed --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0170_tuple_struct.rs @@ -0,0 +1 @@ +struct S(String, usize); diff --git a/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rast b/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rast new file mode 100644 index 0000000000..3134482f0d --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rast @@ -0,0 +1,11 @@ + [email protected] "struct" + [email protected] " " + [email protected] "S" + [email protected] " " + [email protected] "{" + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rs b/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rs new file mode 100644 index 0000000000..5f1a34f49b --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0171_struct_item.rs @@ -0,0 +1 @@ +struct S {} diff --git a/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rast b/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rast new file mode 100644 index 0000000000..ce1135c597 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rast @@ -0,0 +1,35 @@ + [email protected] "struct" + [email protected] " " + [email protected] "S" + [email protected] " " + [email protected] "{" + [email protected] " " + [email protected] "a" + [email protected] ":" + [email protected] " " + [email protected] "i32" + [email protected] "," + [email protected] " " + [email protected] "b" + [email protected] ":" + [email protected] " " + [email protected] "f32" + [email protected] " " + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rs b/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rs new file mode 100644 index 0000000000..a3bd7787db --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0172_record_field_list.rs @@ -0,0 +1 @@ +struct S { a: i32, b: f32 } diff --git a/crates/syntax/test_data/parser/inline/ok/0173_union_item.rast b/crates/syntax/test_data/parser/inline/ok/0173_union_item.rast new file mode 100644 index 0000000000..5d5c0d69bc --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0173_union_item.rast @@ -0,0 +1,35 @@ + [email protected] "struct" + [email protected] " " + [email protected] "U" + [email protected] " " + [email protected] "{" + [email protected] " " + [email protected] "i" + [email protected] ":" + [email protected] " " + [email protected] "i32" + [email protected] "," + [email protected] " " + [email protected] "f" + [email protected] ":" + [email protected] " " + [email protected] "f32" + [email protected] " " + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0173_union_item.rs b/crates/syntax/test_data/parser/inline/ok/0173_union_item.rs new file mode 100644 index 0000000000..5edf50de3b --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0173_union_item.rs @@ -0,0 +1 @@ +struct U { i: i32, f: f32 } diff --git a/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rast b/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rast new file mode 100644 index 0000000000..b20235889f --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rast @@ -0,0 +1,8 @@ + [email protected] "struct" + [email protected] " " + [email protected] "S" + [email protected] ";" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rs b/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rs new file mode 100644 index 0000000000..28377c2760 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0174_unit_struct.rs @@ -0,0 +1 @@ +struct S; |