`: datatypes with whitespace-preserving
+ /// lexical spaces (built-in `string`) compare it exactly (6.2.9).
+ static func rawText(_ node: RNGTree) -> String {
+ node.stringValue
+ }
+
+ /// Grammar components with `` wrappers flattened (4.11): a div's
+ /// children behave as if they appeared directly in the grammar, while the
+ /// div itself still contributes its ns/datatypeLibrary through the
+ /// ancestor walks.
+ static func grammarComponents(_ node: RNGTree) -> [RNGTree] {
+ elementChildren(node).flatMap { child -> [RNGTree] in
+ localName(child) == "div" ? grammarComponents(child) : [child]
+ }
+ }
+
+ /// The flattened components with a given local name.
+ static func components(_ node: RNGTree, named name: String) -> [RNGTree] {
+ grammarComponents(node).filter { localName($0) == name }
+ }
+
static func strip(_ qualified: String) -> String {
qualified.split(separator: ":").last.map(String.init) ?? qualified
}
diff --git a/Sources/Schema/RelaxNGParser.swift b/Sources/Schema/RelaxNGParser.swift
index a81bc52..3267ec7 100644
--- a/Sources/Schema/RelaxNGParser.swift
+++ b/Sources/Schema/RelaxNGParser.swift
@@ -25,6 +25,20 @@ final class RNGCompiler {
/// The start pattern contributed by an `include`d grammar (4.7), used when
/// the including grammar declares no start of its own.
private var includedStart: Pattern?
+ /// Each `grammar` element opens its own define scope; names are mangled
+ /// with the scope identifier so nested grammars do not collide and
+ /// `parentRef` reaches the enclosing grammar's scope.
+ private var scopeStack: [Int] = []
+ private var nextScope = 0
+
+ private func mangled(_ name: String) -> String {
+ "\(scopeStack.last ?? 0)\u{1}\(name)"
+ }
+
+ private func parentMangled(_ name: String) -> String {
+ let parent = scopeStack.count >= 2 ? scopeStack[scopeStack.count - 2] : (scopeStack.last ?? 0)
+ return "\(parent)\u{1}\(name)"
+ }
/// The ns in scope at a schema node: its own/ancestor `ns` attribute, or
/// the namespace inherited across the document boundary.
@@ -41,13 +55,21 @@ final class RNGCompiler {
}
private func grammar(_ node: Tree) -> Pattern {
- for include in RNGNode.children(node, named: "include") {
+ scopeStack.append(nextScope)
+ nextScope += 1
+ let outerIncludedStart = includedStart
+ includedStart = nil
+ defer {
+ scopeStack.removeLast()
+ includedStart = outerIncludedStart
+ }
+ for include in RNGNode.components(node, named: "include") {
mergeInclude(include)
}
- for define in RNGNode.children(node, named: "define") {
+ for define in RNGNode.components(node, named: "define") {
addDefine(define)
}
- let starts = RNGNode.children(node, named: "start")
+ let starts = RNGNode.components(node, named: "start")
guard let first = starts.first else {
return includedStart ?? .notAllowed
}
@@ -73,7 +95,8 @@ final class RNGCompiler {
/// with `combine="choice"`/`"interleave"` merges with the existing one rather
/// than replacing it (a plain redefinition still replaces).
private func addDefine(_ define: Tree) {
- guard let name = RNGNode.attribute(define, "name") else { return }
+ guard let rawName = RNGNode.attribute(define, "name") else { return }
+ let name = mangled(rawName)
let pattern = combined(RNGNode.elementChildren(define), .sequence)
if let method = RNGNode.attribute(define, "combine") {
defineCombine[name] = method
@@ -105,10 +128,19 @@ final class RNGCompiler {
if RNGNode.inheritedNS(grammar) == nil {
fallbackNamespace = effectiveNS(node)
}
- for define in RNGNode.children(grammar, named: "define") {
+ // The included grammar's own includes merge first (4.7 is transitive).
+ for nested in RNGNode.components(grammar, named: "include") {
+ mergeInclude(nested)
+ }
+ // A define carried by the include element REPLACES the included
+ // grammar's define of the same name (4.7 override), so those names
+ // are skipped here and added from the include's children below.
+ let overrides = Set(RNGNode.children(node, named: "define").compactMap { RNGNode.attribute($0, "name") })
+ for define in RNGNode.components(grammar, named: "define") {
+ guard let name = RNGNode.attribute(define, "name"), !overrides.contains(name) else { continue }
addDefine(define)
}
- if includedStart == nil, let start = RNGNode.children(grammar, named: "start").first {
+ if includedStart == nil, let start = RNGNode.components(grammar, named: "start").first {
includedStart = combined(RNGNode.elementChildren(start), .sequence)
}
documentBase = outerBase
@@ -134,10 +166,20 @@ final class RNGCompiler {
case "text": .text
case "data": dataPattern(node)
case "value": valuePattern(node)
- case "ref": .ref(RNGNode.attribute(node, "name") ?? "")
- case "externalRef": externalRef(node)
case "element": element(node)
case "attribute": attribute(node)
+ default: referencePattern(node)
+ }
+ }
+
+ /// The reference-shaped leaves: named refs (scope-mangled), parent refs,
+ /// nested grammars, and external references.
+ private func referencePattern(_ node: Tree) -> Pattern? {
+ switch RNGNode.localName(node) {
+ case "ref": .ref(mangled(RNGNode.attribute(node, "name") ?? ""))
+ case "parentRef": .ref(parentMangled(RNGNode.attribute(node, "name") ?? ""))
+ case "grammar": grammar(node)
+ case "externalRef": externalRef(node)
default: nil
}
}
@@ -174,7 +216,7 @@ final class RNGCompiler {
/// an unknown datatype, so the value matches nothing.
private func valuePattern(_ node: Tree) -> Pattern {
guard let type = valueType(node) else { return .notAllowed }
- return .value(type, RNGNode.text(node))
+ return .value(type, RNGNode.rawText(node))
}
/// The datatype of a ``: `token` when untyped, the library-resolved
diff --git a/Tests/RelaxNGSpecSuiteTests.swift b/Tests/RelaxNGSpecSuiteTests.swift
index cafe128..661e078 100644
--- a/Tests/RelaxNGSpecSuiteTests.swift
+++ b/Tests/RelaxNGSpecSuiteTests.swift
@@ -19,25 +19,19 @@ struct RelaxNGSpecSuiteTests {
}
/// The burn-down frontier, by case index in document order (exact, so
- /// progress and regressions both show). Three classes:
+ /// progress and regressions both show). Two classes remain:
/// 1. Schema correctness is not validated: every `incorrect` schema
/// compiles (the count is asserted exactly below). Closing it means
/// validating schemas against the RELAX NG grammar and the section
/// 4.16-4.18 restrictions before pattern interpretation.
- /// 2. Simplification gaps reject valid instances: foreign-namespace
- /// element/attribute stripping (4.1: cases 90-98), xml:base-aware
- /// include/externalRef resolution (4.5: 100-101), include/div/override
- /// depth (4.6-4.12), the 4.17-4.18 normalization corners, and the
- /// section 6 compatibility datatypes (ID/IDREF, 6.2.x).
- /// 3. The same classes accept some `invalid` instances.
+ /// 2. The QName datatype (cases 378-380): its value space needs prefix
+ /// resolution in BOTH contexts, the schema's xmlns/ns scope at compile
+ /// and the instance element's in-scope namespaces at validation, which
+ /// the derivative engine does not thread yet.
private let knownIncorrectCompiledCount = 213
private let knownCorrectRejected: Set = []
- private let knownValidRejected: Set = [
- 98, 111, 115, 133, 195, 208, 209, 210, 271, 328, 379, 380,
- ]
- private let knownInvalidAccepted: Set = [
- 120, 142, 264, 271, 378, 379,
- ]
+ private let knownValidRejected: Set = [379, 380]
+ private let knownInvalidAccepted: Set = [378, 379]
private struct SpecCase {
let index: Int