meta_compiler.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_LEX_META_COMPILER_APR_20_2009_0756PM)
  7. #define BOOST_SPIRIT_LEX_META_COMPILER_APR_20_2009_0756PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/support/meta_compiler.hpp>
  12. #include <boost/spirit/home/lex/domain.hpp>
  13. #include <boost/spirit/home/lex/lexer_type.hpp>
  14. #include <boost/proto/tags.hpp>
  15. #include <boost/type_traits/remove_reference.hpp>
  16. #include <boost/utility/enable_if.hpp>
  17. namespace boost { namespace spirit
  18. {
  19. template <typename T>
  20. struct use_terminal<lex::domain, T
  21. , typename enable_if<traits::is_lexer<T> >::type> // enables lexers
  22. : mpl::true_ {};
  23. namespace lex
  24. {
  25. template <typename T, typename Modifiers, typename Enable = void>
  26. struct make_primitive // by default, return it as-is
  27. {
  28. typedef T result_type;
  29. template <typename T_>
  30. T_& operator()(T_& val, unused_type) const
  31. {
  32. return val;
  33. }
  34. template <typename T_>
  35. T_ const& operator()(T_ const& val, unused_type) const
  36. {
  37. return val;
  38. }
  39. };
  40. template <typename Tag, typename Elements
  41. , typename Modifiers, typename Enable = void>
  42. struct make_composite;
  43. }
  44. // Lex primitive meta-compiler
  45. template <>
  46. struct make_component<lex::domain, proto::tag::terminal>
  47. {
  48. template <typename Sig>
  49. struct result;
  50. template <typename This, typename Elements, typename Modifiers>
  51. struct result<This(Elements, Modifiers)>
  52. {
  53. typedef typename lex::make_primitive<
  54. typename remove_const<typename Elements::car_type>::type,
  55. typename remove_reference<Modifiers>::type>::result_type
  56. type;
  57. };
  58. template <typename Elements, typename Modifiers>
  59. typename result<make_component(Elements, Modifiers)>::type
  60. operator()(Elements const& elements, Modifiers const& modifiers) const
  61. {
  62. typedef typename remove_const<typename Elements::car_type>::type term;
  63. return lex::make_primitive<term, Modifiers>()(elements.car, modifiers);
  64. }
  65. };
  66. // Lex composite meta-compiler
  67. template <typename Tag>
  68. struct make_component<lex::domain, Tag>
  69. {
  70. template <typename Sig>
  71. struct result;
  72. template <typename This, typename Elements, typename Modifiers>
  73. struct result<This(Elements, Modifiers)>
  74. {
  75. typedef typename
  76. lex::make_composite<Tag, Elements
  77. , typename remove_reference<Modifiers>::type>::result_type
  78. type;
  79. };
  80. template <typename Elements, typename Modifiers>
  81. typename result<make_component(Elements, Modifiers)>::type
  82. operator()(Elements const& elements, Modifiers const& modifiers) const
  83. {
  84. return lex::make_composite<Tag, Elements, Modifiers>()(
  85. elements, modifiers);
  86. }
  87. };
  88. }}
  89. #endif