#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 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 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 RecognizerSharedStateType : public Empty { public: typedef Empty BaseType; }; template class RecognizerType : public Empty { public: typedef Empty BaseType; }; typedef Empty TreeType; typedef Empty TreeUserDataType; typedef Empty TreeAdaptorType; typedef Empty TreeStoreType; template 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 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 TraitsSelector { public: typedef A selected; }; template class TraitsSelector { public: typedef B selected; }; template class TraitsOneArgSelector { public: typedef A selected; }; template class TraitsOneArgSelector { public: typedef B selected; }; template class BoolSelector { public: typedef A selected; }; template class BoolSelector { public: typedef B selected; }; /** * Base traits template * * This class contains default typenames for every trait */ template< template class UserTraits > class TraitsBase { public: typedef TraitsBase TraitsType; typedef typename TraitsSelector< typename UserTraits::AllocPolicyType, DefaultAllocPolicy >::selected AllocPolicyType; typedef typename TraitsSelector< typename UserTraits::StringType, std::string >::selected StringType; typedef typename TraitsSelector< typename UserTraits::StringStreamType, std::stringstream >::selected StringStreamType; typedef typename TraitsSelector< typename UserTraits::StreamDataType, ANTLR_UINT8 >::selected StreamDataType; typedef typename TraitsSelector< typename UserTraits::Endianness, RESOLVE_ENDIAN_AT_RUNTIME >::selected Endianness; typedef typename TraitsSelector< typename UserTraits::BitsetType, Bitset >::selected BitsetType; typedef typename TraitsSelector< typename UserTraits::BitsetListType, BitsetList >::selected BitsetListType; typedef typename TraitsSelector< typename UserTraits::InputStreamType, InputStream >::selected InputStreamType; template class IntStreamType : public TraitsOneArgSelector< typename UserTraits::template IntStreamType, IntStream, typename UserTraits::template IntStreamType::BaseType >::selected { }; typedef typename TraitsSelector< typename UserTraits::LexStateType, LexState >::selected LexStateType; static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = UserTraits::TOKENS_ACCESSED_FROM_OWNING_RULE; static const unsigned TOKEN_FILL_BUFFER_INCREMENT = UserTraits::TOKEN_FILL_BUFFER_INCREMENT; //used only if the above val is true static void displayRecognitionError( const StringType& str ) { UserTraits::displayRecognitionError(str); } }; /** * Final traits * * They combine Traits and user provided traits(UserTraits) */ template< class LxrType, class PsrType, template class UserTraits = CustomTraitsBase //, //class TreePsrType = antlr3::Empty //template class TreePsrType = TreeParser > class Traits : public TraitsBase { public: typedef Traits TraitsType; typedef TraitsBase BaseTraitsType; // CommonTokenType typedef typename TraitsSelector< typename UserTraits::CommonTokenType, CommonToken >::selected CommonTokenType; // TokenUserDataType typedef typename TraitsSelector< typename UserTraits::TokenUserDataType, Empty >::selected TokenUserDataType; // TokenListType typedef typename BaseTraitsType::AllocPolicyType::template ListType TokenListType; // TokenIntStreamType typedef typename TraitsSelector< typename UserTraits::TokenIntStreamType, TokenIntStream >::selected TokenIntStreamType; // TokenStreamType typedef typename TraitsSelector< typename UserTraits::TokenStreamType, CommonTokenStream >::selected TokenStreamType; // TreeNodeIntStreamType typedef typename TraitsSelector< typename UserTraits::TreeNodeIntStreamType, TreeNodeIntStream >::selected TreeNodeIntStreamType; // TreeNodeStreamType typedef typename TraitsSelector< typename UserTraits::TreeNodeStreamType, CommonTreeNodeStream >::selected TreeNodeStreamType; // DebugEventListenerType typedef typename TraitsSelector< typename UserTraits::DebugEventListenerType, DebugEventListener >::selected DebugEventListenerType; // RecognizerSharedStateType template class RecognizerSharedStateType : public TraitsOneArgSelector< typename UserTraits::template RecognizerSharedStateType, RecognizerSharedState, typename UserTraits::template RecognizerSharedStateType::BaseType >::selected {}; // RecognizerType template class RecognizerType : public TraitsOneArgSelector< typename UserTraits::template RecognizerType, BaseRecognizer, typename UserTraits::template RecognizerType::BaseType >::selected { public: typedef typename TraitsOneArgSelector< typename UserTraits::template RecognizerType, BaseRecognizer, typename UserTraits::template RecognizerType::BaseType >::selected BaseType; typedef typename BaseType::RecognizerSharedStateType RecognizerSharedStateType; public: RecognizerType(ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state) : BaseType( sizeHint, state ) { } }; // TreeType typedef typename TraitsSelector< typename UserTraits::TreeType, CommonTree >::selected TreeType; typedef typename TraitsSelector< typename UserTraits::TreeUserDataType, Empty >::selected TreeUserDataType; // TreeAdaptorType typedef typename TraitsSelector< typename UserTraits::TreeAdaptorType, CommonTreeAdaptor >::selected TreeAdaptorType; typedef typename TraitsSelector< typename UserTraits::TreeStoreType, CommonTreeStore >::selected TreeStoreType; typedef typename TreeStoreType::TreeTypePtr TreeTypePtr; //typedef std::unique_ptr> TreeTypePtr; // ExceptionBaseType template class ExceptionBaseType : public TraitsOneArgSelector< typename UserTraits::template ExceptionBaseType, ANTLR_ExceptionBase, typename UserTraits::template ExceptionBaseType::BaseType >::selected { public: typedef typename TraitsOneArgSelector< typename UserTraits::template ExceptionBaseType, ANTLR_ExceptionBase, typename UserTraits::template ExceptionBaseType::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::BaseLexerType, Lexer >::selected BaseLexerType; typedef LxrType LexerType; // TokenSourceType typedef typename TraitsSelector< typename UserTraits::TokenSourceType, TokenSource >::selected TokenSourceType; // this should be overridden with generated parser // BaseParserType typedef typename TraitsSelector< typename UserTraits::BaseParserType, Parser >::selected BaseParserType; typedef PsrType ParserType; // this should be overridden with generated treeparser (not implemented yet) // BaseTreeParserType typedef typename TraitsSelector< typename UserTraits::BaseTreeParserType, TreeParser >::selected BaseTreeParserType; //typedef TreePsrType TreeParserType; typedef BaseTreeParserType TreeParserType; // RewriteStreamType template class RewriteStreamType : public TraitsOneArgSelector< typename UserTraits::template RewriteStreamType, RewriteRuleElementStream, typename UserTraits::template RewriteStreamType::BaseType >::selected { public: typedef typename TraitsOneArgSelector< typename UserTraits::template RewriteStreamType, RewriteRuleElementStream, typename UserTraits::template RewriteStreamType::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::RuleReturnValueType, typename BoolSelector< TraitsType::TOKENS_ACCESSED_FROM_OWNING_RULE, RuleReturnValue_1, RuleReturnValue >::selected >::selected RuleReturnValueType; }; } #endif //_ANTLR3_TRAITS_HPP