forked from JonathanMeans/EvilC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
256 lines (237 loc) · 6.25 KB
/
lexer.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "lexer.h"
#include "ErrorReporter.h"
#include <cassert>
#include <map>
#include <sstream>
namespace
{
char rot13(char c)
{
if (isupper(c))
{
if (c + 13 <= 'Z')
return c + 13;
return c - 13;
}
if (islower(c))
{
if (c + 13 <= 'z')
return c + 13;
return c - 13;
}
return c;
}
const std::map<std::string, TokenType> KEYWORDS = {{"int", TokenType::INT},
{"return",
TokenType::RETURN}};
}
EnvironmentalLimitsException::EnvironmentalLimitsException(const char* msg) :
std::runtime_error(msg)
{
}
bool Token::operator==(const Token& rhs) const
{
return this->type == rhs.type && this->lexeme == rhs.lexeme &&
this->location == rhs.location;
}
std::ostream& operator<<(std::ostream& os, const Token& token)
{
os << token.location.line << ":" << token.location.column << ":"
<< token.lexeme;
return os;
}
bool FileLocation::operator==(const FileLocation& rhs) const
{
return this->offset == rhs.offset && this->line == rhs.line &&
this->column == rhs.column;
}
FileLocation::FileLocation(unsigned int offset,
unsigned int line,
unsigned int column) :
offset(offset),
line(line), column(column)
{
}
FileLocation::FileLocation() : offset(0), line(1), column(1)
{
}
void FileLocation::increment(char c)
{
offset++;
if (c == '\n')
{
line++;
column = 1;
}
else
{
column++;
}
if (column >= 512)
{
// Max line length is 510 (one more than 509 in 2.2.4.1)
// One more for EOF character
// And columns start at 1, rather than 0, so plus one for that
// 512 total
std::stringstream ss;
ss << "Line " << line << " is too long";
throw EnvironmentalLimitsException(ss.str().c_str());
}
}
Lexer::Lexer(std::istream& source,
ErrorReporter& errors,
const Options& options) :
mHasNext(true),
mSource(source),
mErrors(errors), mOptions(options), mLocation()
{
}
void Lexer::skipWhitespace()
{
while (isspace(mSource.peek()))
getNextChar();
}
char Lexer::getNextChar()
{
char c = mSource.get();
const auto originalPosition = mSource.tellg();
bool isTrigraph = false;
if (mOptions.rot13)
c = rot13(c);
// Handle trigraph sequences. See 2.2.1.1
if (c == '?')
{
char trigraph[3] = {};
trigraph[0] = c;
trigraph[1] = mSource.get();
trigraph[2] = mSource.get();
// If we hit the end of the stream, not enough chars to make a trigraph, so go back and return original ?
if (!mSource.good())
{
mSource.clear();
mSource.seekg(originalPosition, std::ios::beg);
mLocation.increment(c);
return c;
}
if (trigraph[1] == '?')
{
isTrigraph = true;
switch (trigraph[2])
{
case '=':
c = '#';
break;
case '(':
c = '[';
break;
case '/':
c = '\\';
break;
case ')':
c = ']';
break;
case '\'':
c = '^';
break;
case '<':
c = '{';
break;
case '!':
c = '|';
break;
case '>':
c = '}';
break;
case '-':
c = '~';
break;
default:
isTrigraph = false;
break;
}
if (!isTrigraph)
{
mSource.seekg(-2, std::ios::cur);
}
}
}
mLocation.increment(c);
if (isTrigraph)
{
// Advance twice more to eat up the other chars
mLocation.increment(c);
mLocation.increment(c);
}
return c;
}
bool Lexer::hasNext() const
{
return mHasNext;
}
Token Lexer::next()
{
std::string lexeme;
skipWhitespace();
const auto tokenLocation = mLocation;
char c = getNextChar();
if (isalpha(c))
{
lexeme.push_back(c);
while (isalnum(mSource.peek()))
{
lexeme.push_back(getNextChar());
}
const auto keywordIt = KEYWORDS.find(lexeme);
const auto tokenType = keywordIt == KEYWORDS.end()
? TokenType::IDENTIFIER
: keywordIt->second;
return {tokenType, lexeme, tokenLocation};
}
else if (isdigit(c))
{
lexeme.push_back(c);
while (isdigit(mSource.peek()))
{
lexeme.push_back(getNextChar());
}
return {TokenType::INTEGER, lexeme, tokenLocation};
}
else if (c == '{')
return {TokenType::LBRACE, "{", tokenLocation};
else if (c == '}')
return {TokenType::RBRACE, "}", tokenLocation};
else if (c == '(')
return {TokenType::LPAREN, "(", tokenLocation};
else if (c == ')')
return {TokenType::RPAREN, ")", tokenLocation};
else if (c == '[')
return {TokenType::LBRACKET, "[", tokenLocation};
else if (c == ']')
return {TokenType::RBRACKET, "]", tokenLocation};
else if (c == '#')
return {TokenType::HASH, "#", tokenLocation};
else if (c == '^')
return {TokenType::CARAT, "^", tokenLocation};
else if (c == '|')
return {TokenType::PIPE, "|", tokenLocation};
else if (c == '~')
return {TokenType::TILDE, "~", tokenLocation};
else if (c == ';')
return {TokenType::SEMICOLON, ";", tokenLocation};
else if (c == '?')
return {TokenType::QUESTION, "?", tokenLocation};
else if (c == EOF)
{
mHasNext = false;
return {TokenType::EOS, "", tokenLocation};
}
else
{
const Token result{TokenType::ERROR, std::string(1, c), tokenLocation};
std::stringstream errorMessage;
errorMessage << "stray '" << result.lexeme << "' in program";
mErrors.reportDiagnostic(
{DiagnosticType::ERROR, result, errorMessage.str()});
return result;
}
}