error if sequence question contains duplicates

This commit is contained in:
jorenchik
2024-11-03 11:54:35 +02:00
parent 73c37659cb
commit 217eebcefc
2 changed files with 28 additions and 9 deletions

View File

@@ -99,7 +99,7 @@ Result<ParseInfo> transpile(std::string fileContent) {
return {
{},
std::format(
"Lexical analysis error: {}",
"Leksiskās analīzes kļūda: {}",
lexRes.error
),
lexRes.row,
@@ -112,10 +112,7 @@ Result<ParseInfo> transpile(std::string fileContent) {
if (parseRes.error.length() > 0) {
return {
{},
std::format(
"Parsing error: {}",
parseRes.error
),
parseRes.error,
parseRes.row,
parseRes.column
};
@@ -123,7 +120,7 @@ Result<ParseInfo> transpile(std::string fileContent) {
end = std::chrono::high_resolution_clock::now();
if (showTimes) {
std::cout << showTime("Transpilation time") << std::endl;
std::cout << showTime("Transpilācijas laiks") << std::endl;
}
return {questions};
}

View File

@@ -1,13 +1,16 @@
#include <cstdio>
#include <ctime>
#include <exception>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <format>
#include "config.h"
#include "lexer.h"
#include "result.h"
@@ -90,7 +93,7 @@ Result<NoneType> ValidateGrammar(const std::vector<Token>& tokens) {
};
return {
.error=std::format(
"Nekorekta tekstvienību secība: {} nevar būt pirms {}",
"nekorekta tekstvienību secība: \"{}\" nevar būt pirms \"{}\"",
std::string(capitalize(Token::toString(&token.tokenType))),
std::string(capitalize(Token::toString(&nextToken.tokenType)))
),
@@ -156,7 +159,7 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
time = parseToUTCTime(tokens[i].content.c_str(), "%d.%m.%Y %H:%M");
} catch (std::exception e) {
return makeResult(
std::format("Parsēšanas kļūda - {}", e.what()),
e.what(),
tokens[i]
);
}
@@ -171,6 +174,7 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
bool isOrderQuestion = false;
bool isGroupQuestion = false;
bool isPlusQuestion = false;
Token questionStartToken;
// Start element parsing & add to the offset.
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
@@ -179,6 +183,8 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
tokens[i + 1]
);
}
questionStartToken = tokens[i];
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::CooldownStart) {
try {
cooldown = std::stod(tokens[i + 2].content);
@@ -317,13 +323,29 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
question->cooldown = cooldown;
question->questionText = cleanContent(questionText);
question->section = section;
auto existingElements = std::set<std::string>();
for (const auto& elem : questionElements) {
Choice choice;
choice.answer = cleanContent(elem.content);
choice.isCorrect = !elem.isDash;
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);
if (isPlusQuestion) {
question->type = MultiElementType::MultiChoice;
} else if (isOrderQuestion) {