~sircmpwn/hare-xml

ref: 3292657ccdf1169997838f17cf2d3492a6da7bf4 hare-xml/types.ha -rw-r--r-- 1.2 KiB
3292657cDrew DeVault all: overhaul switch/match syntax 1 year, 2 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
use encoding::utf8;
use io;
use os;

export type parser = struct {
	orig: *io::stream,
	in: *io::stream,
	buf: [os::BUFSIZ]u8,
	state: state,
	tags: []str,

	// strio buffers:
	namebuf: *io::stream,
	entbuf: *io::stream,
	textbuf: *io::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 = !void; // TODO: Add line number?

// 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 = match (err) {
case syntaxerr =>
	yield "Syntax error";
case utf8::invalid =>
	yield "Document is not valid UTF-8";
case err: io::error =>
	yield io::strerror(err);
};