|
3 | 3 |
|
4 | 4 | #include <string> |
5 | 5 |
|
6 | | -class Token { |
7 | | -public: |
| 6 | +struct Token { |
| 7 | + /** |
| 8 | + * @brief The different kinds of tokens |
| 9 | + * The positive and negative numbers represent different types |
| 10 | + * of tokens but a grammar rule that accepts any kind of number |
| 11 | + * can use the Number type |
| 12 | + */ |
8 | 13 | enum Type { |
9 | | - Literal, |
| 14 | + StringLiteral, |
10 | 15 | Separator, |
| 16 | + PositiveNumber, |
| 17 | + NegativeNumber, |
| 18 | + FloatingPointNumber, |
11 | 19 | Number, |
12 | 20 | Identifier, |
13 | | - Sign, |
| 21 | + ArithmeticSign, |
14 | 22 | Eof |
15 | 23 | }; |
16 | 24 |
|
17 | | - Token() = default; |
18 | | - Token(Type type, const std::string& image) : |
19 | | - type_(type), image_(image) {} |
| 25 | + /** |
| 26 | + * @return A Token representing an arithmetic sign |
| 27 | + */ |
| 28 | + static Token createArithmeticSign(char src); |
| 29 | + |
| 30 | + /** |
| 31 | + * @return A Token representing a separator |
| 32 | + */ |
| 33 | + static Token createSeparator(char src); |
| 34 | + |
| 35 | + /** |
| 36 | + * The Token's type returned here depends on is_positive and is_float |
| 37 | + * @param number The image of the number |
| 38 | + * @param is_positive true if number represents a positive number |
| 39 | + * @param is_float true if the number represents a floating-point number |
| 40 | + * @return A Token representing a number |
| 41 | + */ |
| 42 | + static Token createNumber(const std::string& number, bool is_positive, bool is_float); |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Constructs an EOF token. |
| 46 | + */ |
| 47 | + Token(); |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Construct a new token with the given type and image |
| 51 | + */ |
| 52 | + Token(Type type, const std::string& image = ""); |
| 53 | + |
20 | 54 | Token(const Token&) = default; |
21 | 55 | Token& operator=(const Token&) = default; |
| 56 | + Token(Token&&) = default; |
| 57 | + Token& operator=(Token&&) = default; |
22 | 58 |
|
23 | | - Type type() const { |
24 | | - return type_; |
25 | | - } |
26 | | - |
27 | | - const std::string& image() const { |
28 | | - return image_; |
29 | | - } |
| 59 | + /** |
| 60 | + * @brief Equality comparison with another token |
| 61 | + * @see operator== with string and Token::Type |
| 62 | + */ |
| 63 | + bool operator==(const Token&) const; |
| 64 | + |
| 65 | + /** |
| 66 | + * @return true if the token's image is equal to other |
| 67 | + * @param other String to compare |
| 68 | + */ |
| 69 | + bool operator==(const std::string&) const; |
| 70 | + |
| 71 | + /** |
| 72 | + * If the token's type is Number or if other is Number, |
| 73 | + * then it also matches PositiveNumber, NegativeNumber and FloatingPointNumber |
| 74 | + * |
| 75 | + * @return true if the token's type is equal to other |
| 76 | + * @param other Type to compare |
| 77 | + */ |
| 78 | + bool operator==(Token::Type) const; |
30 | 79 |
|
31 | | - unsigned long long toUInt() const { |
32 | | - return std::stoul(image_); |
33 | | - } |
| 80 | + /** |
| 81 | + * @see operator== |
| 82 | + */ |
| 83 | + bool operator!=(const Token&) const; |
34 | 84 |
|
35 | | - long long toInt() const { |
36 | | - return std::stol(image_); |
37 | | - } |
| 85 | + /** |
| 86 | + * @see operator== |
| 87 | + */ |
| 88 | + bool operator!=(const std::string&) const; |
| 89 | + |
| 90 | + /** |
| 91 | + * @see operator== |
| 92 | + */ |
| 93 | + bool operator!=(Token::Type) const; |
38 | 94 |
|
39 | | - double toDouble() const { |
40 | | - return std::stod(image_); |
41 | | - } |
| 95 | + /** |
| 96 | + * Using the standard library's functionnalities, parses the |
| 97 | + * image to an unsigned integer |
| 98 | + */ |
| 99 | + unsigned long long toUInt() const; |
| 100 | + |
| 101 | + /** |
| 102 | + * Same as above but outputs a signed integer |
| 103 | + */ |
| 104 | + long long toInt() const; |
| 105 | + |
| 106 | + /** |
| 107 | + * Same as above but outputs a floating-point number |
| 108 | + */ |
| 109 | + double toDouble() const; |
| 110 | + |
| 111 | + /** |
| 112 | + * @brief The token's type |
| 113 | + */ |
| 114 | + Type type; |
42 | 115 |
|
43 | | -private: |
44 | | - Type type_; |
45 | | - std::string image_; |
| 116 | + /** |
| 117 | + * @brief The token's image, ie. the string that represents it |
| 118 | + */ |
| 119 | + std::string image; |
46 | 120 | }; |
47 | 121 |
|
48 | 122 | #endif |
0 commit comments