-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg.cpp
executable file
·360 lines (332 loc) · 9.76 KB
/
peg.cpp
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
#pragma warning(disable: 4503)
#include "peg.h"
#include "ParseState.h"
#include "Ast.h"
#include "Action.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
//#include <tr1/functional>
#include <vector>
#include <map>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
//using std::tr1::bind;
//using std::tr1::function;
using std::ostream;
using std::ostringstream;
//using namespace std::tr1::placeholders;
using std::vector;
using std::map;
using std::make_pair;
using std::pair;
using std::shared_ptr;
using std::make_shared;
int parser_id_gen = 0;
extern bool showFails, showRemainder;
ostream& operator<<(ostream& os, const AST& ast)
{
os << ast.to_string();
return os;
}
#include "ParseResult.h"
/*
* Basic function that matches a string at the current state.
* Not a good way to match keywords as most languages want them.
*/
struct token : public ParserBase
{
token(const string& match) : match(match) {}
ParseResultPtr parse(const ParseStatePtr& start)
{
if (start->length() >= match.length())
{
if (start->substr(0, match.length()) == match)
return make_shared<ParseResult>(start->from(match.length()), std::make_shared<IdentifierAST>(match));
}
return ParseResultPtr();
}
string match;
};
// When a character can be from a large set such as a-z this object
// represents testing for a range established at construction time.
struct range : public ParserBase
{
range(int rangeBegin, int rangeEnd) : rangeBegin(rangeBegin), rangeEnd(rangeEnd) {}
ParseResultPtr parse(const ParseStatePtr& start)
{
int ch = start->at(0);
if (ch >= rangeBegin && ch <= rangeEnd)
return make_shared<ParseResult>(start->from(1), std::make_shared<StringAST>(string(1, ch)));
return ParseResultPtr();
}
int rangeBegin;
int rangeEnd;
};
// When a character has to be a given character, this object represents
// testing for a character code established at runtime.
struct ch : public ParserBase
{
ch(int c) : c(c) {}
ParseResultPtr parse(const ParseStatePtr& start)
{
int ch = start->at(0);
if (ch == c)
return make_shared<ParseResult>(start->from(1), std::make_shared<StringAST>(string(1, ch)));
return ParseResultPtr();
}
int c;
};
// When a character must not be a given character, this object represents
// testing for a character code established at runtime.
struct notch : public ParserBase
{
notch(int c) : c(c) {}
ParseResultPtr parse(const ParseStatePtr& start)
{
int ch = start->at(0);
if (ch == 0)
return ParseResultPtr();
if (ch != c)
return make_shared<ParseResult>(start->from(1), std::make_shared<StringAST>(string(1, ch)));
return ParseResultPtr();
}
int c;
};
// When a character must not be a given character, this object represents
// testing for a character code established at compile time.
template <int c> struct tnotch : public ParserBase
{
tnotch() {}
ParseResult* parse(ParseState* start)
{
int ch = start->at(0);
if (ch == 0)
return ParseResultPtr();
if (ch != c)
return make_shared<ParseResult>(start->from(1), std::make_shared<StringAST>(string(1, ch)));
return ParseResultPtr();
}
};
ParseResultPtr WSequenceN::parse(const ParseStatePtr& start)
{
ParseStatePtr t1 = start;
ParseResultPtr r;
shared_ptr<SequenceAST> ast;
Parsers::const_iterator i = elements.begin();
static int sequence_id = 0;
int sequence = ++sequence_id;
while (i != elements.end())
{
ParserPtr parser = *i;
r = skipwhite(t1,parser);
if (!r)
{
if (showFails)
{
cout << name << " failed";
if (showRemainder) cout << " [" << t1->substr(0) << "]";
cout << " because [" << parser->name << "] failed.";
cout << " at " << __LINE__ << endl;
}
return r;
}
if (!ast)
ast = make_shared<SequenceAST>();
ast->append(r->getAST());
t1 = r->getState();
++i;
}
r = r->getNewResult(ast);
//if (showRemainder) std::cout << name <<' ' << sequence << " [" << r->getState()->substr(0) << "]" << std::endl;
return r;
}
ParseResultPtr Choices::parse(const ParseStatePtr& start)
{
ParseStatePtr t1 = start;
ParseResultPtr r;
Parsers::const_iterator i = choices.begin();
string names;
while ((!r) && i != choices.end())
{
ParserPtr parser = *i;
names += " [" + parser->name + "]";
r = (*parser)(t1);
++i;
}
if (!r)
{
if (showFails)
{
std::cout << name << " failed";
if (showRemainder) cout << " [" << t1->substr(0) << "]";
std::cout << " because " << names << " all failed ";
cout << " at " << __LINE__ << std::endl;
}
return r;
}
//if (showRemainder) std::cout << name << " ["<< r->getState()->substr(0) <<"]" << std::endl;
return r;
}
ParseResultPtr repeat1::parse(const ParseStatePtr& start)
{
std::shared_ptr<SequenceAST> ast;
std::shared_ptr<AST> firstAST;
ParseResultPtr rep = (*next)(start);
if (!rep)
{
if (showFails)
{
cout << name<< " failed";
if (showRemainder) cout <<" [" << start->substr(0) << "]";
cout << " because " << next->name << " failed";
cout << " at " << __LINE__ << std::endl;
}
return rep;
}
//ast->append(rep->getAST());
firstAST = rep->getAST();
ParseResultPtr rep2 = (*next)(rep->getState());
while (rep2)
{
if (!ast)
{
ast = std::make_shared<SequenceAST>();
ast->append(firstAST);
}
ast->append(rep2->getAST());
rep = rep2;
rep2 = (*next)(rep2->getState());
}
//if (showRemainder) std::cout << name<< " [" << rep->getState()->substr(0) << "]"<< std::endl;
if (!ast)
return rep->getNewResult(firstAST);
return rep->getNewResult(ast);
}
ParseResultPtr repeat0::parse(const ParseStatePtr& start)
{
shared_ptr<SequenceAST> ast = make_shared<SequenceAST>();
shared_ptr<AST> firstAST;
ParseResultPtr rep = (*next)(start);
if (!rep)
{
return std::make_shared<ParseResult>(start, ast);
}
firstAST = rep->getAST();
ParseResultPtr rep2 = (*next)(rep->getState());
while (rep2)
{
if (!ast->size())
ast->append(firstAST);
ast->append(rep2->getAST());
rep = rep2;
rep2 = (*next)(rep2->getState());
}
//if (showRemainder) std::cout << __FUNCTION__<< ' ' << rep->getState()->substr(0) << std::endl;
if (ast->size())
return std::make_shared<ParseResult>(rep->getState(), ast);
return std::make_shared<ParseResult>(rep->getState(), firstAST);
}
bool showFails = false;
bool showRemainder = false;
void foo()
{
// Leave this in peg.cpp. It's for compile-time.
auto kw = make_shared<token>("kw");
auto kws = Repeat1("kws",kw);
ParseResultPtr pr = (*kws)(ParseStatePtr()); // Not valid input.
auto kws0 = Repeat0(kw);
pr = (*kws0)(ParseStatePtr()); // Not valid input.
typedef Skipwhite<token> SWToken;
SWToken swToken(kw);
pr = swToken(ParseStatePtr()); // Not valid input.
auto tokens2 = kw && kw;
pr = (*tokens2)(ParseStatePtr()); // Not valid input.
}
class TestEquationAST : public AST
{
public:
TestEquationAST(const ASTPtr& lhs, const ASTPtr& rhs)
: lhs(lhs), rhs(rhs) {}
std::string to_string() const
{
return "Equation: " + lhs->to_string() + " = " + rhs->to_string();
}
private:
ASTPtr lhs;
ASTPtr rhs;
};
ASTPtr TestEquation(const ASTPtr& ast)
{
SequenceAST* sequence = dynamic_cast<SequenceAST*>(ast.get());
return make_shared<TestEquationAST>(sequence->at(0), sequence->at(2));
}
auto idparse = make_shared<ID>();
auto numparse = make_shared<NUM>();
auto equals = make_shared<tch<'='>>();
auto plus = make_shared<tch<'+'>>();
auto times = make_shared<tch<'*'>>();
auto leftParen = make_shared<tch<'('>>();
auto rightParen = make_shared<tch<')'>>();
auto rhsRef = make_shared<ParserReference>();
auto parenthesized = (make_shared<WSequenceN>("parenthesized") && leftParen && rhsRef && rightParen);
// Application of a function is a sequence, starting with an id, of Factor's.
// E.g. "sqr cos x" does the square of the cosine of x.
// E.g. hypot o a = sqrt (sqr o + sqr a)
// hypot 3 4 -- Computes 5
// The arity of the functions involved determines the meaning
// and validity.
// An id can be treated as a zero-arity function that always returns the same thing.
// Therefore Application takes the place of ID in some contexts.
auto factorRef = make_shared<ParserReference>(); // factorRef->resolve(factor)
auto anArgumentW = SkipWhite<ParserReference>(factorRef);
auto arguments = Repeat0(anArgumentW);
auto application = (make_shared<WSequenceN>("application") && idparse && arguments);
auto factor = (make_shared<Choices>("factor") || application || numparse || parenthesized);
auto multiplication = RLoop("multiplication",factor,times);
auto addition = RLoop("addition",multiplication,plus);
auto lhs = Repeat1("lhs",SkipWhite(idparse));
auto equation = make_shared<ActionCaller>(TestEquation, make_shared<WSequenceN>("equation") && lhs && equals && addition);
void tests()
{
//typedef trange<32, 0x1FFFF> AnyChar;
//AnyChar anyChar;
//typedef repeat0<AnyChar> Anything;
//Anything anything(anyChar);
auto anything = Repeat0(make_shared<trange<32,0x1FFFF>>());
string input2("circumference r = 2 * pi * r");
/*
equation : lhs '=' rhs
rhs : term addl
rhs : term
addl : '+' term
term : factor mult
term : factor
mult : '*' factor
factor : application
factor : num
factor : '(' rhs ')'
application : id factor*
lhs : id*
*/
//return 0;
ParseState::reset();
ParseStatePtr doc1=make_shared<ParseState>(input2);
rhsRef->resolve(addition);
factorRef->resolve(factor);
ParseResultPtr e0 = (*equation)(doc1);
//ParseResult* e0 = docparse(&doc1);
if (e0)
{
cout << "*e0 [" << *e0->getState() << ']' << endl;
cout << "AST " << *e0->getAST();
cout << endl;
}
else
{
cout << "e0==null" << endl;
}
}