6#include <QtCore/qglobal.h>
7#include <QtCore/qstringlist.h>
9#include <private/qqmljsast_p.h>
10#include <private/qqmljsengine_p.h>
14QmlMarkupVisitor::QmlMarkupVisitor(
const QString &source,
15 const QList<QQmlJS::SourceLocation> &pragmas,
16 QQmlJS::Engine *engine)
18 this->m_source = source;
19 this->m_engine = engine;
27 const QList<QQmlJS::SourceLocation> comments = engine->comments();
28 while (i < comments.size() && j < pragmas.size()) {
29 if (comments[i].offset < pragmas[j].offset) {
30 m_extraTypes.append(Comment);
31 m_extraLocations.append(comments[i]);
34 m_extraTypes.append(Pragma);
35 m_extraLocations.append(comments[j]);
40 while (i < comments.size()) {
41 m_extraTypes.append(Comment);
42 m_extraLocations.append(comments[i]);
46 while (j < pragmas.size()) {
47 m_extraTypes.append(Pragma);
48 m_extraLocations.append(pragmas[j]);
62 qsizetype n = str.size();
64 marked.reserve(n * 2 + 30);
65 const QChar *data = str.constData();
66 for (
int i = 0; i != n; ++i) {
67 switch (data[i].unicode()) {
89 if (
int(m_cursor) < m_source.size())
90 addExtra(m_cursor, m_source.size());
97 return m_hasRecursionDepthError;
102 if (m_extraIndex >= m_extraLocations.size()) {
103 QString extra = m_source.mid(start, finish - start);
104 if (extra.trimmed().isEmpty())
107 m_output += protect(extra);
113 while (m_extraIndex < m_extraLocations.size()) {
114 if (m_extraTypes[m_extraIndex] == Comment) {
115 if (m_extraLocations[m_extraIndex].offset - 2 >= start)
118 if (m_extraLocations[m_extraIndex].offset >= start)
125 while (i < finish && m_extraIndex < m_extraLocations.size()) {
126 quint32 j = m_extraLocations[m_extraIndex].offset - 2;
127 if (i <= j && j < finish) {
129 m_output += protect(m_source.mid(i, j - i));
131 quint32 l = m_extraLocations[m_extraIndex].length;
132 if (m_extraTypes[m_extraIndex] == Comment) {
133 if (m_source.mid(j, 2) == QLatin1String(
"/*"))
137 m_output += QLatin1String(
"<@comment>");
138 m_output += protect(m_source.mid(j, l));
139 m_output += QLatin1String(
"</@comment>");
141 m_output += protect(m_source.mid(j, l));
149 QString extra = m_source.mid(i, finish - i);
150 if (extra.trimmed().isEmpty())
153 m_output += protect(extra);
159 const QString &tagName,
160 const QHash<QString, QString> &attributes)
162 if (!location.isValid())
165 if (m_cursor < location.offset)
166 addExtra(m_cursor, location.offset);
167 else if (m_cursor > location.offset)
170 m_output += QString(QLatin1String(
"<@%1")).arg(tagName);
171 for (
const auto &key : attributes)
172 m_output += QString(QLatin1String(
" %1=\"%2\"")).arg(key, attributes[key]);
173 m_output += QString(QLatin1String(
">%2</@%3>")).arg(protect(sourceText(location)), tagName);
174 m_cursor += location.length;
179 return m_source.mid(location.offset, location.length);
183 QQmlJS::SourceLocation last)
185 if (!first.isValid())
188 quint32 start = first.begin();
193 finish = first.end();
195 if (m_cursor < start)
196 addExtra(m_cursor, start);
197 else if (m_cursor > start)
200 QString text = m_source.mid(start, finish - start);
201 m_output += protect(text);
207 addVerbatim(uiimport->importToken);
208 if (!uiimport->importUri)
209 addMarkedUpToken(uiimport->fileNameToken, QLatin1String(
"headerfile"));
215 if (uiimport->version)
216 addVerbatim(uiimport->version->firstSourceLocation(),
217 uiimport->version->lastSourceLocation());
218 addVerbatim(uiimport->asToken);
219 addMarkedUpToken(uiimport->importIdToken, QLatin1String(
"headerfile"));
220 addVerbatim(uiimport->semicolonToken);
225 if (member->type == QQmlJS::AST::UiPublicMember::Property) {
226 addVerbatim(member->defaultToken());
227 addVerbatim(member->readonlyToken());
228 addVerbatim(member->propertyToken());
229 addVerbatim(member->typeModifierToken);
230 addMarkedUpToken(member->typeToken, QLatin1String(
"type"));
231 addMarkedUpToken(member->identifierToken, QLatin1String(
"name"));
232 addVerbatim(member->colonToken);
234 QQmlJS::AST::Node::accept(member->binding,
this);
235 else if (member->statement)
236 QQmlJS::AST::Node::accept(member->statement,
this);
238 addVerbatim(member->propertyToken());
239 addVerbatim(member->typeModifierToken);
240 addMarkedUpToken(member->typeToken, QLatin1String(
"type"));
242 QQmlJS::AST::Node::accept(member->parameters,
this);
244 addVerbatim(member->semicolonToken);
250 addVerbatim(initializer->lbraceToken, initializer->lbraceToken);
256 addVerbatim(initializer->rbraceToken, initializer->rbraceToken);
261 QQmlJS::AST::Node::accept(binding->qualifiedId,
this);
262 addVerbatim(binding->colonToken);
263 QQmlJS::AST::Node::accept(binding->qualifiedTypeNameId,
this);
264 QQmlJS::AST::Node::accept(binding->initializer,
this);
270 QQmlJS::AST::Node::accept(binding->qualifiedId,
this);
271 addVerbatim(binding->colonToken);
272 QQmlJS::AST::Node::accept(binding->statement,
this);
278 QQmlJS::AST::Node::accept(binding->qualifiedId,
this);
279 addVerbatim(binding->colonToken);
280 addVerbatim(binding->lbracketToken);
281 QQmlJS::AST::Node::accept(binding->members,
this);
282 addVerbatim(binding->rbracketToken);
288 for (QQmlJS::AST::UiArrayMemberList *it = list; it; it = it->next) {
289 QQmlJS::AST::Node::accept(it->member,
this);
297 addMarkedUpToken(id->identifierToken, QLatin1String(
"name"));
303 addVerbatim(expression->thisToken);
309 addMarkedUpToken(identifier->identifierToken, QLatin1String(
"name"));
315 addMarkedUpToken(null->nullToken, QLatin1String(
"number"));
321 addMarkedUpToken(literal->trueToken, QLatin1String(
"number"));
327 addMarkedUpToken(literal->falseToken, QLatin1String(
"number"));
333 addMarkedUpToken(literal->literalToken, QLatin1String(
"number"));
339 addMarkedUpToken(literal->literalToken, QLatin1String(
"string"));
345 addVerbatim(literal->literalToken);
351 addVerbatim(literal->lbracketToken);
352 QQmlJS::AST::Node::accept(literal->elements,
this);
353 addVerbatim(literal->rbracketToken);
359 addVerbatim(literal->lbraceToken);
365 addVerbatim(literal->rbraceToken);
370 for (QQmlJS::AST::PatternElementList *it = list; it; it = it->next) {
371 QQmlJS::AST::Node::accept(it->element,
this);
374 QQmlJS::AST::Node::accept(list->elision,
this);
380 addVerbatim(elision->commaToken, elision->commaToken);
386 QQmlJS::AST::Node::accept(list->name,
this);
387 addVerbatim(list->colonToken, list->colonToken);
388 QQmlJS::AST::Node::accept(list->initializer,
this);
395 QQmlJS::AST::Node::accept(expression->base,
this);
396 addVerbatim(expression->lbracketToken);
397 QQmlJS::AST::Node::accept(expression->expression,
this);
398 addVerbatim(expression->rbracketToken);
404 QQmlJS::AST::Node::accept(expression->base,
this);
405 addVerbatim(expression->dotToken);
406 addMarkedUpToken(expression->identifierToken, QLatin1String(
"name"));
412 addVerbatim(expression->newToken);
413 QQmlJS::AST::Node::accept(expression->base,
this);
414 addVerbatim(expression->lparenToken);
415 QQmlJS::AST::Node::accept(expression->arguments,
this);
416 addVerbatim(expression->rparenToken);
422 addVerbatim(expression->newToken);
428 addVerbatim(list->commaToken, list->commaToken);
434 addVerbatim(expression->incrementToken);
440 addVerbatim(expression->decrementToken);
446 addVerbatim(expression->deleteToken);
452 addVerbatim(expression->voidToken);
458 addVerbatim(expression->typeofToken);
464 addVerbatim(expression->incrementToken);
470 addVerbatim(expression->decrementToken);
476 addVerbatim(expression->plusToken);
482 addVerbatim(expression->minusToken);
488 addVerbatim(expression->tildeToken);
494 addVerbatim(expression->notToken);
500 QQmlJS::AST::Node::accept(expression->left,
this);
501 addMarkedUpToken(expression->operatorToken, QLatin1String(
"op"));
502 QQmlJS::AST::Node::accept(expression->right,
this);
508 QQmlJS::AST::Node::accept(expression->expression,
this);
509 addVerbatim(expression->questionToken);
510 QQmlJS::AST::Node::accept(expression->ok,
this);
511 addVerbatim(expression->colonToken);
512 QQmlJS::AST::Node::accept(expression->ko,
this);
518 QQmlJS::AST::Node::accept(expression->left,
this);
519 addVerbatim(expression->commaToken);
520 QQmlJS::AST::Node::accept(expression->right,
this);
526 addVerbatim(block->lbraceToken);
532 addVerbatim(block->rbraceToken);
537 addVerbatim(statement->declarationKindToken);
538 QQmlJS::AST::Node::accept(statement->declarations,
this);
545 for (QQmlJS::AST::VariableDeclarationList *it = list; it; it = it->next) {
546 QQmlJS::AST::Node::accept(it->declaration,
this);
547 addVerbatim(it->commaToken);
554 addVerbatim(statement->semicolonToken);
560 QQmlJS::AST::Node::accept(statement->expression,
this);
561 addVerbatim(statement->semicolonToken);
567 addMarkedUpToken(statement->ifToken, QLatin1String(
"keyword"));
568 addVerbatim(statement->lparenToken);
569 QQmlJS::AST::Node::accept(statement->expression,
this);
570 addVerbatim(statement->rparenToken);
571 QQmlJS::AST::Node::accept(statement->ok,
this);
573 addMarkedUpToken(statement->elseToken, QLatin1String(
"keyword"));
574 QQmlJS::AST::Node::accept(statement->ko,
this);
581 addMarkedUpToken(statement->doToken, QLatin1String(
"keyword"));
582 QQmlJS::AST::Node::accept(statement->statement,
this);
583 addMarkedUpToken(statement->whileToken, QLatin1String(
"keyword"));
584 addVerbatim(statement->lparenToken);
585 QQmlJS::AST::Node::accept(statement->expression,
this);
586 addVerbatim(statement->rparenToken);
587 addVerbatim(statement->semicolonToken);
593 addMarkedUpToken(statement->whileToken, QLatin1String(
"keyword"));
594 addVerbatim(statement->lparenToken);
595 QQmlJS::AST::Node::accept(statement->expression,
this);
596 addVerbatim(statement->rparenToken);
597 QQmlJS::AST::Node::accept(statement->statement,
this);
603 addMarkedUpToken(statement->forToken, QLatin1String(
"keyword"));
604 addVerbatim(statement->lparenToken);
605 QQmlJS::AST::Node::accept(statement->initialiser,
this);
606 addVerbatim(statement->firstSemicolonToken);
607 QQmlJS::AST::Node::accept(statement->condition,
this);
608 addVerbatim(statement->secondSemicolonToken);
609 QQmlJS::AST::Node::accept(statement->expression,
this);
610 addVerbatim(statement->rparenToken);
611 QQmlJS::AST::Node::accept(statement->statement,
this);
617 addMarkedUpToken(statement->forToken, QLatin1String(
"keyword"));
618 addVerbatim(statement->lparenToken);
619 QQmlJS::AST::Node::accept(statement->lhs,
this);
620 addVerbatim(statement->inOfToken);
621 QQmlJS::AST::Node::accept(statement->expression,
this);
622 addVerbatim(statement->rparenToken);
623 QQmlJS::AST::Node::accept(statement->statement,
this);
629 addMarkedUpToken(statement->continueToken, QLatin1String(
"keyword"));
630 addMarkedUpToken(statement->identifierToken, QLatin1String(
"name"));
631 addVerbatim(statement->semicolonToken);
637 addMarkedUpToken(statement->breakToken, QLatin1String(
"keyword"));
638 addMarkedUpToken(statement->identifierToken, QLatin1String(
"name"));
639 addVerbatim(statement->semicolonToken);
645 addMarkedUpToken(statement->returnToken, QLatin1String(
"keyword"));
646 QQmlJS::AST::Node::accept(statement->expression,
this);
647 addVerbatim(statement->semicolonToken);
653 addMarkedUpToken(statement->withToken, QLatin1String(
"keyword"));
654 addVerbatim(statement->lparenToken);
655 QQmlJS::AST::Node::accept(statement->expression,
this);
656 addVerbatim(statement->rparenToken);
657 QQmlJS::AST::Node::accept(statement->statement,
this);
663 addVerbatim(block->lbraceToken);
669 addVerbatim(block->rbraceToken, block->rbraceToken);
674 addMarkedUpToken(statement->switchToken, QLatin1String(
"keyword"));
675 addVerbatim(statement->lparenToken);
676 QQmlJS::AST::Node::accept(statement->expression,
this);
677 addVerbatim(statement->rparenToken);
678 QQmlJS::AST::Node::accept(statement->block,
this);
684 addMarkedUpToken(clause->caseToken, QLatin1String(
"keyword"));
685 QQmlJS::AST::Node::accept(clause->expression,
this);
686 addVerbatim(clause->colonToken);
687 QQmlJS::AST::Node::accept(clause->statements,
this);
693 addMarkedUpToken(clause->defaultToken, QLatin1String(
"keyword"));
694 addVerbatim(clause->colonToken, clause->colonToken);
700 addMarkedUpToken(statement->identifierToken, QLatin1String(
"name"));
701 addVerbatim(statement->colonToken);
702 QQmlJS::AST::Node::accept(statement->statement,
this);
708 addMarkedUpToken(statement->throwToken, QLatin1String(
"keyword"));
709 QQmlJS::AST::Node::accept(statement->expression,
this);
710 addVerbatim(statement->semicolonToken);
716 addMarkedUpToken(c->catchToken, QLatin1String(
"keyword"));
717 addVerbatim(c->lparenToken);
718 addMarkedUpToken(c->identifierToken, QLatin1String(
"name"));
719 addVerbatim(c->rparenToken);
725 addMarkedUpToken(f->finallyToken, QLatin1String(
"keyword"));
726 QQmlJS::AST::Node::accept(f->statement,
this);
732 addMarkedUpToken(statement->tryToken, QLatin1String(
"keyword"));
733 QQmlJS::AST::Node::accept(statement->statement,
this);
734 QQmlJS::AST::Node::accept(statement->catchExpression,
this);
735 QQmlJS::AST::Node::accept(statement->finallyExpression,
this);
741 addMarkedUpToken(expression->functionToken, QLatin1String(
"keyword"));
742 addMarkedUpToken(expression->identifierToken, QLatin1String(
"name"));
743 addVerbatim(expression->lparenToken);
744 QQmlJS::AST::Node::accept(expression->formals,
this);
745 addVerbatim(expression->rparenToken);
746 addVerbatim(expression->lbraceToken);
747 QQmlJS::AST::Node::accept(expression->body,
this);
748 addVerbatim(expression->rbraceToken);
754 addMarkedUpToken(declaration->functionToken, QLatin1String(
"keyword"));
755 addMarkedUpToken(declaration->identifierToken, QLatin1String(
"name"));
756 addVerbatim(declaration->lparenToken);
757 QQmlJS::AST::Node::accept(declaration->formals,
this);
758 addVerbatim(declaration->rparenToken);
759 addVerbatim(declaration->lbraceToken);
760 QQmlJS::AST::Node::accept(declaration->body,
this);
761 addVerbatim(declaration->rbraceToken);
768 QQmlJS::AST::Node::accept(list->element,
this);
775 addVerbatim(statement->debuggerToken);
776 addVerbatim(statement->semicolonToken);
784 addMarkedUpToken(definition->qualifiedTypeNameId->identifierToken, QLatin1String(
"type"));
785 QQmlJS::AST::Node::accept(definition->initializer,
this);
791 m_hasRecursionDepthError =
true;
QString protect(const QString &string)
void throwRecursionDepthError() final
void endVisit(QQmlJS::AST::UiImport *) override
bool visit(QQmlJS::AST::UiImport *) override
Combined button and popup list for selecting options.
static const QString samp
static const QString squot