swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <QList> |
| 4 | #include <QChar> |
| 5 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 6 | template <typename T> |
| 7 | class TokenBase |
| 8 | { |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 9 | public: |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 10 | TokenBase(); |
| 11 | TokenBase(const T &other); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 12 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 13 | explicit TokenBase(QChar symbol); |
| 14 | explicit TokenBase(QString identifier); |
| 15 | explicit TokenBase(QList<T> parenthesized); |
| 16 | TokenBase(char varType, const QString name); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 17 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 18 | bool operator==(const T &other) const; |
| 19 | bool operator!=(const T &other) const; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 20 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 21 | bool isSym() const; |
| 22 | bool isIdent() const; |
| 23 | bool isParen() const; |
| 24 | bool isVar() const; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 25 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 26 | QList<T> parenContent(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 27 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 28 | char varType() const; |
| 29 | const QString &name() const; |
| 30 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 31 | operator QString() const; |
| 32 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 33 | enum |
| 34 | { |
| 35 | SYM, |
| 36 | IDENT, |
| 37 | PAREN, |
| 38 | VAR, |
| 39 | TOKEN_TYPE_LAST, |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 42 | static QString typeToString(int type); |
| 43 | |
| 44 | int type() const; |
| 45 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 46 | protected: |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 47 | int _type = 0; |
| 48 | QString _stringVal = ""; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 49 | QList<T> _listVal; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 50 | QChar _charVal = 0; |
| 51 | }; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 52 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 53 | class Token : public TokenBase<Token> |
| 54 | { |
| 55 | public: |
| 56 | using TokenBase::TokenBase; |
| 57 | }; |
| 58 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 59 | using LTok = QList<Token>; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame^] | 60 | |
| 61 | template <typename T> |
| 62 | TokenBase<T>::TokenBase(const T &other) |
| 63 | { |
| 64 | _type = other._type; |
| 65 | _stringVal = other._stringVal; |
| 66 | _listVal = other._listVal; |
| 67 | _charVal = other._charVal; |
| 68 | } |
| 69 | |
| 70 | template <typename T> |
| 71 | TokenBase<T>::TokenBase(QChar symbol) |
| 72 | { |
| 73 | _type = SYM; |
| 74 | _charVal = symbol; |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | TokenBase<T>::TokenBase(QString identifier) |
| 79 | { |
| 80 | _type = IDENT; |
| 81 | _stringVal = identifier; |
| 82 | } |
| 83 | |
| 84 | template <typename T> |
| 85 | TokenBase<T>::TokenBase(QList<T> parenthesized) |
| 86 | { |
| 87 | _type = PAREN; |
| 88 | _listVal = std::move(parenthesized); |
| 89 | } |
| 90 | |
| 91 | template <typename T> |
| 92 | TokenBase<T>::TokenBase(char varType, const QString name) |
| 93 | { |
| 94 | _type = VAR; |
| 95 | _charVal = varType; |
| 96 | _stringVal = name; |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | bool TokenBase<T>::isSym() const |
| 101 | { |
| 102 | return _type == SYM; |
| 103 | } |
| 104 | |
| 105 | template <typename T> |
| 106 | bool TokenBase<T>::isIdent() const |
| 107 | { |
| 108 | return _type == IDENT; |
| 109 | } |
| 110 | |
| 111 | template <typename T> |
| 112 | bool TokenBase<T>::isParen() const |
| 113 | { |
| 114 | return _type == PAREN; |
| 115 | } |
| 116 | template <typename T> |
| 117 | bool TokenBase<T>::isVar() const |
| 118 | { |
| 119 | return _type == VAR; |
| 120 | } |
| 121 | |
| 122 | template <typename T> |
| 123 | TokenBase<T>::TokenBase() : TokenBase("Null") |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | template <typename T> |
| 128 | bool TokenBase<T>::operator==(const T &other) const |
| 129 | { |
| 130 | return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal; |
| 131 | } |
| 132 | |
| 133 | template <typename T> |
| 134 | QList<T> TokenBase<T>::parenContent() |
| 135 | { |
| 136 | if (isParen()) |
| 137 | { |
| 138 | return _listVal; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | return {}; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | char TokenBase<T>::varType() const |
| 148 | { |
| 149 | return _charVal.toLatin1(); |
| 150 | } |
| 151 | |
| 152 | template <typename T> |
| 153 | const QString &TokenBase<T>::name() const |
| 154 | { |
| 155 | return _stringVal; |
| 156 | } |
| 157 | |
| 158 | template <typename T> |
| 159 | bool TokenBase<T>::operator!=(const T &other) const |
| 160 | { |
| 161 | return !(this->operator==(other)); |
| 162 | } |
| 163 | |
| 164 | template <typename T> |
| 165 | TokenBase<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 | |
| 187 | template <typename T> |
| 188 | int TokenBase<T>::type() const |
| 189 | { |
| 190 | return _type; |
| 191 | } |
| 192 | |
| 193 | template <typename T> |
| 194 | QString TokenBase<T>::typeToString(int type) |
| 195 | { |
| 196 | static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"}; |
| 197 | return typeNames[type]; |
| 198 | } |