This commit is contained in:
MihailRis 2024-02-01 03:02:54 +03:00
parent da190f3f7b
commit 84e8fd1af6
2 changed files with 29 additions and 3 deletions

View File

@ -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<Node>(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;
}

View File

@ -105,6 +105,7 @@ namespace xml {
void parseDeclaration();
void parseComment();
std::string parseText();
std::string parseXMLName();
public:
Parser(std::string filename, std::string source);