Skip to content

Commit baf3bdd

Browse files
committed
Port to Debian 10
The port was complicated by changes to boost::spirit which I was unable to pinpoint. json5_parser_reader_template.h: new_identifier() now trims trailing whitespace no longer trimmed by boost::spirit parser. json5_parser_reader_test.cpp: test_reading(): reformatted and added test case. check_reading_arrays(): added "unget()" to accommodate change to boost::spirit. json5_parser_value_test.cpp: tweaked to accommodate change to boost::assign::list_of(). json_demo.cpp, json_map_demo.cpp, json_headers_only_demo.cpp: Added "sleep()" call intended to prevent unreproducible "read" failures. Added print statements to test source files. wscript: Replaced "-Ofast" with "-O3".
1 parent 98338b2 commit baf3bdd

11 files changed

Lines changed: 129 additions & 63 deletions

json5_parser/json5_parser_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ void read_or_throw(std::wistream& is, wmValue& value);
6464
void read_or_throw(std::wstring::const_iterator& begin,
6565
std::wstring::const_iterator end, wmValue& value);
6666
#endif
67-
}
67+
} // namespace json5_parser
6868

6969
#endif

json5_parser/json5_parser_reader_template.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#pragma once
1111
#endif
1212

13+
#include <boost/algorithm/string.hpp>
14+
#include <string>
15+
1316
#include "json5_parser_error_position.h"
1417
#include "json5_parser_value.h"
1518

@@ -248,7 +251,7 @@ class Semantic_actions {
248251

249252
String_type result;
250253
result.append(begin, end);
251-
name_ = result;
254+
name_ = boost::algorithm::trim_right_copy(result);
252255
}
253256

254257
void new_str(Iter_type begin, Iter_type end) {
@@ -359,15 +362,15 @@ void throw_error(Iter_type, const std::string& reason) {
359362
throw reason;
360363
}
361364

362-
// the spirit grammer
365+
// the spirit grammar
363366
//
364367
template <class Value_type, class Iter_type>
365-
class Json_grammer
366-
: public spirit_namespace::grammar<Json_grammer<Value_type, Iter_type> > {
368+
class Json_grammar
369+
: public spirit_namespace::grammar<Json_grammar<Value_type, Iter_type> > {
367370
public:
368371
typedef Semantic_actions<Value_type, Iter_type> Semantic_actions_t;
369372

370-
Json_grammer(Semantic_actions_t& semantic_actions) : actions_(semantic_actions) {}
373+
Json_grammar(Semantic_actions_t& semantic_actions) : actions_(semantic_actions) {}
371374

372375
static void throw_not_value(Iter_type begin, Iter_type) {
373376
throw_error(begin, "not a value");
@@ -400,7 +403,7 @@ class Json_grammer
400403
template <typename ScannerT>
401404
class definition {
402405
public:
403-
definition(const Json_grammer& self) {
406+
definition(const Json_grammar& self) {
404407
using namespace spirit_namespace;
405408

406409
typedef typename Value_type::String_type::value_type Char_type;
@@ -445,7 +448,7 @@ class Json_grammer
445448
Uint64_action new_uint64(
446449
boost::bind(&Semantic_actions_t::new_uint64, &self.actions_, _1));
447450

448-
// actual grammer
451+
// actual grammar
449452

450453
json_ = value_ | eps_p[&throw_not_value];
451454

@@ -472,12 +475,12 @@ class Json_grammer
472475
double_quoted_string_ =
473476
lexeme_d // this causes white space and what would appear to be
474477
// comments inside a string to be retained
475-
[confix_p('"', *lex_escape_ch_p, '"')];
478+
[confix_p('"', *lex_escape_ch_p, '"')];
476479

477480
single_quoted_string_ =
478481
lexeme_d // this causes white space and what would appear to be
479482
// comments inside a string to be retained
480-
[confix_p('\'', *lex_escape_ch_p, '\'')];
483+
[confix_p('\'', *lex_escape_ch_p, '\'')];
481484

482485
identifier_ = (alpha_p | ch_p('_') | ch_p('$')) >>
483486
*(alnum_p | ch_p('_') | ch_p('$'));
@@ -494,7 +497,7 @@ class Json_grammer
494497
};
495498

496499
private:
497-
Json_grammer& operator=(const Json_grammer&); // to prevent "assignment operator
500+
Json_grammar& operator=(const Json_grammar&); // to prevent "assignment operator
498501
// could not be generated" warning
499502

500503
Semantic_actions_t& actions_;
@@ -543,7 +546,7 @@ Iter_type read_range_or_throw(Iter_type begin, Iter_type end, Value_type& value)
543546
Semantic_actions<Value_type, Iter_type> semantic_actions(value);
544547

545548
const spirit_namespace::parse_info<Iter_type> info = spirit_namespace::parse(
546-
begin, end, Json_grammer<Value_type, Iter_type>(semantic_actions),
549+
begin, end, Json_grammar<Value_type, Iter_type>(semantic_actions),
547550
spirit_namespace::space_p | spirit_namespace::comment_p("//") |
548551
spirit_namespace::comment_p("/*", "*/"));
549552

json5_parser/json5_parser_value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Null {};
4646
template <class Config> // Config determines whether the value uses std::string or
4747
// std::wstring and whether JSON Objects are represented as
4848
// vectors or maps
49-
class Value_impl {
49+
class Value_impl {
5050
public:
5151
typedef Config Config_type;
5252
typedef typename Config::String_type String_type;

json5_parser/json5_parser_writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ void write_formatted(const wmValue& value, std::wostream& os,
7373
std::wstring write_formatted(const wmValue& value,
7474
unsigned int precision_of_doubles = 0);
7575
#endif
76-
}
76+
} // namespace json5_parser
7777

7878
#endif

json_demo/json_demo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cassert>
1010
#include <fstream>
11+
1112
#include "json5_parser.h"
1213

1314
#ifndef JSON_SPIRIT_VALUE_ENABLED
@@ -103,6 +104,7 @@ vector<Address> read_addrs(const char* file_name) {
103104
}
104105

105106
int main() {
107+
106108
const Address addrs[5] = {{42, "East Street", "Newtown", "Essex", "England"},
107109
{1, "West Street", "Hull", "Yorkshire", "England"},
108110
{12, "South Road", "Aberystwyth", "Dyfed", "Wales"},
@@ -121,5 +123,6 @@ int main() {
121123
assert(new_addrs[i] == addrs[i]);
122124
}
123125

126+
std::cout << "json_demo tests all passed" << std::endl;
124127
return 0;
125128
}

json_headers_only_demo/json_headers_only_demo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cassert>
1111
#include <fstream>
12+
1213
#include "json5_parser_reader_template.h"
1314
#include "json5_parser_writer_template.h"
1415

@@ -112,7 +113,9 @@ int main() {
112113
{45, "North Road", "Paignton", "Devon", "England"},
113114
{78, "Upper Street", "Ware", "Hertfordshire", "England"}};
114115

115-
const char* file_name("demo.txt");
116+
117+
// Changed from "demo.txt" because tests were failing. NSF weirdness?
118+
const char* file_name("/tmp/json5_demo.txt");
116119

117120
write_addrs(file_name, addrs);
118121

@@ -124,5 +127,6 @@ int main() {
124127
assert(new_addrs[i] == addrs[i]);
125128
}
126129

130+
std::cout << "json_headers_only_demo tests all passed" << std::endl;
127131
return 0;
128132
}

json_map_demo/json_map_demo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cassert>
1010
#include <fstream>
11+
1112
#include "json5_parser.h"
1213

1314
#ifndef JSON_SPIRIT_MVALUE_ENABLED
@@ -103,7 +104,7 @@ int main() {
103104
{45, "North Road", "Paignton", "Devon", "England"},
104105
{78, "Upper Street", "Ware", "Hertfordshire", "England"}};
105106

106-
const char* file_name("demo.txt");
107+
const char* file_name("map_demo.txt");
107108

108109
write_addrs(file_name, addrs);
109110

@@ -115,5 +116,6 @@ int main() {
115116
assert(new_addrs[i] == addrs[i]);
116117
}
117118

119+
std::cout << "json_map_demo tests all passed" << std::endl;
118120
return 0;
119-
};
121+
};

json_test/json5_parser_reader_test.cpp

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -203,36 +203,52 @@ struct Test_runner {
203203
check_eq(value.get_obj(), obj);
204204
}
205205

206-
Object_type obj;
207-
Value_type value;
206+
{
207+
Object_type obj;
208+
Value_type value;
208209

209-
read_cstr(
210-
"{\n"
211-
" \"name 1\" : \"value 1\"\n"
212-
"}",
213-
value);
210+
read_cstr(
211+
"{\n"
212+
" \"name 1\" : \"value 1\"\n"
213+
"}",
214+
value);
214215

215-
add_c_str(obj, "name 1", "value 1");
216+
add_c_str(obj, "name 1", "value 1");
216217

217-
check_eq(value.get_obj(), obj);
218+
check_eq(value.get_obj(), obj);
218219

219-
read_cstr("{\"name 1\":\"value 1\",\"name 2\":\"value 2\"}", value);
220+
read_cstr("{\"name 1\":\"value 1\",\"name 2\":\"value 2\"}", value);
220221

221-
add_c_str(obj, "name 2", "value 2");
222+
add_c_str(obj, "name 2", "value 2");
222223

223-
check_eq(value.get_obj(), obj);
224+
check_eq(value.get_obj(), obj);
224225

225-
read_cstr(
226-
"{\n"
227-
" \"name 1\" : \"value 1\",\n"
228-
" \"name 2\" : \"value 2\",\n"
229-
" \"name 3\" : \"value 3\"\n"
230-
"}",
231-
value);
226+
read_cstr(
227+
"{\n"
228+
" \"name 1\" : \"value 1\",\n"
229+
" \"name 2\" : \"value 2\",\n"
230+
" \"name 3\" : \"value 3\"\n"
231+
"}",
232+
value);
233+
234+
add_c_str(obj, "name 3", "value 3");
235+
236+
check_eq(value.get_obj(), obj);
237+
}
238+
239+
{
240+
Object_type obj;
241+
Value_type value;
232242

233-
add_c_str(obj, "name 3", "value 3");
243+
read_cstr(
244+
"{\n"
245+
" name 1 : \"value 1\"\n"
246+
"}",
247+
value);
234248

235-
check_eq(value.get_obj(), obj);
249+
add_c_str(obj, "name 1", "value 1");
250+
check_eq(value.get_obj(), obj);
251+
}
236252

237253
check_reading(
238254
"{\n"
@@ -715,8 +731,11 @@ struct Test_runner {
715731
Istringstream_type is(str);
716732

717733
check_reading_array(is, 0);
734+
is.unget();
718735
check_reading_array(is, 1);
736+
is.unget();
719737
check_reading_array(is, 2);
738+
is.unget();
720739
check_reading_array(is, 3);
721740
}
722741

@@ -729,12 +748,9 @@ struct Test_runner {
729748
check_value_sequence(" 10 11 12", list_of(10)(11)(12), true);
730749
check_value_sequence("10 11 12", list_of(10)(11)(12), true);
731750

732-
//
733-
751+
// Note: check_reading_arrays() expects exactly this list of arrays.
734752
check_reading_arrays("[] [ 1 ] [ 1, 2 ] [ 1, 2, 3 ]");
735-
// check_reading_arrays( "[][1][1,2][1,2,3]" ); // fails due to
736-
// multi_pass iterator bug,
737-
// use stream_reader class instead
753+
check_reading_arrays("[][1][1,2][1,2,3]");
738754
}
739755

740756
void test_uint64(const char* value_str, int expected_int, int64_t expected_int64,
@@ -801,16 +817,27 @@ struct Test_runner {
801817
}
802818

803819
void run_tests() {
820+
std::cout << " before test_syntax()" << std::endl;
804821
test_syntax();
822+
std::cout << " before test_reading()" << std::endl;
805823
test_reading();
824+
std::cout << " before test_reading_reals()" << std::endl;
806825
test_reading_reals();
826+
std::cout << " before test_from_stream()" << std::endl;
807827
test_from_stream();
828+
std::cout << " before test_escape_chars()" << std::endl;
808829
test_escape_chars();
830+
std::cout << " before test_values()" << std::endl;
809831
test_values();
832+
std::cout << " before test_error_cases()" << std::endl;
810833
test_error_cases();
834+
std::cout << " before test_sequence_of_values()" << std::endl;
811835
test_sequence_of_values();
836+
std::cout << " before test_uint64()" << std::endl;
812837
test_uint64();
838+
std::cout << " before test_types()" << std::endl;
813839
test_types();
840+
std::cout << " before test_comments()" << std::endl;
814841
test_comments();
815842
}
816843
};
@@ -848,17 +875,25 @@ void test_extended_ascii() {
848875

849876
void json5_parser::test_reader() {
850877
#ifdef JSON_SPIRIT_VALUE_ENABLED
878+
std::cout << " JSON_SPIRIT_VALUE_ENABLED" << std::endl;
851879
Test_runner<Config>().run_tests();
852880
test_extended_ascii();
853881
#endif
854882
#ifdef JSON_SPIRIT_MVALUE_ENABLED
883+
std::cout << " JSON_SPIRIT_MVALUE_ENABLED" << std::endl;
855884
Test_runner<mConfig>().run_tests();
856885
#endif
857886
#if defined(JSON_SPIRIT_WVALUE_ENABLED) && !defined(BOOST_NO_STD_WSTRING)
887+
std::cout << " defined(JSON_SPIRIT_WVALUE_ENABLED) && "
888+
"!defined(BOOST_NO_STD_WSTRING)"
889+
<< std::endl;
858890
Test_runner<wConfig>().run_tests();
859891
test_wide_esc_u();
860892
#endif
861893
#if defined(JSON_SPIRIT_WMVALUE_ENABLED) && !defined(BOOST_NO_STD_WSTRING)
894+
std::cout << " defined(JSON_SPIRIT_WMVALUE_ENABLED) && "
895+
"!defined(BOOST_NO_STD_WSTRING)"
896+
<< std::endl;
862897
Test_runner<wmConfig>().run_tests();
863898
#endif
864899

@@ -880,7 +915,7 @@ void json5_parser::test_reader() {
880915

881916
// cout << t.elapsed() << endl;
882917

883-
// const string so = write( value );
918+
// const string so = write( value );
884919

885920
// Object obj;
886921

0 commit comments

Comments
 (0)