Merge branch 'main' into devtools

This commit is contained in:
MihailRis 2024-05-08 23:33:02 +03:00
commit 7b651d15b1
9 changed files with 45 additions and 43 deletions

View File

@ -16,13 +16,18 @@ inline double power(double base, int64_t power) {
parsing_error::parsing_error(
std::string message,
std::string filename,
std::string source,
std::string_view filename,
std::string_view source,
uint pos,
uint line,
uint linestart)
: std::runtime_error(message), filename(filename), source(source),
: std::runtime_error(message), filename(filename),
pos(pos), line(line), linestart(linestart) {
size_t end = source.find("\n", linestart);
if (end == std::string::npos) {
end = source.length();
}
source = source.substr(linestart, end-linestart);
}
std::string parsing_error::errorLog() const {
@ -30,11 +35,7 @@ std::string parsing_error::errorLog() const {
uint linepos = pos - linestart;
ss << "parsing error in file '" << filename;
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
size_t end = source.find("\n", linestart);
if (end == std::string::npos) {
end = source.length();
}
ss << source.substr(linestart, end-linestart) << "\n";
ss << source << "\n";
for (uint i = 0; i < linepos; i++) {
ss << " ";
}
@ -43,8 +44,8 @@ std::string parsing_error::errorLog() const {
}
BasicParser::BasicParser(
const std::string& file,
const std::string& source
std::string_view file,
std::string_view source
) : filename(file), source(source) {
}
@ -164,7 +165,7 @@ char BasicParser::peek() {
return source[pos];
}
std::string BasicParser::readUntil(char c) {
std::string_view BasicParser::readUntil(char c) {
int start = pos;
while (hasNext() && source[pos] != c) {
pos++;
@ -185,7 +186,7 @@ std::string BasicParser::parseName() {
while (hasNext() && is_identifier_part(source[pos])) {
pos++;
}
return source.substr(start, pos-start);
return std::string(source.substr(start, pos-start));
}
int64_t BasicParser::parseSimpleInt(int base) {

View File

@ -61,8 +61,8 @@ public:
parsing_error(
std::string message,
std::string filename,
std::string source,
std::string_view filename,
std::string_view source,
uint pos,
uint line,
uint linestart
@ -72,8 +72,8 @@ public:
class BasicParser {
protected:
const std::string& filename;
const std::string& source;
std::string_view filename;
std::string_view source;
uint pos = 0;
uint line = 1;
uint linestart = 0;
@ -95,13 +95,13 @@ protected:
parsing_error error(std::string message);
public:
std::string readUntil(char c);
std::string_view readUntil(char c);
std::string parseName();
bool hasNext();
char peek();
char nextChar();
BasicParser(const std::string& file, const std::string& source);
BasicParser(std::string_view file, std::string_view source);
};
#endif // CODERS_COMMONS_HPP_

View File

@ -11,6 +11,16 @@
using namespace json;
using namespace dynamic;
class Parser : BasicParser {
std::unique_ptr<dynamic::List> parseList();
std::unique_ptr<dynamic::Map> parseObject();
dynamic::Value parseValue();
public:
Parser(std::string_view filename, std::string_view source);
std::unique_ptr<dynamic::Map> parse();
};
inline void newline(
std::stringstream& ss,
bool nice, uint indent,
@ -126,7 +136,7 @@ std::string json::stringify(
return ss.str();
}
Parser::Parser(const std::string& filename, const std::string& source)
Parser::Parser(std::string_view filename, std::string_view source)
: BasicParser(filename, source) {
}

View File

@ -14,16 +14,6 @@
#include <unordered_map>
namespace json {
class Parser : public BasicParser {
std::unique_ptr<dynamic::List> parseList();
std::unique_ptr<dynamic::Map> parseObject();
dynamic::Value parseValue();
public:
Parser(const std::string& filename, const std::string& source);
std::unique_ptr<dynamic::Map> parse();
};
std::unique_ptr<dynamic::Map> parse(const std::string& filename, const std::string& source);
std::unique_ptr<dynamic::Map> parse(const std::string& source);

View File

@ -14,7 +14,7 @@
using namespace toml;
class Reader : public BasicParser {
class Reader : BasicParser {
SettingsHandler& handler;
void skipWhitespace() override {
@ -83,8 +83,8 @@ class Reader : public BasicParser {
public:
Reader(
SettingsHandler& handler,
const std::string& file,
const std::string& source)
std::string_view file,
std::string_view source)
: BasicParser(file, source), handler(handler) {
}

View File

@ -177,7 +177,7 @@ const std::string& Document::getEncoding() const {
return encoding;
}
Parser::Parser(const std::string& filename, const std::string& source)
Parser::Parser(std::string_view filename, std::string_view source)
: BasicParser(filename, source) {
}
@ -250,7 +250,7 @@ std::string Parser::parseText() {
}
nextChar();
}
return source.substr(start, pos-start);
return std::string(source.substr(start, pos-start));
}
inline bool is_xml_identifier_start(char c) {
@ -270,7 +270,7 @@ std::string Parser::parseXMLName() {
while (hasNext() && is_xml_identifier_part(source[pos])) {
pos++;
}
return source.substr(start, pos-start);
return std::string(source.substr(start, pos-start));
}
xmlelement Parser::parseElement() {

View File

@ -108,7 +108,7 @@ namespace xml {
const std::string& getEncoding() const;
};
class Parser : public BasicParser {
class Parser : BasicParser {
xmldocument document;
xmlelement parseOpenTag();
@ -118,7 +118,7 @@ namespace xml {
std::string parseText();
std::string parseXMLName();
public:
Parser(const std::string& filename, const std::string& source);
Parser(std::string_view filename, std::string_view source);
xmldocument parse();
};

View File

@ -35,8 +35,8 @@ const std::string& langs::Lang::getId() const {
return locale;
}
/* Language key-value txt files parser */
class Reader : public BasicParser {
// @brief Language key-value txt files parser
class Reader : BasicParser {
void skipWhitespace() override {
BasicParser::skipWhitespace();
if (hasNext() && source[pos] == '#') {
@ -47,7 +47,7 @@ class Reader : public BasicParser {
}
}
public:
Reader(const std::string& file, const std::string& source) : BasicParser(file, source) {
Reader(std::string_view file, std::string_view source) : BasicParser(file, source) {
}
void read(langs::Lang& lang, std::string prefix) {

View File

@ -46,11 +46,12 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
name = query.substr(0, index);
auto map = std::make_shared<Map>();
BasicParser parser("query for "+name, argstr);
auto filename = "query for "+name;
BasicParser parser(filename, argstr);
while (parser.hasNext()) {
auto key = parser.readUntil('=');
auto key = std::string(parser.readUntil('='));
parser.nextChar();
auto value = parser.readUntil('&');
auto value = std::string(parser.readUntil('&'));
map->put(key, value);
}
args.push_back(map);