aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr3_cpp_runtime/include/antlr3traits.hpp
blob: 420139c2db8cecbeaea2611666d52859c9f6e731 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#ifndef _ANTLR3_TRAITS_HPP 
#define _ANTLR3_TRAITS_HPP 
 
namespace antlr3 {
 
/**
 * Users implementing overrides should inherit from this
 *
 * All classes typenames reffer to Empty class
 */
template<class ImplTraits> 
class CustomTraitsBase 
{ 
public: 
	typedef Empty AllocPolicyType;
	typedef Empty StringType;
	typedef Empty StringStreamType;
	typedef Empty StreamDataType;
	typedef Empty Endianness;
 
	//collections
	typedef Empty BitsetType;
	typedef Empty BitsetListType;
 
	typedef Empty InputStreamType;
 
	template<class StreamType>
	class IntStreamType : public Empty
	{
	public:
		typedef Empty BaseType;
	};
 
	typedef Empty LexStateType;
 
	typedef Empty CommonTokenType;
	typedef Empty TokenUserDataType;
 
	typedef Empty TokenIntStreamType;
	typedef Empty TokenStreamType;
 
	typedef Empty TreeNodeIntStreamType;
	typedef Empty TreeNodeStreamType;
 
 
	typedef Empty DebugEventListenerType;
	template<class StreamType>
	class RecognizerSharedStateType : public Empty
	{
	public:
		typedef Empty BaseType;
	};
 
	template<class StreamType>
	class RecognizerType : public Empty
	{
	public:
		typedef Empty BaseType;
	};
	
	typedef Empty TreeType;
	typedef Empty TreeUserDataType;
	typedef Empty TreeAdaptorType;
	typedef Empty TreeStoreType;
	
	template<class StreamType>
	class ExceptionBaseType : public Empty
	{
	public:
		typedef Empty BaseType;
	};
 
	//this should be overridden with generated lexer
	typedef Empty BaseLexerType;
	
	typedef Empty TokenSourceType;
	typedef Empty BaseParserType;//this should be overridden with generated lexer
	typedef Empty BaseTreeParserType;
	
	template<class ElementType>
	class RewriteStreamType : public Empty
	{
	public:
		typedef Empty BaseType;
	};
 
	typedef Empty  RuleReturnValueType;
 
	//If we want to change the way tokens are stored
	static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = false;
	static const unsigned TOKEN_FILL_BUFFER_INCREMENT = 100; //used only if the above val is true
 
	static void displayRecognitionError( const std::string& str ) {  printf("%s", str.c_str() ); }
}; 
 
/**
 * Traits manipulation classes
 */
template<class A, class B> 
class TraitsSelector 
{ 
public: 
	typedef A selected;
}; 
 
template<class B> 
class TraitsSelector<Empty, B> 
{ 
public: 
	typedef B selected;
}; 
 
template<class A, class B, class C> 
class TraitsOneArgSelector 
{ 
public: 
	typedef A selected;
}; 
 
template<class A, class B> 
class TraitsOneArgSelector<A,B,Empty> 
{ 
public: 
	typedef B selected;
}; 
 
template<bool v, class A, class B> 
class BoolSelector 
{ 
public: 
	typedef A selected;
}; 
 
template<class A, class B> 
class BoolSelector<false, A, B> 
{ 
public: 
	typedef B selected;
}; 
 
/**
 * Base traits template
 *
 * This class contains default typenames for every trait
 */
template< template<class ImplTraits> class UserTraits > 
class TraitsBase 
{ 
public: 
	typedef TraitsBase  TraitsType;
	
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::AllocPolicyType,
					 DefaultAllocPolicy
					 >::selected  AllocPolicyType;
 
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::StringType, 
					 std::string
					 >::selected StringType;
	
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::StringStreamType, 
					 std::stringstream
					 >::selected StringStreamType;
 
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::StreamDataType, 
					 ANTLR_UINT8
					 >::selected StreamDataType;
	
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::Endianness, 
					 RESOLVE_ENDIAN_AT_RUNTIME
					 >::selected Endianness;
 
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::BitsetType, 
					 Bitset<TraitsType>
					 >::selected BitsetType;
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::BitsetListType, 
					 BitsetList<TraitsType>
					 >::selected BitsetListType;
 
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::InputStreamType, 
					 InputStream<TraitsType>
					 >::selected InputStreamType;
 
	template<class SuperType>
	class IntStreamType : public TraitsOneArgSelector< typename UserTraits<TraitsType>::template IntStreamType<SuperType>, 
							   IntStream<TraitsType, SuperType>,
							   typename UserTraits<TraitsType>::template IntStreamType<SuperType>::BaseType
							   >::selected  
	{ };
	
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::LexStateType, 
					 LexState<TraitsType>
					 >::selected LexStateType;
 
	static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = UserTraits<TraitsType>::TOKENS_ACCESSED_FROM_OWNING_RULE;
	static const unsigned TOKEN_FILL_BUFFER_INCREMENT = UserTraits<TraitsType>::TOKEN_FILL_BUFFER_INCREMENT; //used only if the above val is true
 
	static void displayRecognitionError( const StringType& str ) { UserTraits<TraitsType>::displayRecognitionError(str); }
};
 
/**
 * Final traits
 *
 * They combine Traits and user provided traits(UserTraits)
 */
template< class LxrType, 
	  class PsrType, 
	  template<class ImplTraits> class UserTraits = CustomTraitsBase
	  //,
	  //class TreePsrType = antlr3::Empty
	  //template<class ImplTraits> class TreePsrType = TreeParser
	  >
class Traits : public TraitsBase<UserTraits>
{
public:
	typedef Traits TraitsType;
	typedef TraitsBase<UserTraits> BaseTraitsType;	
 
	// CommonTokenType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::CommonTokenType, 
					 CommonToken<TraitsType> >::selected CommonTokenType;
 
	// TokenUserDataType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TokenUserDataType,
					 Empty >::selected TokenUserDataType;
 
	// TokenListType
	typedef typename BaseTraitsType::AllocPolicyType::template ListType<const CommonTokenType*> TokenListType;
 
	// TokenIntStreamType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TokenIntStreamType, 
					 TokenIntStream<TraitsType> >::selected TokenIntStreamType;
	// TokenStreamType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TokenStreamType,
					 CommonTokenStream<TraitsType> >::selected TokenStreamType;
 
	// TreeNodeIntStreamType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeNodeIntStreamType,
					  TreeNodeIntStream<TraitsType> >::selected TreeNodeIntStreamType;
 
	// TreeNodeStreamType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeNodeStreamType, 
					 CommonTreeNodeStream<TraitsType> >::selected TreeNodeStreamType;
 
	// DebugEventListenerType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::DebugEventListenerType, 
					 DebugEventListener<TraitsType> >::selected DebugEventListenerType;
 
	// RecognizerSharedStateType
	template<class StreamType>
	class  RecognizerSharedStateType : public TraitsOneArgSelector< typename UserTraits<TraitsType>::template RecognizerSharedStateType<StreamType>, 
									RecognizerSharedState<TraitsType, StreamType>,
									typename UserTraits<TraitsType>::template RecognizerSharedStateType<StreamType>::BaseType
									>::selected 
	{};
 
	// RecognizerType
	template<class StreamType>
	class  RecognizerType : public TraitsOneArgSelector< typename UserTraits<TraitsType>::template RecognizerType<StreamType>, 
							     BaseRecognizer<TraitsType, StreamType>,
							     typename UserTraits<TraitsType>::template RecognizerType<StreamType>::BaseType
							     >::selected 
	{
	public:
		typedef typename TraitsOneArgSelector< typename UserTraits<TraitsType>::template RecognizerType<StreamType>, 
						       BaseRecognizer<TraitsType, StreamType>,
						       typename UserTraits<TraitsType>::template RecognizerType<StreamType>::BaseType
						       >::selected  BaseType;
		typedef typename BaseType::RecognizerSharedStateType RecognizerSharedStateType;
 
	public:
		RecognizerType(ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state)
			: BaseType( sizeHint, state )
		{
		}
	};
 
	// TreeType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeType, 
					 CommonTree<TraitsType> >::selected TreeType;
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeUserDataType,
					 Empty >::selected TreeUserDataType;
	// TreeAdaptorType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeAdaptorType, 
					 CommonTreeAdaptor<TraitsType> >::selected TreeAdaptorType;
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TreeStoreType,
					 CommonTreeStore<TraitsType> >::selected TreeStoreType;
	typedef typename TreeStoreType::TreeTypePtr TreeTypePtr;
	//typedef std::unique_ptr<TreeType, ResourcePoolManager<ImplTraits>> TreeTypePtr;
 
	// ExceptionBaseType
	template<class StreamType>
	class ExceptionBaseType : public TraitsOneArgSelector< typename UserTraits<TraitsType>::template ExceptionBaseType<StreamType>, 
							       ANTLR_ExceptionBase<TraitsType, StreamType>, 
							       typename UserTraits<TraitsType>::template ExceptionBaseType<StreamType>::BaseType
							       >::selected 
	{
	public:
		typedef typename TraitsOneArgSelector< typename UserTraits<TraitsType>::template ExceptionBaseType<StreamType>, 
						       ANTLR_ExceptionBase<TraitsType, StreamType>,
						       typename UserTraits<TraitsType>::template ExceptionBaseType<StreamType>::BaseType
						       >::selected BaseType;
	
	protected:
		ExceptionBaseType( const typename BaseTraitsType::StringType& message )
			:BaseType(message)
		{
		}
	};
 
	// this should be overridden with generated lexer
	// BaseLexerType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::BaseLexerType, 
					 Lexer<TraitsType> >::selected BaseLexerType;
	typedef LxrType LexerType;
 
	// TokenSourceType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::TokenSourceType, 
					 TokenSource<TraitsType> >::selected TokenSourceType;
 
	// this should be overridden with generated parser
	// BaseParserType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::BaseParserType, 
					 Parser<TraitsType> >::selected BaseParserType;	
	typedef PsrType ParserType;
 
	// this should be overridden with generated treeparser (not implemented yet)
	// BaseTreeParserType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::BaseTreeParserType, 
					 TreeParser<TraitsType> >::selected BaseTreeParserType;
	//typedef TreePsrType<Traits> TreeParserType;
	typedef BaseTreeParserType TreeParserType;
 
	// RewriteStreamType
	template<class ElementType>
	class RewriteStreamType : public TraitsOneArgSelector< typename UserTraits<TraitsType>::template RewriteStreamType<ElementType>,
							       RewriteRuleElementStream<TraitsType, ElementType>,
							       typename UserTraits<TraitsType>::template RewriteStreamType<ElementType>::BaseType
							       >::selected 
	{
	public:
		typedef typename TraitsOneArgSelector< typename UserTraits<TraitsType>::template RewriteStreamType<ElementType>,
						       RewriteRuleElementStream<TraitsType, ElementType>,
						       typename UserTraits<TraitsType>::template RewriteStreamType<ElementType>::BaseType
						       >::selected BaseType;
 
		//typedef typename SuperType::StreamType StreamType;
		//typedef typename BaseType::RecognizerType Recognizer_Type;
		//typedef typename BaseType::ElementType ElementType;
		typedef typename BaseType::ElementsType ElementsType;
 
	public:
		RewriteStreamType(TreeAdaptorType* adaptor = NULL, const char* description = NULL)
			:BaseType(adaptor, description)
		{
		}
		RewriteStreamType(TreeAdaptorType* adaptor, const char* description, ElementType* oneElement)
			:BaseType(adaptor, description, oneElement)
		{
		}
		RewriteStreamType(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements)
			:BaseType(adaptor, description, elements)
		{
		}
	};
 
	// RuleReturnValueType
	typedef typename TraitsSelector< typename UserTraits<TraitsType>::RuleReturnValueType, 
					 typename BoolSelector< TraitsType::TOKENS_ACCESSED_FROM_OWNING_RULE, 
								RuleReturnValue_1<TraitsType>,
								RuleReturnValue<TraitsType>
								>::selected
					 >::selected RuleReturnValueType;
}; 
 
}
 
#endif //_ANTLR3_TRAITS_HPP