Skip to content

Commit

Permalink
Rename and out-source line_at.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Nov 12, 2023
1 parent d291081 commit abdf5c8
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 34 deletions.
5 changes: 2 additions & 3 deletions include/tao/pegtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
#include "pegtl/argv_input.hpp"
#include "pegtl/buffer_input.hpp"
#include "pegtl/cstream_input.hpp"
#include "pegtl/file_input.hpp"
#include "pegtl/istream_input.hpp"
#include "pegtl/memory_input.hpp"
#include "pegtl/read_input.hpp"
#include "pegtl/string_input.hpp"

// This has to be included *after* the above inputs,
// otherwise the amalgamated header will not work!
#include "pegtl/file_input.hpp"
#include "pegtl/line_view_at.hpp"

#include "pegtl/change_action.hpp"
#include "pegtl/change_action_and_state.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/tao/pegtl/contrib/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace TAO_PEGTL_NAMESPACE
{
std::cerr << std::setw( indent() ) << ' ' << TracerTraits::ansi_position << "position" << TracerTraits::ansi_reset << ' ' << m_position << '\n';
if constexpr( TracerTraits::print_source_line ) {
std::cerr << std::setw( indent() ) << ' ' << TracerTraits::ansi_position << "source" << TracerTraits::ansi_reset << ' ' << in.line_at( m_position ) << '\n';
std::cerr << std::setw( indent() ) << ' ' << TracerTraits::ansi_position << "source" << TracerTraits::ansi_reset << ' ' << line_view_at( in, m_position ) << '\n';
std::cerr << std::setw( indent() + 6 + m_position.column ) << ' ' << "^\n";
}
}
Expand Down
48 changes: 48 additions & 0 deletions include/tao/pegtl/line_view_at.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2014-2023 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)

#ifndef TAO_PEGTL_LINE_VIEW_AT_HPP
#define TAO_PEGTL_LINE_VIEW_AT_HPP

#include <string_view>

#include "apply_mode.hpp"
#include "config.hpp"
#include "normal.hpp"
#include "nothing.hpp"
#include "rewind_mode.hpp"
#include "tracking_mode.hpp"

#include "internal/at.hpp"
#include "internal/eolf.hpp"
#include "internal/until.hpp"

namespace TAO_PEGTL_NAMESPACE
{
template< typename Input >
[[nodiscard]] const char* begin_of_line( const Input& in, const TAO_PEGTL_NAMESPACE::position& p ) noexcept
{
return in.at( p ) - ( p.column - 1 );
}

template< typename Input >
[[nodiscard]] const char* end_of_line_or_file( const Input& in, const TAO_PEGTL_NAMESPACE::position& p ) noexcept
{
using input_t = memory_input< tracking_mode::lazy, typename Input::eol_t, const char* >;
input_t i2( in.at( p ), in.end(), "" );
using grammar = internal::until< internal::at< internal::eolf > >;
(void)normal< grammar >::match< apply_mode::nothing, rewind_mode::optional, nothing, normal >( i2 );
return i2.current();
}

template< typename Input >
[[nodiscard]] std::string_view line_view_at( const Input& in, const TAO_PEGTL_NAMESPACE::position& p ) noexcept
{
const char* b = begin_of_line( in, p );
return { b, static_cast< std::size_t >( end_of_line_or_file( in, p ) - b ) };
}

} // namespace TAO_PEGTL_NAMESPACE

#endif
20 changes: 0 additions & 20 deletions include/tao/pegtl/memory_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,26 +360,6 @@ namespace TAO_PEGTL_NAMESPACE
return this->begin() + p.byte;
}

[[nodiscard]] const char* begin_of_line( const TAO_PEGTL_NAMESPACE::position& p ) const noexcept
{
return at( p ) - ( p.column - 1 );
}

[[nodiscard]] const char* end_of_line( const TAO_PEGTL_NAMESPACE::position& p ) const noexcept
{
using input_t = memory_input< tracking_mode::lazy, Eol, const char* >;
input_t in( at( p ), this->end(), "" );
using grammar = internal::until< internal::at< internal::eolf > >;
(void)normal< grammar >::match< apply_mode::nothing, rewind_mode::optional, nothing, normal >( in );
return in.current();
}

[[nodiscard]] std::string_view line_at( const TAO_PEGTL_NAMESPACE::position& p ) const noexcept
{
const char* b = begin_of_line( p );
return { b, static_cast< std::size_t >( end_of_line( p ) - b ) };
}

void private_set_end( const char* new_end ) noexcept
{
this->m_end = new_end;
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/abnf2pegtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << '\n';
}
#else
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ int main( int argc, char** argv )
catch( const TAO_PEGTL_NAMESPACE::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << '\n';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/json_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const pegtl::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << std::endl
<< in.line_at( p ) << std::endl
<< line_view_at( in, p ) << std::endl
<< std::setw( int( p.column ) ) << '^' << std::endl;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/json_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const pegtl::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << std::endl;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/json_coverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const pegtl::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << std::endl;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/json_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const pegtl::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << std::endl;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/json_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const pegtl::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << std::endl;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/proto3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << '\n';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/example/pegtl/s_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
catch( const TAO_PEGTL_NAMESPACE::parse_error& e ) {
const auto& p = e.position_object();
std::cerr << e.what() << '\n'
<< in.line_at( p ) << '\n'
<< line_view_at( in, p ) << '\n'
<< std::setw( int( p.column ) ) << '^' << '\n';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/pegtl/parse_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace TAO_PEGTL_NAMESPACE
TAO_PEGTL_TEST_ASSERT( p.column == 5 );
TAO_PEGTL_TEST_ASSERT( p.source == "test_source" );

TAO_PEGTL_TEST_ASSERT( in.line_at( p ) == "bar bla blubb" );
TAO_PEGTL_TEST_ASSERT( line_view_at( in, p ) == "bar bla blubb" );

position p2 = p;
p2.source = "foo";
Expand Down

0 comments on commit abdf5c8

Please sign in to comment.