ANTLR Support Libraries 2.7.1+
ASTFactory.hpp
Go to the documentation of this file.
1 #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: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/ASTFactory.hpp#2 $
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 <istream>
17 #include <utility>
18 
19 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
20 namespace antlr {
21 #endif
22 
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_;
27 
37 public:
41 protected:
42  /* The mapping of AST node type to factory..
43  */
46 public:
48  ASTFactory();
54  ASTFactory( const char* factory_node_name, factory_type factory );
56  virtual ~ASTFactory();
57 
59  void registerFactory( int type, const char* ast_name, factory_type factory );
61  void setMaxNodeType( int type );
62 
64  void addASTChild(ASTPair& currentAST, RefAST child);
66  virtual RefAST create();
68  RefAST create(int type);
70  RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt);
72  RefAST create(RefAST tr);
74  RefAST create(RefToken tok);
76  RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile );
80  RefAST dup(RefAST t);
82  RefAST dupList(RefAST t);
86  RefAST dupTree(RefAST t);
93  RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes);
98  RefAST make(ASTArray* nodes);
100  void makeASTRoot(ASTPair& currentAST, RefAST root);
101 
109  void setASTNodeFactory( const char* factory_node_name, factory_type factory );
110 
111 #ifdef ANTLR_SUPPORT_XML
112 
116  RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
117 #endif
118 protected:
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 );
122 
123 #ifdef ANTLR_VECTOR_HAS_AT
124  inline RefAST getNodeOfType( unsigned int type )
126  {
127  return RefAST(nodeFactories.at(type)->second());
128  }
130  const char* getASTNodeType( unsigned int type )
131  {
132  return nodeFactories.at(type)->first;
133  }
135  factory_type getASTNodeFactory( unsigned int type )
136  {
137  return nodeFactories.at(type)->second;
138  }
139 #else
140  inline RefAST getNodeOfType( unsigned int type )
141  {
142  return RefAST(nodeFactories[type]->second());
143  }
145  const char* getASTNodeType( unsigned int type )
146  {
147  return nodeFactories[type]->first;
148  }
149  factory_type getASTNodeFactory( unsigned int type )
150  {
151  return nodeFactories[type]->second;
152  }
153 #endif
154 
155 private:
156  // no copying and such..
157  ASTFactory( const ASTFactory& );
158  ASTFactory& operator=( const ASTFactory& );
159 };
160 
161 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
162 }
163 #endif
164 
165 #endif //INC_ASTFactory_hpp__
factory_type_ factory_type
Definition: ASTFactory.hpp:38
Definition: ANTLRException.hpp:15
std ::vector< factory_descriptor_ *> factory_descriptor_list_
Definition: ASTFactory.hpp:26
factory_descriptor default_factory_descriptor
Definition: ASTFactory.hpp:44
std ::pair< const char *, factory_type_ > factory_descriptor_
Definition: ASTFactory.hpp:25
Definition: ASTArray.hpp:23
#define ANTLR_API
Definition: config.hpp:22
const char * getASTNodeType(unsigned int type)
get the name of the node &#39;type&#39;
Definition: ASTFactory.hpp:145
factory_type getASTNodeFactory(unsigned int type)
Definition: ASTFactory.hpp:149
Definition: ASTFactory.hpp:36
factory_descriptor_list_ factory_descriptor_list
Definition: ASTFactory.hpp:40
ASTRefCount< AST > RefAST
Definition: ASTRefCount.hpp:92
Definition: ASTPair.hpp:26
RefAST(* factory_type_)()
Definition: ASTFactory.hpp:24
#define ANTLR_USE_NAMESPACE(_x_)
Definition: config.hpp:18
RefAST getNodeOfType(unsigned int type)
Definition: ASTFactory.hpp:140
factory_descriptor_list nodeFactories
Definition: ASTFactory.hpp:45
factory_descriptor_ factory_descriptor
Definition: ASTFactory.hpp:39