From 1ab78d60561ed701b29d3065061cbc3175e20c4a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 5 Apr 2019 18:23:01 +0800 Subject: Fix literal support in token tree to ast item list --- crates/ra_mbe/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++ crates/ra_mbe/src/syntax_bridge.rs | 4 ++-- 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'crates/ra_mbe') 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) ); } + #[test] + fn expand_literals_to_item_list() { + fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree { + if let tt::TokenTree::Subtree(subtree) = tt { + return &subtree; + } + assert!(false, "It is not a subtree"); + unreachable!(); + } + + fn to_literal(tt: &tt::TokenTree) -> &tt::Literal { + if let tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) = tt { + return lit; + } + assert!(false, "It is not a literal"); + unreachable!(); + } + + let rules = create_rules( + r#" + macro_rules! literals { + ($i:ident) => { + { + let a = 'c'; + let c = 1000; + let f = 12E+99_f64; + } + } + } + "#, + ); + let expansion = expand(&rules, "literals!(foo)"); + let stm_tokens = &to_subtree(&expansion.token_trees[0]).token_trees; + + // [let] [a] [=] ['c'] [;] + assert_eq!(to_literal(&stm_tokens[3]).text, "'c'"); + // [let] [c] [=] [1000] [;] + assert_eq!(to_literal(&stm_tokens[5 + 3]).text, "1000"); + // [let] [f] [=] [12E+99_f64] [;] + assert_eq!(to_literal(&stm_tokens[10 + 3]).text, "12E+99_f64"); + } } 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 @@ use ra_parser::{TokenSource, TreeSink, ParseError}; use ra_syntax::{ AstNode, SyntaxNode, TextRange, SyntaxKind, SmolStr, SyntaxTreeBuilder, TreeArc, SyntaxElement, - ast, SyntaxKind::*, TextUnit + ast, SyntaxKind::*, TextUnit, next_token }; /// Maps `tt::TokenId` to the relative range of the original token. @@ -189,7 +189,7 @@ impl TtTokenSource { { let tok = match token { tt::Leaf::Literal(l) => TtToken { - kind: SyntaxKind::INT_NUMBER, // FIXME + kind: next_token(&l.text).kind, is_joint_to_next: false, text: l.text.clone(), }, -- cgit v1.2.3