diff options
author | Aleksey Kladov <[email protected]> | 2019-02-11 18:31:54 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-11 18:31:54 +0000 |
commit | ae312680d6d7bb0cc00d2b8d9799249d36e0136e (patch) | |
tree | 6547cd1d0e5b7494ac6b9ec9b446ba0db4c149ad /crates/ra_mbe/src | |
parent | 0d34a256de5d33565e9a62d53bf149cf59510937 (diff) |
docs
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 8 |
1 files changed, 8 insertions, 0 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)] |
7 | pub struct TokenMap { | 8 | pub 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). | ||
12 | pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> { | 15 | pub 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 | ||
19 | impl TokenMap { | 22 | 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 | |||
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); |