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