diff --git a/runtime/C/src/antlr3baserecognizer.c b/runtime/C/src/antlr3baserecognizer.c index 8c6b105c7..667ad7ac9 100644 --- a/runtime/C/src/antlr3baserecognizer.c +++ b/runtime/C/src/antlr3baserecognizer.c @@ -2141,14 +2141,16 @@ reset(pANTLR3_BASE_RECOGNIZER recognizer) recognizer->state->ruleMemo = antlr3IntTrieNew(15); /* 16 bit depth is enough for 32768 rules! */ } } - - // ml: 2013-11-05, added reset of old exceptions. - pANTLR3_EXCEPTION thisE = recognizer->state->exception; - if (thisE != NULL) - { - thisE->freeEx(thisE); - recognizer->state->exception = NULL; - } + + { + // ml: 2013-11-05, added reset of old exceptions. + pANTLR3_EXCEPTION thisE = recognizer->state->exception; + if (thisE != NULL) + { + thisE->freeEx(thisE); + recognizer->state->exception = NULL; + } + } // Install a new following set // diff --git a/runtime/C/src/antlr3basetree.c b/runtime/C/src/antlr3basetree.c index f191f6fde..593f52b5d 100644 --- a/runtime/C/src/antlr3basetree.c +++ b/runtime/C/src/antlr3basetree.c @@ -214,9 +214,11 @@ addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child) tree->createChildrenList(tree); } - ANTLR3_UINT32 count = tree->children->add(tree->children, child, (void (ANTLR3_CDECL *)(void *))child->free); - child->setChildIndex(child, count - 1); - child->setParent(child, tree); + { + ANTLR3_UINT32 count = tree->children->add(tree->children, child, (void (ANTLR3_CDECL *)(void *))child->free); + child->setChildIndex(child, count - 1); + child->setParent(child, tree); + } } } diff --git a/runtime/Cpp/include/antlr3.hpp b/runtime/Cpp/include/antlr3.hpp old mode 100755 new mode 100644 index 4e40ba48a..d8cccb4aa --- a/runtime/Cpp/include/antlr3.hpp +++ b/runtime/Cpp/include/antlr3.hpp @@ -28,33 +28,62 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include +#include +#include // ptrdiff_t +#include // uint32_t, ... +#include // stderr (TODO remove fprintf(stderr) +#include // malloc, calloc +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include + +#include "antlr3defs.hpp" + +/* Pre declare the typedefs for all the interfaces, then + * they can be inter-dependant and we will let the linker + * sort it out for us. + */ +#include "antlr3interfaces.hpp" + +// Include the unicode.org conversion library header. +// +#include "antlr3convertutf.hpp" + +#include "antlr3errors.hpp" +#include "antlr3memory.hpp" -#include "antlr3defs.hpp" - -#include "antlr3errors.hpp" -#include "antlr3memory.hpp" - -#include "antlr3recognizersharedstate.hpp" -#include "antlr3baserecognizer.hpp" -#include "antlr3bitset.hpp" -#include "antlr3collections.hpp" -#include "antlr3commontoken.hpp" -#include "antlr3commontree.hpp" -#include "antlr3commontreeadaptor.hpp" -#include "antlr3cyclicdfa.hpp" -#include "antlr3debugeventlistener.hpp" -#include "antlr3exception.hpp" -#include "antlr3filestream.hpp" -#include "antlr3intstream.hpp" -#include "antlr3input.hpp" -#include "antlr3tokenstream.hpp" -#include "antlr3commontreenodestream.hpp" -#include "antlr3lexer.hpp" -#include "antlr3parser.hpp" -#include "antlr3rewritestreams.hpp" -#include "antlr3traits.hpp" -#include "antlr3treeparser.hpp" +#include "antlr3collections.hpp" +#include "antlr3recognizersharedstate.hpp" +#include "antlr3baserecognizer.hpp" +#include "antlr3bitset.hpp" +#include "antlr3commontoken.hpp" +#include "antlr3commontree.hpp" +#include "antlr3commontreeadaptor.hpp" +#include "antlr3cyclicdfa.hpp" +#include "antlr3debugeventlistener.hpp" +#include "antlr3exception.hpp" +#include "antlr3filestream.hpp" +#include "antlr3intstream.hpp" +#include "antlr3input.hpp" +#include "antlr3tokenstream.hpp" +#include "antlr3commontreenodestream.hpp" +#include "antlr3lexer.hpp" +#include "antlr3parser.hpp" +//#include "antlr3rewritestreams.hpp" +#include "antlr3rewriteruletokenstream.hpp" +#include "antlr3rewriterulesubtreestream.hpp" +#include "antlr3traits.hpp" +#include "antlr3treeparser.hpp" #endif diff --git a/runtime/Cpp/include/antlr3.inl b/runtime/Cpp/include/antlr3.inl old mode 100755 new mode 100644 index 79974afc1..b2d223398 --- a/runtime/Cpp/include/antlr3.inl +++ b/runtime/Cpp/include/antlr3.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { //static ANTLR_INLINE void GenericStream::displayRecognitionError( const StringType& str ) @@ -6,4 +6,4 @@ ANTLR_INLINE void GenericStream::displayRecognitionError( const StringType& str fprintf(stderr, str.c_str() ); } -ANTLR_END_NAMESPACE() \ No newline at end of file +} diff --git a/runtime/Cpp/include/antlr3baserecognizer.hpp b/runtime/Cpp/include/antlr3baserecognizer.hpp old mode 100755 new mode 100644 index f125400b9..0374b3a1e --- a/runtime/Cpp/include/antlr3baserecognizer.hpp +++ b/runtime/Cpp/include/antlr3baserecognizer.hpp @@ -35,10 +35,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" -#include "antlr3collections.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** \brief Base tracking context structure for all types of * recognizers. @@ -501,7 +498,7 @@ class BaseRecognizer : public ImplTraits::AllocPolicyType }; -ANTLR_END_NAMESPACE() +} #include "antlr3baserecognizer.inl" diff --git a/runtime/Cpp/include/antlr3baserecognizer.inl b/runtime/Cpp/include/antlr3baserecognizer.inl old mode 100755 new mode 100644 index 5d5acbfc2..8eaa416e1 --- a/runtime/Cpp/include/antlr3baserecognizer.inl +++ b/runtime/Cpp/include/antlr3baserecognizer.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template< class ImplTraits, class StreamType > BaseRecognizer::BaseRecognizer(ANTLR_UINT32 sizeHint, @@ -62,15 +62,16 @@ BaseRecognizer::match(ANTLR_UINT32 ttype, BitsetListType // const UnitType* matchedSymbol = this->getCurrentInputSymbol(is); - if (is->_LA(1) == ttype) - { + //if (is->LA(1) == ttype) + if (matchedSymbol->get_type() == ttype) + { // The token was the one we were told to expect // is->consume(); // Consume that token from the stream m_state->set_errorRecovery(false); // Not in error recovery now (if we were) m_state->set_failed(false); // The match was a success return matchedSymbol; // We are done - } + } // We did not find the expected token type, if we are backtracking then // we just set the failed flag and return. @@ -107,7 +108,7 @@ void BaseRecognizer::matchAny() template< class ImplTraits, class StreamType > bool BaseRecognizer::mismatchIsUnwantedToken(IntStreamType* is, ANTLR_UINT32 ttype) { - ANTLR_UINT32 nextt = is->_LA(2); + ANTLR_UINT32 nextt = is->LA(2); if (nextt == ttype) { @@ -172,7 +173,7 @@ bool BaseRecognizer::mismatchIsMissingToken(IntStreamTyp /// in follow set to indicate that the fall of the start symbol is /// in the set (EOF can follow). /// - if ( followClone->isMember(is->_LA(1)) + if ( followClone->isMember(is->LA(1)) || followClone->isMember(ImplTraits::CommonTokenType::EOR_TOKEN_TYPE) ) { @@ -579,7 +580,7 @@ bool BaseRecognizer::recoverFromMismatchedElement(Bitse * is consistent, then we can "insert" that token by not throwing * an exception and assuming that we saw it. */ - if ( follow->isMember(is->_LA(1)) == true) + if ( follow->isMember(is->LA(1)) == true) { /* report the error, but don't cause any rules to abort and stuff */ @@ -612,14 +613,14 @@ void BaseRecognizer::consumeUntil(ANTLR_UINT32 tokenTy // What do have at the moment? // - ANTLR_UINT32 ttype = is->_LA(1); + ANTLR_UINT32 ttype = is->LA(1); // Start eating tokens until we get to the one we want. // while (ttype != ImplTraits::CommonTokenType::TOKEN_EOF && ttype != tokenType) { is->consume(); - ttype = is->_LA(1); + ttype = is->LA(1); } } @@ -632,14 +633,14 @@ void BaseRecognizer::consumeUntilSet(BitsetType* set) // What do have at the moment? // - ttype = is->_LA(1); + ttype = is->LA(1); // Start eating tokens until we get to one we want. // while (ttype != ImplTraits::CommonTokenType::TOKEN_EOF && set->isMember(ttype) == false) { is->consume(); - ttype = is->_LA(1); + ttype = is->LA(1); } } @@ -773,7 +774,7 @@ BaseRecognizer::getCurrentInputSymbol(IntStreamType* ist { typedef typename ImplTraits::TokenStreamType TokenStreamType; TokenStreamType* token_stream = static_cast(istream); - return token_stream->_LT(1); + return token_stream->LT(1); } template< class ImplTraits, class StreamType > @@ -782,7 +783,7 @@ BaseRecognizer::getCurrentInputSymbol(IntStreamType* ist { typedef typename ImplTraits::TreeNodeStreamType TreeNodeStreamType; TreeNodeStreamType* ctns = static_cast(istream); - return ctns->_LT(1); + return ctns->LT(1); } @@ -916,4 +917,4 @@ BaseRecognizer::~BaseRecognizer() -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3bitset.hpp b/runtime/Cpp/include/antlr3bitset.hpp old mode 100755 new mode 100644 index a711b8a48..68eab6956 --- a/runtime/Cpp/include/antlr3bitset.hpp +++ b/runtime/Cpp/include/antlr3bitset.hpp @@ -36,9 +36,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** How many bits in the elements */ @@ -216,7 +214,7 @@ class Bitset : public ImplTraits::AllocPolicyType }; -ANTLR_END_NAMESPACE() +} #include "antlr3bitset.inl" diff --git a/runtime/Cpp/include/antlr3bitset.inl b/runtime/Cpp/include/antlr3bitset.inl old mode 100755 new mode 100644 index ad2f62050..64318ea0e --- a/runtime/Cpp/include/antlr3bitset.inl +++ b/runtime/Cpp/include/antlr3bitset.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template ANTLR_INLINE BitsetList::BitsetList() @@ -489,4 +489,4 @@ Bitset* Bitset::BitsetFromList(const IntListType& list) return bitset; } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3collections.hpp b/runtime/Cpp/include/antlr3collections.hpp old mode 100755 new mode 100644 index 21114039b..7551c243d --- a/runtime/Cpp/include/antlr3collections.hpp +++ b/runtime/Cpp/include/antlr3collections.hpp @@ -29,9 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /* -------------- TRIE Interfaces ---------------- */ @@ -276,7 +274,7 @@ class Topo : public ImplTraits::AllocPolicyType ~Topo(); }; -ANTLR_END_NAMESPACE() +} #include "antlr3collections.inl" diff --git a/runtime/Cpp/include/antlr3collections.inl b/runtime/Cpp/include/antlr3collections.inl old mode 100755 new mode 100644 index fb713c217..15cf9f179 --- a/runtime/Cpp/include/antlr3collections.inl +++ b/runtime/Cpp/include/antlr3collections.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template< class ImplTraits, class DataType > ANTLR_INLINE TrieEntry::TrieEntry(const DataType& data, TrieEntry* next) @@ -992,4 +992,4 @@ Topo::~Topo() } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3commontoken.hpp b/runtime/Cpp/include/antlr3commontoken.hpp old mode 100755 new mode 100644 index fd306e738..51fa3954a --- a/runtime/Cpp/include/antlr3commontoken.hpp +++ b/runtime/Cpp/include/antlr3commontoken.hpp @@ -39,11 +39,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** The definition of an ANTLR3 common token structure, which all implementations * of a token stream should provide, installing any further structures in the @@ -60,7 +56,7 @@ class CommonToken : public ImplTraits::AllocPolicyType public: /* Base token types, which all lexer/parser tokens come after in sequence. */ - enum TOKEN_TYPE + enum TOKEN_TYPE : ANTLR_UINT32 { /** Indicator of an invalid token */ @@ -78,13 +74,18 @@ class CommonToken : public ImplTraits::AllocPolicyType /** End of file token */ - , TOKEN_EOF = (ANTLR_CHARSTREAM_EOF & 0xFFFFFFFF) +#ifndef _MSC_VER + , TOKEN_EOF = std::numeric_limits::max() +#else + , TOKEN_EOF = 0xFFFFFFFF +#endif }; typedef typename ImplTraits::TokenIntStreamType TokenIntStreamType; typedef typename ImplTraits::StringType StringType; typedef typename ImplTraits::InputStreamType InputStreamType; typedef typename ImplTraits::StreamDataType StreamDataType; + typedef typename ImplTraits::TokenUserDataType UserDataType; private: /** The actual type of this token @@ -134,6 +135,8 @@ class CommonToken : public ImplTraits::AllocPolicyType CommonToken(TOKEN_TYPE type); CommonToken( const CommonToken& ctoken ); + ~CommonToken() {} + CommonToken& operator=( const CommonToken& ctoken ); bool operator==( const CommonToken& ctoken ) const; bool operator<( const CommonToken& ctoken ) const; @@ -150,7 +153,7 @@ class CommonToken : public ImplTraits::AllocPolicyType /** Function that returns the text pointer of a token, use * toString() if you want a pANTLR3_STRING version of the token. */ - StringType getText() const; + StringType const & getText() const; /** Pointer to a function that 'might' be able to set the text associated * with a token. Imaginary tokens such as an ANTLR3_CLASSIC_TOKEN may actually @@ -241,9 +244,10 @@ class CommonToken : public ImplTraits::AllocPolicyType */ StringType toString() const; + UserDataType UserData; }; -ANTLR_END_NAMESPACE() +} #include "antlr3commontoken.inl" diff --git a/runtime/Cpp/include/antlr3commontoken.inl b/runtime/Cpp/include/antlr3commontoken.inl old mode 100755 new mode 100644 index 87194dca1..3277b3def --- a/runtime/Cpp/include/antlr3commontoken.inl +++ b/runtime/Cpp/include/antlr3commontoken.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template CommonToken::CommonToken() @@ -45,6 +45,7 @@ CommonToken::CommonToken(TOKEN_TYPE type) template CommonToken::CommonToken( const CommonToken& ctoken ) :m_tokText( ctoken.m_tokText ) + ,UserData(ctoken.UserData) { m_type = ctoken.m_type; m_channel = ctoken.m_channel; @@ -60,6 +61,7 @@ CommonToken::CommonToken( const CommonToken& ctoken ) template CommonToken& CommonToken::operator=( const CommonToken& ctoken ) { + UserData = ctoken.UserData; m_type = ctoken.m_type; m_channel = ctoken.m_channel; m_lineStart = ctoken.m_lineStart; @@ -119,8 +121,12 @@ void CommonToken::set_input( InputStreamType* input ) } template -typename CommonToken::StringType CommonToken::getText() const +typename CommonToken::StringType const & +CommonToken::getText() const { + static const StringType EOF_STRING(""); + static const StringType EMPTY_STRING(""); + if ( !m_tokText.empty() ) return m_tokText; @@ -128,19 +134,19 @@ typename CommonToken::StringType CommonToken::getText() // if ( m_type == TOKEN_EOF) { - m_tokText = ""; - return m_tokText; + return EOF_STRING; } // We had nothing installed in the token, create a new string // from the input stream // - if (m_input != NULL) - return m_input->substr( this->get_startIndex(), this->get_stopIndex() ); - + if ( m_input != NULL) + { + return m_tokText = m_input->substr( this->get_startIndex(), this->get_stopIndex() ); + } // Nothing to return, there is no input stream // - return ""; + return EMPTY_STRING; } template @@ -164,7 +170,7 @@ ANTLR_INLINE void CommonToken::setText(const char* text) if( text == NULL ) m_tokText.clear(); else - m_tokText = (const char*) text; + m_tokText = text; } template @@ -319,4 +325,4 @@ typename CommonToken::StringType CommonToken::toString( return outtext.str(); } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3commontree.hpp b/runtime/Cpp/include/antlr3commontree.hpp old mode 100755 new mode 100644 index 39096f8a0..2a5e61f94 --- a/runtime/Cpp/include/antlr3commontree.hpp +++ b/runtime/Cpp/include/antlr3commontree.hpp @@ -33,9 +33,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template class CommonTree : public ImplTraits::AllocPolicyType @@ -45,20 +43,17 @@ class CommonTree : public ImplTraits::AllocPolicyType typedef typename ImplTraits::StringType StringType; typedef typename ImplTraits::CommonTokenType CommonTokenType; typedef typename ImplTraits::TreeType TreeType; - typedef CommonTree TokenType; - typedef typename AllocPolicyType::template VectorType ChildrenType; - typedef typename AllocPolicyType::template ListType ChildListType; - -private: + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; + //typedef CommonTree TokenType; + typedef typename AllocPolicyType::template VectorType ChildrenType; + typedef typename AllocPolicyType::template ListType ChildListType; + typedef typename ImplTraits::TreeUserDataType UserDataType; +protected: /// The list of all the children that belong to this node. They are not part of the node /// as they belong to the common tree node that implements this. /// ChildrenType m_children; - /// This is used to store the current child index position while descending - /// and ascending trees as the tree walk progresses. - /// - ANTLR_MARKER m_savedIndex; /// Start token index that encases this tree /// @@ -70,7 +65,7 @@ class CommonTree : public ImplTraits::AllocPolicyType /// A single token, this is the payload for the tree /// - CommonTokenType* m_token; + const CommonTokenType* m_token; /// Points to the node that has this node as a child. /// If this is NULL, then this is the root node. @@ -84,53 +79,71 @@ class CommonTree : public ImplTraits::AllocPolicyType public: CommonTree(); - CommonTree( CommonTokenType* token ); - CommonTree( CommonTree* token ); + CommonTree( const CommonTokenType* token ); + CommonTree( const CommonTree* token ); CommonTree( const CommonTree& ctree ); + ~CommonTree(); + + const CommonTokenType* get_token() const; + void set_token(CommonTokenType const*); - TokenType* get_token() const; ChildrenType& get_children(); const ChildrenType& get_children() const; - ChildrenType* get_children_p(); ANTLR_INT32 get_childIndex() const; TreeType* get_parent() const; + ANTLR_MARKER get_startIndex() const; + void set_startIndex(ANTLR_MARKER index); + + ANTLR_MARKER get_stopIndex() const; + void set_stopIndex(ANTLR_MARKER index); + void set_parent( TreeType* parent); void set_childIndex( ANTLR_INT32 ); - void addChild(TreeType* child); + void addChild(TreeTypePtr& child); /// Add all elements of the supplied list as children of this node /// void addChildren(const ChildListType& kids); - void createChildrenList(); - TreeType* deleteChild(ANTLR_UINT32 i); + + TreeTypePtr deleteChild(ANTLR_UINT32 i); /// Delete children from start to stop and replace with t even if t is /// a list (nil-root tree). Num of children can increase or decrease. /// For huge child lists, inserting children can force walking rest of /// children to set their child index; could be slow. /// - void replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeType* t); + void replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr t); + + // clone itself CommonTree* dupNode() const; - TreeType* dupTree(); - ANTLR_UINT32 getCharPositionInLine(); - TreeType* getChild(ANTLR_UINT32 i); - + // clone itself in pre-allocated storage + CommonTree* dupNode(void *) const; + + + ANTLR_UINT32 get_charPositionInLine() const; + ANTLR_UINT32 get_line() const; + + TreeTypePtr& getChild(ANTLR_UINT32 i); ANTLR_UINT32 getChildCount() const; ANTLR_UINT32 getType(); - TreeType* getFirstChildWithType(ANTLR_UINT32 type); - ANTLR_UINT32 getLine(); + TreeTypePtr& getFirstChildWithType(ANTLR_UINT32 type); + StringType getText(); bool isNilNode(); - void setChild(ANTLR_UINT32 i, TreeType* child); + void setChild(ANTLR_UINT32 i, TreeTypePtr child); StringType toStringTree(); StringType toString(); - void freshenPACIndexesAll(); - void freshenPACIndexes(ANTLR_UINT32 offset); - void reuse(); - ~CommonTree(); + void freshenParentAndChildIndexes(); + void freshenParentAndChildIndexes(ANTLR_UINT32 offset); + void freshenParentAndChildIndexesDeeply(); + void freshenParentAndChildIndexesDeeply(ANTLR_UINT32 offset); + // Prepare tree node to be re-used + void reuse(); + + UserDataType UserData; }; -ANTLR_END_NAMESPACE() +} #include "antlr3commontree.inl" diff --git a/runtime/Cpp/include/antlr3commontree.inl b/runtime/Cpp/include/antlr3commontree.inl old mode 100755 new mode 100644 index 8a3111bb7..a6f3f61f2 --- a/runtime/Cpp/include/antlr3commontree.inl +++ b/runtime/Cpp/include/antlr3commontree.inl @@ -1,92 +1,86 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template CommonTree::CommonTree() { - m_savedIndex = 0; - m_startIndex = 0; - m_stopIndex = 0; + m_startIndex = -1; + m_stopIndex = -1; + m_childIndex = -1; m_token = NULL; m_parent = NULL; - m_childIndex = 0; } template CommonTree::CommonTree( const CommonTree& ctree ) :m_children( ctree.m_children) + ,UserData(ctree.UserData) { - m_savedIndex = ctree.m_savedIndex; m_startIndex = ctree.m_startIndex; m_stopIndex = ctree.m_stopIndex; + m_childIndex = ctree.m_childIndex; m_token = ctree.m_token; m_parent = ctree.m_parent; - m_childIndex = ctree.m_childIndex; } template -CommonTree::CommonTree( CommonTokenType* token ) +CommonTree::CommonTree( const CommonTokenType* token ) { - m_savedIndex = 0; - m_startIndex = 0; - m_stopIndex = 0; + m_startIndex = -1; + m_stopIndex = -1; + m_childIndex = -1; m_token = token; m_parent = NULL; - m_childIndex = 0; } template -CommonTree::CommonTree( CommonTree* tree ) +CommonTree::CommonTree( const CommonTree* tree ) + :UserData(tree->UserData) { - m_savedIndex = 0; - m_startIndex = 0; - m_stopIndex = 0; + m_startIndex = tree->get_startIndex(); + m_stopIndex = tree->get_stopIndex(); + m_childIndex = -1; m_token = tree->get_token(); m_parent = NULL; - m_childIndex = 0; } template -typename CommonTree::TokenType* CommonTree::get_token() const +const typename CommonTree::CommonTokenType* CommonTree::get_token() const { return m_token; } template -typename CommonTree::ChildrenType& CommonTree::get_children() +void CommonTree::set_token(typename CommonTree::CommonTokenType const* token) { - return m_children; + m_token = token; } template -const typename CommonTree::ChildrenType& CommonTree::get_children() const +typename CommonTree::ChildrenType& CommonTree::get_children() { return m_children; } template -typename CommonTree::ChildrenType* CommonTree::get_children_p() +const typename CommonTree::ChildrenType& CommonTree::get_children() const { - return &m_children; + return m_children; } template -void CommonTree::addChild(TreeType* child) +void CommonTree::addChild(TreeTypePtr& child) { - ANTLR_UINT32 n; - ANTLR_UINT32 i; - if (child == NULL) return; ChildrenType& child_children = child->get_children(); - ChildrenType& tree_children = this->get_children(); + //ChildrenType& tree_children = this->get_children(); if (child->isNilNode() == true) { - if ( !child_children.empty() && child_children == tree_children ) + if ( !child_children.empty() && child_children == m_children ) { // TODO: Change to exception rather than ANTLR3_FPRINTF? - // fprintf(stderr, "ANTLR3: An attempt was made to add a child list to itself!\n"); return; } @@ -95,55 +89,46 @@ void CommonTree::addChild(TreeType* child) // if ( !child_children.empty() ) { - if (tree_children.empty()) + if (!m_children.empty()) { + // Need to copy(append) the children + for(auto i = child_children.begin(); i != child_children.end(); ++i) + { + // ANTLR3 lists can be sparse, unlike Array Lists (TODO: really?) + if ((*i) != NULL) + { + m_children.push_back(std::move(*i)); + // static_cast to possible subtype (if TreeType trait defined) + TreeType* tree = static_cast(this); + m_children.back()->set_parent(tree); + m_children.back()->set_childIndex(m_children.size() - 1); + } + } + } else { // We are build ing the tree structure here, so we need not // worry about duplication of pointers as the tree node // factory will only clean up each node once. So we just // copy in the child's children pointer as the child is // a nil node (has not root itself). // - tree_children.swap( child_children ); - this->freshenPACIndexesAll(); - } - else - { - // Need to copy the children - // - n = child_children.size(); - - for (i = 0; i < n; i++) - { - TreeType* entry; - entry = child_children[i]; - - // ANTLR3 lists can be sparse, unlike Array Lists - // - if (entry != NULL) - { - tree_children.push_back(entry); - } - } + m_children.swap( child_children ); + this->freshenParentAndChildIndexes(); } } } else { // Tree we are adding is not a Nil and might have children to copy - // - if (tree_children.empty()) - { - // No children in the tree we are adding to, so create a new list on - // the fly to hold them. - // - this->createChildrenList(); - } - tree_children.push_back( child ); + m_children.push_back( std::move(child) ); + // static_cast to possible subtype (if TreeType trait defined) + TreeType* tree = static_cast(this); + m_children.back()->set_parent(tree); + m_children.back()->set_childIndex(m_children.size() - 1); } } template -void CommonTree::addChildren(const ChildListType& kids) +void CommonTree::addChildren(const ChildListType& kids) { for( typename ChildListType::const_iterator iter = kids.begin(); iter != kids.end(); ++iter ) @@ -152,203 +137,137 @@ void CommonTree::addChildren(const ChildListType& kids) } } -//dummy one, as vector is always there -template -void CommonTree::createChildrenList() -{ -} - template -typename CommonTree::TreeType* CommonTree::deleteChild(ANTLR_UINT32 i) +typename CommonTree::TreeTypePtr CommonTree::deleteChild(ANTLR_UINT32 i) { if( m_children.empty() ) return NULL; - - return m_children.erase( m_children.begin() + i); + TreeTypePtr killed = m_children.erase( m_children.begin() + i); + this->freshenParentAndChildIndexes(i); + return killed; } template -void CommonTree::replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeType* newTree) +void CommonTree::replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr newTree) { - ANTLR_INT32 replacingHowMany; // How many nodes will go away - ANTLR_INT32 replacingWithHowMany; // How many nodes will replace them + ANTLR_INT32 numNewChildren; // Tracking variable ANTLR_INT32 delta; // Difference in new vs existing count - ANTLR_INT32 i; - ANTLR_INT32 j; - if ( m_children.empty() ) { - fprintf(stderr, "replaceChildren call: Indexes are invalid; no children in list for %s", this->getText().c_str() ); + fprintf(stderr, "replaceChildren call: Indexes are invalid; no children in list for %s", this->get_text().c_str() ); + // TODO throw here return; } + // How many nodes will go away + ANTLR_INT32 replacingHowMany = stopChildIndex - startChildIndex + 1; + ANTLR_INT32 replacingWithHowMany; // How many nodes will replace them // Either use the existing list of children in the supplied nil node, or build a vector of the // tree we were given if it is not a nil node, then we treat both situations exactly the same // - ChildrenType newChildren_temp; - ChildrenType* newChildren; // Iterator for whatever we are going to add in + ChildrenType newChildren; + ChildrenType &newChildrenRef(newChildren); if (newTree->isNilNode()) { - newChildren = newTree->get_children_p(); - } - else - { - newChildren = &newChildren_temp; - newChildren->push_back(newTree); + newChildrenRef = newTree->get_children(); + } else { + newChildrenRef.push_back(newTree); } // Initialize - // - replacingHowMany = stopChildIndex - startChildIndex + 1; - replacingWithHowMany = newChildren->size(); + replacingWithHowMany = newChildrenRef.size(); + numNewChildren = newChildrenRef.size(); delta = replacingHowMany - replacingWithHowMany; - numNewChildren = newChildren->size(); // If it is the same number of nodes, then do a direct replacement // if (delta == 0) { - TreeType* child; - - // Same number of nodes - // - j = 0; - for (i = startChildIndex; i <= stopChildIndex; i++) + ANTLR_INT32 j = 0; + for (ANTLR_INT32 i = startChildIndex; i <= stopChildIndex; i++) { - child = newChildren->at(j); - ChildrenType& parent_children = this->get_children(); - parent_children[i] = child; - child->setParent(this); - child->setChildIndex(i); + TreeType *child = newChildrenRef.at(j); + m_children[i] = child; + TreeType* tree = static_cast(this); + child->set_parent(tree); + child->set_childIndex(i); + j++; } } else if (delta > 0) { - ANTLR_UINT32 indexToDelete; - // Less nodes than there were before // reuse what we have then delete the rest - // - ChildrenType& parent_children = this->get_children(); - for (j = 0; j < numNewChildren; j++) + for (ANTLR_UINT32 j = 0; j < numNewChildren; j++) { - parent_children[ startChildIndex + j ] = newChildren->at(j); + m_children[ startChildIndex + j ] = newChildrenRef.at(j); } - // We just delete the same index position until done - // - indexToDelete = startChildIndex + numNewChildren; - - for (j = indexToDelete; j <= stopChildIndex; j++) + ANTLR_UINT32 indexToDelete = startChildIndex + numNewChildren; + for (ANTLR_UINT32 j = indexToDelete; j <= stopChildIndex; j++) { - parent_children.erase( parent_children.begin() + indexToDelete); + m_children.erase( m_children.begin() + indexToDelete); } - - this->freshenPACIndexes(startChildIndex); + this->freshenParentAndChildIndexes(startChildIndex); } else { - ChildrenType& parent_children = this->get_children(); - ANTLR_UINT32 numToInsert; - // More nodes than there were before // Use what we can, then start adding - // - for (j = 0; j < replacingHowMany; j++) + for (ANTLR_UINT32 j = 0; j < replacingHowMany; j++) { - parent_children[ startChildIndex + j ] = newChildren->at(j); + m_children[ startChildIndex + j ] = newChildrenRef.at(j); } - numToInsert = replacingWithHowMany - replacingHowMany; - - for (j = replacingHowMany; j < replacingWithHowMany; j++) + ANTLR_UINT32 numToInsert = replacingWithHowMany - replacingHowMany; + for (ANTLR_UINT32 j = replacingHowMany; j < replacingWithHowMany; j++) { - parent_children.push_back( newChildren->at(j) ); + m_children.push_back( newChildrenRef.at(j) ); } - this->freshenPACIndexes(startChildIndex); + this->freshenParentAndChildIndexes(startChildIndex); } } template CommonTree* CommonTree::dupNode() const { - // The node we are duplicating is in fact the common tree (that's why we are here) - // so we use the super pointer to duplicate. - // - TreeType* clone = new TreeType(); - - // The pointer we return is the base implementation of course - // - clone->set_token( m_token ); - return clone; + return new CommonTree(this); } template -typename CommonTree::TreeType* CommonTree::dupTree() +CommonTree* CommonTree::dupNode(void *p) const { - TreeType* newTree; - ANTLR_UINT32 i; - ANTLR_UINT32 s; - - newTree = this->dupNode(); - - if ( !m_children.empty() ) - { - s = m_children.size(); - - for (i = 0; i < s; i++) - { - TreeType* t; - TreeType* newNode; - - t = m_children[i]; - - if (t!= NULL) - { - newNode = t->dupTree(); - newTree->addChild(newNode); - } - } - } - - return newTree; + return new (p) CommonTree(this); } template -ANTLR_UINT32 CommonTree::getCharPositionInLine() +ANTLR_UINT32 CommonTree::get_charPositionInLine() const { - CommonTokenType* token; - token = m_token; - - if (token == NULL || (token->getCharPositionInLine() == -1) ) + if(m_token == NULL || (m_token->get_charPositionInLine() == 0) ) { - if (this->getChildCount() > 0) - { - TreeType* child; - - child = this->getChild(0); - - return child->getCharPositionInLine(); - } + if(m_children.empty()) + return 0; + if(m_children.front()) + return m_children.front()->get_charPositionInLine(); return 0; } - return token->getCharPositionInLine(); + return m_token->get_charPositionInLine(); } template -typename CommonTree::TreeType* CommonTree::getChild(ANTLR_UINT32 i) +typename CommonTree::TreeTypePtr& CommonTree::getChild(ANTLR_UINT32 i) { - if ( m_children.empty() - || i >= m_children.size() ) + static TreeTypePtr nul; + if ( m_children.empty() || i >= m_children.size() ) { - return NULL; + // TODO throw here should not happen + return nul; } - return m_children[i]; - + return m_children.at(i); } template @@ -381,26 +300,50 @@ void CommonTree::set_parent( TreeType* parent) m_parent = parent; } +template +ANTLR_MARKER CommonTree::get_startIndex() const +{ + if( m_startIndex==-1 && m_token!=NULL) + return m_token->get_tokenIndex(); + return m_startIndex; +} + +template +void CommonTree::set_startIndex( ANTLR_MARKER index) +{ + m_startIndex = index; +} + +template +ANTLR_MARKER CommonTree::get_stopIndex() const +{ + if( m_stopIndex==-1 && m_token!=NULL) + return m_token->get_tokenIndex(); + return m_stopIndex; +} + +template +void CommonTree::set_stopIndex( ANTLR_MARKER index) +{ + m_stopIndex = index; +} + template ANTLR_UINT32 CommonTree::getType() { - if (this == NULL) - { - return 0; - } + if (m_token == NULL) + return CommonTokenType::TOKEN_INVALID; else - { - return m_token->getType(); - } + return m_token->get_type(); } template -typename CommonTree::TreeType* CommonTree::getFirstChildWithType(ANTLR_UINT32 type) +typename CommonTree::TreeTypePtr& CommonTree::getFirstChildWithType(ANTLR_UINT32 type) { ANTLR_UINT32 i; std::size_t cs; - TreeType* t; + TreeTypePtr t; if ( !m_children.empty() ) { cs = m_children.size(); @@ -417,23 +360,17 @@ typename CommonTree::TreeType* CommonTree::getFirstChild } template -ANTLR_UINT32 CommonTree::getLine() +ANTLR_UINT32 CommonTree::get_line() const { - TreeType* cTree = this; - CommonTokenType* token; - token = cTree->get_token(); - - if (token == NULL || token->getLine() == 0) + if(m_token == NULL || m_token->get_line() == 0) { - if ( this->getChildCount() > 0) - { - TreeType* child; - child = this->getChild(0); - return child->getLine(); - } + if ( m_children.empty()) + return 0; + if ( m_children.front()) + return m_children.front()->get_line(); return 0; } - return token->getLine(); + return m_token->get_line(); } template @@ -443,118 +380,137 @@ typename CommonTree::StringType CommonTree::getText() } template -bool CommonTree::isNilNode() +bool CommonTree::isNilNode() { // This is a Nil tree if it has no payload (Token in our case) - // if(m_token == NULL) - { return true; - } else - { return false; - } } template -void CommonTree::setChild(ANTLR_UINT32 i, TreeType* child) +void CommonTree::setChild(ANTLR_UINT32 i, TreeTypePtr child) { - if( m_children.size() >= i ) + if( child==NULL) + return; + + if( child->isNilNode()) + { + // TODO: throw IllegalArgumentException + return; + } + + if( m_children.size() <= i ) m_children.resize(i+1); + m_children[i] = child; + TreeType* tree = static_cast(this); + child->set_parent(tree); + child->set_childIndex(i); } template typename CommonTree::StringType CommonTree::toStringTree() { - StringType string; - ANTLR_UINT32 i; - ANTLR_UINT32 n; - TreeType* t; + StringType retval; if( m_children.empty() ) - { return this->toString(); - } /* Need a new string with nothing at all in it. */ - if (this->isNilNode() == false) + if(this->isNilNode() == false) { - string.append("("); - string.append(this->toString()); - string.append(" "); + retval.append("("); + retval.append(this->toString()); + retval.append(" "); } - if ( m_children != NULL) - { - n = m_children.size(); - for (i = 0; i < n; i++) - { - t = m_children[i]; - - if (i > 0) - { - string.append(" "); - } - string.append(t->toStringTree()); + if ( !m_children.empty()) + { + retval.append( m_children.front()->toStringTree()); + for (auto i = std::next(m_children.begin()); i != m_children.end(); ++i) + { + retval.append(" "); + retval.append((*i)->toStringTree()); } } + if (this->isNilNode() == false) { - string.append(")"); + retval.append(")"); } - - return string; + return retval; } template typename CommonTree::StringType CommonTree::toString() { - if (this->isNilNode() ) - { - StringType nilNode; - - nilNode = "nil"; - - return nilNode; - } - - return m_token->getText(); + if( this->isNilNode()) + return StringType("nil"); + return m_token->toString(); } template -void CommonTree::freshenPACIndexesAll() +void CommonTree::freshenParentAndChildIndexes() { - this->freshenPACIndexes(0); + this->freshenParentAndChildIndexes(0); } template -void CommonTree::freshenPACIndexes(ANTLR_UINT32 offset) +void CommonTree::freshenParentAndChildIndexes(ANTLR_UINT32 offset) { - ANTLR_UINT32 count; - ANTLR_UINT32 c; - - count = this->getChildCount(); // How many children do we have - +// ANTLR_UINT32 count = this->getChildCount(); // Loop from the supplied index and set the indexes and parent - // - for (c = offset; c < count; c++) +// for (ANTLR_UINT32 c = offset; c < count; c++) +// { +// TreeTypePtr child = this->getChild(c); +// child->set_childIndex(c); +// child->set_parent(this); +// } + // Loop from the supplied index and set the indexes and parent + auto i = m_children.begin(); + int c = offset; + if(offset) + std::advance( i, offset ); + for(; i != m_children.end(); ++i, ++c) { - TreeType* child; + (*i)->set_childIndex(c); + TreeType* tree = static_cast(this); + (*i)->set_parent(tree); + } +} - child = this->getChild(c); +template +void CommonTree::freshenParentAndChildIndexesDeeply() +{ + this->freshenParentAndChildIndexes(0); +} - child->setChildIndex(c); - child->setParent(this); +template +void CommonTree::freshenParentAndChildIndexesDeeply(ANTLR_UINT32 offset) +{ + ANTLR_UINT32 count = this->getChildCount(); + for (ANTLR_UINT32 c = offset; c < count; c++) { + TreeTypePtr child = getChild(c); + child->set_childIndex(c); + child->set_parent(this); + child->freshenParentAndChildIndexesDeeply(); } } template -void CommonTree::reuse() +void CommonTree::reuse() { - delete this; //memory re-use should be taken by the library user + m_startIndex = -1; + m_stopIndex = -1; + m_childIndex = -1; + m_token = NULL; + m_parent = NULL; + + ChildrenType empty; + m_children.swap(empty); } template @@ -562,4 +518,4 @@ CommonTree::~CommonTree() { } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3commontreeadaptor.hpp b/runtime/Cpp/include/antlr3commontreeadaptor.hpp old mode 100755 new mode 100644 index 8b40f1c6d..c32968ac9 --- a/runtime/Cpp/include/antlr3commontreeadaptor.hpp +++ b/runtime/Cpp/include/antlr3commontreeadaptor.hpp @@ -33,69 +33,148 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" +namespace antlr3 { -ANTLR_BEGIN_NAMESPACE() +template class CommonTreeStore; + +/** Helper class for unique_ptr. Implements deleter for instances of unique_ptr + While building AST tree dangling pointers are automatically put back into pool + */ +template +class CommonResourcePoolManager +{ +public: + typedef typename ImplTraits::TreeType TreeType; + CommonResourcePoolManager(CommonTreeStore * pool); + CommonResourcePoolManager(); + + ~CommonResourcePoolManager(); + + void operator()(TreeType* releasedResource) const; +private: + CommonTreeStore * m_pool; +}; + +template +class CommonTreeStore +{ +public: + typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::CommonTokenType CommonTokenType; + typedef CommonResourcePoolManager ResourcePoolManagerType; + typedef std::unique_ptr > TreeTypePtr; + //typedef typename ImplTraits::TreeTypePtr TreeTypePtr; + + CommonTreeStore(); + + TreeTypePtr create(); + CommonTokenType* createToken(); + CommonTokenType* createToken(const CommonTokenType* fromToken); + + // Return special kind of NULL pointer wrapped into TreeTypePtr + TreeTypePtr + null() + { + return TreeTypePtr(NULL, m_manager); + } + + std::size_t size() const + { + return m_treeStore.size(); + } + + template + bool contains(const std::vector &vec, const T &value) + { + return std::find(vec.begin(), vec.end(), value) != vec.end(); + } + +protected: + template friend class CommonResourcePoolManager; + template friend class CommonTreeAdaptor; + + void reuse(TreeType* releasedResource); + + std::vector m_recycleBin; + std::vector > m_treeStore; + std::vector > m_tokenStore; + ResourcePoolManagerType m_manager; +}; template -class CommonTreeAdaptor : public ImplTraits::AllocPolicyType +class CommonTreeAdaptor + : public ImplTraits::AllocPolicyType + , public CommonTreeStore { public: typedef typename ImplTraits::StringType StringType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; + typedef typename TreeType::ChildrenType ChildrenType; + typedef TreeType TokenType; typedef typename ImplTraits::CommonTokenType CommonTokenType; typedef typename ImplTraits::DebugEventListenerType DebuggerType; + typedef typename ImplTraits::TokenStreamType TokenStreamType; + typedef CommonTreeStore TreeStoreType; public: //The parameter is there only to provide uniform constructor interface - CommonTreeAdaptor(DebuggerType* dbg = NULL); - TreeType* nilNode(); - TreeType* dupTree( TreeType* tree); - TreeType* dupTreeTT( TreeType* t, TreeType* tree); - - void addChild( TreeType* t, TreeType* child); - void addChildToken( TreeType* t, CommonTokenType* child); - void setParent( TreeType* child, TreeType* parent); - TreeType* getParent( TreeType* child); - - TreeType* errorNode( CommonTokenType* tnstream, CommonTokenType* startToken, CommonTokenType* stopToken); - bool isNilNode( TreeType* t); - - TreeType* becomeRoot( TreeType* newRoot, TreeType* oldRoot); - TreeType* rulePostProcessing( TreeType* root); - - TreeType* becomeRootToken(CommonTokenType* newRoot, TreeType* oldRoot); - - TreeType* create( CommonTokenType* payload); - TreeType* createTypeToken( ANTLR_UINT32 tokenType, CommonTokenType* fromToken); - TreeType* createTypeTokenText ( ANTLR_UINT32 tokenType, CommonTokenType* fromToken, const ANTLR_UINT8* text); - TreeType* createTypeText ( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text); - - TreeType* dupNode( TreeType* treeNode); - ANTLR_UINT32 getType( TreeType* t); - StringType getText( TreeType* t); + CommonTreeAdaptor(DebuggerType* dbg = nullptr); + + TreeTypePtr nilNode(); + TreeTypePtr dupTree( const TreeTypePtr& tree); + TreeTypePtr dupTree( const TreeType* tree); + + TreeTypePtr dupNode(const TreeTypePtr& treeNode); + TreeTypePtr dupNode(const TreeType* treeNode); + + void addChild( TreeTypePtr& t, TreeTypePtr& child); + void addChild( TreeTypePtr& t, TreeTypePtr&& child); + void addChildToken( TreeTypePtr& t, CommonTokenType* child); + void setParent( TreeTypePtr& child, TreeType* parent); + TreeType* getParent( TreeTypePtr& child); + + TreeTypePtr errorNode( CommonTokenType* tnstream, const CommonTokenType* startToken, const CommonTokenType* stopToken); + bool isNilNode( TreeTypePtr& t); + + TreeTypePtr becomeRoot( TreeTypePtr& newRoot, TreeTypePtr& oldRoot); + TreeTypePtr becomeRoot( TreeTypePtr&& newRoot, TreeTypePtr& oldRoot); + TreeTypePtr becomeRootToken(CommonTokenType* newRoot, TreeTypePtr& oldRoot); + TreeTypePtr rulePostProcessing( TreeTypePtr& root); + + TreeTypePtr create( CommonTokenType const* payload); + TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken); + TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, const char* text); + TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, StringType const& text); + TreeTypePtr create( ANTLR_UINT32 tokenType, const char* text); + TreeTypePtr create( ANTLR_UINT32 tokenType, StringType const& text); + + CommonTokenType* createToken( ANTLR_UINT32 tokenType, const char* text); + CommonTokenType* createToken( ANTLR_UINT32 tokenType, StringType const& text); + CommonTokenType* createToken( const CommonTokenType* fromToken); + + ANTLR_UINT32 getType( TreeTypePtr& t); + StringType getText( TreeTypePtr& t); - TreeType* getChild( TreeType* t, ANTLR_UINT32 i); - void setChild( TreeType* t, ANTLR_UINT32 i, TreeType* child); - void deleteChild( TreeType* t, ANTLR_UINT32 i); - void setChildIndex( TreeType* t, ANTLR_INT32 i); - ANTLR_INT32 getChildIndex( TreeType* t); + TreeTypePtr& getChild( TreeTypePtr& t, ANTLR_UINT32 i); + void setChild( TreeTypePtr& t, ANTLR_UINT32 i, TreeTypePtr& child); + void deleteChild( TreeTypePtr& t, ANTLR_UINT32 i); + void setChildIndex( TreeTypePtr& t, ANTLR_INT32 i); + ANTLR_INT32 getChildIndex( TreeTypePtr& t); + + ANTLR_UINT32 getChildCount( TreeTypePtr&); + ANTLR_UINT64 getUniqueID( TreeTypePtr&); - ANTLR_UINT32 getChildCount( TreeType*); - ANTLR_UINT64 getUniqueID( TreeType*); + CommonTokenType* getToken( TreeTypePtr& t); - CommonTokenType* createToken( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text); - CommonTokenType* createTokenFromToken( CommonTokenType* fromToken); - CommonTokenType* getToken( TreeType* t); - - void setTokenBoundaries( TreeType* t, CommonTokenType* startToken, CommonTokenType* stopToken); - ANTLR_MARKER getTokenStartIndex( TreeType* t); - ANTLR_MARKER getTokenStopIndex( TreeType* t); + void setTokenBoundaries( TreeTypePtr& t, const CommonTokenType* startToken, const CommonTokenType* stopToken); + ANTLR_MARKER getTokenStartIndex( TreeTypePtr& t); + ANTLR_MARKER getTokenStopIndex( TreeTypePtr& t); /// Produce a DOT (see graphviz freeware suite) from a base tree /// - StringType makeDot( TreeType* theTree); + StringType makeDot( TreeTypePtr& theTree); /// Replace from start to stop child index of parent with t, which might /// be a list. Number of children may be different @@ -104,14 +183,16 @@ class CommonTreeAdaptor : public ImplTraits::AllocPolicyType /// If parent is null, don't do anything; must be at root of overall tree. /// Can't replace whatever points to the parent externally. Do nothing. /// - void replaceChildren( TreeType* parent, ANTLR_INT32 startChildIndex, - ANTLR_INT32 stopChildIndex, TreeType* t); + void replaceChildren( TreeTypePtr parent, ANTLR_INT32 startChildIndex, + ANTLR_INT32 stopChildIndex, TreeTypePtr t); - ~CommonTreeAdaptor(); + ~CommonTreeAdaptor(); protected: - void defineDotNodes(TreeType* t, const StringType& dotSpec); - void defineDotEdges(TreeType* t, const StringType& dotSpec); + TreeTypePtr dupTreeImpl( const TreeType* root, TreeType* parent); + + void defineDotNodes(TreeTypePtr t, const StringType& dotSpec); + void defineDotEdges(TreeTypePtr t, const StringType& dotSpec); }; //If someone can override the CommonTreeAdaptor at the compile time, that will be @@ -125,7 +206,9 @@ class DebugTreeAdaptor : public ImplTraits::CommonTreeAdaptorType //the template parameter is required for pointing back at the adaptor typedef typename ImplTraits::DebugEventListener DebuggerType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef typename ImplTraits::CommonTokenType CommonTokenType; + typedef typename ImplTraits::CommonTreeAdaptorType super; private: /// If set to something other than NULL, then this structure is @@ -139,24 +222,26 @@ class DebugTreeAdaptor : public ImplTraits::CommonTreeAdaptorType public: DebugTreeAdaptor( DebuggerType* debugger ); void setDebugEventListener( DebuggerType* debugger); - TreeType* nilNode(); - void addChild(TreeType* t, TreeType* child); - void addChildToken(TreeType* t, CommonTokenType* child); - TreeType* becomeRoot( TreeType* newRootTree, TreeType* oldRootTree ); - TreeType* becomeRootToken(TreeType* newRoot, TreeType* oldRoot); - TreeType* createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken); - TreeType* createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text); - TreeType* createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text); - TreeType* dupTree( TreeType* tree); + TreeTypePtr nilNode(); + void addChild(TreeTypePtr& t, TreeTypePtr& child); + void addChildToken(TreeTypePtr& t, CommonTokenType* child); + TreeTypePtr becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree ); + TreeTypePtr becomeRootToken( CommonTokenType* newRoot, TreeTypePtr& oldRoot); + + TreeTypePtr createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken); + TreeTypePtr createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text); + TreeTypePtr createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text); + + TreeTypePtr dupTree( const TreeTypePtr& tree); + TreeTypePtr dupTree( const TreeType* tree); /// Sends the required debugging events for duplicating a tree /// to the debugger. /// - void simulateTreeConstruction(TreeType* tree); + void simulateTreeConstruction(TreeTypePtr& tree); }; - -ANTLR_END_NAMESPACE() +} #include "antlr3commontreeadaptor.inl" diff --git a/runtime/Cpp/include/antlr3commontreeadaptor.inl b/runtime/Cpp/include/antlr3commontreeadaptor.inl old mode 100755 new mode 100644 index 698c7d73e..871b547c1 --- a/runtime/Cpp/include/antlr3commontreeadaptor.inl +++ b/runtime/Cpp/include/antlr3commontreeadaptor.inl @@ -1,53 +1,139 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { -template -ANTLR_INLINE CommonTreeAdaptor::CommonTreeAdaptor(DebuggerType*) +template +CommonResourcePoolManager::CommonResourcePoolManager(CommonTreeStore * pool) + : m_pool(pool) +{} + +template +CommonResourcePoolManager::CommonResourcePoolManager() + : m_pool(NULL) +{} + +template +CommonResourcePoolManager::~CommonResourcePoolManager() +{}; + +template +void +CommonResourcePoolManager::operator()(TreeType* releasedResource) const +{ + if (releasedResource && m_pool) + m_pool->reuse(releasedResource); +} + +template +CommonTreeStore::CommonTreeStore() + : m_manager(this) +{} + +template +typename CommonTreeStore::TreeTypePtr +CommonTreeStore::create() +{ + if (m_recycleBin.empty()) + { + TreeTypePtr retval = TreeTypePtr(new TreeType, m_manager); + m_treeStore.push_back(std::unique_ptr(retval.get())); + return retval; + } else { + TreeType* resource = m_recycleBin.back(); + m_recycleBin.pop_back(); + return TreeTypePtr(resource, m_manager); + } +} + +template +typename CommonTreeStore::CommonTokenType* +CommonTreeStore::createToken() +{ + CommonTokenType* retval = new CommonTokenType; + m_tokenStore.push_back(std::unique_ptr(retval)); + return retval; +} + +template +typename CommonTreeStore::CommonTokenType* +CommonTreeStore::createToken( const CommonTokenType* fromToken) { + CommonTokenType* retval = new CommonTokenType(*fromToken); + m_tokenStore.push_back(std::unique_ptr(retval)); + return retval; +} + +template +void +CommonTreeStore::reuse(TreeType* releasedResource) +{ + if (contains(m_recycleBin, releasedResource)) + { + throw std::string("Grrr double reuse"); + } + releasedResource->reuse(); + m_recycleBin.push_back(releasedResource); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::nilNode() +CommonTreeAdaptor::CommonTreeAdaptor(DebuggerType*) +{} + +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::nilNode() { return this->create(NULL); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::dupTree( TreeType* tree) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::dupTree( const TreeType* tree) { - return this->dupTreeTT(tree, NULL); + if (tree == NULL) + return NULL; + return std::move(this->dupTreeImpl(tree, NULL)); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::dupTreeTT( TreeType* t, TreeType* parent) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::dupTree( const TreeTypePtr& tree) { - TreeType* newTree; - TreeType* child; - TreeType* newSubTree; - ANTLR_UINT32 n; - ANTLR_UINT32 i; - - if (t == NULL) + if (tree == NULL) return NULL; + return std::move(dupTreeImpl(tree.get(), NULL)); +} - newTree = t->dupNode(); +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::dupTreeImpl( const TreeType *root, TreeType* parent) +{ + TreeTypePtr newTree(dupNode(root)); // Ensure new subtree root has parent/child index set // - this->setChildIndex( newTree, t->getChildIndex() ); + this->setChildIndex( newTree, root->get_childIndex() ); this->setParent(newTree, parent); - n = this->getChildCount(t); - for (i=0; i < n; i++) + ChildrenType const& r_children = root->get_children(); + for (auto i = r_children.begin(); i != r_children.end(); ++i) { - child = this->getChild(t, i); - newSubTree = this->dupTreeTT(child, t); - this->addChild(newTree, newSubTree); + // add child's clone + this->addChild(newTree, dupTreeImpl(i->get(), newTree.get())); } + return newTree; } template -void CommonTreeAdaptor::addChild( TreeType* t, TreeType* child) +void CommonTreeAdaptor::addChild( TreeTypePtr& t, TreeTypePtr& child) +{ + if (t != NULL && child != NULL) + { + t->addChild(child); + } +} + +template +void CommonTreeAdaptor::addChild( TreeTypePtr& t, TreeTypePtr&& child) { if (t != NULL && child != NULL) { @@ -56,7 +142,7 @@ void CommonTreeAdaptor::addChild( TreeType* t, TreeType* child) } template -void CommonTreeAdaptor::addChildToken( TreeType* t, CommonTokenType* child) +void CommonTreeAdaptor::addChildToken( TreeTypePtr& t, CommonTokenType* child) { if (t != NULL && child != NULL) { @@ -65,47 +151,52 @@ void CommonTreeAdaptor::addChildToken( TreeType* t, CommonTokenType* } template -void CommonTreeAdaptor::setParent( TreeType* child, TreeType* parent) +void CommonTreeAdaptor::setParent( TreeTypePtr& child, TreeType* parent) { - child->setParent(parent); + child->set_parent(parent); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::getParent( TreeType* child) +typename CommonTreeAdaptor::TreeType* +CommonTreeAdaptor::getParent( TreeTypePtr& child) { + if ( child==NULL ) + return NULL; return child->getParent(); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::errorNode( CommonTokenType* tnstream, CommonTokenType* startToken, CommonTokenType* stopToken) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::errorNode( CommonTokenType*, + const CommonTokenType*, + const CommonTokenType*) { // Use the supplied common tree node stream to get another tree from the factory // TODO: Look at creating the erronode as in Java, but this is complicated by the // need to track and free the memory allocated to it, so for now, we just // want something in the tree that isn't a NULL pointer. // - return this->createTypeText( CommonTokenType::TOKEN_INVALID, "Tree Error Node"); + return this->create( CommonTokenType::TOKEN_INVALID, "Tree Error Node"); } template -bool CommonTreeAdaptor::isNilNode( TreeType* t) +bool CommonTreeAdaptor::isNilNode( TreeTypePtr& t) { return t->isNilNode(); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::becomeRoot( TreeType* newRootTree, TreeType* oldRootTree) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree) { - TreeType* saveRoot; - /* Protect against tree rewrites if we are in some sort of error * state, but have tried to recover. In C we can end up with a null pointer * for a tree that was not produced. */ if (newRootTree == NULL) { - return oldRootTree; + return std::move(oldRootTree); } /* root is just the new tree as is if there is no @@ -113,7 +204,7 @@ typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::TreeType* CommonTreeAdaptorgetChild(0); + TreeTypePtr saveRoot = std::move(newRootTree); + newRootTree = std::move(saveRoot->getChild(0)); - // Reclaim the old nilNode() - // - saveRoot->reuse(); + // Will Reclaim the old nilNode() saveRoot here } /* Add old root into new root. addChild takes care of the case where oldRoot @@ -145,248 +234,297 @@ typename CommonTreeAdaptor::TreeType* CommonTreeAdaptoraddChild(oldRootTree); - // If the oldroot tree was a nil node, then we know at this point - // it has become orphaned by the rewrite logic, so we tell it to do - // whatever it needs to do to be reused. - // - if (oldRootTree->isNilNode()) - { - // We have taken an old Root Tree and appended all its children to the new - // root. In addition though it was a nil node, which means the generated code - // will not reuse it again, so we will reclaim it here. First we want to zero out - // any pointers it was carrying around. We are just the baseTree handler so we - // don't know necessarilly know how to do this for the real node, we just ask the tree itself - // to do it. - // - oldRootTree->reuse(); - } /* Always returns new root structure */ - return newRootTree; -} - -template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::becomeRootToken(CommonTokenType* newRoot, TreeType* oldRoot) -{ - return this->becomeRoot(this->create(newRoot), oldRoot); + return std::move(newRootTree); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::create( CommonTokenType* payload) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::becomeRoot( TreeTypePtr&& newRootTree, TreeTypePtr& oldRootTree) { - return new TreeType(payload); -} + /* Protect against tree rewrites if we are in some sort of error + * state, but have tried to recover. In C we can end up with a null pointer + * for a tree that was not produced. + */ + if (newRootTree == NULL) + { + return std::move(oldRootTree); + } -template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::createTypeToken( ANTLR_UINT32 tokenType, - CommonTokenType* fromToken) -{ - /* Create the new token + /* root is just the new tree as is if there is no + * current root tree. */ - fromToken = this->createTokenFromToken(fromToken); + if (oldRootTree == NULL) + { + return std::move(newRootTree); + } - /* Set the type of the new token to that supplied + /* Produce ^(nil real-node) */ - fromToken->setType(tokenType); + if (newRootTree->isNilNode()) + { + if (newRootTree->getChildCount() > 1) + { + /* TODO: Handle tree exceptions + */ + fprintf(stderr, "More than one node as root! TODO: Create tree exception handling\n"); + return std::move(newRootTree); + } + + /* The new root is the first child, keep track of the original newRoot + * because if it was a Nil Node, then we can reuse it now. + */ + TreeTypePtr saveRoot = std::move(newRootTree); + newRootTree = std::move(saveRoot->getChild(0)); + + // will Reclaim the old nilNode() here saveRoot. + } - /* Return a new node based upon this token + /* Add old root into new root. addChild takes care of the case where oldRoot + * is a flat list (nill rooted tree). All children of oldroot are added to + * new root. */ - return this->create(fromToken); + newRootTree->addChild(oldRootTree); + /* Always returns new root structure + */ + return std::move(newRootTree); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::createTypeTokenText( ANTLR_UINT32 tokenType, CommonTokenType* fromToken, const ANTLR_UINT8* text) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::becomeRootToken( CommonTokenType* newRoot, TreeTypePtr& oldRoot) { - /* Create the new token - */ - fromToken = this->createTokenFromToken(fromToken); + return this->becomeRoot(this->create(newRoot), oldRoot); +} - /* Set the type of the new token to that supplied - */ - fromToken->setType(tokenType); +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( CommonTokenType const* payload) +{ + TreeTypePtr retval = TreeStoreType::create(); + retval->set_token(payload); + return retval; +} - /* Set the text of the token accordingly - */ - fromToken->setText(text); +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken) +{ + /* Create the new token */ + auto newToken = this->createToken(fromToken); + /* Set the type of the new token to that supplied */ + newToken->set_type(tokenType); + /* Return a new node based upon this token */ + return this->create(newToken); +} - /* Return a new node based upon this token - */ - return this->create(fromToken); +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, const char* text) +{ + if (fromToken == NULL) + return create(tokenType, text); + /* Create the new token */ + auto newToken = this->createToken(fromToken); + /* Set the type of the new token to that supplied */ + newToken->set_type(tokenType); + /* Set the text of the token accordingly */ + newToken->setText(text); + /* Return a new node based upon this token */ + return this->create(newToken); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::createTypeText( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, typename CommonTreeAdaptor::StringType const& text) { - CommonTokenType* fromToken; + if (fromToken == NULL) + return create(tokenType, text); + /* Create the new token */ + auto newToken = this->createToken(fromToken); + /* Set the type of the new token to that supplied */ + newToken->set_type(tokenType); + /* Set the text of the token accordingly */ + newToken->set_tokText(text); + /* Return a new node based upon this token */ + return this->create(newToken); +} - /* Create the new token - */ - fromToken = this->createToken(tokenType, text); +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( ANTLR_UINT32 tokenType, const char* text) +{ + auto fromToken = this->createToken(tokenType, text); + return this->create(fromToken); +} - /* Return a new node based upon this token - */ +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::create( ANTLR_UINT32 tokenType, CommonTreeAdaptor::StringType const& text) +{ + auto fromToken = this->createToken(tokenType, text); return this->create(fromToken); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::dupNode( TreeType* treeNode) +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::dupNode(const TreeType* treeNode) +{ + if (treeNode == NULL) + return TreeStoreType::null(); + TreeTypePtr retval(TreeStoreType::create()); + treeNode->dupNode(retval.get()); + return retval; +} + +template +typename CommonTreeAdaptor::TreeTypePtr +CommonTreeAdaptor::dupNode(const TreeTypePtr& treeNode) { - return (treeNode == NULL) ? NULL : treeNode->dupNode(); + if (treeNode == NULL) + return TreeStoreType::null(); + TreeTypePtr retval(TreeStoreType::create()); + treeNode->dupNode(retval.get()); + return retval; } template -ANTLR_UINT32 CommonTreeAdaptor::getType( TreeType* t) +ANTLR_UINT32 CommonTreeAdaptor::getType( TreeTypePtr& t) { + if ( t==NULL) + return CommonTokenType::TOKEN_INVALID; return t->getType(); } template -typename CommonTreeAdaptor::StringType CommonTreeAdaptor::getText( TreeType* t) +typename CommonTreeAdaptor::StringType CommonTreeAdaptor::getText( TreeTypePtr& t) { return t->getText(); } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::getChild( TreeType* t, ANTLR_UINT32 i) +typename CommonTreeAdaptor::TreeTypePtr& CommonTreeAdaptor::getChild( TreeTypePtr& t, ANTLR_UINT32 i) { + if ( t==NULL ) + return NULL; return t->getChild(i); } template -void CommonTreeAdaptor::setChild( TreeType* t, ANTLR_UINT32 i, TreeType* child) +void CommonTreeAdaptor::setChild( TreeTypePtr& t, ANTLR_UINT32 i, TreeTypePtr& child) { t->setChild(i, child); } template -void CommonTreeAdaptor::deleteChild( TreeType* t, ANTLR_UINT32 i) +void CommonTreeAdaptor::deleteChild( TreeTypePtr& t, ANTLR_UINT32 i) { t->deleteChild(i); } template -void CommonTreeAdaptor::setChildIndex( TreeType* t, ANTLR_INT32 i) +void CommonTreeAdaptor::setChildIndex( TreeTypePtr& t, ANTLR_INT32 i) { - t->setChildIndex(i); + if( t!= NULL) + t->set_childIndex(i); } template -ANTLR_INT32 CommonTreeAdaptor::getChildIndex( TreeType * t) +ANTLR_INT32 CommonTreeAdaptor::getChildIndex( TreeTypePtr& t) { + if ( t==NULL ) + return 0; return t->getChildIndex(); } template -ANTLR_UINT32 CommonTreeAdaptor::getChildCount( TreeType* t) +ANTLR_UINT32 CommonTreeAdaptor::getChildCount( TreeTypePtr& t) { + if ( t==NULL ) + return 0; return t->getChildCount(); } template -ANTLR_UINT64 CommonTreeAdaptor::getUniqueID( TreeType* node ) +ANTLR_UINT64 CommonTreeAdaptor::getUniqueID( TreeTypePtr& node ) { return reinterpret_cast(node); } template -typename CommonTreeAdaptor::CommonTokenType* - CommonTreeAdaptor::createToken( ANTLR_UINT32 tokenType, const ANTLR_UINT8* text) +typename CommonTreeAdaptor::CommonTokenType* +CommonTreeAdaptor::createToken( ANTLR_UINT32 tokenType, const char* text) { - CommonTokenType* newToken = new CommonTokenType; - - if (newToken != NULL) - { - newToken->set_tokText( (const char*) text ); - newToken->setType(tokenType); - } - return newToken; - + CommonTokenType* newToken = TreeStoreType::createToken(); + newToken->set_tokText( text ); + newToken->set_type(tokenType); + return newToken; } template -typename CommonTreeAdaptor::CommonTokenType* - CommonTreeAdaptor::createTokenFromToken( CommonTokenType* fromToken) +typename CommonTreeAdaptor::CommonTokenType* +CommonTreeAdaptor::createToken( ANTLR_UINT32 tokenType, CommonTreeAdaptor::StringType const& text) { - CommonTokenType* newToken; - - newToken = new CommonTokenType; - - if (newToken != NULL) - { - // Create the text using our own string factory to avoid complicating - // commontoken. - // - StringType text = fromToken->getText(); - newToken->set_tokText( text ); - newToken->setLine( fromToken->getLine() ); - newToken->setTokenIndex( fromToken->getTokenIndex() ); - newToken->setCharPositionInLine( fromToken->getCharPositionInLine() ); - newToken->setChannel( fromToken->getChannel() ); - newToken->setType( fromToken->getType() ); - } + CommonTokenType* newToken = TreeStoreType::createToken(); + newToken->set_tokText( text ); + newToken->set_type(tokenType); + return newToken; +} - return newToken; +template +typename CommonTreeAdaptor::CommonTokenType* +CommonTreeAdaptor::createToken( const CommonTokenType* fromToken) +{ + CommonTokenType* newToken = TreeStoreType::createToken(fromToken); + return newToken; } template typename CommonTreeAdaptor::CommonTokenType* - CommonTreeAdaptor::getToken( TreeType* t) +CommonTreeAdaptor::getToken( TreeTypePtr& t) { return t->getToken(); } template -void CommonTreeAdaptor::setTokenBoundaries( TreeType* t, CommonTokenType* startToken, CommonTokenType* stopToken) +void CommonTreeAdaptor::setTokenBoundaries( TreeTypePtr& t, const CommonTokenType* startToken, const CommonTokenType* stopToken) { - ANTLR_MARKER start; - ANTLR_MARKER stop; - - TreeType* ct; + ANTLR_MARKER start = 0; + ANTLR_MARKER stop = 0; if (t == NULL) - { return; - } if ( startToken != NULL) - { - start = startToken->getTokenIndex(); - } - else - { - start = 0; - } + start = startToken->get_tokenIndex(); if ( stopToken != NULL) - { - stop = stopToken->getTokenIndex(); - } - else - { - stop = 0; - } + stop = stopToken->get_tokenIndex(); - ct = t; - - ct->set_startIndex(start); - ct->set_stopIndex(stop); + t->set_startIndex(start); + t->set_stopIndex(stop); } template -ANTLR_MARKER CommonTreeAdaptor::getTokenStartIndex( TreeType* t) +ANTLR_MARKER CommonTreeAdaptor::getTokenStartIndex( TreeTypePtr& t) { + if ( t==NULL ) + return -1; return t->get_tokenStartIndex(); } template -ANTLR_MARKER CommonTreeAdaptor::getTokenStopIndex( TreeType* t) +ANTLR_MARKER CommonTreeAdaptor::getTokenStopIndex( TreeTypePtr& t) { + if ( t==NULL ) + return -1; return t->get_tokenStopIndex(); } template -typename CommonTreeAdaptor::StringType CommonTreeAdaptor::makeDot( TreeType* theTree) +typename CommonTreeAdaptor::StringType CommonTreeAdaptor::makeDot( TreeTypePtr& theTree) { // The string we are building up // @@ -451,7 +589,7 @@ typename CommonTreeAdaptor::StringType CommonTreeAdaptor -void CommonTreeAdaptor::replaceChildren( TreeType* parent, ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeType* t) +void CommonTreeAdaptor::replaceChildren( TreeTypePtr parent, ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr t) { if (parent != NULL) parent->replaceChildren(startChildIndex, stopChildIndex, t); @@ -460,19 +598,41 @@ void CommonTreeAdaptor::replaceChildren( TreeType* parent, ANTLR_INT template CommonTreeAdaptor::~CommonTreeAdaptor() { +#ifdef ANTLR3_DEBUG + std::cout << "SZ" << TreeStoreType::size() << std::endl; + std::cout << "RZ" << TreeStoreType::m_recycleBin.size() << std::endl; + + auto i = TreeStoreType::m_treeStore.begin(); + + std::cout + << ' ' + << "Node " << '\t' << "Parent " << '\t' << "Type" << '\t' << "toStringTree" << std::endl; + + for(; i != TreeStoreType::m_treeStore.end(); ++i) + { + std::cout + << (TreeStoreType::contains(TreeStoreType::m_recycleBin, i->get()) ? '*' : ' ') + << i->get() << '\t' + << (const void *) (*i)->get_parent() << '\t' + << (*i)->getType() << '\t' + << (*i)->getChildCount() << '\t' + << (*i)->toStringTree() << '\t' + << std::endl; + } +#endif } template -void CommonTreeAdaptor::defineDotNodes(TreeType* t, const StringType& dotSpec) +void CommonTreeAdaptor::defineDotNodes(TreeTypePtr t, const StringType& dotSpec) { // How many nodes are we talking about? // int nCount; int i; - TreeType* child; + TreeTypePtr child; char buff[64]; StringType text; - int j; + int j; // Count the nodes // @@ -535,7 +695,7 @@ void CommonTreeAdaptor::defineDotNodes(TreeType* t, const StringType } template -void CommonTreeAdaptor::defineDotEdges(TreeType* t, const StringType& dotSpec) +void CommonTreeAdaptor::defineDotEdges(TreeTypePtr t, const StringType& dotSpec) { // How many nodes are we talking about? // @@ -564,7 +724,7 @@ void CommonTreeAdaptor::defineDotEdges(TreeType* t, const StringType // for (int i=0; i::defineDotEdges(TreeType* t, const StringType } template -typename CommonTreeAdaptor::TreeType* CommonTreeAdaptor::rulePostProcessing( TreeType* root) +typename CommonTreeAdaptor::TreeTypePtr CommonTreeAdaptor::rulePostProcessing( TreeTypePtr& root) { - TreeType* saveRoot; - - // Keep track of the root we are given. If it is a nilNode, then we - // can reuse it rather than orphaning it! - // - saveRoot = root; + TreeTypePtr saveRoot = std::move(root); - if (root != NULL && root->isNilNode()) + if (saveRoot != NULL && saveRoot->isNilNode()) { - if (root->getChildCount() == 0) + if (saveRoot->getChildCount() == 0) { - root = NULL; + return TreeTypePtr(NULL, root.get_deleter()); } - else if (root->getChildCount() == 1) + else if (saveRoot->getChildCount() == 1) { - root = root->getChild(0); - root->setParent(NULL); - root->setChildIndex(-1); - - // The root we were given was a nil node, wiht one child, which means it has - // been abandoned and would be lost in the node factory. However - // nodes can be flagged as resuable to prevent this terrible waste - // - saveRoot->reuse(); + TreeTypePtr newRoot = std::move(saveRoot->getChild(0)); + newRoot->set_parent(NULL); + newRoot->set_childIndex(-1); + + // The root we were given was a nil node, with one child, which means it has + // been abandoned and would be lost in the node factory. + // saveRoot will be releases and put back into factory + // + return newRoot; } } - return root; + return saveRoot; } template @@ -684,15 +839,15 @@ void DebugTreeAdaptor::setDebugEventListener( DebuggerType* debugger } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::nilNode() +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::nilNode() { - TreeType* t = this->create(NULL); + TreeTypePtr t = this->create(NULL); m_debugger->createNode(t); return t; } template -void DebugTreeAdaptor::addChild(TreeType* t, TreeType* child) +void DebugTreeAdaptor::addChild(TreeTypePtr& t, TreeTypePtr& child) { if (t != NULL && child != NULL) { @@ -702,9 +857,9 @@ void DebugTreeAdaptor::addChild(TreeType* t, TreeType* child) } template -void DebugTreeAdaptor::addChildToken(TreeType* t, CommonTokenType* child) +void DebugTreeAdaptor::addChildToken(TreeTypePtr& t, CommonTokenType* child) { - TreeType* tc; + TreeTypePtr tc; if (t != NULL && child != NULL) { tc = this->create(child); @@ -714,58 +869,75 @@ void DebugTreeAdaptor::addChildToken(TreeType* t, CommonTokenType* c } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::becomeRoot( TreeType* newRootTree, TreeType* oldRootTree ) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree ) { - TreeType* t; - t = this->becomeRoot(newRootTree, oldRootTree); + TreeTypePtr t = super::becomeRoot(newRootTree, oldRootTree); m_debugger->becomeRoot(newRootTree, oldRootTree); return t; } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::becomeRootToken(TreeType* newRoot, TreeType* oldRoot) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::becomeRootToken(CommonTokenType* newRoot, TreeTypePtr& oldRoot) { - TreeType* t; - t = this->becomeRoot(this->create(newRoot), oldRoot); + TreeTypePtr t = super::becomeRoot(this->create(newRoot), oldRoot); m_debugger->becomeRoot(t, oldRoot); return t; } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken) { - TreeType* t; + TreeTypePtr t; t = this->createTypeToken(tokenType, fromToken); m_debugger->createNode(t); return t; } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text) { - TreeType* t; + TreeTypePtr t; t = this->createTypeTokenText(tokenType, fromToken, text); m_debugger->createNode(t); return t; } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text) { - TreeType* t; + TreeTypePtr t; t = this->createTypeText(tokenType, text); m_debugger->createNode(t); return t; } template -typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::dupTree( TreeType* tree) +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::dupTree( const TreeTypePtr& tree) +{ + TreeTypePtr t; + + // Call the normal dup tree mechanism first + // + t = this->dupTreeImpl(tree, NULL); + + // In order to tell the debugger what we have just done, we now + // simulate the tree building mechanism. THis will fire + // lots of debugging events to the client and look like we + // duped the tree.. + // + this->simulateTreeConstruction( t); + + return t; +} + +template +typename DebugTreeAdaptor::TreeTypePtr DebugTreeAdaptor::dupTree( const TreeType* tree) { - TreeType* t; + TreeTypePtr t; // Call the normal dup tree mechanism first // - t = this->dupTreeTT(tree, NULL); + t = this->dupTreeImpl(tree, NULL); // In order to tell the debugger what we have just done, we now // simulate the tree building mechanism. THis will fire @@ -778,11 +950,11 @@ typename DebugTreeAdaptor::TreeType* DebugTreeAdaptor::d } template -void DebugTreeAdaptor::simulateTreeConstruction(TreeType* tree) +void DebugTreeAdaptor::simulateTreeConstruction(TreeTypePtr& tree) { ANTLR_UINT32 n; ANTLR_UINT32 i; - TreeType* child; + TreeTypePtr child; // Send the create node event // @@ -797,5 +969,4 @@ void DebugTreeAdaptor::simulateTreeConstruction(TreeType* tree) } } - -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3commontreenodestream.hpp b/runtime/Cpp/include/antlr3commontreenodestream.hpp old mode 100755 new mode 100644 index 962758f56..3adf02e93 --- a/runtime/Cpp/include/antlr3commontreenodestream.hpp +++ b/runtime/Cpp/include/antlr3commontreenodestream.hpp @@ -33,9 +33,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType @@ -50,13 +48,14 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType }; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef TreeType UnitType; typedef typename ImplTraits::StringType StringType; typedef typename ImplTraits::StringStreamType StringStreamType; typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; typedef typename ImplTraits::TreeNodeIntStreamType IntStreamType; typedef typename ImplTraits::AllocPolicyType AllocPolicyType; - typedef typename AllocPolicyType::template VectorType NodesType; + typedef typename AllocPolicyType::template VectorType NodesType; typedef typename AllocPolicyType::template VectorType< TreeWalkState > MarkersType; typedef typename AllocPolicyType::template StackType< ANTLR_INT32 > NodeStackType; typedef typename ImplTraits::TreeParserType ComponentType; @@ -100,7 +99,7 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// Which tree are we navigating ? /// - TreeType* m_root; + TreeTypePtr m_root; /// Pointer to tree adaptor interface that manipulates/builds /// the tree. @@ -121,11 +120,11 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// Which node are we currently visiting? /// - TreeType* m_currentNode; + TreeTypePtr m_currentNode; /// Which node did we last visit? Used for LT(-1) /// - TreeType* m_previousNode; + TreeTypePtr m_previousNode; /// Which child are we currently visiting? If -1 we have not visited /// this node yet; next consume() request will set currentIndex to 0. @@ -141,7 +140,7 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// to fit new lookahead depths, but consume() wraps like a circular /// buffer. /// - TreeType** m_lookAhead; + TreeTypePtr* m_lookAhead; /// Number of elements available in the lookahead buffer at any point in /// time. This is the current size of the array. @@ -181,7 +180,7 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType // CommonTreeNodeStream( ANTLR_UINT32 hint ); CommonTreeNodeStream( const CommonTreeNodeStream& ctn ); - CommonTreeNodeStream( TreeType* tree, ANTLR_UINT32 hint ); + CommonTreeNodeStream( TreeTypePtr tree, ANTLR_UINT32 hint ); void init( ANTLR_UINT32 hint ); ~CommonTreeNodeStream(); @@ -197,12 +196,12 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// returns a tree node instead of a token. Makes code gen identical /// for both parser and tree grammars. :) /// - TreeType* _LT(ANTLR_INT32 k); + TreeTypePtr _LT(ANTLR_INT32 k); /// Where is this stream pulling nodes from? This is not the name, but /// the object that provides node objects. /// - TreeType* getTreeSource(); + TreeTypePtr getTreeSource(); /// What adaptor can tell me how to interpret/navigate nodes and /// trees. E.g., get text of a node. @@ -225,7 +224,7 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// null or "" too, but users should not access $ruleLabel.text in /// an action of course in that case. /// - StringType toStringSS(TreeType* start, TreeType* stop); + StringType toStringSS(TreeTypePtr start, TreeTypePtr stop); /// Return the text of all nodes from start to stop, inclusive, into the /// supplied buffer. @@ -234,13 +233,13 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// null or "" too, but users should not access $ruleLabel.text in /// an action of course in that case. /// - void toStringWork(TreeType* start, TreeType* stop, StringType& buf); + void toStringWork(TreeTypePtr start, TreeTypePtr stop, StringType& buf); /// Get a tree node at an absolute index i; 0..n-1. /// If you don't want to buffer up nodes, then this method makes no /// sense for you. /// - TreeType* get(ANTLR_INT32 i); + TreeTypePtr get(ANTLR_INT32 i); // REWRITING TREES (used by tree parser) @@ -254,10 +253,10 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// If parent is null, don't do anything; must be at root of overall tree. /// Can't replace whatever points to the parent externally. Do nothing. /// - void replaceChildren(TreeType* parent, ANTLR_INT32 startChildIndex, - ANTLR_INT32 stopChildIndex, TreeType* t); + void replaceChildren(TreeTypePtr parent, ANTLR_INT32 startChildIndex, + ANTLR_INT32 stopChildIndex, TreeTypePtr t); - TreeType* LB(ANTLR_INT32 k); + TreeTypePtr LB(ANTLR_INT32 k); /// As we flatten the tree, we use UP, DOWN nodes to represent /// the tree structure. When debugging we need unique nodes @@ -265,9 +264,9 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType /// void addNavigationNode(ANTLR_UINT32 ttype); - TreeType* newDownNode(); + TreeTypePtr newDownNode(); - TreeType* newUpNode(); + TreeTypePtr newUpNode(); bool hasUniqueNavigationNodes() const; @@ -280,7 +279,7 @@ class CommonTreeNodeStream : public ImplTraits::TreeNodeIntStreamType void reset(); void fillBufferRoot(); - void fillBuffer(TreeType* t); + void fillBuffer(TreeTypePtr t); }; @@ -295,14 +294,15 @@ class TreeWalkState : public ImplTraits::AllocPolicyType { public: typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; private: ANTLR_UINT32 m_currentChildIndex; ANTLR_MARKER m_absoluteNodeIndex; - TreeType* m_currentNode; - TreeType* m_previousNode; + TreeTypePtr m_currentNode; + TreeTypePtr m_previousNode; ANTLR_UINT32 m_nodeStackSize; - TreeType* m_lookAhead; + TreeTypePtr m_lookAhead; ANTLR_UINT32 m_lookAheadLength; ANTLR_UINT32 m_tail; ANTLR_UINT32 m_head; @@ -310,7 +310,7 @@ class TreeWalkState : public ImplTraits::AllocPolicyType }; -ANTLR_END_NAMESPACE() +} #include "antlr3commontreenodestream.inl" diff --git a/runtime/Cpp/include/antlr3commontreenodestream.inl b/runtime/Cpp/include/antlr3commontreenodestream.inl old mode 100755 new mode 100644 index 4dce47cfb..096e15db1 --- a/runtime/Cpp/include/antlr3commontreenodestream.inl +++ b/runtime/Cpp/include/antlr3commontreenodestream.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template CommonTreeNodeStream::CommonTreeNodeStream(ANTLR_UINT32 hint) @@ -72,7 +72,7 @@ CommonTreeNodeStream::CommonTreeNodeStream( const CommonTreeNodeStre } template -CommonTreeNodeStream::CommonTreeNodeStream( TreeType* tree, ANTLR_UINT32 hint ) +CommonTreeNodeStream::CommonTreeNodeStream( TreeTypePtr tree, ANTLR_UINT32 hint ) { this->init(hint); m_root = tree; @@ -101,7 +101,7 @@ CommonTreeNodeStream::~CommonTreeNodeStream() } template -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::_LT(ANTLR_INT32 k) +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::_LT(ANTLR_INT32 k) { if ( m_p == -1) { @@ -128,7 +128,7 @@ typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::getTreeSource() +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::getTreeSource() { return m_root; } @@ -152,7 +152,7 @@ typename CommonTreeNodeStream::StringType CommonTreeNodeStream -typename CommonTreeNodeStream::StringType CommonTreeNodeStream::toStringSS(TreeType* start, TreeType* stop) +typename CommonTreeNodeStream::StringType CommonTreeNodeStream::toStringSS(TreeTypePtr start, TreeTypePtr stop) { StringType buf; this->toStringWork(start, stop, buf); @@ -160,7 +160,7 @@ typename CommonTreeNodeStream::StringType CommonTreeNodeStream -void CommonTreeNodeStream::toStringWork(TreeType* start, TreeType* stop, StringType& str) +void CommonTreeNodeStream::toStringWork(TreeTypePtr start, TreeTypePtr stop, StringType& str) { ANTLR_UINT32 n; ANTLR_UINT32 c; @@ -196,7 +196,7 @@ void CommonTreeNodeStream::toStringWork(TreeType* start, TreeType* s for (c = 0; cgetChild(c); this->toStringWork(child, stop, buf); @@ -211,7 +211,7 @@ void CommonTreeNodeStream::toStringWork(TreeType* start, TreeType* s } template -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::get(ANTLR_INT32 k) +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::get(ANTLR_INT32 k) { if( m_p == -1 ) { @@ -222,10 +222,10 @@ typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream -void CommonTreeNodeStream::replaceChildren(TreeType* parent, +void CommonTreeNodeStream::replaceChildren(TreeTypePtr parent, ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, - TreeType* t) + TreeTypePtr t) { if (parent != NULL) { @@ -236,7 +236,7 @@ void CommonTreeNodeStream::replaceChildren(TreeType* parent, } template -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::LB(ANTLR_INT32 k) +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::LB(ANTLR_INT32 k) { if ( k==0) { @@ -254,7 +254,7 @@ typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream void CommonTreeNodeStream::addNavigationNode(ANTLR_UINT32 ttype) { - TreeType* node; + TreeTypePtr node; node = NULL; @@ -287,9 +287,9 @@ void CommonTreeNodeStream::addNavigationNode(ANTLR_UINT32 ttype) } template -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::newDownNode() +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::newDownNode() { - TreeType* dNode; + TreeTypePtr dNode; CommonTokenType* token; token = new CommonTokenType(CommonTokenType::TOKEN_DOWN); @@ -299,9 +299,9 @@ typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream -typename CommonTreeNodeStream::TreeType* CommonTreeNodeStream::newUpNode() +typename CommonTreeNodeStream::TreeTypePtr CommonTreeNodeStream::newUpNode() { - TreeType* uNode; + TreeTypePtr uNode; CommonTokenType* token; token = new CommonTokenType(CommonTokenType::TOKEN_UP); @@ -372,7 +372,7 @@ void CommonTreeNodeStream::fillBufferRoot() } template -void CommonTreeNodeStream::fillBuffer(TreeType* t) +void CommonTreeNodeStream::fillBuffer(TreeTypePtr t) { bool nilNode; ANTLR_UINT32 nCount; @@ -418,5 +418,5 @@ void CommonTreeNodeStream::fillBuffer(TreeType* t) -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3compilerwarning.hpp b/runtime/Cpp/include/antlr3compilerwarning.hpp new file mode 100644 index 000000000..fe6907927 --- /dev/null +++ b/runtime/Cpp/include/antlr3compilerwarning.hpp @@ -0,0 +1,65 @@ +#ifndef _ANTLR3_WARNIN_PUSH +#define _ANTLR3_WARNIN_PUSH + +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The name of the author may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifdef _MSC_VER +//#pragma warning (push) +#pragma warning( disable: 4189 ) // local variable is initialized but not referenced +#pragma warning( disable: 4296 ) // expression is always true +#pragma warning( disable: 4625 ) // copy constructor could not be generated +#pragma warning( disable: 4626 ) // assignment operator could not be generated +#pragma warning( disable: 4640 ) // a local static object is not thread-safe +#pragma warning( disable: 4710 ) // call was not inlined +#pragma warning( disable: 4711 ) // call was inlined +#pragma warning( disable: 4820 ) // some padding was added +#pragma warning( disable: 4127 ) // conditional expression is constant +// These are only really displayed at warning level /W4 but that is the code ideal I am aiming at +// and the codegen must generate some of these warnings by necessity, apart from 4100, which is +// usually generated when a parser rule is given a parameter that it does not use. Mostly though +// this is a matter of orthogonality hence I disable that one. +#pragma warning( disable : 4100 ) // unreferenced parameter +#pragma warning( disable : 4101 ) // unreferenced local variable +#endif + +#if defined(__GNUC__) && !defined(__clang__) +//#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +#if __GNUC_MINOR__ >= 8 +#pragma GCC diagnostic ignored "-Wunused-local-typedefs" +#endif +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#endif + +#ifdef __clang__ +//#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-variable" +#pragma clang diagnostic ignored "-Wparentheses-equality" +#pragma clang diagnostic ignored "-Wtautological-compare" +#pragma clang diagnostic ignored "-Wunknown-pragmas" +#endif + +#endif diff --git a/runtime/Cpp/include/antlr3convertutf.hpp b/runtime/Cpp/include/antlr3convertutf.hpp old mode 100755 new mode 100644 index 8085c2977..7f4b7e0d2 --- a/runtime/Cpp/include/antlr3convertutf.hpp +++ b/runtime/Cpp/include/antlr3convertutf.hpp @@ -101,7 +101,7 @@ #ifndef _ANTLR3_CONVERTUTF_H #define _ANTLR3_CONVERTUTF_H -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { typedef ANTLR_UINT32 UTF32; /* at least 32 bits */ typedef ANTLR_UINT16 UTF16; /* at least 16 bits */ @@ -136,7 +136,7 @@ enum ConversionFlags { -ANTLR_END_NAMESPACE() +} #endif diff --git a/runtime/Cpp/include/antlr3cyclicdfa.hpp b/runtime/Cpp/include/antlr3cyclicdfa.hpp old mode 100755 new mode 100644 index a0d66b925..2129552f1 --- a/runtime/Cpp/include/antlr3cyclicdfa.hpp +++ b/runtime/Cpp/include/antlr3cyclicdfa.hpp @@ -34,16 +34,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -#ifdef ANTLR3_WINDOWS -#pragma warning (push) -#pragma warning (disable : 4510) -#pragma warning (disable : 4512) -#pragma warning (disable : 4610) -#endif - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template class CyclicDFA : public ImplTraits::AllocPolicyType @@ -97,11 +88,7 @@ class CyclicDFA : public ImplTraits::AllocPolicyType void noViableAlt(RecognizerType* rec, ANTLR_UINT32 s); }; -ANTLR_END_NAMESPACE() - -#ifdef ANTLR3_WINDOWS -#pragma warning (pop) -#endif +} #include "antlr3cyclicdfa.inl" diff --git a/runtime/Cpp/include/antlr3cyclicdfa.inl b/runtime/Cpp/include/antlr3cyclicdfa.inl old mode 100755 new mode 100644 index b7b526f6d..f88cd692e --- a/runtime/Cpp/include/antlr3cyclicdfa.inl +++ b/runtime/Cpp/include/antlr3cyclicdfa.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template CyclicDFA::CyclicDFA( ANTLR_INT32 decisionNumber @@ -121,7 +121,7 @@ ANTLR_INT32 CyclicDFA::predict(CtxType * ctx, /* Look for a normal transition state based upon the input token element */ - c = is->_LA(1); + c = is->LA(1); /* Check against min and max for this state */ @@ -201,4 +201,4 @@ void CyclicDFA::noViableAlt(RecognizerType* rec, ANTLR_UINT } } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3debugeventlistener.hpp b/runtime/Cpp/include/antlr3debugeventlistener.hpp old mode 100755 new mode 100644 index 21fcf59a9..bc5ea00a3 --- a/runtime/Cpp/include/antlr3debugeventlistener.hpp +++ b/runtime/Cpp/include/antlr3debugeventlistener.hpp @@ -43,9 +43,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /// Default debugging port /// @@ -60,6 +58,7 @@ class DebugEventListener : public ImplTraits::AllocPolicyType { public: typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef typename ImplTraits::StringType StringType; typedef typename ImplTraits::CommonTokenType CommonTokenType; typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; @@ -311,13 +310,13 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * know what the interface is in full, just those bits that are the base. * @param t */ - virtual void consumeNode( TreeType* t); + virtual void consumeNode( TreeTypePtr t); /** The tree parser looked ahead. If the type is UP or DOWN, * then the ID is not really meaningful as it's fixed--there is * just one UP node and one DOWN navigation node. */ - virtual void LTT( int i, TreeType* t); + virtual void LTT( int i, TreeTypePtr t); // A S T E v e n t s @@ -331,13 +330,13 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * If you are receiving this event over a socket via * RemoteDebugEventSocketListener then only t.ID is set. */ - virtual void nilNode( TreeType* t); + virtual void nilNode( TreeTypePtr t); /** If a syntax error occurs, recognizers bracket the error * with an error node if they are building ASTs. This event * notifies the listener that this is the case */ - virtual void errorNode( TreeType* t); + virtual void errorNode( TreeTypePtr t); /** Announce a new node built from token elements such as type etc... * @@ -345,7 +344,7 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * RemoteDebugEventSocketListener then only t.ID, type, text are * set. */ - virtual void createNode( TreeType* t); + virtual void createNode( TreeTypePtr t); /** Announce a new node built from an existing token. * @@ -353,7 +352,7 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * RemoteDebugEventSocketListener then only node.ID and token.tokenIndex * are set. */ - virtual void createNodeTok( TreeType* node, CommonTokenType* token); + virtual void createNodeTok( TreeTypePtr node, CommonTokenType* token); /** Make a node the new root of an existing root. See * @@ -371,7 +370,7 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * * @see org.antlr.runtime.tree.TreeAdaptor.becomeRoot() */ - virtual void becomeRoot( TreeType* newRoot, TreeType* oldRoot); + virtual void becomeRoot( TreeTypePtr newRoot, TreeTypePtr oldRoot); /** Make childID a child of rootID. * @@ -380,21 +379,21 @@ class DebugEventListener : public ImplTraits::AllocPolicyType * * @see org.antlr.runtime.tree.TreeAdaptor.addChild() */ - virtual void addChild( TreeType* root, TreeType* child); + virtual void addChild( TreeTypePtr root, TreeTypePtr child); /** Set the token start/stop token index for a subtree root or node. * * If you are receiving this event over a socket via * RemoteDebugEventSocketListener then only t.ID is set. */ - virtual void setTokenBoundaries( TreeType* t, ANTLR_MARKER tokenStartIndex, ANTLR_MARKER tokenStopIndex); + virtual void setTokenBoundaries( TreeTypePtr t, ANTLR_MARKER tokenStartIndex, ANTLR_MARKER tokenStopIndex); /// Free up the resources allocated to this structure /// virtual ~DebugEventListener(); }; -ANTLR_END_NAMESPACE() +} #endif diff --git a/runtime/Cpp/include/antlr3defs.hpp b/runtime/Cpp/include/antlr3defs.hpp old mode 100755 new mode 100644 index b27db5f87..e3ccfe0f2 --- a/runtime/Cpp/include/antlr3defs.hpp +++ b/runtime/Cpp/include/antlr3defs.hpp @@ -32,102 +32,17 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -/* Following are for generated code, they are not referenced internally!!! - */ -#if !defined(ANTLR_HUGE) && !defined(ANTLR_AVERAGE) && !defined(ANTLR_SMALL) -#define ANTLR_AVERAGE -#endif - -#ifdef ANTLR_HUGE -#ifndef ANTLR_SIZE_HINT -#define ANTLR_SIZE_HINT 2049 -#endif -#ifndef ANTLR_LIST_SIZE_HINT -#define ANTLR_LIST_SIZE_HINT 127 -#endif -#endif - -#ifdef ANTLR_AVERAGE -#ifndef ANTLR_SIZE_HINT -#define ANTLR_SIZE_HINT 1025 -#define ANTLR_LIST_SIZE_HINT 63 -#endif -#endif - -#ifdef ANTLR_SMALL -#ifndef ANTLR_SIZE_HINT -#define ANTLR_SIZE_HINT 211 -#define ANTLR_LIST_SIZE_HINT 31 -#endif -#endif - -// Definitions that indicate the encoding scheme character streams and strings etc -// -/// Indicates Big Endian for encodings where this makes sense -/// -#define ANTLR_BE 1 - -/// Indicates Little Endian for encoidngs where this makes sense -/// -#define ANTLR_LE 2 - -/// General latin-1 or other 8 bit encoding scheme such as straight ASCII -/// -#define ANTLR_ENC_8BIT 4 - -/// UTF-8 encoding scheme -/// -#define ANTLR_ENC_UTF8 8 - -/// UTF-16 encoding scheme (which also covers UCS2 as that does not have surrogates) -/// -#define ANTLR_ENC_UTF16 16 -#define ANTLR_ENC_UTF16BE 16 + ANTLR_BE -#define ANTLR_ENC_UTF16LE 16 + ANTLR_LE - -/// UTF-32 encoding scheme (basically straight 32 bit) -/// -#define ANTLR_ENC_UTF32 32 -#define ANTLR_ENC_UTF32BE 32 + ANTLR_BE -#define ANTLR_ENC_UTF32LE 32 + ANTLR_LE - -/// Input is 8 bit EBCDIC (which we convert to 8 bit ASCII on the fly -/// -#define ANTLR_ENC_EBCDIC 64 - -#define ANTLR_BEGIN_NAMESPACE() namespace antlr3 { -#define ANTLR_END_NAMESPACE() } - -#define ANTLR_USE_64BIT - -/* Common definitions come first - */ -#include +// not used in C++ target (kept for "historical" reasons, the generated code still uses this) +#define ANTLR_SIZE_HINT 0U /* Work out what operating system/compiler this is. We just do this once * here and use an internal symbol after this. */ #ifdef _WIN64 - -# ifndef ANTLR_WINDOWS -# define ANTLR_WINDOWS -# endif -# define ANTLR_WIN64 # define ANTLR_USE_64BIT - -#else - -#ifdef _WIN32 -# ifndef ANTLR_WINDOWS -# define ANTLR_WINDOWS -# endif - -#define ANTLR_WIN32 -#endif - #endif -#ifdef ANTLR_WINDOWS +#ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN @@ -139,183 +54,58 @@ #define _CRT_SECURE_NO_DEPRECATE #endif -#include #include -#include -#include -#include - -#define ANTLR_API __declspec(dllexport) -#define ANTLR_CDECL __cdecl -#define ANTLR_FASTCALL __fastcall - - -#ifndef __MINGW32__ -// Standard Windows types -// -typedef INT32 ANTLR_CHAR; -typedef UINT32 ANTLR_UCHAR; - -typedef INT8 ANTLR_INT8; -typedef INT16 ANTLR_INT16; -typedef INT32 ANTLR_INT32; -typedef INT64 ANTLR_INT64; -typedef UINT8 ANTLR_UINT8; -typedef UINT16 ANTLR_UINT16; -typedef UINT32 ANTLR_UINT32; -typedef UINT64 ANTLR_UINT64; -typedef UINT64 ANTLR_BITWORD; - -#else -// Mingw uses stdint.h and fails to define standard Microsoft typedefs -// such as UINT16, hence we must use stdint.h for Mingw. -// -#include -typedef int32_t ANTLR_CHAR; -typedef uint32_t ANTLR_UCHAR; - -typedef int8_t ANTLR_INT8; -typedef int16_t ANTLR_INT16; -typedef int32_t ANTLR_INT32; -typedef int64_t ANTLR_INT64; - -typedef uint8_t ANTLR_UINT8; -typedef uint16_t ANTLR_UINT16; -typedef uint32_t ANTLR_UINT32; -typedef uint64_t ANTLR_UINT64; -typedef uint64_t ANTLR_BITWORD; - -#endif - - - -#define ANTLR_UINT64_LIT(lit) lit##ULL #define ANTLR_INLINE __inline typedef FILE * ANTLR_FDSC; -typedef struct stat ANTLR_FSTAT_STRUCT; - - - -#ifdef ANTLR_USE_64BIT -#define ANTLR_UINT64_CAST(ptr) ((ANTLR_UINT64)(ptr)) -#define ANTLR_UINT32_CAST(ptr) (ANTLR_UINT32)((ANTLR_UINT64)(ptr)) -typedef ANTLR_INT64 ANTLR_MARKER; -typedef ANTLR_UINT64 ANTLR_INTKEY; -#else -#define ANTLR_UINT64_CAST(ptr) (ANTLR_UINT64)((ANTLR_UINT32)(ptr)) -#define ANTLR_UINT32_CAST(ptr) (ANTLR_UINT32)(ptr) -typedef ANTLR_INT32 ANTLR_MARKER; -typedef ANTLR_UINT32 ANTLR_INTKEY; -#endif -#ifdef ANTLR_WIN32 -#endif - -#ifdef ANTLR_WIN64 -#endif - - -typedef int ANTLR_SALENT; // Type used for size of accept structure typedef struct sockaddr_in ANTLR_SOCKADDRT, * pANTLR_SOCKADDRT; // Type used for socket address declaration typedef struct sockaddr ANTLR_SOCKADDRC, * pANTLR_SOCKADDRC; // Type used for cast on accept() #define ANTLR_CLOSESOCKET closesocket -/* Warnings that are over-zealous such as complaining about strdup, we - * can turn off. - */ - -/* Don't complain about "deprecated" functions such as strdup - */ -#pragma warning( disable : 4996 ) - -#else +#else // Un*x #ifdef __LP64__ #define ANTLR_USE_64BIT #endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define _stat stat +#define ANTLR_INLINE inline typedef int SOCKET; +typedef FILE * ANTLR_FDSC; -/* Inherit type definitions for autoconf - */ -typedef int32_t ANTLR_CHAR; -typedef uint32_t ANTLR_UCHAR; - -typedef int8_t ANTLR_INT8; -typedef int16_t ANTLR_INT16; -typedef int32_t ANTLR_INT32; -typedef int64_t ANTLR_INT64; +#endif -typedef uint8_t ANTLR_UINT8; -typedef uint16_t ANTLR_UINT16; -typedef uint32_t ANTLR_UINT32; -typedef uint64_t ANTLR_UINT64; -typedef uint64_t ANTLR_BITWORD; +// Standard integer types (since C++11) (should work with MSVC 2010/2013, gcc, clang) +// +typedef std::int32_t ANTLR_CHAR; +typedef std::uint32_t ANTLR_UCHAR; -#define ANTLR_INLINE inline -#define ANTLR_API +typedef std::int8_t ANTLR_INT8; +typedef std::int16_t ANTLR_INT16; +typedef std::int32_t ANTLR_INT32; +typedef std::int64_t ANTLR_INT64; -typedef FILE * ANTLR_FDSC; -typedef struct stat ANTLR_FSTAT_STRUCT; +typedef std::uint8_t ANTLR_UINT8; +typedef std::uint16_t ANTLR_UINT16; +typedef std::uint32_t ANTLR_UINT32; +typedef std::uint64_t ANTLR_UINT64; +typedef std::uint64_t ANTLR_BITWORD; #ifdef ANTLR_USE_64BIT -#define ANTLR_FUNC_PTR(ptr) (void *)((ANTLR_UINT64)(ptr)) #define ANTLR_UINT64_CAST(ptr) (ANTLR_UINT64)(ptr)) #define ANTLR_UINT32_CAST(ptr) (ANTLR_UINT32)((ANTLR_UINT64)(ptr)) typedef ANTLR_INT64 ANTLR_MARKER; typedef ANTLR_UINT64 ANTLR_INTKEY; #else -#define ANTLR_FUNC_PTR(ptr) (void *)((ANTLR_UINT32)(ptr)) #define ANTLR_UINT64_CAST(ptr) (ANTLR_UINT64)((ANTLR_UINT32)(ptr)) #define ANTLR_UINT32_CAST(ptr) (ANTLR_UINT32)(ptr) typedef ANTLR_INT32 ANTLR_MARKER; typedef ANTLR_UINT32 ANTLR_INTKEY; -#endif -#define ANTLR_UINT64_LIT(lit) lit##ULL - #endif -#ifdef ANTLR_USE_64BIT -#define ANTLR_TRIE_DEPTH 63 -#else -#define ANTLR_TRIE_DEPTH 31 -#endif -/* Pre declare the typedefs for all the interfaces, then - * they can be inter-dependant and we will let the linker - * sort it out for us. - */ -#include - -// Include the unicode.org conversion library header. -// -#include - -enum ChannelType -{ - /** Default channel for a token - */ - TOKEN_DEFAULT_CHANNEL = 0 - /** Reserved channel number for a HIDDEN token - a token that - * is hidden from the parser. - */ - , HIDDEN = 99 -}; +#define ANTLR_UINT64_LIT(lit) lit##ULL #endif /* _ANTLR3DEFS_H */ diff --git a/runtime/Cpp/include/antlr3errors.hpp b/runtime/Cpp/include/antlr3errors.hpp old mode 100755 new mode 100644 index 876df4f4f..0413d87e3 --- a/runtime/Cpp/include/antlr3errors.hpp +++ b/runtime/Cpp/include/antlr3errors.hpp @@ -40,10 +40,4 @@ #define MEMO_RULE_FAILED 0xFFFFFFFE #define MEMO_RULE_UNKNOWN 0xFFFFFFFF - -#define ANTLR_ERR_BASE 0 -#define ANTLR_ERR_NOMEM (ANTLR_ERR_BASE + 1) -#define ANTLR_ERR_NOFILE (ANTLR_ERR_BASE + 2) -#define ANTLR_ERR_HASHDUP (ANTLR_ERR_BASE + 3) - #endif /* _ANTLR3ERRORS_H */ diff --git a/runtime/Cpp/include/antlr3exception.hpp b/runtime/Cpp/include/antlr3exception.hpp old mode 100755 new mode 100644 index beca1fc5e..9bc18a043 --- a/runtime/Cpp/include/antlr3exception.hpp +++ b/runtime/Cpp/include/antlr3exception.hpp @@ -43,9 +43,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** Base structure for an ANTLR3 exception tracker */ @@ -202,7 +200,7 @@ class ANTLR_Exception : public ImplTraits::template ExceptionBaseType ANTLR_ExceptionBase::ANTLR_ExceptionBase(const StringType& message) @@ -111,6 +111,8 @@ ANTLR_INLINE void ANTLR_ExceptionBase::set_index( ANTLR_ template ANTLR_INLINE void ANTLR_ExceptionBase::set_token( const TokenType* token ) { + if (m_token) + delete m_token; m_token = token; } template @@ -341,6 +343,7 @@ void ANTLR_Exception::displayRecognitionError( ANTLR str_stream << "Actually dude, we didn't seem to be expecting anything here, or at least\n"; str_stream << "I could not work out what I was expecting, like so many of us these days!\n"; } + delete errBits; } break; case EARLY_EXIT_EXCEPTION: @@ -358,10 +361,10 @@ ANTLR_ExceptionBase::~ANTLR_ExceptionBase() ANTLR_ExceptionBase* next; ANTLR_ExceptionBase* ex = m_nextException; - /* Ensure valid pointer - */ - while (ex != NULL) - { + /* Ensure valid pointer + */ + while (ex != NULL) + { /* Pick up anythign following now, before we free the * current memory block. */ @@ -373,7 +376,9 @@ ANTLR_ExceptionBase::~ANTLR_ExceptionBase() delete ex; ex = next; - } + } + if ( m_token) + delete m_token; } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3filestream.hpp b/runtime/Cpp/include/antlr3filestream.hpp old mode 100755 new mode 100644 index 9a46d3582..b93de6d29 --- a/runtime/Cpp/include/antlr3filestream.hpp +++ b/runtime/Cpp/include/antlr3filestream.hpp @@ -29,12 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include -#include - -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template class FileUtils @@ -68,7 +63,7 @@ class ParseFileAbsentException : public std::exception } }; -ANTLR_END_NAMESPACE() +} #include "antlr3filestream.inl" diff --git a/runtime/Cpp/include/antlr3filestream.inl b/runtime/Cpp/include/antlr3filestream.inl old mode 100755 new mode 100644 index 59b4f0658..b67804adf --- a/runtime/Cpp/include/antlr3filestream.inl +++ b/runtime/Cpp/include/antlr3filestream.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template ANTLR_FDSC FileUtils::AntlrFopen(const ANTLR_UINT8* filename, const char * mode) @@ -71,4 +71,4 @@ ANTLR_UINT32 FileUtils::AntlrRead8Bit(InputStreamType* input, const return ANTLR_SUCCESS; } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3input.hpp b/runtime/Cpp/include/antlr3input.hpp old mode 100755 new mode 100644 index 175a5da6e..c3088c0c0 --- a/runtime/Cpp/include/antlr3input.hpp +++ b/runtime/Cpp/include/antlr3input.hpp @@ -36,12 +36,10 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /// Master context structure for an ANTLR3 C runtime based input stream. -/// \ingroup apistructures. Calling _LT on this doesn't seem right. You would +/// \ingroup apistructures. Calling LT on this doesn't seem right. You would /// call it only with parser / TreeParser, and their respective input streams /// has that function. calling it from lexer will throw a compile time error /// @@ -320,7 +318,7 @@ class ParseNullStringException : public std::exception } }; -ANTLR_END_NAMESPACE() +} #include "antlr3input.inl" diff --git a/runtime/Cpp/include/antlr3input.inl b/runtime/Cpp/include/antlr3input.inl old mode 100755 new mode 100644 index f3362cded..6837a0654 --- a/runtime/Cpp/include/antlr3input.inl +++ b/runtime/Cpp/include/antlr3input.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template InputStream::InputStream(const ANTLR_UINT8* fileName, ANTLR_UINT32 encoding) @@ -317,9 +317,9 @@ void InputStream::reuse(ANTLR_UINT8* inString, ANTLR_UINT32 size, /* template -typename InputStream::DataType* InputStream::_LT(ANTLR_INT32 lt) +typename InputStream::DataType* InputStream::LT(ANTLR_INT32 lt) { - return this->_LA(lt); + return this->LA(lt); } */ @@ -465,7 +465,7 @@ void InputStream::setupInputStream() // switch (m_encoding) { - case ANTLR_ENC_UTF8: + case ENC_UTF8: // See if there is a BOM at the start of this UTF-8 sequence // and just eat it if there is. Windows .TXT files have this for instance @@ -488,7 +488,7 @@ void InputStream::setupInputStream() this->set_charByteSize(0); break; - case ANTLR_ENC_UTF16: + case ENC_UTF16: // See if there is a BOM at the start of the input. If not then // we assume that the byte order is the natural order of this @@ -524,7 +524,7 @@ void InputStream::setupInputStream() this->set_charByteSize(2); break; - case ANTLR_ENC_UTF32: + case ENC_UTF32: // See if there is a BOM at the start of the input. If not then // we assume that the byte order is the natural order of this @@ -564,7 +564,7 @@ void InputStream::setupInputStream() this->set_charByteSize(4); break; - case ANTLR_ENC_UTF16BE: + case ENC_UTF16BE: // Encoding is definately Big Endian with no BOM // @@ -572,7 +572,7 @@ void InputStream::setupInputStream() this->set_charByteSize(2); break; - case ANTLR_ENC_UTF16LE: + case ENC_UTF16LE: // Encoding is definately Little Endian with no BOM // @@ -580,7 +580,7 @@ void InputStream::setupInputStream() this->set_charByteSize(2); break; - case ANTLR_ENC_UTF32BE: + case ENC_UTF32BE: // Encoding is definately Big Endian with no BOM // @@ -588,7 +588,7 @@ void InputStream::setupInputStream() this->set_charByteSize(4); break; - case ANTLR_ENC_UTF32LE: + case ENC_UTF32LE: // Encoding is definately Little Endian with no BOM // @@ -596,7 +596,7 @@ void InputStream::setupInputStream() this->set_charByteSize(4); break; - case ANTLR_ENC_EBCDIC: + case ENC_EBCDIC: // EBCDIC is basically the same as ASCII but with an on the // fly translation to ASCII @@ -605,7 +605,7 @@ void InputStream::setupInputStream() this->set_charByteSize(1); break; - case ANTLR_ENC_8BIT: + case ENC_8BIT: default: // Standard 8bit/ASCII @@ -616,4 +616,4 @@ void InputStream::setupInputStream() } } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3interfaces.hpp b/runtime/Cpp/include/antlr3interfaces.hpp old mode 100755 new mode 100644 index f6297844e..5f04b40c4 --- a/runtime/Cpp/include/antlr3interfaces.hpp +++ b/runtime/Cpp/include/antlr3interfaces.hpp @@ -34,37 +34,27 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { -template -class IntStream; - -/// Pointer to an instantiation of 'class' #ANTLR3_RECOGNIZER_SHARED_STATE -/// \ingroup ANTLR3_RECOGNIZER_SHARED_STATE -/// -template -class RecognizerSharedState; - -/// Pointer to an instantiation of 'class' #ANTLR3_BITSET_LIST -/// \ingroup ANTLR3_BITSET_LIST -/// -template -class BitsetList; - -/// Pointer to an instantiation of 'class' #ANTLR3_BITSET -/// \ingroup ANTLR3_BITSET -/// -template -class Bitset; - -/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TOKEN -/// \ingroup ANTLR3_COMMON_TOKEN -/// -template -class CommonToken; +// Definitions that indicate the encoding scheme character streams and strings etc +enum Encoding +{ + ENC_8BIT = 4 /// General latin-1 or other 8 bit encoding scheme such as straight ASCII + , ENC_UTF8 = 8 /// UTF-8 encoding scheme + , ENC_UTF16 = 16 /// UTF-16 encoding scheme (which also covers UCS2 as that does not have surrogates) + , ENC_UTF16BE + , ENC_UTF16LE + , ENC_UTF32 = 32 /// UTF-32 encoding scheme (basically straight 32 bit) + , ENC_UTF32BE + , ENC_UTF32LE + , ENC_EBCDIC = 64 /// Input is 8 bit EBCDIC (which we convert to 8 bit ASCII on the fly +}; -template -class CommonTokenFunctions; +enum ChannelType +{ + TOKEN_DEFAULT_CHANNEL = 0 /// Default channel for a token + , HIDDEN = 99 /// Reserved channel number for a HIDDEN token - a token that is hidden from the parser +}; /// Pointer to an instantiation of 'class' #ANTLR3_EXCEPTION /// \ingroup ANTLR3_EXCEPTION @@ -104,6 +94,33 @@ enum ExceptionType , MISSING_TOKEN_EXCEPTION }; +template +class IntStream; + +/// Pointer to an instantiation of 'class' #ANTLR3_RECOGNIZER_SHARED_STATE +/// \ingroup ANTLR3_RECOGNIZER_SHARED_STATE +/// +template +class RecognizerSharedState; + +/// Pointer to an instantiation of 'class' #ANTLR3_BITSET_LIST +/// \ingroup ANTLR3_BITSET_LIST +/// +template +class BitsetList; + +/// Pointer to an instantiation of 'class' #ANTLR3_BITSET +/// \ingroup ANTLR3_BITSET +/// +template +class Bitset; + +/// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TOKEN +/// \ingroup ANTLR3_COMMON_TOKEN +/// +template +class CommonToken; + template class ANTLR_Exception; @@ -130,25 +147,18 @@ class LexState; /// template class TokenSource; -template -class TokenSourceFunctions; /// Pointer to an instantiation of 'class' #ANTLR3_TOKEN_STREAM /// \ingroup ANTLR3_TOKEN_STREAM /// template class TokenStream; -template -class TokenStreamFunctions; /// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TOKEN_STREAM /// \ingroup ANTLR3_COMMON_TOKEN_STREAM /// template class CommonTokenStream; -template -class CommonTokenStreamFunctions; - /// Pointer to an instantiation of 'class' #ANTLR3_CYCLIC_DFA /// \ingroup ANTLR3_CYCLIC_DFA @@ -173,17 +183,12 @@ class Parser; /// template class BaseTree; -template -class BaseTreeFunctions; - /// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TREE /// \ingroup ANTLR3_COMMON_TREE /// template class CommonTree; -template -class CommonTreeFunctions; /// Pointer to an instantiation of 'class' #ANTLR3_PARSE_TREE /// \ingroup ANTLR3_PARSE_TREE @@ -209,23 +214,11 @@ class CommonTreeNodeStream; template class TreeWalkState; -/// Pointer to an instantiation of 'class' #ANTLR3_BASE_TREE_ADAPTOR -/// \ingroup ANTLR3_BASE_TREE_ADAPTOR -/// -template -class BaseTreeAdaptor; -template -class BaseTreeAdaptorFunctions; - - /// Pointer to an instantiation of 'class' #ANTLR3_COMMON_TREE_ADAPTOR /// \ingroup ANTLR3_COMMON_TREE_ADAPTOR /// template class CommonTreeAdaptor; -template -class CommonTreeAdaptorFunctions; - /// Pointer to an instantiation of 'class' #ANTLR3_TREE_PARSER /// \ingroup ANTLR3_TREE_PARSER @@ -295,7 +288,6 @@ class ComponentTypeFinder< ImplTraits, typename ImplTraits::TreeNodeStreamType> typedef typename ImplTraits::TreeParserType ComponentType; }; - -ANTLR_END_NAMESPACE() +} #endif diff --git a/runtime/Cpp/include/antlr3intstream.hpp b/runtime/Cpp/include/antlr3intstream.hpp old mode 100755 new mode 100644 index 82c116bd2..01bf60a7c --- a/runtime/Cpp/include/antlr3intstream.hpp +++ b/runtime/Cpp/include/antlr3intstream.hpp @@ -59,11 +59,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { enum STREAM_TYPE { @@ -145,7 +141,7 @@ class IntStream : public ImplTraits::AllocPolicyType /** Get ANTLR3_UINT32 at current input pointer + i ahead where i=1 is next ANTLR3_UINT32 */ - ANTLR_UINT32 _LA( ANTLR_INT32 i); + ANTLR_UINT32 LA( ANTLR_INT32 i); /** Tell the stream to start buffering if it hasn't already. Return * current input position, index(), or some other marker so that @@ -219,7 +215,7 @@ class IntStream : public ImplTraits::AllocPolicyType void findout_endian_spec(bool machineBigEndian, bool inputBigEndian); //If the user chooses this option, then we will be resolving stuffs at run-time - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); //resolve into one of the three categories below at runtime void consume( ClassForwarder ); @@ -229,7 +225,7 @@ template class EBCDIC_IntStream : public IntStream { public: - ANTLR_UINT32 _LA( ANTLR_INT32 i); + ANTLR_UINT32 LA( ANTLR_INT32 i); protected: void setupIntStream(); @@ -239,7 +235,7 @@ template class UTF8_IntStream : public IntStream { public: - ANTLR_UINT32 _LA( ANTLR_INT32 i); + ANTLR_UINT32 LA( ANTLR_INT32 i); void consume(); protected: @@ -254,7 +250,7 @@ template class UTF16_IntStream : public IntStream { public: - ANTLR_UINT32 _LA( ANTLR_INT32 i); + ANTLR_UINT32 LA( ANTLR_INT32 i); void consume(); ANTLR_MARKER index(); void seek(ANTLR_MARKER seekPoint); @@ -269,7 +265,7 @@ class UTF16_IntStream : public IntStream /// /// \return Next input character in internal ANTLR3 encoding (UTF32) /// - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); /// \brief Return the input element assuming a UTF16 input when the input is Little Endian and the machine is not /// @@ -278,7 +274,7 @@ class UTF16_IntStream : public IntStream /// /// \return Next input character in internal ANTLR3 encoding (UTF32) /// - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); /// \brief Return the input element assuming a UTF16 input when the input is Little Endian and the machine is not /// @@ -287,7 +283,7 @@ class UTF16_IntStream : public IntStream /// /// \return Next input character in internal ANTLR3 encoding (UTF32) /// - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); /// \brief Consume the next character in a UTF16 input stream /// @@ -318,7 +314,7 @@ template class UTF32_IntStream : public IntStream { public: - ANTLR_UINT32 _LA( ANTLR_INT32 i); + ANTLR_UINT32 LA( ANTLR_INT32 i); void consume(); /// \brief Calculate the current index in the output stream. @@ -329,10 +325,10 @@ class UTF32_IntStream : public IntStream protected: void setupIntStream(bool machineBigEndian, bool inputBigEndian); - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); - ANTLR_UINT32 _LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); + ANTLR_UINT32 LA( ANTLR_INT32 i, ClassForwarder ); void consume( ClassForwarder ); void consume( ClassForwarder ); @@ -363,7 +359,7 @@ class TokenIntStream : public IntStream -class TreeNodeIntStream : public IntStream +class TreeNodeIntStream : public IntStream { public: - typedef typename ImplTraits::CommonTreeNodeStreamType CommonTreeNodeStreamType; - typedef IntStream BaseType; + typedef typename ImplTraits::TreeNodeStreamType TreeNodeStreamType; + typedef IntStream BaseType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef typename ImplTraits::CommonTokenType CommonTokenType; public: void consume(); ANTLR_MARKER tindex(); - ANTLR_UINT32 _LA(ANTLR_INT32 i); + ANTLR_UINT32 LA(ANTLR_INT32 i); ANTLR_MARKER mark(); void release(ANTLR_MARKER marker); void rewindMark(ANTLR_MARKER marker); @@ -396,7 +393,7 @@ class TreeNodeIntStream : public IntStream ANTLR_INLINE IntStream::IntStream() @@ -74,7 +74,7 @@ void IntStream::consume() } template -ANTLR_UINT32 IntStream::_LA( ANTLR_INT32 la ) +ANTLR_UINT32 IntStream::LA( ANTLR_INT32 la ) { SuperType* input = this->get_super(); const ANTLR_UINT8* nextChar = input->get_nextChar(); @@ -230,7 +230,7 @@ IntStream::~IntStream() } template -ANTLR_UINT32 EBCDIC_IntStream::_LA( ANTLR_INT32 la) +ANTLR_UINT32 EBCDIC_IntStream::LA( ANTLR_INT32 la) { // EBCDIC to ASCII conversion table // @@ -295,9 +295,9 @@ void EBCDIC_IntStream::setupIntStream() } template -ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 i) +ANTLR_UINT32 UTF16_IntStream::LA( ANTLR_INT32 i) { - return this->_LA(i, ClassForwarder< typename ImplTraits::Endianness >() ); + return this->LA(i, ClassForwarder< typename ImplTraits::Endianness >() ); } template @@ -331,7 +331,7 @@ void UTF16_IntStream::seek(ANTLR_MARKER seekPoint) { // Call consume until we reach the asked for seek point or EOF // - while( (this->_LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar() ) ) + while( (this->LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar() ) ) { this->consume(); } @@ -396,19 +396,19 @@ void UTF16_IntStream::setupIntStream(bool machineBigEndia } template -ANTLR_UINT32 IntStream::_LA( ANTLR_INT32 i, ClassForwarder ) +ANTLR_UINT32 IntStream::LA( ANTLR_INT32 i, ClassForwarder ) { assert( (m_endian_spec >= 1) && (m_endian_spec <= 3)); switch(m_endian_spec) { case 1: - return this->_LA(i, ClassForwarder() ); + return this->LA(i, ClassForwarder() ); break; case 2: - return this->_LA(i, ClassForwarder() ); + return this->LA(i, ClassForwarder() ); break; case 3: - return this->_LA(i, ClassForwarder() ); + return this->LA(i, ClassForwarder() ); break; default: break; @@ -437,7 +437,7 @@ void IntStream::consume( ClassForwarder -ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF16_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input; UTF32 ch; @@ -572,7 +572,7 @@ ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 la, ClassF } template -ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF16_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input; UTF32 ch; @@ -710,7 +710,7 @@ ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 la, ClassF } template -ANTLR_UINT32 UTF16_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF16_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input; UTF32 ch; @@ -1063,9 +1063,9 @@ void UTF16_IntStream::consume( ClassForwarder -ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 i) +ANTLR_UINT32 UTF32_IntStream::LA( ANTLR_INT32 i) { - return this->_LA( i, ClassForwarder() ); + return this->LA( i, ClassForwarder() ); } template @@ -1095,7 +1095,7 @@ void UTF32_IntStream::seek(ANTLR_MARKER seekPoint) { // Call consume until we reach the asked for seek point or EOF // - while( (this->_LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar()) ) + while( (this->LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar()) ) { this->consume(); } @@ -1113,7 +1113,7 @@ void UTF32_IntStream::setupIntStream(bool machineBigEndia } template -ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF32_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input = this->get_super(); @@ -1128,7 +1128,7 @@ ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 la, ClassF } template -ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF32_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input = this->get_super(); @@ -1149,7 +1149,7 @@ ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 la, ClassF } template -ANTLR_UINT32 UTF32_IntStream::_LA( ANTLR_INT32 la, ClassForwarder ) +ANTLR_UINT32 UTF32_IntStream::LA( ANTLR_INT32 la, ClassForwarder ) { SuperType* input = this->get_super(); @@ -1323,7 +1323,7 @@ void UTF8_IntStream::consume() * \return Next input character in internal ANTLR3 encoding (UTF32) */ template -ANTLR_UCHAR UTF8_IntStream::_LA(ANTLR_INT32 la) +ANTLR_UCHAR UTF8_IntStream::LA(ANTLR_INT32 la) { SuperType* input = this->get_super(); const ANTLR_UINT32* trailingBytesForUTF8 = UTF8_IntStream::TrailingBytesForUTF8(); @@ -1477,12 +1477,12 @@ void TokenIntStream::consumeInitialHiddenTokens() template -ANTLR_UINT32 TokenIntStream::_LA( ANTLR_INT32 i ) +ANTLR_UINT32 TokenIntStream::LA( ANTLR_INT32 i ) { const CommonTokenType* tok; TokenStreamType* ts = static_cast(this); - tok = ts->_LT(i); + tok = ts->LT(i); if (tok != NULL) { @@ -1570,7 +1570,7 @@ TokenIntStream::getSourceName() template void TreeNodeIntStream::consume() { - CommonTreeNodeStreamType* ctns = this->get_super(); + TreeNodeStreamType* ctns = this->get_super(); if( ctns->get_p() == -1 ) ctns->fillBufferRoot(); ctns->inc_p(); @@ -1578,18 +1578,18 @@ void TreeNodeIntStream::consume() template ANTLR_MARKER TreeNodeIntStream::tindex() { - CommonTreeNodeStreamType* ctns = this->get_super(); + TreeNodeStreamType* ctns = this->get_super(); return (ANTLR_MARKER)(ctns->get_p()); } template -ANTLR_UINT32 TreeNodeIntStream::_LA(ANTLR_INT32 i) +ANTLR_UINT32 TreeNodeIntStream::LA(ANTLR_INT32 i) { - CommonTreeNodeStreamType* tns = this->get_super(); + TreeNodeStreamType* tns = this->get_super(); // Ask LT for the 'token' at that position // - TreeType* t = tns->_LT(i); + TreeTypePtr t = tns->LT(i); if (t == NULL) { @@ -1604,7 +1604,7 @@ ANTLR_UINT32 TreeNodeIntStream::_LA(ANTLR_INT32 i) template ANTLR_MARKER TreeNodeIntStream::mark() { - CommonTreeNodeStreamType* ctns = this->get_super(); + TreeNodeStreamType* ctns = this->get_super(); if (ctns->get_p() == -1) { @@ -1640,14 +1640,14 @@ void TreeNodeIntStream::rewindLast() template void TreeNodeIntStream::seek(ANTLR_MARKER index) { - CommonTreeNodeStreamType* ctns = this->get_super(); + TreeNodeStreamType* ctns = this->get_super(); ctns->set_p( ANTLR_UINT32_CAST(index) ); } template ANTLR_UINT32 TreeNodeIntStream::size() { - CommonTreeNodeStreamType* ctns = this->get_super(); + TreeNodeStreamType* ctns = this->get_super(); if (ctns->get_p() == -1) { @@ -1658,4 +1658,4 @@ ANTLR_UINT32 TreeNodeIntStream::size() } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3lexer.hpp b/runtime/Cpp/include/antlr3lexer.hpp old mode 100755 new mode 100644 index cfff29f9e..d23e65dea --- a/runtime/Cpp/include/antlr3lexer.hpp +++ b/runtime/Cpp/include/antlr3lexer.hpp @@ -60,9 +60,8 @@ /* Definitions */ -#include "antlr3defs.hpp" -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { static const ANTLR_UINT32 ANTLR_STRING_TERMINATOR = 0xFFFFFFFF; @@ -241,7 +240,7 @@ class Lexer : public ImplTraits::template RecognizerType< typename ImplTraits:: ~Lexer(); }; -ANTLR_END_NAMESPACE() +} #include "antlr3lexer.inl" diff --git a/runtime/Cpp/include/antlr3lexer.inl b/runtime/Cpp/include/antlr3lexer.inl old mode 100755 new mode 100644 index c8ccbd864..bf6960569 --- a/runtime/Cpp/include/antlr3lexer.inl +++ b/runtime/Cpp/include/antlr3lexer.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template Lexer::Lexer(ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state) @@ -109,7 +109,7 @@ void Lexer::displayRecognitionError( ANTLR_UINT8** , ExceptionBaseTy template void Lexer::fillExceptionData( ExceptionBaseType* ex ) { - ex->set_c( m_input->_LA(1) ); /* Current input character */ + ex->set_c( m_input->LA(1) ); /* Current input character */ ex->set_line( m_input->get_line() ); /* Line number comes from stream */ ex->set_charPositionInLine( m_input->get_charPositionInLine() ); /* Line offset also comes from the stream */ ex->set_index( m_input->index() ); @@ -235,7 +235,7 @@ bool Lexer::matchs(ANTLR_UCHAR* str ) RecognizerSharedStateType* state = this->get_rec()->get_state(); while (*str != ANTLR_STRING_TERMINATOR) { - if ( this->get_istream()->_LA(1) != (*str)) + if ( this->get_istream()->LA(1) != (*str)) { if ( state->get_backtracking() > 0) { @@ -267,7 +267,7 @@ bool Lexer::matchs(ANTLR_UCHAR* str ) template bool Lexer::matchc(ANTLR_UCHAR c) { - if (this->get_istream()->_LA(1) == c) + if (this->get_istream()->LA(1) == c) { /* Matched correctly, do consume it */ @@ -304,7 +304,7 @@ bool Lexer::matchRange(ANTLR_UCHAR low, ANTLR_UCHAR high) /* What is in the stream at the moment? */ - c = this->get_istream()->_LA(1); + c = this->get_istream()->LA(1); if ( c >= low && c <= high) { /* Matched correctly, consume it @@ -579,7 +579,7 @@ ANTLR_INLINE typename Lexer::DebuggerType* Lexer::get_de template< class ImplTraits> ANTLR_INLINE ANTLR_UINT32 Lexer::LA(ANTLR_INT32 i) { - return this->get_istream()->_LA(i); + return this->get_istream()->LA(i); } template< class ImplTraits> @@ -588,5 +588,5 @@ ANTLR_INLINE void Lexer::consume() return this->get_istream()->consume(); } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3memory.hpp b/runtime/Cpp/include/antlr3memory.hpp old mode 100755 new mode 100644 index 7713613d7..f188d3175 --- a/runtime/Cpp/include/antlr3memory.hpp +++ b/runtime/Cpp/include/antlr3memory.hpp @@ -29,17 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include - -#include -#include -#include -#include -#include - -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { class DefaultAllocPolicy { @@ -61,8 +51,8 @@ class DefaultAllocPolicy }; AllocatorType() throw() {} - AllocatorType( const AllocatorType& alloc ) throw() {} - template AllocatorType(const AllocatorType& alloc) throw(){} + AllocatorType( const AllocatorType& ) throw() {} + template AllocatorType(const AllocatorType& ) throw(){} }; template @@ -110,6 +100,26 @@ class DefaultAllocPolicy { }; + template + class SmartPtrType : public std::unique_ptr > + { + typedef typename std::unique_ptr > BaseType; + public: + SmartPtrType() {}; + SmartPtrType( SmartPtrType&& other ) + : BaseType() + {}; + SmartPtrType & operator=(SmartPtrType&& other) //= default; + { + BaseType::swap(other); + //return std::move((BaseType&)other); + return *this; + } + private: + SmartPtrType & operator=(const SmartPtrType&) /*= delete*/; + SmartPtrType(const SmartPtrType&) /*= delete*/; + }; + ANTLR_INLINE static void* operator new (std::size_t bytes) { void* p = alloc(bytes); @@ -159,6 +169,6 @@ class DefaultAllocPolicy } }; -ANTLR_END_NAMESPACE() +} #endif /* _ANTLR3MEMORY_H */ diff --git a/runtime/Cpp/include/antlr3parser.hpp b/runtime/Cpp/include/antlr3parser.hpp old mode 100755 new mode 100644 index cc9b47388..ccf8e9a32 --- a/runtime/Cpp/include/antlr3parser.hpp +++ b/runtime/Cpp/include/antlr3parser.hpp @@ -34,9 +34,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** This is the main interface for an ANTLR3 parser. */ @@ -161,17 +159,15 @@ class RuleReturnValue public: typedef typename ImplTraits::BaseParserType BaseParserType; typedef typename ImplTraits::CommonTokenType CommonTokenType; - -public: + const CommonTokenType* start; const CommonTokenType* stop; - BaseParserType* parser; RuleReturnValue(BaseParserType* psr = NULL ); RuleReturnValue( const RuleReturnValue& val ); RuleReturnValue& operator=( const RuleReturnValue& val ); - void call_start_placeholder(); - void call_stop_placeholder(); + void call_start_placeholder(BaseParserType*); + void call_stop_placeholder(BaseParserType*); RuleReturnValue& get_struct(); ~RuleReturnValue(); }; @@ -185,15 +181,16 @@ class RuleReturnValue_1 : public RuleReturnValue typedef RuleReturnValue BaseType; typedef typename BaseType::BaseParserType BaseParserType; -public: + BaseParserType* parser; + RuleReturnValue_1(); RuleReturnValue_1( BaseParserType* psr); RuleReturnValue_1( const RuleReturnValue_1& val ); - void call_start_placeholder(); //its dummy here + void call_start_placeholder(BaseParserType*); //its dummy here ~RuleReturnValue_1(); }; -ANTLR_END_NAMESPACE() +} #include "antlr3parser.inl" diff --git a/runtime/Cpp/include/antlr3parser.inl b/runtime/Cpp/include/antlr3parser.inl old mode 100755 new mode 100644 index 6f4d15261..d1b099a2b --- a/runtime/Cpp/include/antlr3parser.inl +++ b/runtime/Cpp/include/antlr3parser.inl @@ -1,8 +1,8 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template< class ImplTraits > Parser::Parser( ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state ) - :RecognizerType( sizeHint, state ) + : RecognizerType( sizeHint, state ) { m_tstream = NULL; } @@ -10,7 +10,7 @@ Parser::Parser( ANTLR_UINT32 sizeHint, RecognizerSharedStateType* st template< class ImplTraits > Parser::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream, RecognizerSharedStateType* state ) - :RecognizerType( sizeHint, state ) + : RecognizerType( sizeHint, state ) { this->setTokenStream( tstream ); } @@ -19,7 +19,7 @@ template< class ImplTraits > Parser::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream, DebugEventListenerType* dbg, RecognizerSharedStateType* state ) - :RecognizerType( sizeHint, state ) + : RecognizerType( sizeHint, state ) { this->setTokenStream( tstream ); this->setDebugListener( dbg ); @@ -52,7 +52,7 @@ ANTLR_INLINE typename Parser::TokenStreamType* Parser::g template< class ImplTraits > void Parser::fillExceptionData( ExceptionBaseType* ex ) { - ex->set_token( m_tstream->_LT(1) ); /* Current input token */ + ex->set_token( new CommonTokenType(*(m_tstream->LT(1))) ); /* Current input token (clonned) - held by the exception */ ex->set_line( ex->get_token()->get_line() ); ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() ); ex->set_index( this->get_istream()->index() ); @@ -99,7 +99,7 @@ void Parser::displayRecognitionError( ANTLR_UINT8** tokenNames, Exce // Prepare the knowledge we know we have // const CommonTokenType* theToken = this->get_rec()->get_state()->get_exception()->get_token(); - StringType ttext = theToken->toString(); + StringType ttext = theToken->toString(); errtext << ", at offset , " << this->get_rec()->get_state()->get_exception()->get_charPositionInLine(); @@ -193,25 +193,20 @@ typename Parser::TokenType* Parser::getMissingSymbol( In ANTLR_UINT32 expectedTokenType, BitsetListType* ) { - TokenStreamType* cts; - CommonTokenType* token; - const CommonTokenType* current; - StringType text; - // Dereference the standard pointers // - cts = static_cast(istream); + TokenStreamType *cts = static_cast(istream); // Work out what to use as the current symbol to make a line and offset etc // If we are at EOF, we use the token before EOF // - current = cts->_LT(1); + const CommonTokenType* current = cts->LT(1); if (current->get_type() == CommonTokenType::TOKEN_EOF) { - current = cts->_LT(-1); + current = cts->LT(-1); } - token = new CommonTokenType; + CommonTokenType* token = new CommonTokenType; // Set some of the token properties based on the current token // @@ -219,19 +214,18 @@ typename Parser::TokenType* Parser::getMissingSymbol( In token->set_charPositionInLine( current->get_charPositionInLine()); token->set_channel( TOKEN_DEFAULT_CHANNEL ); token->set_type(expectedTokenType); - token->set_lineStart( current->get_lineStart() ); - + token->set_lineStart( current->get_lineStart() ); + // Create the token text that shows it has been inserted // - token->setText("getText(); - - if (!text.empty()) + if ( expectedTokenType == CommonTokenType::TOKEN_EOF ) { - text.append((const char *) this->get_rec()->get_state()->get_tokenName(expectedTokenType) ); - text.append(">"); + token->setText( "" ); + } else { + typename ImplTraits::StringStreamType text; + text << "get_rec()->get_state()->get_tokenName(expectedTokenType) << ">"; + token->setText( text.str().c_str() ); } - // Finally return the pointer to our new token // return token; @@ -368,13 +362,13 @@ ANTLR_INLINE void Parser::preporterror() template< class ImplTraits> ANTLR_INLINE ANTLR_UINT32 Parser::LA(ANTLR_INT32 i) { - return this->get_istream()->_LA(i); + return this->get_istream()->LA(i); } template< class ImplTraits> ANTLR_INLINE const typename Parser::CommonTokenType* Parser::LT(ANTLR_INT32 k) { - return this->get_input()->_LT(k); + return this->get_input()->LT(k); } template< class ImplTraits> @@ -499,8 +493,7 @@ ANTLR_INLINE typename Parser::TokenStreamType* Parser::g template< class ImplTraits> ANTLR_INLINE RuleReturnValue::RuleReturnValue(BaseParserType* psr) -{ - parser = psr; +{ start = NULL; stop = NULL; } @@ -508,7 +501,6 @@ ANTLR_INLINE RuleReturnValue::RuleReturnValue(BaseParserType* psr) template< class ImplTraits> ANTLR_INLINE RuleReturnValue::RuleReturnValue( const RuleReturnValue& val ) { - parser = val.parser; start = val.start; stop = val.stop; } @@ -516,7 +508,6 @@ ANTLR_INLINE RuleReturnValue::RuleReturnValue( const RuleReturnValue template< class ImplTraits> ANTLR_INLINE RuleReturnValue& RuleReturnValue::operator=( const RuleReturnValue& val ) { - parser = val.parser; start = val.start; stop = val.stop; return *this; @@ -528,26 +519,28 @@ ANTLR_INLINE RuleReturnValue::~RuleReturnValue() } template< class ImplTraits> -ANTLR_INLINE void RuleReturnValue::call_start_placeholder() +ANTLR_INLINE void RuleReturnValue::call_start_placeholder(BaseParserType *parser) { start = parser->LT(1); stop = start; } template< class ImplTraits> -ANTLR_INLINE void RuleReturnValue::call_stop_placeholder() +ANTLR_INLINE void RuleReturnValue::call_stop_placeholder(BaseParserType *parser) { stop = parser->LT(-1); } template< class ImplTraits> ANTLR_INLINE RuleReturnValue_1::RuleReturnValue_1() + : parser() { } template< class ImplTraits> RuleReturnValue_1::RuleReturnValue_1( BaseParserType* psr ) - :RuleReturnValue_1::BaseType(psr) + : RuleReturnValue_1::BaseType(psr) + , parser(psr) { BaseType::start = psr->LT(1); BaseType::stop = BaseType::start; @@ -555,19 +548,20 @@ RuleReturnValue_1::RuleReturnValue_1( BaseParserType* psr ) template< class ImplTraits> RuleReturnValue_1::RuleReturnValue_1( const RuleReturnValue_1& val ) - :BaseType(val) + : RuleReturnValue_1::BaseType(val) + , parser(val.parser) { } template< class ImplTraits> -void RuleReturnValue_1::call_start_placeholder() +void RuleReturnValue_1::call_start_placeholder(BaseParserType*) { } template< class ImplTraits> RuleReturnValue_1::~RuleReturnValue_1() { - if( BaseType::parser && ( BaseType::parser->get_backtracking() == 0 ) ) + if( parser && parser->get_backtracking() == 0 ) { if( BaseType::stop == NULL ) BaseType::stop = BaseType::parser->LT(-1); @@ -577,9 +571,9 @@ RuleReturnValue_1::~RuleReturnValue_1() ANTLR_MARKER stop_token_idx = BaseType::stop->get_index() - 1; if( start_token_idx > stop_token_idx ) return; - BaseType::parser->getTokenStream()->discardTokens( start_token_idx, stop_token_idx); + parser->getTokenStream()->discardTokens( start_token_idx, stop_token_idx); } } } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3recognizersharedstate.hpp b/runtime/Cpp/include/antlr3recognizersharedstate.hpp old mode 100755 new mode 100644 index 05543963d..fc25d696e --- a/runtime/Cpp/include/antlr3recognizersharedstate.hpp +++ b/runtime/Cpp/include/antlr3recognizersharedstate.hpp @@ -42,9 +42,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { /** All the data elements required to track the current state * of any recognizer (lexer, parser, tree parser). @@ -58,7 +56,7 @@ class RecognizerSharedState : public ImplTraits::AllocPolicyType typedef typename ImplTraits::AllocPolicyType AllocPolicyType; typedef typename StreamType::UnitType TokenType; typedef typename ImplTraits::CommonTokenType CommonTokenType; - + typedef typename ComponentTypeFinder::ComponentType ComponentType; typedef typename ImplTraits::template RewriteStreamType< ComponentType > RewriteStreamType; typedef typename ImplTraits::StringType StringType; @@ -67,6 +65,8 @@ class RecognizerSharedState : public ImplTraits::AllocPolicyType typedef typename ImplTraits::BitsetType BitsetType; typedef typename ImplTraits::BitsetListType BitsetListType; + typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; + typedef typename AllocPolicyType::template StackType< BitsetListType > FollowingType; typedef typename AllocPolicyType::template StackType< typename ImplTraits::InputStreamType* > InputStreamsType; typedef InputStreamsType StreamsType; @@ -204,6 +204,11 @@ class RecognizerSharedState : public ImplTraits::AllocPolicyType */ InputStreamsType m_streams; + /** Tree adaptor drives an AST trie construction. + * Is shared between multiple imported grammars. + */ + TreeAdaptorType* m_treeAdaptor; + public: RecognizerSharedState(); ExceptionBaseType* get_exception() const; @@ -228,7 +233,8 @@ class RecognizerSharedState : public ImplTraits::AllocPolicyType ANTLR_MARKER get_tokenStartCharIndex() const; StringType& get_text(); InputStreamsType& get_streams(); - + TreeAdaptorType* get_treeAdaptor() const; + void set_following( const FollowingType& following ); void set_sizeHint( ANTLR_UINT32 sizeHint ); void set_error( bool error ); @@ -250,13 +256,14 @@ class RecognizerSharedState : public ImplTraits::AllocPolicyType void set_tokenStartCharIndex( ANTLR_MARKER tokenStartCharIndex ); void set_text( const StringType& text ); void set_streams( const InputStreamsType& streams ); - + void set_treeAdaptor( TreeAdaptorType* adaptor ); + void inc_errorCount(); void inc_backtracking(); void dec_backtracking(); }; -ANTLR_END_NAMESPACE() +} #include "antlr3recognizersharedstate.inl" diff --git a/runtime/Cpp/include/antlr3recognizersharedstate.inl b/runtime/Cpp/include/antlr3recognizersharedstate.inl old mode 100755 new mode 100644 index 8d9cc7fd6..2357420cb --- a/runtime/Cpp/include/antlr3recognizersharedstate.inl +++ b/runtime/Cpp/include/antlr3recognizersharedstate.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template RecognizerSharedState::RecognizerSharedState() @@ -19,6 +19,7 @@ RecognizerSharedState::RecognizerSharedState() m_tokenStartLine = 0; m_tokenStartCharPositionInLine = 0; m_tokenStartCharIndex = 0; + m_treeAdaptor = NULL; } template @@ -135,6 +136,12 @@ ANTLR_INLINE typename RecognizerSharedState::StreamsType { return m_streams; } +template +ANTLR_INLINE typename RecognizerSharedState::TreeAdaptorType* RecognizerSharedState::get_treeAdaptor() const +{ + return m_treeAdaptor; +} + template ANTLR_INLINE void RecognizerSharedState::set_exception( ExceptionBaseType* exception ) { @@ -245,6 +252,11 @@ ANTLR_INLINE void RecognizerSharedState::set_streams( co { m_streams = streams; } +template +ANTLR_INLINE void RecognizerSharedState::set_treeAdaptor( TreeAdaptorType* adaptor ) +{ + m_treeAdaptor = adaptor; +} template ANTLR_INLINE void RecognizerSharedState::inc_errorCount() @@ -264,4 +276,4 @@ ANTLR_INLINE void RecognizerSharedState::dec_backtrackin --m_backtracking; } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3rewriterulesubtreestream.hpp b/runtime/Cpp/include/antlr3rewriterulesubtreestream.hpp new file mode 100644 index 000000000..db92ee0db --- /dev/null +++ b/runtime/Cpp/include/antlr3rewriterulesubtreestream.hpp @@ -0,0 +1,123 @@ +#ifndef ANTLR3REWRITERULESUBTREESTREAM_HPP +#define ANTLR3REWRITERULESUBTREESTREAM_HPP + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB + +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The name of the author may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace antlr3 { + +/// This is an implementation of a subtree stream which is a set of trees +/// modeled as an element stream. +/// +template +class RewriteRuleSubtreeStream +{ +public: + typedef typename ImplTraits::StringType StringType; + typedef typename ImplTraits::AllocPolicyType AllocPolicyType; + typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; + typedef typename ImplTraits::TreeParserType ComponentType; + typedef typename ComponentType::StreamType StreamType; + typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; + typedef TreeType TokenType; + typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType; + typedef typename AllocPolicyType::template VectorType< TreeTypePtr > ElementsType; + typedef typename ImplTraits::TreeType ElementType; + + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeType* oneElement); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeTypePtr& oneElement); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); + ~RewriteRuleSubtreeStream(); + + /// Reset the condition of this stream so that it appears we have + /// not consumed any of its elements. Elements themselves are untouched. + /// + void reset(); + + TreeTypePtr nextNode(); + + /// TODO copied from RewriteRuleElementStreamType + /// Add a new pANTLR3_BASE_TREE to this stream + /// + void add(TreeTypePtr& el); + bool hasNext(); + TreeTypePtr nextTree(); + typename ElementsType::iterator _next(); + ElementType* toTree(ElementType* el); + + /// Number of elements available in the stream + /// + ANTLR_UINT32 size(); + + /// Returns the description string if there is one available (check for NULL). + /// + StringType getDescription(); + +protected: + TreeTypePtr dup(const TreeTypePtr& el ); + TreeTypePtr dup(const TreeType* el ); + + TreeTypePtr& leftestNode(TreeTypePtr& node) const; +private: + /// Pointer to the tree adaptor in use for this stream + /// + TreeAdaptorType* m_adaptor; + + /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(), + /// which bumps it to 1 meaning no more elements. + /// + typename ElementsType::iterator m_cursor; + + /// The element or stream description; usually has name of the token or + /// rule reference that this list tracks. Can include rulename too, but + /// the exception would track that info. + /// + StringType m_elementDescription; + + /// The list of tokens or subtrees we are tracking + /// + ElementsType m_elements; + + TreeTypePtr dupTree(const TreeTypePtr& el ); + TreeTypePtr dupTree(const TreeType* el ); + + /// Once a node / subtree has been used in a stream, it must be dup'ed + /// from then on. Streams are reset after sub rules so that the streams + /// can be reused in future sub rules. So, reset must set a dirty bit. + /// If dirty, then next() always returns a dup. + /// + bool m_dirty; +}; + +} + +#include "antlr3rewriterulesubtreestream.inl" + +#endif diff --git a/runtime/Cpp/include/antlr3rewriterulesubtreestream.inl b/runtime/Cpp/include/antlr3rewriterulesubtreestream.inl new file mode 100644 index 000000000..ea1c55017 --- /dev/null +++ b/runtime/Cpp/include/antlr3rewriterulesubtreestream.inl @@ -0,0 +1,244 @@ +namespace antlr3 { + +template +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description) + : m_adaptor(adaptor) + , m_elementDescription(description) + , m_dirty(false) +{ + m_cursor = m_elements.begin(); +} + +template +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, + TreeType* oneElement + ) + : m_adaptor(adaptor) + , m_elementDescription(description) + , m_dirty(false) +{ + if( oneElement != NULL ) + { + auto tree_clone = this->dup(oneElement); + this->add( tree_clone ); + } + m_cursor = m_elements.begin(); +} + +template +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, + TreeTypePtr& oneElement + ) + : m_adaptor(adaptor) + , m_elementDescription(description) + , m_dirty(false) +{ + if( oneElement != NULL ) + this->add( oneElement ); + m_cursor = m_elements.begin(); +} + +template +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, + const ElementsType& elements + ) + : m_adaptor(adaptor) + , m_elementDescription(description) + , m_dirty(false) + , m_elements(elements) +{ + m_cursor = m_elements.begin(); +} + +template +void +RewriteRuleSubtreeStream::reset() +{ + m_cursor = m_elements.begin(); + m_dirty = true; +} + +template +void +RewriteRuleSubtreeStream::add(TreeTypePtr& el) +{ + if ( el == NULL ) + return; + + m_elements.push_back(std::move(el)); + m_cursor = m_elements.begin(); +} + +template +typename RewriteRuleSubtreeStream::ElementsType::iterator +RewriteRuleSubtreeStream::_next() +{ + if (m_elements.empty()) + { + // This means that the stream is empty + // Caller must cope with this (TODO throw RewriteEmptyStreamException) + return m_elements.end(); + } + + if (m_dirty || m_cursor == m_elements.end()) + { + if( m_elements.size() == 1) + { + // Special case when size is single element, it will just dup a lot + return m_elements.begin(); + } + + // Out of elements and the size is not 1, so we cannot assume + // that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+) + // This means we ran out of elements earlier than was expected. + // + return m_elements.end(); // Caller must cope with this (TODO throw RewriteEmptyStreamException) + } + + // More than just a single element so we extract it from the + // vector. + return m_cursor++; +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::nextTree() +{ + if ( m_dirty || ( m_cursor == m_elements.end() && m_elements.size() == 1 )) + { + // if out of elements and size is 1, dup + typename ElementsType::iterator el = this->_next(); + return this->dup(*el); + } + + // test size above then fetch + typename ElementsType::iterator el = this->_next(); + return std::move(*el); +} + +/* +template +typename RewriteRuleSubtreeStream::TokenType* +RewriteRuleSubtreeStream::nextToken() +{ + return this->_next(); +} + +template +typename RewriteRuleSubtreeStream::TokenType* +RewriteRuleSubtreeStream::next() +{ + ANTLR_UINT32 s; + s = this->size(); + if ( (m_cursor >= s) && (s == 1) ) + { + TreeTypePtr el; + el = this->_next(); + return this->dup(el); + } + return this->_next(); +} + +*/ + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dup(const TreeTypePtr& element) +{ + return this->dupTree(element); +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dup(const TreeType* element) +{ + return std::move(this->dupTree(element)); +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dupTree(const TreeTypePtr& element) +{ + return std::move(m_adaptor->dupTree(element)); +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dupTree(const TreeType* element) +{ + return std::move(m_adaptor->dupTree(element)); +} + +template +typename RewriteRuleSubtreeStream::ElementType* +RewriteRuleSubtreeStream::toTree( ElementType* element) +{ + return element; +} + +template +bool RewriteRuleSubtreeStream::hasNext() +{ + return m_cursor != m_elements.end(); +} + +/// Number of elements available in the stream +/// +template +ANTLR_UINT32 RewriteRuleSubtreeStream::size() +{ + return (ANTLR_UINT32)(m_elements.size()); +} + +template +typename RewriteRuleSubtreeStream::StringType +RewriteRuleSubtreeStream::getDescription() +{ + if ( m_elementDescription.empty() ) + { + m_elementDescription = ""; + } + return m_elementDescription; +} + +template +RewriteRuleSubtreeStream::~RewriteRuleSubtreeStream() +{ + // Before placing the stream back in the pool, we + // need to clear any vector it has. + m_elements.clear(); +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::nextNode() +{ + //System.out.println("nextNode: elements="+elements+", singleElement="+((Tree)singleElement).toStringTree()); + ANTLR_UINT32 n = this->size(); + if (m_dirty || (m_cursor == m_elements.end() && m_elements.size() == 1)) { + // if out of elements and size is 1, dup (at most a single node + // since this is for making root nodes). + typename ElementsType::iterator el = this->_next(); + return m_adaptor->dupNode(*el); + } + + typename ElementsType::iterator el = this->_next(); + //while (m_adaptor->isNilNode(el) && m_adaptor->getChildCount(el) == 1) + // tree = m_adaptor->getChild(tree, 0); + TreeTypePtr& node = leftestNode(*el); + //System.out.println("_next="+((Tree)tree).toStringTree()); + return m_adaptor->dupNode(node); // dup just the root (want node here) +} + +template +ANTLR_INLINE +typename RewriteRuleSubtreeStream::TreeTypePtr& +RewriteRuleSubtreeStream::leftestNode(TreeTypePtr& node) const +{ + if(m_adaptor->isNilNode(node) && m_adaptor->getChildCount(node) == 1) + return leftestNode(node->getChild(0)); + else + return node; +} + +} diff --git a/runtime/Cpp/include/antlr3rewriteruletokenstream.hpp b/runtime/Cpp/include/antlr3rewriteruletokenstream.hpp new file mode 100644 index 000000000..c982b0a2f --- /dev/null +++ b/runtime/Cpp/include/antlr3rewriteruletokenstream.hpp @@ -0,0 +1,131 @@ +#ifndef ANTLR3REWRITESTREAM_HPP +#define ANTLR3REWRITESTREAM_HPP + +// [The "BSD licence"] +// Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB + +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The name of the author may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace antlr3 { + +/// This is an implementation of a token stream, which is basically an element +/// stream that deals with tokens only. +/// +template +class RewriteRuleTokenStream +{ +public: + typedef typename ImplTraits::StringType StringType; + typedef typename ImplTraits::AllocPolicyType AllocPolicyType; + typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; + typedef typename ImplTraits::ParserType ComponentType; + typedef typename ComponentType::StreamType StreamType; + typedef typename ImplTraits::CommonTokenType TokenType; + typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; + typedef typename AllocPolicyType::template VectorType ElementsType; + typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType; + typedef typename ImplTraits::CommonTokenType ElementType; +public: + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const TokenType* oneElement); + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); + ~RewriteRuleTokenStream(); + + /// Reset the condition of this stream so that it appears we have + /// not consumed any of its elements. Elements themselves are untouched. + /// + void reset(); + + TreeTypePtr nextNode(); + const TokenType* nextToken(); + + /// TODO copied from RewriteRuleElementStreamType + /// Add a new pANTLR3_BASE_TREE to this stream + /// + void add(const ElementType* el); + + /// When constructing trees, sometimes we need to dup a token or AST + /// subtree. Dup'ing a token means just creating another AST node + /// around it. For trees, you must call the adaptor.dupTree(). + /// + ElementType* dup( ElementType* el ); + + /// Ensure stream emits trees; tokens must be converted to AST nodes. + /// AST nodes can be passed through unmolested. + /// + TreeTypePtr toTree(const ElementType* el); + + /// Pointer to the tree adaptor in use for this stream + /// + TreeAdaptorType* m_adaptor; + ElementType nextTree(); + typename ElementsType::iterator _next(); + + /// Returns true if there is a next element available + /// + bool hasNext(); + + /// Number of elements available in the stream + /// + ANTLR_UINT32 size(); + + /// Returns the description string if there is one available (check for NULL). + /// + StringType getDescription(); + +private: + ElementType* dupImpl(typename ImplTraits::CommonTokenType* el); + ElementType* dupImpl(typename ImplTraits::TreeTypePtr el); + + /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(), + /// which bumps it to 1 meaning no more elements. + /// + typename ElementsType::iterator m_cursor; + + /// The element or stream description; usually has name of the token or + /// rule reference that this list tracks. Can include rulename too, but + /// the exception would track that info. + /// + StringType m_elementDescription; + + /// The list of tokens or subtrees we are tracking + /// + ElementsType m_elements; + + /// Once a node / subtree has been used in a stream, it must be dup'ed + /// from then on. Streams are reset after sub rules so that the streams + /// can be reused in future sub rules. So, reset must set a dirty bit. + /// If dirty, then next() always returns a dup. + /// + bool m_dirty; +}; + +} + +#include "antlr3rewriteruletokenstream.inl" + +#endif diff --git a/runtime/Cpp/include/antlr3rewriteruletokenstream.inl b/runtime/Cpp/include/antlr3rewriteruletokenstream.inl new file mode 100644 index 000000000..b8d48a889 --- /dev/null +++ b/runtime/Cpp/include/antlr3rewriteruletokenstream.inl @@ -0,0 +1,224 @@ +namespace antlr3 { + +template +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description) + : m_adaptor(adaptor) + , m_elements() + , m_dirty(false) +{ + m_cursor = m_elements.begin(); +} + +template +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, + const TokenType* oneElement + ) + : m_adaptor(adaptor) + , m_elements() + , m_dirty(false) +{ + if( oneElement != NULL ) + this->add( oneElement ); + m_cursor = m_elements.begin(); +} + +template +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, + const ElementsType& elements + ) + : m_adaptor(adaptor) + , m_elements(elements) + , m_dirty(false) +{ + m_cursor = m_elements.begin(); +} + +template +void RewriteRuleTokenStream::reset() +{ + m_cursor = m_elements.begin(); + m_dirty = true; +} + +template +void RewriteRuleTokenStream::add(const ElementType* el) +{ + if ( el == NULL) + return; + m_elements.push_back(el); + m_cursor = m_elements.begin(); +} + +template +typename RewriteRuleTokenStream::ElementsType::iterator +RewriteRuleTokenStream::_next() +{ + if (m_elements.empty()) + { + // This means that the stream is empty + // Caller must cope with this (TODO throw RewriteEmptyStreamException) + return m_elements.end(); + } + + if (m_dirty || m_cursor == m_elements.end()) + { + if( m_elements.size() == 1) + { + // Special case when size is single element, it will just dup a lot + //return this->toTree(m_singleElement); + return m_elements.begin(); + } + + // Out of elements and the size is not 1, so we cannot assume + // that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+) + // This means we ran out of elements earlier than was expected. + // + return m_elements.end(); // Caller must cope with this (TODO throw RewriteEmptyStreamException) + } + + // More than just a single element so we extract it from the + // vector. + return m_cursor++; +} + +template +typename RewriteRuleTokenStream::ElementType +RewriteRuleTokenStream::nextTree() +{ + ANTLR_UINT32 n = this->size(); + if ( m_dirty || ( (m_cursor >=n) && (n==1)) ) + { + // if out of elements and size is 1, dup + typename ElementsType::iterator el = this->_next(); + return this->dup(*el); + } + + // test size above then fetch + typename ElementsType::iterator el = this->_next(); + return *el; +} + +/* +template +typename RewriteRuleTokenStream::TokenType* +RewriteRuleTokenStream::nextToken() +{ + return this->_next(); +} + +template +typename RewriteRuleTokenStream::TokenType* +RewriteRuleTokenStream::next() +{ + ANTLR_UINT32 s; + s = this->size(); + if ( (m_cursor >= s) && (s == 1) ) + { + TreeTypePtr el; + el = this->_next(); + return this->dup(el); + } + return this->_next(); +} + +*/ + +template +typename RewriteRuleTokenStream::ElementType* +RewriteRuleTokenStream::dup( ElementType* element) +{ + return dupImpl(element); +} + +template +typename RewriteRuleTokenStream::ElementType* +RewriteRuleTokenStream::dupImpl( typename ImplTraits::CommonTokenType* element) +{ + return NULL; // TODO throw here +} + +template +typename RewriteRuleTokenStream::ElementType* +RewriteRuleTokenStream::dupImpl( typename ImplTraits::TreeTypePtr element) +{ + return m_adaptor->dupTree(element); +} + +template +typename RewriteRuleTokenStream::TreeTypePtr +RewriteRuleTokenStream::toTree(const ElementType* element) +{ + return m_adaptor->create(element); +} + +template +bool +RewriteRuleTokenStream::hasNext() +{ + return m_cursor != m_elements.end(); +} + +template +typename RewriteRuleTokenStream::TreeTypePtr +RewriteRuleTokenStream::nextNode() +{ + const TokenType *Token = this->nextToken(); + return m_adaptor->create(Token); +} + +/// Number of elements available in the stream +/// +template +ANTLR_UINT32 RewriteRuleTokenStream::size() +{ + return (ANTLR_UINT32)(m_elements.size()); +} + +template +typename RewriteRuleTokenStream::StringType +RewriteRuleTokenStream::getDescription() +{ + if ( m_elementDescription.empty() ) + { + m_elementDescription = ""; + } + return m_elementDescription; +} + +template +RewriteRuleTokenStream::~RewriteRuleTokenStream() +{ + // Before placing the stream back in the pool, we + // need to clear any vector it has. This is so any + // free pointers that are associated with the + // entries are called. However, if this particular function is called + // then we know that the entries in the stream are definitely + // tree nodes. Hence we check to see if any of them were nilNodes as + // if they were, we can reuse them. + // + // We have some elements to traverse + // + for (ANTLR_UINT32 i = 0; i < m_elements.size(); i++) + { + const ElementType *tree = m_elements.at(i); + //if ( (tree != NULL) && tree->isNilNode() ) + { + // Had to remove this for now, check is not comprehensive enough + // tree->reuse(tree); + } + } + m_elements.clear(); +} + +template +const typename RewriteRuleTokenStream::TokenType* +RewriteRuleTokenStream::nextToken() +{ + auto retval = this->_next(); + if (retval == m_elements.end()) + return NULL; + else + return *retval; +} + +} diff --git a/runtime/Cpp/include/antlr3rewritestreams.hpp b/runtime/Cpp/include/antlr3rewritestreams.hpp old mode 100755 new mode 100644 index e89d70a44..a8d739664 --- a/runtime/Cpp/include/antlr3rewritestreams.hpp +++ b/runtime/Cpp/include/antlr3rewritestreams.hpp @@ -29,8 +29,6 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - /// A generic list of elements tracked in an alternative to be used in /// a -> rewrite rule. /// @@ -51,27 +49,24 @@ /// /// TODO: add mechanism to detect/puke on modification after reading from stream /// -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { -template -class RewriteRuleElementStream : public ImplTraits::AllocPolicyType +template +//template +class RewriteRuleElementStream : public ImplTraits::AllocPolicyType { public: - typedef typename ImplTraits::TreeType TreeType; + //typedef typename ElementTypePtr::element_type ElementType; unique_ptr + //typedef typename ImplTraits::TreeType TreeType; typedef typename ImplTraits::AllocPolicyType AllocPolicyType; typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType; - typedef typename ImplTraits::template RecognizerType< typename SuperType::StreamType > RecognizerType; + //typedef typename ImplTraits::template RecognizerType< typename SuperType::StreamType > RecognizerType; typedef typename ImplTraits::StringType StringType; - typedef typename SuperType::TokenType TokenType; - typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType; + typedef typename AllocPolicyType::template VectorType< ElementType* > ElementsType; protected: - /// Track single elements w/o creating a list. Upon 2nd add, alloc list - /// - TokenType* m_singleElement; - - /// The list of tokens or subtrees we are tracking + /// The list of tokens or subtrees we are tracking /// ElementsType m_elements; @@ -79,63 +74,64 @@ class RewriteRuleElementStream : public ImplTraits::AllocPolicyType /// rule reference that this list tracks. Can include rulename too, but /// the exception would track that info. /// - StringType m_elementDescription; + StringType m_elementDescription; + +private: + ElementType* dupImpl(typename ImplTraits::CommonTokenType* el); + ElementType* dupImpl(typename ImplTraits::TreeTypePtr el); + /// Pointer to the tree adaptor in use for this stream /// TreeAdaptorType* m_adaptor; - // Pointer to the recognizer shared state to which this stream belongs - // - RecognizerType* m_rec; - /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(), /// which bumps it to 1 meaning no more elements. /// - ANTLR_UINT32 m_cursor; + ANTLR_UINT32 m_cursor; /// Once a node / subtree has been used in a stream, it must be dup'ed /// from then on. Streams are reset after sub rules so that the streams /// can be reused in future sub rules. So, reset must set a dirty bit. /// If dirty, then next() always returns a dup. /// - bool m_dirty; + bool m_dirty; public: - RewriteRuleElementStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description); - RewriteRuleElementStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement); - RewriteRuleElementStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements); + RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description, const ElementType* oneElement); + RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); ~RewriteRuleElementStream(); - // Methods + // Methods /// Reset the condition of this stream so that it appears we have /// not consumed any of its elements. Elements themselves are untouched. /// - void reset(); + void reset(); /// Add a new pANTLR3_BASE_TREE to this stream /// - void add(TokenType* el); + void add(ElementType* el); /// Return the next element in the stream. If out of elements, throw /// an exception unless size()==1. If size is 1, then return elements[0]. /// - TokenType* next(); - TreeType* nextTree(); - TokenType* nextToken(); - TokenType* _next(); + //TokenType* next(); + ElementType nextTree(); + //TokenType* nextToken(); + ElementType* _next(); /// When constructing trees, sometimes we need to dup a token or AST /// subtree. Dup'ing a token means just creating another AST node /// around it. For trees, you must call the adaptor.dupTree(). /// - TokenType* dup( TokenType* el ); + ElementType* dup( ElementType* el ); /// Ensure stream emits trees; tokens must be converted to AST nodes. /// AST nodes can be passed through unmolested. /// - TreeType* toTree(TreeType* el); + ElementType* toTree(ElementType* el); /// Returns true if there is a next element available /// @@ -150,7 +146,7 @@ class RewriteRuleElementStream : public ImplTraits::AllocPolicyType /// Referencing to a rule result twice is ok; dup entire tree as /// we can't be adding trees; e.g., expr expr. /// - TreeType* nextNode(); + //TreeTypePtr nextNode(); /// Number of elements available in the stream /// @@ -161,14 +157,16 @@ class RewriteRuleElementStream : public ImplTraits::AllocPolicyType StringType getDescription(); protected: - void init(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description); + void init(TreeAdaptorType* adaptor, const char* description); }; /// This is an implementation of a token stream, which is basically an element /// stream that deals with tokens only. /// template -class RewriteRuleTokenStream : public ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::ParserType> +//class RewriteRuleTokenStream : public ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::ParserType> +class RewriteRuleTokenStream + //: public ImplTraits::template RewriteStreamType< const typename ImplTraits::CommonTokenType > { public: typedef typename ImplTraits::AllocPolicyType AllocPolicyType; @@ -177,25 +175,40 @@ class RewriteRuleTokenStream : public ImplTraits::template RewriteRuleElementStr typedef typename ComponentType::StreamType StreamType; typedef typename ImplTraits::CommonTokenType TokenType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType; typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType; - typedef typename ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::ParserType> BaseType; + typedef typename ImplTraits::template RewriteStreamType< const typename ImplTraits::CommonTokenType > BaseType; public: - RewriteRuleTokenStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description); - RewriteRuleTokenStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement); - RewriteRuleTokenStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements); - TreeType* nextNode(); + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const TokenType* oneElement); + RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); + + TreeTypePtr nextNode(); + TokenType* nextToken(); + + /// TODO copied from RewriteRuleElementStreamType + /// Add a new pANTLR3_BASE_TREE to this stream + /// + typedef typename ImplTraits::CommonTokenType ElementType; + void add(const ElementType* el); + /// Pointer to the tree adaptor in use for this stream + /// + TreeAdaptorType* m_adaptor; + ElementType* _next(); private: - TreeType* nextNodeToken(); + //TreeTypePtr nextNodeToken(); }; /// This is an implementation of a subtree stream which is a set of trees -/// modelled as an element stream. +/// modeled as an element stream. /// template -class RewriteRuleSubtreeStream : public ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeParserType> +//class RewriteRuleSubtreeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeParserType> +class RewriteRuleSubtreeStream + //: public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType > { public: typedef typename ImplTraits::AllocPolicyType AllocPolicyType; @@ -203,27 +216,58 @@ class RewriteRuleSubtreeStream : public ImplTraits::template RewriteRuleElementS typedef typename ImplTraits::TreeParserType ComponentType; typedef typename ComponentType::StreamType StreamType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef TreeType TokenType; typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType; typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType; - typedef typename ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeParserType> BaseType; + typedef typename ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType > BaseType; -public: - RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description); - RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement); - RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeTypePtr& oneElement); + RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); + + TreeTypePtr nextNode(TreeTypePtr); + + /// TODO copied from RewriteRuleElementStreamType + /// Add a new pANTLR3_BASE_TREE to this stream + /// + void add(TreeTypePtr& el); + bool hasNext(); + TreeTypePtr& nextTree(); + void reset(); - TreeType* dup( TreeType* el ); +protected: + TreeTypePtr dup( TreeTypePtr el ); private: - TreeType* dupTree( TreeType* el ); + TreeTypePtr dupTree( TreeTypePtr el ); }; +/* TODO This class is probably used in TreeParser only + * Notes about Java target + * - these classes reimplement only dup and toTree methods: + * base ElementStr + * abstract dup + * toTree(Object e) { return e; } + * TokenStr + * dup { throw } + * toTree(Object e) { return e; } + * SubTreeStr + * dup(Object e) { return adaptor.dupTree } + * NodeStr + * dup { throw } + * toTree(Object e) { return adaptor.dupNode } + * See: RewriteRuleElementStream::dup, RewriteRuleElementStream::dupImpl + * + * There should 3 types of specializations for RewriteRuleElementStreamType (which is not defined yet) + * ATM: RewriteRuleElementStreamType is replaced with ImplTraits::template RewriteStreamType + * /// This is an implementation of a node stream, which is basically an element /// stream that deals with tree nodes only. /// template -class RewriteRuleNodeStream : public ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeParserType> +//class RewriteRuleNodeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeParserType> +class RewriteRuleNodeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType > { public: typedef typename ImplTraits::AllocPolicyType AllocPolicyType; @@ -234,20 +278,21 @@ class RewriteRuleNodeStream : public ImplTraits::template RewriteRuleElementStre typedef TreeType TokenType; typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType; typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType; - typedef typename ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeParserType> BaseType; + typedef typename ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeType > BaseType; public: - RewriteRuleNodeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description); - RewriteRuleNodeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement); - RewriteRuleNodeStream(TreeAdaptorType* adaptor, RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements); + RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description); + RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description, TokenType* oneElement); + RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements); - TreeType* toTree(TreeType* element); +protected: + TreeTypePtr toTree(TreeTypePtr element); private: - TreeType* toTreeNode(TreeType* element); + TreeTypePtr toTreeNode(TreeTypePtr element); }; - -ANTLR_END_NAMESPACE() +*/ +} #include "antlr3rewritestreams.inl" diff --git a/runtime/Cpp/include/antlr3rewritestreams.inl b/runtime/Cpp/include/antlr3rewritestreams.inl old mode 100755 new mode 100644 index 3303e8c41..47568da64 --- a/runtime/Cpp/include/antlr3rewritestreams.inl +++ b/runtime/Cpp/include/antlr3rewritestreams.inl @@ -1,271 +1,266 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { -template -RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description) +template +RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, + const char* description) { - this->init(adaptor, rec, description); + this->init(adaptor, description); } -template -RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description, TokenType* oneElement) +template +RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, + const char* description, + const ElementType* oneElement) { - this->init(adaptor, rec, description); + this->init(adaptor, description); if( oneElement != NULL ) this->add( oneElement ); } -template -RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements) - :m_elements(elements) +template +RewriteRuleElementStream::RewriteRuleElementStream(TreeAdaptorType* adaptor, + const char* description, + const ElementsType& elements) + : m_elements(elements) { - this->init(adaptor, rec, description); + this->init(adaptor, description); } -template -void RewriteRuleElementStream::init(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description) +template +void RewriteRuleElementStream::init(TreeAdaptorType* adaptor, + const char* description) { - m_rec = rec; m_adaptor = adaptor; m_cursor = 0; m_dirty = false; - m_singleElement = NULL; } template -RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description) - :BaseType(adaptor, rec, description) +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, + const char* description) + //: BaseType(adaptor, description) { } template -RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, RecognizerType* rec, - ANTLR_UINT8* description, TokenType* oneElement) - :BaseType(adaptor, rec, description, oneElement) +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, + const char* description, + const TokenType* oneElement) + //: BaseType(adaptor, description, oneElement) { } template -RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements) - :BaseType(adaptor, rec, description, elements) +RewriteRuleTokenStream::RewriteRuleTokenStream(TreeAdaptorType* adaptor, + const char* description, + const ElementsType& elements) + //: BaseType(adaptor, description, elements) { } template -RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description) - :BaseType(adaptor, rec, description) +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, + const char* description) + //: BaseType(adaptor, description) { } template -RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, RecognizerType* rec, - ANTLR_UINT8* description, TokenType* oneElement) - :BaseType(adaptor, rec, description, oneElement) +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, + const char* description, + TreeTypePtr& oneElement) + //: BaseType(adaptor, description, oneElement) { } template -RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements) - :BaseType(adaptor, rec, description, elements) +RewriteRuleSubtreeStream::RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, + const char* description, + const ElementsType& elements) + //: BaseType(adaptor, description, elements) { } +/* template -RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description) - :BaseType(adaptor, rec, description) +RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, + const char* description) + : BaseType(adaptor, description) { } template -RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, RecognizerType* rec, - ANTLR_UINT8* description, TokenType* oneElement) - :BaseType(adaptor, rec, description, oneElement) +RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, + const char* description, + TokenType* oneElement) + : BaseType(adaptor, description, oneElement) { } template -RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, - RecognizerType* rec, ANTLR_UINT8* description, const ElementsType& elements) - :BaseType(adaptor, rec, description, elements) +RewriteRuleNodeStream::RewriteRuleNodeStream(TreeAdaptorType* adaptor, + const char* description, + const ElementsType& elements) + : BaseType(adaptor, description, elements) { } +*/ -template -void RewriteRuleElementStream::reset() +template +void RewriteRuleElementStream::reset() { - m_dirty = true; m_cursor = 0; + m_dirty = true; } -template -void RewriteRuleElementStream::add(TokenType* el) +template +void RewriteRuleElementStream::add(ElementType* el) { - if ( el== NULL ) - return; - - if ( !m_elements.empty() ) - { - // if in list, just add - m_elements.push_back(el); - return; - } - - if ( m_singleElement == NULL ) - { - // no elements yet, track w/o list - m_singleElement = el; + if ( el== NULL ) return; - } - // adding 2nd element, move to list - m_elements.push_back(m_singleElement); - m_singleElement = NULL; m_elements.push_back(el); } -template -typename RewriteRuleElementStream::TokenType* -RewriteRuleElementStream::_next() +template +ElementType* RewriteRuleElementStream::_next() { - ANTLR_UINT32 n; - TreeType* t; - - n = this->size(); + ANTLR_UINT32 n = this->size(); if (n == 0) { // This means that the stream is empty - // - return NULL; // Caller must cope with this + return NULL; // Caller must cope with this (TODO throw RewriteEmptyStreamException) } // Traversed all the available elements already? - // - if ( m_cursor >= n) + if ( m_cursor >= n) // out of elements? { if (n == 1) { // Special case when size is single element, it will just dup a lot - // - return this->toTree(m_singleElement); + //return this->toTree(m_singleElement); + return this->toTree(m_elements.at(0)); } // Out of elements and the size is not 1, so we cannot assume // that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+) // This means we ran out of elements earlier than was expected. // - return NULL; // Caller must cope with this + return NULL; // Caller must cope with this (TODO throw RewriteEmptyStreamException) } - // Elements available either for duping or just available - // - if ( m_singleElement != NULL) - { - m_cursor++; // Cursor advances even for single element as this tells us to dup() - return this->toTree(m_singleElement); - } - - // More than just a single element so we extract it from the + // More than just a single element so we extract it from the // vector. - // - t = this->toTree( m_elements.at(m_cursor)); + ElementType* t = this->toTree(m_elements.at(m_cursor)); m_cursor++; return t; } -template -typename RewriteRuleElementStream::TreeType* -RewriteRuleElementStream::nextTree() +template +ElementType +RewriteRuleElementStream::nextTree() { - ANTLR_UINT32 n; - TreeType* el; - - n = this->size(); - - if ( m_dirty || ( (m_cursor >=n) && (n==1)) ) + ANTLR_UINT32 n = this->size(); + if ( m_dirty || ( (m_cursor >=n) && (n==1)) ) { // if out of elements and size is 1, dup - // - el = this->_next(); + ElementType* el = this->_next(); return this->dup(el); } // test size above then fetch - // - el = this->_next(); + ElementType* el = this->_next(); return el; } +/* template -typename RewriteRuleElementStream::TokenType* +typename RewriteRuleElementStream::TokenType* RewriteRuleElementStream::nextToken() { return this->_next(); } template -typename RewriteRuleElementStream::TokenType* +typename RewriteRuleElementStream::TokenType* RewriteRuleElementStream::next() { ANTLR_UINT32 s; s = this->size(); if ( (m_cursor >= s) && (s == 1) ) { - TreeType* el; + TreeTypePtr el; el = this->_next(); return this->dup(el); } return this->_next(); } +*/ + +template +ElementType* +RewriteRuleElementStream::dup( ElementType* element) +{ + return dupImpl(element); +} + +template +ElementType* +RewriteRuleElementStream::dupImpl( typename ImplTraits::CommonTokenType* element) +{ + return NULL; // TODO throw here +} + +template +ElementType* +RewriteRuleElementStream::dupImpl( typename ImplTraits::TreeTypePtr element) +{ + return m_adaptor->dupTree(element); +} + template -typename RewriteRuleSubtreeStream::TreeType* -RewriteRuleSubtreeStream::dup(TreeType* element) +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dup(TreeTypePtr element) { return this->dupTree(element); } template -typename RewriteRuleSubtreeStream::TreeType* -RewriteRuleSubtreeStream::dupTree(TreeType* element) +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::dupTree(TreeTypePtr element) { return BaseType::m_adaptor->dupNode(element); } -template -typename RewriteRuleElementStream::TreeType* -RewriteRuleElementStream::toTree( TreeType* element) +template +ElementType* +RewriteRuleElementStream::toTree( ElementType* element) { return element; } +/* template -typename RewriteRuleNodeStream::TreeType* -RewriteRuleNodeStream::toTree(TreeType* element) +typename RewriteRuleNodeStream::TreeTypePtr +RewriteRuleNodeStream::toTree(TreeTypePtr element) { return this->toTreeNode(element); } template -typename RewriteRuleNodeStream::TreeType* -RewriteRuleNodeStream::toTreeNode(TreeType* element) +typename RewriteRuleNodeStream::TreeTypePtr +RewriteRuleNodeStream::toTreeNode(TreeTypePtr element) { return BaseType::m_adaptor->dupNode(element); } +*/ -template -bool RewriteRuleElementStream::hasNext() +template +bool RewriteRuleElementStream::hasNext() { - if ( ((m_singleElement != NULL) && (m_cursor < 1)) - || ( !m_elements.empty() && m_cursor < m_elements.size())) + if ( !m_elements.empty() && m_cursor < m_elements.size()) { return true; } @@ -276,48 +271,34 @@ bool RewriteRuleElementStream::hasNext() } template -typename RewriteRuleTokenStream::TreeType* +typename RewriteRuleTokenStream::TreeTypePtr RewriteRuleTokenStream::nextNode() { - return this->nextNodeToken(); + TokenType *Token = this->nextToken(); + //return BaseType::m_adaptor->create(Token); + return m_adaptor->create(Token); } +/* template -typename RewriteRuleTokenStream::TreeType* +typename RewriteRuleTokenStream::TreeTypePtr RewriteRuleTokenStream::nextNodeToken() { return BaseType::m_adaptor->create(this->_next()); } +*/ /// Number of elements available in the stream /// -template -ANTLR_UINT32 RewriteRuleElementStream::size() +template +ANTLR_UINT32 RewriteRuleElementStream::size() { - ANTLR_UINT32 n = 0; - - /// Should be a count of one if singleElement is set. I copied this - /// logic from the java implementation, which I suspect is just guarding - /// against someone setting singleElement and forgetting to NULL it out - /// - if ( m_singleElement != NULL) - { - n = 1; - } - else - { - if ( !m_elements.empty() ) - { - return (ANTLR_UINT32)(m_elements.size()); - } - } - return n; - + return (ANTLR_UINT32)(m_elements.size()); } -template -typename RewriteRuleElementStream::StringType -RewriteRuleElementStream::getDescription() +template +typename RewriteRuleElementStream::StringType +RewriteRuleElementStream::getDescription() { if ( m_elementDescription.empty() ) { @@ -326,49 +307,57 @@ RewriteRuleElementStream::getDescription() return m_elementDescription; } -template -RewriteRuleElementStream::~RewriteRuleElementStream() +template +RewriteRuleElementStream::~RewriteRuleElementStream() { - TreeType* tree; - // Before placing the stream back in the pool, we // need to clear any vector it has. This is so any // free pointers that are associated with the - // entires are called. However, if this particular function is called - // then we know that the entries in the stream are definately + // entries are called. However, if this particular function is called + // then we know that the entries in the stream are definitely // tree nodes. Hence we check to see if any of them were nilNodes as // if they were, we can reuse them. // - if ( !m_elements.empty() ) + // We have some elements to traverse + // + for (ANTLR_UINT32 i = 0; i < m_elements.size(); i++) { - // We have some elements to traverse - // - ANTLR_UINT32 i; - - for (i = 1; i<= m_elements.size(); i++) - { - tree = m_elements.at(i-1); - if ( (tree != NULL) && tree->isNilNode() ) - { - // Had to remove this for now, check is not comprehensive enough - // tree->reuse(tree); - } - } - m_elements.clear(); + ElementType *tree = m_elements.at(i); + //if ( (tree != NULL) && tree->isNilNode() ) + { + // Had to remove this for now, check is not comprehensive enough + // tree->reuse(tree); + } } - else - { - if (m_singleElement != NULL) - { - tree = m_singleElement; - if (tree->isNilNode()) - { - // Had to remove this for now, check is not comprehensive enough - // tree->reuse(tree); - } - } - m_singleElement = NULL; + m_elements.clear(); +} + +template +typename RewriteRuleTokenStream::TokenType* +RewriteRuleTokenStream::nextToken() +{ + return this->_next(); +} + +template +typename RewriteRuleSubtreeStream::TreeTypePtr +RewriteRuleSubtreeStream::nextNode(TreeTypePtr element) +{ + //System.out.println("nextNode: elements="+elements+", singleElement="+((Tree)singleElement).toStringTree()); + ANTLR_UINT32 n = this->size(); + if ( BaseType::m_dirty || (BaseType::m_cursor>=n && n==1) ) { + // if out of elements and size is 1, dup (at most a single node + // since this is for making root nodes). + TreeTypePtr el = this->_next(); + return BaseType::m_adaptor->dupNode(el); } + // test size above then fetch + TreeType *tree = this->_next(); + while (BaseType::m_adaptor.isNil(tree) && BaseType::m_adaptor.getChildCount(tree) == 1) + tree = BaseType::m_adaptor->getChild(tree, 0); + //System.out.println("_next="+((Tree)tree).toStringTree()); + TreeType *el = BaseType::m_adaptor->dupNode(tree); // dup just the root (want node here) + return el; } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3tokenstream.hpp b/runtime/Cpp/include/antlr3tokenstream.hpp old mode 100755 new mode 100644 index 421ba7f7f..d97308788 --- a/runtime/Cpp/include/antlr3tokenstream.hpp +++ b/runtime/Cpp/include/antlr3tokenstream.hpp @@ -34,8 +34,6 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - /** Definition of a token source, which has a pointer to a function that * returns the next token (using a token factory if it is going to be * efficient) and a pointer to an ANTLR3_INPUT_STREAM. This is slightly @@ -43,7 +41,7 @@ * multiple interfaces without defining them in the interface structure * or casting (void *), which is too convoluted. */ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { //We are not making it subclass AllocPolicy, as this will always be a base class template @@ -174,7 +172,7 @@ class TokenStream : public ImplTraits::TokenIntStreamType * Return null for LT(0) and any index that results in an absolute address * that is negative. */ - const TokenType* _LT(ANTLR_INT32 k); + const TokenType* LT(ANTLR_INT32 k); /** Where is this stream pulling tokens from? This is not the name, but * a pointer into an interface that contains a ANTLR3_TOKEN_SOURCE interface. @@ -333,14 +331,14 @@ class CommonTokenStream : public TokenStream void insertToken( const TokenType& tok, BoolForwarder tokens_accessed_from_owning_rule ); /** Get a token at an absolute index i; 0..n-1. This is really only - * needed for profiling and debugging and token stream rewriting. - * If you don't want to buffer up tokens, then this method makes no - * sense for you. Naturally you can't use the rewrite stream feature. - * I believe DebugTokenStream can easily be altered to not use - * this method, removing the dependency. - */ - const TokenType* get(ANTLR_MARKER i); - const TokenType* getToken(ANTLR_MARKER i); + * needed for profiling and debugging and token stream rewriting. + * If you don't want to buffer up tokens, then this method makes no + * sense for you. Naturally you can't use the rewrite stream feature. + * I believe DebugTokenStream can easily be altered to not use + * this method, removing the dependency. + */ + const TokenType* get(ANTLR_MARKER i); + const TokenType* getToken(ANTLR_MARKER i); const TokenType* getToken( ANTLR_MARKER tok_idx, BoolForwarder tokens_accessed_from_owning_rule ); const TokenType* getToken( ANTLR_MARKER tok_idx, BoolForwarder tokens_accessed_from_owning_rule ); @@ -401,7 +399,7 @@ class TokenAccessException : public std::exception } }; -ANTLR_END_NAMESPACE() +} #include "antlr3tokenstream.inl" diff --git a/runtime/Cpp/include/antlr3tokenstream.inl b/runtime/Cpp/include/antlr3tokenstream.inl old mode 100755 new mode 100644 index 9c1c5a92a..b2c4e3bcc --- a/runtime/Cpp/include/antlr3tokenstream.inl +++ b/runtime/Cpp/include/antlr3tokenstream.inl @@ -1,4 +1,4 @@ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template TokenSource::TokenSource() @@ -82,7 +82,7 @@ typename TokenSource::TokenType* TokenSource::nextTokenS state->set_tokenStartLine( input->get_line() ); state->set_text(""); - if (istream->_LA(1) == ANTLR_CHARSTREAM_EOF) + if (istream->LA(1) == ANTLR_CHARSTREAM_EOF) { // Reached the end of the current stream, nothing more to do if this is // the last in the stack. @@ -110,6 +110,10 @@ typename TokenSource::TokenType* TokenSource::nextTokenS state->set_failed(true); lexer->get_rec()->reportError(); lexer->recover(); + if (state->get_token_present()) + // Good(or invalid) token factored by custom recover procedure + // + return state->get_token(); } else { @@ -387,7 +391,7 @@ void TokenStream::setDebugListener(DebugEventListenerType* debugger) } template -const typename TokenStream::TokenType* TokenStream::_LT(ANTLR_INT32 k) +const typename TokenStream::TokenType* TokenStream::LT(ANTLR_INT32 k) { ANTLR_INT32 i; ANTLR_INT32 n; @@ -934,4 +938,4 @@ CommonTokenStream::~CommonTokenStream() m_tokens.clear(); } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/include/antlr3traits.hpp b/runtime/Cpp/include/antlr3traits.hpp old mode 100755 new mode 100644 index c87604412..c5741ce98 --- a/runtime/Cpp/include/antlr3traits.hpp +++ b/runtime/Cpp/include/antlr3traits.hpp @@ -1,12 +1,13 @@ #ifndef _ANTLR3_TRAITS_HPP #define _ANTLR3_TRAITS_HPP -#include "antlr3defs.hpp" +namespace antlr3 { - -ANTLR_BEGIN_NAMESPACE() - -//Users implementing overrides should inherit from this +/** + * Users implementing overrides should inherit from this + * + * All classes typenames reffer to Empty class + */ template class CustomTraitsBase { @@ -33,9 +34,12 @@ class CustomTraitsBase typedef Empty LexStateType; typedef Empty CommonTokenType; - typedef Empty TokenIntStreamType; + typedef Empty TokenUserDataType; + typedef Empty TokenIntStreamType; typedef Empty TokenStreamType; + + typedef Empty TreeNodeIntStreamType; typedef Empty TreeNodeStreamType; @@ -55,7 +59,9 @@ class CustomTraitsBase }; typedef Empty TreeType; + typedef Empty TreeUserDataType; typedef Empty TreeAdaptorType; + typedef Empty TreeStoreType; template class ExceptionBaseType : public Empty @@ -71,7 +77,7 @@ class CustomTraitsBase typedef Empty BaseParserType;//this should be overridden with generated lexer typedef Empty BaseTreeParserType; - template + template class RewriteStreamType : public Empty { public: @@ -79,14 +85,17 @@ class CustomTraitsBase }; typedef Empty RuleReturnValueType; - + //If we want to change the way tokens are stored static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = false; - static const int TOKEN_FILL_BUFFER_INCREMENT = 100; //used only if the above val is true + 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 { @@ -129,100 +138,133 @@ class BoolSelector 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::AllocPolicyType, + DefaultAllocPolicy + >::selected AllocPolicyType; typedef typename TraitsSelector< typename UserTraits::StringType, - std::string >::selected StringType; + std::string + >::selected StringType; typedef typename TraitsSelector< typename UserTraits::StringStreamType, - std::stringstream >::selected StringStreamType; + std::stringstream + >::selected StringStreamType; typedef typename TraitsSelector< typename UserTraits::StreamDataType, - ANTLR_UINT8 >::selected StreamDataType; + ANTLR_UINT8 + >::selected StreamDataType; typedef typename TraitsSelector< typename UserTraits::Endianness, - RESOLVE_ENDIAN_AT_RUNTIME >::selected Endianness; + RESOLVE_ENDIAN_AT_RUNTIME + >::selected Endianness; typedef typename TraitsSelector< typename UserTraits::BitsetType, - Bitset >::selected BitsetType; + Bitset + >::selected BitsetType; typedef typename TraitsSelector< typename UserTraits::BitsetListType, - BitsetList >::selected BitsetListType; + BitsetList + >::selected BitsetListType; typedef typename TraitsSelector< typename UserTraits::InputStreamType, - InputStream >::selected InputStreamType; + InputStream + >::selected InputStreamType; template - class IntStreamType - : public TraitsOneArgSelector< - typename UserTraits::template IntStreamType, - IntStream, - typename UserTraits::template IntStreamType::BaseType - >::selected + class IntStreamType : public TraitsOneArgSelector< typename UserTraits::template IntStreamType, + IntStream, + typename UserTraits::template IntStreamType::BaseType + >::selected { }; typedef typename TraitsSelector< typename UserTraits::LexStateType, - LexState >::selected LexStateType; + LexState + >::selected LexStateType; static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = UserTraits::TOKENS_ACCESSED_FROM_OWNING_RULE; - static const int TOKEN_FILL_BUFFER_INCREMENT = UserTraits::TOKEN_FILL_BUFFER_INCREMENT; //used only if the above val is true + 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); } + static void displayRecognitionError( const StringType& str ) { UserTraits::displayRecognitionError(str); } }; -template< - class LxrType, - class PsrType, - template class UserTraits = CustomTraitsBase, - class TreePsrType = antlr3::Empty - > -class Traits : public TraitsBase +/** + * 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; + 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; - - typedef typename TraitsSelector< typename UserTraits::TokenStreamType, - CommonTokenStream >::selected TokenStreamType; + 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; - + CommonTreeNodeStream >::selected TreeNodeStreamType; + + // DebugEventListenerType typedef typename TraitsSelector< typename UserTraits::DebugEventListenerType, - DebugEventListener >::selected DebugEventListenerType; - + DebugEventListener >::selected DebugEventListenerType; + + // RecognizerSharedStateType template - class RecognizerSharedStateType - : public TraitsOneArgSelector< - typename UserTraits::template RecognizerSharedStateType, - RecognizerSharedState, - typename UserTraits::template RecognizerSharedStateType::BaseType - >::selected + 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 + 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 TraitsOneArgSelector< typename UserTraits::template RecognizerType, + BaseRecognizer, + typename UserTraits::template RecognizerType::BaseType + >::selected BaseType; typedef typename BaseType::RecognizerSharedStateType RecognizerSharedStateType; public: @@ -232,24 +274,31 @@ class Traits : public TraitsBase } }; + // TreeType typedef typename TraitsSelector< typename UserTraits::TreeType, - CommonTree >::selected TreeType; + CommonTree >::selected TreeType; + typedef typename TraitsSelector< typename UserTraits::TreeUserDataType, + Empty >::selected TreeUserDataType; + // TreeAdaptorType typedef typename TraitsSelector< typename UserTraits::TreeAdaptorType, - CommonTreeAdaptor >::selected 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 + 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; + typedef typename TraitsOneArgSelector< typename UserTraits::template ExceptionBaseType, + ANTLR_ExceptionBase, + typename UserTraits::template ExceptionBaseType::BaseType + >::selected BaseType; protected: ExceptionBaseType( const typename BaseTraitsType::StringType& message ) @@ -258,63 +307,71 @@ class Traits : public TraitsBase } }; - //this should be overridden with generated lexer + // this should be overridden with generated lexer + // BaseLexerType typedef typename TraitsSelector< typename UserTraits::BaseLexerType, - Lexer >::selected BaseLexerType; + Lexer >::selected BaseLexerType; typedef LxrType LexerType; + // TokenSourceType typedef typename TraitsSelector< typename UserTraits::TokenSourceType, - TokenSource >::selected TokenSourceType; + TokenSource >::selected TokenSourceType; + + // this should be overridden with generated parser + // BaseParserType typedef typename TraitsSelector< typename UserTraits::BaseParserType, - Parser >::selected 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; - - template - class RewriteStreamType : public TraitsOneArgSelector< - typename UserTraits::template RewriteStreamType, - RewriteRuleElementStream, - typename UserTraits::template RewriteStreamType::BaseType - >::selected + 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 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::TokenType TokenType; - typedef typename BaseType::ElementsType ElementsType; + //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, Recognizer_Type* rec=NULL, ANTLR_UINT8* description = NULL) - :BaseType(adaptor, rec, description) + RewriteStreamType(TreeAdaptorType* adaptor = NULL, const char* description = NULL) + :BaseType(adaptor, description) { } - RewriteStreamType(TreeAdaptorType* adaptor, Recognizer_Type* rec, ANTLR_UINT8* description, TokenType* oneElement) - :BaseType(adaptor, rec, description, oneElement) + RewriteStreamType(TreeAdaptorType* adaptor, const char* description, ElementType* oneElement) + :BaseType(adaptor, description, oneElement) { } - RewriteStreamType(TreeAdaptorType* adaptor, Recognizer_Type* rec, ANTLR_UINT8* description, const ElementsType& elements) - :BaseType(adaptor, rec, description, elements) + 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; + typename BoolSelector< TraitsType::TOKENS_ACCESSED_FROM_OWNING_RULE, + RuleReturnValue_1, + RuleReturnValue + >::selected + >::selected RuleReturnValueType; }; - -ANTLR_END_NAMESPACE() +} #endif //_ANTLR3_TRAITS_HPP diff --git a/runtime/Cpp/include/antlr3treeparser.hpp b/runtime/Cpp/include/antlr3treeparser.hpp old mode 100755 new mode 100644 index 8f83ff8ab..c1395382b --- a/runtime/Cpp/include/antlr3treeparser.hpp +++ b/runtime/Cpp/include/antlr3treeparser.hpp @@ -29,26 +29,25 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "antlr3defs.hpp" - /** Internal structure representing an element in a hash bucket. * Stores the original key so that duplicate keys can be rejected * if necessary, and contains function can be supported If the hash key * could be unique I would have invented the perfect compression algorithm ;-) */ -ANTLR_BEGIN_NAMESPACE() +namespace antlr3 { template -class TreeParser : public ImplTraits::template RecognizerType< TreeParser > +class TreeParser : public ImplTraits::template RecognizerType< typename ImplTraits::TreeNodeStreamType > { public: typedef typename ImplTraits::TreeNodeStreamType TreeNodeStreamType; typedef TreeNodeStreamType StreamType; typedef typename TreeNodeStreamType::IntStreamType IntStreamType; typedef typename ImplTraits::TreeType TreeType; + typedef typename ImplTraits::TreeTypePtr TreeTypePtr; typedef TreeType TokenType; - typedef typename ImplTraits::template ExceptionBase ExceptionBaseType; - typedef typename ImplTraits::template RecognizerType< TreeParser > RecognizerType; + typedef typename ImplTraits::template ExceptionBaseType ExceptionBaseType; + typedef typename ImplTraits::template RecognizerType< typename ImplTraits::TreeNodeStreamType > RecognizerType; typedef typename RecognizerType::RecognizerSharedStateType RecognizerSharedStateType; typedef Empty TokenSourceType; typedef typename ImplTraits::BitsetListType BitsetListType; @@ -94,7 +93,7 @@ class TreeParser : public ImplTraits::template RecognizerType< TreeParser TreeParser::TreeParser( ANTLR_UINT32 sizeHint, TreeNodeStreamType* ctnstream, @@ -44,15 +44,16 @@ typename TreeParser::RecognizerType* TreeParser::get_rec template< class ImplTraits > void TreeParser::fillExceptionData( ExceptionBaseType* ex ) { - ex->set_token( m_ctnstream->_LT(1) ); /* Current input tree node */ - ex->set_line( ex->get_token()->getLine() ); - ex->set_charPositionInLine( ex->get_token()->getCharPositionInLine() ); + auto xxx = m_ctnstream->LT(1); + //ex->set_token( m_ctnstream->LT(1) ); /* Current input tree node */ + ex->set_line( ex->get_token()->get_line() ); + ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() ); ex->set_index( m_ctnstream->index() ); // Are you ready for this? Deep breath now... // { - TreeType* tnode; + TreeTypePtr tnode; tnode = ex->get_token(); @@ -105,12 +106,12 @@ void TreeParser::displayRecognitionError( ANTLR_UINT8** tokenNames, << this->get_rec()->get_state()->get_exception()->get_message(); IntStreamType* is = this->get_istream(); - TreeType* theBaseTree = this->get_rec()->get_state()->get_exception()->get_token(); + TreeTypePtr theBaseTree = this->get_rec()->get_state()->get_exception()->get_token(); StringType ttext = theBaseTree->toStringTree(); if (theBaseTree != NULL) { - TreeType* theCommonTree = static_cast(theBaseTree); + TreeTypePtr theCommonTree = static_cast(theBaseTree); if (theCommonTree != NULL) { CommonTokenType* theToken = theBaseTree->getToken(); @@ -156,8 +157,8 @@ TreeParser::getMissingSymbol( IntStreamType* istream, ExceptionBaseT ANTLR_UINT32 expectedTokenType, BitsetListType* follow) { TreeNodeStreamType* tns; - TreeType* node; - TreeType* current; + TreeTypePtr node; + TreeTypePtr current; CommonTokenType* token; StringType text; ANTLR_INT32 i; @@ -168,12 +169,12 @@ TreeParser::getMissingSymbol( IntStreamType* istream, ExceptionBaseT // Create a new empty node, by stealing the current one, or the previous one if the current one is EOF // - current = tns->_LT(1); + current = tns->LT(1); i = -1; if (current == tns->get_EOF_NODE_p()) { - current = tns->_LT(-1); + current = tns->LT(-1); i--; } node = current->dupNode(); @@ -195,4 +196,4 @@ TreeParser::getMissingSymbol( IntStreamType* istream, ExceptionBaseT } -ANTLR_END_NAMESPACE() +} diff --git a/runtime/Cpp/tests/.gitignore b/runtime/Cpp/tests/.gitignore index dadd82c5d..402a48623 100644 --- a/runtime/Cpp/tests/.gitignore +++ b/runtime/Cpp/tests/.gitignore @@ -1,13 +1,13 @@ # Windows test files -[st][0-9][0-9][0-9].exe +[ast][0-9][0-9][0-9].exe # Unix test files -[st[0-9][0-9][0-9] +[st][0-9][0-9][0-9] # ANTLR generated files -[st][0-9][0-9][0-9]?*.cpp -[st][0-9][0-9][0-9]?*.hpp -[st][0-9][0-9][0-9]*.tokens +[ast][0-9][0-9][0-9]?*.cpp +[ast][0-9][0-9][0-9]?*.hpp +[ast][0-9][0-9][0-9]*.tokens # MSVC files *.vcxproj diff --git a/runtime/Cpp/tests/A007TestTraits.hpp b/runtime/Cpp/tests/A007TestTraits.hpp new file mode 100644 index 000000000..87a4cd551 --- /dev/null +++ b/runtime/Cpp/tests/A007TestTraits.hpp @@ -0,0 +1,59 @@ +#ifndef _A007_TEST_TRAITS_H +#define _A007_TEST_TRAITS_H + +#include + +#include +#include +#include + +// Forward declaration for Lexer&Parser class(es) +namespace Antlr3Test { + //code for overriding + template + class UserTraits : public antlr3::CustomTraitsBase + { + public: + struct A007TokenUserDataType + { + A007TokenUserDataType() : identifierClass(-1), usageType(-1) {}; + int identifierClass, usageType; + }; + + class A007Token : public antlr3::CommonToken + { + typedef antlr3::CommonToken super; + typedef typename antlr3::CommonToken::TOKEN_TYPE TOKEN_TYPE; + typedef typename super::StringType StringType; + public: + // Override all possible constructors + A007Token() : super() {}; + A007Token( ANTLR_UINT32 type) : super(type) {}; + A007Token( TOKEN_TYPE type) : super(type) {}; + A007Token( const A007Token& ctoken ) : super(ctoken) {}; + A007Token& operator=( const A007Token& other ) { super::operator=(other); return *this; }; + + // Override toString method + StringType toString() const + { + StringType m_txt; + m_txt = super::getText(); + if (super::UserData.identifierClass > 0) + m_txt += "[" + std::to_string(super::UserData.identifierClass) + "]"; + return m_txt; + } + }; + + // Override default trait's types + typedef A007Token CommonTokenType; + typedef A007TokenUserDataType TokenUserDataType; + }; + + class a007Lexer; class a007Parser; + + // Instantiate the Traits class(will be used for Lexer/Parser template instantiations) + typedef antlr3::Traits a007LexerTraits; + typedef a007LexerTraits a007ParserTraits; +}; + +#endif diff --git a/runtime/Cpp/tests/A008TestTraits.hpp b/runtime/Cpp/tests/A008TestTraits.hpp new file mode 100644 index 000000000..afefa0012 --- /dev/null +++ b/runtime/Cpp/tests/A008TestTraits.hpp @@ -0,0 +1,57 @@ +#ifndef _A008_TEST_TRAITS_H +#define _A008_TEST_TRAITS_H + +#include + +#include +#include +#include + +namespace Antlr3Test { + //code for overriding + template + class UserTraits : public antlr3::CustomTraitsBase + { + public: + struct A008TreeUserDataType + { + A008TreeUserDataType() : identifierClass(-1), usageType(-1) {}; + int identifierClass, usageType; + }; + + class A008Token : public antlr3::CommonToken + { + typedef antlr3::CommonToken super; + typedef typename antlr3::CommonToken::TOKEN_TYPE TOKEN_TYPE; + typedef typename super::StringType StringType; + public: + // Override all possible constructors + A008Token() : super() {}; + A008Token( ANTLR_UINT32 type) : super(type) {}; + A008Token( TOKEN_TYPE type) : super(type) {}; + A008Token( const A008Token& ctoken ) : super(ctoken) {}; + A008Token& operator=( const A008Token& other ) { super::operator=(other); return *this; }; + + // Override toString method + StringType toString() const + { + return super::getText(); + } + }; + + // Override default trait's types + typedef A008Token CommonTokenType; + typedef A008TreeUserDataType TreeUserDataType; + }; + + // Forward declaration for Lexer&Parser class(es) + class a008Lexer; + class a008Parser; + + // Instantiate the Traits class(will be used for Lexer/Parser template instantiations) + typedef antlr3::Traits a008Traits; + typedef a008Traits a008LexerTraits; + typedef a008Traits a008ParserTraits; +}; + +#endif diff --git a/runtime/Cpp/tests/ATestTraits.hpp b/runtime/Cpp/tests/ATestTraits.hpp new file mode 100644 index 000000000..fe6d35be3 --- /dev/null +++ b/runtime/Cpp/tests/ATestTraits.hpp @@ -0,0 +1,37 @@ +#ifndef _A_TEST_TRAITS_H +#define _A_TEST_TRAITS_H + +#include + +#include +#include +#include + +// Forward declaration for Lexer&Parser class(es) +namespace Antlr3Test { + //code for overriding + template + class UserTraits : public antlr3::CustomTraitsBase + { + public: + //static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = true; + //static const int TOKEN_FILL_BUFFER_INCREMENT = 2; + }; + + class a001Lexer; class a001Parser; + class a002Lexer; class a002Parser; + class a003Lexer; class a003Parser; + class a004Lexer; class a004Parser; + class a005Lexer; class a005Parser; + class a006Lexer; class a006Parser; + + // Instantiate the Traits class(will be used for Lexer/Parser template instantiations) + typedef antlr3::Traits a001LexerTraits; typedef a001LexerTraits a001ParserTraits; + typedef antlr3::Traits a002LexerTraits; typedef a002LexerTraits a002ParserTraits; + typedef antlr3::Traits a003LexerTraits; typedef a003LexerTraits a003ParserTraits; + typedef antlr3::Traits a004LexerTraits; typedef a004LexerTraits a004ParserTraits; + typedef antlr3::Traits a005LexerTraits; typedef a005LexerTraits a005ParserTraits; + typedef antlr3::Traits a006LexerTraits; typedef a006LexerTraits a006ParserTraits; +}; + +#endif diff --git a/runtime/Cpp/tests/Makefile b/runtime/Cpp/tests/Makefile index 1efad371e..dde77a085 100644 --- a/runtime/Cpp/tests/Makefile +++ b/runtime/Cpp/tests/Makefile @@ -1,6 +1,6 @@ .PRECIOUS=.o -ANTLRGRAMMARS ?= $(wildcard t0[01]*.g) +ANTLRGRAMMARS ?= $(wildcard [ast]*.g) HEADERS = $(wildcard *.hpp) $(wildcard *.inl) RUNTIME_HEADERS = $(wildcard ../include/*.hpp) $(wildcard ../include/*.inl) SOURCES = $(wildcard *.cpp) @@ -8,74 +8,130 @@ POBJS = $(PSOURCES:.cpp=.o) TOKENS = $(ANTLRGRAMMARS:.g=.tokens) GRAMMAROPTIONS= #-report -Xconversiontimeout 1500000 -Xmultithreaded -Xwatchconversion -ANTLR = ../../../antlr-complete/target/antlr-complete-3.5.2-SNAPSHOT.jar +ANTLR = ../../../antlr-complete/target/antlr-complete-3.5.3-SNAPSHOT.jar STGS = $(wildcard ../../../tool/src/main/resources/org/antlr/codegen/templates/Cpp/*.stg) .SUFFIXES: INCLUDES= -I. -I../include/ -CFLAGS=-ggdb3 -O0 -fno-inline -Wall -Wno-unused-variable -#CFLAGS=-ggdb3 -O3 +#-I/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/ -I/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/x86_64-pc-cygwin + +#CXX=/usr/bin/clang++ +CFLAGS=-ggdb3 -O0 -fno-inline -Wall -std=c++0x +#CFLAGS=-ggdb3 -O3 -std=c++0x TOBJS= utils.o -all: $(ANTLR) $(TOKENS) t001 t002 t003 t004 t005 t006 t006 t007 t008 t009 t010 t011 t012 +all: $(ANTLR) $(TOKENS) t001 t002 t003 t004 t005 t006 t006 t007 t008 t009 t010 t011 t012 t020 t039 \ + s001 s002 s003 s004 a001 a002 a003 a004 a005 # For devel only. This will replace .stg files in the tool in a fast way # $(ANTLR): $(STGS) - jar uvf $(ANTLR) -C ../../../tool/src/main/resources \ - org/antlr/codegen/templates/Cpp/Cpp.stg + jar uvf $(ANTLR) \ + -C ../../../tool/src/main/resources org/antlr/codegen/templates/Cpp/Cpp.stg \ + -C ../../../tool/src/main/resources org/antlr/codegen/templates/Cpp/AST.stg \ + -C ../../../tool/src/main/resources org/antlr/codegen/templates/Cpp/ASTParser.stg + +a001: a001.cpp a001.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a002: a002.cpp a002.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a003: a003.cpp a003.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a004: a004.cpp a004.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a005: a005.cpp a005.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a006: a006.cpp a006.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a007: a007.cpp a007.tokens A007TestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a008: a008.cpp a008.tokens A008TestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +a009: a009.cpp a009.tokens A009TestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +s001: s001.cpp s001.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) utils.cpp -o $@ + +s002: s002.cpp s002.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) utils.cpp -o $@ + +s003: s003.cpp s003.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) utils.cpp -o $@ t001: t001.cpp t001lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t002: t002.cpp t002lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t003: t003.cpp t003lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t004: t004.cpp t004lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t005: t005.cpp t005lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t006: t006.cpp t006lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t007: t007.cpp t007lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t008: t008.cpp t008lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t009: t009.cpp t009lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t010: t010.cpp t010lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t011: t011.cpp t011lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t012: t012.cpp t012lexerXMLLexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +t020: t020.cpp t020fuzzyLexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +t039: t039.cpp t039labels.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ t051: t051.cpp t051lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) - g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ + +s004: s004.cpp PLSQLLexer.tokens PLSQLParser.tokens PLSQL_DMLParser.g PTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< PLSQLLexer*.cpp PLSQLParser*.cpp utils.cpp -o $@ + +s005: s005.cpp SQLMini.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< SQLMiniLexer.cpp SQLMiniParser.cpp utils.cpp -o $@ + +s006: s006.cpp SQLMini2.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) + $(CXX) $(CFLAGS) $(INCLUDES) $< SQLMiniLexer.cpp SQLMiniParser.cpp utils.cpp -o $@ # AST commented out -# t039: t039.cpp t039labels.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) -# g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ # # t042: t042.cpp t005lexer.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS) -# g++ $(CFLAGS) -DUSESTL $(INCLUDES) $< $@?*.cpp -o $@ +# $(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@ clean: - rm -f *.o t0[0-9][0-9]??*.[ch]pp *.tokens t[0-9][0-9][0-9] t0[0-9][0-9].exe + rm -f *.o [ast]0[0-9][0-9]??*.[ch]pp *.tokens [ast][0-9][0-9][0-9] [ast]0[0-9][0-9].exe + rm -f PLSQLLexer.[ch]pp PLSQLParser*.[ch]pp # %.u: %.g # @echo "Bulding dependencies for "$< @@ -84,8 +140,8 @@ clean: # @cat $@.tmp >> $@ # $(RM) $@.tmp -%.tokens %.cpp %Lexer.c %Parser.c %Lexer.h %Parser.h %.hpp: %.g $(ANTLR) - java -jar $(ANTLR) $(GRAMMAROPTIONS) $< +%.tokens %Lexer.cpp %Parser.cpp %Lexer.hpp %Parser.hpp: %.g $(ANTLR) + java -Xmx512m -jar $(ANTLR) $(GRAMMAROPTIONS) $< ifneq ($(MAKECMDGOALS),clean) ifneq ($(strip $(ANTLRGRAMMARS)),) @@ -94,4 +150,4 @@ endif endif %.o: %.cpp $(HEADERS) utils.hpp - g++ $(CFLAGS) -DUSESTL $(INCLUDES) -c $< -o $@ + $(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@ diff --git a/runtime/Cpp/tests/PLSQLCommons.g b/runtime/Cpp/tests/PLSQLCommons.g new file mode 100644 index 000000000..1c3c11900 --- /dev/null +++ b/runtime/Cpp/tests/PLSQLCommons.g @@ -0,0 +1,426 @@ +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +parser grammar PLSQLCommons; + +@includes +{ + #include "PTestTraits.hpp" +} + +@namespace{ Antlr3Test } + +// $ + +// $ PERIOD id_expression)* (AT_SIGN link_name)? + ; + +package_name + : id + ; + +implementation_type_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ; + +parameter_name + : id + ; + +reference_model_name + : id + ; + +main_model_name + : id + ; + +aggregate_function_name + : id ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +query_name + : id + ; + +constraint_name + : id ((PERIOD id_expression)=> PERIOD id_expression)* (AT_SIGN link_name)? + ; + +label_name + : id_expression + ; + +type_name + : id_expression ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +sequence_name + : id_expression ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +exception_name + : id ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +function_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ; + +procedure_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ; + +trigger_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ; + +variable_name + : (INTRODUCER char_set_name)? + id_expression ((PERIOD id_expression)=> PERIOD id_expression)? + | bind_variable + ; + +index_name + : id + ; + +cursor_name + : id + | bind_variable + ; + +record_name + : id + | bind_variable + ; + +collection_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ; + +link_name + : id + ; + +column_name + : id ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +tableview_name + : id ((PERIOD id_expression)=> PERIOD id_expression)? + ( AT_SIGN link_name + | {!(LA(2) == SQL92_RESERVED_BY)}?=> partition_extension_clause + )? + ; + +char_set_name + : id_expression ((PERIOD id_expression)=> PERIOD id_expression)* + ; + +// $> + +// $ tableview_name PERIOD ASTERISK + | ASTERISK + | expression column_alias? (COMMA expression column_alias?)* + ) + RIGHT_PAREN + keep_clause? + ; + +respect_or_ignore_nulls + : (respect_key | ignore_key) nulls_key + ; + +argument + : ((id EQUALS_OP GREATER_THAN_OP)=> id EQUALS_OP GREATER_THAN_OP)? expression_wrapper + ; + +type_spec + : datatype + | ref_key? type_name (percent_rowtype_key|percent_type_key)? + ; + +datatype + : native_datatype_element + precision_part? + (with_key local_key? time_key zone_key)? + | interval_key (year_key|day_key) + (LEFT_PAREN expression_wrapper RIGHT_PAREN)? + to_key (month_key|second_key) + (LEFT_PAREN expression_wrapper RIGHT_PAREN)? + ; + +precision_part + : LEFT_PAREN numeric (COMMA numeric)? (char_key | byte_key)? RIGHT_PAREN + ; + +native_datatype_element + : binary_integer_key + | pls_integer_key + | natural_key + | binary_float_key + | binary_double_key + | naturaln_key + | positive_key + | positiven_key + | signtype_key + | simple_integer_key + | nvarchar2_key + | dec_key + | integer_key + | int_key + | numeric_key + | smallint_key + | number_key + | decimal_key + | double_key precision_key? + | float_key + | real_key + | nchar_key + | long_key raw_key? + | char_key + | character_key + | varchar2_key + | varchar_key + | string_key + | raw_key + | boolean_key + | date_key + | rowid_key + | urowid_key + | year_key + | month_key + | day_key + | hour_key + | minute_key + | second_key + | timezone_hour_key + | timezone_minute_key + | timezone_region_key + | timezone_abbr_key + | timestamp_key + | timestamp_unconstrained_key + | timestamp_tz_unconstrained_key + | timestamp_ltz_unconstrained_key + | yminterval_unconstrained_key + | dsinterval_unconstrained_key + | bfile_key + | blob_key + | clob_key + | nclob_key + | mlslabel_key + ; + +bind_variable + : ( BINDVAR | COLON UNSIGNED_INTEGER) + ( indicator_key? (BINDVAR | COLON UNSIGNED_INTEGER))? + ((PERIOD general_element_part)=> PERIOD general_element_part)* + ; + +general_element + : general_element_part ((PERIOD general_element_part)=> PERIOD general_element_part)* + ; + +general_element_part + : (INTRODUCER char_set_name)? id_expression + ((PERIOD id_expression)=> PERIOD id_expression)* function_argument? + ; + +table_element + : (INTRODUCER char_set_name)? id_expression (PERIOD id_expression)* + ; + +// $> + +// $ diff --git a/runtime/Cpp/tests/PLSQLKeys.g b/runtime/Cpp/tests/PLSQLKeys.g new file mode 100644 index 000000000..652218421 --- /dev/null +++ b/runtime/Cpp/tests/PLSQLKeys.g @@ -0,0 +1,2219 @@ +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "LICENSE"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +parser grammar PLSQLKeys; + +@includes +{ + #include "PTestTraits.hpp" + #include "PLSQLLexer.hpp" +} +@namespace { Antlr3Test } + + +// @members{ +// // int (pANTLR3_COMMON_TOKEN s1, const char* s2) +// // { +// // return !strcasecmp(s1->getText(s1)->chars, s2); +// // } +// } + +create_key + : SQL92_RESERVED_CREATE + ; + +replace_key +// : {input.LT(1).getText(). ("REPLACE" }?=> REGULAR_ID + : { LT(1)->getText() == "REPLACE" }? => REGULAR_ID + ; + +package_key + : { LT(1)->getText() == "PACKAGE" }?=> REGULAR_ID + ; + +body_key + : { LT(1)->getText() == "BODY" }? REGULAR_ID + ; + +begin_key + : SQL92_RESERVED_BEGIN + ; + +exit_key + : { LT(1)->getText() == "EXIT" }? REGULAR_ID + ; + +declare_key + : SQL92_RESERVED_DECLARE + ; + +exception_key + : SQL92_RESERVED_EXCEPTION + ; + +serveroutput_key + : { LT(1)->getText() == "SERVEROUTPUT" }? REGULAR_ID + ; + +off_key + : { LT(1)->getText() == "OFF" }? REGULAR_ID + ; + +constant_key + : { LT(1)->getText() == "CONSTANT" }? REGULAR_ID + ; + +subtype_key + : { LT(1)->getText() == "SUBTYPE" }? REGULAR_ID + ; + +cursor_key//{ LT(1)->getText() == "CURSOR" }? REGULAR_ID + : SQL92_RESERVED_CURSOR + ; + +nextval_key + : { LT(1)->getText() == "NEXTVAL" }?=> REGULAR_ID + ; + +goto_key + : SQL92_RESERVED_GOTO + ; + +execute_key + : { LT(1)->getText() == "EXECUTE" }? REGULAR_ID + ; + +immediate_key + : { LT(1)->getText() == "IMMEDIATE" }?=> REGULAR_ID + ; + +return_key + : { LT(1)->getText() == "RETURN" }? REGULAR_ID + ; + +procedure_key + : SQL92_RESERVED_PROCEDURE + ; + +function_key + : { LT(1)->getText() == "FUNCTION" }?=> REGULAR_ID + ; + +pragma_key + : { LT(1)->getText() == "PRAGMA" }? REGULAR_ID + ; + +exception_init_key + : { LT(1)->getText() == "EXCEPTION_INIT" }? REGULAR_ID + ; + +type_key + : { LT(1)->getText() == "TYPE" }?=> REGULAR_ID + ; + +record_key + : { LT(1)->getText() == "RECORD" }?=> REGULAR_ID + ; + +indexed_key + : { LT(1)->getText() == "INDEXED" }? REGULAR_ID + ; + +index_key + : PLSQL_RESERVED_INDEX + ; + +percent_notfound_key + : { LT(2)->getText() == "NOTFOUND" }?=> PERCENT REGULAR_ID + ; + +percent_found_key + : { LT(2)->getText() == "FOUND" }?=> PERCENT REGULAR_ID + ; + +percent_isopen_key + : { LT(2)->getText() == "ISOPEN" }?=> PERCENT REGULAR_ID + ; + +percent_rowcount_key + : { LT(2)->getText() == "ROWCOUNT" }?=> PERCENT REGULAR_ID + ; + +percent_rowtype_key + : { LT(2)->getText() == "ROWTYPE" }?=> PERCENT REGULAR_ID + ; + +percent_type_key + : { LT(2)->getText() == "TYPE" }?=> PERCENT REGULAR_ID + ; + +out_key + : { LT(1)->getText() == "OUT" }?=> REGULAR_ID + ; + +inout_key + : { LT(1)->getText() == "INOUT" }? REGULAR_ID + ; + +extend_key + : { LT(1)->getText() == "EXTEND" }?=> REGULAR_ID + ; + +raise_key + : { LT(1)->getText() == "RAISE" }? REGULAR_ID + ; + +while_key + : { LT(1)->getText() == "WHILE" }? REGULAR_ID + ; + +loop_key + : { LT(1)->getText() == "LOOP" }? REGULAR_ID + ; + +commit_key + : { LT(1)->getText() == "COMMIT" }?=> REGULAR_ID + ; + +work_key + : { LT(1)->getText() == "WORK" }? REGULAR_ID + ; + +if_key + : PLSQL_RESERVED_IF + ; + +elsif_key + : PLSQL_NON_RESERVED_ELSIF + ; + +authid_key + : { LT(1)->getText() == "AUTHID" }?=> REGULAR_ID + ; + +definer_key + : { LT(1)->getText() == "DEFINER" }? REGULAR_ID + ; + +external_key + : { LT(1)->getText() == "EXTERNAL" }? REGULAR_ID + ; + +language_key + : { LT(1)->getText() == "LANGUAGE" }? REGULAR_ID + ; + +java_key + : { LT(1)->getText() == "JAVA" }? REGULAR_ID + ; + +name_key + : { LT(1)->getText() == "NAME" }?=> REGULAR_ID + ; + +deterministic_key + : { LT(1)->getText() == "DETERMINISTIC" }?=> REGULAR_ID + ; + +parallel_enable_key + : { LT(1)->getText() == "PARALLEL_ENABLE" }?=> REGULAR_ID + ; + +result_cache_key + : { LT(1)->getText() == "RESULT_CACHE" }?=> REGULAR_ID + ; + +pipelined_key + : { LT(1)->getText() == "PIPELINED" }?=> REGULAR_ID + ; + +aggregate_key + : { LT(1)->getText() == "AGGREGATE" }? REGULAR_ID + ; + +alter_key + : SQL92_RESERVED_ALTER + ; + +compile_key + : { LT(1)->getText() == "COMPILE" }? REGULAR_ID + ; + +debug_key + : { LT(1)->getText() == "DEBUG" }? REGULAR_ID + ; + +reuse_key + : { LT(1)->getText() == "REUSE" }? REGULAR_ID + ; + +settings_key + : { LT(1)->getText() == "SETTINGS" }? REGULAR_ID + ; + +specification_key + : { LT(1)->getText() == "SPECIFICATION" }? REGULAR_ID + ; + +drop_key + : SQL92_RESERVED_DROP + ; + +trigger_key + : { LT(1)->getText() == "TRIGGER" }?=> REGULAR_ID + ; + +force_key + : { LT(1)->getText() == "FORCE" }?=> REGULAR_ID + ; + +validate_key + : { LT(1)->getText() == "VALIDATE" }? REGULAR_ID + ; + +ref_key + : { LT(1)->getText() == "REF" }?=> REGULAR_ID + ; + +array_key + : { LT(1)->getText() == "ARRAY" }?=> REGULAR_ID + ; + +varray_key + : { LT(1)->getText() == "VARRAY" }?=> REGULAR_ID + ; + +pls_integer_key + : { LT(1)->getText() == "PLS_INTEGER" }?=> REGULAR_ID + ; + +serially_reusable_key + : { LT(1)->getText() == "SERIALLY_REUSABLE" }?=> REGULAR_ID + ; + +autonomous_transaction_key + : { LT(1)->getText() == "AUTONOMOUS_TRANSACTION" }?=> REGULAR_ID + ; + +inline_key + : { LT(1)->getText() == "INLINE" }?=> REGULAR_ID + ; + +restrict_references_key + : { LT(1)->getText() == "RESTRICT_REFERENCES" }?=> REGULAR_ID + ; + +exceptions_key + : { LT(1)->getText() == "EXCEPTIONS" }?=> REGULAR_ID + ; + +save_key + : { LT(1)->getText() == "SAVE" }?=> REGULAR_ID + ; + +forall_key + : { LT(1)->getText() == "FORALL" }?=> REGULAR_ID + ; + +continue_key + : { LT(1)->getText() == "CONTINUE" }?=> REGULAR_ID + ; + +indices_key + : { LT(1)->getText() == "INDICES" }?=> REGULAR_ID + ; + +values_key + : SQL92_RESERVED_VALUES + ; + +case_key + : SQL92_RESERVED_CASE + ; + +bulk_key + : { LT(1)->getText() == "BULK" }?=> REGULAR_ID + ; + +collect_key + : { LT(1)->getText() == "COLLECT" }?=> REGULAR_ID + ; + +committed_key + : { LT(1)->getText() == "COMMITTED" }? REGULAR_ID + ; + +use_key + : { LT(1)->getText() == "USE" }?=> REGULAR_ID + ; + +level_key + : { LT(1)->getText() == "LEVEL" }? REGULAR_ID + ; + +isolation_key + : { LT(1)->getText() == "ISOLATION" }?=> REGULAR_ID + ; + +serializable_key + : { LT(1)->getText() == "SERIALIZABLE" }? REGULAR_ID + ; + +segment_key + : { LT(1)->getText() == "SEGMENT" }? REGULAR_ID + ; + +write_key + : { LT(1)->getText() == "WRITE" }?=> REGULAR_ID + ; + +wait_key + : { LT(1)->getText() == "WAIT" }?=> REGULAR_ID + ; + +corrupt_xid_all_key + : { LT(1)->getText() == "CORRUPT_XID_ALL" }?=> REGULAR_ID + ; + +corrupt_xid_key + : { LT(1)->getText() == "CORRUPT_XID" }?=> REGULAR_ID + ; + +batch_key + : { LT(1)->getText() == "BATCH" }?=> REGULAR_ID + ; + +session_key + : { LT(1)->getText() == "SESSION" }?=> REGULAR_ID + ; + +role_key + : { LT(1)->getText() == "ROLE" }?=> REGULAR_ID + ; + +constraint_key + : { LT(1)->getText() == "CONSTRAINT" }?=> REGULAR_ID + ; + +constraints_key + : { LT(1)->getText() == "CONSTRAINTS" }?=> REGULAR_ID + ; + +call_key + : { LT(1)->getText() == "CALL" }?=> REGULAR_ID + ; + +explain_key + : { LT(1)->getText() == "EXPLAIN" }?=> REGULAR_ID + ; + +merge_key + : { LT(1)->getText() == "MERGE" }?=> REGULAR_ID + ; + +plan_key + : { LT(1)->getText() == "PLAN" }?=> REGULAR_ID + ; + +system_key + : { LT(1)->getText() == "SYSTEM" }?=> REGULAR_ID + ; + +subpartition_key + : { LT(1)->getText() == "SUBPARTITION" }?=> REGULAR_ID + ; + +partition_key + : { LT(1)->getText() == "PARTITION" }?=> REGULAR_ID + ; + +matched_key + : { LT(1)->getText() == "MATCHED" }?=> REGULAR_ID + ; + +reject_key + : { LT(1)->getText() == "REJECT" }?=> REGULAR_ID + ; + +log_key + : { LT(1)->getText() == "LOG" }?=> REGULAR_ID + ; + +unlimited_key + : { LT(1)->getText() == "UNLIMITED" }?=> REGULAR_ID + ; + +limit_key + : { LT(1)->getText() == "LIMIT" }?=> REGULAR_ID + ; + +errors_key + : { LT(1)->getText() == "ERRORS" }?=> REGULAR_ID + ; + +timestamp_tz_unconstrained_key + : { LT(1)->getText() == "TIMESTAMP_TZ_UNCONSTRAINED" }?=> REGULAR_ID + ; + +urowid_key + : { LT(1)->getText() == "UROWID" }?=> REGULAR_ID + ; + +binary_float_min_subnormal_key + : { LT(1)->getText() == "BINARY_FLOAT_MIN_SUBNORMAL" }?=> REGULAR_ID + ; + +binary_double_min_normal_key + : { LT(1)->getText() == "BINARY_DOUBLE_MIN_NORMAL" }?=> REGULAR_ID + ; + +binary_float_max_normal_key + : { LT(1)->getText() == "BINARY_FLOAT_MAX_NORMAL" }?=> REGULAR_ID + ; + +positiven_key + : { LT(1)->getText() == "POSITIVEN" }?=> REGULAR_ID + ; + +timezone_abbr_key + : { LT(1)->getText() == "TIMEZONE_ABBR" }?=> REGULAR_ID + ; + +binary_double_min_subnormal_key + : { LT(1)->getText() == "BINARY_DOUBLE_MIN_SUBNORMAL" }?=> REGULAR_ID + ; + +binary_float_max_subnormal_key + : { LT(1)->getText() == "BINARY_FLOAT_MAX_SUBNORMAL" }?=> REGULAR_ID + ; + +binary_double_key + : { LT(1)->getText() == "BINARY_DOUBLE" }?=> REGULAR_ID + ; + +bfile_key + : { LT(1)->getText() == "BFILE" }?=> REGULAR_ID + ; + +binary_double_infinity_key + : { LT(1)->getText() == "BINARY_DOUBLE_INFINITY" }?=> REGULAR_ID + ; + +timezone_region_key + : { LT(1)->getText() == "TIMEZONE_REGION" }?=> REGULAR_ID + ; + +timestamp_ltz_unconstrained_key + : { LT(1)->getText() == "TIMESTAMP_LTZ_UNCONSTRAINED" }?=> REGULAR_ID + ; + +naturaln_key + : { LT(1)->getText() == "NATURALN" }?=> REGULAR_ID + ; + +simple_integer_key + : { LT(1)->getText() == "SIMPLE_INTEGER" }?=> REGULAR_ID + ; + +binary_double_max_subnormal_key + : { LT(1)->getText() == "BINARY_DOUBLE_MAX_SUBNORMAL" }?=> REGULAR_ID + ; + +byte_key + : { LT(1)->getText() == "BYTE" }?=> REGULAR_ID + ; + +binary_float_infinity_key + : { LT(1)->getText() == "BINARY_FLOAT_INFINITY" }?=> REGULAR_ID + ; + +binary_float_key + : { LT(1)->getText() == "BINARY_FLOAT" }?=> REGULAR_ID + ; + +range_key + : { LT(1)->getText() == "RANGE" }?=> REGULAR_ID + ; + +nclob_key + : { LT(1)->getText() == "NCLOB" }?=> REGULAR_ID + ; + +clob_key + : { LT(1)->getText() == "CLOB" }?=> REGULAR_ID + ; + +dsinterval_unconstrained_key + : { LT(1)->getText() == "DSINTERVAL_UNCONSTRAINED" }?=> REGULAR_ID + ; + +yminterval_unconstrained_key + : { LT(1)->getText() == "YMINTERVAL_UNCONSTRAINED" }?=> REGULAR_ID + ; + +rowid_key + : { LT(1)->getText() == "ROWID" }?=> REGULAR_ID + ; + +binary_double_nan_key + : { LT(1)->getText() == "BINARY_DOUBLE_NAN" }?=> REGULAR_ID + ; + +timestamp_unconstrained_key + : { LT(1)->getText() == "TIMESTAMP_UNCONSTRAINED" }?=> REGULAR_ID + ; + +binary_float_min_normal_key + : { LT(1)->getText() == "BINARY_FLOAT_MIN_NORMAL" }?=> REGULAR_ID + ; + +signtype_key + : { LT(1)->getText() == "SIGNTYPE" }?=> REGULAR_ID + ; + +blob_key + : { LT(1)->getText() == "BLOB" }?=> REGULAR_ID + ; + +nvarchar2_key + : { LT(1)->getText() == "NVARCHAR2" }?=> REGULAR_ID + ; + +binary_double_max_normal_key + : { LT(1)->getText() == "BINARY_DOUBLE_MAX_NORMAL" }?=> REGULAR_ID + ; + +binary_float_nan_key + : { LT(1)->getText() == "BINARY_FLOAT_NAN" }?=> REGULAR_ID + ; + +string_key + : { LT(1)->getText() == "STRING" }?=> REGULAR_ID + ; + +c_key + : { LT(1)->getText() == "C" }?=> REGULAR_ID + ; + +library_key + : { LT(1)->getText() == "LIBRARY" }?=> REGULAR_ID + ; + +context_key + : { LT(1)->getText() == "CONTEXT" }?=> REGULAR_ID + ; + +parameters_key + : { LT(1)->getText() == "PARAMETERS" }?=> REGULAR_ID + ; + +agent_key + : { LT(1)->getText() == "AGENT" }?=> REGULAR_ID + ; + +cluster_key + : { LT(1)->getText() == "CLUSTER" }?=> REGULAR_ID + ; + +hash_key + : { LT(1)->getText() == "HASH" }?=> REGULAR_ID + ; + +relies_on_key + : { LT(1)->getText() == "RELIES_ON" }?=> REGULAR_ID + ; + +returning_key + : { LT(1)->getText() == "RETURNING" }?=> REGULAR_ID + ; + +statement_id_key + : { LT(1)->getText() == "STATEMENT_ID" }?=> REGULAR_ID + ; + +deferred_key + : { LT(1)->getText() == "DEFERRED" }?=> REGULAR_ID + ; + +advise_key + : { LT(1)->getText() == "ADVISE" }?=> REGULAR_ID + ; + +resumable_key + : { LT(1)->getText() == "RESUMABLE" }?=> REGULAR_ID + ; + +timeout_key + : { LT(1)->getText() == "TIMEOUT" }?=> REGULAR_ID + ; + +parallel_key + : { LT(1)->getText() == "PARALLEL" }?=> REGULAR_ID + ; + +ddl_key + : { LT(1)->getText() == "DDL" }?=> REGULAR_ID + ; + +query_key + : { LT(1)->getText() == "QUERY" }?=> REGULAR_ID + ; + +dml_key + : { LT(1)->getText() == "DML" }?=> REGULAR_ID + ; + +guard_key + : { LT(1)->getText() == "GUARD" }?=> REGULAR_ID + ; + +nothing_key + : { LT(1)->getText() == "NOTHING" }?=> REGULAR_ID + ; + +enable_key + : { LT(1)->getText() == "ENABLE" }?=> REGULAR_ID + ; + +database_key + : { LT(1)->getText() == "DATABASE" }?=> REGULAR_ID + ; + +disable_key + : { LT(1)->getText() == "DISABLE" }?=> REGULAR_ID + ; + +link_key + : { LT(1)->getText() == "LINK" }?=> REGULAR_ID + ; + +identified_key + : PLSQL_RESERVED_IDENTIFIED + ; + +none_key + : { LT(1)->getText() == "NONE" }?=> REGULAR_ID + ; + +before_key + : { LT(1)->getText() == "BEFORE" }?=> REGULAR_ID + ; + +referencing_key + : { LT(1)->getText() == "REFERENCING" }?=> REGULAR_ID + ; + +logon_key + : { LT(1)->getText() == "LOGON" }?=> REGULAR_ID + ; + +after_key + : { LT(1)->getText() == "AFTER" }? REGULAR_ID + ; + +schema_key + : { LT(1)->getText() == "SCHEMA" }?=> REGULAR_ID + ; + +grant_key + : SQL92_RESERVED_GRANT + ; + +truncate_key + : { LT(1)->getText() == "TRUNCATE" }?=> REGULAR_ID + ; + +startup_key + : { LT(1)->getText() == "STARTUP" }?=> REGULAR_ID + ; + +statistics_key + : { LT(1)->getText() == "STATISTICS" }?=> REGULAR_ID + ; + +noaudit_key + : { LT(1)->getText() == "NOAUDIT" }?=> REGULAR_ID + ; + +suspend_key + : { LT(1)->getText() == "SUSPEND" }?=> REGULAR_ID + ; + +audit_key + : { LT(1)->getText() == "AUDIT" }?=> REGULAR_ID + ; + +disassociate_key + : { LT(1)->getText() == "DISASSOCIATE" }?=> REGULAR_ID + ; + +shutdown_key + : { LT(1)->getText() == "SHUTDOWN" }?=> REGULAR_ID + ; + +compound_key + : { LT(1)->getText() == "COMPOUND" }?=> REGULAR_ID + ; + +servererror_key + : { LT(1)->getText() == "SERVERERROR" }?=> REGULAR_ID + ; + +parent_key + : { LT(1)->getText() == "PARENT" }?=> REGULAR_ID + ; + +follows_key + : { LT(1)->getText() == "FOLLOWS" }?=> REGULAR_ID + ; + +nested_key + : { LT(1)->getText() == "NESTED" }?=> REGULAR_ID + ; + +old_key + : { LT(1)->getText() == "OLD" }?=> REGULAR_ID + ; + +statement_key + : { LT(1)->getText() == "STATEMENT" }?=> REGULAR_ID + ; + +db_role_change_key + : { LT(1)->getText() == "DB_ROLE_CHANGE" }?=> REGULAR_ID + ; + +each_key + : { LT(1)->getText() == "EACH" }?=> REGULAR_ID + ; + +logoff_key + : { LT(1)->getText() == "LOGOFF" }?=> REGULAR_ID + ; + +analyze_key + : { LT(1)->getText() == "ANALYZE" }?=> REGULAR_ID + ; + +instead_key + : { LT(1)->getText() == "INSTEAD" }?=> REGULAR_ID + ; + +associate_key + : { LT(1)->getText() == "ASSOCIATE" }?=> REGULAR_ID + ; + +new_key + : { LT(1)->getText() == "NEW" }?=> REGULAR_ID + ; + +revoke_key + : SQL92_RESERVED_REVOKE + ; + +rename_key + : { LT(1)->getText() == "RENAME" }?=> REGULAR_ID + ; + +customdatum_key + : { LT(1)->getText() == "CUSTOMDATUM" }?=> REGULAR_ID + ; + +oradata_key + : { LT(1)->getText() == "ORADATA" }?=> REGULAR_ID + ; + +constructor_key + : { LT(1)->getText() == "CONSTRUCTOR" }?=> REGULAR_ID + ; + +sqldata_key + : { LT(1)->getText() == "SQLDATA" }?=> REGULAR_ID + ; + +member_key + : { LT(1)->getText() == "MEMBER" }?=> REGULAR_ID + ; + +self_key + : { LT(1)->getText() == "SELF" }?=> REGULAR_ID + ; + +object_key + : { LT(1)->getText() == "OBJECT" }?=> REGULAR_ID + ; + +variable_key + : { LT(1)->getText() == "VARIABLE" }?=> REGULAR_ID + ; + +instantiable_key + : { LT(1)->getText() == "INSTANTIABLE" }?=> REGULAR_ID + ; + +final_key + : { LT(1)->getText() == "FINAL" }?=> REGULAR_ID + ; + +static_key + : { LT(1)->getText() == "STATIC" }?=> REGULAR_ID + ; + +oid_key + : { LT(1)->getText() == "OID" }?=> REGULAR_ID + ; + +result_key + : { LT(1)->getText() == "RESULT" }?=> REGULAR_ID + ; + +under_key + : { LT(1)->getText() == "UNDER" }?=> REGULAR_ID + ; + +map_key + : { LT(1)->getText() == "MAP" }?=> REGULAR_ID + ; + +overriding_key + : { LT(1)->getText() == "OVERRIDING" }?=> REGULAR_ID + ; + +add_key + : { LT(1)->getText() == "ADD" }?=> REGULAR_ID + ; + +modify_key + : { LT(1)->getText() == "MODIFY" }?=> REGULAR_ID + ; + +including_key + : { LT(1)->getText() == "INCLUDING" }?=> REGULAR_ID + ; + +substitutable_key + : { LT(1)->getText() == "SUBSTITUTABLE" }?=> REGULAR_ID + ; + +attribute_key + : { LT(1)->getText() == "ATTRIBUTE" }?=> REGULAR_ID + ; + +cascade_key + : { LT(1)->getText() == "CASCADE" }?=> REGULAR_ID + ; + +data_key + : { LT(1)->getText() == "DATA" }?=> REGULAR_ID + ; + +invalidate_key + : { LT(1)->getText() == "INVALIDATE" }? REGULAR_ID + ; + +element_key + : { LT(1)->getText() == "ELEMENT" }?=> REGULAR_ID + ; + +first_key + : { LT(1)->getText() == "FIRST" }?=> REGULAR_ID + ; + +check_key + : SQL92_RESERVED_CHECK + ; + +option_key + : SQL92_RESERVED_OPTION + ; + +nocycle_key + : { LT(1)->getText() == "NOCYCLE" }?=> REGULAR_ID + ; + +locked_key + : { LT(1)->getText() == "LOCKED" }?=> REGULAR_ID + ; + +block_key + : { LT(1)->getText() == "BLOCK" }?=> REGULAR_ID + ; + +xml_key + : { LT(1)->getText() == "XML" }?=> REGULAR_ID + ; + +pivot_key//: {(input.LT(1).getText(). ("PIVOT") }?=> REGULAR_ID + : PLSQL_NON_RESERVED_PIVOT + ; + +prior_key + : SQL92_RESERVED_PRIOR + ; + +sequential_key + : { LT(1)->getText() == "SEQUENTIAL" }?=> REGULAR_ID + ; + +single_key + : { LT(1)->getText() == "SINGLE" }?=> REGULAR_ID + ; + +skip_key + : { LT(1)->getText() == "SKIP" }?=> REGULAR_ID + ; + +model_key + : //{input.LT(1).getText(). ("MODEL" }?=> REGULAR_ID + PLSQL_NON_RESERVED_MODEL + ; + +updated_key + : { LT(1)->getText() == "UPDATED" }?=> REGULAR_ID + ; + +increment_key + : { LT(1)->getText() == "INCREMENT" }?=> REGULAR_ID + ; + +exclude_key + : { LT(1)->getText() == "EXCLUDE" }?=> REGULAR_ID + ; + +reference_key + : { LT(1)->getText() == "REFERENCE" }?=> REGULAR_ID + ; + +sets_key + : { LT(1)->getText() == "SETS" }?=> REGULAR_ID + ; + +until_key + : { LT(1)->getText() == "UNTIL" }?=> REGULAR_ID + ; + +seed_key + : { LT(1)->getText() == "SEED" }?=> REGULAR_ID + ; + +maxvalue_key + : { LT(1)->getText() == "MAXVALUE" }?=> REGULAR_ID + ; + +siblings_key + : { LT(1)->getText() == "SIBLINGS" }?=> REGULAR_ID + ; + +cube_key + : { LT(1)->getText() == "CUBE" }?=> REGULAR_ID + ; + +nulls_key + : { LT(1)->getText() == "NULLS" }?=> REGULAR_ID + ; + +dimension_key + : { LT(1)->getText() == "DIMENSION" }?=> REGULAR_ID + ; + +scn_key + : { LT(1)->getText() == "SCN" }?=> REGULAR_ID + ; + +snapshot_key + : { LT(1)->getText() == "SNAPSHOT" }?=> REGULAR_ID + ; + +decrement_key + : { LT(1)->getText() == "DECREMENT" }?=> REGULAR_ID + ; + +unpivot_key//: {(input.LT(1).getText(). ("UNPIVOT") }?=> REGULAR_ID + : PLSQL_NON_RESERVED_UNPIVOT + ; + +keep_key + : { LT(1)->getText() == "KEEP" }?=> REGULAR_ID + ; + +measures_key + : { LT(1)->getText() == "MEASURES" }?=> REGULAR_ID + ; + +rows_key + : { LT(1)->getText() == "ROWS" }?=> REGULAR_ID + ; + +sample_key + : { LT(1)->getText() == "SAMPLE" }?=> REGULAR_ID + ; + +upsert_key + : { LT(1)->getText() == "UPSERT" }?=> REGULAR_ID + ; + +versions_key + : { LT(1)->getText() == "VERSIONS" }?=> REGULAR_ID + ; + +rules_key + : { LT(1)->getText() == "RULES" }?=> REGULAR_ID + ; + +iterate_key + : { LT(1)->getText() == "ITERATE" }?=> REGULAR_ID + ; + +minvalue_key + : { LT(1)->getText() == "MINVALUE" }?=> REGULAR_ID + ; + +rollup_key + : { LT(1)->getText() == "ROLLUP" }?=> REGULAR_ID + ; + +nav_key + : { LT(1)->getText() == "NAV" }?=> REGULAR_ID + ; + +automatic_key + : { LT(1)->getText() == "AUTOMATIC" }?=> REGULAR_ID + ; + +last_key + : { LT(1)->getText() == "LAST" }?=> REGULAR_ID + ; + +main_key + : { LT(1)->getText() == "MAIN" }?=> REGULAR_ID + ; + +grouping_key + : { LT(1)->getText() == "GROUPING" }?=> REGULAR_ID + ; + +include_key + : { LT(1)->getText() == "INCLUDE" }?=> REGULAR_ID + ; + +ignore_key + : { LT(1)->getText() == "IGNORE" }?=> REGULAR_ID + ; + +respect_key + : { LT(1)->getText() == "RESPECT" }?=> REGULAR_ID + ; + +unique_key + : SQL92_RESERVED_UNIQUE + ; + +submultiset_key + : { LT(1)->getText() == "SUBMULTISET" }?=> REGULAR_ID + ; + +at_key + : { LT(1)->getText() == "AT" }?=> REGULAR_ID +// : SQL92_RESERVED_AT + ; + +a_key + : { LT(1)->getText() == "A" }?=> REGULAR_ID + ; + +empty_key + : { LT(1)->getText() == "EMPTY" }?=> REGULAR_ID + ; + +likec_key + : { LT(1)->getText() == "LIKEC" }?=> REGULAR_ID + ; + +nan_key + : { LT(1)->getText() == "NAN" }?=> REGULAR_ID + ; + +infinite_key + : { LT(1)->getText() == "INFINITE" }?=> REGULAR_ID + ; + +like2_key + : { LT(1)->getText() == "LIKE2" }?=> REGULAR_ID + ; + +like4_key + : { LT(1)->getText() == "LIKE4" }?=> REGULAR_ID + ; + +present_key + : { LT(1)->getText() == "PRESENT" }?=> REGULAR_ID + ; + +dbtimezone_key + : { LT(1)->getText() == "DBTIMEZONE" }?=> REGULAR_ID + ; + +sessiontimezone_key + : { LT(1)->getText() == "SESSIONTIMEZONE" }?=> REGULAR_ID + ; + +nchar_cs_key + : { LT(1)->getText() == "NCHAR_CS" }?=> REGULAR_ID + ; + +decompose_key + : { LT(1)->getText() == "DECOMPOSE" }?=> REGULAR_ID + ; + +following_key + : { LT(1)->getText() == "FOLLOWING" }?=> REGULAR_ID + ; + +first_value_key + : { LT(1)->getText() == "FIRST_VALUE" }?=> REGULAR_ID + ; + +preceding_key + : { LT(1)->getText() == "PRECEDING" }?=> REGULAR_ID + ; + +within_key + : { LT(1)->getText() == "WITHIN" }?=> REGULAR_ID + ; + +canonical_key + : { LT(1)->getText() == "CANONICAL" }?=> REGULAR_ID + ; + +compatibility_key + : { LT(1)->getText() == "COMPATIBILITY" }?=> REGULAR_ID + ; + +over_key + : { LT(1)->getText() == "OVER" }?=> REGULAR_ID + ; + +multiset_key + : { LT(1)->getText() == "MULTISET" }?=> REGULAR_ID + ; + +connect_by_root_key + : PLSQL_NON_RESERVED_CONNECT_BY_ROOT + ; + +last_value_key + : { LT(1)->getText() == "LAST_VALUE" }?=> REGULAR_ID + ; + +current_key + : SQL92_RESERVED_CURRENT + ; + +unbounded_key + : { LT(1)->getText() == "UNBOUNDED" }?=> REGULAR_ID + ; + +dense_rank_key + : { LT(1)->getText() == "DENSE_RANK" }?=> REGULAR_ID + ; + +cost_key + : { LT(1)->getText() == "COST" }?=> REGULAR_ID + ; + +char_cs_key + : { LT(1)->getText() == "CHAR_CS" }?=> REGULAR_ID + ; + +auto_key + : { LT(1)->getText() == "AUTO" }?=> REGULAR_ID + ; + +treat_key + : { LT(1)->getText() == "TREAT" }?=> REGULAR_ID + ; + +content_key + : { LT(1)->getText() == "CONTENT" }?=> REGULAR_ID + ; + +xmlparse_key + : { LT(1)->getText() == "XMLPARSE" }?=> REGULAR_ID + ; + +xmlelement_key + : { LT(1)->getText() == "XMLELEMENT" }?=> REGULAR_ID + ; + +entityescaping_key + : { LT(1)->getText() == "ENTITYESCAPING" }?=> REGULAR_ID + ; + +standalone_key + : { LT(1)->getText() == "STANDALONE" }?=> REGULAR_ID + ; + +wellformed_key + : { LT(1)->getText() == "WELLFORMED" }?=> REGULAR_ID + ; + +xmlexists_key + : { LT(1)->getText() == "XMLEXISTS" }?=> REGULAR_ID + ; + +version_key + : { LT(1)->getText() == "VERSION" }?=> REGULAR_ID + ; + +xmlcast_key + : { LT(1)->getText() == "XMLCAST" }?=> REGULAR_ID + ; + +yes_key + : { LT(1)->getText() == "YES" }?=> REGULAR_ID + ; + +no_key + : { LT(1)->getText() == "NO" }?=> REGULAR_ID + ; + +evalname_key + : { LT(1)->getText() == "EVALNAME" }?=> REGULAR_ID + ; + +xmlpi_key + : { LT(1)->getText() == "XMLPI" }?=> REGULAR_ID + ; + +xmlcolattval_key + : { LT(1)->getText() == "XMLCOLATTVAL" }?=> REGULAR_ID + ; + +document_key + : { LT(1)->getText() == "DOCUMENT" }?=> REGULAR_ID + ; + +xmlforest_key + : { LT(1)->getText() == "XMLFOREST" }?=> REGULAR_ID + ; + +passing_key + : { LT(1)->getText() == "PASSING" }?=> REGULAR_ID + ; + +columns_key //: PLSQL_RESERVED_COLUMNS + : { LT(1)->getText() == "COLUMNS" }?=> REGULAR_ID + ; + +indent_key + : { LT(1)->getText() == "INDENT" }?=> REGULAR_ID + ; + +hide_key + : { LT(1)->getText() == "HIDE" }?=> REGULAR_ID + ; + +xmlagg_key + : { LT(1)->getText() == "XMLAGG" }?=> REGULAR_ID + ; + +path_key + : { LT(1)->getText() == "PATH" }?=> REGULAR_ID + ; + +xmlnamespaces_key + : { LT(1)->getText() == "XMLNAMESPACES" }?=> REGULAR_ID + ; + +size_key + : SQL92_RESERVED_SIZE + ; + +noschemacheck_key + : { LT(1)->getText() == "NOSCHEMACHECK" }?=> REGULAR_ID + ; + +noentityescaping_key + : { LT(1)->getText() == "NOENTITYESCAPING" }?=> REGULAR_ID + ; + +xmlquery_key + : { LT(1)->getText() == "XMLQUERY" }?=> REGULAR_ID + ; + +xmltable_key + : { LT(1)->getText() == "XMLTABLE" }?=> REGULAR_ID + ; + +xmlroot_key + : { LT(1)->getText() == "XMLROOT" }?=> REGULAR_ID + ; + +schemacheck_key + : { LT(1)->getText() == "SCHEMACHECK" }?=> REGULAR_ID + ; + +xmlattributes_key + : { LT(1)->getText() == "XMLATTRIBUTES" }?=> REGULAR_ID + ; + +encoding_key + : { LT(1)->getText() == "ENCODING" }?=> REGULAR_ID + ; + +show_key + : { LT(1)->getText() == "SHOW" }?=> REGULAR_ID + ; + +xmlserialize_key + : { LT(1)->getText() == "XMLSERIALIZE" }?=> REGULAR_ID + ; + +ordinality_key + : { LT(1)->getText() == "ORDINALITY" }?=> REGULAR_ID + ; + +defaults_key + : { LT(1)->getText() == "DEFAULTS" }?=> REGULAR_ID + ; + +sqlerror_key + : { LT(1)->getText() == "SQLERROR" }? REGULAR_ID + ; + +oserror_key + : { LT(1)->getText() == "OSERROR" }? REGULAR_ID + ; + +success_key + : { LT(1)->getText() == "SUCCESS" }? REGULAR_ID + ; + +warning_key + : { LT(1)->getText() == "WARNING" }? REGULAR_ID + ; + +failure_key + : { LT(1)->getText() == "FAILURE" }? REGULAR_ID + ; + +insert_key + : SQL92_RESERVED_INSERT + ; + +order_key + : SQL92_RESERVED_ORDER + ; + +minus_key + : PLSQL_RESERVED_MINUS + ; + +row_key + : { LT(1)->getText() == "ROW" }? REGULAR_ID + ; + +mod_key + : { LT(1)->getText() == "MOD" }? REGULAR_ID + ; + +raw_key + : { LT(1)->getText() == "RAW" }?=> REGULAR_ID + ; + +power_key + : { LT(1)->getText() == "POWER" }? REGULAR_ID + ; + +lock_key + : PLSQL_RESERVED_LOCK + ; + +exists_key + : SQL92_RESERVED_EXISTS + ; + +having_key + : SQL92_RESERVED_HAVING + ; + +any_key + : SQL92_RESERVED_ANY + ; + +with_key + : SQL92_RESERVED_WITH + ; + +transaction_key + : { LT(1)->getText() == "TRANSACTION" }?=> REGULAR_ID + ; + +rawtohex_key + : { LT(1)->getText() == "RAWTOHEX" }? REGULAR_ID + ; + +number_key + : { LT(1)->getText() == "NUMBER" }?=> REGULAR_ID + ; + +nocopy_key + : { LT(1)->getText() == "NOCOPY" }?=> REGULAR_ID + ; + +to_key + : SQL92_RESERVED_TO + ; + +abs_key + : { LT(1)->getText() == "ABS" }? REGULAR_ID + ; + +rollback_key + : { LT(1)->getText() == "ROLLBACK" }?=> REGULAR_ID + ; + +share_key + : PLSQL_RESERVED_SHARE + ; + +greatest_key + : { LT(1)->getText() == "GREATEST" }? REGULAR_ID + ; + +vsize_key + : { LT(1)->getText() == "VSIZE" }? REGULAR_ID + ; + +exclusive_key + : PLSQL_RESERVED_EXCLUSIVE + ; + +varchar2_key + : { LT(1)->getText() == "VARCHAR2" }?=> REGULAR_ID + ; + +rowidtochar_key + : { LT(1)->getText() == "ROWIDTOCHAR" }? REGULAR_ID + ; + +open_key + : { LT(1)->getText() == "OPEN" }?=> REGULAR_ID + ; + +comment_key + : { LT(1)->getText() == "COMMENT" }?=> REGULAR_ID + ; + +sqrt_key + : { LT(1)->getText() == "SQRT" }? REGULAR_ID + ; + +instr_key + : { LT(1)->getText() == "INSTR" }? REGULAR_ID + ; + +nowait_key + : PLSQL_RESERVED_NOWAIT + ; + +lpad_key + : { LT(1)->getText() == "LPAD" }? REGULAR_ID + ; + +boolean_key + : { LT(1)->getText() == "BOOLEAN" }?=> REGULAR_ID + ; + +rpad_key + : { LT(1)->getText() == "RPAD" }? REGULAR_ID + ; + +savepoint_key + : { LT(1)->getText() == "SAVEPOINT" }?=> REGULAR_ID + ; + +decode_key + : { LT(1)->getText() == "DECODE" }? REGULAR_ID + ; + +reverse_key + : { LT(1)->getText() == "REVERSE" }? REGULAR_ID + ; + +least_key + : { LT(1)->getText() == "LEAST" }? REGULAR_ID + ; + +nvl_key + : { LT(1)->getText() == "NVL" }? REGULAR_ID + ; + +variance_key + : { LT(1)->getText() == "VARIANCE" }? REGULAR_ID + ; + +start_key + : PLSQL_RESERVED_START + ; + +desc_key + : SQL92_RESERVED_DESC + ; + +concat_key + : { LT(1)->getText() == "CONCAT" }? REGULAR_ID + ; + +dump_key + : { LT(1)->getText() == "DUMP" }? REGULAR_ID + ; + +soundex_key + : { LT(1)->getText() == "SOUNDEX" }? REGULAR_ID + ; + +positive_key + : { LT(1)->getText() == "POSITIVE" }?=> REGULAR_ID + ; + +union_key + : SQL92_RESERVED_UNION + ; + +ascii_key + : { LT(1)->getText() == "ASCII" }? REGULAR_ID + ; + +connect_key + : SQL92_RESERVED_CONNECT + ; + +asc_key + : SQL92_RESERVED_ASC + ; + +hextoraw_key + : { LT(1)->getText() == "HEXTORAW" }? REGULAR_ID + ; + +to_date_key + : { LT(1)->getText() == "TO_DATE" }? REGULAR_ID + ; + +floor_key + : { LT(1)->getText() == "FLOOR" }? REGULAR_ID + ; + +sign_key + : { LT(1)->getText() == "SIGN" }? REGULAR_ID + ; + +update_key + : SQL92_RESERVED_UPDATE + ; + +trunc_key + : { LT(1)->getText() == "TRUNC" }? REGULAR_ID + ; + +rtrim_key + : { LT(1)->getText() == "RTRIM" }? REGULAR_ID + ; + +close_key + : { LT(1)->getText() == "CLOSE" }?=> REGULAR_ID + ; + +to_char_key + : { LT(1)->getText() == "TO_CHAR" }? REGULAR_ID + ; + +ltrim_key + : { LT(1)->getText() == "LTRIM" }? REGULAR_ID + ; + +mode_key + : PLSQL_RESERVED_MODE + ; + +uid_key + : { LT(1)->getText() == "UID" }? REGULAR_ID + ; + +chr_key + : { LT(1)->getText() == "CHR" }? REGULAR_ID + ; + +intersect_key + : SQL92_RESERVED_INTERSECT + ; + +chartorowid_key + : { LT(1)->getText() == "CHARTOROWID" }? REGULAR_ID + ; + +mlslabel_key + : { LT(1)->getText() == "MLSLABEL" }?=> REGULAR_ID + ; + +userenv_key + : { LT(1)->getText() == "USERENV" }? REGULAR_ID + ; + +stddev_key + : { LT(1)->getText() == "STDDEV" }? REGULAR_ID + ; + +length_key + : { LT(1)->getText() == "LENGTH" }? REGULAR_ID + ; + +fetch_key + : SQL92_RESERVED_FETCH + ; + +group_key + : SQL92_RESERVED_GROUP + ; + +sysdate_key + : { LT(1)->getText() == "SYSDATE" }? REGULAR_ID + ; + +binary_integer_key + : { LT(1)->getText() == "BINARY_INTEGER" }?=> REGULAR_ID + ; + +to_number_key + : { LT(1)->getText() == "TO_NUMBER" }? REGULAR_ID + ; + +substr_key + : { LT(1)->getText() == "SUBSTR" }? REGULAR_ID + ; + +ceil_key + : { LT(1)->getText() == "CEIL" }? REGULAR_ID + ; + +initcap_key + : { LT(1)->getText() == "INITCAP" }? REGULAR_ID + ; + +round_key + : { LT(1)->getText() == "ROUND" }? REGULAR_ID + ; + +long_key + : { LT(1)->getText() == "LONG" }?=> REGULAR_ID + ; + +read_key + : { LT(1)->getText() == "READ" }?=> REGULAR_ID + ; + +only_key + : { LT(1)->getText() == "ONLY" }? REGULAR_ID + ; + +set_key + : { LT(1)->getText() == "SET" }?=> REGULAR_ID + ; + +nullif_key + : { LT(1)->getText() == "NULLIF" }? REGULAR_ID + ; + +coalesce_key + : { LT(1)->getText() == "COALESCE" }? REGULAR_ID + ; + +count_key + : { LT(1)->getText() == "COUNT" }? REGULAR_ID + ; + +avg_key : { LT(1)->getText() == "AVG" }? REGULAR_ID + ; + +max_key : { LT(1)->getText() == "MAX" }? REGULAR_ID + ; + +min_key : { LT(1)->getText() == "MIN" }? REGULAR_ID + ; + +sum_key : { LT(1)->getText() == "SUM" }? REGULAR_ID + ; + +unknown_key + : { LT(1)->getText() == "UNKNOWN" }? REGULAR_ID + ; + +escape_key + : { LT(1)->getText() == "ESCAPE" }? REGULAR_ID + ; + +some_key + : { LT(1)->getText() == "SOME" }? REGULAR_ID + ; + +match_key + : { LT(1)->getText() == "MATCH" }? REGULAR_ID + ; + +cast_key + : { LT(1)->getText() == "CAST" }? REGULAR_ID + ; + +full_key + : { LT(1)->getText() == "FULL" }?=> REGULAR_ID + ; + +partial_key + : { LT(1)->getText() == "PARTIAL" }? REGULAR_ID + ; + +character_key + : { LT(1)->getText() == "CHARACTER" }?=> REGULAR_ID + ; + +except_key + : { LT(1)->getText() == "EXCEPT" }? REGULAR_ID + ; + +char_key + : { LT(1)->getText() == "CHAR" }?=> REGULAR_ID + ; + +varying_key + : { LT(1)->getText() == "VARYING" }?=> REGULAR_ID + ; + +varchar_key + : { LT(1)->getText() == "VARCHAR" }?=> REGULAR_ID + ; + +national_key + : { LT(1)->getText() == "NATIONAL" }? REGULAR_ID + ; + +nchar_key + : { LT(1)->getText() == "NCHAR" }? REGULAR_ID + ; + +bit_key : { LT(1)->getText() == "BIT" }? REGULAR_ID + ; + +float_key + : { LT(1)->getText() == "FLOAT" }? REGULAR_ID + ; + +real_key + : { LT(1)->getText() == "REAL" }?=> REGULAR_ID + ; + +double_key + : { LT(1)->getText() == "DOUBLE" }?=> REGULAR_ID + ; + +precision_key + : { LT(1)->getText() == "PRECISION" }? REGULAR_ID + ; + +interval_key + : { LT(1)->getText() == "INTERVAL" }?=> REGULAR_ID + ; + +time_key + : { LT(1)->getText() == "TIME" }? REGULAR_ID + ; + +zone_key + : { LT(1)->getText() == "ZONE" }? REGULAR_ID + ; + +timestamp_key + : { LT(1)->getText() == "TIMESTAMP" }? REGULAR_ID + ; + +date_key//: {input.LT(1).getText(). ("DATE" }?=> REGULAR_ID + : SQL92_RESERVED_DATE + ; + +numeric_key + : { LT(1)->getText() == "NUMERIC" }?=> REGULAR_ID + ; + +decimal_key + : { LT(1)->getText() == "DECIMAL" }?=> REGULAR_ID + ; + +dec_key + : { LT(1)->getText() == "DEC" }?=> REGULAR_ID + ; + +integer_key + : { LT(1)->getText() == "INTEGER" }?=> REGULAR_ID + ; + +int_key + : { LT(1)->getText() == "INT" }?=> REGULAR_ID + ; + +smallint_key + : { LT(1)->getText() == "SMALLINT" }?=> REGULAR_ID + ; + +corresponding_key + : { LT(1)->getText() == "CORRESPONDING" }? REGULAR_ID + ; + +cross_key + : { LT(1)->getText() == "CROSS" }?=> REGULAR_ID + ; + +join_key + : { LT(1)->getText() == "JOIN" }?=> REGULAR_ID + ; + +left_key + : { LT(1)->getText() == "LEFT" }?=> REGULAR_ID + ; + +right_key + : { LT(1)->getText() == "RIGHT" }?=> REGULAR_ID + ; + +inner_key + : { LT(1)->getText() == "INNER" }?=> REGULAR_ID + ; + +natural_key + : { LT(1)->getText() == "NATURAL" }?=> REGULAR_ID + ; + +outer_key + : { LT(1)->getText() == "OUTER" }?=> REGULAR_ID + ; + +using_key + : PLSQL_NON_RESERVED_USING + ; + +indicator_key + : { LT(1)->getText() == "INDICATOR" }? REGULAR_ID + ; + +user_key + : { LT(1)->getText() == "USER" }? REGULAR_ID + ; + +current_user_key + : { LT(1)->getText() == "CURRENT_USER" }? REGULAR_ID + ; + +session_user_key + : { LT(1)->getText() == "SESSION_USER" }? REGULAR_ID + ; + +system_user_key + : { LT(1)->getText() == "SYSTEM_USER" }? REGULAR_ID + ; + +value_key + : { LT(1)->getText() == "VALUE" }? REGULAR_ID + ; + +substring_key + : { LT(1)->getText() == "SUBSTRING" }?=> REGULAR_ID + ; + +upper_key + : { LT(1)->getText() == "UPPER" }? REGULAR_ID + ; + +lower_key + : { LT(1)->getText() == "LOWER" }? REGULAR_ID + ; + +convert_key + : { LT(1)->getText() == "CONVERT" }? REGULAR_ID + ; + +translate_key + : { LT(1)->getText() == "TRANSLATE" }? REGULAR_ID + ; + +trim_key + : { LT(1)->getText() == "TRIM" }? REGULAR_ID + ; + +leading_key + : { LT(1)->getText() == "LEADING" }? REGULAR_ID + ; + +trailing_key + : { LT(1)->getText() == "TRAILING" }? REGULAR_ID + ; + +both_key + : { LT(1)->getText() == "BOTH" }? REGULAR_ID + ; + +collate_key + : { LT(1)->getText() == "COLLATE" }? REGULAR_ID + ; + +position_key + : { LT(1)->getText() == "POSITION" }? REGULAR_ID + ; + +extract_key + : { LT(1)->getText() == "EXTRACT" }? REGULAR_ID + ; + +second_key + : { LT(1)->getText() == "SECOND" }? REGULAR_ID + ; + +timezone_hour_key + : { LT(1)->getText() == "TIMEZONE_HOUR" }? REGULAR_ID + ; + +timezone_minute_key + : { LT(1)->getText() == "TIMEZONE_MINUTE" }? REGULAR_ID + ; + +char_length_key + : { LT(1)->getText() == "CHAR_LENGTH" }? REGULAR_ID + ; + +octet_length_key + : { LT(1)->getText() == "OCTET_LENGTH" }? REGULAR_ID + ; + +character_length_key + : { LT(1)->getText() == "CHARACTER_LENGTH" }? REGULAR_ID + ; + +bit_length_key + : { LT(1)->getText() == "BIT_LENGTH" }? REGULAR_ID + ; + +local_key + : { LT(1)->getText() == "LOCAL" }? REGULAR_ID + ; + +current_timestamp_key + : { LT(1)->getText() == "CURRENT_TIMESTAMP" }? REGULAR_ID + ; + +current_date_key + : { LT(1)->getText() == "CURRENT_DATE" }? REGULAR_ID + ; + +current_time_key + : { LT(1)->getText() == "CURRENT_TIME" }? REGULAR_ID + ; + +module_key + : { LT(1)->getText() == "MODULE" }? REGULAR_ID + ; + +global_key + : { LT(1)->getText() == "GLOBAL" }? REGULAR_ID + ; + +year_key + : { LT(1)->getText() == "YEAR" }?=> REGULAR_ID + ; + +month_key + : { LT(1)->getText() == "MONTH" }? REGULAR_ID + ; + +day_key + : { LT(1)->getText() == "DAY" }?=> REGULAR_ID + ; + +hour_key + : { LT(1)->getText() == "HOUR" }? REGULAR_ID + ; + +minute_key + : { LT(1)->getText() == "MINUTE" }? REGULAR_ID + ; + +whenever_key + : { LT(1)->getText() == "WHENEVER" }? REGULAR_ID + ; + +is_key + : SQL92_RESERVED_IS + ; + +else_key + : SQL92_RESERVED_ELSE + ; + +table_key + : SQL92_RESERVED_TABLE + ; + +the_key + : SQL92_RESERVED_THE + ; + +then_key + : SQL92_RESERVED_THEN + ; + +end_key + : SQL92_RESERVED_END + ; + +all_key + : SQL92_RESERVED_ALL + ; + +on_key + : SQL92_RESERVED_ON + ; + +or_key + : SQL92_RESERVED_OR + ; + +and_key + : SQL92_RESERVED_AND + ; + +not_key + : SQL92_RESERVED_NOT + ; + +true_key + : SQL92_RESERVED_TRUE + ; + +false_key + : SQL92_RESERVED_FALSE + ; + +default_key + : SQL92_RESERVED_DEFAULT + ; + +distinct_key + : SQL92_RESERVED_DISTINCT + ; + +into_key + : SQL92_RESERVED_INTO + ; + +by_key + : SQL92_RESERVED_BY + ; + +as_key + : SQL92_RESERVED_AS + ; + +in_key + : SQL92_RESERVED_IN + ; + +of_key + : SQL92_RESERVED_OF + ; + +null_key + : SQL92_RESERVED_NULL + ; + +for_key + : SQL92_RESERVED_FOR + ; + +select_key + : SQL92_RESERVED_SELECT + ; + +when_key + : SQL92_RESERVED_WHEN + ; + +delete_key + : SQL92_RESERVED_DELETE + ; + +between_key + : SQL92_RESERVED_BETWEEN + ; + +like_key + : SQL92_RESERVED_LIKE + ; + +from_key + : SQL92_RESERVED_FROM + ; + +where_key + : SQL92_RESERVED_WHERE + ; + +sequence_key + : { LT(1)->getText() == "SEQUENCE" }? REGULAR_ID + ; + +noorder_key + : { LT(1)->getText() == "NOORDER" }? REGULAR_ID + ; + +cycle_key + : { LT(1)->getText() == "CYCLE" }? REGULAR_ID + ; + +cache_key + : { LT(1)->getText() == "CACHE" }? REGULAR_ID + ; + +nocache_key + : { LT(1)->getText() == "NOCACHE" }? REGULAR_ID + ; + +nomaxvalue_key + : { LT(1)->getText() == "NOMAXVALUE" }? REGULAR_ID + ; + +nominvalue_key + : { LT(1)->getText() == "NOMINVALUE" }? REGULAR_ID + ; + +search_key + : { LT(1)->getText() == "SEARCH" }? REGULAR_ID + ; + +depth_key + : { LT(1)->getText() == "DEPTH" }? REGULAR_ID + ; + +breadth_key + : { LT(1)->getText() == "BREADTH" }? REGULAR_ID + ; diff --git a/runtime/Cpp/tests/PLSQLLexer.g b/runtime/Cpp/tests/PLSQLLexer.g new file mode 100644 index 000000000..9dbe451bc --- /dev/null +++ b/runtime/Cpp/tests/PLSQLLexer.g @@ -0,0 +1,1070 @@ +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +lexer grammar PLSQLLexer; + +options { + language=Cpp; +// filter=true; +} + +tokens { // moved to the import vocabulary + UNSIGNED_INTEGER; // Imaginary token based on subtoken typecasting - see the rule + APPROXIMATE_NUM_LIT; // Imaginary token based on subtoken typecasting - see the rule + MINUS_SIGN; // Imaginary token based on subtoken typecasting - see the rule + DOUBLE_PERIOD; + UNDERSCORE; // Imaginary token based on subtoken typecasting - see the rule + + //TORA TOKENS + T_UNKNOWN; + T_RESERVED; + // Identifier subtypes AST leafs + T_TABLE_ALIAS; + T_COLUMN_ALIAS; + T_COLUMN_NAME; + T_TABLE_NAME; + T_SCHEMA_NAME; + T_FUNCTION_NAME; + T_PACKAGE_NAME; + T_DBLINK_NAME; + T_BINDVAR_NAME; + + //Alias type(declaration, usage) + T_DECL; + T_USE ; + + //Tree nodes + T_IDENTIFIER; + T_TABLE_REF; + T_WITH; + T_SELECT; + T_SUBQUERY; + T_TABLE_CAST; + T_COLUMN_LIST; + T_SELECT_COLUMN; + T_FROM; + //T_SELECTED_TABLE; + T_WHERE; + T_HIERARCHICAL; + T_GROUP_BY; + T_MODEL; + T_UNION; + T_ORDER_BY_CLAUSE; + T_FOR_UPDATE_CLAUSE; + T_JOINING_CLAUSE; + + T_OPERATOR_UNARY; + T_OPERATOR_BINARY; + T_OPERATOR_PART; + T_COND_OR; + T_COND_OR_SEQ; + T_COND_AND; + T_COND_AND_SEQ; + T_COND_NOT; + T_COND_EXISTS; + T_COND_IS; + T_COND_COMPARISON; + T_COND_GROUP_COMPARISON; + T_COND_IN; + T_COND_IS_A_SET; + T_COND_IS_ANY; + T_COND_IS_EMPTY; + T_COND_IS_OF_TYPE; + T_COND_IS_PRESENT; + T_COND_LIKE; + T_COND_MEMEBER; + T_COND_BETWEEN; + T_COND_REGEXP_LIKE; + T_COND_SUBMULTISET; + T_COND_EQUALS_PATH; + T_COND_UNDER_PATH; + T_COND_PAREN; + + EXPLAIN_STATEMENT; + SELECT_STATEMENT; + FACTORING; + SUBQUERY; + SELECT_LIST; + SELECT_ITEM; + DOT_ASTERISK; + TABLE_REF; + TABLE_REF_ELEMENT; + JOIN_DEF; + LOGIC_EXPR; + SELECTED_TABLEVIEW; + TABLE_EXPRESSION; + COLLECTION_MODE; + SELECT_MODE; + DIRECT_MODE; + PIVOT_ELEMENT; + ANY_MODE; + ELEMENTS_MODE; + PIVOT_IN_ELEMENT; + UNPIVOT_IN_ELEMENT; + HIERARCHICAL; + GROUP_BY_CLAUSE; + GROUP_BY_ELEMENT; + GROUPIN_SET; + MAIN_MODEL; + MODEL_COLUMN; + MODEL_COLUMNS; + MODEL_RULES; + MODEL_RULE; + ASSIGN; + ORDER_BY_ELEMENTS; + ORDER_BY_ELEMENT; + SINGLE_TABLE_MODE; + MULTI_TABLE_MODE; + TABLE_ELEMENT; + CONDITIONAL_INSERT; + COLUMNS; + MERGE_UPDATE; + MERGE_INSERT; + LOCK_TABLE_ELEMENT; + STATIC_RETURNING; + EXPR_LIST; + IS_NOT_NULL; + IS_NULL; + IS_NOT_NAN; + IS_NAN; + IS_NOT_PRESENT; + IS_PRESENT; + IS_NOT_INFINITE; + IS_INFINITE; + IS_NOT_A_SET; + IS_A_SET; + IS_NOT_EMPTY; + IS_EMPTY; + IS_NOT_OF_TYPE; + IS_OF_TYPE; + NOT_IN; + NOT_BETWEEN; + NOT_LIKE; + UNARY_OPERATOR; + STANDARD_FUNCTION; + MODEL_EXPRESSION; + FOR_SINGLE_COLUMN; + FOR_MULTI_COLUMN; + SIMPLE_CASE; + SEARCHED_CASE; + VECTOR_EXPR; + FUNCTION_ENABLING_OVER; + FUNCTION_ENABLING_USING; + FUNCTION_ENABLING_WITHIN_OR_OVER; + PREDICTION_FUNCTION; + ELEMENT; + XML_COLUMN; + XML_ALIAS; + PIVOT_ALIAS; + DATETIME_OP; + XML_ELEMENT; + EXPR; + ROUTINE_CALL; + ARGUMENTS; + ARGUMENT; +} + +@lexer::includes +{ + #include "PTestTraits.hpp" +} +@lexer::namespace +{ +Antlr3Test +} + +@header { +/* + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +} + +@members { +// // buffer (queue) to hold the emit()'d tokens +// // private LinkedList tokenBuffer = new LinkedList(); + +// // public void emit(Token t) { +// // tokenBuffer.add(t); +// // } + +// // private void advanceInput(){ +// // state.tokenStartCharIndex = input.index(); +// // state.tokenStartCharPositionInLine = input.getCharPositionInLine(); +// // state.tokenStartLine = input.getLine(); +// // } + + + /** + * Return a token from this source; i.e., match a token on the char stream. + */ + // public Token nextToken() { + // while (true) { + // if (tokenBuffer.size() == 0) { + // state.token = null; + // state.channel = Token.DEFAULT_CHANNEL; + // state.tokenStartCharIndex = input.index(); + // state.tokenStartCharPositionInLine = input.getCharPositionInLine(); + // state.tokenStartLine = input.getLine(); + // state.text = null; + // if (input.LA(1) == CharStream.EOF) { + // return Token.EOF_TOKEN; + // } + // try { + // int m = input.mark(); + // state.backtracking = 1; + // state.failed = false; + // mTokens(); + // state.backtracking = 0; + + // if (state.failed) { + // input.rewind(m); + // input.consume(); + // } else { + // emit(); + // } + // } catch (RecognitionException re) { + // // shouldn't happen in backtracking mode, but... + // reportError(re); + // recover(re); + // } + // } else { + // Token result = tokenBuffer.poll(); + // if (result == Token.SKIP_TOKEN || result.getType() == Token.INVALID_TOKEN_TYPE || result == null) + // { + // // discard + // // SKIP & INVALID + // // tokens + // continue; + // } + // return result; + // } + // } + // } +} + +FOR_NOTATION + : UNSIGNED_INTEGER + ///{state.type = UNSIGNED_INTEGER; emit(); advanceInput();} + '..' + ///{state.type = DOUBLE_PERIOD; emit(); advanceInput();} + UNSIGNED_INTEGER + ///{state.type = UNSIGNED_INTEGER; emit(); advanceInput(); $channel=HIDDEN;} + ; + +//{ Rule #358 - subtoken typecast in , it also incorporates +// Lowercase 'n' is a usual addition to the standard +NATIONAL_CHAR_STRING_LIT + : ('N' | 'n') '\'' (options{greedy=true;}: ~('\'' | '\r' | '\n' ) | '\'' '\'' | NEWLINE)* '\'' + ; +//} + +//{ Rule #040 - subtoken typecast in +// Lowercase 'b' is a usual addition to the standard +BIT_STRING_LIT + : ('B' | 'b') ('\'' ('0' | '1')* '\'' SEPARATOR? )+ + ; +//} + + +//{ Rule #284 - subtoken typecast in +// Lowercase 'x' is a usual addition to the standard +HEX_STRING_LIT + : ('X' | 'x') ('\'' ('a'..'f' | 'A'..'F' | '0'..'9')* '\'' SEPARATOR? )+ + ; +//} + +PERIOD + : '.' + { if ((char) LA(1) == '.') { + consume(); + $type = DOUBLE_PERIOD; + } + } + ; + +//{ Rule #238 +// This rule is a bit tricky - it resolves the ambiguity with +// It als44o incorporates and for the +// Rule #501 was incorporated directly in the token +// See also the rule #617 +EXACT_NUM_LIT + : ( + UNSIGNED_INTEGER + ( '.' UNSIGNED_INTEGER? + | {$type = UNSIGNED_INTEGER;} + ) ( ('E' | 'e') ('+' | '-')? UNSIGNED_INTEGER {$type = APPROXIMATE_NUM_LIT;} )? + | '.' UNSIGNED_INTEGER ( ('E' | 'e') ('+' | '-')? UNSIGNED_INTEGER {$type = APPROXIMATE_NUM_LIT;} )? + ) + ( 'D' | 'd' | 'f' | 'F')? + ; +//} + +//{ Rule #--- is a base for Rule #065 , it incorporates +// and a superfluous subtoken typecasting of the "QUOTE" +CHAR_STRING + : '\'' (options{greedy=true;}: ~('\'' | '\r' | '\n') | '\'' '\'' | NEWLINE)* '\'' + ; +//} + +// Perl-style quoted string, see Oracle SQL reference, chapter String Literals +CHAR_STRING_PERL : ('q'|'Q') ( QS_ANGLE | QS_BRACE | QS_BRACK | QS_PAREN | QS_OTHER) {$type = CHAR_STRING;}; +fragment QUOTE : '\'' ; +fragment QS_ANGLE : QUOTE '<' ( options {greedy=false;} : . )* '>' QUOTE ; +fragment QS_BRACE : QUOTE '{' ( options {greedy=false;} : . )* '}' QUOTE ; +fragment QS_BRACK : QUOTE '[' ( options {greedy=false;} : . )* ']' QUOTE ; +fragment QS_PAREN : QUOTE '(' ( options {greedy=false;} : . )* ')' QUOTE ; + +fragment QS_OTHER_CH: ~('<'|'{'|'['|'('|' '|'\t'|'\n'|'\r'); +fragment QS_OTHER +// For C target we have to preserve case sensitivity. + // @declarations { + // ANTLR3_UINT32 (*oldLA)(struct ANTLR3_INT_STREAM_struct *, ANTLR3_INT32); + // } + // @init { + // oldLA = INPUT->istream->_LA; + // INPUT->setUcaseLA(INPUT, ANTLR3_FALSE); + // } + : + QUOTE delimiter=QS_OTHER_CH +/* JAVA Syntax */ +// ( { input.LT(1) != $delimiter.text.charAt(0) || ( input.LT(1) == $delimiter.text.charAt(0) && input.LT(2) != '\'') }? => . )* +// ( { input.LT(1) == $delimiter.text.charAt(0) && input.LT(2) == '\'' }? => . ) QUOTE +/* C Syntax */ +// ( { LA(1) != $delimiter->getText()->chars[0] || LA(2) != '\'' }? => . )* +// ( { LA(1) == $delimiter->getText()->chars[0] && LA(2) == '\'' }? => . ) QUOTE +// { INPUT->istream->_LA = oldLA; } +/* C++ Syntax */ + ( { LA(1) != $delimiter->getText().at(0) || LA(2) != '\'' }? => . )* + ( { LA(1) == $delimiter->getText().at(0) && LA(2) == '\'' }? => . ) QUOTE + ; + + +//{ Rule #163 +DELIMITED_ID + : '"' (~('"' | '\r' | '\n') | '"' '"')+ '"' + ; +//} + +//{ Rule #546 was split into single rules +PERCENT + : '%' + ; + +AMPERSAND + : '&' + ; + +LEFT_PAREN + : '(' + ; + +RIGHT_PAREN + : ')' + ; + +DOUBLE_ASTERISK + : '**' + ; + +ASTERISK + : '*' + ; + +PLUS_SIGN + : '+' + ; + +COMMA + : ',' + ; + +SOLIDUS + : '/' + ; + +AT_SIGN + : '@' + ; + +ASSIGN_OP + : ':=' + ; + +// See OCI reference for more information about this +BINDVAR + : COLON SIMPLE_LETTER ( SIMPLE_LETTER | '0' .. '9' | '_' )* + | COLON DELIMITED_ID // not used in SQL but spotted in v$sqltext when using cursor_sharing + | COLON UNSIGNED_INTEGER + | QUESTION_MARK // not in SQL, not in Oracle, not in OCI, use this for JDBC + ; + +COLON + : ':' + ; + +SEMICOLON + : ';' + ; + +LESS_THAN_OR_EQUALS_OP + : '<=' + ; + +LESS_THAN_OP + : '<' + ; + +GREATER_THAN_OR_EQUALS_OP + : '>=' + ; + +NOT_EQUAL_OP + : '!=' + | '<>' + | '^=' + | '~=' + ; +CARRET_OPERATOR_PART + : '^' + ; + +TILDE_OPERATOR_PART + : '~' + ; + +EXCLAMATION_OPERATOR_PART + : '!' + ; + +GREATER_THAN_OP + : '>' + ; + +fragment +QUESTION_MARK + : '?' + ; + +// protected UNDERSCORE : '_' SEPARATOR ; // subtoken typecast within +CONCATENATION_OP + : '||' + ; + +VERTICAL_BAR + : '|' + ; + +EQUALS_OP + : '=' + ; + +//{ Rule #532 was split into single rules: +LEFT_BRACKET + : '[' + ; + +RIGHT_BRACKET + : ']' + ; + +//} + +//{ Rule #319 +INTRODUCER + : '_' (SEPARATOR {$type = UNDERSCORE;})? + ; + +//{ Rule #479 +// It was originally a protected rule set to be filtered out but the and clashed. +SEPARATOR + : '-' {$type = MINUS_SIGN;} + | COMMENT { $channel=HIDDEN; } + | (SPACE | NEWLINE)+ { $channel=HIDDEN; } + ; +//} + +//{ Rule #504 - simple_latin _letter was generalised into SIMPLE_LETTER +// Unicode is yet to be implemented - see NSF0 +fragment +SIMPLE_LETTER + : 'a'..'z' + | 'A'..'Z' + ; +//} + +// Rule #176 was incorporated by +//{ Rule #615 - subtoken typecast in +fragment +UNSIGNED_INTEGER + : ('0'..'9')+ + ; +//} + +//{ Rule #097 +fragment +COMMENT + : '--' ( ~('\r' | '\n') )* (NEWLINE|EOF) + | '/*' (options{greedy=false;} : .)* '*/' + ; +//} + +// SQL*Plus prompt +// TODO should be grammar rule, but tricky to implement +PROMPT + : 'PROMPT' SPACE ( ~('\r' | '\n') )* (NEWLINE|EOF) + ; +//} + + + +//{ Rule #360 +fragment +NEWLINE + : '\r' (options{greedy=true;}: '\n')? + | '\n' + ; +//} + +//{ Rule #522 +fragment +SPACE : ' ' + | '\t' + ; +//} + +fragment APPROXIMATE_NUM_LIT: ; +fragment MINUS_SIGN: ; +fragment UNDERSCORE: ; +fragment DOUBLE_PERIOD: ; + +//{ Rule #442 additionally encapsulates a few STRING_LITs. +// Within testLiterals all reserved and non-reserved words are being resolved + +SQL92_RESERVED_ALL + : 'ALL' + ; + +SQL92_RESERVED_ALTER + : 'ALTER' + ; + +SQL92_RESERVED_AND + : 'AND' + ; + +SQL92_RESERVED_ANY + : 'ANY' + ; + +SQL92_RESERVED_AS + : 'AS' + ; + +SQL92_RESERVED_ASC + : 'ASC' + ; + +//SQL92_RESERVED_AT +// : 'AT' +// ; + +SQL92_RESERVED_BEGIN + : 'BEGIN' + ; + +SQL92_RESERVED_BETWEEN + : 'BETWEEN' + ; + +SQL92_RESERVED_BY + : 'BY' + ; + +SQL92_RESERVED_CASE + : 'CASE' + ; + +SQL92_RESERVED_CHECK + : 'CHECK' + ; + +PLSQL_RESERVED_CLUSTERS + : 'CLUSTERS' + ; + +PLSQL_RESERVED_COLAUTH + : 'COLAUTH' + ; + +PLSQL_RESERVED_COMPRESS + : 'COMPRESS' + ; + +SQL92_RESERVED_CONNECT + : 'CONNECT' + ; + +//PLSQL_NON_RESERVED_COLUMNS +// : 'COLUMNS' +// ; + +PLSQL_NON_RESERVED_CONNECT_BY_ROOT + : 'CONNECT_BY_ROOT' + ; + +PLSQL_RESERVED_CRASH + : 'CRASH' + ; + +SQL92_RESERVED_CREATE + : 'CREATE' + ; + +SQL92_RESERVED_CURRENT + : 'CURRENT' + ; + +SQL92_RESERVED_CURSOR + : 'CURSOR' + ; + +SQL92_RESERVED_DATE + : 'DATE' + ; + +SQL92_RESERVED_DECLARE + : 'DECLARE' + ; + +SQL92_RESERVED_DEFAULT + : 'DEFAULT' + ; + +SQL92_RESERVED_DELETE + : 'DELETE' + ; + +SQL92_RESERVED_DESC + : 'DESC' + ; + +SQL92_RESERVED_DISTINCT + : 'DISTINCT' + ; + +SQL92_RESERVED_DROP + : 'DROP' + ; + +SQL92_RESERVED_ELSE + : 'ELSE' + ; + +SQL92_RESERVED_END + : 'END' + ; + +SQL92_RESERVED_EXCEPTION +//@declarations { +// ANTLR3_MARKER excStart; // ANTLR should declare it for us, but it does not +//} + : 'EXCEPTION' + // "exception" is a keyword only withing the contex of the PL/SQL language + // while it can be an identifier(column name, table name) in SQL + // "exception" is a keyword if and only it is followed by "when" + //{ + // $type = SQL92_RESERVED_EXCEPTION; + // excStart = MARK(); + // exc->set_type(exc, SQL92_RESERVED_EXCEPTION); + // // emit(exc); + // // advanceInput(); + + // $type = Token.INVALID_TOKEN_TYPE; + // int markModel = input.mark(); + + // // Now loop over next Tokens in the input and eventually set Token's type to REGULAR_ID + + // // Subclassed version will return NULL unless EOF is reached. + // // nextToken either returns NULL => then the next token is put into the queue tokenBuffer + // // or it returns Token.EOF, then nothing is put into the queue + // Token t1 = super.nextToken(); + // { // This "if" handles the situation when the "model" is the last text in the input. + // if( t1 != null && t1.getType() == Token.EOF) + // { + // $e.set_type(REGULAR_ID); + // } else { + // t1 = tokenBuffer.pollLast(); // "withdraw" the next token from the queue + // while(true) + // { + // if(t1.getType() == EOF) // is it EOF? + // { + // $e.set_type(REGULAR_ID); + // break; + // } + + // if(t1.getChannel() == HIDDEN) // is it a white space? then advance to the next token + // { + // t1 = super.nextToken(); if( t1 == null) { t1 = tokenBuffer.pollLast(); }; + // continue; + // } + + // if( t1.getType() != SQL92_RESERVED_WHEN && t1.getType() != SEMICOLON) // is something other than "when" + // { + // $e.set_type(REGULAR_ID); + // break; + // } + + // break; // we are in the model_clase do not rewrite anything + // } // while true + // } // else if( t1 != null && t1.getType() == Token.EOF) + // } + // input.rewind(markModel); + // REWIND(excStart); + //} + ; + +PLSQL_RESERVED_EXCLUSIVE + : 'EXCLUSIVE' + ; + +SQL92_RESERVED_EXISTS + : 'EXISTS' + ; + +SQL92_RESERVED_FALSE + : 'FALSE' + ; + +SQL92_RESERVED_FETCH + : 'FETCH' + ; + +SQL92_RESERVED_FOR + : 'FOR' + ; + +SQL92_RESERVED_FROM + : 'FROM' + ; + +SQL92_RESERVED_GOTO + : 'GOTO' + ; + +SQL92_RESERVED_GRANT + : 'GRANT' + ; + +SQL92_RESERVED_GROUP + : 'GROUP' + ; + +SQL92_RESERVED_HAVING + : 'HAVING' + ; + +PLSQL_RESERVED_IDENTIFIED + : 'IDENTIFIED' + ; + +PLSQL_RESERVED_IF + : 'IF' + ; + +SQL92_RESERVED_IN + : 'IN' + ; + +PLSQL_RESERVED_INDEX + : 'INDEX' + ; + +PLSQL_RESERVED_INDEXES + : 'INDEXES' + ; + +SQL92_RESERVED_INSERT + : 'INSERT' + ; + +SQL92_RESERVED_INTERSECT + : 'INTERSECT' + ; + +SQL92_RESERVED_INTO + : 'INTO' + ; + +SQL92_RESERVED_IS + : 'IS' + ; + +SQL92_RESERVED_LIKE + : 'LIKE' + ; + +PLSQL_RESERVED_LOCK + : 'LOCK' + ; + +PLSQL_RESERVED_MINUS + : 'MINUS' + ; + +PLSQL_RESERVED_MODE + : 'MODE' + ; + +PLSQL_RESERVED_NOCOMPRESS + : 'NOCOMPRESS' + ; + +SQL92_RESERVED_NOT + : 'NOT' + ; + +PLSQL_RESERVED_NOWAIT + : 'NOWAIT' + ; + +SQL92_RESERVED_NULL + : 'NULL' + ; + +SQL92_RESERVED_OF + : 'OF' + ; + +SQL92_RESERVED_ON + : 'ON' + ; + +SQL92_RESERVED_OPTION + : 'OPTION' + ; + +SQL92_RESERVED_OR + : 'OR' + ; + +SQL92_RESERVED_ORDER + : 'ORDER' + ; + +SQL92_RESERVED_OVERLAPS + : 'OVERLAPS' + ; + +SQL92_RESERVED_PRIOR + : 'PRIOR' + ; + +SQL92_RESERVED_PROCEDURE + : 'PROCEDURE' + ; + +SQL92_RESERVED_PUBLIC + : 'PUBLIC' + ; + +PLSQL_RESERVED_RESOURCE + : 'RESOURCE' + ; + +SQL92_RESERVED_REVOKE + : 'REVOKE' + ; + +SQL92_RESERVED_SELECT + : 'SELECT' + ; + +PLSQL_RESERVED_SHARE + : 'SHARE' + ; + +SQL92_RESERVED_SIZE + : 'SIZE' + ; + +// SQL92_RESERVED_SQL +// : 'SQL' +// ; + +PLSQL_RESERVED_START + : 'START' + ; + +PLSQL_RESERVED_TABAUTH + : 'TABAUTH' + ; + +SQL92_RESERVED_TABLE + : 'TABLE' + ; + +SQL92_RESERVED_THE + : 'THE' + ; + +SQL92_RESERVED_THEN + : 'THEN' + ; + +SQL92_RESERVED_TO + : 'TO' + ; + +SQL92_RESERVED_TRUE + : 'TRUE' + ; + +SQL92_RESERVED_UNION + : 'UNION' + ; + +SQL92_RESERVED_UNIQUE + : 'UNIQUE' + ; + +SQL92_RESERVED_UPDATE + : 'UPDATE' + ; + +SQL92_RESERVED_VALUES + : 'VALUES' + ; + +SQL92_RESERVED_VIEW + : 'VIEW' + ; + +PLSQL_RESERVED_VIEWS + : 'VIEWS' + ; + +SQL92_RESERVED_WHEN + : 'WHEN' + ; + +SQL92_RESERVED_WHERE + : 'WHERE' + ; + +SQL92_RESERVED_WITH + : 'WITH' + ; + +PLSQL_NON_RESERVED_USING + : 'USING' + ; + +PLSQL_NON_RESERVED_MODEL + : 'MODEL' + { + // "MODEL" is a keyword if and only if it is followed by ("main"|"partition"|"dimension") + // otherwise it is a identifier(REGULAR_ID). + // This wodoo implements something like context sensitive lexer. + // Here we've matched the word "model". Then the Token is created and en-queued in tokenBuffer + // We still remember the reference(m) onto this Token + $type = PLSQL_NON_RESERVED_MODEL; + // emit(m); + // advanceInput(); + + // $type = Token.INVALID_TOKEN_TYPE; + // int markModel = input.mark(); + + // // Now loop over next Tokens in the input and eventually set Token's type to REGULAR_ID + + // // Subclassed version will return NULL unless EOF is reached. + // // nextToken either returns NULL => then the next token is put into the queue tokenBuffer + // // or it returns Token.EOF, then nothing is put into the queue + // Token t1 = super.nextToken(); + // { // This "if" handles the situation when the "model" is the last text in the input. + // if( t1 != null && t1.getType() == Token.EOF) + // { + // $m.set_type(REGULAR_ID); + // } else { + // t1 = tokenBuffer.pollLast(); // "withdraw" the next token from the queue + // while(true) + // { + // if(t1.getType() == EOF) // is it EOF? + // { + // $m.set_type(REGULAR_ID); + // break; + // } + + // if(t1.getChannel() == HIDDEN) // is it a white space? then advance to the next token + // { + // t1 = super.nextToken(); if( t1 == null) { t1 = tokenBuffer.pollLast(); }; + // continue; + // } + + // if( t1.getType() != REGULAR_ID || // is something other than ("main"|"partition"|"dimension") + // ( !t1.getText().equalsIgnoreCase("main") && + // !t1.getText().equalsIgnoreCase("partition") && + // !t1.getText().equalsIgnoreCase("dimension") + // )) + // { + // $m.set_type(REGULAR_ID); + // break; + // } + + // break; // we are in the model_clase do not rewrite anything + // } // while true + // } // else if( t1 != null && t1.getType() == Token.EOF) + // } + // input.rewind(markModel); + } + ; + +PLSQL_NON_RESERVED_ELSIF + : 'ELSIF' + ; + +PLSQL_NON_RESERVED_PIVOT + : 'PIVOT' + ; + +PLSQL_NON_RESERVED_UNPIVOT + : 'UNPIVOT' + ; + +REGULAR_ID + : (SIMPLE_LETTER) (SIMPLE_LETTER | '$' | '_' | '#' | '0'..'9')* + ; + +ZV + : '@!' {$channel=HIDDEN;} + ; + +// disambiguate these diff --git a/runtime/Cpp/tests/PLSQLParser.g b/runtime/Cpp/tests/PLSQLParser.g new file mode 100644 index 000000000..7b4d813d7 --- /dev/null +++ b/runtime/Cpp/tests/PLSQLParser.g @@ -0,0 +1,1104 @@ +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +parser grammar PLSQLParser; + +options { + tokenVocab=PLSQLLexer; + language=Cpp; + //memoize=true; + output=AST; +} + +import PLSQLKeys, PLSQLCommons, PLSQL_DMLParser, SQLPLUSParser; + +@parser::includes +{ + #include "PTestTraits.hpp" + #include "PLSQLLexer.hpp" +} +@parser::namespace { Antlr3Test } + +@header { +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +} + +swallow_to_semi + : ~( SEMICOLON )+ + ; + +compilation_unit + : unit_statement* EOF + ; + +sql_script + : (unit_statement|sql_plus_command)* EOF + ; + +unit_statement +options{ +backtrack=true; +} : alter_function + | alter_package + | alter_procedure + | alter_sequence + | alter_trigger + | alter_type + + | create_function_body + | create_procedure_body + | create_package + +// | create_index //TODO +// | create_table //TODO +// | create_view //TODO +// | create_directory //TODO +// | create_materialized_view //TODO + + | create_sequence + | create_trigger + | create_type + + | drop_function + | drop_package + | drop_procedure + | drop_sequence + | drop_trigger + | drop_type + | data_manipulation_language_statements + ; + +// $ SQL Statements for Stored PL/SQL Units + +// $ +// $> + +// $ + +// $> + +// $ + +// $ compound_trigger_block + | (call_key id)=> call_key routine_clause + | trigger_block + ; + +routine_clause + : routine_name function_argument? + ; + +compound_trigger_block + : compound_key trigger_key declare_spec* timing_point_section+ end_key trigger_name + ; + +timing_point_section +options{ +k=3; +} : bk=before_key statement_key is_key trigger_block before_key statement_key SEMICOLON + | bk=before_key each_key row_key is_key trigger_block before_key each_key row_key SEMICOLON + | ak=after_key statement_key is_key trigger_block after_key statement_key SEMICOLON + | ak=after_key each_key row_key is_key trigger_block after_key each_key row_key SEMICOLON + ; + +non_dml_event + : alter_key + | analyze_key + | associate_key statistics_key + | audit_key + | comment_key + | create_key + | disassociate_key statistics_key + | drop_key + | grant_key + | noaudit_key + | rename_key + | revoke_key + | truncate_key + | ddl_key + | startup_key + | shutdown_key + | db_role_change_key + | logon_key + | logoff_key + | servererror_key + | suspend_key + | database_key + | schema_key + | follows_key + ; + +dml_event_clause + : dml_event_element (or_key dml_event_element)* + on_key + dml_event_nested_clause? tableview_name + ; + +dml_event_element + : (delete_key|insert_key|update_key) (of_key column_name (COMMA column_name)*)? + ; + +dml_event_nested_clause + : nested_key table_key tableview_name of_key + ; + +referencing_clause + : referencing_key referencing_element+ + ; + +referencing_element + : ( new_key | old_key | parent_key ) column_alias + ; + +// $> +// $> + +// $getText() == "ATTRIBUTE" }? alter_attribute_definition + | alter_method_spec + | alter_collection_clauses + | modifier_clause + ) + dependent_handling_clause? + SEMICOLON + ; + +// $ + +create_type + : create_key (or_key replace_key)? type_key + ( type_definition | type_body) + SEMICOLON + ; + +// $ + +// $ +// $> + +// $ +// $> + +// $ + +parameter + : parameter_name ( in_key | out_key | inout_key | nocopy_key)* type_spec? default_value_part? + ; + +default_value_part + : (ASSIGN_OP|default_key) expression + ; + +// $> + +// $> + +// $ + +table_declaration + : (table_type_dec | table_var_dec) SEMICOLON + ; + +table_type_dec + : type_key type_name is_key + ( table_key + of_key type_spec + table_indexed_by_part? + (not_key null_key)? + | varray_type_def + ) + ; + +table_indexed_by_part + : (idx1=indexed_key|idx2=index_key) by_key type_spec + ; + +varray_type_def + : (varray_key| varying_key array_key) + LEFT_PAREN expression RIGHT_PAREN + of_key type_spec + (not_key null_key)? + ; + +table_var_dec + : table_var_name type_spec + ; + +// $> + +// $ body + | (declare_key) => block + | assignment_statement + | continue_statement + | exit_statement + | goto_statement + | if_statement + | loop_statement + | forall_statement + | null_statement + | raise_statement + | return_statement + | case_statement[true] + | sql_statement + | function_call + ; + +assignment_statement + : (general_element|bind_variable) ASSIGN_OP expression + ; + +continue_statement + : continue_key label_name? (when_key condition)? + ; + +exit_statement + : exit_key label_name? (when_key condition)? + ; + +goto_statement + : goto_key label_name + ; + +if_statement + : if_key condition then_key seq_of_statements + elsif_part* else_part? + end_key if_key + ; + +elsif_part + : elsif_key condition then_key seq_of_statements + ; + +else_part + : else_key seq_of_statements + ; + +loop_statement + : label_name? + (while_key condition | for_key cursor_loop_param)? + loop_key + seq_of_statements + end_key loop_key label_name? + ; + +// $ + index_name in_key reverse_key? lower_bound DOUBLE_PERIOD upper_bound + | record_name in_key ( cursor_name expression_list? | LEFT_PAREN select_statement RIGHT_PAREN) + ; +// $> + +forall_statement + : forall_key index_name in_key bounds_clause sql_statement (save_key exceptions_key)? + ; + +bounds_clause + : lower_bound DOUBLE_PERIOD upper_bound + | indices_key of_key collection_name between_bound? + | values_key of_key index_name + ; + +between_bound + : between_key lower_bound and_key upper_bound + ; + +lower_bound + : concatenation + ; + +upper_bound + : concatenation + ; + +null_statement + : null_key + ; + +raise_statement + : raise_key exception_name? + ; + +return_statement + : return_key cn1=condition? + ; + +function_call + : call_key? routine_name function_argument? + ; + +body + : begin_key + seq_of_statements + exception_clause? + end_key label_name? + ; + +// $ + +trigger_block + : (declare_key? declare_spec+)? + body + ; + +block + : declare_key? declare_spec+ + body + ; + +// $> + +// $ + + +// $ + +// $ select_statement + | expression + ) + using_clause? + ; + +// $> + +// $ diff --git a/runtime/Cpp/tests/PLSQL_DMLParser.g b/runtime/Cpp/tests/PLSQL_DMLParser.g new file mode 100644 index 000000000..0c1c77266 --- /dev/null +++ b/runtime/Cpp/tests/PLSQL_DMLParser.g @@ -0,0 +1,1241 @@ +/** + * Oracle(c) PL/SQL 11g Parser + * + * Copyright (c) 2009-2011 Alexandre Porcelli + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +parser grammar PLSQL_DMLParser; + +// options { +// tokenVocab=PLSQLLexer; +// language=Cpp; +// } + +//import PLSQLKeys, PLSQLCommons; + +@parser::includes +{ + #include "PTestTraits.hpp" + #include "PLSQLLexer.hpp" +} +@parser::namespace { Antlr3Test } + +//SHOULD BE OVERRIDEN! +compilation_unit + : seq_of_statements* EOF + ; + +//SHOULD BE OVERRIDEN! +seq_of_statements + : + ( select_statement + | update_statement + | delete_statement + | insert_statement + | lock_table_statement + | merge_statement + | explain_statement + ) + ( SEMICOLON | EOF ) + ; + +explain_statement + : explain_key plan_key + (set_key statement_id_key EQUALS_OP quoted_string)? + (into_key tableview_name)? + for_key + ( select_statement + | update_statement + | delete_statement + | insert_statement + | merge_statement + ) + -> ^(EXPLAIN_STATEMENT select_statement? update_statement? delete_statement? insert_statement? merge_statement?) + ; + +select_statement + : subquery_factoring_clause? + subquery + (for_update_clause|(order_key siblings_key? by_key)=> order_by_clause)* + -> ^(SELECT_STATEMENT subquery_factoring_clause? subquery for_update_clause* order_by_clause*) + ; + +// $