Skip to content

Commit 84b94c3

Browse files
committed
New
1 parent d2b363f commit 84b94c3

File tree

10 files changed

+485
-0
lines changed

10 files changed

+485
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2014-2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_COUNTER_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_COUNTER_HPP
6+
7+
#include <cassert>
8+
#include <utility>
9+
10+
#include "../config.hpp"
11+
#include "../normal.hpp"
12+
13+
#include "../internal/demangle.hpp"
14+
15+
namespace tao
16+
{
17+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
18+
{
19+
struct counter_data
20+
{
21+
unsigned start = 0;
22+
unsigned success = 0;
23+
unsigned failure = 0;
24+
};
25+
26+
struct counter_state
27+
{
28+
std::map< std::string, counter_data > counts;
29+
};
30+
31+
template< typename Rule >
32+
struct counter
33+
: normal< Rule >
34+
{
35+
template< typename Input >
36+
static void start( const Input&, counter_state& ts )
37+
{
38+
++ts.counts[ internal::demangle< Rule >() ].start;
39+
}
40+
41+
template< typename Input >
42+
static void success( const Input&, counter_state& ts )
43+
{
44+
++ts.counts[ internal::demangle< Rule >() ].success;
45+
}
46+
47+
template< typename Input >
48+
static void failure( const Input&, counter_state& ts )
49+
{
50+
++ts.counts[ internal::demangle< Rule >() ].failure;
51+
}
52+
};
53+
54+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
55+
56+
} // namespace tao
57+
58+
#endif
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) 2014-2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_TRACER_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_CONTRIB_TRACER_HPP
6+
7+
#include <cassert>
8+
#include <iomanip>
9+
#include <iostream>
10+
#include <utility>
11+
#include <vector>
12+
13+
#include "../config.hpp"
14+
#include "../normal.hpp"
15+
16+
#include "../internal/demangle.hpp"
17+
18+
namespace tao
19+
{
20+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
21+
{
22+
struct trace_state
23+
{
24+
unsigned rule = 0;
25+
unsigned line = 0;
26+
std::vector< unsigned > stack;
27+
};
28+
29+
template< typename Rule >
30+
struct tracer
31+
: normal< Rule >
32+
{
33+
template< typename Input, typename... States >
34+
static void start( const Input& in, States&&... )
35+
{
36+
std::cerr << in.position() << " start " << internal::demangle< Rule >() << std::endl;
37+
}
38+
39+
template< typename Input >
40+
static void start( const Input& in, trace_state& ts )
41+
{
42+
std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ++ts.rule << " " << in.position() << " start " << internal::demangle< Rule >() << std::endl;
43+
ts.stack.push_back( ts.rule );
44+
}
45+
46+
template< typename Input, typename... States >
47+
static void success( const Input& in, States&&... )
48+
{
49+
std::cerr << in.position() << " success " << internal::demangle< Rule >() << std::endl;
50+
}
51+
52+
template< typename Input >
53+
static void success( const Input& in, trace_state& ts )
54+
{
55+
assert( !ts.stack.empty() );
56+
std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ts.stack.back() << " " << in.position() << " success " << internal::demangle< Rule >() << std::endl;
57+
ts.stack.pop_back();
58+
}
59+
60+
template< typename Input, typename... States >
61+
static void failure( const Input& in, States&&... )
62+
{
63+
std::cerr << in.position() << " failure " << internal::demangle< Rule >() << std::endl;
64+
}
65+
66+
template< typename Input >
67+
static void failure( const Input& in, trace_state& ts )
68+
{
69+
assert( !ts.stack.empty() );
70+
std::cerr << std::setw( 6 ) << ++ts.line << " " << std::setw( 6 ) << ts.stack.back() << " " << in.position() << " failure " << internal::demangle< Rule >() << std::endl;
71+
ts.stack.pop_back();
72+
}
73+
74+
template< template< typename... > class Action, typename Input, typename... States >
75+
static void apply0( const Input&, States&&... st )
76+
{
77+
std::cerr << "apply0 " << internal::demangle< Action< Rule > >() << std::endl;
78+
Action< Rule >::apply0( st... );
79+
}
80+
81+
template< template< typename... > class Action, typename Input >
82+
static void apply0( const Input&, trace_state& ts )
83+
{
84+
std::cerr << std::setw( 6 ) << ++ts.line << " " << internal::demangle< Action< Rule > >() << "::apply0()" << std::endl;
85+
Action< Rule >::apply0( ts );
86+
}
87+
88+
template< template< typename... > class Action, typename Iterator, typename Input, typename... States >
89+
static void apply( const Iterator& begin, const Input& in, States&&... st )
90+
{
91+
std::cerr << "apply " << internal::demangle< Action< Rule > >() << std::endl;
92+
using action_t = typename Input::action_t;
93+
const action_t action_input( begin, in );
94+
Action< Rule >::apply( action_input, st... );
95+
}
96+
97+
template< template< typename... > class Action, typename Iterator, typename Input >
98+
static void apply( const Iterator& begin, const Input& in, trace_state& ts )
99+
{
100+
std::cerr << std::setw( 6 ) << ++ts.line << " " << internal::demangle< Action< Rule > >() << "::apply()" << std::endl;
101+
using action_t = typename Input::action_t;
102+
const action_t action_input( begin, in );
103+
Action< Rule >::apply( action_input, ts );
104+
}
105+
};
106+
107+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
108+
109+
} // namespace tao
110+
111+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_CSTREAM_INPUT_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_CSTREAM_INPUT_HPP
6+
7+
#include <cstdio>
8+
9+
#include "buffer_input.hpp"
10+
#include "config.hpp"
11+
#include "eol.hpp"
12+
13+
#include "internal/cstream_reader.hpp"
14+
15+
namespace tao
16+
{
17+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
18+
{
19+
template< typename Eol = eol::lf_crlf >
20+
struct cstream_input
21+
: buffer_input< internal::cstream_reader, Eol >
22+
{
23+
template< typename T >
24+
cstream_input( std::FILE* in_stream, const std::size_t in_maximum, T&& in_source )
25+
: buffer_input< internal::cstream_reader, Eol >( std::forward< T >( in_source ), in_maximum, in_stream )
26+
{
27+
}
28+
};
29+
30+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
31+
32+
} // namespace tao
33+
34+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2016-2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CR_CRLF_EOL_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CR_CRLF_EOL_HPP
6+
7+
#include "../config.hpp"
8+
9+
namespace tao
10+
{
11+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
12+
{
13+
namespace internal
14+
{
15+
struct cr_crlf_eol
16+
{
17+
static constexpr int ch = '\r';
18+
19+
template< typename Input >
20+
static eol_pair match( Input& in )
21+
{
22+
eol_pair p = { false, in.size( 2 ) };
23+
if( p.second ) {
24+
if( in.peek_char() == '\r' ) {
25+
in.bump_to_next_line( 1 + ( ( p.second > 1 ) && ( in.peek_char( 1 ) == '\n' ) ) );
26+
p.first = true;
27+
}
28+
}
29+
return p;
30+
}
31+
};
32+
33+
} // namespace internal
34+
35+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
36+
37+
} // namespace tao
38+
39+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2016-2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CR_EOL_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CR_EOL_HPP
6+
7+
#include "../config.hpp"
8+
9+
namespace tao
10+
{
11+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
12+
{
13+
namespace internal
14+
{
15+
struct cr_eol
16+
{
17+
static constexpr int ch = '\r';
18+
19+
template< typename Input >
20+
static eol_pair match( Input& in )
21+
{
22+
eol_pair p = { false, in.size( 1 ) };
23+
if( p.second ) {
24+
if( in.peek_char() == '\r' ) {
25+
in.bump_to_next_line();
26+
p.first = true;
27+
}
28+
}
29+
return p;
30+
}
31+
};
32+
33+
} // namespace internal
34+
35+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
36+
37+
} // namespace tao
38+
39+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2016-2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CRLF_EOL_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_CRLF_EOL_HPP
6+
7+
#include "../config.hpp"
8+
9+
namespace tao
10+
{
11+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
12+
{
13+
namespace internal
14+
{
15+
struct crlf_eol
16+
{
17+
static constexpr int ch = '\n';
18+
19+
template< typename Input >
20+
static eol_pair match( Input& in )
21+
{
22+
eol_pair p = { false, in.size( 2 ) };
23+
if( p.second > 1 ) {
24+
if( ( in.peek_char() == '\r' ) && ( in.peek_char( 1 ) == '\n' ) ) {
25+
in.bump_to_next_line( 2 );
26+
p.first = true;
27+
}
28+
}
29+
return p;
30+
}
31+
};
32+
33+
} // namespace internal
34+
35+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
36+
37+
} // namespace tao
38+
39+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3+
4+
#ifndef TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_DEMANGLE_SANITISE_HPP
5+
#define TAOCPP_JSON_PEGTL_INCLUDE_INTERNAL_DEMANGLE_SANITISE_HPP
6+
7+
#include <string>
8+
9+
#include "../config.hpp"
10+
11+
namespace tao
12+
{
13+
namespace TAOCPP_JSON_PEGTL_NAMESPACE
14+
{
15+
namespace internal
16+
{
17+
inline void demangle_sanitise_chars( std::string& s )
18+
{
19+
std::string::size_type p;
20+
while( ( p = s.find( "(char)" ) ) != std::string::npos ) {
21+
int c = 0;
22+
std::string::size_type q;
23+
for( q = p + 6; ( q < s.size() ) && ( s[ q ] >= '0' ) && ( s[ q ] <= '9' ); ++q ) {
24+
c *= 10;
25+
c += s[ q ] - '0';
26+
}
27+
if( c == '\'' ) {
28+
s.replace( p, q - p, "'\\''" );
29+
}
30+
else if( c == '\\' ) {
31+
s.replace( p, q - p, "'\\\\'" );
32+
}
33+
else if( ( c < 32 ) || ( c > 126 ) ) {
34+
s.replace( p, 6, std::string() );
35+
}
36+
else {
37+
s.replace( p, q - p, std::string( 1, '\'' ) + char( c ) + '\'' );
38+
}
39+
}
40+
}
41+
42+
} // namespace internal
43+
44+
} // namespace TAOCPP_JSON_PEGTL_NAMESPACE
45+
46+
} // namespace tao
47+
48+
#endif

0 commit comments

Comments
 (0)