From 993abedd77cf23ce2281b6c8e60cab49ab4fa97e Mon Sep 17 00:00:00 2001 From: Sergey Parilin Date: Wed, 15 May 2019 15:35:47 +0300 Subject: apply T! macro where it is possible --- crates/ra_parser/src/grammar/items/consts.rs | 10 ++-- crates/ra_parser/src/grammar/items/nominal.rs | 66 +++++++++++++------------- crates/ra_parser/src/grammar/items/traits.rs | 38 +++++++-------- crates/ra_parser/src/grammar/items/use_item.rs | 30 ++++++------ 4 files changed, 72 insertions(+), 72 deletions(-) (limited to 'crates/ra_parser/src/grammar/items') diff --git a/crates/ra_parser/src/grammar/items/consts.rs b/crates/ra_parser/src/grammar/items/consts.rs index 1f802246f..b4908ebba 100644 --- a/crates/ra_parser/src/grammar/items/consts.rs +++ b/crates/ra_parser/src/grammar/items/consts.rs @@ -1,22 +1,22 @@ use super::*; pub(super) fn static_def(p: &mut Parser, m: Marker) { - const_or_static(p, m, STATIC_KW, STATIC_DEF) + const_or_static(p, m, T![static], STATIC_DEF) } pub(super) fn const_def(p: &mut Parser, m: Marker) { - const_or_static(p, m, CONST_KW, CONST_DEF) + const_or_static(p, m, T![const], CONST_DEF) } fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) { assert!(p.at(kw)); p.bump(); - p.eat(MUT_KW); // FIXME: validator to forbid const mut + p.eat(T![mut]); // FIXME: validator to forbid const mut name(p); types::ascription(p); - if p.eat(EQ) { + if p.eat(T![=]) { expressions::expr(p); } - p.expect(SEMI); + p.expect(T![;]); m.complete(p, def); } diff --git a/crates/ra_parser/src/grammar/items/nominal.rs b/crates/ra_parser/src/grammar/items/nominal.rs index e93bd76b8..bd4edab89 100644 --- a/crates/ra_parser/src/grammar/items/nominal.rs +++ b/crates/ra_parser/src/grammar/items/nominal.rs @@ -1,38 +1,38 @@ use super::*; pub(super) fn struct_def(p: &mut Parser, m: Marker, kind: SyntaxKind) { - assert!(p.at(STRUCT_KW) || p.at_contextual_kw("union")); + assert!(p.at(T![struct]) || p.at_contextual_kw("union")); p.bump_remap(kind); name_r(p, ITEM_RECOVERY_SET); type_params::opt_type_param_list(p); match p.current() { - WHERE_KW => { + T![where] => { type_params::opt_where_clause(p); match p.current() { - SEMI => { + T![;] => { p.bump(); } - L_CURLY => named_field_def_list(p), + T!['{'] => named_field_def_list(p), _ => { //FIXME: special case `(` error message p.error("expected `;` or `{`"); } } } - SEMI if kind == STRUCT_KW => { + T![;] if kind == T![struct] => { p.bump(); } - L_CURLY => named_field_def_list(p), - L_PAREN if kind == STRUCT_KW => { + T!['{'] => named_field_def_list(p), + T!['('] if kind == T![struct] => { pos_field_def_list(p); // test tuple_struct_where // struct Test(T) where T: Clone; // struct Test(T); type_params::opt_where_clause(p); - p.expect(SEMI); + p.expect(T![;]); } - _ if kind == STRUCT_KW => { + _ if kind == T![struct] => { p.error("expected `;`, `{`, or `(`"); } _ => { @@ -43,12 +43,12 @@ pub(super) fn struct_def(p: &mut Parser, m: Marker, kind: SyntaxKind) { } pub(super) fn enum_def(p: &mut Parser, m: Marker) { - assert!(p.at(ENUM_KW)); + assert!(p.at(T![enum])); p.bump(); name_r(p, ITEM_RECOVERY_SET); type_params::opt_type_param_list(p); type_params::opt_where_clause(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { enum_variant_list(p); } else { p.error("expected `{`") @@ -57,11 +57,11 @@ pub(super) fn enum_def(p: &mut Parser, m: Marker) { } pub(crate) fn enum_variant_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - if p.at(L_CURLY) { + while !p.at(EOF) && !p.at(T!['}']) { + if p.at(T!['{']) { error_block(p, "expected enum variant"); continue; } @@ -70,9 +70,9 @@ pub(crate) fn enum_variant_list(p: &mut Parser) { if p.at(IDENT) { name(p); match p.current() { - L_CURLY => named_field_def_list(p), - L_PAREN => pos_field_def_list(p), - EQ => { + T!['{'] => named_field_def_list(p), + T!['('] => pos_field_def_list(p), + T![=] => { p.bump(); expressions::expr(p); } @@ -83,29 +83,29 @@ pub(crate) fn enum_variant_list(p: &mut Parser) { var.abandon(p); p.err_and_bump("expected enum variant"); } - if !p.at(R_CURLY) { - p.expect(COMMA); + if !p.at(T!['}']) { + p.expect(T![,]); } } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, ENUM_VARIANT_LIST); } pub(crate) fn named_field_def_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); - while !p.at(R_CURLY) && !p.at(EOF) { - if p.at(L_CURLY) { + while !p.at(T!['}']) && !p.at(EOF) { + if p.at(T!['{']) { error_block(p, "expected field"); continue; } named_field_def(p); - if !p.at(R_CURLY) { - p.expect(COMMA); + if !p.at(T!['}']) { + p.expect(T![,]); } } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, NAMED_FIELD_DEF_LIST); fn named_field_def(p: &mut Parser) { @@ -119,7 +119,7 @@ pub(crate) fn named_field_def_list(p: &mut Parser) { opt_visibility(p); if p.at(IDENT) { name(p); - p.expect(COLON); + p.expect(T![:]); types::type_(p); m.complete(p, NAMED_FIELD_DEF); } else { @@ -130,12 +130,12 @@ pub(crate) fn named_field_def_list(p: &mut Parser) { } fn pos_field_def_list(p: &mut Parser) { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = p.start(); - if !p.expect(L_PAREN) { + if !p.expect(T!['(']) { return; } - while !p.at(R_PAREN) && !p.at(EOF) { + while !p.at(T![')']) && !p.at(EOF) { let m = p.start(); // test pos_field_attrs // struct S ( @@ -156,10 +156,10 @@ fn pos_field_def_list(p: &mut Parser) { types::type_(p); m.complete(p, POS_FIELD_DEF); - if !p.at(R_PAREN) { - p.expect(COMMA); + if !p.at(T![')']) { + p.expect(T![,]); } } - p.expect(R_PAREN); + p.expect(T![')']); m.complete(p, POS_FIELD_DEF_LIST); } diff --git a/crates/ra_parser/src/grammar/items/traits.rs b/crates/ra_parser/src/grammar/items/traits.rs index d03a6be0d..09ab3bfd4 100644 --- a/crates/ra_parser/src/grammar/items/traits.rs +++ b/crates/ra_parser/src/grammar/items/traits.rs @@ -4,15 +4,15 @@ use super::*; // trait T: Hash + Clone where U: Copy {} // trait X: Hash + Clone where U: Copy {} pub(super) fn trait_def(p: &mut Parser) { - assert!(p.at(TRAIT_KW)); + assert!(p.at(T![trait])); p.bump(); name_r(p, ITEM_RECOVERY_SET); type_params::opt_type_param_list(p); - if p.at(COLON) { + if p.at(T![:]) { type_params::bounds(p); } type_params::opt_where_clause(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { trait_item_list(p); } else { p.error("expected `{`"); @@ -27,24 +27,24 @@ pub(super) fn trait_def(p: &mut Parser) { // fn bar(&self); // } pub(crate) fn trait_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { - if p.at(L_CURLY) { + while !p.at(EOF) && !p.at(T!['}']) { + if p.at(T!['{']) { error_block(p, "expected an item"); continue; } item_or_macro(p, true, ItemFlavor::Trait); } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, ITEM_LIST); } // test impl_block // impl Foo {} pub(super) fn impl_block(p: &mut Parser) { - assert!(p.at(IMPL_KW)); + assert!(p.at(T![impl ])); p.bump(); if choose_type_params_over_qpath(p) { type_params::opt_type_param_list(p); @@ -55,13 +55,13 @@ pub(super) fn impl_block(p: &mut Parser) { // test impl_block_neg // impl !Send for X {} - p.eat(EXCL); + p.eat(T![!]); impl_type(p); - if p.eat(FOR_KW) { + if p.eat(T![for]) { impl_type(p); } type_params::opt_where_clause(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { impl_item_list(p); } else { p.error("expected `{`"); @@ -76,7 +76,7 @@ pub(super) fn impl_block(p: &mut Parser) { // fn bar(&self) {} // } pub(crate) fn impl_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); // test impl_inner_attributes @@ -87,14 +87,14 @@ pub(crate) fn impl_item_list(p: &mut Parser) { // } attributes::inner_attributes(p); - while !p.at(EOF) && !p.at(R_CURLY) { - if p.at(L_CURLY) { + while !p.at(EOF) && !p.at(T!['}']) { + if p.at(T!['{']) { error_block(p, "expected an item"); continue; } item_or_macro(p, true, ItemFlavor::Mod); } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, ITEM_LIST); } @@ -114,14 +114,14 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool { // we disambiguate it in favor of generics (`impl ::absolute::Path { ... }`) // because this is what almost always expected in practice, qualified paths in impls // (`impl ::AssocTy { ... }`) aren't even allowed by type checker at the moment. - if !p.at(L_ANGLE) { + if !p.at(T![<]) { return false; } - if p.nth(1) == POUND || p.nth(1) == R_ANGLE { + if p.nth(1) == T![#] || p.nth(1) == T![>] { return true; } (p.nth(1) == LIFETIME || p.nth(1) == IDENT) - && (p.nth(2) == R_ANGLE || p.nth(2) == COMMA || p.nth(2) == COLON || p.nth(2) == EQ) + && (p.nth(2) == T![>] || p.nth(2) == T![,] || p.nth(2) == T![:] || p.nth(2) == T![=]) } // test_err impl_type @@ -130,7 +130,7 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool { // impl impl NotType {} // impl Trait2 for impl NotType {} pub(crate) fn impl_type(p: &mut Parser) { - if p.at(IMPL_KW) { + if p.at(T![impl ]) { p.error("expected trait or type"); return; } diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs index 908493789..c3a0b4410 100644 --- a/crates/ra_parser/src/grammar/items/use_item.rs +++ b/crates/ra_parser/src/grammar/items/use_item.rs @@ -1,10 +1,10 @@ use super::*; pub(super) fn use_item(p: &mut Parser, m: Marker) { - assert!(p.at(USE_KW)); + assert!(p.at(T![use])); p.bump(); use_tree(p); - p.expect(SEMI); + p.expect(T![;]); m.complete(p, USE_ITEM); } @@ -28,8 +28,8 @@ fn use_tree(p: &mut Parser) { // use ::*; // use some::path::{*}; // use some::path::{::*}; - (STAR, _) => p.bump(), - (COLONCOLON, STAR) => { + (T![*], _) => p.bump(), + (T![::], T![*]) => { // Parse `use ::*;`, which imports all from the crate root in Rust 2015 // This is invalid inside a use_tree_list, (e.g. `use some::path::{::*}`) // but still parses and errors later: ('crate root in paths can only be used in start position') @@ -47,8 +47,8 @@ fn use_tree(p: &mut Parser) { // use {path::from::root}; // Rust 2015 // use ::{some::arbritrary::path}; // Rust 2015 // use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig - (L_CURLY, _) | (COLONCOLON, L_CURLY) => { - if p.at(COLONCOLON) { + (T!['{'], _) | (T![::], T!['{']) => { + if p.at(T![::]) { p.bump(); } use_tree_list(p); @@ -68,7 +68,7 @@ fn use_tree(p: &mut Parser) { _ if paths::is_path_start(p) => { paths::use_path(p); match p.current() { - AS_KW => { + T![as] => { // test use_alias // use some::path as some_name; // use some::{ @@ -80,16 +80,16 @@ fn use_tree(p: &mut Parser) { // use Trait as _; opt_alias(p); } - COLONCOLON => { + T![::] => { p.bump(); match p.current() { - STAR => { + T![*] => { p.bump(); } // test use_tree_list_after_path // use crate::{Item}; // use self::{Item}; - L_CURLY => use_tree_list(p), + T!['{'] => use_tree_list(p), _ => { // is this unreachable? p.error("expected `{` or `*`"); @@ -109,15 +109,15 @@ fn use_tree(p: &mut Parser) { } pub(crate) fn use_tree_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); - while !p.at(EOF) && !p.at(R_CURLY) { + while !p.at(EOF) && !p.at(T!['}']) { use_tree(p); - if !p.at(R_CURLY) { - p.expect(COMMA); + if !p.at(T!['}']) { + p.expect(T![,]); } } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, USE_TREE_LIST); } -- cgit v1.2.3