1 /** \file
2  * Definition of the ANTLR3 common tree adaptor.
3  */
4 
5 #ifndef	_ANTLR3_COMMON_TREE_ADAPTOR_HPP
6 #define	_ANTLR3_COMMON_TREE_ADAPTOR_HPP
7 
8 // [The "BSD licence"]
9 // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
10 
11 //
12 // All rights reserved.
13 //
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
16 // are met:
17 // 1. Redistributions of source code must retain the above copyright
18 //    notice, this list of conditions and the following disclaimer.
19 // 2. Redistributions in binary form must reproduce the above copyright
20 //    notice, this list of conditions and the following disclaimer in the
21 //    documentation and/or other materials provided with the distribution.
22 // 3. The name of the author may not be used to endorse or promote products
23 //    derived from this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 #include    "antlr3defs.hpp"
37 
38 ANTLR_BEGIN_NAMESPACE()
39 
40 template<class ImplTraits>
41 class CommonTreeAdaptor : public ImplTraits::AllocPolicyType
42 {
43 public:
44 	typedef typename ImplTraits::StringType StringType;
45 	typedef typename ImplTraits::TreeType TreeType;
46 	typedef	TreeType TokenType;
47 	typedef typename ImplTraits::CommonTokenType CommonTokenType;
48 	typedef typename ImplTraits::DebugEventListenerType DebuggerType;
49 
50 public:
51 	//The parameter is there only to provide uniform constructor interface
52 	CommonTreeAdaptor(DebuggerType* dbg = NULL);
53     TreeType*	  nilNode();
54 	TreeType*	  dupTree( TreeType* tree);
55     TreeType*	  dupTreeTT( TreeType* t, TreeType* tree);
56 
57     void	addChild( TreeType* t, TreeType* child);
58     void	addChildToken( TreeType* t, CommonTokenType* child);
59     void	setParent( TreeType* child, TreeType* parent);
60     TreeType*		getParent( TreeType* child);
61 
62 	TreeType*		errorNode( CommonTokenType* tnstream, CommonTokenType* startToken, CommonTokenType* stopToken);
63 	bool	isNilNode( TreeType* t);
64 
65     TreeType*	    becomeRoot( TreeType* newRoot, TreeType* oldRoot);
66     TreeType*	   	rulePostProcessing( TreeType* root);
67 
68     TreeType*	becomeRootToken(CommonTokenType* newRoot, TreeType* oldRoot);
69 
70     TreeType*	 	create( CommonTokenType* payload);
71     TreeType* 		createTypeToken( ANTLR_UINT32 tokenType, CommonTokenType* fromToken);
72     TreeType*	   	createTypeTokenText	( ANTLR_UINT32 tokenType, CommonTokenType* fromToken, const ANTLR_UINT8* text);
73     TreeType*	    createTypeText		( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text);
74 
75     TreeType*	    dupNode( TreeType* treeNode);
76     ANTLR_UINT32			getType( TreeType* t);
77     StringType			getText( TreeType* t);
78 
79     TreeType*	    getChild( TreeType* t, ANTLR_UINT32 i);
80     void	setChild( TreeType* t, ANTLR_UINT32 i, TreeType* child);
81     void	deleteChild( TreeType* t, ANTLR_UINT32 i);
82     void	setChildIndex( TreeType* t, ANTLR_INT32 i);
83     ANTLR_INT32	getChildIndex( TreeType* t);
84 
85     ANTLR_UINT32	getChildCount( TreeType*);
86 	ANTLR_UINT64	getUniqueID( TreeType*);
87 
88     CommonTokenType*    createToken( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text);
89     CommonTokenType*    createTokenFromToken( CommonTokenType* fromToken);
90     CommonTokenType*    getToken( TreeType* t);
91 
92     void setTokenBoundaries( TreeType* t, CommonTokenType* startToken, CommonTokenType* stopToken);
93     ANTLR_MARKER	getTokenStartIndex( TreeType* t);
94     ANTLR_MARKER	getTokenStopIndex( TreeType* t);
95 
96 	/// Produce a DOT (see graphviz freeware suite) from a base tree
97 	///
98 	StringType			makeDot( TreeType* theTree);
99 
100 	/// Replace from start to stop child index of parent with t, which might
101 	/// be a list.  Number of children may be different
102 	/// after this call.
103 	///
104 	/// If parent is null, don't do anything; must be at root of overall tree.
105 	/// Can't replace whatever points to the parent externally.  Do nothing.
106 	///
107 	void replaceChildren( TreeType* parent, ANTLR_INT32 startChildIndex,
108 								  ANTLR_INT32 stopChildIndex, TreeType* t);
109 
110     ~CommonTreeAdaptor();
111 
112 protected:
113 	void defineDotNodes(TreeType* t, const StringType& dotSpec);
114 	void defineDotEdges(TreeType* t, const StringType& dotSpec);
115 };
116 
117 //If someone can override the CommonTreeAdaptor at the compile time, that will be
118 //inherited here. Still you can choose to override the DebugTreeAdaptor, if you wish to
119 //change the DebugTreeAdaptor
120 template<class ImplTraits>
121 class DebugTreeAdaptor : public ImplTraits::CommonTreeAdaptorType
122 {
123 public:
124 	//DebugEventListener implements functionality through virtual functions
125 	//the template parameter is required for pointing back at the adaptor
126 	typedef typename ImplTraits::DebugEventListener DebuggerType;
127 	typedef typename ImplTraits::TreeType TreeType;
128 	typedef typename ImplTraits::CommonTokenType CommonTokenType;
129 
130 private:
131 	/// If set to something other than NULL, then this structure is
132 	/// points to an instance of the debugger interface. In general, the
133 	/// debugger is only referenced internally in recovery/error operations
134 	/// so that it does not cause overhead by having to check this pointer
135 	/// in every function/method
136 	///
137 	DebuggerType*		m_debugger;
138 
139 public:
140 	DebugTreeAdaptor( DebuggerType* debugger );
141 	void setDebugEventListener( DebuggerType* debugger);
142 	TreeType*	  nilNode();
143 	void	addChild(TreeType* t, TreeType* child);
144 	void	addChildToken(TreeType* t, CommonTokenType* child);
145 	TreeType* becomeRoot( TreeType* newRootTree, TreeType* oldRootTree );
146 	TreeType* becomeRootToken(TreeType* newRoot, TreeType* oldRoot);
147 	TreeType* createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken);
148 	TreeType* createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text);
149 	TreeType* createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text);
150 	TreeType* dupTree( TreeType* tree);
151 
152 	/// Sends the required debugging events for duplicating a tree
153 	/// to the debugger.
154 	///
155 	void simulateTreeConstruction(TreeType* tree);
156 };
157 
158 
159 ANTLR_END_NAMESPACE()
160 
161 #include "antlr3commontreeadaptor.inl"
162 
163 #endif
164