-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtoken.cpp
More file actions
242 lines (221 loc) · 6.96 KB
/
token.cpp
File metadata and controls
242 lines (221 loc) · 6.96 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "token.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
#include <sstream>
Token::Token(TokenType t, const Location& w) : type(t), where(w)
{
if (where)
{
ICE_IF(t == Token::Identifier || t == Token::StringLiteral || t == Token::Integer || t == Token::Real,
"Incorrect token type");
}
}
Token::Token(TokenType t, const Location& w, const std::string& str) : type(t), where(w), strVal(str)
{
ICE_IF(t != Token::Identifier && t != Token::StringLiteral, "Invalid token for string argument");
ICE_IF(t == Token::Identifier && str.empty(), "String should not be empty for identifier");
}
Token::Token(TokenType t, const Location& w, uint64_t v) : type(t), where(w), intVal(v)
{
ICE_IF(t != Token::Integer && t != Token::Char, "Invalid token construction");
}
Token::Token(const Location& w, double v) : type(Token::Real), where(w), realVal(v) {}
std::string Token::ToString() const
{
std::stringstream ss;
dump(ss);
return ss.str();
}
void Token::dump() const
{
dump(std::cerr);
}
void Token::dump(std::ostream& out) const
{
out << "Token { Type: " << TypeStr() << " ";
switch (type)
{
case Token::Identifier:
out << strVal << " ";
break;
case Token::StringLiteral:
out << "'" << strVal << "' ";
break;
case Token::Integer:
out << intVal << " ";
break;
case Token::Real:
out << realVal << " ";
break;
case Token::Boolean:
out << std::boolalpha << (bool)intVal << " ";
break;
default:
break;
}
out << "} @ " << where.to_string() << std::endl;
}
struct TokenEntry
{
Token::TokenType type;
bool isKeyWord;
int precedence;
const char* str;
};
const TokenEntry tokenTable[] = {
{ Token::For, true, -1, "for" },
{ Token::To, true, -1, "to" },
{ Token::Downto, true, -1, "downto" },
{ Token::Do, true, -1, "do" },
{ Token::Function, true, -1, "function" },
{ Token::Procedure, true, -1, "procedure" },
{ Token::If, true, -1, "if" },
{ Token::Then, true, -1, "then" },
{ Token::Else, true, -1, "else" },
// clang-format off
{ Token::While, true, -1, "while", },
{ Token::Repeat, true, -1, "repeat", },
{ Token::Until, true, -1, "until", },
// clang-format on
{ Token::Begin, true, -1, "begin" },
{ Token::End, true, -1, "end" },
{ Token::Case, true, -1, "case" },
{ Token::Otherwise, true, -1, "otherwise" },
{ Token::With, true, -1, "with" },
{ Token::Program, true, -1, "program" },
{ Token::Unit, true, -1, "unit" },
{ Token::Module, true, -1, "module" },
{ Token::Write, true, -1, "write" },
{ Token::Writeln, true, -1, "writeln" },
{ Token::WriteStr, true, -1, "writestr" },
{ Token::Read, true, -1, "read" },
{ Token::Readln, true, -1, "readln" },
{ Token::ReadStr, true, -1, "readstr" },
{ Token::Var, true, -1, "var" },
{ Token::Array, true, -1, "array" },
{ Token::Of, true, -1, "of" },
{ Token::Packed, true, -1, "packed" },
{ Token::Record, true, -1, "record" },
{ Token::Class, true, -1, "class" },
{ Token::Class, true, -1, "object" }, /* Synonym! */
{ Token::Type, true, -1, "type" },
{ Token::File, true, -1, "file" },
{ Token::String, true, -1, "string" },
{ Token::Set, true, -1, "set" },
{ Token::Forward, true, -1, "forward" },
{ Token::Inline, true, -1, "inline" },
{ Token::Implementation, true, -1, "implementation" },
{ Token::Interface, true, -1, "interface" },
{ Token::Const, true, -1, "const" },
{ Token::Nil, true, -1, "nil" },
{ Token::And, true, 40, "and" },
{ Token::And_Then, true, 40, "and_then" },
{ Token::Or, true, 10, "or" },
{ Token::Or_Else, true, 10, "or_else" },
{ Token::Not, true, 60, "not" },
{ Token::Div, true, 40, "div" },
{ Token::Mod, true, 40, "mod" },
{ Token::Xor, true, 10, "xor" },
{ Token::Shr, true, 40, "shr" },
{ Token::Shl, true, 40, "shl" },
{ Token::SymDiff, false, 40, "><" },
{ Token::Pow, true, 50, "pow" },
{ Token::In, true, 5, "in" },
{ Token::Plus, false, 10, "+" },
{ Token::Minus, false, 10, "-" },
{ Token::Multiply, false, 40, "*" },
{ Token::Divide, false, 40, "/" },
{ Token::Power, false, 50, "**" },
{ Token::Assign, false, 2, ":=" },
{ Token::LessThan, false, 5, "<" },
{ Token::LessOrEqual, false, 5, "<=" },
{ Token::GreaterOrEqual, false, 5, ">=" },
{ Token::GreaterThan, false, 5, ">" },
{ Token::Equal, false, 5, "=" },
{ Token::NotEqual, false, 5, "<>" },
{ Token::Integer, false, -1, "integer" },
{ Token::Real, false, -1, "real" },
{ Token::Boolean, false, -1, "boolean" },
{ Token::Char, false, -1, "char" },
{ Token::StringLiteral, false, -1, "string" },
{ Token::LeftParen, false, -1, "(" },
{ Token::RightParen, false, -1, ")" },
{ Token::LeftSquare, false, -1, "[" },
{ Token::RightSquare, false, -1, "]" },
{ Token::Comma, false, -1, "," },
{ Token::Semicolon, false, -1, ";" },
{ Token::Colon, false, -1, ":" },
{ Token::Period, false, -1, "." },
{ Token::DotDot, false, -1, ".." },
{ Token::Uparrow, false, -1, "^" },
{ Token::Static, true, -1, "static" },
{ Token::Virtual, true, -1, "virtual" },
{ Token::Override, true, -1, "override" },
{ Token::Private, true, -1, "private" },
{ Token::Public, true, -1, "public" },
{ Token::Protected, true, -1, "protected" },
{ Token::Restricted, true, -1, "restricted" },
{ Token::Constructor, true, -1, "constructor" },
{ Token::Destructor, true, -1, "destructor" },
{ Token::Label, true, -1, "label" },
{ Token::Goto, true, -1, "goto" },
{ Token::Uses, true, -1, "uses" },
{ Token::At, false, -1, "@" },
{ Token::Bindable, true, -1, "bindable" },
{ Token::Default, true, -1, "default" },
{ Token::Value, true, -1, "value" },
{ Token::Import, true, -1, "import" },
{ Token::LineNumber, true, -1, "__LINE__" },
{ Token::FileName, true, -1, "__FILE__" },
{ Token::SizeOf, true, -1, "sizeof" },
{ Token::Identifier, false, -1, "identifier" },
{ Token::Unknown, false, -1, "Unknown" },
{ Token::EndOfFile, false, -1, "EOF" },
};
static const TokenEntry* FindToken(Token::TokenType type)
{
for (auto& i : tokenTable)
{
if (type == i.type)
{
return &i;
}
}
ICE("Could not find token!");
}
static const TokenEntry* FindToken(std::string kw)
{
/* Don't "tolower" the keyword if it starts with __ */
if (kw.substr(0, 2) != "__")
{
strlower(kw);
}
for (auto& i : tokenTable)
{
if (kw == i.str)
{
return &i;
}
}
return 0;
}
std::string Token::TypeStr() const
{
const TokenEntry* t = FindToken(type);
return t->str;
}
int Token::Precedence() const
{
const TokenEntry* t = FindToken(type);
return t->precedence;
}
Token::TokenType Token::KeyWordToToken(const std::string& str)
{
const TokenEntry* t = FindToken(str);
if (t && t->isKeyWord)
{
return t->type;
}
return Token::Unknown;
}