blob: 76020ef27366354b4fe4e2c40e1a958f75f63dc7 [file] [log] [blame]
swissChili7babd922021-12-02 22:46:48 -08001#pragma once
2
3#include <QList>
4#include <QChar>
5
swissChilic71acc62021-12-07 08:03:37 -08006template <typename T>
7class TokenBase
8{
swissChili7babd922021-12-02 22:46:48 -08009public:
swissChilic71acc62021-12-07 08:03:37 -080010 TokenBase();
11 TokenBase(const T &other);
swissChili7babd922021-12-02 22:46:48 -080012
swissChilic71acc62021-12-07 08:03:37 -080013 explicit TokenBase(QChar symbol);
14 explicit TokenBase(QString identifier);
15 explicit TokenBase(QList<T> parenthesized);
16 TokenBase(char varType, const QString name);
swissChili7babd922021-12-02 22:46:48 -080017
swissChilic71acc62021-12-07 08:03:37 -080018 bool operator==(const T &other) const;
19 bool operator!=(const T &other) const;
swissChili7babd922021-12-02 22:46:48 -080020
swissChilid17b5a12021-12-05 20:46:42 -080021 bool isSym() const;
22 bool isIdent() const;
23 bool isParen() const;
24 bool isVar() const;
swissChili7babd922021-12-02 22:46:48 -080025
swissChilic71acc62021-12-07 08:03:37 -080026 QList<T> parenContent();
swissChili7babd922021-12-02 22:46:48 -080027
swissChili3e98c062021-12-04 22:07:38 -080028 char varType() const;
29 const QString &name() const;
30
swissChilid17b5a12021-12-05 20:46:42 -080031 operator QString() const;
32
swissChilic71acc62021-12-07 08:03:37 -080033 enum
34 {
35 SYM,
36 IDENT,
37 PAREN,
38 VAR,
39 TOKEN_TYPE_LAST,
swissChili7babd922021-12-02 22:46:48 -080040 };
41
swissChilid17b5a12021-12-05 20:46:42 -080042 static QString typeToString(int type);
43
44 int type() const;
45
swissChilic71acc62021-12-07 08:03:37 -080046protected:
swissChili7babd922021-12-02 22:46:48 -080047 int _type = 0;
48 QString _stringVal = "";
swissChilic71acc62021-12-07 08:03:37 -080049 QList<T> _listVal;
swissChili7babd922021-12-02 22:46:48 -080050 QChar _charVal = 0;
51};
swissChili3e98c062021-12-04 22:07:38 -080052
swissChilic71acc62021-12-07 08:03:37 -080053class Token : public TokenBase<Token>
54{
55public:
56 using TokenBase::TokenBase;
57};
58
swissChili3e98c062021-12-04 22:07:38 -080059using LTok = QList<Token>;
swissChilic71acc62021-12-07 08:03:37 -080060
61template <typename T>
62TokenBase<T>::TokenBase(const T &other)
63{
64 _type = other._type;
65 _stringVal = other._stringVal;
66 _listVal = other._listVal;
67 _charVal = other._charVal;
68}
69
70template <typename T>
71TokenBase<T>::TokenBase(QChar symbol)
72{
73 _type = SYM;
74 _charVal = symbol;
75}
76
77template <typename T>
78TokenBase<T>::TokenBase(QString identifier)
79{
80 _type = IDENT;
81 _stringVal = identifier;
82}
83
84template <typename T>
85TokenBase<T>::TokenBase(QList<T> parenthesized)
86{
87 _type = PAREN;
88 _listVal = std::move(parenthesized);
89}
90
91template <typename T>
92TokenBase<T>::TokenBase(char varType, const QString name)
93{
94 _type = VAR;
95 _charVal = varType;
96 _stringVal = name;
97}
98
99template <typename T>
100bool TokenBase<T>::isSym() const
101{
102 return _type == SYM;
103}
104
105template <typename T>
106bool TokenBase<T>::isIdent() const
107{
108 return _type == IDENT;
109}
110
111template <typename T>
112bool TokenBase<T>::isParen() const
113{
114 return _type == PAREN;
115}
116template <typename T>
117bool TokenBase<T>::isVar() const
118{
119 return _type == VAR;
120}
121
122template <typename T>
123TokenBase<T>::TokenBase() : TokenBase("Null")
124{
125}
126
127template <typename T>
128bool TokenBase<T>::operator==(const T &other) const
129{
130 return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal;
131}
132
133template <typename T>
134QList<T> TokenBase<T>::parenContent()
135{
136 if (isParen())
137 {
138 return _listVal;
139 }
140 else
141 {
142 return {};
143 }
144}
145
146template <typename T>
147char TokenBase<T>::varType() const
148{
149 return _charVal.toLatin1();
150}
151
152template <typename T>
153const QString &TokenBase<T>::name() const
154{
155 return _stringVal;
156}
157
158template <typename T>
159bool TokenBase<T>::operator!=(const T &other) const
160{
161 return !(this->operator==(other));
162}
163
164template <typename T>
165TokenBase<T>::operator QString() const
166{
167 if (isIdent())
168 return _stringVal;
169 if (isSym())
170 return _charVal;
171 if (isVar())
172 return QString(_charVal) + "." + _stringVal;
173 if (isParen())
174 {
175 QStringList parts;
176 for (const T &tok : _listVal)
177 {
178 parts.append(static_cast<QString>(tok));
179 }
180
181 return "(" + parts.join(" ") + ")";
182 }
183
184 return "Null";
185}
186
187template <typename T>
188int TokenBase<T>::type() const
189{
190 return _type;
191}
192
193template <typename T>
194QString TokenBase<T>::typeToString(int type)
195{
196 static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"};
197 return typeNames[type];
198}