-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.h
125 lines (111 loc) · 3.08 KB
/
Token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Created by ki11errabbit on 9/4/22.
//
#ifndef CWITHCLASSES_TOKEN_H
#define CWITHCLASSES_TOKEN_H
#include <string>
#include <sstream>
enum TokenType {
IDENTIFIER, KEYWORD, CONSTANT, OPERATOR, SPECIALCHAR, STRING, UNDEFINED, COMMENT, BRACE, TERMINATOR, PREPROC
};
enum SubTokenType {//for keywords and operators
NONE=0,TYPE=1, CONTROL=2, CODE=3, ASSIGNMENT, SIZEOF, RETURN, COMMA, OPEN, CLOSE, ACCESSOR
};
class Token {
private:
TokenType type;
SubTokenType subType;
std::string value;
std::string typeName (TokenType type) const {
switch (type) {
case IDENTIFIER:
return "IDENTIFIER";
case KEYWORD:
return "KEYWORD";
case CONSTANT:
return "CONSTANT";
case OPERATOR:
return "OPERATOR";
case SPECIALCHAR:
return "SPECIALCHAR";
case STRING:
return "STRING";
case COMMENT:
return "COMMENT";
case BRACE:
return "BRACE";
case TERMINATOR:
return "TERMINATOR";
case PREPROC:
return "PREPROC";
case UNDEFINED:
default:
return "UNDEFINED";
}
return "ERROR";
}
std::string typeName (SubTokenType type) const {
switch (type) {
case NONE:
return "";
case TYPE:
return "TYPE";
case CONTROL:
return "CONTROL";
case CODE:
return "CODE";
case ASSIGNMENT:
return "ASSIGNMENT";
case SIZEOF:
return "SIZEOF";
case RETURN:
return "RETURN";
case COMMA:
return "COMMA";
case OPEN:
return "OPEN";
case CLOSE:
return "CLOSE";
case ACCESSOR:
return "ACCESSOR";
default:
return "UNDEFINED";
}
return "ERROR";
}
public:
Token (TokenType type, std::string value)
: type(type), value(value) {}
Token (TokenType type, SubTokenType subType, std::string value)
: type(type), subType(subType), value(value) {}
TokenType getType () const {
return type;
}
std::string getValue() const {
return value;
}
SubTokenType getSubType () const {
return subType;
}
void setSubType(SubTokenType type){
subType = type;
}
void setType(TokenType type) {
this->type = type;
}
std::string toString() const {
std::stringstream out;
if (subType != NONE) {
out << typeName(type) << ", " << typeName(subType) << ": " << value;
return out.str();
}
out << typeName(type) << ": " << value;
return out.str();
}
friend std::ostream& operator<<(std::ostream& os, const Token& token)
{
os << token.toString();
return os;
}
};
#endif //CWITHCLASSES_TOKEN_H