1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef ATOM_H
30 #define ATOM_H
31 
32 #include "node.h"
33 
34 #include <QtCore/qdebug.h>
35 #include <QtCore/qstringlist.h>
36 
37 QT_BEGIN_NAMESPACE
38 
39 class Tree;
40 class LinkAtom;
41 
42 class Atom
43 {
44 public:
45     enum AtomType {
46         AnnotatedList,
47         AutoLink,
48         BaseName,
49         BR,
50         BriefLeft,
51         BriefRight,
52         C,
53         CaptionLeft,
54         CaptionRight,
55         Code,
56         CodeBad,
57         CodeNew,
58         CodeOld,
59         CodeQuoteArgument,
60         CodeQuoteCommand,
61         DivLeft,
62         DivRight,
63         EndQmlText,
64         ExampleFileLink,
65         ExampleImageLink,
66         FootnoteLeft,
67         FootnoteRight,
68         FormatElse,
69         FormatEndif,
70         FormatIf,
71         FormattingLeft,
72         FormattingRight,
73         GeneratedList,
74         HR,
75         Image,
76         ImageText,
77         ImportantLeft,
78         ImportantRight,
79         InlineImage,
80         JavaScript,
81         EndJavaScript,
82         Keyword,
83         LegaleseLeft,
84         LegaleseRight,
85         LineBreak,
86         Link,
87         LinkNode,
88         ListLeft,
89         ListItemNumber,
90         ListTagLeft,
91         ListTagRight,
92         ListItemLeft,
93         ListItemRight,
94         ListRight,
95         NavAutoLink,
96         NavLink,
97         Nop,
98         NoteLeft,
99         NoteRight,
100         ParaLeft,
101         ParaRight,
102         Qml,
103         QmlText,
104         QuotationLeft,
105         QuotationRight,
106         RawString,
107         SectionLeft,
108         SectionRight,
109         SectionHeadingLeft,
110         SectionHeadingRight,
111         SidebarLeft,
112         SidebarRight,
113         SinceList,
114         SinceTagLeft,
115         SinceTagRight,
116         SnippetCommand,
117         SnippetIdentifier,
118         SnippetLocation,
119         String,
120         TableLeft,
121         TableRight,
122         TableHeaderLeft,
123         TableHeaderRight,
124         TableRowLeft,
125         TableRowRight,
126         TableItemLeft,
127         TableItemRight,
128         TableOfContents,
129         Target,
130         UnhandledFormat,
131         UnknownCommand,
132         Last = UnknownCommand
133     };
134 
135     friend class LinkAtom;
136 
type_(type)137     Atom(AtomType type, const QString &string = "") : type_(type), strs(string) {}
138 
Atom(AtomType type,const QString & p1,const QString & p2)139     Atom(AtomType type, const QString &p1, const QString &p2) : type_(type), strs(p1)
140     {
141         if (!p2.isEmpty())
142             strs << p2;
143     }
144 
Atom(Atom * previous,AtomType type,const QString & string)145     Atom(Atom *previous, AtomType type, const QString &string)
146         : next_(previous->next_), type_(type), strs(string)
147     {
148         previous->next_ = this;
149     }
150 
Atom(Atom * previous,AtomType type,const QString & p1,const QString & p2)151     Atom(Atom *previous, AtomType type, const QString &p1, const QString &p2)
152         : next_(previous->next_), type_(type), strs(p1)
153     {
154         if (!p2.isEmpty())
155             strs << p2;
156         previous->next_ = this;
157     }
158 
159     virtual ~Atom() = default;
160 
appendChar(QChar ch)161     void appendChar(QChar ch) { strs[0] += ch; }
appendString(const QString & string)162     void appendString(const QString &string) { strs[0] += string; }
chopString()163     void chopString() { strs[0].chop(1); }
setString(const QString & string)164     void setString(const QString &string) { strs[0] = string; }
next()165     Atom *next() { return next_; }
setNext(Atom * newNext)166     void setNext(Atom *newNext) { next_ = newNext; }
167 
next()168     const Atom *next() const { return next_; }
169     const Atom *next(AtomType t) const;
170     const Atom *next(AtomType t, const QString &s) const;
type()171     AtomType type() const { return type_; }
172     QString typeString() const;
string()173     const QString &string() const { return strs[0]; }
string(int i)174     const QString &string(int i) const { return strs[i]; }
count()175     int count() const { return strs.size(); }
176     void dump() const;
strings()177     const QStringList &strings() const { return strs; }
178 
isLinkAtom()179     virtual bool isLinkAtom() const { return false; }
genus()180     virtual Node::Genus genus() { return Node::DontCare; }
specifiesDomain()181     virtual bool specifiesDomain() { return false; }
domain()182     virtual Tree *domain() { return nullptr; }
goal()183     virtual Node::NodeType goal() { return Node::NoType; }
error()184     virtual const QString &error() { return noError_; }
resolveSquareBracketParams()185     virtual void resolveSquareBracketParams() {}
186 
187 protected:
188     static QString noError_;
189     Atom *next_ = nullptr;
190     AtomType type_;
191     QStringList strs;
192 };
193 
194 class LinkAtom : public Atom
195 {
196 public:
197     LinkAtom(const QString &p1, const QString &p2);
198     LinkAtom(const LinkAtom &t);
199     LinkAtom(Atom *previous, const LinkAtom &t);
~LinkAtom()200     ~LinkAtom() override {}
201 
isLinkAtom()202     bool isLinkAtom() const override { return true; }
genus()203     Node::Genus genus() override
204     {
205         resolveSquareBracketParams();
206         return genus_;
207     }
specifiesDomain()208     bool specifiesDomain() override
209     {
210         resolveSquareBracketParams();
211         return (domain_ != nullptr);
212     }
domain()213     Tree *domain() override
214     {
215         resolveSquareBracketParams();
216         return domain_;
217     }
goal()218     Node::NodeType goal() override
219     {
220         resolveSquareBracketParams();
221         return goal_;
222     }
error()223     const QString &error() override { return error_; }
224     void resolveSquareBracketParams() override;
225 
226 protected:
227     bool resolved_;
228     Node::Genus genus_;
229     Node::NodeType goal_;
230     Tree *domain_;
231     QString error_;
232     QString squareBracketParams_;
233 };
234 
235 #define ATOM_FORMATTING_BOLD "bold"
236 #define ATOM_FORMATTING_INDEX "index"
237 #define ATOM_FORMATTING_ITALIC "italic"
238 #define ATOM_FORMATTING_LINK "link"
239 #define ATOM_FORMATTING_PARAMETER "parameter"
240 #define ATOM_FORMATTING_SPAN "span "
241 #define ATOM_FORMATTING_SUBSCRIPT "subscript"
242 #define ATOM_FORMATTING_SUPERSCRIPT "superscript"
243 #define ATOM_FORMATTING_TELETYPE "teletype"
244 #define ATOM_FORMATTING_UICONTROL "uicontrol"
245 #define ATOM_FORMATTING_UNDERLINE "underline"
246 
247 #define ATOM_LIST_BULLET "bullet"
248 #define ATOM_LIST_TAG "tag"
249 #define ATOM_LIST_VALUE "value"
250 #define ATOM_LIST_LOWERALPHA "loweralpha"
251 #define ATOM_LIST_LOWERROMAN "lowerroman"
252 #define ATOM_LIST_NUMERIC "numeric"
253 #define ATOM_LIST_UPPERALPHA "upperalpha"
254 #define ATOM_LIST_UPPERROMAN "upperroman"
255 
256 QT_END_NAMESPACE
257 
258 #endif
259