aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-05 15:17:07 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-05 15:17:07 +0100
commita52e86f9a9a21313a1543823b92e82c0a30e0870 (patch)
tree35d8b8b04a8e17a162fd6b95105db34919d59507 /crates/ra_mbe/src/syntax_bridge.rs
parentbe9a44e9bad262ac5e615730e540fd434f846a0e (diff)
parent7abc06bd576264cb6b7c8becdbd1a8c0e914463d (diff)
Merge #1112
1112: Fix literal support in token tree to ast item list r=matklad a=edwin0cheng This PR implements following things : 1. Expose `next_token` from `ra_parse` 2. Fix the literal conversion in `token_tree_to_ast_item_list` 3. Add test for the conversion Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs47
1 files changed, 45 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 257503de8..139a0fd33 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, classify_literal
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.
@@ -103,10 +103,12 @@ fn convert_tt(
103 Some(res) 103 Some(res)
104} 104}
105 105
106#[derive(Debug)]
106struct TtTokenSource { 107struct TtTokenSource {
107 tokens: Vec<TtToken>, 108 tokens: Vec<TtToken>,
108} 109}
109 110
111#[derive(Debug)]
110struct TtToken { 112struct TtToken {
111 kind: SyntaxKind, 113 kind: SyntaxKind,
112 is_joint_to_next: bool, 114 is_joint_to_next: bool,
@@ -189,7 +191,7 @@ impl TtTokenSource {
189 { 191 {
190 let tok = match token { 192 let tok = match token {
191 tt::Leaf::Literal(l) => TtToken { 193 tt::Leaf::Literal(l) => TtToken {
192 kind: SyntaxKind::INT_NUMBER, // FIXME 194 kind: classify_literal(&l.text).unwrap().kind,
193 is_joint_to_next: false, 195 is_joint_to_next: false,
194 text: l.text.clone(), 196 text: l.text.clone(),
195 }, 197 },
@@ -355,3 +357,44 @@ impl<'a> TreeSink for TtTreeSink<'a> {
355 self.inner.error(error, self.text_pos) 357 self.inner.error(error, self.text_pos)
356 } 358 }
357} 359}
360
361#[cfg(test)]
362mod tests {
363 use super::*;
364 use crate::tests::{expand, create_rules};
365
366 #[test]
367 fn convert_tt_token_source() {
368 let rules = create_rules(
369 r#"
370 macro_rules! literals {
371 ($i:ident) => {
372 {
373 let a = 'c';
374 let c = 1000;
375 let f = 12E+99_f64;
376 let s = "rust1";
377 }
378 }
379 }
380 "#,
381 );
382 let expansion = expand(&rules, "literals!(foo)");
383 let tt_src = TtTokenSource::new(&expansion);
384
385 // [{]
386 // [let] [a] [=] ['c'] [;]
387 assert_eq!(tt_src.tokens[1 + 3].text, "'c'");
388 assert_eq!(tt_src.tokens[1 + 3].kind, CHAR);
389 // [let] [c] [=] [1000] [;]
390 assert_eq!(tt_src.tokens[1 + 5 + 3].text, "1000");
391 assert_eq!(tt_src.tokens[1 + 5 + 3].kind, INT_NUMBER);
392 // [let] [f] [=] [12E+99_f64] [;]
393 assert_eq!(tt_src.tokens[1 + 10 + 3].text, "12E+99_f64");
394 assert_eq!(tt_src.tokens[1 + 10 + 3].kind, FLOAT_NUMBER);
395
396 // [let] [s] [=] ["rust1"] [;]
397 assert_eq!(tt_src.tokens[1 + 15 + 3].text, "\"rust1\"");
398 assert_eq!(tt_src.tokens[1 + 15 + 3].kind, STRING);
399 }
400}