tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

116 lignes · 6531 octetsBrut · Historique

ADR 0057: std's templaters are compiled syntax for the HTML tree

Status: Proposed (2026-10-22); needs engineering review (#44).

Supersedes the templater part of ADR 0048 (§std's templaters); resolves the "tagged templates" follow-up of ADR 0054. Issue #298.

Context

ADR 0048 made every templater an ordinary function called at run time with a Template(parts, holes). That left std's pug, html and xml worse than the HTML literals in three ways (issue #298):

  • Boolean attributes render backwards. Every template hole is stringified, so pug\input(autofocus={off})`withoff = Falserendersautofocus="False"— and HTML reads a present boolean attribute as on. The literal's checker lowers aBool` attribute hole to a flag; a runtime templater cannot see a hole's position.
  • Template errors happen at run time. A malformed pug template passed louise check and panicked at the first render, with a template-relative line number and no source span.
  • Pug was cut off from the HTML pipeline. ADR 0054's precompiled renderers skip templater calls, so a pug page always built and walked the whole Element tree.

Decision

When a template's tag resolves lexically to std/template.pug, .html or .xml, the compiler lowers the template's text to the HIR an HTML literal produces; there is no Template value and no call. A tag resolving to anything else (a user templater like css or sql) keeps the ADR 0048 runtime path untouched.

  • The pug parser moves into the compiler (louise_hir/src/lower/ pug.rs), a faithful port of the Louise grammar ADR 0048 wrote in std/template.lse: the indentation tree, .class/#id shorthands (bare shorthands are divs), (attrs) (name="value", name={hole}, bare flags), | text lines, // comments, holes inline or alone on a line, and deeper lines as children. Its output is Expr::Html nodes with the holes at their positions; an uppercase tag is a component call (Card(title={t}) with indented children is card(title: t, children: [...])), exactly as in a literal.
  • Spans map back into the source. The template's text tokens keep their source ranges; the parser decodes escapes itself, so every character carries the range of itself or of its escape sequence and every hole the range of its {…}. Every runtime panic of the Louise parser is now a spanned diagnostic (E0601) at the token or line at fault, with the same message (its line number is the template's own, as before). The first error aborts the template, as the first panic did. Two panics were type-directed — "an element can't be an attribute value" — and are type errors now: E0602 in name={e}, E0401 (Element has no Show) inside a quoted value.
  • Holes are typed by position, with one rule set for both syntaxes (extending ADR 0009): an attribute hole is a String or Int (text), a Bool (a flag, present iff true) or — new, for literals too — an Option<String> (None leaves the attribute out); a child hole is a String or shown value (escaped text), an Element or a List<Element> (embedded); a hole anywhere else (a tag or attribute name) is a parse error. A {e:?} hole shows with Debug, as templates always did: the lowering wraps it in a one-hole string interpolation, so it is a String to the HTML rules.
  • html is raw markup. Its text parts become Raw children (a new HtmlChild::Raw, lowered to the existing Raw constructor) and its holes children typed as above — byte-identical to the runtime html, which never parsed.
  • xml is the same fragment, rendered into the opaque Xml by a kept runtime helper the compiler calls: std/template.xml_of_element(e) = Xml(html.render(e)). Raw parts append as written, text holes are escaped, element holes render — the runtime xml's output byte for byte. (A tree is still built for xml; its result is a String, so ADR 0054 does not apply — as before.)
  • Option<String> attributes lower to a new std/html constructor Optional(String, Option<String>): render_attributes appends name="v" for Some(v) and nothing for None. ADR 0054's pass matches it: a constant folds, else the append sits under a switch on the option.
  • std/template keeps Template and Hole (user templaters, e.g. the postgres package's sql), and keeps the pug/html/xml declarations as the tags' resolution targets, with unreachable bodies — they are never called: tag position is intercepted, and a reference to one of them anywhere else is E0605 ("std's templaters are syntax, not values"). A local shadowing the name resolves to the local and is fine; the check is on the resolved definition (module path std/template + name), like Core's std_ctor.

Consequences

  • pug\input(required={on} autofocus={off})`withoff = Falserenders<input required>`. Output changes only where it was wrong.
  • A malformed template fails to compile, with a span, in the editor and at the command line; go-to-definition and hover work inside pug holes (the holes are ordinary expressions with their own spans).
  • A compiled template is constructor calls, so ADR 0054's precompilation fires on pug pages unchanged: no Element tree is built for a precompiled page.
  • Code passing pug (or html, xml) around as a value stops compiling; nothing in the repo did.
  • The interpreter and the C backend needed no template code: compiled templates are Expr::Html, which both already lower.
  • Formatter follow-up (not here): the formatter keeps template text verbatim; normalising pug indentation is a separate change.

Alternatives considered

  • Compile-time evaluation of the Louise pug parser. ADR 0048's "run pure templaters while compiling" would have kept one implementation, but nothing runs user code at compile time, and the port keeps the grammar next to the literal lowering it must match anyway (spans, components, hole positions).
  • xml as constructor calls like html. The Xml wrapper is an opaque string, not an Element; rendering through the kept helper is byte-identical and adds no Core concept.
  • Recovering after a pug error to report more of them. The runtime panicked on the first; one clear spanned error is already the improvement, and recovery rules would be guesswork.