From 3dc2aeea0f242a7fe7a9c9add731a1de1495a59a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 18 Sep 2021 15:46:28 +0300 Subject: [PATCH] internal: parser cleanup --- crates/parser/src/grammar/items.rs | 2 +- crates/parser/src/grammar/type_params.rs | 87 +++++++++---------- .../0122_generic_lifetime_type_attribute.rast | 69 --------------- .../0122_generic_lifetime_type_attribute.rs | 2 - ...81_generic_param_list_param_attribute.rast | 45 ++++++++++ ...0181_generic_param_list_param_attribute.rs | 1 + .../parser/inline/ok/0182_lifetime_param.rast | 24 +++++ .../parser/inline/ok/0182_lifetime_param.rs | 1 + .../parser/inline/ok/0183_type_param.rast | 29 +++++++ .../parser/inline/ok/0183_type_param.rs | 1 + .../inline/ok/0184_generic_param_list.rast | 29 +++++++ .../inline/ok/0184_generic_param_list.rs | 1 + 12 files changed, 175 insertions(+), 116 deletions(-) delete mode 100644 crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast delete mode 100644 crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs create mode 100644 crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rs create mode 100644 crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rs create mode 100644 crates/syntax/test_data/parser/inline/ok/0183_type_param.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0183_type_param.rs create mode 100644 crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rs diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 13d9f9e1ab4..96a2ba40129 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -19,7 +19,7 @@ // 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 a7a0109fd1b..f5db4c7222f 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() {} 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() {} 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; if p.at(T![=]) { + // test type_param_default + // struct S; p.bump(T![=]); types::type_(p) } @@ -64,7 +68,6 @@ fn type_param(p: &mut Parser, m: Marker) { // test const_param // struct S; 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; - // struct B; - // struct C; if p.at(T![=]) { + // test const_param_defaults + // struct A; + // struct B; + // struct C; 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; -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; +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() - // where - // for<'a> F: Fn(&'a str) - // { } if p.at(T![for]) { + // test where_pred_for + // fn for_trait() + // 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 5682bd28c57..00000000000 --- a/crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast +++ /dev/null @@ -1,69 +0,0 @@ -SOURCE_FILE@0..64 - FN@0..63 - FN_KW@0..2 "fn" - WHITESPACE@2..3 " " - NAME@3..6 - IDENT@3..6 "foo" - GENERIC_PARAM_LIST@6..49 - L_ANGLE@6..7 "<" - LIFETIME_PARAM@7..29 - ATTR@7..26 - POUND@7..8 "#" - L_BRACK@8..9 "[" - META@9..25 - PATH@9..15 - PATH_SEGMENT@9..15 - NAME_REF@9..15 - IDENT@9..15 "derive" - TOKEN_TREE@15..25 - L_PAREN@15..16 "(" - IDENT@16..24 "Lifetime" - R_PAREN@24..25 ")" - R_BRACK@25..26 "]" - WHITESPACE@26..27 " " - LIFETIME@27..29 - LIFETIME_IDENT@27..29 "'a" - COMMA@29..30 "," - WHITESPACE@30..31 " " - TYPE_PARAM@31..48 - ATTR@31..46 - POUND@31..32 "#" - L_BRACK@32..33 "[" - META@33..45 - PATH@33..39 - PATH_SEGMENT@33..39 - NAME_REF@33..39 - IDENT@33..39 "derive" - TOKEN_TREE@39..45 - L_PAREN@39..40 "(" - IDENT@40..44 "Type" - R_PAREN@44..45 ")" - R_BRACK@45..46 "]" - WHITESPACE@46..47 " " - NAME@47..48 - IDENT@47..48 "T" - R_ANGLE@48..49 ">" - PARAM_LIST@49..59 - L_PAREN@49..50 "(" - PARAM@50..58 - WILDCARD_PAT@50..51 - UNDERSCORE@50..51 "_" - COLON@51..52 ":" - WHITESPACE@52..53 " " - REF_TYPE@53..58 - AMP@53..54 "&" - LIFETIME@54..56 - LIFETIME_IDENT@54..56 "'a" - WHITESPACE@56..57 " " - PATH_TYPE@57..58 - PATH@57..58 - PATH_SEGMENT@57..58 - NAME_REF@57..58 - IDENT@57..58 "T" - R_PAREN@58..59 ")" - WHITESPACE@59..60 " " - BLOCK_EXPR@60..63 - L_CURLY@60..61 "{" - WHITESPACE@61..62 "\n" - R_CURLY@62..63 "}" - WHITESPACE@63..64 "\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 e8fdf741fb9..00000000000 --- 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 00000000000..475c2367663 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0181_generic_param_list_param_attribute.rast @@ -0,0 +1,45 @@ +SOURCE_FILE@0..40 + FN@0..39 + FN_KW@0..2 "fn" + WHITESPACE@2..3 " " + NAME@3..6 + IDENT@3..6 "foo" + GENERIC_PARAM_LIST@6..34 + L_ANGLE@6..7 "<" + LIFETIME_PARAM@7..20 + ATTR@7..17 + POUND@7..8 "#" + L_BRACK@8..9 "[" + META@9..16 + PATH@9..16 + PATH_SEGMENT@9..16 + NAME_REF@9..16 + IDENT@9..16 "lt_attr" + R_BRACK@16..17 "]" + WHITESPACE@17..18 " " + LIFETIME@18..20 + LIFETIME_IDENT@18..20 "'a" + COMMA@20..21 "," + WHITESPACE@21..22 " " + TYPE_PARAM@22..33 + ATTR@22..31 + POUND@22..23 "#" + L_BRACK@23..24 "[" + META@24..30 + PATH@24..30 + PATH_SEGMENT@24..30 + NAME_REF@24..30 + IDENT@24..30 "t_attr" + R_BRACK@30..31 "]" + WHITESPACE@31..32 " " + NAME@32..33 + IDENT@32..33 "T" + R_ANGLE@33..34 ">" + PARAM_LIST@34..36 + L_PAREN@34..35 "(" + R_PAREN@35..36 ")" + WHITESPACE@36..37 " " + BLOCK_EXPR@37..39 + L_CURLY@37..38 "{" + R_CURLY@38..39 "}" + WHITESPACE@39..40 "\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 00000000000..0509f81da7e --- /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 00000000000..0eb7361ce42 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0182_lifetime_param.rast @@ -0,0 +1,24 @@ +SOURCE_FILE@0..18 + FN@0..17 + FN_KW@0..2 "fn" + WHITESPACE@2..3 " " + NAME@3..4 + IDENT@3..4 "f" + GENERIC_PARAM_LIST@4..12 + L_ANGLE@4..5 "<" + LIFETIME_PARAM@5..11 + LIFETIME@5..7 + LIFETIME_IDENT@5..7 "'a" + COLON@7..8 ":" + WHITESPACE@8..9 " " + LIFETIME@9..11 + LIFETIME_IDENT@9..11 "'b" + R_ANGLE@11..12 ">" + PARAM_LIST@12..14 + L_PAREN@12..13 "(" + R_PAREN@13..14 ")" + WHITESPACE@14..15 " " + BLOCK_EXPR@15..17 + L_CURLY@15..16 "{" + R_CURLY@16..17 "}" + WHITESPACE@17..18 "\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 00000000000..2bb38ece8c6 --- /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 00000000000..9d8ab493e07 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rast @@ -0,0 +1,29 @@ +SOURCE_FILE@0..20 + FN@0..19 + FN_KW@0..2 "fn" + WHITESPACE@2..3 " " + NAME@3..4 + IDENT@3..4 "f" + GENERIC_PARAM_LIST@4..14 + L_ANGLE@4..5 "<" + TYPE_PARAM@5..13 + NAME@5..6 + IDENT@5..6 "T" + COLON@6..7 ":" + WHITESPACE@7..8 " " + TYPE_BOUND_LIST@8..13 + TYPE_BOUND@8..13 + PATH_TYPE@8..13 + PATH@8..13 + PATH_SEGMENT@8..13 + NAME_REF@8..13 + IDENT@8..13 "Clone" + R_ANGLE@13..14 ">" + PARAM_LIST@14..16 + L_PAREN@14..15 "(" + R_PAREN@15..16 ")" + WHITESPACE@16..17 " " + BLOCK_EXPR@17..19 + L_CURLY@17..18 "{" + R_CURLY@18..19 "}" + WHITESPACE@19..20 "\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 00000000000..b250bc6bf07 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0183_type_param.rs @@ -0,0 +1 @@ +fn f() {} 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 00000000000..9d8ab493e07 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rast @@ -0,0 +1,29 @@ +SOURCE_FILE@0..20 + FN@0..19 + FN_KW@0..2 "fn" + WHITESPACE@2..3 " " + NAME@3..4 + IDENT@3..4 "f" + GENERIC_PARAM_LIST@4..14 + L_ANGLE@4..5 "<" + TYPE_PARAM@5..13 + NAME@5..6 + IDENT@5..6 "T" + COLON@6..7 ":" + WHITESPACE@7..8 " " + TYPE_BOUND_LIST@8..13 + TYPE_BOUND@8..13 + PATH_TYPE@8..13 + PATH@8..13 + PATH_SEGMENT@8..13 + NAME_REF@8..13 + IDENT@8..13 "Clone" + R_ANGLE@13..14 ">" + PARAM_LIST@14..16 + L_PAREN@14..15 "(" + R_PAREN@15..16 ")" + WHITESPACE@16..17 " " + BLOCK_EXPR@17..19 + L_CURLY@17..18 "{" + R_CURLY@18..19 "}" + WHITESPACE@19..20 "\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 00000000000..b250bc6bf07 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0184_generic_param_list.rs @@ -0,0 +1 @@ +fn f() {}