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