summaryrefslogtreecommitdiff
path: root/tools/common/toml.h
blob: 4c97e7f2ddcaed72e8520673d941aa6af747a4e2 (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
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
/*
 * mrustc common tools
 * - by John Hodge (Mutabah)
 *
 * tools/common/toml.h
 * - A very basic (and probably incomplete) streaming TOML parser
 */
#pragma once

#include <fstream>
#include <vector>
#include <string>
#include <unordered_map>

class TomlFileIter;
struct TomlKeyValue;

class TomlFile
{
    /// Input file stream
    ::std::ifstream m_if;

    /// Name of the current `[]` block
    ::std::vector<::std::string>    m_current_block;

    /// Path suffix of the current composite (none if empty)
    ::std::vector<::std::string>    m_current_composite;

    /// Index of the next array field (if zero, not parsing an array)
    unsigned int m_next_array_index;

    /// Next indexes if top-level defined arrays (e.g. `[[foo]]`)
    ::std::unordered_map<::std::string,unsigned>    m_array_counts;

public:
    TomlFile(const ::std::string& filename);

    TomlFileIter begin();
    TomlFileIter end();

    // Obtain the next value in the file
    TomlKeyValue get_next_value();
};

struct TomlValue
{
    enum class Type
    {
        // A true/false, 1/0, yes/no value
        Boolean,
        // A double-quoted string
        String,
        // Integer
        Integer,
        // A list of other values
        List,
    };
    friend ::std::ostream& operator<<(::std::ostream& os, const Type& e) {
        switch(e)
        {
        case Type::Boolean: os << "boolean";    break;
        case Type::String:  os << "string";     break;
        case Type::Integer: os << "integer";    break;
        case Type::List:    os << "list";       break;
        }
        return os;
    }
    struct TypeError:
        public ::std::exception
    {
        Type have;
        Type exp;

        TypeError(Type h, Type e):
            have(h),
            exp(e)
        {
        }

        const char* what() const noexcept override {
            return "toml type error";
        }
        friend ::std::ostream& operator<<(::std::ostream& os, const TypeError& e) {
            os << "expected " << e.exp << ", got " << e.have;
            return os;
        }
    };

    Type m_type;
    uint64_t    m_int_value;
    ::std::string   m_str_value;
    ::std::vector<TomlValue>    m_sub_values;

    TomlValue():
        m_type(Type::String)
    {
    }
    TomlValue(::std::string s):
        m_type( Type::String ),
        m_str_value(::std::move(s))
    {
    }
    TomlValue(int64_t v):
        m_type(Type::Integer),
        m_int_value(v)
    {
    }
    TomlValue(bool v) :
        m_type(Type::Boolean),
        m_int_value(v ? 1 : 0)
    {
    }

    const ::std::string& as_string() const {
        if( m_type != Type::String ) {
            throw TypeError { m_type, Type::String };
        }
        return m_str_value;
    }
    bool as_bool() const {
        if(m_type != Type::Boolean) {
            throw TypeError { m_type, Type::Boolean };
        }
        return m_int_value != 0;
    }
    uint64_t as_int() const {
        if(m_type != Type::Integer) {
            throw TypeError { m_type, Type::Integer };
        }
        return m_int_value;
    }
    const ::std::vector<TomlValue>& as_list() const {
        if(m_type != Type::List) {
            throw TypeError { m_type, Type::List };
        }
        return m_sub_values;
    }

    friend ::std::ostream& operator<<(::std::ostream& os, const TomlValue& x) {
        switch(x.m_type)
        {
        case Type::Boolean: os << (x.m_int_value != 0 ? "true" : "false");  break;
        case Type::Integer: os << x.m_int_value; break;
        case Type::List:
            os << "[";
            for(auto& e : x.m_sub_values)
                os << e << ",";
            os << "]";
            break;
        case Type::String:
            os << "\"" << x.m_str_value << "\"";
            break;
        }
        return os;
    }
};

struct TomlKeyValue
{
    // Path to the value (last node is the value name)
    // TODO: How are things like `[[bin]]` handled?
    ::std::vector<::std::string>    path;
    // Relevant value
    TomlValue   value;
};

class TomlFileIter
{
    friend class TomlFile;
    TomlFile& m_reader;
    TomlKeyValue    m_cur_value;

    TomlFileIter(TomlFile& tf):
        m_reader(tf)
    {

    }

public:
    TomlKeyValue operator*() const
    {
        return m_cur_value;
    }
    void operator++()
    {
        m_cur_value = m_reader.get_next_value();
    }
    bool operator!=(const TomlFileIter& x) const
    {
        return m_cur_value.path != x.m_cur_value.path;
    }
};