Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
nlohmann::detail::parser< BasicJsonType > Class Template Reference

syntax analysis More...

#include <json.hpp>

Public Types

enum  parse_event_t : uint8_t {
  parse_event_t::object_start, parse_event_t::object_end, parse_event_t::array_start, parse_event_t::array_end,
  parse_event_t::key, parse_event_t::value
}
 
using parser_callback_t = std::function< bool(int depth, parse_event_t event, BasicJsonType &parsed)>
 

Public Member Functions

 parser (detail::input_adapter_t &&adapter, const parser_callback_t cb=nullptr, const bool allow_exceptions_=true)
 a parser reading from an input adapter More...
 
void parse (const bool strict, BasicJsonType &result)
 public parser interface More...
 
bool accept (const bool strict=true)
 public accept interface More...
 
template<typename SAX >
bool sax_parse (SAX *sax, const bool strict=true)
 

Private Types

using number_integer_t = typename BasicJsonType::number_integer_t
 
using number_unsigned_t = typename BasicJsonType::number_unsigned_t
 
using number_float_t = typename BasicJsonType::number_float_t
 
using string_t = typename BasicJsonType::string_t
 
using lexer_t = lexer< BasicJsonType >
 
using token_type = typename lexer_t::token_type
 

Private Member Functions

template<typename SAX >
bool sax_parse_internal (SAX *sax)
 
token_type get_token ()
 get next token from lexer More...
 
std::string exception_message (const token_type expected, const std::string &context)
 

Private Attributes

const parser_callback_t callback = nullptr
 callback function More...
 
token_type last_token = token_type::uninitialized
 the type of the last read token More...
 
lexer_t m_lexer
 the lexer More...
 
const bool allow_exceptions = true
 whether to throw exceptions in case of errors More...
 

Detailed Description

template<typename BasicJsonType>
class nlohmann::detail::parser< BasicJsonType >

syntax analysis

This class implements a recursive decent parser.

Definition at line 4839 of file json.hpp.

Member Typedef Documentation

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::lexer_t = lexer<BasicJsonType>
private

Definition at line 4845 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::number_float_t = typename BasicJsonType::number_float_t
private

Definition at line 4843 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::number_integer_t = typename BasicJsonType::number_integer_t
private

Definition at line 4841 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::number_unsigned_t = typename BasicJsonType::number_unsigned_t
private

Definition at line 4842 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::parser_callback_t = std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>

Definition at line 4866 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::string_t = typename BasicJsonType::string_t
private

Definition at line 4844 of file json.hpp.

template<typename BasicJsonType >
using nlohmann::detail::parser< BasicJsonType >::token_type = typename lexer_t::token_type
private

Definition at line 4846 of file json.hpp.

Member Enumeration Documentation

template<typename BasicJsonType >
enum nlohmann::detail::parser::parse_event_t : uint8_t
strong
Enumerator
object_start 

the parser read { and started to process a JSON object

object_end 

the parser read } and finished processing a JSON object

array_start 

the parser read [ and started to process a JSON array

array_end 

the parser read ] and finished processing a JSON array

key 

the parser read a key of a value in an object

value 

the parser finished reading a JSON value

Definition at line 4849 of file json.hpp.

4849  : uint8_t
4850  {
4852  object_start,
4854  object_end,
4856  array_start,
4858  array_end,
4860  key,
4862  value
4863  };
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177

Constructor & Destructor Documentation

template<typename BasicJsonType >
nlohmann::detail::parser< BasicJsonType >::parser ( detail::input_adapter_t &&  adapter,
const parser_callback_t  cb = nullptr,
const bool  allow_exceptions_ = true 
)
inlineexplicit

a parser reading from an input adapter

Definition at line 4869 of file json.hpp.

4872  : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
4873  {
4874  // read first token
4875  get_token();
4876  }
const parser_callback_t callback
callback function
Definition: json.hpp:5302
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
token_type get_token()
get next token from lexer
Definition: json.hpp:5266
const bool allow_exceptions
whether to throw exceptions in case of errors
Definition: json.hpp:5308

Member Function Documentation

template<typename BasicJsonType >
bool nlohmann::detail::parser< BasicJsonType >::accept ( const bool  strict = true)
inline

public accept interface

Parameters
[in]strictwhether to expect the last token to be EOF
Returns
whether the input is a proper JSON text

Definition at line 4949 of file json.hpp.

References nlohmann::detail::strict.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::accept().

4950  {
4951  json_sax_acceptor<BasicJsonType> sax_acceptor;
4952  return sax_parse(&sax_acceptor, strict);
4953  }
throw a type_error exception in case of invalid UTF-8
bool sax_parse(SAX *sax, const bool strict=true)
Definition: json.hpp:4956
template<typename BasicJsonType >
std::string nlohmann::detail::parser< BasicJsonType >::exception_message ( const token_type  expected,
const std::string context 
)
inlineprivate

Definition at line 5271 of file json.hpp.

References nlohmann::detail::string.

5272  {
5273  std::string error_msg = "syntax error ";
5274 
5275  if (not context.empty())
5276  {
5277  error_msg += "while parsing " + context + " ";
5278  }
5279 
5280  error_msg += "- ";
5281 
5282  if (last_token == token_type::parse_error)
5283  {
5284  error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
5285  m_lexer.get_token_string() + "'";
5286  }
5287  else
5288  {
5289  error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
5290  }
5291 
5292  if (expected != token_type::uninitialized)
5293  {
5294  error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
5295  }
5296 
5297  return error_msg;
5298  }
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
constexpr const char * get_error_message() const noexcept
return syntax error message
Definition: json.hpp:3823
token_type last_token
the type of the last read token
Definition: json.hpp:5304
static const char * token_type_name(const token_type t) noexcept
return name of values of type token_type (only used for errors)
Definition: json.hpp:2506
std::string get_token_string() const
Definition: json.hpp:3799
template<typename BasicJsonType >
token_type nlohmann::detail::parser< BasicJsonType >::get_token ( )
inlineprivate

get next token from lexer

Definition at line 5266 of file json.hpp.

5267  {
5268  return (last_token = m_lexer.scan());
5269  }
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
token_type scan()
Definition: json.hpp:3850
token_type last_token
the type of the last read token
Definition: json.hpp:5304
template<typename BasicJsonType >
void nlohmann::detail::parser< BasicJsonType >::parse ( const bool  strict,
BasicJsonType &  result 
)
inline

public parser interface

Parameters
[in]strictwhether to expect the last token to be EOF
[in,out]resultparsed JSON value
Exceptions
parse_error.101in case of an unexpected token
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails

Definition at line 4888 of file json.hpp.

References nlohmann::detail::parse_error::create(), nlohmann::detail::discarded, nlohmann::detail::json_sax_dom_parser< BasicJsonType >::is_errored(), nlohmann::detail::json_sax_dom_callback_parser< BasicJsonType >::is_errored(), nlohmann::detail::json_sax_dom_parser< BasicJsonType >::parse_error(), and nlohmann::detail::json_sax_dom_callback_parser< BasicJsonType >::parse_error().

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse().

4889  {
4890  if (callback)
4891  {
4892  json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
4893  sax_parse_internal(&sdp);
4894  result.assert_invariant();
4895 
4896  // in strict mode, input must be completely read
4897  if (strict and (get_token() != token_type::end_of_input))
4898  {
4899  sdp.parse_error(m_lexer.get_position(),
4902  exception_message(token_type::end_of_input, "value")));
4903  }
4904 
4905  // in case of an error, return discarded value
4906  if (sdp.is_errored())
4907  {
4908  result = value_t::discarded;
4909  return;
4910  }
4911 
4912  // set top-level value to null if it was discarded by the callback
4913  // function
4914  if (result.is_discarded())
4915  {
4916  result = nullptr;
4917  }
4918  }
4919  else
4920  {
4921  json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
4922  sax_parse_internal(&sdp);
4923  result.assert_invariant();
4924 
4925  // in strict mode, input must be completely read
4926  if (strict and (get_token() != token_type::end_of_input))
4927  {
4928  sdp.parse_error(m_lexer.get_position(),
4931  exception_message(token_type::end_of_input, "value")));
4932  }
4933 
4934  // in case of an error, return discarded value
4935  if (sdp.is_errored())
4936  {
4937  result = value_t::discarded;
4938  return;
4939  }
4940  }
4941  }
const parser_callback_t callback
callback function
Definition: json.hpp:5302
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
token_type get_token()
get next token from lexer
Definition: json.hpp:5266
const bool allow_exceptions
whether to throw exceptions in case of errors
Definition: json.hpp:5308
throw a type_error exception in case of invalid UTF-8
constexpr position_t get_position() const noexcept
return position of last read token
Definition: json.hpp:3791
bool sax_parse_internal(SAX *sax)
Definition: json.hpp:4975
std::string exception_message(const token_type expected, const std::string &context)
Definition: json.hpp:5271
std::string get_token_string() const
Definition: json.hpp:3799
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
discarded by the the parser callback function
template<typename BasicJsonType >
template<typename SAX >
bool nlohmann::detail::parser< BasicJsonType >::sax_parse ( SAX *  sax,
const bool  strict = true 
)
inline

Definition at line 4956 of file json.hpp.

References nlohmann::detail::parse_error::create(), nlohmann::detail::strict, and nlohmann::detail::void().

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::sax_parse().

4957  {
4958  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
4959  const bool result = sax_parse_internal(sax);
4960 
4961  // strict mode: next byte must be EOF
4962  if (result and strict and (get_token() != token_type::end_of_input))
4963  {
4964  return sax->parse_error(m_lexer.get_position(),
4967  exception_message(token_type::end_of_input, "value")));
4968  }
4969 
4970  return result;
4971  }
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
token_type get_token()
get next token from lexer
Definition: json.hpp:5266
throw a type_error exception in case of invalid UTF-8
constexpr position_t get_position() const noexcept
return position of last read token
Definition: json.hpp:3791
bool sax_parse_internal(SAX *sax)
Definition: json.hpp:4975
j template void())
Definition: json.hpp:1424
std::string exception_message(const token_type expected, const std::string &context)
Definition: json.hpp:5271
std::string get_token_string() const
Definition: json.hpp:3799
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
template<typename BasicJsonType >
template<typename SAX >
bool nlohmann::detail::parser< BasicJsonType >::sax_parse_internal ( SAX *  sax)
inlineprivate

Definition at line 4975 of file json.hpp.

References nlohmann::detail::parse_error::create(), nlohmann::detail::out_of_range::create(), JSON_LIKELY, and JSON_UNLIKELY.

4976  {
4977  // stack to remember the hierarchy of structured values we are parsing
4978  // true = array; false = object
4979  std::vector<bool> states;
4980  // value to avoid a goto (see comment where set to true)
4981  bool skip_to_state_evaluation = false;
4982 
4983  while (true)
4984  {
4985  if (not skip_to_state_evaluation)
4986  {
4987  // invariant: get_token() was called before each iteration
4988  switch (last_token)
4989  {
4990  case token_type::begin_object:
4991  {
4992  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
4993  {
4994  return false;
4995  }
4996 
4997  // closing } -> we are done
4998  if (get_token() == token_type::end_object)
4999  {
5000  if (JSON_UNLIKELY(not sax->end_object()))
5001  {
5002  return false;
5003  }
5004  break;
5005  }
5006 
5007  // parse key
5008  if (JSON_UNLIKELY(last_token != token_type::value_string))
5009  {
5010  return sax->parse_error(m_lexer.get_position(),
5013  exception_message(token_type::value_string, "object key")));
5014  }
5015  if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
5016  {
5017  return false;
5018  }
5019 
5020  // parse separator (:)
5021  if (JSON_UNLIKELY(get_token() != token_type::name_separator))
5022  {
5023  return sax->parse_error(m_lexer.get_position(),
5026  exception_message(token_type::name_separator, "object separator")));
5027  }
5028 
5029  // remember we are now inside an object
5030  states.push_back(false);
5031 
5032  // parse values
5033  get_token();
5034  continue;
5035  }
5036 
5037  case token_type::begin_array:
5038  {
5039  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
5040  {
5041  return false;
5042  }
5043 
5044  // closing ] -> we are done
5045  if (get_token() == token_type::end_array)
5046  {
5047  if (JSON_UNLIKELY(not sax->end_array()))
5048  {
5049  return false;
5050  }
5051  break;
5052  }
5053 
5054  // remember we are now inside an array
5055  states.push_back(true);
5056 
5057  // parse values (no need to call get_token)
5058  continue;
5059  }
5060 
5061  case token_type::value_float:
5062  {
5063  const auto res = m_lexer.get_number_float();
5064 
5065  if (JSON_UNLIKELY(not std::isfinite(res)))
5066  {
5067  return sax->parse_error(m_lexer.get_position(),
5069  out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'"));
5070  }
5071  else
5072  {
5073  if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
5074  {
5075  return false;
5076  }
5077  break;
5078  }
5079  }
5080 
5081  case token_type::literal_false:
5082  {
5083  if (JSON_UNLIKELY(not sax->boolean(false)))
5084  {
5085  return false;
5086  }
5087  break;
5088  }
5089 
5090  case token_type::literal_null:
5091  {
5092  if (JSON_UNLIKELY(not sax->null()))
5093  {
5094  return false;
5095  }
5096  break;
5097  }
5098 
5099  case token_type::literal_true:
5100  {
5101  if (JSON_UNLIKELY(not sax->boolean(true)))
5102  {
5103  return false;
5104  }
5105  break;
5106  }
5107 
5108  case token_type::value_integer:
5109  {
5110  if (JSON_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer())))
5111  {
5112  return false;
5113  }
5114  break;
5115  }
5116 
5117  case token_type::value_string:
5118  {
5119  if (JSON_UNLIKELY(not sax->string(m_lexer.get_string())))
5120  {
5121  return false;
5122  }
5123  break;
5124  }
5125 
5126  case token_type::value_unsigned:
5127  {
5128  if (JSON_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned())))
5129  {
5130  return false;
5131  }
5132  break;
5133  }
5134 
5135  case token_type::parse_error:
5136  {
5137  // using "uninitialized" to avoid "expected" message
5138  return sax->parse_error(m_lexer.get_position(),
5141  exception_message(token_type::uninitialized, "value")));
5142  }
5143 
5144  default: // the last token was unexpected
5145  {
5146  return sax->parse_error(m_lexer.get_position(),
5149  exception_message(token_type::literal_or_value, "value")));
5150  }
5151  }
5152  }
5153  else
5154  {
5155  skip_to_state_evaluation = false;
5156  }
5157 
5158  // we reached this line after we successfully parsed a value
5159  if (states.empty())
5160  {
5161  // empty stack: we reached the end of the hierarchy: done
5162  return true;
5163  }
5164  else
5165  {
5166  if (states.back()) // array
5167  {
5168  // comma -> next value
5169  if (get_token() == token_type::value_separator)
5170  {
5171  // parse a new value
5172  get_token();
5173  continue;
5174  }
5175 
5176  // closing ]
5177  if (JSON_LIKELY(last_token == token_type::end_array))
5178  {
5179  if (JSON_UNLIKELY(not sax->end_array()))
5180  {
5181  return false;
5182  }
5183 
5184  // We are done with this array. Before we can parse a
5185  // new value, we need to evaluate the new state first.
5186  // By setting skip_to_state_evaluation to false, we
5187  // are effectively jumping to the beginning of this if.
5188  assert(not states.empty());
5189  states.pop_back();
5190  skip_to_state_evaluation = true;
5191  continue;
5192  }
5193  else
5194  {
5195  return sax->parse_error(m_lexer.get_position(),
5198  exception_message(token_type::end_array, "array")));
5199  }
5200  }
5201  else // object
5202  {
5203  // comma -> next value
5204  if (get_token() == token_type::value_separator)
5205  {
5206  // parse key
5207  if (JSON_UNLIKELY(get_token() != token_type::value_string))
5208  {
5209  return sax->parse_error(m_lexer.get_position(),
5212  exception_message(token_type::value_string, "object key")));
5213  }
5214  else
5215  {
5216  if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
5217  {
5218  return false;
5219  }
5220  }
5221 
5222  // parse separator (:)
5223  if (JSON_UNLIKELY(get_token() != token_type::name_separator))
5224  {
5225  return sax->parse_error(m_lexer.get_position(),
5228  exception_message(token_type::name_separator, "object separator")));
5229  }
5230 
5231  // parse values
5232  get_token();
5233  continue;
5234  }
5235 
5236  // closing }
5237  if (JSON_LIKELY(last_token == token_type::end_object))
5238  {
5239  if (JSON_UNLIKELY(not sax->end_object()))
5240  {
5241  return false;
5242  }
5243 
5244  // We are done with this object. Before we can parse a
5245  // new value, we need to evaluate the new state first.
5246  // By setting skip_to_state_evaluation to false, we
5247  // are effectively jumping to the beginning of this if.
5248  assert(not states.empty());
5249  states.pop_back();
5250  skip_to_state_evaluation = true;
5251  continue;
5252  }
5253  else
5254  {
5255  return sax->parse_error(m_lexer.get_position(),
5258  exception_message(token_type::end_object, "object")));
5259  }
5260  }
5261  }
5262  }
5263  }
constexpr number_unsigned_t get_number_unsigned() const noexcept
return unsigned integer value
Definition: json.hpp:3769
lexer_t m_lexer
the lexer
Definition: json.hpp:5306
token_type get_token()
get next token from lexer
Definition: json.hpp:5266
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
string_t & get_string()
return current string value (implicitly resets the token; useful only once)
Definition: json.hpp:3781
constexpr position_t get_position() const noexcept
return position of last read token
Definition: json.hpp:3791
constexpr number_integer_t get_number_integer() const noexcept
return integer value
Definition: json.hpp:3763
constexpr number_float_t get_number_float() const noexcept
return floating-point value
Definition: json.hpp:3775
#define JSON_LIKELY(x)
Definition: json.hpp:193
token_type last_token
the type of the last read token
Definition: json.hpp:5304
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
std::string exception_message(const token_type expected, const std::string &context)
Definition: json.hpp:5271
std::string get_token_string() const
Definition: json.hpp:3799
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894

Member Data Documentation

template<typename BasicJsonType >
const bool nlohmann::detail::parser< BasicJsonType >::allow_exceptions = true
private

whether to throw exceptions in case of errors

Definition at line 5308 of file json.hpp.

template<typename BasicJsonType >
const parser_callback_t nlohmann::detail::parser< BasicJsonType >::callback = nullptr
private

callback function

Definition at line 5302 of file json.hpp.

template<typename BasicJsonType >
token_type nlohmann::detail::parser< BasicJsonType >::last_token = token_type::uninitialized
private

the type of the last read token

Definition at line 5304 of file json.hpp.

template<typename BasicJsonType >
lexer_t nlohmann::detail::parser< BasicJsonType >::m_lexer
private

the lexer

Definition at line 5306 of file json.hpp.


The documentation for this class was generated from the following file: