antlr3commontoken.inl 7.5 KB

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