summaryrefslogtreecommitdiff
path: root/src/syntax/tokens.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-07-26 19:14:23 +0200
committerLaurenz <laurmaedje@gmail.com>2020-07-26 19:14:23 +0200
commit9f400042cbb8aef7fa9b77b080f15a3701abf7a9 (patch)
tree68d407c0b90dfe2c636d4000976556b97ce159e8 /src/syntax/tokens.rs
parentea64ce9aeba411c7dc2f75f39a41935718d03122 (diff)
Streamline parser and tokenizer test code ✈
Diffstat (limited to 'src/syntax/tokens.rs')
-rw-r--r--src/syntax/tokens.rs55
1 files changed, 24 insertions, 31 deletions
diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs
index 0fc52f26..b1bf76e3 100644
--- a/src/syntax/tokens.rs
+++ b/src/syntax/tokens.rs
@@ -532,6 +532,7 @@ pub fn is_identifier(string: &str) -> bool {
#[cfg(test)]
+#[allow(non_snake_case)]
mod tests {
use super::super::test::check;
use super::*;
@@ -552,31 +553,23 @@ mod tests {
Slash,
};
- #[allow(non_snake_case)]
- fn Str(string: &'static str, terminated: bool) -> Token<'static> {
- Token::ExprStr { string, terminated }
- }
-
- #[allow(non_snake_case)]
- fn Raw(raw: &'static str, terminated: bool) -> Token<'static> {
- Token::Raw { raw, terminated }
- }
-
/// Test whether the given string tokenizes into the given list of tokens.
macro_rules! t {
($mode:expr, $source:expr => [$($tokens:tt)*]) => {
- let (exp, spans) = spanned![vec $($tokens)*];
+ let (exp, spans) = span_vec![$($tokens)*];
let found = Tokens::new(Position::ZERO, $source, $mode).collect::<Vec<_>>();
check($source, exp, found, spans);
}
}
- /// Write down a function token compactly.
+ fn Str(string: &str, terminated: bool) -> Token { Token::ExprStr { string, terminated } }
+ fn Raw(raw: &str, terminated: bool) -> Token { Token::Raw { raw, terminated } }
+
macro_rules! func {
($header:expr, Some($($tokens:tt)*), $terminated:expr) => {
Function {
header: $header,
- body: Some(spanned![item $($tokens)*]),
+ body: Some(span_item!(($($tokens)*))),
terminated: $terminated,
}
};
@@ -677,12 +670,12 @@ mod tests {
fn tokenize_functions() {
t!(Body, "a[f]" => [T("a"), func!("f", None, true)]);
t!(Body, "[f]a" => [func!("f", None, true), T("a")]);
- t!(Body, "\n\n[f][ ]" => [S(2), func!("f", Some((0:4, 0:5, " ")), true)]);
- t!(Body, "abc [f][ ]a" => [T("abc"), S(0), func!("f", Some((0:4, 0:5, " ")), true), T("a")]);
+ t!(Body, "\n\n[f][ ]" => [S(2), func!("f", Some(0:4, 0:5, " "), true)]);
+ t!(Body, "abc [f][ ]a" => [T("abc"), S(0), func!("f", Some(0:4, 0:5, " "), true), T("a")]);
t!(Body, "[f: [=][*]]" => [func!("f: [=][*]", None, true)]);
- t!(Body, "[_][[,],]," => [func!("_", Some((0:4, 0:8, "[,],")), true), T(",")]);
- t!(Body, "[=][=][=]" => [func!("=", Some((0:4, 0:5, "=")), true), func!("=", None, true)]);
- t!(Body, "[=][[=][=][=]]" => [func!("=", Some((0:4, 0:13, "[=][=][=]")), true)]);
+ t!(Body, "[_][[,],]," => [func!("_", Some(0:4, 0:8, "[,],"), true), T(",")]);
+ t!(Body, "[=][=][=]" => [func!("=", Some(0:4, 0:5, "="), true), func!("=", None, true)]);
+ t!(Body, "[=][[=][=][=]]" => [func!("=", Some(0:4, 0:13, "[=][=][=]"), true)]);
t!(Header, "[" => [func!("", None, false)]);
t!(Header, "]" => [Invalid("]")]);
}
@@ -696,25 +689,25 @@ mod tests {
t!(Body, "[f: `]" => [func!("f: `", None, true)]);
// End of function with strings and carets in bodies
- t!(Body, "[f][\"]" => [func!("f", Some((0:4, 0:5, "\"")), true)]);
- t!(Body, r#"[f][\"]"# => [func!("f", Some((0:4, 0:6, r#"\""#)), true)]);
- t!(Body, "[f][`]" => [func!("f", Some((0:4, 0:6, "`]")), false)]);
- t!(Body, "[f][\\`]" => [func!("f", Some((0:4, 0:6, "\\`")), true)]);
- t!(Body, "[f][`raw`]" => [func!("f", Some((0:4, 0:9, "`raw`")), true)]);
- t!(Body, "[f][`raw]" => [func!("f", Some((0:4, 0:9, "`raw]")), false)]);
- t!(Body, "[f][`raw]`]" => [func!("f", Some((0:4, 0:10, "`raw]`")), true)]);
- t!(Body, "[f][`\\`]" => [func!("f", Some((0:4, 0:8, "`\\`]")), false)]);
- t!(Body, "[f][`\\\\`]" => [func!("f", Some((0:4, 0:8, "`\\\\`")), true)]);
+ t!(Body, "[f][\"]" => [func!("f", Some(0:4, 0:5, "\""), true)]);
+ t!(Body, r#"[f][\"]"# => [func!("f", Some(0:4, 0:6, r#"\""#), true)]);
+ t!(Body, "[f][`]" => [func!("f", Some(0:4, 0:6, "`]"), false)]);
+ t!(Body, "[f][\\`]" => [func!("f", Some(0:4, 0:6, "\\`"), true)]);
+ t!(Body, "[f][`raw`]" => [func!("f", Some(0:4, 0:9, "`raw`"), true)]);
+ t!(Body, "[f][`raw]" => [func!("f", Some(0:4, 0:9, "`raw]"), false)]);
+ t!(Body, "[f][`raw]`]" => [func!("f", Some(0:4, 0:10, "`raw]`"), true)]);
+ t!(Body, "[f][`\\`]" => [func!("f", Some(0:4, 0:8, "`\\`]"), false)]);
+ t!(Body, "[f][`\\\\`]" => [func!("f", Some(0:4, 0:8, "`\\\\`"), true)]);
// End of function with comments
- t!(Body, "[f][/*]" => [func!("f", Some((0:4, 0:7, "/*]")), false)]);
- t!(Body, "[f][/*`*/]" => [func!("f", Some((0:4, 0:9, "/*`*/")), true)]);
+ t!(Body, "[f][/*]" => [func!("f", Some(0:4, 0:7, "/*]"), false)]);
+ t!(Body, "[f][/*`*/]" => [func!("f", Some(0:4, 0:9, "/*`*/"), true)]);
t!(Body, "[f: //]\n]" => [func!("f: //]\n", None, true)]);
t!(Body, "[f: \"//]\n]" => [func!("f: \"//]\n]", None, false)]);
// End of function with escaped brackets
- t!(Body, "[f][\\]]" => [func!("f", Some((0:4, 0:6, "\\]")), true)]);
- t!(Body, "[f][\\[]" => [func!("f", Some((0:4, 0:6, "\\[")), true)]);
+ t!(Body, "[f][\\]]" => [func!("f", Some(0:4, 0:6, "\\]"), true)]);
+ t!(Body, "[f][\\[]" => [func!("f", Some(0:4, 0:6, "\\["), true)]);
}
#[test]