-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.cpp
184 lines (164 loc) · 3.68 KB
/
scanner.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
#include "scanner.h"
#include <cctype>
#include <iostream>
#include <map>
static std::map<std::string, TokenType> keywords = {
{"and", AND}, {"class", CLASS}, {"else", ELSE}, {"false", FALSE},
{"for", FOR}, {"fun", FUN}, {"if", IF}, {"nil", NIL},
{"or", OR}, {"print", PRINT}, {"return", RETURN}, {"super", SUPER},
{"this", THIS}, {"true", TRUE}, {"var", VAR}, {"while", WHILE},
};
void Scanner::identifier() {
while (std::isalnum(peek()))
advance();
std::string text = source.substr(start, current - start);
TokenType type;
auto it = keywords.find(text);
if (it == keywords.end())
type = IDENTIFIER;
else
type = it->second;
addToken(type);
}
char Scanner::peekNext() {
if (current + 1 >= source.size())
return '\0';
return source[current + 1];
}
void Scanner::number() {
while (std::isdigit(peek()))
advance();
// Look for a fractional part.
if (peek() == '.' && std::isdigit(peekNext())) {
// Consume the "."
advance();
while (std::isdigit(peek()))
advance();
}
// addToken(NUMBER, (source.substr(start, current)));
tokens.push_back(Token(NUMBER, source.substr(start, current - start), line));
}
void Scanner::String() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n')
line++;
advance();
}
// Unterminated string.
if (isAtEnd()) {
std::cerr << "line " << line << " meet unterminated string.\n";
return;
}
// The closing ".
advance();
// Trim the surrounding quotes.
std::string value = source.substr(start + 1, current - 2 - start);
// addToken(STRING, value);
tokens.push_back(Token(STRING, value, line));
}
char Scanner::peek() {
if (isAtEnd())
return '\0';
return source[current];
}
char Scanner::advance() {
current++;
return source[current - 1];
}
void Scanner::addToken(TokenType type) {
std::string text = source.substr(start, current - start);
tokens.push_back(Token(type, text, line));
}
bool Scanner::match(char expected) {
if (isAtEnd())
return false;
if (source[current] != expected)
return false;
current++;
return true;
}
void Scanner::scanToken() {
char c = advance();
switch (c) {
case '(':
addToken(LEFT_PAREN);
break;
case ')':
addToken(RIGHT_PAREN);
break;
case '{':
addToken(LEFT_BRACE);
break;
case '}':
addToken(RIGHT_BRACE);
break;
case ',':
addToken(COMMA);
break;
case '.':
addToken(DOT);
break;
case '-':
addToken(MINUS);
break;
case '+':
addToken(PLUS);
break;
case ';':
addToken(SEMICOLON);
break;
case '*':
addToken(STAR);
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 '"':
String();
break;
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
case '/':
if (match('/')) {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd())
advance();
} else {
addToken(SLASH);
}
break;
default:
if (isdigit(c)) {
number();
} else if (std::isalpha(c)) {
identifier();
} else {
std::cerr << "line " << line << " meet unexpected character.\n";
}
}
}
std::vector<Token> Scanner::scanTokens() {
while (!isAtEnd()) {
// We are at the beginning of the next lexeme.
start = current;
scanToken();
// char c = advance();
}
tokens.push_back(Token(eof, "eof", line));
return tokens;
}