ANTLR Support Libraries 2.7.1+
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
antlr/ASTFactory.hpp
Go to the documentation of this file.
00001 #ifndef INC_ASTFactory_hpp__
00002 #define INC_ASTFactory_hpp__
00003 
00004 /* ANTLR Translator Generator
00005  * Project led by Terence Parr at http://www.jGuru.com
00006  * Software rights: http://www.antlr.org/license.html
00007  *
00008  * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/ASTFactory.hpp#2 $
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 // Using these extra types to appease MSVC
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    /* The mapping of AST node type to factory..
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
00124 
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    // no copying and such..
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__