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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
#ifndef AST_HPP
#define AST_HPP
#include <cassert>
#include <list>
#include <stdexcept>
#include "parser.hpp"
namespace parserlib {
class ast_node;
template <class T, bool OPT> class ast_ptr;
template <class T> class ast_list;
template <class T> class ast;
/** type of AST node stack.
*/
typedef std::vector<ast_node *> ast_stack;
/** Base class for AST nodes.
*/
class ast_node : public input_range {
public:
///constructor.
ast_node() : m_parent(0) {}
/** copy constructor.
@param n source object.
*/
ast_node(const ast_node &n) : m_parent(0) {}
///destructor.
virtual ~ast_node() {}
/** assignment operator.
@param n source object.
@return reference to this.
*/
ast_node &operator = (const ast_node &n) { return *this; }
/** get the parent node.
@return the parent node, if there is one.
*/
ast_node *parent() const { return m_parent; }
/** interface for filling the contents of the node
from a node stack.
@param st stack.
*/
virtual void construct(ast_stack &st) {}
/** interface for visiting AST tree use.
@param user_data vector for storing user data.
*/
virtual void visit(void* user_data) {}
private:
//parent
ast_node *m_parent;
template <class T, bool OPT> friend class ast_ptr;
template <class ...Args> friend class ast_choice;
template <class T> friend class ast_list;
template <class T> friend class ast;
};
class ast_member;
/** type of ast member vector.
*/
typedef std::vector<ast_member *> ast_member_vector;
/** base class for AST nodes with children.
*/
class ast_container : public ast_node {
public:
/** sets the container under construction to be this.
*/
ast_container();
/** sets the container under construction to be this.
Members are not copied.
@param src source object.
*/
ast_container(const ast_container &src);
/** the assignment operator.
The members are not copied.
@param src source object.
@return reference to this.
*/
ast_container &operator = (const ast_container &src) {
return *this;
}
/** returns the vector of AST members.
@return the vector of AST members.
*/
const ast_member_vector &members() const {
return m_members;
}
/** Asks all members to construct themselves from the stack.
The members are asked to construct themselves in reverse order.
from a node stack.
@param st stack.
*/
virtual void construct(ast_stack &st);
private:
ast_member_vector m_members;
friend class ast_member;
};
/** Base class for children of ast_container.
*/
class ast_member {
public:
/** automatically registers itself to the container under construction.
*/
ast_member() { _init(); }
/** automatically registers itself to the container under construction.
@param src source object.
*/
ast_member(const ast_member &src) { _init(); }
/** the assignment operator.
@param src source object.
@return reference to this.
*/
ast_member &operator = (const ast_member &src) {
return *this;
}
/** returns the container this belongs to.
@return the container this belongs to.
*/
ast_container *container() const { return m_container; }
/** interface for filling the the member from a node stack.
@param st stack.
*/
virtual void construct(ast_stack &st) = 0;
private:
//the container this belongs to.
ast_container *m_container;
//register the AST member to the current container.
void _init();
};
/** pointer to an AST object.
It assumes ownership of the object.
It pops an object of the given type from the stack.
@tparam T type of object to control.
@tparam OPT if true, the object becomes optional.
*/
template <class T, bool OPT = false> class ast_ptr : public ast_member {
public:
/** the default constructor.
@param obj object.
*/
ast_ptr(T *obj = 0) : m_ptr(obj) {
_set_parent();
}
/** the copy constructor.
It duplicates the underlying object.
@param src source object.
*/
ast_ptr(const ast_ptr<T, OPT> &src) :
m_ptr(src.m_ptr ? new T(*src.m_ptr) : 0)
{
_set_parent();
}
/** deletes the underlying object.
*/
~ast_ptr() {
delete m_ptr;
}
/** copies the given object.
The old object is deleted.
@param obj new object.
@return reference to this.
*/
ast_ptr<T, OPT> &operator = (const T *obj) {
delete m_ptr;
m_ptr = obj ? new T(*obj) : 0;
_set_parent();
return *this;
}
/** copies the underlying object.
The old object is deleted.
@param src source object.
@return reference to this.
*/
ast_ptr<T, OPT> &operator = (const ast_ptr<T, OPT> &src) {
delete m_ptr;
m_ptr = src.m_ptr ? new T(*src.m_ptr) : 0;
_set_parent();
return *this;
}
/** gets the underlying ptr value.
@return the underlying ptr value.
*/
T *get() const {
return m_ptr;
}
/** auto conversion to the underlying object ptr.
@return the underlying ptr value.
*/
operator T *() const {
return m_ptr;
}
/** member access.
@return the underlying ptr value.
*/
T *operator ->() const {
assert(m_ptr);
return m_ptr;
}
/** Pops a node from the stack.
@param st stack.
@exception std::logic_error thrown if the node is not of the appropriate type;
thrown only if OPT == false or if the stack is empty.
*/
virtual void construct(ast_stack &st) {
//check the stack node
if (st.empty()) {
if (OPT) return;
else throw std::logic_error("invalid AST stack");
}
//get the node
ast_node *node = st.back();
//get the object
T *obj = dynamic_cast<T *>(node);
//if the object is optional, simply return
if (OPT) {
if (!obj) return;
}
//else if the object is mandatory, throw an exception
else {
if (!obj) throw std::logic_error("invalid AST node");
}
//pop the node from the stack
st.pop_back();
//set the new object
delete m_ptr;
m_ptr = obj;
_set_parent();
}
private:
//ptr
T *m_ptr;
//set parent of object
void _set_parent() {
if (m_ptr) m_ptr->m_parent = container();
}
};
template <class ...Args> class ast_choice : public ast_member {
public:
ast_choice(ast_node *obj = 0) : m_ptr(obj) {
_set_parent();
}
ast_choice(const ast_choice<Args...> &src) :
m_ptr(src.m_ptr ? new ast_node(*src.m_ptr) : 0)
{
_set_parent();
}
~ast_choice() {
delete m_ptr;
}
ast_choice<Args...> &operator = (const ast_node *obj) {
delete m_ptr;
m_ptr = obj ? new ast_node(*obj) : 0;
_set_parent();
return *this;
}
ast_choice<Args...> &operator = (const ast_choice<Args...> &src) {
delete m_ptr;
m_ptr = src.m_ptr ? new ast_node(*src.m_ptr) : 0;
_set_parent();
return *this;
}
ast_node *get() const {
return m_ptr;
}
operator ast_node *() const {
return m_ptr;
}
ast_node *operator ->() const {
assert(m_ptr);
return m_ptr;
}
virtual void construct(ast_stack &st) {
if (st.empty()) {
throw std::logic_error("invalid AST stack");
}
ast_node *node = st.back();
ast_node *obj = nullptr;
using swallow = bool[];
(void)swallow{obj || (obj = dynamic_cast<Args*>(node))...};
if (!obj) throw std::logic_error("invalid AST node");
st.pop_back();
delete m_ptr;
m_ptr = obj;
_set_parent();
}
private:
//ptr
ast_node *m_ptr;
void _set_parent() {
if (m_ptr) m_ptr->m_parent = container();
}
};
/** A list of objects.
It pops objects of the given type from the ast stack, until no more objects can be popped.
It assumes ownership of objects.
@tparam T type of object to control.
*/
template <class T> class ast_list : public ast_member {
public:
///list type.
typedef std::list<T *> container;
///the default constructor.
ast_list() {}
/** duplicates the objects of the given list.
@param src source object.
*/
ast_list(const ast_list<T> &src) {
_dup(src);
}
/** deletes the objects.
*/
~ast_list() {
_clear();
}
/** deletes the objects of this list and duplicates the given one.
@param src source object.
@return reference to this.
*/
ast_list<T> &operator = (const ast_list<T> &src) {
if (&src != this) {
_clear();
_dup(src);
}
return *this;
}
/** returns the container of objects.
@return the container of objects.
*/
const container &objects() const {
return m_objects;
}
/** Pops objects of type T from the stack until no more objects can be popped.
@param st stack.
*/
virtual void construct(ast_stack &st) {
for(;;) {
//if the stack is empty
if (st.empty()) break;
//get the node
ast_node *node = st.back();
//get the object
T *obj = dynamic_cast<T *>(node);
//if the object was not not of the appropriate type,
//end the list parsing
if (!obj) return;
//remove the node from the stack
st.pop_back();
//insert the object in the list, in reverse order
m_objects.push_front(obj);
//set the object's parent
obj->m_parent = ast_member::container();
}
}
private:
//objects
container m_objects;
//deletes the objects of this list.
void _clear() {
while (!m_objects.empty()) {
delete m_objects.back();
m_objects.pop_back();
}
}
//duplicate the given list.
void _dup(const ast_list<T> &src) {
for(typename container::const_iterator it = src.m_objects.begin();
it != src.m_objects.end();
++it)
{
T *obj = new T(*it);
m_objects.push_back(obj);
obj->m_parent = ast_member::container();
}
}
};
/** AST function which creates an object of type T
and pushes it to the node stack.
*/
template <class T> class ast {
public:
/** constructor.
@param r rule to attach the AST function to.
*/
ast(rule &r) {
r.set_parse_proc(&_parse_proc);
}
private:
//parse proc
static void _parse_proc(const pos &b, const pos &e, void *d) {
ast_stack *st = reinterpret_cast<ast_stack *>(d);
T *obj = new T;
obj->m_begin = b;
obj->m_end = e;
obj->construct(*st);
st->push_back(obj);
}
};
/** parses the given input.
@param i input.
@param g root rule of grammar.
@param el list of errors.
@param ud user data, passed to the parse procedures.
@return pointer to ast node created, or null if there was an error.
The return object must be deleted by the caller.
*/
ast_node *parse(input &i, rule &g, error_list &el, void* ud);
/** parses the given input.
@param i input.
@param g root rule of grammar.
@param el list of errors.
@param ast result pointer to created ast.
@param ud user data, passed to the parse procedures.
@return true on success, false on error.
*/
template <class T> bool parse(input &i, rule &g, error_list &el, T *&ast, void* ud = nullptr) {
ast_node *node = parse(i, g, el, ud);
ast = dynamic_cast<T *>(node);
if (ast) return true;
delete node;
return false;
}
} //namespace parserlib
#endif //AST_HPP
|