-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_statements.cpp
More file actions
414 lines (356 loc) · 15.8 KB
/
Copy pathparse_statements.cpp
File metadata and controls
414 lines (356 loc) · 15.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <variant>
#include <vector>
#include <sstream>
#include <memory>
#include "statement.h"
#include "parser.h"
#include "namespace.h"
#include "expression.h"
#include "proof.h"
#include "proofsyntax.h"
using std::vector;
using std::pair;
using std::shared_ptr;
[[nodiscard]] shared_ptr<const Proof> Parser::readStatementsAndMaybeOneProof(
Namespace& ns,
vector<shared_ptr<const Statement>>& ret,
bool syntaxAssumePermitted
) {
static unsigned long assumeProofsyntaxCounter = 0;
while (true) {
skipWhitespace();
std::println("readStatementsAndMaybeOneProof has remaining: '{}'...", str.substr(0, 100));
if (areAtEnd() || tryPeekChar(')')) {
return nullptr;
}
if (tryReadKeyword("proofsyntax"sv)) {
ns.addProofSyntax(std::move(*readProofSyntax(false)));
} else if (tryReadKeyword("syntax"sv)) {
Namespace ns2(&ns);
vector<Id> syntaxNames;
{
auto savedPos = currentFilePos();
skipWhitespace();
while (!tryReadChar('(')) {
skipWhitespace();
auto startPos = currentFilePos();
auto syntaxPieceName = readIdentifier("Expected ( or syntax piece name followed by : ("sv);
skipWhitespace();
readChar(':', "Expected :"sv);
Id syntaxPieceId = ns2.make(syntaxPieceName, FileRange::startEnd(startPos, currentFilePos()));
syntaxNames.push_back(syntaxPieceId);
skipWhitespace();
skipParenEnclosedStuff(false);
skipWhitespace();
readChar(',', "Expected ,"sv);
skipWhitespace();
}
rewindTo(savedPos);
}
map<
Id,
vector<pair<
vector<std::variant<Id, char, Space, pair<Id, Id>>>,
shared_ptr<const Expression>
>>
> syntaxId_to_piecesAndExpr;
while (true) {
skipWhitespace();
if (tryReadChar('(')) break;
auto syntaxPieceName = readIdentifier("Expected ( or syntax piece name followed by : ("sv);
optional<Id> syntaxPieceId;
for (auto& sn : syntaxNames) {
if (sn.name == syntaxPieceName) { syntaxPieceId = sn; break; }
}
if (!syntaxPieceId.has_value()) throw 12345;
skipWhitespace();
readChar(':', "Expected :"sv);
skipWhitespace();
readChar('(', "Expected ("sv);
vector<pair<
vector<std::variant<Id, char, Space, pair<Id, Id>>>,
shared_ptr<const Expression>
>> syntaxVariants;
while (true) {
Namespace ns3(&ns2);
skipWhitespace();
readChar('(', "Expected ("sv);
skipWhitespace();
auto parts = readSyntaxPieces(ns3);
skipWhitespace();
readChar(')', "Expected )"sv);
skipWhitespace();
readChar('=', "Expected ="sv);
auto expr = readExpression(ns3, ',');
syntaxVariants.emplace_back(std::make_pair(std::move(parts), std::move(expr)));
skipWhitespace();
if (tryReadChar(')')) break;
readChar(',', "Expected ) or ,"sv);
skipWhitespace();
if (tryReadChar(')')) break;
}
syntaxId_to_piecesAndExpr.emplace(std::make_pair(syntaxPieceId.value(), std::move(syntaxVariants)));
skipWhitespace();
readChar(',', "Expected ,"sv);
}
skipWhitespace();
Namespace ns3(&ns2);
auto parts = readSyntaxPieces(ns3);
if (parts.size() == 0) throw SyntaxError("Main syntax must not be empty"sv, currentFilePos());
skipWhitespace();
readChar(')', "Expected )"sv);
skipWhitespace();
readChar('=', "Expected ="sv);
auto expr = readExpression(ns3, ',');
skipWhitespace();
readChar(',', "Expected , followed by 'precedence'");
skipWhitespace();
readKeyword("precedence"sv, "Expected keyword 'precedence'"sv);
skipWhitespace();
optional<long> precedence;
if (tryReadKeyword("reset"sv)) {
precedence = std::nullopt;
} else {
precedence = readInteger("Expected 'reset' or integer specifying precedence"sv);
}
skipWhitespace();
readChar(',', "Expected , followed by 'associativity'");
skipWhitespace();
readKeyword("associativity"sv, "Expected keyword 'associativity'"sv);
skipWhitespace();
Associativity associativity;
if (tryReadKeyword("left"sv)) associativity = LEFT;
else if (tryReadKeyword("right"sv)) associativity = RIGHT;
else if (tryReadKeyword("noassoc"sv)) associativity = NOASSOC;
else throw SyntaxError("Expected left/right/noassoc"sv, currentFilePos());
skipWhitespace();
readChar(';', "Expected ;"sv);
ns.addSyntax(Syntax(
std::move(syntaxId_to_piecesAndExpr), std::move(parts), std::move(expr), precedence, associativity
));
} else if (tryReadKeyword("scope"sv)) {
skipWhitespace();
FilePos openingBracePos = currentFilePos();
readChar('(', "Expected ( after keyword 'scope'"sv);
Namespace ns2(&ns);
vector<shared_ptr<const Statement>> statements;
auto endExpr = readStatementsAndMaybeOneProof(ns2, statements, syntaxAssumePermitted);
if (!tryReadChar(')')) {
throw SyntaxError("Expected ) to match ("sv, openingBracePos, currentFilePos());
}
readChar(';', "Expected ; at end of scope block"sv);
skipWhitespace();
if (endExpr != nullptr) throw SyntaxError("Proof at end of block is not allowed in scope block"sv, endExpr->fileRange);
ret.push_back(std::make_shared<Statement_Block>(std::move(statements)));
} else if (tryReadKeyword("atom"sv)) {
vector<Id> atomIds;
do {
skipWhitespace();
auto atomNameStart = currentFilePos();
auto atomName = tryReadIdentifier();
if (!atomName.has_value()) {
throw SyntaxError("Expected identifier (atom name) after keyword 'atom'", currentFilePos());
}
Id atomId = ns.make(atomName.value(), FileRange::startEnd(atomNameStart, currentFilePos()));
atomIds.push_back(atomId);
skipWhitespace();
} while (tryReadChar(','));
readChar(';', "Expected ; after atom declaration"sv);
ret.push_back(std::make_shared<Statement_Atoms>(std::move(atomIds)));
} else if (tryReadKeyword("print"sv)) {
skipWhitespace();
auto proof = readProof(ns, ';', syntaxAssumePermitted);
//skipWhitespace();
readChar(';', "Expected ; after print statement"sv);
ret.push_back(std::make_shared<Statement_Print>(std::move(proof)));
} else if (tryReadKeyword("musterror"sv)) {
skipWhitespace();
auto proof = readProof(ns, ';', syntaxAssumePermitted);
//skipWhitespace();
readChar(';', "Expected ; after musterror statement"sv);
ret.push_back(std::make_shared<Statement_MustError>(std::move(proof)));
} else if (tryReadKeyword("syntaxassume"sv)) {
if (!syntaxAssumePermitted) throw SyntaxError("syntaxssume is only permitted in an 'assume proofsyntax'"sv, currentFilePos());
skipWhitespace();
auto assumptionNameStart = currentFilePos();
auto assumptionName = readIdentifier("Expected identifier (assumption name) after keyword 'syntaxassume'"sv);
auto assumptionNameEnd = currentFilePos();
skipWhitespace();
readKeyword("proves"sv, "Expected keyword 'proves' after assumption name"sv);
skipWhitespace();
auto assumedProposition = readExpression(ns, (char)0x00);
skipWhitespace();
readChar(';', "Expected ; after assumed proposition"sv);
skipWhitespace();
Id assumptionId = ns.make(assumptionName, FileRange::startEnd(assumptionNameStart, assumptionNameEnd));
ret.push_back(std::make_shared<Statement_SyntaxAssume>(assumptionId, std::move(assumedProposition)));
} else if (tryReadKeyword("assume"sv)) {
skipWhitespace();
if (tryReadKeyword("proofsyntax"sv)) {
auto pos = currentFilePos();
ns.addProofSyntax(std::move(*readProofSyntax(true)));
string_view name = *new std::string("assume_proofsyntax_"s + std::to_string(pos.line) + "_" + std::to_string(pos.col)+"_"+std::to_string(assumeProofsyntaxCounter));
ret.push_back(std::make_shared<Statement_Assume>(
// TODO don't leak
Id::make(name),
std::make_shared<Expression_Id>(FileRange::startEnd(pos, currentFilePos()), ns.make(name, FileRange::startEnd(pos, currentFilePos())))
));
} else {
auto assumptionNameStart = currentFilePos();
auto assumptionName = readIdentifier("Expected identifier (assumption name) after keyword 'assume'"sv);
auto assumptionNameEnd = currentFilePos();
skipWhitespace();
readKeyword("proves"sv, "Expected keyword 'proves' after assumption name"sv);
skipWhitespace();
auto assumedProposition = readExpression(ns, (char)0x00);
skipWhitespace();
readChar(';', "Expected ; after assumed proposition"sv);
skipWhitespace();
Id assumptionId = ns.make(assumptionName, FileRange::startEnd(assumptionNameStart, assumptionNameEnd));
ret.push_back(std::make_shared<Statement_Assume>(assumptionId, std::move(assumedProposition)));
}
} else if (tryReadKeyword("require"sv)) {
skipWhitespace();
auto requirementNameStart = currentFilePos();
auto requirementName = readIdentifier("Expected identifier (requirement name) after keyword 'require'"sv);
auto requirementNameEnd = currentFilePos();
skipWhitespace();
readKeyword("proves"sv, "Expected keyword 'proves' after requirement name"sv);
skipWhitespace();
auto requiredProposition = readExpression(ns, (char)0x00);
skipWhitespace();
readKeyword("by"sv, "Expected keyword 'by' after required proposition"sv);
skipWhitespace();
auto proof = readProof(ns, ';', syntaxAssumePermitted);
//skipWhitespace();
readChar(';', "Expected ; after proof"sv);
skipWhitespace();
Id requirementId = ns.make(requirementName, FileRange::startEnd(requirementNameStart, requirementNameEnd));
ret.push_back(std::make_shared<Statement_Require>(requirementId, std::move(requiredProposition), std::move(proof)));
} /*else if (tryReadKeyword("equivalent"sv)) {
skipWhitespace();
vector<vector<shared_ptr<const Statement>>> statementss;
auto openSquareBracketPos = currentFilePos();
readChar('[', "Expected [ after keyword 'equivalent'"sv);
do {
skipWhitespace();
auto openBracePos = currentFilePos();
readChar('{', "Expected { to start block in equivalent list"sv);
Namespace ns2(&ns);
auto [statements, endProofExpr] = readStatementsAndMaybeOneProof(ns2);
skipWhitespace();
if (!tryReadChar('}')) throw SyntaxError("Expected } to match {"sv, currentFilePos(), openBracePos);
if (endProofExpr != nullptr) throw SyntaxError("Proof at end of block not allowed"sv, endProofExpr->fileRange);
skipWhitespace();
statementss.emplace_back(std::move(statements));
} while (tryReadChar(','));
if (!tryReadChar(']')) throw SyntaxError("Expected ] to match ["sv, currentFilePos(), openSquareBracketPos);
skipWhitespace();
readChar(';', "Expected ; at end of 'equivalent' statement"sv);
ret.push_back(std::make_shared<Statement_Equivalent>(std::move(statementss)));
}*/
else if (tryReadKeyword("forany"sv)) {
vector<Id> varIds;
do {
skipWhitespace();
auto varNameStart = currentFilePos();
auto varName = tryReadIdentifier();
if (!varName.has_value()) {
throw SyntaxError("Expected identifier (variable name) or : in forany variable list", currentFilePos());
}
varIds.push_back(ns.make(varName.value(), FileRange::startEnd(varNameStart, currentFilePos())));
skipWhitespace();
} while (tryReadChar(','));
readChar(':', "Expected : after forany variable list"sv);
skipWhitespace();
ret.push_back(std::make_shared<Statement_ForAny>(std::move(varIds)));
} else {
auto statementStartPos = currentFilePos();
auto str_ = readUntilCharOrEnd(';', true);
if (tryReadChar(';')) {
auto statementEndPos = currentFilePos();
auto str = string_view(&str_[0], str_.size()+1);
Parser parser(str);
//parser.line = filePos.line;
//parser.col = filePos.col;
//parser.index = TODO
std::println("statement = '{}'", str);
auto trim = [](string_view sv)noexcept->string_view{
string_view ret = sv;
while (ret.size() != 0 && (ret[0] == ' ' || ret[0] == '\t' || ret[0] == '\n' || ret[0] == '\r')) ret = ret.substr(1);
while (ret.size() != 0 && (ret.back() == ' ' || ret.back() == '\t' || ret.back() == '\n' || ret.back() == '\r')) ret = ret.substr(0, ret.size()-1);
return ret;
};
parser.skipWhitespace();
auto startPos = parser.currentFilePos();
for (auto& proofSyntax : ns.getProofSyntaxes()) {
span<const ProofSyntax_OutputSegment> outputSegments;
if (proofSyntax.mainParseDefinition.size() >= 2 &&
std::holds_alternative<ProofSyntax_ParsePiece_Typed>(proofSyntax.mainParseDefinition[proofSyntax.mainParseDefinition.size()-1]) &&
std::holds_alternative<ProofSyntax_Type_Any>(std::get<ProofSyntax_ParsePiece_Typed>(proofSyntax.mainParseDefinition[proofSyntax.mainParseDefinition.size()-1]).type) &&
std::holds_alternative<ProofSyntax_ParsePiece_Semicolon>(proofSyntax.mainParseDefinition[proofSyntax.mainParseDefinition.size()-2])
) {
if (
proofSyntax.mainOutput.size() >= 1 &&
std::holds_alternative<ProofSyntax_OutputSegment_Variable>(proofSyntax.mainOutput.back()) &&
std::get<ProofSyntax_OutputSegment_Variable>(proofSyntax.mainOutput.back()).varName == std::get<ProofSyntax_ParsePiece_Typed>(proofSyntax.mainParseDefinition[proofSyntax.mainParseDefinition.size()-1]).name
) {
outputSegments = span(proofSyntax.mainOutput).subspan(0, proofSyntax.mainOutput.size()-1);
} else if (
proofSyntax.mainOutput.size() >= 2 &&
std::holds_alternative<ProofSyntax_OutputSegment_Variable>(proofSyntax.mainOutput[proofSyntax.mainOutput.size()-2]) &&
std::get<ProofSyntax_OutputSegment_Variable>(proofSyntax.mainOutput[proofSyntax.mainOutput.size()-2]).varName == std::get<ProofSyntax_ParsePiece_Typed>(proofSyntax.mainParseDefinition[proofSyntax.mainParseDefinition.size()-1]).name &&
std::holds_alternative<ProofSyntax_OutputSegment_Literal>(proofSyntax.mainOutput.back()) &&
trim(std::get<ProofSyntax_OutputSegment_Literal>(proofSyntax.mainOutput.back()).strv()).size() == 0
) {
outputSegments = span(proofSyntax.mainOutput).subspan(0, proofSyntax.mainOutput.size()-2);
} else {
continue;
}
} else {
continue;
}
auto proofSyntaxSubstitute = [](span<const ProofSyntax_OutputSegment> outputSegments, const ProofSyntax& proofSyntax, const map<string_view, ProofSyntax_MatchedValue>& matches)->std::string{
std::stringstream newString;
for (auto& outputSegment : outputSegments) {
getOutputSegmentStr(outputSegment, proofSyntax, matches, newString);
}
return std::move(newString).str();
};
map<string_view, ProofSyntax_MatchedValue> matches;
if (tryReadProofByCustomSyntax(parser, ns, span(proofSyntax.mainParseDefinition).subspan(0, proofSyntax.mainParseDefinition.size()-1), proofSyntax, matches, {NothingHere{}}, 0)) {
parser.skipWhitespace();
if (parser.areAtEnd()) {
std::string* newString_ = new std::string(proofSyntaxSubstitute(outputSegments, proofSyntax, matches)); // TODO: leak
//std::print("isAssumed={}\n\n{}\n", proofSyntax.isAssumed, *newString_);
std::print("\nCustom proofsyntax gave:\n{}\n", *newString_);
Parser subParser(*newString_);
auto proofAtEnd = subParser.readStatementsAndMaybeOneProof(ns, ret, proofSyntax.isAssumed);
if (proofAtEnd != nullptr) {
throw SyntaxError("Proof is only allowed at end of block"sv, currentFilePos());
} else {
goto good;
}
} else {
parser.rewindTo(startPos);
}
} else {
parser.rewindTo(startPos);
}
}
throw SyntaxError("Could not parse statement"sv, FileRange::startEnd(statementStartPos, statementEndPos));
good:;
} else {
rewindTo(statementStartPos);
FilePos exprStart = currentFilePos();
auto proof = readProof(ns, ')', syntaxAssumePermitted);
//skipWhitespace();
if (!tryPeekChar(')')) {
throw SyntaxError("Expected ). Proof is only allowed at end of block"sv, currentFilePos(), exprStart);
} else {
return proof;
}
}
}
}
}