Skip to content

Commit

Permalink
Single-rule nofollow.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Aug 6, 2024
1 parent 68f4461 commit 36fa073
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 17 deletions.
68 changes: 63 additions & 5 deletions include/tao/pegtl/contrib/trace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey
// Copyright (c) 2020-2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

Expand All @@ -21,6 +21,7 @@
#include "../nothing.hpp"
#include "../parse.hpp"
#include "../rewind_mode.hpp"
#include "../type_list.hpp"

namespace TAO_PEGTL_NAMESPACE
{
Expand Down Expand Up @@ -54,6 +55,26 @@ namespace TAO_PEGTL_NAMESPACE
using standard_tracer_traits = tracer_traits< true >;
using complete_tracer_traits = tracer_traits< false >;

template< typename RuleList >
struct include_tracer_traits
: tracer_traits<>
{
static_assert( is_type_list_v< RuleList > );

template< typename Rule >
static constexpr bool enable = type_list_contains< Rule, RuleList >::value;
};

template< typename RuleList, bool HideInternal = true >
struct exclude_tracer_traits
: tracer_traits< HideInternal >
{
static_assert( is_type_list_v< RuleList > );

template< typename Rule >
static constexpr bool enable = ( !type_list_contains< Rule, RuleList >::value ) && ( tracer_traits< HideInternal >::template enable< Rule > );
};

template< typename TracerTraits >
struct tracer
{
Expand Down Expand Up @@ -191,14 +212,22 @@ namespace TAO_PEGTL_NAMESPACE
}
};

using standard_tracer = tracer< standard_tracer_traits >;
using complete_tracer = tracer< complete_tracer_traits >;

template< typename RuleList >
using include_tracer = tracer< include_tracer_traits< RuleList > >;
template< typename RuleList >
using exclude_tracer = tracer< exclude_tracer_traits< RuleList > >;

template< typename Rule,
template< typename... > class Action = nothing,
template< typename... > class Control = normal,
typename ParseInput,
typename... States >
bool standard_trace( ParseInput&& in, States&&... st )
{
tracer< standard_tracer_traits > tr( in );
standard_tracer tr( in );
return tr.parse< Rule, Action, Control >( in, st... );
}

Expand All @@ -209,10 +238,34 @@ namespace TAO_PEGTL_NAMESPACE
typename... States >
bool complete_trace( ParseInput&& in, States&&... st )
{
tracer< complete_tracer_traits > tr( in );
complete_tracer tr( in );
return tr.parse< Rule, Action, Control >( in, st... );
}

template< typename RuleList,
typename Rule,
template< typename... > class Action = nothing,
template< typename... > class Control = normal,
typename ParseInput,
typename... States >
bool include_trace( ParseInput&& in, States&&... st )
{
include_tracer< RuleList > tr( in );
return tr.template parse< Rule, Action, Control >( in, st... );
}

template< typename RuleList,
typename Rule,
template< typename... > class Action = nothing,
template< typename... > class Control = normal,
typename ParseInput,
typename... States >
bool exclude_trace( ParseInput&& in, States&&... st )
{
exclude_tracer< RuleList > tr( in );
return tr.template parse< Rule, Action, Control >( in, st... );
}

template< typename Tracer >
struct trace
: maybe_nothing
Expand Down Expand Up @@ -240,8 +293,13 @@ namespace TAO_PEGTL_NAMESPACE
}
};

using trace_standard = trace< tracer< standard_tracer_traits > >;
using trace_complete = trace< tracer< complete_tracer_traits > >;
using trace_standard = trace< standard_tracer >;
using trace_complete = trace< complete_tracer >;

template< typename RuleList >
using trace_include = trace< include_tracer< RuleList > >;
template< typename RuleList >
using trace_exclude = trace< exclude_tracer< RuleList > >;

} // namespace TAO_PEGTL_NAMESPACE

Expand Down
28 changes: 27 additions & 1 deletion include/tao/pegtl/type_list.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey
// Copyright (c) 2020-2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

Expand Down Expand Up @@ -42,6 +42,32 @@ namespace TAO_PEGTL_NAMESPACE
template< typename... Ts >
using type_list_concat_t = typename type_list_concat< Ts... >::type;

template< typename Type, typename... Types >
inline constexpr bool type_list_contains_v = ( std::is_same_v< Type, Types > || ... );

template< typename Type, typename... Types >
struct type_list_contains
: std::bool_constant< type_list_contains_v< Type, Types... > >
{};

template< typename Type, typename... Types >
struct type_list_contains< Type, type_list< Types... > >
: type_list_contains< Type, Types... >
{};

template< typename >
struct is_type_list
: std::false_type
{};

template< typename... Ts >
struct is_type_list< type_list< Ts... > >
: std::true_type
{};

template< typename T >
inline constexpr bool is_type_list_v = is_type_list< T >::value;

} // 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 @@ -52,6 +52,7 @@ set(test_sources
contrib_to_string.cpp
contrib_trace1.cpp
contrib_trace2.cpp
contrib_trace3.cpp
contrib_unescape.cpp
contrib_uri.cpp
control_unwind.cpp
Expand Down
6 changes: 2 additions & 4 deletions src/test/pegtl/contrib_trace1.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2020-2023 Dr. Colin Hirsch and Daniel Frey
// Copyright (c) 2020-2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include <iostream>

#include "test.hpp"

#include <tao/pegtl/contrib/trace.hpp>
Expand All @@ -25,7 +23,7 @@ namespace TAO_PEGTL_NAMESPACE
void unit_test()
{
const std::string data = "F";
memory_input in( data, __FILE__ );
memory_input in( data, "trace test -- please ignore" );
// Just enough to see that it compiles and nothing explodes;
// the output format probabaly changes between compilers and
// versions making a proper test difficult.
Expand Down
14 changes: 7 additions & 7 deletions src/test/pegtl/contrib_trace2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2023 Dr. Colin Hirsch and Daniel Frey
// Copyright (c) 2014-2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

Expand Down Expand Up @@ -51,41 +51,41 @@ namespace TAO_PEGTL_NAMESPACE
void unit_test()
{
{
memory_input in( "ab", "trace test please ignore" );
memory_input in( "ab", "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR1 >( in );
TAO_PEGTL_TEST_ASSERT( result );
TAO_PEGTL_TEST_ASSERT( test::a0 == 0 );
TAO_PEGTL_TEST_ASSERT( test::a == 0 );
}
{
memory_input in( "ab", "trace test please ignore" );
memory_input in( "ab", "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR1, test::trace_action >( in );
TAO_PEGTL_TEST_ASSERT( result );
TAO_PEGTL_TEST_ASSERT( test::a0 == 1 );
TAO_PEGTL_TEST_ASSERT( test::a == 1 );
}
{
memory_input in( "a\r\n\t\0b", 6, "trace test please ignore" );
memory_input in( "a\r\n\t\0b", 6, "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR2 >( in );
TAO_PEGTL_TEST_ASSERT( result );
TAO_PEGTL_TEST_ASSERT( test::a0 == 1 );
TAO_PEGTL_TEST_ASSERT( test::a == 1 );
}
{
memory_input in( "a\r\n\t\0b", 6, "trace test please ignore" );
memory_input in( "a\r\n\t\0b", 6, "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR2, test::trace_action >( in );
TAO_PEGTL_TEST_ASSERT( result );
TAO_PEGTL_TEST_ASSERT( test::a0 == 2 );
TAO_PEGTL_TEST_ASSERT( test::a == 1 );
}
{
memory_input in( "c", "trace test please ignore" );
memory_input in( "c", "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR3 >( in );
TAO_PEGTL_TEST_ASSERT( !result );
}
#if defined( __cpp_exceptions )
{
memory_input in( "c", "trace test please ignore" );
memory_input in( "c", "trace test -- please ignore" );
const auto result = standard_trace< test::GRAMMAR4 >( in );
TAO_PEGTL_TEST_ASSERT( !result );
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/pegtl/contrib_trace3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#include "test.hpp"

#include <tao/pegtl/contrib/trace.hpp>

namespace test
{
using namespace TAO_PEGTL_NAMESPACE;

using grammar = seq< sor< one< 'a' >, one< 'F' > >, eof >;

} // namespace test

namespace TAO_PEGTL_NAMESPACE
{
void unit_test()
{
const std::string data = "F";
memory_input in( data, "trace test -- please ignore" );
// Just enough to see that it compiles and nothing explodes.
include_trace< type_list< one< 'a' >, one< 'F' > >, test::grammar >( in );
in.restart();
exclude_trace< type_list< one< 'a' > >, test::grammar >( in );
}

} // namespace TAO_PEGTL_NAMESPACE

#include "main.hpp"

0 comments on commit 36fa073

Please sign in to comment.