summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/metaparse/test/sequence_apply.cpp
blob: 4d4cd5c6c58d09deaf50163909ec419c50b4e29b (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
// Copyright Abel Sinkovics (abel@sinkovics.hu) 2015.
// 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)

#include <boost/metaparse/sequence_apply.hpp>
#include <boost/metaparse/is_error.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/get_result.hpp>
#include <boost/metaparse/always.hpp>
#include <boost/metaparse/one_char.hpp>

#include "common.hpp"

#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/char.hpp>

#include <boost/type_traits/is_same.hpp>

#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/tuple/eat.hpp>
#include <boost/preprocessor/cat.hpp>

#include "test_case.hpp"

namespace
{
#ifdef BOOST_METAPARSE_C_VALUE
#  error BOOST_METAPARSE_C_VALUE already defined
#endif
#define BOOST_METAPARSE_C_VALUE(z, n, unused) BOOST_PP_CAT(C, n)::value

#ifdef BOOST_METAPARSE_TEMPLATE
#  error BOOST_METAPARSE_TEMPLATE already defined
#endif
#define BOOST_METAPARSE_TEMPLATE(z, n, unused) \
  template <BOOST_PP_ENUM(n, char BOOST_PP_TUPLE_EAT(3), ~)> \
  struct BOOST_PP_CAT(template_c, n) \
  { \
    typedef BOOST_PP_CAT(template_c, n) type; \
  }; \
  \
  template <BOOST_PP_ENUM_PARAMS(n, class C)> \
  struct BOOST_PP_CAT(template, n) \
  { \
    typedef \
      BOOST_PP_CAT(template_c, n)< \
        BOOST_PP_ENUM(n, BOOST_METAPARSE_C_VALUE, ~) \
      > \
      type; \
  };

  BOOST_PP_REPEAT_FROM_TO(1, 4, BOOST_METAPARSE_TEMPLATE, ~)

#undef BOOST_METAPARSE_TEMPLATE
#undef BOOST_METAPARSE_C_VALUE

  template <class T> struct has_no_type {};

  // "is_same<T::type::type, double_eval<T>::type>" - helper tool to avoid
  // writing type::type (which is interpreted as the constructor of ::type by
  // msvc-7.1)
  template <class T> struct double_eval : T::type {};
}

BOOST_METAPARSE_TEST_CASE(sequence_apply)
{
  using boost::metaparse::get_result;
  using boost::metaparse::sequence_apply1;
  using boost::metaparse::sequence_apply2;
  using boost::metaparse::sequence_apply3;
  using boost::metaparse::start;
  using boost::metaparse::is_error;
  using boost::metaparse::always;
  using boost::metaparse::one_char;
  
  using boost::mpl::list;
  using boost::mpl::equal_to;
  using boost::mpl::at_c;
  using boost::mpl::vector_c;
  using boost::mpl::vector;
  using boost::mpl::char_;

  using boost::is_same;

  typedef always<one_char, int> always_int;

  // test_one_parser
  BOOST_MPL_ASSERT((
    is_same<
      template_c1<'h'>,
      double_eval<
        get_result<
          sequence_apply1<template1, lit_h>::apply<str_hello, start>
        >
      >::type
    >
  ));

  // test_one_failing_parser
  BOOST_MPL_ASSERT((
    is_error<sequence_apply1<template1, lit_e>::apply<str_hello, start> >
  ));
  
  // test_two_chars
  BOOST_MPL_ASSERT((
    is_same<
      template_c2<'h', 'e'>,
      double_eval<
        get_result<
          sequence_apply2<template2, lit_h, lit_e>::apply<str_hello, start>
        >
      >::type
    >
  ));

  // test_first_fails
  BOOST_MPL_ASSERT((
    is_error<sequence_apply2<template2, lit_x, lit_e>::apply<str_hello, start> >
  ));

  // test_second_fails
  BOOST_MPL_ASSERT((
    is_error<sequence_apply2<template2, lit_h, lit_x>::apply<str_hello, start> >
  ));

  // test_empty_input
  BOOST_MPL_ASSERT((
    is_error<sequence_apply2<template2, lit_h, lit_e>::apply<str_,start> >
  ));

  // test_three_chars
  BOOST_MPL_ASSERT((
    is_same<
      template_c3<'h', 'e', 'l'>,
      double_eval<
        get_result<
          sequence_apply3<template3, lit_h, lit_e, lit_l>
            ::apply<str_hello, start>
        >
      >::type
    >
  ));

  // test_no_extra_evaluation
  BOOST_MPL_ASSERT((
    is_same<
      has_no_type<int>,
      get_result<
        sequence_apply1<has_no_type, always_int>::apply<str_ca, start>
      >::type
    >
  ));
}