Skip to content

Commit ae6e86d

Browse files
committed
Fix incorrect line number tracking in lexer
The emit() function was always adding 1 to the line count for ItemText, ItemRawString, ItemLeftDelim, and ItemRightDelim tokens, regardless of actual newlines in the token. This caused incorrect line numbers in error messages, especially when using trim markers ({{- and -}}). Now counts actual newlines in all token types for correct line tracking.
1 parent 3125cd3 commit ae6e86d

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "gtmpl"
3-
version = "0.7.1"
4-
authors = ["Florian Dieminger <me@fiji-flo.de>"]
3+
version = "0.7.2"
4+
authors = ["Florian Dieminger <me@fiji-flo.de>", "Paul Colin Hennig"]
55
description = "The Golang Templating Language for Rust"
66
license = "MIT"
7-
repository = "https://github.com/fiji-flo/gtmpl-rust"
7+
repository = "https://github.com/firstdorsal/gtmpl-rust"
88
documentation = "https://docs.rs/crate/gtmpl"
99
keywords = ["golang", "template", "templating"]
1010
categories = ["template-engine"]

src/lexer.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,11 @@ impl LexerStateMachine {
251251

252252
fn emit(&mut self, t: ItemType) {
253253
let s = &self.input[self.start..self.pos];
254-
let lines = match t {
255-
ItemType::ItemText
256-
| ItemType::ItemRawString
257-
| ItemType::ItemLeftDelim
258-
| ItemType::ItemRightDelim => 1,
259-
_ => s.chars().filter(|c| *c == '\n').count(),
260-
};
254+
// Count actual newlines in the token to track line numbers correctly.
255+
// Previously, ItemText/ItemRawString/ItemLeftDelim/ItemRightDelim always
256+
// added 1 to the line count, which was incorrect (e.g., {{ and }} contain
257+
// no newlines, so they shouldn't increment the line counter).
258+
let lines = s.chars().filter(|c| *c == '\n').count();
261259
self.items_sender
262260
.send(Item::new(t, self.start, s, self.line))
263261
.unwrap();

src/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ mod tests_mocked {
891891
let r = p.parse_tree();
892892
assert_eq!(
893893
r.err().unwrap().to_string(),
894-
"template: foo:2:function eq not defined"
894+
"template: foo:1:function eq not defined"
895895
);
896896
let funcs = &["eq"];
897897
let mut p = make_parser_with_funcs(r#"{{ if eq .foo "bar" }} 2000 {{ end }}"#, funcs);

0 commit comments

Comments
 (0)