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