-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
166 lines (149 loc) · 4.09 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
#include <assembler/lexer.hpp>
bool is_alpha(char c)
{
return ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) || (c == '_');
}
bool is_digit(char c)
{
return (c >= '0') && (c <= '9');
}
bool is_binary_digit(char c)
{
return (c == '0') || (c == '1');
}
bool is_hex_digit(char c)
{
return is_digit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
bool is_alpha_numeric(char c)
{
return is_alpha(c) || is_digit(c);
}
bool is_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\r';
}
bool is_linebreak(char c)
{
return c == '\n';
}
std::vector<Token> Lexer::tokens_from_file(std::string const &input_file)
{
auto file_contents = std::ifstream{input_file, std::ios::in};
if (file_contents.fail()) {
throw std::runtime_error("Could not open file " + input_file);
}
auto lex = Lexer{file_contents};
return lex.generate_tokens();
}
Lexer::Lexer(std::istream &ss) : ss(ss)
{
}
std::vector<Token> Lexer::generate_tokens()
{
this->ss.seekg(0);
for (auto i = 0; !this->ss.eof(); ++i) {
auto c = this->next();
this->startcol = this->col;
if (c == EOF) {
break;
} else if (is_whitespace(c)) {
if (c == ' ') {
this->col += 1;
} else if (c == '\t') {
this->col += 4;
}
} else if (is_linebreak(c)) {
this->tokens.push_back(Token{this->line, this->startcol, TokenType::ENDLINE});
this->line += 1;
this->col = 1;
} else if (is_alpha(c)) {
auto data = std::string{c};
this->identifier(data);
} else if (is_digit(c)) {
auto data = std::string{c};
this->number(data);
} else if (c == '/') {
this->comment();
} else if (c == '.') {
this->section();
} else {
throw LexerError("Unexpected '" + std::string{c} + "' at line " + std::to_string(this->line), this->line);
}
}
return this->tokens;
}
void Lexer::identifier(std::string &data)
{
while (is_alpha_numeric(this->peek_next())) {
data += this->next();
}
this->tokens.push_back(Token{this->line, this->startcol, TokenType::IDENTIFIER, data});
}
void Lexer::number(std::string &data)
{
if (data[0] == '0') {
if (this->peek_next() == 'x') {
// Hex
data += this->next(); // consume 'x'
while (is_hex_digit(this->peek_next())) {
data += this->next();
}
} else if (this->peek_next() == 'b') {
// Binary
data += this->next(); // consume 'b'
while (is_binary_digit(this->peek_next())) {
data += this->next();
}
}
} else {
// Decimal
while (is_digit(this->peek_next())) {
data += this->next();
}
}
this->tokens.push_back(Token{this->line, this->startcol, TokenType::INTEGER, data});
}
void Lexer::section()
{
auto data = std::string{"."};
while (is_alpha_numeric(this->peek_next())) {
data += this->next();
}
this->tokens.push_back(Token{this->line, this->startcol, TokenType::SECTION, data});
}
void Lexer::comment()
{
auto c = this->next();
if (c == '/') {
// Single line comment
while (!is_linebreak(this->peek_next())) {
(void) this->next();
continue;
}
} else if (c == '*') {
// Comment block
c = this->next();
while (!(c == '*' && this->peek_next() == '/')) {
c = this->next();
if (is_linebreak(c)) {
this->line += 1;
this->col = 0;
}
continue;
}
(void) this->next(); // consume last '/'
} else {
throw LexerError("Unexpected '" + std::string{c} + "' at line " + std::to_string(this->line) + " (Was expecting a comment)", this->line);
}
}
char Lexer::peek_next()
{
return static_cast<char>(this->ss.peek());
}
char Lexer::next()
{
this->col += 1;
return static_cast<char>(this->ss.get());
}