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_mbe/src/subtree_source.rs | 22 +++++++++--------- crates/ra_mbe/src/syntax_bridge.rs | 45 +++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 35 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 3554dc110..c938acf64 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -1,5 +1,5 @@ use ra_parser::{TokenSource}; -use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*}; +use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; use std::cell::{RefCell}; // A Sequece of Token, @@ -284,9 +284,9 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken { let (kinds, texts) = match d { - tt::Delimiter::Parenthesis => ([L_PAREN, R_PAREN], "()"), - tt::Delimiter::Brace => ([L_CURLY, R_CURLY], "{}"), - tt::Delimiter::Bracket => ([L_BRACK, R_BRACK], "[]"), + tt::Delimiter::Parenthesis => ([T!['('], T![')']], "()"), + tt::Delimiter::Brace => ([T!['{'], T!['}']], "{}"), + tt::Delimiter::Bracket => ([T!['['], T![']']], "[]"), tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""), }; @@ -299,8 +299,8 @@ fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken { fn convert_literal(l: &tt::Literal) -> TtToken { let kind = classify_literal(&l.text).map(|tkn| tkn.kind).unwrap_or_else(|| match l.text.as_ref() { - "true" => SyntaxKind::TRUE_KW, - "false" => SyntaxKind::FALSE_KW, + "true" => T![true], + "false" => T![false], _ => panic!("Fail to convert given literal {:#?}", &l), }); @@ -320,11 +320,11 @@ fn convert_ident(ident: &tt::Ident) -> TtToken { fn convert_punct(p: &tt::Punct) -> TtToken { let kind = match p.char { // lexer may produce compound tokens for these ones - '.' => DOT, - ':' => COLON, - '=' => EQ, - '!' => EXCL, - '-' => MINUS, + '.' => T![.], + ':' => T![:], + '=' => T![=], + '!' => T![!], + '-' => T![-], c => SyntaxKind::from_char(c).unwrap(), }; let text = { diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 5e6a6f2a1..d8e344557 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -1,7 +1,7 @@ use ra_parser::{TreeSink, ParseError}; use ra_syntax::{ AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement, - ast, SyntaxKind::*, TextUnit + ast, SyntaxKind::*, TextUnit, T }; use crate::subtree_source::{SubtreeTokenSource, Querier}; @@ -211,9 +211,9 @@ fn convert_tt( let first_child = tt.first_child_or_token()?; let last_child = tt.last_child_or_token()?; let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { - (L_PAREN, R_PAREN) => (tt::Delimiter::Parenthesis, true), - (L_CURLY, R_CURLY) => (tt::Delimiter::Brace, true), - (L_BRACK, R_BRACK) => (tt::Delimiter::Bracket, true), + (T!['('], T![')']) => (tt::Delimiter::Parenthesis, true), + (T!['{'], T!['}']) => (tt::Delimiter::Brace, true), + (T!['['], T![']']) => (tt::Delimiter::Bracket, true), _ => (tt::Delimiter::None, false), }; @@ -248,23 +248,22 @@ fn convert_tt( token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); } else { - let child: tt::TokenTree = if token.kind() == SyntaxKind::TRUE_KW - || token.kind() == SyntaxKind::FALSE_KW - { - tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() - } else if token.kind().is_keyword() - || token.kind() == IDENT - || token.kind() == LIFETIME - { - let relative_range = token.range() - global_offset; - let id = token_map.alloc(relative_range); - let text = token.text().clone(); - tt::Leaf::from(tt::Ident { text, id }).into() - } else if token.kind().is_literal() { - tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() - } else { - return None; - }; + let child: tt::TokenTree = + if token.kind() == T![true] || token.kind() == T![false] { + tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() + } else if token.kind().is_keyword() + || token.kind() == IDENT + || token.kind() == LIFETIME + { + let relative_range = token.range() - global_offset; + let id = token_map.alloc(relative_range); + let text = token.text().clone(); + tt::Leaf::from(tt::Ident { text, id }).into() + } else if token.kind().is_literal() { + tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() + } else { + return None; + }; token_trees.push(child); } } @@ -305,10 +304,8 @@ impl<'a, Q: Querier> TtTreeSink<'a, Q> { } fn is_delimiter(kind: SyntaxKind) -> bool { - use SyntaxKind::*; - match kind { - L_PAREN | L_BRACK | L_CURLY | R_PAREN | R_BRACK | R_CURLY => true, + T!['('] | T!['['] | T!['{'] | T![')'] | T![']'] | T!['}'] => true, _ => false, } } -- cgit v1.2.3