|
|
@ -3,239 +3,267 @@
|
|
|
|
#include <charconv>
|
|
|
|
#include <charconv>
|
|
|
|
#include <regex>
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <components/misc/strings/format.hpp>
|
|
|
|
#include <components/misc/strings/format.hpp>
|
|
|
|
#include <components/misc/strings/lower.hpp>
|
|
|
|
#include <components/misc/strings/lower.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace LuaUtil
|
|
|
|
namespace LuaUtil
|
|
|
|
{
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
namespace YamlLoader
|
|
|
|
{
|
|
|
|
{
|
|
|
|
constexpr uint64_t maxDepth = 250;
|
|
|
|
constexpr uint64_t maxDepth = 250;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::load(const std::string& input, const sol::state_view& lua)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
|
|
|
|
return LuaUtil::YamlLoader::load(rootNodes, lua);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::load(std::istream& 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)
|
|
|
|
enum class ScalarType
|
|
|
|
{
|
|
|
|
|
|
|
|
if (rootNodes.empty())
|
|
|
|
|
|
|
|
return sol::nil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rootNodes.size() == 1)
|
|
|
|
|
|
|
|
return getNode(rootNodes[0], lua, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::table documentsTable(lua, sol::create);
|
|
|
|
|
|
|
|
for (const auto& root : rootNodes)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
documentsTable.add(getNode(root, lua, 1));
|
|
|
|
Boolean,
|
|
|
|
}
|
|
|
|
Decimal,
|
|
|
|
|
|
|
|
Float,
|
|
|
|
|
|
|
|
Hexadecimal,
|
|
|
|
|
|
|
|
Infinity,
|
|
|
|
|
|
|
|
NotNumber,
|
|
|
|
|
|
|
|
Null,
|
|
|
|
|
|
|
|
Octal,
|
|
|
|
|
|
|
|
String
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return documentsTable;
|
|
|
|
sol::object load(const std::vector<YAML::Node>& rootNodes, const sol::state_view& lua);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::getNode(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
++depth;
|
|
|
|
sol::table getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth);
|
|
|
|
|
|
|
|
|
|
|
|
if (node.IsMap())
|
|
|
|
sol::table getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth);
|
|
|
|
return getMap(node, lua, depth);
|
|
|
|
|
|
|
|
else if (node.IsSequence())
|
|
|
|
|
|
|
|
return getArray(node, lua, depth);
|
|
|
|
|
|
|
|
else if (node.IsScalar())
|
|
|
|
|
|
|
|
return getScalar(node, lua);
|
|
|
|
|
|
|
|
else if (node.IsNull())
|
|
|
|
|
|
|
|
return sol::nil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "An unknown YAML node encountered");
|
|
|
|
ScalarType getScalarType(const YAML::Node& node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::table YamlLoader::getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
sol::object getScalar(const YAML::Node& node, const sol::state_view& lua);
|
|
|
|
{
|
|
|
|
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& pair : node)
|
|
|
|
[[noreturn]] void nodeError(const YAML::Node& node, const std::string& message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object load(const std::string& input, const sol::state_view& lua)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (pair.first.IsMap())
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered map instead");
|
|
|
|
return load(rootNodes, lua);
|
|
|
|
if (pair.first.IsSequence())
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered array instead");
|
|
|
|
|
|
|
|
if (pair.first.IsNull())
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered null instead");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto key = getNode(pair.first, lua, depth);
|
|
|
|
|
|
|
|
if (key.get_type() == sol::type::number && std::isnan(key.as<double>()))
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered nan instead");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
childTable[key] = getNode(pair.second, lua, depth);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return childTable;
|
|
|
|
sol::object load(const std::vector<YAML::Node>& rootNodes, const sol::state_view& lua)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::table YamlLoader::getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& child : node)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
childTable.add(getNode(child, lua, depth));
|
|
|
|
if (rootNodes.empty())
|
|
|
|
}
|
|
|
|
return sol::nil;
|
|
|
|
|
|
|
|
|
|
|
|
return childTable;
|
|
|
|
if (rootNodes.size() == 1)
|
|
|
|
}
|
|
|
|
return getNode(rootNodes[0], lua, 0);
|
|
|
|
|
|
|
|
|
|
|
|
YamlLoader::ScalarType YamlLoader::getScalarType(const YAML::Node& node)
|
|
|
|
sol::table documentsTable(lua, sol::create);
|
|
|
|
{
|
|
|
|
for (const auto& root : rootNodes)
|
|
|
|
const auto& tag = node.Tag();
|
|
|
|
{
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
documentsTable.add(getNode(root, lua, 1));
|
|
|
|
if (tag == "!")
|
|
|
|
}
|
|
|
|
return ScalarType::String;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Note that YAML allows to explicitely specify a scalar type via tag (e.g. "!!bool"), but it makes no
|
|
|
|
return documentsTable;
|
|
|
|
// sense in Lua:
|
|
|
|
}
|
|
|
|
// 1. Both integers and floats use the "number" type prior to Lua 5.3
|
|
|
|
|
|
|
|
// 2. Strings can be quoted, which is more readable than "!!str"
|
|
|
|
|
|
|
|
// 3. Most of possible conversions are invalid or their result is unclear
|
|
|
|
|
|
|
|
// So ignore this feature for now.
|
|
|
|
|
|
|
|
if (tag != "?")
|
|
|
|
|
|
|
|
nodeError(node, "An invalid tag'" + tag + "' encountered");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (value.empty())
|
|
|
|
|
|
|
|
return ScalarType::Null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve type according to YAML 1.2 Core Schema (see https://yaml.org/spec/1.2.2/#103-core-schema)
|
|
|
|
|
|
|
|
static const std::regex boolRegex("true|True|TRUE|false|False|FALSE", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), boolRegex))
|
|
|
|
|
|
|
|
return ScalarType::Boolean;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex decimalRegex("[-+]?[0-9]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), decimalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Decimal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex floatRegex(
|
|
|
|
|
|
|
|
"[-+]?([.][0-9]+|[0-9]+([.][0-9]*)?)([eE][-+]?[0-9]+)?", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), floatRegex))
|
|
|
|
|
|
|
|
return ScalarType::Float;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex octalRegex("0o[0-7]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), octalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Octal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex hexdecimalRegex("0x[0-9a-fA-F]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), hexdecimalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Hexadecimal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex infinityRegex("[-+]?([.]inf|[.]Inf|[.]INF)", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), infinityRegex))
|
|
|
|
|
|
|
|
return ScalarType::Infinity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex nanRegex("[.]nan|[.]NaN|[.]NAN", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), nanRegex))
|
|
|
|
|
|
|
|
return ScalarType::NotNumber;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex nullRegex("null|Null|NULL|~", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), nullRegex))
|
|
|
|
|
|
|
|
return ScalarType::Null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ScalarType::String;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object YamlLoader::getScalar(const YAML::Node& node, const sol::state_view& lua)
|
|
|
|
sol::object load(std::istream& input, const sol::state_view& lua)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto type = getScalarType(node);
|
|
|
|
std::vector<YAML::Node> rootNodes = YAML::LoadAll(input);
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
return load(rootNodes, lua);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
sol::object getNode(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case ScalarType::Null:
|
|
|
|
if (depth >= maxDepth)
|
|
|
|
|
|
|
|
throw std::runtime_error("Maximum layers depth exceeded, probably caused by a circular reference");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
++depth;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (node.IsMap())
|
|
|
|
|
|
|
|
return getMap(node, lua, depth);
|
|
|
|
|
|
|
|
else if (node.IsSequence())
|
|
|
|
|
|
|
|
return getArray(node, lua, depth);
|
|
|
|
|
|
|
|
else if (node.IsScalar())
|
|
|
|
|
|
|
|
return getScalar(node, lua);
|
|
|
|
|
|
|
|
else if (node.IsNull())
|
|
|
|
return sol::nil;
|
|
|
|
return sol::nil;
|
|
|
|
case ScalarType::String:
|
|
|
|
|
|
|
|
return sol::make_object<std::string>(lua, value);
|
|
|
|
|
|
|
|
case ScalarType::NotNumber:
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, std::nan(""));
|
|
|
|
|
|
|
|
case ScalarType::Infinity:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!value.empty() && value[0] == '-')
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, -std::numeric_limits<double>::infinity());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, std::numeric_limits<double>::infinity());
|
|
|
|
nodeError(node, "An unknown YAML node encountered");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ScalarType::Boolean:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Misc::StringUtils::lowerCase(value) == "true")
|
|
|
|
|
|
|
|
return sol::make_object<bool>(lua, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Misc::StringUtils::lowerCase(value) == "false")
|
|
|
|
sol::table getMap(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
return sol::make_object<bool>(lua, false);
|
|
|
|
{
|
|
|
|
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a boolean value '" + value + "'");
|
|
|
|
for (const auto& pair : node)
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Decimal:
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
if (pair.first.IsMap())
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered map instead");
|
|
|
|
|
|
|
|
if (pair.first.IsSequence())
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered array instead");
|
|
|
|
|
|
|
|
if (pair.first.IsNull())
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered null instead");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto key = getNode(pair.first, lua, depth);
|
|
|
|
|
|
|
|
if (key.get_type() == sol::type::number && std::isnan(key.as<double>()))
|
|
|
|
|
|
|
|
nodeError(pair.first, "Only scalar nodes can be used as keys, encountered nan instead");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
childTable[key] = getNode(pair.second, lua, depth);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// std::from_chars does not support "+" sign
|
|
|
|
return childTable;
|
|
|
|
if (!value.empty() && value[0] == '+')
|
|
|
|
}
|
|
|
|
++offset;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
sol::table getArray(const YAML::Node& node, const sol::state_view& lua, uint64_t depth)
|
|
|
|
const auto status = std::from_chars(value.data() + offset, value.data() + value.size(), result);
|
|
|
|
{
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
sol::table childTable(lua, sol::create);
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a decimal value '" + value + "'");
|
|
|
|
for (const auto& child : node)
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Float:
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Not all compilers support std::from_chars for floats
|
|
|
|
childTable.add(getNode(child, lua, depth));
|
|
|
|
double result = 0.0;
|
|
|
|
|
|
|
|
bool success = YAML::convert<double>::decode(node, result);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a float value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ScalarType::Hexadecimal:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
const auto status = std::from_chars(value.data() + 2, value.data() + value.size(), result, 16);
|
|
|
|
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a hexadecimal value '" + value + "'");
|
|
|
|
return childTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ScalarType::Octal:
|
|
|
|
|
|
|
|
{
|
|
|
|
ScalarType getScalarType(const YAML::Node& node)
|
|
|
|
int result = 0;
|
|
|
|
{
|
|
|
|
const auto status = std::from_chars(value.data() + 2, value.data() + value.size(), result, 8);
|
|
|
|
const auto& tag = node.Tag();
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
if (tag == "!")
|
|
|
|
|
|
|
|
return ScalarType::String;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Note that YAML allows to explicitely specify a scalar type via tag (e.g. "!!bool"), but it makes no
|
|
|
|
|
|
|
|
// sense in Lua:
|
|
|
|
|
|
|
|
// 1. Both integers and floats use the "number" type prior to Lua 5.3
|
|
|
|
|
|
|
|
// 2. Strings can be quoted, which is more readable than "!!str"
|
|
|
|
|
|
|
|
// 3. Most of possible conversions are invalid or their result is unclear
|
|
|
|
|
|
|
|
// So ignore this feature for now.
|
|
|
|
|
|
|
|
if (tag != "?")
|
|
|
|
|
|
|
|
nodeError(node, "An invalid tag'" + tag + "' encountered");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (value.empty())
|
|
|
|
|
|
|
|
return ScalarType::Null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve type according to YAML 1.2 Core Schema (see https://yaml.org/spec/1.2.2/#103-core-schema)
|
|
|
|
|
|
|
|
static const std::regex boolRegex("true|True|TRUE|false|False|FALSE", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), boolRegex))
|
|
|
|
|
|
|
|
return ScalarType::Boolean;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex decimalRegex("[-+]?[0-9]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), decimalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Decimal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex floatRegex(
|
|
|
|
|
|
|
|
"[-+]?([.][0-9]+|[0-9]+([.][0-9]*)?)([eE][-+]?[0-9]+)?", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), floatRegex))
|
|
|
|
|
|
|
|
return ScalarType::Float;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex octalRegex("0o[0-7]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), octalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Octal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex hexdecimalRegex("0x[0-9a-fA-F]+", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), hexdecimalRegex))
|
|
|
|
|
|
|
|
return ScalarType::Hexadecimal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex infinityRegex("[-+]?([.]inf|[.]Inf|[.]INF)", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), infinityRegex))
|
|
|
|
|
|
|
|
return ScalarType::Infinity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex nanRegex("[.]nan|[.]NaN|[.]NAN", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), nanRegex))
|
|
|
|
|
|
|
|
return ScalarType::NotNumber;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const std::regex nullRegex("null|Null|NULL|~", std::regex_constants::extended);
|
|
|
|
|
|
|
|
if (std::regex_match(node.Scalar(), nullRegex))
|
|
|
|
|
|
|
|
return ScalarType::Null;
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read an octal value '" + value + "'");
|
|
|
|
return ScalarType::String;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sol::object getScalar(const YAML::Node& node, const sol::state_view& lua)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
auto type = getScalarType(node);
|
|
|
|
|
|
|
|
const auto& value = node.Scalar();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case ScalarType::Null:
|
|
|
|
|
|
|
|
return sol::nil;
|
|
|
|
|
|
|
|
case ScalarType::String:
|
|
|
|
|
|
|
|
return sol::make_object<std::string>(lua, value);
|
|
|
|
|
|
|
|
case ScalarType::NotNumber:
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, std::nan(""));
|
|
|
|
|
|
|
|
case ScalarType::Infinity:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!value.empty() && value[0] == '-')
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, -std::numeric_limits<double>::infinity());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, std::numeric_limits<double>::infinity());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Boolean:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Misc::StringUtils::lowerCase(value) == "true")
|
|
|
|
|
|
|
|
return sol::make_object<bool>(lua, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Misc::StringUtils::lowerCase(value) == "false")
|
|
|
|
|
|
|
|
return sol::make_object<bool>(lua, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a boolean value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Decimal:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// std::from_chars does not support "+" sign
|
|
|
|
|
|
|
|
if (!value.empty() && value[0] == '+')
|
|
|
|
|
|
|
|
++offset;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
const auto status = std::from_chars(value.data() + offset, value.data() + value.size(), result);
|
|
|
|
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a decimal value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Float:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Not all compilers support std::from_chars for floats
|
|
|
|
|
|
|
|
double result = 0.0;
|
|
|
|
|
|
|
|
bool success = YAML::convert<double>::decode(node, result);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
|
|
return sol::make_object<double>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a float value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Hexadecimal:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
const auto status = std::from_chars(value.data() + 2, value.data() + value.size(), result, 16);
|
|
|
|
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read a hexadecimal value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScalarType::Octal:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
const auto status = std::from_chars(value.data() + 2, value.data() + value.size(), result, 8);
|
|
|
|
|
|
|
|
if (status.ec == std::errc())
|
|
|
|
|
|
|
|
return sol::make_object<int>(lua, result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nodeError(node, "Can not read an octal value '" + value + "'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
nodeError(node, "An unknown scalar '" + value + "' encountered");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
|
|
|
|
nodeError(node, "An unknown scalar '" + value + "' encountered");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[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();
|
|
|
|
const auto& mark = node.Mark();
|
|
|
|
std::string error = Misc::StringUtils::format(
|
|
|
|
std::string error = Misc::StringUtils::format(
|
|
|
|
" at line=%d column=%d position=%d", mark.line + 1, mark.column + 1, mark.pos + 1);
|
|
|
|
" at line=%d column=%d position=%d", mark.line + 1, mark.column + 1, mark.pos + 1);
|
|
|
|
throw std::runtime_error(message + error);
|
|
|
|
throw std::runtime_error(message + error);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|