-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.hpp
230 lines (188 loc) · 5.19 KB
/
parse.hpp
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
#pragma once
#include "common.hpp"
#include "data.hpp"
#include <optional>
struct Location {
unsigned line {};
unsigned col {};
std::vector<std::string_view> nest {};
};
struct Parser {
std::string_view input;
Location location {};
};
enum class ErrorType {
none,
unexpectedEnd,
highIndentation,
duplicateName,
emptyName,
mixedTableArray,
emptyTableArray
};
struct Error {
ErrorType type;
Location location;
std::string_view data {}; // dependent on 'type'
};
struct NamedValue {
Value value;
std::string_view name {}; // optional, might be empty
};
using ParseResult = std::variant<NamedValue, Error>;
ParseResult parse(Parser& parser);
ParseResult parseTableOrArray(Parser& parser) {
using std::move;
std::optional<bool> isTable;
Table table;
Vector vector;
while(!parser.input.empty()) {
auto after = parser.input;
auto first = after.find_first_not_of('\t');
if(first == after.npos) {
parser.location.col += first;
parser.input = {}; // reached end of document
break;
}
// Comment, skip to next line.
// Notice how this comes before the indent check.
// Comments don't have to be aligned, we don't care.
if(after[first] == '#') {
auto nl = after.find('\n');
if(nl == after.npos) {
// reached end of document
parser.location.col += parser.input.size();
parser.input = {};
break;
}
++parser.location.line;
parser.location.col = 0u;
parser.input = after.substr(nl + 1);
continue;
}
// Empty lines are also always allowed
if(after[first] == '\n') {
++parser.location.line;
parser.location.col = 0u;
parser.input = after.substr(first + 1);
continue;
}
// indentation is suddenly too high
if(first > parser.location.nest.size()) {
return Error{ErrorType::highIndentation, parser.location};
}
// indentation is too low, line does not belong to this value anymore
if(first < parser.location.nest.size()) {
break;
}
parser.location.col += first;
parser.input = after.substr(first);
if(parser.input.empty()) {
return Error{ErrorType::unexpectedEnd, parser.location};
}
// NOTE: extended array-nest syntax
if(parser.input[0] == '-' && parser.input[1] == '\n') {
if(isTable && *isTable) {
return Error{ErrorType::mixedTableArray, parser.location};
}
parser.input = parser.input.substr(2);
parser.location.col = 0u;
++parser.location.line;
parser.location.nest.push_back(std::to_string(vector.size()));
isTable = {false};
auto res = parseTableOrArray(parser);
if(auto err = std::get_if<Error>(&res)) {
return {*err};
}
parser.location.nest.pop_back();
auto& nv = std::get<NamedValue>(res);
vector.push_back(std::make_unique<Value>(std::move(nv.value)));
continue;
}
auto ploc = parser.location; // save it for later
auto res = parse(parser);
if(auto err = std::get_if<Error>(&res)) {
return {*err};
}
auto& nv = std::get<NamedValue>(res);
if(isTable && *isTable == nv.name.empty()) {
return Error{ErrorType::mixedTableArray, ploc};
}
auto v = std::make_unique<Value>(move(nv.value));
if(nv.name.empty()) {
isTable = {false};
vector.push_back(move(v));
} else {
isTable = {true};
if(!table.emplace(nv.name, std::move(v)).second) {
return Error{ErrorType::duplicateName, ploc, nv.name};
}
}
}
if(!isTable) {
return Error{ErrorType::mixedTableArray, parser.location};
}
NamedValue nv;
nv.value = *isTable ? Value{{move(table)}} : Value{{move(vector)}};
return nv;
}
ParseResult parse(Parser& parser) {
using std::move;
// constexpr auto whitespace = "\n\t\f\r\v "; // as by std::isspace
if(parser.input.empty()) {
return Error{ErrorType::unexpectedEnd, parser.location};
}
auto nl = parser.input.find("\n");
auto line = parser.input;
auto after = std::string_view {};
Location afterLoc = parser.location;
if(nl != parser.input.npos) {
line = parser.input.substr(0, nl);
after = parser.input.substr(nl + 1);
afterLoc.col = 0u;
++afterLoc.line;
}
auto sep = line.find(":");
if(sep == parser.input.npos) {
parser.input = after;
parser.location = afterLoc;
return NamedValue{Value{std::string(line)}};
}
auto [name, val] = split(line, sep);
// Strip all beginning whitespace from 'val' and all ending whitespace
// from 'name'.
// auto vws = val.find_first_not_of(whitespace);
// if(vws != val.npos) {
// val.remove_prefix(vws);
// }
// auto nws = name.find_last_not_of(whitespace);
// if(nws != val.npos) {
// name = name.substr(0, nws + 1);
// }
if(!val.empty() && val[0] == ' ') {
val.remove_prefix(1);
}
if(name.empty()) {
return Error{ErrorType::emptyName, parser.location};
}
if(!val.empty()) { // table assignment
parser.input = after;
parser.location = afterLoc;
return NamedValue{Value{std::string(val)}, name};
}
// if it's neither a table assignment or an array value,
// we have a nested table.
parser.input = after;
parser.location = afterLoc;
parser.location.nest.push_back(name);
auto res = parseTableOrArray(parser);
assert(parser.location.nest.back() == name);
parser.location.nest.pop_back();
if(auto err = std::get_if<Error>(&res)) {
return *err;
}
auto& nv = std::get<NamedValue>(res);
assert(nv.name.empty());
nv.name = name;
return {std::move(nv)};
}