libzypp  17.38.17
jsonparser_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
14 #ifndef ZYPP_CORE_PARSER_JSON_PARSER_H
15 #define ZYPP_CORE_PARSER_JSON_PARSER_H
16 
17 #include <optional>
23 #include <zypp-core/Globals.h>
24 #include "../JsonValue.h"
25 
26 namespace zypp::json::detail {
27 
30  inline zyppng::expected<Number> numberFromString( const std::string & str ) {
31  using namespace zyppng::operators;
32  using zyppng::operators::operator|;
33  return zyppng::mtry( [&](){ return std::stod( str, nullptr ); } )
34  | and_then( []( double res ) -> zyppng::expected<Number> { return zyppng::make_expected_success( Number(res) ); } );
35  }
36 
37  inline zyppng::expected<Int> intFromString( const std::string & str ) {
38  using namespace zyppng::operators;
39  using zyppng::operators::operator|;
40  return zyppng::mtry( [&](){ return std::stoll( str, nullptr ); } )
41  | and_then( []( std::int64_t res ) { return zyppng::make_expected_success( Int(res) ); } );
42  }
43 
44  inline zyppng::expected<UInt> uintFromString( const std::string & str ) {
45  using namespace zyppng::operators;
46  using zyppng::operators::operator|;
47  return zyppng::mtry( [&](){ return std::stoull( str, nullptr ); } )
48  | and_then( []( unsigned long long res ) { return zyppng::make_expected_success( UInt(res) ); } );
49  }
50 
51  // Parser is an internal implementation detail — no ZYPP_API.
52  // External code uses zypp::json::parseDocument() / parseDocumentExpected().
53  class Parser : private base::NonCopyable
54  {
55  public:
57  Parser() = default;
58  Parser(const Parser &) = delete;
59  Parser(Parser &&) = delete;
60  Parser &operator=(const Parser &) = delete;
61  Parser &operator=(Parser &&) = delete;
62 
64  virtual ~Parser(){}
65 
69  zyppng::expected<Value> parse( const InputStream & input_r );
70 
71 
72  struct Token {
73 
74  enum Type {
88  TOK_END, // end of document
90  };
91 
93  std::string _token;
94 
95  static Token eof();
96  };
97 
99 
100  private:
101 
106 
107 
112 
113 
114  std::istream::char_type popChar();
115  std::istream::char_type peekChar();
116  void consumeChar();
117 
118  zyppng::expected<void> consumeString( const std::string &str );
119 
120  static inline std::istream::char_type eofChar() {
121  return std::istream::traits_type::eof();
122  }
123 
124  template <typename T = Token>
126  {
128  }
129 
130  std::optional<InputStream> _stream;
131  int _nestingDepth = 0; //< how deep is the nesting ( prevent memory overflow by too deep nesting )
132  };
133 
134 } // namespace zypp::json::detail
135 #endif
Parser & operator=(const Parser &)=delete
zyppng::expected< Array > parseArray()
std::istream::char_type popChar()
zyppng::expected< T > makeParseError(const std::string &message, exception_detail::CodeLocation &&loc)
Definition: jsonparser_p.h:125
zyppng::expected< void > consumeString(const std::string &str)
String related utilities and Regular expression matching.
zyppng::expected< Value > parse(const InputStream &input_r)
Parse the stream.
Definition: jsonparser_p.cc:37
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition: expected.h:470
Helper to create and pass std::istream.
Definition: inputstream.h:56
zyppng::expected< UInt > uintFromString(const std::string &str)
Definition: jsonparser_p.h:44
zyppng::expected< Token > parseBoolToken()
static std::istream::char_type eofChar()
Definition: jsonparser_p.h:120
std::istream::char_type peekChar()
zyppng::expected< Token > parseNullToken()
Provides API related macros.
virtual ~Parser()
Dtor.
Definition: jsonparser_p.h:64
zyppng::expected< Value > finishParseValue(Token begin)
zyppng::expected< Object > parseObject()
Definition: jsonparser_p.cc:87
zyppng::expected< Token > nextToken()
zyppng::expected< Value > parseValue()
zyppng::expected< Token > parseStringToken()
Parser()=default
Default ctor.
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:35
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition: expected.h:520
auto mtry(F &&f, Args &&...args)
Definition: mtry.h:50
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, CodeLocation &&where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition: Exception.h:435
zyppng::expected< Number > numberFromString(const std::string &str)
fromString helpers — only callable from within the tokeniser.
Definition: jsonparser_p.h:30
zyppng::expected< Int > intFromString(const std::string &str)
Definition: jsonparser_p.h:37
zyppng::expected< Token > parseNumberToken()
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
std::optional< InputStream > _stream
Definition: jsonparser_p.h:130