aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/read_ops.cpp
blob: d565ac7e51112923e92be84408bd66d32e5d3ac7 (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
#include "read_ops.h"

using namespace NYsonPull;
using namespace NYsonPull::NReadOps;

namespace {
    bool TrySkipValueUntil(EEventType end, TReader& reader) {
        const auto& event = reader.NextEvent();
        if (event.Type() == end) {
            return false; 
        } 
        SkipCurrentValue(event, reader); 
        return true; 
    } 

    bool TrySkipKeyValueUntil(EEventType end, TReader& reader) {
        const auto& event = reader.NextEvent();
        if (event.Type() == end) {
            return false; 
        } 
        Expect(event, EEventType::Key);
        SkipValue(reader); 
        return true; 
    } 
}

void NYsonPull::NReadOps::SkipCurrentValue(const TEvent& event, TReader& reader) {
    switch (event.Type()) {
        case EEventType::BeginList:
            while (TrySkipValueUntil(EEventType::EndList, reader)) {
            } 
            return; 

        case EEventType::BeginMap:
            while (TrySkipKeyValueUntil(EEventType::EndMap, reader)) {
            } 
            return; 

        case EEventType::BeginAttributes:
            while (TrySkipKeyValueUntil(EEventType::EndAttributes, reader)) {
            } 
            // attributes after attributes are disallowed in TReader 
            SkipValue(reader); 
            return; 

        case EEventType::Scalar:
            return; 

        default: 
            throw yexception() << "Unexpected event: " << event; 
    } 
}

void NYsonPull::NReadOps::SkipValue(TReader& reader) {
    const auto& event = reader.NextEvent();
    SkipCurrentValue(event, reader); 
}

void NYsonPull::NReadOps::SkipControlRecords(TReader& reader) {
    const auto* event = &reader.LastEvent();
    while (event->Type() == EEventType::BeginAttributes) {
        SkipCurrentValue(*event, reader);
        event = &reader.NextEvent();
    }
    Expect(*event, EEventType::BeginMap);
}