aboutsummaryrefslogtreecommitdiff
path: root/MoonParser/ast.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'MoonParser/ast.cpp')
-rw-r--r--MoonParser/ast.cpp85
1 files changed, 8 insertions, 77 deletions
diff --git a/MoonParser/ast.cpp b/MoonParser/ast.cpp
index 7f46bd0..6c86854 100644
--- a/MoonParser/ast.cpp
+++ b/MoonParser/ast.cpp
@@ -4,10 +4,6 @@
4 4
5namespace parserlib { 5namespace parserlib {
6 6
7
8//current AST container.
9static ast_container* _current = 0;
10
11int ast_type_id = 0; 7int ast_type_id = 0;
12 8
13traversal ast_node::traverse(const std::function<traversal (ast_node*)>& func) { 9traversal ast_node::traverse(const std::function<traversal (ast_node*)>& func) {
@@ -42,12 +38,6 @@ bool ast_node::visitChild(const std::function<bool (ast_node*)>&) {
42 return false; 38 return false;
43} 39}
44 40
45/** sets the container under construction to be this.
46 */
47ast_container::ast_container() {
48 _current = this;
49}
50
51 41
52/** Asks all members to construct themselves from the stack. 42/** Asks all members to construct themselves from the stack.
53 The members are asked to construct themselves in reverse order. 43 The members are asked to construct themselves in reverse order.
@@ -106,71 +96,6 @@ bool ast_container::visitChild(const std::function<bool (ast_node*)>& func) {
106 return false; 96 return false;
107} 97}
108 98
109ast_node* ast_container::getChild(int index) const {
110 int i = 0;
111 const auto& members = this->members();
112 for (auto member : members) {
113 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
114 if (i == index) return ptr->get();
115 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
116 for (auto obj : list->objects()) {
117 if (i == index) return obj;
118 }
119 }
120 i++;
121 }
122 return nullptr;
123}
124
125ast_node* ast_container::getFirstChild() const {
126 const auto& members = this->members();
127 if (!members.empty()) {
128 auto member = members.front();
129 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
130 return ptr->get();
131 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
132 if (!list->objects().empty()) {
133 return list->objects().front();
134 }
135 }
136 }
137 return nullptr;
138}
139
140ast_node* ast_container::getLastChild() const {
141 const auto& members = this->members();
142 if (!members.empty()) {
143 auto member = members.back();
144 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
145 return ptr->get();
146 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
147 if (!list->objects().empty()) {
148 return list->objects().front();
149 }
150 }
151 }
152 return nullptr;
153}
154
155size_t ast_container::getChildCount() const {
156 size_t count = 0;
157 const auto& members = this->members();
158 for (auto member : members) {
159 if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) {
160 count += 1;
161 } else if (_ast_list* list = ast_cast<_ast_list>(member)) {
162 count += list->objects().size();
163 }
164 }
165 return count;
166}
167
168//register the AST member to the current container.
169void ast_member::add_to_owner() {
170 assert(_current);
171 _current->m_members.push_back(this);
172}
173
174 99
175/** parses the given input. 100/** parses the given input.
176 @param i input. 101 @param i input.
@@ -182,9 +107,15 @@ void ast_member::add_to_owner() {
182 */ 107 */
183ast_node* _parse(input &i, rule &g, error_list &el, void* ud) { 108ast_node* _parse(input &i, rule &g, error_list &el, void* ud) {
184 ast_stack st; 109 ast_stack st;
185 if (!parse(i, g, el, &st, ud)) return 0; 110 if (!parse(i, g, el, &st, ud)) {
111 for (auto node : st) {
112 delete node;
113 }
114 st.clear();
115 return nullptr;
116 }
186 assert(st.size() == 1); 117 assert(st.size() == 1);
187 return st[0]; 118 return st.front();
188} 119}
189 120
190 121