10269: internal: cleanup item parsing r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-09-17 21:39:06 +00:00 committed by GitHub
commit 894abd1efd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 115 deletions

View file

@ -183,6 +183,7 @@ fn opt_visibility(p: &mut Parser) -> bool {
} }
} }
m.complete(p, VISIBILITY); m.complete(p, VISIBILITY);
true
} }
// test crate_keyword_vis // test crate_keyword_vis
// crate fn main() { } // crate fn main() { }
@ -197,10 +198,10 @@ fn opt_visibility(p: &mut Parser) -> bool {
let m = p.start(); let m = p.start();
p.bump(T![crate]); p.bump(T![crate]);
m.complete(p, VISIBILITY); m.complete(p, VISIBILITY);
}
_ => return false,
}
true true
}
_ => false,
}
} }
fn opt_rename(p: &mut Parser) { fn opt_rename(p: &mut Parser) {

View file

@ -135,18 +135,15 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
p.bump_remap(T![default]); p.bump_remap(T![default]);
has_mods = true; has_mods = true;
} }
T![unsafe] => {
// test default_unsafe_item // test default_unsafe_item
// default unsafe impl T for Foo { // default unsafe impl T for Foo {
// default unsafe fn foo() {} // default unsafe fn foo() {}
// } // }
if matches!(p.nth(2), T![impl] | T![fn]) { T![unsafe] if matches!(p.nth(2), T![impl] | T![fn]) => {
p.bump_remap(T![default]); p.bump_remap(T![default]);
p.bump(T![unsafe]); p.bump(T![unsafe]);
has_mods = true; has_mods = true;
} }
}
T![async] => {
// test default_async_fn // test default_async_fn
// impl T for Foo { // impl T for Foo {
// default async fn foo() {} // default async fn foo() {}
@ -156,6 +153,7 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// impl T for Foo { // impl T for Foo {
// default async unsafe fn foo() {} // default async unsafe fn foo() {}
// } // }
T![async] => {
let mut maybe_fn = p.nth(2); let mut maybe_fn = p.nth(2);
let is_unsafe = if matches!(maybe_fn, T![unsafe]) { let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
maybe_fn = p.nth(3); maybe_fn = p.nth(3);
@ -186,34 +184,14 @@ pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// items // items
match p.current() { match p.current() {
// test fn T![fn] => fn_(p, m),
// fn foo() {}
T![fn] => {
fn_(p);
m.complete(p, FN);
}
// test trait T![const] if p.nth(1) != T!['{'] => consts::konst(p, m),
// trait T {}
T![trait] => {
traits::trait_(p);
m.complete(p, TRAIT);
}
T![const] if p.nth(1) != T!['{'] => { T![trait] => traits::trait_(p, m),
consts::konst(p, m); T![impl] => traits::impl_(p, m),
}
// test impl T![type] => type_alias(p, m),
// impl T for S {}
T![impl] => {
traits::impl_(p);
m.complete(p, IMPL);
}
T![type] => {
type_alias(p, m);
}
// test extern_block // test extern_block
// unsafe extern "C" {} // unsafe extern "C" {}
@ -341,38 +319,6 @@ pub(crate) fn extern_item_list(p: &mut Parser) {
m.complete(p, EXTERN_ITEM_LIST); m.complete(p, EXTERN_ITEM_LIST);
} }
fn fn_(p: &mut Parser) {
assert!(p.at(T![fn]));
p.bump(T![fn]);
name_r(p, ITEM_RECOVERY_SET);
// test function_type_params
// fn foo<T: Clone + Copy>(){}
type_params::opt_generic_param_list(p);
if p.at(T!['(']) {
params::param_list_fn_def(p);
} else {
p.error("expected function arguments");
}
// test function_ret_type
// fn foo() {}
// fn bar() -> () {}
opt_ret_type(p);
// test function_where_clause
// fn foo<T>() where T: Copy {}
type_params::opt_where_clause(p);
// test fn_decl
// trait T { fn foo(); }
if p.at(T![;]) {
p.bump(T![;]);
} else {
expressions::block_expr(p)
}
}
fn macro_rules(p: &mut Parser, m: Marker) { fn macro_rules(p: &mut Parser, m: Marker) {
assert!(p.at_contextual_kw("macro_rules")); assert!(p.at_contextual_kw("macro_rules"));
p.bump_remap(T![macro_rules]); p.bump_remap(T![macro_rules]);
@ -430,6 +376,40 @@ fn macro_def(p: &mut Parser, m: Marker) {
m.complete(p, MACRO_DEF); m.complete(p, MACRO_DEF);
} }
// test fn
// fn foo() {}
fn fn_(p: &mut Parser, m: Marker) {
p.bump(T![fn]);
name_r(p, ITEM_RECOVERY_SET);
// test function_type_params
// fn foo<T: Clone + Copy>(){}
type_params::opt_generic_param_list(p);
if p.at(T!['(']) {
params::param_list_fn_def(p);
} else {
p.error("expected function arguments");
}
// test function_ret_type
// fn foo() {}
// fn bar() -> () {}
opt_ret_type(p);
// test function_where_clause
// fn foo<T>() where T: Copy {}
type_params::opt_where_clause(p);
// test fn_decl
// trait T { fn foo(); }
if p.at(T![;]) {
p.bump(T![;]);
} else {
expressions::block_expr(p)
}
m.complete(p, FN);
}
fn macro_call(p: &mut Parser) -> BlockLike { fn macro_call(p: &mut Parser) -> BlockLike {
assert!(paths::is_use_path_start(p)); assert!(paths::is_use_path_start(p));
paths::use_path(p); paths::use_path(p);

View file

@ -3,7 +3,7 @@ use super::*;
// test trait_item // test trait_item
// trait T<U>: Hash + Clone where U: Copy {} // trait T<U>: Hash + Clone where U: Copy {}
// trait X<U: Debug + Display>: Hash + Clone where U: Copy {} // trait X<U: Debug + Display>: Hash + Clone where U: Copy {}
pub(super) fn trait_(p: &mut Parser) { pub(super) fn trait_(p: &mut Parser, m: Marker) {
assert!(p.at(T![trait])); assert!(p.at(T![trait]));
p.bump(T![trait]); p.bump(T![trait]);
name_r(p, ITEM_RECOVERY_SET); name_r(p, ITEM_RECOVERY_SET);
@ -16,6 +16,7 @@ pub(super) fn trait_(p: &mut Parser) {
type_params::bounds_without_colon(p); type_params::bounds_without_colon(p);
type_params::opt_where_clause(p); type_params::opt_where_clause(p);
p.expect(T![;]); p.expect(T![;]);
m.complete(p, TRAIT);
return; return;
} }
if p.at(T![:]) { if p.at(T![:]) {
@ -27,11 +28,12 @@ pub(super) fn trait_(p: &mut Parser) {
} else { } else {
p.error("expected `{`"); p.error("expected `{`");
} }
m.complete(p, TRAIT);
} }
// test impl_def // test impl_def
// impl Foo {} // impl Foo {}
pub(super) fn impl_(p: &mut Parser) { pub(super) fn impl_(p: &mut Parser, m: Marker) {
assert!(p.at(T![impl])); assert!(p.at(T![impl]));
p.bump(T![impl]); p.bump(T![impl]);
if choose_type_params_over_qpath(p) { if choose_type_params_over_qpath(p) {
@ -58,6 +60,7 @@ pub(super) fn impl_(p: &mut Parser) {
} else { } else {
p.error("expected `{`"); p.error("expected `{`");
} }
m.complete(p, IMPL);
} }
// test impl_item_list // test impl_item_list

View file

@ -1,22 +0,0 @@
SOURCE_FILE@0..16
IMPL@0..15
IMPL_KW@0..4 "impl"
WHITESPACE@4..5 " "
PATH_TYPE@5..6
PATH@5..6
PATH_SEGMENT@5..6
NAME_REF@5..6
IDENT@5..6 "T"
WHITESPACE@6..7 " "
FOR_KW@7..10 "for"
WHITESPACE@10..11 " "
PATH_TYPE@11..12
PATH@11..12
PATH_SEGMENT@11..12
NAME_REF@11..12
IDENT@11..12 "S"
WHITESPACE@12..13 " "
ASSOC_ITEM_LIST@13..15
L_CURLY@13..14 "{"
R_CURLY@14..15 "}"
WHITESPACE@15..16 "\n"

View file

@ -1 +0,0 @@
impl T for S {}

View file

@ -1,11 +0,0 @@
SOURCE_FILE@0..11
TRAIT@0..10
TRAIT_KW@0..5 "trait"
WHITESPACE@5..6 " "
NAME@6..7
IDENT@6..7 "T"
WHITESPACE@7..8 " "
ASSOC_ITEM_LIST@8..10
L_CURLY@8..9 "{"
R_CURLY@9..10 "}"
WHITESPACE@10..11 "\n"

View file

@ -1 +0,0 @@
trait T {}