Source: import("./Source.protos")
Builders: import("./Builders.protos")
Blocks: import("./Blocks.protos")
Inlines: import("./Inlines.protos")
Nodes: import("./Nodes.protos")
Limits: import("./Limits.protos")
Tasks: import("./TaskLists.protos")
Entities: import("./Entities.protos")
parse: (source, profile, positions, prototype) => {
extension: profile == "commonmark-0.31.2-gfm-extensions"
prepared: Source.scan(source)
budget: Limits.create(prototype)
names: Entities.state()
blocks: Blocks.parse(Source.lines(prepared), extension, budget, names)
root: blocks.root
root.end = Source.size(prepared)
parseState: { definitions: blocks.definitions; extension: extension; positions: positions; budget: budget; names: names }
stack: { frame: root; visited: false; children: null; next: null }
while(stack !== null) {
entry: stack
stack = entry.next
frame: entry.frame
if(entry.visited === false) {
children: frame.finished
if(children === null) { children = Builders.finish(frame.children) }
if(extension && (frame.kind == "listItem")) {
index: 0
while((index < children.size()) && (children[index].kind == "paragraph") && (Source.size(children[index].value) == 0)) { index = index + 1 }
if((index < children.size()) && (children[index].kind == "paragraph")) {
task: Tasks.recognize(children[index].value)
if(task !== null) { frame.task = task.task; children[index].value = task.subject }
}
}
if(branch(frame.kind)) {
stack = { frame: frame; visited: true; children: children; next: stack }
i: children.size() - 1
while(i >= 0) { stack = { frame: children[i]; visited: false; children: null; next: stack }; i = i - 1 }
}
if(branch(frame.kind) === false) { finalize(frame, children, parseState) }
}
if(entry.visited) { finalize(frame, entry.children, parseState) }
}
Limits.verify(root.result, prototype)
}
branch: (kind) => {
(kind == "document") || (kind == "blockquote") || (kind == "tableRow") || (kind == "list") || (kind == "listItem") || (kind == "table")
}
finalize: (frame, children, parseState) => {
positions: parseState.positions
extent: null
if(positions && (frame.start !== null)) { extent = { start: frame.start; end: frame.end } }
kind: frame.kind
finalized: null
if(branch(kind)) { finalized = results(children) }
if((kind == "paragraph") || (kind == "heading") || (kind == "tableCell")) {
finalized = Inlines.parse(frame.value, parseState.definitions, parseState.extension, positions, parseState.budget, frame.depth, parseState.names)
if(kind == "heading") { frame.result = Nodes.heading(frame.level, finalized, positions, extent) }
(kind == "heading").ifFalse(() => {
if((kind != "paragraph") || (Source.size(frame.value) > 0)) { frame.result = Nodes.container(kind, finalized, positions, extent) }
})
}
if((kind == "document") || (kind == "blockquote") || (kind == "tableRow")) { frame.result = Nodes.container(kind, finalized, positions, extent) }
if(kind == "list") { frame.result = Nodes.list(frame.ordered, frame.number, frame.tight, finalized, positions, extent) }
if(kind == "listItem") { frame.result = Nodes.item(frame.task, finalized, positions, extent) }
if(kind == "codeBlock") { frame.result = Nodes.code(frame.value, frame.info, positions, extent) }
if(kind == "htmlBlock") { frame.result = Nodes.value(kind, frame.value, positions, extent) }
if(kind == "thematicBreak") { frame.result = Nodes.leaf(kind, positions, extent) }
if(kind == "table") { frame.result = Nodes.table(frame.alignments, finalized, positions, extent) }
}
results: (children) => {
total: children.size()
if(total == 0) { ^[] }
if(total == 1) {
only: children[0].result
if(only === null) { ^[] }
^[only]
}
count: 0
for(child in children) { if(child.result !== null) { count = count + 1 } }
output: Builders.sized(count)
index: 0
for(child in children) {
if(child.result !== null) { output[index] = child.result; index = index + 1 }
}
output
}