feat(relaxng): grammar scoping, parentRef, include overrides, div flattening
Each grammar element opens a define scope (scope-mangled names in the flat table) with parentRef reaching the enclosing scope; includes are transitive and include-level defines replace the included grammar's same-name defines (4.7 override); div wrappers flatten while keeping ns/datatypeLibrary inheritance (4.11); value text stays untrimmed so whitespace-preserving datatypes compare exactly (6.2.9); dataExcept recurses through choices. Spec suite: valid 286/289, invalid 287/291; the six remaining instance cases are all the QName datatype.
This commit is contained in:
parent
b8fa90ccf5
commit
5e7cc292f9
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- RELAX NG grammar scoping and include depth, third burn-down (#131): each `grammar` element opens its own define scope (names are scope-mangled in the flat table, so nested grammars do not collide) and `parentRef` reaches the enclosing scope; `include` is transitive (an included grammar's own includes merge first) and a define carried by the include element replaces the included grammar's same-name define (the 4.7 override); `div` wrappers flatten into their grammar while still contributing ns/datatypeLibrary inheritance (4.11); `value` text is kept untrimmed so whitespace-preserving datatypes compare exactly (6.2.9); and `dataExcept` recurses through choice patterns. The spec suite is now clean except one class: valid 286/289 and invalid 287/291, with all six remaining instance cases the QName datatype (its value space needs prefix resolution in both the schema and instance contexts), plus the asserted schema-correctness item.
|
||||
- RELAX NG namespaces and semantics, second burn-down (#131): the compiler now implements `ns` inheritance and the QName form of names (4.9-4.10: `ns` walks ancestors and crosses include/externalRef document boundaries; `name="eg:foo"` and `<name>eg:foo</name>` resolve their prefix against the schema's in-scope xmlns declarations; the `name=` shorthand on an attribute stays namespace-less rather than inheriting), an `include` brings in the included grammar's `start` with an include-level override honored (4.7), multiple `<start>` elements combine like defines and a single define or start may omit `combine` while still merging by the named method (4.17), `<data>` supports `<except>` via a new `dataExcept` pattern in the derivative engine (4.12), an empty element matches a data/value pattern whose lexical space admits the empty string (Clark's empty-text rule), and a whitespace-only attribute value matches a pattern that matches empty. Spec-suite baselines tighten from valid 242/289 to 274/289 and invalid 278/291 to 279/291; the name-class and tree-helper sections split to their own files for the length caps.
|
||||
- RELAX NG simplification, first burn-down (#131): foreign-namespace elements are stripped before pattern interpretation (4.1: only RELAX NG-namespace elements are schema content; a namespace-less element counts only inside a fully unqualified schema), name/type/combine attribute values are whitespace-trimmed (4.2), and include/externalRef hrefs resolve against the xml:base attributes in scope and the loaded document's own URI (4.5, reusing the RFC 3986 resolver), with the document base threaded through nested loads and visited-tracking keyed on resolved hrefs. Spec-suite baselines tighten: valid 242/289 accepted (from 233), invalid 278/291 rejected.
|
||||
- RELAX NG spec test suite runner (#122, #131): James Clark's spectest.xml (385 cases, distributed with jing-trang) runs via an opt-in RNG_TS_ROOT runner, never vendored; the manifest is parsed with PureXML itself, nested testSuite scopes thread their resource/dir definitions into the compiler's loader, and each case asserts compile/reject for the schema plus accept/reject per instance. First measurement, carried as exact baselines: every correct schema compiles and most instances behave (valid 233/289 accepted, invalid 277/291 rejected), while schema-correctness validation is absent (all 213 incorrect schemas compile, count asserted exactly). The burn-down classes are recorded in the runner: schema validation against the RELAX NG grammar and the 4.16-4.18 restrictions, foreign-namespace stripping (4.1), xml:base-aware include/externalRef resolution (4.5), and the section 6 compatibility datatypes.
|
||||
|
||||
@ -93,7 +93,7 @@ extension PureXML.Schema {
|
||||
type.valueMatches(string, literal: literal) ? .empty : .notAllowed
|
||||
case let .data(type): type.validate(string) == nil ? .empty : .notAllowed
|
||||
case let .dataExcept(type, except):
|
||||
type.validate(string) == nil && !nullable(valueDeriv(except, string)) ? .empty : .notAllowed
|
||||
type.validate(string) == nil && !nullable(textDeriv(except, string)) ? .empty : .notAllowed
|
||||
case let .list(inner): listMatches(inner, string) ? .empty : .notAllowed
|
||||
default: .notAllowed
|
||||
}
|
||||
|
||||
@ -40,6 +40,27 @@ enum RNGNode {
|
||||
node.stringValue.trimmingXMLWhitespace()
|
||||
}
|
||||
|
||||
/// The untrimmed text of a `<value>`: 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 `<div>` 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
|
||||
}
|
||||
|
||||
@ -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 `<value>`: `token` when untyped, the library-resolved
|
||||
|
||||
@ -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<Int> = []
|
||||
private let knownValidRejected: Set<Int> = [
|
||||
98, 111, 115, 133, 195, 208, 209, 210, 271, 328, 379, 380,
|
||||
]
|
||||
private let knownInvalidAccepted: Set<Int> = [
|
||||
120, 142, 264, 271, 378, 379,
|
||||
]
|
||||
private let knownValidRejected: Set<Int> = [379, 380]
|
||||
private let knownInvalidAccepted: Set<Int> = [378, 379]
|
||||
|
||||
private struct SpecCase {
|
||||
let index: Int
|
||||
|
||||
Loading…
Reference in New Issue
Block a user