mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
perf(mustache): Improved performance.
This commit is contained in:
@@ -32,6 +32,7 @@ thor/
|
|||||||
├── frontmatter.odin # JSON frontmatter parser (supports nested og + lastmod)
|
├── frontmatter.odin # JSON frontmatter parser (supports nested og + lastmod)
|
||||||
├── defaults.odin # DEFAULTS_PATH constant (#directory)
|
├── defaults.odin # DEFAULTS_PATH constant (#directory)
|
||||||
├── main.odin # Entry point
|
├── main.odin # Entry point
|
||||||
|
├── bench/ # Template rendering benchmark
|
||||||
└── defaults/layouts/ # Bundled default templates
|
└── defaults/layouts/ # Bundled default templates
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -64,6 +65,7 @@ thor/
|
|||||||
| | `sectionate.odin` | `wrap_sections` — splits HTML at `<h2>` into `<section>` wrappers |
|
| | `sectionate.odin` | `wrap_sections` — splits HTML at `<h2>` into `<section>` wrappers |
|
||||||
| | `highlight.odin` | Syntax highlighting via tree-sitter. Imports `../treesitter`. |
|
| | `highlight.odin` | Syntax highlighting via tree-sitter. Imports `../treesitter`. |
|
||||||
| `mustache/` | See [Mustache engine](#mustache-engine) below | Template engine |
|
| `mustache/` | See [Mustache engine](#mustache-engine) below | Template engine |
|
||||||
|
| `bench/` | `bench.odin` + `templates/` | Standalone template rendering benchmark. Generates 500 posts + 100 comments, renders with indented partials + inheritance + pipes. `--dump <path>` for output validation, positional arg for iteration count (default 250). |
|
||||||
|
|
||||||
Icon SVGs live as HTML partials in `layouts/partials/icons/` (home, github, rss, chevron_up, star).
|
Icon SVGs live as HTML partials in `layouts/partials/icons/` (home, github, rss, chevron_up, star).
|
||||||
|
|
||||||
@@ -333,6 +335,16 @@ odin test . # main package tests (site, frontmatter)
|
|||||||
odin test . -all-packages # includes mustache specs, lambdas, pipes, diagnostics, markdown tests
|
odin test . -all-packages # includes mustache specs, lambdas, pipes, diagnostics, markdown tests
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Benchmark
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd thor
|
||||||
|
odin build bench -o:speed
|
||||||
|
./bench.bin # 250 iterations, prints timing
|
||||||
|
./bench.bin --dump output.html # render once, write to file for diff validation
|
||||||
|
./bench.bin 1000 # custom iteration count
|
||||||
|
```
|
||||||
|
|
||||||
## Mustache engine
|
## Mustache engine
|
||||||
|
|
||||||
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.
|
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.
|
||||||
@@ -341,11 +353,11 @@ Spec-compliant implementation at `mustache/`. See `mustache/SPEC.md` for the imp
|
|||||||
|
|
||||||
| File | Responsibility |
|
| File | Responsibility |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `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. |
|
| `mustache.odin` | Public API (`parse`, `render`, `Template`), parser (`parse_section`), renderer (`render_nodes` with `Indent_State` for partial indentation), 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 |
|
| `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`, `call_interp_lambda`/`call_section_lambda` |
|
| `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. |
|
| `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`. |
|
| `diagnostic.odin` | Rust-style error formatter: `format_error` (multi-line context, ANSI colors via `core:terminal/ansi`, `colorize` param), `format_render_error` (formats `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`. |
|
| `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. |
|
| `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 |
|
| `lambda_test.odin` | Spec lambda tests |
|
||||||
@@ -361,7 +373,7 @@ render(tmpl, data, partials) → render_nodes (walks flat node array against con
|
|||||||
```
|
```
|
||||||
|
|
||||||
- **Two-phase API**: `parse()` produces a reusable `Template`, `render()` walks it against data. Templates parsed once, rendered many times.
|
- **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. Each `Node` carries `pos: int` (byte offset into source) for diagnostics.
|
- **Flat `[dynamic]Node` array** with `children: []Node` slices (pre-order layout; slices point into the backing array). 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.
|
- **`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.
|
- **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.
|
- **`render_nodes` takes `Template` by value** (not `^Template`) — Odin's calling convention promotes to pointer when efficient. Eliminates "local copy" patterns at call sites.
|
||||||
@@ -370,12 +382,13 @@ render(tmpl, data, partials) → render_nodes (walks flat node array against con
|
|||||||
- **`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.
|
- **`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**: `{{<parent}}` loads parent from partials, `{{$block}}` defines overridable sections. `merge_block_overrides` propagates overrides through multi-level chains.
|
- **Template inheritance**: `{{<parent}}` loads parent from partials, `{{$block}}` defines overridable sections. `merge_block_overrides` propagates overrides through multi-level chains.
|
||||||
- **Dynamic partial names**: `{{>*key}}` resolves partial name from data context at render time.
|
- **Dynamic partial names**: `{{>*key}}` resolves partial name from data context at render time.
|
||||||
|
- **Render-time partial indentation**: `Indent_State` threads `at_line_start` through `render_nodes` so partial indent is applied at render time (via `write_indented` on Text nodes) instead of reparsing the partial's source. `render_template` writes initial indent, creates state, calls `render_nodes`. Data-injected newlines don't pick up indent (Variables don't update `at_line_start`).
|
||||||
|
|
||||||
### Diagnostics
|
### 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).
|
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.)
|
**Error types**: `Error_Body{msg, pos, kind}` where `kind` is `Error_Kind.Syntax` (parse-time) or `Error_Kind.Data` (render-time). `Error` is a single-variant union wrapping `Error_Body` (nilable for `!= nil` / `or_return`).
|
||||||
|
|
||||||
**Strict-by-default warnings** — `render_nodes` emits `log.warnf` diagnostics for:
|
**Strict-by-default warnings** — `render_nodes` emits `log.warnf` diagnostics for:
|
||||||
- Unknown keys in `{{k}}`, `{{{k}}}`, `{{#k}}`, `{{^k}}` (via `validate_key_path` + `suggest_correction`)
|
- Unknown keys in `{{k}}`, `{{{k}}}`, `{{#k}}`, `{{^k}}` (via `validate_key_path` + `suggest_correction`)
|
||||||
@@ -424,7 +437,6 @@ See `mustache/EXTENSIONS.md`.
|
|||||||
|
|
||||||
You may never, *ever* remove `TODO:` or `FIXME:` comments. Those are for humans, not machines.
|
You may never, *ever* remove `TODO:` or `FIXME:` comments. Those are for humans, not machines.
|
||||||
See `HUGO.md` for analysis of why thor doesn't need Hugo's shortcode context isolation.
|
See `HUGO.md` for analysis of why thor doesn't need Hugo's shortcode context isolation.
|
||||||
See `mustache/PARTIAL_INDENT.md` for whitespace handling analysis.
|
|
||||||
See `mustache/SPEC.md` for the original implementation specification.
|
See `mustache/SPEC.md` for the original implementation specification.
|
||||||
See `mustache/EXTENSIONS.md` for non-standard extensions (pipes).
|
See `mustache/EXTENSIONS.md` for non-standard extensions (pipes).
|
||||||
|
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
+98
-65
@@ -81,6 +81,14 @@ Block_Override :: struct {
|
|||||||
source: Template,
|
source: Template,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indent_State threads partial-indent tracking through render_nodes so
|
||||||
|
// the renderer can apply indentation at render time instead of reparsing
|
||||||
|
// the partial's source with indentation baked in.
|
||||||
|
Indent_State :: struct {
|
||||||
|
indent: string,
|
||||||
|
at_line_start: bool,
|
||||||
|
}
|
||||||
|
|
||||||
delete_template :: proc(tmpl: ^Template) {
|
delete_template :: proc(tmpl: ^Template) {
|
||||||
if tmpl != nil && len(tmpl.nodes) > 0 {
|
if tmpl != nil && len(tmpl.nodes) > 0 {
|
||||||
delete(tmpl.nodes)
|
delete(tmpl.nodes)
|
||||||
@@ -393,20 +401,29 @@ find_common_indent :: proc(children: []Node) -> string {
|
|||||||
text := children[i].text
|
text := children[i].text
|
||||||
if len(text) > 0 {
|
if len(text) > 0 {
|
||||||
line_start := 0
|
line_start := 0
|
||||||
for j in 0 ..= len(text) {
|
for {
|
||||||
if j == len(text) || text[j] == '\n' {
|
// Bulk-scan to next newline (AVX2-backed) instead of byte-by-byte.
|
||||||
line := text[line_start:j]
|
rel := strings.index_byte(text[line_start:], '\n')
|
||||||
if len(strings.trim_space(line)) > 0 {
|
line_end: int
|
||||||
ws := leading_whitespace(line)
|
if rel < 0 {
|
||||||
if !found {
|
line_end = len(text)
|
||||||
common = ws
|
} else {
|
||||||
found = true
|
line_end = line_start + rel
|
||||||
} else if len(ws) < len(common) {
|
|
||||||
common = ws
|
|
||||||
}
|
|
||||||
}
|
|
||||||
line_start = j + 1
|
|
||||||
}
|
}
|
||||||
|
line := text[line_start:line_end]
|
||||||
|
if len(strings.trim_space(line)) > 0 {
|
||||||
|
ws := leading_whitespace(line)
|
||||||
|
if !found {
|
||||||
|
common = ws
|
||||||
|
found = true
|
||||||
|
} else if len(ws) < len(common) {
|
||||||
|
common = ws
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rel < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line_start = line_end + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -442,16 +459,19 @@ remove_line_indent :: proc(s: string, indent: string, allocator := context.alloc
|
|||||||
if at_line_start {
|
if at_line_start {
|
||||||
if i + len(indent) <= len(s) && s[i:i + len(indent)] == indent {
|
if i + len(indent) <= len(s) && s[i:i + len(indent)] == indent {
|
||||||
i += len(indent)
|
i += len(indent)
|
||||||
at_line_start = false
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
at_line_start = false
|
at_line_start = false
|
||||||
}
|
}
|
||||||
append(&buf, s[i])
|
// Bulk-append up to and including the next newline (AVX2-backed).
|
||||||
if s[i] == '\n' {
|
rel := strings.index_byte(s[i:], '\n')
|
||||||
at_line_start = true
|
if rel < 0 {
|
||||||
|
append(&buf, s[i:])
|
||||||
|
break
|
||||||
}
|
}
|
||||||
i += 1
|
next := i + rel
|
||||||
|
append(&buf, s[i:next + 1])
|
||||||
|
i = next + 1
|
||||||
|
at_line_start = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(buf[:])
|
return string(buf[:])
|
||||||
@@ -461,34 +481,6 @@ remove_line_indent :: proc(s: string, indent: string, allocator := context.alloc
|
|||||||
// Indentation helpers
|
// Indentation helpers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
indent_lines :: proc(source: string, indent: string) -> string {
|
|
||||||
if len(indent) == 0 || len(source) == 0 {
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
b: strings.Builder
|
|
||||||
strings.builder_init(&b, context.temp_allocator)
|
|
||||||
write_indented(&b, indent, source)
|
|
||||||
return strings.to_string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
write_indented :: proc(b: ^strings.Builder, indent: string, content: string) {
|
|
||||||
if len(indent) == 0 || len(content) == 0 {
|
|
||||||
strings.write_string(b, content)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
at_line_start := true
|
|
||||||
for i in 0 ..< len(content) {
|
|
||||||
if at_line_start {
|
|
||||||
strings.write_string(b, indent)
|
|
||||||
at_line_start = false
|
|
||||||
}
|
|
||||||
strings.write_byte(b, content[i])
|
|
||||||
if content[i] == '\n' {
|
|
||||||
at_line_start = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
render_template :: proc(
|
render_template :: proc(
|
||||||
pt: Template,
|
pt: Template,
|
||||||
ctx: ^[dynamic]any,
|
ctx: ^[dynamic]any,
|
||||||
@@ -497,21 +489,36 @@ render_template :: proc(
|
|||||||
blocks: map[string]Block_Override,
|
blocks: map[string]Block_Override,
|
||||||
indent: string,
|
indent: string,
|
||||||
) -> Error {
|
) -> Error {
|
||||||
if len(indent) > 0 && len(pt.source) > 0 {
|
if len(indent) > 0 {
|
||||||
// Per Mustache spec: the partial's source is indented before rendering,
|
state := Indent_State{indent = indent, at_line_start = false}
|
||||||
// not its output. This is necessary so that data-injected newlines
|
strings.write_string(b, indent) // first line always gets indent
|
||||||
// (e.g. from `{{{content}}}` where content contains `\n`) do NOT pick
|
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, &state)
|
||||||
// up the indent — only source-level line breaks do.
|
}
|
||||||
indented := indent_lines(pt.source, indent)
|
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, nil)
|
||||||
reparse := parse(
|
}
|
||||||
indented,
|
|
||||||
pt.path,
|
write_indented :: proc(b: ^strings.Builder, indent: string, content: string, at_line_start: ^bool) {
|
||||||
context.temp_allocator,
|
if len(indent) == 0 || len(content) == 0 {
|
||||||
context.temp_allocator,
|
strings.write_string(b, content)
|
||||||
) or_return
|
return
|
||||||
return render_nodes(reparse, reparse.nodes[:], ctx, partials, b, blocks)
|
}
|
||||||
|
i := 0
|
||||||
|
for i < len(content) {
|
||||||
|
if at_line_start^ {
|
||||||
|
strings.write_string(b, indent)
|
||||||
|
at_line_start^ = false
|
||||||
|
}
|
||||||
|
// Bulk-write up to and including the next newline (AVX2-backed).
|
||||||
|
rel := strings.index_byte(content[i:], '\n')
|
||||||
|
if rel < 0 {
|
||||||
|
strings.write_string(b, content[i:])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next := i + rel
|
||||||
|
strings.write_string(b, content[i:next + 1])
|
||||||
|
i = next + 1
|
||||||
|
at_line_start^ = true
|
||||||
}
|
}
|
||||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -525,16 +532,25 @@ render_nodes :: proc(
|
|||||||
partials: map[string]Template,
|
partials: map[string]Template,
|
||||||
b: ^strings.Builder,
|
b: ^strings.Builder,
|
||||||
blocks: map[string]Block_Override = nil,
|
blocks: map[string]Block_Override = nil,
|
||||||
|
indent_state: ^Indent_State = nil,
|
||||||
) -> Error {
|
) -> Error {
|
||||||
i := 0
|
i := 0
|
||||||
for i < len(nodes) {
|
for i < len(nodes) {
|
||||||
node := nodes[i]
|
node := nodes[i]
|
||||||
switch node.kind {
|
switch node.kind {
|
||||||
case .Text:
|
case .Text:
|
||||||
strings.write_string(b, node.text)
|
if indent_state != nil {
|
||||||
|
write_indented(b, indent_state.indent, node.text, &indent_state.at_line_start)
|
||||||
|
} else {
|
||||||
|
strings.write_string(b, node.text)
|
||||||
|
}
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
case .Variable:
|
case .Variable:
|
||||||
|
if indent_state != nil && indent_state.at_line_start {
|
||||||
|
strings.write_string(b, indent_state.indent)
|
||||||
|
indent_state.at_line_start = false
|
||||||
|
}
|
||||||
val := resolve_name(node.key, ctx[:])
|
val := resolve_name(node.key, ctx[:])
|
||||||
if val == nil {
|
if val == nil {
|
||||||
warn_unknown_key(current, ctx[:], node)
|
warn_unknown_key(current, ctx[:], node)
|
||||||
@@ -563,6 +579,7 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
&temp,
|
&temp,
|
||||||
blocks,
|
blocks,
|
||||||
|
nil,
|
||||||
) or_return
|
) or_return
|
||||||
write_value(b, strings.to_string(temp), escape = true)
|
write_value(b, strings.to_string(temp), escape = true)
|
||||||
}
|
}
|
||||||
@@ -572,6 +589,10 @@ render_nodes :: proc(
|
|||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
case .Unescaped:
|
case .Unescaped:
|
||||||
|
if indent_state != nil && indent_state.at_line_start {
|
||||||
|
strings.write_string(b, indent_state.indent)
|
||||||
|
indent_state.at_line_start = false
|
||||||
|
}
|
||||||
val := resolve_name(node.key, ctx[:])
|
val := resolve_name(node.key, ctx[:])
|
||||||
if val == nil {
|
if val == nil {
|
||||||
warn_unknown_key(current, ctx[:], node)
|
warn_unknown_key(current, ctx[:], node)
|
||||||
@@ -600,6 +621,7 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
&temp,
|
&temp,
|
||||||
blocks,
|
blocks,
|
||||||
|
nil,
|
||||||
) or_return
|
) or_return
|
||||||
write_value(b, strings.to_string(temp), escape = false)
|
write_value(b, strings.to_string(temp), escape = false)
|
||||||
}
|
}
|
||||||
@@ -635,6 +657,7 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
b,
|
b,
|
||||||
blocks,
|
blocks,
|
||||||
|
nil,
|
||||||
) or_return
|
) or_return
|
||||||
}
|
}
|
||||||
} else if is_truthy(val) {
|
} else if is_truthy(val) {
|
||||||
@@ -652,12 +675,13 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
b,
|
b,
|
||||||
blocks,
|
blocks,
|
||||||
|
indent_state,
|
||||||
) or_return
|
) or_return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
append(ctx, val)
|
append(ctx, val)
|
||||||
defer pop(ctx)
|
defer pop(ctx)
|
||||||
render_nodes(current, children, ctx, partials, b, blocks) or_return
|
render_nodes(current, children, ctx, partials, b, blocks, indent_state) or_return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i += 1 + len(node.children)
|
i += 1 + len(node.children)
|
||||||
@@ -675,7 +699,7 @@ render_nodes :: proc(
|
|||||||
val = transformed
|
val = transformed
|
||||||
}
|
}
|
||||||
if !is_truthy(val) {
|
if !is_truthy(val) {
|
||||||
render_nodes(current, node.children, ctx, partials, b, blocks) or_return
|
render_nodes(current, node.children, ctx, partials, b, blocks, indent_state) or_return
|
||||||
}
|
}
|
||||||
i += 1 + len(node.children)
|
i += 1 + len(node.children)
|
||||||
|
|
||||||
@@ -690,6 +714,9 @@ render_nodes :: proc(
|
|||||||
warn_missing_partial(current, partials, node, name)
|
warn_missing_partial(current, partials, node, name)
|
||||||
} else {
|
} else {
|
||||||
render_template(pt, ctx, partials, b, nil, node.indent) or_return
|
render_template(pt, ctx, partials, b, nil, node.indent) or_return
|
||||||
|
if indent_state != nil {
|
||||||
|
indent_state.at_line_start = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
@@ -720,8 +747,10 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
&temp,
|
&temp,
|
||||||
content_blocks,
|
content_blocks,
|
||||||
|
nil,
|
||||||
) or_return
|
) or_return
|
||||||
write_indented(b, node.indent, strings.to_string(temp))
|
at_ls := true
|
||||||
|
write_indented(b, node.indent, strings.to_string(temp), &at_ls)
|
||||||
} else {
|
} else {
|
||||||
render_nodes(
|
render_nodes(
|
||||||
render_current,
|
render_current,
|
||||||
@@ -730,6 +759,7 @@ render_nodes :: proc(
|
|||||||
partials,
|
partials,
|
||||||
b,
|
b,
|
||||||
content_blocks,
|
content_blocks,
|
||||||
|
indent_state,
|
||||||
) or_return
|
) or_return
|
||||||
}
|
}
|
||||||
i += 1 + len(node.children)
|
i += 1 + len(node.children)
|
||||||
@@ -743,6 +773,9 @@ render_nodes :: proc(
|
|||||||
} else {
|
} else {
|
||||||
warn_unmatched_block_overrides(current, pt, parent_children)
|
warn_unmatched_block_overrides(current, pt, parent_children)
|
||||||
render_template(pt, ctx, partials, b, merged, node.indent) or_return
|
render_template(pt, ctx, partials, b, merged, node.indent) or_return
|
||||||
|
if indent_state != nil {
|
||||||
|
indent_state.at_line_start = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
i += 1 + len(node.children)
|
i += 1 + len(node.children)
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-80
@@ -36,60 +36,73 @@ tokenize :: proc(
|
|||||||
text_start := 0
|
text_start := 0
|
||||||
|
|
||||||
for i < len(src) {
|
for i < len(src) {
|
||||||
if src[i] == '{' && i + 1 < len(src) && src[i + 1] == '{' {
|
// Find next '{' via AVX2/SSE-backed memchr instead of byte-by-byte scan.
|
||||||
if i > text_start {
|
rel := strings.index_byte(src[i:], '{')
|
||||||
append(&tokens, Token{kind = .Text, value = src[text_start:i], pos = text_start})
|
if rel < 0 {
|
||||||
|
i = len(src)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
next := i + rel
|
||||||
|
// Single '{' (not '{{') — advance past it and keep scanning.
|
||||||
|
if next + 1 >= len(src) || src[next + 1] != '{' {
|
||||||
|
i = next + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i = next
|
||||||
|
|
||||||
|
if i > text_start {
|
||||||
|
append(&tokens, Token{kind = .Text, value = src[text_start:i], pos = text_start})
|
||||||
|
}
|
||||||
|
|
||||||
|
tag_pos := i
|
||||||
|
|
||||||
|
if i + 2 < len(src) && src[i + 2] == '{' {
|
||||||
|
content_start := i + 3
|
||||||
|
idx := strings.index(src[content_start:], "}}}")
|
||||||
|
if idx < 0 {
|
||||||
|
return tokens, Error_Body {
|
||||||
|
msg = "unclosed triple mustache '{{{'",
|
||||||
|
pos = tag_pos,
|
||||||
|
kind = .Syntax,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close := content_start + idx
|
||||||
|
key := strings.trim_space(src[content_start:close])
|
||||||
|
append(&tokens, Token{kind = .Unescaped, value = key, pos = tag_pos})
|
||||||
|
i = close + 3
|
||||||
|
text_start = i
|
||||||
|
} else {
|
||||||
|
content_start := i + 2
|
||||||
|
sigil: byte = 0
|
||||||
|
if content_start < len(src) {
|
||||||
|
sigil = src[content_start]
|
||||||
}
|
}
|
||||||
|
|
||||||
tag_pos := i
|
kind: Token_Kind
|
||||||
|
key_start := content_start
|
||||||
|
|
||||||
if i + 2 < len(src) && src[i + 2] == '{' {
|
switch sigil {
|
||||||
content_start := i + 3
|
case '&':
|
||||||
idx := strings.index(src[content_start:], "}}}")
|
kind = .Unescaped; key_start = content_start + 1
|
||||||
if idx < 0 {
|
case '#':
|
||||||
return tokens, Error_Body {
|
kind = .Section_Open; key_start = content_start + 1
|
||||||
msg = "unclosed triple mustache '{{{'",
|
case '^':
|
||||||
pos = tag_pos,
|
kind = .Inverted_Open; key_start = content_start + 1
|
||||||
kind = .Syntax,
|
case '/':
|
||||||
}
|
kind = .Section_Close; key_start = content_start + 1
|
||||||
}
|
case '!':
|
||||||
close := content_start + idx
|
kind = .Comment; key_start = content_start + 1
|
||||||
key := strings.trim_space(src[content_start:close])
|
case '>':
|
||||||
append(&tokens, Token{kind = .Unescaped, value = key, pos = tag_pos})
|
kind = .Partial; key_start = content_start + 1
|
||||||
i = close + 3
|
case '<':
|
||||||
text_start = i
|
kind = .Parent; key_start = content_start + 1
|
||||||
} else {
|
case '$':
|
||||||
content_start := i + 2
|
kind = .Block_Open; key_start = content_start + 1
|
||||||
sigil: byte = 0
|
case:
|
||||||
if content_start < len(src) {
|
kind = .Variable
|
||||||
sigil = src[content_start]
|
}
|
||||||
}
|
|
||||||
|
|
||||||
kind: Token_Kind
|
close_idx := strings.index(src[key_start:], "}}")
|
||||||
key_start := content_start
|
|
||||||
|
|
||||||
switch sigil {
|
|
||||||
case '&':
|
|
||||||
kind = .Unescaped; key_start = content_start + 1
|
|
||||||
case '#':
|
|
||||||
kind = .Section_Open; key_start = content_start + 1
|
|
||||||
case '^':
|
|
||||||
kind = .Inverted_Open; key_start = content_start + 1
|
|
||||||
case '/':
|
|
||||||
kind = .Section_Close; key_start = content_start + 1
|
|
||||||
case '!':
|
|
||||||
kind = .Comment; key_start = content_start + 1
|
|
||||||
case '>':
|
|
||||||
kind = .Partial; key_start = content_start + 1
|
|
||||||
case '<':
|
|
||||||
kind = .Parent; key_start = content_start + 1
|
|
||||||
case '$':
|
|
||||||
kind = .Block_Open; key_start = content_start + 1
|
|
||||||
case:
|
|
||||||
kind = .Variable
|
|
||||||
}
|
|
||||||
|
|
||||||
close_idx := strings.index(src[key_start:], "}}")
|
|
||||||
if close_idx < 0 {
|
if close_idx < 0 {
|
||||||
return tokens, Error_Body {
|
return tokens, Error_Body {
|
||||||
msg = "unclosed tag '{{'",
|
msg = "unclosed tag '{{'",
|
||||||
@@ -97,40 +110,37 @@ tokenize :: proc(
|
|||||||
kind = .Syntax,
|
kind = .Syntax,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close := key_start + close_idx
|
close := key_start + close_idx
|
||||||
|
|
||||||
content := src[key_start:close]
|
content := src[key_start:close]
|
||||||
|
|
||||||
if kind == .Comment {
|
if kind == .Comment {
|
||||||
append(&tokens, Token{kind = .Comment, value = content, pos = tag_pos})
|
append(&tokens, Token{kind = .Comment, value = content, pos = tag_pos})
|
||||||
} else if kind == .Partial {
|
} else if kind == .Partial {
|
||||||
trimmed := strings.trim_space(content)
|
trimmed := strings.trim_space(content)
|
||||||
is_dyn := false
|
is_dyn := false
|
||||||
if len(trimmed) > 0 && trimmed[0] == '*' {
|
if len(trimmed) > 0 && trimmed[0] == '*' {
|
||||||
is_dyn = true
|
is_dyn = true
|
||||||
trimmed = strings.trim_space(trimmed[1:])
|
trimmed = strings.trim_space(trimmed[1:])
|
||||||
}
|
|
||||||
append(
|
|
||||||
&tokens,
|
|
||||||
Token {
|
|
||||||
kind = .Partial,
|
|
||||||
value = trimmed,
|
|
||||||
is_dynamic = is_dyn,
|
|
||||||
pos = tag_pos,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
append(
|
|
||||||
&tokens,
|
|
||||||
Token{kind = kind, value = strings.trim_space(content), pos = tag_pos},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
append(
|
||||||
i = close + 2
|
&tokens,
|
||||||
text_start = i
|
Token {
|
||||||
|
kind = .Partial,
|
||||||
|
value = trimmed,
|
||||||
|
is_dynamic = is_dyn,
|
||||||
|
pos = tag_pos,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
append(
|
||||||
|
&tokens,
|
||||||
|
Token{kind = kind, value = strings.trim_space(content), pos = tag_pos},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
i += 1
|
i = close + 2
|
||||||
|
text_start = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user