grammar.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2001-2011 Joel de Guzman
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  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_KARMA_GRAMMAR_MAR_05_2007_0542PM)
  7. #define BOOST_SPIRIT_KARMA_GRAMMAR_MAR_05_2007_0542PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/support/unused.hpp>
  12. #include <boost/spirit/home/support/info.hpp>
  13. #include <boost/spirit/home/support/assert_msg.hpp>
  14. #include <boost/spirit/home/karma/domain.hpp>
  15. #include <boost/spirit/home/karma/nonterminal/rule.hpp>
  16. #include <boost/spirit/home/karma/nonterminal/nonterminal_fwd.hpp>
  17. #include <boost/spirit/home/karma/reference.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/proto/extends.hpp>
  20. #include <boost/proto/traits.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. namespace boost { namespace spirit { namespace karma
  23. {
  24. template <
  25. typename OutputIterator, typename T1, typename T2, typename T3
  26. , typename T4>
  27. struct grammar
  28. : proto::extends<
  29. typename proto::terminal<
  30. reference<rule<OutputIterator, T1, T2, T3, T4> const>
  31. >::type
  32. , grammar<OutputIterator, T1, T2, T3, T4>
  33. >
  34. , generator<grammar<OutputIterator, T1, T2, T3, T4> >
  35. , noncopyable
  36. {
  37. typedef OutputIterator iterator_type;
  38. typedef rule<OutputIterator, T1, T2, T3, T4> start_type;
  39. typedef typename start_type::properties properties;
  40. typedef typename start_type::sig_type sig_type;
  41. typedef typename start_type::locals_type locals_type;
  42. typedef typename start_type::delimiter_type delimiter_type;
  43. typedef typename start_type::encoding_type encoding_type;
  44. typedef grammar<OutputIterator, T1, T2, T3, T4> base_type;
  45. typedef reference<start_type const> reference_;
  46. typedef typename proto::terminal<reference_>::type terminal;
  47. static size_t const params_size = start_type::params_size;
  48. template <typename Context, typename Unused>
  49. struct attribute
  50. {
  51. typedef typename start_type::attr_type type;
  52. };
  53. // the output iterator is always wrapped by karma
  54. typedef detail::output_iterator<OutputIterator, properties>
  55. output_iterator;
  56. grammar(start_type const& start
  57. , std::string const& name_ = "unnamed-grammar")
  58. : proto::extends<terminal, base_type>(terminal::make(reference_(start)))
  59. , name_(name_)
  60. {}
  61. // This constructor is used to catch if the start rule is not
  62. // compatible with the grammar.
  63. template <typename Iterator_, typename T1_, typename T2_, typename T3_,
  64. typename T4_>
  65. grammar(rule<Iterator_, T1_, T2_, T3_, T4_> const&
  66. , std::string const& = "unnamed-grammar")
  67. {
  68. // If you see the assertion below failing then the start rule
  69. // passed to the constructor of the grammar is not compatible with
  70. // the grammar (i.e. it uses different template parameters).
  71. BOOST_SPIRIT_ASSERT_MSG(
  72. (is_same<start_type, rule<Iterator_, T1_, T2_, T3_, T4_> >::value)
  73. , incompatible_start_rule, (rule<Iterator_, T1_, T2_, T3_, T4_>));
  74. }
  75. std::string name() const
  76. {
  77. return name_;
  78. }
  79. void name(std::string const& str)
  80. {
  81. name_ = str;
  82. }
  83. template <typename Context, typename Delimiter, typename Attribute>
  84. bool generate(output_iterator& sink, Context& context
  85. , Delimiter const& delim, Attribute const& attr) const
  86. {
  87. return this->proto_base().child0.generate(
  88. sink, context, delim, attr);
  89. }
  90. template <typename Context>
  91. info what(Context&) const
  92. {
  93. return info(name_);
  94. }
  95. // bring in the operator() overloads
  96. start_type const& get_parameterized_subject() const
  97. { return this->proto_base().child0.ref.get(); }
  98. typedef start_type parameterized_subject_type;
  99. #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
  100. std::string name_;
  101. };
  102. }}}
  103. namespace boost { namespace spirit { namespace traits
  104. {
  105. ///////////////////////////////////////////////////////////////////////////
  106. template <
  107. typename IteratorA, typename IteratorB, typename Attribute
  108. , typename Context, typename T1, typename T2, typename T3, typename T4>
  109. struct handles_container<
  110. karma::grammar<IteratorA, T1, T2, T3, T4>, Attribute, Context
  111. , IteratorB>
  112. : detail::nonterminal_handles_container<
  113. typename attribute_of<
  114. karma::grammar<IteratorA, T1, T2, T3, T4>
  115. , Context, IteratorB
  116. >::type, Attribute>
  117. {};
  118. }}}
  119. #endif