BasicParser now uses string_view
This commit is contained in:
parent
65e85f362c
commit
5da2a30326
@ -16,13 +16,18 @@ inline double power(double base, int64_t power) {
|
|||||||
|
|
||||||
parsing_error::parsing_error(
|
parsing_error::parsing_error(
|
||||||
std::string message,
|
std::string message,
|
||||||
std::string filename,
|
std::string_view filename,
|
||||||
std::string source,
|
std::string_view source,
|
||||||
uint pos,
|
uint pos,
|
||||||
uint line,
|
uint line,
|
||||||
uint linestart)
|
uint linestart)
|
||||||
: std::runtime_error(message), filename(filename), source(source),
|
: std::runtime_error(message), filename(filename),
|
||||||
pos(pos), line(line), linestart(linestart) {
|
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 {
|
std::string parsing_error::errorLog() const {
|
||||||
@ -30,11 +35,7 @@ std::string parsing_error::errorLog() const {
|
|||||||
uint linepos = pos - linestart;
|
uint linepos = pos - linestart;
|
||||||
ss << "parsing error in file '" << filename;
|
ss << "parsing error in file '" << filename;
|
||||||
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
||||||
size_t end = source.find("\n", linestart);
|
ss << source << "\n";
|
||||||
if (end == std::string::npos) {
|
|
||||||
end = source.length();
|
|
||||||
}
|
|
||||||
ss << source.substr(linestart, end-linestart) << "\n";
|
|
||||||
for (uint i = 0; i < linepos; i++) {
|
for (uint i = 0; i < linepos; i++) {
|
||||||
ss << " ";
|
ss << " ";
|
||||||
}
|
}
|
||||||
@ -43,8 +44,8 @@ std::string parsing_error::errorLog() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BasicParser::BasicParser(
|
BasicParser::BasicParser(
|
||||||
const std::string& file,
|
std::string_view file,
|
||||||
const std::string& source
|
std::string_view source
|
||||||
) : filename(file), source(source) {
|
) : filename(file), source(source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,7 +165,7 @@ char BasicParser::peek() {
|
|||||||
return source[pos];
|
return source[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string BasicParser::readUntil(char c) {
|
std::string_view BasicParser::readUntil(char c) {
|
||||||
int start = pos;
|
int start = pos;
|
||||||
while (hasNext() && source[pos] != c) {
|
while (hasNext() && source[pos] != c) {
|
||||||
pos++;
|
pos++;
|
||||||
@ -185,7 +186,7 @@ std::string BasicParser::parseName() {
|
|||||||
while (hasNext() && is_identifier_part(source[pos])) {
|
while (hasNext() && is_identifier_part(source[pos])) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
return source.substr(start, pos-start);
|
return std::string(source.substr(start, pos-start));
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t BasicParser::parseSimpleInt(int base) {
|
int64_t BasicParser::parseSimpleInt(int base) {
|
||||||
|
|||||||
@ -61,8 +61,8 @@ public:
|
|||||||
|
|
||||||
parsing_error(
|
parsing_error(
|
||||||
std::string message,
|
std::string message,
|
||||||
std::string filename,
|
std::string_view filename,
|
||||||
std::string source,
|
std::string_view source,
|
||||||
uint pos,
|
uint pos,
|
||||||
uint line,
|
uint line,
|
||||||
uint linestart
|
uint linestart
|
||||||
@ -72,8 +72,8 @@ public:
|
|||||||
|
|
||||||
class BasicParser {
|
class BasicParser {
|
||||||
protected:
|
protected:
|
||||||
const std::string& filename;
|
std::string_view filename;
|
||||||
const std::string& source;
|
std::string_view source;
|
||||||
uint pos = 0;
|
uint pos = 0;
|
||||||
uint line = 1;
|
uint line = 1;
|
||||||
uint linestart = 0;
|
uint linestart = 0;
|
||||||
@ -95,13 +95,13 @@ protected:
|
|||||||
parsing_error error(std::string message);
|
parsing_error error(std::string message);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string readUntil(char c);
|
std::string_view readUntil(char c);
|
||||||
std::string parseName();
|
std::string parseName();
|
||||||
bool hasNext();
|
bool hasNext();
|
||||||
char peek();
|
char peek();
|
||||||
char nextChar();
|
char nextChar();
|
||||||
|
|
||||||
BasicParser(const std::string& file, const std::string& source);
|
BasicParser(std::string_view file, std::string_view source);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CODERS_COMMONS_HPP_
|
#endif // CODERS_COMMONS_HPP_
|
||||||
|
|||||||
@ -11,6 +11,16 @@
|
|||||||
using namespace json;
|
using namespace json;
|
||||||
using namespace dynamic;
|
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(
|
inline void newline(
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
bool nice, uint indent,
|
bool nice, uint indent,
|
||||||
@ -126,7 +136,7 @@ std::string json::stringify(
|
|||||||
return ss.str();
|
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) {
|
: BasicParser(filename, source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,16 +14,6 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace json {
|
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& filename, const std::string& source);
|
||||||
std::unique_ptr<dynamic::Map> parse(const std::string& source);
|
std::unique_ptr<dynamic::Map> parse(const std::string& source);
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
using namespace toml;
|
using namespace toml;
|
||||||
|
|
||||||
class Reader : public BasicParser {
|
class Reader : BasicParser {
|
||||||
SettingsHandler& handler;
|
SettingsHandler& handler;
|
||||||
|
|
||||||
void skipWhitespace() override {
|
void skipWhitespace() override {
|
||||||
@ -83,8 +83,8 @@ class Reader : public BasicParser {
|
|||||||
public:
|
public:
|
||||||
Reader(
|
Reader(
|
||||||
SettingsHandler& handler,
|
SettingsHandler& handler,
|
||||||
const std::string& file,
|
std::string_view file,
|
||||||
const std::string& source)
|
std::string_view source)
|
||||||
: BasicParser(file, source), handler(handler) {
|
: BasicParser(file, source), handler(handler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -177,7 +177,7 @@ const std::string& Document::getEncoding() const {
|
|||||||
return encoding;
|
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) {
|
: BasicParser(filename, source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +250,7 @@ std::string Parser::parseText() {
|
|||||||
}
|
}
|
||||||
nextChar();
|
nextChar();
|
||||||
}
|
}
|
||||||
return source.substr(start, pos-start);
|
return std::string(source.substr(start, pos-start));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_xml_identifier_start(char c) {
|
inline bool is_xml_identifier_start(char c) {
|
||||||
@ -270,7 +270,7 @@ std::string Parser::parseXMLName() {
|
|||||||
while (hasNext() && is_xml_identifier_part(source[pos])) {
|
while (hasNext() && is_xml_identifier_part(source[pos])) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
return source.substr(start, pos-start);
|
return std::string(source.substr(start, pos-start));
|
||||||
}
|
}
|
||||||
|
|
||||||
xmlelement Parser::parseElement() {
|
xmlelement Parser::parseElement() {
|
||||||
|
|||||||
@ -108,7 +108,7 @@ namespace xml {
|
|||||||
const std::string& getEncoding() const;
|
const std::string& getEncoding() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Parser : public BasicParser {
|
class Parser : BasicParser {
|
||||||
xmldocument document;
|
xmldocument document;
|
||||||
|
|
||||||
xmlelement parseOpenTag();
|
xmlelement parseOpenTag();
|
||||||
@ -118,7 +118,7 @@ namespace xml {
|
|||||||
std::string parseText();
|
std::string parseText();
|
||||||
std::string parseXMLName();
|
std::string parseXMLName();
|
||||||
public:
|
public:
|
||||||
Parser(const std::string& filename, const std::string& source);
|
Parser(std::string_view filename, std::string_view source);
|
||||||
|
|
||||||
xmldocument parse();
|
xmldocument parse();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -35,8 +35,8 @@ const std::string& langs::Lang::getId() const {
|
|||||||
return locale;
|
return locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Language key-value txt files parser */
|
// @brief Language key-value txt files parser
|
||||||
class Reader : public BasicParser {
|
class Reader : BasicParser {
|
||||||
void skipWhitespace() override {
|
void skipWhitespace() override {
|
||||||
BasicParser::skipWhitespace();
|
BasicParser::skipWhitespace();
|
||||||
if (hasNext() && source[pos] == '#') {
|
if (hasNext() && source[pos] == '#') {
|
||||||
@ -47,7 +47,7 @@ class Reader : public BasicParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public:
|
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) {
|
void read(langs::Lang& lang, std::string prefix) {
|
||||||
|
|||||||
@ -46,11 +46,12 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
|
|||||||
name = query.substr(0, index);
|
name = query.substr(0, index);
|
||||||
|
|
||||||
auto map = std::make_shared<Map>();
|
auto map = std::make_shared<Map>();
|
||||||
BasicParser parser("query for "+name, argstr);
|
auto filename = "query for "+name;
|
||||||
|
BasicParser parser(filename, argstr);
|
||||||
while (parser.hasNext()) {
|
while (parser.hasNext()) {
|
||||||
auto key = parser.readUntil('=');
|
auto key = std::string(parser.readUntil('='));
|
||||||
parser.nextChar();
|
parser.nextChar();
|
||||||
auto value = parser.readUntil('&');
|
auto value = std::string(parser.readUntil('&'));
|
||||||
map->put(key, value);
|
map->put(key, value);
|
||||||
}
|
}
|
||||||
args.push_back(map);
|
args.push_back(map);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user