feat: verbose error messages for parsing functions

This commit is contained in:
MihailRis 2026-07-23 19:59:31 +03:00
parent d3eb7a10be
commit 900f118f94
7 changed files with 58 additions and 16 deletions

View file

@ -57,3 +57,7 @@ std::string parsing_error::errorLog() const {
ss << "^";
return ss.str();
}
std::runtime_error parsing_error::toRuntimeError() const {
return std::runtime_error("parsing error: " + errorLog());
}

View file

@ -73,4 +73,6 @@ public:
uint linestart
);
std::string errorLog() const;
std::runtime_error toRuntimeError() const;
};

View file

@ -385,10 +385,22 @@ public:
}
if (c == '(') {
nextChar();
// TODO: replace with array parsing after moving to dv::value's
std::string value = std::string(readUntil(')'));
int depth = 1;
size_t start = pos;
while (hasNext()) {
char c = nextChar();
if (c == '(') {
depth++;
} else if (c == ')') {
depth--;
if (depth == 0) {
goBack(1);
break;
}
}
}
expect(')');
return value;
return std::string(source.substr(start, pos - start - 1));
}
return std::string(readUntilWhitespace());
}

View file

@ -1,4 +1,5 @@
#include "coders/json.hpp"
#include "coders/commons.hpp"
#include "api_lua.hpp"
static int l_json_stringify(lua::State* L) {
@ -12,8 +13,12 @@ static int l_json_stringify(lua::State* L) {
static int l_json_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = json::parse("[string]", string);
return lua::pushvalue(L, element);
try {
auto element = json::parse("[string]", string);
return lua::pushvalue(L, element);
} catch (const parsing_error& err) {
throw err.toRuntimeError();
}
}
const luaL_Reg jsonlib[] = {

View file

@ -1,4 +1,5 @@
#include "coders/toml.hpp"
#include "coders/commons.hpp"
#include "api_lua.hpp"
using namespace scripting;
@ -16,8 +17,12 @@ static int l_toml_stringify(lua::State* L) {
static int l_toml_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = toml::parse("[string]", string);
return lua::pushvalue(L, element);
try {
auto element = toml::parse("[string]", string);
return lua::pushvalue(L, element);
} catch (const parsing_error& err) {
throw err.toRuntimeError();
}
}
const luaL_Reg tomllib[] = {

View file

@ -1,8 +1,8 @@
#include "coders/xml.hpp"
#include "coders/commons.hpp"
#include "api_lua.hpp"
static const char* TAG_ATTR = "#";
static const char* DEFAULT_ROOT_TAG = "root";
static int push_xml(lua::State* L, const xml::xmlelement& elem) {
if (elem.isText()) {
@ -73,17 +73,26 @@ static int l_tostring(lua::State* L) {
static int l_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto document = xml::parse("[string]", string);
return push_xml(L, *document->getRoot());
try {
auto document = xml::parse("[string]", string);
return push_xml(L, *document->getRoot());
} catch (const parsing_error& err) {
throw err.toRuntimeError();
}
}
static int l_parse_vcd(lua::State* L) {
auto string = lua::require_string(L, 1);
auto rootTag = lua::tostring(L, 2);
auto document = xml::parse_vcm(
"[string]", string, rootTag ? rootTag : ""
);
std::unique_ptr<xml::Document> document;
try {
document = xml::parse_vcm(
"[string]", string, rootTag ? rootTag : ""
);
} catch (const parsing_error& err) {
throw err.toRuntimeError();
}
const auto& root = *document->getRoot();
if (rootTag != nullptr) {
return push_xml(L, root);

View file

@ -1,4 +1,5 @@
#include "coders/yaml.hpp"
#include "coders/commons.hpp"
#include "api_lua.hpp"
static int l_stringify(lua::State* L) {
@ -9,8 +10,12 @@ static int l_stringify(lua::State* L) {
static int l_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = yaml::parse("[string]", string);
return lua::pushvalue(L, element);
try {
auto element = yaml::parse("[string]", string);
return lua::pushvalue(L, element);
} catch (const parsing_error& err) {
throw err.toRuntimeError();
}
}
const luaL_Reg yamllib[] = {