blob: 41d803166b1bf02866d5748a4526cfb7961a7aa5 [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
swissChili9dddbf72021-12-08 23:03:25 -080013 TokenBase(QString integer, int base);
swissChilic71acc62021-12-07 08:03:37 -080014 explicit TokenBase(QChar symbol);
15 explicit TokenBase(QString identifier);
16 explicit TokenBase(QList<T> parenthesized);
17 TokenBase(char varType, const QString name);
swissChili7babd922021-12-02 22:46:48 -080018
swissChili1060c0e2021-12-09 09:46:42 -080019 static T fromInteger(int integer);
20
swissChilic71acc62021-12-07 08:03:37 -080021 bool operator==(const T &other) const;
22 bool operator!=(const T &other) const;
swissChili7babd922021-12-02 22:46:48 -080023
swissChilid17b5a12021-12-05 20:46:42 -080024 bool isSym() const;
25 bool isIdent() const;
26 bool isParen() const;
27 bool isVar() const;
swissChili9dddbf72021-12-08 23:03:25 -080028 bool isInteger() const;
swissChili7babd922021-12-02 22:46:48 -080029
swissChilic71acc62021-12-07 08:03:37 -080030 QList<T> parenContent();
swissChili7babd922021-12-02 22:46:48 -080031
swissChili3e98c062021-12-04 22:07:38 -080032 char varType() const;
33 const QString &name() const;
swissChili682e7bc2021-12-07 09:04:54 -080034 QChar symbol() const;
swissChili9dddbf72021-12-08 23:03:25 -080035 int integer() const;
swissChili3e98c062021-12-04 22:07:38 -080036
swissChilid17b5a12021-12-05 20:46:42 -080037 operator QString() const;
38
swissChilic71acc62021-12-07 08:03:37 -080039 enum
40 {
41 SYM,
42 IDENT,
43 PAREN,
44 VAR,
swissChili9dddbf72021-12-08 23:03:25 -080045 INTEGER,
swissChilic71acc62021-12-07 08:03:37 -080046 TOKEN_TYPE_LAST,
swissChili7babd922021-12-02 22:46:48 -080047 };
48
swissChilid17b5a12021-12-05 20:46:42 -080049 static QString typeToString(int type);
50
51 int type() const;
52
swissChilic71acc62021-12-07 08:03:37 -080053protected:
swissChili7babd922021-12-02 22:46:48 -080054 int _type = 0;
swissChili9dddbf72021-12-08 23:03:25 -080055 int _intVal = 0;
swissChili7babd922021-12-02 22:46:48 -080056 QString _stringVal = "";
swissChilic71acc62021-12-07 08:03:37 -080057 QList<T> _listVal;
swissChili7babd922021-12-02 22:46:48 -080058 QChar _charVal = 0;
59};
swissChili3e98c062021-12-04 22:07:38 -080060
swissChilic71acc62021-12-07 08:03:37 -080061class Token : public TokenBase<Token>
62{
63public:
64 using TokenBase::TokenBase;
65};
66
swissChili3e98c062021-12-04 22:07:38 -080067using LTok = QList<Token>;