grammar.hpp 4.8 KB

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