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/RIGHTS.html
10
#include <antlr/config.hpp>
11
#include <antlr/AST.hpp>
12
#include <antlr/ASTArray.hpp>
13
#include <antlr/ASTPair.hpp>
17
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
21
/** AST Super Factory shared by TreeParser and Parser.
22
* This super factory maintains a map of all AST node types to their respective
23
* AST factories. One instance should be shared among a parser/treeparser
26
* @todo check all this code for possible use of references in
29
class ANTLR_API ASTFactory {
31
typedef RefAST (*factory_type)();
32
typedef ANTLR_USE_NAMESPACE(std)pair< const char*, factory_type > factory_descriptor;
33
typedef ANTLR_USE_NAMESPACE(std)vector< factory_descriptor* > factory_descriptor_list;
35
/* The mapping of AST node type to factory..
37
factory_descriptor default_factory_descriptor;
38
factory_descriptor_list nodeFactories;
40
/// Make new factory. Per default (Ref)CommonAST instances are generated.
42
/** Initialize factory with a non default node type.
43
* factory_node_name should be the name of the AST node type the factory
44
* generates. (should exist during the existance of this ASTFactory
47
ASTFactory( const char* factory_node_name, factory_type factory );
49
virtual ~ASTFactory();
51
/// Register a node factory for the node type type with name ast_name
52
void registerFactory( int type, const char* ast_name, factory_type factory );
53
/// Set the maximum node (AST) type this factory may encounter
54
void setMaxNodeType( int type );
56
/// Add a child to the current AST
57
void addASTChild(ASTPair& currentAST, RefAST child);
58
/// Create new empty AST node. The right default type shou
59
virtual RefAST create();
60
/// Create AST node of the right type for 'type'
61
RefAST create(int type);
62
/// Create AST node of the right type for 'type' and initialize with txt
63
RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt);
64
/// Create duplicate of tr
65
RefAST create(RefAST tr);
66
/// Create new AST node and initialize contents from a token.
67
RefAST create(RefToken tok);
68
/// Create new AST node and initialize contents from a stream.
69
RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile );
70
/** Deep copy a single node. This function the new clone() methods in the
71
* AST interface. Returns a new RefAST(nullASTptr) if t is null.
74
/// Duplicate tree including siblings of root.
75
RefAST dupList(RefAST t);
76
/** Duplicate a tree, assuming this is a root node of a tree--
77
* duplicate that node and what's below; ignore siblings of root node.
79
RefAST dupTree(RefAST t);
80
/** Make a tree from a list of nodes. The first element in the
81
* array is the root. If the root is null, then the tree is
82
* a simple list not a tree. Handles null children nodes correctly.
83
* For example, make(a, b, null, c) yields tree (a b c). make(null,a,b)
84
* yields tree (nil a b).
86
RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes);
87
/** Make a tree from a list of nodes, where the nodes are contained
88
* in an ASTArray object. The ASTArray is deleted after use.
89
* \FIXME I have a feeling we can get rid of this ugly ASTArray thing
91
RefAST make(ASTArray* nodes);
92
/// Make an AST the root of current AST
93
void makeASTRoot(ASTPair& currentAST, RefAST root);
95
/** Set a new default AST type.
96
* factory_node_name should be the name of the AST node type the factory
97
* generates. (should exist during the existance of this ASTFactory
99
* Only change factory between parser runs. You might get unexpected results
102
void setASTNodeFactory( const char* factory_node_name, factory_type factory );
104
#ifdef ANTLR_SUPPORT_XML
105
/** Load a XML AST from stream. Make sure you have all the factories
106
* registered before use.
107
* @note this 'XML' stuff is quite rough still. YMMV.
109
RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
112
void loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
113
void loadSiblings( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
114
bool checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& infile );
116
#ifdef ANTLR_VECTOR_HAS_AT
117
/// construct a node of 'type'
118
inline RefAST getNodeOfType( unsigned int type )
120
return RefAST(nodeFactories.at(type)->second());
122
/// get the name of the node 'type'
123
const char* getASTNodeType( unsigned int type )
125
return nodeFactories.at(type)->first;
127
/// get the factory used for node 'type'
128
factory_type getASTNodeFactory( unsigned int type )
130
return nodeFactories.at(type)->second;
133
inline RefAST getNodeOfType( unsigned int type )
135
return RefAST(nodeFactories[type]->second());
137
/// get the name of the node 'type'
138
const char* getASTNodeType( unsigned int type )
140
return nodeFactories[type]->first;
142
factory_type getASTNodeFactory( unsigned int type )
144
return nodeFactories[type]->second;
149
// no copying and such..
150
ASTFactory( const ASTFactory& );
151
ASTFactory& operator=( const ASTFactory& );
154
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
158
#endif //INC_ASTFactory_hpp__