Skip to content

Commit 0d34a25

Browse files
committed
assign ids when converting tt
1 parent 58897dd commit 0d34a25

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

crates/ra_mbe/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
144144
let macro_invocation =
145145
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
146146

147-
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
148-
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
147+
let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
148+
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
149149
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
150150
let expansion = rules.expand(&invocation_tt).unwrap();
151151
assert_eq!(
@@ -160,7 +160,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
160160
let macro_definition =
161161
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
162162

163-
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
163+
let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
164164
crate::MacroRules::parse(&definition_tt).unwrap()
165165
}
166166

@@ -169,7 +169,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
169169
let macro_invocation =
170170
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
171171

172-
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
172+
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
173173

174174
let expaned = rules.expand(&invocation_tt).unwrap();
175175
assert_eq!(expaned.to_string(), expansion);

crates/ra_mbe/src/syntax_bridge.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*};
1+
use ra_syntax::{
2+
AstNode, SyntaxNode, TextRange,
3+
ast, SyntaxKind::*, TextUnit
4+
};
25

3-
pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<tt::Subtree> {
4-
convert_tt(ast.syntax())
6+
#[derive(Default)]
7+
pub struct TokenMap {
8+
/// Maps `tt::TokenId` to the *relative* source range.
9+
toknes: Vec<TextRange>,
510
}
611

7-
fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
12+
pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> {
13+
let mut token_map = TokenMap::default();
14+
let node = ast.syntax();
15+
let tt = convert_tt(&mut token_map, node.range().start(), node)?;
16+
Some((tt, token_map))
17+
}
18+
19+
impl TokenMap {
20+
fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId {
21+
let id = self.toknes.len();
22+
self.toknes.push(relative_range);
23+
tt::TokenId(id as u32)
24+
}
25+
}
26+
27+
fn convert_tt(
28+
token_map: &mut TokenMap,
29+
global_offset: TextUnit,
30+
tt: &SyntaxNode,
31+
) -> Option<tt::Subtree> {
832
let first_child = tt.first_child()?;
933
let last_child = tt.last_child()?;
1034
let delimiter = match (first_child.kind(), last_child.kind()) {
@@ -34,10 +58,12 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
3458
}
3559
} else {
3660
let child: tt::TokenTree = if child.kind() == TOKEN_TREE {
37-
convert_tt(child)?.into()
61+
convert_tt(token_map, global_offset, child)?.into()
3862
} else if child.kind().is_keyword() || child.kind() == IDENT {
63+
let relative_range = child.range() - global_offset;
64+
let id = token_map.alloc(relative_range);
3965
let text = child.leaf_text().unwrap().clone();
40-
tt::Leaf::from(tt::Ident { text, id: tt::TokenId::unspecified() }).into()
66+
tt::Leaf::from(tt::Ident { text, id }).into()
4167
} else if child.kind().is_literal() {
4268
tt::Leaf::from(tt::Literal { text: child.leaf_text().unwrap().clone() }).into()
4369
} else {

0 commit comments

Comments
 (0)