Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 33 additions & 6 deletions pkg/html/cut.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/html/node.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion pkg/html/odinfmt.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"tabs": true,
"tabs_width": 4,
"sort_imports": true,
"newline_limit": 2
"newline_limit": 2,
"convert_do": true
}
50 changes: 43 additions & 7 deletions pkg/html/parser.odin
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,34 @@ 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
}

// destroy frees the document and everything in it. Nodes from this parser are
// 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^ = {}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
Loading
Loading