1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
// Copyright (c) 2014-2020 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_JSON_PEGTL_MEMORY_INPUT_HPP
#define TAO_JSON_PEGTL_MEMORY_INPUT_HPP
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include "config.hpp"
#include "eol.hpp"
#include "normal.hpp"
#include "nothing.hpp"
#include "position.hpp"
#include "tracking_mode.hpp"
#include "internal/action_input.hpp"
#include "internal/at.hpp"
#include "internal/bump.hpp"
#include "internal/eolf.hpp"
#include "internal/iterator.hpp"
#include "internal/marker.hpp"
#include "internal/until.hpp"
namespace TAO_JSON_PEGTL_NAMESPACE
{
namespace internal
{
template< tracking_mode, typename Eol, typename Source >
class memory_input_base;
template< typename Eol, typename Source >
class memory_input_base< tracking_mode::eager, Eol, Source >
{
public:
using iterator_t = internal::iterator;
template< typename T >
memory_input_base( const iterator_t& in_begin, const char* in_end, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: m_begin( in_begin.data ),
m_current( in_begin ),
m_end( in_end ),
m_source( std::forward< T >( in_source ) )
{}
template< typename T >
memory_input_base( const char* in_begin, const char* in_end, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: m_begin( in_begin ),
m_current( in_begin ),
m_end( in_end ),
m_source( std::forward< T >( in_source ) )
{}
memory_input_base( const memory_input_base& ) = delete;
memory_input_base( memory_input_base&& ) = delete;
~memory_input_base() = default;
memory_input_base operator=( const memory_input_base& ) = delete;
memory_input_base operator=( memory_input_base&& ) = delete;
[[nodiscard]] const char* current() const noexcept
{
return m_current.data;
}
[[nodiscard]] const char* begin() const noexcept
{
return m_begin;
}
[[nodiscard]] const char* end( const std::size_t /*unused*/ = 0 ) const noexcept
{
return m_end;
}
[[nodiscard]] std::size_t byte() const noexcept
{
return m_current.byte;
}
[[nodiscard]] std::size_t line() const noexcept
{
return m_current.line;
}
[[nodiscard]] std::size_t column() const noexcept
{
return m_current.column;
}
void bump( const std::size_t in_count = 1 ) noexcept
{
internal::bump( m_current, in_count, Eol::ch );
}
void bump_in_this_line( const std::size_t in_count = 1 ) noexcept
{
internal::bump_in_this_line( m_current, in_count );
}
void bump_to_next_line( const std::size_t in_count = 1 ) noexcept
{
internal::bump_to_next_line( m_current, in_count );
}
[[nodiscard]] TAO_JSON_PEGTL_NAMESPACE::position position( const iterator_t& it ) const
{
return TAO_JSON_PEGTL_NAMESPACE::position( it, m_source );
}
void restart( const std::size_t in_byte = 0, const std::size_t in_line = 1, const std::size_t in_column = 1 )
{
assert( in_line != 0 );
assert( in_column != 0 );
m_current.data = m_begin;
m_current.byte = in_byte;
m_current.line = in_line;
m_current.column = in_column;
}
protected:
const char* const m_begin;
iterator_t m_current;
const char* const m_end;
const Source m_source;
};
template< typename Eol, typename Source >
class memory_input_base< tracking_mode::lazy, Eol, Source >
{
public:
using iterator_t = const char*;
template< typename T >
memory_input_base( const internal::iterator& in_begin, const char* in_end, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: m_begin( in_begin ),
m_current( in_begin.data ),
m_end( in_end ),
m_source( std::forward< T >( in_source ) )
{}
template< typename T >
memory_input_base( const char* in_begin, const char* in_end, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: m_begin( in_begin ),
m_current( in_begin ),
m_end( in_end ),
m_source( std::forward< T >( in_source ) )
{}
memory_input_base( const memory_input_base& ) = delete;
memory_input_base( memory_input_base&& ) = delete;
~memory_input_base() = default;
memory_input_base operator=( const memory_input_base& ) = delete;
memory_input_base operator=( memory_input_base&& ) = delete;
[[nodiscard]] const char* current() const noexcept
{
return m_current;
}
[[nodiscard]] const char* begin() const noexcept
{
return m_begin.data;
}
[[nodiscard]] const char* end( const std::size_t /*unused*/ = 0 ) const noexcept
{
return m_end;
}
[[nodiscard]] std::size_t byte() const noexcept
{
return std::size_t( current() - m_begin.data );
}
void bump( const std::size_t in_count = 1 ) noexcept
{
m_current += in_count;
}
void bump_in_this_line( const std::size_t in_count = 1 ) noexcept
{
m_current += in_count;
}
void bump_to_next_line( const std::size_t in_count = 1 ) noexcept
{
m_current += in_count;
}
[[nodiscard]] TAO_JSON_PEGTL_NAMESPACE::position position( const iterator_t it ) const
{
internal::iterator c( m_begin );
internal::bump( c, std::size_t( it - m_begin.data ), Eol::ch );
return TAO_JSON_PEGTL_NAMESPACE::position( c, m_source );
}
void restart()
{
m_current = m_begin.data;
}
protected:
const internal::iterator m_begin;
iterator_t m_current;
const char* const m_end;
const Source m_source;
};
} // namespace internal
template< tracking_mode P = tracking_mode::eager, typename Eol = eol::lf_crlf, typename Source = std::string >
class memory_input
: public internal::memory_input_base< P, Eol, Source >
{
public:
static constexpr tracking_mode tracking_mode_v = P;
using eol_t = Eol;
using source_t = Source;
using typename internal::memory_input_base< P, Eol, Source >::iterator_t;
using action_t = internal::action_input< memory_input >;
using internal::memory_input_base< P, Eol, Source >::memory_input_base;
template< typename T >
memory_input( const char* in_begin, const std::size_t in_size, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: memory_input( in_begin, in_begin + in_size, std::forward< T >( in_source ) )
{}
template< typename T >
memory_input( const std::string& in_string, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: memory_input( in_string.data(), in_string.size(), std::forward< T >( in_source ) )
{}
template< typename T >
memory_input( const std::string_view in_string, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: memory_input( in_string.data(), in_string.size(), std::forward< T >( in_source ) )
{}
template< typename T >
memory_input( std::string&&, T&& ) = delete;
template< typename T >
memory_input( const char* in_begin, T&& in_source ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: memory_input( in_begin, std::strlen( in_begin ), std::forward< T >( in_source ) )
{}
template< typename T >
memory_input( const char* in_begin, const char* in_end, T&& in_source, const std::size_t in_byte, const std::size_t in_line, const std::size_t in_column ) noexcept( std::is_nothrow_constructible_v< Source, T&& > )
: memory_input( { in_begin, in_byte, in_line, in_column }, in_end, std::forward< T >( in_source ) )
{}
memory_input( const memory_input& ) = delete;
memory_input( memory_input&& ) = delete;
~memory_input() = default;
memory_input operator=( const memory_input& ) = delete;
memory_input operator=( memory_input&& ) = delete;
[[nodiscard]] const Source& source() const noexcept
{
return this->m_source;
}
[[nodiscard]] bool empty() const noexcept
{
return this->current() == this->end();
}
[[nodiscard]] std::size_t size( const std::size_t /*unused*/ = 0 ) const noexcept
{
return std::size_t( this->end() - this->current() );
}
[[nodiscard]] char peek_char( const std::size_t offset = 0 ) const noexcept
{
return this->current()[ offset ];
}
[[nodiscard]] std::uint8_t peek_uint8( const std::size_t offset = 0 ) const noexcept
{
return static_cast< std::uint8_t >( peek_char( offset ) );
}
[[nodiscard]] iterator_t& iterator() noexcept
{
return this->m_current;
}
[[nodiscard]] const iterator_t& iterator() const noexcept
{
return this->m_current;
}
using internal::memory_input_base< P, Eol, Source >::restart;
template< rewind_mode M >
void restart( const internal::marker< iterator_t, M >& m ) noexcept
{
iterator() = m.iterator();
}
using internal::memory_input_base< P, Eol, Source >::position;
[[nodiscard]] TAO_JSON_PEGTL_NAMESPACE::position position() const
{
return position( iterator() );
}
void discard() const noexcept {}
void require( const std::size_t /*unused*/ ) const noexcept {}
template< rewind_mode M >
[[nodiscard]] internal::marker< iterator_t, M > mark() noexcept
{
return internal::marker< iterator_t, M >( iterator() );
}
[[nodiscard]] const char* at( const TAO_JSON_PEGTL_NAMESPACE::position& p ) const noexcept
{
return this->begin() + p.byte;
}
[[nodiscard]] const char* begin_of_line( const TAO_JSON_PEGTL_NAMESPACE::position& p ) const noexcept
{
return at( p ) - ( p.column - 1 );
}
[[nodiscard]] const char* end_of_line( const TAO_JSON_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::dontcare, nothing, normal >( in );
return in.current();
}
[[nodiscard]] std::string_view line_at( const TAO_JSON_PEGTL_NAMESPACE::position& p ) const noexcept
{
const char* b = begin_of_line( p );
return std::string_view( b, static_cast< std::size_t >( end_of_line( p ) - b ) );
}
};
template< typename... Ts >
memory_input( Ts&&... ) -> memory_input<>;
} // namespace TAO_JSON_PEGTL_NAMESPACE
#endif
|