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 | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 13 | TokenBase(QString integer, int base); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 14 | explicit TokenBase(QChar symbol); |
15 | explicit TokenBase(QString identifier); | ||||
16 | explicit TokenBase(QList<T> parenthesized); | ||||
17 | TokenBase(char varType, const QString name); | ||||
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 18 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 19 | bool operator==(const T &other) const; |
20 | bool operator!=(const T &other) const; | ||||
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 21 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 22 | bool isSym() const; |
23 | bool isIdent() const; | ||||
24 | bool isParen() const; | ||||
25 | bool isVar() const; | ||||
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 26 | bool isInteger() const; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 27 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 28 | QList<T> parenContent(); |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 29 | |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 30 | char varType() const; |
31 | const QString &name() const; | ||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 32 | QChar symbol() const; |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 33 | int integer() const; |
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 34 | |
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 35 | operator QString() const; |
36 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 37 | enum |
38 | { | ||||
39 | SYM, | ||||
40 | IDENT, | ||||
41 | PAREN, | ||||
42 | VAR, | ||||
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 43 | INTEGER, |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 44 | TOKEN_TYPE_LAST, |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 45 | }; |
46 | |||||
swissChili | d17b5a1 | 2021-12-05 20:46:42 -0800 | [diff] [blame] | 47 | static QString typeToString(int type); |
48 | |||||
49 | int type() const; | ||||
50 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 51 | protected: |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 52 | int _type = 0; |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 53 | int _intVal = 0; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 54 | QString _stringVal = ""; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 55 | QList<T> _listVal; |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 56 | QChar _charVal = 0; |
57 | }; | ||||
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 58 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 59 | class Token : public TokenBase<Token> |
60 | { | ||||
61 | public: | ||||
62 | using TokenBase::TokenBase; | ||||
63 | }; | ||||
64 | |||||
swissChili | 3e98c06 | 2021-12-04 22:07:38 -0800 | [diff] [blame] | 65 | using LTok = QList<Token>; |