diff options
Diffstat (limited to 'Source/cmExprParserHelper.cxx')
-rw-r--r-- | Source/cmExprParserHelper.cxx | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/Source/cmExprParserHelper.cxx b/Source/cmExprParserHelper.cxx index c3f026a4e..56dfc6c1d 100644 --- a/Source/cmExprParserHelper.cxx +++ b/Source/cmExprParserHelper.cxx @@ -2,25 +2,24 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmExprParserHelper.h" -#include "cmConfigure.h" - -#include "cmExprLexer.h" - #include <iostream> #include <sstream> +#include <stdexcept> +#include <utility> + +#include "cmExprLexer.h" +#include "cmStringAlgorithms.h" int cmExpr_yyparse(yyscan_t yyscanner); // cmExprParserHelper::cmExprParserHelper() { this->FileLine = -1; - this->FileName = CM_NULLPTR; + this->FileName = nullptr; + this->Result = 0; } -cmExprParserHelper::~cmExprParserHelper() -{ - this->CleanupParser(); -} +cmExprParserHelper::~cmExprParserHelper() = default; int cmExprParserHelper::ParseString(const char* str, int verb) { @@ -39,16 +38,31 @@ int cmExprParserHelper::ParseString(const char* str, int verb) yyscan_t yyscanner; cmExpr_yylex_init(&yyscanner); cmExpr_yyset_extra(this, yyscanner); - int res = cmExpr_yyparse(yyscanner); + + try { + int res = cmExpr_yyparse(yyscanner); + if (res != 0) { + std::string e = cmStrCat("cannot parse the expression: \"", InputBuffer, + "\": ", ErrorString, '.'); + this->SetError(std::move(e)); + } + } catch (std::runtime_error const& fail) { + std::string e = cmStrCat("cannot evaluate the expression: \"", InputBuffer, + "\": ", fail.what(), '.'); + this->SetError(std::move(e)); + } catch (std::out_of_range const&) { + std::string e = "cannot evaluate the expression: \"" + InputBuffer + + "\": a numeric value is out of range."; + this->SetError(std::move(e)); + } catch (...) { + std::string e = "cannot parse the expression: \"" + InputBuffer + "\"."; + this->SetError(std::move(e)); + } cmExpr_yylex_destroy(yyscanner); - if (res != 0) { - // str << "CAL_Parser returned: " << res << std::endl; - // std::cerr << "When parsing: [" << str << "]" << std::endl; + if (!this->ErrorString.empty()) { return 0; } - this->CleanupParser(); - if (Verbose) { std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]" << std::endl; @@ -56,10 +70,6 @@ int cmExprParserHelper::ParseString(const char* str, int verb) return 1; } -void cmExprParserHelper::CleanupParser() -{ -} - int cmExprParserHelper::LexInput(char* buf, int maxlen) { // std::cout << "JPLexInput "; @@ -87,7 +97,21 @@ void cmExprParserHelper::Error(const char* str) this->ErrorString = ostr.str(); } -void cmExprParserHelper::SetResult(int value) +void cmExprParserHelper::UnexpectedChar(char c) +{ + unsigned long pos = static_cast<unsigned long>(this->InputBufferPos); + std::ostringstream ostr; + ostr << "Unexpected character in expression at position " << pos << ": " << c + << "\n"; + this->WarningString += ostr.str(); +} + +void cmExprParserHelper::SetResult(KWIML_INT_int64_t value) { this->Result = value; } + +void cmExprParserHelper::SetError(std::string errorString) +{ + this->ErrorString = std::move(errorString); +} |