Seven correctness fixes to the recently landed anchors/styles, canonical, timestamp/binary, and full-node-key work, each reproduced and covered by a regression test. - Stream dump doubled the `---` marker under explicitDocumentStart or canonical: the single-document body now renders marker-free and the stream path owns marker emission, so a two-document stream reparses as two documents, not four. - Tagged flow-mapping keys used block-context safety, so a key like `a,b` emitted bare and reparsed as several entries; flow keys now use the flow-aware plain-scalar check. - Plain scalars ending in `:` reparse as an empty-valued mapping key, so both the model and tagged emit paths now fall back to quoting them. - An alias mapping key pointing at a defined non-scalar anchor raised the misleading undefinedAlias; it now raises expectedScalarKey, and a truly undefined alias key still raises undefinedAlias. - Tagged duplicate-key detection keyed on source spelling: it now keys on the resolved scalar value plus effective tag, so 0x1 and 1 collide while 1 and "1" do not and custom-tagged keys stay distinct. - Dumping a tree nested past the emitter limit overflowed the stack. - Timestamp now accepts a leap-second value of 60, counted as 60 elapsed seconds in the epoch math. - Tagged.Dumper honors sortKeys in its block and canonical paths for all-plain-string-key mappings, matching the model dumper. Nesting-guard design: dump is public and non-throwing across 90+ call sites, so making it throwing would be a breaking change. A tree nested deeper than the parser's own maxNestingDepth cannot round-trip through PureYAML (the parser rejects it), so no faithful emission exists. Each dump entry point now measures depth iteratively (no recursion, so the guard itself cannot overflow) and, past the limit, returns a documented, reparseable marker document that resolves to null. Under the limit the existing recursive renderers run byte-identically, so parsed trees and every realistic hand-built tree are unaffected. This mirrors the parser's enforceNestingLimit, which bounds depth before any recursive consumer.
133 lines
4.8 KiB
Swift
133 lines
4.8 KiB
Swift
// MARK: - Canonical form
|
|
|
|
extension PureYAML.Tagged.Dumper {
|
|
/// The YAML-specification canonical form over a tagged tree: an
|
|
/// explicit `---` document start, every node's tag explicit (the
|
|
/// carried tag when present, the value's implicit tag otherwise),
|
|
/// double-quoted scalar content, flow collections one entry per line,
|
|
/// and anchors and alias references preserved. Canonical normalizes
|
|
/// scalar spellings: content renders from the resolved value, not the
|
|
/// source rawValue.
|
|
func canonicalDocument(_ node: PureYAML.Tagged.Node) -> String {
|
|
"---\n" + canonicalLines(node, indent: 0).joined(separator: "\n") + "\n"
|
|
}
|
|
|
|
func canonicalLines(
|
|
_ node: PureYAML.Tagged.Node,
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
switch node {
|
|
case let .alias(anchor, _):
|
|
return [prefix + "*" + anchor]
|
|
case let .scalar(scalar):
|
|
return [prefix + canonicalProperties(node) + canonicalScalarContent(scalar)]
|
|
case let .sequence(sequence):
|
|
return canonicalSequenceLines(sequence, node: node, indent: indent)
|
|
case let .mapping(mapping):
|
|
return canonicalMappingLines(mapping, node: node, indent: indent)
|
|
}
|
|
}
|
|
|
|
func canonicalSequenceLines(
|
|
_ sequence: PureYAML.Tagged.Sequence,
|
|
node: PureYAML.Tagged.Node,
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
let props = canonicalProperties(node, fallback: .sequence)
|
|
guard !sequence.values.isEmpty else {
|
|
return [prefix + props + "[]"]
|
|
}
|
|
var lines = [prefix + props + "["]
|
|
for value in sequence.values {
|
|
var entry = canonicalLines(value, indent: indent + options.indentWidth)
|
|
entry[entry.count - 1] += ","
|
|
lines.append(contentsOf: entry)
|
|
}
|
|
lines.append(prefix + "]")
|
|
return lines
|
|
}
|
|
|
|
func canonicalMappingLines(
|
|
_ mapping: PureYAML.Tagged.Mapping,
|
|
node: PureYAML.Tagged.Node,
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
let props = canonicalProperties(node, fallback: .mapping)
|
|
guard !mapping.pairs.isEmpty else {
|
|
return [prefix + props + "{}"]
|
|
}
|
|
let inner = indent + options.indentWidth
|
|
let innerPrefix = String(repeating: " ", count: inner)
|
|
var lines = [prefix + props + "{"]
|
|
for pair in orderedPairs(mapping) {
|
|
var keyLines = canonicalLines(pair.keyNode, indent: inner)
|
|
keyLines[0] = innerPrefix + "? " + String(keyLines[0].dropFirst(innerPrefix.count))
|
|
lines.append(contentsOf: keyLines)
|
|
var valueLines = canonicalLines(pair.value, indent: inner)
|
|
valueLines[0] = innerPrefix + ": " + String(valueLines[0].dropFirst(innerPrefix.count))
|
|
valueLines[valueLines.count - 1] += ","
|
|
lines.append(contentsOf: valueLines)
|
|
}
|
|
lines.append(prefix + "}")
|
|
return lines
|
|
}
|
|
|
|
/// The canonical `&anchor !tag ` prefix: anchor first, then the carried
|
|
/// tag, or the value's implicit tag made explicit when none is carried.
|
|
func canonicalProperties(
|
|
_ node: PureYAML.Tagged.Node,
|
|
fallback: PureYAML.Tagged.Tag? = nil,
|
|
) -> String {
|
|
var parts: [String] = []
|
|
if let anchor = node.anchor, node.kind != .alias {
|
|
parts.append("&" + anchor)
|
|
}
|
|
if let tag = node.tag ?? fallback ?? implicitTag(of: node) {
|
|
parts.append(renderTag(tag))
|
|
}
|
|
return parts.joined(separator: " ") + " "
|
|
}
|
|
|
|
func implicitTag(of node: PureYAML.Tagged.Node) -> PureYAML.Tagged.Tag? {
|
|
guard case let .scalar(scalar) = node else {
|
|
return nil
|
|
}
|
|
switch scalar.value {
|
|
case .null:
|
|
return .null
|
|
case .bool:
|
|
return .bool
|
|
case .int:
|
|
return .int
|
|
case .double:
|
|
return .float
|
|
case .string:
|
|
return .string
|
|
case .sequence, .mapping:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// Canonical scalar content: always double-quoted, rendered from the
|
|
/// resolved value so `NO` normalizes to `"false"` under its bool tag.
|
|
func canonicalScalarContent(_ scalar: PureYAML.Tagged.Scalar) -> String {
|
|
switch scalar.value {
|
|
case .null:
|
|
"\"null\""
|
|
case let .bool(flag):
|
|
"\"\(flag ? "true" : "false")\""
|
|
case let .int(number):
|
|
"\"\(number)\""
|
|
case let .double(number):
|
|
"\"\(number)\""
|
|
case let .string(string):
|
|
PureYAML.Emitting.Dumper(options: options).quote(string)
|
|
case .sequence, .mapping:
|
|
"\"\""
|
|
}
|
|
}
|
|
}
|