mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
stylistic parsing fixes
This commit is contained in:
@@ -546,7 +546,6 @@ Mdem* makeMdem() {
|
|||||||
|
|
||||||
// Aizmuguras saturs.
|
// Aizmuguras saturs.
|
||||||
for (size_t i = 0; i < 20; ++i) {
|
for (size_t i = 0; i < 20; ++i) {
|
||||||
// @Improve: back label pooling
|
|
||||||
QLabel *elBackText = new QLabel();
|
QLabel *elBackText = new QLabel();
|
||||||
mdem->hBack.addWidget(elBackText);
|
mdem->hBack.addWidget(elBackText);
|
||||||
mdem->backLabels.push_back(elBackText);
|
mdem->backLabels.push_back(elBackText);
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ void initTransitions() {
|
|||||||
* Pārbauda, vai tekstvienību saraksts atbilst valodas
|
* Pārbauda, vai tekstvienību saraksts atbilst valodas
|
||||||
* definētām pieļaujamām pārejām.
|
* definētām pieļaujamām pārejām.
|
||||||
* */
|
* */
|
||||||
Result<NoneType> ValidateGrammar(const std::vector<Token>& tokens) {
|
Result<NoneType> validateGrammar(const std::vector<Token>& tokens) {
|
||||||
if (!transitions) {
|
if (!transitions) {
|
||||||
initTransitions();
|
initTransitions();
|
||||||
}
|
}
|
||||||
@@ -125,6 +125,9 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
|||||||
// kļūdas informāciju.
|
// kļūdas informāciju.
|
||||||
auto makeResult = [&questions, &time](std::string error, Token token)
|
auto makeResult = [&questions, &time](std::string error, Token token)
|
||||||
-> Result<ParseInfo> {
|
-> Result<ParseInfo> {
|
||||||
|
if (debug) {
|
||||||
|
std::cout << "SECTION END: PARSER:\n";
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
{ questions, time },
|
{ questions, time },
|
||||||
error,
|
error,
|
||||||
@@ -137,7 +140,7 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
|||||||
if (tokens.size() == 0) {
|
if (tokens.size() == 0) {
|
||||||
return makeResult("", Token());
|
return makeResult("", Token());
|
||||||
}
|
}
|
||||||
auto result = ValidateGrammar(tokens);
|
auto result = validateGrammar(tokens);
|
||||||
if (result.error.length() > 0) {
|
if (result.error.length() > 0) {
|
||||||
return makeResult(
|
return makeResult(
|
||||||
result.error,
|
result.error,
|
||||||
@@ -145,8 +148,6 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
std::cout << "SECTION: PARSER:\n";
|
std::cout << "SECTION: PARSER:\n";
|
||||||
}
|
}
|
||||||
@@ -157,6 +158,7 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
|||||||
return i < tokens.size() && tokens[i].tokenType != TokenType::EndOfFile;
|
return i < tokens.size() && tokens[i].tokenType != TokenType::EndOfFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
// Sākuma datumu un laiku mēģina nolasīt, ja tāds ir norādīts.
|
// Sākuma datumu un laiku mēģina nolasīt, ja tāds ir norādīts.
|
||||||
if (isInBounds(i) && tokens[i].tokenType == TokenType::TextFragment) {
|
if (isInBounds(i) && tokens[i].tokenType == TokenType::TextFragment) {
|
||||||
|
|
||||||
@@ -191,288 +193,289 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
|
|||||||
|
|
||||||
// Pamata parsēšana.
|
// Pamata parsēšana.
|
||||||
while (i < tokens.size()) {
|
while (i < tokens.size()) {
|
||||||
if (tokens[i].tokenType == TokenType::ElementDashStart) {
|
switch (tokens[i].tokenType) {
|
||||||
std::string questionText;
|
case TokenType::ElementDashStart: {
|
||||||
std::vector<QuestionElement> questionElements;
|
std::string questionText;
|
||||||
double cooldown;
|
std::vector<QuestionElement> questionElements;
|
||||||
bool isOrderQuestion = false;
|
double cooldown;
|
||||||
bool isGroupQuestion = false;
|
bool isOrderQuestion = false;
|
||||||
bool isPlusQuestion = false;
|
bool isGroupQuestion = false;
|
||||||
bool hasGroupEncountered = false;
|
bool isPlusQuestion = false;
|
||||||
Token questionStartToken;
|
bool hasGroupEncountered = false;
|
||||||
|
Token questionStartToken;
|
||||||
|
|
||||||
// Šajā momentā ir sagaidāms jautājuma sākums - tam nevar būt secības modifikators.
|
// Šajā momentā ir sagaidāms jautājuma sākums - tam nevar būt secības modifikators.
|
||||||
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
||||||
return makeResult(
|
return makeResult(
|
||||||
"Nevar izmantot secības modifikatoru ('^') jautājuma sākumā",
|
"Nevar izmantot secības modifikatoru ('^') jautājuma sākumā",
|
||||||
tokens[i + 1]);
|
tokens[i + 1]);
|
||||||
}
|
|
||||||
|
|
||||||
// Piefiksē sākumu, lai varētu sniegt labāku kļūdas
|
|
||||||
// atrašanās vietu kļūdas gadījumā.
|
|
||||||
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 skaitlis
|
|
||||||
// ar norādīto vai bez norādītās veselās daļas.
|
|
||||||
static const std::regex decimalNumExp(
|
|
||||||
"^\\d*(\\.\\d+)?$",
|
|
||||||
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
||||||
);
|
|
||||||
if (!std::regex_match(cooldownContent, decimalNumExp)) {
|
|
||||||
throw std::invalid_argument("Nekorekts pārtraukuma formāts");
|
|
||||||
}
|
|
||||||
cooldown = std::stod(cooldownContent);
|
|
||||||
} catch (std::exception e) {
|
|
||||||
return makeResult(
|
|
||||||
"Nekorekts pārtraukums. Pārtraukums ir viens pozitīvs "
|
|
||||||
"decimāls skaitlis ar punktu vai bez punkta",
|
|
||||||
tokens[i + 1]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
questionText = tokens[i + 4].content;
|
|
||||||
hasCooldown = true;
|
|
||||||
} else {
|
|
||||||
cooldown = 0;
|
|
||||||
questionText = tokens[i + 1].content;
|
|
||||||
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)) {
|
|
||||||
|
|
||||||
// Pārbauda, vai nav sastapts cits jautājuma sākums, un noslēdz, ja tas tā ir.
|
|
||||||
if (isInBounds(i + 3) && tokens[i].tokenType == TokenType::ElementDashStart) {
|
|
||||||
|
|
||||||
// Jautājumam var būt un var nebūt pārtraukums - nosaka vai tas būtu.
|
|
||||||
size_t offset;
|
|
||||||
if (tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
|
||||||
offset = tokens[i + 2].tokenType == TokenType::CooldownStart ? 6 : 3;
|
|
||||||
} else {
|
|
||||||
offset = tokens[i + 1].tokenType == TokenType::CooldownStart ? 5 : 2;
|
|
||||||
}
|
|
||||||
if (isInBounds(i + offset) && tokens[i + offset].tokenType == TokenType::QuestionEnd) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offset == 5 && tokens[i + 5].tokenType != TokenType::QuestionEnd) {
|
|
||||||
return makeResult(
|
|
||||||
"Jautājuma elementam nevar būt pārtraukums",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jautājuma elementa noteikšana un ar to saistītās kļūdas.
|
// Piefiksē sākumu, lai varētu sniegt labāku kļūdas
|
||||||
bool isDash;
|
// atrašanās vietu kļūdas gadījumā.
|
||||||
bool isGroup = false;
|
questionStartToken = tokens[i];
|
||||||
bool isOrder = false;
|
|
||||||
|
|
||||||
// Elementa sākums.
|
// Apstrādā pārtraukumu, ja tāds ir.
|
||||||
if (tokens[i].tokenType == TokenType::ElementDashStart) {
|
bool hasCooldown;
|
||||||
isDash = true;
|
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::CooldownStart) {
|
||||||
} else {
|
try {
|
||||||
isDash = false;
|
auto cooldownContent = tokens[i + 2].content;
|
||||||
isPlusQuestion = true;
|
// Pārbauda, vai dotais pārtraukums ir viens skaitlis, kas ir:
|
||||||
if (isGroupQuestion) {
|
// - pozitīvs;
|
||||||
return makeResult(
|
// - viens no: vesels skaitlis vai ar punktu atdalīts skaitlis
|
||||||
"Jautājumos ar grupām nevar būt secības elementu ('+' and ':')",
|
// ar norādīto vai bez norādītās veselās daļas.
|
||||||
tokens[i]
|
static const std::regex decimalNumExp(
|
||||||
);
|
"^\\d*(\\.\\d+)?$",
|
||||||
}
|
std::regex_constants::ECMAScript | std::regex_constants::icase
|
||||||
if (isOrderQuestion) {
|
);
|
||||||
return makeResult(
|
if (!std::regex_match(cooldownContent, decimalNumExp)) {
|
||||||
"Secības jautājumos nevar būt izvēles elementu ('-^' and '+')",
|
throw std::invalid_argument("Nekorekts pārtraukuma formāts");
|
||||||
tokens[i]
|
}
|
||||||
);
|
cooldown = std::stod(cooldownContent);
|
||||||
}
|
} catch (std::exception e) {
|
||||||
}
|
return makeResult(
|
||||||
|
"Nekorekts pārtraukums. Pārtraukums ir viens pozitīvs "
|
||||||
|
"decimāls skaitlis ar punktu vai bez punkta",
|
||||||
|
tokens[i + 1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
questionText = tokens[i + 4].content;
|
||||||
|
hasCooldown = true;
|
||||||
|
} else {
|
||||||
|
cooldown = 0;
|
||||||
|
questionText = tokens[i + 1].content;
|
||||||
|
hasCooldown = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Elementa secības modifikators.
|
int questionStartOffset = hasCooldown ? 5 : 2;
|
||||||
if (isInBounds(i+1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
// Pārbauda, vai jautājums ir nobeigts ar korektu simbolu.
|
||||||
isOrder = true;
|
if (isInBounds(questionStartOffset) &&
|
||||||
isOrderQuestion = true;
|
tokens[i + questionStartOffset].tokenType != TokenType::QuestionEnd) {
|
||||||
if (!isDash) {
|
return makeResult(
|
||||||
return makeResult(
|
"Jautājumu var iesākt tikai ar \">\"",
|
||||||
"Secības jautājumus var izmantot tikai ar svītrām ('-')",
|
tokens[i + questionStartOffset]
|
||||||
tokens[i]
|
);
|
||||||
);
|
}
|
||||||
}
|
i += hasCooldown ? 6 : 3;
|
||||||
if (isGroupQuestion) {
|
|
||||||
return makeResult(
|
|
||||||
"Jautājumos ar grupām nevar būt secības elementu ('-^' and ':')",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (isPlusQuestion) {
|
|
||||||
return makeResult(
|
|
||||||
"Izvēles jautājumos nevar būt secības elementu ('+' and '-^')",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (isInBounds(i + 3) && tokens[i + 3].tokenType == TokenType::MatchGroupEnd) {
|
|
||||||
return makeResult(
|
|
||||||
"Secības jautājumā nevar būt grupas ('-^' and ':')",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Elementa grupas modifikators.
|
// Jautājumu elementu parsēšana.
|
||||||
if (isInBounds(i + 2) && tokens[i + 2].tokenType == TokenType::MatchGroupEnd) {
|
while (isInBounds(i)) {
|
||||||
isGroup = true;
|
|
||||||
isGroupQuestion = true;
|
|
||||||
if (!isDash) {
|
|
||||||
return makeResult(
|
|
||||||
"Grupas jautājumus var definēt tikai ar svītru elementiem ('-')",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!hasGroupEncountered) {
|
|
||||||
if (questionElements.size() > 0) {
|
|
||||||
return makeResult(
|
|
||||||
"Elementi grupas jautājumā nevar eksistēt bez grupas",
|
|
||||||
tokens[i]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hasGroupEncountered = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Izveido atbilstoša veida jautājuma elementu.
|
// Pārbauda, vai nav sastapts cits jautājuma sākums, un noslēdz, ja tas tā ir.
|
||||||
QuestionElement questionElement;
|
if (isInBounds(i + 3) && tokens[i].tokenType == TokenType::ElementDashStart) {
|
||||||
questionElement.isDash = isDash;
|
|
||||||
questionElement.isGroup = isGroup;
|
|
||||||
if (isOrder) {
|
|
||||||
questionElement.content = tokens[i + 2].content;
|
|
||||||
} else {
|
|
||||||
questionElement.content = tokens[i + 1].content;
|
|
||||||
}
|
|
||||||
questionElements.push_back(questionElement);
|
|
||||||
|
|
||||||
// Nākamā elementa atrašanās vieta ir atkarīga no elementu
|
// Jautājumam var būt un var nebūt pārtraukums - nosaka vai tas būtu.
|
||||||
// veida, kas ir sastapts.
|
size_t offset;
|
||||||
size_t offset = 2;
|
if (tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
||||||
if (isOrder) {
|
offset = tokens[i + 2].tokenType == TokenType::CooldownStart ? 6 : 3;
|
||||||
offset += 1;
|
} else {
|
||||||
}
|
offset = tokens[i + 1].tokenType == TokenType::CooldownStart ? 5 : 2;
|
||||||
if (isGroup) {
|
}
|
||||||
offset += 1;
|
if (isInBounds(i + offset) && tokens[i + offset].tokenType == TokenType::QuestionEnd) {
|
||||||
}
|
break;
|
||||||
i += offset;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Izveido jautājuma objektu.
|
if (offset == 5 && tokens[i + 5].tokenType != TokenType::QuestionEnd) {
|
||||||
// Fix: else block - jautājums bez elementiem.
|
return makeResult(
|
||||||
if (questionElements.size() > 0) {
|
"Jautājuma elementam nevar būt pārtraukums",
|
||||||
if (isGroupQuestion) {
|
tokens[i]
|
||||||
auto *question = new GroupQuestion();
|
);
|
||||||
question->cooldown = cooldown;
|
}
|
||||||
question->questionText = cleanContent(questionText);
|
}
|
||||||
|
|
||||||
// Izveido grupas; i - elementu iterators; k - grupu iterators.
|
// Jautājuma elementa noteikšana un ar to saistītās kļūdas.
|
||||||
int32_t k = -1;
|
bool isDash;
|
||||||
for (size_t i = 0; i < questionElements.size(); ++i) {
|
bool isGroup = false;
|
||||||
auto questionElement = questionElements[i];
|
bool isOrder = false;
|
||||||
if (questionElement.isGroup) {
|
|
||||||
++k;
|
|
||||||
auto group = Group();
|
|
||||||
group.name = cleanContent(questionElement.content);
|
|
||||||
question->groups.push_back(group);
|
|
||||||
} else {
|
|
||||||
if (k >= 0) {
|
|
||||||
question->groups[k].elements.push_back(
|
|
||||||
cleanContent(
|
|
||||||
questionElement.content
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
questions.push_back(question);
|
|
||||||
if (debug) {
|
|
||||||
std::cout << question->toString() << "\n";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
auto *question = new MultiElementQuestion();
|
|
||||||
question->cooldown = cooldown;
|
|
||||||
question->questionText = cleanContent(questionText);
|
|
||||||
|
|
||||||
// Izveido vairāku elementu jautājumu.
|
// Elementa sākums.
|
||||||
auto existingElements = std::set<std::string>();
|
if (tokens[i].tokenType == TokenType::ElementDashStart) {
|
||||||
for (const auto& elem : questionElements) {
|
isDash = true;
|
||||||
Choice choice;
|
} else {
|
||||||
choice.answer = cleanContent(elem.content);
|
isDash = false;
|
||||||
choice.isCorrect = !elem.isDash;
|
isPlusQuestion = true;
|
||||||
|
if (isGroupQuestion) {
|
||||||
|
return makeResult(
|
||||||
|
"Jautājumos ar grupām nevar būt secības elementu ('+' and ':')",
|
||||||
|
tokens[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (isOrderQuestion) {
|
||||||
|
return makeResult(
|
||||||
|
"Secības jautājumos nevar būt izvēles elementu ('-^' and '+')",
|
||||||
|
tokens[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Secības elementiem nedrīkst būt vienādi elementi.
|
// Elementa secības modifikators.
|
||||||
if (isOrderQuestion) {
|
if (isInBounds(i+1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
|
||||||
if (existingElements.contains(choice.answer)) {
|
isOrder = true;
|
||||||
return makeResult(
|
isOrderQuestion = true;
|
||||||
"Secības jautājumi atbildes nedrīkst atkārtoties",
|
if (!isDash) {
|
||||||
questionStartToken
|
return makeResult(
|
||||||
);
|
"Secības jautājumus var izmantot tikai ar svītrām ('-')",
|
||||||
} else {
|
tokens[i]
|
||||||
question->choices.push_back(choice);
|
);
|
||||||
existingElements.insert(choice.answer);
|
}
|
||||||
}
|
if (isGroupQuestion) {
|
||||||
} else {
|
return makeResult(
|
||||||
question->choices.push_back(choice);
|
"Jautājumos ar grupām nevar būt secības elementu ('-^' and ':')",
|
||||||
}
|
tokens[i]
|
||||||
}
|
);
|
||||||
questions.push_back(question);
|
}
|
||||||
|
if (isPlusQuestion) {
|
||||||
|
return makeResult(
|
||||||
|
"Izvēles jautājumos nevar būt secības elementu ('+' and '-^')",
|
||||||
|
tokens[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (isInBounds(i + 3) && tokens[i + 3].tokenType == TokenType::MatchGroupEnd) {
|
||||||
|
return makeResult(
|
||||||
|
"Secības jautājumā nevar būt grupas ('-^' and ':')",
|
||||||
|
tokens[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Uzstāda vairāku elementu jautājuma specializēto veidu.
|
// Elementa grupas modifikators.
|
||||||
if (isPlusQuestion) {
|
if (isInBounds(i + 2) && tokens[i + 2].tokenType == TokenType::MatchGroupEnd) {
|
||||||
question->type = MultiElementType::MultiChoice;
|
isGroup = true;
|
||||||
} else if (isOrderQuestion) {
|
isGroupQuestion = true;
|
||||||
question->type = MultiElementType::Order;
|
if (!isDash) {
|
||||||
} else {
|
return makeResult(
|
||||||
question->type = MultiElementType::Regular;
|
"Grupas jautājumus var definēt tikai ar svītru elementiem ('-')",
|
||||||
}
|
tokens[i]
|
||||||
if (debug) {
|
);
|
||||||
std::cout << question->toString() << "\n";
|
}
|
||||||
}
|
if (!hasGroupEncountered) {
|
||||||
}
|
if (questionElements.size() > 0) {
|
||||||
} else {
|
return makeResult(
|
||||||
return makeResult(
|
"Elementi grupas jautājumā nevar eksistēt bez grupas",
|
||||||
"Jautājums nevar būt bez atbildes elementiem",
|
tokens[i]
|
||||||
questionStartToken
|
);
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
} else if (tokens[i].tokenType == TokenType::EndOfFile) {
|
hasGroupEncountered = true;
|
||||||
if (debug) {
|
}
|
||||||
std::cout << "Fails beidzās: EndOfFile\n";
|
|
||||||
}
|
// Izveido atbilstoša veida jautājuma elementu.
|
||||||
break;
|
QuestionElement questionElement;
|
||||||
} else {
|
questionElement.isDash = isDash;
|
||||||
return makeResult(
|
questionElement.isGroup = isGroup;
|
||||||
"Negaidīta tekstvienība",
|
if (isOrder) {
|
||||||
tokens[i]
|
questionElement.content = tokens[i + 2].content;
|
||||||
);
|
} else {
|
||||||
|
questionElement.content = tokens[i + 1].content;
|
||||||
|
}
|
||||||
|
questionElements.push_back(questionElement);
|
||||||
|
|
||||||
|
// Nākamā elementa atrašanās vieta ir atkarīga no elementu
|
||||||
|
// veida, kas ir sastapts.
|
||||||
|
size_t offset = 2;
|
||||||
|
if (isOrder) {
|
||||||
|
offset += 1;
|
||||||
|
}
|
||||||
|
if (isGroup) {
|
||||||
|
offset += 1;
|
||||||
|
}
|
||||||
|
i += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (questionElements.size() > 0) {
|
||||||
|
|
||||||
|
// Izveido jautājuma objektu.
|
||||||
|
if (isGroupQuestion) {
|
||||||
|
auto *question = new GroupQuestion();
|
||||||
|
question->cooldown = cooldown;
|
||||||
|
question->questionText = cleanContent(questionText);
|
||||||
|
|
||||||
|
// Izveido grupas; i - elementu iterators; k - grupu iterators.
|
||||||
|
int32_t k = -1;
|
||||||
|
for (size_t i = 0; i < questionElements.size(); ++i) {
|
||||||
|
auto questionElement = questionElements[i];
|
||||||
|
if (questionElement.isGroup) {
|
||||||
|
++k;
|
||||||
|
auto group = Group();
|
||||||
|
group.name = cleanContent(questionElement.content);
|
||||||
|
question->groups.push_back(group);
|
||||||
|
} else {
|
||||||
|
if (k >= 0) {
|
||||||
|
question->groups[k].elements.push_back(
|
||||||
|
cleanContent(
|
||||||
|
questionElement.content
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
questions.push_back(question);
|
||||||
|
if (debug) {
|
||||||
|
std::cout << question->toString() << "\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto *question = new MultiElementQuestion();
|
||||||
|
question->cooldown = cooldown;
|
||||||
|
question->questionText = cleanContent(questionText);
|
||||||
|
|
||||||
|
// Izveido vairāku elementu jautājumu.
|
||||||
|
auto existingElements = std::set<std::string>();
|
||||||
|
for (const auto& elem : questionElements) {
|
||||||
|
Choice choice;
|
||||||
|
choice.answer = cleanContent(elem.content);
|
||||||
|
choice.isCorrect = !elem.isDash;
|
||||||
|
|
||||||
|
// Secības elementiem nedrīkst būt vienādi elementi.
|
||||||
|
if (isOrderQuestion) {
|
||||||
|
if (existingElements.contains(choice.answer)) {
|
||||||
|
return makeResult(
|
||||||
|
"Secības jautājumi atbildes nedrīkst atkārtoties",
|
||||||
|
questionStartToken
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
question->choices.push_back(choice);
|
||||||
|
existingElements.insert(choice.answer);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
question->choices.push_back(choice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
questions.push_back(question);
|
||||||
|
|
||||||
|
// Uzstāda vairāku elementu jautājuma specializēto veidu.
|
||||||
|
if (isPlusQuestion) {
|
||||||
|
question->type = MultiElementType::MultiChoice;
|
||||||
|
} else if (isOrderQuestion) {
|
||||||
|
question->type = MultiElementType::Order;
|
||||||
|
} else {
|
||||||
|
question->type = MultiElementType::Regular;
|
||||||
|
}
|
||||||
|
if (debug) {
|
||||||
|
std::cout << question->toString() << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return makeResult(
|
||||||
|
"Jautājums nevar būt bez atbildes elementiem",
|
||||||
|
questionStartToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case TokenType::EndOfFile: {
|
||||||
|
return makeResult(
|
||||||
|
"",
|
||||||
|
Token()
|
||||||
|
);
|
||||||
|
} break;
|
||||||
|
default: {
|
||||||
|
return makeResult(
|
||||||
|
"",
|
||||||
|
Token()
|
||||||
|
);
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug) {
|
|
||||||
std::cout << "SECTION END: PARSER:\n";
|
|
||||||
}
|
|
||||||
return makeResult(
|
return makeResult(
|
||||||
"",
|
"",
|
||||||
Token()
|
Token()
|
||||||
|
|||||||
Reference in New Issue
Block a user