Skip to content

Commit 0f0c39c

Browse files
author
Avi SZYCHTER
committed
Improvment of the Token cass
1 parent 954f39d commit 0f0c39c

File tree

2 files changed

+138
-31
lines changed

2 files changed

+138
-31
lines changed

include/Token.h

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,100 @@
33

44
#include <string>
55

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+
*/
813
enum Type {
9-
Literal,
14+
StringLiteral,
1015
Separator,
16+
PositiveNumber,
17+
NegativeNumber,
1118
Number,
1219
Identifier,
13-
Sign,
20+
ArithmeticSign,
1421
Eof
1522
};
1623

17-
Token() = default;
18-
Token(Type type, const std::string& image) :
19-
type_(type), image_(image) {}
24+
/**
25+
* @brief Constructs an EOF token.
26+
*/
27+
Token();
28+
29+
/**
30+
* @brief Construct a new token with the given type and image
31+
*/
32+
Token(Type type, const std::string& image = "");
33+
2034
Token(const Token&) = default;
2135
Token& operator=(const Token&) = default;
36+
Token(Token&&) = default;
37+
Token& operator=(Token&&) = default;
2238

23-
Type type() const {
24-
return type_;
25-
}
26-
27-
const std::string& image() const {
28-
return image_;
29-
}
39+
/**
40+
* @brief Equality comparison with another token
41+
* @see operator== with string and Token::Type
42+
*/
43+
bool operator==(const Token&) const;
44+
45+
/**
46+
* @return true if the token's image is equal to other
47+
* @param other String to compare
48+
*/
49+
bool operator==(const std::string&) const;
50+
51+
/**
52+
* If the token's type is Number or if other is Number,
53+
* then it also matches PositiveNumber and NegativeNumber
54+
*
55+
* @return true if the token's type is equal to other
56+
* @param other Type to compare
57+
*/
58+
bool operator==(Token::Type) const;
3059

31-
unsigned long long toUInt() const {
32-
return std::stoul(image_);
33-
}
60+
/**
61+
* @see operator==
62+
*/
63+
bool operator!=(const Token&) const;
3464

35-
long long toInt() const {
36-
return std::stol(image_);
37-
}
65+
/**
66+
* @see operator==
67+
*/
68+
bool operator!=(const std::string&) const;
69+
70+
/**
71+
* @see operator==
72+
*/
73+
bool operator!=(Token::Type) const;
3874

39-
double toDouble() const {
40-
return std::stod(image_);
41-
}
75+
/**
76+
* Using the standard library's functionnalities, parses the
77+
* image to an unsigned integer
78+
*/
79+
unsigned long long toUInt() const;
80+
81+
/**
82+
* Same as above but outputs a signed integer
83+
*/
84+
long long toInt() const;
85+
86+
/**
87+
* Same as above but outputs a floating-point number
88+
*/
89+
double toDouble() const;
90+
91+
/**
92+
* @brief The token's type
93+
*/
94+
Type type;
4295

43-
private:
44-
Type type_;
45-
std::string image_;
96+
/**
97+
* @brief The token's image, ie. the string that represents it
98+
*/
99+
std::string image;
46100
};
47101

48102
#endif

src/parsing/Tokenizer.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,59 @@ bool isEOF(char c) {
2828
return c == '\0';
2929
}
3030

31+
// I decided to move the implementation of of the Token class
32+
// from Token.h to Tokenizer.cpp
33+
// IMPLEMENTATION: Token class
34+
Token::Token()
35+
: type(Token::Eof), image() {}
36+
37+
Token::Token(Token::Type t, const std::string& i)
38+
: type(t), image(i) { }
39+
40+
bool Token::operator==(const std::string& other) const {
41+
return image == other;
42+
}
43+
44+
bool Token::operator==(Token::Type other) const {
45+
if(type == Number) {
46+
return other == Number || other == PositiveNumber || other == NegativeNumber;
47+
}
48+
else if(other == Number) {
49+
return type == Number || type == PositiveNumber || type == NegativeNumber;
50+
}
51+
52+
return type == other;
53+
}
54+
55+
bool Token::operator==(const Token& other) const {
56+
return (*this == other.image) && (*this == other.type);
57+
}
58+
59+
bool Token::operator!=(const std::string& other) const {
60+
return !(*this == other);
61+
}
62+
63+
bool Token::operator!=(Token::Type other) const {
64+
return !(*this == other);
65+
}
66+
67+
bool Token::operator!=(const Token& other) const {
68+
return !(*this == other);
69+
}
70+
71+
unsigned long long Token::toUInt() const {
72+
return std::stoul(image);
73+
}
74+
75+
long long Token::toInt() const {
76+
return std::stol(image);
77+
}
78+
79+
double Token::toDouble() const {
80+
return std::stod(image);
81+
}
82+
// END OF IMPLEMENTATION Token class
83+
3184
Token Tokenizer::getCurrentToken() const {
3285
return currentToken;
3386
}
@@ -80,13 +133,13 @@ Token Tokenizer::getNextToken() {
80133
currentChar = getNextChar();
81134
}
82135
else if (currentChar == '+') {
83-
currentToken = Token(Token::Sign, std::string(1, currentChar));
136+
currentToken = Token(Token::ArithmeticSign, std::string(1, currentChar));
84137
currentChar = getNextChar();
85138
}
86139
else if (currentChar == '-') {
87140
currentChar = getNextChar();
88141
if (!isDigit(currentChar))
89-
currentToken = Token(Token::Sign, "-");
142+
currentToken = Token(Token::ArithmeticSign, "-");
90143
else { // Negative number
91144
std::string literal = "-";
92145
while (isDigit(currentChar) || currentChar == '.') {
@@ -113,7 +166,7 @@ Token Tokenizer::getNextToken() {
113166
std::cout << "Error in literal parsing: reached EOF before literal end" << std::endl;
114167
}
115168

116-
currentToken = Token(Token::Literal, literal);
169+
currentToken = Token(Token::StringLiteral, literal);
117170
}
118171
else if (isDigit(currentChar)) {
119172
std::string literal = std::string(1, currentChar);
@@ -172,8 +225,8 @@ void Tokenizer::skipLine() {
172225
void Tokenizer::skipUntil(const std::string& token) {
173226
unsigned long long initLine = lineCount();
174227

175-
while(currentToken.image() != token &&
176-
currentToken.type() != Token::Eof) {
228+
while(currentToken.image != token &&
229+
currentToken.type != Token::Eof) {
177230
getNextToken();
178231
}
179232

0 commit comments

Comments
 (0)