From 1754161dea7853977905d8dec81d48cf97fdda6f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 1 Aug 2024 13:43:01 -0500 Subject: [PATCH] fix(parser): Correctly bind 'char' boundaries Fixes #553 --- crates/core/src/parser/parser.rs | 5 ++++- tests/errors.rs | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/core/src/parser/parser.rs b/crates/core/src/parser/parser.rs index 92f7ddbcf..a1dbed61e 100644 --- a/crates/core/src/parser/parser.rs +++ b/crates/core/src/parser/parser.rs @@ -649,7 +649,10 @@ impl<'a> InvalidLiquidToken<'a> { let invalid_token_position = invalid_token_span.start_pos(); let (offset_l, offset_c) = invalid_token_position.line_col(); let offset_l = offset_l - 1; - let offset_c = offset_c - 1; + let offset_c = (0..offset_c) + .rev() + .find(|i| invalid_token_position.line_of().is_char_boundary(*i)) + .unwrap_or(0); let end_position = match next_elements.last() { Some(element) => element.as_span().end_pos(), diff --git a/tests/errors.rs b/tests/errors.rs index 6879152f1..98edc88a0 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -1,8 +1,22 @@ +use snapbox::assert_data_eq; +use snapbox::str; + #[test] -#[should_panic] fn test_fuzz() { - let _ = liquid::ParserBuilder::with_stdlib() + match liquid::ParserBuilder::with_stdlib() .build() .unwrap() - .parse("˄{%"); + .parse("˄{%") + { + Ok(_) => panic!("should fail"), + Err(err) => assert_data_eq!(err.to_string(), str![[r#" +liquid: --> 1:3 + | +1 | {% + | ^--- + | + = expected Identifier + +"#]]), + } }