tree.hpp

Go to the documentation of this file.
00001 //  This file is part of jdl_parser.
00002 //
00003 //  jdl_parser is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  jdl_parser is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with jdl_parser.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 // Copyright Simone Pellegrini 2007-2008; e-mail: motonacciu@gmail.com
00017 
00018 #ifndef TREE_HPP_
00019 #define TREE_HPP_
00020 
00021 #include "defs.hpp"
00022 #include "parser_stack.hpp"
00023 
00024 using namespace std;
00025 
00026 namespace jdl{
00027 
00028 //template <class Ret, class Arg>
00029 //class arg_visitor;
00030 
00031 void print_tree(component *root);
00032 
00033 // Composite pattern
00034 class component {
00035 public:
00036 
00037         virtual void accept(const_visitor &v) const = 0;
00038         
00039         virtual void accept(visitor &v) = 0;
00040                 
00041         virtual string to_string() const = 0;
00042         
00043         virtual component* clone() const = 0;
00044         
00045         friend ostream& operator<<(ostream &out, component *c) {        return out << c->to_string(); }
00046                 
00047         virtual ~component(){};
00048 };
00049 
00051 // ABSTRACT LEAF
00053 template <class T>
00054 class abstract_leaf : public component{
00055         T _data;
00056 public:
00057         abstract_leaf(T data) : _data(data){}
00058                 
00059         T data() const { return _data; }
00060                 
00061         void accept(const_visitor &v) const = 0;
00062         
00063         void accept(visitor &v) = 0;
00064         
00065 //      template<class Ret, class Arg>
00066 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg) = 0;
00067                 
00068         ~abstract_leaf(){}
00069 };
00070 
00072 // INTEGER NODE
00074 class int_node : public abstract_leaf<int>{
00075 public:
00076         int_node(const char* begin, const char* end) : 
00077                 abstract_leaf<int>(boost::lexical_cast<int>(string(begin,end))){ }
00078         
00079         int_node(int val): abstract_leaf<int>(val){ } 
00080         
00081         bool operator==(int val){ return data() == val; }
00082         
00083         void accept(const_visitor &v) const;
00084         
00085         void accept(visitor &v);
00086         
00087         component* clone() const { return new int_node(data()); }
00088         
00089 //      template<class Ret, class Arg>
00090 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00091                 
00092         string to_string() const {      return "" + data(); }
00093 };
00094 
00096 // FLOAT NODE
00098 class float_node : public abstract_leaf<double>{
00099 public:
00100         float_node(const char* begin, const char* end) : 
00101                 abstract_leaf<double>(boost::lexical_cast<double>(string(begin,end))){ }
00102         
00103         float_node(float_node const& node): abstract_leaf<double>(node.data()){ } 
00104         
00105         bool operator==(float val){ return data() == val; }
00106         
00107         void accept(const_visitor &v) const;
00108         
00109         void accept(visitor &v);
00110         
00111         component* clone() const { return new float_node(*this); }
00112         
00113 //      template<class Ret, class Arg>
00114 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00115                 
00116         string to_string() const {
00117                 return boost::lexical_cast<string>(data());     
00118         }
00119 };
00120 
00122 // STRING NODE
00124 class string_node : public abstract_leaf<string>{
00125 public:
00126         string_node(const char* begin, const char* end) : 
00127                 abstract_leaf<string>(string(begin, end)){}
00128         
00129         string_node(string_node const& node): abstract_leaf<string>(node.data()){ } 
00130         
00131         bool operator==(string const& val){ return data() == val; }
00132         
00133         void accept(const_visitor &v) const;
00134         
00135         void accept(visitor &v);
00136                 
00137         component* clone() const { return new string_node(*this); }
00138         
00139 //      template<class Ret, class Arg>
00140 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00141                 
00142         string to_string() const {      return data(); }
00143 };
00144 
00146 // STRING NODE
00148 class string_literal_node : public abstract_leaf<string>{
00149 public:
00150         string_literal_node(const char* begin, const char* end) : 
00151                 abstract_leaf<string>(string(begin, end)){}
00152         
00153         string_literal_node(string_literal_node const& node): abstract_leaf<string>(node.data()){}
00154         
00155         bool operator==(string const& val){ return data() == val; }
00156         
00157         void accept(const_visitor &v) const;
00158         
00159         void accept(visitor &v);
00160         
00161         component* clone() const { return new string_literal_node(*this); }
00162         
00163 //      template<class Ret, class Arg>
00164 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00165                 
00166         string to_string() const {      return "\"" + data() + "\""; }
00167 };
00168 
00170 // ABSTRACT NODE
00172 class abstract_node : public component{
00173 protected:
00174         vector<boost::shared_ptr<component> > children;
00175 public:
00176         abstract_node(){}
00177         
00178         abstract_node(abstract_node const& node){
00179                 vector<boost::shared_ptr<component> >::const_iterator it = node.get_children().begin();
00180                 for(; it != node.get_children().end(); it++ )
00181                         children.push_back(boost::shared_ptr<component>((*it)->clone()));
00182         }
00183         
00184         void accept(const_visitor &v) const = 0;
00185                 
00186         void accept(visitor &v) = 0;
00187         
00188         vector<boost::shared_ptr<component> > const& get_children() const { return children; }
00189         
00190         vector<boost::shared_ptr<component> >& get_children() { return children; }
00191                 
00192 //      template<class Ret, class Arg>
00193 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg) = 0;
00194                 
00195         ~abstract_node();
00196 };
00197 
00199 // BINARY EXPRESSION
00201 class binary_expr_node : public abstract_node{
00202         string _op;
00203 public:
00204         binary_expr_node(){ children.resize(2); }
00205         
00206         binary_expr_node(binary_expr_node const& node): abstract_node(node){ 
00207                 _op = node.get_operator();
00208         }
00209                 
00210         void left_operand(boost::shared_ptr<component> comp){ children[0] = comp; }
00211                 
00212         boost::shared_ptr<component> get_left_operand() const { return children[0]; }
00213                 
00214         void right_operand(boost::shared_ptr<component> comp){ children[1] = comp; }
00215                 
00216         boost::shared_ptr<component> get_right_operand() const {        return children[1];     }
00217                 
00218         void set_operator(string op){ _op = op; }
00219                 
00220         const string &get_operator() const { return _op; }
00221                 
00222         string to_string() const;
00223         
00224         component* clone() const{ return new binary_expr_node(*this); }
00225         
00226         void accept(const_visitor &v) const; 
00227         
00228         void accept(visitor &v);
00229         
00230 //      template<class Ret, class Arg>
00231 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00232 };
00233 
00235 // UNARY EXPRESSION
00237 class unary_expr_node : public abstract_node{
00238         string _op;
00239 public:
00240         unary_expr_node(){
00241 #ifdef DEBUG
00242                 cout << "Creating UNARY_EXPR_NODE" << endl; 
00243 #endif
00244                 children.resize(1); 
00245         }
00246         
00247         unary_expr_node(unary_expr_node const& node): abstract_node(node){
00248                 _op = node.get_operator();
00249         }
00250         
00251         void operand(boost::shared_ptr<component> comp){ children[0] = comp; }
00252                 
00253         boost::shared_ptr<component> get_operand() const { return children[0]; }
00254                 
00255         void set_operator(string op){ _op = op; }
00256                 
00257         const string &get_operator() const { return _op; }
00258                 
00259         string to_string() const;
00260         
00261         component* clone() const{ return new unary_expr_node(*this); }
00262         
00263         void accept(const_visitor &v) const;
00264         
00265         void accept(visitor &v);
00266         
00267 //      template<class Ret, class Arg>
00268 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00269 };
00270 
00272 // ATTRIBUTE DEFINITION NODE
00274 class attribute_definition_node : public abstract_node{
00275 public:
00276         attribute_definition_node() { 
00277 #ifdef DEBUG
00278                 cout << "Created ATTRIBUTE_DEFINITION_NODE!" << endl;
00279 #endif
00280                 children.resize(2); 
00281         }
00282         
00283         attribute_definition_node(attribute_definition_node const& node): abstract_node(node) { }
00284         
00285         void set_name(boost::shared_ptr<string_node> comp){ children[0] = comp; }
00286                 
00287         boost::shared_ptr<string_node> get_name() const { return boost::dynamic_pointer_cast<string_node>(children[0]); }
00288                 
00289         void set_value(boost::shared_ptr<component> comp){ children[1] = comp; }
00290                 
00291         boost::shared_ptr<component> get_value() const { return children[1]; }
00292                 
00293         string to_string() const;
00294         
00295         component* clone() const{ return new attribute_definition_node(*this); }
00296         
00297         void accept(const_visitor &v) const;
00298         
00299         void accept(visitor &v);
00300         
00301 //      template<class Ret, class Arg>
00302 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00303 };
00304 
00306 // CONDITIONAL EXPRESSION
00308 class conditional_expr: public abstract_node{
00309 public:
00310         conditional_expr() { 
00311 #ifdef DEBUG
00312                 cout << "Created CONDITIONAL_EXPRESSION_NODE." << endl;
00313 #endif
00314                 children.resize(3); 
00315         }
00316         
00317         conditional_expr(conditional_expr const& node): abstract_node(node) { }
00318         
00319         void set_condition(boost::shared_ptr<component> comp){ children[0] = comp; }
00320                 
00321         boost::shared_ptr<component> get_condition() const { return children[0];        }
00322                 
00323         void set_then(boost::shared_ptr<component> comp){       children[1] = comp;     }
00324                 
00325         boost::shared_ptr<component> get_then() const { return children[1]; }
00326                 
00327         void set_else(boost::shared_ptr<component> comp){       children[2] = comp; }
00328                 
00329         boost::shared_ptr<component> get_else() const { return children[2]; }
00330                 
00331         string to_string() const;
00332         
00333         component* clone() const{ return new conditional_expr(*this); }
00334         
00335         void accept(const_visitor &v) const;
00336         
00337         void accept(visitor &v);
00338         
00339 //      template<class Ret, class Arg>
00340 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00341 };
00342 
00344 // DOT NODE
00346 class dot_node : public abstract_node{
00347 public:
00348         dot_node(){
00349 #ifdef DEBUG
00350                 cout << "Creating DOT_NODE." << endl;
00351 #endif 
00352                 children.resize(2); 
00353         }
00354         
00355         dot_node(dot_node const& node): abstract_node(node){}
00356         
00357         void left(boost::shared_ptr<component> node){ children[0] = node; }
00358                 
00359         boost::shared_ptr<component> get_left() const { return children[0]; }
00360                 
00361         void right(boost::shared_ptr<component> node){ children[1] = node; }
00362                 
00363         boost::shared_ptr<component> get_right() const { return children[1]; }
00364                 
00365         string to_string() const;
00366         
00367         component* clone() const{ return new dot_node(*this); }
00368         
00369         void accept(const_visitor &v) const;
00370         
00371         void accept(visitor &v);
00372         
00373 //      template<class Ret, class Arg>
00374 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00375 };
00376 
00378 // BRACKET NODE
00380 class bracket_node : public abstract_node{
00381 public:
00382         bracket_node(){
00383 #ifdef DEBUG
00384                 cout << "Created BRACKET_NODE." << endl;
00385 #endif 
00386                 children.resize(2); 
00387         }
00388         
00389         bracket_node(bracket_node const& node): abstract_node(node){ }
00390         
00391         void left(boost::shared_ptr<component> node){ children[0] = node; }
00392                 
00393         boost::shared_ptr<component> get_left() const { return children[0]; }
00394                 
00395         void right(boost::shared_ptr<component> node){ children[1] = node; }
00396                 
00397         boost::shared_ptr<component> get_right() const { return children[1]; }
00398                 
00399         string to_string() const;
00400         
00401         component* clone() const{ return new bracket_node(*this); }
00402         
00403         void accept(const_visitor &v) const;
00404         
00405         void accept(visitor &v);
00406         
00407 //      template<class Ret, class Arg>
00408 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00409 };
00410 
00412 // TERM NODE
00414 class term_node: public abstract_node{
00415 public:
00416         term_node(){
00417 #ifdef DEBUG
00418                 cout << "Created TERM_NODE." << endl;
00419 #endif
00420         }
00421         
00422         term_node(term_node const& node): abstract_node(node){}
00423         
00424         void add_child(boost::shared_ptr<component> comp){ children.push_back(comp); }
00425                 
00426         string to_string() const;
00427         
00428         component* clone() const{ return new term_node(*this); }
00429         
00430         void accept(const_visitor &v) const;
00431         
00432         void accept(visitor &v);
00433         
00434 //      template<class Ret, class Arg>
00435 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00436 };
00437 
00439 // LIST NODE
00441 class list_node: public abstract_node{
00442 public:
00443         list_node(){
00444 #ifdef DEBUG
00445                 cout << "Created LIST_NODE." << endl;
00446 #endif
00447         }
00448         
00449         list_node(list_node const& node): abstract_node(node) {}
00450         
00451         void add_child(boost::shared_ptr<component> comp){ children.push_back(comp); }
00452                 
00453         string to_string() const;
00454         
00455         component* clone() const{ return new list_node(*this); }
00456         
00457         void accept(const_visitor &v) const;
00458         
00459         void accept(visitor &v);
00460         
00461 //      template<class Ret, class Arg>
00462 //      Ret accept(arg_visitor<Ret,Arg> &v, Arg &arg);
00463 };
00464 
00466 // FUNCTION CALL NODE
00468 class func_call_node: public abstract_node{
00469 public:
00470         func_call_node(){
00471 #ifdef DEBUG 
00472                 cout << "Creating function call node." << endl;
00473 #endif
00474                 children.resize(2); 
00475         }
00476         
00477         void func_name(boost::shared_ptr<component> comp){ children[0] = comp; }
00478                 
00479         boost::shared_ptr<component> get_func_name() const { return children[0]; }
00480                 
00481         void args(boost::shared_ptr<component> comp){ children[1] = comp; }
00482                 
00483         boost::shared_ptr<component> get_args() const { return children[1]; }
00484                 
00485         string to_string() const;
00486         
00487         component* clone() const{ return new func_call_node(*this); }
00488         
00489         void accept(const_visitor &v) const;
00490         
00491         void accept(visitor &v);
00492 };
00493 
00494 }
00495 
00496 #endif /*TREE_HPP_*/

Generated on Sun Feb 17 16:55:14 2008 for JDLParser by  doxygen 1.5.3