1 // Copyright (c) 2019-2020 Dr. Colin Hirsch and Daniel Frey
2 // Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3 
4 #ifndef TAO_PEGTL_MATCH_HPP
5 #define TAO_PEGTL_MATCH_HPP
6 
7 #include <type_traits>
8 
9 #include "apply_mode.hpp"
10 #include "config.hpp"
11 #include "nothing.hpp"
12 #include "require_apply.hpp"
13 #include "require_apply0.hpp"
14 #include "rewind_mode.hpp"
15 
16 #include "internal/dusel_mode.hpp"
17 #include "internal/duseltronik.hpp"
18 #include "internal/has_apply.hpp"
19 #include "internal/has_apply0.hpp"
20 #include "internal/if_missing.hpp"
21 #include "internal/skip_control.hpp"
22 
23 namespace tao
24 {
25    namespace TAO_PEGTL_NAMESPACE
26    {
27       template< typename Rule,
28                 apply_mode A,
29                 rewind_mode M,
30                 template< typename... >
31                 class Action,
32                 template< typename... >
33                 class Control,
34                 typename Input,
35                 typename... States >
match(Input & in,States &&...st)36       bool match( Input& in, States&&... st )
37       {
38          constexpr bool enable_control = !internal::skip_control< Rule >::value;
39          constexpr bool enable_action = enable_control && ( A == apply_mode::action );
40 
41          using iterator_t = typename Input::iterator_t;
42          constexpr bool has_apply_void = enable_action && internal::has_apply< Control< Rule >, void, Action, const iterator_t&, const Input&, States... >::value;
43          constexpr bool has_apply_bool = enable_action && internal::has_apply< Control< Rule >, bool, Action, const iterator_t&, const Input&, States... >::value;
44          constexpr bool has_apply = has_apply_void || has_apply_bool;
45 
46          constexpr bool has_apply0_void = enable_action && internal::has_apply0< Control< Rule >, void, Action, const Input&, States... >::value;
47          constexpr bool has_apply0_bool = enable_action && internal::has_apply0< Control< Rule >, bool, Action, const Input&, States... >::value;
48          constexpr bool has_apply0 = has_apply0_void || has_apply0_bool;
49 
50          static_assert( !( has_apply && has_apply0 ), "both apply() and apply0() defined" );
51 
52          constexpr bool is_nothing = std::is_base_of< nothing< Rule >, Action< Rule > >::value;
53          static_assert( !( has_apply && is_nothing ), "unexpected apply() defined" );
54          static_assert( !( has_apply0 && is_nothing ), "unexpected apply0() defined" );
55 
56          internal::if_missing< !has_apply && std::is_base_of< require_apply, Action< Rule > >::value >::template apply< Control< Rule >, Action >( in, st... );
57          internal::if_missing< !has_apply0 && std::is_base_of< require_apply0, Action< Rule > >::value >::template apply0< Control< Rule >, Action >( in, st... );
58 
59          constexpr bool validate_nothing = std::is_base_of< maybe_nothing, Action< void > >::value;
60          constexpr bool is_maybe_nothing = std::is_base_of< maybe_nothing, Action< Rule > >::value;
61          static_assert( !enable_action || !validate_nothing || is_nothing || is_maybe_nothing || has_apply || has_apply0, "either apply() or apply0() must be defined" );
62 
63          constexpr auto mode = static_cast< internal::dusel_mode >( enable_control + has_apply_void + 2 * has_apply_bool + 3 * has_apply0_void + 4 * has_apply0_bool );
64          return internal::duseltronik< Rule, A, M, Action, Control, mode >::match( in, st... );
65       }
66 
67    }  // namespace TAO_PEGTL_NAMESPACE
68 
69 }  // namespace tao
70 
71 #endif
72