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
}
state: () => { { bare: null; angles: null; titleFailures: null } }
indexDestinations: (s, state) => {
end: Source.size(s)
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)
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 }
}