summaryrefslogtreecommitdiff
path: root/boost/spirit/repository/home/qi/operator/keywords.hpp
blob: 350c2fe775941f4cbe696a66991f4f7390926466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2011-2012 Thomas Bernard

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(SPIRIT_KEYWORDS_OR_MARCH_13_2007_1145PM)
#define SPIRIT_KEYWORDS_OR_MARCH_13_2007_1145PM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/spirit/home/qi/meta_compiler.hpp>
#include <boost/spirit/home/qi/domain.hpp>
#include <boost/spirit/home/qi/detail/permute_function.hpp>
#include <boost/spirit/home/qi/detail/attributes.hpp>
#include <boost/spirit/home/support/detail/what_function.hpp>
#include <boost/spirit/home/support/info.hpp>
#include <boost/spirit/home/support/unused.hpp>
#include <boost/fusion/include/iter_fold.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/value_at.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/optional.hpp>
#include <boost/foreach.hpp>
#include <boost/array.hpp>
#include <boost/spirit/home/qi/string/symbols.hpp>
#include <boost/spirit/home/qi/string/lit.hpp>
#include <boost/spirit/home/qi/action/action.hpp>
#include <boost/spirit/home/qi/directive/hold.hpp>
#include <boost/mpl/count_if.hpp>
#include <boost/mpl/greater.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/filter_view.hpp>
#include <boost/fusion/include/zip_view.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/spirit/repository/home/qi/operator/detail/keywords.hpp>
#include <boost/fusion/include/any.hpp>


namespace boost { namespace spirit
{
    ///////////////////////////////////////////////////////////////////////////
    // Enablers
    ///////////////////////////////////////////////////////////////////////////
    template <>
    struct use_operator<qi::domain, proto::tag::divides > // enables /
      : mpl::true_ {};

    template <>
    struct flatten_tree<qi::domain, proto::tag::divides> // flattens /
      : mpl::true_ {};
}}

namespace boost { namespace spirit { namespace repository { namespace qi
{

    // kwd directive parser type identification
    namespace detail
    {
        BOOST_MPL_HAS_XXX_TRAIT_DEF(kwd_parser_id)
        BOOST_MPL_HAS_XXX_TRAIT_DEF(complex_kwd_parser_id)


    }

    // kwd directive type query
    template <typename T>
    struct is_kwd_parser : detail::has_kwd_parser_id<T> {};

    template <typename Subject, typename Action>
    struct is_kwd_parser<spirit::qi::action<Subject,Action> > : detail::has_kwd_parser_id<Subject> {};

    template <typename Subject>
    struct is_kwd_parser<spirit::qi::hold_directive<Subject> > : detail::has_kwd_parser_id<Subject> {};

    template <typename T>
    struct is_complex_kwd_parser : detail::has_complex_kwd_parser_id<T> {};

    template <typename Subject, typename Action>
    struct is_complex_kwd_parser<spirit::qi::action<Subject,Action> > : detail::has_complex_kwd_parser_id<Subject> {};

    template <typename Subject>
    struct is_complex_kwd_parser<spirit::qi::hold_directive<Subject> > : detail::has_complex_kwd_parser_id<Subject> {};


    // Keywords operator
    template <typename Elements, typename Modifiers>
    struct keywords : spirit::qi::nary_parser<keywords<Elements,Modifiers> >
    {
        template <typename Context, typename Iterator>
        struct attribute
        {
            // Put all the element attributes in a tuple
            typedef typename traits::build_attribute_sequence<
                Elements, Context, traits::sequence_attribute_transform, Iterator, spirit::qi::domain >::type
            all_attributes;

            // Now, build a fusion vector over the attributes. Note
            // that build_fusion_vector 1) removes all unused attributes
            // and 2) may return unused_type if all elements have
            // unused_type(s).
            typedef typename
                traits::build_fusion_vector<all_attributes>::type
            type;
        };

        /// Make sure that all subjects are of the kwd type
        typedef typename mpl::count_if<
                Elements,
                        mpl::not_<
                           mpl::or_<
                              is_kwd_parser<
                                mpl::_1
                              > ,
                              is_complex_kwd_parser<
                                mpl::_1
                              >
                          >
                        >
                > non_kwd_subject_count;

        /// If the assertion fails here then you probably forgot to wrap a
        /// subject of the / operator in a kwd directive
        BOOST_MPL_ASSERT_RELATION( non_kwd_subject_count::value, ==, 0 );

        ///////////////////////////////////////////////////////////////////////////
        // build_parser_tags
        //
        // Builds a boost::variant from an mpl::range_c in order to "mark" every
        // parser of the fusion sequence. The integer constant is used in the parser
        // dispatcher functor in order to use the parser corresponding to the recognised
        // keyword.
        ///////////////////////////////////////////////////////////////////////////

        template <typename Sequence>
        struct build_parser_tags
        {
            // Get the sequence size
            typedef typename mpl::size< Sequence >::type sequence_size;

            // Create an integer_c constant for every parser in the sequence
            typedef typename mpl::range_c<int, 0, sequence_size::value>::type int_range;

            // Transform the range_c to an mpl vector in order to be able to transform it into a variant
            typedef typename mpl::copy<int_range, mpl::back_inserter<mpl::vector<> > >::type type;

        };
        // Build an index mpl vector
        typedef typename build_parser_tags< Elements >::type parser_index_vector;

        template <typename idx>
        struct is_complex_kwd_parser_filter : is_complex_kwd_parser< typename mpl::at<Elements, idx>::type >
        {};

        template <typename idx>
        struct is_kwd_parser_filter : is_kwd_parser< typename mpl::at<Elements, idx>::type >
        {};

        // filter out the string kwd directives
        typedef typename mpl::filter_view< Elements, is_kwd_parser<mpl::_> >::type string_keywords;

        typedef typename mpl::filter_view< parser_index_vector ,
                                         is_kwd_parser_filter< mpl::_ >
                                             >::type string_keyword_indexes;
        // filter out the complex keywords
        typedef typename mpl::filter_view< parser_index_vector ,
                                         is_complex_kwd_parser_filter< mpl::_ >
                                            >::type complex_keywords_indexes;

        //typedef typename fusion::filter_view< Elements, is_complex_kwd_parser< mpl::_ > > complex_keywords_view;

        typedef typename mpl::if_<
                typename mpl::empty<complex_keywords_indexes>::type,
                detail::empty_keywords_list,
                detail::complex_keywords< complex_keywords_indexes >
                >::type complex_keywords_type;

        // build a bool array and an integer array which will be used to
        // check that the repetition constraints of the kwd parsers are
        // met and bail out a soon as possible
        typedef boost::array<bool, fusion::result_of::size<Elements>::value> flags_type;
        typedef boost::array<int, fusion::result_of::size<Elements>::value> counters_type;

        typedef typename mpl::if_<
                        typename mpl::empty<string_keyword_indexes>::type,
                        detail::empty_keywords_list,
                        detail::string_keywords<
                                Elements,
                                string_keywords,
                                string_keyword_indexes,
                                flags_type,
                                Modifiers>
                >::type string_keywords_type;

        keywords(Elements const& elements_) :
              elements(elements_)
            , string_keywords_inst(elements,flags_init)
      , complex_keywords_inst(elements,flags_init)
        {
        }

        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context& context, Skipper const& skipper
          , Attribute& attr_) const
        {
            // Select which parse function to call
            // We need to handle the case where kwd / ikwd directives have been mixed
            // This is where we decide which function should be called.
            return parse_impl(first, last, context, skipper, attr_,
                                typename string_keywords_type::requires_one_pass()
                             );
        }

        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse_impl(Iterator& first, Iterator const& last
          , Context& context, Skipper const& skipper
          , Attribute& attr_,mpl::true_ /* one pass */) const
          {

            // wrap the attribute in a tuple if it is not a tuple
            typename traits::wrap_if_not_tuple<Attribute>::type attr(attr_);

            flags_type flags(flags_init);
            //flags.assign(false);

            counters_type counters;
            counters.assign(0);

            typedef repository::qi::detail::parse_dispatcher<Elements,Iterator, Context, Skipper
                                    , flags_type, counters_type
                                    , typename traits::wrap_if_not_tuple<Attribute>::type
                                    , mpl::false_ > parser_visitor_type;

            parser_visitor_type parse_visitor(elements, first, last
                                             , context, skipper, flags
                                             , counters, attr);

            typedef repository::qi::detail::complex_kwd_function< parser_visitor_type > complex_kwd_function_type;

            complex_kwd_function_type
                     complex_function(first,last,context,skipper,parse_visitor);

            // We have a bool array 'flags' with one flag for each parser as well as a 'counter'
            // array.
            // The kwd directive sets and increments the counter when a successeful parse occurred
            // as well as the slot of the corresponding parser to true in the flags array as soon
            // the minimum repetition requirement is met and keeps that value to true as long as
            // the maximum repetition requirement is met.
            // The parsing takes place here in two steps:
            // 1) parse a keyword and fetch the parser index associated with that keyword
            // 2) call the associated parser and store the parsed value in the matching attribute.

            while(true)
            {

                spirit::qi::skip_over(first, last, skipper);
                Iterator save = first;
                if (string_keywords_inst.parse(first, last,parse_visitor,skipper))
                {
                    save = first;
                }
                else {
                  // restore the position to the last successful keyword parse
                  first = save;
                  if(!complex_keywords_inst.parse(complex_function))
                  {
                    first = save;
                    // Check that we are leaving the keywords parser in a successfull state
                    BOOST_FOREACH(bool &valid,flags)
                    {
                      if(!valid)
                      {
                        return false;
                      }
                    }
                    return true;
                  }
                  else
                    save = first;
                }
            }
            return false;
          }

        // Handle the mixed kwd and ikwd case
        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse_impl(Iterator& first, Iterator const& last
          , Context& context, Skipper const& skipper
          , Attribute& attr_,mpl::false_ /* two passes */) const
          {

            // wrap the attribute in a tuple if it is not a tuple
            typename traits::wrap_if_not_tuple<Attribute>::type attr(attr_);

            flags_type flags(flags_init);
            //flags.assign(false);

            counters_type counters;
            counters.assign(0);

            typedef detail::parse_dispatcher<Elements, Iterator, Context, Skipper
                                    , flags_type, counters_type
                                    , typename traits::wrap_if_not_tuple<Attribute>::type
                                    , mpl::false_> parser_visitor_type;

           typedef detail::parse_dispatcher<Elements, Iterator, Context, Skipper
                                    , flags_type, counters_type
                                    , typename traits::wrap_if_not_tuple<Attribute>::type
                                    , mpl::true_> no_case_parser_visitor_type;


            parser_visitor_type parse_visitor(elements,first,last
                                             ,context,skipper,flags,counters,attr);
            no_case_parser_visitor_type no_case_parse_visitor(elements,first,last
                                             ,context,skipper,flags,counters,attr);

            typedef repository::qi::detail::complex_kwd_function< parser_visitor_type > complex_kwd_function_type;

            complex_kwd_function_type
                     complex_function(first,last,context,skipper,parse_visitor);


            // We have a bool array 'flags' with one flag for each parser as well as a 'counter'
            // array.
            // The kwd directive sets and increments the counter when a successeful parse occurred
            // as well as the slot of the corresponding parser to true in the flags array as soon
            // the minimum repetition requirement is met and keeps that value to true as long as
            // the maximum repetition requirement is met.
            // The parsing takes place here in two steps:
            // 1) parse a keyword and fetch the parser index associated with that keyword
            // 2) call the associated parser and store the parsed value in the matching attribute.

            while(true)
            {
                spirit::qi::skip_over(first, last, skipper);
                Iterator save = first;
                // String keywords pass
                if (string_keywords_inst.parse(first,last,parse_visitor,no_case_parse_visitor,skipper))
                {
                    save = first;
                }
                else {
                  first = save;

                  if(!complex_keywords_inst.parse(complex_function))
                  {
                    first = save;
                    // Check that we are leaving the keywords parser in a successfull state
                    BOOST_FOREACH(bool &valid,flags)
                    {
                      if(!valid)
                      {
                        return false;
                      }
                    }
                    return true;
                  }
                  else
                  {
                    save = first;
                  }
                }
            }
            return false;
          }

        template <typename Context>
        info what(Context& context) const
        {
            info result("keywords");
            fusion::for_each(elements,
                spirit::detail::what_function<Context>(result, context));
            return result;
        }
        flags_type flags_init;
        Elements elements;
        string_keywords_type string_keywords_inst;
        complex_keywords_type complex_keywords_inst;

    };
}}}}

namespace boost { namespace spirit { namespace qi {
    ///////////////////////////////////////////////////////////////////////////
    // Parser generators: make_xxx function (objects)
    ///////////////////////////////////////////////////////////////////////////
    template <typename Elements, typename Modifiers >
    struct make_composite<proto::tag::divides, Elements, Modifiers >
    {
        typedef repository::qi::keywords<Elements,Modifiers> result_type;
        result_type operator()(Elements ref, unused_type) const
        {
            return result_type(ref);
        }
    };


}}}

namespace boost { namespace spirit { namespace traits
{
    // We specialize this for keywords (see support/attributes.hpp).
    // For keywords, we only wrap the attribute in a tuple IFF
    // it is not already a fusion tuple.
    template <typename Elements, typename Modifiers,typename Attribute>
    struct pass_attribute<repository::qi::keywords<Elements,Modifiers>, Attribute>
      : wrap_if_not_tuple<Attribute> {};

    template <typename Elements, typename Modifiers>
    struct has_semantic_action<repository::qi::keywords<Elements, Modifiers> >
      : nary_has_semantic_action<Elements> {};

    template <typename Elements, typename Attribute, typename Context
        , typename Iterator, typename Modifiers>
    struct handles_container<repository::qi::keywords<Elements,Modifiers>, Attribute
        , Context, Iterator>
      : nary_handles_container<Elements, Attribute, Context, Iterator> {};


}}}

#endif