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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::fmt;
use std::error;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct TextPosition {
pub row: u64,
pub column: u64,
}
impl TextPosition {
#[inline]
pub fn new() -> TextPosition {
TextPosition { row: 0, column: 0 }
}
#[inline]
pub fn advance(&mut self, count: u8) {
self.column += count as u64;
}
#[inline]
pub fn advance_to_tab(&mut self, width: u8) {
let width = width as u64;
self.column += width - self.column % width
}
#[inline]
pub fn new_line(&mut self) {
self.column = 0;
self.row += 1;
}
}
impl fmt::Debug for TextPosition {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.row + 1, self.column + 1)
}
}
impl fmt::Display for TextPosition {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.row + 1, self.column + 1)
}
}
pub trait Position {
fn position(&self) -> TextPosition;
}
impl Position for TextPosition {
#[inline]
fn position(&self) -> TextPosition {
*self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Error {
pos: TextPosition,
msg: String
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.pos, self.msg)
}
}
impl Position for Error {
#[inline]
fn position(&self) -> TextPosition { self.pos }
}
impl Error {
#[inline]
pub fn new<O: Position>(o: &O, msg: String) -> Error {
Error { pos: o.position(), msg: msg }
}
#[inline]
pub fn msg<'a>(&'a self) -> &'a str { &self.msg }
}
impl error::Error for Error {
#[inline]
fn description(&self) -> &str { &*self.msg }
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum XmlVersion {
Version10,
Version11
}
impl fmt::Display for XmlVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
XmlVersion::Version10 => write!(f, "1.0"),
XmlVersion::Version11 => write!(f, "1.1")
}
}
}
pub fn is_whitespace_char(c: char) -> bool {
match c {
'\x20' | '\x09' | '\x0d' | '\x0a' => true,
_ => false
}
}
pub fn is_name_start_char(c: char) -> bool {
match c {
':' | 'A'...'Z' | '_' | 'a'...'z' |
'\u{C0}'...'\u{D6}' | '\u{D8}'...'\u{F6}' | '\u{F8}'...'\u{2FF}' |
'\u{370}'...'\u{37D}' | '\u{37F}'...'\u{1FFF}' |
'\u{200C}'...'\u{200D}' | '\u{2070}'...'\u{218F}' |
'\u{2C00}'...'\u{2FEF}' | '\u{3001}'...'\u{D7FF}' |
'\u{F900}'...'\u{FDCF}' | '\u{FDF0}'...'\u{FFFD}' |
'\u{10000}'...'\u{EFFFF}' => true,
_ => false
}
}
pub fn is_name_char(c: char) -> bool {
match c {
_ if is_name_start_char(c) => true,
'-' | '.' | '0'...'9' | '\u{B7}' |
'\u{300}'...'\u{3F6}' | '\u{203F}'...'\u{2040}' => true,
_ => false
}
}