mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
parsing improvements from grammar
This commit is contained in:
@@ -153,8 +153,23 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
||||
|
||||
// Sākuma datumu un laiku mēģina nolasīt, ja tāds ir norādīts.
|
||||
if (isInBounds(i) && tokens[i].tokenType == TokenType::TextFragment) {
|
||||
|
||||
// Pārbauda datuma un laika formātu.
|
||||
auto datetimeContent = cleanContent(tokens[i].content);
|
||||
static const std::regex datetimeExp(
|
||||
"^\\d\\d?\\.\\d\\d?\\.\\d\\d\\d\\d \\d\\d?:\\d\\d?$",
|
||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
||||
);
|
||||
if (!std::regex_match(datetimeContent, datetimeExp)) {
|
||||
return makeResult(
|
||||
"Nekorekts datuma un laika formāts",
|
||||
tokens[i]
|
||||
);
|
||||
}
|
||||
|
||||
// Parsē datumu un laiku.
|
||||
const std::string format = "%d.%m.%Y %H:%M";
|
||||
const std::string datetime = tokens[i].content.c_str();
|
||||
const std::string datetime = datetimeContent.c_str();
|
||||
std::tm tm = {};
|
||||
std::istringstream ss(datetime);
|
||||
ss >> std::get_time(&tm, format.c_str());
|
||||
@@ -192,12 +207,13 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
||||
questionStartToken = tokens[i];
|
||||
|
||||
// Apstrādā pārtraukumu, ja tāds ir.
|
||||
bool hasCooldown;
|
||||
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::CooldownStart) {
|
||||
try {
|
||||
auto cooldownContent = tokens[i + 2].content;
|
||||
// Pārbauda, vai dotais pārtraukums ir viens skaitlis, kas ir:
|
||||
// - pozitīvs;
|
||||
// - viens no: vesels skaitlis vai ar punktu atdalīts skatilis
|
||||
// - viens no: vesels skaitlis vai ar punktu atdalīts skaitlis
|
||||
// ar norādīto vai bez norādītās veselās daļas.
|
||||
static const std::regex decimalNumExp(
|
||||
"^\\d*(\\.\\d+)?$",
|
||||
@@ -215,13 +231,24 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
||||
);
|
||||
}
|
||||
questionText = tokens[i + 4].content;
|
||||
i += 6;
|
||||
hasCooldown = true;
|
||||
} else {
|
||||
cooldown = 0;
|
||||
questionText = tokens[i + 1].content;
|
||||
i += 3;
|
||||
hasCooldown = false;
|
||||
}
|
||||
|
||||
int questionStartOffset = hasCooldown ? 5 : 2;
|
||||
// Pārbauda, vai jautājums ir nobeigts ar korektu simbolu.
|
||||
if (isInBounds(questionStartOffset) &&
|
||||
tokens[i + questionStartOffset].tokenType != TokenType::QuestionEnd) {
|
||||
return makeResult(
|
||||
"Jautājumu var iesākt tikai ar \">\"",
|
||||
tokens[i + questionStartOffset]
|
||||
);
|
||||
}
|
||||
i += hasCooldown ? 6 : 3;
|
||||
|
||||
// Jautājumu elementu parsēšana.
|
||||
while (isInBounds(i)) {
|
||||
|
||||
@@ -352,7 +379,7 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
||||
if (isGroupQuestion) {
|
||||
auto *question = new GroupQuestion();
|
||||
question->cooldown = cooldown;
|
||||
question->questionText = questionText;
|
||||
question->questionText = cleanContent(questionText);
|
||||
|
||||
// Izveido grupas; i - elementu iterators; k - grupu iterators.
|
||||
int32_t k = -1;
|
||||
|
||||
Reference in New Issue
Block a user