internal: improve style

Group related stuff together, use only on path for parsing extern blocks
(they actually have modifiers).

Perhaps we should get rid of items_without_modifiers altogether? Better
to handle these kinds on diagnostics in validation layer...
This commit is contained in:
Aleksey Kladov 2021-08-30 15:55:40 +03:00
parent 8dc3b46017
commit 4452f9ec48
5 changed files with 40 additions and 43 deletions

View file

@ -213,8 +213,9 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
type_alias(p, m);
}
// test unsafe_extern_block
// test extern_block
// unsafe extern "C" {}
// extern {}
T!['{'] if has_extern => {
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
@ -240,10 +241,11 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// test extern_crate
// extern crate foo;
T![extern] if la == T![crate] => extern_crate(p, m),
T![type] => {
type_alias(p, m);
}
T![use] => use_item::use_(p, m),
T![mod] => mod_item(p, m),
T![type] => type_alias(p, m),
T![struct] => {
// test struct_items
// struct Foo;
@ -256,14 +258,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// }
adt::strukt(p, m);
}
// test pub_macro_def
// pub macro m($:ident) {}
T![macro] => {
macro_def(p, m);
}
IDENT if p.at_contextual_kw("macro_rules") && p.nth(1) == BANG => {
macro_rules(p, m);
}
T![enum] => adt::enum_(p, m),
IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
// test union_items
// union Foo {}
@ -273,17 +268,19 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
// }
adt::union(p, m);
}
T![enum] => adt::enum_(p, m),
T![use] => use_item::use_(p, m),
// test pub_macro_def
// pub macro m($:ident) {}
T![macro] => {
macro_def(p, m);
}
IDENT if p.at_contextual_kw("macro_rules") && p.nth(1) == BANG => {
macro_rules(p, m);
}
T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
T![static] => consts::static_(p, m),
// test extern_block
// extern {}
T![extern] if la == T!['{'] || (la == STRING && p.nth(2) == T!['{']) => {
abi(p);
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
}
_ => return Err(m),
};
Ok(())
@ -292,6 +289,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
fn extern_crate(p: &mut Parser, m: Marker) {
assert!(p.at(T![extern]));
p.bump(T![extern]);
assert!(p.at(T![crate]));
p.bump(T![crate]);

View file

@ -1,9 +1,21 @@
SOURCE_FILE@0..10
EXTERN_BLOCK@0..9
ABI@0..6
EXTERN_KW@0..6 "extern"
SOURCE_FILE@0..31
EXTERN_BLOCK@0..20
UNSAFE_KW@0..6 "unsafe"
WHITESPACE@6..7 " "
EXTERN_ITEM_LIST@7..9
L_CURLY@7..8 "{"
R_CURLY@8..9 "}"
WHITESPACE@9..10 "\n"
ABI@7..17
EXTERN_KW@7..13 "extern"
WHITESPACE@13..14 " "
STRING@14..17 "\"C\""
WHITESPACE@17..18 " "
EXTERN_ITEM_LIST@18..20
L_CURLY@18..19 "{"
R_CURLY@19..20 "}"
WHITESPACE@20..21 "\n"
EXTERN_BLOCK@21..30
ABI@21..27
EXTERN_KW@21..27 "extern"
WHITESPACE@27..28 " "
EXTERN_ITEM_LIST@28..30
L_CURLY@28..29 "{"
R_CURLY@29..30 "}"
WHITESPACE@30..31 "\n"

View file

@ -1 +1,2 @@
unsafe extern "C" {}
extern {}

View file

@ -1,13 +0,0 @@
SOURCE_FILE@0..21
EXTERN_BLOCK@0..20
UNSAFE_KW@0..6 "unsafe"
WHITESPACE@6..7 " "
ABI@7..17
EXTERN_KW@7..13 "extern"
WHITESPACE@13..14 " "
STRING@14..17 "\"C\""
WHITESPACE@17..18 " "
EXTERN_ITEM_LIST@18..20
L_CURLY@18..19 "{"
R_CURLY@19..20 "}"
WHITESPACE@20..21 "\n"

View file

@ -1 +0,0 @@
unsafe extern "C" {}