Skip to content

Commit

Permalink
Transitional refactoring of inputerator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Nov 15, 2023
1 parent 7f8953f commit b83a4c8
Show file tree
Hide file tree
Showing 26 changed files with 211 additions and 201 deletions.
3 changes: 3 additions & 0 deletions doc/Changelog.md
Expand Up @@ -29,6 +29,9 @@
* Moved depth counter to adapter class `input_with_depth` in [contrib](Contrib-and-Examples#contrib).
* Changed default top-level `rewind_mode` to ~~`dontcare`~~ `optional`.
* Replaced `rewind_mode` values `dontcare` and `active` with new value `optional`.
* Moved `line_at()` from input member function to global function `line_view_at()`.
* Moved `begin_of_line()` from input member function to global function of same name.
* Moved `end_of_line()` from input member function to global function of same name.
* Removed support for `boost::filesystem` and `std::experimental::filesystem`.
* Removed support for building an amalgamated header.
* Removed support for Visual Studio 2017.
Expand Down
22 changes: 9 additions & 13 deletions include/tao/pegtl/buffer_input.hpp
Expand Up @@ -37,12 +37,13 @@ namespace TAO_PEGTL_NAMESPACE
class buffer_input
{
public:
using data_t = char;
using reader_t = Reader;

using eol_t = Eol;
using source_t = Source;

using inputerator_t = internal::inputerator;
using rewind_position_t = internal::large_position;

using action_t = internal::action_input< buffer_input >;

Expand Down Expand Up @@ -170,27 +171,22 @@ namespace TAO_PEGTL_NAMESPACE
return internal::rewind_guard< M, buffer_input >( this );
}

[[nodiscard]] const inputerator_t& rewind_position() noexcept
{
return m_current;
}

void rewind_position( const inputerator_t& data ) noexcept
void rewind_position( const rewind_position_t& data ) noexcept
{
m_current = data;
}

[[nodiscard]] TAO_PEGTL_NAMESPACE::position previous_position( const inputerator_t& it ) const
[[nodiscard]] position current_position() const
{
return TAO_PEGTL_NAMESPACE::position( it, m_source );
return position( m_current );
}

[[nodiscard]] TAO_PEGTL_NAMESPACE::position current_position() const
[[nodiscard]] position previous_position( const rewind_position_t& it ) const
{
return position( m_current );
return position( it, m_source );
}

[[nodiscard]] const inputerator_t& inputerator() const noexcept
[[nodiscard]] const auto& rewind_position() const noexcept
{
return m_current;
}
Expand Down Expand Up @@ -222,7 +218,7 @@ namespace TAO_PEGTL_NAMESPACE
Reader m_reader;
std::size_t m_maximum;
std::unique_ptr< char[] > m_buffer;
inputerator_t m_current;
rewind_position_t m_current;
char* m_end;
const Source m_source;

Expand Down
2 changes: 2 additions & 0 deletions include/tao/pegtl/contrib/input_with_depth.hpp
Expand Up @@ -56,6 +56,8 @@ namespace TAO_PEGTL_NAMESPACE
return internal::depth_guard( m_depth ); // NOLINT(google-readability-casting)
}

// TODO: Reset depth to 0 on restart() for restartable inputs including test!

[[nodiscard]] std::size_t current_depth() const noexcept
{
return m_depth;
Expand Down
11 changes: 6 additions & 5 deletions include/tao/pegtl/contrib/parse_tree.hpp
Expand Up @@ -43,8 +43,8 @@ namespace TAO_PEGTL_NAMESPACE::parse_tree
std::string_view type;
Source source;

TAO_PEGTL_NAMESPACE::internal::inputerator m_begin;
TAO_PEGTL_NAMESPACE::internal::inputerator m_end;
TAO_PEGTL_NAMESPACE::internal::large_position m_begin;
TAO_PEGTL_NAMESPACE::internal::large_position m_end;

// each node will be default constructed
basic_node() = default;
Expand Down Expand Up @@ -115,7 +115,8 @@ namespace TAO_PEGTL_NAMESPACE::parse_tree
template< typename... States >
void remove_content( States&&... /*unused*/ ) noexcept
{
m_end = TAO_PEGTL_NAMESPACE::internal::inputerator();
m_begin = TAO_PEGTL_NAMESPACE::internal::large_position();
m_end = TAO_PEGTL_NAMESPACE::internal::large_position();
}

// all non-root nodes are initialized by calling this method
Expand All @@ -124,14 +125,14 @@ namespace TAO_PEGTL_NAMESPACE::parse_tree
{
set_type< Rule >();
source = in.source();
m_begin = TAO_PEGTL_NAMESPACE::internal::inputerator( in.inputerator() );
m_begin = TAO_PEGTL_NAMESPACE::internal::large_position( in.rewind_position() );
}

// if parsing of the rule succeeded, this method is called
template< typename Rule, typename ParseInput, typename... States >
void success( const ParseInput& in, States&&... /*unused*/ ) noexcept
{
m_end = TAO_PEGTL_NAMESPACE::internal::inputerator( in.inputerator() );
m_end = TAO_PEGTL_NAMESPACE::internal::large_position( in.rewind_position() );
}

// if parsing of the rule failed, this method is called
Expand Down
4 changes: 2 additions & 2 deletions include/tao/pegtl/contrib/remove_first_state.hpp
Expand Up @@ -59,8 +59,8 @@ namespace TAO_PEGTL_NAMESPACE
Base::unwind( in, st... );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename State, typename... States >
static auto apply( const Inputerator& begin, const ParseInput& in, State&& /*unused*/, States&&... st ) noexcept( noexcept( Base::template apply< Action >( begin, in, st... ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename State, typename... States >
static auto apply( const RewindPosition& begin, const ParseInput& in, State&& /*unused*/, States&&... st ) noexcept( noexcept( Base::template apply< Action >( begin, in, st... ) ) )
-> decltype( Base::template apply< Action >( begin, in, st... ) )
{
return Base::template apply< Action >( begin, in, st... );
Expand Down
8 changes: 4 additions & 4 deletions include/tao/pegtl/contrib/remove_last_states.hpp
Expand Up @@ -97,15 +97,15 @@ namespace TAO_PEGTL_NAMESPACE
unwind_impl( in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) - N >() );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename Tuple, std::size_t... Is >
static auto apply_impl( const Inputerator& begin, const ParseInput& in, const Tuple& t, std::index_sequence< Is... > /*unused*/ ) noexcept( noexcept( Base::template apply< Action >( begin, in, std::get< Is >( t )... ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename Tuple, std::size_t... Is >
static auto apply_impl( const RewindPosition& begin, const ParseInput& in, const Tuple& t, std::index_sequence< Is... > /*unused*/ ) noexcept( noexcept( Base::template apply< Action >( begin, in, std::get< Is >( t )... ) ) )
-> decltype( Base::template apply< Action >( begin, in, std::get< Is >( t )... ) )
{
return Base::template apply< Action >( begin, in, std::get< Is >( t )... );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename... States >
static auto apply( const Inputerator& begin, const ParseInput& in, States&&... st ) noexcept( noexcept( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) - N >() ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename... States >
static auto apply( const RewindPosition& begin, const ParseInput& in, States&&... st ) noexcept( noexcept( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) - N >() ) ) )
-> decltype( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) - N >() ) )
{
return apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) - N >() );
Expand Down
12 changes: 6 additions & 6 deletions include/tao/pegtl/contrib/shuffle_states.hpp
Expand Up @@ -155,22 +155,22 @@ namespace TAO_PEGTL_NAMESPACE
Base::unwind( in, st );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename Tuple, std::size_t... Is >
static auto apply_impl( const Inputerator& begin, const ParseInput& in, const Tuple& t, std::index_sequence< Is... > /*unused*/ ) noexcept( noexcept( Base::template apply< Action >( begin, in, std::get< Shuffle::template value< Is, sizeof...( Is ) > >( t )... ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename Tuple, std::size_t... Is >
static auto apply_impl( const RewindPosition& begin, const ParseInput& in, const Tuple& t, std::index_sequence< Is... > /*unused*/ ) noexcept( noexcept( Base::template apply< Action >( begin, in, std::get< Shuffle::template value< Is, sizeof...( Is ) > >( t )... ) ) )
-> decltype( Base::template apply< Action >( begin, in, std::get< Shuffle::template value< Is, sizeof...( Is ) > >( t )... ) )
{
return Base::template apply< Action >( begin, in, std::get< Shuffle::template value< Is, sizeof...( Is ) > >( t )... );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename... States >
static auto apply( const Inputerator& begin, const ParseInput& in, States&&... st ) noexcept( noexcept( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) >() ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename... States >
static auto apply( const RewindPosition& begin, const ParseInput& in, States&&... st ) noexcept( noexcept( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) >() ) ) )
-> decltype( apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) >() ) )
{
return apply_impl< Action >( begin, in, std::tie( st... ), std::make_index_sequence< sizeof...( st ) >() );
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename State >
static auto apply( const Inputerator& begin, const ParseInput& in, State&& st ) noexcept( noexcept( Base::template apply< Action >( begin, in, st ) ) )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename State >
static auto apply( const RewindPosition& begin, const ParseInput& in, State&& st ) noexcept( noexcept( Base::template apply< Action >( begin, in, st ) ) )
-> decltype( Base::template apply< Action >( begin, in, st ) )
{
return Base::template apply< Action >( begin, in, st );
Expand Down
4 changes: 2 additions & 2 deletions include/tao/pegtl/contrib/state_control.hpp
Expand Up @@ -103,8 +103,8 @@ namespace TAO_PEGTL_NAMESPACE
#endif
}

template< template< typename... > class Action, typename Inputerator, typename ParseInput, typename State, typename... States >
static auto apply( const Inputerator& begin, const ParseInput& in, [[maybe_unused]] State& state, States&&... st )
template< template< typename... > class Action, typename RewindPosition, typename ParseInput, typename State, typename... States >
static auto apply( const RewindPosition& begin, const ParseInput& in, [[maybe_unused]] State& state, States&&... st )
-> decltype( Control< Rule >::template apply< Action >( begin, in, st... ) )
{
if constexpr( State::template enable< Rule > ) {
Expand Down
77 changes: 40 additions & 37 deletions include/tao/pegtl/internal/action_input.hpp
Expand Up @@ -10,61 +10,44 @@
#include <string>
#include <string_view>

#include "inputerator.hpp"

#include "../config.hpp"
#include "../position.hpp"

namespace TAO_PEGTL_NAMESPACE::internal
{
template< typename ParseInput >
class action_input
{
public:
using data_t = typename ParseInput::data_t;
using input_t = ParseInput;
using inputerator_t = typename ParseInput::inputerator_t;
using rewind_position_t = typename ParseInput::rewind_position_t;

action_input( const inputerator_t& in_begin, const ParseInput& in_input ) noexcept
action_input( const rewind_position_t& in_begin, const ParseInput& in_input ) noexcept
: m_begin( in_begin ),
m_input( in_input )
{}

action_input( const action_input& ) = delete;
action_input( action_input&& ) = delete;
action_input( const action_input& ) = delete;

~action_input() = default;

action_input& operator=( const action_input& ) = delete;
action_input& operator=( action_input&& ) = delete;

[[nodiscard]] const inputerator_t& inputerator() const noexcept
{
return m_begin;
}

[[nodiscard]] const ParseInput& input() const noexcept
{
return m_input;
}
void operator=( action_input&& ) = delete;
void operator=( const action_input& ) = delete;

[[nodiscard]] const char* current() const noexcept
[[nodiscard]] const auto* begin() const noexcept
{
return begin();
return m_begin.data;
}

[[nodiscard]] const char* begin() const noexcept
[[nodiscard]] const auto* current() const noexcept
{
if constexpr( std::is_same_v< inputerator_t, const char* > ) {
return inputerator();
}
else {
return inputerator().data;
}
return m_begin.data;
}

[[nodiscard]] const char* end() const noexcept
[[nodiscard]] const auto* end() const noexcept
{
return input().current();
return m_input.current();
}

[[nodiscard]] bool empty() const noexcept
Expand All @@ -74,36 +57,56 @@ namespace TAO_PEGTL_NAMESPACE::internal

[[nodiscard]] std::size_t size() const noexcept
{
return std::size_t( end() - begin() );
return std::size_t( end() - m_begin.data );
}

[[nodiscard]] std::string string() const
{
return std::string( begin(), size() );
return std::string( static_cast< const char* >( m_begin.data ), size() );
}

[[nodiscard]] std::string_view string_view() const noexcept
{
return std::string_view( begin(), size() );
return std::string_view( static_cast< const char* >( m_begin.data ), size() );
}

[[nodiscard]] data_t peek( const std::size_t offset = 0 ) const noexcept
{
return m_begin.data[ offset ];
}

[[nodiscard]] char peek_char( const std::size_t offset = 0 ) const noexcept
{
return begin()[ offset ];
return static_cast< char >( peek( offset ) );
}

[[nodiscard]] std::byte peek_byte( const std::size_t offset = 0 ) const noexcept
{
return static_cast< std::byte >( peek( offset ) );
}

[[nodiscard]] std::uint8_t peek_uint8( const std::size_t offset = 0 ) const noexcept
{
return static_cast< std::uint8_t >( peek_char( offset ) );
return static_cast< std::uint8_t >( peek( offset ) );
}

[[nodiscard]] const ParseInput& input() const noexcept
{
return m_input;
}

[[nodiscard]] const rewind_position_t& rewind_position() const noexcept
{
return m_begin;
}

[[nodiscard]] TAO_PEGTL_NAMESPACE::position current_position() const
[[nodiscard]] decltype( auto ) current_position() const
{
return input().previous_position( inputerator() ); // NOTE: Not efficient with lazy inputs.
return m_input.previous_position( m_begin ); // NOTE: O(n) with lazy inputs -- n is return value!
}

protected:
const inputerator_t m_begin;
const rewind_position_t m_begin;
const ParseInput& m_input;
};

Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/internal/apply.hpp
Expand Up @@ -33,7 +33,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
{
if constexpr( ( A == apply_mode::action ) && ( sizeof...( Actions ) > 0 ) ) {
using action_t = typename ParseInput::action_t;
const action_t i2( in.inputerator(), in ); // No data -- range is from begin to begin.
const action_t i2( in.rewind_position(), in ); // No data -- range is from begin to begin.
return ( apply_single< Actions >::match( i2, st... ) && ... );
}
else {
Expand Down
6 changes: 3 additions & 3 deletions include/tao/pegtl/internal/bump.hpp
Expand Up @@ -11,7 +11,7 @@

namespace TAO_PEGTL_NAMESPACE::internal
{
inline void bump( inputerator& iter, const std::size_t count, const int ch ) noexcept
inline void bump( large_position& iter, const std::size_t count, const int ch ) noexcept
{
for( std::size_t i = 0; i < count; ++i ) {
if( iter.data[ i ] == ch ) {
Expand All @@ -26,14 +26,14 @@ namespace TAO_PEGTL_NAMESPACE::internal
iter.data += count;
}

inline void bump_in_this_line( inputerator& iter, const std::size_t count ) noexcept
inline void bump_in_this_line( large_position& iter, const std::size_t count ) noexcept
{
iter.data += count;
iter.byte += count;
iter.column += count;
}

inline void bump_to_next_line( inputerator& iter, const std::size_t count ) noexcept
inline void bump_to_next_line( large_position& iter, const std::size_t count ) noexcept
{
++iter.line;
iter.byte += count;
Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/internal/if_apply.hpp
Expand Up @@ -35,7 +35,7 @@ namespace TAO_PEGTL_NAMESPACE::internal
using action_t = typename ParseInput::action_t;
auto m = in.template make_rewind_guard< rewind_mode::required >();
if( Control< Rule >::template match< apply_mode::action, rewind_mode::optional, Action, Control >( in, st... ) ) {
const action_t i2( m.inputerator(), in );
const action_t i2( m.rewind_position(), in );
return m( ( apply_single< Actions >::match( i2, st... ) && ... ) );
}
return false;
Expand Down

0 comments on commit b83a4c8

Please sign in to comment.