aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs8
-rw-r--r--crates/ra_tt/src/lib.rs8
2 files changed, 15 insertions, 1 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 798f9d8fa..848c785f8 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -3,12 +3,15 @@ use ra_syntax::{
3 ast, SyntaxKind::*, TextUnit 3 ast, SyntaxKind::*, TextUnit
4}; 4};
5 5
6/// Maps `tt::TokenId` to the relative range of the original token.
6#[derive(Default)] 7#[derive(Default)]
7pub struct TokenMap { 8pub struct TokenMap {
8 /// Maps `tt::TokenId` to the *relative* source range. 9 /// Maps `tt::TokenId` to the *relative* source range.
9 toknes: Vec<TextRange>, 10 toknes: Vec<TextRange>,
10} 11}
11 12
13/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
14/// will consume).
12pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> { 15pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> {
13 let mut token_map = TokenMap::default(); 16 let mut token_map = TokenMap::default();
14 let node = ast.syntax(); 17 let node = ast.syntax();
@@ -17,6 +20,11 @@ pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)
17} 20}
18 21
19impl TokenMap { 22impl TokenMap {
23 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> {
24 let idx = tt.0 as usize;
25 self.toknes.get(idx).map(|&it| it)
26 }
27
20 fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { 28 fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId {
21 let id = self.toknes.len(); 29 let id = self.toknes.len();
22 self.toknes.push(relative_range); 30 self.toknes.push(relative_range);
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs
index e0a4cdb8b..c1f37b889 100644
--- a/crates/ra_tt/src/lib.rs
+++ b/crates/ra_tt/src/lib.rs
@@ -1,4 +1,4 @@
1/// `tt` crate defines a `TokenTree` datastructure: this is the interface (both 1/// `tt` crate defines a `TokenTree` data structure: this is the interface (both
2/// input and output) of macros. It closely mirrors `proc_macro` crate's 2/// input and output) of macros. It closely mirrors `proc_macro` crate's
3/// `TokenTree`. 3/// `TokenTree`.
4 4
@@ -18,6 +18,12 @@ use std::fmt;
18 18
19use smol_str::SmolStr; 19use smol_str::SmolStr;
20 20
21/// Represents identity of the token.
22///
23/// For hygiene purposes, we need to track which expanded tokens originated from
24/// which source tokens. We do it by assigning an distinct identity to each
25/// source token and making sure that identities are preserved during macro
26/// expansion.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct TokenId(pub u32); 28pub struct TokenId(pub u32);
23 29