From 71b8a874e7931e2213e3864e1eae90ceb2551fc2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Feb 2019 15:29:06 +0300 Subject: flatten tt --- crates/ra_mbe/src/syntax_bridge.rs | 61 ++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index e64ba7ff2..7a4ba9e93 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -1,6 +1,6 @@ use ra_parser::TokenSource; use ra_syntax::{ - AstNode, SyntaxNode, TextRange, SyntaxKind, + AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, ast, SyntaxKind::*, TextUnit }; @@ -91,22 +91,71 @@ fn convert_tt( Some(res) } -struct TtTokenSource; +struct TtTokenSource { + tokens: Vec, +} + +struct Tok { + kind: SyntaxKind, + is_joint_to_next: bool, + text: Option, +} impl TtTokenSource { fn new(tt: &tt::Subtree) -> TtTokenSource { - unimplemented!() + let mut res = TtTokenSource { tokens: Vec::new() }; + res.convert_subtree(tt); + res + } + fn convert_subtree(&mut self, sub: &tt::Subtree) { + self.push_delim(sub.delimiter, false); + sub.token_trees.iter().for_each(|tt| self.convert_tt(tt)); + self.push_delim(sub.delimiter, true) + } + fn convert_tt(&mut self, tt: &tt::TokenTree) { + match tt { + tt::TokenTree::Leaf(leaf) => self.convert_leaf(leaf), + tt::TokenTree::Subtree(sub) => self.convert_subtree(sub), + } + } + fn convert_leaf(&mut self, leaf: &tt::Leaf) { + let tok = match leaf { + tt::Leaf::Literal(l) => Tok { + kind: SyntaxKind::INT_NUMBER, // FIXME + is_joint_to_next: false, + text: Some(l.text.clone()), + }, + tt::Leaf::Punct(p) => Tok { + kind: SyntaxKind::from_char(p.char).unwrap(), + is_joint_to_next: p.spacing == tt::Spacing::Joint, + text: None, + }, + tt::Leaf::Ident(ident) => { + Tok { kind: IDENT, is_joint_to_next: false, text: Some(ident.text.clone()) } + } + }; + self.tokens.push(tok) + } + fn push_delim(&mut self, d: tt::Delimiter, closing: bool) { + let kinds = 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::None => return, + }; + let tok = Tok { kind: kinds[closing as usize], is_joint_to_next: false, text: None }; + self.tokens.push(tok) } } impl TokenSource for TtTokenSource { fn token_kind(&self, pos: usize) -> SyntaxKind { - unimplemented!() + self.tokens[pos].kind } fn is_token_joint_to_next(&self, pos: usize) -> bool { - unimplemented!() + self.tokens[pos].is_joint_to_next } fn is_keyword(&self, pos: usize, kw: &str) -> bool { - unimplemented!() + self.tokens[pos].text.as_ref().map(|it| it.as_str()) == Some(kw) } } -- cgit v1.2.3