Skip to content

Commit e0514ad

Browse files
committed
Small refactorings and improvements.
1 parent e6dc5c4 commit e0514ad

File tree

14 files changed

+337
-22
lines changed

14 files changed

+337
-22
lines changed

include/tao/pegtl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#if defined( __cpp_exceptions )
1616
#include "pegtl/parse_error.hpp"
1717
#include "pegtl/parse_error_base.hpp"
18+
#include "pegtl/parse_nested.hpp"
1819
#endif
1920

20-
#include "pegtl/parse_nested.hpp"
2121
#include "pegtl/pegtl_string.hpp"
2222

2323
#include "pegtl/unicode/utf8.hpp"

include/tao/pegtl/contrib/unescape.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace TAO_PEGTL_NAMESPACE::unescape
3737
};
3838

3939
// The unescape_c action MUST be called for a character matching One which MUST
40-
// be a rule with the same rule_t as TAO_PEGTL_NAMESPACE::ascii::one< ... >.
40+
// be, or publicly derive from, TAO_PEGTL_NAMESPACE::ascii::one< ... >.
4141

4242
template< typename One, char... Rs >
4343
struct unescape_c

include/tao/pegtl/internal/mmap_file_base.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ namespace TAO_PEGTL_NAMESPACE::internal
4141
template< typename Data >
4242
[[nodiscard]] std::size_t mmap_file_size() const
4343
{
44-
if( ( sizeof( Data ) == 1 ) || ( m_mmap_data.size() % sizeof( Data ) == 0 ) ) {
45-
return m_mmap_data.size() / sizeof( Data );
44+
if constexpr( sizeof( Data ) == 1 ) {
45+
return m_mmap_data.size();
4646
}
47+
else {
48+
if( m_mmap_data.size() % sizeof( Data ) == 0 ) {
49+
return m_mmap_data.size() / sizeof( Data );
50+
}
4751
#if defined( __cpp_exceptions )
48-
throw std::runtime_error( "mmap() file size is not multiple of data size" );
52+
throw std::runtime_error( "mmap() file size is not multiple of data size" );
4953
#else
50-
std::perror( "mmap() file size is not multiple of data size" );
51-
std::terminate();
54+
std::perror( "mmap() file size is not multiple of data size" );
55+
std::terminate();
5256
#endif
57+
}
5358
}
5459

5560
template< typename Data >

include/tao/pegtl/internal/peek_member.hpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ namespace TAO_PEGTL_NAMESPACE::internal
6363
}
6464
};
6565

66+
#if defined( __GNUC__ ) || defined( __clang__ )
67+
6668
// For global getter functions that return a T.
6769

6870
template< bool N, typename C, typename T, T ( *P )( const C& ) noexcept( N ) >
@@ -159,6 +161,178 @@ namespace TAO_PEGTL_NAMESPACE::internal
159161
}
160162
};
161163

164+
#else // MSVC can't deduce the N in template< bool N ..... noexcept( N ) >
165+
166+
template< typename C, typename T, T ( *P )( const C& ) noexcept( true ) >
167+
struct peek_member_impl< T ( * )( const C& ) noexcept( true ), P >
168+
{
169+
using data_t = T;
170+
using pair_t = data_and_size< data_t, std::uint8_t >;
171+
172+
template< typename ParseInput >
173+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
174+
{
175+
if( in.size( offset + 1 ) >= ( offset + 1 ) ) {
176+
return pair_t( P( *in.current( offset ) ), 1 );
177+
}
178+
return pair_t();
179+
}
180+
};
181+
182+
template< typename C, typename T, T ( *P )( const C& ) noexcept( false ) >
183+
struct peek_member_impl< T ( * )( const C& ) noexcept( false ), P >
184+
{
185+
using data_t = T;
186+
using pair_t = data_and_size< data_t, std::uint8_t >;
187+
188+
template< typename ParseInput >
189+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false )
190+
{
191+
if( in.size( offset + 1 ) >= ( offset + 1 ) ) {
192+
return pair_t( P( *in.current( offset ) ), 1 );
193+
}
194+
return pair_t();
195+
}
196+
};
197+
198+
template< typename C, typename T, const T& ( *P )( const C& ) noexcept( true ) >
199+
struct peek_member_impl< const T& ( * )( const C& ) noexcept( true ), P >
200+
{
201+
using data_t = T;
202+
using pair_t = data_and_size< data_t, void >;
203+
204+
template< typename ParseInput >
205+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
206+
{
207+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? &P( *in.current( offset ) ) : nullptr );
208+
}
209+
};
210+
211+
template< typename C, typename T, const T& ( *P )( const C& ) noexcept( false ) >
212+
struct peek_member_impl< const T& ( * )( const C& ) noexcept( false ), P >
213+
{
214+
using data_t = T;
215+
using pair_t = data_and_size< data_t, void >;
216+
217+
template< typename ParseInput >
218+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false)
219+
{
220+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? &P( *in.current( offset ) ) : nullptr );
221+
}
222+
};
223+
224+
template< typename C, typename T, const T* ( *P )( const C& ) noexcept( true ) >
225+
struct peek_member_impl< const T* ( * )( const C& ) noexcept( true ), P >
226+
{
227+
using data_t = T;
228+
using pair_t = data_and_size< data_t, void >;
229+
230+
template< typename ParseInput >
231+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
232+
{
233+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? P( *in.current( offset ) ) : nullptr );
234+
}
235+
};
236+
237+
template< typename C, typename T, const T* ( *P )( const C& ) noexcept( false ) >
238+
struct peek_member_impl< const T* ( * )( const C& ) noexcept( false ), P >
239+
{
240+
using data_t = T;
241+
using pair_t = data_and_size< data_t, void >;
242+
243+
template< typename ParseInput >
244+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false )
245+
{
246+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? P( *in.current( offset ) ) : nullptr );
247+
}
248+
};
249+
250+
template< typename C, typename T, T ( C::*P )() const noexcept( true ) >
251+
struct peek_member_impl< T ( C::* )() const noexcept( true ), P >
252+
{
253+
using data_t = T;
254+
using pair_t = data_and_size< data_t, std::uint8_t >;
255+
256+
template< typename ParseInput >
257+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
258+
{
259+
if( in.size( offset + 1 ) >= ( offset + 1 ) ) {
260+
return pair_t( ( in.current( offset )->*P )(), 1 );
261+
}
262+
return pair_t();
263+
}
264+
};
265+
266+
template< typename C, typename T, T ( C::*P )() const noexcept( false ) >
267+
struct peek_member_impl< T ( C::* )() const noexcept( false ), P >
268+
{
269+
using data_t = T;
270+
using pair_t = data_and_size< data_t, std::uint8_t >;
271+
272+
template< typename ParseInput >
273+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false )
274+
{
275+
if( in.size( offset + 1 ) >= ( offset + 1 ) ) {
276+
return pair_t( ( in.current( offset )->*P )(), 1 );
277+
}
278+
return pair_t();
279+
}
280+
};
281+
282+
template< typename C, typename T, const T& ( C::*P )() const noexcept( true ) >
283+
struct peek_member_impl< const T& ( C::* )() const noexcept( true ), P >
284+
{
285+
using data_t = T;
286+
using pair_t = data_and_size< data_t, void >;
287+
288+
template< typename ParseInput >
289+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
290+
{
291+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? &( ( in.current( offset )->*P )() ) : nullptr );
292+
}
293+
};
294+
295+
template< typename C, typename T, const T& ( C::*P )() const noexcept( false ) >
296+
struct peek_member_impl< const T& ( C::* )() const noexcept( false ), P >
297+
{
298+
using data_t = T;
299+
using pair_t = data_and_size< data_t, void >;
300+
301+
template< typename ParseInput >
302+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false )
303+
{
304+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? &( ( in.current( offset )->*P )() ) : nullptr );
305+
}
306+
};
307+
308+
template< typename C, typename T, const T* ( C::*P )() const noexcept( true ) >
309+
struct peek_member_impl< const T* ( C::* )() const noexcept( true ), P >
310+
{
311+
using data_t = T;
312+
using pair_t = data_and_size< data_t, void >;
313+
314+
template< typename ParseInput >
315+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( noexcept( in.size( 1 ) ) )
316+
{
317+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? ( in.current( offset )->*P )() : nullptr );
318+
}
319+
};
320+
321+
template< typename C, typename T, const T* ( C::*P )() const noexcept( false ) >
322+
struct peek_member_impl< const T* ( C::* )() const noexcept( false ), P >
323+
{
324+
using data_t = T;
325+
using pair_t = data_and_size< data_t, void >;
326+
327+
template< typename ParseInput >
328+
[[nodiscard]] static pair_t peek( ParseInput& in, const std::size_t offset = 0 ) noexcept( false )
329+
{
330+
return data_and_size( ( in.size( offset + 1 ) >= ( offset + 1 ) ) ? ( in.current( offset )->*P )() : nullptr );
331+
}
332+
};
333+
334+
#endif
335+
162336
template< auto M >
163337
struct peek_member
164338
: peek_member_impl< decltype( M ), M >

include/tao/pegtl/parse_error.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#ifndef TAO_PEGTL_PARSE_ERROR_HPP
66
#define TAO_PEGTL_PARSE_ERROR_HPP
77

8+
#if !defined( __cpp_exceptions )
9+
#error "Exception support required for tao/pegtl/parse_error.hpp"
10+
#endif
11+
812
#include <exception>
913
#include <string>
1014
#include <type_traits>
1115
#include <utility>
1216

13-
#if !defined( __cpp_exceptions )
14-
#error "Exception support required for tao/pegtl/parse_error.hpp"
15-
#endif
16-
1717
#include "config.hpp"
1818
#include "parse_error_base.hpp"
1919

include/tao/pegtl/parse_error_base.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#ifndef TAO_PEGTL_PARSE_ERROR_BASE_HPP
66
#define TAO_PEGTL_PARSE_ERROR_BASE_HPP
77

8+
#if !defined( __cpp_exceptions )
9+
#error "Exception support required for tao/pegtl/parse_error_base.hpp"
10+
#endif
11+
812
#include <cstddef>
913
#include <stdexcept>
1014
#include <string>
1115
#include <string_view>
1216

13-
#if !defined( __cpp_exceptions )
14-
#error "Exception support required for tao/pegtl/parse_error_base.hpp"
15-
#endif
16-
1717
#include "config.hpp"
1818

1919
namespace TAO_PEGTL_NAMESPACE

include/tao/pegtl/parse_nested.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#ifndef TAO_PEGTL_PARSE_NESTED_HPP
66
#define TAO_PEGTL_PARSE_NESTED_HPP
77

8-
#include <type_traits>
8+
#if !defined( __cpp_exceptions )
9+
#error "Exception support required for tao/pegtl/parse_error.hpp"
10+
#endif
911

10-
#if defined( __cpp_exceptions )
1112
#include <exception>
1213
#include <stdexcept>
13-
#endif
14+
#include <type_traits>
1415

1516
#include "apply_mode.hpp"
1617
#include "config.hpp"

src/test/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ set(test_sources
3434
ascii_string.cpp
3535
ascii_three.cpp
3636
ascii_two.cpp
37+
base_empty.cpp
38+
base_exceptions.cpp
39+
base_result.cpp
40+
base_setup.cpp
3741
contrib_alphabet.cpp
3842
contrib_charconv.cpp
3943
contrib_check_consume.cpp
@@ -218,9 +222,6 @@ set(test_sources
218222
stream_rule_require.cpp
219223
stream_stream_input_base.cpp
220224
stream_text_stream_input.cpp
221-
test_empty.cpp
222-
test_result.cpp
223-
test_setup.cpp
224225
text_position.cpp
225226
type_list.cpp
226227
uint16_general.cpp

0 commit comments

Comments
 (0)