diff options
author | Edwin Cheng <[email protected]> | 2019-12-13 13:53:34 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-12-13 13:53:34 +0000 |
commit | 98f98cbb5404385703a404547aa2477d4a2fd1cb (patch) | |
tree | fe84a7b9a434cb268faea247d9d6dbffed894696 /crates/ra_mbe/src | |
parent | 16cf6bcf4b614a7a1a3a3f3659f7bb3df7cff086 (diff) |
Refactor tt::Delimiter
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_expander/matcher.rs | 6 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_expander/transcriber.rs | 10 | ||||
-rw-r--r-- | crates/ra_mbe/src/subtree_source.rs | 10 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 34 | ||||
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 2 |
6 files changed, 32 insertions, 34 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index bbddebe67..0d2d43bef 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -159,14 +159,14 @@ impl Rule { | |||
159 | .expect_subtree() | 159 | .expect_subtree() |
160 | .map_err(|()| ParseError::Expected("expected subtree".to_string()))? | 160 | .map_err(|()| ParseError::Expected("expected subtree".to_string()))? |
161 | .clone(); | 161 | .clone(); |
162 | lhs.delimiter = tt::Delimiter::None; | 162 | lhs.delimiter = None; |
163 | src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?; | 163 | src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?; |
164 | src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?; | 164 | src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?; |
165 | let mut rhs = src | 165 | let mut rhs = src |
166 | .expect_subtree() | 166 | .expect_subtree() |
167 | .map_err(|()| ParseError::Expected("expected subtree".to_string()))? | 167 | .map_err(|()| ParseError::Expected("expected subtree".to_string()))? |
168 | .clone(); | 168 | .clone(); |
169 | rhs.delimiter = tt::Delimiter::None; | 169 | rhs.delimiter = None; |
170 | Ok(crate::Rule { lhs, rhs }) | 170 | Ok(crate::Rule { lhs, rhs }) |
171 | } | 171 | } |
172 | } | 172 | } |
diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs index 33b9d483d..3f5136478 100644 --- a/crates/ra_mbe/src/mbe_expander/matcher.rs +++ b/crates/ra_mbe/src/mbe_expander/matcher.rs | |||
@@ -16,7 +16,7 @@ impl Bindings { | |||
16 | fn push_optional(&mut self, name: &SmolStr) { | 16 | fn push_optional(&mut self, name: &SmolStr) { |
17 | // FIXME: Do we have a better way to represent an empty token ? | 17 | // FIXME: Do we have a better way to represent an empty token ? |
18 | // Insert an empty subtree for empty token | 18 | // Insert an empty subtree for empty token |
19 | let tt = tt::Subtree { delimiter: tt::Delimiter::None, token_trees: vec![] }.into(); | 19 | let tt = tt::Subtree::default().into(); |
20 | self.inner.insert(name.clone(), Binding::Fragment(Fragment::Tokens(tt))); | 20 | self.inner.insert(name.clone(), Binding::Fragment(Fragment::Tokens(tt))); |
21 | } | 21 | } |
22 | 22 | ||
@@ -65,7 +65,7 @@ macro_rules! bail { | |||
65 | } | 65 | } |
66 | 66 | ||
67 | pub(super) fn match_(pattern: &tt::Subtree, src: &tt::Subtree) -> Result<Bindings, ExpandError> { | 67 | pub(super) fn match_(pattern: &tt::Subtree, src: &tt::Subtree) -> Result<Bindings, ExpandError> { |
68 | assert!(pattern.delimiter == tt::Delimiter::None); | 68 | assert!(pattern.delimiter == None); |
69 | 69 | ||
70 | let mut res = Bindings::default(); | 70 | let mut res = Bindings::default(); |
71 | let mut src = TtIter::new(src); | 71 | let mut src = TtIter::new(src); |
@@ -210,7 +210,7 @@ impl<'a> TtIter<'a> { | |||
210 | 0 => Err(()), | 210 | 0 => Err(()), |
211 | 1 => Ok(res[0].clone()), | 211 | 1 => Ok(res[0].clone()), |
212 | _ => Ok(tt::TokenTree::Subtree(tt::Subtree { | 212 | _ => Ok(tt::TokenTree::Subtree(tt::Subtree { |
213 | delimiter: tt::Delimiter::None, | 213 | delimiter: None, |
214 | token_trees: res.into_iter().cloned().collect(), | 214 | token_trees: res.into_iter().cloned().collect(), |
215 | })), | 215 | })), |
216 | } | 216 | } |
diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index ed094d5bb..f7636db11 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs | |||
@@ -50,7 +50,7 @@ pub(super) fn transcribe( | |||
50 | template: &tt::Subtree, | 50 | template: &tt::Subtree, |
51 | bindings: &Bindings, | 51 | bindings: &Bindings, |
52 | ) -> Result<tt::Subtree, ExpandError> { | 52 | ) -> Result<tt::Subtree, ExpandError> { |
53 | assert!(template.delimiter == tt::Delimiter::None); | 53 | assert!(template.delimiter == None); |
54 | let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new(), var_expanded: false }; | 54 | let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new(), var_expanded: false }; |
55 | expand_subtree(&mut ctx, template) | 55 | expand_subtree(&mut ctx, template) |
56 | } | 56 | } |
@@ -106,7 +106,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result<Fragment, ExpandError> | |||
106 | // ``` | 106 | // ``` |
107 | // We just treat it a normal tokens | 107 | // We just treat it a normal tokens |
108 | let tt = tt::Subtree { | 108 | let tt = tt::Subtree { |
109 | delimiter: tt::Delimiter::None, | 109 | delimiter: None, |
110 | token_trees: vec![ | 110 | token_trees: vec![ |
111 | tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone }).into(), | 111 | tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone }).into(), |
112 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) | 112 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) |
@@ -147,7 +147,7 @@ fn expand_repeat( | |||
147 | ctx.var_expanded = false; | 147 | ctx.var_expanded = false; |
148 | 148 | ||
149 | while let Ok(mut t) = expand_subtree(ctx, template) { | 149 | while let Ok(mut t) = expand_subtree(ctx, template) { |
150 | t.delimiter = tt::Delimiter::None; | 150 | t.delimiter = None; |
151 | // if no var expanded in the child, we count it as a fail | 151 | // if no var expanded in the child, we count it as a fail |
152 | if !ctx.var_expanded { | 152 | if !ctx.var_expanded { |
153 | break; | 153 | break; |
@@ -212,7 +212,7 @@ fn expand_repeat( | |||
212 | 212 | ||
213 | // Check if it is a single token subtree without any delimiter | 213 | // Check if it is a single token subtree without any delimiter |
214 | // e.g {Delimiter:None> ['>'] /Delimiter:None>} | 214 | // e.g {Delimiter:None> ['>'] /Delimiter:None>} |
215 | let tt = tt::Subtree { delimiter: tt::Delimiter::None, token_trees: buf }.into(); | 215 | let tt = tt::Subtree { delimiter: None, token_trees: buf }.into(); |
216 | Ok(Fragment::Tokens(tt)) | 216 | Ok(Fragment::Tokens(tt)) |
217 | } | 217 | } |
218 | 218 | ||
@@ -225,7 +225,7 @@ fn push_fragment(buf: &mut Vec<tt::TokenTree>, fragment: Fragment) { | |||
225 | 225 | ||
226 | fn push_subtree(buf: &mut Vec<tt::TokenTree>, tt: tt::Subtree) { | 226 | fn push_subtree(buf: &mut Vec<tt::TokenTree>, tt: tt::Subtree) { |
227 | match tt.delimiter { | 227 | match tt.delimiter { |
228 | tt::Delimiter::None => buf.extend(tt.token_trees), | 228 | None => buf.extend(tt.token_trees), |
229 | _ => buf.push(tt.into()), | 229 | _ => buf.push(tt.into()), |
230 | } | 230 | } |
231 | } | 231 | } |
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 7ef45f6dc..061e9f20b 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs | |||
@@ -114,12 +114,12 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { | |||
114 | } | 114 | } |
115 | } | 115 | } |
116 | 116 | ||
117 | fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken { | 117 | fn convert_delim(d: Option<tt::Delimiter>, closing: bool) -> TtToken { |
118 | let (kinds, texts) = match d { | 118 | let (kinds, texts) = match d { |
119 | tt::Delimiter::Parenthesis => ([T!['('], T![')']], "()"), | 119 | Some(tt::Delimiter::Parenthesis) => ([T!['('], T![')']], "()"), |
120 | tt::Delimiter::Brace => ([T!['{'], T!['}']], "{}"), | 120 | Some(tt::Delimiter::Brace) => ([T!['{'], T!['}']], "{}"), |
121 | tt::Delimiter::Bracket => ([T!['['], T![']']], "[]"), | 121 | Some(tt::Delimiter::Bracket) => ([T!['['], T![']']], "[]"), |
122 | tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""), | 122 | None => ([L_DOLLAR, R_DOLLAR], ""), |
123 | }; | 123 | }; |
124 | 124 | ||
125 | let idx = closing as usize; | 125 | let idx = closing as usize; |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 66c1f0337..b8e2cfc1d 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -51,7 +51,7 @@ pub fn token_tree_to_syntax_node( | |||
51 | ) -> Result<(Parse<SyntaxNode>, TokenMap), ExpandError> { | 51 | ) -> Result<(Parse<SyntaxNode>, TokenMap), ExpandError> { |
52 | let tmp; | 52 | let tmp; |
53 | let tokens = match tt { | 53 | let tokens = match tt { |
54 | tt::Subtree { delimiter: tt::Delimiter::None, token_trees } => token_trees.as_slice(), | 54 | tt::Subtree { delimiter: None, token_trees } => token_trees.as_slice(), |
55 | _ => { | 55 | _ => { |
56 | tmp = [tt.clone().into()]; | 56 | tmp = [tt.clone().into()]; |
57 | &tmp[..] | 57 | &tmp[..] |
@@ -121,7 +121,7 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTr | |||
121 | token_trees.push(mk_punct('!')); | 121 | token_trees.push(mk_punct('!')); |
122 | } | 122 | } |
123 | token_trees.push(tt::TokenTree::from(tt::Subtree { | 123 | token_trees.push(tt::TokenTree::from(tt::Subtree { |
124 | delimiter: tt::Delimiter::Bracket, | 124 | delimiter: Some(tt::Delimiter::Bracket), |
125 | token_trees: meta_tkns, | 125 | token_trees: meta_tkns, |
126 | })); | 126 | })); |
127 | 127 | ||
@@ -156,7 +156,7 @@ impl Convertor { | |||
156 | fn go(&mut self, tt: &SyntaxNode) -> Option<tt::Subtree> { | 156 | fn go(&mut self, tt: &SyntaxNode) -> Option<tt::Subtree> { |
157 | // This tree is empty | 157 | // This tree is empty |
158 | if tt.first_child_or_token().is_none() { | 158 | if tt.first_child_or_token().is_none() { |
159 | return Some(tt::Subtree { token_trees: vec![], delimiter: tt::Delimiter::None }); | 159 | return Some(tt::Subtree { token_trees: vec![], delimiter: None }); |
160 | } | 160 | } |
161 | 161 | ||
162 | let first_child = tt.first_child_or_token()?; | 162 | let first_child = tt.first_child_or_token()?; |
@@ -173,7 +173,7 @@ impl Convertor { | |||
173 | .last() | 173 | .last() |
174 | .unwrap(); | 174 | .unwrap(); |
175 | if first_child.kind().is_trivia() { | 175 | if first_child.kind().is_trivia() { |
176 | return Some(tt::Subtree { token_trees: vec![], delimiter: tt::Delimiter::None }); | 176 | return Some(tt::Subtree { token_trees: vec![], delimiter: None }); |
177 | } | 177 | } |
178 | 178 | ||
179 | let last_child = successors(Some(last_child), |it| { | 179 | let last_child = successors(Some(last_child), |it| { |
@@ -187,10 +187,10 @@ impl Convertor { | |||
187 | .unwrap(); | 187 | .unwrap(); |
188 | 188 | ||
189 | let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { | 189 | let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { |
190 | (T!['('], T![')']) => (tt::Delimiter::Parenthesis, true), | 190 | (T!['('], T![')']) => (Some(tt::Delimiter::Parenthesis), true), |
191 | (T!['{'], T!['}']) => (tt::Delimiter::Brace, true), | 191 | (T!['{'], T!['}']) => (Some(tt::Delimiter::Brace), true), |
192 | (T!['['], T![']']) => (tt::Delimiter::Bracket, true), | 192 | (T!['['], T![']']) => (Some(tt::Delimiter::Bracket), true), |
193 | _ => (tt::Delimiter::None, false), | 193 | _ => (None, false), |
194 | }; | 194 | }; |
195 | 195 | ||
196 | let mut token_trees = Vec::new(); | 196 | let mut token_trees = Vec::new(); |
@@ -246,9 +246,7 @@ impl Convertor { | |||
246 | } | 246 | } |
247 | NodeOrToken::Node(node) => { | 247 | NodeOrToken::Node(node) => { |
248 | let child_subtree = self.go(&node)?; | 248 | let child_subtree = self.go(&node)?; |
249 | if child_subtree.delimiter == tt::Delimiter::None | 249 | if child_subtree.delimiter.is_none() && node.kind() != SyntaxKind::TOKEN_TREE { |
250 | && node.kind() != SyntaxKind::TOKEN_TREE | ||
251 | { | ||
252 | token_trees.extend(child_subtree.token_trees); | 250 | token_trees.extend(child_subtree.token_trees); |
253 | } else { | 251 | } else { |
254 | token_trees.push(child_subtree.into()); | 252 | token_trees.push(child_subtree.into()); |
@@ -299,16 +297,16 @@ impl<'a> TtTreeSink<'a> { | |||
299 | } | 297 | } |
300 | } | 298 | } |
301 | 299 | ||
302 | fn delim_to_str(d: tt::Delimiter, closing: bool) -> SmolStr { | 300 | fn delim_to_str(d: Option<tt::Delimiter>, closing: bool) -> SmolStr { |
303 | let texts = match d { | 301 | let texts = match d { |
304 | tt::Delimiter::Parenthesis => "()", | 302 | Some(tt::Delimiter::Parenthesis) => "()", |
305 | tt::Delimiter::Brace => "{}", | 303 | Some(tt::Delimiter::Brace) => "{}", |
306 | tt::Delimiter::Bracket => "[]", | 304 | Some(tt::Delimiter::Bracket) => "[]", |
307 | tt::Delimiter::None => "", | 305 | None => return "".into(), |
308 | }; | 306 | }; |
309 | 307 | ||
310 | let idx = closing as usize; | 308 | let idx = closing as usize; |
311 | let text = if !texts.is_empty() { &texts[idx..texts.len() - (1 - idx)] } else { "" }; | 309 | let text = &texts[idx..texts.len() - (1 - idx)]; |
312 | text.into() | 310 | text.into() |
313 | } | 311 | } |
314 | 312 | ||
@@ -497,7 +495,7 @@ mod tests { | |||
497 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); | 495 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); |
498 | let tt = ast_to_token_tree(&token_tree).unwrap().0; | 496 | let tt = ast_to_token_tree(&token_tree).unwrap().0; |
499 | 497 | ||
500 | assert_eq!(tt.delimiter, tt::Delimiter::Brace); | 498 | assert_eq!(tt.delimiter, Some(tt::Delimiter::Brace)); |
501 | } | 499 | } |
502 | 500 | ||
503 | #[test] | 501 | #[test] |
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 0109a4d98..148cc2625 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -1463,7 +1463,7 @@ pub(crate) fn assert_expansion( | |||
1463 | let wrapped = ast::SourceFile::parse(&wrapped); | 1463 | let wrapped = ast::SourceFile::parse(&wrapped); |
1464 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 1464 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
1465 | let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; | 1465 | let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; |
1466 | wrapped.delimiter = tt::Delimiter::None; | 1466 | wrapped.delimiter = None; |
1467 | wrapped | 1467 | wrapped |
1468 | }; | 1468 | }; |
1469 | let (expanded_tree, expected_tree) = match kind { | 1469 | let (expanded_tree, expected_tree) = match kind { |