as_parser.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. Copyright (c) 2002-2003 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_AS_PARSER_HPP)
  9. #define BOOST_SPIRIT_AS_PARSER_HPP
  10. #include <boost/spirit/home/classic/namespace.hpp>
  11. #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
  12. namespace boost { namespace spirit {
  13. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  14. ///////////////////////////////////////////////////////////////////////////
  15. //
  16. // Helper templates to derive the parser type from an auxilliary type
  17. // and to generate an object of the required parser type given an
  18. // auxilliary object. Supported types to convert are parsers,
  19. // single characters and character strings.
  20. //
  21. ///////////////////////////////////////////////////////////////////////////
  22. namespace impl
  23. {
  24. template<typename T>
  25. struct default_as_parser
  26. {
  27. typedef T type;
  28. static type const& convert(type const& p)
  29. {
  30. return p;
  31. }
  32. };
  33. struct char_as_parser
  34. {
  35. typedef chlit<char> type;
  36. static type convert(char ch)
  37. {
  38. return type(ch);
  39. }
  40. };
  41. struct wchar_as_parser
  42. {
  43. typedef chlit<wchar_t> type;
  44. static type convert(wchar_t ch)
  45. {
  46. return type(ch);
  47. }
  48. };
  49. struct string_as_parser
  50. {
  51. typedef strlit<char const*> type;
  52. static type convert(char const* str)
  53. {
  54. return type(str);
  55. }
  56. };
  57. struct wstring_as_parser
  58. {
  59. typedef strlit<wchar_t const*> type;
  60. static type convert(wchar_t const* str)
  61. {
  62. return type(str);
  63. }
  64. };
  65. }
  66. template<typename T>
  67. struct as_parser : impl::default_as_parser<T> {};
  68. template<>
  69. struct as_parser<char> : impl::char_as_parser {};
  70. template<>
  71. struct as_parser<wchar_t> : impl::wchar_as_parser {};
  72. template<>
  73. struct as_parser<char*> : impl::string_as_parser {};
  74. template<>
  75. struct as_parser<char const*> : impl::string_as_parser {};
  76. template<>
  77. struct as_parser<wchar_t*> : impl::wstring_as_parser {};
  78. template<>
  79. struct as_parser<wchar_t const*> : impl::wstring_as_parser {};
  80. template<int N>
  81. struct as_parser<char[N]> : impl::string_as_parser {};
  82. template<int N>
  83. struct as_parser<wchar_t[N]> : impl::wstring_as_parser {};
  84. template<int N>
  85. struct as_parser<char const[N]> : impl::string_as_parser {};
  86. template<int N>
  87. struct as_parser<wchar_t const[N]> : impl::wstring_as_parser {};
  88. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  89. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  90. #endif