Repository navigation
Expand file tree
/
Copy pathjson_count.cpp
More file actions
71 lines (58 loc) · 1.98 KB
/
Copy pathjson_count.cpp
File metadata and controls
71 lines (58 loc) · 1.98 KB
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
// Copyright (c) 2017-2026 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)
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <map>
#include <string_view>
#include <tao/pegtl.hpp>
#include <tao/pegtl/example/json.hpp>
namespace pegtl = TAO_PEGTL_NAMESPACE;
namespace example
{
struct counter_data
{
std::size_t start = 0;
std::size_t success = 0;
std::size_t failure = 0;
};
struct counter_state
{
std::map< std::string_view, counter_data > counts;
};
template< typename Rule >
struct counter
: pegtl::normal< Rule >
{
template< typename Input >
static void start( const Input& /*unused*/, counter_state& ts )
{
++ts.counts[ pegtl::demangle< Rule >() ].start;
}
template< typename Input >
static void success( const Input& /*unused*/, counter_state& ts )
{
++ts.counts[ pegtl::demangle< Rule >() ].success;
}
template< typename Input >
static void failure( const Input& /*unused*/, counter_state& ts )
{
++ts.counts[ pegtl::demangle< Rule >() ].failure;
}
};
using grammar = pegtl::seq< pegtl::json::text, pegtl::eof >;
} // namespace example
int main( int argc, char** argv )
{
example::counter_state cs;
for( int i = 1; i < argc; ++i ) {
pegtl::text_file_input< pegtl::scan::lf_crlf > in( argv[ i ] );
pegtl::parse< example::grammar, pegtl::nothing, example::counter >( in, cs );
}
std::cout << std::right << std::setw( 72 ) << "RULE NAME" << std::left << " START SUCCESS FAILURE" << std::endl;
for( const auto& j : cs.counts ) {
std::cout << std::right << std::setw( 72 ) << j.first << " " << std::setw( 8 ) << j.second.start << " " << std::setw( 8 ) << j.second.success << " " << std::setw( 8 ) << j.second.failure << std::endl;
}
return 0;
}