diff options
author | Aleksey Kladov <[email protected]> | 2019-02-23 12:29:06 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-23 13:55:18 +0000 |
commit | 71b8a874e7931e2213e3864e1eae90ceb2551fc2 (patch) | |
tree | 2880a8d61481d2e63919c6565af548bce9de826b | |
parent | 10deefd3718b5665b4955908e057a48b7ad61464 (diff) |
flatten tt
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 61 |
1 files 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 @@ | |||
1 | use ra_parser::TokenSource; | 1 | use ra_parser::TokenSource; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, SyntaxNode, TextRange, SyntaxKind, | 3 | AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, |
4 | ast, SyntaxKind::*, TextUnit | 4 | ast, SyntaxKind::*, TextUnit |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -91,22 +91,71 @@ fn convert_tt( | |||
91 | Some(res) | 91 | Some(res) |
92 | } | 92 | } |
93 | 93 | ||
94 | struct TtTokenSource; | 94 | struct TtTokenSource { |
95 | tokens: Vec<Tok>, | ||
96 | } | ||
97 | |||
98 | struct Tok { | ||
99 | kind: SyntaxKind, | ||
100 | is_joint_to_next: bool, | ||
101 | text: Option<SmolStr>, | ||
102 | } | ||
95 | 103 | ||
96 | impl TtTokenSource { | 104 | impl TtTokenSource { |
97 | fn new(tt: &tt::Subtree) -> TtTokenSource { | 105 | fn new(tt: &tt::Subtree) -> TtTokenSource { |
98 | unimplemented!() | 106 | let mut res = TtTokenSource { tokens: Vec::new() }; |
107 | res.convert_subtree(tt); | ||
108 | res | ||
109 | } | ||
110 | fn convert_subtree(&mut self, sub: &tt::Subtree) { | ||
111 | self.push_delim(sub.delimiter, false); | ||
112 | sub.token_trees.iter().for_each(|tt| self.convert_tt(tt)); | ||
113 | self.push_delim(sub.delimiter, true) | ||
114 | } | ||
115 | fn convert_tt(&mut self, tt: &tt::TokenTree) { | ||
116 | match tt { | ||
117 | tt::TokenTree::Leaf(leaf) => self.convert_leaf(leaf), | ||
118 | tt::TokenTree::Subtree(sub) => self.convert_subtree(sub), | ||
119 | } | ||
120 | } | ||
121 | fn convert_leaf(&mut self, leaf: &tt::Leaf) { | ||
122 | let tok = match leaf { | ||
123 | tt::Leaf::Literal(l) => Tok { | ||
124 | kind: SyntaxKind::INT_NUMBER, // FIXME | ||
125 | is_joint_to_next: false, | ||
126 | text: Some(l.text.clone()), | ||
127 | }, | ||
128 | tt::Leaf::Punct(p) => Tok { | ||
129 | kind: SyntaxKind::from_char(p.char).unwrap(), | ||
130 | is_joint_to_next: p.spacing == tt::Spacing::Joint, | ||
131 | text: None, | ||
132 | }, | ||
133 | tt::Leaf::Ident(ident) => { | ||
134 | Tok { kind: IDENT, is_joint_to_next: false, text: Some(ident.text.clone()) } | ||
135 | } | ||
136 | }; | ||
137 | self.tokens.push(tok) | ||
138 | } | ||
139 | fn push_delim(&mut self, d: tt::Delimiter, closing: bool) { | ||
140 | let kinds = match d { | ||
141 | tt::Delimiter::Parenthesis => [L_PAREN, R_PAREN], | ||
142 | tt::Delimiter::Brace => [L_CURLY, R_CURLY], | ||
143 | tt::Delimiter::Bracket => [L_BRACK, R_BRACK], | ||
144 | tt::Delimiter::None => return, | ||
145 | }; | ||
146 | let tok = Tok { kind: kinds[closing as usize], is_joint_to_next: false, text: None }; | ||
147 | self.tokens.push(tok) | ||
99 | } | 148 | } |
100 | } | 149 | } |
101 | 150 | ||
102 | impl TokenSource for TtTokenSource { | 151 | impl TokenSource for TtTokenSource { |
103 | fn token_kind(&self, pos: usize) -> SyntaxKind { | 152 | fn token_kind(&self, pos: usize) -> SyntaxKind { |
104 | unimplemented!() | 153 | self.tokens[pos].kind |
105 | } | 154 | } |
106 | fn is_token_joint_to_next(&self, pos: usize) -> bool { | 155 | fn is_token_joint_to_next(&self, pos: usize) -> bool { |
107 | unimplemented!() | 156 | self.tokens[pos].is_joint_to_next |
108 | } | 157 | } |
109 | fn is_keyword(&self, pos: usize, kw: &str) -> bool { | 158 | fn is_keyword(&self, pos: usize, kw: &str) -> bool { |
110 | unimplemented!() | 159 | self.tokens[pos].text.as_ref().map(|it| it.as_str()) == Some(kw) |
111 | } | 160 | } |
112 | } | 161 | } |