antlr3traits.hpp 12 KB

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