summaryrefslogtreecommitdiffstats
path: root/include/orcus/json_parser_thread.hpp
blob: 8328ef1171a8592d981247f9794b74e8697a1d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_ORCUS_JSON_PARSER_THREAD_HPP
#define INCLUDED_ORCUS_JSON_PARSER_THREAD_HPP

#include "env.hpp"
#include "types.hpp"

#include <memory>
#include <vector>
#include <ostream>
#include <variant>

namespace orcus {

class string_pool;

namespace json {

struct ORCUS_PSR_DLLPUBLIC parser_stats
{
    size_t token_buffer_size_threshold;
};

enum class parse_token_t
{
    unknown,
    begin_parse,
    end_parse,
    begin_array,
    end_array,
    begin_object,
    object_key,
    end_object,
    boolean_true,
    boolean_false,
    null,
    string,
    number,
    parse_error,
};

struct ORCUS_PSR_DLLPUBLIC parse_token
{
    using value_type = std::variant<std::string_view, parse_error_value_t, double>;

    parse_token_t type;
    value_type value;

    parse_token();
    parse_token(parse_token_t _type);
    parse_token(parse_token_t _type, std::string_view s);
    parse_token(std::string_view s, std::ptrdiff_t offset);
    parse_token(double value);

    parse_token(const parse_token& other);

    parse_token& operator= (parse_token) = delete;

    bool operator== (const parse_token& other) const;
    bool operator!= (const parse_token& other) const;
};

typedef std::vector<parse_token> parse_tokens_t;

ORCUS_PSR_DLLPUBLIC std::ostream& operator<< (std::ostream& os, const parse_tokens_t& tokens);

class ORCUS_PSR_DLLPUBLIC parser_thread
{
    struct impl;
    std::unique_ptr<impl> mp_impl;

public:
    parser_thread(const char* p, size_t n, size_t min_token_size);
    parser_thread(const char* p, size_t n, size_t min_token_size, size_t max_token_size);
    ~parser_thread();

    void start();

    /**
     * Wait until new set of tokens becomes available.
     *
     * @param tokens new set of tokens.
     *
     * @return true if the parsing is still in progress (therefore more tokens
     *         to come), false if it's done i.e. this is the last token set.
     */
    bool next_tokens(parse_tokens_t& tokens);

    parser_stats get_stats() const;

    void swap_string_pool(string_pool& pool);
};

}}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */