aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/token.cpp
blob: f614656bbef755e90f26769739fff335b78924fd (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "token.h"

#include <util/string/vector.h>
#include <util/string/printf.h>

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

    ETokenType CharToTokenType(char ch) {
        switch (ch) {
            case ';':
                return ETokenType::Semicolon;
            case '=':
                return ETokenType::Equals;
            case '{':
                return ETokenType::LeftBrace;
            case '}':
                return ETokenType::RightBrace;
            case '#':
                return ETokenType::Hash;
            case '[':
                return ETokenType::LeftBracket;
            case ']':
                return ETokenType::RightBracket;
            case '<':
                return ETokenType::LeftAngle;
            case '>':
                return ETokenType::RightAngle;
            case '(':
                return ETokenType::LeftParenthesis;
            case ')':
                return ETokenType::RightParenthesis;
            case '+':
                return ETokenType::Plus;
            case ':':
                return ETokenType::Colon;
            case ',':
                return ETokenType::Comma;
            default:
                return ETokenType::EndOfStream;
        }
    }

    char TokenTypeToChar(ETokenType type) {
        switch (type) {
            case ETokenType::Semicolon:
                return ';';
            case ETokenType::Equals:
                return '=';
            case ETokenType::Hash:
                return '#';
            case ETokenType::LeftBracket:
                return '[';
            case ETokenType::RightBracket:
                return ']';
            case ETokenType::LeftBrace:
                return '{';
            case ETokenType::RightBrace:
                return '}';
            case ETokenType::LeftAngle:
                return '<';
            case ETokenType::RightAngle:
                return '>';
            case ETokenType::LeftParenthesis:
                return '(';
            case ETokenType::RightParenthesis:
                return ')';
            case ETokenType::Plus:
                return '+';
            case ETokenType::Colon:
                return ':';
            case ETokenType::Comma:
                return ',';
            default:
                Y_FAIL("unreachable");
        }
    }

    TString TokenTypeToString(ETokenType type) {
        return TString(1, TokenTypeToChar(type));
    }

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

    const TToken TToken::EndOfStream;

    TToken::TToken()
        : Type_(ETokenType::EndOfStream)
        , Int64Value(0)
        , Uint64Value(0)
        , DoubleValue(0.0)
        , BooleanValue(false)
    {
    }

    TToken::TToken(ETokenType type)
        : Type_(type)
        , Int64Value(0)
        , Uint64Value(0)
        , DoubleValue(0.0)
        , BooleanValue(false)
    {
        switch (type) {
            case ETokenType::String:
            case ETokenType::Int64:
            case ETokenType::Uint64:
            case ETokenType::Double:
            case ETokenType::Boolean:
                Y_FAIL("unreachable");
            default:
                break;
        }
    }

    TToken::TToken(const TStringBuf& stringValue)
        : Type_(ETokenType::String)
        , StringValue(stringValue)
        , Int64Value(0)
        , Uint64Value(0)
        , DoubleValue(0.0)
        , BooleanValue(false)
    {
    }

    TToken::TToken(i64 int64Value)
        : Type_(ETokenType::Int64)
        , Int64Value(int64Value)
        , Uint64Value(0)
        , DoubleValue(0.0)
    {
    }

    TToken::TToken(ui64 uint64Value)
        : Type_(ETokenType::Uint64)
        , Int64Value(0)
        , Uint64Value(uint64Value)
        , DoubleValue(0.0)
        , BooleanValue(false)
    {
    }

    TToken::TToken(double doubleValue)
        : Type_(ETokenType::Double)
        , Int64Value(0)
        , Uint64Value(0)
        , DoubleValue(doubleValue)
        , BooleanValue(false)
    {
    }

    TToken::TToken(bool booleanValue)
        : Type_(ETokenType::Boolean)
        , Int64Value(0)
        , DoubleValue(0.0)
        , BooleanValue(booleanValue)
    {
    }

    bool TToken::IsEmpty() const {
        return Type_ == ETokenType::EndOfStream;
    }

    const TStringBuf& TToken::GetStringValue() const {
        CheckType(ETokenType::String);
        return StringValue;
    }

    i64 TToken::GetInt64Value() const {
        CheckType(ETokenType::Int64);
        return Int64Value;
    }

    ui64 TToken::GetUint64Value() const {
        CheckType(ETokenType::Uint64);
        return Uint64Value;
    }

    double TToken::GetDoubleValue() const {
        CheckType(ETokenType::Double);
        return DoubleValue;
    }

    bool TToken::GetBooleanValue() const {
        CheckType(ETokenType::Boolean);
        return BooleanValue;
    }

    void TToken::CheckType(ETokenType expectedType) const {
        if (Type_ != expectedType) {
            if (Type_ == ETokenType::EndOfStream) {
                ythrow TYsonException() << "Unexpected end of stream (ExpectedType: " << TokenTypeToString(expectedType) << ")";
            } else {
                ythrow TYsonException() << "Unexpected token (Token: '" << ToString(*this)
                                        << "', Type: " << TokenTypeToString(Type_)
                                        << ", ExpectedType: " << TokenTypeToString(expectedType) << ")";
            }
        }
    }

    void TToken::Reset() {
        Type_ = ETokenType::EndOfStream;
        Int64Value = 0;
        Uint64Value = 0;
        DoubleValue = 0.0;
        StringValue = TStringBuf();
        BooleanValue = false;
    }

    TString ToString(const TToken& token) {
        switch (token.GetType()) {
            case ETokenType::EndOfStream:
                return TString();

            case ETokenType::String:
                return TString(token.GetStringValue());

            case ETokenType::Int64:
                return ::ToString(token.GetInt64Value());

            case ETokenType::Uint64:
                return ::ToString(token.GetUint64Value());

            case ETokenType::Double:
                return ::ToString(token.GetDoubleValue());

            case ETokenType::Boolean:
                return token.GetBooleanValue() ? "true" : "false";

            default:
                return TokenTypeToString(token.GetType());
        }
    }

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

} // namespace NYson