diff options
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 592fcf527..8d9217518 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -12,12 +12,26 @@ use tt::buffer::{Cursor, TokenBuffer}; | |||
12 | 12 | ||
13 | use crate::subtree_source::SubtreeTokenSource; | 13 | use crate::subtree_source::SubtreeTokenSource; |
14 | use crate::ExpandError; | 14 | use crate::ExpandError; |
15 | use std::sync::atomic::{AtomicU32, Ordering}; | ||
15 | 16 | ||
16 | /// Maps `tt::TokenId` to the relative range of the original token. | 17 | /// Maps `tt::TokenId` to the relative range of the original token. |
17 | #[derive(Default)] | ||
18 | pub struct TokenMap { | 18 | pub struct TokenMap { |
19 | /// Maps `tt::TokenId` to the *relative* source range. | 19 | /// Maps `tt::TokenId` to the *relative* source range. |
20 | tokens: Vec<TextRange>, | 20 | tokens: Vec<TextRange>, |
21 | map_id: u32, | ||
22 | } | ||
23 | |||
24 | static TOKEN_MAP_COUNTER: AtomicU32 = AtomicU32::new(0); | ||
25 | |||
26 | /// Generate an unique token map id for each instance | ||
27 | fn make_uniq_token_map_id() -> u32 { | ||
28 | TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst) | ||
29 | } | ||
30 | |||
31 | impl std::default::Default for TokenMap { | ||
32 | fn default() -> TokenMap { | ||
33 | TokenMap { tokens: Default::default(), map_id: make_uniq_token_map_id() } | ||
34 | } | ||
21 | } | 35 | } |
22 | 36 | ||
23 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 37 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
@@ -105,14 +119,17 @@ pub fn token_tree_to_items(tt: &tt::Subtree) -> Result<Parse<ast::MacroItems>, E | |||
105 | 119 | ||
106 | impl TokenMap { | 120 | impl TokenMap { |
107 | pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { | 121 | pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { |
108 | let idx = tt.0 as usize; | 122 | if self.map_id != tt.map_id() { |
123 | return None; | ||
124 | } | ||
125 | let idx = tt.token_id() as usize; | ||
109 | self.tokens.get(idx).copied() | 126 | self.tokens.get(idx).copied() |
110 | } | 127 | } |
111 | 128 | ||
112 | fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { | 129 | fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { |
113 | let id = self.tokens.len(); | 130 | let id = self.tokens.len(); |
114 | self.tokens.push(relative_range); | 131 | self.tokens.push(relative_range); |
115 | tt::TokenId(id as u32) | 132 | tt::TokenId::new(id as u32, self.map_id) |
116 | } | 133 | } |
117 | } | 134 | } |
118 | 135 | ||