Utf8: import("./Utf8.protos")
Builder: import("./ArrayBuilder.protos")
Errors: import("./Errors.protos")
simpleEscape: (scalar) => {
result: -1
if((scalar == 92) || (scalar == 39) || (scalar == 34)) {
result = scalar
}
if(scalar == 110) {
result = 10
}
if(scalar == 114) {
result = 13
}
if(scalar == 116) {
result = 9
}
if(scalar == 98) {
result = 8
}
if(scalar == 102) {
result = 12
}
result
}
hexValue: (scalar) => {
result: -1
if((scalar >= 48) && (scalar <= 57)) {
result = scalar - 48
}
if((scalar >= 65) && (scalar <= 70)) {
result = scalar - 55
}
if((scalar >= 97) && (scalar <= 102)) {
result = scalar - 87
}
result
}
isScalarValue: (value) => {
(value <= 1114111) && ((value < 55296) || (value > 57343))
}
readEscape: (cursor) => {
next: cursor.peekAt(1)
result: simpleEscape(next)
if(result >= 0) {
cursor.advanceAscii(2)
} else {
if(next == 117) {
valid: cursor.peekAt(2) == 123
value: 0
digits: 0
distance: 3
if(valid) {
digit: hexValue(cursor.peekAt(distance))
while((digit >= 0) && (digits < 7)) {
value = (value * 16) + digit
digits = digits + 1
distance = distance + 1
digit = hexValue(cursor.peekAt(distance))
}
valid = (digits >= 1) && (digits <= 6) && (cursor.peekAt(distance) == 125) &&
isScalarValue(value)
}
if(valid) {
result = value
cursor.advanceAscii(distance + 1)
} else {
cursor.advanceAscii(2)
}
} else {
cursor.advanceAscii(1)
if((next >= 0) && (next != 10) && (next != 13)) {
cursor.advance()
}
}
}
result
}
escapeSpan: (first, last) => {
{
start: first
end: last
}
}
outcome: (diagnosticCode, decoded, spans) => {
{
code: diagnosticCode
value: decoded
escapes: spans
}
}
scanQuoted: (cursor, quote) => {
bytes: cursor.bytes()
cursor.advanceAscii(1)
buffer: Utf8.emptyBytes()
spans: Builder.create()
diagnosticCode: null
runStart: cursor.byteOffset()
done: false
while(!done) {
current: cursor.peek()
if((current < 0) || (current == 10) || (current == 13)) {
if(diagnosticCode === null) {
if(current < 0) {
diagnosticCode = Errors.UNTERMINATED_STRING
} else {
diagnosticCode = Errors.STRING_NEWLINE
}
}
done = true
} else {
if(current == quote) {
if(diagnosticCode === null) {
Utf8.appendInterval(buffer, bytes, runStart, cursor.byteOffset())
}
cursor.advanceAscii(1)
done = true
} else {
if(current == 92) {
if(diagnosticCode === null) {
Utf8.appendInterval(buffer, bytes, runStart, cursor.byteOffset())
}
escapeStart: cursor.scalarOffset()
decoded: readEscape(cursor)
if(decoded >= 0) {
if(diagnosticCode === null) {
Utf8.appendScalar(buffer, decoded)
spans.add(escapeSpan(escapeStart, cursor.scalarOffset()))
}
} else {
if(diagnosticCode === null) {
diagnosticCode = Errors.INVALID_STRING_ESCAPE
}
}
runStart = cursor.byteOffset()
} else {
cursor.advance()
}
}
}
}
result: null
if(diagnosticCode === null) {
result = outcome(null, Utf8.decodeBuffer(buffer), spans.toArray())
} else {
result = outcome(diagnosticCode, null, Array())
}
result
}
isIndentation: (octet) => {
(octet == 32) || (octet == 9)
}
allIndentation: (bytes, first, last) => {
result: true
index: first
while(result && (index < last)) {
result = isIndentation(bytes[index])
index = index + 1
}
result
}
normalizeIndentation: (bytes, first, last) => {
lastLineStart: -1
scan: last - 1
while((lastLineStart < 0) && (scan >= first)) {
octet: bytes[scan]
if((octet == 10) || (octet == 13)) {
lastLineStart = scan + 1
}
scan = scan - 1
}
structural: false
prefixStart: 0
prefixLength: 0
contentEnd: last
if((lastLineStart >= 0) && allIndentation(bytes, lastLineStart, last)) {
structural = true
prefixStart = lastLineStart
prefixLength = last - lastLineStart
newlineStart: lastLineStart - 1
if((bytes[newlineStart] == 10) && (newlineStart > first) && (bytes[newlineStart - 1] == 13)) {
newlineStart = newlineStart - 1
}
contentEnd = newlineStart
}
contentStart: first
if(contentStart < contentEnd) {
if(bytes[contentStart] == 10) {
contentStart = contentStart + 1
} else {
if(bytes[contentStart] == 13) {
contentStart = contentStart + 1
if((contentStart < contentEnd) && (bytes[contentStart] == 10)) {
contentStart = contentStart + 1
}
}
}
}
output: Utf8.emptyBytes()
if(!structural) {
Utf8.appendInterval(output, bytes, contentStart, contentEnd)
} else {
lineStart: contentStart
going: true
while(going) {
lineEnd: lineStart
while((lineEnd < contentEnd) && (bytes[lineEnd] != 10) && (bytes[lineEnd] != 13)) {
lineEnd = lineEnd + 1
}
if(!allIndentation(bytes, lineStart, lineEnd)) {
matches: (lineEnd - lineStart) >= prefixLength
offset: 0
while(matches && (offset < prefixLength)) {
matches = bytes[lineStart + offset] == bytes[prefixStart + offset]
offset = offset + 1
}
if(matches) {
Utf8.appendInterval(output, bytes, lineStart + prefixLength, lineEnd)
} else {
output = null
going = false
}
}
if(going) {
if(lineEnd >= contentEnd) {
going = false
} else {
newlineLength: 1
if((bytes[lineEnd] == 13) && ((lineEnd + 1) < contentEnd) && (bytes[lineEnd + 1] == 10)) {
newlineLength = 2
}
Utf8.appendInterval(output, bytes, lineEnd, lineEnd + newlineLength)
lineStart = lineEnd + newlineLength
}
}
}
}
output
}
decodeEscapes: (raw) => {
output: Utf8.emptyBytes()
count: raw.size()
index: 0
while(index < count) {
octet: raw[index]
if(octet == 92) {
next: raw[index + 1]
simple: simpleEscape(next)
if(simple >= 0) {
output.add(simple)
index = index + 2
} else {
value: 0
index = index + 3
while(raw[index] != 125) {
value = (value * 16) + hexValue(raw[index])
index = index + 1
}
Utf8.appendScalar(output, value)
index = index + 1
}
} else {
output.add(octet)
index = index + 1
}
}
Utf8.decodeBuffer(output)
}
scanTriple: (cursor) => {
bytes: cursor.bytes()
cursor.advanceAscii(3)
contentStart: cursor.byteOffset()
contentEnd: -1
spans: Builder.create()
diagnosticCode: null
done: false
while(!done) {
current: cursor.peek()
if(current < 0) {
if(diagnosticCode === null) {
diagnosticCode = Errors.UNTERMINATED_STRING
}
done = true
} else {
if((current == 34) && (cursor.peekAt(1) == 34) && (cursor.peekAt(2) == 34)) {
contentEnd = cursor.byteOffset()
cursor.advanceAscii(3)
done = true
} else {
if(current == 92) {
escapeStart: cursor.scalarOffset()
decoded: readEscape(cursor)
if(decoded >= 0) {
if(diagnosticCode === null) {
spans.add(escapeSpan(escapeStart, cursor.scalarOffset()))
}
} else {
if(diagnosticCode === null) {
diagnosticCode = Errors.INVALID_STRING_ESCAPE
}
}
} else {
cursor.advance()
}
}
}
}
result: null
if(diagnosticCode === null) {
normalized: normalizeIndentation(bytes, contentStart, contentEnd)
if(normalized === null) {
result = outcome(Errors.INVALID_TRIPLE_INDENTATION, null, Array())
} else {
result = outcome(null, decodeEscapes(normalized), spans.toArray())
}
} else {
result = outcome(diagnosticCode, null, Array())
}
result
}