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 <istream>
00017 #include <utility>
00018
00019 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00020 namespace antlr {
00021 #endif
00022
00023
00024 typedef RefAST (*factory_type_)();
00025 typedef ANTLR_USE_NAMESPACE(std)pair< const char*, factory_type_ > factory_descriptor_;
00026 typedef ANTLR_USE_NAMESPACE(std)vector< factory_descriptor_* > factory_descriptor_list_;
00027
00036 class ANTLR_API ASTFactory {
00037 public:
00038 typedef factory_type_ factory_type;
00039 typedef factory_descriptor_ factory_descriptor;
00040 typedef factory_descriptor_list_ factory_descriptor_list;
00041 protected:
00042
00043
00044 factory_descriptor default_factory_descriptor;
00045 factory_descriptor_list nodeFactories;
00046 public:
00048 ASTFactory();
00054 ASTFactory( const char* factory_node_name, factory_type factory );
00056 virtual ~ASTFactory();
00057
00059 void registerFactory( int type, const char* ast_name, factory_type factory );
00061 void setMaxNodeType( int type );
00062
00064 void addASTChild(ASTPair& currentAST, RefAST child);
00066 virtual RefAST create();
00068 RefAST create(int type);
00070 RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt);
00072 RefAST create(RefAST tr);
00074 RefAST create(RefToken tok);
00076 RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile );
00080 RefAST dup(RefAST t);
00082 RefAST dupList(RefAST t);
00086 RefAST dupTree(RefAST t);
00093 RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes);
00098 RefAST make(ASTArray* nodes);
00100 void makeASTRoot(ASTPair& currentAST, RefAST root);
00101
00109 void setASTNodeFactory( const char* factory_node_name, factory_type factory );
00110
00111 #ifdef ANTLR_SUPPORT_XML
00112
00116 RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
00117 #endif
00118 protected:
00119 void loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
00120 void loadSiblings( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
00121 bool checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& infile );
00122
00123 #ifdef ANTLR_VECTOR_HAS_AT
00125 inline RefAST getNodeOfType( unsigned int type )
00126 {
00127 return RefAST(nodeFactories.at(type)->second());
00128 }
00130 const char* getASTNodeType( unsigned int type )
00131 {
00132 return nodeFactories.at(type)->first;
00133 }
00135 factory_type getASTNodeFactory( unsigned int type )
00136 {
00137 return nodeFactories.at(type)->second;
00138 }
00139 #else
00140 inline RefAST getNodeOfType( unsigned int type )
00141 {
00142 return RefAST(nodeFactories[type]->second());
00143 }
00145 const char* getASTNodeType( unsigned int type )
00146 {
00147 return nodeFactories[type]->first;
00148 }
00149 factory_type getASTNodeFactory( unsigned int type )
00150 {
00151 return nodeFactories[type]->second;
00152 }
00153 #endif
00154
00155 private:
00156
00157 ASTFactory( const ASTFactory& );
00158 ASTFactory& operator=( const ASTFactory& );
00159 };
00160
00161 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00162 }
00163 #endif
00164
00165 #endif //INC_ASTFactory_hpp__