|
|
|
@ -3,29 +3,51 @@
|
|
|
|
|
#include <charconv>
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
|
|
|
|
|
#include <components/misc/strings/format.hpp>
|
|
|
|
|
#include <components/misc/strings/lower.hpp>
|
|
|
|
|
|
|
|
|
|
namespace LuaUtil
|
|
|
|
|
{
|
|
|
|
|
namespace
|
|
|
|
|
namespace YamlLoader
|
|
|
|
|
{
|
|
|
|
|
constexpr uint64_t maxDepth = 250;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::load(const std::string& input, const sol::state_view& lua)
|
|
|
|
|
enum class ScalarType
|
|
|
|
|
{
|
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
|
return LuaUtil::YamlLoader::load(rootNodes, lua);
|
|
|
|
|
}
|
|
|
|
|
Boolean,
|
|
|
|
|
Decimal,
|
|
|
|
|
Float,
|
|
|
|
|
Hexadecimal,
|
|
|
|
|
Infinity,
|
|
|
|
|
NotNumber,
|
|
|
|
|
Null,
|
|
|
|
|
Octal,
|
|
|
|
|
String
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sol::object load(const std::vector<YAML::Node>& rootNodes, const sol::state_view& lua);
|
|
|
|
|
|
|
|
|
|
sol::object getNode(const YAML::Node& node, const sol::state_view& lua, uint64_t depth);
|
|
|
|
|
|
|
|
|
|
sol::table getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth);
|
|
|
|
|
|
|
|
|
|
sol::table getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth);
|
|
|
|
|
|
|
|
|
|
ScalarType getScalarType(const YAML::Node& node);
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::load(std::istream& input, const sol::state_view& lua)
|
|
|
|
|
sol::object getScalar(const YAML::Node& node, const sol::state_view& lua);
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void nodeError(const YAML::Node& node, const std::string& message);
|
|
|
|
|
|
|
|
|
|
sol::object load(const std::string& input, const sol::state_view& lua)
|
|
|
|
|
{
|
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
|
return load(rootNodes, lua);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::load(const std::vector<YAML::Node>& rootNodes, const sol::state_view& lua)
|
|
|
|
|
sol::object load(const std::vector<YAML::Node>& rootNodes, const sol::state_view& lua)
|
|
|
|
|
{
|
|
|
|
|
if (rootNodes.empty())
|
|
|
|
|
return sol::nil;
|
|
|
|
@ -42,7 +64,13 @@ namespace LuaUtil
|
|
|
|
|
return documentsTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::getNode(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
sol::object load(std::istream& input, const sol::state_view& lua)
|
|
|
|
|
{
|
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
|
return load(rootNodes, lua);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::object getNode(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
{
|
|
|
|
|
if (depth >= maxDepth)
|
|
|
|
|
throw std::runtime_error("Maximum layers depth exceeded, probably caused by a circular reference");
|
|
|
|
@ -61,7 +89,7 @@ namespace LuaUtil
|
|
|
|
|
nodeError(node, "An unknown YAML node encountered");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::table YamlLoader::getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
sol::table getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
{
|
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
|
|
|
|
|
@ -84,7 +112,7 @@ namespace LuaUtil
|
|
|
|
|
return childTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::table YamlLoader::getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
sol::table getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
{
|
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
|
|
|
|
|
@ -96,7 +124,7 @@ namespace LuaUtil
|
|
|
|
|
return childTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
YamlLoader::ScalarType YamlLoader::getScalarType(const YAML::Node& node)
|
|
|
|
|
ScalarType getScalarType(const YAML::Node& node)
|
|
|
|
|
{
|
|
|
|
|
const auto& tag = node.Tag();
|
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
@ -152,7 +180,7 @@ namespace LuaUtil
|
|
|
|
|
return ScalarType::String;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::getScalar(const YAML::Node& node, const sol::state_view& lua)
|
|
|
|
|
sol::object getScalar(const YAML::Node& node, const sol::state_view& lua)
|
|
|
|
|
{
|
|
|
|
|
auto type = getScalarType(node);
|
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
@ -230,12 +258,12 @@ namespace LuaUtil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void YamlLoader::nodeError(const YAML::Node& node, const std::string& message)
|
|
|
|
|
[[noreturn]] void nodeError(const YAML::Node& node, const std::string& message)
|
|
|
|
|
{
|
|
|
|
|
const auto& mark = node.Mark();
|
|
|
|
|
std::string error = Misc::StringUtils::format(
|
|
|
|
|
" at line=%d column=%d position=%d", mark.line + 1, mark.column + 1, mark.pos + 1);
|
|
|
|
|
throw std::runtime_error(message + error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|