feat: Added Rust-style error messages for template errors.

This commit is contained in:
Spencer Brower
2026-07-21 12:23:23 -04:00
parent b239bf2d87
commit f04532229b
13 changed files with 1786 additions and 121 deletions
+332
View File
@@ -0,0 +1,332 @@
package mustache
import "core:fmt"
import "core:os"
import "core:strings"
import "core:terminal/ansi"
import "core:unicode/utf8"
// line_col returns the 1-indexed line and column for a byte offset in source.
// Newlines ('\n') separate lines; '\r' is treated as part of '\r\n'. Column is
// counted in bytes from the start of the line.
line_col :: proc(source: string, pos_in: int) -> (line: int, col: int) {
pos := pos_in
if pos < 0 {
return 1, 1
}
if pos > len(source) {
pos = len(source)
}
line = 1
col = 1
for i := 0; i < pos; i += 1 {
if source[i] == '\n' {
line += 1
col = 1
} else {
col += 1
}
}
return
}
// line_text returns the Nth (1-indexed) line of source, without the trailing
// newline. Returns "" if line is out of range.
line_text :: proc(source: string, line: int) -> string {
if line < 1 {
return ""
}
current := 1
start := 0
for i := 0; i < len(source); i += 1 {
if current == line {
end := i
for end < len(source) && source[end] != '\n' {
end += 1
}
return source[start:end]
}
if source[i] == '\n' {
current += 1
start = i + 1
}
}
if current == line {
return source[start:]
}
return ""
}
// context_extent returns the byte offset of the start of the line containing
// pos, plus the byte offsets of the start and end of the mustache tag at pos.
// Used to underline the offending tag. If pos is not inside a tag, the
// returned [token_start, token_end) is a single rune at pos.
context_extent :: proc(
source: string,
pos_in: int,
) -> (
line_start: int,
token_start: int,
token_end: int,
) {
pos := pos_in
if pos < 0 {
return 0, 0, 0
}
if pos >= len(source) {
pos = len(source) - 1
}
line_start = pos
for line_start > 0 && source[line_start - 1] != '\n' {
line_start -= 1
}
// Scan forward from line_start for `{{ ... }}` tags. If pos falls inside
// any tag's byte range, return that tag's extent.
i := line_start
for i + 1 < len(source) {
if source[i] == '{' && source[i + 1] == '{' {
tag_start := i
// Find closing }}
j := i + 2
depth := 1
for j + 1 < len(source) && depth > 0 {
if source[j] == '{' && source[j + 1] == '{' {
depth += 1
j += 2
} else if source[j] == '}' && source[j + 1] == '}' {
depth -= 1
j += 2
} else {
j += 1
}
}
tag_end := j
if pos >= tag_start && pos < tag_end {
return line_start, tag_start, tag_end
}
i = tag_end
} else {
i += 1
}
}
// Not inside a tag — underline a single rune at pos.
return line_start, pos, pos + 1
}
// should_colorize returns true if stderr is a TTY and color output is wanted.
should_colorize :: proc() -> bool {
return os.is_tty(os.stderr)
}
// count_lines returns the number of '\n'-separated lines in source.
// A trailing newline does not add an extra line.
count_lines :: proc(source: string) -> int {
if len(source) == 0 {
return 1
}
n := 1
for c in source {
if c == '\n' {
n += 1
}
}
// Drop phantom last line if source ends with '\n'.
if len(source) > 0 && source[len(source) - 1] == '\n' {
n -= 1
}
return n
}
// digit_count returns the number of decimal digits in n (min 1).
digit_count :: proc(n: int) -> int {
if n <= 0 {
return 1
}
c := 0
x := n
for x > 0 {
c += 1
x /= 10
}
return c
}
// display_width returns the number of terminal cells `s` occupies.
// For ASCII this is byte length; for UTF-8 we count runes (combining
// marks and wide CJK chars are still approximate).
display_width :: proc(s: string) -> int {
return utf8.rune_count_in_string(s)
}
// format_error produces a rust-style multi-line diagnostic string.
//
// <msg>
// --> <path>:<line>:<col>
// |
// N | <source line N-2>
// N | <source line N-1>
// N | <source line N — the error line>
// | ^^^^^^^^^^^ <hint>
// N | <source line N+1>
// N | <source line N+2>
// |
//
// `context_before`/`context_after` lines of context are shown around the
// error line. Line numbers are right-aligned to the width of the largest
// line number shown.
format_error :: proc(
path: string,
source: string,
pos: int,
msg: string,
hint: string = "",
context_before: int = 2,
context_after: int = 2,
colorize: bool = false,
) -> string {
line, col := line_col(source, pos)
total_lines := count_lines(source)
start_line := line - context_before
if start_line < 1 {
start_line = 1
}
end_line := line + context_after
if end_line > total_lines {
end_line = total_lines
}
// Width of the line-number column (right-align).
width := digit_count(end_line)
if width < 1 {
width = 1
}
sb := strings.builder_make(context.temp_allocator)
defer strings.builder_destroy(&sb)
color := colorize
red, faint, reset := "", "", ""
if color {
red = ansi.CSI + ansi.FG_RED + ansi.SGR
faint = ansi.CSI + ansi.FAINT + ansi.SGR
reset = ansi.CSI + ansi.RESET + ansi.SGR
}
// Header line: message.
strings.write_string(&sb, msg)
strings.write_byte(&sb, '\n')
// Location line: " --> path:line:col" (width spaces + arrow).
strings.write_string(&sb, faint)
for _ in 0 ..< width {
strings.write_byte(&sb, ' ')
}
strings.write_string(&sb, "--> ")
strings.write_string(&sb, reset)
strings.write_string(&sb, fmt.tprintf("%s:%d:%d\n", path, line, col))
// Top gutter line.
write_gutter(&sb, width, faint, reset)
// Caret extent for the error line.
_, token_start, token_end := context_extent(source, pos)
line_start, _, _ := context_extent(source, pos)
caret_start_col := token_start - line_start + 1
caret_end_col := token_end - line_start + 1
if caret_end_col <= caret_start_col {
caret_end_col = caret_start_col + 1
}
// Context lines.
for n in start_line ..= end_line {
// Line number (right-aligned, faint).
num_str := fmt.tprintf("%d", n)
strings.write_string(&sb, faint)
for _ in 0 ..< width - len(num_str) {
strings.write_byte(&sb, ' ')
}
strings.write_string(&sb, num_str)
strings.write_string(&sb, " | ")
strings.write_string(&sb, reset)
strings.write_string(&sb, line_text(source, n))
strings.write_byte(&sb, '\n')
// After the error line, emit the caret row.
if n == line {
strings.write_string(&sb, faint)
for _ in 0 ..< width + 1 {
strings.write_byte(&sb, ' ')
}
strings.write_string(&sb, "| ")
strings.write_string(&sb, reset)
for _ in 1 ..< caret_start_col {
strings.write_byte(&sb, ' ')
}
if color {
strings.write_string(&sb, red)
}
for _ in 0 ..< caret_end_col - caret_start_col {
strings.write_byte(&sb, '^')
}
if color {
strings.write_string(&sb, reset)
}
if hint != "" {
strings.write_byte(&sb, ' ')
if color {
strings.write_string(&sb, faint)
}
strings.write_string(&sb, hint)
if color {
strings.write_string(&sb, reset)
}
}
strings.write_byte(&sb, '\n')
}
}
// Trailing gutter line for visual closure.
write_gutter(&sb, width, faint, reset)
return strings.to_string(sb)
}
// write_gutter emits a faint pipe-only gutter line: `<width+1 spaces> |`.
write_gutter :: proc(sb: ^strings.Builder, width: int, faint: string, reset: string) {
strings.write_string(sb, faint)
for _ in 0 ..< width + 1 {
strings.write_byte(sb, ' ')
}
strings.write_string(sb, "|")
strings.write_string(sb, reset)
strings.write_byte(sb, '\n')
}
// format_render_error dispatches on Render_Error variant and produces a
// diagnostic for it. Returns "" for nil errors.
format_render_error :: proc(err: Render_Error, tmpl: Template, colorize: bool = false) -> string {
if err == nil {
return ""
}
switch e in err {
case Syntax_Error:
path := tmpl.path
if path == "" {
path = "<input>"
}
return format_error(path, tmpl.source, e.pos, e.msg, colorize = colorize)
case Data_Error:
path := tmpl.path
if path == "" {
path = "<input>"
}
return format_error(path, tmpl.source, e.pos, e.msg, colorize = colorize)
}
return ""
}
+576
View File
@@ -0,0 +1,576 @@
#+test
package mustache
import "core:fmt"
import "core:strings"
import "core:testing"
// ---------------------------------------------------------------------------
// line_col / line_text / count_lines / digit_count — primitive helpers
// ---------------------------------------------------------------------------
@(test)
test_line_col_basic :: proc(t: ^testing.T) {
src := "abc\ndef\nghi"
cases := [?]struct {
pos: int,
line: int,
col: int,
}{{0, 1, 1}, {2, 1, 3}, {3, 1, 4}, {4, 2, 1}, {6, 2, 3}, {7, 2, 4}, {8, 3, 1}}
for c in cases {
l, col := line_col(src, c.pos)
testing.expect(t, l == c.line, fmt.tprintf("pos %d: line %d, want %d", c.pos, l, c.line))
testing.expect(t, col == c.col, fmt.tprintf("pos %d: col %d, want %d", c.pos, col, c.col))
}
}
@(test)
test_line_col_empty :: proc(t: ^testing.T) {
l, col := line_col("", 0)
testing.expect_value(t, l, 1)
testing.expect_value(t, col, 1)
}
@(test)
test_line_col_negative :: proc(t: ^testing.T) {
l, col := line_col("abc", -1)
testing.expect_value(t, l, 1)
testing.expect_value(t, col, 1)
}
@(test)
test_line_col_past_end :: proc(t: ^testing.T) {
l, col := line_col("abc", 100)
testing.expect_value(t, l, 1)
testing.expect_value(t, col, 4)
}
@(test)
test_line_text_first :: proc(t: ^testing.T) {
src := "first\nsecond\nthird"
testing.expect_value(t, line_text(src, 1), "first")
testing.expect_value(t, line_text(src, 2), "second")
testing.expect_value(t, line_text(src, 3), "third")
}
@(test)
test_line_text_trailing_newline :: proc(t: ^testing.T) {
src := "first\nsecond\n"
testing.expect_value(t, line_text(src, 1), "first")
testing.expect_value(t, line_text(src, 2), "second")
testing.expect_value(t, line_text(src, 3), "")
}
@(test)
test_line_text_out_of_range :: proc(t: ^testing.T) {
testing.expect_value(t, line_text("abc", 5), "")
testing.expect_value(t, line_text("abc", 0), "")
}
@(test)
test_count_lines :: proc(t: ^testing.T) {
testing.expect_value(t, count_lines(""), 1)
testing.expect_value(t, count_lines("abc"), 1)
testing.expect_value(t, count_lines("a\nb"), 2)
testing.expect_value(t, count_lines("a\nb\n"), 2)
testing.expect_value(t, count_lines("a\nb\nc"), 3)
}
@(test)
test_digit_count :: proc(t: ^testing.T) {
testing.expect_value(t, digit_count(0), 1)
testing.expect_value(t, digit_count(1), 1)
testing.expect_value(t, digit_count(9), 1)
testing.expect_value(t, digit_count(10), 2)
testing.expect_value(t, digit_count(99), 2)
testing.expect_value(t, digit_count(100), 3)
testing.expect_value(t, digit_count(-5), 1)
}
// ---------------------------------------------------------------------------
// format_error — golden output tests
// ---------------------------------------------------------------------------
@(test)
test_format_error_basic :: proc(t: ^testing.T) {
src := "line 1\nline 2\n{{bad}}\nline 4\nline 5"
out := format_error("p.html", src, 14, "unknown key 'bad'", "", colorize = false)
expected := `unknown key 'bad'
--> p.html:3:1
|
1 | line 1
2 | line 2
3 | {{bad}}
| ^^^^^^^
4 | line 4
5 | line 5
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_with_hint :: proc(t: ^testing.T) {
src := "line 1\nline 2\n{{titel}}\nline 4\nline 5"
out := format_error(
"post.html",
src,
14,
"unknown key 'titel'",
"did you mean 'title'?",
colorize = false,
)
expected := `unknown key 'titel'
--> post.html:3:1
|
1 | line 1
2 | line 2
3 | {{titel}}
| ^^^^^^^^^ did you mean 'title'?
4 | line 4
5 | line 5
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_no_hint_omits_trailing_space :: proc(t: ^testing.T) {
src := "{{bad}}"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
// Caret line ends immediately after the carets — no trailing space.
testing.expect(t, strings.contains(out, "^^^^^^^\n"), out)
testing.expect(t, !strings.contains(out, "^^^^^^^ \n"), out)
}
// ---------------------------------------------------------------------------
// Edge cases — context window clamping
// ---------------------------------------------------------------------------
@(test)
test_format_error_first_line_only_after_context :: proc(t: ^testing.T) {
src := "{{bad}}\nline 2\nline 3\nline 4\nline 5"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
expected := `msg
--> p.html:1:1
|
1 | {{bad}}
| ^^^^^^^
2 | line 2
3 | line 3
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_last_line_only_before_context :: proc(t: ^testing.T) {
src := "line 1\nline 2\nline 3\nline 4\n{{bad}}"
out := format_error("p.html", src, 28, "msg", "", colorize = false)
expected := `msg
--> p.html:5:1
|
3 | line 3
4 | line 4
5 | {{bad}}
| ^^^^^^^
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_short_source_clamped :: proc(t: ^testing.T) {
src := "x\n{{bad}}\ny"
out := format_error("p.html", src, 2, "msg", "", colorize = false)
expected := `msg
--> p.html:2:1
|
1 | x
2 | {{bad}}
| ^^^^^^^
3 | y
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_single_line_source :: proc(t: ^testing.T) {
src := "{{bad}}"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
expected := `msg
--> p.html:1:1
|
1 | {{bad}}
| ^^^^^^^
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_format_error_two_digit_line_numbers :: proc(t: ^testing.T) {
// 12-line source; error on line 9. end_line=11 → width=2.
src := "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\n{{bad}}\nl10\nl11\nl12"
// Position of `{{bad}}`: 8 lines of "l0N\n" = 8*4 = 32 bytes.
out := format_error("p.html", src, 32, "msg", "", colorize = false)
testing.expect(t, strings.contains(out, " --> p.html:9:1\n"), out)
testing.expect(t, strings.contains(out, " |\n"), out)
testing.expect(t, strings.contains(out, " 7 | l07\n"), out)
testing.expect(t, strings.contains(out, " 9 | {{bad}}\n"), out)
testing.expect(t, strings.contains(out, "11 | l11\n"), out)
}
@(test)
test_format_error_three_digit_line_numbers :: proc(t: ^testing.T) {
// 102-line source; error on line 100. Width=3 because end_line=102 has 3 digits.
parts: [dynamic]string
defer delete(parts)
for i in 1 ..= 99 {
append(&parts, fmt.tprintf("l%03d", i))
}
append(&parts, "{{bad}}")
append(&parts, "l101")
append(&parts, "l102")
src := strings.join(parts[:], "\n", context.temp_allocator)
// Find byte position of "{{bad}}": after 99 lines.
pos := 0
for i in 1 ..= 99 {
pos += len(parts[i - 1]) + 1
}
out := format_error("p.html", src, pos, "msg", "", colorize = false)
testing.expect(t, strings.contains(out, " --> p.html:100:1\n"), out)
testing.expect(t, strings.contains(out, " |\n"), out)
testing.expect(t, strings.contains(out, " 98 | l098\n"), out)
testing.expect(t, strings.contains(out, "100 | {{bad}}\n"), out)
testing.expect(t, strings.contains(out, "102 | l102\n"), out)
}
// ---------------------------------------------------------------------------
// Caret position
// ---------------------------------------------------------------------------
@(test)
test_caret_at_column_1 :: proc(t: ^testing.T) {
src := "{{bad}} at start"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
// Caret line should start with "^" right after "| " (no leading spaces).
testing.expect(t, strings.contains(out, " | ^^^^^^^\n"), out)
}
@(test)
test_caret_at_column_N :: proc(t: ^testing.T) {
src := " {{bad}}"
// pos=4 is the first '{'. Line 1, col 5.
out := format_error("p.html", src, 4, "msg", "", colorize = false)
// 4 leading spaces, then 7 carets.
testing.expect(t, strings.contains(out, " | ^^^^^^^\n"), out)
}
@(test)
test_caret_width_matches_token :: proc(t: ^testing.T) {
src := "{{x}}"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
// {{x}} is 5 chars wide.
testing.expect(t, strings.contains(out, " | ^^^^^\n"), out)
}
// ---------------------------------------------------------------------------
// Context count
// ---------------------------------------------------------------------------
@(test)
test_context_before_zero :: proc(t: ^testing.T) {
src := "l1\nl2\nl3\n{{bad}}\nl5\nl6"
out := format_error(
"p.html",
src,
9,
"msg",
"",
context_before = 0,
context_after = 1,
colorize = false,
)
expected := `msg
--> p.html:4:1
|
4 | {{bad}}
| ^^^^^^^
5 | l5
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_context_after_zero :: proc(t: ^testing.T) {
src := "l1\nl2\nl3\n{{bad}}\nl5\nl6"
out := format_error(
"p.html",
src,
9,
"msg",
"",
context_before = 1,
context_after = 0,
colorize = false,
)
expected := `msg
--> p.html:4:1
|
3 | l3
4 | {{bad}}
| ^^^^^^^
|
`
testing.expect_value(t, out, expected)
}
@(test)
test_context_both_zero :: proc(t: ^testing.T) {
src := "l1\nl2\nl3\n{{bad}}\nl5\nl6"
out := format_error(
"p.html",
src,
9,
"msg",
"",
context_before = 0,
context_after = 0,
colorize = false,
)
expected := `msg
--> p.html:4:1
|
4 | {{bad}}
| ^^^^^^^
|
`
testing.expect_value(t, out, expected)
}
// ---------------------------------------------------------------------------
// Gutter/alignment
// ---------------------------------------------------------------------------
@(test)
test_gutter_pipes_align_with_source_pipe :: proc(t: ^testing.T) {
src := "l1\n{{bad}}\nl3"
out := format_error("p.html", src, 3, "msg", "", colorize = false)
// All "|" characters should appear at the same column.
// For width=1: source line is "N | ...", so "|" at col 2.
// Empty gutter is " |" (width+1 spaces + "|"), so "|" at col 2.
lines := strings.split(out, "\n", context.temp_allocator)
defer delete(lines)
pipe_col := -1
for line in lines {
idx := strings.index(line, "|")
if idx < 0 {
continue
}
if pipe_col < 0 {
pipe_col = idx
} else {
testing.expect_value(t, idx, pipe_col)
}
}
}
@(test)
test_arrow_points_at_pipe :: proc(t: ^testing.T) {
src := "{{bad}}"
out := format_error("p.html", src, 0, "msg", "", colorize = false)
// For width=1: arrow line is " --> ..." so ">" at col 3.
// Pipe lines are " |" so "|" at col 2.
lines := strings.split(out, "\n", context.temp_allocator)
defer delete(lines)
pipe_col := -1
for line in lines {
idx := strings.index(line, "|")
if idx >= 0 {
pipe_col = idx
break
}
}
testing.expect(t, pipe_col >= 0, "expected pipe in output")
// Find the arrow line specifically and verify its ">" column.
arrow_col := -1
for line in lines {
idx := strings.index(line, "-->")
if idx >= 0 {
arrow_col = idx + 2 // ">" is the last char of "-->"
break
}
}
testing.expect(t, arrow_col >= 0, "expected --> in output")
testing.expect_value(t, arrow_col, pipe_col + 1)
}
// ---------------------------------------------------------------------------
// format_render_error — dispatch
// ---------------------------------------------------------------------------
@(test)
test_format_render_error_dispatch :: proc(t: ^testing.T) {
src := "{{#unclosed}}\ncontent"
tmpl, parse_err := parse(src, "test.html")
testing.expect(t, parse_err != nil, "should fail to parse unclosed section")
if parse_err == nil {
return
}
#partial switch e in parse_err {
case Syntax_Error:
out := format_error("test.html", src, e.pos, e.msg, colorize = false)
testing.expect(t, strings.contains(out, "unclosed section"), out)
testing.expect(t, strings.contains(out, "test.html:"), out)
}
}
@(test)
test_diagnostic_for_pipe_error :: proc(t: ^testing.T) {
src := "{{#name | group_by year}}x{{/name}}"
tmpl, perr := parse(src, "test.html")
testing.expect(t, perr == nil, "should parse")
if perr != nil {
return
}
defer delete_template(&tmpl)
Data :: struct {
name: string,
}
_, rerr := render(tmpl, Data{name = "hello"})
testing.expect(t, rerr != nil, "should fail to render")
if rerr == nil {
return
}
out := format_render_error(rerr, tmpl, colorize = false)
testing.expect(t, strings.contains(out, "group_by expects a list"), out)
testing.expect(t, strings.contains(out, "test.html:"), out)
}
// ---------------------------------------------------------------------------
// Parser error messages preserve double braces in tag syntax
// ---------------------------------------------------------------------------
@(test)
test_parse_error_expected_got_keeps_double_braces :: proc(t: ^testing.T) {
src := "{{#content}}body{{/cotent}}"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{/content}}"),
fmt.tprintf("msg should contain literal {{/content}}, got %q", e.msg),
)
testing.expect(
t,
strings.contains(e.msg, "{{/cotent}}"),
fmt.tprintf("msg should contain literal {{/cotent}}, got %q", e.msg),
)
}
}
@(test)
test_parse_error_unclosed_section_keeps_double_braces :: proc(t: ^testing.T) {
src := "{{#content}}body"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{#content}}"),
fmt.tprintf("msg should contain literal {{#content}}, got %q", e.msg),
)
}
}
@(test)
test_parse_error_unexpected_close_keeps_double_braces :: proc(t: ^testing.T) {
src := "text{{/content}}"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{/content}}"),
fmt.tprintf("msg should contain literal {{/content}}, got %q", e.msg),
)
}
}
@(test)
test_parse_error_pipe_in_close_tag_keeps_double_braces :: proc(t: ^testing.T) {
src := "{{#posts | group_by year}}x{{/posts | group_by year}}"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{/"),
fmt.tprintf("msg should contain literal '{{/', got %q", e.msg),
)
}
}
@(test)
test_parse_error_pipe_parse_in_section_keeps_double_braces :: proc(t: ^testing.T) {
src := "{{#posts |}}x{{/posts}}"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{#"),
fmt.tprintf("msg should contain literal '{{#', got %q", e.msg),
)
}
}
@(test)
test_parse_error_pipe_parse_in_inverted_keeps_double_braces :: proc(t: ^testing.T) {
src := "{{^posts |}}x{{/posts}}"
_, err := parse(src, "test.html")
testing.expect(t, err != nil, "should fail to parse")
if err == nil {
return
}
#partial switch e in err {
case Syntax_Error:
testing.expect(
t,
strings.contains(e.msg, "{{^"),
fmt.tprintf("msg should contain literal '{{^', got %q", e.msg),
)
}
}
+236 -44
View File
@@ -1,28 +1,27 @@
package mustache
import "core:fmt"
import "core:log"
import "core:strings"
// ---------------------------------------------------------------------------
// Error types
// ---------------------------------------------------------------------------
// TODO: I don't think this is the best way to do our errors.
Syntax_Error :: struct {
msg: string,
pos: int,
}
Data_Error :: struct {
msg: string,
}
Partial_Error :: struct {
name: string,
msg: string,
pos: int,
}
Render_Error :: union {
Syntax_Error,
Data_Error,
Partial_Error,
}
// ---------------------------------------------------------------------------
@@ -50,6 +49,7 @@ Node :: struct {
first_child: int,
child_count: int,
content: string,
pos: int,
}
// node_span returns the number of flat-array entries a node occupies:
@@ -67,12 +67,14 @@ node_span :: proc(n: Node) -> int {
Template :: struct {
nodes: [dynamic]Node,
source: string,
path: string,
}
Block_Override :: struct {
all_nodes: []Node,
first: int,
count: int,
source: Template,
}
delete_template :: proc(tmpl: ^Template) {
@@ -94,6 +96,7 @@ delete_partials :: proc(partials: map[string]Template) {
parse :: proc(
source: string,
path := "",
allocator := context.allocator,
tokens_allocator := context.temp_allocator,
) -> (
@@ -111,6 +114,7 @@ parse :: proc(
return {}, err
}
tmpl.source = source
tmpl.path = path
deindent_blocks(tmpl.nodes[:], 0, len(tmpl.nodes), allocator)
return tmpl, nil
}
@@ -133,7 +137,7 @@ render :: proc(
append(&ctx, data)
all_nodes := tmpl.nodes[:]
err = render_nodes(all_nodes, all_nodes, &ctx, partials, &builder)
err = render_nodes(tmpl, all_nodes, all_nodes, &ctx, partials, &builder)
if err != nil {
return result, err
}
@@ -158,7 +162,7 @@ parse_tokens :: proc(
) {
nodes = make([dynamic]Node, 0, len(tokens), allocator)
pos := 0
err = parse_section(tokens, &pos, &nodes, "", source, allocator)
err = parse_section(tokens, &pos, &nodes, "", source, allocator, 0)
return
}
@@ -169,22 +173,23 @@ parse_section :: proc(
end_tag: string,
source: string,
allocator := context.allocator,
open_pos: int = 0,
) -> Render_Error {
for pos^ < len(tokens) {
tok := tokens[pos^]
switch tok.kind {
case .Text:
append(nodes, Node{kind = .Text, text = tok.value, first_child = -1})
append(nodes, Node{kind = .Text, text = tok.value, first_child = -1, pos = tok.pos})
pos^ += 1
case .Variable:
idx := len(nodes)
append(nodes, Node{kind = .Variable, first_child = -1})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters)
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
if perr != nil {
return Syntax_Error {
msg = fmt.tprintf("pipe parse error in '{{%s}}': %v", tok.value, perr),
msg = fmt.tprintf("pipe parse error in '{{{{%s}}}}': %v", tok.value, perr),
pos = tok.pos,
}
}
@@ -194,10 +199,10 @@ parse_section :: proc(
case .Unescaped:
idx := len(nodes)
append(nodes, Node{kind = .Unescaped, first_child = -1})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters)
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
if perr != nil {
return Syntax_Error {
msg = fmt.tprintf("pipe parse error in '{{&%s}}': %v", tok.value, perr),
msg = fmt.tprintf("pipe parse error in '{{{{&%s}}}}': %v", tok.value, perr),
pos = tok.pos,
}
}
@@ -212,16 +217,16 @@ parse_section :: proc(
idx := len(nodes)
content_start := 0
if pos^ < len(tokens) {content_start = tokens[pos^].pos}
append(nodes, Node{kind = .Section, first_child = -1})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters)
append(nodes, Node{kind = .Section, first_child = -1, pos = tok.pos})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
if perr != nil {
return Syntax_Error {
msg = fmt.tprintf("pipe parse error in '{{#%s}}': %v", tok.value, perr),
msg = fmt.tprintf("pipe parse error in '{{{{#%s}}}}': %v", tok.value, perr),
pos = tok.pos,
}
}
nodes[idx].key = pipe_key
parse_section(tokens, pos, nodes, pipe_key, source, allocator) or_return
parse_section(tokens, pos, nodes, pipe_key, source, allocator, tok.pos) or_return
close_pos := 0
if pos^ - 1 >= 0 && pos^ - 1 < len(tokens) {close_pos = tokens[pos^ - 1].pos}
nodes[idx].first_child = idx + 1
@@ -233,16 +238,16 @@ parse_section :: proc(
idx := len(nodes)
content_start := 0
if pos^ < len(tokens) {content_start = tokens[pos^].pos}
append(nodes, Node{kind = .Inverted, first_child = -1})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters)
append(nodes, Node{kind = .Inverted, first_child = -1, pos = tok.pos})
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
if perr != nil {
return Syntax_Error {
msg = fmt.tprintf("pipe parse error in '{{^%s}}': %v", tok.value, perr),
msg = fmt.tprintf("pipe parse error in '{{{{^%s}}}}': %v", tok.value, perr),
pos = tok.pos,
}
}
nodes[idx].key = pipe_key
parse_section(tokens, pos, nodes, pipe_key, source, allocator) or_return
parse_section(tokens, pos, nodes, pipe_key, source, allocator, tok.pos) or_return
close_pos := 0
if pos^ - 1 >= 0 && pos^ - 1 < len(tokens) {close_pos = tokens[pos^ - 1].pos}
nodes[idx].first_child = idx + 1
@@ -253,7 +258,7 @@ parse_section :: proc(
if strings.contains(tok.value, "|") {
return Syntax_Error {
msg = fmt.tprintf(
"pipe expression not allowed in close tag '{{/%s}}' — use the bare key",
"pipe expression not allowed in close tag '{{{{/%s}}}}' — use the bare key",
tok.value,
),
pos = tok.pos,
@@ -265,12 +270,12 @@ parse_section :: proc(
}
if end_tag == "" {
return Syntax_Error {
msg = fmt.tprintf("unexpected {{/%s}}", tok.value),
msg = fmt.tprintf("unexpected {{{{/%s}}}}", tok.value),
pos = tok.pos,
}
}
return Syntax_Error {
msg = fmt.tprintf("expected {{/%s}}, got {{/%s}}", end_tag, tok.value),
msg = fmt.tprintf("expected {{{{/%s}}}}, got {{{{/%s}}}}", end_tag, tok.value),
pos = tok.pos,
}
@@ -283,6 +288,7 @@ parse_section :: proc(
is_dynamic = tok.is_dynamic,
indent = tok.indent,
first_child = -1,
pos = tok.pos,
},
)
pos^ += 1
@@ -292,9 +298,15 @@ parse_section :: proc(
idx := len(nodes)
append(
nodes,
Node{kind = .Parent, key = tok.value, indent = tok.indent, first_child = -1},
Node {
kind = .Parent,
key = tok.value,
indent = tok.indent,
first_child = -1,
pos = tok.pos,
},
)
parse_section(tokens, pos, nodes, tok.value, source, allocator) or_return
parse_section(tokens, pos, nodes, tok.value, source, allocator, tok.pos) or_return
nodes[idx].first_child = idx + 1
nodes[idx].child_count = len(nodes) - idx - 1
@@ -303,16 +315,25 @@ parse_section :: proc(
idx := len(nodes)
append(
nodes,
Node{kind = .Block, key = tok.value, indent = tok.indent, first_child = -1},
Node {
kind = .Block,
key = tok.value,
indent = tok.indent,
first_child = -1,
pos = tok.pos,
},
)
parse_section(tokens, pos, nodes, tok.value, source, allocator) or_return
parse_section(tokens, pos, nodes, tok.value, source, allocator, tok.pos) or_return
nodes[idx].first_child = idx + 1
nodes[idx].child_count = len(nodes) - idx - 1
}
}
if end_tag != "" {
return Syntax_Error{msg = fmt.tprintf("unclosed section '{{#%s}}'", end_tag)}
return Syntax_Error {
msg = fmt.tprintf("unclosed section '{{{{#%s}}}}'", end_tag),
pos = open_pos,
}
}
return nil
}
@@ -476,11 +497,20 @@ render_template :: proc(
indent: string,
) -> Render_Error {
if len(indent) > 0 && len(pt.source) > 0 {
// Per Mustache spec: the partial's source is indented before rendering,
// not its output. This is necessary so that data-injected newlines
// (e.g. from `{{{content}}}` where content contains `\n`) do NOT pick
// up the indent — only source-level line breaks do.
indented := indent_lines(pt.source, indent)
reparse := parse(indented, context.temp_allocator, context.temp_allocator) or_return
return render_nodes(reparse.nodes[:], reparse.nodes[:], ctx, partials, b, blocks)
reparse := parse(
indented,
pt.path,
context.temp_allocator,
context.temp_allocator,
) or_return
return render_nodes(reparse, reparse.nodes[:], reparse.nodes[:], ctx, partials, b, blocks)
}
return render_nodes(pt.nodes[:], pt.nodes[:], ctx, partials, b, blocks)
return render_nodes(pt, pt.nodes[:], pt.nodes[:], ctx, partials, b, blocks)
}
// ---------------------------------------------------------------------------
@@ -488,6 +518,7 @@ render_template :: proc(
// ---------------------------------------------------------------------------
render_nodes :: proc(
current: Template,
all_nodes: []Node,
nodes: []Node,
ctx: ^[dynamic]any,
@@ -505,19 +536,28 @@ render_nodes :: proc(
case .Variable:
val := resolve_name(node.key, ctx[:])
if val == nil {
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:])
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
if perr != nil {
return perr
}
val = transformed
}
if result_str, ok := call_interp_lambda(val); ok {
sub_tpl, perr := parse(result_str, context.temp_allocator, context.temp_allocator)
sub_tpl, perr := parse(
result_str,
fmt.tprintf("<lambda output from '%s'>", node.key),
context.temp_allocator,
context.temp_allocator,
)
if perr == nil {
temp: strings.Builder
strings.builder_init(&temp, context.temp_allocator)
render_nodes(
sub_tpl,
sub_tpl.nodes[:],
sub_tpl.nodes[:],
ctx,
@@ -534,19 +574,28 @@ render_nodes :: proc(
case .Unescaped:
val := resolve_name(node.key, ctx[:])
if val == nil {
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:])
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
if perr != nil {
return perr
}
val = transformed
}
if result_str, ok := call_interp_lambda(val); ok {
sub_tpl, perr := parse(result_str, context.temp_allocator, context.temp_allocator)
sub_tpl, perr := parse(
result_str,
fmt.tprintf("<lambda output from '%s'>", node.key),
context.temp_allocator,
context.temp_allocator,
)
if perr == nil {
temp: strings.Builder
strings.builder_init(&temp, context.temp_allocator)
render_nodes(
sub_tpl,
sub_tpl.nodes[:],
sub_tpl.nodes[:],
ctx,
@@ -563,17 +612,26 @@ render_nodes :: proc(
case .Section:
val := resolve_name(node.key, ctx[:])
if val == nil {
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:])
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
if perr != nil {
return perr
}
val = transformed
}
if result_str, ok := call_section_lambda(val, node.content); ok {
sub_tpl, perr := parse(result_str, context.temp_allocator, context.temp_allocator)
sub_tpl, perr := parse(
result_str,
fmt.tprintf("<lambda output from '%s'>", node.key),
context.temp_allocator,
context.temp_allocator,
)
if perr == nil {
render_nodes(
sub_tpl,
sub_tpl.nodes[:],
sub_tpl.nodes[:],
ctx,
@@ -590,20 +648,31 @@ render_nodes :: proc(
elem := extract_list_element(elem_info, data, j)
append(ctx, elem)
defer pop(ctx)
render_nodes(all_nodes, children, ctx, partials, b, blocks) or_return
render_nodes(
current,
all_nodes,
children,
ctx,
partials,
b,
blocks,
) or_return
}
} else {
append(ctx, val)
defer pop(ctx)
render_nodes(all_nodes, children, ctx, partials, b, blocks) or_return
render_nodes(current, all_nodes, children, ctx, partials, b, blocks) or_return
}
}
i += 1 + node.child_count
case .Inverted:
val := resolve_name(node.key, ctx[:])
if val == nil {
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {
transformed, perr := apply_pipeline(val, node.filters[:])
transformed, perr := apply_pipeline(val, node.filters[:], node.pos)
if perr != nil {
return perr
}
@@ -611,7 +680,7 @@ render_nodes :: proc(
}
if !is_truthy(val) {
children := all_nodes[node.first_child:node.first_child + node.child_count]
render_nodes(all_nodes, children, ctx, partials, b, blocks) or_return
render_nodes(current, all_nodes, children, ctx, partials, b, blocks) or_return
}
i += 1 + node.child_count
@@ -622,7 +691,9 @@ render_nodes :: proc(
name = any_to_string(val)
}
pt, found := partials[name]
if found {
if !found {
warn_missing_partial(current, partials, node, name)
} else {
render_template(pt, ctx, partials, b, nil, node.indent) or_return
}
i += 1
@@ -631,6 +702,7 @@ render_nodes :: proc(
content_nodes: []Node
content_pool: []Node
content_blocks := blocks
render_current := current
found_override := false
if blocks != nil {
@@ -638,6 +710,7 @@ render_nodes :: proc(
content_nodes = o.all_nodes[o.first:o.first + o.count]
content_pool = o.all_nodes
found_override = true
render_current = o.source
}
}
if !found_override {
@@ -649,6 +722,7 @@ render_nodes :: proc(
temp: strings.Builder
strings.builder_init(&temp, context.temp_allocator)
render_nodes(
render_current,
content_pool,
content_nodes,
ctx,
@@ -659,6 +733,7 @@ render_nodes :: proc(
write_indented(b, node.indent, strings.to_string(temp))
} else {
render_nodes(
render_current,
content_pool,
content_nodes,
ctx,
@@ -671,9 +746,12 @@ render_nodes :: proc(
case .Parent:
parent_children := all_nodes[node.first_child:node.first_child + node.child_count]
merged := merge_block_overrides(parent_children, all_nodes, blocks)
merged := merge_block_overrides(parent_children, all_nodes, blocks, current)
pt, found := partials[node.key]
if found {
if !found {
warn_missing_partial(current, partials, node, node.key)
} else {
warn_unmatched_block_overrides(current, pt, parent_children)
render_template(pt, ctx, partials, b, merged, node.indent) or_return
}
i += 1 + node.child_count
@@ -686,6 +764,7 @@ merge_block_overrides :: proc(
children: []Node,
all_nodes: []Node,
existing: map[string]Block_Override,
source: Template,
) -> map[string]Block_Override {
result := make(map[string]Block_Override, context.temp_allocator)
@@ -702,6 +781,7 @@ merge_block_overrides :: proc(
all_nodes = all_nodes,
first = child.first_child,
count = child.child_count,
source = source,
}
}
}
@@ -711,3 +791,115 @@ merge_block_overrides :: proc(
return result
}
// warn_unknown_key checks whether the missing key is a genuine typo (vs. a
// legitimate path through a user-defined map) and, if so, emits a diagnostic
// warning with the closest field-name suggestion via Levenshtein.
warn_unknown_key :: proc(current: Template, ctx: []any, node: Node) {
// `{{.}}` and dot-prefixed names refer to the current context — always valid.
if node.key == "." || (len(node.key) > 0 && node.key[0] == '.') {
return
}
path_ok, missing, available := validate_key_path(ctx, node.key)
if path_ok {
return
}
hint := ""
if len(available) > 0 {
suggestion := suggest_correction(available, missing)
if suggestion != "" {
hint = fmt.tprintf("did you mean '%s'?", suggestion)
}
}
msg := fmt.tprintf("unknown key '%s'", node.key)
path := current.path
if path == "" {
path = "<input>"
}
diag := format_error(path, current.source, node.pos, msg, hint, colorize = should_colorize())
log.warnf("%s", diag)
}
// warn_missing_partial emits a warning when a `{{> name}}` or `{{<name}}` tag
// references a partial that isn't in the partials map.
warn_missing_partial :: proc(
current: Template,
partials: map[string]Template,
node: Node,
name: string,
) {
hint := ""
available := collect_partial_names(partials)
defer delete(available)
suggestion := suggest_correction(available, name)
if suggestion != "" {
hint = fmt.tprintf("did you mean '%s'?", suggestion)
}
msg := fmt.tprintf("partial '%s' not found", name)
path := current.path
if path == "" {
path = "<input>"
}
diag := format_error(path, current.source, node.pos, msg, hint, colorize = should_colorize())
log.warnf("%s", diag)
}
// warn_unmatched_block_overrides checks each `{{$name}}...{{/name}}` block
// defined inside a `{{<parent}}` tag and warns when the name doesn't match
// any block in the parent template.
warn_unmatched_block_overrides :: proc(
current: Template,
parent: Template,
parent_children: []Node,
) {
if len(parent_children) == 0 {
return
}
available := collect_block_names(parent)
defer delete(available)
parent_path := parent.path
if parent_path == "" {
parent_path = "<input>"
}
for child in parent_children {
if child.kind != .Block {
continue
}
matched := false
for name in available {
if name == child.key {
matched = true
break
}
}
if matched {
continue
}
hint := ""
suggestion := suggest_correction(available, child.key)
if suggestion != "" {
hint = fmt.tprintf("did you mean '%s'?", suggestion)
}
msg := fmt.tprintf(
"block override '%s' has no match in parent template '%s'",
child.key,
parent_path,
)
path := current.path
if path == "" {
path = "<input>"
}
diag := format_error(
path,
current.source,
child.pos,
msg,
hint,
colorize = should_colorize(),
)
log.warnf("%s", diag)
}
}
+6 -1
View File
@@ -18,7 +18,12 @@ leak_parse_free_tokens :: proc(t: ^testing.T) {
mem.dynamic_arena_init(&arena)
defer mem.dynamic_arena_destroy(&arena)
tmpl, err := parse("Hello {{name}}!", context.allocator, mem.dynamic_arena_allocator(&arena))
tmpl, err := parse(
"Hello {{name}}!",
"<test>",
context.allocator,
mem.dynamic_arena_allocator(&arena),
)
testing.expect(t, err == nil)
defer delete_template(&tmpl)
}
+33 -17
View File
@@ -27,6 +27,7 @@ Group :: struct {
parse_pipeline :: proc(
content: string,
filters_out: ^[dynamic; MAX_PIPES]Pipe_Filter,
pos: int,
) -> (
key: string,
err: Render_Error,
@@ -46,12 +47,13 @@ parse_pipeline :: proc(
filter_count,
MAX_PIPES,
),
pos = pos,
}
}
key = strings.trim_space(segments[0])
if len(key) == 0 {
return "", Syntax_Error{msg = "pipe expression missing key"}
return "", Syntax_Error{msg = "pipe expression missing key", pos = pos}
}
if filter_count == 0 {
@@ -61,12 +63,12 @@ parse_pipeline :: proc(
for i in 0 ..< filter_count {
seg := strings.trim_space(segments[i + 1])
if len(seg) == 0 {
return "", Syntax_Error{msg = "empty filter"}
return "", Syntax_Error{msg = "empty filter", pos = pos}
}
tokens := strings.fields(seg)
if len(tokens) == 0 {
return "", Syntax_Error{msg = "filter missing op name"}
return "", Syntax_Error{msg = "filter missing op name", pos = pos}
}
arg_count := len(tokens) - 1
@@ -78,6 +80,7 @@ parse_pipeline :: proc(
arg_count,
MAX_PIPE_ARGS,
),
pos = pos,
}
}
@@ -94,10 +97,10 @@ parse_pipeline :: proc(
return key, nil
}
apply_pipeline :: proc(value: any, filters: []Pipe_Filter) -> (any, Render_Error) {
apply_pipeline :: proc(value: any, filters: []Pipe_Filter, pos: int) -> (any, Render_Error) {
current := value
for &filter in filters {
result, err := apply_filter(current, &filter)
result, err := apply_filter(current, &filter, pos)
if err != nil {
return nil, err
}
@@ -106,19 +109,22 @@ apply_pipeline :: proc(value: any, filters: []Pipe_Filter) -> (any, Render_Error
return current, nil
}
apply_filter :: proc(value: any, filter: ^Pipe_Filter) -> (any, Render_Error) {
apply_filter :: proc(value: any, filter: ^Pipe_Filter, pos: int) -> (any, Render_Error) {
switch filter.op {
case "group_by":
return apply_group_by(value, filter.args[:])
return apply_group_by(value, filter.args[:], pos)
case "format":
str, ok := reflect.as_string(value)
if !ok {
return value, Data_Error{msg = "format may only be used on dates"}
return value, Data_Error{msg = "format may only be used on dates", pos = pos}
} else {
return apply_format(str, filter.args[:])
return apply_format(str, filter.args[:], pos)
}
case:
return nil, Data_Error{msg = fmt.tprintf("unknown pipe op '%s'", filter.op)}
return nil, Data_Error{
msg = fmt.tprintf("unknown pipe op '%s'", filter.op),
pos = pos,
}
}
}
@@ -138,9 +144,9 @@ apply_filter :: proc(value: any, filter: ^Pipe_Filter) -> (any, Render_Error) {
// 2023-10-15T13:18:50Z
// 2023-10-15T13:18:50
// 2023-10-15
apply_format :: proc(iso: string, args: []string) -> (result: any, err: Render_Error) {
apply_format :: proc(iso: string, args: []string, pos: int) -> (result: any, err: Render_Error) {
if len(iso) < 10 {
return nil, Data_Error{msg = "format may only be used on dates"}
return nil, Data_Error{msg = "format may only be used on dates", pos = pos}
}
year := iso[:4]
@@ -148,7 +154,7 @@ apply_format :: proc(iso: string, args: []string) -> (result: any, err: Render_E
day_num := (int(iso[8]) - 0x30) * 10 + (int(iso[9]) - 0x30)
if month_num < 1 || month_num > 12 {
return nil, Data_Error{msg = fmt.tprintf("invalid date: \"%s\"", iso)}
return nil, Data_Error{msg = fmt.tprintf("invalid date: \"%s\"", iso), pos = pos}
}
month := fmt.tprintf("%s", time.Month(month_num))[:3]
@@ -156,15 +162,21 @@ apply_format :: proc(iso: string, args: []string) -> (result: any, err: Render_E
}
// Groups preserve first-appearance order from the input list.
apply_group_by :: proc(value: any, args: []string) -> (result: any, err: Render_Error) {
apply_group_by :: proc(value: any, args: []string, pos: int) -> (result: any, err: Render_Error) {
if len(args) != 1 {
return nil, Data_Error{msg = fmt.tprintf("group_by expects 1 argument, got %d", len(args))}
return nil, Data_Error{
msg = fmt.tprintf("group_by expects 1 argument, got %d", len(args)),
pos = pos,
}
}
field := args[0]
elem_info, count, data := list_info(value)
if elem_info == nil {
return nil, Data_Error{msg = "group_by expects a list"}
return nil, Data_Error{
msg = "group_by expects a list",
pos = pos,
}
}
groups := make([dynamic]Group, 0, 8, context.temp_allocator)
@@ -179,11 +191,15 @@ apply_group_by :: proc(value: any, args: []string) -> (result: any, err: Render_
if !found {
return nil, Data_Error {
msg = fmt.tprintf("group_by: element missing field '%s'", field),
pos = pos,
}
}
key_str := any_to_string(key_val)
if len(key_str) == 0 {
return nil, Data_Error{msg = fmt.tprintf("group_by: field '%s' is empty", field)}
return nil, Data_Error{
msg = fmt.tprintf("group_by: field '%s' is empty", field),
pos = pos,
}
}
idx, exists := key_to_idx[key_str]
+13 -12
View File
@@ -216,7 +216,7 @@ test_interp_pipe_basic :: proc(t: ^testing.T) {
data := Scalar_Data {
name = "2026-03-15T08:49:54-04:00",
}
tpl, _ := parse("[{{name | format}}]", context.temp_allocator)
tpl, _ := parse("[{{name | format}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "[15 Mar 2026]")
}
@@ -229,7 +229,7 @@ test_interp_pipe_unescaped :: proc(t: ^testing.T) {
data := Scalar_Data {
name = "2025-12-25T00:00:00Z",
}
tpl, _ := parse("[{{&name | format}}]", context.temp_allocator)
tpl, _ := parse("[{{&name | format}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "[25 Dec 2025]")
}
@@ -242,7 +242,7 @@ test_interp_pipe_dot_current :: proc(t: ^testing.T) {
data := List_Data {
items = {"2026-01-06T00:00:00Z", "2026-06-15T00:00:00Z", "2026-10-15T00:00:00Z"},
}
tpl, _ := parse("{{#items}}[{{. | format}}]{{/items}}", context.temp_allocator)
tpl, _ := parse("{{#items}}[{{. | format}}]{{/items}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "[6 Jan 2026][15 Jun 2026][15 Oct 2026]")
}
@@ -260,7 +260,7 @@ test_format_typical_iso :: proc(t: ^testing.T) {
data := Format_Data {
date = "2026-03-15T08:49:54-04:00",
}
tpl, _ := parse("{{date | format}}", context.temp_allocator)
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "15 Mar 2026")
}
@@ -270,7 +270,7 @@ test_format_short_date_only :: proc(t: ^testing.T) {
data := Format_Data {
date = "2026-06-06",
}
tpl, _ := parse("{{date | format}}", context.temp_allocator)
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "6 Jun 2026")
}
@@ -280,7 +280,7 @@ test_format_empty_input_errors :: proc(t: ^testing.T) {
data := Format_Data {
date = "",
}
tpl, _ := parse("[{{date | format}}]", context.temp_allocator)
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl)
_, err := render(tpl, data, {}, context.temp_allocator)
testing.expect(t, err != nil, "empty date should error")
@@ -291,7 +291,7 @@ test_format_non_date_string_errors :: proc(t: ^testing.T) {
data := Format_Data {
date = "abc",
}
tpl, _ := parse("[{{date | format}}]", context.temp_allocator)
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl)
_, err := render(tpl, data, {}, context.temp_allocator)
testing.expect(t, err != nil, "non-date string should error")
@@ -305,7 +305,7 @@ test_format_non_string_value_errors :: proc(t: ^testing.T) {
data := Int_Data {
count = 42,
}
tpl, _ := parse("{{count | format}}", context.temp_allocator)
tpl, _ := parse("{{count | format}}", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl)
_, err := render(tpl, data, {}, context.temp_allocator)
testing.expect(t, err != nil, "non-string value should error")
@@ -316,7 +316,7 @@ test_format_invalid_month_errors :: proc(t: ^testing.T) {
data := Format_Data {
date = "2023-13-15",
}
tpl, _ := parse("{{date | format}}", context.temp_allocator)
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl)
_, err := render(tpl, data, {}, context.temp_allocator)
testing.expect(t, err != nil, "invalid month should error")
@@ -331,7 +331,8 @@ test_format_inside_section_renders :: proc(t: ^testing.T) {
}
tpl, _ := parse(
"{{#date}}<time datetime=\"{{.}}\">{{. | format}}</time>{{/date}}",
context.temp_allocator,
"<test>",
allocator = context.temp_allocator,
)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "<time datetime=\"2025-12-25T00:00:00Z\">25 Dec 2025</time>")
@@ -342,7 +343,7 @@ test_format_inside_section_skips_when_empty :: proc(t: ^testing.T) {
data := Format_Data {
date = "",
}
tpl, _ := parse("[{{#date}}<time>{{. | format}}</time>{{/date}}]", context.temp_allocator)
tpl, _ := parse("[{{#date}}<time>{{. | format}}</time>{{/date}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, "[]")
}
@@ -362,7 +363,7 @@ test_format_handles_all_iso8601_variants :: proc(t: ^testing.T) {
data := Format_Data {
date = c.input,
}
tpl, _ := parse("{{date | format}}", context.temp_allocator)
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator)
testing.expect_value(t, result, c.expected)
}
+7 -2
View File
@@ -3,6 +3,7 @@ package mustache
import "core:encoding/json"
import "core:fmt"
import "core:log"
import "core:os"
import "core:path/filepath"
import "core:testing"
@@ -54,6 +55,10 @@ run_one_test :: proc(
test: json.Object,
name, template_src, expected: string,
) -> bool {
// Spec tests deliberately exercise missing keys, missing partials, etc.
// Silence the warnings to keep test output readable.
context.logger = log.nil_logger()
partials := make_map(map[string]Template, context.temp_allocator)
if "partials" in test {
@@ -63,7 +68,7 @@ run_one_test :: proc(
psrc, ok := pval.(string)
assert(ok)
pt, perr := parse(psrc, context.temp_allocator)
pt, perr := parse(psrc, "<spec>", context.temp_allocator)
if perr != nil {
testing.expectf(t, false, "[%s] partial '%s' parse error", name, pname)
return false
@@ -72,7 +77,7 @@ run_one_test :: proc(
}
}
tmpl, terr := parse(template_src, context.temp_allocator)
tmpl, terr := parse(template_src, "<spec>", context.temp_allocator)
if terr != nil {
testing.expectf(t, false, "[%s] template parse error", name)
return false
+206
View File
@@ -0,0 +1,206 @@
package mustache
import "base:runtime"
import "core:strings"
// collect_struct_keys enumerates the visible field names of a struct value,
// including fields promoted via `using`-embedded structs.
collect_struct_keys :: proc(val: any, allocator := context.temp_allocator) -> []string {
out: [dynamic]string
collect_struct_keys_into(val, &out, allocator)
return out[:]
}
collect_struct_keys_into :: proc(val: any, out: ^[dynamic]string, allocator := context.allocator) {
v, info := base_value(val)
if info == nil {
return
}
s, ok := info.variant.(runtime.Type_Info_Struct)
if !ok {
return
}
for i in 0 ..< int(s.field_count) {
name := s.names[i]
if len(name) == 0 {
continue
}
if name[0] == '_' {
continue
}
append(out, name)
// Recurse into using-embedded struct fields to surface promoted names.
if s.usings[i] {
field_info := type_info_of(s.types[i].id)
if field_info != nil {
collect_struct_keys_into(any{v.data, s.types[i].id}, out, allocator)
}
}
}
}
// struct_has_field reports whether a struct value has a named field,
// independent of whether that field's value is currently nil. This matters
// for fields like `Maybe(bool)` which can be nil but still exist.
struct_has_field :: proc(val: any, key: string) -> bool {
v, info := base_value(val)
if info == nil {
return false
}
s, ok := info.variant.(runtime.Type_Info_Struct)
if !ok {
return false
}
for i in 0 ..< int(s.field_count) {
if s.names[i] == key {
return true
}
// Recurse into using-embedded fields.
if s.usings[i] {
if struct_has_field(any{v.data, s.types[i].id}, key) {
return true
}
}
}
return false
}
// validate_key_path walks a dotted key path against the context stack and
// reports where (if anywhere) the lookup fails. Returns:
// - ok: true if the entire path resolves, OR if the path crosses a map
// (map keys are user-defined and not validated)
// - missing_segment: the segment that failed (empty when ok)
// - available: keys available at the failing level, for suggestions
validate_key_path :: proc(
ctx: []any,
key: string,
allocator := context.temp_allocator,
) -> (
ok: bool,
missing_segment: string,
available: []string,
) {
parts: [16]string
part_count := 0
start := 0
for i in 0 ..< len(key) {
if key[i] == '.' {
if part_count < len(parts) {
parts[part_count] = key[start:i]
part_count += 1
}
start = i + 1
}
}
if part_count < len(parts) {
parts[part_count] = key[start:]
part_count += 1
}
if part_count == 0 {
return true, "", nil
}
current: any = nil
found := false
for i := len(ctx) - 1; i >= 0; i -= 1 {
current, found = lookup_in(ctx[i], parts[0])
if found {
break
}
}
if !found {
keys: [dynamic]string
for i := len(ctx) - 1; i >= 0; i -= 1 {
collect_struct_keys_into(ctx[i], &keys, allocator)
}
return false, parts[0], keys[:]
}
for i in 1 ..< part_count {
v, info := base_value(current)
if info == nil {
return false, parts[i], nil
}
if _, is_map := info.variant.(runtime.Type_Info_Map); is_map {
return true, "", nil
}
if _, is_struct := info.variant.(runtime.Type_Info_Struct); is_struct {
if !struct_has_field(current, parts[i]) {
return false, parts[i], collect_struct_keys(current, allocator)
}
// Field exists — descend into it. If the value is nil, stop here
// (further segments can't be resolved but the current field is
// legitimately present).
next, found := lookup_in(current, parts[i])
if !found {
return true, "", nil
}
current = next
continue
}
return false, parts[i], nil
}
return true, "", nil
}
// suggest_correction returns the closest match from `available` to `missing`
// using Levenshtein distance, or "" if no good match exists. The threshold
// scales with the length of the missing key.
suggest_correction :: proc(available: []string, missing: string) -> string {
if len(available) == 0 || len(missing) == 0 {
return ""
}
threshold := 2
if len(missing) > 8 {
threshold = len(missing) / 4
}
best: string
best_dist := threshold + 1
for candidate in available {
if abs(len(candidate) - len(missing)) > threshold {
continue
}
d := strings.levenshtein_distance(missing, candidate)
if d <= threshold && d < best_dist {
best = candidate
best_dist = d
}
}
return best
}
// collect_partial_names enumerates the keys of the partials map.
collect_partial_names :: proc(
partials: map[string]Template,
allocator := context.temp_allocator,
) -> []string {
out: [dynamic]string
for name in partials {
append(&out, name)
}
return out[:]
}
// collect_block_names enumerates the unique `{{$name}}` block definitions in
// a template's node array.
collect_block_names :: proc(
tmpl: Template,
allocator := context.temp_allocator,
) -> []string {
out: [dynamic]string
seen := make(map[string]bool, allocator)
defer delete(seen)
for &node in tmpl.nodes {
if node.kind == .Block {
if !seen[node.key] {
seen[node.key] = true
append(&out, node.key)
}
}
}
return out[:]
}
+194
View File
@@ -0,0 +1,194 @@
#+test
#+feature dynamic-literals
package mustache
import "core:fmt"
import "core:testing"
Inner :: struct {
foo: string,
bar: int,
}
Outer :: struct {
title: string,
page_title: string,
inner: Inner,
numbers: [3]int,
}
@(test)
test_validate_simple_found :: proc(t: ^testing.T) {
data := Outer {
title = "hi",
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
ok, missing, _ := validate_key_path(ctx[:], "title")
testing.expect_value(t, ok, true)
testing.expect(t, missing == "", fmt.tprintf("expected empty missing, got %q", missing))
}
@(test)
test_validate_simple_missing :: proc(t: ^testing.T) {
data := Outer {
title = "hi",
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
ok, missing, available := validate_key_path(ctx[:], "page_titel")
testing.expect_value(t, ok, false)
testing.expect_value(t, missing, "page_titel")
testing.expect(t, len(available) > 0, "should have suggestions")
}
@(test)
test_validate_dotted_found :: proc(t: ^testing.T) {
data := Outer {
inner = Inner{foo = "x"},
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
ok, missing, _ := validate_key_path(ctx[:], "inner.foo")
testing.expect_value(t, ok, true)
testing.expect_value(t, missing, "")
}
@(test)
test_validate_dotted_missing :: proc(t: ^testing.T) {
data := Outer {
inner = Inner{foo = "x"},
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
ok, missing, available := validate_key_path(ctx[:], "inner.fooo")
testing.expect_value(t, ok, false)
testing.expect_value(t, missing, "fooo")
testing.expect(t, len(available) > 0, "should have inner field suggestions")
}
Params_Data :: struct {
params: map[string]string,
}
Maybe_Bool_Data :: struct {
flag: Maybe(bool),
name: string,
}
Inner_For_Using :: struct {
flag: Maybe(bool),
label: string,
}
Outer_With_Using :: struct {
using inner: Inner_For_Using,
other: int,
}
@(test)
test_validate_path_through_using_to_maybe_bool :: proc(t: ^testing.T) {
data := Outer_With_Using {
inner = Inner_For_Using{label = "hi"},
other = 42,
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
// `flag` is promoted via using; field exists even when Maybe is nil.
ok, missing, _ := validate_key_path(ctx[:], "flag")
testing.expect_value(t, ok, true)
testing.expect_value(t, missing, "")
// `label` is also promoted via using.
ok2, missing2, _ := validate_key_path(ctx[:], "label")
testing.expect_value(t, ok2, true)
testing.expect_value(t, missing2, "")
}
@(test)
test_struct_has_field_with_maybe_bool :: proc(t: ^testing.T) {
data := Maybe_Bool_Data {
name = "hi",
} // flag is nil Maybe
testing.expect_value(t, struct_has_field(data, "flag"), true)
testing.expect_value(t, struct_has_field(data, "name"), true)
testing.expect_value(t, struct_has_field(data, "missing"), false)
}
@(test)
test_validate_path_through_maybe_bool :: proc(t: ^testing.T) {
data := Maybe_Bool_Data {
name = "hi",
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
// `flag` exists as a field even when its Maybe value is nil — should NOT warn.
ok, missing, _ := validate_key_path(ctx[:], "flag")
testing.expect_value(t, ok, true)
testing.expect_value(t, missing, "")
}
@(test)
test_validate_map_path_silent :: proc(t: ^testing.T) {
data := Params_Data {
params = {"social" = "x"},
}
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, data)
// `params` exists and is a map — subsequent segments are user-defined.
ok, _, _ := validate_key_path(ctx[:], "params.anything_here")
testing.expect_value(t, ok, true)
}
@(test)
test_suggest_correction_exact :: proc(t: ^testing.T) {
available := []string{"title", "page_title", "body"}
testing.expect_value(t, suggest_correction(available, "page_titel"), "page_title")
}
@(test)
test_suggest_correction_close :: proc(t: ^testing.T) {
available := []string{"title", "body", "now"}
testing.expect_value(t, suggest_correction(available, "titel"), "title")
}
@(test)
test_suggest_correction_no_match :: proc(t: ^testing.T) {
available := []string{"completely_different", "unrelated"}
testing.expect_value(t, suggest_correction(available, "page_titel"), "")
}
@(test)
test_suggest_correction_empty :: proc(t: ^testing.T) {
testing.expect_value(t, suggest_correction([]string{}, "anything"), "")
testing.expect_value(t, suggest_correction([]string{"a"}, ""), "")
}
@(test)
test_warn_no_false_positive_for_valid_keys :: proc(t: ^testing.T) {
Data :: struct {
name: string,
}
src := "Hello {{name}}"
tmpl, err := parse(src, "<test>", context.temp_allocator)
testing.expect(t, err == nil, "should parse")
if err != nil {
return
}
// We can't easily capture log output in tests, but we can verify the
// validation procs agree the key exists.
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
append(&ctx, Data{name = "World"})
ok, missing, _ := validate_key_path(ctx[:], "name")
testing.expect_value(t, ok, true)
}