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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*=============================================================================
Copyright (c) 2017 Daniel James
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "xml_parse.hpp"
#include "simple_parse.hpp"
#include "stream.hpp"
#include "utils.hpp"
namespace quickbook
{
namespace detail
{
// write_xml_tree
void write_xml_tree_impl(
std::string& out, xml_element* node, unsigned int depth);
void write_xml_tree(xml_element* node)
{
std::string result;
write_xml_tree_impl(result, node, 0);
quickbook::detail::out() << result << std::flush;
}
void write_xml_tree_impl(
std::string& out, xml_element* node, unsigned int depth)
{
if (!node) {
return;
}
for (unsigned i = 0; i < depth; ++i) {
out += " ";
}
switch (node->type_) {
case xml_element::element_node:
out += "Node: ";
out += node->name_;
break;
case xml_element::element_text:
out += "Text";
break;
case xml_element::element_html:
out += "HTML";
break;
default:
out += "Unknown node type";
break;
}
out += "\n";
for (xml_element* it = node->children(); it; it = it->next()) {
write_xml_tree_impl(out, it, depth + 1);
}
}
// xml_parse
void read_tag(
xml_tree_builder&,
string_iterator& it,
string_iterator start,
string_iterator end);
void read_close_tag(
xml_tree_builder&,
string_iterator& it,
string_iterator start,
string_iterator end);
void skip_question_mark_tag(
string_iterator& it, string_iterator start, string_iterator end);
void skip_exclamation_mark_tag(
string_iterator& it, string_iterator start, string_iterator end);
quickbook::string_view read_tag_name(
string_iterator& it, string_iterator start, string_iterator end);
quickbook::string_view read_attribute_value(
string_iterator& it, string_iterator start, string_iterator end);
quickbook::string_view read_string(
string_iterator& it, string_iterator end);
xml_tree xml_parse(quickbook::string_view source)
{
typedef string_iterator iterator;
iterator it = source.begin(), end = source.end();
xml_tree_builder builder;
while (true) {
iterator start = it;
read_to(it, end, '<');
if (start != it) {
builder.add_element(xml_element::text_node(
quickbook::string_view(start, it - start)));
}
if (it == end) {
break;
}
start = it++;
if (it == end) {
throw xml_parse_error("Invalid tag", start);
}
switch (*it) {
case '?':
skip_question_mark_tag(it, start, end);
break;
case '!':
skip_exclamation_mark_tag(it, start, end);
break;
case '/':
read_close_tag(builder, it, start, end);
break;
default:
read_tag(builder, it, start, end);
break;
}
}
return builder.release();
}
void read_tag(
xml_tree_builder& builder,
string_iterator& it,
string_iterator start,
string_iterator end)
{
assert(it == start + 1 && it != end);
quickbook::string_view name = read_tag_name(it, start, end);
xml_element* node = xml_element::node(name);
builder.add_element(node);
// Read attributes
while (true) {
read_some_of(it, end, " \t\n\r");
if (it == end) {
throw xml_parse_error("Invalid tag", start);
}
if (*it == '>') {
++it;
builder.start_children();
break;
}
if (*it == '/') {
++it;
read_some_of(it, end, " \t\n\r");
if (it == end || *it != '>') {
throw xml_parse_error("Invalid tag", start);
}
++it;
break;
}
quickbook::string_view attribute_name =
read_tag_name(it, start, end);
read_some_of(it, end, " \t\n\r");
if (it == end) {
throw xml_parse_error("Invalid tag", start);
}
quickbook::string_view attribute_value;
if (*it == '=') {
++it;
attribute_value = read_attribute_value(it, start, end);
}
node->set_attribute(
attribute_name,
quickbook::detail::decode_string(attribute_value));
}
}
void read_close_tag(
xml_tree_builder& builder,
string_iterator& it,
string_iterator start,
string_iterator end)
{
assert(it == start + 1 && it != end && *it == '/');
++it;
quickbook::string_view name = read_tag_name(it, start, end);
read_some_of(it, end, " \t\n\r");
if (it == end || *it != '>') {
throw xml_parse_error("Invalid close tag", start);
}
++it;
if (!builder.parent() || builder.parent()->name_ != name) {
throw xml_parse_error("Close tag doesn't match", start);
}
builder.end_children();
}
void skip_question_mark_tag(
string_iterator& it, string_iterator start, string_iterator end)
{
assert(it == start + 1 && it != end && *it == '?');
++it;
while (true) {
read_to_one_of(it, end, "\"'?<>");
if (it == end) {
throw xml_parse_error("Invalid tag", start);
}
switch (*it) {
case '"':
case '\'':
read_string(it, end);
break;
case '?':
if (read(it, end, "?>")) {
return;
}
else {
++it;
}
break;
default:
throw xml_parse_error("Invalid tag", start);
}
}
}
void skip_exclamation_mark_tag(
string_iterator& it, string_iterator start, string_iterator end)
{
assert(it == start + 1 && it != end && *it == '!');
++it;
if (read(it, end, "--")) {
if (read_past(it, end, "-->")) {
return;
}
else {
throw xml_parse_error("Invalid comment", start);
}
}
while (true) {
read_to_one_of(it, end, "\"'<>");
if (it == end) {
throw xml_parse_error("Invalid tag", start);
}
switch (*it) {
case '"':
case '\'':
read_string(it, end);
break;
case '>':
++it;
return;
default:
throw xml_parse_error("Invalid tag", start);
}
}
}
quickbook::string_view read_tag_name(
string_iterator& it, string_iterator start, string_iterator end)
{
read_some_of(it, end, " \t\n\r");
string_iterator name_start = it;
read_some_of(
it, end,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:-");
if (name_start == it) {
throw xml_parse_error("Invalid tag", start);
}
return quickbook::string_view(name_start, it - name_start);
}
quickbook::string_view read_attribute_value(
string_iterator& it, string_iterator start, string_iterator end)
{
read_some_of(it, end, " \t\n\r");
if (*it == '"' || *it == '\'') {
return read_string(it, end);
}
else {
throw xml_parse_error("Invalid tag", start);
}
}
quickbook::string_view read_string(
string_iterator& it, string_iterator end)
{
assert(it != end && (*it == '"' || *it == '\''));
string_iterator start = it;
char deliminator = *it;
++it;
read_to(it, end, deliminator);
if (it == end) {
throw xml_parse_error("Invalid string", start);
}
++it;
return quickbook::string_view(start + 1, it - start - 2);
}
}
}
|