| e8555c89 Andy McCall |
1 |
#ifndef RAPIDSAX_HH |
| 2 |
#define RAPIDSAX_HH |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "one_of.hh" |
| 17 |
#include "small_compare.hh" |
| 18 |
#include "zstring_view.hh" |
| 19 |
|
| 20 |
#include <array> |
| 21 |
#include <cassert> |
| 22 |
#include <cstdint> |
| 23 |
#include <string_view> |
| 24 |
|
| 25 |
namespace rapidsax { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
template<int FLAGS, typename HANDLER> void parse(HANDLER& handler, char* xml); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
inline constexpr size_t EXTRA_BUFFER_SPACE = 8; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
inline constexpr int noEntityTranslation = 0x1; |
| 51 |
|
| 52 |
inline constexpr int trimWhitespace = 0x2; |
| 53 |
|
| 54 |
|
| 55 |
inline constexpr int normalizeWhitespace = 0x4; |
| 56 |
|
| 57 |
inline constexpr int zeroTerminateStrings = 0x8; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
class NullHandler |
| 67 |
{ |
| 68 |
public: |
| 69 |
|
| 70 |
|
| 71 |
void start(std::string_view ) {} |
| 72 |
void start(zstring_view ) {} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void stop() {} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void text(std::string_view ) {} |
| 87 |
void text(zstring_view ) {} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void attribute(std::string_view , std::string_view ) {} |
| 92 |
void attribute(zstring_view , zstring_view ) {} |
| 93 |
|
| 94 |
|
| 95 |
void cdata(std::string_view ) {} |
| 96 |
void cdata(zstring_view ) {} |
| 97 |
|
| 98 |
|
| 99 |
void comment(std::string_view ) {} |
| 100 |
void comment(zstring_view ) {} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
void declarationStart() {} |
| 105 |
void declAttribute(std::string_view , std::string_view ) {} |
| 106 |
void declAttribute(zstring_view , zstring_view ) {} |
| 107 |
void declarationStop() {} |
| 108 |
|
| 109 |
|
| 110 |
void doctype(std::string_view ) {} |
| 111 |
void doctype(zstring_view ) {} |
| 112 |
|
| 113 |
|
| 114 |
void procInstr(std::string_view , std::string_view ) {} |
| 115 |
void procInstr(zstring_view , zstring_view ) {} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
class ParseError |
| 120 |
{ |
| 121 |
public: |
| 122 |
ParseError(const char* what_, char* where_) |
| 123 |
: m_what(what_) |
| 124 |
, m_where(where_) |
| 125 |
{ |
| 126 |
} |
| 127 |
|
| 128 |
[[nodiscard]] const char* what() const { return m_what; } |
| 129 |
[[nodiscard]] char* where() const { return m_where; } |
| 130 |
|
| 131 |
private: |
| 132 |
const char* m_what; |
| 133 |
char* m_where; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
namespace internal { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
inline constexpr std::array<const uint8_t, 256> lutChar = |
| 149 |
{ |
| 150 |
|
| 151 |
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x02,0x00,0x00, |
| 152 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 153 |
0x02,0x80,0x20,0x00,0x00,0x00,0x08,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40, |
| 154 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x80,0x40,0x40, |
| 155 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 156 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 157 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 158 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 159 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 160 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 161 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 162 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 163 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 164 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 165 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 166 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
inline constexpr std::array<const uint8_t, 256> lutDigits = |
| 171 |
{ |
| 172 |
|
| 173 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 174 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 175 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 176 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, |
| 177 |
255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, |
| 178 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 179 |
255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, |
| 180 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 181 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 182 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 183 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 184 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 185 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 186 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 187 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
| 188 |
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
struct WhitespacePred { |
| 193 |
[[nodiscard]] static bool test(char ch) { return (lutChar[uint8_t(ch)] & 0x02) != 0; } |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
struct NodeNamePred { |
| 198 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x43); } |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
struct AttributeNamePred { |
| 203 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0xC7); } |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
struct TextPred { |
| 208 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x05); } |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
struct TextPureNoWsPred { |
| 214 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x0D); } |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
struct TextPureWithWsPred { |
| 220 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x0F); } |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
struct AttPred1 { |
| 225 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x11); } |
| 226 |
}; |
| 227 |
|
| 228 |
struct AttPred2 { |
| 229 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x21); } |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
struct AttPurePred1 { |
| 235 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x19); } |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
struct AttPurePred2 { |
| 240 |
[[nodiscard]] static bool test(char ch) { return !(lutChar[uint8_t(ch)] & 0x29); } |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
inline void insertUTF8char(char*& text, uint32_t code) |
| 245 |
{ |
| 246 |
if (code < 0x80) { |
| 247 |
text[0] = char(code); |
| 248 |
text += 1; |
| 249 |
} else if (code < 0x800) { |
| 250 |
text[1] = char((code | 0x80) & 0xBF); code >>= 6; |
| 251 |
text[0] = char (code | 0xC0); |
| 252 |
text += 2; |
| 253 |
} else if (code < 0x10000) { |
| 254 |
text[2] = char((code | 0x80) & 0xBF); code >>= 6; |
| 255 |
text[1] = char((code | 0x80) & 0xBF); code >>= 6; |
| 256 |
text[0] = char (code | 0xE0); |
| 257 |
text += 3; |
| 258 |
} else if (code < 0x110000) { |
| 259 |
text[3] = char((code | 0x80) & 0xBF); code >>= 6; |
| 260 |
text[2] = char((code | 0x80) & 0xBF); code >>= 6; |
| 261 |
text[1] = char((code | 0x80) & 0xBF); code >>= 6; |
| 262 |
text[0] = char (code | 0xF0); |
| 263 |
text += 4; |
| 264 |
} else { |
| 265 |
throw ParseError("invalid numeric character entity", text); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
template<StringLiteral Str> [[nodiscard]] static inline bool next(const char* p) |
| 270 |
{ |
| 271 |
return small_compare<Str>(p); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
template<typename StopPred> static inline void skip(char*& text) |
| 277 |
{ |
| 278 |
char* tmp = text; |
| 279 |
while (StopPred::test(*tmp)) ++tmp; |
| 280 |
text = tmp; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
template<typename StopPred, class StopPredPure, int FLAGS> |
| 288 |
[[nodiscard]] static inline char* skipAndExpand(char*& text) |
| 289 |
{ |
| 290 |
|
| 291 |
|
| 292 |
if constexpr ( (FLAGS & noEntityTranslation) && |
| 293 |
!(FLAGS & normalizeWhitespace) && |
| 294 |
!(FLAGS & trimWhitespace)) { |
| 295 |
skip<StopPred>(text); |
| 296 |
return text; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
skip<StopPredPure>(text); |
| 301 |
|
| 302 |
|
| 303 |
char* src = text; |
| 304 |
char* dest = src; |
| 305 |
while (StopPred::test(*src)) { |
| 306 |
|
| 307 |
if (!(FLAGS & noEntityTranslation) && |
| 308 |
(src[0] == '&')) { |
| 309 |
switch (src[1]) { |
| 310 |
case 'a': |
| 311 |
if (next<"amp;">(&src[1])) { |
| 312 |
*dest = '&'; |
| 313 |
++dest; |
| 314 |
src += 5; |
| 315 |
continue; |
| 316 |
} |
| 317 |
if (next<"pos;">(&src[2])) { |
| 318 |
*dest = '\''; |
| 319 |
++dest; |
| 320 |
src += 6; |
| 321 |
continue; |
| 322 |
} |
| 323 |
break; |
| 324 |
|
| 325 |
case 'q': |
| 326 |
if (next<"uot;">(&src[2])) { |
| 327 |
*dest = '"'; |
| 328 |
++dest; |
| 329 |
src += 6; |
| 330 |
continue; |
| 331 |
} |
| 332 |
break; |
| 333 |
|
| 334 |
case 'g': |
| 335 |
if (next<"t;">(&src[2])) { |
| 336 |
*dest = '>'; |
| 337 |
++dest; |
| 338 |
src += 4; |
| 339 |
continue; |
| 340 |
} |
| 341 |
break; |
| 342 |
|
| 343 |
case 'l': |
| 344 |
if (next<"t;">(&src[2])) { |
| 345 |
*dest = '<'; |
| 346 |
++dest; |
| 347 |
src += 4; |
| 348 |
continue; |
| 349 |
} |
| 350 |
break; |
| 351 |
|
| 352 |
case '#': |
| 353 |
if (src[2] == 'x') { |
| 354 |
uint32_t code = 0; |
| 355 |
src += 3; |
| 356 |
while (true) { |
| 357 |
uint8_t digit = lutDigits[uint8_t(*src)]; |
| 358 |
if (digit == 0xFF) break; |
| 359 |
code = code * 16 + digit; |
| 360 |
++src; |
| 361 |
} |
| 362 |
insertUTF8char(dest, code); |
| 363 |
} else { |
| 364 |
uint32_t code = 0; |
| 365 |
src += 2; |
| 366 |
while (true) { |
| 367 |
uint8_t digit = lutDigits[uint8_t(*src)]; |
| 368 |
if (digit == 0xFF) break; |
| 369 |
code = code * 10 + digit; |
| 370 |
++src; |
| 371 |
} |
| 372 |
insertUTF8char(dest, code); |
| 373 |
} |
| 374 |
if (*src != ';') { |
| 375 |
throw ParseError("expected ;", src); |
| 376 |
} |
| 377 |
++src; |
| 378 |
continue; |
| 379 |
|
| 380 |
default: |
| 381 |
|
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
if ((FLAGS & normalizeWhitespace) && |
| 388 |
(WhitespacePred::test(*src))) { |
| 389 |
*dest++ = ' '; |
| 390 |
++src; |
| 391 |
|
| 392 |
while (WhitespacePred::test(*src)) ++src; |
| 393 |
continue; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
*dest++ = *src++; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
text = src; |
| 402 |
return dest; |
| 403 |
} |
| 404 |
|
| 405 |
inline void skipBOM(char*& text) |
| 406 |
{ |
| 407 |
if (next<"\357\273\277">(text)) { |
| 408 |
text += 3; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
template<int FLAGS, typename HANDLER> class Parser |
| 414 |
{ |
| 415 |
HANDLER& handler; |
| 416 |
|
| 417 |
public: |
| 418 |
Parser(HANDLER& handler_, char* text) |
| 419 |
: handler(handler_) |
| 420 |
{ |
| 421 |
skipBOM(text); |
| 422 |
while (true) { |
| 423 |
|
| 424 |
skip<WhitespacePred>(text); |
| 425 |
if (*text == 0) break; |
| 426 |
|
| 427 |
if (*text != '<') { |
| 428 |
throw ParseError("expected <", text); |
| 429 |
} |
| 430 |
++text; |
| 431 |
parseNode(text); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
private: |
| 436 |
|
| 437 |
void parseDeclaration(char*& text) |
| 438 |
{ |
| 439 |
handler.declarationStart(); |
| 440 |
skip<WhitespacePred>(text); |
| 441 |
parseAttributes(text, true); |
| 442 |
handler.declarationStop(); |
| 443 |
|
| 444 |
|
| 445 |
if (!next<"?>">(text)) { |
| 446 |
throw ParseError("expected ?>", text); |
| 447 |
} |
| 448 |
text += 2; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
void parseComment(char*& text) |
| 453 |
{ |
| 454 |
|
| 455 |
const char* value = text; |
| 456 |
while (!next<"-->">(text)) { |
| 457 |
if (text[0] == 0) { |
| 458 |
throw ParseError("unexpected end of data", text); |
| 459 |
} |
| 460 |
++text; |
| 461 |
} |
| 462 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 463 |
*text = '\0'; |
| 464 |
handler.comment(zstring_view(value, text - value)); |
| 465 |
} else { |
| 466 |
handler.comment(std::string_view(value, text - value)); |
| 467 |
} |
| 468 |
text += 3; |
| 469 |
} |
| 470 |
|
| 471 |
void parseDoctype(char*& text) |
| 472 |
{ |
| 473 |
const char* value = text; |
| 474 |
|
| 475 |
|
| 476 |
while (*text != '>') { |
| 477 |
switch (*text) { |
| 478 |
case '[': { |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
++text; |
| 484 |
int depth = 1; |
| 485 |
while (depth > 0) { |
| 486 |
switch (*text) { |
| 487 |
case '[': ++depth; break; |
| 488 |
case ']': --depth; break; |
| 489 |
case 0: throw ParseError( |
| 490 |
"unexpected end of data", text); |
| 491 |
} |
| 492 |
++text; |
| 493 |
} |
| 494 |
break; |
| 495 |
} |
| 496 |
case '\0': |
| 497 |
throw ParseError("unexpected end of data", text); |
| 498 |
|
| 499 |
default: |
| 500 |
++text; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 505 |
*text = '\0'; |
| 506 |
handler.doctype(zstring_view(value, text - value)); |
| 507 |
} else { |
| 508 |
handler.doctype(std::string_view(value, text - value)); |
| 509 |
} |
| 510 |
text += 1; |
| 511 |
} |
| 512 |
|
| 513 |
void parsePI(char*& text) |
| 514 |
{ |
| 515 |
|
| 516 |
const char* name = text; |
| 517 |
skip<NodeNamePred>(text); |
| 518 |
char* nameEnd = text; |
| 519 |
if (name == nameEnd) { |
| 520 |
throw ParseError("expected PI target", text); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
skip<WhitespacePred>(text); |
| 525 |
|
| 526 |
|
| 527 |
const char* value = text; |
| 528 |
while (!next<"?>">(text)) { |
| 529 |
if (*text == 0) { |
| 530 |
throw ParseError("unexpected end of data", text); |
| 531 |
} |
| 532 |
++text; |
| 533 |
} |
| 534 |
|
| 535 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 536 |
*nameEnd = '\0'; |
| 537 |
*text = '\0'; |
| 538 |
handler.procInstr(zstring_view(name, nameEnd - name), |
| 539 |
zstring_view(value, text - value)); |
| 540 |
} else { |
| 541 |
handler.procInstr(std::string_view(name, nameEnd - name), |
| 542 |
std::string_view(value, text - value)); |
| 543 |
} |
| 544 |
text += 2; |
| 545 |
} |
| 546 |
|
| 547 |
void parseText(char*& text, char* contentsStart) |
| 548 |
{ |
| 549 |
|
| 550 |
if constexpr (!(FLAGS & trimWhitespace)) { |
| 551 |
text = contentsStart; |
| 552 |
} |
| 553 |
|
| 554 |
const char* value = text; |
| 555 |
char* end = (FLAGS & normalizeWhitespace) |
| 556 |
? skipAndExpand<TextPred, TextPureWithWsPred, FLAGS>(text) |
| 557 |
: skipAndExpand<TextPred, TextPureNoWsPred , FLAGS>(text); |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
if constexpr ((FLAGS & trimWhitespace) != 0) { |
| 562 |
if constexpr (FLAGS & normalizeWhitespace) { |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
if (end[-1] == ' ') { |
| 567 |
--end; |
| 568 |
} |
| 569 |
} else { |
| 570 |
|
| 571 |
while (WhitespacePred::test(end[-1])) { |
| 572 |
--end; |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
if (*text == '\0') { |
| 579 |
throw ParseError("unexpected end of data", text); |
| 580 |
} else { |
| 581 |
assert(*text == '<'); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
auto len = end - value; |
| 586 |
if (len) { |
| 587 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 588 |
*end = '\0'; |
| 589 |
handler.text(zstring_view(value, len)); |
| 590 |
} else { |
| 591 |
handler.text(std::string_view(value, len)); |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
void parseCdata(char*& text) |
| 597 |
{ |
| 598 |
|
| 599 |
const char* value = text; |
| 600 |
while (!next<"]]>">(text)) { |
| 601 |
if (text[0] == 0) { |
| 602 |
throw ParseError("unexpected end of data", text); |
| 603 |
} |
| 604 |
++text; |
| 605 |
} |
| 606 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 607 |
*text = '\0'; |
| 608 |
handler.cdata(zstring_view(value, text - value)); |
| 609 |
} else { |
| 610 |
handler.cdata(std::string_view(value, text - value)); |
| 611 |
} |
| 612 |
text += 3; |
| 613 |
} |
| 614 |
|
| 615 |
void parseElement(char*& text) |
| 616 |
{ |
| 617 |
|
| 618 |
const char* name = text; |
| 619 |
skip<NodeNamePred>(text); |
| 620 |
char* nameEnd = text; |
| 621 |
if (name == nameEnd) { |
| 622 |
throw ParseError("expected element name", text); |
| 623 |
} |
| 624 |
char savedChar = *nameEnd; |
| 625 |
skip<WhitespacePred>(text); |
| 626 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 627 |
*nameEnd = '\0'; |
| 628 |
handler.start(zstring_view(name, nameEnd - name)); |
| 629 |
} else { |
| 630 |
handler.start(std::string_view(name, nameEnd - name)); |
| 631 |
} |
| 632 |
|
| 633 |
parseAttributes(text, false); |
| 634 |
|
| 635 |
|
| 636 |
char endChar = ((FLAGS & zeroTerminateStrings) && (text == nameEnd)) |
| 637 |
? savedChar : *text; |
| 638 |
if (endChar == '>') { |
| 639 |
++text; |
| 640 |
parseNodeContents(text); |
| 641 |
} else if (endChar == '/') { |
| 642 |
handler.stop(); |
| 643 |
++text; |
| 644 |
if (*text != '>') { |
| 645 |
throw ParseError("expected >", text); |
| 646 |
} |
| 647 |
++text; |
| 648 |
} else { |
| 649 |
throw ParseError("expected >", text); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
void parseNode(char*& text) |
| 655 |
{ |
| 656 |
switch (text[0]) { |
| 657 |
case '?': |
| 658 |
++text; |
| 659 |
|
| 660 |
|
| 661 |
if ((next<"xml">(text) || next<"XML">(text)) && |
| 662 |
WhitespacePred::test(text[3])) { |
| 663 |
|
| 664 |
text += 4; |
| 665 |
parseDeclaration(text); |
| 666 |
} else { |
| 667 |
parsePI(text); |
| 668 |
} |
| 669 |
break; |
| 670 |
|
| 671 |
case '!': |
| 672 |
|
| 673 |
switch (text[1]) { |
| 674 |
case '-': |
| 675 |
if (text[2] == '-') { |
| 676 |
|
| 677 |
text += 3; |
| 678 |
parseComment(text); |
| 679 |
return; |
| 680 |
} |
| 681 |
break; |
| 682 |
|
| 683 |
case '[': |
| 684 |
if (next<"CDATA[">(&text[2])) { |
| 685 |
|
| 686 |
text += 8; |
| 687 |
parseCdata(text); |
| 688 |
return; |
| 689 |
} |
| 690 |
break; |
| 691 |
|
| 692 |
case 'D': |
| 693 |
if (next<"OCTYPE">(&text[2]) && |
| 694 |
WhitespacePred::test(text[8])) { |
| 695 |
|
| 696 |
text += 9; |
| 697 |
parseDoctype(text); |
| 698 |
return; |
| 699 |
} |
| 700 |
break; |
| 701 |
} |
| 702 |
|
| 703 |
++text; |
| 704 |
while (*text != '>') { |
| 705 |
if (*text == 0) { |
| 706 |
throw ParseError( |
| 707 |
"unexpected end of data", text); |
| 708 |
} |
| 709 |
++text; |
| 710 |
} |
| 711 |
++text; |
| 712 |
break; |
| 713 |
|
| 714 |
default: |
| 715 |
parseElement(text); |
| 716 |
break; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
void parseNodeContents(char*& text) |
| 722 |
{ |
| 723 |
while (true) { |
| 724 |
char* contentsStart = text; |
| 725 |
skip<WhitespacePred>(text); |
| 726 |
|
| 727 |
switch (*text) { |
| 728 |
case '<': |
| 729 |
afterText: |
| 730 |
|
| 731 |
if (text[1] == '/') { |
| 732 |
|
| 733 |
text += 2; |
| 734 |
skip<NodeNamePred>(text); |
| 735 |
|
| 736 |
handler.stop(); |
| 737 |
|
| 738 |
skip<WhitespacePred>(text); |
| 739 |
if (*text != '>') { |
| 740 |
throw ParseError("expected >", text); |
| 741 |
} |
| 742 |
++text; |
| 743 |
return; |
| 744 |
} else { |
| 745 |
|
| 746 |
++text; |
| 747 |
parseNode(text); |
| 748 |
} |
| 749 |
break; |
| 750 |
|
| 751 |
case '\0': |
| 752 |
throw ParseError("unexpected end of data", text); |
| 753 |
|
| 754 |
default: |
| 755 |
parseText(text, contentsStart); |
| 756 |
goto afterText; |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
void parseAttributes(char*& text, bool declaration) |
| 763 |
{ |
| 764 |
|
| 765 |
while (AttributeNamePred::test(*text)) { |
| 766 |
|
| 767 |
char* name = text; |
| 768 |
++text; |
| 769 |
skip<AttributeNamePred>(text); |
| 770 |
char* nameEnd = text; |
| 771 |
if (name == nameEnd) { |
| 772 |
throw ParseError("expected attribute name", name); |
| 773 |
} |
| 774 |
|
| 775 |
skip<WhitespacePred>(text); |
| 776 |
if (*text != '=') { |
| 777 |
throw ParseError("expected =", text); |
| 778 |
} |
| 779 |
++text; |
| 780 |
skip<WhitespacePred>(text); |
| 781 |
|
| 782 |
|
| 783 |
char quote = *text; |
| 784 |
if (quote != one_of('\'', '"')) { |
| 785 |
throw ParseError("expected ' or \"", text); |
| 786 |
} |
| 787 |
++text; |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
constexpr int FLAGS2 = FLAGS & ~normalizeWhitespace; |
| 792 |
const char* value = text; |
| 793 |
char* valueEnd = (quote == '\'') |
| 794 |
? skipAndExpand<AttPred1, AttPurePred1, FLAGS2>(text) |
| 795 |
: skipAndExpand<AttPred2, AttPurePred2, FLAGS2>(text); |
| 796 |
|
| 797 |
|
| 798 |
if (*text != quote) { |
| 799 |
throw ParseError("expected ' or \"", text); |
| 800 |
} |
| 801 |
++text; |
| 802 |
|
| 803 |
if constexpr (FLAGS & zeroTerminateStrings) { |
| 804 |
*nameEnd = '\0'; |
| 805 |
*valueEnd = '\0'; |
| 806 |
if (!declaration) { |
| 807 |
handler.attribute(zstring_view(name, nameEnd - name), |
| 808 |
zstring_view(value, valueEnd - value)); |
| 809 |
} else { |
| 810 |
handler.declAttribute(zstring_view(name, nameEnd - name), |
| 811 |
zstring_view(value, valueEnd - value)); |
| 812 |
} |
| 813 |
} else { |
| 814 |
if (!declaration) { |
| 815 |
handler.attribute(std::string_view(name, nameEnd - name), |
| 816 |
std::string_view(value, valueEnd - value)); |
| 817 |
} else { |
| 818 |
handler.declAttribute(std::string_view(name, nameEnd - name), |
| 819 |
std::string_view(value, valueEnd - value)); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
skip<WhitespacePred>(text); |
| 824 |
} |
| 825 |
} |
| 826 |
}; |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
template<int FLAGS, typename HANDLER> |
| 831 |
inline void parse(HANDLER& handler, char* xml) |
| 832 |
{ |
| 833 |
internal::Parser<FLAGS, HANDLER> parser(handler, xml); |
| 834 |
} |
| 835 |
|
| 836 |
} |
| 837 |
|
| 838 |
#endif |