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