diff options
author | Edwin Cheng <[email protected]> | 2019-04-05 11:23:01 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-04-05 11:23:01 +0100 |
commit | 1ab78d60561ed701b29d3065061cbc3175e20c4a (patch) | |
tree | 3302dd6d1fbc89ad11e5a9a519a20d7c1a109e77 /crates/ra_mbe | |
parent | be9a44e9bad262ac5e615730e540fd434f846a0e (diff) |
Fix literal support in token tree to ast item list
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 41 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 4 |
2 files changed, 43 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 93246f54a..3b127526d 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -337,4 +337,45 @@ SOURCE_FILE@[0; 40) | |||
337 | ); | 337 | ); |
338 | } | 338 | } |
339 | 339 | ||
340 | #[test] | ||
341 | fn expand_literals_to_item_list() { | ||
342 | fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree { | ||
343 | if let tt::TokenTree::Subtree(subtree) = tt { | ||
344 | return &subtree; | ||
345 | } | ||
346 | assert!(false, "It is not a subtree"); | ||
347 | unreachable!(); | ||
348 | } | ||
349 | |||
350 | fn to_literal(tt: &tt::TokenTree) -> &tt::Literal { | ||
351 | if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt { | ||
352 | return lit; | ||
353 | } | ||
354 | assert!(false, "It is not a literal"); | ||
355 | unreachable!(); | ||
356 | } | ||
357 | |||
358 | let rules = create_rules( | ||
359 | r#" | ||
360 | macro_rules! literals { | ||
361 | ($i:ident) => { | ||
362 | { | ||
363 | let a = 'c'; | ||
364 | let c = 1000; | ||
365 | let f = 12E+99_f64; | ||
366 | } | ||
367 | } | ||
368 | } | ||
369 | "#, | ||
370 | ); | ||
371 | let expansion = expand(&rules, "literals!(foo)"); | ||
372 | let stm_tokens = &to_subtree(&expansion.token_trees[0]).token_trees; | ||
373 | |||
374 | // [let] [a] [=] ['c'] [;] | ||
375 | assert_eq!(to_literal(&stm_tokens[3]).text, "'c'"); | ||
376 | // [let] [c] [=] [1000] [;] | ||
377 | assert_eq!(to_literal(&stm_tokens[5 + 3]).text, "1000"); | ||
378 | // [let] [f] [=] [12E+99_f64] [;] | ||
379 | assert_eq!(to_literal(&stm_tokens[10 + 3]).text, "12E+99_f64"); | ||
380 | } | ||
340 | } | 381 | } |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 257503de8..fbf3d2e71 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ra_parser::{TokenSource, TreeSink, ParseError}; | 1 | use ra_parser::{TokenSource, TreeSink, ParseError}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement, | 3 | AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement, |
4 | ast, SyntaxKind::*, TextUnit | 4 | ast, SyntaxKind::*, TextUnit, next_token |
5 | }; | 5 | }; |
6 | 6 | ||
7 | /// Maps `tt::TokenId` to the relative range of the original token. | 7 | /// Maps `tt::TokenId` to the relative range of the original token. |
@@ -189,7 +189,7 @@ impl TtTokenSource { | |||
189 | { | 189 | { |
190 | let tok = match token { | 190 | let tok = match token { |
191 | tt::Leaf::Literal(l) => TtToken { | 191 | tt::Leaf::Literal(l) => TtToken { |
192 | kind: SyntaxKind::INT_NUMBER, // FIXME | 192 | kind: next_token(&l.text).kind, |
193 | is_joint_to_next: false, | 193 | is_joint_to_next: false, |
194 | text: l.text.clone(), | 194 | text: l.text.clone(), |
195 | }, | 195 | }, |