// // XMLScanner.swift // RSParser // // Created by Brent Simmons on 4/18/26. // // Low-level byte-oriented scanner for XML events. // // Feeds the parser with one event at a time. Liberal: never throws for bad // input, recovers as best it can, and passes garbage through as literal text. // // The scanner doesn't know about namespace resolution — it surfaces raw // (prefix, localName) byte ranges and lets the parser layer resolve URIs. struct XMLScanner { /// An event emitted by the scanner. Byte ranges point into the input buffer. /// Attributes surface with raw byte ranges only — the parser resolves namespaces. enum Event { case startElement(prefix: Range?, localName: Range, attributes: [RawAttribute], selfClosing: Bool) case endElement(prefix: Range?, localName: Range) case characters([UInt8]) // entity-expanded bytes case endOfDocument } /// Raw attribute data as produced by the scanner. The parser turns each of these /// into an `XMLAttributes.Attribute` after resolving the prefix to a URI. /// /// Byte fields are `ArraySlice` — by default they view into the scanner's /// input buffer (no copy). When entity expansion produces bytes that differ from /// the input (e.g. `&` → `&`), the slice wraps a fresh owned `[UInt8]`. struct RawAttribute { let prefixSlice: ArraySlice? let localNameSlice: ArraySlice let valueSlice: ArraySlice } private let input: [UInt8] private var pos: Int = 0 init(_ input: [UInt8]) { self.input = input } /// Current byte offset into the input. After `next()` returns, this is the position /// just past the last event's bytes. Before calling `next()`, this is where the /// next event will start scanning from — useful for capturing byte ranges that /// span multiple events (e.g. raw-inner-content capture). var position: Int { pos } // MARK: - Entry point /// Advance and return the next event, or .endOfDocument. mutating func next() -> Event { while pos < input.count { let b = input[pos] if b == .asciiLessThan { if let event = scanMarkup() { return event } // scanMarkup returned nil — it consumed a comment/PI/DOCTYPE silently, // keep scanning for the next event. continue } // Character content. return scanCharacters() } return .endOfDocument } } // MARK: - Private private extension XMLScanner { // MARK: - Markup dispatch /// Handle everything starting with `<`. Returns nil for silently-consumed markup /// (comments, PIs, DOCTYPE, CDATA that produced no output). mutating func scanMarkup() -> Event? { assert(input[pos] == .asciiLessThan) let next = peek(1) // ``. consumeUnknownDeclaration() return nil } // `` processing instruction (already-handled XML decl case too) if next == .asciiQuestion { consumeProcessingInstruction() return nil } // ` Event { assert(input[pos] == .asciiLessThan) pos += 1 // consume `<` let (prefix, local) = scanQualifiedName() var attributes = [RawAttribute]() var selfClosing = false // Attribute loop. while pos < input.count { skipWhitespace() if pos >= input.count { break } let b = input[pos] if b == .asciiGreaterThan { pos += 1 break } if b == .asciiSlash { // Self-closing `/>` if peek(1) == .asciiGreaterThan { selfClosing = true pos += 2 break } // Stray `/`. Eat it, keep going (liberal). pos += 1 continue } if !b.isXMLNameStart { // Unrecognized garbage — advance and try again. pos += 1 continue } if let attribute = scanAttribute() { attributes.append(attribute) } } return .startElement(prefix: prefix, localName: local, attributes: attributes, selfClosing: selfClosing) } mutating func scanAttribute() -> RawAttribute? { let (prefix, local) = scanQualifiedName() let prefixSlice: ArraySlice? = prefix.map { input[$0] } let localSlice = input[local] skipWhitespace() // Expect `=`. Liberal: if missing, treat as empty value. guard pos < input.count, input[pos] == .asciiEquals else { return RawAttribute(prefixSlice: prefixSlice, localNameSlice: localSlice, valueSlice: ArraySlice()) } pos += 1 skipWhitespace() guard pos < input.count else { return RawAttribute(prefixSlice: prefixSlice, localNameSlice: localSlice, valueSlice: ArraySlice()) } let valueSlice = scanAttributeValue() return RawAttribute(prefixSlice: prefixSlice, localNameSlice: localSlice, valueSlice: valueSlice) } mutating func scanAttributeValue() -> ArraySlice { guard pos < input.count else { return ArraySlice() } let quote = input[pos] if quote == .asciiDoubleQuote || quote == .asciiSingleQuote { pos += 1 let start = pos var out = [UInt8]() var expanded = false while pos < input.count { let b = input[pos] if b == quote { // Fast path: no entities — return a view into the input buffer. if !expanded { let slice = input[start..`, or `/`. Liberal. let start = pos var out = [UInt8]() var expanded = false while pos < input.count { let b = input[pos] if b.isASCIIWhitespace || b == .asciiGreaterThan || b == .asciiSlash { break } if b == .asciiAmpersand { if !expanded { out.append(contentsOf: input[start.. Event { assert(input[pos] == .asciiLessThan && peek(1) == .asciiSlash) pos += 2 let (prefix, local) = scanQualifiedName() // Skip to `>` (liberal: tolerate whitespace and garbage). while pos < input.count && input[pos] != .asciiGreaterThan { pos += 1 } if pos < input.count { pos += 1 } return .endElement(prefix: prefix, localName: local) } // MARK: - CDATA mutating func scanCDATA() -> Event { // ``. var end = pos while end + 2 < input.count { if input[end] == .asciiRightBracket && input[end + 1] == .asciiRightBracket && input[end + 2] == .asciiGreaterThan { break } end += 1 } // If we didn't find `]]>` before EOF, consume to end of buffer (liberal). if end + 2 >= input.count { end = input.count pos = end } else { pos = end + 3 } // Per our liberal mode: inside CDATA, preserve the predefined XML entities // (& < > " ') but still expand numeric and HTML named entities. let bytes = Array(input[start.. [UInt8] { // Quick check: if no `&`, return as-is. if !bytes.contains(.asciiAmpersand) { return bytes } var out = [UInt8]() out.reserveCapacity(bytes.count) var i = 0 while i < bytes.count { let b = bytes[i] if b == .asciiAmpersand { let result = XMLEntities.decode(bytes: bytes, at: i, mode: .preservePredefinedXML) out.append(contentsOf: result.bytes) i = result.nextIndex } else { out.append(b) i += 1 } } return out } // MARK: - Consumption of non-emitting markup mutating func consumeComment() { // Caller already matched `