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