Characters: import("./Characters.protos")
Components: import("./Components.protos")
Authority: import("./Authority.protos")
parse: (text) => {
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")
asciiIndex: 0
while(asciiIndex < size) {
if(source[asciiIndex] > 127) {
fail()
}
asciiIndex = asciiIndex + 1
}
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()
}