Deduplicate and flexibilize code token & node building 🧺

This commit is contained in:
Laurenz 2020-09-01 14:57:25 +02:00
parent 862f1ccad8
commit b2f3730013
2 changed files with 20 additions and 21 deletions

View File

@ -866,24 +866,20 @@ mod tests {
} }
macro_rules! C { macro_rules! C {
(None, $($line:expr),* $(,)?) => {{ ($lang:expr, $($line:expr),* $(,)?) => {{
let lines = vec![$($line.to_string()) ,*]; let lines = vec![$($line.to_string()) ,*];
SyntaxNode::Code(Code { SyntaxNode::Code(Code {
lang: None, lang: $lang,
block: lines.len() > 1,
lines,
})
}};
(Some($lang:expr), $($line:expr),* $(,)?) => {{
let lines = vec![$($line.to_string()) ,*];
SyntaxNode::Code(Code {
lang: Some(Into::<Spanned<&str>>::into($lang).map(|s| Ident(s.to_string()))),
block: lines.len() > 1, block: lines.len() > 1,
lines, lines,
}) })
}}; }};
} }
fn Lang<'a, T: Into<Spanned<&'a str>>>(lang: T) -> Option<Spanned<Ident>> {
Some(Into::<Spanned<&str>>::into(lang).map(|s| Ident(s.to_string())))
}
macro_rules! F { macro_rules! F {
($($tts:tt)*) => { SyntaxNode::Call(Call!(@$($tts)*)) } ($($tts:tt)*) => { SyntaxNode::Call(Call!(@$($tts)*)) }
} }
@ -1086,10 +1082,10 @@ mod tests {
e!("`hi\nyou" => s(1,3, 1,3, "expected backtick")); e!("`hi\nyou" => s(1,3, 1,3, "expected backtick"));
t!("`hi\\`du`" => R!["hi`du"]); t!("`hi\\`du`" => R!["hi`du"]);
t!("```java System.out.print```" => C![Some("java"), "System.out.print"]); ts!("```java out```" => s(0,0, 0,14, C![Lang(s(0,3, 0,7, "java")), "out"]));
t!("``` console.log(\n\"alert\"\n)" => C![None, "console.log(", "\"alert\"", ")"]); t!("``` console.log(\n\"alert\"\n)" => C![None, "console.log(", "\"alert\"", ")"]);
t!("```typst \r\n Typst uses `\\`` to indicate code blocks" => C![ t!("```typst \r\n Typst uses `\\`` to indicate code blocks" => C![
Some("typst"), " Typst uses ``` to indicate code blocks" Lang("typst"), " Typst uses ``` to indicate code blocks"
]); ]);
e!("``` hi\nyou" => s(1,3, 1,3, "expected backticks")); e!("``` hi\nyou" => s(1,3, 1,3, "expected backticks"));

View File

@ -619,12 +619,15 @@ mod tests {
fn Raw(raw: &str, terminated: bool) -> Token { fn Raw(raw: &str, terminated: bool) -> Token {
Token::Raw { raw, terminated } Token::Raw { raw, terminated }
} }
fn Code<'a>(lang: Option<&'a str>, raw: &'a str, terminated: bool) -> Token<'a> { fn Code<'a>(
Token::Code { lang: Option<Spanned<&'a str>>,
lang: lang.map(Spanned::zero), raw: &'a str,
raw, terminated: bool,
terminated, ) -> Token<'a> {
Token::Code { lang, raw, terminated }
} }
fn Lang<'a, T: Into<Spanned<&'a str>>>(lang: T) -> Option<Spanned<&'a str>> {
Some(Into::<Spanned<&str>>::into(lang))
} }
fn UE(sequence: &str, terminated: bool) -> Token { fn UE(sequence: &str, terminated: bool) -> Token {
Token::UnicodeEscape { sequence, terminated } Token::UnicodeEscape { sequence, terminated }
@ -684,12 +687,12 @@ mod tests {
t!(Body, "#()" => Hashtag, T("()")); t!(Body, "#()" => Hashtag, T("()"));
t!(Body, "`[func]`" => Raw("[func]", true)); t!(Body, "`[func]`" => Raw("[func]", true));
t!(Body, "`]" => Raw("]", false)); t!(Body, "`]" => Raw("]", false));
t!(Body, "\\ " => Backslash, S(0));
t!(Body, "`\\``" => Raw("\\`", true)); t!(Body, "`\\``" => Raw("\\`", true));
t!(Body, "``not code`" => Raw("", true), T("not"), S(0), T("code"), Raw("", false)); t!(Body, "``not code`" => Raw("", true), T("not"), S(0), T("code"), Raw("", false));
t!(Body, "```rust hi```" => Code(Some("rust"), "hi", true)); t!(Body, "```rust hi```" => Code(Lang("rust"), "hi", true));
t!(Body, "``` hi`\\``" => Code(None, "hi`\\``", false)); t!(Body, "``` hi`\\``" => Code(None, "hi`\\``", false));
t!(Body, "```js \r\n document.write(\"go\")" => Code(Some("js"), " document.write(\"go\")", false)); t!(Body, "```js \r\n document.write(\"go\")" => Code(Lang("js"), " document.write(\"go\")", false));
t!(Body, "\\ " => Backslash, S(0));
t!(Header, "_`" => Invalid("_`")); t!(Header, "_`" => Invalid("_`"));
} }