1 /*=============================================================================
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 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_GRAMMAR_FEBRUARY_19_2007_0236PM)
8 #define BOOST_SPIRIT_GRAMMAR_FEBRUARY_19_2007_0236PM
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/qi/domain.hpp>
18 #include <boost/spirit/home/qi/nonterminal/rule.hpp>
19 #include <boost/spirit/home/qi/nonterminal/nonterminal_fwd.hpp>
20 #include <boost/spirit/home/qi/reference.hpp>
21 #include <boost/noncopyable.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 
24 namespace boost { namespace spirit { namespace qi
25 {
26     template <
27         typename Iterator, typename T1, typename T2, typename T3
28       , typename T4>
29     struct grammar
30       : proto::extends<
31             typename proto::terminal<
32                 reference<rule<Iterator, T1, T2, T3, T4> const>
33             >::type
34           , grammar<Iterator, T1, T2, T3, T4>
35         >
36       , parser<grammar<Iterator, T1, T2, T3, T4> >
37       , noncopyable
38     {
39         typedef Iterator iterator_type;
40         typedef rule<Iterator, T1, T2, T3, T4> start_type;
41         typedef typename start_type::sig_type sig_type;
42         typedef typename start_type::locals_type locals_type;
43         typedef typename start_type::skipper_type skipper_type;
44         typedef typename start_type::encoding_type encoding_type;
45         typedef grammar<Iterator, T1, T2, T3, T4> base_type;
46         typedef reference<start_type const> reference_;
47         typedef typename proto::terminal<reference_>::type terminal;
48 
49         static size_t const params_size = start_type::params_size;
50 
51         template <typename Context, typename Iterator_>
52         struct attribute
53         {
54             typedef typename start_type::attr_type type;
55         };
56 
grammarboost::spirit::qi::grammar57         grammar(
58             start_type const& start
59           , std::string const& name = "unnamed-grammar")
60         : proto::extends<terminal, base_type>(terminal::make(reference_(start)))
61         , name_(name)
62         {}
63 
64         // This constructor is used to catch if the start rule is not
65         // compatible with the grammar.
66         template <typename Iterator_,
67             typename T1_, typename T2_, typename T3_, typename T4_>
grammarboost::spirit::qi::grammar68         grammar(
69             rule<Iterator_, T1_, T2_, T3_, T4_> const&
70           , std::string const& = "unnamed-grammar")
71         {
72             // If you see the assertion below failing then the start rule
73             // passed to the constructor of the grammar is not compatible with
74             // the grammar (i.e. it uses different template parameters).
75             BOOST_SPIRIT_ASSERT_MSG(
76                 (is_same<start_type, rule<Iterator_, T1_, T2_, T3_, T4_> >::value)
77               , incompatible_start_rule, (rule<Iterator_, T1_, T2_, T3_, T4_>));
78         }
79 
nameboost::spirit::qi::grammar80         std::string name() const
81         {
82             return name_;
83         }
84 
nameboost::spirit::qi::grammar85         void name(std::string const& str)
86         {
87             name_ = str;
88         }
89 
90         template <typename Context, typename Skipper, typename Attribute>
parseboost::spirit::qi::grammar91         bool parse(Iterator& first, Iterator const& last
92           , Context& context, Skipper const& skipper
93           , Attribute& attr_) const
94         {
95             return this->proto_base().child0.parse(
96                 first, last, context, skipper, attr_);
97         }
98 
99         template <typename Context>
whatboost::spirit::qi::grammar100         info what(Context&) const
101         {
102             return info(name_);
103         }
104 
105         // bring in the operator() overloads
get_parameterized_subjectboost::spirit::qi::grammar106         start_type const& get_parameterized_subject() const
107         { return this->proto_base().child0.ref.get(); }
108         typedef start_type parameterized_subject_type;
109         #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
110 
111         std::string name_;
112 
113     };
114 }}}
115 
116 namespace boost { namespace spirit { namespace traits
117 {
118     ///////////////////////////////////////////////////////////////////////////
119     template <
120         typename IteratorA, typename IteratorB, typename Attribute
121       , typename Context, typename T1, typename T2, typename T3, typename T4>
122     struct handles_container<
123         qi::grammar<IteratorA, T1, T2, T3, T4>, Attribute, Context, IteratorB>
124       : traits::is_container<
125           typename attribute_of<
126               qi::grammar<IteratorA, T1, T2, T3, T4>, Context, IteratorB
127           >::type
128         >
129     {};
130 }}}
131 
132 #endif
133