Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
atom.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "atom.h"
5
6#include "genustypes.h"
7#include "qdocdatabase.h"
8
9#include <QtCore/qregularexpression.h>
10
11#include <cstdio>
12
14
15/*! \class Atom
16 \brief The Atom class is the fundamental unit for representing
17 documents internally.
18
19 Atoms have a \i type and are completed by a \i string whose
20 meaning depends on the \i type. For example, the string
21 \quotation
22 \i italic text looks nicer than \bold bold text
23 \endquotation
24 is represented by the following atoms:
25 \quotation
26 (FormattingLeft, ATOM_FORMATTING_ITALIC)
27 (String, "italic")
28 (FormattingRight, ATOM_FORMATTING_ITALIC)
29 (String, " text is more attractive than ")
30 (FormattingLeft, ATOM_FORMATTING_BOLD)
31 (String, "bold")
32 (FormattingRight, ATOM_FORMATTING_BOLD)
33 (String, " text")
34 \endquotation
35
36 \also Text
37*/
38
39/*! \enum Atom::AtomType
40
41 \value AnnotatedList
42 \value AutoLink
43 \value BaseName
44 \value BriefLeft
45 \value BriefRight
46 \value C
47 \value CaptionLeft
48 \value CaptionRight
49 \value Code
50 \value CodeBad
51 \value CodeQuoteArgument
52 \value CodeQuoteCommand
53 \value DetailsLeft
54 \value DetailsRight
55 \value DetailsSummaryLeft
56 \value DetailsSummaryRight
57 \value DivLeft
58 \value DivRight
59 \value ExampleFileLink
60 \value ExampleImageLink
61 \value FormatElse
62 \value FormatEndif
63 \value FormatIf
64 \value FootnoteLeft
65 \value FootnoteRight
66 \value FormattingLeft
67 \value FormattingRight
68 \value GeneratedList
69 \value Image
70 \value ImageText
71 \value ImportantNote
72 \value InlineImage
73 \value Keyword
74 \value LineBreak
75 \value Link
76 \value LinkNode
77 \value ListLeft
78 \value ListItemNumber
79 \value ListTagLeft
80 \value ListTagRight
81 \value ListItemLeft
82 \value ListItemRight
83 \value ListRight
84 \value NavAutoLink
85 \value NavLink
86 \value Nop
87 \value Note
88 \value ParaLeft
89 \value ParaRight
90 \value Qml
91 \value QuotationLeft
92 \value QuotationRight
93 \value RawString
94 \value SectionLeft
95 \value SectionRight
96 \value SectionHeadingLeft
97 \value SectionHeadingRight
98 \value SidebarLeft
99 \value SidebarRight
100 \value SinceList
101 \value SinceTagLeft
102 \value SinceTagRight
103 \value String
104 \value TableLeft
105 \value TableRight
106 \value TableHeaderLeft
107 \value TableHeaderRight
108 \value TableRowLeft
109 \value TableRowRight
110 \value TableItemLeft
111 \value TableItemRight
112 \value TableOfContentsLeft
113 \value TableOfContentsRight
114 \value Target
115 \value UnhandledFormat
116 \value UnknownCommand
117*/
118
119static const struct
120{
121 const char *english;
122 int no;
123} atms[] = { { "AnnotatedList", Atom::AnnotatedList },
124 { "AutoLink", Atom::AutoLink },
125 { "BaseName", Atom::BaseName },
126 { "br", Atom::BR },
127 { "BriefLeft", Atom::BriefLeft },
128 { "BriefRight", Atom::BriefRight },
129 { "C", Atom::C },
130 { "CaptionLeft", Atom::CaptionLeft },
131 { "CaptionRight", Atom::CaptionRight },
132 { "Code", Atom::Code },
133 { "CodeBad", Atom::CodeBad },
134 { "CodeQuoteArgument", Atom::CodeQuoteArgument },
135 { "CodeQuoteCommand", Atom::CodeQuoteCommand },
136 { "ComparesLeft", Atom::ComparesLeft },
137 { "ComparesRight", Atom::ComparesRight },
138 { "DetailsLeft", Atom::DetailsLeft },
139 { "DetailsRight", Atom::DetailsRight },
140 { "DetailsSummaryLeft", Atom::DetailsSummaryLeft },
141 { "DetailsSummaryRight", Atom::DetailsSummaryRight },
142 { "DivLeft", Atom::DivLeft },
143 { "DivRight", Atom::DivRight },
144 { "ExampleFileLink", Atom::ExampleFileLink },
145 { "ExampleImageLink", Atom::ExampleImageLink },
146 { "FootnoteLeft", Atom::FootnoteLeft },
147 { "FootnoteRight", Atom::FootnoteRight },
148 { "FormatElse", Atom::FormatElse },
149 { "FormatEndif", Atom::FormatEndif },
150 { "FormatIf", Atom::FormatIf },
151 { "FormattingLeft", Atom::FormattingLeft },
152 { "FormattingRight", Atom::FormattingRight },
153 { "GeneratedList", Atom::GeneratedList },
154 { "hr", Atom::HR },
155 { "Image", Atom::Image },
156 { "ImageText", Atom::ImageText },
157 { "ImportantLeft", Atom::ImportantLeft },
158 { "ImportantRight", Atom::ImportantRight },
159 { "InlineImage", Atom::InlineImage },
160 { "Keyword", Atom::Keyword },
161 { "LegaleseLeft", Atom::LegaleseLeft },
162 { "LegaleseRight", Atom::LegaleseRight },
163 { "LineBreak", Atom::LineBreak },
164 { "Link", Atom::Link },
165 { "LinkNode", Atom::LinkNode },
166 { "ListLeft", Atom::ListLeft },
167 { "ListItemNumber", Atom::ListItemNumber },
168 { "ListTagLeft", Atom::ListTagLeft },
169 { "ListTagRight", Atom::ListTagRight },
170 { "ListItemLeft", Atom::ListItemLeft },
171 { "ListItemRight", Atom::ListItemRight },
172 { "ListRight", Atom::ListRight },
173 { "NavAutoLink", Atom::NavAutoLink },
174 { "NavLink", Atom::NavLink },
175 { "Nop", Atom::Nop },
176 { "NoteLeft", Atom::NoteLeft },
177 { "NoteRight", Atom::NoteRight },
178 { "ParaLeft", Atom::ParaLeft },
179 { "ParaRight", Atom::ParaRight },
180 { "Qml", Atom::Qml },
181 { "QuotationLeft", Atom::QuotationLeft },
182 { "QuotationRight", Atom::QuotationRight },
183 { "RawString", Atom::RawString },
184 { "SectionLeft", Atom::SectionLeft },
185 { "SectionRight", Atom::SectionRight },
186 { "SectionHeadingLeft", Atom::SectionHeadingLeft },
187 { "SectionHeadingRight", Atom::SectionHeadingRight },
188 { "SidebarLeft", Atom::SidebarLeft },
189 { "SidebarRight", Atom::SidebarRight },
190 { "SinceList", Atom::SinceList },
191 { "SinceTagLeft", Atom::SinceTagLeft },
192 { "SinceTagRight", Atom::SinceTagRight },
193 { "SnippetCommand", Atom::SnippetCommand },
194 { "SnippetIdentifier", Atom::SnippetIdentifier },
195 { "SnippetLocation", Atom::SnippetLocation },
196 { "String", Atom::String },
197 { "TableLeft", Atom::TableLeft },
198 { "TableRight", Atom::TableRight },
199 { "TableHeaderLeft", Atom::TableHeaderLeft },
200 { "TableHeaderRight", Atom::TableHeaderRight },
201 { "TableRowLeft", Atom::TableRowLeft },
202 { "TableRowRight", Atom::TableRowRight },
203 { "TableItemLeft", Atom::TableItemLeft },
204 { "TableItemRight", Atom::TableItemRight },
205 { "TableOfContentsLeft", Atom::TableOfContentsLeft },
206 { "TableOfContentsRight", Atom::TableOfContentsRight },
207 { "Target", Atom::Target },
208 { "TitleLeft", Atom::TitleLeft },
209 { "TitleRight", Atom::TitleRight },
210 { "UnhandledFormat", Atom::UnhandledFormat },
211 { "WarningLeft", Atom::WarningLeft },
212 { "WarningRight", Atom::WarningRight },
213 { "UnknownCommand", Atom::UnknownCommand },
214 { nullptr, 0 } };
215
216/*! \fn Atom::Atom(AtomType type, const QString &string)
217
218 Constructs an atom of the specified \a type with the single
219 parameter \a string and does not put the new atom in a list.
220*/
221
222/*! \fn Atom::Atom(AtomType type, const QString &p1, const QString &p2)
223
224 Constructs an atom of the specified \a type with the two
225 parameters \a p1 and \a p2 and does not put the new atom
226 in a list.
227*/
228
229/*! \fn Atom(Atom *previous, AtomType type, const QString &string)
230
231 Constructs an atom of the specified \a type with the single
232 parameter \a string and inserts the new atom into the list
233 after the \a previous atom.
234*/
235
236/*! \fn Atom::Atom(Atom *previous, AtomType type, const QString &p1, const QString &p2)
237
238 Constructs an atom of the specified \a type with the two
239 parameters \a p1 and \a p2 and inserts the new atom into
240 the list after the \a previous atom.
241*/
242
243/*! \fn void Atom::appendChar(QChar ch)
244
245 Appends \a ch to the string parameter of this atom.
246
247 \also string()
248*/
249
250/*! \fn void Atom::concatenateString(const QString &string)
251
252 Appends \a string to the string parameter of this atom.
253
254 \also string()
255*/
256
257/*! \fn void Atom::chopString()
258
259 \also string()
260*/
261
262/*!
263 Starting from this Atom, searches the linked list for the
264 atom of specified type \a t and returns it. Returns \nullptr
265 if no such atom is found.
266
267 If an atom is found and \a offset is not \nullptr, it's set
268 to the number of links traversed from this atom to the found
269 atom (0 if this atom matches). \a offset is untouched when
270 not found.
271*/
272const Atom *Atom::find(AtomType t, qsizetype *offset) const
273{
274 qsizetype i{0};
275 const auto *a{this};
276 while (a && a->type() != t) {
277 a = a->next();
278 ++i;
279 }
280 if (a && offset)
281 *offset = i;
282 return a;
283}
284
285/*!
286 Starting from this Atom, searches the linked list for the
287 atom of specified type \a t and string \a s, and returns it.
288 Returns \nullptr if no such atom is found.
289*/
290const Atom *Atom::find(AtomType t, const QString &s) const
291{
292 const auto *a{this};
293 while (a && (a->type() != t || a->string() != s))
294 a = a->next();
295 return a;
296}
297
298/*! \fn Atom *Atom::next()
299 Return the next atom in the atom list.
300 \also type(), string()
301*/
302
303/*!
304 Return the next Atom in the list if it is of AtomType \a t.
305 Otherwise return 0.
306 */
307const Atom *Atom::next(AtomType t) const
308{
309 return (m_next && (m_next->type() == t)) ? m_next : nullptr;
310}
311
312/*!
313 Return the next Atom in the list if it is of AtomType \a t
314 and its string part is \a s. Otherwise return 0.
315 */
316const Atom *Atom::next(AtomType t, const QString &s) const
317{
318 return (m_next && (m_next->type() == t) && (m_next->string() == s)) ? m_next : nullptr;
319}
320
321/*! \fn const Atom *Atom::next() const
322 Return the next atom in the atom list.
323 \also type(), string()
324*/
325
326/*! \fn AtomType Atom::type() const
327 Return the type of this atom.
328 \also string(), next()
329*/
330
331/*!
332 Return the type of this atom as a string. Return "Invalid" if
333 type() returns an impossible value.
334
335 This is only useful for debugging.
336
337 \also type()
338*/
340{
341 static bool deja = false;
342
343 if (!deja) {
344 int i = 0;
345 while (atms[i].english != nullptr) {
346 if (atms[i].no != i)
347 Location::internalError(QStringLiteral("QDoc::Atom: atom %1 missing").arg(i));
348 ++i;
349 }
350 deja = true;
351 }
352
353 int i = static_cast<int>(type());
354 if (i < 0 || i > static_cast<int>(Last))
355 return QLatin1String("Invalid");
356 return QLatin1String(atms[i].english);
357}
358
359/*! \fn const QString &Atom::string() const
360
361 Returns the string parameter that together with the type
362 characterizes this atom.
363
364 \also type(), next()
365*/
366
367/*!
368 For a link atom, returns the string representing the link text
369 if one exist in the list of atoms.
370*/
372{
373 Q_ASSERT(m_type == Atom::Link);
374 QString result;
375
376 if (next() && next()->string() == ATOM_FORMATTING_LINK) {
377 auto *atom = next()->next();
378 while (atom && atom->type() != Atom::FormattingRight) {
379 result += atom->string();
380 atom = atom->next();
381 }
382 return result;
383 }
384
385 return string();
386}
387
388/*!
389 Constructs a LinkAtom of type Atom::Link. \a p1 is the link target.
390 \a p2 holds the parameters that appeared in square brackets. Normally
391 there is just one word in the square brackets, but there can be up
392 to three words separated by spaces. The constructor splits \a p2 on
393 the space character. \a location identifies where in the source
394 the link directive was written, so downstream consumers can attribute
395 diagnostics back to the originating line.
396 */
397LinkAtom::LinkAtom(const QString &p1, const QString &p2, Location location)
398 : Atom(Atom::Link, p1),
400 m_genus(Genus::DontCare),
401 m_domain(nullptr)
402{
403 resolveSquareBracketParams(p2);
404}
405
406/*!
407 Constructs a LinkAtom carrying an explicit atom \a type. This overload
408 exists so DocParser can promote autolink atoms (Atom::AutoLink) to
409 LinkAtom while preserving the type tag that downstream generators
410 dispatch on. \a p1, \a p2, and \a location follow the same semantics
411 as the primary constructor.
412 */
413LinkAtom::LinkAtom(Atom::AtomType type, const QString &p1, const QString &p2,
414 Location location)
415 : Atom(type, p1),
417 m_genus(Genus::DontCare),
418 m_domain(nullptr)
419{
420 resolveSquareBracketParams(p2);
421}
422
423/*!
424 Resolves the parameters that were enclosed in square brackets, supplied as
425 \a text, setting the domain and genus to appropriate values.
426 */
427void LinkAtom::resolveSquareBracketParams(const QString &text)
428{
429 m_squareBracketParams = text.toLower().split(QLatin1Char(' '));
430 for (const auto &param : m_squareBracketParams) {
431 if (!m_domain) {
432 m_domain = QDocDatabase::qdocDB()->findTree(param);
433 if (m_domain) {
434 continue;
435 }
436 }
437
438 if (param == "qml") {
439 m_genus = Genus::QML;
440 continue;
441 }
442 if (param == "cpp") {
443 m_genus = Genus::CPP;
444 continue;
445 }
446 if (param == "doc") {
447 m_genus = Genus::DOC;
448 continue;
449 }
450 if (param == "api") {
451 m_genus = Genus::API;
452 continue;
453 }
454 break;
455 }
456}
457
458/*!
459 Standard copy constructor of LinkAtom \a t.
460 */
462 : Atom(t.m_type, t.string()),
467{
468 // nothing
469}
470
471/*!
472 Special copy constructor of LinkAtom \a t, where
473 where the new LinkAtom will not be the first one
474 in the list.
475 */
476LinkAtom::LinkAtom(Atom *previous, const LinkAtom &t)
477 : Atom(previous, t.m_type, t.string()),
482{
483 previous->m_next = this;
484}
485
486int LinkAtom::flags() const
487{
488 int flags = 0;
489 if (m_squareBracketParams.contains("attached"))
490 flags |= QmlAttachedProperties;
491
492 return flags;
493}
494
495QT_END_NAMESPACE
int no
Definition atom.cpp:122
const char * english
Definition atom.cpp:121
#define ATOM_FORMATTING_LINK
Definition atom.h:209
The Atom class is the fundamental unit for representing documents internally.
Definition atom.h:19
AtomType type() const
Return the type of this atom.
Definition atom.h:155
AtomType
\value AnnotatedList \value AutoLink \value BaseName \value BriefLeft \value BriefRight \value C \val...
Definition atom.h:21
@ CaptionLeft
Definition atom.h:29
@ ListTagLeft
Definition atom.h:67
@ TableRight
Definition atom.h:97
@ DivRight
Definition atom.h:42
@ GeneratedList
Definition atom.h:52
@ BriefRight
Definition atom.h:27
@ CodeQuoteArgument
Definition atom.h:33
@ WarningLeft
Definition atom.h:110
@ SinceList
Definition atom.h:89
@ TableOfContentsLeft
Definition atom.h:104
@ SidebarLeft
Definition atom.h:87
@ TableOfContentsRight
Definition atom.h:105
@ Keyword
Definition atom.h:59
@ TableHeaderRight
Definition atom.h:99
@ FormatElse
Definition atom.h:47
@ InlineImage
Definition atom.h:58
@ TableRowRight
Definition atom.h:101
@ FootnoteRight
Definition atom.h:46
@ LineBreak
Definition atom.h:62
@ SnippetCommand
Definition atom.h:92
@ TableRowLeft
Definition atom.h:100
@ Nop
Definition atom.h:74
@ WarningRight
Definition atom.h:111
@ LegaleseRight
Definition atom.h:61
@ ListTagRight
Definition atom.h:68
@ CaptionRight
Definition atom.h:30
@ NavLink
Definition atom.h:73
@ ListItemNumber
Definition atom.h:66
@ SinceTagRight
Definition atom.h:91
@ DetailsLeft
Definition atom.h:37
@ CodeBad
Definition atom.h:32
@ RawString
Definition atom.h:82
@ Target
Definition atom.h:106
@ AnnotatedList
Definition atom.h:22
@ SectionRight
Definition atom.h:84
@ SectionHeadingLeft
Definition atom.h:85
@ TableLeft
Definition atom.h:96
@ ListItemRight
Definition atom.h:70
@ TitleLeft
Definition atom.h:107
@ Image
Definition atom.h:54
@ TableItemRight
Definition atom.h:103
@ ListItemLeft
Definition atom.h:69
@ ImportantRight
Definition atom.h:57
@ Code
Definition atom.h:31
@ String
Definition atom.h:95
@ ListLeft
Definition atom.h:65
@ NavAutoLink
Definition atom.h:72
@ CodeQuoteCommand
Definition atom.h:34
@ BriefLeft
Definition atom.h:26
@ ImageText
Definition atom.h:55
@ ExampleFileLink
Definition atom.h:43
@ LegaleseLeft
Definition atom.h:60
@ ListRight
Definition atom.h:71
@ C
Definition atom.h:28
@ ParaRight
Definition atom.h:78
@ Last
Definition atom.h:113
@ Qml
Definition atom.h:79
@ FormattingLeft
Definition atom.h:50
@ FormattingRight
Definition atom.h:51
@ SectionHeadingRight
Definition atom.h:86
@ Link
Definition atom.h:63
@ ImportantLeft
Definition atom.h:56
@ FormatEndif
Definition atom.h:48
@ UnhandledFormat
Definition atom.h:109
@ SinceTagLeft
Definition atom.h:90
@ ExampleImageLink
Definition atom.h:44
@ BR
Definition atom.h:25
@ DetailsRight
Definition atom.h:38
@ FootnoteLeft
Definition atom.h:45
@ AutoLink
Definition atom.h:23
@ SnippetLocation
Definition atom.h:94
@ TableHeaderLeft
Definition atom.h:98
@ ComparesRight
Definition atom.h:36
@ QuotationLeft
Definition atom.h:80
@ SectionLeft
Definition atom.h:83
@ LinkNode
Definition atom.h:64
@ HR
Definition atom.h:53
@ DivLeft
Definition atom.h:41
@ TableItemLeft
Definition atom.h:102
@ NoteRight
Definition atom.h:76
@ QuotationRight
Definition atom.h:81
@ ParaLeft
Definition atom.h:77
@ BaseName
Definition atom.h:24
@ ComparesLeft
Definition atom.h:35
@ FormatIf
Definition atom.h:49
@ TitleRight
Definition atom.h:108
@ SnippetIdentifier
Definition atom.h:93
@ NoteLeft
Definition atom.h:75
@ SidebarRight
Definition atom.h:88
@ UnknownCommand
Definition atom.h:112
@ DetailsSummaryRight
Definition atom.h:40
@ DetailsSummaryLeft
Definition atom.h:39
Atom * m_next
Definition atom.h:170
const Atom * next(AtomType t, const QString &s) const
Return the next Atom in the list if it is of AtomType t and its string part is s.
Definition atom.cpp:316
QString linkText() const
For a link atom, returns the string representing the link text if one exist in the list of atoms.
Definition atom.cpp:371
const Atom * next() const
Return the next atom in the atom list.
Definition atom.h:152
const Atom * next(AtomType t) const
Return the next Atom in the list if it is of AtomType t.
Definition atom.cpp:307
const Atom * find(AtomType t, qsizetype *offset=nullptr) const
Starting from this Atom, searches the linked list for the atom of specified type t and returns it.
Definition atom.cpp:272
const Atom * find(AtomType t, const QString &s) const
Starting from this Atom, searches the linked list for the atom of specified type t and string s,...
Definition atom.cpp:290
QString typeString() const
Return the type of this atom as a string.
Definition atom.cpp:339
AtomType m_type
Definition atom.h:171
virtual int flags() const override
Definition atom.cpp:486
LinkAtom(Atom::AtomType type, const QString &p1, const QString &p2, Location location=Location())
Constructs a LinkAtom carrying an explicit atom type.
Definition atom.cpp:413
Genus m_genus
Definition atom.h:201
LinkAtom(const QString &p1, const QString &p2, Location location=Location())
Constructs a LinkAtom of type Atom::Link.
Definition atom.cpp:397
LinkAtom(const LinkAtom &t)
Standard copy constructor of LinkAtom t.
Definition atom.cpp:461
Tree * m_domain
Definition atom.h:202
LinkAtom(Atom *previous, const LinkAtom &t)
Special copy constructor of LinkAtom t, where where the new LinkAtom will not be the first one in the...
Definition atom.cpp:476
void resolveSquareBracketParams(const QString &text) override
Resolves the parameters that were enclosed in square brackets, supplied as text, setting the domain a...
Definition atom.cpp:427
The Location class provides a way to mark a location in a file.
Definition location.h:20
Combined button and popup list for selecting options.
@ QmlAttachedProperties