aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/ast.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/MoonP/ast.cpp')
-rw-r--r--src/MoonP/ast.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/MoonP/ast.cpp b/src/MoonP/ast.cpp
new file mode 100644
index 0000000..cda2339
--- /dev/null
+++ b/src/MoonP/ast.cpp
@@ -0,0 +1,132 @@
1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li
2All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
9THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
10
11#include <cassert>
12#include "MoonP/ast.hpp"
13
14
15namespace parserlib {
16
17int ast_type_id = 0;
18
19traversal ast_node::traverse(const std::function<traversal (ast_node*)>& func) {
20 return func(this);
21}
22
23ast_node* ast_node::getByTypeIds(int* begin, int* end) {
24 ast_node* current = this;
25 auto it = begin;
26 while (it != end) {
27 ast_node* findNode = nullptr;
28 int type = *it;
29 current->visitChild([&](ast_node* node) {
30 if (node->get_type() == type) {
31 findNode = node;
32 return true;
33 }
34 return false;
35 });
36 if (findNode) {
37 current = findNode;
38 } else {
39 current = nullptr;
40 break;
41 }
42 ++it;
43 }
44 return current;
45}
46
47bool ast_node::visitChild(const std::function<bool (ast_node*)>&) {
48 return false;
49}
50
51
52/** Asks all members to construct themselves from the stack.
53 The members are asked to construct themselves in reverse order.
54 from a node stack.
55 @param st stack.
56 */
57void ast_container::construct(ast_stack &st) {
58 for(ast_member_vector::reverse_iterator it = m_members.rbegin();
59 it != m_members.rend();
60 ++it)
61 {
62 ast_member* member = *it;
63 member->construct(st);
64 }
65}
66
67traversal ast_container::traverse(const std::function<traversal (ast_node*)>& func) {
68 traversal action = func(this);
69 switch (action) {
70 case traversal::Stop: return traversal::Stop;
71 case traversal::Return: return traversal::Continue;
72 default: break;
73 }
74 const auto& members = this->members();
75 for (auto member : members) {
76 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
77 if (ptr->get() && ptr->get()->traverse(func) == traversal::Stop) {
78 return traversal::Stop;
79 }
80 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
81 for (auto obj : list->objects()) {
82 if (obj->traverse(func) == traversal::Stop) {
83 return traversal::Stop;
84 }
85 }
86 }
87 }
88 return traversal::Continue;
89}
90
91bool ast_container::visitChild(const std::function<bool (ast_node*)>& func) {
92 const auto& members = this->members();
93 for (auto member : members) {
94 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
95 if (ptr->get()) {
96 if (func(ptr->get())) return true;
97 }
98 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
99 for (auto obj : list->objects()) {
100 if (obj) {
101 if (func(obj)) return true;
102 }
103 }
104 }
105 }
106 return false;
107}
108
109
110/** parses the given input.
111 @param i input.
112 @param g root rule of grammar.
113 @param el list of errors.
114 @param ud user data, passed to the parse procedures.
115 @return pointer to ast node created, or null if there was an error.
116 The return object must be deleted by the caller.
117 */
118ast_node* _parse(input &i, rule &g, error_list &el, void* ud) {
119 ast_stack st;
120 if (!parse(i, g, el, &st, ud)) {
121 for (auto node : st) {
122 delete node;
123 }
124 st.clear();
125 return nullptr;
126 }
127 assert(st.size() == 1);
128 return st.front();
129}
130
131
132} //namespace parserlib