SSR: Use T! instead of SyntaxKind::* where possible

This commit is contained in:
David Lattimore 2020-07-02 09:19:58 +10:00
parent 3d9997889b
commit 83588a1c45

View file

@ -6,7 +6,7 @@
//! e.g. expressions, type references etc. //! e.g. expressions, type references etc.
use crate::{SsrError, SsrPattern, SsrRule}; use crate::{SsrError, SsrPattern, SsrRule};
use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind}; use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use std::str::FromStr; use std::str::FromStr;
@ -161,7 +161,7 @@ fn parse_pattern(pattern_str: &str) -> Result<Vec<PatternElement>, SsrError> {
let mut placeholder_names = FxHashSet::default(); let mut placeholder_names = FxHashSet::default();
let mut tokens = tokenize(pattern_str)?.into_iter(); let mut tokens = tokenize(pattern_str)?.into_iter();
while let Some(token) = tokens.next() { while let Some(token) = tokens.next() {
if token.kind == SyntaxKind::DOLLAR { if token.kind == T![$] {
let placeholder = parse_placeholder(&mut tokens)?; let placeholder = parse_placeholder(&mut tokens)?;
if !placeholder_names.insert(placeholder.ident.clone()) { if !placeholder_names.insert(placeholder.ident.clone()) {
bail!("Name `{}` repeats more than once", placeholder.ident); bail!("Name `{}` repeats more than once", placeholder.ident);
@ -226,7 +226,7 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placehold
SyntaxKind::IDENT => { SyntaxKind::IDENT => {
name = Some(token.text); name = Some(token.text);
} }
SyntaxKind::L_CURLY => { T!['{'] => {
let token = let token =
tokens.next().ok_or_else(|| SsrError::new("Unexpected end of placeholder"))?; tokens.next().ok_or_else(|| SsrError::new("Unexpected end of placeholder"))?;
if token.kind == SyntaxKind::IDENT { if token.kind == SyntaxKind::IDENT {
@ -237,10 +237,10 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placehold
.next() .next()
.ok_or_else(|| SsrError::new("Placeholder is missing closing brace '}'"))?; .ok_or_else(|| SsrError::new("Placeholder is missing closing brace '}'"))?;
match token.kind { match token.kind {
SyntaxKind::COLON => { T![:] => {
constraints.push(parse_constraint(tokens)?); constraints.push(parse_constraint(tokens)?);
} }
SyntaxKind::R_CURLY => break, T!['}'] => break,
_ => bail!("Unexpected token while parsing placeholder: '{}'", token.text), _ => bail!("Unexpected token while parsing placeholder: '{}'", token.text),
} }
} }
@ -330,24 +330,24 @@ mod tests {
result.pattern.raw.tokens, result.pattern.raw.tokens,
vec![ vec![
token(SyntaxKind::IDENT, "foo"), token(SyntaxKind::IDENT, "foo"),
token(SyntaxKind::L_PAREN, "("), token(T!['('], "("),
placeholder("a"), placeholder("a"),
token(SyntaxKind::COMMA, ","), token(T![,], ","),
token(SyntaxKind::WHITESPACE, " "), token(SyntaxKind::WHITESPACE, " "),
placeholder("b"), placeholder("b"),
token(SyntaxKind::R_PAREN, ")"), token(T![')'], ")"),
] ]
); );
assert_eq!( assert_eq!(
result.template.tokens, result.template.tokens,
vec![ vec![
token(SyntaxKind::IDENT, "bar"), token(SyntaxKind::IDENT, "bar"),
token(SyntaxKind::L_PAREN, "("), token(T!['('], "("),
placeholder("b"), placeholder("b"),
token(SyntaxKind::COMMA, ","), token(T![,], ","),
token(SyntaxKind::WHITESPACE, " "), token(SyntaxKind::WHITESPACE, " "),
placeholder("a"), placeholder("a"),
token(SyntaxKind::R_PAREN, ")"), token(T![')'], ")"),
] ]
); );
} }