Unnamed repository; edit this file 'description' to name the repository.
internal: parser cleanups
| -rw-r--r-- | crates/parser/src/grammar/attributes.rs | 40 | ||||
| -rw-r--r-- | crates/parser/src/grammar/expressions.rs | 2 | ||||
| -rw-r--r-- | crates/parser/src/grammar/items.rs | 31 |
3 files changed, 36 insertions, 37 deletions
diff --git a/crates/parser/src/grammar/attributes.rs b/crates/parser/src/grammar/attributes.rs index a44c5e4841..80d7b09b3e 100644 --- a/crates/parser/src/grammar/attributes.rs +++ b/crates/parser/src/grammar/attributes.rs @@ -12,31 +12,13 @@ pub(super) fn outer_attrs(p: &mut Parser) { } } -pub(super) fn meta(p: &mut Parser) { - let meta = p.start(); - paths::use_path(p); - - match p.current() { - T![=] => { - p.bump(T![=]); - if expressions::expr(p).0.is_none() { - p.error("expected expression"); - } - } - T!['('] | T!['['] | T!['{'] => items::token_tree(p), - _ => {} - } - - meta.complete(p, META); -} - fn attr(p: &mut Parser, inner: bool) { - let attr = p.start(); assert!(p.at(T![#])); + + let attr = p.start(); p.bump(T![#]); if inner { - assert!(p.at(T![!])); p.bump(T![!]); } @@ -51,3 +33,21 @@ fn attr(p: &mut Parser, inner: bool) { } attr.complete(p, ATTR); } + +pub(super) fn meta(p: &mut Parser) { + let meta = p.start(); + paths::use_path(p); + + match p.current() { + T![=] => { + p.bump(T![=]); + if expressions::expr(p).0.is_none() { + p.error("expected expression"); + } + } + T!['('] | T!['['] | T!['{'] => items::token_tree(p), + _ => {} + } + + meta.complete(p, META); +} diff --git a/crates/parser/src/grammar/expressions.rs b/crates/parser/src/grammar/expressions.rs index 001be099e6..12e62cddbc 100644 --- a/crates/parser/src/grammar/expressions.rs +++ b/crates/parser/src/grammar/expressions.rs @@ -71,7 +71,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { // test block_items // fn a() { fn b() {} } - let m = match items::maybe_item(p, m) { + let m = match items::opt_item(p, m) { Ok(()) => return, Err(m) => m, }; diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 3421078bba..c3ccf6d126 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -44,7 +44,8 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = TokenSet::new(&[ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { let m = p.start(); attributes::outer_attrs(p); - let m = match maybe_item(p, m) { + + let m = match opt_item(p, m) { Ok(()) => { if p.at(T![;]) { p.err_and_bump( @@ -56,6 +57,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { } Err(m) => m, }; + if paths::is_use_path_start(p) { match macro_call(p) { BlockLike::Block => (), @@ -64,30 +66,30 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { } } m.complete(p, MACRO_CALL); - } else { - m.abandon(p); - if p.at(T!['{']) { - error_block(p, "expected an item"); - } else if p.at(T!['}']) && !stop_on_r_curly { + return; + } + + m.abandon(p); + match p.current() { + T!['{'] => error_block(p, "expected an item"), + T!['}'] if !stop_on_r_curly => { let e = p.start(); p.error("unmatched `}`"); p.bump(T!['}']); e.complete(p, ERROR); - } else if !p.at(EOF) && !p.at(T!['}']) { - p.err_and_bump("expected an item"); - } else { - p.error("expected an item"); } + EOF | T!['}'] => p.error("expected an item"), + _ => p.err_and_bump("expected an item"), } } /// Try to parse an item, completing `m` in case of success. -pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { +pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { // test_err pub_expr // fn foo() { pub 92; } let has_visibility = opt_visibility(p); - let m = match items_without_modifiers(p, m) { + let m = match opt_item_without_modifiers(p, m) { Ok(()) => return Ok(()), Err(m) => m, }; @@ -235,7 +237,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { Ok(()) } -fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { +fn opt_item_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { let la = p.nth(1); match p.current() { // test extern_crate @@ -287,10 +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]); if p.at(T![self]) { |