libzypp 17.38.14
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
26namespace 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
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>
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
Provides API related macros.
Helper to create and pass std::istream.
Definition inputstream.h:57
zyppng::expected< Token > parseNumberToken()
zyppng::expected< void > consumeString(const std::string &str)
static std::istream::char_type eofChar()
Parser & operator=(Parser &&)=delete
zyppng::expected< Token > parseBoolToken()
Parser(const Parser &)=delete
zyppng::expected< Value > parseValue()
std::istream::char_type popChar()
std::istream::char_type peekChar()
zyppng::expected< T > makeParseError(const std::string &message, exception_detail::CodeLocation &&loc)
zyppng::expected< Token > parseStringToken()
Parser & operator=(const Parser &)=delete
Parser(Parser &&)=delete
zyppng::expected< Token > nextToken()
zyppng::expected< Array > parseArray()
std::optional< InputStream > _stream
zyppng::expected< Value > finishParseValue(Token begin)
zyppng::expected< Object > parseObject()
zyppng::expected< Value > parse(const InputStream &input_r)
Parse the stream.
Parser()=default
Default ctor.
zyppng::expected< Token > parseNullToken()
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
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< UInt > uintFromString(const std::string &str)
zyppng::expected< Number > numberFromString(const std::string &str)
fromString helpers — only callable from within the tokeniser.
zyppng::expected< Int > intFromString(const std::string &str)
auto and_then(Fun &&function)
Definition expected.h:708
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
Definition expected.h:470
auto mtry(F &&f, Args &&...args)
Definition mtry.h:50
Keep FILE, FUNCTION and LINE.
Definition Exception.h:36