From abdf5c85303ebc368fb6cd9f280ee7ac374e89d0 Mon Sep 17 00:00:00 2001 From: "Dr. Colin Hirsch" Date: Sun, 12 Nov 2023 11:22:20 +0100 Subject: [PATCH] Rename and out-source line_at. --- include/tao/pegtl.hpp | 5 ++- include/tao/pegtl/contrib/trace.hpp | 2 +- include/tao/pegtl/line_view_at.hpp | 48 +++++++++++++++++++++++++++++ include/tao/pegtl/memory_input.hpp | 20 ------------ src/example/pegtl/abnf2pegtl.cpp | 2 +- src/example/pegtl/expression.cpp | 2 +- src/example/pegtl/json_ast.cpp | 2 +- src/example/pegtl/json_build.cpp | 2 +- src/example/pegtl/json_coverage.cpp | 2 +- src/example/pegtl/json_parse.cpp | 2 +- src/example/pegtl/json_trace.cpp | 2 +- src/example/pegtl/proto3.cpp | 2 +- src/example/pegtl/s_expression.cpp | 2 +- src/test/pegtl/parse_error.cpp | 2 +- 14 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 include/tao/pegtl/line_view_at.hpp diff --git a/include/tao/pegtl.hpp b/include/tao/pegtl.hpp index 4e7b9823f..2ef83e22e 100644 --- a/include/tao/pegtl.hpp +++ b/include/tao/pegtl.hpp @@ -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" diff --git a/include/tao/pegtl/contrib/trace.hpp b/include/tao/pegtl/contrib/trace.hpp index e71659138..60b73ba8a 100644 --- a/include/tao/pegtl/contrib/trace.hpp +++ b/include/tao/pegtl/contrib/trace.hpp @@ -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"; } } diff --git a/include/tao/pegtl/line_view_at.hpp b/include/tao/pegtl/line_view_at.hpp new file mode 100644 index 000000000..361a8118f --- /dev/null +++ b/include/tao/pegtl/line_view_at.hpp @@ -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 + +#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 diff --git a/include/tao/pegtl/memory_input.hpp b/include/tao/pegtl/memory_input.hpp index a7a637c15..bca435be4 100644 --- a/include/tao/pegtl/memory_input.hpp +++ b/include/tao/pegtl/memory_input.hpp @@ -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; diff --git a/src/example/pegtl/abnf2pegtl.cpp b/src/example/pegtl/abnf2pegtl.cpp index 951a521ba..ea15466a4 100644 --- a/src/example/pegtl/abnf2pegtl.cpp +++ b/src/example/pegtl/abnf2pegtl.cpp @@ -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 diff --git a/src/example/pegtl/expression.cpp b/src/example/pegtl/expression.cpp index ea1ad7d62..1f9dc9a36 100644 --- a/src/example/pegtl/expression.cpp +++ b/src/example/pegtl/expression.cpp @@ -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'; } } diff --git a/src/example/pegtl/json_ast.cpp b/src/example/pegtl/json_ast.cpp index 3ee91897e..e619c5dcc 100644 --- a/src/example/pegtl/json_ast.cpp +++ b/src/example/pegtl/json_ast.cpp @@ -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; } diff --git a/src/example/pegtl/json_build.cpp b/src/example/pegtl/json_build.cpp index 3dd9234ea..83ff2380f 100644 --- a/src/example/pegtl/json_build.cpp +++ b/src/example/pegtl/json_build.cpp @@ -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; } diff --git a/src/example/pegtl/json_coverage.cpp b/src/example/pegtl/json_coverage.cpp index 5773bae9f..3c89f0701 100644 --- a/src/example/pegtl/json_coverage.cpp +++ b/src/example/pegtl/json_coverage.cpp @@ -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; } diff --git a/src/example/pegtl/json_parse.cpp b/src/example/pegtl/json_parse.cpp index 1fedb8346..3c1a6e5d4 100644 --- a/src/example/pegtl/json_parse.cpp +++ b/src/example/pegtl/json_parse.cpp @@ -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; } diff --git a/src/example/pegtl/json_trace.cpp b/src/example/pegtl/json_trace.cpp index dc38bf992..0d21f8097 100644 --- a/src/example/pegtl/json_trace.cpp +++ b/src/example/pegtl/json_trace.cpp @@ -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; } diff --git a/src/example/pegtl/proto3.cpp b/src/example/pegtl/proto3.cpp index 0a7b449b4..5352fe126 100644 --- a/src/example/pegtl/proto3.cpp +++ b/src/example/pegtl/proto3.cpp @@ -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'; } } diff --git a/src/example/pegtl/s_expression.cpp b/src/example/pegtl/s_expression.cpp index 51a928f7d..688525fb1 100644 --- a/src/example/pegtl/s_expression.cpp +++ b/src/example/pegtl/s_expression.cpp @@ -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'; } } diff --git a/src/test/pegtl/parse_error.cpp b/src/test/pegtl/parse_error.cpp index 5c0c39545..20b3dd8ea 100644 --- a/src/test/pegtl/parse_error.cpp +++ b/src/test/pegtl/parse_error.cpp @@ -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";