29#include <QtCore/private/qglobal_p.h>
30#include <qxmlstream.h>
33#include <qstringconverter.h>
37#ifndef QXMLSTREAMPARSER_P_H
38#define QXMLSTREAMPARSER_P_H
42#if QT_CONFIG(xmlstreamreader)
44bool QXmlStreamReaderPrivate::parse()
48 using namespace Qt::StringLiterals;
51 case QXmlStreamReader::StartElement:
54 qualifiedName.clear();
56 publicNamespaceDeclarations.clear();
59 setType(QXmlStreamReader::EndElement);
60 Tag tag = tagStack_pop();
61 namespaceUri = tag.namespaceDeclaration.namespaceUri;
62 prefix = tag.namespaceDeclaration.prefix;
64 qualifiedName = tag.qualifiedName;
65 isEmptyElement =
false;
70 case QXmlStreamReader::EndElement:
73 qualifiedName.clear();
77 case QXmlStreamReader::DTD:
78 publicNotationDeclarations.clear();
79 publicEntityDeclarations.clear();
83 dtdInternalSubsetValue.clear();
85 case QXmlStreamReader::Comment:
86 case QXmlStreamReader::Characters:
92 case QXmlStreamReader::EntityReference:
97 case QXmlStreamReader::ProcessingInstruction:
98 processingInstructionTarget.clear();
99 processingInstructionData.clear();
102 case QXmlStreamReader::NoToken:
103 case QXmlStreamReader::Invalid:
105 case QXmlStreamReader::StartDocument:
107 documentVersion.clear();
108 documentEncoding.clear();
109 if (decoder.isValid() && decoder.hasError()) {
110 raiseWellFormedError(QXmlStream::tr(
"Encountered incorrectly encoded content."));
115 case QXmlStreamReader::EndDocument:
119 setType(QXmlStreamReader::NoToken);
125 if (resumeReduction) {
126 act = state_stack[tos-1];
129 goto ResumeReduction;
132 act = state_stack[tos];
135 if (token == -1 && - TERMINAL_COUNT != action_index[act]) {
138 token_char = cu == ~0U ? cu : ushort(cu);
139 if ((cu != ~0U) && (cu & 0xff0000)) {
141 }
else switch (token_char) {
149 if ((token_char = filterCarriageReturn())) {
151 lastLineStart = characterOffset + readBufferPos;
160 if (!tagsDone && !inParseEntity) {
161 int a = t_action(act, token);
163 raiseError(QXmlStreamReader::PrematureEndOfDocumentError);
171 lastLineStart = characterOffset + readBufferPos;
241 token = QUESTIONMARK;
267 act = t_action (act, token);
268 if (act == ACCEPT_STATE) {
271 state_stack[tos++] = 0;
272 state_stack[tos] = 0;
274 }
else if (act > 0) {
275 if (tos + 1 >= stack_size - 1)
279 Value &val = sym_stack[tos];
281 val.pos = textBuffer.size();
285 textBuffer += QChar(token_char);
287 state_stack[tos] = act;
291 }
else if (act < 0) {
294#if defined (QLALR_DEBUG)
295 int ridx = rule_index[r];
296 printf (
"%3d) %s ::=", r + 1, spell[rule_info[ridx]]);
298 for (
int i = ridx; i < ridx + rhs[r]; ++i) {
299 int symbol = rule_info[i];
300 if (
const char *name = spell[symbol])
301 printf (
" %s", name);
303 printf (
" #%d", symbol);
309 act = state_stack[tos++];
314 setType(QXmlStreamReader::EndDocument);
318 if (type != QXmlStreamReader::Invalid) {
319 if (hasSeenTag || inParseEntity) {
320 setType(QXmlStreamReader::EndDocument);
322 raiseError(QXmlStreamReader::NotWellFormedError, QXmlStream::tr(
"Start tag expected."));
325 state_stack[tos++] = 0;
326 state_stack[tos] = 0;
333 auto reference = entityReferenceStack.pop();
334 auto it = reference.hash->find(reference.name);
335 Q_ASSERT(it != reference.hash->end());
336 it->isCurrentlyReferenced =
false;
337 if (entityReferenceStack.isEmpty())
343 if (!scanString(spell[VERSION], VERSION,
false) && atEnd) {
350 setType(QXmlStreamReader::StartDocument);
351 documentVersion = symString(6);
356 hasExternalDtdSubset =
true;
357 dtdSystemId = symString(2);
361 checkPublicLiteral(symString(2));
362 dtdPublicId = symString(2);
363 dtdSystemId = symString(4);
364 hasExternalDtdSubset =
true;
368 if (!scanPublicOrSystem() && atEnd) {
372 dtdName = symString(3);
377 dtdName = symString(3);
382 setType(QXmlStreamReader::DTD);
393 Q_ASSERT(sym(1).len == 1);
394 const auto start = sym(1).pos + 1;
395 const auto end = sym(3).pos;
396 dtdInternalSubsetValue = XmlStringRef(&textBuffer, start, end - start);
401 if (!scanString(spell[EMPTY], EMPTY,
false)
402 && !scanString(spell[ANY], ANY,
false)
410 if (!scanString(spell[PCDATA], PCDATA,
false) && atEnd) {
417 lastAttributeIsCData =
true;
421 if (!scanAfterDefaultDecl() && atEnd) {
429 lastAttributeValue.clear();
430 lastAttributeIsCData =
false;
431 if (!scanAttType() && atEnd) {
438 DtdAttribute &dtdAttribute = dtdAttributes.push();
439 dtdAttribute.tagName.clear();
440 dtdAttribute.isCDATA = lastAttributeIsCData;
441 auto symbol = symQName(1);
442 symbol.resetBackingStorage(addToStringStorage(symbol.all()));
443 dtdAttribute.attributePrefix = symbol.prefix();
444 dtdAttribute.attributeName = symbol.localPart();
445 dtdAttribute.attributeQualifiedName = symbol.qname();
446 dtdAttribute.isNamespaceAttribute = (dtdAttribute.attributePrefix ==
"xmlns"_L1
447 || (dtdAttribute.attributePrefix.isEmpty()
448 && dtdAttribute.attributeName ==
"xmlns"_L1));
449 if (lastAttributeValue.isNull()) {
450 dtdAttribute.defaultValue.clear();
452 if (dtdAttribute.isCDATA)
453 dtdAttribute.defaultValue = addToStringStorage(lastAttributeValue);
455 dtdAttribute.defaultValue = addToStringStorage(lastAttributeValue.toString().simplified());
461 if (referenceToUnparsedEntityDetected && !standalone)
463 qsizetype n = dtdAttributes.size();
464 XmlStringRef tagName = addToStringStorage(symName(3));
466 DtdAttribute &dtdAttribute = dtdAttributes[n];
467 if (!dtdAttribute.tagName.isNull())
469 dtdAttribute.tagName = tagName;
470 for (qsizetype i = 0; i < n; ++i) {
471 if ((dtdAttributes[i].tagName.isNull() || dtdAttributes[i].tagName == tagName)
472 && dtdAttributes[i].attributeQualifiedName == dtdAttribute.attributeQualifiedName) {
473 dtdAttribute.attributeQualifiedName.clear();
481 if (!scanPublicOrSystem() && atEnd) {
485 EntityDeclaration &entityDeclaration = entityDeclarations.push();
486 entityDeclaration.clear();
487 entityDeclaration.name = symString(3);
491 if (!scanPublicOrSystem() && atEnd) {
495 EntityDeclaration &entityDeclaration = entityDeclarations.push();
496 entityDeclaration.clear();
497 entityDeclaration.name = symString(5);
498 entityDeclaration.parameter =
true;
502 if (!scanNData() && atEnd) {
506 EntityDeclaration &entityDeclaration = entityDeclarations.top();
507 entityDeclaration.systemId = symString(3);
508 entityDeclaration.external =
true;
512 if (!scanNData() && atEnd) {
516 EntityDeclaration &entityDeclaration = entityDeclarations.top();
517 checkPublicLiteral((entityDeclaration.publicId = symString(3)));
518 entityDeclaration.systemId = symString(5);
519 entityDeclaration.external =
true;
523 EntityDeclaration &entityDeclaration = entityDeclarations.top();
524 entityDeclaration.notationName = symString(3);
525 if (entityDeclaration.parameter)
526 raiseWellFormedError(QXmlStream::tr(
"NDATA in parameter entity declaration."));
532 if (referenceToUnparsedEntityDetected && !standalone) {
533 entityDeclarations.pop();
536 EntityDeclaration &entityDeclaration = entityDeclarations.top();
537 if (!entityDeclaration.external)
538 entityDeclaration.value = symString(2);
539 auto &hash = entityDeclaration.parameter ? parameterEntityHash : entityHash;
540 if (!hash.contains(entityDeclaration.name)) {
541 Entity entity(entityDeclaration.name.toString(),
542 entityDeclaration.value.toString());
543 entity.unparsed = (!entityDeclaration.notationName.isNull());
544 entity.external = entityDeclaration.external;
545 hash.insert(qToStringViewIgnoringNull(entity.name), entity);
550 setType(QXmlStreamReader::ProcessingInstruction);
551 const qsizetype pos = sym(4).pos + sym(4).len;
552 processingInstructionTarget = symString(3);
553 if (scanUntil(
"?>")) {
554 processingInstructionData = XmlStringRef(&textBuffer, pos, textBuffer.size() - pos - 2);
555 if (!processingInstructionTarget.view().compare(
"xml"_L1, Qt::CaseInsensitive)) {
556 raiseWellFormedError(QXmlStream::tr(
"XML declaration not at start of document."));
558 else if (!QXmlUtils::isNCName(processingInstructionTarget))
559 raiseWellFormedError(QXmlStream::tr(
"%1 is an invalid processing instruction name.")
560 .arg(processingInstructionTarget));
561 }
else if (type != QXmlStreamReader::Invalid){
568 setType(QXmlStreamReader::ProcessingInstruction);
569 processingInstructionTarget = symString(3);
570 if (!processingInstructionTarget.view().compare(
"xml"_L1, Qt::CaseInsensitive))
571 raiseWellFormedError(QXmlStream::tr(
"Invalid processing instruction name."));
575 if (!scanAfterLangleBang() && atEnd) {
582 if (!scanUntil(
"--")) {
589 setType(QXmlStreamReader::Comment);
590 const qsizetype pos = sym(1).pos + 4;
591 text = XmlStringRef(&textBuffer, pos, textBuffer.size() - pos - 3);
595 setType(QXmlStreamReader::Characters);
597 isWhitespace =
false;
598 const qsizetype pos = sym(2).pos;
599 if (scanUntil(
"]]>", -1)) {
600 text = XmlStringRef(&textBuffer, pos, textBuffer.size() - pos - 3);
608 if (!scanPublicOrSystem() && atEnd) {
612 NotationDeclaration ¬ationDeclaration = notationDeclarations.push();
613 notationDeclaration.name = symString(3);
617 NotationDeclaration ¬ationDeclaration = notationDeclarations.top();
618 notationDeclaration.systemId = symString(3);
619 notationDeclaration.publicId.clear();
623 NotationDeclaration ¬ationDeclaration = notationDeclarations.top();
624 notationDeclaration.systemId.clear();
625 checkPublicLiteral((notationDeclaration.publicId = symString(3)));
629 NotationDeclaration ¬ationDeclaration = notationDeclarations.top();
630 checkPublicLiteral((notationDeclaration.publicId = symString(3)));
631 notationDeclaration.systemId = symString(5);
635 isWhitespace =
false;
639 sym(1).len += fastScanContentCharList();
640 if (atEnd && !inParseEntity) {
647 if (!textBuffer.isEmpty()) {
648 setType(QXmlStreamReader::Characters);
667 sym(1).len += sym(2).len;
671 if (normalizeLiterals)
672 textBuffer.data()[textBuffer.size()-1] = u' ';
676 sym(1).len += fastScanLiteralContent();
684 if (!QXmlUtils::isPublicID(symString(1))) {
685 raiseWellFormedError(QXmlStream::tr(
"%1 is an invalid PUBLIC identifier.").arg(symString(1)));
705 sym(1).len += sym(2).len;
716 lastAttributeValue = symString(1);
723 sym(1).len += sym(2).len;
727 const XmlStringRef prfx = symPrefix(1);
728 if (prfx.isEmpty() && symString(1) ==
"xmlns"_L1 && namespaceProcessing) {
729 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push();
730 namespaceDeclaration.prefix.clear();
732 const XmlStringRef ns(symString(5));
733 if (ns.view() ==
"http://www.w3.org/2000/xmlns/"_L1 ||
734 ns.view() ==
"http://www.w3.org/XML/1998/namespace"_L1)
735 raiseWellFormedError(QXmlStream::tr(
"Illegal namespace declaration."));
737 namespaceDeclaration.namespaceUri = addToStringStorage(ns);
739 Attribute &attribute = attributeStack.push();
740 attribute.key = sym(1);
741 attribute.value = sym(5);
743 XmlStringRef attributeQualifiedName = symName(1);
744 bool normalize =
false;
745 for (
const DtdAttribute &dtdAttribute : std::as_const(dtdAttributes)) {
746 if (!dtdAttribute.isCDATA
747 && dtdAttribute.tagName == qualifiedName
748 && dtdAttribute.attributeQualifiedName == attributeQualifiedName
756 const qsizetype pos = textBuffer.size();
758 bool wasSpace =
true;
759 for (qsizetype i = 0; i < attribute.value.len; ++i) {
760 QChar c = textBuffer.at(attribute.value.pos + i);
761 if (c.unicode() ==
' ') {
768 textBuffer += textBuffer.at(attribute.value.pos + i);
772 while (n && textBuffer.at(pos + n - 1).unicode() ==
' ')
774 attribute.value.pos = pos;
775 attribute.value.len = n;
777 if (prfx ==
"xmlns"_L1 && namespaceProcessing) {
778 NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.push();
779 XmlStringRef namespacePrefix = symString(attribute.key);
780 XmlStringRef namespaceUri = symString(attribute.value);
781 attributeStack.pop();
782 if (((namespacePrefix ==
"xml"_L1)
783 ^ (namespaceUri ==
"http://www.w3.org/XML/1998/namespace"_L1))
784 || namespaceUri ==
"http://www.w3.org/2000/xmlns/"_L1
785 || namespaceUri.isEmpty()
786 || namespacePrefix ==
"xmlns"_L1)
787 raiseWellFormedError(QXmlStream::tr(
"Illegal namespace declaration."));
789 namespaceDeclaration.prefix = addToStringStorage(namespacePrefix);
790 namespaceDeclaration.namespaceUri = addToStringStorage(namespaceUri);
796 normalizeLiterals =
true;
797 Tag &tag = tagStack_push();
798 auto symbol = symQName(2);
799 symbol.resetBackingStorage(addToStringStorage(symbol.all()));
800 prefix = tag.namespaceDeclaration.prefix = symbol.prefix();
801 name = tag.name = symbol.localPart();
802 qualifiedName = tag.qualifiedName = symbol.qname();
803 if ((!prefix.isEmpty() && !QXmlUtils::isNCName(prefix)) || !QXmlUtils::isNCName(name))
804 raiseWellFormedError(QXmlStream::tr(
"Invalid XML name."));
808 isEmptyElement =
true;
812 setType(QXmlStreamReader::StartElement);
814 if (tagStack.size() == 1 && hasSeenTag && !inParseEntity)
815 raiseWellFormedError(QXmlStream::tr(
"Extra content at end of document."));
820 setType(QXmlStreamReader::EndElement);
821 Tag tag = tagStack_pop();
823 namespaceUri = tag.namespaceDeclaration.namespaceUri;
824 prefix = tag.namespaceDeclaration.prefix;
826 qualifiedName = tag.qualifiedName;
827 if (qualifiedName != symName(3))
828 raiseWellFormedError(QXmlStream::tr(
"Opening and ending tag mismatch."));
832 if (entitiesMustBeDeclared()) {
833 raiseWellFormedError(QXmlStream::tr(
"Entity '%1' not declared.").arg(unresolvedEntity));
836 setType(QXmlStreamReader::EntityReference);
837 name = &unresolvedEntity;
841 sym(1).len += sym(2).len + 1;
842 QStringView reference = symView(2);
843 if (
const auto it = entityHash.find(reference); it != entityHash.end()) {
844 Entity &entity = *it;
845 if (entity.unparsed) {
846 raiseWellFormedError(QXmlStream::tr(
"Reference to unparsed entity '%1'.").arg(reference));
848 if (!entity.hasBeenParsed) {
849 parseEntity(entity.value);
850 entity.hasBeenParsed =
true;
853 putStringLiteral(entity.value);
854 else if (referenceEntity(&entityHash, entity))
855 putReplacement(entity.value);
856 textBuffer.chop(2 + sym(2).len);
862 if (entityResolver) {
863 QString replacementText = resolveUndeclaredEntity(reference.toString());
864 if (!replacementText.isNull()) {
865 putReplacement(replacementText);
866 textBuffer.chop(2 + sym(2).len);
872 injectToken(UNRESOLVED_ENTITY);
873 unresolvedEntity = symString(2).toString();
874 textBuffer.chop(2 + sym(2).len);
880 sym(1).len += sym(2).len + 1;
881 QStringView reference = symView(2);
882 if (
const auto it = parameterEntityHash.find(reference); it != parameterEntityHash.end()) {
883 referenceToParameterEntityDetected =
true;
884 Entity &entity = *it;
885 if (entity.unparsed || entity.external) {
886 referenceToUnparsedEntityDetected =
true;
888 if (referenceEntity(¶meterEntityHash, entity))
889 putString(entity.value);
890 textBuffer.chop(2 + sym(2).len);
893 }
else if (entitiesMustBeDeclared()) {
894 raiseWellFormedError(QXmlStream::tr(
"Entity '%1' not declared.").arg(symString(2)));
899 sym(1).len += sym(2).len + 1;
903 sym(1).len += sym(2).len + 1;
904 QStringView reference = symView(2);
905 if (
const auto it = entityHash.find(reference); it != entityHash.end()) {
906 Entity &entity = *it;
907 if (entity.unparsed || entity.value.isNull()) {
908 raiseWellFormedError(QXmlStream::tr(
"Reference to external entity '%1' in attribute value.").arg(reference));
911 if (!entity.hasBeenParsed) {
912 parseEntity(entity.value);
913 entity.hasBeenParsed =
true;
916 putStringLiteral(entity.value);
917 else if (referenceEntity(&entityHash, entity))
918 putReplacementInAttributeValue(entity.value);
919 textBuffer.chop(2 + sym(2).len);
924 if (entityResolver) {
925 QString replacementText = resolveUndeclaredEntity(reference.toString());
926 if (!replacementText.isNull()) {
927 putReplacement(replacementText);
928 textBuffer.chop(2 + sym(2).len);
933 if (entitiesMustBeDeclared()) {
934 raiseWellFormedError(QXmlStream::tr(
"Entity '%1' not declared.").arg(reference));
939 if (
char32_t s = resolveCharRef(3)) {
940 putStringLiteral(QChar::fromUcs4(s));
941 textBuffer.chop(3 + sym(3).len);
944 raiseWellFormedError(QXmlStream::tr(
"Invalid character reference."));
950 sym(1).len += sym(2).len;
954 sym(1).len += fastScanSpace();
963 if (
auto res = fastScanName(&val))
975 if (
auto res = fastScanName())
991 sym(1).len += fastScanNMTOKEN();
1002 act = state_stack[tos] = nt_action (act, lhs[r] - TERMINAL_COUNT);
1003 if (type != QXmlStreamReader::NoToken)
Combined button and popup list for selecting options.