Repository navigation
♻️ refactor(c): house style, decompose run_drain - #482
Conversation
Codify the C-tree and Python-shim conventions the consistency audit found in the good majority: file-header why-comments, no section banners, no single-letter names, the th_/turbohtml_ prefix split, one child-iteration idiom, shared vec.h/ascii.h helpers, and the docstring/__all__/Final rules for the shim. part of tox-dev#478
The consistency audit found divider banners in ~28 C files and none in the rest, including the largest hand-written units. House style bans section and grouping-header comments, so drop the dividers tree-wide; where a banner carried a spec or issue reference (EXSLT families, issue tox-dev#182/tox-dev#267, the CSS Level 4 input pseudo-classes) fold that text into a plain comment on the declaration it described. part of tox-dev#478
markdown.c had drifted from the strict dom/ naming convention with single-letter locals. Rename them to descriptive names: loop indices to index/scan, the code point to ch, the run/pad/row/column counters, and the text and count parameters. part of tox-dev#478
run_drain was a ~1640-line function whose body was one giant switch over the twenty insertion modes. Lift each case into a drain_<mode> handler that takes the tree, the current token, and a th_insert context (the tokenizer handle plus the insertion-mode state the WHATWG algorithm threads across tokens). A handler returns TH_DRAIN_NEXT to advance or TH_DRAIN_REPROCESS to run the same token again under the mode it just set; run_drain keeps the token pump, the shared foreign/template/select pre-dispatch, and the reprocess loop, and now reads top to bottom in call order. Behavior is byte-identical: each case body moved verbatim, with break and goto reprocess rewritten to the two return codes and the threaded state reached through the context. The handlers are single-call-site statics the compiler still inlines, so the hot tree-construction path is unchanged. part of tox-dev#478
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
@gaborbernat is this file your invention? Is it wired somewhere into the agent configs? Is it documented anywhere? I'd like to learn more and maybe borrow the approach into other projects.
There was a problem hiding this comment.
A bit of both 🤔 but based on my experience using it the last week I think I'll just integrate it into https://turbohtml.readthedocs.io/en/stable/development/index.html
This patch establishes the entry-point chain `CLAUDE.md` -> `AGENTS.md` -> `.agents-ctx/HOUSE_RULES.md`, along with `.agents-ctx/HOUSE_STYLE.md` and a `.gitignore` that restricts `.agents-ctx/` to Markdown files. Together these form the initial scaffolding for the house-style/tribal-knowledge memory that Claude Code and other agentic tools load when working in this repo. In this patch, `HOUSE_STYLE.md` documents the only rule so far: no new `@`-mention imports below `AGENTS.md`. `@`-syntax loads eagerly at the start of every session regardless of relevance, so letting it spread would pollute the context window; everything past `HOUSE_RULES.md` links out with plain Markdown instead, to be pulled in on demand. This mirrors the approach outlined in [1]. The `.gitignore` also gains a `CLAUDE.local.md` entry, matching how `ansible/ansible` keeps that personal override file untracked. The naming and idea behind `HOUSE_STYLE.md` come from Bernát Gábor (@gaborbernat), as discussed in the PR thread linked below [2]. [1] https://www.humanlayer.dev/blog/writing-a-good-claude-md [2] tox-dev/turbohtml#482 (comment)
This is the consistency-and-house-style workstream of the pre-1.0 review (#478). The C tree had grown a well-built but unevenly-styled surface: file-section banner comments in a minority of files and none in the rest,
markdown.clocals that had drifted to single letters against the strictdom/convention, and arun_draintree-construction driver that had swollen to roughly 1640 lines of one giant switch. None of this changes what the parser does; all of it makes the code harder to read and to keep consistent as 1.0 approaches.The headline is
run_drain. Its body was a single switch over the twenty WHATWG insertion modes, so following any one mode meant scrolling past the other nineteen. 🧵 Each case now lifts into adrain_<mode>handler that takes the tree, the current token, and ath_insertcontext carrying the tokenizer handle and the insertion-mode state the algorithm threads across tokens. A handler returnsTH_DRAIN_NEXTto advance orTH_DRAIN_REPROCESSto run the same token again under the mode it just set;run_drainkeeps the token pump, the shared foreign/template/select pre-dispatch, and the reprocess loop, and now reads top to bottom in call order. The handlers are single-call-site statics the compiler still inlines, so the hot path is unchanged.Alongside that, a new repository
HOUSE_STYLE.mdrecords the conventions the audit found in the good majority, for both the C tree and the Python shim. The banner sweep applies the majority convention uniformly by dropping the section dividers, folding the few that carried a spec or issue reference (the EXSLT families, issue #182/#267, the CSS Level 4 input pseudo-classes) into a plain comment on the declaration. Themarkdown.crename restores descriptive local names.Every change is behavior-preserving: parser output is byte-identical. The shared
core/vec.handcore/ascii.hdeduplication the workstream also scopes is deferred to a focused follow-up, since it is an all-or-nothing consistency change across heterogeneous allocators and cap types that a partial pass would leave less consistent, not more. Part of #478.