symbols.ipp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_SPIRIT_SYMBOLS_IPP
  9. #define BOOST_SPIRIT_SYMBOLS_IPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <boost/spirit/home/classic/symbols/impl/tst.ipp>
  12. #include <boost/detail/workaround.hpp>
  13. // MSVC: void warning about the use of 'this' pointer in constructors
  14. #if defined(BOOST_MSVC)
  15. #pragma warning(push)
  16. #pragma warning(disable : 4355)
  17. #endif
  18. ///////////////////////////////////////////////////////////////////////////////
  19. namespace boost { namespace spirit {
  20. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // symbols class implementation
  24. //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. template <typename T, typename CharT, typename SetT>
  27. inline symbols<T, CharT, SetT>::symbols()
  28. : SetT()
  29. , add(*this)
  30. {
  31. }
  32. //////////////////////////////////
  33. template <typename T, typename CharT, typename SetT>
  34. symbols<T, CharT, SetT>::symbols(symbols const& other)
  35. : SetT(other)
  36. // Tru64 CXX seems to be confused by the explicit call of the default
  37. // constructor and generates wrong code which invalidates the just contructed
  38. // first base class in the line above.
  39. #if !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590041))
  40. , parser<symbols<T, CharT, SetT> >()
  41. #endif
  42. , add(*this)
  43. {
  44. }
  45. //////////////////////////////////
  46. template <typename T, typename CharT, typename SetT>
  47. inline symbols<T, CharT, SetT>::~symbols()
  48. {}
  49. //////////////////////////////////
  50. template <typename T, typename CharT, typename SetT>
  51. inline symbols<T, CharT, SetT>&
  52. symbols<T, CharT, SetT>::operator=(symbols const& other)
  53. {
  54. SetT::operator=(other);
  55. return *this;
  56. }
  57. //////////////////////////////////
  58. template <typename T, typename CharT, typename SetT>
  59. inline symbol_inserter<T, SetT> const&
  60. symbols<T, CharT, SetT>::operator=(CharT const* str)
  61. {
  62. return add, str;
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. //
  66. // Symbol table utilities
  67. //
  68. ///////////////////////////////////////////////////////////////////////////////
  69. template <typename T, typename CharT, typename SetT>
  70. inline T*
  71. find(symbols<T, CharT, SetT> const& table, CharT const* sym)
  72. {
  73. CharT const* last = sym;
  74. while (*last)
  75. last++;
  76. scanner<CharT const *> scan(sym, last);
  77. T* result = table.find(scan);
  78. return scan.at_end()? result: 0;
  79. }
  80. //////////////////////////////////
  81. template <typename T, typename CharT, typename SetT>
  82. inline T*
  83. add(symbols<T, CharT, SetT>& table, CharT const* sym, T const& data)
  84. {
  85. CharT const* first = sym;
  86. CharT const* last = sym;
  87. while (*last)
  88. last++;
  89. scanner<CharT const *> scan(first, last);
  90. if (table.find(scan) && scan.at_end())
  91. return 0; // symbol already contained in symbol table
  92. table.add(sym, last, data);
  93. first = sym;
  94. return table.find(scan); // refind the inserted symbol
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////
  97. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  98. }} // namespace boost::spirit
  99. #if defined(BOOST_MSVC)
  100. #pragma warning(pop)
  101. #endif
  102. #endif