~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/antlr/antlr/ASTFactory.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/RIGHTS.html
 
7
 *
 
8
 */
 
9
 
 
10
#include <antlr/config.hpp>
 
11
#include <antlr/AST.hpp>
 
12
#include <antlr/ASTArray.hpp>
 
13
#include <antlr/ASTPair.hpp>
 
14
 
 
15
#include <utility>
 
16
 
 
17
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 
18
namespace antlr {
 
19
#endif
 
20
 
 
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
 
24
 * chain.
 
25
 *
 
26
 * @todo check all this code for possible use of references in
 
27
 * stead of RefAST's.
 
28
 */
 
29
class ANTLR_API ASTFactory {
 
30
public:
 
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;
 
34
protected:
 
35
        /* The mapping of AST node type to factory..
 
36
         */
 
37
        factory_descriptor default_factory_descriptor;
 
38
        factory_descriptor_list nodeFactories;
 
39
public:
 
40
        /// Make new factory. Per default (Ref)CommonAST instances are generated.
 
41
        ASTFactory();
 
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
 
45
         * instance)
 
46
         */
 
47
        ASTFactory( const char* factory_node_name, factory_type factory );
 
48
        /// Destroy factory
 
49
        virtual ~ASTFactory();
 
50
 
 
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 );
 
55
 
 
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.
 
72
         */
 
73
        RefAST dup(RefAST t);
 
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.
 
78
         */
 
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).
 
85
         */
 
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
 
90
         */
 
91
        RefAST make(ASTArray* nodes);
 
92
        /// Make an AST the root of current AST
 
93
        void makeASTRoot(ASTPair& currentAST, RefAST root);
 
94
 
 
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
 
98
         * instance).
 
99
         * Only change factory between parser runs. You might get unexpected results
 
100
         * otherwise.
 
101
         */
 
102
        void setASTNodeFactory( const char* factory_node_name, factory_type factory );
 
103
 
 
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.
 
108
         */
 
109
        RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
 
110
#endif
 
111
protected:
 
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 );
 
115
 
 
116
#ifdef ANTLR_VECTOR_HAS_AT
 
117
        /// construct a node of 'type'
 
118
        inline RefAST getNodeOfType( unsigned int type )
 
119
        {
 
120
                return RefAST(nodeFactories.at(type)->second());
 
121
        }
 
122
        /// get the name of the node 'type'
 
123
        const char* getASTNodeType( unsigned int type )
 
124
        {
 
125
                return nodeFactories.at(type)->first;
 
126
        }
 
127
        /// get the factory used for node 'type'
 
128
        factory_type getASTNodeFactory( unsigned int type )
 
129
        {
 
130
                return nodeFactories.at(type)->second;
 
131
        }
 
132
#else
 
133
        inline RefAST getNodeOfType( unsigned int type )
 
134
        {
 
135
                return RefAST(nodeFactories[type]->second());
 
136
        }
 
137
        /// get the name of the node 'type'
 
138
        const char* getASTNodeType( unsigned int type )
 
139
        {
 
140
                return nodeFactories[type]->first;
 
141
        }
 
142
        factory_type getASTNodeFactory( unsigned int type )
 
143
        {
 
144
                return nodeFactories[type]->second;
 
145
        }
 
146
#endif
 
147
 
 
148
private:
 
149
        // no copying and such..
 
150
        ASTFactory( const ASTFactory& );
 
151
        ASTFactory& operator=( const ASTFactory& );
 
152
};
 
153
 
 
154
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 
155
}
 
156
#endif
 
157
 
 
158
#endif //INC_ASTFactory_hpp__