Skip to content

Commit

Permalink
Contrib instantiate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Dec 17, 2020
1 parent 89f03e5 commit 5d26c14
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/tao/pegtl/contrib/instantiate.hpp
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
1 change: 1 addition & 0 deletions src/test/pegtl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(test_sources
contrib_function.cpp
contrib_http.cpp
contrib_if_then.cpp
contrib_instantiate.cpp
contrib_integer.cpp
contrib_json.cpp
contrib_parse_tree.cpp
Expand Down
72 changes: 72 additions & 0 deletions src/test/pegtl/contrib_instantiate.cpp
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"

0 comments on commit 5d26c14

Please sign in to comment.