-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_proofsyntax.cpp
More file actions
348 lines (301 loc) · 10.7 KB
/
Copy pathparse_proofsyntax.cpp
File metadata and controls
348 lines (301 loc) · 10.7 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
#include <sstream>
#include "proofsyntax.h"
#include "parser.h"
ProofSyntax_Type Parser::readParseType() {
skipWhitespace();
auto ident = readIdentifier("Expected parse type"sv);
if (ident == "IDENT"sv) return ProofSyntax_Type_Identifier();
if (ident == "PAREN"sv) return ProofSyntax_Type_Parentheses();
//if (ident == "ANY"sv) return ProofSyntax_Type_Any();
skipWhitespace();
if (tryReadChar('.')) {
skipWhitespace();
auto alternativeIndex = readInteger("Expected integer"sv);
return ProofSyntax_Type_SubParseSpecificAlternative(ident, alternativeIndex);
} else {
return ProofSyntax_Type_SubParse(ident);
}
}
ProofSyntax_ParsePiece Parser::readParsePiece() {
skipWhitespace();
if (tryPeekChar(')')) { std::println("\n'{}'\n", this->str.substr(0, 100)); throw "wtf 983589235"; }
if (tryReadChar(';')) {
return ProofSyntax_ParsePiece_Semicolon();
} else if (tryReadChar('(')) {
skipWhitespace();
std::string_view name;
if (auto maybeName=tryReadIdentifier(); maybeName.has_value()) {
name = maybeName.value();
skipWhitespace();
if (tryReadChar(')')) {
return ProofSyntax_ParsePiece_Typed(name, ProofSyntax_Type_Any());
}
}
readChar(':', "Expected :"sv);
skipWhitespace();
auto type = readParseType();
skipWhitespace();
readChar(')', "Expected )"sv);
return ProofSyntax_ParsePiece_Typed(name, std::move(type));
} else if (auto keyword=tryReadIdentifier()) {
return ProofSyntax_ParsePiece_Keyword(keyword.value());
} else {
auto startPos = currentFilePos();
while (!areAtEnd()) {
if (!isOperatorChar(str[0])) break;
auto _ = readNonWhitespaceChar();
}
if (startPos.index == currentFilePos().index) {
std::println("\n'{}'\n", str.substr(0, 100));
throw SyntaxError("Expected ( or ) or keyword or operator"sv, currentFilePos());
} else {
return ProofSyntax_ParsePiece_OperatorChars(getStringViewFromTo(startPos, currentFilePos()));
}
}
}
ProofSyntax_OutputSegment Parser::readStuffInDoubleParens_sub() {
skipWhitespace();
auto startPos = currentFilePos();
if (tryReadChar('"')) {
std::stringstream ret;
bool backslashing = false;
while (true) {
if (areAtEnd()) throw SyntaxError("Expected \" to match \""sv, startPos, currentFilePos());
char c = str[0];
if (c == '\n' || c == '\r') throw SyntaxError("Encountered newline inside string literal, please use \\n and/or \\r", currentFilePos());
str = str.substr(1);
col++;
if (backslashing) {
if (c == 'r') ret << '\r';
else if (c == 'n') ret << '\n';
else if (c == 't') ret << '\t';
else ret << c;
backslashing = false;
} else {
if (c == '\\') backslashing = true;
else if (c == '"') break;
else ret << c;
}
}
return ProofSyntax_OutputSegment_Literal(std::move(ret).str());
}
ProofSyntax_OutputSegment ret = [this]()->ProofSyntax_OutputSegment{
if (tryReadChar('(')) {
auto ret = readStuffInDoubleParens();
skipWhitespace();
readChar(')', "Expected )"sv);
return ret;
} else {
return ProofSyntax_OutputSegment_Variable{.varName = readIdentifier("Expected identifier"sv)};
}
}();
skipWhitespace();
while (true) {
if (tryReadChar('.')) {
skipWhitespace();
auto ident = readIdentifier("Expected identifier"sv);
ret = ProofSyntax_OutputSegment_Sub{
.lhs = std::make_shared<ProofSyntax_OutputSegment>(std::move(ret)),
.rhs = ident
};
} else {
return ret;
}
skipWhitespace();
}
}
ProofSyntax_OutputSegment Parser::readStuffInDoubleParens() {
skipWhitespace();
auto firstStuff = readStuffInDoubleParens_sub();
skipWhitespace();
if (tryPeekChar(',') || tryPeekChar(')')) return firstStuff;
if (!std::holds_alternative<ProofSyntax_OutputSegment_Variable>(firstStuff)) throw SyntaxError("Expected , or )"sv, currentFilePos());
vector<shared_ptr<ProofSyntax_OutputSegment>> funcArgs;
while (true) {
funcArgs.push_back(std::make_shared<ProofSyntax_OutputSegment>(readStuffInDoubleParens_sub()));
skipWhitespace();
if (tryPeekChar(',') || tryPeekChar(')')) break;
}
return ProofSyntax_OutputSegment_FunctionCall{
.funcName = std::get<ProofSyntax_OutputSegment_Variable>(firstStuff).varName, .args = std::move(funcArgs)
};
}
ProofSyntax_OutputSegment Parser::readSyntaxOutputSegment(unsigned int& depth) {
if (str.size() == 0) throw SyntaxError("File ended in the middle of syntax output spec"sv, currentFilePos());
//if (tryPeekChar(')')) throw SyntaxError("Unexpected )"sv, currentFilePos());
auto tryReadSyntaxOutputSegment_doubleParen = [this]() -> optional<ProofSyntax_OutputSegment> {
auto startPos = currentFilePos();
if (tryReadChar('(')) {
if (tryReadChar('(')) {
auto startPos2 = currentFilePos();
while (true) {
skipWhitespace();
if (tryPeekChar('(')) {
skipParenEnclosedStuff(false);
} else if (str.size() >= 2 && str[0] == ')' && str[1] == ')') {
break;
} else if (tryPeekChar(')')) {
rewindTo(startPos);
return std::nullopt;
} else {
auto _ = readNonWhitespaceChar();
}
}
rewindTo(startPos2);
auto stuff = readStuffInDoubleParens();
readChar(')', "Expected ))"sv);
readChar(')', "Expected )"sv);
return stuff;
}
}
rewindTo(startPos);
return std::nullopt;
};
if (auto doubleParenContent=tryReadSyntaxOutputSegment_doubleParen(); doubleParenContent.has_value()) {
return std::move(doubleParenContent.value());
}
auto startPos = currentFilePos();
while (true) {
skipWhitespace();
if (areAtEnd()) throw SyntaxError("Unexpected end of file"sv, currentFilePos());
auto beforePos = currentFilePos();
if (tryReadSyntaxOutputSegment_doubleParen().has_value()) {
rewindTo(beforePos);
break;
}
if (tryReadChar('(')) depth++;
else if (depth == 0 && tryPeekChar(')')) break;
else if (tryReadChar(')')) depth--;
else { auto _ = readNonWhitespaceChar(); }
}
auto literal = getStringViewFromTo(startPos, currentFilePos());
//std::println("literal='{}' depth={}", literal, depth);
return ProofSyntax_OutputSegment_Literal(literal);
}
shared_ptr<ProofSyntax> Parser::readProofSyntax(bool assumed) {
map<std::string_view, ProofSyntax_SubParseDefinition> subParseDefinitions;
vector<ProofSyntax_Function> functions;
optional<vector<ProofSyntax_ParsePiece>> mainParseDefinition;
optional<vector<ProofSyntax_OutputSegment>> mainOutput;
while (true) {
if (tryReadChar(';')) break;
skipWhitespace();
if (tryReadChar(',')) {
skipWhitespace();
if (tryReadChar(';')) break;
}
if (auto maybeIdent=tryReadIdentifier(); maybeIdent.has_value()) {
skipWhitespace();
if (tryReadChar(':')) {
// It's a type
skipWhitespace();
readChar('(', "Expected ( after :"sv);
vector<vector<ProofSyntax_ParsePiece>> alternatives;
while (true) {
skipWhitespace();
readChar('(', "Expected ("sv);
vector<ProofSyntax_ParsePiece> parsePieces;
std::println("Reading sub syntax");
while (true) {
skipWhitespace();
if (tryReadChar(')')) break;
parsePieces.push_back(readParsePiece());
}
alternatives.push_back(std::move(parsePieces));
std::println("Read sub syntax: {} pieces", parsePieces.size());
skipWhitespace();
if (!tryReadChar(',')) {
readChar(')', "Expected , or )"sv);
break;
}
skipWhitespace();
if (tryReadChar(')')) break;
}
subParseDefinitions[maybeIdent.value()] = ProofSyntax_SubParseDefinition(std::move(alternatives));
} else {
// It's a function
vector<pair<string_view, ProofSyntax_Type>> paramNamesAndTypes;
while (true) {
skipWhitespace();
if (areAtEnd()) throw SyntaxError("Unexpected end of file"sv, currentFilePos());
if (tryReadChar('=')) break;
if (auto ident = tryReadIdentifier(); ident.has_value()) {
paramNamesAndTypes.emplace_back(ident.value(), ProofSyntax_Type_Any());
} else {
readChar('(', "Expected = or identifier or ("sv);
skipWhitespace();
auto paramName = readIdentifier("Expected identifier (parameter name)"sv);
skipWhitespace();
readChar(':', "Expected : followed by parameter type"sv);
skipWhitespace();
auto paramType = readParseType();
skipWhitespace();
readChar(')', "Expected ) after parameter type"sv);
paramNamesAndTypes.emplace_back(paramName, std::move(paramType));
}
}
skipWhitespace();
if (tryReadChar('(')) {
unsigned int depth = 0;
vector<ProofSyntax_OutputSegment> outputSegments;
while (true) {
if (areAtEnd()) throw SyntaxError("Unexpected end of file"sv, currentFilePos());
if (depth == 0 && tryReadChar(')')) break;
outputSegments.push_back(readSyntaxOutputSegment(depth));
}
functions.emplace_back(
maybeIdent.value(),
std::move(paramNamesAndTypes),
std::move(outputSegments)
);
} else {
throw SyntaxError("Expected ( after ="sv, currentFilePos());
/*unsigned int depth = 0;
auto output = readSyntaxOutputSegment(depth);
functions.emplace_back(
maybeIdent.value(),
std::move(paramNamesAndTypes),
std::move(output)
);*/
}
}
} else if (tryReadChar('(')) {
if (mainParseDefinition.has_value()) throw SyntaxError("Only one main parse definition is allowed"sv, currentFilePos());
// It's the main syntax
std::println("Reading main syntax...");
skipWhitespace();
vector<ProofSyntax_ParsePiece> parsePieces;
while (true) {
parsePieces.push_back(readParsePiece());
skipWhitespace();
if (tryReadChar(')')) break;
}
std::println("Read main syntax: {} pieces", parsePieces.size());
skipWhitespace();
readChar('=', "Expected ="sv);
skipWhitespace();
readChar('(', "Expected ("sv);
mainParseDefinition = std::move(parsePieces);
unsigned int depth = 0;
vector<ProofSyntax_OutputSegment> outputSegments;
while (true) {
//std::println("Reading output segment from '{}'", this->str.substr(0, 100));
if (areAtEnd()) throw SyntaxError("Unexpected end of file"sv, currentFilePos());
if (depth == 0 && tryReadChar(')')) break;
outputSegments.push_back(readSyntaxOutputSegment(depth));
}
mainOutput = std::move(outputSegments);
//std::println("Read main output: {} segments", outputSegments.size());
} else {
throw SyntaxError("Expected identifier or ( inside proof syntax"sv, currentFilePos());
}
}
if (!mainParseDefinition.has_value()) throw SyntaxError("No main parse definition in proofsyntax"sv, currentFilePos());
return std::make_shared<ProofSyntax>(ProofSyntax{
.subParseDefinitions = std::move(subParseDefinitions),
.functions = std::move(functions),
.mainParseDefinition = std::move(mainParseDefinition.value()),
.mainOutput = std::move(mainOutput.value()),
.isAssumed = assumed,
});
}