antlr3commontreeadaptor.inl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. namespace antlr3 {
  2. template <typename ImplTraits>
  3. CommonResourcePoolManager<ImplTraits>::CommonResourcePoolManager(CommonTreeStore<ImplTraits> * pool)
  4. : m_pool(pool)
  5. {}
  6. template <typename ImplTraits>
  7. CommonResourcePoolManager<ImplTraits>::CommonResourcePoolManager()
  8. : m_pool(NULL)
  9. {}
  10. template <typename ImplTraits>
  11. CommonResourcePoolManager<ImplTraits>::~CommonResourcePoolManager()
  12. {};
  13. template <typename ImplTraits>
  14. void
  15. CommonResourcePoolManager<ImplTraits>::operator()(TreeType* releasedResource) const
  16. {
  17. if (releasedResource && m_pool)
  18. m_pool->reuse(releasedResource);
  19. }
  20. template <class ImplTraits>
  21. CommonTreeStore<ImplTraits>::CommonTreeStore()
  22. : m_manager(this)
  23. {}
  24. template <class ImplTraits>
  25. typename CommonTreeStore<ImplTraits>::TreeTypePtr
  26. CommonTreeStore<ImplTraits>::create()
  27. {
  28. if (m_recycleBin.empty())
  29. {
  30. TreeTypePtr retval = TreeTypePtr(new TreeType, m_manager);
  31. m_treeStore.push_back(std::unique_ptr<TreeType>(retval.get()));
  32. return retval;
  33. } else {
  34. TreeType* resource = m_recycleBin.back();
  35. m_recycleBin.pop_back();
  36. return TreeTypePtr(resource, m_manager);
  37. }
  38. }
  39. template <class ImplTraits>
  40. typename CommonTreeStore<ImplTraits>::CommonTokenType*
  41. CommonTreeStore<ImplTraits>::createToken()
  42. {
  43. CommonTokenType* retval = new CommonTokenType;
  44. m_tokenStore.push_back(std::unique_ptr<CommonTokenType>(retval));
  45. return retval;
  46. }
  47. template <class ImplTraits>
  48. typename CommonTreeStore<ImplTraits>::CommonTokenType*
  49. CommonTreeStore<ImplTraits>::createToken( const CommonTokenType* fromToken)
  50. {
  51. CommonTokenType* retval = new CommonTokenType(*fromToken);
  52. m_tokenStore.push_back(std::unique_ptr<CommonTokenType>(retval));
  53. return retval;
  54. }
  55. template <class ImplTraits>
  56. void
  57. CommonTreeStore<ImplTraits>::reuse(TreeType* releasedResource)
  58. {
  59. if (contains(m_recycleBin, releasedResource))
  60. {
  61. throw std::string("Grrr double reuse");
  62. }
  63. releasedResource->reuse();
  64. m_recycleBin.push_back(releasedResource);
  65. }
  66. template<class ImplTraits>
  67. CommonTreeAdaptor<ImplTraits>::CommonTreeAdaptor(DebuggerType*)
  68. {}
  69. template<class ImplTraits>
  70. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  71. CommonTreeAdaptor<ImplTraits>::nilNode()
  72. {
  73. return this->create(NULL);
  74. }
  75. template<class ImplTraits>
  76. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  77. CommonTreeAdaptor<ImplTraits>::dupTree( const TreeType* tree)
  78. {
  79. if (tree == NULL)
  80. return NULL;
  81. return std::move(this->dupTreeImpl(tree, NULL));
  82. }
  83. template<class ImplTraits>
  84. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  85. CommonTreeAdaptor<ImplTraits>::dupTree( const TreeTypePtr& tree)
  86. {
  87. if (tree == NULL)
  88. return NULL;
  89. return std::move(dupTreeImpl(tree.get(), NULL));
  90. }
  91. template<class ImplTraits>
  92. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  93. CommonTreeAdaptor<ImplTraits>::dupTreeImpl( const TreeType *root, TreeType* parent)
  94. {
  95. TreeTypePtr newTree(dupNode(root));
  96. // Ensure new subtree root has parent/child index set
  97. //
  98. this->setChildIndex( newTree, root->get_childIndex() );
  99. this->setParent(newTree, parent);
  100. ChildrenType const& r_children = root->get_children();
  101. for (auto i = r_children.begin(); i != r_children.end(); ++i)
  102. {
  103. // add child's clone
  104. this->addChild(newTree, dupTreeImpl(i->get(), newTree.get()));
  105. }
  106. return newTree;
  107. }
  108. template<class ImplTraits>
  109. void CommonTreeAdaptor<ImplTraits>::addChild( TreeTypePtr& t, TreeTypePtr& child)
  110. {
  111. if (t != NULL && child != NULL)
  112. {
  113. t->addChild(child);
  114. }
  115. }
  116. template<class ImplTraits>
  117. void CommonTreeAdaptor<ImplTraits>::addChild( TreeTypePtr& t, TreeTypePtr&& child)
  118. {
  119. if (t != NULL && child != NULL)
  120. {
  121. t->addChild(child);
  122. }
  123. }
  124. template<class ImplTraits>
  125. void CommonTreeAdaptor<ImplTraits>::addChildToken( TreeTypePtr& t, CommonTokenType* child)
  126. {
  127. if (t != NULL && child != NULL)
  128. {
  129. this->addChild(t, this->create(child));
  130. }
  131. }
  132. template<class ImplTraits>
  133. void CommonTreeAdaptor<ImplTraits>::setParent( TreeTypePtr& child, TreeType* parent)
  134. {
  135. child->set_parent(parent);
  136. }
  137. template<class ImplTraits>
  138. typename CommonTreeAdaptor<ImplTraits>::TreeType*
  139. CommonTreeAdaptor<ImplTraits>::getParent( TreeTypePtr& child)
  140. {
  141. if ( child==NULL )
  142. return NULL;
  143. return child->getParent();
  144. }
  145. template<class ImplTraits>
  146. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  147. CommonTreeAdaptor<ImplTraits>::errorNode( CommonTokenType*,
  148. const CommonTokenType*,
  149. const CommonTokenType*)
  150. {
  151. // Use the supplied common tree node stream to get another tree from the factory
  152. // TODO: Look at creating the erronode as in Java, but this is complicated by the
  153. // need to track and free the memory allocated to it, so for now, we just
  154. // want something in the tree that isn't a NULL pointer.
  155. //
  156. return this->create( CommonTokenType::TOKEN_INVALID, "Tree Error Node");
  157. }
  158. template<class ImplTraits>
  159. bool CommonTreeAdaptor<ImplTraits>::isNilNode( TreeTypePtr& t)
  160. {
  161. return t->isNilNode();
  162. }
  163. template<class ImplTraits>
  164. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  165. CommonTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree)
  166. {
  167. /* Protect against tree rewrites if we are in some sort of error
  168. * state, but have tried to recover. In C we can end up with a null pointer
  169. * for a tree that was not produced.
  170. */
  171. if (newRootTree == NULL)
  172. {
  173. return std::move(oldRootTree);
  174. }
  175. /* root is just the new tree as is if there is no
  176. * current root tree.
  177. */
  178. if (oldRootTree == NULL)
  179. {
  180. return std::move(newRootTree);
  181. }
  182. /* Produce ^(nil real-node)
  183. */
  184. if (newRootTree->isNilNode())
  185. {
  186. if (newRootTree->getChildCount() > 1)
  187. {
  188. /* TODO: Handle tree exceptions
  189. */
  190. fprintf(stderr, "More than one node as root! TODO: Create tree exception handling\n");
  191. return std::move(newRootTree);
  192. }
  193. /* The new root is the first child, keep track of the original newRoot
  194. * because if it was a Nil Node, then we can reuse it now.
  195. */
  196. TreeTypePtr saveRoot = std::move(newRootTree);
  197. newRootTree = std::move(saveRoot->getChild(0));
  198. // Will Reclaim the old nilNode() saveRoot here
  199. }
  200. /* Add old root into new root. addChild takes care of the case where oldRoot
  201. * is a flat list (nill rooted tree). All children of oldroot are added to
  202. * new root.
  203. */
  204. newRootTree->addChild(oldRootTree);
  205. /* Always returns new root structure
  206. */
  207. return std::move(newRootTree);
  208. }
  209. template<class ImplTraits>
  210. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  211. CommonTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr&& newRootTree, TreeTypePtr& oldRootTree)
  212. {
  213. /* Protect against tree rewrites if we are in some sort of error
  214. * state, but have tried to recover. In C we can end up with a null pointer
  215. * for a tree that was not produced.
  216. */
  217. if (newRootTree == NULL)
  218. {
  219. return std::move(oldRootTree);
  220. }
  221. /* root is just the new tree as is if there is no
  222. * current root tree.
  223. */
  224. if (oldRootTree == NULL)
  225. {
  226. return std::move(newRootTree);
  227. }
  228. /* Produce ^(nil real-node)
  229. */
  230. if (newRootTree->isNilNode())
  231. {
  232. if (newRootTree->getChildCount() > 1)
  233. {
  234. /* TODO: Handle tree exceptions
  235. */
  236. fprintf(stderr, "More than one node as root! TODO: Create tree exception handling\n");
  237. return std::move(newRootTree);
  238. }
  239. /* The new root is the first child, keep track of the original newRoot
  240. * because if it was a Nil Node, then we can reuse it now.
  241. */
  242. TreeTypePtr saveRoot = std::move(newRootTree);
  243. newRootTree = std::move(saveRoot->getChild(0));
  244. // will Reclaim the old nilNode() here saveRoot.
  245. }
  246. /* Add old root into new root. addChild takes care of the case where oldRoot
  247. * is a flat list (nill rooted tree). All children of oldroot are added to
  248. * new root.
  249. */
  250. newRootTree->addChild(oldRootTree);
  251. /* Always returns new root structure
  252. */
  253. return std::move(newRootTree);
  254. }
  255. template<class ImplTraits>
  256. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  257. CommonTreeAdaptor<ImplTraits>::becomeRootToken( CommonTokenType* newRoot, TreeTypePtr& oldRoot)
  258. {
  259. return this->becomeRoot(this->create(newRoot), oldRoot);
  260. }
  261. template<class ImplTraits>
  262. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  263. CommonTreeAdaptor<ImplTraits>::create( CommonTokenType const* payload)
  264. {
  265. TreeTypePtr retval = TreeStoreType::create();
  266. retval->set_token(payload);
  267. return retval;
  268. }
  269. template<class ImplTraits>
  270. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  271. CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken)
  272. {
  273. /* Create the new token */
  274. auto newToken = this->createToken(fromToken);
  275. /* Set the type of the new token to that supplied */
  276. newToken->set_type(tokenType);
  277. /* Return a new node based upon this token */
  278. return this->create(newToken);
  279. }
  280. template<class ImplTraits>
  281. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  282. CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, const char* text)
  283. {
  284. if (fromToken == NULL)
  285. return create(tokenType, text);
  286. /* Create the new token */
  287. auto newToken = this->createToken(fromToken);
  288. /* Set the type of the new token to that supplied */
  289. newToken->set_type(tokenType);
  290. /* Set the text of the token accordingly */
  291. newToken->setText(text);
  292. /* Return a new node based upon this token */
  293. return this->create(newToken);
  294. }
  295. template<class ImplTraits>
  296. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  297. CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, typename CommonTreeAdaptor<ImplTraits>::StringType const& text)
  298. {
  299. if (fromToken == NULL)
  300. return create(tokenType, text);
  301. /* Create the new token */
  302. auto newToken = this->createToken(fromToken);
  303. /* Set the type of the new token to that supplied */
  304. newToken->set_type(tokenType);
  305. /* Set the text of the token accordingly */
  306. newToken->set_tokText(text);
  307. /* Return a new node based upon this token */
  308. return this->create(newToken);
  309. }
  310. template<class ImplTraits>
  311. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  312. CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, const char* text)
  313. {
  314. auto fromToken = this->createToken(tokenType, text);
  315. return this->create(fromToken);
  316. }
  317. template<class ImplTraits>
  318. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  319. CommonTreeAdaptor<ImplTraits>::create( ANTLR_UINT32 tokenType, typename CommonTreeAdaptor<ImplTraits>::StringType const& text)
  320. {
  321. auto fromToken = this->createToken(tokenType, text);
  322. return this->create(fromToken);
  323. }
  324. template<class ImplTraits>
  325. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  326. CommonTreeAdaptor<ImplTraits>::dupNode(const TreeType* treeNode)
  327. {
  328. if (treeNode == NULL)
  329. return TreeStoreType::null();
  330. TreeTypePtr retval(TreeStoreType::create());
  331. treeNode->dupNode(retval.get());
  332. return retval;
  333. }
  334. template<class ImplTraits>
  335. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr
  336. CommonTreeAdaptor<ImplTraits>::dupNode(const TreeTypePtr& treeNode)
  337. {
  338. if (treeNode == NULL)
  339. return TreeStoreType::null();
  340. TreeTypePtr retval(TreeStoreType::create());
  341. treeNode->dupNode(retval.get());
  342. return retval;
  343. }
  344. template<class ImplTraits>
  345. ANTLR_UINT32 CommonTreeAdaptor<ImplTraits>::getType( TreeTypePtr& t)
  346. {
  347. if ( t==NULL)
  348. return CommonTokenType::TOKEN_INVALID;
  349. return t->getType();
  350. }
  351. template<class ImplTraits>
  352. typename CommonTreeAdaptor<ImplTraits>::StringType CommonTreeAdaptor<ImplTraits>::getText( TreeTypePtr& t)
  353. {
  354. return t->getText();
  355. }
  356. template<class ImplTraits>
  357. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr& CommonTreeAdaptor<ImplTraits>::getChild( TreeTypePtr& t, ANTLR_UINT32 i)
  358. {
  359. if ( t==NULL )
  360. return NULL;
  361. return t->getChild(i);
  362. }
  363. template<class ImplTraits>
  364. void CommonTreeAdaptor<ImplTraits>::setChild( TreeTypePtr& t, ANTLR_UINT32 i, TreeTypePtr& child)
  365. {
  366. t->setChild(i, child);
  367. }
  368. template<class ImplTraits>
  369. void CommonTreeAdaptor<ImplTraits>::deleteChild( TreeTypePtr& t, ANTLR_UINT32 i)
  370. {
  371. t->deleteChild(i);
  372. }
  373. template<class ImplTraits>
  374. void CommonTreeAdaptor<ImplTraits>::setChildIndex( TreeTypePtr& t, ANTLR_INT32 i)
  375. {
  376. if( t!= NULL)
  377. t->set_childIndex(i);
  378. }
  379. template<class ImplTraits>
  380. ANTLR_INT32 CommonTreeAdaptor<ImplTraits>::getChildIndex( TreeTypePtr& t)
  381. {
  382. if ( t==NULL )
  383. return 0;
  384. return t->getChildIndex();
  385. }
  386. template<class ImplTraits>
  387. ANTLR_UINT32 CommonTreeAdaptor<ImplTraits>::getChildCount( TreeTypePtr& t)
  388. {
  389. if ( t==NULL )
  390. return 0;
  391. return t->getChildCount();
  392. }
  393. template<class ImplTraits>
  394. ANTLR_UINT64 CommonTreeAdaptor<ImplTraits>::getUniqueID( TreeTypePtr& node )
  395. {
  396. return reinterpret_cast<ANTLR_UINT64>(node);
  397. }
  398. template<class ImplTraits>
  399. typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
  400. CommonTreeAdaptor<ImplTraits>::createToken( ANTLR_UINT32 tokenType, const char* text)
  401. {
  402. CommonTokenType* newToken = TreeStoreType::createToken();
  403. newToken->set_tokText( text );
  404. newToken->set_type(tokenType);
  405. return newToken;
  406. }
  407. template<class ImplTraits>
  408. typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
  409. CommonTreeAdaptor<ImplTraits>::createToken( ANTLR_UINT32 tokenType, typename CommonTreeAdaptor<ImplTraits>::StringType const& text)
  410. {
  411. CommonTokenType* newToken = TreeStoreType::createToken();
  412. newToken->set_tokText( text );
  413. newToken->set_type(tokenType);
  414. return newToken;
  415. }
  416. template<class ImplTraits>
  417. typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
  418. CommonTreeAdaptor<ImplTraits>::createToken( const CommonTokenType* fromToken)
  419. {
  420. CommonTokenType* newToken = TreeStoreType::createToken(fromToken);
  421. return newToken;
  422. }
  423. template<class ImplTraits>
  424. typename CommonTreeAdaptor<ImplTraits>::CommonTokenType*
  425. CommonTreeAdaptor<ImplTraits>::getToken( TreeTypePtr& t)
  426. {
  427. return t->getToken();
  428. }
  429. template<class ImplTraits>
  430. void CommonTreeAdaptor<ImplTraits>::setTokenBoundaries( TreeTypePtr& t, const CommonTokenType* startToken, const CommonTokenType* stopToken)
  431. {
  432. ANTLR_MARKER start = 0;
  433. ANTLR_MARKER stop = 0;
  434. if (t == NULL)
  435. return;
  436. if ( startToken != NULL)
  437. start = startToken->get_tokenIndex();
  438. if ( stopToken != NULL)
  439. stop = stopToken->get_tokenIndex();
  440. t->set_startIndex(start);
  441. t->set_stopIndex(stop);
  442. }
  443. template<class ImplTraits>
  444. ANTLR_MARKER CommonTreeAdaptor<ImplTraits>::getTokenStartIndex( TreeTypePtr& t)
  445. {
  446. if ( t==NULL )
  447. return -1;
  448. return t->get_tokenStartIndex();
  449. }
  450. template<class ImplTraits>
  451. ANTLR_MARKER CommonTreeAdaptor<ImplTraits>::getTokenStopIndex( TreeTypePtr& t)
  452. {
  453. if ( t==NULL )
  454. return -1;
  455. return t->get_tokenStopIndex();
  456. }
  457. template<class ImplTraits>
  458. typename CommonTreeAdaptor<ImplTraits>::StringType CommonTreeAdaptor<ImplTraits>::makeDot( TreeTypePtr& theTree)
  459. {
  460. // The string we are building up
  461. //
  462. StringType dotSpec;
  463. char buff[64];
  464. StringType text;
  465. dotSpec = "digraph {\n\n"
  466. "\tordering=out;\n"
  467. "\tranksep=.4;\n"
  468. "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n"
  469. "\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n\n"
  470. "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]\n\n";
  471. if (theTree == NULL)
  472. {
  473. // No tree, so create a blank spec
  474. //
  475. dotSpec->append("n0[label=\"EMPTY TREE\"]\n");
  476. return dotSpec;
  477. }
  478. sprintf(buff, "\tn%p[label=\"", theTree);
  479. dotSpec.append(buff);
  480. text = this->getText(theTree);
  481. for (std::size_t j = 0; j < text.size(); j++)
  482. {
  483. switch(text[j])
  484. {
  485. case '"':
  486. dotSpec.append("\\\"");
  487. break;
  488. case '\n':
  489. dotSpec.append("\\n");
  490. break;
  491. case '\r':
  492. dotSpec.append("\\r");
  493. break;
  494. default:
  495. dotSpec += text[j];
  496. break;
  497. }
  498. }
  499. dotSpec->append("\"]\n");
  500. // First produce the node defintions
  501. //
  502. this->defineDotNodes(theTree, dotSpec);
  503. dotSpec.append("\n");
  504. this->defineDotEdges(theTree, dotSpec);
  505. // Terminate the spec
  506. //
  507. dotSpec.append("\n}");
  508. // Result
  509. //
  510. return dotSpec;
  511. }
  512. template<class ImplTraits>
  513. void CommonTreeAdaptor<ImplTraits>::replaceChildren( TreeTypePtr parent, ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr t)
  514. {
  515. if (parent != NULL)
  516. parent->replaceChildren(startChildIndex, stopChildIndex, t);
  517. }
  518. template<class ImplTraits>
  519. CommonTreeAdaptor<ImplTraits>::~CommonTreeAdaptor()
  520. {
  521. #ifdef ANTLR3_DEBUG
  522. std::cout << "SZ" << TreeStoreType::size() << std::endl;
  523. std::cout << "RZ" << TreeStoreType::m_recycleBin.size() << std::endl;
  524. auto i = TreeStoreType::m_treeStore.begin();
  525. std::cout
  526. << ' '
  527. << "Node " << '\t' << "Parent " << '\t' << "Type" << '\t' << "toStringTree" << std::endl;
  528. for(; i != TreeStoreType::m_treeStore.end(); ++i)
  529. {
  530. std::cout
  531. << (TreeStoreType::contains(TreeStoreType::m_recycleBin, i->get()) ? '*' : ' ')
  532. << i->get() << '\t'
  533. << (const void *) (*i)->get_parent() << '\t'
  534. << (*i)->getType() << '\t'
  535. << (*i)->getChildCount() << '\t'
  536. << (*i)->toStringTree() << '\t'
  537. << std::endl;
  538. }
  539. #endif
  540. }
  541. template<class ImplTraits>
  542. void CommonTreeAdaptor<ImplTraits>::defineDotNodes(TreeTypePtr t, const StringType& dotSpec)
  543. {
  544. // How many nodes are we talking about?
  545. //
  546. int nCount;
  547. int i;
  548. TreeTypePtr child;
  549. char buff[64];
  550. StringType text;
  551. int j;
  552. // Count the nodes
  553. //
  554. nCount = this->getChildCount(t);
  555. if (nCount == 0)
  556. {
  557. // This will already have been included as a child of another node
  558. // so there is nothing to add.
  559. //
  560. return;
  561. }
  562. // For each child of the current tree, define a node using the
  563. // memory address of the node to name it
  564. //
  565. for (i = 0; i<nCount; i++)
  566. {
  567. // Pick up a pointer for the child
  568. //
  569. child = this->getChild(t, i);
  570. // Name the node
  571. //
  572. sprintf(buff, "\tn%p[label=\"", child);
  573. dotSpec->append(buff);
  574. text = this->getText(child);
  575. for (j = 0; j < text.size(); j++)
  576. {
  577. switch(text[j])
  578. {
  579. case '"':
  580. dotSpec.append("\\\"");
  581. break;
  582. case '\n':
  583. dotSpec.append("\\n");
  584. break;
  585. case '\r':
  586. dotSpec.append("\\r");
  587. break;
  588. default:
  589. dotSpec += text[j];
  590. break;
  591. }
  592. }
  593. dotSpec.append("\"]\n");
  594. // And now define the children of this child (if any)
  595. //
  596. this->defineDotNodes(child, dotSpec);
  597. }
  598. // Done
  599. //
  600. return;
  601. }
  602. template<class ImplTraits>
  603. void CommonTreeAdaptor<ImplTraits>::defineDotEdges(TreeTypePtr t, const StringType& dotSpec)
  604. {
  605. // How many nodes are we talking about?
  606. //
  607. int nCount;
  608. if (t == NULL)
  609. {
  610. // No tree, so do nothing
  611. //
  612. return;
  613. }
  614. // Count the nodes
  615. //
  616. nCount = this->getChildCount(t);
  617. if (nCount == 0)
  618. {
  619. // This will already have been included as a child of another node
  620. // so there is nothing to add.
  621. //
  622. return;
  623. }
  624. // For each child, define an edge from this parent, then process
  625. // and children of this child in the same way
  626. //
  627. for (int i=0; i<nCount; i++)
  628. {
  629. TreeTypePtr child;
  630. char buff[128];
  631. StringType text;
  632. // Next child
  633. //
  634. child = this->getChild(t, i);
  635. // Create the edge relation
  636. //
  637. sprintf(buff, "\t\tn%p -> n%p\t\t// ", t, child);
  638. dotSpec.append(buff);
  639. // Document the relationship
  640. //
  641. text = this->getText(t);
  642. for (std::size_t j = 0; j < text.size(); j++)
  643. {
  644. switch(text[j])
  645. {
  646. case '"':
  647. dotSpec.append("\\\"");
  648. break;
  649. case '\n':
  650. dotSpec.append("\\n");
  651. break;
  652. case '\r':
  653. dotSpec.append("\\r");
  654. break;
  655. default:
  656. dotSpec += text[j];
  657. break;
  658. }
  659. }
  660. dotSpec.append(" -> ");
  661. text = this->getText(child);
  662. for (std::size_t j = 0; j < text.size(); j++)
  663. {
  664. switch(text[j])
  665. {
  666. case '"':
  667. dotSpec.append("\\\"");
  668. break;
  669. case '\n':
  670. dotSpec.append("\\n");
  671. break;
  672. case '\r':
  673. dotSpec.append("\\r");
  674. break;
  675. default:
  676. dotSpec += text[j];
  677. break;
  678. }
  679. }
  680. dotSpec.append("\n");
  681. // Define edges for this child
  682. //
  683. this->defineDotEdges(child, dotSpec);
  684. }
  685. // Done
  686. //
  687. return;
  688. }
  689. template<class ImplTraits>
  690. typename CommonTreeAdaptor<ImplTraits>::TreeTypePtr CommonTreeAdaptor<ImplTraits>::rulePostProcessing( TreeTypePtr& root)
  691. {
  692. TreeTypePtr saveRoot = std::move(root);
  693. if (saveRoot != NULL && saveRoot->isNilNode())
  694. {
  695. if (saveRoot->getChildCount() == 0)
  696. {
  697. return TreeTypePtr(NULL, root.get_deleter());
  698. }
  699. else if (saveRoot->getChildCount() == 1)
  700. {
  701. TreeTypePtr newRoot = std::move(saveRoot->getChild(0));
  702. newRoot->set_parent(NULL);
  703. newRoot->set_childIndex(-1);
  704. // The root we were given was a nil node, with one child, which means it has
  705. // been abandoned and would be lost in the node factory.
  706. // saveRoot will be releases and put back into factory
  707. //
  708. return newRoot;
  709. }
  710. }
  711. return saveRoot;
  712. }
  713. template<class ImplTraits>
  714. DebugTreeAdaptor<ImplTraits>::DebugTreeAdaptor( DebuggerType* debugger )
  715. {
  716. m_debugger = debugger;
  717. }
  718. template<class ImplTraits>
  719. void DebugTreeAdaptor<ImplTraits>::setDebugEventListener( DebuggerType* debugger)
  720. {
  721. m_debugger = debugger;
  722. }
  723. template<class ImplTraits>
  724. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::nilNode()
  725. {
  726. TreeTypePtr t = this->create(NULL);
  727. m_debugger->createNode(t);
  728. return t;
  729. }
  730. template<class ImplTraits>
  731. void DebugTreeAdaptor<ImplTraits>::addChild(TreeTypePtr& t, TreeTypePtr& child)
  732. {
  733. if (t != NULL && child != NULL)
  734. {
  735. t->addChild(child);
  736. m_debugger->addChild(t, child);
  737. }
  738. }
  739. template<class ImplTraits>
  740. void DebugTreeAdaptor<ImplTraits>::addChildToken(TreeTypePtr& t, CommonTokenType* child)
  741. {
  742. TreeTypePtr tc;
  743. if (t != NULL && child != NULL)
  744. {
  745. tc = this->create(child);
  746. this->addChild(t, tc);
  747. m_debugger->addChild(t, tc);
  748. }
  749. }
  750. template<class ImplTraits>
  751. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree )
  752. {
  753. TreeTypePtr t = super::becomeRoot(newRootTree, oldRootTree);
  754. m_debugger->becomeRoot(newRootTree, oldRootTree);
  755. return t;
  756. }
  757. template<class ImplTraits>
  758. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::becomeRootToken(CommonTokenType* newRoot, TreeTypePtr& oldRoot)
  759. {
  760. TreeTypePtr t = super::becomeRoot(this->create(newRoot), oldRoot);
  761. m_debugger->becomeRoot(t, oldRoot);
  762. return t;
  763. }
  764. template<class ImplTraits>
  765. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken)
  766. {
  767. TreeTypePtr t;
  768. t = this->createTypeToken(tokenType, fromToken);
  769. m_debugger->createNode(t);
  770. return t;
  771. }
  772. template<class ImplTraits>
  773. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text)
  774. {
  775. TreeTypePtr t;
  776. t = this->createTypeTokenText(tokenType, fromToken, text);
  777. m_debugger->createNode(t);
  778. return t;
  779. }
  780. template<class ImplTraits>
  781. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text)
  782. {
  783. TreeTypePtr t;
  784. t = this->createTypeText(tokenType, text);
  785. m_debugger->createNode(t);
  786. return t;
  787. }
  788. template<class ImplTraits>
  789. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::dupTree( const TreeTypePtr& tree)
  790. {
  791. TreeTypePtr t;
  792. // Call the normal dup tree mechanism first
  793. //
  794. t = this->dupTreeImpl(tree, NULL);
  795. // In order to tell the debugger what we have just done, we now
  796. // simulate the tree building mechanism. THis will fire
  797. // lots of debugging events to the client and look like we
  798. // duped the tree..
  799. //
  800. this->simulateTreeConstruction( t);
  801. return t;
  802. }
  803. template<class ImplTraits>
  804. typename DebugTreeAdaptor<ImplTraits>::TreeTypePtr DebugTreeAdaptor<ImplTraits>::dupTree( const TreeType* tree)
  805. {
  806. TreeTypePtr t;
  807. // Call the normal dup tree mechanism first
  808. //
  809. t = this->dupTreeImpl(tree, NULL);
  810. // In order to tell the debugger what we have just done, we now
  811. // simulate the tree building mechanism. THis will fire
  812. // lots of debugging events to the client and look like we
  813. // duped the tree..
  814. //
  815. this->simulateTreeConstruction( t);
  816. return t;
  817. }
  818. template<class ImplTraits>
  819. void DebugTreeAdaptor<ImplTraits>::simulateTreeConstruction(TreeTypePtr& tree)
  820. {
  821. ANTLR_UINT32 n;
  822. ANTLR_UINT32 i;
  823. TreeTypePtr child;
  824. // Send the create node event
  825. //
  826. m_debugger->createNode(tree);
  827. n = this->getChildCount(tree);
  828. for (i = 0; i < n; i++)
  829. {
  830. child = this->getChild(tree, i);
  831. this->simulateTreeConstruction(child);
  832. m_debugger->addChild(tree, child);
  833. }
  834. }
  835. }