// The block parser: CommonMark's two-phase algorithm. Lines are fed into a tree of open blocks; inline content is // parsed once every link reference is known. Blocks live in a list and point at each other by index. import type { Align, Block, Document, Inline, ListItem, TableCell } from "../ast" import { isAlpha, isBlank, isDigit, isSpaceOrTab, unescape } from "./chars" import { parseInlines, parseReference, scanCloseTag, scanOpenTag } from "./inlines" import type { Ref } from "./inlines" /// How deep quotes and lists nest. Deeper markers are text, so a hostile document can't exhaust the stack. let MAX_DEPTH = 64 type Kind = | "document" | "quote" | "list" | "item" | "code" | "html" | "para" | "heading" | "break" | "table" type Node = { kind: Kind, parent: int, children: int[], open: boolean, depth: int, startLine: int, endLine: int, lines: string[], ordered: boolean, bullet: int, delimiter: int, start: int, padding: int, markerOffset: int, tight: boolean, fenced: boolean, fenceChar: int, fenceLength: int, fenceOffset: int, info: string, htmlType: int, level: int, align: Align[], rows: string[][], } type Marker = { ordered: boolean, bullet: int, delimiter: int, start: int, padding: int, markerOffset: int, } type Parser = { nodes: Node[], refs: Map, gfm: boolean, line: string, lineNo: int, offset: int, column: int, nextNonspace: int, nextNonspaceColumn: int, indent: int, indented: boolean, blank: boolean, partialTab: boolean, tip: int, oldTip: int, lastMatched: int, allClosed: boolean, } fn newNode(kind: Kind, parent: int, line: int, depth: int): Node { return { kind, parent, children: [], open: true, depth, startLine: line, endLine: line, lines: [], ordered: false, bullet: 0, delimiter: 0, start: 1, padding: 0, markerOffset: 0, tight: true, fenced: false, fenceChar: 0, fenceLength: 0, fenceOffset: 0, info: "", htmlType: 0, level: 0, align: [], rows: [], } } fn canContain(parent: Kind, child: Kind): boolean { if parent == "document" || parent == "quote" || parent == "item" { return child != "item" } return parent == "list" && child == "item" } fn acceptsLines(kind: Kind): boolean { return kind == "para" || kind == "code" || kind == "html" } // ---- what the parser asks about the line ---- fn isLazyLine(p: Parser): boolean { return !p.allClosed && !p.blank && p.nodes[p.tip].kind == "para" } fn continuesList(p: Parser, id: int, m: Marker): boolean { let n = p.nodes[id] return n.kind == "list" && n.ordered == m.ordered && n.bullet == m.bullet && n.delimiter == m.delimiter } fn fenceCloses(p: Parser, id: int): boolean { return p.indent <= 3 && closingFence(p.line, p.nextNonspace, p.nodes[id].fenceChar, p.nodes[id].fenceLength) } fn itemEnd(p: Parser, id: int, last: int): int { return last != -1 ? p.nodes[last].endLine : p.nodes[id].startLine } // ---- moving along the line ---- fn peekAt(p: Parser, i: int): int { return p.line.codeUnitAt(i) ?? -1 } fn unitAt(line: string, i: int): int { return line.codeUnitAt(i) ?? -1 } fn findNextNonspace(mut p: Parser) { let mut i = p.offset let mut cols = p.column let mut c = -1 while true { c = peekAt(p, i) if c == 32 { i += 1 cols += 1 } else if c == 9 { i += 1 cols += 4 - cols % 4 } else { break } } p.blank = c == -1 p.nextNonspace = i p.nextNonspaceColumn = cols p.indent = cols - p.column p.indented = p.indent >= 4 } fn advanceNextNonspace(mut p: Parser) { p.offset = p.nextNonspace p.column = p.nextNonspaceColumn p.partialTab = false } fn advance(mut p: Parser, count: int, columns: boolean) { let mut left = count while left > 0 { let c = (p.line.codeUnitAt(p.offset) ?? -1) if c == -1 { break } if c == 9 { let toTab = 4 - p.column % 4 if columns { let partial = toTab > left let step = partial ? left : toTab p.partialTab = partial p.column += step p.offset += partial ? 0 : 1 left -= step } else { p.partialTab = false p.column += toTab p.offset += 1 left -= 1 } } else { p.partialTab = false p.offset += 1 p.column += 1 left -= 1 } } } fn addLine(mut p: Parser) { let mut prefix = "" if p.partialTab { p.offset += 1 prefix = " ".repeat(4 - p.column % 4) } let text = prefix + p.line.slice(p.offset) let tip = p.tip p.nodes[tip].lines.push(text) } // ---- the tree ---- fn addChild(mut p: Parser, kind: Kind): int { while true { let open = p.tip let openKind = p.nodes[open].kind if canContain(openKind, kind) { break } let before = p.lineNo - 1 finalize(&p, open, before) } let parent = p.tip let id = p.nodes.length let depth = p.nodes[parent].depth + 1 let line = p.lineNo p.nodes.push(newNode(kind, parent, line, depth)) p.nodes[parent].children.push(id) p.tip = id return id } fn closeUnmatched(mut p: Parser) { if p.allClosed { return } while p.oldTip != p.lastMatched { let old = p.oldTip let parent = p.nodes[old].parent let before = p.lineNo - 1 finalize(&p, old, before) p.oldTip = parent } p.allClosed = true } /// The last child of a block, or -1. fn lastChild(p: Parser, id: int): int { return p.nodes[id].children.at(-1) ?? -1 } fn childIndex(p: Parser, parent: int, id: int): int { return p.nodes[parent].children.lastIndexOf(id) } fn unlinkLast(mut p: Parser, id: int) { let parent = p.nodes[id].parent let at = childIndex(p, parent, id) if at >= 0 { p.nodes[parent].children.remove(at) } } /// Takes the link reference definitions off the front of a paragraph, and gives what is left. fn stripReferences(mut p: Parser, id: int): string { let text = p.nodes[id].lines.join("\n") let mut at = 0 while text.codeUnitAt(at) == 91 { let next = parseReference(text, at, &p.refs) if next == at { break } at = next } return at == 0 ? text : text.slice(at) } fn finalize(mut p: Parser, id: int, lineNo: int) { let parent = p.nodes[id].parent let kind = p.nodes[id].kind p.nodes[id].open = false p.nodes[id].endLine = lineNo if kind == "para" { let text = stripReferences(&p, id) if isBlank(text) { unlinkLast(&p, id) } else { p.nodes[id].lines = [text] } } else if kind == "code" { finishCode(&p, id) } else if kind == "html" { let mut lines = p.nodes[id].lines while lines.length > 1 && isBlank(lines[lines.length - 1]) { lines.pop() } p.nodes[id].lines = lines } else if kind == "item" { let last = lastChild(p, id) let end = itemEnd(p, id, last) p.nodes[id].endLine = end } else if kind == "list" { finishList(&p, id) } p.tip = parent } fn finishCode(mut p: Parser, id: int) { let mut lines = p.nodes[id].lines if p.nodes[id].fenced { let first = lines.length > 0 ? lines[0] : "" p.nodes[id].info = unescape(first.trim()) lines = lines.slice(1) } else { while lines.length > 0 && isBlank(lines[lines.length - 1]) { lines.pop() } p.nodes[id].endLine = p.nodes[id].startLine + lines.length - 1 } p.nodes[id].lines = lines } fn finishList(mut p: Parser, id: int) { let items = p.nodes[id].children let mut tight = true for [i, item] of items.entries() { if i + 1 < items.length { let end = p.nodes[item].endLine let next = p.nodes[items[i + 1]].startLine if end != next - 1 { tight = false break } } let subs = p.nodes[item].children for [j, sub] of subs.entries() { if j + 1 < subs.length { let end = p.nodes[sub].endLine let next = p.nodes[subs[j + 1]].startLine if end != next - 1 { tight = false } } } if !tight { break } } p.nodes[id].tight = tight if items.length > 0 { p.nodes[id].endLine = p.nodes[items[items.length - 1]].endLine } } // ---- continuing open blocks: 0 matched, 1 not matched, 2 the line is used up ---- fn closingFence(line: string, start: int, fenceChar: int, fenceLength: int): boolean { if unitAt(line, start) != fenceChar { return false } let mut i = start while unitAt(line, i) == fenceChar { i += 1 } if i - start < fenceLength { return false } while isSpaceOrTab(unitAt(line, i)) { i += 1 } return unitAt(line, i) == -1 } fn continueBlock(mut p: Parser, id: int): int { let kind = p.nodes[id].kind if kind == "quote" { if p.indented || (p.line.codeUnitAt(p.nextNonspace) ?? -1) != 62 { return 1 } advanceNextNonspace(&p) advance(&p, 1, false) if isSpaceOrTab(p.line.codeUnitAt(p.offset) ?? -1) { advance(&p, 1, true) } return 0 } if kind == "item" { if p.blank { if p.nodes[id].children.length == 0 { return 1 } advanceNextNonspace(&p) return 0 } let need = p.nodes[id].markerOffset + p.nodes[id].padding if p.indent >= need { advance(&p, need, true) return 0 } return 1 } if kind == "code" { if p.nodes[id].fenced { if fenceCloses(p, id) { let now = p.lineNo finalize(&p, id, now) return 2 } let mut i = p.nodes[id].fenceOffset while i > 0 && isSpaceOrTab(p.line.codeUnitAt(p.offset) ?? -1) { advance(&p, 1, true) i -= 1 } return 0 } if p.indent >= 4 { advance(&p, 4, true) return 0 } if p.blank { advanceNextNonspace(&p) return 0 } return 1 } if kind == "html" { let t = p.nodes[id].htmlType return p.blank && (t == 6 || t == 7) ? 1 : 0 } if kind == "para" || kind == "table" { return p.blank ? 1 : 0 } if kind == "heading" || kind == "break" { return 1 } return 0 } // ---- starting blocks: 0 no, 1 a container, 2 a leaf ---- fn atxHeading(mut p: Parser): int { if p.indented { return 0 } let start = p.nextNonspace let mut i = start while peekAt(p, i) == 35 { i += 1 } let level = i - start if level < 1 || level > 6 { return 0 } let after = peekAt(p, i) if after != -1 && !isSpaceOrTab(after) { return 0 } advanceNextNonspace(&p) closeUnmatched(&p) let id = addChild(&p, "heading") p.nodes[id].level = level let mut end = p.line.length while end > i && isSpaceOrTab(peekAt(p, end - 1)) { end -= 1 } let mut hashes = end while hashes > i && peekAt(p, hashes - 1) == 35 { hashes -= 1 } if hashes == i || isSpaceOrTab(peekAt(p, hashes - 1)) { end = hashes } p.nodes[id].lines = [p.line.slice(i, end).trim()] p.offset = p.line.length return 2 } fn fencedCode(mut p: Parser): int { let c = peekAt(p, p.nextNonspace) if p.indented || (c != 96 && c != 126) { return 0 } let mut i = p.nextNonspace while peekAt(p, i) == c { i += 1 } let length = i - p.nextNonspace if length < 3 || (c == 96 && (p.line.indexOf("`", i) >= 0)) { return 0 } closeUnmatched(&p) let id = addChild(&p, "code") p.nodes[id].fenced = true p.nodes[id].fenceChar = c p.nodes[id].fenceLength = length p.nodes[id].fenceOffset = p.indent advanceNextNonspace(&p) advance(&p, length, false) return 2 } fn startsWithTag(s: string, names: string[]): boolean { let lower = s.slice(0, 16).toLowerCase() for name of names { if lower.startsWith(name) { let c = lower.codeUnitAt(name.length) ?? -1 if c == -1 || isSpaceOrTab(c) || c == 62 || (c == 47 && lower.codeUnitAt(name.length + 1) == 62) { return true } } } return false } let RAW_TAGS = ["script", "pre", "textarea", "style"] let BLOCK_TAGS = [ "address", "article", "aside", "base", "basefont", "blockquote", "body", "caption", "center", "col", "colgroup", "dd", "details", "dialog", "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", "form", "frame", "frameset", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hr", "html", "iframe", "legend", "li", "link", "main", "menu", "menuitem", "meta", "nav", "noframes", "ol", "optgroup", "option", "p", "param", "section", "search", "summary", "table", "tbody", "td", "tfoot", "th", "thead", "title", "tr", "track", "ul", ] fn htmlBlockType(s: string): int { let next = s.codeUnitAt(1) ?? -1 if next == 33 { if s.startsWith("" : kind == 3 ? "?>" : kind == 4 ? ">" : "]]>") } fn htmlBlock(mut p: Parser, container: int): int { if p.indented || (p.line.codeUnitAt(p.nextNonspace) ?? -1) != 60 { return 0 } let t = htmlBlockType(p.line.slice(p.nextNonspace)) if t == 0 { return 0 } let lazy = isLazyLine(p) if t == 7 && (p.nodes[container].kind == "para" || lazy) { return 0 } closeUnmatched(&p) let id = addChild(&p, "html") p.nodes[id].htmlType = t return 2 } fn setextHeading(mut p: Parser, container: int): int { if p.indented || p.nodes[container].kind != "para" { return 0 } let c = peekAt(p, p.nextNonspace) if c != 61 && c != 45 { return 0 } let mut i = p.nextNonspace while peekAt(p, i) == c { i += 1 } while isSpaceOrTab(peekAt(p, i)) { i += 1 } if peekAt(p, i) != -1 { return 0 } closeUnmatched(&p) let text = stripReferences(&p, container) if isBlank(text) { p.nodes[container].lines = [] return 0 } p.nodes[container].kind = "heading" p.nodes[container].level = c == 61 ? 1 : 2 p.nodes[container].lines = [text] p.tip = container p.offset = p.line.length return 2 } fn isThematicBreak(line: string, start: int): boolean { let c = unitAt(line, start) if c != 42 && c != 95 && c != 45 { return false } let mut count = 0 let mut i = start while true { let u = unitAt(line, i) if u == c { count += 1 } else if u == -1 { break } else if !isSpaceOrTab(u) { return false } i += 1 } return count >= 3 } fn thematicBreak(mut p: Parser): int { if p.indented || !isThematicBreak(p.line, p.nextNonspace) { return 0 } closeUnmatched(&p) addChild(&p, "break") p.offset = p.line.length return 2 } fn listMarker(mut p: Parser, container: int): Marker? { if p.indent >= 4 { return null } let at = p.nextNonspace let c = peekAt(p, at) let mut length = 1 let mut ordered = false let mut bullet = 0 let mut delimiter = 0 let mut start = 1 if c == 42 || c == 43 || c == 45 { bullet = c } else if isDigit(c) { let mut i = at let mut n = 0 while isDigit(peekAt(p, i)) && i - at < 9 { n = n * 10 + (peekAt(p, i) - 48) i += 1 } let d = peekAt(p, i) if (d != 46 && d != 41) || (p.nodes[container].kind == "para" && n != 1) { return null } ordered = true delimiter = d start = n length = i - at + 1 } else { return null } let next = peekAt(p, at + length) if next != -1 && !isSpaceOrTab(next) { return null } if p.nodes[container].kind == "para" && isBlank(p.line.slice((at + length))) { return null } let markerOffset = p.indent advanceNextNonspace(&p) advance(&p, length, true) let spacesCol = p.column let spacesOffset = p.offset while true { advance(&p, 1, true) if !(p.column - spacesCol < 5 && isSpaceOrTab(p.line.codeUnitAt(p.offset) ?? -1)) { break } } let blankItem = peekAt(p, p.offset) == -1 let spaces = p.column - spacesCol let mut padding = length + spaces if spaces >= 5 || spaces < 1 || blankItem { padding = length + 1 p.column = spacesCol p.offset = spacesOffset p.partialTab = false if isSpaceOrTab(p.line.codeUnitAt(p.offset) ?? -1) { advance(&p, 1, true) } } return { ordered, bullet, delimiter, start, padding, markerOffset } } fn listItem(mut p: Parser, container: int): int { if (p.indented && p.nodes[container].kind != "list") || p.nodes[p.tip].depth >= MAX_DEPTH { return 0 } let m = listMarker(&p, container) if m == null { return 0 } closeUnmatched(&p) let open = p.tip if !continuesList(p, open, m) { let list = addChild(&p, "list") p.nodes[list].ordered = m.ordered p.nodes[list].bullet = m.bullet p.nodes[list].delimiter = m.delimiter p.nodes[list].start = m.start } let item = addChild(&p, "item") p.nodes[item].padding = m.padding p.nodes[item].markerOffset = m.markerOffset return 1 } fn blockQuote(mut p: Parser): int { if p.indented || (p.line.codeUnitAt(p.nextNonspace) ?? -1) != 62 || p.nodes[p.tip].depth >= MAX_DEPTH { return 0 } advanceNextNonspace(&p) advance(&p, 1, false) if isSpaceOrTab(p.line.codeUnitAt(p.offset) ?? -1) { advance(&p, 1, true) } closeUnmatched(&p) addChild(&p, "quote") return 1 } fn indentedCode(mut p: Parser): int { if !p.indented || p.nodes[p.tip].kind == "para" || p.blank { return 0 } advance(&p, 4, true) closeUnmatched(&p) addChild(&p, "code") return 2 } // ---- GFM tables ---- /// The cells of a table row: split at pipes that aren't escaped, each trimmed, with `\|` unescaped. fn splitRow(line: string): string[] { let text = line.trim() let mut cells: string[] = [] let mut cell: string[] = [] let mut from = text.startsWith("|") ? 1 : 0 let mut i = from while i < text.length { let c = text.codeUnitAt(i) ?? 0 if c == 92 && text.codeUnitAt(i + 1) == 124 { cell.push(text.slice(from, i)) cell.push("|") i += 2 from = i } else if c == 124 { cell.push(text.slice(from, i)) cells.push(cell.join("").trim()) cell = [] i += 1 from = i } else { i += 1 } } if from < text.length || cell.length > 0 { cell.push(text.slice(from)) cells.push(cell.join("").trim()) } return cells } /// The alignments of a delimiter row, or `null` when the line isn't one. fn delimiterRow(line: string): Align[]? { let text = line.trim() if text == "" || !(text.includes("-")) { return null } let mut out: Align[] = [] for cell of splitRow(text) { let left = cell.startsWith(":") let right = cell.endsWith(":") && cell.length > 1 let dashes = cell.slice(left ? 1 : 0, right ? cell.length - 1 : cell.length) if dashes == "" || dashes.replaceAll("-", "") != "" { return null } out.push(left && right ? "center" : left ? "left" : right ? "right" : null) } return out.length > 0 ? out : null } fn tableStart(mut p: Parser, container: int): int { if !p.gfm || p.indented || p.nodes[container].kind != "para" { return 0 } let align = delimiterRow(p.line.slice(p.nextNonspace)) if align == null { return 0 } let mut lines = p.nodes[container].lines let last = lines.at(-1) if last == null { return 0 } let head = splitRow(last) if head.length != align.length { return 0 } closeUnmatched(&p) lines.pop() p.nodes[container].lines = lines if lines.length == 0 { unlinkLast(&p, container) p.tip = p.nodes[container].parent } else { let before = p.lineNo - 1 finalize(&p, container, before) } let id = addChild(&p, "table") p.nodes[id].align = align p.nodes[id].rows = [head] p.offset = p.line.length return 2 } fn addRow(mut p: Parser, id: int) { let width = p.nodes[id].align.length let mut cells = splitRow(p.line.slice(p.offset)).slice(0, width) while cells.length < width { cells.push("") } p.nodes[id].rows.push(cells) } // ---- one line ---- fn tryStarts(mut p: Parser, container: int): int { let mut r = blockQuote(&p) if r == 0 { r = atxHeading(&p) } if r == 0 { r = fencedCode(&p) } if r == 0 { r = htmlBlock(&p, container) } if r == 0 { r = setextHeading(&p, container) } if r == 0 { r = thematicBreak(&p) } if r == 0 { r = listItem(&p, container) } if r == 0 { r = indentedCode(&p) } if r == 0 { r = tableStart(&p, container) } return r } fn maybeSpecial(c: int): boolean { return c == 35 || c == 96 || c == 126 || c == 42 || c == 43 || c == 95 || c == 61 || c == 60 || c == 62 || c == 45 || c == 124 || c == 58 || isDigit(c) } fn incorporate(mut p: Parser, line: string) { p.oldTip = p.tip p.offset = 0 p.column = 0 p.blank = false p.partialTab = false p.lineNo += 1 p.line = line let mut container = 0 while true { let last = lastChild(p, container) if last == -1 || !p.nodes[last].open { break } container = last findNextNonspace(&p) let r = continueBlock(&p, container) if r == 2 { return } if r == 1 { container = p.nodes[container].parent break } } p.allClosed = container == p.oldTip p.lastMatched = container let first = p.nodes[container].kind let mut leaf = first != "para" && acceptsLines(first) while !leaf { findNextNonspace(&p) if !p.indented && !maybeSpecial(p.line.codeUnitAt(p.nextNonspace) ?? -1) { advanceNextNonspace(&p) break } let r = tryStarts(&p, container) if r == 0 { advanceNextNonspace(&p) break } container = p.tip leaf = r == 2 } if !p.allClosed && !p.blank && p.nodes[p.tip].kind == "para" { addLine(&p) return } closeUnmatched(&p) let kind = p.nodes[container].kind if acceptsLines(kind) { addLine(&p) let t = p.nodes[container].htmlType if kind == "html" && t >= 1 && t <= 5 && htmlCloses(t, p.line.slice(p.offset)) { let now = p.lineNo finalize(&p, container, now) } } else if (p.offset < p.line.length) && !p.blank { if kind == "table" { addRow(&p, container) } else { addChild(&p, "para") advanceNextNonspace(&p) addLine(&p) } } } // ---- from the block list to the tree ---- fn inlineCells(cells: string[], p: Parser, mut budget: int[]): TableCell[] { let mut out: TableCell[] = [] for cell of cells { out.push({ children: parseInlines(cell, p.refs, p.gfm, &budget) }) } return out } /// A task item's marker at the start of `text`: whether it is checked and the text after it. fn taskMarker(text: string): [boolean, string]? { if text.length < 5 || text.codeUnitAt(0) != 91 || text.codeUnitAt(2) != 93 || !isSpaceOrTab(text.codeUnitAt(3) ?? -1) { return null } let c = text.codeUnitAt(1) ?? 0 if c != 32 && c != 120 && c != 88 { return null } let rest = text.slice(4).trimStart() return rest == "" ? null : [c != 32, rest] } fn toItem(p: Parser, id: int, mut budget: int[]): ListItem { let kids = p.nodes[id].children if p.gfm && kids.length > 0 && p.nodes[kids[0]].kind == "para" { let marker = taskMarker(p.nodes[kids[0]].lines[0]) if marker != null { let first: Block = Paragraph { children: parseInlines(marker[1], p.refs, p.gfm, &budget) } return { children: [first, ...toBlocks(p, kids.slice(1), &budget)], checked: marker[0] } } } return { children: toBlocks(p, kids, &budget), checked: null } } fn toBlocks(p: Parser, ids: int[], mut budget: int[]): Block[] { let mut out: Block[] = [] for id of ids { let n = p.nodes[id] if n.kind == "para" { out.push(Paragraph { children: parseInlines(n.lines[0], p.refs, p.gfm, &budget) }) } else if n.kind == "heading" { out.push( Heading { level: n.level, children: parseInlines(n.lines[0], p.refs, p.gfm, &budget) }, ) } else if n.kind == "break" { out.push(ThematicBreak) } else if n.kind == "code" { out.push( CodeBlock { info: n.info, code: n.lines.length > 0 ? n.lines.join("\n") + "\n" : "" }, ) } else if n.kind == "html" { out.push(HtmlBlock { html: n.lines.join("\n") }) } else if n.kind == "quote" { out.push(Blockquote { children: toBlocks(p, n.children, &budget) }) } else if n.kind == "list" { let mut items: ListItem[] = [] for item of n.children { items.push(toItem(p, item, &budget)) } if n.ordered { out.push(OrderedList { tight: n.tight, start: n.start, items }) } else { out.push(BulletList { tight: n.tight, items }) } } else if n.kind == "table" { let head = inlineCells(n.rows[0], p, &budget) let mut rows: TableCell[][] = [] for row of n.rows.slice(1) { rows.push(inlineCells(row, p, &budget)) } out.push(Table { align: n.align, head, rows }) } } return out } /// Parses a whole document. export fn parseDocument(source: string, gfm: boolean): Document { let text = source.replaceAll("\r\n", "\n").replaceAll("\r", "\n").replaceAll("\u{0}", "\u{fffd}") let mut lines = text.split("\n") if text.endsWith("\n") { lines.pop() } let mut p: Parser = { nodes: [newNode("document", -1, 0, 0)], refs: Map(), gfm, line: "", lineNo: 0, offset: 0, column: 0, nextNonspace: 0, nextNonspaceColumn: 0, indent: 0, indented: false, blank: false, partialTab: false, tip: 0, oldTip: 0, lastMatched: 0, allClosed: true, } for line of lines { incorporate(&p, line) } while p.tip != -1 { let tip = p.tip finalize(&p, tip, lines.length) } let mut budget = [text.length > 100000 ? text.length : 100000] return { children: toBlocks(p, p.nodes[0].children, &budget) } }