diff options
-rw-r--r-- | crates/ra_hir_expand/src/builtin_derive.rs | 28 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/quote.rs | 13 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_expander/transcriber.rs | 7 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 45 | ||||
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 10 | ||||
-rw-r--r-- | crates/ra_tt/src/lib.rs | 2 |
6 files changed, 72 insertions, 33 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index b26441253..62c60e336 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs | |||
@@ -97,11 +97,24 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> { | |||
97 | 97 | ||
98 | fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { | 98 | fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { |
99 | let mut result = Vec::<tt::TokenTree>::new(); | 99 | let mut result = Vec::<tt::TokenTree>::new(); |
100 | result.push(tt::Leaf::Punct(tt::Punct { char: '<', spacing: tt::Spacing::Alone }).into()); | 100 | result.push( |
101 | tt::Leaf::Punct(tt::Punct { | ||
102 | char: '<', | ||
103 | spacing: tt::Spacing::Alone, | ||
104 | id: tt::TokenId::unspecified(), | ||
105 | }) | ||
106 | .into(), | ||
107 | ); | ||
101 | for i in 0..n { | 108 | for i in 0..n { |
102 | if i > 0 { | 109 | if i > 0 { |
103 | result | 110 | result.push( |
104 | .push(tt::Leaf::Punct(tt::Punct { char: ',', spacing: tt::Spacing::Alone }).into()); | 111 | tt::Leaf::Punct(tt::Punct { |
112 | char: ',', | ||
113 | spacing: tt::Spacing::Alone, | ||
114 | id: tt::TokenId::unspecified(), | ||
115 | }) | ||
116 | .into(), | ||
117 | ); | ||
105 | } | 118 | } |
106 | result.push( | 119 | result.push( |
107 | tt::Leaf::Ident(tt::Ident { | 120 | tt::Leaf::Ident(tt::Ident { |
@@ -112,7 +125,14 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { | |||
112 | ); | 125 | ); |
113 | result.extend(bound.iter().cloned()); | 126 | result.extend(bound.iter().cloned()); |
114 | } | 127 | } |
115 | result.push(tt::Leaf::Punct(tt::Punct { char: '>', spacing: tt::Spacing::Alone }).into()); | 128 | result.push( |
129 | tt::Leaf::Punct(tt::Punct { | ||
130 | char: '>', | ||
131 | spacing: tt::Spacing::Alone, | ||
132 | id: tt::TokenId::unspecified(), | ||
133 | }) | ||
134 | .into(), | ||
135 | ); | ||
116 | result | 136 | result |
117 | } | 137 | } |
118 | 138 | ||
diff --git a/crates/ra_hir_expand/src/quote.rs b/crates/ra_hir_expand/src/quote.rs index aa8a5f23f..bce38cc67 100644 --- a/crates/ra_hir_expand/src/quote.rs +++ b/crates/ra_hir_expand/src/quote.rs | |||
@@ -29,6 +29,7 @@ macro_rules! __quote { | |||
29 | tt::Leaf::Punct(tt::Punct { | 29 | tt::Leaf::Punct(tt::Punct { |
30 | char: $first, | 30 | char: $first, |
31 | spacing: tt::Spacing::Alone, | 31 | spacing: tt::Spacing::Alone, |
32 | id: tt::TokenId::unspecified(), | ||
32 | }).into() | 33 | }).into() |
33 | ] | 34 | ] |
34 | } | 35 | } |
@@ -40,10 +41,12 @@ macro_rules! __quote { | |||
40 | tt::Leaf::Punct(tt::Punct { | 41 | tt::Leaf::Punct(tt::Punct { |
41 | char: $first, | 42 | char: $first, |
42 | spacing: tt::Spacing::Joint, | 43 | spacing: tt::Spacing::Joint, |
44 | id: tt::TokenId::unspecified(), | ||
43 | }).into(), | 45 | }).into(), |
44 | tt::Leaf::Punct(tt::Punct { | 46 | tt::Leaf::Punct(tt::Punct { |
45 | char: $sec, | 47 | char: $sec, |
46 | spacing: tt::Spacing::Alone, | 48 | spacing: tt::Spacing::Alone, |
49 | id: tt::TokenId::unspecified(), | ||
47 | }).into() | 50 | }).into() |
48 | ] | 51 | ] |
49 | } | 52 | } |
@@ -179,15 +182,15 @@ macro_rules! impl_to_to_tokentrees { | |||
179 | } | 182 | } |
180 | 183 | ||
181 | impl_to_to_tokentrees! { | 184 | impl_to_to_tokentrees! { |
182 | u32 => self { tt::Literal{text: self.to_string().into()} }; | 185 | u32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; |
183 | usize => self { tt::Literal{text: self.to_string().into()}}; | 186 | usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; |
184 | i32 => self { tt::Literal{text: self.to_string().into()}}; | 187 | i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; |
185 | tt::Leaf => self { self }; | 188 | tt::Leaf => self { self }; |
186 | tt::Literal => self { self }; | 189 | tt::Literal => self { self }; |
187 | tt::Ident => self { self }; | 190 | tt::Ident => self { self }; |
188 | tt::Punct => self { self }; | 191 | tt::Punct => self { self }; |
189 | &str => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into()}}; | 192 | &str => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}}; |
190 | String => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into()}} | 193 | String => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}} |
191 | } | 194 | } |
192 | 195 | ||
193 | #[cfg(test)] | 196 | #[cfg(test)] |
diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index f7636db11..eda66cd50 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs | |||
@@ -108,7 +108,12 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result<Fragment, ExpandError> | |||
108 | let tt = tt::Subtree { | 108 | let tt = tt::Subtree { |
109 | 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 { |
112 | char: '$', | ||
113 | spacing: tt::Spacing::Alone, | ||
114 | id: tt::TokenId::unspecified(), | ||
115 | }) | ||
116 | .into(), | ||
112 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) | 117 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) |
113 | .into(), | 118 | .into(), |
114 | ], | 119 | ], |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index b8e2cfc1d..8f65ff125 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -136,11 +136,15 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTr | |||
136 | } | 136 | } |
137 | 137 | ||
138 | fn mk_punct(c: char) -> tt::TokenTree { | 138 | fn mk_punct(c: char) -> tt::TokenTree { |
139 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { char: c, spacing: tt::Spacing::Alone })) | 139 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { |
140 | char: c, | ||
141 | spacing: tt::Spacing::Alone, | ||
142 | id: tt::TokenId::unspecified(), | ||
143 | })) | ||
140 | } | 144 | } |
141 | 145 | ||
142 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { | 146 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { |
143 | let lit = tt::Literal { text: doc_comment_text(comment) }; | 147 | let lit = tt::Literal { text: doc_comment_text(comment), id: tt::TokenId::unspecified() }; |
144 | 148 | ||
145 | tt::TokenTree::from(tt::Leaf::from(lit)) | 149 | tt::TokenTree::from(tt::Leaf::from(lit)) |
146 | } | 150 | } |
@@ -223,24 +227,29 @@ impl Convertor { | |||
223 | .take(token.text().len() - 1) | 227 | .take(token.text().len() - 1) |
224 | .chain(std::iter::once(last_spacing)); | 228 | .chain(std::iter::once(last_spacing)); |
225 | for (char, spacing) in token.text().chars().zip(spacing_iter) { | 229 | for (char, spacing) in token.text().chars().zip(spacing_iter) { |
226 | token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); | 230 | let id = self.alloc(token.text_range()); |
231 | token_trees | ||
232 | .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); | ||
227 | } | 233 | } |
228 | } else { | 234 | } else { |
229 | let child: tt::TokenTree = | 235 | let child: tt::TokenTree = if token.kind() == T![true] |
230 | if token.kind() == T![true] || token.kind() == T![false] { | 236 | || token.kind() == T![false] |
231 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | 237 | { |
232 | } else if token.kind().is_keyword() | 238 | let id = self.alloc(token.text_range()); |
233 | || token.kind() == IDENT | 239 | tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() |
234 | || token.kind() == LIFETIME | 240 | } else if token.kind().is_keyword() |
235 | { | 241 | || token.kind() == IDENT |
236 | let id = self.alloc(token.text_range()); | 242 | || token.kind() == LIFETIME |
237 | let text = token.text().clone(); | 243 | { |
238 | tt::Leaf::from(tt::Ident { text, id }).into() | 244 | let id = self.alloc(token.text_range()); |
239 | } else if token.kind().is_literal() { | 245 | let text = token.text().clone(); |
240 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | 246 | tt::Leaf::from(tt::Ident { text, id }).into() |
241 | } else { | 247 | } else if token.kind().is_literal() { |
242 | return None; | 248 | let id = self.alloc(token.text_range()); |
243 | }; | 249 | tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() |
250 | } else { | ||
251 | return None; | ||
252 | }; | ||
244 | token_trees.push(child); | 253 | token_trees.push(child); |
245 | } | 254 | } |
246 | } | 255 | } |
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 148cc2625..70e65bc74 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -78,12 +78,12 @@ macro_rules! foobar { | |||
78 | 78 | ||
79 | assert_eq!(expansion.token_trees.len(), 3); | 79 | assert_eq!(expansion.token_trees.len(), 3); |
80 | // ($e:ident) => { foo bar $e } | 80 | // ($e:ident) => { foo bar $e } |
81 | // 0 1 2 3 4 | 81 | // 0123 45 6 7 89 |
82 | assert_eq!(get_id(&expansion.token_trees[0]), Some(2)); | 82 | assert_eq!(get_id(&expansion.token_trees[0]), Some(6)); |
83 | assert_eq!(get_id(&expansion.token_trees[1]), Some(3)); | 83 | assert_eq!(get_id(&expansion.token_trees[1]), Some(7)); |
84 | 84 | ||
85 | // So baz should be 5 | 85 | // So baz should be 10 |
86 | assert_eq!(get_id(&expansion.token_trees[2]), Some(5)); | 86 | assert_eq!(get_id(&expansion.token_trees[2]), Some(10)); |
87 | } | 87 | } |
88 | 88 | ||
89 | #[test] | 89 | #[test] |
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs index e7bfd5fd2..209ca4048 100644 --- a/crates/ra_tt/src/lib.rs +++ b/crates/ra_tt/src/lib.rs | |||
@@ -64,12 +64,14 @@ pub enum Delimiter { | |||
64 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 64 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
65 | pub struct Literal { | 65 | pub struct Literal { |
66 | pub text: SmolStr, | 66 | pub text: SmolStr, |
67 | pub id: TokenId, | ||
67 | } | 68 | } |
68 | 69 | ||
69 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 70 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
70 | pub struct Punct { | 71 | pub struct Punct { |
71 | pub char: char, | 72 | pub char: char, |
72 | pub spacing: Spacing, | 73 | pub spacing: Spacing, |
74 | pub id: TokenId, | ||
73 | } | 75 | } |
74 | 76 | ||
75 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 77 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |