diff options
Diffstat (limited to 'MoonParser/ast.cpp')
-rw-r--r-- | MoonParser/ast.cpp | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/MoonParser/ast.cpp b/MoonParser/ast.cpp index 6217f3e..dbf5d17 100644 --- a/MoonParser/ast.cpp +++ b/MoonParser/ast.cpp | |||
@@ -6,7 +6,7 @@ namespace parserlib { | |||
6 | 6 | ||
7 | 7 | ||
8 | //current AST container. | 8 | //current AST container. |
9 | static ast_container *_current = 0; | 9 | static ast_container* _current = 0; |
10 | 10 | ||
11 | int ast_type_id = 0; | 11 | int ast_type_id = 0; |
12 | 12 | ||
@@ -30,14 +30,6 @@ ast_container::ast_container() { | |||
30 | _current = this; | 30 | _current = this; |
31 | } | 31 | } |
32 | 32 | ||
33 | |||
34 | /** sets the container under construction to be this. | ||
35 | @param src source object. | ||
36 | */ | ||
37 | ast_container::ast_container(const ast_container &src) { | ||
38 | _current = this; | ||
39 | } | ||
40 | |||
41 | 33 | ||
42 | /** Asks all members to construct themselves from the stack. | 34 | /** Asks all members to construct themselves from the stack. |
43 | The members are asked to construct themselves in reverse order. | 35 | The members are asked to construct themselves in reverse order. |
@@ -49,7 +41,7 @@ void ast_container::construct(ast_stack &st) { | |||
49 | it != m_members.rend(); | 41 | it != m_members.rend(); |
50 | ++it) | 42 | ++it) |
51 | { | 43 | { |
52 | ast_member *member = *it; | 44 | ast_member* member = *it; |
53 | member->construct(st); | 45 | member->construct(st); |
54 | } | 46 | } |
55 | } | 47 | } |
@@ -141,42 +133,64 @@ ast_node* ast_container::getChild(int index) const { | |||
141 | const auto& members = this->members(); | 133 | const auto& members = this->members(); |
142 | for (auto member : members) { | 134 | for (auto member : members) { |
143 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { | 135 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { |
144 | if (ptr->get()) { | 136 | if (i == index) return ptr->get(); |
145 | if (i == index) return ptr->get(); | ||
146 | i++; | ||
147 | } | ||
148 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { | 137 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { |
149 | for (auto obj : list->objects()) { | 138 | for (auto obj : list->objects()) { |
150 | if (obj) { | 139 | if (i == index) return obj; |
151 | if (i == index) return obj; | ||
152 | i++; | ||
153 | } | ||
154 | } | 140 | } |
155 | } | 141 | } |
142 | i++; | ||
156 | } | 143 | } |
157 | return nullptr; | 144 | return nullptr; |
158 | } | 145 | } |
159 | 146 | ||
160 | int ast_container::getChildCount() const { | 147 | ast_node* ast_container::getFirstChild() const { |
161 | int count = 0; | ||
162 | const auto& members = this->members(); | 148 | const auto& members = this->members(); |
163 | for (auto member : members) { | 149 | if (!members.empty()) { |
150 | auto member = members.front(); | ||
164 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { | 151 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { |
165 | if (ptr->get()) count++; | 152 | return ptr->get(); |
166 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { | 153 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { |
167 | for (auto obj : list->objects()) { | 154 | if (!list->objects().empty()) { |
168 | if (obj) count++; | 155 | return list->objects().front(); |
156 | } | ||
157 | } | ||
158 | } | ||
159 | return nullptr; | ||
160 | } | ||
161 | |||
162 | ast_node* ast_container::getLastChild() const { | ||
163 | const auto& members = this->members(); | ||
164 | if (!members.empty()) { | ||
165 | auto member = members.back(); | ||
166 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { | ||
167 | return ptr->get(); | ||
168 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { | ||
169 | if (!list->objects().empty()) { | ||
170 | return list->objects().front(); | ||
169 | } | 171 | } |
170 | } | 172 | } |
171 | } | 173 | } |
174 | return nullptr; | ||
175 | } | ||
176 | |||
177 | size_t ast_container::getChildCount() const { | ||
178 | size_t count = 0; | ||
179 | const auto& members = this->members(); | ||
180 | for (auto member : members) { | ||
181 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { | ||
182 | count += 1; | ||
183 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { | ||
184 | count += list->objects().size(); | ||
185 | } | ||
186 | } | ||
172 | return count; | 187 | return count; |
173 | } | 188 | } |
174 | 189 | ||
175 | //register the AST member to the current container. | 190 | //register the AST member to the current container. |
176 | void ast_member::_init() { | 191 | void ast_member::add_to_owner() { |
177 | assert(_current); | 192 | assert(_current); |
178 | m_container = _current; | 193 | _current->m_members.push_back(this); |
179 | _current->m_members.push_back(this); | ||
180 | } | 194 | } |
181 | 195 | ||
182 | 196 | ||
@@ -188,7 +202,7 @@ void ast_member::_init() { | |||
188 | @return pointer to ast node created, or null if there was an error. | 202 | @return pointer to ast node created, or null if there was an error. |
189 | The return object must be deleted by the caller. | 203 | The return object must be deleted by the caller. |
190 | */ | 204 | */ |
191 | ast_node *parse(input &i, rule &g, error_list &el, void* ud) { | 205 | ast_node* _parse(input &i, rule &g, error_list &el, void* ud) { |
192 | ast_stack st; | 206 | ast_stack st; |
193 | if (!parse(i, g, el, &st, ud)) return 0; | 207 | if (!parse(i, g, el, &st, ud)) return 0; |
194 | assert(st.size() == 1); | 208 | assert(st.size() == 1); |