Skip to content

Commit ae31268

Browse files
committed
docs
1 parent 0d34a25 commit ae31268

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

crates/ra_mbe/src/syntax_bridge.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use ra_syntax::{
33
ast, SyntaxKind::*, TextUnit
44
};
55

6+
/// Maps `tt::TokenId` to the relative range of the original token.
67
#[derive(Default)]
78
pub struct TokenMap {
89
/// Maps `tt::TokenId` to the *relative* source range.
910
toknes: Vec<TextRange>,
1011
}
1112

13+
/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
14+
/// will consume).
1215
pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> {
1316
let mut token_map = TokenMap::default();
1417
let node = ast.syntax();
@@ -17,6 +20,11 @@ pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)
1720
}
1821

1922
impl 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+
2028
fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId {
2129
let id = self.toknes.len();
2230
self.toknes.push(relative_range);

crates/ra_tt/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
22
/// input and output) of macros. It closely mirrors `proc_macro` crate's
33
/// `TokenTree`.
44
@@ -18,6 +18,12 @@ use std::fmt;
1818

1919
use smol_str::SmolStr;
2020

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.
2127
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2228
pub struct TokenId(pub u32);
2329

0 commit comments

Comments
 (0)