439 lines
11 KiB
C++
439 lines
11 KiB
C++
#pragma once
|
|
|
|
#include <charconv>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <map>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace Void::Json::Strict
|
|
{
|
|
inline constexpr std::size_t default_maximum_depth{64U};
|
|
|
|
struct ParseOptions {
|
|
std::size_t maximumDepth{default_maximum_depth};
|
|
bool allowDecimals{true};
|
|
};
|
|
|
|
struct ParseError {
|
|
std::size_t offset{};
|
|
std::string message;
|
|
};
|
|
|
|
struct Value {
|
|
using Array = std::vector<Value>;
|
|
using Object = std::map<std::string, Value, std::less<>>;
|
|
std::variant<std::nullptr_t, bool, std::int64_t, double,
|
|
std::string, Array, Object>
|
|
value;
|
|
};
|
|
|
|
namespace Detail
|
|
{
|
|
struct ParseFailure {
|
|
ParseError error;
|
|
};
|
|
|
|
class Parser final
|
|
{
|
|
public:
|
|
|
|
explicit Parser(const std::string_view input,
|
|
const ParseOptions options)
|
|
: input_{input}, options_{options}
|
|
{
|
|
}
|
|
|
|
[[nodiscard]] Value parse()
|
|
{
|
|
skipWhitespace();
|
|
Value result = parseValue(0U);
|
|
skipWhitespace();
|
|
if (position_ != input_.size()) {
|
|
fail("trailing data");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
|
|
[[noreturn]] void fail(const std::string_view message) const
|
|
{
|
|
throw ParseFailure{
|
|
.error =
|
|
{
|
|
.offset = position_,
|
|
.message = std::string{message},
|
|
},
|
|
};
|
|
}
|
|
|
|
void skipWhitespace()
|
|
{
|
|
while (position_ < input_.size() &&
|
|
(input_[position_] == ' ' ||
|
|
input_[position_] == '\t' ||
|
|
input_[position_] == '\r' ||
|
|
input_[position_] == '\n')) {
|
|
++position_;
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] char take()
|
|
{
|
|
if (position_ >= input_.size()) {
|
|
fail("unexpected end of input");
|
|
}
|
|
return input_[position_++];
|
|
}
|
|
|
|
void expect(const char expected)
|
|
{
|
|
if (take() != expected) {
|
|
fail("unexpected character");
|
|
}
|
|
}
|
|
|
|
void expectLiteral(const std::string_view literal)
|
|
{
|
|
if (input_.substr(position_, literal.size()) != literal) {
|
|
fail("invalid literal");
|
|
}
|
|
position_ += literal.size();
|
|
}
|
|
|
|
[[nodiscard]] static int hexValue(const char value)
|
|
{
|
|
if (value >= '0' && value <= '9') {
|
|
return value - '0';
|
|
}
|
|
if (value >= 'a' && value <= 'f') {
|
|
return value - 'a' + 10;
|
|
}
|
|
if (value >= 'A' && value <= 'F') {
|
|
return value - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
[[nodiscard]] std::uint32_t unicodeEscape()
|
|
{
|
|
std::uint32_t value{};
|
|
for (int index{}; index < 4; ++index) {
|
|
const int digit = hexValue(take());
|
|
if (digit < 0) {
|
|
fail("invalid unicode escape");
|
|
}
|
|
value = value * 16U + static_cast<std::uint32_t>(digit);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
static void appendUtf8(std::string& output,
|
|
const std::uint32_t codepoint)
|
|
{
|
|
if (codepoint <= 0x7FU) {
|
|
output.push_back(static_cast<char>(codepoint));
|
|
} else if (codepoint <= 0x7FFU) {
|
|
output.push_back(
|
|
static_cast<char>(0xC0U | (codepoint >> 6U)));
|
|
output.push_back(
|
|
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
|
} else if (codepoint <= 0xFFFFU) {
|
|
output.push_back(
|
|
static_cast<char>(0xE0U | (codepoint >> 12U)));
|
|
output.push_back(static_cast<char>(
|
|
0x80U | ((codepoint >> 6U) & 0x3FU)));
|
|
output.push_back(
|
|
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
|
} else {
|
|
output.push_back(
|
|
static_cast<char>(0xF0U | (codepoint >> 18U)));
|
|
output.push_back(static_cast<char>(
|
|
0x80U | ((codepoint >> 12U) & 0x3FU)));
|
|
output.push_back(static_cast<char>(
|
|
0x80U | ((codepoint >> 6U) & 0x3FU)));
|
|
output.push_back(
|
|
static_cast<char>(0x80U | (codepoint & 0x3FU)));
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] std::string parseString()
|
|
{
|
|
expect('"');
|
|
std::string output;
|
|
while (true) {
|
|
const unsigned char value =
|
|
static_cast<unsigned char>(take());
|
|
if (value == '"') {
|
|
return output;
|
|
}
|
|
if (value < 0x20U) {
|
|
fail("control character in string");
|
|
}
|
|
if (value != '\\') {
|
|
output.push_back(static_cast<char>(value));
|
|
continue;
|
|
}
|
|
switch (take()) {
|
|
case '"':
|
|
output.push_back('"');
|
|
break;
|
|
case '\\':
|
|
output.push_back('\\');
|
|
break;
|
|
case '/':
|
|
output.push_back('/');
|
|
break;
|
|
case 'b':
|
|
output.push_back('\b');
|
|
break;
|
|
case 'f':
|
|
output.push_back('\f');
|
|
break;
|
|
case 'n':
|
|
output.push_back('\n');
|
|
break;
|
|
case 'r':
|
|
output.push_back('\r');
|
|
break;
|
|
case 't':
|
|
output.push_back('\t');
|
|
break;
|
|
case 'u':
|
|
{
|
|
std::uint32_t codepoint = unicodeEscape();
|
|
if (codepoint >= 0xD800U &&
|
|
codepoint <= 0xDBFFU) {
|
|
if (take() != '\\' || take() != 'u') {
|
|
fail("missing low surrogate");
|
|
}
|
|
const std::uint32_t low =
|
|
unicodeEscape();
|
|
if (low < 0xDC00U || low > 0xDFFFU) {
|
|
fail("invalid low surrogate");
|
|
}
|
|
codepoint =
|
|
0x10000U +
|
|
((codepoint - 0xD800U) << 10U) +
|
|
low - 0xDC00U;
|
|
} else if (codepoint >= 0xDC00U &&
|
|
codepoint <= 0xDFFFU) {
|
|
fail("unexpected low surrogate");
|
|
}
|
|
appendUtf8(output, codepoint);
|
|
break;
|
|
}
|
|
default:
|
|
fail("invalid string escape");
|
|
}
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] static bool isDigit(const char ch) noexcept
|
|
{
|
|
return ch >= '0' && ch <= '9';
|
|
}
|
|
|
|
[[nodiscard]] std::size_t consumeDigits() noexcept
|
|
{
|
|
const std::size_t begin = position_;
|
|
while (position_ < input_.size() &&
|
|
isDigit(input_[position_])) {
|
|
++position_;
|
|
}
|
|
return position_ - begin;
|
|
}
|
|
|
|
[[nodiscard]] Value parseNumber()
|
|
{
|
|
const std::size_t start = position_;
|
|
if (input_[position_] == '-') {
|
|
++position_;
|
|
}
|
|
if (position_ >= input_.size()) {
|
|
fail("incomplete number");
|
|
}
|
|
if (input_[position_] == '0') {
|
|
++position_;
|
|
if (position_ < input_.size() &&
|
|
isDigit(input_[position_])) {
|
|
fail("leading zero in number");
|
|
}
|
|
} else {
|
|
if (input_[position_] < '1' ||
|
|
input_[position_] > '9') {
|
|
fail("invalid number");
|
|
}
|
|
static_cast<void>(consumeDigits());
|
|
}
|
|
bool isDecimal = false;
|
|
if (position_ < input_.size() && input_[position_] == '.') {
|
|
isDecimal = true;
|
|
++position_;
|
|
if (consumeDigits() == 0U) {
|
|
fail("incomplete fractional part");
|
|
}
|
|
}
|
|
if (position_ < input_.size() &&
|
|
(input_[position_] == 'e' || input_[position_] == 'E')) {
|
|
isDecimal = true;
|
|
++position_;
|
|
if (position_ < input_.size() &&
|
|
(input_[position_] == '+' ||
|
|
input_[position_] == '-')) {
|
|
++position_;
|
|
}
|
|
if (consumeDigits() == 0U) {
|
|
fail("incomplete exponent");
|
|
}
|
|
}
|
|
if (isDecimal) {
|
|
if (!options_.allowDecimals) {
|
|
fail("floating-point numbers are not supported");
|
|
}
|
|
double value{};
|
|
const auto result = std::from_chars(
|
|
input_.data() + start, input_.data() + position_,
|
|
value, std::chars_format::general);
|
|
if (result.ec != std::errc{} ||
|
|
result.ptr != input_.data() + position_ ||
|
|
!std::isfinite(value)) {
|
|
fail("number is out of range");
|
|
}
|
|
return Value{value};
|
|
}
|
|
std::int64_t value{};
|
|
const auto result =
|
|
std::from_chars(input_.data() + start,
|
|
input_.data() + position_, value);
|
|
if (result.ec != std::errc{} ||
|
|
result.ptr != input_.data() + position_) {
|
|
fail("integer is out of range");
|
|
}
|
|
return Value{value};
|
|
}
|
|
|
|
[[nodiscard]] Value parseArray(const std::size_t depth)
|
|
{
|
|
expect('[');
|
|
skipWhitespace();
|
|
Value::Array result;
|
|
if (position_ < input_.size() && input_[position_] == ']') {
|
|
++position_;
|
|
return Value{std::move(result)};
|
|
}
|
|
while (true) {
|
|
result.push_back(parseValue(depth + 1U));
|
|
skipWhitespace();
|
|
const char separator = take();
|
|
if (separator == ']') {
|
|
return Value{std::move(result)};
|
|
}
|
|
if (separator != ',') {
|
|
fail("array requires comma or closing bracket");
|
|
}
|
|
skipWhitespace();
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] Value parseObject(const std::size_t depth)
|
|
{
|
|
expect('{');
|
|
skipWhitespace();
|
|
Value::Object result;
|
|
if (position_ < input_.size() && input_[position_] == '}') {
|
|
++position_;
|
|
return Value{std::move(result)};
|
|
}
|
|
while (true) {
|
|
if (position_ >= input_.size() ||
|
|
input_[position_] != '"') {
|
|
fail("object key must be a string");
|
|
}
|
|
std::string key = parseString();
|
|
skipWhitespace();
|
|
expect(':');
|
|
skipWhitespace();
|
|
auto [iterator, inserted] = result.emplace(
|
|
std::move(key), parseValue(depth + 1U));
|
|
static_cast<void>(iterator);
|
|
if (!inserted) {
|
|
fail("duplicate object key");
|
|
}
|
|
skipWhitespace();
|
|
const char separator = take();
|
|
if (separator == '}') {
|
|
return Value{std::move(result)};
|
|
}
|
|
if (separator != ',') {
|
|
fail("object requires comma or closing brace");
|
|
}
|
|
skipWhitespace();
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] Value parseValue(const std::size_t depth)
|
|
{
|
|
if (depth > options_.maximumDepth ||
|
|
position_ >= input_.size()) {
|
|
fail("nesting limit or unexpected end of input");
|
|
}
|
|
switch (input_[position_]) {
|
|
case '{':
|
|
return parseObject(depth);
|
|
case '[':
|
|
return parseArray(depth);
|
|
case '"':
|
|
return Value{parseString()};
|
|
case 't':
|
|
expectLiteral("true");
|
|
return Value{true};
|
|
case 'f':
|
|
expectLiteral("false");
|
|
return Value{false};
|
|
case 'n':
|
|
expectLiteral("null");
|
|
return Value{nullptr};
|
|
default:
|
|
if (input_[position_] == '-' ||
|
|
isDigit(input_[position_])) {
|
|
return parseNumber();
|
|
}
|
|
fail("invalid value");
|
|
}
|
|
}
|
|
|
|
std::string_view input_;
|
|
ParseOptions options_;
|
|
std::size_t position_{};
|
|
};
|
|
} // namespace Detail
|
|
|
|
[[nodiscard]] inline std::expected<Value, ParseError>
|
|
parse(const std::string_view input, const ParseOptions options)
|
|
{
|
|
try {
|
|
return Detail::Parser{input, options}.parse();
|
|
} catch (const Detail::ParseFailure& failure) {
|
|
return std::unexpected{failure.error};
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] inline std::expected<Value, ParseError>
|
|
parse(const std::string_view input,
|
|
const std::size_t maximumDepth = default_maximum_depth)
|
|
{
|
|
return parse(input, ParseOptions{.maximumDepth = maximumDepth,
|
|
.allowDecimals = true});
|
|
}
|
|
|
|
} // namespace Void::Json::Strict
|