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