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/event.rs | 2 +- crates/ra_parser/src/grammar.rs | 42 +++--- crates/ra_parser/src/grammar/attributes.rs | 10 +- crates/ra_parser/src/grammar/expressions.rs | 156 +++++++++++------------ crates/ra_parser/src/grammar/expressions/atom.rs | 144 ++++++++++----------- crates/ra_parser/src/grammar/items.rs | 114 ++++++++--------- 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 ++--- crates/ra_parser/src/grammar/params.rs | 38 +++--- crates/ra_parser/src/grammar/paths.rs | 18 +-- crates/ra_parser/src/grammar/patterns.rs | 78 ++++++------ crates/ra_parser/src/grammar/type_args.rs | 14 +- crates/ra_parser/src/grammar/type_params.rs | 46 +++---- crates/ra_parser/src/grammar/types.rs | 84 ++++++------ crates/ra_parser/src/parser.rs | 25 ++-- 17 files changed, 457 insertions(+), 458 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/event.rs b/crates/ra_parser/src/event.rs index 87cf4eca0..51beb0866 100644 --- a/crates/ra_parser/src/event.rs +++ b/crates/ra_parser/src/event.rs @@ -38,7 +38,7 @@ pub(crate) enum Event { /// The events for it would look like this: /// /// - /// START(PATH) IDENT('foo') FINISH START(PATH) COLONCOLON IDENT('bar') FINISH + /// START(PATH) IDENT('foo') FINISH START(PATH) T![::] IDENT('bar') FINISH /// | /\ /// | | /// +------forward-parent------+ diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index a538ec081..cf603eba1 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -59,7 +59,7 @@ pub(crate) fn macro_stmts(p: &mut Parser) { let m = p.start(); while !p.at(EOF) { - if p.current() == SEMI { + if p.current() == T![;] { p.bump(); continue; } @@ -103,7 +103,7 @@ pub(crate) fn block(p: &mut Parser) { pub(crate) fn meta_item(p: &mut Parser) { fn is_delimiter(p: &mut Parser) -> bool { match p.current() { - L_CURLY | L_PAREN | L_BRACK => true, + T!['{'] | T!['('] | T!['['] => true, _ => false, } } @@ -123,12 +123,12 @@ pub(crate) fn meta_item(p: &mut Parser) { // https://doc.rust-lang.org/reference/paths.html#simple-paths // The start of an meta must be a simple path match p.current() { - IDENT | COLONCOLON | SUPER_KW | SELF_KW | CRATE_KW => p.bump(), - EQ => { + IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump(), + T![=] => { p.bump(); match p.current() { c if c.is_literal() => p.bump(), - TRUE_KW | FALSE_KW => p.bump(), + T![true] | T![false] => p.bump(), _ => {} } break; @@ -158,7 +158,7 @@ pub(crate) fn reparser( MATCH_ARM_LIST => items::match_arm_list, USE_TREE_LIST => items::use_tree_list, EXTERN_ITEM_LIST => items::extern_item_list, - TOKEN_TREE if first_child? == L_CURLY => items::token_tree, + TOKEN_TREE if first_child? == T!['{'] => items::token_tree, ITEM_LIST => match parent? { IMPL_BLOCK => items::impl_item_list, TRAIT_DEF => items::trait_item_list, @@ -184,26 +184,26 @@ impl BlockLike { pub(crate) fn opt_visibility(p: &mut Parser) -> bool { match p.current() { - PUB_KW => { + T![pub] => { let m = p.start(); p.bump(); - if p.at(L_PAREN) { + if p.at(T!['(']) { match p.nth(1) { // test crate_visibility // pub(crate) struct S; // pub(self) struct S; // pub(self) struct S; // pub(self) struct S; - CRATE_KW | SELF_KW | SUPER_KW => { + T![crate] | T![self] | T![super] => { p.bump(); p.bump(); - p.expect(R_PAREN); + p.expect(T![')']); } - IN_KW => { + T![in] => { p.bump(); p.bump(); paths::use_path(p); - p.expect(R_PAREN); + p.expect(T![')']); } _ => (), } @@ -217,7 +217,7 @@ pub(crate) fn opt_visibility(p: &mut Parser) -> bool { // // test crate_keyword_path // fn foo() { crate::foo(); } - CRATE_KW if p.nth(1) != COLONCOLON => { + T![crate] if p.nth(1) != T![::] => { let m = p.start(); p.bump(); m.complete(p, VISIBILITY); @@ -228,10 +228,10 @@ pub(crate) fn opt_visibility(p: &mut Parser) -> bool { } fn opt_alias(p: &mut Parser) { - if p.at(AS_KW) { + if p.at(T![as]) { let m = p.start(); p.bump(); - if !p.eat(UNDERSCORE) { + if !p.eat(T![_]) { name(p); } m.complete(p, ALIAS); @@ -239,7 +239,7 @@ fn opt_alias(p: &mut Parser) { } fn abi(p: &mut Parser) { - assert!(p.at(EXTERN_KW)); + assert!(p.at(T![extern])); let abi = p.start(); p.bump(); match p.current() { @@ -250,7 +250,7 @@ fn abi(p: &mut Parser) { } fn opt_fn_ret_type(p: &mut Parser) -> bool { - if p.at(THIN_ARROW) { + if p.at(T![->]) { let m = p.start(); p.bump(); types::type_(p); @@ -280,21 +280,21 @@ fn name_ref(p: &mut Parser) { let m = p.start(); p.bump(); m.complete(p, NAME_REF); - } else if p.at(SELF_KW) { + } else if p.at(T![self]) { let m = p.start(); p.bump(); - m.complete(p, SELF_KW); + m.complete(p, T![self]); } else { p.err_and_bump("expected identifier"); } } fn error_block(p: &mut Parser, message: &str) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.error(message); p.bump(); expressions::expr_block_contents(p); - p.eat(R_CURLY); + p.eat(T!['}']); m.complete(p, ERROR); } diff --git a/crates/ra_parser/src/grammar/attributes.rs b/crates/ra_parser/src/grammar/attributes.rs index cd30e8a45..20d58445f 100644 --- a/crates/ra_parser/src/grammar/attributes.rs +++ b/crates/ra_parser/src/grammar/attributes.rs @@ -1,28 +1,28 @@ use super::*; pub(super) fn inner_attributes(p: &mut Parser) { - while p.current() == POUND && p.nth(1) == EXCL { + while p.current() == T![#] && p.nth(1) == T![!] { attribute(p, true) } } pub(super) fn outer_attributes(p: &mut Parser) { - while p.at(POUND) { + while p.at(T![#]) { attribute(p, false) } } fn attribute(p: &mut Parser, inner: bool) { let attr = p.start(); - assert!(p.at(POUND)); + assert!(p.at(T![#])); p.bump(); if inner { - assert!(p.at(EXCL)); + assert!(p.at(T![!])); p.bump(); } - if p.at(L_BRACK) { + if p.at(T!['[']) { items::token_tree(p); } else { p.error("expected `[`"); diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 9fe529f53..bb6c78b5f 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -36,14 +36,14 @@ fn expr_no_struct(p: &mut Parser) { // fn c() { 1; 2; } // fn d() { 1; 2 } pub(crate) fn block(p: &mut Parser) { - if !p.at(L_CURLY) { + if !p.at(T!['{']) { p.error("expected a block"); return; } let m = p.start(); p.bump(); expr_block_contents(p); - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, BLOCK); } @@ -65,10 +65,10 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { // #[C] #[D] {} // #[D] return (); // } - let has_attrs = p.at(POUND); + let has_attrs = p.at(T![#]); attributes::outer_attributes(p); - if p.at(LET_KW) { + if p.at(T![let]) { let_stmt(p, m, with_semi); return; } @@ -90,7 +90,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { p.error(format!("attributes are not allowed on {:?}", kind)); } - if p.at(R_CURLY) { + if p.at(T!['}']) { // test attr_on_last_expr_in_block // fn foo() { // { #[A] bar!()? } @@ -121,15 +121,15 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { match with_semi { StmtWithSemi::Yes => { if blocklike.is_block() { - p.eat(SEMI); + p.eat(T![;]); } else { - p.expect(SEMI); + p.expect(T![;]); } } StmtWithSemi::No => {} StmtWithSemi::Optional => { - if p.at(SEMI) { - p.eat(SEMI); + if p.at(T![;]) { + p.eat(T![;]); } } } @@ -145,24 +145,24 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { // let d: i32 = 92; // } fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) { - assert!(p.at(LET_KW)); + assert!(p.at(T![let])); p.bump(); patterns::pattern(p); - if p.at(COLON) { + if p.at(T![:]) { types::ascription(p); } - if p.eat(EQ) { + if p.eat(T![=]) { expressions::expr(p); } match with_semi { StmtWithSemi::Yes => { - p.expect(SEMI); + p.expect(T![;]); } StmtWithSemi::No => {} StmtWithSemi::Optional => { - if p.at(SEMI) { - p.eat(SEMI); + if p.at(T![;]) { + p.eat(T![;]); } } } @@ -174,12 +174,12 @@ pub(crate) fn expr_block_contents(p: &mut Parser) { // This is checked by a validator attributes::inner_attributes(p); - while !p.at(EOF) && !p.at(R_CURLY) { + while !p.at(EOF) && !p.at(T!['}']) { // test nocontentexpr // fn foo(){ // ;;;some_expr();;;;{;;;};;;;Ok(()) // } - if p.current() == SEMI { + if p.current() == T![;] { p.bump(); continue; } @@ -202,41 +202,41 @@ enum Op { fn current_op(p: &Parser) -> (u8, Op) { if let Some(t) = p.current3() { match t { - (L_ANGLE, L_ANGLE, EQ) => return (1, Op::Composite(SHLEQ, 3)), - (R_ANGLE, R_ANGLE, EQ) => return (1, Op::Composite(SHREQ, 3)), + (T![<], T![<], T![=]) => return (1, Op::Composite(T![<<=], 3)), + (T![>], T![>], T![=]) => return (1, Op::Composite(T![>>=], 3)), _ => (), } } if let Some(t) = p.current2() { match t { - (PLUS, EQ) => return (1, Op::Composite(PLUSEQ, 2)), - (MINUS, EQ) => return (1, Op::Composite(MINUSEQ, 2)), - (STAR, EQ) => return (1, Op::Composite(STAREQ, 2)), - (PERCENT, EQ) => return (1, Op::Composite(PERCENTEQ, 2)), - (SLASH, EQ) => return (1, Op::Composite(SLASHEQ, 2)), - (PIPE, EQ) => return (1, Op::Composite(PIPEEQ, 2)), - (AMP, EQ) => return (1, Op::Composite(AMPEQ, 2)), - (CARET, EQ) => return (1, Op::Composite(CARETEQ, 2)), - (PIPE, PIPE) => return (3, Op::Composite(PIPEPIPE, 2)), - (AMP, AMP) => return (4, Op::Composite(AMPAMP, 2)), - (L_ANGLE, EQ) => return (5, Op::Composite(LTEQ, 2)), - (R_ANGLE, EQ) => return (5, Op::Composite(GTEQ, 2)), - (L_ANGLE, L_ANGLE) => return (9, Op::Composite(SHL, 2)), - (R_ANGLE, R_ANGLE) => return (9, Op::Composite(SHR, 2)), + (T![+], T![=]) => return (1, Op::Composite(T![+=], 2)), + (T![-], T![=]) => return (1, Op::Composite(T![-=], 2)), + (T![*], T![=]) => return (1, Op::Composite(T![*=], 2)), + (T![%], T![=]) => return (1, Op::Composite(T![%=], 2)), + (T![/], T![=]) => return (1, Op::Composite(T![/=], 2)), + (T![|], T![=]) => return (1, Op::Composite(T![|=], 2)), + (T![&], T![=]) => return (1, Op::Composite(T![&=], 2)), + (T![^], T![=]) => return (1, Op::Composite(T![^=], 2)), + (T![|], T![|]) => return (3, Op::Composite(T![||], 2)), + (T![&], T![&]) => return (4, Op::Composite(T![&&], 2)), + (T![<], T![=]) => return (5, Op::Composite(T![<=], 2)), + (T![>], T![=]) => return (5, Op::Composite(T![>=], 2)), + (T![<], T![<]) => return (9, Op::Composite(T![<<], 2)), + (T![>], T![>]) => return (9, Op::Composite(T![>>], 2)), _ => (), } } let bp = match p.current() { - EQ => 1, - DOTDOT | DOTDOTEQ => 2, - EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, - PIPE => 6, - CARET => 7, - AMP => 8, - MINUS | PLUS => 10, - STAR | SLASH | PERCENT => 11, + T![=] => 1, + T![..] | T![..=] => 2, + T![==] | T![!=] | T![<] | T![>] => 5, + T![|] => 6, + T![^] => 7, + T![&] => 8, + T![-] | T![+] => 10, + T![*] | T![/] | T![%] => 11, _ => 0, }; (bp, Op::Simple) @@ -284,7 +284,7 @@ fn expr_bp( newly_dollar_open = false; } - let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ; + let is_range = p.current() == T![..] || p.current() == T![..=]; let (op_bp, op) = current_op(p); if op_bp < bp { break; @@ -318,10 +318,10 @@ fn lhs( // let _ = &1; // let _ = &mut &f(); // } - AMP => { + T![&] => { m = p.start(); p.bump(); - p.eat(MUT_KW); + p.eat(T![mut]); REF_EXPR } // test unary_expr @@ -330,14 +330,14 @@ fn lhs( // !!true; // --1; // } - STAR | EXCL | MINUS => { + T![*] | T![!] | T![-] => { m = p.start(); p.bump(); PREFIX_EXPR } // test full_range_expr // fn foo() { xs[..]; } - DOTDOT | DOTDOTEQ => { + T![..] | T![..=] => { m = p.start(); p.bump(); if p.at_ts(EXPR_FIRST) { @@ -375,21 +375,21 @@ fn postfix_expr( // [] => {} // } // } - L_PAREN if allow_calls => call_expr(p, lhs), - L_BRACK if allow_calls => index_expr(p, lhs), - DOT if p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON) => { + T!['('] if allow_calls => call_expr(p, lhs), + T!['['] if allow_calls => index_expr(p, lhs), + T![.] if p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth(2) == T![::]) => { method_call_expr(p, lhs) } - DOT => field_expr(p, lhs), + T![.] => field_expr(p, lhs), // test postfix_range // fn foo() { let x = 1..; } - DOTDOT | DOTDOTEQ if !EXPR_FIRST.contains(p.nth(1)) => { + T![..] | T![..=] if !EXPR_FIRST.contains(p.nth(1)) => { let m = lhs.precede(p); p.bump(); m.complete(p, RANGE_EXPR) } - QUESTION => try_expr(p, lhs), - AS_KW => cast_expr(p, lhs), + T![?] => try_expr(p, lhs), + T![as] => cast_expr(p, lhs), _ => break, }; allow_calls = true @@ -405,7 +405,7 @@ fn postfix_expr( // f(::func()); // } fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = lhs.precede(p); arg_list(p); m.complete(p, CALL_EXPR) @@ -416,11 +416,11 @@ fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // x[1][2]; // } fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(L_BRACK)); + assert!(p.at(T!['['])); let m = lhs.precede(p); p.bump(); expr(p); - p.expect(R_BRACK); + p.expect(T![']']); m.complete(p, INDEX_EXPR) } @@ -430,12 +430,12 @@ fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // y.bar::(1, 2,); // } fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(DOT) && p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON)); + assert!(p.at(T![.]) && p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth(2) == T![::])); let m = lhs.precede(p); p.bump(); name_ref(p); type_args::opt_type_arg_list(p, true); - if p.at(L_PAREN) { + if p.at(T!['(']) { arg_list(p); } m.complete(p, METHOD_CALL_EXPR) @@ -455,7 +455,7 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // x.0x01; // } fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(DOT)); + assert!(p.at(T![.])); let m = lhs.precede(p); p.bump(); if p.at(IDENT) { @@ -463,7 +463,7 @@ fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { } else if p.at(INT_NUMBER) { p.bump(); } else if p.at(FLOAT_NUMBER) { - // FIXME: How to recover and instead parse INT + DOT? + // FIXME: How to recover and instead parse INT + T![.]? p.bump(); } else { p.error("expected field name or number") @@ -476,7 +476,7 @@ fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // x?; // } fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(QUESTION)); + assert!(p.at(T![?])); let m = lhs.precede(p); p.bump(); m.complete(p, TRY_EXPR) @@ -490,7 +490,7 @@ fn try_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { // 0x36 as u8 <= 0x37; // } fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { - assert!(p.at(AS_KW)); + assert!(p.at(T![as])); let m = lhs.precede(p); p.bump(); // Use type_no_bounds(), because cast expressions are not @@ -500,20 +500,20 @@ fn cast_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { } fn arg_list(p: &mut Parser) { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = p.start(); p.bump(); - while !p.at(R_PAREN) && !p.at(EOF) { + while !p.at(T![')']) && !p.at(EOF) { if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); break; } expr(p); - if !p.at(R_PAREN) && !p.expect(COMMA) { + if !p.at(T![')']) && !p.expect(T![,]) { break; } } - p.eat(R_PAREN); + p.eat(T![')']); m.complete(p, ARG_LIST); } @@ -525,15 +525,15 @@ fn arg_list(p: &mut Parser) { // let _ = format!(); // } fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) { - assert!(paths::is_path_start(p) || p.at(L_ANGLE)); + assert!(paths::is_path_start(p) || p.at(T![<])); let m = p.start(); paths::expr_path(p); match p.current() { - L_CURLY if !r.forbid_structs => { + T!['{'] if !r.forbid_structs => { named_field_list(p); (m.complete(p, STRUCT_LIT), BlockLike::NotBlock) } - EXCL => { + T![!] => { let block_like = items::macro_call_after_excl(p); (m.complete(p, MACRO_CALL), block_like) } @@ -548,35 +548,35 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) { // S { x, y: 32, ..Default::default() }; // } pub(crate) fn named_field_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!['}']) { match p.current() { // test struct_literal_field_with_attr // fn main() { // S { #[cfg(test)] field: 1 } // } - IDENT | POUND => { + IDENT | T![#] => { let m = p.start(); attributes::outer_attributes(p); name_ref(p); - if p.eat(COLON) { + if p.eat(T![:]) { expr(p); } m.complete(p, NAMED_FIELD); } - DOTDOT => { + T![..] => { p.bump(); expr(p); } - L_CURLY => error_block(p, "expected a field"), + T!['{'] => error_block(p, "expected a field"), _ => p.err_and_bump("expected identifier"), } - 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_LIST); } diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 8dc7e44a9..8b1a1de49 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -60,29 +60,29 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar if let Some(m) = literal(p) { return Some((m, BlockLike::NotBlock)); } - if paths::is_path_start(p) || p.at(L_ANGLE) { + if paths::is_path_start(p) || p.at(T![<]) { return Some(path_expr(p, r)); } let la = p.nth(1); let done = match p.current() { - L_PAREN => tuple_expr(p), - L_BRACK => array_expr(p), - PIPE => lambda_expr(p), - MOVE_KW if la == PIPE => lambda_expr(p), - ASYNC_KW if la == PIPE || (la == MOVE_KW && p.nth(2) == PIPE) => lambda_expr(p), - IF_KW => if_expr(p), + T!['('] => tuple_expr(p), + T!['['] => array_expr(p), + T![|] => lambda_expr(p), + T![move] if la == T![|] => lambda_expr(p), + T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p), + T![if] => if_expr(p), - LOOP_KW => loop_expr(p, None), - FOR_KW => for_expr(p, None), - WHILE_KW => while_expr(p, None), - LIFETIME if la == COLON => { + T![loop] => loop_expr(p, None), + T![for] => for_expr(p, None), + T![while] => while_expr(p, None), + LIFETIME if la == T![:] => { let m = p.start(); label(p); match p.current() { - LOOP_KW => loop_expr(p, Some(m)), - FOR_KW => for_expr(p, Some(m)), - WHILE_KW => while_expr(p, Some(m)), - L_CURLY => block_expr(p, Some(m)), + T![loop] => loop_expr(p, Some(m)), + T![for] => for_expr(p, Some(m)), + T![while] => while_expr(p, Some(m)), + T!['{'] => block_expr(p, Some(m)), _ => { // test_err misplaced_label_err // fn main() { @@ -94,22 +94,22 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar } } } - ASYNC_KW if la == L_CURLY || (la == MOVE_KW && p.nth(2) == L_CURLY) => { + T![async] if la == T!['{'] || (la == T![move] && p.nth(2) == T!['{']) => { let m = p.start(); p.bump(); - p.eat(MOVE_KW); + p.eat(T![move]); block_expr(p, Some(m)) } - MATCH_KW => match_expr(p), - UNSAFE_KW if la == L_CURLY => { + T![match] => match_expr(p), + T![unsafe] if la == T!['{'] => { let m = p.start(); p.bump(); block_expr(p, Some(m)) } - L_CURLY => block_expr(p, None), - RETURN_KW => return_expr(p), - CONTINUE_KW => continue_expr(p), - BREAK_KW => break_expr(p, r), + T!['{'] => block_expr(p, None), + T![return] => return_expr(p), + T![continue] => continue_expr(p), + T![break] => break_expr(p, r), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); return None; @@ -129,25 +129,25 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar // (1,); // } fn tuple_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = p.start(); - p.expect(L_PAREN); + p.expect(T!['(']); let mut saw_comma = false; let mut saw_expr = false; - while !p.at(EOF) && !p.at(R_PAREN) { + while !p.at(EOF) && !p.at(T![')']) { saw_expr = true; if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); break; } expr(p); - if !p.at(R_PAREN) { + if !p.at(T![')']) { saw_comma = true; - p.expect(COMMA); + p.expect(T![,]); } } - p.expect(R_PAREN); + p.expect(T![')']); m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR }) } @@ -159,21 +159,21 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker { // [1; 2]; // } fn array_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(L_BRACK)); + assert!(p.at(T!['['])); let m = p.start(); p.bump(); - if p.eat(R_BRACK) { + if p.eat(T![']']) { return m.complete(p, ARRAY_EXPR); } expr(p); - if p.eat(SEMI) { + if p.eat(T![;]) { expr(p); - p.expect(R_BRACK); + p.expect(T![']']); return m.complete(p, ARRAY_EXPR); } - while !p.at(EOF) && !p.at(R_BRACK) { - p.expect(COMMA); - if p.at(R_BRACK) { + while !p.at(EOF) && !p.at(T![']']) { + p.expect(T![,]); + if p.at(T![']']) { break; } if !p.at_ts(EXPR_FIRST) { @@ -182,7 +182,7 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { } expr(p); } - p.expect(R_BRACK); + p.expect(T![']']); m.complete(p, ARRAY_EXPR) } @@ -198,17 +198,17 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { // } fn lambda_expr(p: &mut Parser) -> CompletedMarker { assert!( - p.at(PIPE) - || (p.at(MOVE_KW) && p.nth(1) == PIPE) - || (p.at(ASYNC_KW) && p.nth(1) == PIPE) - || (p.at(ASYNC_KW) && p.nth(1) == MOVE_KW && p.nth(2) == PIPE) + p.at(T![|]) + || (p.at(T![move]) && p.nth(1) == T![|]) + || (p.at(T![async]) && p.nth(1) == T![|]) + || (p.at(T![async]) && p.nth(1) == T![move] && p.nth(2) == T![|]) ); let m = p.start(); - p.eat(ASYNC_KW); - p.eat(MOVE_KW); + p.eat(T![async]); + p.eat(T![move]); params::param_list_opt_types(p); if opt_fn_ret_type(p) { - if !p.at(L_CURLY) { + if !p.at(T!['{']) { p.error("expected `{`"); } } @@ -224,14 +224,14 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { // if S {}; // } fn if_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(IF_KW)); + assert!(p.at(T![if])); let m = p.start(); p.bump(); cond(p); block(p); - if p.at(ELSE_KW) { + if p.at(T![else]) { p.bump(); - if p.at(IF_KW) { + if p.at(T![if]) { if_expr(p); } else { block(p); @@ -247,7 +247,7 @@ fn if_expr(p: &mut Parser) -> CompletedMarker { // 'c: for x in () {} // } fn label(p: &mut Parser) { - assert!(p.at(LIFETIME) && p.nth(1) == COLON); + assert!(p.at(LIFETIME) && p.nth(1) == T![:]); let m = p.start(); p.bump(); p.bump(); @@ -259,7 +259,7 @@ fn label(p: &mut Parser) { // loop {}; // } fn loop_expr(p: &mut Parser, m: Option) -> CompletedMarker { - assert!(p.at(LOOP_KW)); + assert!(p.at(T![loop])); let m = m.unwrap_or_else(|| p.start()); p.bump(); block(p); @@ -272,7 +272,7 @@ fn loop_expr(p: &mut Parser, m: Option) -> CompletedMarker { // while let Some(x) = it.next() {}; // } fn while_expr(p: &mut Parser, m: Option) -> CompletedMarker { - assert!(p.at(WHILE_KW)); + assert!(p.at(T![while])); let m = m.unwrap_or_else(|| p.start()); p.bump(); cond(p); @@ -285,11 +285,11 @@ fn while_expr(p: &mut Parser, m: Option) -> CompletedMarker { // for x in [] {}; // } fn for_expr(p: &mut Parser, m: Option) -> CompletedMarker { - assert!(p.at(FOR_KW)); + assert!(p.at(T![for])); let m = m.unwrap_or_else(|| p.start()); p.bump(); patterns::pattern(p); - p.expect(IN_KW); + p.expect(T![in]); expr_no_struct(p); block(p); m.complete(p, FOR_EXPR) @@ -305,9 +305,9 @@ fn for_expr(p: &mut Parser, m: Option) -> CompletedMarker { // } fn cond(p: &mut Parser) { let m = p.start(); - if p.eat(LET_KW) { + if p.eat(T![let]) { patterns::pattern_list(p); - p.expect(EQ); + p.expect(T![=]); } expr_no_struct(p); m.complete(p, CONDITION); @@ -319,11 +319,11 @@ fn cond(p: &mut Parser) { // match S {}; // } fn match_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(MATCH_KW)); + assert!(p.at(T![match])); let m = p.start(); p.bump(); expr_no_struct(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { match_arm_list(p); } else { p.error("expected `{`") @@ -332,9 +332,9 @@ fn match_expr(p: &mut Parser) -> CompletedMarker { } pub(crate) fn match_arm_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); - p.eat(L_CURLY); + p.eat(T!['{']); // test match_arms_inner_attribute // fn foo() { @@ -347,8 +347,8 @@ pub(crate) fn match_arm_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 match arm"); continue; } @@ -362,12 +362,12 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // } // } if match_arm(p).is_block() { - p.eat(COMMA); - } else if !p.at(R_CURLY) { - p.expect(COMMA); + p.eat(T![,]); + } else if !p.at(T!['}']) { + p.expect(T![,]); } } - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, MATCH_ARM_LIST); } @@ -399,10 +399,10 @@ fn match_arm(p: &mut Parser) -> BlockLike { attributes::outer_attributes(p); patterns::pattern_list_r(p, TokenSet::empty()); - if p.at(IF_KW) { + if p.at(T![if]) { match_guard(p); } - p.expect(FAT_ARROW); + p.expect(T![=>]); let blocklike = expr_stmt(p).1; m.complete(p, MATCH_ARM); blocklike @@ -415,7 +415,7 @@ fn match_arm(p: &mut Parser) -> BlockLike { // } // } fn match_guard(p: &mut Parser) -> CompletedMarker { - assert!(p.at(IF_KW)); + assert!(p.at(T![if])); let m = p.start(); p.bump(); expr(p); @@ -429,7 +429,7 @@ fn match_guard(p: &mut Parser) -> CompletedMarker { // 'label: {}; // } fn block_expr(p: &mut Parser, m: Option) -> CompletedMarker { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = m.unwrap_or_else(|| p.start()); block(p); m.complete(p, BLOCK_EXPR) @@ -441,7 +441,7 @@ fn block_expr(p: &mut Parser, m: Option) -> CompletedMarker { // return 92; // } fn return_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(RETURN_KW)); + assert!(p.at(T![return])); let m = p.start(); p.bump(); if p.at_ts(EXPR_FIRST) { @@ -458,7 +458,7 @@ fn return_expr(p: &mut Parser) -> CompletedMarker { // } // } fn continue_expr(p: &mut Parser) -> CompletedMarker { - assert!(p.at(CONTINUE_KW)); + assert!(p.at(T![continue])); let m = p.start(); p.bump(); p.eat(LIFETIME); @@ -475,7 +475,7 @@ fn continue_expr(p: &mut Parser) -> CompletedMarker { // } // } fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { - assert!(p.at(BREAK_KW)); + assert!(p.at(T![break])); let m = p.start(); p.bump(); p.eat(LIFETIME); @@ -486,7 +486,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // for i in break {} // match break {} // } - if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(L_CURLY)) { + if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(T!['{'])) { expr(p); } m.complete(p, BREAK_EXPR) diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 97f8122a9..6728e395f 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -19,7 +19,7 @@ use super::*; // struct S; pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attributes(p); - while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { + while !p.at(EOF) && !(stop_on_r_curly && p.at(T!['}'])) { item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) } } @@ -45,20 +45,20 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF match macro_call(p) { BlockLike::Block => (), BlockLike::NotBlock => { - p.expect(SEMI); + p.expect(T![;]); } } m.complete(p, MACRO_CALL); } else { m.abandon(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { error_block(p, "expected an item"); - } else if p.at(R_CURLY) && !stop_on_r_curly { + } else if p.at(T!['}']) && !stop_on_r_curly { let e = p.start(); p.error("unmatched `}`"); p.bump(); e.complete(p, ERROR); - } else if !p.at(EOF) && !p.at(R_CURLY) { + } else if !p.at(EOF) && !p.at(T!['}']) { p.err_and_bump("expected an item"); } else { p.error("expected an item"); @@ -79,32 +79,32 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul let mut has_mods = false; // modifiers - has_mods |= p.eat(CONST_KW); + has_mods |= p.eat(T![const]); // test_err unsafe_block_in_mod // fn foo(){} unsafe { } fn bar(){} - if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { - p.eat(UNSAFE_KW); + if p.at(T![unsafe]) && p.nth(1) != T!['{'] { + p.eat(T![unsafe]); has_mods = true; } // test_err async_without_semicolon // fn foo() { let _ = async {} } - if p.at(ASYNC_KW) && p.nth(1) != L_CURLY && p.nth(1) != MOVE_KW && p.nth(1) != PIPE { - p.eat(ASYNC_KW); + if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] { + p.eat(T![async]); has_mods = true; } - if p.at(EXTERN_KW) { + if p.at(T![extern]) { has_mods = true; abi(p); } - if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW { - p.bump_remap(AUTO_KW); + if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] { + p.bump_remap(T![auto]); has_mods = true; } - if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW { - p.bump_remap(DEFAULT_KW); + if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == T![impl ] { + p.bump_remap(T![default]); has_mods = true; } @@ -135,7 +135,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul // test_err wrong_order_fns // async unsafe fn foo() {} // unsafe const fn bar() {} - FN_KW => { + T![fn] => { fn_def(p, flavor); m.complete(p, FN_DEF); } @@ -148,7 +148,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul // test unsafe_auto_trait // unsafe auto trait T {} - TRAIT_KW => { + T![trait] => { traits::trait_def(p); m.complete(p, TRAIT_DEF); } @@ -161,7 +161,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul // test unsafe_default_impl // unsafe default impl Foo {} - IMPL_KW => { + T![impl ] => { traits::impl_block(p); m.complete(p, IMPL_BLOCK); } @@ -186,10 +186,10 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { match p.current() { // test extern_crate // extern crate foo; - EXTERN_KW if la == CRATE_KW => extern_crate_item(p, m), - TYPE_KW => type_def(p, m), - MOD_KW => mod_item(p, m), - STRUCT_KW => { + T![extern] if la == T![crate] => extern_crate_item(p, m), + T![type] => type_def(p, m), + T![mod] => mod_item(p, m), + T![struct] => { // test struct_items // struct Foo; // struct Foo {} @@ -199,7 +199,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { // a: i32, // b: f32, // } - nominal::struct_def(p, m, STRUCT_KW); + nominal::struct_def(p, m, T![struct]); } IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { // test union_items @@ -208,16 +208,16 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { // a: i32, // b: f32, // } - nominal::struct_def(p, m, UNION_KW); + nominal::struct_def(p, m, T![union]); } - ENUM_KW => nominal::enum_def(p, m), - USE_KW => use_item::use_item(p, m), - CONST_KW if (la == IDENT || la == MUT_KW) => consts::const_def(p, m), - STATIC_KW => consts::static_def(p, m), + T![enum] => nominal::enum_def(p, m), + T![use] => use_item::use_item(p, m), + T![const] if (la == IDENT || la == T![mut]) => consts::const_def(p, m), + T![static] => consts::static_def(p, m), // test extern_block // extern {} - EXTERN_KW - if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => + T![extern] + if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) => { abi(p); extern_item_list(p); @@ -225,7 +225,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { } _ => return Err(m), }; - if p.at(SEMI) { + if p.at(T![;]) { p.err_and_bump( "expected item, found `;`\n\ consider removing this semicolon", @@ -235,27 +235,27 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { } fn extern_crate_item(p: &mut Parser, m: Marker) { - assert!(p.at(EXTERN_KW)); + assert!(p.at(T![extern])); p.bump(); - assert!(p.at(CRATE_KW)); + assert!(p.at(T![crate])); p.bump(); name_ref(p); opt_alias(p); - p.expect(SEMI); + p.expect(T![;]); m.complete(p, EXTERN_CRATE_ITEM); } pub(crate) fn extern_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); mod_contents(p, true); - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, EXTERN_ITEM_LIST); } fn fn_def(p: &mut Parser, flavor: ItemFlavor) { - assert!(p.at(FN_KW)); + assert!(p.at(T![fn])); p.bump(); name_r(p, ITEM_RECOVERY_SET); @@ -263,7 +263,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) { // fn foo(){} type_params::opt_type_param_list(p); - if p.at(L_PAREN) { + if p.at(T!['(']) { match flavor { ItemFlavor::Mod => params::param_list(p), ItemFlavor::Trait => params::param_list_opt_patterns(p), @@ -282,7 +282,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) { // test fn_decl // trait T { fn foo(); } - if p.at(SEMI) { + if p.at(T![;]) { p.bump(); } else { expressions::block(p) @@ -292,7 +292,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) { // test type_item // type Foo = Bar; fn type_def(p: &mut Parser, m: Marker) { - assert!(p.at(TYPE_KW)); + assert!(p.at(T![type])); p.bump(); name(p); @@ -301,7 +301,7 @@ fn type_def(p: &mut Parser, m: Marker) { // type Result = (); type_params::opt_type_param_list(p); - if p.at(COLON) { + if p.at(T![:]) { type_params::bounds(p); } @@ -309,32 +309,32 @@ fn type_def(p: &mut Parser, m: Marker) { // type Foo where Foo: Copy = (); type_params::opt_where_clause(p); - if p.eat(EQ) { + if p.eat(T![=]) { types::type_(p); } - p.expect(SEMI); + p.expect(T![;]); m.complete(p, TYPE_ALIAS_DEF); } pub(crate) fn mod_item(p: &mut Parser, m: Marker) { - assert!(p.at(MOD_KW)); + assert!(p.at(T![mod])); p.bump(); name(p); - if p.at(L_CURLY) { + if p.at(T!['{']) { mod_item_list(p); - } else if !p.eat(SEMI) { + } else if !p.eat(T![;]) { p.error("expected `;` or `{`"); } m.complete(p, MODULE); } pub(crate) fn mod_item_list(p: &mut Parser) { - assert!(p.at(L_CURLY)); + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); mod_contents(p, true); - p.expect(R_CURLY); + p.expect(T!['}']); m.complete(p, ITEM_LIST); } @@ -345,16 +345,16 @@ fn macro_call(p: &mut Parser) -> BlockLike { } pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { - p.expect(EXCL); + p.expect(T![!]); if p.at(IDENT) { name(p); } match p.current() { - L_CURLY => { + T!['{'] => { token_tree(p); BlockLike::Block } - L_PAREN | L_BRACK => { + T!['('] | T!['['] => { token_tree(p); BlockLike::NotBlock } @@ -367,22 +367,22 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { pub(crate) fn token_tree(p: &mut Parser) { let closing_paren_kind = match p.current() { - L_CURLY => R_CURLY, - L_PAREN => R_PAREN, - L_BRACK => R_BRACK, + T!['{'] => T!['}'], + T!['('] => T![')'], + T!['['] => T![']'], _ => unreachable!(), }; let m = p.start(); p.bump(); while !p.at(EOF) && !p.at(closing_paren_kind) { match p.current() { - L_CURLY | L_PAREN | L_BRACK => token_tree(p), - R_CURLY => { + T!['{'] | T!['('] | T!['['] => token_tree(p), + T!['}'] => { p.error("unmatched `}`"); m.complete(p, TOKEN_TREE); return; } - R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"), + T![')'] | T![']'] => p.err_and_bump("unmatched brace"), _ => p.bump_raw(), } } 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); } diff --git a/crates/ra_parser/src/grammar/params.rs b/crates/ra_parser/src/grammar/params.rs index 3d3bd4cc1..723b56343 100644 --- a/crates/ra_parser/src/grammar/params.rs +++ b/crates/ra_parser/src/grammar/params.rs @@ -36,27 +36,27 @@ impl Flavor { } fn list_(p: &mut Parser, flavor: Flavor) { - let (bra, ket) = if flavor.type_required() { (L_PAREN, R_PAREN) } else { (PIPE, PIPE) }; + let (bra, ket) = if flavor.type_required() { (T!['('], T![')']) } else { (T![|], T![|]) }; assert!(p.at(bra)); let m = p.start(); p.bump(); if flavor.type_required() { opt_self_param(p); } - while !p.at(EOF) && !p.at(ket) && !(flavor.type_required() && p.at(DOTDOTDOT)) { + while !p.at(EOF) && !p.at(ket) && !(flavor.type_required() && p.at(T![...])) { if !p.at_ts(VALUE_PARAMETER_FIRST) { p.error("expected value parameter"); break; } value_parameter(p, flavor); if !p.at(ket) { - p.expect(COMMA); + p.expect(T![,]); } } // test param_list_vararg // extern "C" { fn printf(format: *const i8, ...) -> i32; } if flavor.type_required() { - p.eat(DOTDOTDOT); + p.eat(T![...]); } p.expect(ket); m.complete(p, PARAM_LIST); @@ -69,7 +69,7 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) { match flavor { Flavor::OptionalType | Flavor::Normal => { patterns::pattern(p); - if p.at(COLON) || flavor.type_required() { + if p.at(T![:]) || flavor.type_required() { types::ascription(p) } } @@ -85,10 +85,10 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) { // trait Foo { // fn bar(_: u64, mut x: i32); // } - if (la0 == IDENT || la0 == UNDERSCORE) && la1 == COLON - || la0 == MUT_KW && la1 == IDENT && la2 == COLON - || la0 == AMP && la1 == IDENT && la2 == COLON - || la0 == AMP && la1 == MUT_KW && la2 == IDENT && la3 == COLON + if (la0 == IDENT || la0 == T![_]) && la1 == T![:] + || la0 == T![mut] && la1 == IDENT && la2 == T![:] + || la0 == T![&] && la1 == IDENT && la2 == T![:] + || la0 == T![&] && la1 == T![mut] && la2 == IDENT && la3 == T![:] { patterns::pattern(p); types::ascription(p); @@ -110,16 +110,16 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) { // } fn opt_self_param(p: &mut Parser) { let m; - if p.at(SELF_KW) || p.at(MUT_KW) && p.nth(1) == SELF_KW { + if p.at(T![self]) || p.at(T![mut]) && p.nth(1) == T![self] { m = p.start(); - p.eat(MUT_KW); - p.eat(SELF_KW); + p.eat(T![mut]); + p.eat(T![self]); // test arb_self_types // impl S { // fn a(self: &Self) {} // fn b(mut self: Box) {} // } - if p.at(COLON) { + if p.at(T![:]) { types::ascription(p); } } else { @@ -127,10 +127,10 @@ fn opt_self_param(p: &mut Parser) { let la2 = p.nth(2); let la3 = p.nth(3); let n_toks = match (p.current(), la1, la2, la3) { - (AMP, SELF_KW, _, _) => 2, - (AMP, MUT_KW, SELF_KW, _) => 3, - (AMP, LIFETIME, SELF_KW, _) => 3, - (AMP, LIFETIME, MUT_KW, SELF_KW) => 4, + (T![&], T![self], _, _) => 2, + (T![&], T![mut], T![self], _) => 3, + (T![&], LIFETIME, T![self], _) => 3, + (T![&], LIFETIME, T![mut], T![self]) => 4, _ => return, }; m = p.start(); @@ -139,7 +139,7 @@ fn opt_self_param(p: &mut Parser) { } } m.complete(p, SELF_PARAM); - if !p.at(R_PAREN) { - p.expect(COMMA); + if !p.at(T![')']) { + p.expect(T![,]); } } diff --git a/crates/ra_parser/src/grammar/paths.rs b/crates/ra_parser/src/grammar/paths.rs index 33a11886c..3537b0da1 100644 --- a/crates/ra_parser/src/grammar/paths.rs +++ b/crates/ra_parser/src/grammar/paths.rs @@ -5,7 +5,7 @@ pub(super) const PATH_FIRST: TokenSet = pub(super) fn is_path_start(p: &Parser) -> bool { match p.current() { - IDENT | SELF_KW | SUPER_KW | CRATE_KW | COLONCOLON => true, + IDENT | T![self] | T![super] | T![crate] | T![::] => true, _ => false, } } @@ -35,10 +35,10 @@ fn path(p: &mut Parser, mode: Mode) { let mut qual = path.complete(p, PATH); loop { let use_tree = match p.nth(1) { - STAR | L_CURLY => true, + T![*] | T!['{'] => true, _ => false, }; - if p.at(COLONCOLON) && !use_tree { + if p.at(T![::]) && !use_tree { let path = qual.precede(p); p.bump(); path_segment(p, mode, false); @@ -55,19 +55,19 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) { // test qual_paths // type X = ::Output; // fn foo() { ::default(); } - if first && p.eat(L_ANGLE) { + if first && p.eat(T![<]) { types::type_(p); - if p.eat(AS_KW) { + if p.eat(T![as]) { if is_path_start(p) { types::path_type(p); } else { p.error("expected a trait"); } } - p.expect(R_ANGLE); + p.expect(T![>]); } else { if first { - p.eat(COLONCOLON); + p.eat(T![::]); } match p.current() { IDENT => { @@ -76,7 +76,7 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) { } // test crate_path // use crate::foo; - SELF_KW | SUPER_KW | CRATE_KW => p.bump(), + T![self] | T![super] | T![crate] => p.bump(), _ => { p.err_recover("expected identifier", items::ITEM_RECOVERY_SET); } @@ -91,7 +91,7 @@ fn opt_path_type_args(p: &mut Parser, mode: Mode) { Mode::Type => { // test path_fn_trait_args // type F = Box ()>; - if p.at(L_PAREN) { + if p.at(T!['(']) { params::param_list_opt_patterns(p); opt_fn_ret_type(p); } else { diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 12dd22bde..16ae9da63 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs @@ -16,10 +16,10 @@ pub(super) fn pattern_list(p: &mut Parser) { /// Parses a pattern list separated by pipes `|` /// using the given `recovery_set` pub(super) fn pattern_list_r(p: &mut Parser, recovery_set: TokenSet) { - p.eat(PIPE); + p.eat(T![|]); pattern_r(p, recovery_set); - while p.eat(PIPE) { + while p.eat(T![|]) { pattern_r(p, recovery_set); } } @@ -34,7 +34,7 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { // 200 .. 301=> (), // } // } - if p.at(DOTDOTDOT) || p.at(DOTDOTEQ) || p.at(DOTDOT) { + if p.at(T![...]) || p.at(T![..=]) || p.at(T![..]) { let m = lhs.precede(p); p.bump(); atom_pat(p, recovery_set); @@ -44,7 +44,7 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { // fn main() { // let m!(x) = 0; // } - else if lhs.kind() == PATH_PAT && p.at(EXCL) { + else if lhs.kind() == PATH_PAT && p.at(T![!]) { let m = lhs.precede(p); items::macro_call_after_excl(p); m.complete(p, MACRO_CALL); @@ -58,9 +58,9 @@ const PAT_RECOVERY_SET: TokenSet = fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { let la0 = p.nth(0); let la1 = p.nth(1); - if la0 == REF_KW - || la0 == MUT_KW - || (la0 == IDENT && !(la1 == COLONCOLON || la1 == L_PAREN || la1 == L_CURLY || la1 == EXCL)) + if la0 == T![ref] + || la0 == T![mut] + || (la0 == IDENT && !(la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!])) { return Some(bind_pat(p, true)); } @@ -73,10 +73,10 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { } let m = match la0 { - UNDERSCORE => placeholder_pat(p), - AMP => ref_pat(p), - L_PAREN => tuple_pat(p), - L_BRACK => slice_pat(p), + T![_] => placeholder_pat(p), + T![&] => ref_pat(p), + T!['('] => tuple_pat(p), + T!['['] => slice_pat(p), _ => { p.err_recover("expected pattern", recovery_set); return None; @@ -86,7 +86,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { } fn is_literal_pat_start(p: &mut Parser) -> bool { - p.at(MINUS) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) + p.at(T![-]) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) || p.at_ts(expressions::LITERAL_FIRST) } @@ -102,7 +102,7 @@ fn is_literal_pat_start(p: &mut Parser) -> bool { fn literal_pat(p: &mut Parser) -> CompletedMarker { assert!(is_literal_pat_start(p)); let m = p.start(); - if p.at(MINUS) { + if p.at(T![-]) { p.bump(); } expressions::literal(p); @@ -121,11 +121,11 @@ fn path_pat(p: &mut Parser) -> CompletedMarker { let m = p.start(); paths::expr_path(p); let kind = match p.current() { - L_PAREN => { + T!['('] => { tuple_pat_fields(p); TUPLE_STRUCT_PAT } - L_CURLY => { + T!['{'] => { field_pat_list(p); STRUCT_PAT } @@ -142,10 +142,10 @@ fn path_pat(p: &mut Parser) -> CompletedMarker { // let S(_, .. , x) = (); // } fn tuple_pat_fields(p: &mut Parser) { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); p.bump(); - pat_list(p, R_PAREN); - p.expect(R_PAREN); + pat_list(p, T![')']); + p.expect(T![')']); } // test field_pat_list @@ -156,29 +156,29 @@ fn tuple_pat_fields(p: &mut Parser) { // let S { h: _, } = (); // } fn field_pat_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!['}']) { match p.current() { - DOTDOT => p.bump(), - IDENT if p.nth(1) == COLON => field_pat(p), - L_CURLY => error_block(p, "expected ident"), + T![..] => p.bump(), + IDENT if p.nth(1) == T![:] => field_pat(p), + T!['{'] => error_block(p, "expected ident"), _ => { bind_pat(p, false); } } - 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, FIELD_PAT_LIST); } fn field_pat(p: &mut Parser) { assert!(p.at(IDENT)); - assert!(p.nth(1) == COLON); + assert!(p.nth(1) == T![:]); let m = p.start(); name(p); @@ -190,7 +190,7 @@ fn field_pat(p: &mut Parser) { // test placeholder_pat // fn main() { let _ = (); } fn placeholder_pat(p: &mut Parser) -> CompletedMarker { - assert!(p.at(UNDERSCORE)); + assert!(p.at(T![_])); let m = p.start(); p.bump(); m.complete(p, PLACEHOLDER_PAT) @@ -202,10 +202,10 @@ fn placeholder_pat(p: &mut Parser) -> CompletedMarker { // let &mut b = (); // } fn ref_pat(p: &mut Parser) -> CompletedMarker { - assert!(p.at(AMP)); + assert!(p.at(T![&])); let m = p.start(); p.bump(); - p.eat(MUT_KW); + p.eat(T![mut]); pattern(p); m.complete(p, REF_PAT) } @@ -215,7 +215,7 @@ fn ref_pat(p: &mut Parser) -> CompletedMarker { // let (a, b, ..) = (); // } fn tuple_pat(p: &mut Parser) -> CompletedMarker { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = p.start(); tuple_pat_fields(p); m.complete(p, TUPLE_PAT) @@ -226,18 +226,18 @@ fn tuple_pat(p: &mut Parser) -> CompletedMarker { // let [a, b, ..] = []; // } fn slice_pat(p: &mut Parser) -> CompletedMarker { - assert!(p.at(L_BRACK)); + assert!(p.at(T!['['])); let m = p.start(); p.bump(); - pat_list(p, R_BRACK); - p.expect(R_BRACK); + pat_list(p, T![']']); + p.expect(T![']']); m.complete(p, SLICE_PAT) } fn pat_list(p: &mut Parser, ket: SyntaxKind) { while !p.at(EOF) && !p.at(ket) { match p.current() { - DOTDOT => p.bump(), + T![..] => p.bump(), _ => { if !p.at_ts(PATTERN_FIRST) { p.error("expected a pattern"); @@ -247,7 +247,7 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) { } } if !p.at(ket) { - p.expect(COMMA); + p.expect(T![,]); } } } @@ -263,10 +263,10 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) { // } fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker { let m = p.start(); - p.eat(REF_KW); - p.eat(MUT_KW); + p.eat(T![ref]); + p.eat(T![mut]); name(p); - if with_at && p.eat(AT) { + if with_at && p.eat(T![@]) { pattern(p); } m.complete(p, BIND_PAT) diff --git a/crates/ra_parser/src/grammar/type_args.rs b/crates/ra_parser/src/grammar/type_args.rs index 684976b99..f391b63db 100644 --- a/crates/ra_parser/src/grammar/type_args.rs +++ b/crates/ra_parser/src/grammar/type_args.rs @@ -3,26 +3,26 @@ use super::*; pub(super) fn opt_type_arg_list(p: &mut Parser, colon_colon_required: bool) { let m; match (colon_colon_required, p.nth(0), p.nth(1)) { - (_, COLONCOLON, L_ANGLE) => { + (_, T![::], T![<]) => { m = p.start(); p.bump(); p.bump(); } - (false, L_ANGLE, EQ) => return, - (false, L_ANGLE, _) => { + (false, T![<], T![=]) => return, + (false, T![<], _) => { m = p.start(); p.bump(); } _ => return, }; - while !p.at(EOF) && !p.at(R_ANGLE) { + while !p.at(EOF) && !p.at(T![>]) { type_arg(p); - if !p.at(R_ANGLE) && !p.expect(COMMA) { + if !p.at(T![>]) && !p.expect(T![,]) { break; } } - p.expect(R_ANGLE); + p.expect(T![>]); m.complete(p, TYPE_ARG_LIST); } @@ -35,7 +35,7 @@ fn type_arg(p: &mut Parser) { p.bump(); m.complete(p, LIFETIME_ARG); } - IDENT if p.nth(1) == EQ => { + IDENT if p.nth(1) == T![=] => { name_ref(p); p.bump(); types::type_(p); diff --git a/crates/ra_parser/src/grammar/type_params.rs b/crates/ra_parser/src/grammar/type_params.rs index 07d9b0792..4bbfed780 100644 --- a/crates/ra_parser/src/grammar/type_params.rs +++ b/crates/ra_parser/src/grammar/type_params.rs @@ -1,18 +1,18 @@ use super::*; pub(super) fn opt_type_param_list(p: &mut Parser) { - if !p.at(L_ANGLE) { + if !p.at(T![<]) { return; } type_param_list(p); } fn type_param_list(p: &mut Parser) { - assert!(p.at(L_ANGLE)); + assert!(p.at(T![<])); let m = p.start(); p.bump(); - while !p.at(EOF) && !p.at(R_ANGLE) { + while !p.at(EOF) && !p.at(T![>]) { let m = p.start(); // test generic_lifetime_type_attribute @@ -28,18 +28,18 @@ fn type_param_list(p: &mut Parser) { p.err_and_bump("expected type parameter") } } - if !p.at(R_ANGLE) && !p.expect(COMMA) { + if !p.at(T![>]) && !p.expect(T![,]) { break; } } - p.expect(R_ANGLE); + p.expect(T![>]); m.complete(p, TYPE_PARAM_LIST); } fn lifetime_param(p: &mut Parser, m: Marker) { assert!(p.at(LIFETIME)); p.bump(); - if p.at(COLON) { + if p.at(T![:]) { lifetime_bounds(p); } m.complete(p, LIFETIME_PARAM); @@ -48,12 +48,12 @@ fn lifetime_param(p: &mut Parser, m: Marker) { fn type_param(p: &mut Parser, m: Marker) { assert!(p.at(IDENT)); name(p); - if p.at(COLON) { + if p.at(T![:]) { bounds(p); } // test type_param_default // struct S; - if p.at(EQ) { + if p.at(T![=]) { p.bump(); types::type_(p) } @@ -63,17 +63,17 @@ fn type_param(p: &mut Parser, m: Marker) { // test type_param_bounds // struct S; pub(super) fn bounds(p: &mut Parser) { - assert!(p.at(COLON)); + assert!(p.at(T![:])); p.bump(); bounds_without_colon(p); } fn lifetime_bounds(p: &mut Parser) { - assert!(p.at(COLON)); + assert!(p.at(T![:])); p.bump(); while p.at(LIFETIME) { p.bump(); - if !p.eat(PLUS) { + if !p.eat(T![+]) { break; } } @@ -81,7 +81,7 @@ fn lifetime_bounds(p: &mut Parser) { pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker { while type_bound(p) { - if !p.eat(PLUS) { + if !p.eat(T![+]) { break; } } @@ -96,11 +96,11 @@ pub(super) fn bounds_without_colon(p: &mut Parser) { fn type_bound(p: &mut Parser) -> bool { let m = p.start(); - let has_paren = p.eat(L_PAREN); - p.eat(QUESTION); + let has_paren = p.eat(T!['(']); + p.eat(T![?]); match p.current() { LIFETIME => p.bump(), - FOR_KW => types::for_type(p), + T![for] => types::for_type(p), _ if paths::is_path_start(p) => types::path_type_(p, false), _ => { m.abandon(p); @@ -108,7 +108,7 @@ fn type_bound(p: &mut Parser) -> bool { } } if has_paren { - p.expect(R_PAREN); + p.expect(T![')']); } m.complete(p, TYPE_BOUND); @@ -124,7 +124,7 @@ fn type_bound(p: &mut Parser) -> bool { // ::Item: 'a // {} pub(super) fn opt_where_clause(p: &mut Parser) { - if !p.at(WHERE_KW) { + if !p.at(T![where]) { return; } let m = p.start(); @@ -133,7 +133,7 @@ pub(super) fn opt_where_clause(p: &mut Parser) { while is_where_predicate(p) { where_predicate(p); - let comma = p.eat(COMMA); + let comma = p.eat(T![,]); if is_where_clause_end(p) { break; @@ -150,13 +150,13 @@ pub(super) fn opt_where_clause(p: &mut Parser) { fn is_where_predicate(p: &mut Parser) -> bool { match p.current() { LIFETIME => true, - IMPL_KW => false, + T![impl ] => false, token => types::TYPE_FIRST.contains(token), } } fn is_where_clause_end(p: &mut Parser) -> bool { - p.current() == L_CURLY || p.current() == SEMI || p.current() == EQ + p.current() == T!['{'] || p.current() == T![;] || p.current() == T![=] } fn where_predicate(p: &mut Parser) { @@ -164,13 +164,13 @@ fn where_predicate(p: &mut Parser) { match p.current() { LIFETIME => { p.bump(); - if p.at(COLON) { + if p.at(T![:]) { bounds(p); } else { p.error("expected colon"); } } - IMPL_KW => { + T![impl ] => { p.error("expected lifetime or type"); } _ => { @@ -181,7 +181,7 @@ fn where_predicate(p: &mut Parser) { // { } types::type_(p); - if p.at(COLON) { + if p.at(T![:]) { bounds(p); } else { p.error("expected colon"); diff --git a/crates/ra_parser/src/grammar/types.rs b/crates/ra_parser/src/grammar/types.rs index 686c80f3c..438e3ab0e 100644 --- a/crates/ra_parser/src/grammar/types.rs +++ b/crates/ra_parser/src/grammar/types.rs @@ -17,18 +17,18 @@ pub(super) fn type_no_bounds(p: &mut Parser) { fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) { match p.current() { - L_PAREN => paren_or_tuple_type(p), - EXCL => never_type(p), - STAR => pointer_type(p), - L_BRACK => array_or_slice_type(p), - AMP => reference_type(p), - UNDERSCORE => placeholder_type(p), - FN_KW | UNSAFE_KW | EXTERN_KW => fn_pointer_type(p), - FOR_KW => for_type(p), - IMPL_KW => impl_trait_type(p), - DYN_KW => dyn_trait_type(p), + T!['('] => paren_or_tuple_type(p), + T![!] => never_type(p), + T![*] => pointer_type(p), + T!['['] => array_or_slice_type(p), + T![&] => reference_type(p), + T![_] => placeholder_type(p), + T![fn] | T![unsafe] | T![extern] => fn_pointer_type(p), + T![for] => for_type(p), + T![impl ] => impl_trait_type(p), + T![dyn ] => dyn_trait_type(p), // Some path types are not allowed to have bounds (no plus) - L_ANGLE => path_type_(p, allow_bounds), + T![<] => path_type_(p, allow_bounds), _ if paths::is_path_start(p) => path_or_macro_type_(p, allow_bounds), _ => { p.err_recover("expected type", TYPE_RECOVERY_SET); @@ -37,27 +37,27 @@ fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) { } pub(super) fn ascription(p: &mut Parser) { - p.expect(COLON); + p.expect(T![:]); type_(p) } fn paren_or_tuple_type(p: &mut Parser) { - assert!(p.at(L_PAREN)); + assert!(p.at(T!['('])); let m = p.start(); p.bump(); let mut n_types: u32 = 0; let mut trailing_comma: bool = false; - while !p.at(EOF) && !p.at(R_PAREN) { + while !p.at(EOF) && !p.at(T![')']) { n_types += 1; type_(p); - if p.eat(COMMA) { + if p.eat(T![,]) { trailing_comma = true; } else { trailing_comma = false; break; } } - p.expect(R_PAREN); + p.expect(T![')']); let kind = if n_types == 1 && !trailing_comma { // test paren_type @@ -77,14 +77,14 @@ fn paren_or_tuple_type(p: &mut Parser) { // test never_type // type Never = !; fn never_type(p: &mut Parser) { - assert!(p.at(EXCL)); + assert!(p.at(T![!])); let m = p.start(); p.bump(); m.complete(p, NEVER_TYPE); } fn pointer_type(p: &mut Parser) { - assert!(p.at(STAR)); + assert!(p.at(T![*])); let m = p.start(); p.bump(); @@ -92,7 +92,7 @@ fn pointer_type(p: &mut Parser) { // test pointer_type_mut // type M = *mut (); // type C = *mut (); - MUT_KW | CONST_KW => p.bump(), + T![mut] | T![const] => p.bump(), _ => { // test_err pointer_type_no_mutability // type T = *(); @@ -108,7 +108,7 @@ fn pointer_type(p: &mut Parser) { } fn array_or_slice_type(p: &mut Parser) { - assert!(p.at(L_BRACK)); + assert!(p.at(T!['['])); let m = p.start(); p.bump(); @@ -116,17 +116,17 @@ fn array_or_slice_type(p: &mut Parser) { let kind = match p.current() { // test slice_type // type T = [()]; - R_BRACK => { + T![']'] => { p.bump(); SLICE_TYPE } // test array_type // type T = [(); 92]; - SEMI => { + T![;] => { p.bump(); expressions::expr(p); - p.expect(R_BRACK); + p.expect(T![']']); ARRAY_TYPE } // test_err array_type_missing_semi @@ -144,11 +144,11 @@ fn array_or_slice_type(p: &mut Parser) { // type B = &'static (); // type C = &mut (); fn reference_type(p: &mut Parser) { - assert!(p.at(AMP)); + assert!(p.at(T![&])); let m = p.start(); p.bump(); p.eat(LIFETIME); - p.eat(MUT_KW); + p.eat(T![mut]); type_no_bounds(p); m.complete(p, REFERENCE_TYPE); } @@ -156,7 +156,7 @@ fn reference_type(p: &mut Parser) { // test placeholder_type // type Placeholder = _; fn placeholder_type(p: &mut Parser) { - assert!(p.at(UNDERSCORE)); + assert!(p.at(T![_])); let m = p.start(); p.bump(); m.complete(p, PLACEHOLDER_TYPE); @@ -169,18 +169,18 @@ fn placeholder_type(p: &mut Parser) { // type D = extern "C" fn ( u8 , ... ) -> u8; fn fn_pointer_type(p: &mut Parser) { let m = p.start(); - p.eat(UNSAFE_KW); - if p.at(EXTERN_KW) { + p.eat(T![unsafe]); + if p.at(T![extern]) { abi(p); } // test_err fn_pointer_type_missing_fn // type F = unsafe (); - if !p.eat(FN_KW) { + if !p.eat(T![fn]) { m.abandon(p); p.error("expected `fn`"); return; } - if p.at(L_PAREN) { + if p.at(T!['(']) { params::param_list_opt_patterns(p); } else { p.error("expected parameters") @@ -192,9 +192,9 @@ fn fn_pointer_type(p: &mut Parser) { } pub(super) fn for_binder(p: &mut Parser) { - assert!(p.at(FOR_KW)); + assert!(p.at(T![for])); p.bump(); - if p.at(L_ANGLE) { + if p.at(T![<]) { type_params::opt_type_param_list(p); } else { p.error("expected `<`"); @@ -206,12 +206,12 @@ pub(super) fn for_binder(p: &mut Parser) { // fn foo(_t: &T) where for<'a> &'a T: Iterator {} // fn bar(_t: &T) where for<'a> &'a mut T: Iterator {} pub(super) fn for_type(p: &mut Parser) { - assert!(p.at(FOR_KW)); + assert!(p.at(T![for])); let m = p.start(); for_binder(p); match p.current() { - FN_KW | UNSAFE_KW | EXTERN_KW => fn_pointer_type(p), - AMP => reference_type(p), + T![fn] | T![unsafe] | T![extern] => fn_pointer_type(p), + T![&] => reference_type(p), _ if paths::is_path_start(p) => path_type_(p, false), _ => p.error("expected a path"), } @@ -221,7 +221,7 @@ pub(super) fn for_type(p: &mut Parser) { // test impl_trait_type // type A = impl Iterator> + 'a; fn impl_trait_type(p: &mut Parser) { - assert!(p.at(IMPL_KW)); + assert!(p.at(T![impl ])); let m = p.start(); p.bump(); type_params::bounds_without_colon(p); @@ -231,7 +231,7 @@ fn impl_trait_type(p: &mut Parser) { // test dyn_trait_type // type A = dyn Iterator> + 'a; fn dyn_trait_type(p: &mut Parser) { - assert!(p.at(DYN_KW)); + assert!(p.at(T![dyn ])); let m = p.start(); p.bump(); type_params::bounds_without_colon(p); @@ -251,11 +251,11 @@ pub(super) fn path_type(p: &mut Parser) { // type A = foo!(); // type B = crate::foo!(); fn path_or_macro_type_(p: &mut Parser, allow_bounds: bool) { - assert!(paths::is_path_start(p) || p.at(L_ANGLE)); + assert!(paths::is_path_start(p) || p.at(T![<])); let m = p.start(); paths::type_path(p); - let kind = if p.at(EXCL) { + let kind = if p.at(T![!]) { items::macro_call_after_excl(p); MACRO_CALL } else { @@ -270,7 +270,7 @@ fn path_or_macro_type_(p: &mut Parser, allow_bounds: bool) { } pub(super) fn path_type_(p: &mut Parser, allow_bounds: bool) { - assert!(paths::is_path_start(p) || p.at(L_ANGLE)); + assert!(paths::is_path_start(p) || p.at(T![<])); let m = p.start(); paths::type_path(p); @@ -286,7 +286,7 @@ pub(super) fn path_type_(p: &mut Parser, allow_bounds: bool) { /// This turns a parsed PATH_TYPE optionally into a DYN_TRAIT_TYPE /// with a TYPE_BOUND_LIST fn opt_path_type_bounds_as_dyn_trait_type(p: &mut Parser, path_type_marker: CompletedMarker) { - if !p.at(PLUS) { + if !p.at(T![+]) { return; } @@ -298,7 +298,7 @@ fn opt_path_type_bounds_as_dyn_trait_type(p: &mut Parser, path_type_marker: Comp // This gets consumed here so it gets properly set // in the TYPE_BOUND_LIST - p.eat(PLUS); + p.eat(T![+]); // Parse rest of the bounds into the TYPE_BOUND_LIST let m = type_params::bounds_without_colon_m(p, m); diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs index 8eff930db..4434dfb09 100644 --- a/crates/ra_parser/src/parser.rs +++ b/crates/ra_parser/src/parser.rs @@ -6,6 +6,7 @@ use crate::{ SyntaxKind::{self, ERROR, EOF, TOMBSTONE}, TokenSource, ParseError, TokenSet, event::Event, + T }; /// `Parser` struct provides the low-level API for @@ -155,10 +156,10 @@ impl<'t> Parser<'t> { // Handle parser composites match kind { - DOTDOTDOT | DOTDOTEQ => { + T![...] | T![..=] => { self.bump_compound(kind, 3); } - DOTDOT | COLONCOLON | EQEQ | FAT_ARROW | NEQ | THIN_ARROW => { + T![..] | T![::] | T![==] | T![=>] | T![!=] | T![->] => { self.bump_compound(kind, 2); } _ => { @@ -223,7 +224,7 @@ impl<'t> Parser<'t> { /// Create an error node and consume the next token. pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) { - if self.at(SyntaxKind::L_CURLY) || self.at(SyntaxKind::R_CURLY) || self.at_ts(recovery) { + if self.at(T!['{']) || self.at(T!['}']) || self.at_ts(recovery) { self.error(message); } else { let m = self.start(); @@ -253,19 +254,17 @@ impl<'t> Parser<'t> { let jn2 = self.token_source.is_token_joint_to_next(self.token_pos + n + 1); let la3 = self.token_source.token_kind(self.token_pos + n + 2); - use SyntaxKind::*; - match kind { - DOT if jn1 && la2 == DOT && jn2 && la3 == DOT => Some((DOTDOTDOT, 3)), - DOT if jn1 && la2 == DOT && la3 == EQ => Some((DOTDOTEQ, 3)), - DOT if jn1 && la2 == DOT => Some((DOTDOT, 2)), + T![.] if jn1 && la2 == T![.] && jn2 && la3 == T![.] => Some((T![...], 3)), + T![.] if jn1 && la2 == T![.] && la3 == T![=] => Some((T![..=], 3)), + T![.] if jn1 && la2 == T![.] => Some((T![..], 2)), - COLON if jn1 && la2 == COLON => Some((COLONCOLON, 2)), - EQ if jn1 && la2 == EQ => Some((EQEQ, 2)), - EQ if jn1 && la2 == R_ANGLE => Some((FAT_ARROW, 2)), + T![:] if jn1 && la2 == T![:] => Some((T![::], 2)), + T![=] if jn1 && la2 == T![=] => Some((T![==], 2)), + T![=] if jn1 && la2 == T![>] => Some((T![=>], 2)), - EXCL if la2 == EQ => Some((NEQ, 2)), - MINUS if la2 == R_ANGLE => Some((THIN_ARROW, 2)), + T![!] if la2 == T![=] => Some((T![!=], 2)), + T![-] if la2 == T![>] => Some((T![->], 2)), _ => None, } } -- cgit v1.2.3