diff --git a/.clang-format b/.clang-format index 24d8f20..3048399 100644 --- a/.clang-format +++ b/.clang-format @@ -15,6 +15,8 @@ AccessModifierOffset: -4 AllowShortFunctionsOnASingleLine: Inline AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: Never +# Every if/else/for/while body gets braces (CLAUDE.md §4). +InsertBraces: true AllowShortLoopsOnASingleLine: false IncludeBlocks: Preserve # #includes sorted within each blank-line-separated block. clang-format cannot reorder diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..61b3549 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": ["${workspaceFolder}/.deps/lexbor/include", "${workspaceFolder}/pkg/runtime/picohttpparser"], + "defines": [], + "compilerPath": "/usr/bin/cc", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} diff --git a/CLAUDE.md b/CLAUDE.md index 8ff0e44..a1f980d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -159,6 +159,11 @@ appears in CI, read the code or shrink the CPU set; do not just re-run. `host_native_create` path. The UTF-16→UTF-8 bridge belongs in `pkg/jsc`, not `environment.odin`. - `make fmt` (`odin strip-semicolon`) before committing Odin. +- **`if` always has braces, on their own lines** — no `if x do y`, no `if x {y}` — + and **a blank line before and after every `if` statement**, unless it opens or + closes a block or a `case` (a comment directly above belongs to the `if`). Applied + by `make fmt`: odinfmt `convert_do`, clang-format `InsertBraces`, then + `scripts/fmt-if.mjs`, which neither formatter covers. - **Declaration order in a file**, Odin and C alike: imports/includes (sorted), then constants, then types, then procedures — public ones first, private helpers last. The formatters sort imports and includes (`odinfmt.json` `sort_imports`, diff --git a/Makefile b/Makefile index e6e782a..69d4833 100644 --- a/Makefile +++ b/Makefile @@ -388,6 +388,9 @@ fmt: # to strip-semicolon above, so a sync does not fight over whitespace. @if command -v $(ODINFMT) >/dev/null 2>&1; then $(ODINFMT) -w pkg/html; \ else echo 'make fmt: odinfmt not found; set ODINFMT=/path/to/odinfmt to format pkg/html'; fi + # Multi-line blocks after if/else/for and blank lines around if: no formatter does + # either (scripts/fmt-if.mjs). Run after odinfmt, whose convert_do makes the braces. + node scripts/fmt-if.mjs pkg/html # `rm` isn't a Windows command and can't take the $(RUNSCRIPT) prefix (bash would treat # `rm` as a script), so invoke the shell explicitly: $(SHELL) is Git Bash on Windows and diff --git a/pkg/html/cut.odin b/pkg/html/cut.odin index a039510..43e8d72 100644 --- a/pkg/html/cut.odin +++ b/pkg/html/cut.odin @@ -74,11 +74,15 @@ source_reset :: proc(s: ^Source) { // so the copy is amortized instead of paid on every piece of input. @(private) source_compact :: proc(s: ^Source) { - if s.off == 0 || s.off * 2 < len(s.buf) do return + if s.off == 0 || s.off * 2 < len(s.buf) { + return + } + n := s.off remove_range(&s.buf, 0, n) s.off = 0 s.scan = max(s.scan - n, 0) + if s.tag >= 0 { s.tag -= n s.tag_pos -= n @@ -90,14 +94,17 @@ source_compact :: proc(s: ^Source) { @(private) next_cut :: proc(s: ^Source, eof: bool) -> int { end := len(s.buf) + if s.tag < 0 { s.scan = max(s.scan, s.off) search: for s.scan < end { k := bytes.index_byte(s.buf[s.scan:], '<') + if k < 0 { s.scan = end break } + at := s.scan + k switch match_script_end(s.buf[at:]) { case .No: @@ -108,6 +115,7 @@ next_cut :: proc(s: ^Source, eof: bool) -> int { s.scan = end return end - s.off } + s.scan = at return at - s.off case .Yes: @@ -117,17 +125,25 @@ next_cut :: proc(s: ^Source, eof: bool) -> int { break search } } - if s.tag < 0 do return end - s.off + + if s.tag < 0 { + return end - s.off + } } // Inside a candidate: find its `>`, skipping quoted attribute values, as the // tokenizer does in the attribute states of an end tag. for ; s.tag_pos < end; s.tag_pos += 1 { b := s.buf[s.tag_pos] + if s.quote != 0 { - if b == s.quote do s.quote = 0 + if b == s.quote { + s.quote = 0 + } + continue } + switch { case b == '>': cut := s.tag_pos + 1 @@ -145,11 +161,13 @@ next_cut :: proc(s: ^Source, eof: bool) -> int { s.after_eq = false } } + if eof || s.tag_pos - s.tag > END_TAG_MAX { s.tag = -1 s.scan = end return end - s.off } + return s.tag - s.off } @@ -160,10 +178,19 @@ next_cut :: proc(s: ^Source, eof: bool) -> int { match_script_end :: proc(b: []byte) -> Match { needle := SCRIPT_END_TAG for i in 0 ..< len(needle) { - if i >= len(b) do return .Need_More - if ascii_lower(b[i]) != needle[i] do return .No + if i >= len(b) { + return .Need_More + } + + if ascii_lower(b[i]) != needle[i] { + return .No + } } - if len(b) == len(needle) do return .Need_More + + if len(b) == len(needle) { + return .Need_More + } + next := b[len(needle)] return .Yes if next == '/' || next == '>' || is_html_space(next) else .No } diff --git a/pkg/html/node.odin b/pkg/html/node.odin index 2bfd3ee..0f958ad 100644 --- a/pkg/html/node.odin +++ b/pkg/html/node.odin @@ -11,7 +11,11 @@ Node :: distinct rawptr text :: proc(node: Node, allocator := context.allocator) -> string { n: c.size_t t := larva_html_text(node, &n) - if t == nil do return "" + + if t == nil { + return "" + } + defer larva_html_text_free(node, t) return strings.clone(string(t[:n]), allocator) } @@ -24,16 +28,22 @@ text :: proc(node: Node, allocator := context.allocator) -> string { attr :: proc(node: Node, name: string, allocator := context.allocator) -> (value: string, ok: bool) { n: c.size_t v := larva_html_attr(node, raw_data(name), c.size_t(len(name)), &n) - if v == nil do return "", false + + if v == nil { + return "", false + } + return strings.clone(string(v[:n]), allocator), true } // outer_html returns `node`'s markup, the node itself included, as outerHTML does. outer_html :: proc(node: Node, allocator := context.allocator) -> (markup: string, ok: bool) { b := strings.builder_make(allocator) + if larva_html_serialize_node(node, collect_markup, &b) != LXB_STATUS_OK { strings.builder_destroy(&b) return "", false } + return strings.to_string(b), true } diff --git a/pkg/html/odinfmt.json b/pkg/html/odinfmt.json index 50a4519..7cc33a8 100644 --- a/pkg/html/odinfmt.json +++ b/pkg/html/odinfmt.json @@ -4,5 +4,6 @@ "tabs": true, "tabs_width": 4, "sort_imports": true, - "newline_limit": 2 + "newline_limit": 2, + "convert_do": true } diff --git a/pkg/html/parser.odin b/pkg/html/parser.odin index 03fc4cb..cdd1f98 100644 --- a/pkg/html/parser.odin +++ b/pkg/html/parser.odin @@ -73,15 +73,19 @@ init :: proc(p: ^Parser, allocator := context.allocator) -> bool { return false } p.doc = lxb_html_document_create() + if p.doc == nil { p.err = "pkg/html: lexbor could not create a document" return false } + p.hook = larva_html_begin(p.doc) + if p.hook == nil { p.err = "pkg/html: lexbor could not start parsing" return false } + return true } @@ -89,8 +93,14 @@ init :: proc(p: ^Parser, allocator := context.allocator) -> bool { // dangling afterwards. destroy :: proc(p: ^Parser) { // The document first: the hook is lexbor's tokenizer callback context until then. - if p.doc != nil do lxb_html_document_destroy(p.doc) - if p.hook != nil do larva_html_hook_free(p.hook) + if p.doc != nil { + lxb_html_document_destroy(p.doc) + } + + if p.hook != nil { + larva_html_hook_free(p.hook) + } + delete(p.input.buf) delete(p.written.buf) p^ = {} @@ -114,7 +124,10 @@ finish :: proc(p: ^Parser) { // Returns: // false, and nothing is inserted, once the document is done or the parser failed. write :: proc(p: ^Parser, text: string) -> bool { - if p.ended || p.err != "" do return false + if p.ended || p.err != "" { + return false + } + inject_at(&p.written.buf, p.ins, ..transmute([]byte)text) p.ins += len(text) return true @@ -123,10 +136,17 @@ write :: proc(p: ^Parser, text: string) -> bool { // pump parses until the next parser-blocking script, the end of what has been fed, or // the end of the document. pump :: proc(p: ^Parser) -> (script: Node, status: Pump_Status) { - if p.err != "" do return nil, .Failed - if p.ended do return nil, .Done + if p.err != "" { + return nil, .Failed + } + + if p.ended { + return nil, .Done + } + for { src, src_eof := &p.input, p.eof + if source_pending(&p.written) > 0 { src, src_eof = &p.written, true } else if len(p.written.buf) > 0 { @@ -139,30 +159,38 @@ pump :: proc(p: ^Parser) -> (script: Node, status: Pump_Status) { source_compact(&p.input) return nil, .Need_Input } + p.ended = true + if lxb_html_document_parse_chunk_end(p.doc) != LXB_STATUS_OK { return nil, fail(p, "pkg/html: lexbor failed to end the document") } + return nil, .Done } n := next_cut(src, src_eof) + if n == 0 { source_compact(&p.input) return nil, .Need_Input } + if lxb_html_document_parse_chunk(p.doc, raw_data(src.buf[src.off:]), c.size_t(n)) != LXB_STATUS_OK { return nil, fail(p, "pkg/html: lexbor failed to parse") } + src.off += n closed: c.size_t s := larva_html_take_script(p.hook, &closed) + if closed > 1 { // A slice closed two scripts: the first was never handed out. cut.odin keeps // this from happening; failing beats losing a script quietly. return nil, fail(p, "pkg/html: two scripts closed in one slice") } + if s != nil { p.ins = p.written.off return s, .Script @@ -185,12 +213,15 @@ parse_all :: proc( script, status := pump(p) switch status { case .Script: - if on_script != nil do on_script(p, script, user_data) + if on_script != nil { + on_script(p, script, user_data) + } case .Need_Input: if off == len(data) { finish(p) continue } + end := len(data) if chunk <= 0 else min(off + chunk, len(data)) feed(p, data[off:end]) off = end @@ -216,12 +247,17 @@ error_message :: proc(p: ^Parser) -> string { // serialize returns the document's markup — the doctype, then the html element. // Comparing it with Chrome's is the oracle's job (#2), not done yet. serialize :: proc(p: ^Parser, allocator := context.allocator) -> (markup: string, ok: bool) { - if p.doc == nil do return "", false + if p.doc == nil { + return "", false + } + b := strings.builder_make(allocator) + if lxb_html_serialize_deep_cb(larva_html_document_node(p.doc), collect_markup, &b) != LXB_STATUS_OK { strings.builder_destroy(&b) return "", false } + return strings.to_string(b), true } diff --git a/pkg/html/shim/larva_html.c b/pkg/html/shim/larva_html.c index c9358e2..6d0df2d 100644 --- a/pkg/html/shim/larva_html.c +++ b/pkg/html/shim/larva_html.c @@ -46,27 +46,38 @@ typedef struct { // end tag with that current node, so it is ignored. static lxb_html_token_t *larva_hook_cb(lxb_html_tokenizer_t *tkz, lxb_html_token_t *token, void *ctx) { larva_html_hook *h = ctx; + if (token->tag_id == LXB_TAG_SCRIPT && (token->type & LXB_HTML_TOKEN_TYPE_CLOSE)) { lxb_dom_node_t *cur = lxb_html_tree_current_node(h->tree); + if (cur != NULL && cur->local_name == LXB_TAG_SCRIPT) { h->script = cur; h->closed++; } } + return h->orig(tkz, token, h->orig_ctx); } // Starts chunked parsing of `doc` and installs the hook. NULL on failure; free with // larva_html_hook_free after the document's parse has ended. larva_html_hook *larva_html_begin(lxb_html_document_t *doc) { - if (lxb_html_document_parse_chunk_begin(doc) != LXB_STATUS_OK) + if (lxb_html_document_parse_chunk_begin(doc) != LXB_STATUS_OK) { return NULL; + } + lxb_html_parser_t *parser = doc->dom_document.parser; - if (parser == NULL || parser->tkz == NULL || parser->tree == NULL) + + if (parser == NULL || parser->tkz == NULL || parser->tree == NULL) { return NULL; + } + larva_html_hook *h = calloc(1, sizeof(*h)); - if (h == NULL) + + if (h == NULL) { return NULL; + } + h->orig = parser->tkz->callback_token_done; h->orig_ctx = parser->tkz->callback_token_ctx; h->tree = parser->tree; @@ -100,8 +111,9 @@ lxb_char_t *larva_html_text(lxb_dom_node_t *node, size_t *len) { } void larva_html_text_free(lxb_dom_node_t *node, lxb_char_t *text) { - if (text != NULL) + if (text != NULL) { lxb_dom_document_destroy_text(node->owner_document, text); + } } // The value of attribute `name` on element `node`, or NULL when absent. Owned by the @@ -112,19 +124,28 @@ const lxb_char_t *larva_html_attr(lxb_dom_node_t *node, const lxb_char_t *name, larva_css *larva_css_create(void) { larva_css *e = calloc(1, sizeof(*e)); - if (e == NULL) + + if (e == NULL) { return NULL; + } + e->parser = lxb_css_parser_create(); e->selectors = lxb_selectors_create(); + if (e->parser == NULL || e->selectors == NULL || lxb_css_parser_init(e->parser, NULL) != LXB_STATUS_OK || lxb_selectors_init(e->selectors) != LXB_STATUS_OK) { - if (e->selectors != NULL) + if (e->selectors != NULL) { lxb_selectors_destroy(e->selectors, true); - if (e->parser != NULL) + } + + if (e->parser != NULL) { lxb_css_parser_destroy(e->parser, true); + } + free(e); return NULL; } + // querySelectorAll reports a node once, however many selectors of a list // (`a, a.x`) it matches; lexbor's default reports it once per selector. lxb_selectors_opt_set(e->selectors, LXB_SELECTORS_OPT_MATCH_FIRST); @@ -132,8 +153,10 @@ larva_css *larva_css_create(void) { } void larva_css_destroy(larva_css *e) { - if (e == NULL) + if (e == NULL) { return; + } + lxb_selectors_destroy(e->selectors, true); lxb_css_parser_destroy(e->parser, true); free(e); @@ -142,10 +165,12 @@ void larva_css_destroy(larva_css *e) { // NULL when `css` is not a valid selector list. lxb_css_selector_list_t *larva_css_compile(larva_css *e, const lxb_char_t *css, size_t len) { lxb_css_selector_list_t *list = lxb_css_selectors_parse(e->parser, css, len); + if (e->parser->status != LXB_STATUS_OK) { lxb_css_selector_list_destroy_memory(list); return NULL; } + return list; } diff --git a/scripts/fmt-if.mjs b/scripts/fmt-if.mjs new file mode 100644 index 0000000..2e4f94d --- /dev/null +++ b/scripts/fmt-if.mjs @@ -0,0 +1,198 @@ +// Two layout rules no formatter here enforces, for Odin and C/C++ (CLAUDE.md §4): +// +// 1. A block opened on the same line as its `if`, `else`, `for`, `when` or `defer if` +// is spread over lines: `if x {return 2}` becomes three lines. odinfmt's convert_do +// turns `if x do y` into the one-line form, and clang-format's InsertBraces adds the +// braces in C, so after them every `if` has braces and this makes them multi-line. +// 2. An `if` statement has a blank line before and after it, unless it opens or closes +// a block or a case: a `{` or `case …:` line just above, a `}` or `case` just below. +// Comments directly above an `if` belong to it, so the blank line goes above them. +// +// Line-based on purpose: both rules are about lines, and a parser per language would be +// far more code than the rules. It tracks just enough lexical state to be safe — strings, +// runes, line and block comments, and Odin's multi-line raw strings (embedded JS in the +// tests is full of `if`) — and leaves any line it cannot read with confidence alone. +// +// Usage: node scripts/fmt-if.mjs ... (rewrites in place; idempotent) + +import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs'; +import { extname, join } from 'node:path'; + +const EXTS = new Set(['.odin', '.c', '.h', '.cpp', '.hpp']); +const BLOCK_KEYWORD = /^(\} else if |\} else \{|if |else |for |when |defer if |\} else when )/; + +/** + * Splits `line` into code characters and everything else, carrying multi-line state. + * Returns per-character flags `code[i]` (true outside strings and comments) and the + * state at the end of the line. + */ +function scan(line, state, isC) { + const code = new Array(line.length).fill(false); + let { raw, block } = state; + for (let i = 0; i < line.length; i++) { + const ch = line[i]; + if (raw) { + if (ch === '`') raw = false; + continue; + } + if (block) { + if (ch === '*' && line[i + 1] === '/') { + block = false; + i++; + } + continue; + } + if (ch === '/' && line[i + 1] === '/') break; + if (ch === '/' && line[i + 1] === '*') { + block = true; + i++; + continue; + } + if (ch === '`' && !isC) { + raw = true; + continue; + } + if (ch === '"' || ch === "'") { + const quote = ch; + for (i++; i < line.length && line[i] !== quote; i++) if (line[i] === '\\') i++; + continue; + } + code[i] = true; + } + return { code, state: { raw, block } }; +} + +function braceDelta(line, code) { + let d = 0; + for (let i = 0; i < line.length; i++) { + if (!code[i]) continue; + if (line[i] === '{') d++; + else if (line[i] === '}') d--; + } + return d; +} + +// Rule 1: `head {body}` on one line, where the closing brace is the line's last code +// character and matches a `{` that follows the keyword's condition. +function spreadOneLineBlock(line, code, indent) { + const t = line.trim(); + if (!BLOCK_KEYWORD.test(t) || !t.endsWith('}')) return null; + const end = line.lastIndexOf('}'); + if (!code[end]) return null; + let depth = 0; + let open = -1; + for (let i = end; i >= 0; i--) { + if (!code[i]) continue; + if (line[i] === '}') depth++; + else if (line[i] === '{' && --depth === 0) { + open = i; + break; + } + } + if (open < 0) return null; + const head = line.slice(0, open).trimEnd(); + const body = line.slice(open + 1, end).trim(); + // `} else if c {x}`: the head's own leading `}` closes an earlier block, fine. Bail on + // an empty body (`for {}` is deliberate) or several statements. + if (body === '' || body.includes(';')) return null; + if (head.trim() === '}' || head.trim() === '') return null; + return [`${head} {`, `${indent}\t${body}`, `${indent}}`]; +} + +const isBlank = (s) => s.trim() === ''; +const isComment = (s) => /^\s*(\/\/|\/\*|\*)/.test(s); +const opensOrLabels = (s) => { + const t = s.trim(); + return t.endsWith('{') || /^(case\b.*|default):\s*$/.test(t) || t.endsWith(':'); +}; +const closesOrLabels = (s) => { + const t = s.trim(); + return t.startsWith('}') || /^(case\b|default:)/.test(t) || t.startsWith('#'); +}; + +function formatSource(src, isC) { + const lines = src.split('\n'); + + // Pass 1: spread one-line blocks. Odin only: in C, clang-format already did it. + let out = []; + let state = { raw: false, block: false }; + for (const line of lines) { + const startState = state; + const { code, state: next } = scan(line, state, isC); + state = next; + const indent = line.match(/^\s*/)[0]; + const spread = !isC && !startState.raw && !startState.block ? spreadOneLineBlock(line, code, indent) : null; + out.push(...(spread ?? [line])); + } + + // Pass 2: blank lines around `if` statements. + const src2 = out; + const ifStart = isC ? /^if\s*\(/ : /^if /; + const result = []; + const blankAfter = new Set(); // indexes into src2 after which a blank line goes + state = { raw: false, block: false }; + const scans = src2.map((line) => { + const startState = state; + const r = scan(line, state, isC); + state = r.state; + return { code: r.code, inCode: !startState.raw && !startState.block }; + }); + + const blankBefore = new Set(); // indexes into src2 before which a blank line goes + for (let i = 0; i < src2.length; i++) { + const line = src2[i]; + if (!scans[i].inCode || !ifStart.test(line.trim())) continue; + // Before: skip up over comments attached to the if. + let top = i; + while (top > 0 && isComment(src2[top - 1]) && scans[top - 1].inCode) top--; + if (top > 0 && !isBlank(src2[top - 1]) && !opensOrLabels(src2[top - 1])) blankBefore.add(top); + // After: follow the braces to the end of the whole if/else chain. + let depth = 0; + let seenOpen = false; + let end = i; + for (let j = i; j < src2.length; j++) { + if (!scans[j].inCode) break; + depth += braceDelta(src2[j], scans[j].code); + if (src2[j].includes('{')) seenOpen = true; + end = j; + // The chain ends on a line that closes the last block: `}` (or a one-line block's + // `}`). `} else if a ||` continues it, and so does `} else {`. + if (seenOpen && depth <= 0 && src2[j].trim().endsWith('}')) break; + if (!seenOpen && isC && /;\s*$/.test(src2[j])) break; // braceless C statement + if (!seenOpen && !isC && / do /.test(src2[j])) break; // a `do` form odinfmt left + if (j - i > 2000) break; + } + if (end + 1 < src2.length && !isBlank(src2[end + 1]) && !closesOrLabels(src2[end + 1])) blankAfter.add(end); + } + + for (let i = 0; i < src2.length; i++) { + if (blankBefore.has(i) && !(result.length && isBlank(result[result.length - 1]))) result.push(''); + result.push(src2[i]); + if (blankAfter.has(i)) result.push(''); + } + return result.join('\n'); +} + +function walk(path, files) { + const st = statSync(path); + if (st.isDirectory()) { + for (const name of readdirSync(path)) { + if (name === 'picohttpparser' || name.startsWith('.') || name === 'node_modules') continue; + walk(join(path, name), files); + } + } else if (EXTS.has(extname(path))) files.push(path); +} + +const files = []; +for (const arg of process.argv.slice(2)) walk(arg, files); +let changed = 0; +for (const file of files) { + const src = readFileSync(file, 'utf8'); + const isC = extname(file) !== '.odin'; + const formatted = formatSource(src, isC); + if (formatted !== src) { + writeFileSync(file, formatted); + changed++; + } +} +console.log(`fmt-if: ${changed} of ${files.length} files changed`);