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