markdown/internal/Parser.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
e6dec09f36c14212ee4437b66e4d55b934f17f98
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Iterative composition and public-tree materialization.
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)
    // Private entity-name state of this invocation; never part of the tree.
    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 }
    // Work records keep the child frames materialized at the first visit; they
    // never reach the public tree. Frames without block children are
    // finalized at their only visit.
    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)
}

// Frames whose results are built from block children.
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) }
}

// A fresh exact-length Array of the non-null child results, in order.
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
}