aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-03 05:19:50 +0000
committerEdwin Cheng <[email protected]>2019-11-04 17:38:20 +0000
commit9fd546bec23ac817a45da28889e76118969db91e (patch)
treed00a96f165b9d986fb7ebb37c8ec5733082fcb47 /crates/ra_mbe/src
parentd9fb01f803671bfb6fca6b873bd140d8177ecb1c (diff)
Add map_id to TokenId
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs23
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
13use crate::subtree_source::SubtreeTokenSource; 13use crate::subtree_source::SubtreeTokenSource;
14use crate::ExpandError; 14use crate::ExpandError;
15use 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)]
18pub struct TokenMap { 18pub 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
24static TOKEN_MAP_COUNTER: AtomicU32 = AtomicU32::new(0);
25
26/// Generate an unique token map id for each instance
27fn make_uniq_token_map_id() -> u32 {
28 TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst)
29}
30
31impl 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
106impl TokenMap { 120impl 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