Skip to content

Fix tag and expression parsing bugs, make parentheses optional, and speed up rendering - #11

Merged
mierau merged 4 commits into
mainfrom
claude/project-thread-nn9aly
Oct 10, 2026
Merged

mierau merged 4 commits into
mainfrom
claude/project-thread-nn9aly

Conversation

@mierau

@mierau mierau commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Before: {% if (a) or (b) %} and {% log(a) | uppercase %} failed to parse, {% if x %} failed with "Unexpected x", foreach(item in with a newline before items was rejected, x IN [c] failed, "inf" + 1 rendered inf and "0x10" + 1 rendered 17, and tags | default("none") ignored an empty list.

After: all of these work. if, elseif and foreach no longer need parentheses, as in {% if count > 2 %} and {% foreach item in items %}. Rendering the list benchmark is about 14% faster (156 → 134 µs per render, Linux, Swift 6.0).

How:

  • A tag's parentheses no longer have to wrap the whole expression. The parser finds the ) that matches the opening ( (skipping quoted text). When that ) ends the tag, it strips the pair as before. Otherwise the whole (...) ... remainder is the expression.
  • When no parentheses follow, if, elseif, else if and foreach take the rest of the tag as the expression after a space. A bare {% if %} or {% foreach %} is still a key. log keeps requiring parentheses, so {% log | escape %} still renders a key named log.
  • foreach finds in with any whitespace on either side.
  • [c] / [d] after a comparison counts as flags only when an operand follows. Otherwise it's a list.
  • Text reads as a number only when it's decimal. Swift's Double(String) also accepts nan, inf and hex. Such text now joins as text with +, so "inf" + 1 gives inf1.
  • default also falls back for an empty list or dictionary.
  • Speed: function calls build one array instead of two, the last if branch skips an unneeded jump, @sum only computes the floating-point total when it's needed, and reading a date from text tries only the ISO 8601 forms it could be in, since each failed parse throws.

Tests: 105 pass on Linux with Swift 6.0.3, including new cases for each fix.

- Conditions like if (a) or (b) parse: a tag's parentheses no longer have
  to wrap the whole condition. log(a) | uppercase works for the same reason.
- foreach accepts any whitespace around 'in'.
- if x and foreach x in y explain that they need parentheses.
- x IN [c] reads [c] as a list, not the case-insensitive flag.
- Only decimal text reads as a number, so 'inf', 'nan' and '0x10' don't.
- default(...) also replaces an empty list or dictionary.
- Function calls allocate one array instead of two.
- The last if branch no longer emits a jump when there's no else.
- Reading a date from text tries only the ISO 8601 forms it could be in.
@mierau mierau self-assigned this Oct 10, 2026
{% if count > 2 %} and {% foreach item in items %} now work alongside the
parenthesized forms. A bare {% if %} or {% foreach %} is still a key.
@mierau mierau changed the title Fix tag and expression parsing bugs, and speed up rendering Fix tag and expression parsing bugs, make parentheses optional, and speed up rendering Oct 10, 2026
@mierau
mierau merged commit 3010147 into main Oct 10, 2026
2 checks passed
@mierau
mierau deleted the claude/project-thread-nn9aly branch October 10, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant