diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-09-01 14:57:25 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-09-01 14:57:25 +0200 |
| commit | b2f37300132376760c6528f9e0044901638d0115 (patch) | |
| tree | 09b6b3f597861484664dd0e0608af37bb91fe92b /src/syntax | |
| parent | 862f1ccad821820c10eef85c204e2b7b0c9f8d84 (diff) | |
Deduplicate and flexibilize code token & node building 🧺
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/parsing.rs | 20 | ||||
| -rw-r--r-- | src/syntax/tokens.rs | 21 |
2 files changed, 20 insertions, 21 deletions
diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index fc19d61e..85a8880d 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -866,24 +866,20 @@ mod tests { } macro_rules! C { - (None, $($line:expr),* $(,)?) => {{ + ($lang:expr, $($line:expr),* $(,)?) => {{ let lines = vec![$($line.to_string()) ,*]; SyntaxNode::Code(Code { - lang: None, - 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()))), + lang: $lang, block: lines.len() > 1, 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 { ($($tts:tt)*) => { SyntaxNode::Call(Call!(@$($tts)*)) } } @@ -1086,10 +1082,10 @@ mod tests { e!("`hi\nyou" => s(1,3, 1,3, "expected backtick")); 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!("```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")); diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 23bad7c8..0c37e992 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -619,12 +619,15 @@ mod tests { fn Raw(raw: &str, terminated: bool) -> Token { Token::Raw { raw, terminated } } - fn Code<'a>(lang: Option<&'a str>, raw: &'a str, terminated: bool) -> Token<'a> { - Token::Code { - lang: lang.map(Spanned::zero), - raw, - terminated, - } + fn Code<'a>( + lang: Option<Spanned<&'a str>>, + raw: &'a str, + terminated: bool, + ) -> 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 { Token::UnicodeEscape { sequence, terminated } @@ -684,12 +687,12 @@ mod tests { t!(Body, "#()" => Hashtag, T("()")); t!(Body, "`[func]`" => Raw("[func]", true)); t!(Body, "`]" => Raw("]", false)); + t!(Body, "\\ " => Backslash, S(0)); t!(Body, "`\\``" => Raw("\\`", true)); 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, "```js \r\n document.write(\"go\")" => Code(Some("js"), " document.write(\"go\")", false)); - t!(Body, "\\ " => Backslash, S(0)); + t!(Body, "```js \r\n document.write(\"go\")" => Code(Lang("js"), " document.write(\"go\")", false)); t!(Header, "_`" => Invalid("_`")); } |
