-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2020 Dr. Colin Hirsch and Daniel Frey | ||
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ | ||
|
||
#ifndef TAO_PEGTL_CONTRIB_INSTANTIATE_HPP | ||
#define TAO_PEGTL_CONTRIB_INSTANTIATE_HPP | ||
|
||
#include "../config.hpp" | ||
|
||
#include "../apply_mode.hpp" | ||
#include "../match.hpp" | ||
#include "../nothing.hpp" | ||
#include "../rewind_mode.hpp" | ||
|
||
namespace TAO_PEGTL_NAMESPACE | ||
{ | ||
template< typename T > | ||
struct instantiate | ||
: maybe_nothing | ||
{ | ||
template< typename Rule, | ||
apply_mode A, | ||
rewind_mode M, | ||
template< typename... > | ||
class Action, | ||
template< typename... > | ||
class Control, | ||
typename ParseInput, | ||
typename... States > | ||
[[nodiscard]] static bool match( ParseInput& in, States&... st ) | ||
{ | ||
const T t( static_cast< const ParseInput& >( in ), st... ); | ||
return TAO_PEGTL_NAMESPACE::match< Rule, A, M, Action, Control >( in, st... ); | ||
} | ||
}; | ||
|
||
} // namespace TAO_PEGTL_NAMESPACE | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2020 Dr. Colin Hirsch and Daniel Frey | ||
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ | ||
|
||
#include "test.hpp" | ||
|
||
#include <tao/pegtl/contrib/instantiate.hpp> | ||
|
||
namespace TAO_PEGTL_NAMESPACE | ||
{ | ||
bool ctor = false; | ||
bool dtor = false; | ||
|
||
struct test_class | ||
{ | ||
template< typename ParseInput > | ||
test_class( const ParseInput& /*unused*/ ) | ||
{ | ||
TAO_PEGTL_TEST_ASSERT( ctor == false ); | ||
TAO_PEGTL_TEST_ASSERT( dtor == false ); | ||
|
||
ctor = true; | ||
} | ||
|
||
test_class( test_class&& ) = delete; | ||
test_class( const test_class& ) = delete; | ||
|
||
~test_class() | ||
{ | ||
TAO_PEGTL_TEST_ASSERT( ctor == true ); | ||
TAO_PEGTL_TEST_ASSERT( dtor == false ); | ||
|
||
dtor = true; | ||
} | ||
|
||
void operator=( test_class&& ) = delete; | ||
void operator=( const test_class& ) = delete; | ||
}; | ||
|
||
using test_grammar = must< sor< alpha, digit > >; | ||
|
||
template< typename Rule > | ||
struct test_action | ||
: nothing< Rule > | ||
{}; | ||
|
||
template<> | ||
struct test_action< alpha > | ||
{ | ||
static void apply0() | ||
{ | ||
TAO_PEGTL_TEST_ASSERT( ctor == true ); | ||
TAO_PEGTL_TEST_ASSERT( dtor == false ); | ||
} | ||
}; | ||
|
||
template<> | ||
struct test_action< sor< alpha, digit > > | ||
: instantiate< test_class > | ||
{}; | ||
|
||
void unit_test() | ||
{ | ||
memory_input in( "a", __FUNCTION__ ); | ||
parse< test_grammar, test_action >( in ); | ||
|
||
TAO_PEGTL_TEST_ASSERT( ctor == true ); | ||
TAO_PEGTL_TEST_ASSERT( dtor == true ); | ||
} | ||
|
||
} // namespace TAO_PEGTL_NAMESPACE | ||
|
||
#include "main.hpp" |