aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-05 11:23:01 +0100
committerEdwin Cheng <[email protected]>2019-04-05 11:23:01 +0100
commit1ab78d60561ed701b29d3065061cbc3175e20c4a (patch)
tree3302dd6d1fbc89ad11e5a9a519a20d7c1a109e77 /crates
parentbe9a44e9bad262ac5e615730e540fd434f846a0e (diff)
Fix literal support in token tree to ast item list
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_mbe/src/lib.rs41
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs4
-rw-r--r--crates/ra_syntax/src/lib.rs2
-rw-r--r--crates/ra_syntax/src/parsing.rs2
4 files changed, 45 insertions, 4 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 @@
1use ra_parser::{TokenSource, TreeSink, ParseError}; 1use ra_parser::{TokenSource, TreeSink, ParseError};
2use ra_syntax::{ 2use 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 },
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index e1088e296..1cbc3e9e1 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -40,7 +40,7 @@ pub use crate::{
40 syntax_text::SyntaxText, 40 syntax_text::SyntaxText,
41 syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken}, 41 syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc, SyntaxTreeBuilder, SyntaxElement, SyntaxToken},
42 ptr::{SyntaxNodePtr, AstPtr}, 42 ptr::{SyntaxNodePtr, AstPtr},
43 parsing::{tokenize, Token}, 43 parsing::{tokenize, next_token, Token},
44}; 44};
45 45
46use ra_text_edit::AtomTextEdit; 46use ra_text_edit::AtomTextEdit;
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs
index ad5668a65..f0750d737 100644
--- a/crates/ra_syntax/src/parsing.rs
+++ b/crates/ra_syntax/src/parsing.rs
@@ -11,7 +11,7 @@ use crate::{
11 syntax_node::GreenNode, 11 syntax_node::GreenNode,
12}; 12};
13 13
14pub use self::lexer::{tokenize, Token}; 14pub use self::lexer::{tokenize, next_token, Token};
15 15
16pub(crate) use self::reparsing::incremental_reparse; 16pub(crate) use self::reparsing::incremental_reparse;
17 17