-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cc
236 lines (184 loc) · 5.16 KB
/
lexer.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
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
#include <cctype>
#include "lexer.h"
auto token_to_string(TokenType type) -> std::string_view {
switch (type) {
case TokenType::OpenCurlyBrace:
return "{";
case TokenType::CloseCurlyBrace:
return "}";
case TokenType::OpenBrace:
return "[";
case TokenType::CloseBrace:
return "]";
case TokenType::Colon:
return ":";
case TokenType::Comma:
return ",";
case TokenType::StringLiteral:
return "string literal";
case TokenType::NumberLiteral:
return "number literal";
case TokenType::BooleanTrue:
return "true";
case TokenType::BooleanFalse:
return "false";
case TokenType::Null:
return "null";
case TokenType::EndOfFile:
return "end of file";
case TokenType::Garbage:
return "garbage";
}
}
auto Token::to_string() const -> std::string {
std::string result;
result.append(token_to_string(m_type));
if (m_lexeme.empty())
return result;
result.append(": ");
result.append(m_lexeme);
return result;
}
auto Token::type() const -> TokenType {
return m_type;
}
auto Token::lexeme() const -> const std::string& {
return m_lexeme;
}
auto JsonLexer::is_eof() const -> bool {
return m_cursor >= m_input.length();
}
auto JsonLexer::get_token() -> Token {
skip_whitespaces();
if (is_eof())
return Token(TokenType::EndOfFile);
switch (current()) {
case '{':
advance();
return Token(TokenType::OpenCurlyBrace);
case '}':
advance();
return Token(TokenType::CloseCurlyBrace);
case '[':
advance();
return Token(TokenType::OpenBrace);
case ']':
advance();
return Token(TokenType::CloseBrace);
case ':':
advance();
return Token(TokenType::Colon);
case ',':
advance();
return Token(TokenType::Comma);
case 't':
return get_boolean();
case 'f':
return get_boolean();
case 'n':
return get_null();
case '"':
return get_string_literal();
default:
break;
}
if (std::isdigit(current()))
return get_number_literal();
std::string lexeme;
do {
lexeme.push_back(current());
advance();
} while (not is_eof() and not std::isspace(current()));
return Token(TokenType::Garbage, std::move(lexeme));
}
auto JsonLexer::get_boolean() -> Token {
std::string lexeme;
do {
lexeme.push_back(current());
advance();
} while (not is_eof() and std::isalpha(current()));
if (lexeme == "true")
return Token(TokenType::BooleanTrue);
if (lexeme == "false")
return Token(TokenType::BooleanFalse);
return Token(TokenType::Garbage, std::move(lexeme));
}
auto JsonLexer::get_number_literal() -> Token {
if (not std::isdigit(current())) {
std::string lexeme;
do {
lexeme.push_back(current());
advance();
} while (not is_eof() and not std::isspace(current()));
return Token(TokenType::Garbage, std::move(lexeme));
}
std::string number;
do {
number.push_back(current());
advance();
} while (not is_eof() and std::isdigit(current()));
if (current() == '.') {
number.push_back(current());
advance();
if (is_eof() or not std::isdigit(current())) {
do {
number.push_back(current());
advance();
} while (not is_eof() and not std::isspace(current()));
return Token(TokenType::Garbage, std::move(number));
}
do {
number.push_back(current());
advance();
} while (not is_eof() and std::isdigit(current()));
}
if (number[0] == '0')
return Token(TokenType::Garbage, std::move(number));
return Token(TokenType::NumberLiteral, std::move(number));
}
auto JsonLexer::get_string_literal() -> Token {
if (current() != '"') {
std::string lexeme;
do {
lexeme.push_back(current());
advance();
} while (not is_eof() and not std::isspace(current()));
return Token(TokenType::Garbage, std::move(lexeme));
}
// skip the '"'
advance();
std::string content;
do {
content.push_back(current());
advance();
} while (not is_eof() and current() != '"');
if (is_eof())
return Token(TokenType::Garbage, std::move(content));
// skip the '"'
advance();
return Token(TokenType::StringLiteral, std::move(content));
}
auto JsonLexer::get_null() -> Token {
std::string lexeme;
do {
lexeme.push_back(current());
advance();
} while (not is_eof() and std::isalpha(current()));
if (lexeme != "null")
return Token(TokenType::Garbage, std::move(lexeme));
return Token(TokenType::Null);
}
auto JsonLexer::current() const -> char {
if (is_eof())
return 0;
return m_input[m_cursor];
}
auto JsonLexer::advance() -> void {
if (is_eof())
return;
m_cursor++;
}
auto JsonLexer::skip_whitespaces() -> void {
while (not is_eof() and std::isspace(current()))
advance();
}