// License: MPL-2.0 // (c) 2022 Alexey Yerin // (c) 2022 Chris Palmer // (c) 2021 Drew DeVault // (c) 2021 Eyal Sawady use encoding::utf8; use errors; use fmt; use io; use memio; use os; export type parser = struct { buf: []u8, cursor: size, unread: rune, state: state, tags: []str, line: size, // memio buffers: namebuf: memio::stream, entbuf: memio::stream, textbuf: memio::stream, }; export type state = enum { ROOT, ELEMENT, ATTRS, }; // The start of an XML element, e.g. or export type elementend = str; // An attribute of an XML element, e.g. foo="bar" export type attribute = (str, str); // Text content of an XML element, e.g. baz or export type text = str; // Any valid XML token export type token = (elementstart | elementend | attribute | text); // A syntax error was encountered in the document. export type syntaxerr = !size; // Any error which can occur during XML parsing. export type error = !(syntaxerr | utf8::invalid | io::error | errors::error | nomem); // Converts an [[error]] to a user-friendly string representation. export fn strerror(err: error) const str = { static let buf: [2048]u8 = [0...]; match (err) { case let err: syntaxerr => return fmt::bsprintf(buf, "Syntax error on line {}", err: size)!; case utf8::invalid => return "Document is not valid UTF-8"; case let err: io::error => return io::strerror(err); case let err: errors::error => return errors::strerror(err); }; };