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