upd: ready to lex the command

This commit is contained in:
2023-12-13 04:42:33 +00:00
parent af3d49c3d3
commit 132eeded36
6 changed files with 139 additions and 6 deletions

45
backend/src/lexer.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "lexer.h"
bool CommandShowLexer(const std::string &command, std::string &ISBN,
std::string &name, std::string &author,
std::string &keyword) {
static std::basic_regex main_pattern(
R"(^ *show(?: +-ISBN=(?:\S{1,20})| +-name=\"(?:[^\s"]{1,60})\"| +-author=\"(?:[^\s"]{1,60})\"| +-keyword=\"(?:[^\s"]{1,60})\")* *$)",
std::regex_constants::optimize);
std::smatch results;
bool has_ISBN = false;
bool has_name = false;
bool has_author = false;
bool has_keyword = false;
ISBN = "";
name = "";
author = "";
keyword = "";
if (std::regex_match(command, results, main_pattern)) {
std::stringstream ss(command);
std::string token;
ss >> token;
while (ss >> token) {
if (token[1] == 'I') {
if (has_ISBN) return false;
has_ISBN = true;
ISBN = token.substr(6);
} else if (token[1] == 'n') {
if (has_name) return false;
has_name = true;
name = token.substr(6 + 1, token.size() - 7 - 1);
} else if (token[1] == 'a') {
if (has_author) return false;
has_author = true;
author = token.substr(8 + 1, token.size() - 9 - 1);
} else if (token[1] == 'k') {
if (has_keyword) return false;
has_keyword = true;
keyword = token.substr(9 + 1, token.size() - 10 - 1);
} else
return false;
}
return true;
} else
return false;
}