From 62302a0cd1478c17345daf1ac887d48b90ae297e Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Fri, 17 Jan 2025 15:49:57 +0200 Subject: [PATCH] Allow expr and block tags to be multiline This is particularly useful now that we have list and map literals. --- src/compile/lex.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/compile/lex.rs b/src/compile/lex.rs index 18bd26e..c8cdfd6 100644 --- a/src/compile/lex.rs +++ b/src/compile/lex.rs @@ -585,7 +585,7 @@ impl Token { } fn is_whitespace(c: char) -> bool { - matches!(c, '\t' | ' ') + matches!(c, '\t' | ' ' | '\r' | '\n') } #[cfg(feature = "unicode")] @@ -720,6 +720,23 @@ mod tests { ); } + #[test] + fn lex_expr_multiline() { + let tokens = lex("lorem {{\n ipsum }} dolor").unwrap(); + assert_eq!( + tokens, + [ + (Token::Raw, "lorem "), + (Token::BeginExpr, "{{"), + (Token::Whitespace, "\n "), + (Token::Ident, "ipsum"), + (Token::Whitespace, " "), + (Token::EndExpr, "}}"), + (Token::Raw, " dolor") + ] + ); + } + #[test] fn lex_expr_literals() { let tokens = lex("lorem {{ [1, 3] {.} }} dolor").unwrap(); @@ -862,6 +879,23 @@ mod tests { ); } + #[test] + fn lex_block_multiline() { + let tokens = lex("lorem {%\n ipsum %} dolor").unwrap(); + assert_eq!( + tokens, + [ + (Token::Raw, "lorem "), + (Token::BeginBlock, "{%"), + (Token::Whitespace, "\n "), + (Token::Ident, "ipsum"), + (Token::Whitespace, " "), + (Token::EndBlock, "%}"), + (Token::Raw, " dolor") + ] + ); + } + #[test] fn lex_block_and_expr() { let tokens =