markdown/internal/LinkSyntax.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
9b044833b4a661854b98768508a948ac9eaea331
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.

//! Transactional link syntax scanners; parenthesis depth is an Integer counter.
Source: import("./Source.protos")
Characters: import("./Characters.protos")
Entities: import("./Entities.protos")
Builders: import("./Builders.protos")

space: (s, start) => {
    i: start
    while(Characters.labelSpace(Source.at(s, i))) { i = i + 1 }
    i
}

label: (s, start) => {
    if(Source.at(s, start) != 91) { ^null }
    i: start + 1
    nonspace: false
    count: 0
    length: Source.size(s)
    while(i < length) {
        c: Source.at(s, i)
        if(c == 93) {
            if((count == 0) || (nonspace === false)) { ^null }
            ^{ subject: Source.slice(s, start + 1, i); end: i + 1 }
        }
        if(c == 91) { ^null }
        if(c == 10) {
            j: i + 1
            while((Source.at(s, j) == 32) || (Source.at(s, j) == 9)) { j = j + 1 }
            if(Source.at(s, j) == 10) { ^null }
        }
        if((c == 92) && Characters.escapable(Source.at(s, i + 1))) { i = i + 1; count = count + 1 }
        Characters.labelSpace(c).ifFalse(() => { nonspace = true })
        count = count + 1
        if(count > 999) { ^null }
        i = i + 1
    }
    null
}

// Destination indexes are prepared on first use; title failures are recorded
// only after a title search fails.
state: () => { { bare: null; angles: null; titleFailures: null } }

// Integer endpoints are shared by speculative candidates. Parentheses are
// matched without recursion; no candidate decodes a destination before commit.
indexDestinations: (s, state) => {
    end: Source.size(s)
    // pairs[i] is the matching closer of an opening parenthesis, or -1.
    pairs: Builders.sized(end + 2, -1)
    stack: null
    depth: 0
    i: 0
    while(i < end) {
        c: Source.at(s, i)
        escaped: (c == 92) && Characters.escapable(Source.at(s, i + 1))
        if(escaped) { i = i + 2 }
        escaped.ifFalse(() => {
            if((c <= 32) || (c == 127)) { depth = 0 }
            if(c == 40) {
                if(stack === null) { stack = Builders.sized(end, 0) }
                stack[depth] = i
                depth = depth + 1
            }
            if((c == 41) && (depth > 0)) { depth = depth - 1; pairs[stack[depth]] = i }
            i = i + 1
        })
    }
    bare: Builders.sized(end + 2, end)
    angle: Builders.sized(end + 2, end)
    i = end - 1
    while(i >= 0) {
        c: Source.at(s, i)
        escaped: (c == 92) && Characters.escapable(Source.at(s, i + 1))
        bare[i] = bare[i + 1]
        angle[i] = angle[i + 1]
        if((c <= 32) || (c == 127) || (c == 41)) { bare[i] = i }
        if(c == 40) {
            bare[i] = -1
            if(pairs[i] >= 0) { bare[i] = bare[pairs[i] + 1] }
        }
        if((c == 10) || (c == 13) || (c == 60) || (c == 62)) { angle[i] = i }
        if(escaped) { bare[i] = bare[i + 2]; angle[i] = angle[i + 2] }
        i = i - 1
    }
    state.bare = bare
    state.angles = angle
}

destination: (s, start, scan) => {
    length: Source.size(s)
    if((start < 0) || (start > length)) { ^null }
    first: Source.at(s, start)
    // An empty bare destination ends at its own terminator without an index.
    if((first <= 32) || (first == 127) || (first == 41)) { ^{ subject: Source.slice(s, start, start); end: start } }
    if(scan.bare === null) { indexDestinations(s, scan) }
    if(first == 60) {
        end: scan.angles[start + 1]
        if(Source.at(s, end) != 62) { ^null }
        ^{ subject: Source.slice(s, start + 1, end); end: end + 1 }
    }
    end: scan.bare[start]
    if(end < 0) { ^null }
    { subject: Source.slice(s, start, end); end: end }
}

title: (s, start, scan) => {
    opener: Source.at(s, start)
    if((opener != 34) && (opener != 39) && (opener != 40)) { ^null }
    closer: opener
    if(opener == 40) { closer = 41 }
    i: start + 1
    failures: scan.titleFailures
    if((failures !== null) && failures.containsKey(opener)) {
        failed: failures[opener]
        if((i >= failed.begin) && (i < failed.end)) { ^null }
    }
    fail: (end) => {
        if(scan.titleFailures === null) { scan.titleFailures = Map() }
        scan.titleFailures[opener] = { begin: start + 1; end: end }
        null
    }
    length: Source.size(s)
    while(i < length) {
        c: Source.at(s, i)
        if(c == closer) { ^{ subject: Source.slice(s, start + 1, i); end: i + 1 } }
        if((opener == 40) && (c == 40)) { ^fail(i) }
        if(c == 10) {
            j: i + 1
            while((Source.at(s, j) == 32) || (Source.at(s, j) == 9)) { j = j + 1 }
            if(Source.at(s, j) == 10) { ^fail(i) }
        }
        if((c == 92) && Characters.escapable(Source.at(s, i + 1))) { i = i + 1 }
        i = i + 1
    }
    fail(Source.size(s))
}

inline: (s, start, scan) => {
    if(Source.at(s, start) != 40) { ^null }
    begin: space(s, start + 1)
    dest: destination(s, begin, scan)
    if(dest === null) { ^null }
    i: space(s, dest.end)
    caption: null
    if((i > dest.end) && (Source.at(s, i) != 41)) {
        parsed: title(s, i, scan)
        if(parsed === null) { ^null }
        caption = parsed.subject
        i = space(s, parsed.end)
    }
    if(Source.at(s, i) != 41) { ^null }
    { destinationRange: dest.subject; titleRange: caption; end: i + 1 }
}

commit: (candidate, names: null) => {
    destination: Entities.decode(candidate.destinationRange, names)
    title: null
    if(candidate.titleRange !== null) { title = Entities.decode(candidate.titleRange, names) }
    cost: destination.size()
    if(title !== null) { cost = cost + title.size() }
    { destination: destination; title: title; textSize: cost; end: candidate.end }
}