toml/toml/internal/DocumentParser.protos
- Repository
- guillermomolina/protos
- Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635- Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492- Blob
10e54d528ad5c7ed2d220ba418407504571fd41c- 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 TOML document assembly from recognized statements.
Builders: import("./Builders.protos")
create: (documentStatements, syntaxKeyPath, syntaxValue) => {
documentTable: (text) => {
// Every statement of the source is recognized before any is assembled.
items: documentStatements(text)
node: (kind, value) => {
result: {
kind: kind
value: value
}
result
}
root: node("table", Map())
states: IdentityMap()
states[root] = "root"
current: root
// Each array-table node owns a private accumulator; the nodes are
// registered in creation order and materialized once at the end.
arrayTableBuilders: IdentityMap()
arrayTableNodes: Builders.array()
appendArrayTableElement: (arrayNode, element) => {
builder: null
if(arrayTableBuilders.containsKey(arrayNode)) {
builder = arrayTableBuilders[arrayNode]
} else {
builder = Builders.array()
arrayTableBuilders[arrayNode] = builder
Builders.push(arrayTableNodes, arrayNode)
}
Builders.push(builder, element)
}
isSpace: (octet) => {
(octet == 32) || (octet == 9)
}
// Absolute bounds of [first, last) without surrounding spaces and tabs.
trimBounds: (source, first, last) => {
left: first
right: last
while((left < right) && isSpace(source[left])) {
left = left + 1
}
while((right > left) && isSpace(source[right - 1])) {
right = right - 1
}
{
left: left
right: right
}
}
markInlineTable: (valueNode) => {
if(valueNode.kind == "table") {
states[valueNode] = "inline"
}
}
putDotted: (tableNode, parts, ignoredPosition, valueNode) => {
selected: tableNode
position: 0
count: parts.size()
while(position < count) {
name: parts[position]
last: position == count - 1
entries: selected.value
present: entries.containsKey(name)
if(last) {
if(present) {
fail()
}
entries[name] = valueNode
markInlineTable(valueNode)
position = count
} else {
child: null
if(present) {
child = entries[name]
(child.kind == "table").ifFalse(() => {
fail()
})
states.containsKey(child).ifFalse(() => {
fail()
})
state: states[child]
if(state == "header") {
fail()
}
if(state == "inline") {
fail()
}
if(state == "implicit") {
states[child] = "dotted"
}
} else {
child = node("table", Map())
entries[name] = child
states[child] = "dotted"
}
selected = child
position = position + 1
}
}
}
latestArrayTable: (arrayNode) => {
(arrayNode.kind == "array").ifFalse(() => {
fail()
})
states.containsKey(arrayNode).ifFalse(() => {
fail()
})
(states[arrayNode] == "array-table").ifFalse(() => {
fail()
})
arrayTableBuilders.containsKey(arrayNode).ifFalse(() => {
fail()
})
builder: arrayTableBuilders[arrayNode]
if(builder.size == 0) {
fail()
}
selected: builder.last
(selected.kind == "table").ifFalse(() => {
fail()
})
selected
}
existingParent: (child) => {
selected: null
if(child.kind == "table") {
states.containsKey(child).ifFalse(() => {
fail()
})
if(states[child] == "inline") {
fail()
}
selected = child
}
if(child.kind == "array") {
selected = latestArrayTable(child)
}
if(selected === null) {
fail()
}
selected
}
selectHeader: (parts) => {
selected: root
position: 0
count: parts.size()
while(position < count) {
name: parts[position]
last: position == count - 1
entries: selected.value
child: null
if(entries.containsKey(name)) {
child = entries[name]
if(last) {
(child.kind == "table").ifFalse(() => {
fail()
})
states.containsKey(child).ifFalse(() => {
fail()
})
state: states[child]
if(state == "implicit") {
states[child] = "header"
}
(state == "implicit").ifFalse(() => {
fail()
})
selected = child
} else {
selected = existingParent(child)
}
} else {
child = node("table", Map())
entries[name] = child
if(last) {
states[child] = "header"
} else {
states[child] = "implicit"
}
selected = child
}
position = position + 1
}
selected
}
selectArrayHeader: (parts) => {
selected: root
position: 0
count: parts.size()
created: null
while(position < count) {
name: parts[position]
last: position == count - 1
entries: selected.value
present: entries.containsKey(name)
child: null
if(last) {
element: node("table", Map())
states[element] = "array-element"
if(present) {
child = entries[name]
(child.kind == "array").ifFalse(() => {
fail()
})
states.containsKey(child).ifFalse(() => {
fail()
})
(states[child] == "array-table").ifFalse(() => {
fail()
})
} else {
child = node("array", Array())
entries[name] = child
states[child] = "array-table"
}
appendArrayTableElement(child, element)
created = element
} else {
if(present) {
child = entries[name]
selected = existingParent(child)
} else {
child = node("table", Map())
entries[name] = child
states[child] = "implicit"
selected = child
}
}
position = position + 1
}
created
}
// [left, right) is a trimmed statement interval opening with "[".
parseHeader: (source, left, right) => {
(source[right - 1] == 93).ifFalse(() => {
fail()
})
arrayHeader: false
keyStart: left + 1
keyEnd: right - 1
if(keyStart < keyEnd) {
if(source[keyStart] == 91) {
arrayHeader = true
if(keyStart + 1 >= keyEnd) {
fail()
}
(source[keyEnd - 1] == 93).ifFalse(() => {
fail()
})
keyStart = keyStart + 1
keyEnd = keyEnd - 1
}
}
path: syntaxKeyPath(source, keyStart, keyEnd)
if(arrayHeader) {
current = selectArrayHeader(path)
} else {
current = selectHeader(path)
}
}
// Splits [first, last) at the first "=" outside a quoted key.
parseAssignment: (source, first, last) => {
position: first
quote: -1
separator: -1
while((position < last) && (separator < 0)) {
octet: source[position]
if(quote >= 0) {
if((quote == 34) && (octet == 92)) {
position = position + 1
if(position >= last) {
fail()
}
position = position + 1
} else {
if(octet == quote) {
quote = -1
}
position = position + 1
}
} else {
if((octet == 34) || (octet == 39)) {
quote = octet
position = position + 1
} else {
if((octet == 10) || (octet == 13)) {
fail()
}
if(octet == 61) {
separator = position
}
position = position + 1
}
}
}
if(quote >= 0) {
fail()
}
if(separator < 0) {
fail()
}
path: syntaxKeyPath(source, first, separator)
value: syntaxValue(source, separator + 1, last)
putDotted(current, path, 0, value)
}
for(statement in items) {
source: statement.bytes
bounds: trimBounds(source, statement.first, statement.last)
left: bounds.left
right: bounds.right
if(left >= right) {
fail()
}
if(source[left] == 91) {
parseHeader(source, left, right)
} else {
parseAssignment(source, left, right)
}
}
for(arrayNode in Builders.finish(arrayTableNodes)) {
arrayNode.value = Builders.finish(arrayTableBuilders[arrayNode])
}
root
}
{
documentTable: documentTable
}
}