-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.h
47 lines (38 loc) · 1.32 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
#pragma once
#include <string>
#include <sstream>
enum TokenType {
COMMA, PERIOD, Q_MARK, LEFT_PAREN, RIGHT_PAREN,COLON,COLON_DASH,MULTIPLY,ADD,SCHEMES,FACTS,RULES,QUERIES,ID,STRING,COMMENT,UNDEFINED,END_F
};
class Token {
private:
TokenType type;
std::string value;
int lineNumber;
static const std::string typeNames[];
public:
Token(TokenType type, std::string value, int lineNumber)
: type(type), value(value), lineNumber(lineNumber) {}
std::string toString() const {
std::stringstream out;
std::string typeStr = typeName(this->type);
out << "(" << typeStr << "," << "\"" << value << "\"" << "," << lineNumber << ")";
return out.str();
}
int getLineNumber() const {
return lineNumber;
}
TokenType getType() const {
return type;
}
std::string getValue() const {
return value;
}
std::string typeName(TokenType type) const {
return typeNames[type];
}
};
const std::string Token::typeNames[] = {
"COMMA", "PERIOD", "Q_MARK", "LEFT_PAREN", "RIGHT_PAREN", "COLON", "COLON_DASH", "MULTIPLY", "ADD",
"SCHEMES", "FACTS", "RULES", "QUERIES", "ID", "STRING", "COMMENT", "UNDEFINED", "EOF"
};