-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
According to the TOML specification, "defining a key multiple times is invalid". Return an error if this is the case, including a trace.
- Loading branch information
Showing
11 changed files
with
165 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,99 @@ | ||
package toml | ||
|
||
object Embed { | ||
def mergeValues(stack: List[String], | ||
result: Map[String, Value], | ||
insert: (String, Value) | ||
): Either[Parse.Error, Map[String, Value]] = { | ||
val (key, value) = insert | ||
if (result.contains(key)) Left((stack :+ key) -> "Cannot redefine value") | ||
else Right(result + (key -> value)) | ||
} | ||
|
||
def mergeTable(insert: List[(String, Value)], | ||
trace: List[String] | ||
): Either[Parse.Error, Value.Tbl] = | ||
merge(insert)(Value.Tbl(Map())) { | ||
case (result, cur) => | ||
mergeValues(trace, result.values, cur) | ||
.right | ||
.map(values => result.copy(values = values)) | ||
} | ||
|
||
def merge[T, U] | ||
(values: List[T]) | ||
(init: U) | ||
(f: (U, T) => Either[Parse.Error, U]): Either[Parse.Error, U] = | ||
{ | ||
values.foldLeft(Right(init): Either[Parse.Error, U]) { | ||
case (Left(m), _) => Left(m) | ||
case (Right(result), node) => f(result, node) | ||
} | ||
} | ||
|
||
def updateTable(value: Value.Tbl, | ||
stack: List[String], | ||
insert: Map[String, Value]): Value.Tbl = | ||
trace: List[String], | ||
insert: List[(String, Value)] | ||
): Either[Parse.Error, Value.Tbl] = | ||
stack match { | ||
case Nil => Value.Tbl(insert) | ||
case Nil => mergeTable(insert, trace) | ||
|
||
case stackHead :: stackTail => | ||
val child = | ||
value.values.get(stackHead) match { | ||
case Some(v: Value.Tbl) => updateTable(v, stackTail, insert) | ||
case Some(v: Value.Tbl) => updateTable(v, stackTail, trace, insert) | ||
case Some(Value.Arr(init :+ last)) => | ||
Value.Arr(init :+ updateTable( | ||
last.asInstanceOf[Value.Tbl], stackTail, insert)) | ||
case None => updateTable(Value.Tbl(Map.empty), stackTail, insert) | ||
updateTable( | ||
last.asInstanceOf[Value.Tbl], stackTail, trace, insert | ||
).right.map(v => Value.Arr(init :+ v)) | ||
case None => updateTable(Value.Tbl(Map()), stackTail, trace, insert) | ||
} | ||
|
||
value.copy(values = value.values + (stackHead -> child)) | ||
child.right.map(c => | ||
value.copy(values = value.values + (stackHead -> c))) | ||
} | ||
|
||
def addArrayRow(value: Value, | ||
stack: List[String], | ||
insert: Map[String, Value]): Value = | ||
trace: List[String], | ||
insert: List[(String, Value)]): Either[Parse.Error, Value] = | ||
stack match { | ||
case Nil => | ||
value match { | ||
case v: Value.Arr => v.copy(values = v.values :+ Value.Tbl(insert)) | ||
case _: Value.Tbl => Value.Arr(List(Value.Tbl(insert))) | ||
} | ||
mergeTable(insert, trace).right.map(tbl => | ||
value match { | ||
case v: Value.Arr => v.copy(values = v.values :+ tbl) | ||
case _: Value.Tbl => Value.Arr(List(tbl)) | ||
}) | ||
|
||
case stackHead :: stackTail => | ||
value match { | ||
case Value.Arr(init :+ last) => | ||
Value.Arr(init :+ addArrayRow(last, stack, insert)) | ||
addArrayRow(last, stack, trace, insert) | ||
.right | ||
.map(v => Value.Arr(init :+ v)) | ||
|
||
case v: Value.Tbl => | ||
val oldChild = v.values.getOrElse(stackHead, Value.Tbl(Map.empty)) | ||
val newChild = addArrayRow(oldChild, stackTail, insert) | ||
|
||
v.copy(values = v.values + (stackHead -> newChild)) | ||
val oldChild = v.values.getOrElse(stackHead, Value.Tbl(Map())) | ||
addArrayRow(oldChild, stackTail, trace, insert).right.map( | ||
newChild => v.copy(values = v.values + (stackHead -> newChild))) | ||
} | ||
} | ||
|
||
/** Eliminates all [[Node]] instances, converting them to tables and arrays */ | ||
def root(root: Root): Value.Tbl = | ||
root.nodes.foldLeft(Value.Tbl(Map.empty)) { (result, node) => | ||
def root(root: Root): Either[Parse.Error, Value.Tbl] = | ||
merge(root.nodes)(Value.Tbl(Map())) { case (result, node) => | ||
node match { | ||
case Node.Pair(key, value) => | ||
result.copy(values = result.values + (key -> value)) | ||
mergeValues(List(), result.values, key -> value) | ||
.right | ||
.map(values => result.copy(values = values)) | ||
case Node.NamedArray(path, pairs) => | ||
addArrayRow(result, path, pairs).asInstanceOf[Value.Tbl] | ||
case Node.NamedTable(path, pairs) => updateTable(result, path, pairs) | ||
addArrayRow(result, path, path, pairs) | ||
.right | ||
.map(_.asInstanceOf[Value.Tbl]) | ||
case Node.NamedTable(path, pairs) => | ||
updateTable(result, path, path, pairs) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package toml | ||
|
||
object Parse { | ||
type Field = String | ||
type Address = List[Field] | ||
type Message = String | ||
type Error = (Address, Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.