-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.cc
208 lines (195 loc) · 4.48 KB
/
scanner.cc
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
#include "scanner.h"
std::string Scanner::get_lexeme(TokenType type) {
if (type == STRING)
return source.substr(start + 1, current - start - 2);
else if (type == CHAR)
return source.substr(start + 1, current - start - 2);
else
return source.substr(start, current - start);
}
void Scanner::addToken(TokenType type) {
auto t = Token(type, get_lexeme(type), line);
tokens.push_back(t);
}
void Scanner::error(std::string message) {
errorMessage = message;
badbit = true;
std::cerr << "line " << line << ": " << errorMessage << std::endl;
exit(-1);
}
char Scanner::advance() {
if (current == source.size()) {
error("Unexpected EOF");
}
return source[current++];
}
char Scanner::peek() { return current >= source.size() ? 0 : source[current]; }
char Scanner::peekNext() {
return current + 1 >= source.size() ? 0 : source[current + 1];
}
bool Scanner::match(char expected) {
if (peek() == expected) {
advance();
return true;
}
return false;
}
void Scanner::string() {
while (peek() != '"') {
if (peek() == '\n') line++;
advance();
}
advance();
addToken(STRING);
}
void Scanner::readChar() {
while (peek() != '\'') {
if (peek() == '\n') line++;
advance();
}
advance();
std::string lexeme = get_lexeme(CHAR);
int len = lexeme.size();
if (len == 1)
;
else if (len == 2) {
if (source[start + 1] == '\\') {
char c = source[start + 2];
char cc = 0;
if (c == 'n')
cc = '\n';
else if (c == 't')
cc = '\t';
else if (c == '0')
cc = '\0';
else
error("unimplemented escape sequence");
lexeme = std::string(1, cc);
} else
error("bad escape sequence");
} else
error("bad char literal " + lexeme);
tokens.push_back(Token(CHARACTER, lexeme, line));
}
void Scanner::number() {
while (isdigit(peek())) advance();
if (peek() == '.') {
advance();
if (isdigit(peek())) {
while (isdigit(peek())) advance();
} else {
error("broken number expression when lexing");
}
}
addToken(NUMBER);
}
void Scanner::identifierOrKeyword() {
while (isalnum(peek())) advance();
std::string lexeme = get_lexeme(INVALID);
TokenType t = string2keyword(lexeme);
addToken(t == INVALID ? IDENTIFIER : t);
}
void Scanner::scanToken() {
char c = advance();
switch (c) {
case '"':
string();
break;
case '*':
addToken(match('=') ? STAR_EQUAL : STAR);
break;
case '+':
addToken(match('+') ? PLUSPLUS : match('=') ? PLUS_EQUAL : PLUS);
break;
case '-':
addToken(match('-') ? MINUSMINUS
: match('>') ? RIGHT_ARROW
: match('=') ? MINUS_EQUAL
: MINUS);
break;
case '/':
if (match('/')) {
while (peek() != '\n' && !eof()) advance();
} else if (match('*')) {
char prev = 0, now = 0;
while (!eof()) {
prev = now;
now = advance();
if (now == '\n') line++;
if (now == '/' && prev == '*') break;
}
if (now == '/' && prev == '*')
;
else
error("unterminate block comment");
} else {
addToken(match('=') ? SLASH_EQUAL : SLASH);
}
break;
case '!':
addToken(match('=') ? BANG_EQUAL : BANG);
break;
case '=':
addToken(match('=') ? EQUAL_EQUAL : EQUAL);
break;
case '<':
addToken(match('=') ? LESS_EQUAL : LESS);
break;
case '>':
addToken(match('=') ? GREATER_EQUAL : GREATER);
break;
case '%':
addToken(match('=') ? PERCENT_EQUAL : PERCENT);
break;
case '(':
addToken(LEFT_PAREN);
break;
case ')':
addToken(RIGHT_PAREN);
break;
case '[':
addToken(LEFT_SQUARE);
break;
case ']':
addToken(RIGHT_SQUARE);
break;
case '{':
addToken(LEFT_BRACE);
break;
case '}':
addToken(RIGHT_BRACE);
break;
case ',':
addToken(COMMA);
break;
case '.':
addToken(DOT);
break;
case ';':
addToken(SEMICOLON);
break;
case '\'':
readChar();
break;
case '\n':
line++;
case ' ':
case '\t':
break;
default:
if (isdigit(c))
number();
else if (isalpha(c))
identifierOrKeyword();
else {
error("Unexpected character");
}
}
}
std::vector<Token> Scanner::scanTokens() {
while (!eof()) {
start = current;
scanToken();
}
return tokens;
}