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 | |
---|
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 |
---|
31 | * chain. |
---|
32 | * |
---|
33 | * @todo check all this code for possible use of references in |
---|
34 | * stead of RefAST's. |
---|
35 | */ |
---|
36 | class ANTLR_API ASTFactory { |
---|
37 | public: |
---|
38 | typedef factory_type_ factory_type; |
---|
39 | typedef factory_descriptor_ factory_descriptor; |
---|
40 | typedef factory_descriptor_list_ factory_descriptor_list; |
---|
41 | protected: |
---|
42 | /* The mapping of AST node type to factory.. |
---|
43 | */ |
---|
44 | factory_descriptor default_factory_descriptor; |
---|
45 | factory_descriptor_list nodeFactories; |
---|
46 | public: |
---|
47 | /// Make new factory. Per default (Ref)CommonAST instances are generated. |
---|
48 | ASTFactory(); |
---|
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 |
---|
52 | * instance) |
---|
53 | */ |
---|
54 | ASTFactory( const char* factory_node_name, factory_type factory ); |
---|
55 | /// Destroy factory |
---|
56 | virtual ~ASTFactory(); |
---|
57 | |
---|
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 ); |
---|
62 | |
---|
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. |
---|
79 | */ |
---|
80 | RefAST dup(RefAST t); |
---|
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. |
---|
85 | */ |
---|
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). |
---|
92 | */ |
---|
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 |
---|
97 | */ |
---|
98 | RefAST make(ASTArray* nodes); |
---|
99 | /// Make an AST the root of current AST |
---|
100 | void makeASTRoot(ASTPair& currentAST, RefAST root); |
---|
101 | |
---|
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 |
---|
105 | * instance). |
---|
106 | * Only change factory between parser runs. You might get unexpected results |
---|
107 | * otherwise. |
---|
108 | */ |
---|
109 | void setASTNodeFactory( const char* factory_node_name, factory_type factory ); |
---|
110 | |
---|
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. |
---|
115 | */ |
---|
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 | /// construct a node of 'type' |
---|
125 | inline RefAST getNodeOfType( unsigned int type ) |
---|
126 | { |
---|
127 | return RefAST(nodeFactories.at(type)->second()); |
---|
128 | } |
---|
129 | /// get the name of the node 'type' |
---|
130 | const char* getASTNodeType( unsigned int type ) |
---|
131 | { |
---|
132 | return nodeFactories.at(type)->first; |
---|
133 | } |
---|
134 | /// get the factory used for node 'type' |
---|
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 | } |
---|
144 | /// get the name of the node 'type' |
---|
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__ |
---|