From 9fd546bec23ac817a45da28889e76118969db91e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 3 Nov 2019 13:19:50 +0800 Subject: Add map_id to TokenId --- crates/ra_mbe/src/syntax_bridge.rs | 23 ++++++++++++++++++++--- crates/ra_tt/src/lib.rs | 16 ++++++++++++++-- 2 files changed, 34 insertions(+), 5 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}; use crate::subtree_source::SubtreeTokenSource; use crate::ExpandError; +use std::sync::atomic::{AtomicU32, Ordering}; /// Maps `tt::TokenId` to the relative range of the original token. -#[derive(Default)] pub struct TokenMap { /// Maps `tt::TokenId` to the *relative* source range. tokens: Vec, + map_id: u32, +} + +static TOKEN_MAP_COUNTER: AtomicU32 = AtomicU32::new(0); + +/// Generate an unique token map id for each instance +fn make_uniq_token_map_id() -> u32 { + TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst) +} + +impl std::default::Default for TokenMap { + fn default() -> TokenMap { + TokenMap { tokens: Default::default(), map_id: make_uniq_token_map_id() } + } } /// 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, E impl TokenMap { pub fn relative_range_of(&self, tt: tt::TokenId) -> Option { - let idx = tt.0 as usize; + if self.map_id != tt.map_id() { + return None; + } + let idx = tt.token_id() as usize; self.tokens.get(idx).copied() } fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { let id = self.tokens.len(); self.tokens.push(relative_range); - tt::TokenId(id as u32) + tt::TokenId::new(id as u32, self.map_id) } } diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs index 20c251ff4..96410ff22 100644 --- a/crates/ra_tt/src/lib.rs +++ b/crates/ra_tt/src/lib.rs @@ -25,11 +25,23 @@ use smol_str::SmolStr; /// source token and making sure that identities are preserved during macro /// expansion. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TokenId(pub u32); +pub struct TokenId(u32, u32); impl TokenId { + pub fn new(token_id: u32, map_id: u32) -> TokenId { + TokenId(token_id, map_id) + } + pub const fn unspecified() -> TokenId { - TokenId(!0) + TokenId(!0, !0) + } + + pub fn token_id(&self) -> u32 { + self.0 + } + + pub fn map_id(&self) -> u32 { + self.1 } } -- cgit v1.2.3