diff --git a/AGENTS.md b/AGENTS.md
index 8b245c7..6e17f71 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -5,7 +5,7 @@ Thor is a static site generator written in [Odin](https://odin-lang.org), replac
## Architecture
```
-thor.json ← site config (title, base_url, params, modules)
+thor.json ← site config (title, base_url, params, modules, og)
content/ ← markdown and HTML content files
layouts/ ← Mustache templates + partials (user overrides)
assets/ ← CSS (Tufte-based), JS, fonts, images
@@ -19,7 +19,7 @@ public/ ← build output (generated)
thor/
├── treesitter/ # FFI types + grammar management (standalone package)
├── markdown/ # Content transformation pipeline (imports ../treesitter)
-├── mustache/ # Template engine with lambdas + pipe filters
+├── mustache/ # Template engine with lambdas + pipe filters + diagnostics
├── content.odin # Page struct, scan_content, load_page
├── render.odin # Template rendering, data structs, RSS, sitemap
├── site.odin # Config (Flags, Config_File, Site), init_site
@@ -27,8 +27,9 @@ thor/
├── feed.odin # RSS + sitemap generation
├── vfs.odin # Union file system (defaults → modules → site)
├── assets.odin # VFS-based asset copying
-├── opengraph.odin # Open_Graph struct + og_init/og_for_page
-├── frontmatter.odin # JSON frontmatter parser
+├── html.odin # HTML helpers: strip_html_tags, unescape_html, generate_summary
+├── opengraph.odin # Open_Graph struct + og_for_site/og_for_page
+├── frontmatter.odin # JSON frontmatter parser (supports nested og + lastmod)
├── defaults.odin # DEFAULTS_PATH constant (#directory)
├── main.odin # Entry point
└── defaults/layouts/ # Bundled default templates
@@ -39,15 +40,16 @@ thor/
| File | Responsibility |
|---|---|
| `main.odin` | Entry point. Sets `context.logger`, calls `init_site`, `build_vfs`, `site_load_content`, `render_site`. Optional Spall profiling via `SPALL` config flag. |
-| `site.odin` | `Flags` (CLI), `Config_File` (thor.json), `Site` (runtime state + arena + VFS + pages + modules). `Feature` enum. 5-step `init_site`. Imports `md "markdown"` for `Extension` enum. |
-| `content.odin` | `Page` struct, `scan_content` (section-aware walk that handles leaf bundles), `load_page`, `infer_layout`. Calls `md.process()` for the markdown pipeline. |
+| `site.odin` | `Flags` (CLI), `Config_File` (thor.json, includes `og: Open_Graph`), `Site` (runtime state + arena + VFS + pages + modules + `og`). `Feature` enum. 5-step `init_site`. Imports `md "markdown"` for `Extension` enum. |
+| `content.odin` | `Page` struct (includes `lastmod`, `og`), `scan_content` (section-aware walk that handles leaf bundles), `load_page`, `infer_layout`. Calls `md.process()` for the markdown pipeline. |
| `render.odin` | Template rendering: `render_site`, `render_page_html`, `render_home_html`, `render_section`. Data structs (`Base_Data`, `Page_Data`, `Home_Data`, `Section_Data`). VFS-based template loading with fallback chain (`get_template`). |
| `minify.odin` | HTML/CSS minification via tree-sitter. Imports `ts "treesitter"`. |
| `feed.odin` | RSS feed + sitemap XML. Uses `page.url` for canonical URLs. |
-| `vfs.odin` | Union file system: `VFS`, `build_vfs`, `mount_dir`, `mount_subdir`, `mount_recursive`, `vfs_get`. Layers defaults → modules → site. |
+| `vfs.odin` | Union file system: `VFS`, `build_vfs`, `mount_dir`, `mount_subdir`, `mount_recursive`, `vfs_get`, `vfs_get_entry`, `vfs_entry_data`. Layers defaults → modules → site. |
| `assets.odin` | `copy_assets_dir` — iterates VFS entries with `assets/` prefix, minifies CSS, copies verbatim or via `os.copy_file`. |
-| `opengraph.odin` | `Open_Graph` struct (fields ordered per OGP spec). `og_init(site)` for site defaults, `og_for_page(site, page, base)` for page-specific OG data. |
-| `frontmatter.odin` | JSON frontmatter parser (`{ }` delimited). Supports `layout` field for template override. |
+| `html.odin` | `strip_html_tags` (moved from render.odin), `unescape_html`, `generate_summary` (Hugo-style body summary for OG descriptions). |
+| `opengraph.odin` | `Open_Graph` struct (fields ordered per OGP spec, `is_article: Maybe(bool)`). `og_for_site(site)` for site defaults (from config + derived), `og_for_page(site_og, page)` for page-specific (overlay page.og + derive from page data). |
+| `frontmatter.odin` | JSON frontmatter parser (`{ }` delimited). Supports `layout`, `lastmod`, and nested `og` object (via `json_get_open_graph`). |
| `defaults.odin` | `DEFAULTS_PATH` constant, resolved at compile time via `#directory` so bundled templates ship in the binary. |
### Subpackages
@@ -90,10 +92,12 @@ Page :: struct {
title: string,
description: string,
date: string,
+ lastmod: string,
menu: string,
body_html: string,
draft: bool,
is_starred: bool,
+ og: Open_Graph, // per-page OG overrides from frontmatter
_is_index: bool `private`,
}
```
@@ -112,8 +116,8 @@ No `Page_Type` enum — page type is inferred from section + `_is_index`. Layout
Config is split into three structs with a clear 5-step initialization flow:
- **`Flags`** — CLI args only. Parsed by `core:flags`. Includes path overrides (`--content`, `--assets`, `--output`, `--layouts`), build-mode toggles (`-drafts`, `-watch`, `-minify`), and `-ext`/`-no-ext` for markdown extension overrides.
-- **`Config_File`** — parsed from `thor.json` via `json.unmarshal_string`. Holds title, paths, `markdown_extensions` (JSON), `params` (JSON), `modules` (JSON array of relative paths).
-- **`Site`** — runtime state: arena, pages, modules, VFS, `features: bit_set[Feature]`, `markdown_extensions: bit_set[md.Extension]`.
+- **`Config_File`** — parsed from `thor.json` via `json.unmarshal_string`. Holds title, paths, `markdown_extensions` (JSON), `params` (JSON), `modules` (JSON array of relative paths), `og` (`Open_Graph` struct for site-level OG defaults).
+- **`Site`** — runtime state: arena, pages, modules, VFS, `features: bit_set[Feature]`, `markdown_extensions: bit_set[md.Extension]`, `og: Open_Graph` (resolved site-level OG).
**`Feature` enum** — `Drafts`, `Minify`, `Watch`. Checked with `.Minify in site.features`.
@@ -130,15 +134,11 @@ Config precedence: `CLI flags > thor.json values > hardcoded defaults`.
"title": "...",
"base_url": "...",
"modules": ["../path/to/module"],
- "markdown_extensions": {
- "emoji": true,
- "sidenotes": true,
- "alerts": true,
- "highlight": true,
- "sections": true
+ "og": {
+ "image": "https://example.com/og.png"
},
+ "markdown_extensions": { "emoji": true, "highlight": false },
"params": {
- "author": "...",
"social": [
{ "name": "github", "url": "...", "icon": "icons/github" }
]
@@ -155,13 +155,33 @@ VFS :: struct { files: map[string]VFS_Entry }
VFS_Entry :: struct { fs_path: string, data: []byte }
```
-`build_vfs` mounts in reverse precedence (defaults first, site last overwrites). `DEFAULTS_PATH` resolved at compile time via `#directory`, so bundled templates ship inside the binary. Modules configured via `"modules": ["../path"]` in `thor.json` — each module contributes `layouts/` and `assets/` subdirectories. `vfs_get` lazily reads file contents on first access.
+`build_vfs` mounts in reverse precedence (defaults first, site last overwrites). `DEFAULTS_PATH` resolved at compile time via `#directory`, so bundled templates ship inside the binary. Modules configured via `"modules": ["../path"]` in `thor.json` — each module contributes `layouts/` and `assets/` subdirectories.
+
+Three access patterns:
+- `vfs_get(vfs, path) -> ([]byte, bool)` — data only (lazy-loaded from disk)
+- `vfs_get_entry(vfs, path) -> (VFS_Entry, []byte, bool)` — entry + data (for callers that need `fs_path` for diagnostics)
+- `vfs_entry_data(entry) -> ([]byte, bool)` — data from an entry already in hand (avoids redundant map lookup when iterating `vfs.files`)
Content is **not yet in the VFS** — `scan_content` still uses direct filesystem reads. (See `TODOS.md`.)
## Open Graph
-`Open_Graph` struct in `opengraph.odin` with fields ordered per [ogp.me](https://ogp.me/) spec. Site defaults set via `og_init(site)` (site_name, description, default image, locale). Page-specific fields via `og_for_page(site, page, base)` (copies base, overrides url/title/type/is_article/section/published_time). Templates access via `{{og.url}}`, `{{og.title}}`, `{{#og.is_article}}`, etc.
+`Open_Graph` struct in `opengraph.odin` with fields ordered per [ogp.me](https://ogp.me/) spec. `is_article` is `Maybe(bool)` — nil means "unset" (distinguished from explicitly `false`).
+
+**Site-level** (`og_for_site`): starts from `Config_File.og` (user-supplied defaults from `thor.json`), then fills empty fields derivable from `Site`:
+- `site_name ← site.title`
+- `locale ← "en_US"` (default if unset)
+
+**Page-level** (`og_for_page`): copies site OG, derives page-specific fields, then overlays `Page.og` (from frontmatter):
+- `url ← page.url`
+- `title ← page.title` (falls back to `site_name` if empty)
+- `type ← "article" if !page._is_index else "website"`
+- `is_article ← !page._is_index`
+- `section ← page.section`
+- `published_time / modified_time ← page.date / page.lastmod`
+- `description ← page.description`, else body summary (via `generate_summary`)
+
+Paths through maps (e.g. `params.*`) are silently allowed — not validated. Templates access via `{{og.url}}`, `{{og.title}}`, `{{#og.is_article}}`, etc.
## Markdown pipeline
@@ -237,7 +257,7 @@ Section tags and interpolation tags may transform the resolved value before rend
```
-Currently implemented: `group_by ` (list → list-of-groups) and `format` (ISO date string → display string). Filter results live in `context.temp_allocator` (render-scoped). See `mustache/EXTENSIONS.md` for syntax details, caps (`MAX_PIPES`, `MAX_PIPE_ARGS`), and the `Group` struct shape.
+Currently implemented: `group_by ` (list → list-of-groups) and `format` (ISO date string → display string like "15 Mar 2026"). Filter results live in `context.temp_allocator` (render-scoped). See `mustache/EXTENSIONS.md` for syntax details, caps (`MAX_PIPES`, `MAX_PIPE_ARGS`), and the `Group` struct shape.
### Comments
@@ -310,40 +330,66 @@ nix build # runs thor, outputs to ./result/
```bash
cd thor
odin test . # main package tests (site, frontmatter)
-odin test . -all-packages # includes mustache specs, lambdas, pipes, markdown tests
+odin test . -all-packages # includes mustache specs, lambdas, pipes, diagnostics, markdown tests
```
## Mustache engine
-Spec-compliant implementation at `mustache/`. See `mustache/SPEC.md` for the implementation specification and `mustache/EXTENSIONS.md` for non-standard extensions (pipes).
+Spec-compliant implementation at `mustache/`. See `mustache/SPEC.md` for the implementation specification, `mustache/EXTENSIONS.md` for non-standard extensions (pipes), and `mustache/diagnostic.odin` for the rust-style error formatter.
### Files
| File | Responsibility |
|---|---|
-| `mustache.odin` | Public API (`parse`, `render`, `Template`), parser (`parse_section` with allocator threading), renderer (`render_nodes`), template inheritance (`merge_block_overrides`), `delete_template`/`delete_partials` |
+| `mustache.odin` | Public API (`parse`, `render`, `Template`), parser (`parse_section`), renderer (`render_nodes`, takes `Template` by value), template inheritance (`merge_block_overrides`), `delete_template`/`delete_partials`. Pipe support in Variable/Unescaped/Section/Inverted tags. |
| `tokenizer.odin` | Tokenizer (template string → `[]Token`), standalone whitespace detection |
-| `data.odin` | Reflection-based data model: `base_value` (peels union/any/nested-any layers), `lookup_in` (structs + maps, handles `Type_Info_Any` value kind in maps), `resolve_name`, `is_truthy`, `any_to_string`, `list_info`, `extract_list_element` (unwraps `[dynamic]any` element types so downstream lookups see the real value), `call_interp_lambda`/`call_section_lambda` |
-| `pipes.odin` | Pipes extension: `Pipe_Filter` AST, `parse_pipeline`, `apply_pipeline`, `apply_filter` (switch dispatch), `apply_group_by`. Stored on `Node.filters`; render-scoped results in temp allocator. |
-| `spec_test.odin` | JSON spec test runner — loads `spec/specs/*.json`, runs each test case |
+| `data.odin` | Reflection-based data model: `base_value` (peels union/any/nested-any layers), `lookup_in` (structs + maps, handles `Type_Info_Any` value kind in maps), `resolve_name`, `is_truthy`, `any_to_string`, `list_info`, `extract_list_element`, `call_interp_lambda`/`call_section_lambda` |
+| `pipes.odin` | Pipes extension: `Pipe_Filter` AST, `parse_pipeline` (takes `pos`), `apply_pipeline`, `apply_filter` (switch dispatch: `group_by` + `format`), `apply_group_by`, `apply_format`. Stored on `Node.filters`; render-scoped results in temp allocator. |
+| `diagnostic.odin` | Rust-style error formatter: `format_error` (multi-line context, ANSI colors via `core:terminal/ansi`, `colorize` param), `format_render_error` (dispatch on `Render_Error`), `line_col`, `line_text`, `context_extent`, `count_lines`, `digit_count`, `should_colorize`. |
+| `suggest.odin` | Strict-warning helpers: `validate_key_path` (walks dotted path, crosses maps silently), `suggest_correction` (Levenshtein via `core:strings/levenshtein_distance`), `collect_struct_keys` (via reflection, recurses into `using`), `struct_has_field` (distinguishes missing field from nil value — needed for `Maybe(bool)`), `collect_partial_names`, `collect_block_names`. |
+| `spec_test.odin` | JSON spec test runner — loads `spec/specs/*.json`, runs each test case. Uses `log.nil_logger()` to suppress expected warnings. |
| `lambda_test.odin` | Spec lambda tests |
-| `pipes_test.odin` | Pipe filter tests |
+| `pipes_test.odin` | Pipe filter tests (`group_by` + `format`) |
+| `diagnostic_test.odin` | Golden-output tests for `format_error` (multi-line context, edge cases, alignment, caret position, hint) + parser error message brace-escaping |
+| `suggest_test.odin` | Tests for `validate_key_path`, `suggest_correction`, `struct_has_field` with `Maybe(bool)` and `using`-promoted fields |
### Architecture
```
-parse(source) → tokenize → trim_standalone_whitespace → parse_section → Template
+parse(source, path) → tokenize → trim_standalone_whitespace → parse_section → Template
render(tmpl, data, partials) → render_nodes (walks flat node array against context stack) → string
```
- **Two-phase API**: `parse()` produces a reusable `Template`, `render()` walks it against data. Templates parsed once, rendered many times.
-- **Flat `[dynamic]Node` array** with `first_child`/`child_count` indices — pre-order layout.
+- **Flat `[dynamic]Node` array** with `first_child`/`child_count` indices — pre-order layout. Each `Node` carries `pos: int` (byte offset into source) for diagnostics.
+- **`Template`** carries `source` and `path` — used by diagnostics to show file location and source context.
- **Context stack**: `^[dynamic]any` with `append`/`pop` for section push/pop.
+- **`render_nodes` takes `Template` by value** (not `^Template`) — Odin's calling convention promotes to pointer when efficient. Eliminates "local copy" patterns at call sites.
+- **`Block_Override.source: Template`** — carries the template that defined the override, so warnings inside block overrides point at the correct file.
- **`base_value`** peels Named/Distinct/Union layers (including `json.Value`). Also unwraps nested `any`-of-`any` (which occurs when `map[string]any` values are read via runtime map internals).
- **`lookup_in`** resolves keys on structs (via `reflect.struct_field_value_by_name` with `allow_using = true`) and maps. Detects `Type_Info_Any` value kind in maps and reads the inner any directly to avoid double-wrap.
- **Template inheritance**: `{{*key}}` resolves partial name from data context at render time.
+### Diagnostics
+
+Rust-style error messages with multi-line source context, caret underlines, and Levenshtein suggestions. ANSI colors via `core:terminal/ansi`, gated on `should_colorize()` (TTY detection on stderr).
+
+**Error types**: `Syntax_Error{msg, pos}` and `Data_Error{msg, pos}` — both carry byte offset into template source. (`Partial_Error` was removed — dead code.)
+
+**Strict-by-default warnings** — `render_nodes` emits `log.warnf` diagnostics for:
+- Unknown keys in `{{k}}`, `{{{k}}}`, `{{#k}}`, `{{^k}}` (via `validate_key_path` + `suggest_correction`)
+- Missing partials (`{{> name}}` not in partials map)
+- Missing parent templates (`{{` is implemented (returns `[dynamic]Group` where `Group{key, items}`). See `mustache/EXTENSIONS.md`.
+`{{key | op args…}}` for interpolation, `{{#key | op args…}}…{{/key}}` for sections. Stored as `[dynamic; MAX_PIPES]Pipe_Filter` on each `Node`. Applied in the renderer via `apply_pipeline` before truthiness/interpolation. Implemented filters:
+
+- `group_by ` — list → `[dynamic]Group` where `Group{key, items}`
+- `format` — ISO 8601 date string → display string (e.g., "15 Mar 2026")
+
+See `mustache/EXTENSIONS.md`.
### Not implemented
- Set delimiters (`{{= =}}`, `delimiters.json`)
+- Partial invocation stack in diagnostics (warnings inside partials point at the partial file but don't show the `{{> name}}` invocation site — see TODOS.md)
## Known limitations
diff --git a/mustache/diagnostic.odin b/mustache/diagnostic.odin
new file mode 100644
index 0000000..fba81d0
--- /dev/null
+++ b/mustache/diagnostic.odin
@@ -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.
+//
+//
+// --> ::
+// |
+// N |
+// N |
+// N |
+// | ^^^^^^^^^^^
+// N |
+// N |
+// |
+//
+// `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: ` |`.
+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 = ""
+ }
+ return format_error(path, tmpl.source, e.pos, e.msg, colorize = colorize)
+ case Data_Error:
+ path := tmpl.path
+ if path == "" {
+ path = ""
+ }
+ return format_error(path, tmpl.source, e.pos, e.msg, colorize = colorize)
+ }
+ return ""
+}
+
diff --git a/mustache/diagnostic_test.odin b/mustache/diagnostic_test.odin
new file mode 100644
index 0000000..7d9ae39
--- /dev/null
+++ b/mustache/diagnostic_test.odin
@@ -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),
+ )
+ }
+}
+
diff --git a/mustache/mustache.odin b/mustache/mustache.odin
index 17746bf..b1963aa 100644
--- a/mustache/mustache.odin
+++ b/mustache/mustache.odin
@@ -13,6 +13,7 @@ Syntax_Error :: struct {
msg: string,
pos: int,
}
+
Data_Error :: struct {
msg: string,
pos: int,
@@ -535,7 +536,7 @@ render_nodes :: proc(
case .Variable:
val := resolve_name(node.key, ctx[:])
- if val == nil {
+ if val == nil {
warn_unknown_key(current, ctx[:], node)
}
if len(node.filters) > 0 {