Skip to content

Commit f53de73

Browse files
committed
Less intrusive auto discard.
1 parent 4380ff6 commit f53de73

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

include/tao/pegtl/internal/input_with_fakes.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ namespace TAO_PEGTL_NAMESPACE::internal
4141
void discard() const noexcept
4242
{}
4343

44-
void inc_rewind_guards() const noexcept
45-
{}
46-
47-
void dec_rewind_guards() const noexcept
48-
{}
49-
5044
// The stream_foo() member functions are only for actual stream inputs.
5145
};
5246

include/tao/pegtl/internal/rewind_guard.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
namespace TAO_PEGTL_NAMESPACE::internal
1414
{
15+
template< typename Input, typename = void >
16+
inline constexpr bool has_auto_discard = false;
17+
18+
template< typename Input >
19+
inline constexpr bool has_auto_discard< Input, decltype( (void)std::declval< Input >().inc_rewind_guards() ) > = true;
20+
1521
template< typename ParseInput >
1622
class rewind_guard
1723
{
@@ -20,7 +26,9 @@ namespace TAO_PEGTL_NAMESPACE::internal
2026
: m_input( &in ),
2127
m_saved( in.rewind_position() )
2228
{
23-
in.inc_rewind_guards();
29+
if constexpr( has_auto_discard< ParseInput > ) {
30+
in.inc_rewind_guards();
31+
}
2432
}
2533

2634
rewind_guard( rewind_guard&& rg ) noexcept
@@ -36,7 +44,9 @@ namespace TAO_PEGTL_NAMESPACE::internal
3644
{
3745
if( active() ) {
3846
m_input->rewind_to_position( m_saved );
39-
m_input->dec_rewind_guards();
47+
if constexpr( has_auto_discard< ParseInput > ) {
48+
m_input->dec_rewind_guards();
49+
}
4050
}
4151
}
4252

@@ -51,7 +61,9 @@ namespace TAO_PEGTL_NAMESPACE::internal
5161
[[nodiscard]] bool operator()( const bool result ) noexcept
5262
{
5363
if( result ) {
54-
m_input->dec_rewind_guards();
64+
if constexpr( has_auto_discard< ParseInput > ) {
65+
m_input->dec_rewind_guards();
66+
}
5567
m_input = nullptr;
5668
return true;
5769
}

include/tao/pegtl/stream/stream_input_base.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ namespace TAO_PEGTL_NAMESPACE::internal
139139
return std::size_t( this->buffer_start() + this->buffer_capacity() - m_end );
140140
}
141141

142-
void inc_rewind_guards() const noexcept
143-
{}
144-
145-
void dec_rewind_guards() const noexcept
146-
{}
147-
148142
protected:
149143
const data_t* m_current;
150144
const data_t* m_end;

include/tao/pegtl/stream/stream_input_with_auto_discard.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace TAO_PEGTL_NAMESPACE::internal
3737
auto_discard();
3838
}
3939

40+
[[nodiscard]] std::size_t get_rewind_guards() const noexcept
41+
{
42+
return m_rewind_guards;
43+
}
44+
4045
private:
4146
std::size_t m_rewind_guards = 0;
4247

src/test/stream_endless_input.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,25 @@ namespace TAO_PEGTL_NAMESPACE
3838
{
3939
array_endless_auto_input< void > in( 'a' );
4040
TAO_PEGTL_TEST_ASSERT( in.buffer_capacity() < 10000 );
41+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
4142

4243
for( std::size_t i = 0; i < 42; ++i ) {
4344
TAO_PEGTL_TEST_ASSERT( parse< rep< 3000, one< 'a' > > >( in ) );
45+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
4446
}
4547
for( std::size_t i = 0; i < 42; ++i ) {
4648
TAO_PEGTL_TEST_ASSERT( parse< rep< 3000, one< 'a' > >, nothing, normal, apply_mode::action, rewind_mode::required >( in ) );
49+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
4750
}
51+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
4852
TAO_PEGTL_TEST_THROWS( parse< rep< 12000, one< 'a' > >, nothing, normal, apply_mode::action, rewind_mode::required >( in ) );
53+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
4954
TAO_PEGTL_TEST_ASSERT( parse< rep< 12000, one< 'a' > > >( in ) );
55+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
5056
TAO_PEGTL_TEST_THROWS( parse< at< rep< 12000, one< 'a' > > > >( in ) );
57+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
5158
TAO_PEGTL_TEST_THROWS( parse< rep< 12000, one< 'a' > >, nop_action >( in ) );
59+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
5260
}
5361

5462
void test_multiple()
@@ -69,17 +77,25 @@ namespace TAO_PEGTL_NAMESPACE
6977
array_endless_auto_input< void > in( "abc" );
7078
TAO_PEGTL_TEST_ASSERT( parse< not_at< eof > >( in ) );
7179
TAO_PEGTL_TEST_ASSERT( in.buffer_capacity() < 10000 );
80+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
7281

7382
for( std::size_t i = 0; i < 42; ++i ) {
7483
TAO_PEGTL_TEST_ASSERT( parse< rep< 1000, string< 'a', 'b', 'c' > > >( in ) );
84+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
7585
}
7686
for( std::size_t i = 0; i < 42; ++i ) {
7787
TAO_PEGTL_TEST_ASSERT( parse< rep< 1000, string< 'a', 'b', 'c' > >, nothing, normal, apply_mode::action, rewind_mode::required >( in ) );
88+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
7889
}
90+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
7991
TAO_PEGTL_TEST_THROWS( parse< rep< 4000, string< 'a', 'b', 'c' > >, nothing, normal, apply_mode::action, rewind_mode::required >( in ) );
92+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
8093
TAO_PEGTL_TEST_ASSERT( parse< rep< 4000, string< 'a', 'b', 'c' > > >( in ) );
94+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
8195
TAO_PEGTL_TEST_THROWS( parse< at< rep< 4000, string< 'a', 'b', 'c' > > > >( in ) );
96+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
8297
TAO_PEGTL_TEST_THROWS( parse< rep< 4000, string< 'a', 'b', 'c' > >, nop_action >( in ) );
98+
TAO_PEGTL_TEST_ASSERT( in.get_rewind_guards() == 0 );
8399
}
84100

85101
void unit_test()

0 commit comments

Comments
 (0)