From 7cc7bd5a2e539bb127d92b06a4d12a64325196ba Mon Sep 17 00:00:00 2001 From: Thomas Ubensee <34603111+tomuben@users.noreply.github.com> Date: Tue, 22 Oct 2024 07:19:50 -0300 Subject: [PATCH] #988: Improved error message from CTPG parser. --- .../ctpg/script_option_lines_ctpg.cc | 17 +++++++++----- .../ctpg/test/script_option_lines_test.cpp | 22 ++++++++++++++++++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/exaudfclient/base/script_options_parser/ctpg/script_option_lines_ctpg.cc b/exaudfclient/base/script_options_parser/ctpg/script_option_lines_ctpg.cc index ad6501c8..b17dddab 100644 --- a/exaudfclient/base/script_options_parser/ctpg/script_option_lines_ctpg.cc +++ b/exaudfclient/base/script_options_parser/ctpg/script_option_lines_ctpg.cc @@ -190,9 +190,7 @@ void parse(std::string&& code, options_type& result) { result = res.value(); } else { - std::stringstream ss; - ss << "Error parsing script options: " << error_buffer.str(); - throw OptionParserException(ss.str()); + throw OptionParserException(error_buffer.str()); } } @@ -238,9 +236,18 @@ void parseOptions(const std::string& code, options_map_t & result) { std::optional currentLinePositions = getNextLine(current_pos, code); while (currentLinePositions) { - std::string line = code.substr(currentLinePositions->mStartPos, currentLinePositions->mEndPos); + std::string line = code.substr(currentLinePositions->mStartPos, + currentLinePositions->mEndPos - currentLinePositions->mStartPos); options_type parser_result; - ParserInternals::parse(std::move(line), parser_result); + try { + ParserInternals::parse(std::move(line), parser_result); + } catch(OptionParserException& ex) { + const std::string::difference_type lineNumber = + std::count(code.begin(), code.begin()+currentLinePositions->mStartPos, '\n'); + std::stringstream ss; + ss << "Error parsing script options at line " << lineNumber << ": " << ex.what(); + throw OptionParserException(ss.str()); + } for (const auto & option: parser_result) { ScriptOption entry = { .value = option.value, diff --git a/exaudfclient/base/script_options_parser/ctpg/test/script_option_lines_test.cpp b/exaudfclient/base/script_options_parser/ctpg/test/script_option_lines_test.cpp index 76ca72b0..ed22df95 100644 --- a/exaudfclient/base/script_options_parser/ctpg/test/script_option_lines_test.cpp +++ b/exaudfclient/base/script_options_parser/ctpg/test/script_option_lines_test.cpp @@ -84,7 +84,27 @@ TEST(ScriptOptionLinesTest, need_option_termination_character) { catch( const OptionParserException& e ) { // and this tests that it has the correct message - EXPECT_STREQ( e.what(), "Error parsing script options: [1:17] PARSE: Syntax error: Unexpected ''\n"); + EXPECT_STREQ( e.what(), "Error parsing script options at line 0: [1:17] PARSE: Syntax error: Unexpected ''\n"); + throw; + } + }, OptionParserException ); +} + +TEST(ScriptOptionLinesTest, need_option_termination_character_second_line) { + const std::string code = + "%optionA myoption;\n" + "%optionB myoption\n" + "\nmycode"; + options_map_t result; + EXPECT_THROW({ + try + { + parseOptions(code, result); + } + catch( const OptionParserException& e ) + { + // and this tests that it has the correct message + EXPECT_STREQ( e.what(), "Error parsing script options at line 1: [1:18] PARSE: Syntax error: Unexpected ''\n"); throw; } }, OptionParserException );