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