From 84e8fd1af6fbc655176a0b49fd9e9ef78362e29f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 1 Feb 2024 03:02:54 +0300 Subject: [PATCH] fixes --- src/coders/xml.cpp | 31 ++++++++++++++++++++++++++++--- src/coders/xml.h | 1 + 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/coders/xml.cpp b/src/coders/xml.cpp index b88f01b0..8613ceee 100644 --- a/src/coders/xml.cpp +++ b/src/coders/xml.cpp @@ -115,7 +115,7 @@ Parser::Parser(std::string filename, std::string source) } xmlelement Parser::parseOpenTag() { - std::string tag = parseName(); + std::string tag = parseXMLName(); auto node = std::make_shared(tag); char c; @@ -124,7 +124,7 @@ xmlelement Parser::parseOpenTag() { c = peek(); if (c == '/' || c == '>' || c == '?') break; - std::string attrname = parseName(); + std::string attrname = parseXMLName(); std::string attrtext = ""; skipWhitespace(); if (peek() == '=') { @@ -181,6 +181,26 @@ std::string Parser::parseText() { return source.substr(start, pos-start); } +inline bool is_xml_identifier_start(char c) { + return is_identifier_start(c) || c == ':'; +} + +inline bool is_xml_identifier_part(char c) { + return is_identifier_part(c) || c == '-' || c == '.' || c == ':'; +} + +std::string Parser::parseXMLName() { + char c = peek(); + if (!is_xml_identifier_start(c)) { + throw error("identifier expected"); + } + int start = pos; + while (hasNext() && is_xml_identifier_part(source[pos])) { + pos++; + } + return source.substr(start, pos-start); +} + xmlelement Parser::parseElement() { // text element if (peek() != '<') { @@ -235,7 +255,12 @@ xmlelement Parser::parseElement() { xmldocument Parser::parse() { parseDeclaration(); - document->setRoot(parseElement()); + + xmlelement root = nullptr; + while (root == nullptr) { + root = parseElement(); + } + document->setRoot(root); return document; } diff --git a/src/coders/xml.h b/src/coders/xml.h index 29c5a919..c198889c 100644 --- a/src/coders/xml.h +++ b/src/coders/xml.h @@ -105,6 +105,7 @@ namespace xml { void parseDeclaration(); void parseComment(); std::string parseText(); + std::string parseXMLName(); public: Parser(std::string filename, std::string source);