diff --git a/json5_parser/json5_parser_reader_template.h b/json5_parser/json5_parser_reader_template.h index 557035b..b52b011 100644 --- a/json5_parser/json5_parser_reader_template.h +++ b/json5_parser/json5_parser_reader_template.h @@ -13,9 +13,12 @@ #include "json5_parser_value.h" #include "json5_parser_error_position.h" +// emscripten threads are complicated to use and experimental +#ifndef __EMSCRIPTEN__ #define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread +#endif -#include +#include #include #include @@ -37,6 +40,7 @@ namespace json5_parser { + using namespace boost::placeholders; const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >(); const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >(); @@ -312,6 +316,13 @@ namespace json5_parser add_to_current( -std::numeric_limits::infinity() ); } + + void new_nan( Iter_type begin, Iter_type end ) + { + assert( is_eq( begin, end, "NaN" ) ); + + add_to_current( -std::numeric_limits::quiet_NaN() ); + } private: @@ -474,6 +485,7 @@ namespace json5_parser Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) ); Str_action new_infinity ( boost::bind( &Semantic_actions_t::new_infinity, &self.actions_, _1, _2 ) ); Str_action new_minus_infinity ( boost::bind( &Semantic_actions_t::new_minus_infinity, &self.actions_, _1, _2 ) ); + Str_action new_nan ( boost::bind( &Semantic_actions_t::new_nan, &self.actions_, _1, _2 ) ); Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) ); Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) ); @@ -494,6 +506,7 @@ namespace json5_parser | str_p( "null" ) [ new_null ] | (!ch_p('+') >> str_p( "Infinity" ) [ new_infinity ]) | str_p( "-Infinity" ) [ new_minus_infinity ] + | str_p( "NaN" ) [ new_nan ] ; object_ @@ -507,7 +520,7 @@ namespace json5_parser ; pair_ - = (double_quoted_string_[ new_name ] | identifier_[ new_identifier ]) + = (single_quoted_string_[ new_name ] | double_quoted_string_[ new_name ] | identifier_[ new_identifier ]) >> ( ':' | eps_p[ &throw_not_colon ] ) >> ( value_ | eps_p[ &throw_not_value ] ) ; diff --git a/json5_parser/json5_parser_writer_template.h b/json5_parser/json5_parser_writer_template.h index b9ab62e..37d8e66 100644 --- a/json5_parser/json5_parser_writer_template.h +++ b/json5_parser/json5_parser_writer_template.h @@ -190,7 +190,12 @@ namespace json5_parser void output( double d ) { + if (std::isfinite(d)) os_ << std::setprecision( precision_of_doubles_ ) << d; + else if (std::isnan(d)) + os_<<"NaN"; + else // is infinite + os_<<(d<0?"-":"")<<"Infinity"; } static bool contains_composite_elements( const Array_type& arr )