about summary refs log tree commit diff
path: root/format/fastxml/types.ha
blob: c0e57d650172aa5f61c4998e08e826adf92043a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// License: MPL-2.0
// (c) 2022 Alexey Yerin <yyp@disroot.org>
// (c) 2022 Chris Palmer <chris@red-oxide.org>
// (c) 2021 Drew DeVault <sir@cmpwn.com>
// (c) 2021 Eyal Sawady <ecs@d2evs.net>
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. <example
export type elementstart = str;

// The end of an XML element, e.g. /> or </example>
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 <![CDATA[baz]]>
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);
	};
};