uri/internal/Parser.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
0629e6a50215884851823a838234d613aeeda59e
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 URI parser implementation.

Characters: import("./Characters.protos")
Components: import("./Components.protos")
Authority: import("./Authority.protos")

parse: (text) => {
    // Validate the exact semantic String family without conversion.
    String.recognizes(text).ifFalse(() => {
        Error().signal()
    })
    source: Encoding.UTF8.encode(text)
    size: source.size()

    schemeValue: null
    userInfoValue: null
    hostValue: null
    portValue: null
    pathValue: ""
    queryValue: null
    fragmentValue: null

    isAlpha: Characters.slotValue("isAlpha")
    isDigit: Characters.slotValue("isDigit")
    components: Components.create(source)
    validatePercentTriplet: components.slotValue("validatePercentTriplet")
    sliceText: components.slotValue("sliceText")
    validateComponent: components.slotValue("validateComponent")
    authority: Authority.create(
        source, validatePercentTriplet, sliceText,
        (value) => { userInfoValue = value },
        (value) => { hostValue = value },
        (value) => { portValue = value }
    )
    validateAuthority: authority.slotValue("validateAuthority")

    // The initial profile is RFC 3986 URI syntax, not IRI or WHATWG URL input.
    asciiIndex: 0
    while(asciiIndex < size) {
        if(source[asciiIndex] > 127) {
            fail()
        }
        asciiIndex = asciiIndex + 1
    }

    // RFC 3986 distinguishes a scheme from a relative first path segment by the
    // first ':', '/', '?', or '#'. A colon before the other delimiters commits
    // the input to scheme syntax; an invalid scheme prefix therefore fails.
    scanIndex: 0
    firstColon: -1
    scanningSchemeCandidate: true

    while((scanIndex < size) && scanningSchemeCandidate) {
        octet: source[scanIndex]

        if(octet == 58) {
            firstColon = scanIndex
            scanningSchemeCandidate = false
        }

        if((octet == 47) || (octet == 63) || (octet == 35)) {
            scanningSchemeCandidate = false
        }

        if(scanningSchemeCandidate) {
            scanIndex = scanIndex + 1
        }
    }

    hasScheme: firstColon >= 0
    index: 0

    if(hasScheme) {
        if(firstColon == 0) {
            fail()
        }
        isAlpha(source[0]).ifFalse(() => {
            fail()
        })

        schemeIndex: 1
        while(schemeIndex < firstColon) {
            octet: source[schemeIndex]
            allowed: isAlpha(octet) ||
                isDigit(octet) ||
                (octet == 43) ||
                (octet == 45) ||
                (octet == 46)
            allowed.ifFalse(() => {
                fail()
            })
            schemeIndex = schemeIndex + 1
        }

        schemeValue = sliceText(0, firstColon)
        index = firstColon + 1
    }

    hasAuthority: ((index + 1) < size) &&
        (source[index] == 47) &&
        (source[index + 1] == 47)

    if(hasAuthority) {
        authorityStart: index + 2
        authorityEnd: authorityStart

        while(
            (authorityEnd < size) &&
                (source[authorityEnd] != 47) &&
                (source[authorityEnd] != 63) &&
                (source[authorityEnd] != 35)
        ) {
            authorityEnd = authorityEnd + 1
        }

        validateAuthority(authorityStart, authorityEnd)
        index = authorityEnd
    }

    pathStart: index
    while((index < size) && (source[index] != 63) && (source[index] != 35)) {
        index = index + 1
    }
    pathEnd: index

    validateComponent(pathStart, pathEnd, true, false)

    if(hasAuthority && (pathStart < pathEnd) && (source[pathStart] != 47)) {
        fail()
    }

    if((!hasScheme) && (!hasAuthority) && (pathStart < pathEnd) && (source[pathStart] != 47)) {
        firstSegmentIndex: pathStart
        while((firstSegmentIndex < pathEnd) && (source[firstSegmentIndex] != 47)) {
            if(source[firstSegmentIndex] == 58) {
                fail()
            }
            firstSegmentIndex = firstSegmentIndex + 1
        }
    }

    pathValue = sliceText(pathStart, pathEnd)

    if((index < size) && (source[index] == 63)) {
        queryStart: index + 1
        index = queryStart

        while((index < size) && (source[index] != 35)) {
            index = index + 1
        }

        validateComponent(queryStart, index, true, true)
        queryValue = sliceText(queryStart, index)
    }

    if((index < size) && (source[index] == 35)) {
        fragmentStart: index + 1
        validateComponent(fragmentStart, size, true, true)
        fragmentValue = sliceText(fragmentStart, size)
        index = size
    }

    (index == size).ifFalse(() => {
        fail()
    })

    reference: {
        scheme: schemeValue
        userInfo: userInfoValue
        host: hostValue
        port: portValue
        path: pathValue
        query: queryValue
        fragment: fragmentValue
    }

    reference.freeze()
}