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.cpp63
1 files changed, 32 insertions, 31 deletions
diff --git a/src/MoonP/ast.cpp b/src/MoonP/ast.cpp
index 7cdefba..71c90c5 100644
--- a/src/MoonP/ast.cpp
+++ b/src/MoonP/ast.cpp
@@ -1,12 +1,13 @@
1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li 1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li
2All rights reserved. 2All rights reserved.
3 3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 5
6 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6Redistributions 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 7
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. */ 8Redistributions 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.
9
10THIS 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
11#include <cassert> 12#include <cassert>
12#include "MoonP/ast.hpp" 13#include "MoonP/ast.hpp"
@@ -48,20 +49,20 @@ bool ast_node::visitChild(const std::function<bool (ast_node*)>&) {
48 return false; 49 return false;
49} 50}
50 51
51 52
52/** Asks all members to construct themselves from the stack. 53/** Asks all members to construct themselves from the stack.
53 The members are asked to construct themselves in reverse order. 54 The members are asked to construct themselves in reverse order.
54 from a node stack. 55 from a node stack.
55 @param st stack. 56 @param st stack.
56 */ 57*/
57void ast_container::construct(ast_stack &st) { 58void ast_container::construct(ast_stack &st) {
58 for(ast_member_vector::reverse_iterator it = m_members.rbegin(); 59 for(ast_member_vector::reverse_iterator it = m_members.rbegin();
59 it != m_members.rend(); 60 it != m_members.rend();
60 ++it) 61 ++it)
61 { 62 {
62 ast_member* member = *it; 63 ast_member* member = *it;
63 member->construct(st); 64 member->construct(st);
64 } 65 }
65} 66}
66 67
67traversal ast_container::traverse(const std::function<traversal (ast_node*)>& func) { 68traversal ast_container::traverse(const std::function<traversal (ast_node*)>& func) {
@@ -108,24 +109,24 @@ bool ast_container::visitChild(const std::function<bool (ast_node*)>& func) {
108 109
109 110
110/** parses the given input. 111/** parses the given input.
111 @param i input. 112 @param i input.
112 @param g root rule of grammar. 113 @param g root rule of grammar.
113 @param el list of errors. 114 @param el list of errors.
114 @param ud user data, passed to the parse procedures. 115 @param ud user data, passed to the parse procedures.
115 @return pointer to ast node created, or null if there was an error. 116 @return pointer to ast node created, or null if there was an error.
116 The return object must be deleted by the caller. 117 The return object must be deleted by the caller.
117 */ 118*/
118ast_node* parse(input &i, rule &g, error_list &el, void* ud) { 119ast_node* parse(input& i, rule& g, error_list& el, void* ud) {
119 ast_stack st; 120 ast_stack st;
120 if (!parse(i, g, el, &st, ud)) { 121 if (!parse(i, g, el, &st, ud)) {
121 for (auto node : st) { 122 for (auto node : st) {
122 delete node; 123 delete node;
123 } 124 }
124 st.clear(); 125 st.clear();
125 return nullptr; 126 return nullptr;
126 } 127 }
127 assert(st.size() == 1); 128 assert(st.size() == 1);
128 return st.front(); 129 return st.front();
129} 130}
130 131
131 132