summaryrefslogtreecommitdiffstats
path: root/Source/cmExprParserHelper.cxx
diff options
context:
space:
mode:
authorDaniel Franke <franke@edf-online.de>2018-05-18 19:59:46 (GMT)
committerBrad King <brad.king@kitware.com>2018-06-26 18:15:21 (GMT)
commit7c4c13ffef87d748b896e2c762ad0b2c00afcd31 (patch)
tree912ac1661b74cc13fbc735eb8a38f617c1598de9 /Source/cmExprParserHelper.cxx
parent5b0f73a15a7b49461123a969bbebceff4284df9c (diff)
downloadCMake-7c4c13ffef87d748b896e2c762ad0b2c00afcd31.zip
CMake-7c4c13ffef87d748b896e2c762ad0b2c00afcd31.tar.gz
CMake-7c4c13ffef87d748b896e2c762ad0b2c00afcd31.tar.bz2
math: Reject unexpected expression input explicitly
Switch to C++ exceptions for lexer/parser error handling. Teach the lexer/parser to fail on unexpected input.
Diffstat (limited to 'Source/cmExprParserHelper.cxx')
-rw-r--r--Source/cmExprParserHelper.cxx35
1 files changed, 34 insertions, 1 deletions
diff --git a/Source/cmExprParserHelper.cxx b/Source/cmExprParserHelper.cxx
index c68ebc0..d90d8b9 100644
--- a/Source/cmExprParserHelper.cxx
+++ b/Source/cmExprParserHelper.cxx
@@ -6,6 +6,8 @@
#include <iostream>
#include <sstream>
+#include <stdexcept>
+#include <utility>
int cmExpr_yyparse(yyscan_t yyscanner);
//
@@ -38,7 +40,33 @@ 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);
+ int res;
+
+ try {
+ res = cmExpr_yyparse(yyscanner);
+ if (res != 0) {
+ std::string e = "cannot parse the expression: \"" + InputBuffer + "\": ";
+ e += ErrorString;
+ e += ".";
+ this->SetError(std::move(e));
+ }
+ } catch (std::runtime_error const& fail) {
+ std::string e =
+ "cannot evaluate the expression: \"" + InputBuffer + "\": ";
+ e += fail.what();
+ e += ".";
+ this->SetError(std::move(e));
+ res = 1;
+ } 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));
+ res = 1;
+ } catch (...) {
+ std::string e = "cannot parse the expression: \"" + InputBuffer + "\".";
+ this->SetError(std::move(e));
+ res = 1;
+ }
cmExpr_yylex_destroy(yyscanner);
if (res != 0) {
// str << "CAL_Parser returned: " << res << std::endl;
@@ -90,3 +118,8 @@ void cmExprParserHelper::SetResult(KWIML_INT_int64_t value)
{
this->Result = value;
}
+
+void cmExprParserHelper::SetError(std::string errorString)
+{
+ this->ErrorString = std::move(errorString);
+}