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_syntax/src/ast/expr_extensions.rs | 77 +++++++++++++++-------------- crates/ra_syntax/src/ast/extensions.rs | 44 ++++++++--------- crates/ra_syntax/src/lib.rs | 9 +--- crates/ra_syntax/src/parsing/lexer.rs | 25 +++++----- crates/ra_syntax/src/parsing/reparsing.rs | 11 +++-- crates/ra_syntax/src/validation.rs | 7 +-- 6 files changed, 86 insertions(+), 87 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 9484c3b9b..17763809d 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -3,7 +3,8 @@ use crate::{ SyntaxToken, SyntaxElement, SmolStr, ast::{self, AstNode, AstChildren, children, child_opt}, - SyntaxKind::* + SyntaxKind::*, + T }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -34,7 +35,7 @@ impl ast::IfExpr { impl ast::RefExpr { pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) } } @@ -51,9 +52,9 @@ pub enum PrefixOp { impl ast::PrefixExpr { pub fn op_kind(&self) -> Option { match self.op_token()?.kind() { - STAR => Some(PrefixOp::Deref), - EXCL => Some(PrefixOp::Not), - MINUS => Some(PrefixOp::Neg), + T![*] => Some(PrefixOp::Deref), + T![!] => Some(PrefixOp::Not), + T![-] => Some(PrefixOp::Neg), _ => None, } } @@ -133,37 +134,37 @@ impl ast::BinExpr { fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { self.syntax().children_with_tokens().filter_map(|it| it.as_token()).find_map(|c| { match c.kind() { - PIPEPIPE => Some((c, BinOp::BooleanOr)), - AMPAMP => Some((c, BinOp::BooleanAnd)), - EQEQ => Some((c, BinOp::EqualityTest)), - NEQ => Some((c, BinOp::NegatedEqualityTest)), - LTEQ => Some((c, BinOp::LesserEqualTest)), - GTEQ => Some((c, BinOp::GreaterEqualTest)), - L_ANGLE => Some((c, BinOp::LesserTest)), - R_ANGLE => Some((c, BinOp::GreaterTest)), - PLUS => Some((c, BinOp::Addition)), - STAR => Some((c, BinOp::Multiplication)), - MINUS => Some((c, BinOp::Subtraction)), - SLASH => Some((c, BinOp::Division)), - PERCENT => Some((c, BinOp::Remainder)), - SHL => Some((c, BinOp::LeftShift)), - SHR => Some((c, BinOp::RightShift)), - CARET => Some((c, BinOp::BitwiseXor)), - PIPE => Some((c, BinOp::BitwiseOr)), - AMP => Some((c, BinOp::BitwiseAnd)), - DOTDOT => Some((c, BinOp::RangeRightOpen)), - DOTDOTEQ => Some((c, BinOp::RangeRightClosed)), - EQ => Some((c, BinOp::Assignment)), - PLUSEQ => Some((c, BinOp::AddAssign)), - SLASHEQ => Some((c, BinOp::DivAssign)), - STAREQ => Some((c, BinOp::MulAssign)), - PERCENTEQ => Some((c, BinOp::RemAssign)), - SHREQ => Some((c, BinOp::ShrAssign)), - SHLEQ => Some((c, BinOp::ShlAssign)), - MINUSEQ => Some((c, BinOp::SubAssign)), - PIPEEQ => Some((c, BinOp::BitOrAssign)), - AMPEQ => Some((c, BinOp::BitAndAssign)), - CARETEQ => Some((c, BinOp::BitXorAssign)), + T![||] => Some((c, BinOp::BooleanOr)), + T![&&] => Some((c, BinOp::BooleanAnd)), + T![==] => Some((c, BinOp::EqualityTest)), + T![!=] => Some((c, BinOp::NegatedEqualityTest)), + T![<=] => Some((c, BinOp::LesserEqualTest)), + T![>=] => Some((c, BinOp::GreaterEqualTest)), + T![<] => Some((c, BinOp::LesserTest)), + T![>] => Some((c, BinOp::GreaterTest)), + T![+] => Some((c, BinOp::Addition)), + T![*] => Some((c, BinOp::Multiplication)), + T![-] => Some((c, BinOp::Subtraction)), + T![/] => Some((c, BinOp::Division)), + T![%] => Some((c, BinOp::Remainder)), + T![<<] => Some((c, BinOp::LeftShift)), + T![>>] => Some((c, BinOp::RightShift)), + T![^] => Some((c, BinOp::BitwiseXor)), + T![|] => Some((c, BinOp::BitwiseOr)), + T![&] => Some((c, BinOp::BitwiseAnd)), + T![..] => Some((c, BinOp::RangeRightOpen)), + T![..=] => Some((c, BinOp::RangeRightClosed)), + T![=] => Some((c, BinOp::Assignment)), + T![+=] => Some((c, BinOp::AddAssign)), + T![/=] => Some((c, BinOp::DivAssign)), + T![*=] => Some((c, BinOp::MulAssign)), + T![%=] => Some((c, BinOp::RemAssign)), + T![>>=] => Some((c, BinOp::ShrAssign)), + T![<<=] => Some((c, BinOp::ShlAssign)), + T![-=] => Some((c, BinOp::SubAssign)), + T![|=] => Some((c, BinOp::BitOrAssign)), + T![&=] => Some((c, BinOp::BitAndAssign)), + T![^=] => Some((c, BinOp::BitXorAssign)), _ => None, } }) @@ -211,7 +212,7 @@ impl ast::ArrayExpr { } fn is_repeat(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == SEMI) + self.syntax().children_with_tokens().any(|it| it.kind() == T![;]) } } @@ -258,7 +259,7 @@ impl ast::Literal { LiteralKind::FloatNumber { suffix: suffix } } STRING | RAW_STRING => LiteralKind::String, - TRUE_KW | FALSE_KW => LiteralKind::Bool, + T![true] | T![false] => LiteralKind::Bool, BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString, CHAR => LiteralKind::Char, BYTE => LiteralKind::Byte, diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index f3466c585..f030e0df8 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -3,7 +3,7 @@ use itertools::Itertools; -use crate::{SmolStr, SyntaxToken, ast::{self, AstNode, children, child_opt}, SyntaxKind::*, SyntaxElement}; +use crate::{SmolStr, SyntaxToken, ast::{self, AstNode, children, child_opt}, SyntaxKind::*, SyntaxElement, T}; use ra_parser::SyntaxKind; impl ast::Name { @@ -32,7 +32,7 @@ impl ast::Attr { Some(prev) => prev, }; - prev.kind() == EXCL + prev.kind() == T![!] } pub fn as_atom(&self) -> Option { @@ -102,9 +102,9 @@ impl ast::PathSegment { PathSegmentKind::Name(name_ref) } else { match self.syntax().first_child_or_token()?.kind() { - SELF_KW => PathSegmentKind::SelfKw, - SUPER_KW => PathSegmentKind::SuperKw, - CRATE_KW => PathSegmentKind::CrateKw, + T![self] => PathSegmentKind::SelfKw, + T![super] => PathSegmentKind::SuperKw, + T![crate] => PathSegmentKind::CrateKw, _ => return None, } }; @@ -113,7 +113,7 @@ impl ast::PathSegment { pub fn has_colon_colon(&self) -> bool { match self.syntax.first_child_or_token().map(|s| s.kind()) { - Some(COLONCOLON) => true, + Some(T![::]) => true, _ => false, } } @@ -129,14 +129,14 @@ impl ast::Module { pub fn has_semi(&self) -> bool { match self.syntax().last_child_or_token() { None => false, - Some(node) => node.kind() == SEMI, + Some(node) => node.kind() == T![;], } } } impl ast::UseTree { pub fn has_star(&self) -> bool { - self.syntax().children_with_tokens().any(|it| it.kind() == STAR) + self.syntax().children_with_tokens().any(|it| it.kind() == T![*]) } } @@ -172,7 +172,7 @@ impl ast::ImplBlock { } pub fn is_negative(&self) -> bool { - self.syntax().children_with_tokens().any(|t| t.kind() == EXCL) + self.syntax().children_with_tokens().any(|t| t.kind() == T![!]) } } @@ -219,7 +219,7 @@ impl ast::FnDef { self.syntax() .last_child_or_token() .and_then(|it| it.as_token()) - .filter(|it| it.kind() == SEMI) + .filter(|it| it.kind() == T![;]) } } @@ -227,7 +227,7 @@ impl ast::LetStmt { pub fn has_semi(&self) -> bool { match self.syntax().last_child_or_token() { None => false, - Some(node) => node.kind() == SEMI, + Some(node) => node.kind() == T![;], } } } @@ -236,7 +236,7 @@ impl ast::ExprStmt { pub fn has_semi(&self) -> bool { match self.syntax().last_child_or_token() { None => false, - Some(node) => node.kind() == SEMI, + Some(node) => node.kind() == T![;], } } } @@ -270,29 +270,29 @@ impl ast::FieldExpr { impl ast::RefPat { pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) } } impl ast::BindPat { pub fn is_mutable(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) } pub fn is_ref(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == REF_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![ref]) } } impl ast::PointerType { pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) } } impl ast::ReferenceType { pub fn is_mut(&self) -> bool { - self.syntax().children_with_tokens().any(|n| n.kind() == MUT_KW) + self.syntax().children_with_tokens().any(|n| n.kind() == T![mut]) } } @@ -311,19 +311,19 @@ impl ast::SelfParam { self.syntax() .children_with_tokens() .filter_map(|it| it.as_token()) - .find(|it| it.kind() == SELF_KW) + .find(|it| it.kind() == T![self]) .expect("invalid tree: self param must have self") } pub fn kind(&self) -> SelfParamKind { - let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == AMP); + let borrowed = self.syntax().children_with_tokens().any(|n| n.kind() == T![&]); if borrowed { // check for a `mut` coming after the & -- `mut &self` != `&mut self` if self .syntax() .children_with_tokens() - .skip_while(|n| n.kind() != AMP) - .any(|n| n.kind() == MUT_KW) + .skip_while(|n| n.kind() != T![&]) + .any(|n| n.kind() == T![mut]) { SelfParamKind::MutRef } else { @@ -355,6 +355,6 @@ impl ast::WherePred { impl ast::TraitDef { pub fn is_auto(&self) -> bool { - self.syntax().children_with_tokens().any(|t| t.kind() == AUTO_KW) + self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) } } diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 65c65d6aa..0ceabc203 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -179,10 +179,7 @@ fn api_walkthrough() { // There's a bunch of traversal methods on `SyntaxNode`: assert_eq!(expr_syntax.parent(), Some(block.syntax())); - assert_eq!( - block.syntax().first_child_or_token().map(|it| it.kind()), - Some(SyntaxKind::L_CURLY) - ); + assert_eq!(block.syntax().first_child_or_token().map(|it| it.kind()), Some(T!['{'])); assert_eq!( expr_syntax.next_sibling_or_token().map(|it| it.kind()), Some(SyntaxKind::WHITESPACE) @@ -191,9 +188,7 @@ fn api_walkthrough() { // As well as some iterator helpers: let f = expr_syntax.ancestors().find_map(ast::FnDef::cast); assert_eq!(f, Some(&*func)); - assert!(expr_syntax - .siblings_with_tokens(Direction::Next) - .any(|it| it.kind() == SyntaxKind::R_CURLY)); + assert!(expr_syntax.siblings_with_tokens(Direction::Next).any(|it| it.kind() == T!['}'])); assert_eq!( expr_syntax.descendants_with_tokens().count(), 8, // 5 tokens `1`, ` `, `+`, ` `, `!` diff --git a/crates/ra_syntax/src/parsing/lexer.rs b/crates/ra_syntax/src/parsing/lexer.rs index a3791b503..6eb96f03d 100644 --- a/crates/ra_syntax/src/parsing/lexer.rs +++ b/crates/ra_syntax/src/parsing/lexer.rs @@ -7,6 +7,7 @@ mod strings; use crate::{ SyntaxKind::{self, *}, TextUnit, + T, }; use self::{ @@ -90,16 +91,16 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind { match c { // Possiblily multi-byte tokens, // but we only produce single byte token now - // DOTDOTDOT, DOTDOT, DOTDOTEQ, DOT - '.' => return DOT, - // COLONCOLON COLON - ':' => return COLON, - // EQEQ FATARROW EQ - '=' => return EQ, - // NEQ EXCL - '!' => return EXCL, - // THIN_ARROW MINUS - '-' => return MINUS, + // T![...], T![..], T![..=], T![.] + '.' => return T![.], + // T![::] T![:] + ':' => return T![:], + // T![==] FATARROW T![=] + '=' => return T![=], + // T![!=] T![!] + '!' => return T![!], + // T![->] T![-] + '-' => return T![-], // If the character is an ident start not followed by another single // quote, then this is a lifetime name: @@ -148,8 +149,8 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind { ptr.bump(); true } - ('_', None) => return UNDERSCORE, - ('_', Some(c)) if !is_ident_continue(c) => return UNDERSCORE, + ('_', None) => return T![_], + ('_', Some(c)) if !is_ident_continue(c) => return T![_], _ => false, }; ptr.bump_while(is_ident_continue); diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index 434f850d1..6de02a15a 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -17,7 +17,8 @@ use crate::{ text_token_source::TextTokenSource, text_tree_sink::TextTreeSink, lexer::{tokenize, Token}, - } + }, + T, }; pub(crate) fn incremental_reparse( @@ -122,16 +123,16 @@ fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxN fn is_balanced(tokens: &[Token]) -> bool { if tokens.is_empty() - || tokens.first().unwrap().kind != L_CURLY - || tokens.last().unwrap().kind != R_CURLY + || tokens.first().unwrap().kind != T!['{'] + || tokens.last().unwrap().kind != T!['}'] { return false; } let mut balance = 0usize; for t in &tokens[1..tokens.len() - 1] { match t.kind { - L_CURLY => balance += 1, - R_CURLY => { + T!['{'] => balance += 1, + T!['}'] => { balance = match balance.checked_sub(1) { Some(b) => b, None => return false, diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 11a1fb4a7..b53900a4b 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -5,9 +5,10 @@ mod field_expr; use crate::{ SourceFile, SyntaxError, AstNode, SyntaxNode, TextUnit, - SyntaxKind::{L_CURLY, R_CURLY, BYTE, BYTE_STRING, STRING, CHAR}, + SyntaxKind::{BYTE, BYTE_STRING, STRING, CHAR}, ast, algo::visit::{visitor_ctx, VisitorCtx}, + T, }; pub(crate) use unescape::EscapeError; @@ -83,8 +84,8 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) { let mut stack = Vec::new(); for node in root.descendants() { match node.kind() { - L_CURLY => stack.push(node), - R_CURLY => { + T!['{'] => stack.push(node), + T!['}'] => { if let Some(pair) = stack.pop() { assert_eq!( node.parent(), -- cgit v1.2.3