Unnamed repository; edit this file 'description' to name the repository.
internal: parser cleanup
12 files changed, 175 insertions, 116 deletions
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 13d9f9e1ab..96a2ba4012 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -19,7 +19,7 @@ use super::*; // struct S; pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attrs(p); - while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) { + while !p.at(EOF) && !(p.at(T!['}']) && stop_on_r_curly) { item_or_macro(p, stop_on_r_curly) } } diff --git a/crates/parser/src/grammar/type_params.rs b/crates/parser/src/grammar/type_params.rs index a7a0109fd1..f5db4c7222 100644 --- a/crates/parser/src/grammar/type_params.rs +++ b/crates/parser/src/grammar/type_params.rs @@ -1,12 +1,13 @@ use super::*; pub(super) fn opt_generic_param_list(p: &mut Parser) { - if !p.at(T![<]) { - return; + if p.at(T![<]) { + generic_param_list(p); } - generic_param_list(p); } +// test generic_param_list +// fn f<T: Clone>() {} fn generic_param_list(p: &mut Parser) { assert!(p.at(T![<])); let m = p.start(); @@ -15,9 +16,8 @@ fn generic_param_list(p: &mut Parser) { while !p.at(EOF) && !p.at(T![>]) { let m = p.start(); - // test generic_lifetime_type_attribute - // fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) { - // } + // test generic_param_list_param_attribute + // fn foo<#[lt_attr] 'a, #[t_attr] T>() {} attributes::outer_attrs(p); match p.current() { @@ -37,6 +37,8 @@ fn generic_param_list(p: &mut Parser) { m.complete(p, GENERIC_PARAM_LIST); } +// test lifetime_param +// fn f<'a: 'b>() {} fn lifetime_param(p: &mut Parser, m: Marker) { assert!(p.at(LIFETIME_IDENT)); lifetime(p); @@ -46,15 +48,17 @@ fn lifetime_param(p: &mut Parser, m: Marker) { m.complete(p, LIFETIME_PARAM); } +// test type_param +// fn f<T: Clone>() {} fn type_param(p: &mut Parser, m: Marker) { assert!(p.at(IDENT)); name(p); if p.at(T![:]) { bounds(p); } - // test type_param_default - // struct S<T = i32>; if p.at(T![=]) { + // test type_param_default + // struct S<T = i32>; p.bump(T![=]); types::type_(p) } @@ -64,7 +68,6 @@ fn type_param(p: &mut Parser, m: Marker) { // test const_param // struct S<const N: u32>; fn const_param(p: &mut Parser, m: Marker) { - assert!(p.at(T![const])); p.bump(T![const]); name(p); if p.at(T![:]) { @@ -73,11 +76,11 @@ fn const_param(p: &mut Parser, m: Marker) { p.error("missing type for const parameter"); } - // test const_param_defaults - // struct A<const N: i32 = -1>; - // struct B<const N: i32 = {}>; - // struct C<const N: i32 = some::CONST>; if p.at(T![=]) { + // test const_param_defaults + // struct A<const N: i32 = -1>; + // struct B<const N: i32 = {}>; + // struct C<const N: i32 = some::CONST>; p.bump(T![=]); type_args::const_arg(p); } @@ -85,14 +88,6 @@ fn const_param(p: &mut Parser, m: Marker) { m.complete(p, CONST_PARAM); } -// test type_param_bounds -// struct S<T: 'a + ?Sized + (Copy)>; -pub(super) fn bounds(p: &mut Parser) { - assert!(p.at(T![:])); - p.bump(T![:]); - bounds_without_colon(p); -} - fn lifetime_bounds(p: &mut Parser) { assert!(p.at(T![:])); p.bump(T![:]); @@ -104,21 +99,28 @@ fn lifetime_bounds(p: &mut Parser) { } } +// test type_param_bounds +// struct S<T: 'a + ?Sized + (Copy)>; +pub(super) fn bounds(p: &mut Parser) { + assert!(p.at(T![:])); + p.bump(T![:]); + bounds_without_colon(p); +} + +pub(super) fn bounds_without_colon(p: &mut Parser) { + let m = p.start(); + bounds_without_colon_m(p, m); +} + pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker { while type_bound(p) { if !p.eat(T![+]) { break; } } - marker.complete(p, TYPE_BOUND_LIST) } -pub(super) fn bounds_without_colon(p: &mut Parser) { - let m = p.start(); - bounds_without_colon_m(p, m); -} - fn type_bound(p: &mut Parser) -> bool { let m = p.start(); let has_paren = p.eat(T!['(']); @@ -160,8 +162,9 @@ pub(super) fn opt_where_clause(p: &mut Parser) { let comma = p.eat(T![,]); - if is_where_clause_end(p) { - break; + match p.current() { + T!['{'] | T![;] | T![=] => break, + _ => (), } if !comma { @@ -170,20 +173,16 @@ pub(super) fn opt_where_clause(p: &mut Parser) { } m.complete(p, WHERE_CLAUSE); -} -fn is_where_predicate(p: &mut Parser) -> bool { - match p.current() { - LIFETIME_IDENT => true, - T![impl] => false, - token => types::TYPE_FIRST.contains(token), + fn is_where_predicate(p: &mut Parser) -> bool { + match p.current() { + LIFETIME_IDENT => true, + T![impl] => false, + token => types::TYPE_FIRST.contains(token), + } } } -fn is_where_clause_end(p: &mut Parser) -> bool { - matches!(p.current(), T!['{'] | T![;] | T![=]) -} - fn where_predicate(p: &mut Parser) { let m = p.start(); match p.current() { @@ -199,12 +198,12 @@ fn where_predicate(p: &mut Parser) { p.error("expected lifetime or type"); } _ => { - // test where_pred_for - // fn for_trait<F>() - // where - // for<'a> F: Fn(&'a str) - // { } if p.at(T![for]) { + // test where_pred_for + // fn for_trait<F>() + // where + // for<'a> F: Fn(&'a str) + // { } types::for_binder(p); } diff --git a/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast b/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast deleted file mode 100644 index 5682bd28c5..0000000000 --- a/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast +++ /dev/null @@ -1,69 +0,0 @@ - [email protected] "fn" - [email protected] " " - [email protected] "foo" - [email protected] "<" - [email protected] "#" - [email protected] "[" - [email protected] "derive" - [email protected] "(" - [email protected] "Lifetime" - [email protected] ")" - [email protected] "]" - [email protected] " " - [email protected] "'a" - [email protected] "," - [email protected] " " - [email protected] "#" - [email protected] "[" - [email protected] "derive" - [email protected] "(" - [email protected] "Type" - [email protected] ")" - [email protected] "]" - [email protected] " " - [email protected] "T" - [email protected] ">" - [email protected] "(" - [email protected] "_" - [email protected] ":" - [email protected] " " - [email protected] "&" - [email protected] "'a" - [email protected] " " - [email protected] "T" - [email protected] ")" - [email protected] " " - [email protected] "{" - [email protected] "\n" - [email protected] "}" - [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs b/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs deleted file mode 100644 index e8fdf741fb..0000000000 --- a/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs +++ /dev/null @@ -1,2 +0,0 @@ -fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) { -} diff --git a/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rast b/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rast new file mode 100644 index 0000000000..475c236766 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rast @@ -0,0 +1,45 @@ + [email protected] "fn" + [email protected] " " + [email protected] "foo" + [email protected] "<" + [email protected] "#" + [email protected] "[" + [email protected] "lt_attr" + [email protected] "]" + [email protected] " " + [email protected] "'a" + [email protected] "," + [email protected] " " + [email protected] "#" + [email protected] "[" + [email protected] "t_attr" + [email protected] "]" + [email protected] " " + [email protected] "T" + [email protected] ">" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rs b/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rs new file mode 100644 index 0000000000..0509f81da7 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rs @@ -0,0 +1 @@ +fn foo<#[lt_attr] 'a, #[t_attr] T>() {} diff --git a/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rast b/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rast new file mode 100644 index 0000000000..0eb7361ce4 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rast @@ -0,0 +1,24 @@ + [email protected] "fn" + [email protected] " " + [email protected] "f" + [email protected] "<" + [email protected] "'a" + [email protected] ":" + [email protected] " " + [email protected] "'b" + [email protected] ">" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rs b/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rs new file mode 100644 index 0000000000..2bb38ece8c --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rs @@ -0,0 +1 @@ +fn f<'a: 'b>() {} diff --git a/crates/syntax/test_data/parser/inline/ok/0183_type_param.rast b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rast new file mode 100644 index 0000000000..9d8ab493e0 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rast @@ -0,0 +1,29 @@ + [email protected] "fn" + [email protected] " " + [email protected] "f" + [email protected] "<" + [email protected] "T" + [email protected] ":" + [email protected] " " + [email protected] "Clone" + [email protected] ">" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0183_type_param.rs b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rs new file mode 100644 index 0000000000..b250bc6bf0 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rs @@ -0,0 +1 @@ +fn f<T: Clone>() {} diff --git a/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rast b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rast new file mode 100644 index 0000000000..9d8ab493e0 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rast @@ -0,0 +1,29 @@ + [email protected] "fn" + [email protected] " " + [email protected] "f" + [email protected] "<" + [email protected] "T" + [email protected] ":" + [email protected] " " + [email protected] "Clone" + [email protected] ">" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rs b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rs new file mode 100644 index 0000000000..b250bc6bf0 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rs @@ -0,0 +1 @@ +fn f<T: Clone>() {} |