antlr3treeparser.inl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. namespace antlr3 {
  2. template< class ImplTraits >
  3. TreeParser<ImplTraits>::TreeParser( ANTLR_UINT32 sizeHint, TreeNodeStreamType* ctnstream,
  4. RecognizerSharedStateType* state)
  5. :RecognizerType( sizeHint, state )
  6. {
  7. /* Install the tree node stream
  8. */
  9. this->setTreeNodeStream(ctnstream);
  10. }
  11. template< class ImplTraits >
  12. TreeParser<ImplTraits>::~TreeParser()
  13. {
  14. this->get_rec()->get_state()->get_following().clear();
  15. }
  16. template< class ImplTraits >
  17. typename TreeParser<ImplTraits>::TreeNodeStreamType* TreeParser<ImplTraits>::get_ctnstream() const
  18. {
  19. return m_ctnstream;
  20. }
  21. template< class ImplTraits >
  22. typename TreeParser<ImplTraits>::IntStreamType* TreeParser<ImplTraits>::get_istream() const
  23. {
  24. return m_ctnstream;
  25. }
  26. template< class ImplTraits >
  27. typename TreeParser<ImplTraits>::IntStreamType* TreeParser<ImplTraits>::get_parser_istream() const
  28. {
  29. return m_ctnstream;
  30. }
  31. template< class ImplTraits >
  32. typename TreeParser<ImplTraits>::RecognizerType* TreeParser<ImplTraits>::get_rec()
  33. {
  34. return this;
  35. }
  36. template< class ImplTraits >
  37. void TreeParser<ImplTraits>::fillExceptionData( ExceptionBaseType* ex )
  38. {
  39. auto xxx = m_ctnstream->LT(1);
  40. //ex->set_token( m_ctnstream->LT(1) ); /* Current input tree node */
  41. ex->set_line( ex->get_token()->get_line() );
  42. ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() );
  43. ex->set_index( m_ctnstream->index() );
  44. // Are you ready for this? Deep breath now...
  45. //
  46. {
  47. TreeTypePtr tnode;
  48. tnode = ex->get_token();
  49. if (tnode->get_token() == NULL)
  50. {
  51. ex->set_streamName("-unknown source-" );
  52. }
  53. else
  54. {
  55. if ( tnode->get_token()->get_input() == NULL)
  56. {
  57. ex->set_streamName("");
  58. }
  59. else
  60. {
  61. ex->set_streamName( tnode->get_token()->get_input()->get_fileName() );
  62. }
  63. }
  64. ex->set_message("Unexpected node");
  65. }
  66. }
  67. template< class ImplTraits >
  68. void TreeParser<ImplTraits>::displayRecognitionError( ANTLR_UINT8** /*tokenNames*/, ExceptionBaseType* ex )
  69. {
  70. typename ImplTraits::StringStreamType errtext;
  71. // See if there is a 'filename' we can use
  72. //
  73. if( ex->get_streamName().empty() )
  74. {
  75. if(ex->get_token()->get_type() == ImplTraits::CommonTokenType::TOKEN_EOF)
  76. {
  77. errtext << "-end of input-(";
  78. }
  79. else
  80. {
  81. errtext << "-unknown source-(";
  82. }
  83. }
  84. else
  85. {
  86. errtext << ex->get_streamName() << "(";
  87. }
  88. // Next comes the line number
  89. //
  90. errtext << this->get_rec()->get_state()->get_exception()->get_line() << ") ";
  91. errtext << " : error " << this->get_rec()->get_state()->get_exception()->getType()
  92. << " : "
  93. << this->get_rec()->get_state()->get_exception()->get_message();
  94. IntStreamType* is = this->get_istream();
  95. TreeTypePtr theBaseTree = this->get_rec()->get_state()->get_exception()->get_token();
  96. StringType ttext = theBaseTree->toStringTree();
  97. if (theBaseTree != NULL)
  98. {
  99. TreeTypePtr theCommonTree = static_cast<TreeTypePtr>(theBaseTree);
  100. if (theCommonTree != NULL)
  101. {
  102. CommonTokenType* theToken = theBaseTree->getToken();
  103. }
  104. errtext << ", at offset "
  105. << theBaseTree->getCharPositionInLine();
  106. errtext << ", near " << ttext;
  107. }
  108. ex->displayRecognitionError( errtext );
  109. ImplTraits::displayRecognitionError( errtext.str() );
  110. }
  111. template< class ImplTraits >
  112. void TreeParser<ImplTraits>::setTreeNodeStream(TreeNodeStreamType* input)
  113. {
  114. m_ctnstream = input;
  115. this->get_rec()->reset();
  116. m_ctnstream->reset();
  117. }
  118. template< class ImplTraits >
  119. typename TreeParser<ImplTraits>::TreeNodeStreamType* TreeParser<ImplTraits>::getTreeNodeStream()
  120. {
  121. return m_ctnstream;
  122. }
  123. template< class ImplTraits >
  124. void TreeParser<ImplTraits>::exConstruct()
  125. {
  126. new ANTLR_Exception<ImplTraits, MISMATCHED_TREE_NODE_EXCEPTION, TreeNodeStreamType>( this->get_rec(), "" );
  127. }
  128. template< class ImplTraits >
  129. void TreeParser<ImplTraits>::mismatch(ANTLR_UINT32 ttype, BitsetListType* follow)
  130. {
  131. this->exConstruct();
  132. this->recoverFromMismatchedToken(ttype, follow);
  133. }
  134. template< class ImplTraits >
  135. typename TreeParser<ImplTraits>::TokenType*
  136. TreeParser<ImplTraits>::getMissingSymbol( IntStreamType* istream, ExceptionBaseType* /*e*/,
  137. ANTLR_UINT32 expectedTokenType, BitsetListType* /*follow*/)
  138. {
  139. TreeNodeStreamType* tns;
  140. TreeTypePtr node;
  141. TreeTypePtr current;
  142. CommonTokenType* token;
  143. StringType text;
  144. // Dereference the standard pointers
  145. //
  146. tns = static_cast<TreeNodeStreamType*>(istream);
  147. // Create a new empty node, by stealing the current one, or the previous one if the current one is EOF
  148. //
  149. current = tns->LT(1);
  150. if (current == tns->get_EOF_NODE_p())
  151. {
  152. current = tns->LT(-1);
  153. }
  154. node = current->dupNode();
  155. // Find the newly dupicated token
  156. //
  157. token = node->getToken();
  158. // Create the token text that shows it has been inserted
  159. //
  160. token->setText("<missing ");
  161. text = token->getText();
  162. text.append((const char *)this->get_rec()->get_state()->get_tokenName(expectedTokenType));
  163. text.append((const char *)">");
  164. // Finally return the pointer to our new node
  165. //
  166. return node;
  167. }
  168. }