-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
140 lines (119 loc) · 3.39 KB
/
lexer.cpp
File metadata and controls
140 lines (119 loc) · 3.39 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
#include "lexer.hpp"
#include <stdexcept>
Lexer::Lexer(std::string str) : src_(std::move(str)) {}
bool Lexer::isAtEnd() const { return pos_ >= src_.size(); }
char Lexer::peek() const { return isAtEnd() ? '\0' : src_[pos_]; }
char Lexer::advance() {
if (isAtEnd())
return '\0';
char c = src_[pos_++];
if (c == '\n') {
line_++;
col_ = 1;
} else {
col_++;
}
return c;
}
std::vector<Token> Lexer::tokenize() {
std::vector<Token> out;
while (!isAtEnd()) {
char c = peek();
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
advance();
continue;
}
std::size_t startPos = pos_;
int startLine = line_;
int startCol = col_;
// string literal
if (c == '"') {
advance(); // opening quote
std::string value;
while (!isAtEnd() && peek() != '"') {
char ch = advance();
if (ch == '\\') {
if (isAtEnd())
throw std::runtime_error("unterminated escape at " +
std::to_string(startLine) + ":" +
std::to_string(startCol));
char esc = advance();
switch (esc) {
case 'n':
value.push_back('\n');
break;
case 't':
value.push_back('\t');
break;
case '"':
value.push_back('"');
break;
case '\\':
value.push_back('\\');
break;
default:
value.push_back(esc);
break;
}
} else {
value.push_back(ch);
}
}
if (isAtEnd())
throw std::runtime_error("unterminated string at " +
std::to_string(startLine) + ":" +
std::to_string(startCol));
advance(); // closing quote
out.push_back(Token{TokenKind::String, value, startLine, startCol});
continue;
}
// Identifier / Keyword
if (isIdentStart(c)) {
advance();
while (!isAtEnd() && isIdentCont((unsigned char)peek()))
advance();
Token t;
t.lexeme = src_.substr(startPos, pos_ - startPos);
t.line = startLine;
t.col = startCol;
t.kind = (t.lexeme == "var" || t.lexeme == "while" || t.lexeme == "if" ||
t.lexeme == "else" || t.lexeme == "print" ||
t.lexeme == "true" || t.lexeme == "false")
? TokenKind::Keyword
: TokenKind::Identifier;
out.push_back(std::move(t));
continue;
}
// Number
if (std::isdigit((unsigned char)c)) {
advance();
while (!isAtEnd() && std::isdigit((unsigned char)peek()))
advance();
Token t;
t.kind = TokenKind::Number;
t.lexeme = src_.substr(startPos, pos_ - startPos);
t.line = startLine;
t.col = startCol;
out.push_back(std::move(t));
continue;
}
// Symbol
char a = advance();
std::string s{a};
if (!isAtEnd()) {
char b = peek();
if ((a == '=' && b == '=') || (a == '!' && b == '=') ||
(a == '<' && b == '=') || (a == '>' && b == '=')) {
s.push_back(advance());
}
}
Token t;
t.kind = TokenKind::Symbol;
t.lexeme = std::move(s);
t.line = startLine;
t.col = startCol;
out.push_back(std::move(t));
}
out.push_back(Token{TokenKind::End, "", line_, col_});
return out;
}