1ANTLR_BEGIN_NAMESPACE() 2 3template<class ImplTraits> 4CommonToken<ImplTraits>::CommonToken() 5{ 6 m_type = 0; 7 m_channel = 0; 8 m_lineStart = NULL; 9 m_line = 0; 10 m_charPositionInLine = 0; 11 m_input = NULL; 12 m_index = 0; 13 m_startIndex = 0; 14 m_stopIndex = 0; 15} 16 17template<class ImplTraits> 18CommonToken<ImplTraits>::CommonToken(ANTLR_UINT32 type) 19{ 20 m_type = type; 21 m_channel = 0; 22 m_lineStart = NULL; 23 m_line = 0; 24 m_charPositionInLine = 0; 25 m_input = NULL; 26 m_index = 0; 27 m_startIndex = 0; 28 m_stopIndex = 0; 29} 30 31template<class ImplTraits> 32CommonToken<ImplTraits>::CommonToken(TOKEN_TYPE type) 33{ 34 m_type = type; 35 m_channel = 0; 36 m_lineStart = NULL; 37 m_line = 0; 38 m_charPositionInLine = 0; 39 m_input = NULL; 40 m_index = 0; 41 m_startIndex = 0; 42 m_stopIndex = 0; 43} 44 45template<class ImplTraits> 46CommonToken<ImplTraits>::CommonToken( const CommonToken& ctoken ) 47 :m_tokText( ctoken.m_tokText ) 48{ 49 m_type = ctoken.m_type; 50 m_channel = ctoken.m_channel; 51 m_lineStart = ctoken.m_lineStart; 52 m_line = ctoken.m_line; 53 m_charPositionInLine = ctoken.m_charPositionInLine; 54 m_input = ctoken.m_input; 55 m_index = ctoken.m_index; 56 m_startIndex = ctoken.m_startIndex; 57 m_stopIndex = ctoken.m_stopIndex; 58} 59 60template<class ImplTraits> 61CommonToken<ImplTraits>& CommonToken<ImplTraits>::operator=( const CommonToken& ctoken ) 62{ 63 m_type = ctoken.m_type; 64 m_channel = ctoken.m_channel; 65 m_lineStart = ctoken.m_lineStart; 66 m_line = ctoken.m_line; 67 m_charPositionInLine = ctoken.m_charPositionInLine; 68 m_input = ctoken.m_input; 69 m_index = ctoken.m_index; 70 m_startIndex = ctoken.m_startIndex; 71 m_stopIndex = ctoken.m_stopIndex; 72 73 m_tokText = ctoken.m_tokText; 74 return *this; 75} 76 77template<class ImplTraits> 78ANTLR_INLINE bool CommonToken<ImplTraits>::operator<( const CommonToken& ctoken ) const 79{ 80 return (m_index < ctoken.m_index); 81} 82 83template<class ImplTraits> 84bool CommonToken<ImplTraits>::operator==( const CommonToken& ctoken ) const 85{ 86 return ( (m_type == ctoken.m_type) && 87 (m_channel == ctoken.m_channel) && 88 (m_lineStart == ctoken.m_lineStart) && 89 (m_line == ctoken.m_line) && 90 (m_charPositionInLine == ctoken.m_charPositionInLine) && 91 (m_input == ctoken.m_input) && 92 (m_index == ctoken.m_index) && 93 (m_startIndex == ctoken.m_startIndex) && 94 (m_stopIndex == ctoken.m_stopIndex) ); 95} 96 97template<class ImplTraits> 98ANTLR_INLINE typename CommonToken<ImplTraits>::InputStreamType* CommonToken<ImplTraits>::get_input() const 99{ 100 return m_input; 101} 102 103template<class ImplTraits> 104ANTLR_INLINE ANTLR_MARKER CommonToken<ImplTraits>::get_index() const 105{ 106 return m_index; 107} 108 109template<class ImplTraits> 110ANTLR_INLINE void CommonToken<ImplTraits>::set_index( ANTLR_MARKER index ) 111{ 112 m_index = index; 113} 114 115template<class ImplTraits> 116void CommonToken<ImplTraits>::set_input( InputStreamType* input ) 117{ 118 m_input = input; 119} 120 121template<class ImplTraits> 122typename CommonToken<ImplTraits>::StringType CommonToken<ImplTraits>::getText() const 123{ 124 if ( !m_tokText.empty() ) 125 return m_tokText; 126 127 // EOF is a special case 128 // 129 if ( m_type == TOKEN_EOF) 130 { 131 m_tokText = "<EOF>"; 132 return m_tokText; 133 } 134 135 // We had nothing installed in the token, create a new string 136 // from the input stream 137 // 138 if (m_input != NULL) 139 return m_input->substr( this->get_startIndex(), this->get_stopIndex() ); 140 141 // Nothing to return, there is no input stream 142 // 143 return ""; 144} 145 146template<class ImplTraits> 147ANTLR_INLINE void CommonToken<ImplTraits>::set_tokText( const StringType& text ) 148{ 149 m_tokText = text; 150} 151 152template<class ImplTraits> 153ANTLR_INLINE void CommonToken<ImplTraits>::setText(ANTLR_UINT8* text) 154{ 155 if( text == NULL ) 156 m_tokText.clear(); 157 else 158 m_tokText = (const char*) text; 159} 160 161template<class ImplTraits> 162ANTLR_INLINE void CommonToken<ImplTraits>::setText(const char* text) 163{ 164 if( text == NULL ) 165 m_tokText.clear(); 166 else 167 m_tokText = (const char*) text; 168} 169 170template<class ImplTraits> 171ANTLR_INLINE ANTLR_UINT32 CommonToken<ImplTraits>::get_type() const 172{ 173 return m_type; 174} 175 176template<class ImplTraits> 177ANTLR_INLINE ANTLR_UINT32 CommonToken<ImplTraits>::getType() const 178{ 179 return m_type; 180} 181 182template<class ImplTraits> 183ANTLR_INLINE void CommonToken<ImplTraits>::set_type(ANTLR_UINT32 ttype) 184{ 185 m_type = ttype; 186} 187 188template<class ImplTraits> 189ANTLR_INLINE ANTLR_UINT32 CommonToken<ImplTraits>::get_line() const 190{ 191 return m_line; 192} 193 194template<class ImplTraits> 195ANTLR_INLINE void CommonToken<ImplTraits>::set_line(ANTLR_UINT32 line) 196{ 197 m_line = line; 198} 199 200template<class ImplTraits> 201ANTLR_INLINE ANTLR_INT32 CommonToken<ImplTraits>::get_charPositionInLine() const 202{ 203 return m_charPositionInLine; 204} 205 206template<class ImplTraits> 207ANTLR_INLINE ANTLR_INT32 CommonToken<ImplTraits>::getCharPositionInLine() const 208{ 209 return this->get_charPositionInLine(); 210} 211 212template<class ImplTraits> 213ANTLR_INLINE void CommonToken<ImplTraits>::set_charPositionInLine(ANTLR_INT32 pos) 214{ 215 m_charPositionInLine = pos; 216} 217 218template<class ImplTraits> 219ANTLR_INLINE ANTLR_UINT32 CommonToken<ImplTraits>::get_channel() const 220{ 221 return m_channel; 222} 223 224template<class ImplTraits> 225ANTLR_INLINE void CommonToken<ImplTraits>::set_channel(ANTLR_UINT32 channel) 226{ 227 m_channel = channel; 228} 229 230template<class ImplTraits> 231ANTLR_INLINE ANTLR_MARKER CommonToken<ImplTraits>::get_tokenIndex() const 232{ 233 return m_index; 234} 235 236template<class ImplTraits> 237ANTLR_INLINE void CommonToken<ImplTraits>::set_tokenIndex(ANTLR_MARKER tokenIndex) 238{ 239 m_index = tokenIndex; 240} 241 242template<class ImplTraits> 243ANTLR_INLINE ANTLR_MARKER CommonToken<ImplTraits>::get_startIndex() const 244{ 245 return (m_startIndex == -1) ? (ANTLR_MARKER)(m_input->get_data()) : m_startIndex; 246} 247 248template<class ImplTraits> 249ANTLR_INLINE void CommonToken<ImplTraits>::set_startIndex(ANTLR_MARKER index) 250{ 251 m_startIndex = index; 252} 253 254template<class ImplTraits> 255ANTLR_INLINE ANTLR_MARKER CommonToken<ImplTraits>::get_stopIndex() const 256{ 257 return m_stopIndex; 258} 259 260template<class ImplTraits> 261ANTLR_INLINE void CommonToken<ImplTraits>::set_stopIndex(ANTLR_MARKER index) 262{ 263 m_stopIndex = index; 264} 265 266template<class ImplTraits> 267ANTLR_INLINE const typename CommonToken<ImplTraits>::StreamDataType* CommonToken<ImplTraits>::get_lineStart() const 268{ 269 return m_lineStart; 270} 271 272template<class ImplTraits> 273ANTLR_INLINE void CommonToken<ImplTraits>::set_lineStart( const StreamDataType* lineStart ) 274{ 275 m_lineStart = lineStart; 276} 277 278template<class ImplTraits> 279typename CommonToken<ImplTraits>::StringType CommonToken<ImplTraits>::toString() const 280{ 281 StringType text; 282 typedef typename ImplTraits::StringStreamType StringStreamType; 283 StringStreamType outtext; 284 285 text = this->getText(); 286 287 if (text.empty()) 288 return ""; 289 290 /* Now we use our handy dandy string utility to assemble the 291 * the reporting string 292 * return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]"; 293 */ 294 outtext << "[Index: "; 295 outtext << (int)this->get_tokenIndex(); 296 outtext << " (Start: "; 297 outtext << (int)this->get_startIndex(); 298 outtext << "-Stop: "; 299 outtext << (int)this->get_stopIndex(); 300 outtext << ") ='"; 301 outtext << text; 302 outtext << "', type<"; 303 outtext << (int)m_type; 304 outtext << "> "; 305 306 if (this->get_channel() > TOKEN_DEFAULT_CHANNEL) 307 { 308 outtext << "(channel = "; 309 outtext << (int)this->get_channel(); 310 outtext << ") "; 311 } 312 313 outtext << "Line: "; 314 outtext << (int)this->get_line(); 315 outtext << " LinePos:"; 316 outtext << (int)this->get_charPositionInLine(); 317 outtext << "]"; 318 319 return outtext.str(); 320} 321 322ANTLR_END_NAMESPACE() 323