From 2db5b83461b356c1ca97718efc7e44cc72b87602 Mon Sep 17 00:00:00 2001 From: Michael Pfaff Date: Mon, 11 Mar 2024 22:42:08 -0400 Subject: [PATCH] Improve comment syntax --- .../javascripts/workers/templates/simple.stpl | 2 +- examples/templates/simple.stpl | 2 +- sailfish-compiler/src/parser.rs | 20 ++++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/assets/javascripts/workers/templates/simple.stpl b/assets/javascripts/workers/templates/simple.stpl index 9ec4dd9..8bd6907 100644 --- a/assets/javascripts/workers/templates/simple.stpl +++ b/assets/javascripts/workers/templates/simple.stpl @@ -1,7 +1,7 @@ - <%# This is a comment %> + <%# This is a comment #%> <% for (i, msg) in messages.iter().enumerate() { %> <% if i == 0 { %>

Hello, world!

diff --git a/examples/templates/simple.stpl b/examples/templates/simple.stpl index 9ec4dd9..8bd6907 100644 --- a/examples/templates/simple.stpl +++ b/examples/templates/simple.stpl @@ -1,7 +1,7 @@ - <%# This is a comment %> + <%# This is a comment #%> <% for (i, msg) in messages.iter().enumerate() { %> <% if i == 0 { %>

Hello, world!

diff --git a/sailfish-compiler/src/parser.rs b/sailfish-compiler/src/parser.rs index ac95609..7ab7ca8 100644 --- a/sailfish-compiler/src/parser.rs +++ b/sailfish-compiler/src/parser.rs @@ -175,8 +175,14 @@ impl<'a> ParseStream<'a> { } if token_kind == TokenKind::Comment { + let block_delim_end = self.block_delimiter.1.as_bytes(); let pos = self.source[start..] - .find(&*self.block_delimiter.1) + .as_bytes() + .windows(1 + block_delim_end.len()) + .enumerate() + .position(|(_, window)| { + window[0] == b'#' && &window[1..] == block_delim_end + }) .ok_or_else(|| self.error("Unterminated comment block"))?; self.take_n(start); @@ -186,7 +192,7 @@ impl<'a> ParseStream<'a> { kind: token_kind, }; - self.take_n(pos + self.block_delimiter.1.len()); + self.take_n(pos + 1 + self.block_delimiter.1.len()); return Ok(token); } @@ -431,7 +437,7 @@ mod tests { #[test] fn non_ascii_delimiter() { - let src = r##"foo <🍣# This is a comment 🍣> bar <🍣= r"🍣>" 🍣> baz <🍣🍣"##; + let src = r##"foo <🍣# This is a comment #🍣> bar <🍣= r"🍣>" 🍣> baz <🍣🍣"##; let parser = Parser::new().delimiter('🍣'); let tokens = parser.parse(src).into_vec().unwrap(); assert_eq!( @@ -449,22 +455,22 @@ mod tests { }, Token { content: " bar ", - offset: 34, + offset: 35, kind: TokenKind::Text }, Token { content: "r\"🍣>\"", - offset: 46, + offset: 47, kind: TokenKind::BufferedCode { escape: true } }, Token { content: " baz ", - offset: 60, + offset: 61, kind: TokenKind::Text }, Token { content: "<🍣", - offset: 65, + offset: 66, kind: TokenKind::Text }, ]