aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/Support/YAML.h
blob: 2e403a91752d1d8e2555fb825ebffbdcd20a0d6d (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
193
194
195
196
197
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_SUPPORT_YAML_H
#define RCC_SUPPORT_YAML_H

#include "rcc/Support/TextDiagnostic.h"

#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace rcc::yaml {

class Parser;

class NodeID final {
public:
  constexpr NodeID() noexcept = default;

  [[nodiscard]] constexpr bool isValid() const noexcept {
    return Value != InvalidValue;
  }

  [[nodiscard]] constexpr std::uint32_t rawValue() const noexcept {
    return Value;
  }

  friend constexpr bool operator==(NodeID Left, NodeID Right) noexcept = default;

private:
  static constexpr std::uint32_t InvalidValue = std::numeric_limits<std::uint32_t>::max();

  explicit constexpr NodeID(std::uint32_t Value) noexcept : Value(Value) {}

  std::uint32_t Value = InvalidValue;

  friend class Parser;
  friend class Stream;
};

enum class NodeKind : std::uint8_t {
  Null,
  Scalar,
  Sequence,
  Mapping,
};

enum class ScalarStyle : std::uint8_t {
  Plain,
  SingleQuoted,
  DoubleQuoted,
  Literal,
  Folded,
};

enum class CollectionStyle : std::uint8_t {
  Block,
  Flow,
};

struct MappingEntry final {
  std::string Key;
  TextRange KeyRange;
  NodeID Value;
};

class Node final {
public:
  [[nodiscard]] NodeKind kind() const noexcept {
    return Kind;
  }

  [[nodiscard]] const TextRange &range() const noexcept {
    return Range;
  }

  [[nodiscard]] ScalarStyle scalarStyle() const noexcept {
    return ScalarStyleValue;
  }

  [[nodiscard]] CollectionStyle collectionStyle() const noexcept {
    return CollectionStyleValue;
  }

  [[nodiscard]] std::string_view scalar() const noexcept {
    return Scalar;
  }

  [[nodiscard]] std::span<const NodeID> sequence() const noexcept {
    return Sequence;
  }

  [[nodiscard]] std::span<const MappingEntry> mapping() const noexcept {
    return Mapping;
  }

  [[nodiscard]] std::optional<NodeID> mappingValue(std::string_view Key) const noexcept;

private:
  explicit Node(NodeKind Kind, TextRange Range) : Kind(Kind), Range(Range) {}

  NodeKind Kind;
  TextRange Range;
  ScalarStyle ScalarStyleValue = ScalarStyle::Plain;
  CollectionStyle CollectionStyleValue = CollectionStyle::Block;
  std::string Scalar;
  std::vector<NodeID> Sequence;
  std::vector<MappingEntry> Mapping;

  friend class Parser;
};

struct Document final {
  NodeID Root;
  TextRange Range;
  bool HasExplicitStart = false;
  bool HasExplicitEnd = false;
};

class Stream final {
public:
  Stream(const Stream &) = delete;
  Stream &operator=(const Stream &) = delete;
  Stream(Stream &&) noexcept = default;
  Stream &operator=(Stream &&) noexcept = default;
  ~Stream() = default;

  [[nodiscard]] const TextSource &source() const noexcept {
    return Source;
  }

  [[nodiscard]] std::span<const Document> documents() const noexcept {
    return Documents;
  }

  [[nodiscard]] const Node *node(NodeID ID) const noexcept;
  [[nodiscard]] std::size_t nodeCount() const noexcept {
    return Nodes.size();
  }

private:
  Stream(std::string SourceName, std::string SourceText)
      : Source(std::move(SourceName), std::move(SourceText)) {}

  TextSource Source;
  std::vector<Node> Nodes;
  std::vector<Document> Documents;

  friend class Parser;
};

struct ParseOptions final {
  std::size_t MaximumDepth = 128;
  std::size_t MaximumNodes = 1U << 20U;
};

class ParseResult final {
public:
  ParseResult(const ParseResult &) = delete;
  ParseResult &operator=(const ParseResult &) = delete;
  ParseResult(ParseResult &&) noexcept = default;
  ParseResult &operator=(ParseResult &&) noexcept = default;
  ~ParseResult() = default;

  [[nodiscard]] bool succeeded() const noexcept {
    return ParsedStream.has_value() && Diagnostics.empty();
  }

  [[nodiscard]] const Stream *stream() const noexcept {
    return ParsedStream.has_value() ? ParsedStream.operator->() : nullptr;
  }

  [[nodiscard]] std::span<const TextDiagnostic> diagnostics() const noexcept {
    return Diagnostics;
  }

private:
  ParseResult() = default;

  std::optional<Stream> ParsedStream;
  std::vector<TextDiagnostic> Diagnostics;

  friend class Parser;
};

[[nodiscard]] ParseResult parse(std::string SourceName, std::string SourceText,
                                ParseOptions Options = {});

} // namespace rcc::yaml

#endif // RCC_SUPPORT_YAML_H