aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/parser.cpp
blob: 99d2e55b9c8322e6a5b095fb794e77431f8deed6 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "parser.h"
#include "consumer.h"
#include "format.h"
#include "parser_detail.h"

#include <util/stream/input.h>
#include <util/generic/buffer.h>

namespace NYson {
    ////////////////////////////////////////////////////////////////////////////////

    class TYsonParser::TImpl {
    public:
        TImpl(
            NYT::NYson::IYsonConsumer* consumer,
            IInputStream* stream,
            EYsonType type,
            bool enableLinePositionInfo,
            TMaybe<ui64> memoryLimit = Nothing())
            : Consumer_(consumer)
            , Stream_(stream)
            , Type_(type)
            , EnableLinePositionInfo_(enableLinePositionInfo)
            , MemoryLimit_(memoryLimit)
        {
        }

        void Parse() {
            TBuffer buffer(64 << 10);
            ParseYsonStreamImpl<NYT::NYson::IYsonConsumer, TStreamReader>(
                TStreamReader(Stream_, buffer.Data(), buffer.Capacity()),
                Consumer_,
                Type_,
                EnableLinePositionInfo_,
                MemoryLimit_);
        }

    private:
        NYT::NYson::IYsonConsumer* Consumer_;
        IInputStream* Stream_;
        EYsonType Type_;
        bool EnableLinePositionInfo_;
        TMaybe<ui64> MemoryLimit_;
    };

    ////////////////////////////////////////////////////////////////////////////////

    TYsonParser::TYsonParser(
        NYT::NYson::IYsonConsumer* consumer,
        IInputStream* stream, 
        EYsonType type,
        bool enableLinePositionInfo,
        TMaybe<ui64> memoryLimit)
        : Impl(new TImpl(consumer, stream, type, enableLinePositionInfo, memoryLimit))
    {
    }

    TYsonParser::~TYsonParser() {
    }

    void TYsonParser::Parse() {
        Impl->Parse();
    }

    ////////////////////////////////////////////////////////////////////////////////

    class TStatelessYsonParser::TImpl {
    private:
        THolder<TStatelessYsonParserImplBase> Impl;

    public:
        TImpl(
            NYT::NYson::IYsonConsumer* consumer,
            bool enableLinePositionInfo,
            TMaybe<ui64> memoryLimit)
            : Impl(
                  enableLinePositionInfo
                      ? static_cast<TStatelessYsonParserImplBase*>(new TStatelessYsonParserImpl<NYT::NYson::IYsonConsumer, true>(consumer, memoryLimit))
                      : static_cast<TStatelessYsonParserImplBase*>(new TStatelessYsonParserImpl<NYT::NYson::IYsonConsumer, false>(consumer, memoryLimit)))
                  {
        }

        void Parse(const TStringBuf& data, EYsonType type = ::NYson::EYsonType::Node) {
            Impl->Parse(data, type);
        }
    };

    ////////////////////////////////////////////////////////////////////////////////

    TStatelessYsonParser::TStatelessYsonParser(
        NYT::NYson::IYsonConsumer* consumer,
        bool enableLinePositionInfo,
        TMaybe<ui64> memoryLimit)
        : Impl(new TImpl(consumer, enableLinePositionInfo, memoryLimit))
    {
    }

    TStatelessYsonParser::~TStatelessYsonParser() {
    }

    void TStatelessYsonParser::Parse(const TStringBuf& data, EYsonType type) {
        Impl->Parse(data, type);
    }

    ////////////////////////////////////////////////////////////////////////////////

    void ParseYsonStringBuffer(
        const TStringBuf& buffer,
        NYT::NYson::IYsonConsumer* consumer,
        EYsonType type,
        bool enableLinePositionInfo,
        TMaybe<ui64> memoryLimit) {
        ParseYsonStreamImpl<NYT::NYson::IYsonConsumer, TStringReader>(
            TStringReader(buffer.begin(), buffer.end()),
            consumer,
            type,
            enableLinePositionInfo,
            memoryLimit);
    }

    ////////////////////////////////////////////////////////////////////////////////

    class TYsonListParser::TImpl {
    public:
        TImpl(
            NYT::NYson::IYsonConsumer* consumer,
            IInputStream* stream,
            bool enableLinePositionInfo,
            TMaybe<ui64> memoryLimit = Nothing())
            : Consumer_(consumer)
            , Stream_(stream)
            , EnableLinePositionInfo_(enableLinePositionInfo)
            , MemoryLimit_(memoryLimit)
            , Buffer_(64 << 10)
            , Reader_(Stream_, Buffer_.Data(), Buffer_.Capacity())
        {
        }

        bool Parse() {
            if (!Impl_) {
                Impl_.Reset(
                    EnableLinePositionInfo_
                        ? static_cast<TYsonListParserImplBase*>(new TYsonListParserImpl<NYT::NYson::IYsonConsumer, TStreamReader, true>(Reader_, Consumer_, MemoryLimit_))
                        : static_cast<TYsonListParserImplBase*>(new TYsonListParserImpl<NYT::NYson::IYsonConsumer, TStreamReader, false>(Reader_, Consumer_, MemoryLimit_)));
            }
            return Impl_->Parse();
        }

    private:
        NYT::NYson::IYsonConsumer* Consumer_;
        IInputStream* Stream_;
        bool EnableLinePositionInfo_;
        TMaybe<ui64> MemoryLimit_;
        TBuffer Buffer_;
        TStreamReader Reader_;
        THolder<TYsonListParserImplBase> Impl_;
    };

    ////////////////////////////////////////////////////////////////////////////////

    TYsonListParser::TYsonListParser(
        NYT::NYson::IYsonConsumer* consumer,
        IInputStream* stream, 
        bool enableLinePositionInfo,
        TMaybe<ui64> memoryLimit)
        : Impl(new TImpl(consumer, stream, enableLinePositionInfo, memoryLimit))
    {
    }

    TYsonListParser::~TYsonListParser() {
    }

    bool TYsonListParser::Parse() {
        return Impl->Parse();
    }

    ////////////////////////////////////////////////////////////////////////////////

} // namespace NYson