Skip to content

Commit 7abc06b

Browse files
committed
Add proper test for literals and fixed typo bug
1 parent 1ea0238 commit 7abc06b

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

crates/ra_mbe/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
167167
)
168168
}
169169

170-
fn create_rules(macro_definition: &str) -> MacroRules {
170+
pub(crate) fn create_rules(macro_definition: &str) -> MacroRules {
171171
let source_file = ast::SourceFile::parse(macro_definition);
172172
let macro_definition =
173173
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
@@ -176,7 +176,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
176176
crate::MacroRules::parse(&definition_tt).unwrap()
177177
}
178178

179-
fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree {
179+
pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree {
180180
let source_file = ast::SourceFile::parse(invocation);
181181
let macro_invocation =
182182
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
@@ -186,7 +186,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
186186
rules.expand(&invocation_tt).unwrap()
187187
}
188188

189-
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
189+
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
190190
let expanded = expand(rules, invocation);
191191
assert_eq!(expanded.to_string(), expansion);
192192
}
@@ -338,7 +338,7 @@ SOURCE_FILE@[0; 40)
338338
}
339339

340340
#[test]
341-
fn expand_literals_to_item_list() {
341+
fn expand_literals_to_token_tree() {
342342
fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree {
343343
if let tt::TokenTree::Subtree(subtree) = tt {
344344
return &subtree;
@@ -361,6 +361,7 @@ SOURCE_FILE@[0; 40)
361361
let a = 'c';
362362
let c = 1000;
363363
let f = 12E+99_f64;
364+
let s = "rust1";
364365
}
365366
}
366367
}
@@ -375,5 +376,7 @@ SOURCE_FILE@[0; 40)
375376
assert_eq!(to_literal(&stm_tokens[5 + 3]).text, "1000");
376377
// [let] [f] [=] [12E+99_f64] [;]
377378
assert_eq!(to_literal(&stm_tokens[10 + 3]).text, "12E+99_f64");
379+
// [let] [s] [=] ["rust1"] [;]
380+
assert_eq!(to_literal(&stm_tokens[15 + 3]).text, "\"rust1\"");
378381
}
379382
}

crates/ra_mbe/src/syntax_bridge.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ fn convert_tt(
103103
Some(res)
104104
}
105105

106+
#[derive(Debug)]
106107
struct TtTokenSource {
107108
tokens: Vec<TtToken>,
108109
}
109110

111+
#[derive(Debug)]
110112
struct TtToken {
111113
kind: SyntaxKind,
112114
is_joint_to_next: bool,
@@ -355,3 +357,44 @@ impl<'a> TreeSink for TtTreeSink<'a> {
355357
self.inner.error(error, self.text_pos)
356358
}
357359
}
360+
361+
#[cfg(test)]
362+
mod 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+
}

crates/ra_syntax/src/parsing/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn scan_literal_suffix(ptr: &mut Ptr) {
217217

218218
pub fn classify_literal(text: &str) -> Option<Token> {
219219
let tkn = next_token(text);
220-
if tkn.kind.is_literal() || tkn.len.to_usize() != text.len() {
220+
if !tkn.kind.is_literal() || tkn.len.to_usize() != text.len() {
221221
return None;
222222
}
223223

0 commit comments

Comments
 (0)