| 1 | tim | 770 | #ifndef INC_ASTFactory_hpp__ | 
| 2 |  |  | #define INC_ASTFactory_hpp__ | 
| 3 |  |  |  | 
| 4 |  |  | /* ANTLR Translator Generator | 
| 5 |  |  | * Project led by Terence Parr at http://www.jGuru.com | 
| 6 |  |  | * Software rights: http://www.antlr.org/license.html | 
| 7 |  |  | * | 
| 8 |  |  | * $Id: ASTFactory.hpp,v 1.1 2005-12-02 15:38:02 tim Exp $ | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include <antlr/config.hpp> | 
| 12 |  |  | #include <antlr/AST.hpp> | 
| 13 |  |  | #include <antlr/ASTArray.hpp> | 
| 14 |  |  | #include <antlr/ASTPair.hpp> | 
| 15 |  |  |  | 
| 16 |  |  | #include <utility> | 
| 17 |  |  |  | 
| 18 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 19 |  |  | namespace antlr { | 
| 20 |  |  | #endif | 
| 21 |  |  |  | 
| 22 |  |  | // Using these extra types to appease MSVC | 
| 23 |  |  | typedef RefAST (*factory_type_)(); | 
| 24 |  |  | typedef ANTLR_USE_NAMESPACE(std)pair< const char*, factory_type_ >  factory_descriptor_; | 
| 25 |  |  | typedef ANTLR_USE_NAMESPACE(std)vector< factory_descriptor_* >      factory_descriptor_list_; | 
| 26 |  |  |  | 
| 27 |  |  | /** AST Super Factory shared by TreeParser and Parser. | 
| 28 |  |  | * This super factory maintains a map of all AST node types to their respective | 
| 29 |  |  | * AST factories. One instance should be shared among a parser/treeparser | 
| 30 |  |  | * chain. | 
| 31 |  |  | * | 
| 32 |  |  | * @todo check all this code for possible use of references in | 
| 33 |  |  | * stead of RefAST's. | 
| 34 |  |  | */ | 
| 35 |  |  | class ANTLR_API ASTFactory { | 
| 36 |  |  | public: | 
| 37 |  |  | typedef factory_type_             factory_type; | 
| 38 |  |  | typedef factory_descriptor_       factory_descriptor; | 
| 39 |  |  | typedef factory_descriptor_list_  factory_descriptor_list; | 
| 40 |  |  | protected: | 
| 41 |  |  | /* The mapping of AST node type to factory.. | 
| 42 |  |  | */ | 
| 43 |  |  | factory_descriptor default_factory_descriptor; | 
| 44 |  |  | factory_descriptor_list nodeFactories; | 
| 45 |  |  | public: | 
| 46 |  |  | /// Make new factory. Per default (Ref)CommonAST instances are generated. | 
| 47 |  |  | ASTFactory(); | 
| 48 |  |  | /** Initialize factory with a non default node type. | 
| 49 |  |  | * factory_node_name should be the name of the AST node type the factory | 
| 50 |  |  | * generates. (should exist during the existance of this ASTFactory | 
| 51 |  |  | * instance) | 
| 52 |  |  | */ | 
| 53 |  |  | ASTFactory( const char* factory_node_name, factory_type factory ); | 
| 54 |  |  | /// Destroy factory | 
| 55 |  |  | virtual ~ASTFactory(); | 
| 56 |  |  |  | 
| 57 |  |  | /// Register a node factory for the node type type with name ast_name | 
| 58 |  |  | void registerFactory( int type, const char* ast_name, factory_type factory ); | 
| 59 |  |  | /// Set the maximum node (AST) type this factory may encounter | 
| 60 |  |  | void setMaxNodeType( int type ); | 
| 61 |  |  |  | 
| 62 |  |  | /// Add a child to the current AST | 
| 63 |  |  | void addASTChild(ASTPair& currentAST, RefAST child); | 
| 64 |  |  | /// Create new empty AST node. The right default type shou | 
| 65 |  |  | virtual RefAST create(); | 
| 66 |  |  | /// Create AST node of the right type for 'type' | 
| 67 |  |  | RefAST create(int type); | 
| 68 |  |  | /// Create AST node of the right type for 'type' and initialize with txt | 
| 69 |  |  | RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt); | 
| 70 |  |  | /// Create duplicate of tr | 
| 71 |  |  | RefAST create(RefAST tr); | 
| 72 |  |  | /// Create new AST node and initialize contents from a token. | 
| 73 |  |  | RefAST create(RefToken tok); | 
| 74 |  |  | /// Create new AST node and initialize contents from a stream. | 
| 75 |  |  | RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile ); | 
| 76 |  |  | /** Deep copy a single node. This function the new clone() methods in the | 
| 77 |  |  | * AST interface. Returns a new RefAST(nullASTptr) if t is null. | 
| 78 |  |  | */ | 
| 79 |  |  | RefAST dup(RefAST t); | 
| 80 |  |  | /// Duplicate tree including siblings of root. | 
| 81 |  |  | RefAST dupList(RefAST t); | 
| 82 |  |  | /** Duplicate a tree, assuming this is a root node of a tree-- | 
| 83 |  |  | * duplicate that node and what's below; ignore siblings of root node. | 
| 84 |  |  | */ | 
| 85 |  |  | RefAST dupTree(RefAST t); | 
| 86 |  |  | /** Make a tree from a list of nodes. The first element in the | 
| 87 |  |  | * array is the root. If the root is null, then the tree is | 
| 88 |  |  | * a simple list not a tree. Handles null children nodes correctly. | 
| 89 |  |  | * For example, make(a, b, null, c) yields tree (a b c).  make(null,a,b) | 
| 90 |  |  | * yields tree (nil a b). | 
| 91 |  |  | */ | 
| 92 |  |  | RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes); | 
| 93 |  |  | /** Make a tree from a list of nodes, where the nodes are contained | 
| 94 |  |  | * in an ASTArray object. The ASTArray is deleted after use. | 
| 95 |  |  | * @todo FIXME! I have a feeling we can get rid of this ugly ASTArray thing | 
| 96 |  |  | */ | 
| 97 |  |  | RefAST make(ASTArray* nodes); | 
| 98 |  |  | /// Make an AST the root of current AST | 
| 99 |  |  | void makeASTRoot(ASTPair& currentAST, RefAST root); | 
| 100 |  |  |  | 
| 101 |  |  | /** Set a new default AST type. | 
| 102 |  |  | * factory_node_name should be the name of the AST node type the factory | 
| 103 |  |  | * generates. (should exist during the existance of this ASTFactory | 
| 104 |  |  | * instance). | 
| 105 |  |  | * Only change factory between parser runs. You might get unexpected results | 
| 106 |  |  | * otherwise. | 
| 107 |  |  | */ | 
| 108 |  |  | void setASTNodeFactory( const char* factory_node_name, factory_type factory ); | 
| 109 |  |  |  | 
| 110 |  |  | #ifdef ANTLR_SUPPORT_XML | 
| 111 |  |  | /** Load a XML AST from stream. Make sure you have all the factories | 
| 112 |  |  | * registered before use. | 
| 113 |  |  | * @note this 'XML' stuff is quite rough still. YMMV. | 
| 114 |  |  | */ | 
| 115 |  |  | RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile ); | 
| 116 |  |  | #endif | 
| 117 |  |  | protected: | 
| 118 |  |  | void loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current ); | 
| 119 |  |  | void loadSiblings( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current ); | 
| 120 |  |  | bool checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& infile ); | 
| 121 |  |  |  | 
| 122 |  |  | #ifdef ANTLR_VECTOR_HAS_AT | 
| 123 |  |  | /// construct a node of 'type' | 
| 124 |  |  | inline RefAST getNodeOfType( unsigned int type ) | 
| 125 |  |  | { | 
| 126 |  |  | return RefAST(nodeFactories.at(type)->second()); | 
| 127 |  |  | } | 
| 128 |  |  | /// get the name of the node 'type' | 
| 129 |  |  | const char* getASTNodeType( unsigned int type ) | 
| 130 |  |  | { | 
| 131 |  |  | return nodeFactories.at(type)->first; | 
| 132 |  |  | } | 
| 133 |  |  | /// get the factory used for node 'type' | 
| 134 |  |  | factory_type getASTNodeFactory( unsigned int type ) | 
| 135 |  |  | { | 
| 136 |  |  | return nodeFactories.at(type)->second; | 
| 137 |  |  | } | 
| 138 |  |  | #else | 
| 139 |  |  | inline RefAST getNodeOfType( unsigned int type ) | 
| 140 |  |  | { | 
| 141 |  |  | return RefAST(nodeFactories[type]->second()); | 
| 142 |  |  | } | 
| 143 |  |  | /// get the name of the node 'type' | 
| 144 |  |  | const char* getASTNodeType( unsigned int type ) | 
| 145 |  |  | { | 
| 146 |  |  | return nodeFactories[type]->first; | 
| 147 |  |  | } | 
| 148 |  |  | factory_type getASTNodeFactory( unsigned int type ) | 
| 149 |  |  | { | 
| 150 |  |  | return nodeFactories[type]->second; | 
| 151 |  |  | } | 
| 152 |  |  | #endif | 
| 153 |  |  |  | 
| 154 |  |  | private: | 
| 155 |  |  | // no copying and such.. | 
| 156 |  |  | ASTFactory( const ASTFactory& ); | 
| 157 |  |  | ASTFactory& operator=( const ASTFactory& ); | 
| 158 |  |  | }; | 
| 159 |  |  |  | 
| 160 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 161 |  |  | } | 
| 162 |  |  | #endif | 
| 163 |  |  |  | 
| 164 |  |  | #endif //INC_ASTFactory_hpp__ |