internal: more focused parsing tests

This commit is contained in:
Aleksey Kladov
2021-09-17 22:15:12 +03:00
parent 55078c81e0
commit 073d5f7733
19 changed files with 206 additions and 187 deletions

View File

@@ -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]);

View File

@@ -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();