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 { "TitleLeft", Atom::TitleLeft },
205 { "TitleRight", Atom::TitleRight },
206 { "UnhandledFormat", Atom::UnhandledFormat },
207 { "WarningLeft", Atom::WarningLeft },
208 { "WarningRight", Atom::WarningRight },
209 { "UnknownCommand", Atom::UnknownCommand },
210 { nullptr, 0 } };
211
212/*! \fn Atom::Atom(AtomType type, const QString &string)
213
214 Constructs an atom of the specified \a type with the single
215 parameter \a string and does not put the new atom in a list.
216*/
217
218/*! \fn Atom::Atom(AtomType type, const QString &p1, const QString &p2)
219
220 Constructs an atom of the specified \a type with the two
221 parameters \a p1 and \a p2 and does not put the new atom
222 in a list.
223*/
224
225/*! \fn Atom(Atom *previous, AtomType type, const QString &string)
226
227 Constructs an atom of the specified \a type with the single
228 parameter \a string and inserts the new atom into the list
229 after the \a previous atom.
230*/
231
232/*! \fn Atom::Atom(Atom *previous, AtomType type, const QString &p1, const QString &p2)
233
234 Constructs an atom of the specified \a type with the two
235 parameters \a p1 and \a p2 and inserts the new atom into
236 the list after the \a previous atom.
237*/
238
239/*! \fn void Atom::appendChar(QChar ch)
240
241 Appends \a ch to the string parameter of this atom.
242
243 \also string()
244*/
245
246/*! \fn void Atom::concatenateString(const QString &string)
247
248 Appends \a string to the string parameter of this atom.
249
250 \also string()
251*/
252
253/*! \fn void Atom::chopString()
254
255 \also string()
256*/
257
258/*!
259 Starting from this Atom, searches the linked list for the
260 atom of specified type \a t and returns it. Returns \nullptr
261 if no such atom is found.
262
263 If an atom is found and \a offset is not \nullptr, it's set
264 to the number of links traversed from this atom to the found
265 atom (0 if this atom matches). \a offset is untouched when
266 not found.
267*/
268const Atom *Atom::find(AtomType t, qsizetype *offset) const
269{
270 qsizetype i{0};
271 const auto *a{this};
272 while (a && a->type() != t) {
273 a = a->next();
274 ++i;
275 }
276 if (a && offset)
277 *offset = i;
278 return a;
279}
280
281/*!
282 Starting from this Atom, searches the linked list for the
283 atom of specified type \a t and string \a s, and returns it.
284 Returns \nullptr if no such atom is found.
285*/
286const Atom *Atom::find(AtomType t, const QString &s) const
287{
288 const auto *a{this};
289 while (a && (a->type() != t || a->string() != s))
290 a = a->next();
291 return a;
292}
293
294/*! \fn Atom *Atom::next()
295 Return the next atom in the atom list.
296 \also type(), string()
297*/
298
299/*!
300 Return the next Atom in the list if it is of AtomType \a t.
301 Otherwise return 0.
302 */
303const Atom *Atom::next(AtomType t) const
304{
305 return (m_next && (m_next->type() == t)) ? m_next : nullptr;
306}
307
308/*!
309 Return the next Atom in the list if it is of AtomType \a t
310 and its string part is \a s. Otherwise return 0.
311 */
312const Atom *Atom::next(AtomType t, const QString &s) const
313{
314 return (m_next && (m_next->type() == t) && (m_next->string() == s)) ? m_next : nullptr;
315}
316
317/*! \fn const Atom *Atom::next() const
318 Return the next atom in the atom list.
319 \also type(), string()
320*/
321
322/*! \fn AtomType Atom::type() const
323 Return the type of this atom.
324 \also string(), next()
325*/
326
327/*!
328 Return the type of this atom as a string. Return "Invalid" if
329 type() returns an impossible value.
330
331 This is only useful for debugging.
332
333 \also type()
334*/
336{
337 static bool deja = false;
338
339 if (!deja) {
340 int i = 0;
341 while (atms[i].english != nullptr) {
342 if (atms[i].no != i)
343 Location::internalError(QStringLiteral("QDoc::Atom: atom %1 missing").arg(i));
344 ++i;
345 }
346 deja = true;
347 }
348
349 int i = static_cast<int>(type());
350 if (i < 0 || i > static_cast<int>(Last))
351 return QLatin1String("Invalid");
352 return QLatin1String(atms[i].english);
353}
354
355/*! \fn const QString &Atom::string() const
356
357 Returns the string parameter that together with the type
358 characterizes this atom.
359
360 \also type(), next()
361*/
362
363/*!
364 For a link atom, returns the string representing the link text
365 if one exist in the list of atoms.
366*/
368{
369 Q_ASSERT(m_type == Atom::Link);
370 QString result;
371
372 if (next() && next()->string() == ATOM_FORMATTING_LINK) {
373 auto *atom = next()->next();
374 while (atom && atom->type() != Atom::FormattingRight) {
375 result += atom->string();
376 atom = atom->next();
377 }
378 return result;
379 }
380
381 return string();
382}
383
384/*!
385 The only constructor for LinkAtom. It creates an Atom of
386 type Atom::Link. \a p1 being the link target. \a p2 is the
387 parameters in square brackets. Normally there is just one
388 word in the square brackets, but there can be up to three
389 words separated by spaces. The constructor splits \a p2 on
390 the space character.
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 Resolves the parameters that were enclosed in square brackets, supplied as
403 \a text, setting the domain and genus to appropriate values.
404 */
405void LinkAtom::resolveSquareBracketParams(const QString &text)
406{
407 m_squareBracketParams = text.toLower().split(QLatin1Char(' '));
408 for (const auto &param : m_squareBracketParams) {
409 if (!m_domain) {
410 m_domain = QDocDatabase::qdocDB()->findTree(param);
411 if (m_domain) {
412 continue;
413 }
414 }
415
416 if (param == "qml") {
417 m_genus = Genus::QML;
418 continue;
419 }
420 if (param == "cpp") {
421 m_genus = Genus::CPP;
422 continue;
423 }
424 if (param == "doc") {
425 m_genus = Genus::DOC;
426 continue;
427 }
428 if (param == "api") {
429 m_genus = Genus::API;
430 continue;
431 }
432 break;
433 }
434}
435
436/*!
437 Standard copy constructor of LinkAtom \a t.
438 */
440 : Atom(Link, t.string()),
445{
446 // nothing
447}
448
449/*!
450 Special copy constructor of LinkAtom \a t, where
451 where the new LinkAtom will not be the first one
452 in the list.
453 */
454LinkAtom::LinkAtom(Atom *previous, const LinkAtom &t)
455 : Atom(previous, Link, t.string()),
460{
461 previous->m_next = this;
462}
463
464int LinkAtom::flags() const
465{
466 int flags = 0;
467 if (m_squareBracketParams.contains("attached"))
468 flags |= QmlAttachedProperties;
469
470 return flags;
471}
472
473QT_END_NAMESPACE
int no
Definition atom.cpp:120
const char * english
Definition atom.cpp:119
#define ATOM_FORMATTING_LINK
Definition atom.h:205
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:153
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:108
@ 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:109
@ 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
@ TitleLeft
Definition atom.h:105
@ 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:111
@ 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:107
@ 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
@ TitleRight
Definition atom.h:106
@ SnippetIdentifier
Definition atom.h:91
@ NoteLeft
Definition atom.h:73
@ SidebarRight
Definition atom.h:86
@ UnknownCommand
Definition atom.h:110
Atom * m_next
Definition atom.h:168
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:312
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:367
const Atom * next() const
Return the next atom in the atom list.
Definition atom.h:150
const Atom * next(AtomType t) const
Return the next Atom in the list if it is of AtomType t.
Definition atom.cpp:303
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:268
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:286
QString typeString() const
Return the type of this atom as a string.
Definition atom.cpp:335
AtomType m_type
Definition atom.h:169
virtual int flags() const override
Definition atom.cpp:464
Genus m_genus
Definition atom.h:197
LinkAtom(const QString &p1, const QString &p2, Location location=Location())
The only constructor for LinkAtom.
Definition atom.cpp:392
LinkAtom(const LinkAtom &t)
Standard copy constructor of LinkAtom t.
Definition atom.cpp:439
Tree * m_domain
Definition atom.h:198
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:454
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:405
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