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#ifndef ATOM_H
5#define ATOM_H
6
7#include "node.h"
8
9#include <QtCore/qdebug.h>
10#include <QtCore/qstringlist.h>
11
12QT_BEGIN_NAMESPACE
13
14class Tree;
15class LinkAtom;
16
17class Atom
18{
19public:
20 enum AtomType {
21 AnnotatedList,
22 AutoLink,
23 BaseName,
24 BR,
25 BriefLeft,
26 BriefRight,
27 C,
28 CaptionLeft,
29 CaptionRight,
30 Code,
31 CodeBad,
32 CodeQuoteArgument,
33 CodeQuoteCommand,
34 DetailsLeft,
35 DetailsRight,
36 DivLeft,
37 DivRight,
38 ExampleFileLink,
39 ExampleImageLink,
40 FootnoteLeft,
41 FootnoteRight,
42 FormatElse,
43 FormatEndif,
44 FormatIf,
45 FormattingLeft,
46 FormattingRight,
47 GeneratedList,
48 HR,
49 Image,
50 ImageText,
51 ImportantLeft,
52 ImportantRight,
53 InlineImage,
54 Keyword,
55 LegaleseLeft,
56 LegaleseRight,
57 LineBreak,
58 Link,
59 LinkNode,
60 ListLeft,
61 ListItemNumber,
62 ListTagLeft,
63 ListTagRight,
64 ListItemLeft,
65 ListItemRight,
66 ListRight,
67 NavAutoLink,
68 NavLink,
69 Nop,
70 NoteLeft,
71 NoteRight,
72 ParaLeft,
73 ParaRight,
74 Qml,
75 QuotationLeft,
76 QuotationRight,
77 RawString,
78 SectionLeft,
79 SectionRight,
80 SectionHeadingLeft,
81 SectionHeadingRight,
82 SidebarLeft,
83 SidebarRight,
84 SinceList,
85 SinceTagLeft,
86 SinceTagRight,
87 SnippetCommand,
88 SnippetIdentifier,
89 SnippetLocation,
90 String,
91 TableLeft,
92 TableRight,
93 TableHeaderLeft,
94 TableHeaderRight,
95 TableRowLeft,
96 TableRowRight,
97 TableItemLeft,
98 TableItemRight,
99 TableOfContents,
100 Target,
101 UnhandledFormat,
102 WarningLeft,
103 WarningRight,
104 UnknownCommand,
105 Last = UnknownCommand
106 };
107
108 friend class LinkAtom;
109
110 explicit Atom(AtomType type, const QString &string = "") : m_type(type), m_strs(string) { }
111
112 Atom(AtomType type, const QString &p1, const QString &p2) : m_type(type), m_strs(p1)
113 {
114 if (!p2.isEmpty())
115 m_strs << p2;
116 }
117
118 Atom(Atom *previous, AtomType type, const QString &string)
119 : m_next(previous->m_next), m_type(type), m_strs(string)
120 {
121 previous->m_next = this;
122 }
123
124 Atom(Atom *previous, AtomType type, const QString &p1, const QString &p2)
125 : m_next(previous->m_next), m_type(type), m_strs(p1)
126 {
127 if (!p2.isEmpty())
128 m_strs << p2;
129 previous->m_next = this;
130 }
131
132 virtual ~Atom() = default;
133
134 void appendChar(QChar ch) { m_strs[0] += ch; }
135 void appendString(const QString &string) { m_strs[0] += string; }
136 void chopString() { m_strs[0].chop(n: 1); }
137 void setString(const QString &string) { m_strs[0] = string; }
138 Atom *next() { return m_next; }
139 void setNext(Atom *newNext) { m_next = newNext; }
140
141 [[nodiscard]] const Atom *next() const { return m_next; }
142 [[nodiscard]] const Atom *next(AtomType t) const;
143 [[nodiscard]] const Atom *next(AtomType t, const QString &s) const;
144 [[nodiscard]] AtomType type() const { return m_type; }
145 [[nodiscard]] QString typeString() const;
146 [[nodiscard]] const QString &string() const { return m_strs[0]; }
147 [[nodiscard]] const QString &string(int i) const { return m_strs[i]; }
148 [[nodiscard]] qsizetype count() const { return m_strs.size(); }
149 [[nodiscard]] QString linkText() const;
150 [[nodiscard]] const QStringList &strings() const { return m_strs; }
151
152 [[nodiscard]] virtual bool isLinkAtom() const { return false; }
153 virtual Node::Genus genus() { return Node::DontCare; }
154 virtual Tree *domain() { return nullptr; }
155 virtual const QString &error() { return s_noError; }
156 virtual void resolveSquareBracketParams() {}
157
158protected:
159 static QString s_noError;
160 Atom *m_next = nullptr;
161 AtomType m_type {};
162 QStringList m_strs {};
163};
164
165class LinkAtom : public Atom
166{
167public:
168 LinkAtom(const QString &p1, const QString &p2);
169 LinkAtom(const LinkAtom &t);
170 LinkAtom(Atom *previous, const LinkAtom &t);
171 ~LinkAtom() override = default;
172
173 [[nodiscard]] bool isLinkAtom() const override { return true; }
174 Node::Genus genus() override
175 {
176 resolveSquareBracketParams();
177 return m_genus;
178 }
179 Tree *domain() override
180 {
181 resolveSquareBracketParams();
182 return m_domain;
183 }
184 const QString &error() override { return m_error; }
185 void resolveSquareBracketParams() override;
186
187protected:
188 bool m_resolved {};
189 Node::Genus m_genus {};
190 Tree *m_domain {};
191 QString m_error {};
192 QString m_squareBracketParams {};
193};
194
195#define ATOM_FORMATTING_BOLD "bold"
196#define ATOM_FORMATTING_INDEX "index"
197#define ATOM_FORMATTING_ITALIC "italic"
198#define ATOM_FORMATTING_LINK "link"
199#define ATOM_FORMATTING_PARAMETER "parameter"
200#define ATOM_FORMATTING_SPAN "span "
201#define ATOM_FORMATTING_SUBSCRIPT "subscript"
202#define ATOM_FORMATTING_SUPERSCRIPT "superscript"
203#define ATOM_FORMATTING_TELETYPE "teletype"
204#define ATOM_FORMATTING_UICONTROL "uicontrol"
205#define ATOM_FORMATTING_UNDERLINE "underline"
206
207#define ATOM_LIST_BULLET "bullet"
208#define ATOM_LIST_TAG "tag"
209#define ATOM_LIST_VALUE "value"
210#define ATOM_LIST_LOWERALPHA "loweralpha"
211#define ATOM_LIST_LOWERROMAN "lowerroman"
212#define ATOM_LIST_NUMERIC "numeric"
213#define ATOM_LIST_UPPERALPHA "upperalpha"
214#define ATOM_LIST_UPPERROMAN "upperroman"
215
216QT_END_NAMESPACE
217
218#endif
219

source code of qttools/src/qdoc/qdoc/atom.h