00001 #ifndef INC_ASTFactory_hpp__
00002 #define INC_ASTFactory_hpp__
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <antlr/config.hpp>
00012 #include <antlr/AST.hpp>
00013 #include <antlr/ASTArray.hpp>
00014 #include <antlr/ASTPair.hpp>
00015
00016 #include <utility>
00017
00018 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00019 namespace antlr {
00020 #endif
00021
00022
00023 typedef RefAST (*factory_type_)();
00024 typedef ANTLR_USE_NAMESPACE(std)pair< const char*, factory_type_ > factory_descriptor_;
00025 typedef ANTLR_USE_NAMESPACE(std)vector< factory_descriptor_* > factory_descriptor_list_;
00026
00035 class ANTLR_API ASTFactory {
00036 public:
00037 typedef factory_type_ factory_type;
00038 typedef factory_descriptor_ factory_descriptor;
00039 typedef factory_descriptor_list_ factory_descriptor_list;
00040 protected:
00041
00042
00043 factory_descriptor default_factory_descriptor;
00044 factory_descriptor_list nodeFactories;
00045 public:
00047 ASTFactory();
00053 ASTFactory( const char* factory_node_name, factory_type factory );
00055 virtual ~ASTFactory();
00056
00058 void registerFactory( int type, const char* ast_name, factory_type factory );
00060 void setMaxNodeType( int type );
00061
00063 void addASTChild(ASTPair& currentAST, RefAST child);
00065 virtual RefAST create();
00067 RefAST create(int type);
00069 RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt);
00071 RefAST create(RefAST tr);
00073 RefAST create(RefToken tok);
00075 RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile );
00079 RefAST dup(RefAST t);
00081 RefAST dupList(RefAST t);
00085 RefAST dupTree(RefAST t);
00092 RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes);
00097 RefAST make(ASTArray* nodes);
00099 void makeASTRoot(ASTPair& currentAST, RefAST root);
00100
00108 void setASTNodeFactory( const char* factory_node_name, factory_type factory );
00109
00110 #ifdef ANTLR_SUPPORT_XML
00111
00115 RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
00116 #endif
00117 protected:
00118 void loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
00119 void loadSiblings( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
00120 bool checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& infile );
00121
00122 #ifdef ANTLR_VECTOR_HAS_AT
00123
00124 inline RefAST getNodeOfType( unsigned int type )
00125 {
00126 return RefAST(nodeFactories.at(type)->second());
00127 }
00129 const char* getASTNodeType( unsigned int type )
00130 {
00131 return nodeFactories.at(type)->first;
00132 }
00134 factory_type getASTNodeFactory( unsigned int type )
00135 {
00136 return nodeFactories.at(type)->second;
00137 }
00138 #else
00139 inline RefAST getNodeOfType( unsigned int type )
00140 {
00141 return RefAST(nodeFactories[type]->second());
00142 }
00144 const char* getASTNodeType( unsigned int type )
00145 {
00146 return nodeFactories[type]->first;
00147 }
00148 factory_type getASTNodeFactory( unsigned int type )
00149 {
00150 return nodeFactories[type]->second;
00151 }
00152 #endif
00153
00154 private:
00155
00156 ASTFactory( const ASTFactory& );
00157 ASTFactory& operator=( const ASTFactory& );
00158 };
00159
00160 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00161 }
00162 #endif
00163
00164 #endif //INC_ASTFactory_hpp__