Add parser
diff --git a/Token.h b/Token.h
index 4835de0..76020ef 100644
--- a/Token.h
+++ b/Token.h
@@ -3,44 +3,196 @@
#include <QList>
#include <QChar>
-class Token {
+template <typename T>
+class TokenBase
+{
public:
- Token();
- Token(const Token &other);
+ TokenBase();
+ TokenBase(const T &other);
- explicit Token(QChar symbol);
- explicit Token(QString &&identifier);
- explicit Token(QList<Token> parenthesized);
- Token(char varType, const QString &&name);
+ explicit TokenBase(QChar symbol);
+ explicit TokenBase(QString identifier);
+ explicit TokenBase(QList<T> parenthesized);
+ TokenBase(char varType, const QString name);
- bool operator ==(const Token &other) const;
- bool operator !=(const Token &other) const;
+ bool operator==(const T &other) const;
+ bool operator!=(const T &other) const;
bool isSym() const;
bool isIdent() const;
bool isParen() const;
bool isVar() const;
- QList<Token> parenContent();
+ QList<T> parenContent();
char varType() const;
const QString &name() const;
operator QString() const;
- enum Type {
- SYM, IDENT, PAREN, VAR,
+ enum
+ {
+ SYM,
+ IDENT,
+ PAREN,
+ VAR,
+ TOKEN_TYPE_LAST,
};
static QString typeToString(int type);
int type() const;
-private:
+protected:
int _type = 0;
QString _stringVal = "";
- QList<Token> _listVal;
+ QList<T> _listVal;
QChar _charVal = 0;
};
+class Token : public TokenBase<Token>
+{
+public:
+ using TokenBase::TokenBase;
+};
+
using LTok = QList<Token>;
+
+template <typename T>
+TokenBase<T>::TokenBase(const T &other)
+{
+ _type = other._type;
+ _stringVal = other._stringVal;
+ _listVal = other._listVal;
+ _charVal = other._charVal;
+}
+
+template <typename T>
+TokenBase<T>::TokenBase(QChar symbol)
+{
+ _type = SYM;
+ _charVal = symbol;
+}
+
+template <typename T>
+TokenBase<T>::TokenBase(QString identifier)
+{
+ _type = IDENT;
+ _stringVal = identifier;
+}
+
+template <typename T>
+TokenBase<T>::TokenBase(QList<T> parenthesized)
+{
+ _type = PAREN;
+ _listVal = std::move(parenthesized);
+}
+
+template <typename T>
+TokenBase<T>::TokenBase(char varType, const QString name)
+{
+ _type = VAR;
+ _charVal = varType;
+ _stringVal = name;
+}
+
+template <typename T>
+bool TokenBase<T>::isSym() const
+{
+ return _type == SYM;
+}
+
+template <typename T>
+bool TokenBase<T>::isIdent() const
+{
+ return _type == IDENT;
+}
+
+template <typename T>
+bool TokenBase<T>::isParen() const
+{
+ return _type == PAREN;
+}
+template <typename T>
+bool TokenBase<T>::isVar() const
+{
+ return _type == VAR;
+}
+
+template <typename T>
+TokenBase<T>::TokenBase() : TokenBase("Null")
+{
+}
+
+template <typename T>
+bool TokenBase<T>::operator==(const T &other) const
+{
+ return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal;
+}
+
+template <typename T>
+QList<T> TokenBase<T>::parenContent()
+{
+ if (isParen())
+ {
+ return _listVal;
+ }
+ else
+ {
+ return {};
+ }
+}
+
+template <typename T>
+char TokenBase<T>::varType() const
+{
+ return _charVal.toLatin1();
+}
+
+template <typename T>
+const QString &TokenBase<T>::name() const
+{
+ return _stringVal;
+}
+
+template <typename T>
+bool TokenBase<T>::operator!=(const T &other) const
+{
+ return !(this->operator==(other));
+}
+
+template <typename T>
+TokenBase<T>::operator QString() const
+{
+ if (isIdent())
+ return _stringVal;
+ if (isSym())
+ return _charVal;
+ if (isVar())
+ return QString(_charVal) + "." + _stringVal;
+ if (isParen())
+ {
+ QStringList parts;
+ for (const T &tok : _listVal)
+ {
+ parts.append(static_cast<QString>(tok));
+ }
+
+ return "(" + parts.join(" ") + ")";
+ }
+
+ return "Null";
+}
+
+template <typename T>
+int TokenBase<T>::type() const
+{
+ return _type;
+}
+
+template <typename T>
+QString TokenBase<T>::typeToString(int type)
+{
+ static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"};
+ return typeNames[type];
+}