feat(relaxng): compare typed <value> in the datatype value space

A <value> pattern now carries its datatype, so matching compares the
instance and the literal in that type's value space rather than purely
lexically. Numeric and date/time types compare by value (1 equals 01,
1.5 equals 1.50); booleans treat 1/true and 0/false as equal; string and
token compare by their whitespace-normalized lexical form, as before.
Untyped values default to the built-in token type, and a bare string
literal in the compact syntax is token-typed. Durations and binary types
fall under the lexical rule.
This commit is contained in:
Mihaela Mihaljevic 2026-06-08 17:36:16 +02:00
parent c1e181db50
commit 79f424321c
6 changed files with 153 additions and 16 deletions

View File

@ -249,7 +249,8 @@ extension RNCParser {
private func parseNonWordPrimary() -> Pattern {
switch peek() {
case let .string(value): advance()
return .value(value)
// A bare string literal is shorthand for a `token`-typed value.
return .value(PureXML.Schema.SimpleType(base: .token), value)
case .symbol("("): advance()
let pattern = parsePattern()
expectSymbol(")")
@ -268,7 +269,7 @@ extension RNCParser {
var facets = PureXML.Schema.Facets()
if peek() == .symbol("{") { parseParams(into: &facets) }
if case let .string(value) = peek() { advance()
return .value(value)
return .value(PureXML.Schema.SimpleType(base: type), value)
}
return .data(PureXML.Schema.SimpleType(base: type, facets: facets))
}

View File

@ -83,9 +83,8 @@ extension PureXML.Schema {
private func valueDeriv(_ pattern: PureXML.Schema.Pattern, _ string: String) -> PureXML.Schema.Pattern {
switch pattern {
case let .value(literal):
SimpleType(base: .token).validate(string) == nil
&& SimpleType.process(string, whiteSpace: .collapse) == literal ? .empty : .notAllowed
case let .value(type, literal):
type.valueMatches(string, literal: literal) ? .empty : .notAllowed
case let .data(type): type.validate(string) == nil ? .empty : .notAllowed
case let .list(inner): listMatches(inner, string) ? .empty : .notAllowed
default: .notAllowed

View File

@ -134,19 +134,25 @@ private final class RNGCompiler {
return .data(PureXML.Schema.SimpleType(base: base, facets: facets))
}
/// Builds a `<value>` pattern. An explicit `type` that the in-scope
/// `datatypeLibrary` does not define makes the value unmatchable; otherwise
/// the literal is compared lexically.
/// Builds a `<value>` pattern carrying its datatype, so the value compares in
/// that type's value space. An untyped `<value>` uses the built-in `token`
/// type; an explicit `type` the in-scope `datatypeLibrary` does not define is
/// an unknown datatype, so the value matches nothing.
private func valuePattern(_ node: Tree) -> Pattern {
if hasUnknownType(node) { return .notAllowed }
return .value(RNGNode.text(node))
guard let type = valueType(node) else { return .notAllowed }
return .value(type, RNGNode.text(node))
}
/// Whether `node` declares a `type` the in-scope `datatypeLibrary` does not
/// define (an untyped `<value>` is always known: it compares lexically).
private func hasUnknownType(_ node: Tree) -> Bool {
guard let declared = RNGNode.attribute(node, "type") else { return false }
return resolvedBuiltin(RNGNode.strip(declared), library: datatypeLibrary(of: node)) == nil
/// The datatype of a `<value>`: `token` when untyped, the library-resolved
/// built-in when typed, or nil when the declared type is unknown.
private func valueType(_ node: Tree) -> PureXML.Schema.SimpleType? {
guard let declared = RNGNode.attribute(node, "type") else {
return PureXML.Schema.SimpleType(base: .token)
}
guard let base = resolvedBuiltin(RNGNode.strip(declared), library: datatypeLibrary(of: node)) else {
return nil
}
return PureXML.Schema.SimpleType(base: base)
}
/// Resolves a datatype name within a library. The default (empty) library

View File

@ -36,7 +36,7 @@ public extension PureXML.Schema {
case element(NameClass, Pattern)
case attribute(NameClass, Pattern)
case data(SimpleType)
case value(String)
case value(SimpleType, String)
case list(Pattern)
case ref(String)
case after(Pattern, Pattern)

View File

@ -109,6 +109,37 @@ public extension PureXML.Schema {
return nil
}
/// Whether `instance` and `literal` denote the same value in this type's
/// value space, the comparison a RELAX NG `<value>` performs. Ordered
/// types (numeric, date/time) compare in value space, so `1` equals `01`
/// and `1.0` equals `1.00`; booleans treat `1`/`true` and `0`/`false` as
/// equal. Every other type, including `string`/`token`, compares by its
/// whitespace-normalized lexical form. (Durations and binary types fall
/// under the lexical rule, so only their normalized forms compare equal.)
public func valueMatches(_ instance: String, literal: String) -> Bool {
let whiteSpace = facets.whiteSpace ?? base.whiteSpace
let left = Self.process(instance, whiteSpace: whiteSpace)
let right = Self.process(literal, whiteSpace: whiteSpace)
switch base.primitive {
case .decimal, .integer, .double, .float, .dateKind:
guard let leftValue = base.primitive.ordered(left), let rightValue = base.primitive.ordered(right) else { return false }
return leftValue == rightValue
case .boolean:
guard let leftValue = Self.booleanValue(left) else { return false }
return leftValue == Self.booleanValue(right)
default:
return left == right
}
}
private static func booleanValue(_ value: String) -> Bool? {
switch value {
case "true", "1": true
case "false", "0": false
default: nil
}
}
private func validateUnion(_ lexical: String, members: [SimpleType]) -> String? {
for member in members where member.validate(lexical) == nil {
return nil

View File

@ -0,0 +1,100 @@
@testable import PureXML
import Testing
@Suite("RELAX NG typed <value> value-space comparison")
struct RelaxNGValueSpaceTests {
private let xsd = "http://www.w3.org/2001/XMLSchema-datatypes"
private func validXML(_ rng: String, _ xml: String) throws -> Bool {
try PureXML.Schema.RelaxNG(rng).validate(xml)
}
private func validRNC(_ rnc: String, _ xml: String) throws -> Bool {
try PureXML.Schema.RelaxNG(compact: rnc).validate(xml)
}
private func valueElement(_ type: String, _ literal: String) -> String {
"""
<element name="n" datatypeLibrary="\(xsd)" xmlns="http://relaxng.org/ns/structure/1.0">
<value type="\(type)">\(literal)</value>
</element>
"""
}
@Test("An integer value matches in value space, not lexically")
func test_integerValueSpace() throws {
let rng = valueElement("integer", "1")
#expect(try validXML(rng, "<n>1</n>"))
#expect(try validXML(rng, "<n>01</n>"))
#expect(try validXML(rng, "<n>+1</n>"))
#expect(try !validXML(rng, "<n>2</n>"))
#expect(try !validXML(rng, "<n>1x</n>"))
}
@Test("A decimal value matches regardless of trailing zeros")
func test_decimalValueSpace() throws {
let rng = valueElement("decimal", "1.5")
#expect(try validXML(rng, "<n>1.5</n>"))
#expect(try validXML(rng, "<n>1.50</n>"))
#expect(try validXML(rng, "<n>01.500</n>"))
#expect(try !validXML(rng, "<n>1.6</n>"))
}
@Test("A boolean value treats 1/true and 0/false as equal")
func test_booleanValueSpace() throws {
let trueRNG = valueElement("boolean", "true")
#expect(try validXML(trueRNG, "<n>true</n>"))
#expect(try validXML(trueRNG, "<n>1</n>"))
#expect(try !validXML(trueRNG, "<n>false</n>"))
#expect(try !validXML(trueRNG, "<n>0</n>"))
}
@Test("A token value still compares by normalized lexical form")
func test_tokenLexical() throws {
// Untyped value defaults to token: whitespace collapses, then compares.
let rng = """
<element name="flag" xmlns="http://relaxng.org/ns/structure/1.0">
<value>yes please</value>
</element>
"""
#expect(try validXML(rng, "<flag>yes please</flag>"))
#expect(try validXML(rng, "<flag> yes please </flag>"))
#expect(try !validXML(rng, "<flag>no</flag>"))
}
@Test("A string value preserves and compares exactly after the type's whitespace rule")
func test_stringPreserve() throws {
let rng = valueElement("string", "abc")
#expect(try validXML(rng, "<n>abc</n>"))
#expect(try !validXML(rng, "<n>abcd</n>"))
}
@Test("Compact syntax typed value compares in value space")
func test_compactIntegerValue() throws {
let rnc = """
namespace x = "urn:x"
element n { xsd:integer "10" }
"""
#expect(try validRNC(rnc, "<n>10</n>"))
#expect(try validRNC(rnc, "<n>010</n>"))
#expect(try !validRNC(rnc, "<n>11</n>"))
}
@Test("Compact bare string literal is a token value")
func test_compactBareLiteral() throws {
let rnc = "element flag { \"on\" }"
#expect(try validRNC(rnc, "<flag>on</flag>"))
#expect(try validRNC(rnc, "<flag> on </flag>"))
#expect(try !validRNC(rnc, "<flag>off</flag>"))
}
@Test("SimpleType.valueMatches compares numeric value space directly")
func test_valueMatchesUnit() {
let integer = PureXML.Schema.SimpleType(base: .integer)
#expect(integer.valueMatches("007", literal: "7"))
#expect(!integer.valueMatches("7", literal: "8"))
let token = PureXML.Schema.SimpleType(base: .token)
#expect(token.valueMatches(" a b ", literal: "a b"))
#expect(!token.valueMatches("a b", literal: "ab"))
}
}