-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse.cpp
More file actions
382 lines (353 loc) · 10.8 KB
/
Copy pathparse.cpp
File metadata and controls
382 lines (353 loc) · 10.8 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <parse.h>
#include <Interpreter.h>
#include <Term.h>
#include <chartype.h>
#include <nested_exception.h>
#include <util.h>
#include <utf8.h>
#include <limits>
#include <map>
#include <string>
namespace {
typedef std::numeric_limits<double> double_limits;
typedef std::vector<Term> Terms;
typedef std::pair<std::string, Terms> Command;
const std::map<std::string, Terms> commands {
Command("f32", Terms { Term::FLOAT, Term::Width(32) }),
Command("f64", Terms { Term::FLOAT, Term::Width(64) }),
Command("utf8", Terms { Term::UNICODE, Term::Width(8) }),
Command("utf16", Terms { Term::UNICODE, Term::Width(16) }),
Command("utf32", Terms { Term::INTEGER, Term::Width(32) }),
Command("ucs2", Terms { Term::INTEGER, Term::Width(16) }),
Command("native", Terms { Term::NATIVE }),
Command("little", Terms { Term::LITTLE }),
Command("big", Terms { Term::BIG }),
Command("epsilon", Terms { Term::write(double_limits::epsilon()) }),
Command("nan", Terms { Term::write(double_limits::quiet_NaN()) }),
Command("+inf", Terms { Term::write(double_limits::infinity()) }),
Command("-inf", Terms { Term::write(-double_limits::infinity()) }),
};
template<class I, class O>
bool accept(uint32_t, I&, I, O);
template<class I>
bool accept(uint32_t, I&, I);
template<class P, class I, class O>
bool accept_if(P, I&, I, O);
template<class P, class I>
bool accept_if(P, I&, I);
template<class T, class... Args>
bool transition(T&, const T&, Args&&...);
template<class T, class... Args>
bool transition_if(T&, const T&, Args&&...);
enum State {
NORMAL,
NUMBER,
COMMENT,
IDENTIFIER,
SIGNED,
UNSIGNED,
ZERO,
BINARY,
OCTAL,
DECIMAL,
HEXADECIMAL,
FLOAT,
STRING,
ESCAPE,
};
Term write_double_term(const std::string&);
Term write_integer_term(const std::string&, int);
unsigned long parse_width(const std::string&);
}
void parse_internal
(std::istream&, Interpreter&, unsigned int&, unsigned int&);
void parse(std::istream& input, Interpreter& interpreter) {
unsigned int line = 1;
unsigned int column = 0;
try {
parse_internal(input, interpreter, line, column);
} catch (...) {
::throw_with_nested(std::runtime_error
(join("At line ", line, ", column ", column, ":")));
}
}
void parse_internal(std::istream& input, Interpreter& interpreter,
unsigned int& line, unsigned int& column) {
State state = NORMAL;
std::vector<Term> terms;
std::string token;
auto append = std::back_inserter(token);
std::vector<uint32_t> runes;
auto here = runes.begin(), end = runes.end();
while (true) {
if (here == end) {
std::string buffer;
std::getline(input, buffer);
runes.clear();
utf8::utf8to32(buffer.begin(), buffer.end(), std::back_inserter(runes));
if (!input.eof()) {
runes.push_back(U'\n');
++line;
}
here = runes.begin();
end = runes.end();
}
switch (state) {
case NORMAL:
token.clear();
column = here - runes.begin();
if (accept_if(is_whitespace, here, end)
|| transition(state, STRING, U'"', here, end)
|| transition(state, UNSIGNED, U'u', here, end, append)
|| transition(state, SIGNED, U's', here, end, append)
|| transition_if(state, IDENTIFIER, is_alphabetic, here, end, append)
|| transition(state, COMMENT, U'#', here, end)
|| transition(state, NUMBER, U'+', here, end, append)
|| transition(state, NUMBER, U'-', here, end, append))
break;
if (accept(U'{', here, end))
terms.push_back(Term::push());
else if (accept(U'}', here, end))
terms.push_back(Term::pop());
else
state = NUMBER;
break;
case NUMBER:
if (transition(state, ZERO, U'0', here, end, append)
|| transition_if(state, DECIMAL, is_decimal, here, end, append)
|| transition_if(state, IDENTIFIER, is_alphabetic, here, end, append))
break;
if (here == end)
return;
{
std::string message("Invalid character: '");
utf8::append(*here, std::back_inserter(message));
message += "'.";
throw std::runtime_error(message);
}
case COMMENT:
if (transition(state, NORMAL, U'\n', here, end))
break;
if (here == end)
return;
++here;
break;
case IDENTIFIER:
if (accept_if(is_alphanumeric, here, end, append))
break;
{
const auto command = commands.find(token);
if (command == commands.end())
throw std::runtime_error(join
("Unimplemented command: '", token, "'.\n"));
terms.insert(terms.end(),
command->second.begin(), command->second.end());
}
state = NORMAL;
break;
case UNSIGNED:
if (accept_if(is_decimal, here, end, append)
|| transition_if(state, IDENTIFIER, is_alphabetic, here, end, append)
|| transition(state, IDENTIFIER, U'_', here, end, append))
break;
{
const auto width = parse_width(token);
terms.push_back(Term::INTEGER);
terms.push_back(Term::UNSIGNED);
terms.push_back(Term::Width(width));
state = NORMAL;
}
break;
case SIGNED:
if (accept_if(is_decimal, here, end, append)
|| transition_if(state, IDENTIFIER, is_alphabetic, here, end, append)
|| transition(state, IDENTIFIER, U'_', here, end, append))
break;
{
const auto width = parse_width(token);
terms.push_back(Term::INTEGER);
terms.push_back(Term::SIGNED);
terms.push_back(Term::Width(width));
state = NORMAL;
}
break;
case ZERO:
if (transition(state, BINARY, U'b', here, end)
|| transition(state, OCTAL, U'o', here, end)
|| transition(state, HEXADECIMAL, U'x', here, end)
|| transition(state, FLOAT, U'.', here, end, append))
break;
state = DECIMAL;
break;
case BINARY:
if (accept_if(is_binary, here, end, append)
|| (accept(U'_', here, end)))
break;
terms.push_back(write_integer_term(token, 2));
state = NORMAL;
break;
case OCTAL:
if (accept_if(is_octal, here, end, append)
|| accept(U'_', here, end))
break;
terms.push_back(write_integer_term(token, 8));
state = NORMAL;
break;
case DECIMAL:
if (accept_if(is_decimal, here, end, append)
|| accept(U'_', here, end)
|| transition(state, FLOAT, U'.', here, end, append))
break;
terms.push_back(write_integer_term(token, 10));
state = NORMAL;
break;
case HEXADECIMAL:
if (accept_if(is_hexadecimal, here, end, append)
|| accept(U'_', here, end))
break;
terms.push_back(write_integer_term(token, 16));
state = NORMAL;
break;
case FLOAT:
if (accept_if(is_decimal, here, end, append)
|| accept(U'_', here, end))
break;
terms.push_back(write_double_term(token));
state = NORMAL;
break;
case STRING:
if (transition(state, NORMAL, U'"', here, end)
|| transition(state, ESCAPE, U'\\', here, end))
break;
if (here == end)
throw std::runtime_error("Unexpected end of file in string.");
terms.push_back(Term::write(Term::Unsigned(*here++)));
break;
case ESCAPE:
{
Term::Unsigned value = 0;
switch (*here) {
case U'"': value = U'"'; break;
case U'a': value = U'\a'; break;
case U'b': value = U'\b'; break;
case U'e': value = U'\e'; break;
case U'f': value = U'\f'; break;
case U'n': value = U'\n'; break;
case U'r': value = U'\r'; break;
case U't': value = U'\t'; break;
case U'v': value = U'\v'; break;
case U'\\': value = U'\\'; break;
default:
{
std::string message("Invalid escape character: '");
utf8::append(*here, std::back_inserter(message));
message += "'.";
throw std::runtime_error(message);
}
}
terms.push_back(Term::write(value));
++here;
state = STRING;
}
}
interpreter.run(terms);
terms.clear();
}
}
namespace {
Term write_double_term(const std::string& token) {
char* boundary;
const auto value = std::strtod(token.c_str(), &boundary);
if (boundary != token.c_str() + token.size())
throw std::runtime_error
(join("Invalid floating-point literal: \"", token, "\""));
return Term::write(value);
}
Term write_integer_term(const std::string& token, int base) {
const bool has_sign = token[0] == '-' || token[0] == '+';
char* boundary;
const auto begin = token.c_str(), end = begin + token.size();
if (has_sign) {
Term::Signed value = std::strtoll(begin, &boundary, base);
if (boundary != end)
throw std::runtime_error(join("Invalid base-", base,
" signed integer literal: \"", token, "\""));
return Term::write(value);
} else {
Term::Unsigned value = std::strtoull(begin, &boundary, base);
if (boundary != end)
throw std::runtime_error(join("Invalid base-", base,
" unsigned integer literal: \"", token, "\""));
return Term::write(value);
}
}
unsigned long parse_width(const std::string& token) {
const auto begin = token.c_str() + 1;
char* boundary;
const auto width = std::strtoul(begin, &boundary, 10);
if (boundary != token.c_str() + token.size())
throw std::runtime_error
(join("Invalid unsigned type: '", token, "'."));
if (width == 0 || width > 64)
throw std::runtime_error
(join("Unsupported width of unsigned type: '", token, "'."));
return width;
}
template<class I, class O>
bool accept(uint32_t rune, I& input, I end, O output) {
if (input == end)
return false;
if (*input == rune) {
utf8::append(*input, output);
++input;
return true;
}
return false;
}
template<class I>
bool accept(uint32_t rune, I& input, I end) {
if (input == end)
return false;
if (*input == rune) {
++input;
return true;
}
return false;
}
template<class P, class I, class O>
bool accept_if(P predicate, I& input, I end, O output) {
if (input == end)
return false;
if (predicate(*input)) {
utf8::append(*input++, output);
return true;
}
return false;
}
template<class P, class I>
bool accept_if(P predicate, I& input, I end) {
if (input == end)
return false;
if (predicate(*input)) {
++input;
return true;
}
return false;
}
template<class T, class... Args>
bool transition(T& state, const T& target, Args&&... args) {
if (accept(std::forward<Args>(args)...)) {
state = target;
return true;
}
return false;
}
template<class T, class... Args>
bool transition_if(T& state, const T& target, Args&&... args) {
if (accept_if(std::forward<Args>(args)...)) {
state = target;
return true;
}
return false;
}
}