-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.cpp
94 lines (82 loc) · 2.01 KB
/
Token.cpp
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
#include <string>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include "Token.h"
const std::string gTokenTypeNames[] = {
// Reserved words
"VOID", "MAIN", "INT", "COUT", "ENDL",
"IF", "WHILE", "FOR", "AND", "OR", "DO", "RESERVED_COUNT",
// Scope tokens
"SEMICOLON",
"LEFT_PAREN", "RIGHT_PAREN",
"LEFT_BRACE", "RIGHT_BRACE",
// Relational tokens
"LESS", "LESS_EQUAL",
"GREATER", "GREATER_EQUAL",
"EQUAL", "NOT_EQUAL",
"BITWISE_AND", "BITWISE_OR",
"NOT", "AND", "OR",
"INSERTION", "EXTRACTION", "ASSIGNMENT",
"PLUS", "MINUS", "MULTIPLY", "DIVIDE",
"PLUS_EQUALS", "MINUS_EQUALS",
"MULTIPLY_BY",
// Other token types
"IDENTIFIER", "INTEGER",
"BAD", "END_FILE"};
std::string toUpper(std::string in)
{
std::string up = in;
for (int i = 0; i < up.length(); i++)
{
up[i] = toupper(up[i]);
}
return up;
}
const std::string &Token::GetTokenTypeName(TokenType type)
{
return gTokenTypeNames[type];
}
Token::Token()
{
}
Token::Token(TokenType type, const std::string &lexeme)
: type(type), lexeme(lexeme), filename(""), line(1)
{
}
Token::Token(TokenType type, const std::string &lexeme, std::string fileName, int line)
: type(type), lexeme(lexeme), filename(fileName), line(line) {}
Token::~Token()
{
}
const std::string &Token::GetTokenTypeName() const
{
return gTokenTypeNames[this->type];
}
TokenType Token::GetTokenType() const
{
return this->type;
}
std::string Token::GetLexeme() const
{
return this->lexeme;
}
void Token::CheckReserved()
{
for (int i = 0; i < RESERVED_COUNT; i++)
{
if (toUpper(this->lexeme) == gTokenTypeNames[i])
{
this->type = TokenType(i);
}
}
}
std::ostream &operator<<(std::ostream &out, const Token &tc)
{
out.setf(std::ios::left);
out << "type: "
<< "\e[31m" << std::setw(15) << tc.GetTokenTypeName() << "\e[0m"
<< "lexeme: "
<< "\e[31m" << std::setw(5) << tc.GetLexeme() << "\e[0m";
return out;
}