json/json/internal/TreeParserTokens.protos

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

//! Private stateless complete-token scanners. Positions are UTF-8 octet indices.

create: (fail, hexDigit, appendScalarUtf8, scanNumber) => {
    // Writes the decoded String into `result.value` and answers the position after
    // the closing quote. `result` belongs exclusively to the calling parse.
    scanString: (source, start, limit, result) => {
        if(start >= limit || source[start] != 34) { fail() }
        position: start + 1
        if(position < limit && source[position] == 34) {
            // The empty String needs neither storage nor decoding.
            result.value = ""
            position = position + 1
        } else {
            buffer: Encoding.UTF8.encode("")
            closed: false
            while(position < limit && closed == false) {
                octet: source[position]
                // Ordinary UTF-8 is read once, copied unchanged and consumed once.
                while(octet >= 32 && octet != 34 && octet != 92) {
                    buffer.add(octet)
                    position = position + 1
                    octet = -1
                    if(position < limit) { octet = source[position] }
                }
                if(octet == 34) {
                    position = position + 1
                    closed = true
                } else {
                    // End of input or an unescaped control character.
                    if(octet != 92) { fail() }
                    position = position + 1
                    if(position >= limit) { fail() }
                    escape: source[position]
                    position = position + 1
                    if(escape == 117) {
                        if(position + 4 > limit) { fail() }
                        scalar: 0
                        end: position + 4
                        while(position < end) {
                            digit: hexDigit(source[position])
                            if(digit < 0) { fail() }
                            scalar = scalar * 16 + digit
                            position = position + 1
                        }
                        if(scalar >= 55296 && scalar <= 56319) {
                            // A high surrogate requires an immediately adjacent escaped low one.
                            if(position + 6 > limit) { fail() }
                            if(source[position] != 92 || source[position + 1] != 117) { fail() }
                            position = position + 2
                            low: 0
                            end = position + 4
                            while(position < end) {
                                digit: hexDigit(source[position])
                                if(digit < 0) { fail() }
                                low = low * 16 + digit
                                position = position + 1
                            }
                            if(low < 56320 || low > 57343) { fail() }
                            scalar = 65536 + (scalar - 55296) * 1024 + (low - 56320)
                        } else {
                            if(scalar >= 56320 && scalar <= 57343) { fail() }
                        }
                        appendScalarUtf8(buffer, scalar)
                    } else {
                        decoded: -1
                        if(escape == 34 || escape == 92 || escape == 47) { decoded = escape }
                        if(escape == 98) { decoded = 8 }
                        if(escape == 102) { decoded = 12 }
                        if(escape == 110) { decoded = 10 }
                        if(escape == 114) { decoded = 13 }
                        if(escape == 116) { decoded = 9 }
                        if(decoded < 0) { fail() }
                        buffer.add(decoded)
                    }
                }
            }
            if(closed == false) { fail() }
            result.value = Encoding.UTF8.decode(buffer)
        }
        position
    }

    // Answers the canonical literal value; the caller advances by its spelling length.
    scanLiteral: (source, start, limit) => {
        if(start >= limit) { fail() }
        first: source[start]
        value: null
        if(first == 116) {
            if(start + 4 > limit) { fail() }
            if(source[start + 1] != 114 || source[start + 2] != 117 || source[start + 3] != 101) { fail() }
            value = true
        } else {
            if(first == 102) {
                if(start + 5 > limit) { fail() }
                if(source[start + 1] != 97 || source[start + 2] != 108 || source[start + 3] != 115 || source[start + 4] != 101) { fail() }
                value = false
            } else {
                if(first != 110 || start + 4 > limit) { fail() }
                if(source[start + 1] != 117 || source[start + 2] != 108 || source[start + 3] != 108) { fail() }
            }
        }
        // The structural parser checks the next octet in the enclosing grammar.
        value
    }

    {
        scanString: scanString
        scanLiteral: scanLiteral
        scanNumber: scanNumber
    }
}