From 217eebcefceb6eb3cabe0cc57d0ad7073228f315 Mon Sep 17 00:00:00 2001 From: jorenchik Date: Sun, 3 Nov 2024 11:54:35 +0200 Subject: [PATCH] error if sequence question contains duplicates --- src/transpiler/api.cpp | 9 +++------ src/transpiler/parser.cpp | 28 +++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/transpiler/api.cpp b/src/transpiler/api.cpp index 664ea29..8d26ee5 100644 --- a/src/transpiler/api.cpp +++ b/src/transpiler/api.cpp @@ -99,7 +99,7 @@ Result 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 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 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}; } diff --git a/src/transpiler/parser.cpp b/src/transpiler/parser.cpp index 6aa57da..49b7ac6 100644 --- a/src/transpiler/parser.cpp +++ b/src/transpiler/parser.cpp @@ -1,13 +1,16 @@ #include #include +#include #include #include #include #include #include +#include #include #include + #include "config.h" #include "lexer.h" #include "result.h" @@ -90,7 +93,7 @@ Result ValidateGrammar(const std::vector& 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 parseQuestions(const std::vector& 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 parseQuestions(const std::vector& 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 parseQuestions(const std::vector& 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 parseQuestions(const std::vector& tokens) { question->cooldown = cooldown; question->questionText = cleanContent(questionText); question->section = section; + + auto existingElements = std::set(); for (const auto& elem : questionElements) { Choice choice; choice.answer = cleanContent(elem.content); choice.isCorrect = !elem.isDash; - question->choices.push_back(choice); + + 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) {