aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/yson/token.h
blob: e2ab6ef68a3f2e73623a28e940a2a74f320cb9f0 (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
#pragma once

#include "public.h"

#include <yt/yt/core/misc/property.h>

namespace NYT::NYson {

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

DEFINE_ENUM(ETokenType,
    (EndOfStream) // Empty or uninitialized token

    (String)
    (Int64)
    (Uint64)
    (Double)
    (Boolean)

    // Special values:
    // YSON
    (Semicolon) // ;
    (Equals) // =
    (Hash) // #
    (LeftBracket) // [
    (RightBracket) // ]
    (LeftBrace) // {
    (RightBrace) // }
    (LeftAngle) // <
    (RightAngle) // >
    // Table ranges
    (LeftParenthesis) // (
    (RightParenthesis) // )
    (Plus) // +
    (Colon) // :
    (Comma) // ,
    // YPath
    (Slash) // /
);

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

ETokenType CharToTokenType(char ch);        // returns ETokenType::EndOfStream for non-special chars
char TokenTypeToChar(ETokenType type);      // YT_ABORT for non-special types
TString TokenTypeToString(ETokenType type);  // YT_ABORT for non-special types

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

class TToken
{
public:
    static const TToken EndOfStream;

    TToken();
    TToken(ETokenType type); // for special types
    explicit TToken(TStringBuf stringValue); // for string values
    explicit TToken(i64 int64Value); // for int64 values
    explicit TToken(ui64 int64Value); // for uint64 values
    explicit TToken(double doubleValue); // for double values
    explicit TToken(bool booleanValue); // for booleans

    DEFINE_BYVAL_RO_PROPERTY(ETokenType, Type);

    bool IsEmpty() const;
    TStringBuf GetStringValue() const;
    i64 GetInt64Value() const;
    ui64 GetUint64Value() const;
    double GetDoubleValue() const;
    bool GetBooleanValue() const;

    void ExpectType(ETokenType expectedType) const;
    void ExpectTypes(const std::vector<ETokenType>& expectedTypes) const;
    void ThrowUnexpected() const;

    void Reset();

private:
    TStringBuf StringValue_;
    i64 Int64Value_;
    ui64 Uint64Value_;
    double DoubleValue_;
    bool BooleanValue_;
};

TString ToString(const TToken& token);

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

} // namespace NYT::NYson