~sircmpwn/hare-xml

ref: 221487482aa18dca9af5cf7bd5a92fe4c9cc8623 hare-xml/format/xml/types.ha -rw-r--r-- 1.5 KiB
22148748Chris Palmer change dynamic_stream to stream due to stdlib change 5 months ago
                                                                                
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
// 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 bufio;
use encoding::utf8;
use fmt;
use io;
use os;
use strio;

export type parser = struct {
	in: *bufio::bufstream,
	buf: [os::BUFSIZ]u8,
	close: bool,
	state: state,
	tags: []str,
	line: size,

	// strio buffers:
	namebuf: strio::stream,
	entbuf: strio::stream,
	textbuf: strio::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);

// 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);
	};
};