123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972 |
- namespace antlr3 {
- template <typename ImplTraits>
- CommonResourcePoolManager<ImplTraits>::CommonResourcePoolManager(CommonTreeStore<ImplTraits> * pool)
- : m_pool(pool)
- {}
- template <typename ImplTraits>
- CommonResourcePoolManager<ImplTraits>::CommonResourcePoolManager()
- : m_pool(NULL)
- {}
- template <typename ImplTraits>
- CommonResourcePoolManager<ImplTraits>::~CommonResourcePoolManager()
- {};
- template <typename ImplTraits>
- void
- CommonResourcePoolManager<ImplTraits>::operator()(TreeType* releasedResource) const
- {
- if (releasedResource && m_pool)
- m_pool->reuse(releasedResource);
- }
- template <class ImplTraits>
- CommonTreeStore<ImplTraits>::CommonTreeStore()
- : m_manager(this)
- {}
- template <class ImplTraits>
- typename CommonTreeStore<ImplTraits>::TreeTypePtr
- CommonTreeStore<ImplTraits>::create()
- {
- if (m_recycleBin.empty())
- {
- TreeTypePtr retval = TreeTypePtr(new TreeType, m_manager);
- m_treeStore.push_back(std::unique_ptr<TreeType>(retval.get()));
- return retval;
- } else {
- TreeType* resource = m_recycleBin.back();
- m_recycleBin.pop_back();
- return TreeTypePtr(resource, m_manager);
- }
- }
- template <class ImplTraits>
- typename CommonTreeStore<ImplTraits>::CommonTokenType*
- CommonTreeStore<ImplTraits>::createToken()
- {
- CommonTokenType* retval = new CommonTokenType;
- m_tokenStore.push_back(std::unique_ptr<CommonTokenType>(retval));
- return retval;
- }
- template <class ImplTraits>
- typename CommonTreeStore<ImplTraits>::CommonTokenType*
- CommonTreeStore<ImplTraits>::createToken( const CommonTokenType* fromToken)
- {
- CommonTokenType* retval = new CommonTokenType(*fromToken);
- m_tokenStore.push_back(std::unique_ptr<CommonTokenType>(retval));
- return retval;
- }
- template <class ImplTraits>
- void
- CommonTreeStore<ImplTraits>::reuse(TreeType* releasedResource)
- {
- if (contains(m_recycleBin, releasedResource))
- {
- throw std::string("Grrr double reuse");
- }
- releasedResource->reuse();
- m_recycleBin.push_back(releasedResource);
- }
- template<class ImplTraits>
- CommonTreeAdaptor<ImplTraits>::CommonTreeAdaptor(DebuggerType*)
- {}
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::nilNode()
- {
- return this->create(NULL);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::dupTree( const TreeType* tree)
- {
- if (tree == NULL)
- return NULL;
- return std::move(this->dupTreeImpl(tree, NULL));
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::dupTree( const TreeTypePtr& tree)
- {
- if (tree == NULL)
- return NULL;
- return std::move(dupTreeImpl(tree.get(), NULL));
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::dupTreeImpl( const TreeType *root, TreeType* parent)
- {
- TreeTypePtr newTree(dupNode(root));
- // Ensure new subtree root has parent/child index set
- //
- this->setChildIndex( newTree, root->get_childIndex() );
- this->setParent(newTree, parent);
- ChildrenType const& r_children = root->get_children();
- for (auto i = r_children.begin(); i != r_children.end(); ++i)
- {
- // add child's clone
- this->addChild(newTree, dupTreeImpl(i->get(), newTree.get()));
- }
- return newTree;
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::addChild( TreeTypePtr& t, TreeTypePtr& child)
- {
- if (t != NULL && child != NULL)
- {
- t->addChild(child);
- }
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::addChild( TreeTypePtr& t, TreeTypePtr&& child)
- {
- if (t != NULL && child != NULL)
- {
- t->addChild(child);
- }
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::addChildToken( TreeTypePtr& t, CommonTokenType* child)
- {
- if (t != NULL && child != NULL)
- {
- this->addChild(t, this->create(child));
- }
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::setParent( TreeTypePtr& child, TreeType* parent)
- {
- child->set_parent(parent);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeType*
- CommonTreeAdaptor<ImplTraits>::getParent( TreeTypePtr& child)
- {
- if ( child==NULL )
- return NULL;
- return child->getParent();
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::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->create( CommonTokenType::TOKEN_INVALID, "Tree Error Node");
- }
- template<class ImplTraits>
- bool CommonTreeAdaptor<ImplTraits>::isNilNode( TreeTypePtr& t)
- {
- return t->isNilNode();
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree)
- {
- /* 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);
- }
- /* root is just the new tree as is if there is no
- * current root tree.
- */
- if (oldRootTree == NULL)
- {
- return std::move(newRootTree);
- }
- /* Produce ^(nil real-node)
- */
- 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() saveRoot here
- }
- /* 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.
- */
- newRootTree->addChild(oldRootTree);
- /* Always returns new root structure
- */
- return std::move(newRootTree);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr&& newRootTree, TreeTypePtr& oldRootTree)
- {
- /* 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);
- }
- /* root is just the new tree as is if there is no
- * current root tree.
- */
- if (oldRootTree == NULL)
- {
- return std::move(newRootTree);
- }
- /* Produce ^(nil real-node)
- */
- 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.
- }
- /* 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.
- */
- newRootTree->addChild(oldRootTree);
- /* Always returns new root structure
- */
- return std::move(newRootTree);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::becomeRootToken( CommonTokenType* newRoot, TreeTypePtr& oldRoot)
- {
- return this->becomeRoot(this->create(newRoot), oldRoot);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::create( CommonTokenType const* payload)
- {
- TreeTypePtr retval = TreeStoreType::create();
- retval->set_token(payload);
- return retval;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::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);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::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<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, typename CommonTreeAdaptor<ImplTraits>::StringType const& 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->set_tokText(text);
- /* Return a new node based upon this token */
- return this->create(newToken);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const char* text)
- {
- auto fromToken = this->createToken(tokenType, text);
- return this->create(fromToken);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, typename CommonTreeAdaptor<ImplTraits>::StringType const& text)
- {
- auto fromToken = this->createToken(tokenType, text);
- return this->create(fromToken);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::dupNode(const TreeType* treeNode)
- {
- if (treeNode == NULL)
- return TreeStoreType::null();
- TreeTypePtr retval(TreeStoreType::create());
- treeNode->dupNode(retval.get());
- return retval;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
- CommonTreeAdaptor<ImplTraits>::dupNode(const TreeTypePtr& treeNode)
- {
- if (treeNode == NULL)
- return TreeStoreType::null();
- TreeTypePtr retval(TreeStoreType::create());
- treeNode->dupNode(retval.get());
- return retval;
- }
- template<class ImplTraits>
- ANTLR_UINT32 CommonTreeAdaptor<ImplTraits>::getType( TreeTypePtr& t)
- {
- if ( t==NULL)
- return CommonTokenType::TOKEN_INVALID;
- return t->getType();
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::StringType CommonTreeAdaptor<ImplTraits>::getText( TreeTypePtr& t)
- {
- return t->getText();
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr& CommonTreeAdaptor<ImplTraits>::getChild( TreeTypePtr& t, ANTLR_UINT32 i)
- {
- if ( t==NULL )
- return NULL;
- return t->getChild(i);
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::setChild( TreeTypePtr& t, ANTLR_UINT32 i, TreeTypePtr& child)
- {
- t->setChild(i, child);
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::deleteChild( TreeTypePtr& t, ANTLR_UINT32 i)
- {
- t->deleteChild(i);
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::setChildIndex( TreeTypePtr& t, ANTLR_INT32 i)
- {
- if( t!= NULL)
- t->set_childIndex(i);
- }
- template<class ImplTraits>
- ANTLR_INT32 CommonTreeAdaptor<ImplTraits>::getChildIndex( TreeTypePtr& t)
- {
- if ( t==NULL )
- return 0;
- return t->getChildIndex();
- }
- template<class ImplTraits>
- ANTLR_UINT32 CommonTreeAdaptor<ImplTraits>::getChildCount( TreeTypePtr& t)
- {
- if ( t==NULL )
- return 0;
- return t->getChildCount();
- }
- template<class ImplTraits>
- ANTLR_UINT64 CommonTreeAdaptor<ImplTraits>::getUniqueID( TreeTypePtr& node )
- {
- return reinterpret_cast<ANTLR_UINT64>(node);
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
- CommonTreeAdaptor<ImplTraits>::createToken( ANTLR_UINT32 tokenType, const char* text)
- {
- CommonTokenType* newToken = TreeStoreType::createToken();
- newToken->set_tokText( text );
- newToken->set_type(tokenType);
- return newToken;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
- CommonTreeAdaptor<ImplTraits>::createToken( ANTLR_UINT32 tokenType, typename CommonTreeAdaptor<ImplTraits>::StringType const& text)
- {
- CommonTokenType* newToken = TreeStoreType::createToken();
- newToken->set_tokText( text );
- newToken->set_type(tokenType);
- return newToken;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
- CommonTreeAdaptor<ImplTraits>::createToken( const CommonTokenType* fromToken)
- {
- CommonTokenType* newToken = TreeStoreType::createToken(fromToken);
- return newToken;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
- CommonTreeAdaptor<ImplTraits>::getToken( TreeTypePtr& t)
- {
- return t->getToken();
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::setTokenBoundaries( TreeTypePtr& t, const CommonTokenType* startToken, const CommonTokenType* stopToken)
- {
- ANTLR_MARKER start = 0;
- ANTLR_MARKER stop = 0;
- if (t == NULL)
- return;
- if ( startToken != NULL)
- start = startToken->get_tokenIndex();
- if ( stopToken != NULL)
- stop = stopToken->get_tokenIndex();
- t->set_startIndex(start);
- t->set_stopIndex(stop);
- }
- template<class ImplTraits>
- ANTLR_MARKER CommonTreeAdaptor<ImplTraits>::getTokenStartIndex( TreeTypePtr& t)
- {
- if ( t==NULL )
- return -1;
- return t->get_tokenStartIndex();
- }
- template<class ImplTraits>
- ANTLR_MARKER CommonTreeAdaptor<ImplTraits>::getTokenStopIndex( TreeTypePtr& t)
- {
- if ( t==NULL )
- return -1;
- return t->get_tokenStopIndex();
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::StringType CommonTreeAdaptor<ImplTraits>::makeDot( TreeTypePtr& theTree)
- {
- // The string we are building up
- //
- StringType dotSpec;
- char buff[64];
- StringType text;
-
- dotSpec = "digraph {\n\n"
- "\tordering=out;\n"
- "\tranksep=.4;\n"
- "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n"
- "\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n\n"
- "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]\n\n";
- if (theTree == NULL)
- {
- // No tree, so create a blank spec
- //
- dotSpec->append("n0[label=\"EMPTY TREE\"]\n");
- return dotSpec;
- }
- sprintf(buff, "\tn%p[label=\"", theTree);
- dotSpec.append(buff);
- text = this->getText(theTree);
- for (std::size_t j = 0; j < text.size(); j++)
- {
- switch(text[j])
- {
- case '"':
- dotSpec.append("\\\"");
- break;
- case '\n':
- dotSpec.append("\\n");
- break;
- case '\r':
- dotSpec.append("\\r");
- break;
- default:
- dotSpec += text[j];
- break;
- }
- }
- dotSpec->append("\"]\n");
- // First produce the node defintions
- //
- this->defineDotNodes(theTree, dotSpec);
- dotSpec.append("\n");
- this->defineDotEdges(theTree, dotSpec);
-
- // Terminate the spec
- //
- dotSpec.append("\n}");
- // Result
- //
- return dotSpec;
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::replaceChildren( TreeTypePtr parent, ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr t)
- {
- if (parent != NULL)
- parent->replaceChildren(startChildIndex, stopChildIndex, t);
- }
- template<class ImplTraits>
- CommonTreeAdaptor<ImplTraits>::~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<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::defineDotNodes(TreeTypePtr t, const StringType& dotSpec)
- {
- // How many nodes are we talking about?
- //
- int nCount;
- int i;
- TreeTypePtr child;
- char buff[64];
- StringType text;
- int j;
- // Count the nodes
- //
- nCount = this->getChildCount(t);
- if (nCount == 0)
- {
- // This will already have been included as a child of another node
- // so there is nothing to add.
- //
- return;
- }
- // For each child of the current tree, define a node using the
- // memory address of the node to name it
- //
- for (i = 0; i<nCount; i++)
- {
- // Pick up a pointer for the child
- //
- child = this->getChild(t, i);
- // Name the node
- //
- sprintf(buff, "\tn%p[label=\"", child);
- dotSpec->append(buff);
- text = this->getText(child);
- for (j = 0; j < text.size(); j++)
- {
- switch(text[j])
- {
- case '"':
- dotSpec.append("\\\"");
- break;
- case '\n':
- dotSpec.append("\\n");
- break;
- case '\r':
- dotSpec.append("\\r");
- break;
- default:
- dotSpec += text[j];
- break;
- }
- }
- dotSpec.append("\"]\n");
- // And now define the children of this child (if any)
- //
- this->defineDotNodes(child, dotSpec);
- }
-
- // Done
- //
- return;
- }
- template<class ImplTraits>
- void CommonTreeAdaptor<ImplTraits>::defineDotEdges(TreeTypePtr t, const StringType& dotSpec)
- {
- // How many nodes are we talking about?
- //
- int nCount;
- if (t == NULL)
- {
- // No tree, so do nothing
- //
- return;
- }
- // Count the nodes
- //
- nCount = this->getChildCount(t);
- if (nCount == 0)
- {
- // This will already have been included as a child of another node
- // so there is nothing to add.
- //
- return;
- }
- // For each child, define an edge from this parent, then process
- // and children of this child in the same way
- //
- for (int i=0; i<nCount; i++)
- {
- TreeTypePtr child;
- char buff[128];
- StringType text;
- // Next child
- //
- child = this->getChild(t, i);
- // Create the edge relation
- //
- sprintf(buff, "\t\tn%p -> n%p\t\t// ", t, child);
-
- dotSpec.append(buff);
- // Document the relationship
- //
- text = this->getText(t);
- for (std::size_t j = 0; j < text.size(); j++)
- {
- switch(text[j])
- {
- case '"':
- dotSpec.append("\\\"");
- break;
- case '\n':
- dotSpec.append("\\n");
- break;
- case '\r':
- dotSpec.append("\\r");
- break;
- default:
- dotSpec += text[j];
- break;
- }
- }
- dotSpec.append(" -> ");
- text = this->getText(child);
- for (std::size_t j = 0; j < text.size(); j++)
- {
- switch(text[j])
- {
- case '"':
- dotSpec.append("\\\"");
- break;
- case '\n':
- dotSpec.append("\\n");
- break;
- case '\r':
- dotSpec.append("\\r");
- break;
- default:
- dotSpec += text[j];
- break;
- }
- }
- dotSpec.append("\n");
-
- // Define edges for this child
- //
- this->defineDotEdges(child, dotSpec);
- }
- // Done
- //
- return;
- }
- template<class ImplTraits>
- typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr CommonTreeAdaptor<ImplTraits>::rulePostProcessing( TreeTypePtr& root)
- {
- TreeTypePtr saveRoot = std::move(root);
- if (saveRoot != NULL && saveRoot->isNilNode())
- {
- if (saveRoot->getChildCount() == 0)
- {
- return TreeTypePtr(NULL, root.get_deleter());
- }
- else if (saveRoot->getChildCount() == 1)
- {
- 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 saveRoot;
- }
- template<class ImplTraits>
- DebugTreeAdaptor<ImplTraits>::DebugTreeAdaptor( DebuggerType* debugger )
- {
- m_debugger = debugger;
- }
- template<class ImplTraits>
- void DebugTreeAdaptor<ImplTraits>::setDebugEventListener( DebuggerType* debugger)
- {
- m_debugger = debugger;
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::nilNode()
- {
- TreeTypePtr t = this->create(NULL);
- m_debugger->createNode(t);
- return t;
- }
- template<class ImplTraits>
- void DebugTreeAdaptor<ImplTraits>::addChild(TreeTypePtr& t, TreeTypePtr& child)
- {
- if (t != NULL && child != NULL)
- {
- t->addChild(child);
- m_debugger->addChild(t, child);
- }
- }
- template<class ImplTraits>
- void DebugTreeAdaptor<ImplTraits>::addChildToken(TreeTypePtr& t, CommonTokenType* child)
- {
- TreeTypePtr tc;
- if (t != NULL && child != NULL)
- {
- tc = this->create(child);
- this->addChild(t, tc);
- m_debugger->addChild(t, tc);
- }
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree )
- {
- TreeTypePtr t = super::becomeRoot(newRootTree, oldRootTree);
- m_debugger->becomeRoot(newRootTree, oldRootTree);
- return t;
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::becomeRootToken(CommonTokenType* newRoot, TreeTypePtr& oldRoot)
- {
- TreeTypePtr t = super::becomeRoot(this->create(newRoot), oldRoot);
- m_debugger->becomeRoot(t, oldRoot);
- return t;
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken)
- {
- TreeTypePtr t;
- t = this->createTypeToken(tokenType, fromToken);
- m_debugger->createNode(t);
- return t;
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text)
- {
- TreeTypePtr t;
- t = this->createTypeTokenText(tokenType, fromToken, text);
- m_debugger->createNode(t);
- return t;
- }
-
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text)
- {
- TreeTypePtr t;
- t = this->createTypeText(tokenType, text);
- m_debugger->createNode(t);
- return t;
- }
- template<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::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<class ImplTraits>
- typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::dupTree( const TreeType* 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<class ImplTraits>
- void DebugTreeAdaptor<ImplTraits>::simulateTreeConstruction(TreeTypePtr& tree)
- {
- ANTLR_UINT32 n;
- ANTLR_UINT32 i;
- TreeTypePtr child;
- // Send the create node event
- //
- m_debugger->createNode(tree);
- n = this->getChildCount(tree);
- for (i = 0; i < n; i++)
- {
- child = this->getChild(tree, i);
- this->simulateTreeConstruction(child);
- m_debugger->addChild(tree, child);
- }
- }
- }
|