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