import { QuoteType, Tokenizer, type TokenizerCallbacks } from "htmlparser2"; import { LineIndex } from "@/core/xml/parse/line-index.ts"; import { XML_NODE_KIND, XML_PARSE, XML_PARSE_TEXT } from "@/core/xml/parse/parse.constants.ts"; import type { XmlLocation, XmlNode } from "@/core/xml/parse/parse.types.ts"; import { XmlSyntaxError } from "@/core/xml/parse/xml-syntax-error.ts"; import { XmlText } from "@/core/xml/parse/xml-text.ts"; interface OpenElement { readonly type: string; readonly at: XmlLocation; readonly attributes: Map; readonly children: XmlNode[]; } interface OpenAttribute { readonly name: string; readonly at: XmlLocation; readonly nameEnd: number; /** Where the value starts: 0.7.0 reports a missing quote there. */ valueAt?: XmlLocation; raw: string; } /** * One parse: htmlparser2's tokenizer says where each piece starts and ends and forgives everything; this reader * is the strict half, and fails at the first fault with its place. Built per source, read once. */ export class XmlReader implements TokenizerCallbacks { readonly #source: string; readonly #lines: LineIndex; readonly #text = new XmlText(); readonly #root: XmlNode[] = []; readonly #stack: OpenElement[] = []; /** The tag whose attributes are being read, and the attribute in it. */ #tag: OpenElement | null = null; #attribute: OpenAttribute | null = null; /** The text read so far, as a range of the source: the tokenizer may cut one text in several. */ #pending: { start: number; end: number } | null = null; constructor(source: string) { this.#source = source; this.#lines = new LineIndex(source); } read(): readonly XmlNode[] { const tokenizer = new Tokenizer(XML_PARSE.tokenizer, this); tokenizer.write(this.#source); tokenizer.end(); return this.#root; } onopentagname(start: number, end: number): void { this.#flushText(); const type = this.#source.slice(start, end); if (!XML_PARSE.name.test(type)) { throw this.#fail(start, XML_PARSE_TEXT.expectedTagName); } if (this.#stack.length >= XML_PARSE.maxDepth) { throw this.#fail(start - XML_PARSE.openChars, XML_PARSE_TEXT.tooDeep(XML_PARSE.maxDepth)); } this.#tag = { type, at: this.#lines.locate(start - XML_PARSE.openChars), attributes: new Map(), children: [] }; this.#stack.push(this.#tag); } onattribname(start: number, end: number): void { const name = this.#source.slice(start, end); if (!XML_PARSE.name.test(name)) { throw this.#fail(start, XML_PARSE_TEXT.expectedAttribute); } if (!XML_PARSE.spaceChar.test(this.#source.charAt(start - 1))) { throw this.#fail(start, XML_PARSE_TEXT.noSpace); } if (this.#tag?.attributes.has(name)) { throw this.#fail(start, XML_PARSE_TEXT.duplicate(name)); } this.#attribute = { name, at: this.#lines.locate(start), nameEnd: end, raw: "" }; } onattribdata(start: number, end: number): void { if (this.#attribute !== null) { this.#attribute.valueAt ??= this.#lines.locate(start); this.#attribute.raw += this.#source.slice(start, end); } } onattribend(quote: QuoteType): void { const attribute = this.#attribute; this.#attribute = null; if (attribute === null) { return; } if (quote === QuoteType.NoValue || quote === QuoteType.Unquoted) { throw new XmlSyntaxError(attribute.valueAt ?? attribute.at, XML_PARSE_TEXT.needsQuotes(attribute.name)); } if (attribute.raw.includes(XML_PARSE.chars.lt)) { const mark = quote === QuoteType.Single ? XML_PARSE.chars.single : XML_PARSE.chars.double; throw new XmlSyntaxError(attribute.at, XML_PARSE_TEXT.badValue(attribute.name, mark)); } this.#tag?.attributes.set(attribute.name, this.#text.decode(attribute.raw, attribute.at)); } onopentagend(): void { this.#tag = null; } onselfclosingtag(): void { this.#tag = null; this.#finish(); } onclosetag(start: number, end: number): void { this.#flushText(); const found = this.#source.slice(start, end); const at = this.#lines.locate(this.#source.lastIndexOf(XML_PARSE.chars.closeOpen, start)); const open = this.#stack.at(-1); if (open === undefined) { throw new XmlSyntaxError(at, XML_PARSE_TEXT.closesNothing(found)); } if (open.type !== found) { throw new XmlSyntaxError(at, XML_PARSE_TEXT.wrongClose(open.type, open.at.line, found)); } this.#finish(); } ontext(start: number, end: number): void { // Text that starts right after `` the tokenizer saw. */ oncdata( start: number, end: number, closing: number, ): void { this.#flushText(); const at = this.#lines.locate(start - XML_PARSE.cdataOpenChars); if (closing === 0) { throw new XmlSyntaxError(at, XML_PARSE_TEXT.cdataNotClosed); } this.#siblings().push({ kind: XML_NODE_KIND.text, value: this.#source.slice(start, end - closing), ...at }); } /** Comments and processing instructions are nothing; a comment never closed is a fault. */ oncomment( start: number, _end: number, closing: number, ): void { this.#flushText(); if (closing === 0) { throw this.#fail(start - XML_PARSE.commentOpenChars, XML_PARSE_TEXT.commentNotClosed); } } onprocessinginstruction(): void { this.#flushText(); } ondeclaration(start: number): void { throw this.#fail(start - XML_PARSE.declarationChars, XML_PARSE_TEXT.expectedTagName); } onend(): void { this.#flushText(); // The tokenizer emits no text after a final `