-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathjson_stream.cpp
More file actions
48 lines (40 loc) · 1.33 KB
/
json_stream.cpp
File metadata and controls
48 lines (40 loc) · 1.33 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
// Copyright (c) 2014-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 <iomanip>
#include <iostream>
#include <tao/pegtl.hpp>
#include <tao/pegtl/example/json.hpp>
#include <tao/pegtl/stream.hpp>
#include "json_errors.hpp"
namespace pegtl = TAO_PEGTL_NAMESPACE;
namespace example
{
using grammar = pegtl::seq< pegtl::json::text, pegtl::eof >;
} // namespace example
int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
{
if( argc != 2 ) {
std::cerr << "Usage: " << argv[ 0 ] << " JSON\n"
<< "Parse a JSON text.\n\n"
<< "Example: " << argv[ 0 ] << " '{\"foo\":[42,null]}'" << std::endl;
return 1;
}
using input_t = pegtl::array_cstring_auto_input< pegtl::scan::lf_crlf >;
input_t in( argv[ 1 ] );
#if defined( __cpp_exceptions )
try {
pegtl::parse< example::grammar, pegtl::nothing, example::control >( in );
}
catch( const decltype( in )::parse_error_t& e ) {
std::cerr << e.what() << std::endl;
return 1;
}
#else
if( !pegtl::parse< example::grammar, pegtl::nothing, example::control >( in ) ) {
std::cerr << "error occurred" << std::endl;
return 1;
}
#endif
return 0;
}