mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-25 22:50:25 -05:00
#13603 Replace bool/errorText with std::expected in ExpressionParser
evaluate() and expandIfStatementsAndEvaluate() now return std::expected<void, QString>: the error message is always part of the return value rather than an optional out-parameter, making it impossible for callers to silently discard errors. Update RimGridCalculation and RimSummaryCalculation to use evaluateResult.error() instead of a separate errorText variable.
This commit is contained in:
1 parent
8bbd0d2551
commit
ab1cd9bb22
6 files changed
+54
-66
No files matched your search
+22
-27
@@ -1,17 +1,17 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017- Statoil ASA
|
||||
//
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -21,59 +21,54 @@
|
||||
#include "ExpressionParserImpl.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
ExpressionParser::ExpressionParser()
|
||||
{
|
||||
m_expressionParserImpl = std::unique_ptr<ExpressionParserImpl>(new ExpressionParserImpl);
|
||||
m_expressionParserImpl = std::unique_ptr<ExpressionParserImpl>( new ExpressionParserImpl );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
ExpressionParser::~ExpressionParser()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::vector<QString> ExpressionParser::detectReferencedVariables(const QString& expression)
|
||||
std::vector<QString> ExpressionParser::detectReferencedVariables( const QString& expression )
|
||||
{
|
||||
return ExpressionParserImpl::detectReferencedVariables(expression);
|
||||
return ExpressionParserImpl::detectReferencedVariables( expression );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void ExpressionParser::assignVector(const QString& variableName, std::vector<double>& vector)
|
||||
void ExpressionParser::assignVector( const QString& variableName, std::vector<double>& vector )
|
||||
{
|
||||
m_expressionParserImpl->assignVector(variableName, vector);
|
||||
m_expressionParserImpl->assignVector( variableName, vector );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool ExpressionParser::evaluate(const QString& expressionText, QString* errorText)
|
||||
std::expected<void, QString> ExpressionParser::evaluate( const QString& expressionText )
|
||||
{
|
||||
return m_expressionParserImpl->evaluate(expressionText, errorText);
|
||||
return m_expressionParserImpl->evaluate( expressionText );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool ExpressionParser::expandIfStatementsAndEvaluate(const QString& expressionText, QString* errorText /*= nullptr*/)
|
||||
std::expected<void, QString> ExpressionParser::expandIfStatementsAndEvaluate( const QString& expressionText )
|
||||
{
|
||||
if (expressionText.contains("if", Qt::CaseInsensitive))
|
||||
if ( expressionText.contains( "if", Qt::CaseInsensitive ) )
|
||||
{
|
||||
QString expandedExpressionText = ExpressionParserImpl::expandIfStatements(expressionText);
|
||||
|
||||
return m_expressionParserImpl->evaluate(expandedExpressionText, errorText);
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_expressionParserImpl->evaluate(expressionText, errorText);
|
||||
QString expanded = ExpressionParserImpl::expandIfStatements( expressionText );
|
||||
return m_expressionParserImpl->evaluate( expanded );
|
||||
}
|
||||
|
||||
return m_expressionParserImpl->evaluate( expressionText );
|
||||
}
|
||||
+8
-5
@@ -20,13 +20,14 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class ExpressionParserImpl;
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
///
|
||||
//==================================================================================================
|
||||
class ExpressionParser
|
||||
{
|
||||
@@ -34,11 +35,13 @@ public:
|
||||
ExpressionParser();
|
||||
~ExpressionParser();
|
||||
|
||||
static std::vector<QString> detectReferencedVariables(const QString& expression);
|
||||
static std::vector<QString> detectReferencedVariables( const QString& expression );
|
||||
|
||||
void assignVector(const QString& variableName, std::vector<double>& vector);
|
||||
bool evaluate(const QString& expressionText, QString* errorText = nullptr);
|
||||
bool expandIfStatementsAndEvaluate(const QString& expressionText, QString* errorText = nullptr);
|
||||
void assignVector( const QString& variableName, std::vector<double>& vector );
|
||||
|
||||
// Returns an empty expected on success, or an unexpected containing the error message on failure.
|
||||
std::expected<void, QString> evaluate( const QString& expressionText );
|
||||
std::expected<void, QString> expandIfStatementsAndEvaluate( const QString& expressionText );
|
||||
|
||||
private:
|
||||
std::unique_ptr<ExpressionParserImpl> m_expressionParserImpl;
|
||||
|
||||
+7
-17
@@ -76,32 +76,22 @@ void ExpressionParserImpl::assignVector(const QString& variableName, std::vector
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool ExpressionParserImpl::evaluate(const QString& expressionText, QString* errorText)
|
||||
std::expected<void, QString> ExpressionParserImpl::evaluate( const QString& expressionText )
|
||||
{
|
||||
expression_t expression;
|
||||
|
||||
expression.register_symbol_table(m_symbol_table);
|
||||
expression.register_symbol_table( m_symbol_table );
|
||||
|
||||
parser_t parser;
|
||||
if (!parser.compile(expressionText.toStdString(), expression))
|
||||
{
|
||||
if (errorText)
|
||||
{
|
||||
*errorText = parserErrorText(parser);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
if ( !parser.compile( expressionText.toStdString(), expression ) )
|
||||
return std::unexpected( parserErrorText( parser ) );
|
||||
|
||||
// Trigger evaluation
|
||||
expression.value();
|
||||
|
||||
if (errorText)
|
||||
{
|
||||
*errorText = parserErrorText(parser);
|
||||
}
|
||||
if ( parser.error_count() > 0 )
|
||||
return std::unexpected( parserErrorText( parser ) );
|
||||
|
||||
return (parser.error_count() == 0);
|
||||
return {};
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
+11
-9
@@ -22,27 +22,29 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <expected>
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
///
|
||||
//==================================================================================================
|
||||
class ExpressionParserImpl
|
||||
{
|
||||
typedef exprtk::symbol_table<double> symbol_table_t;
|
||||
typedef exprtk::expression<double> expression_t;
|
||||
typedef exprtk::parser<double> parser_t;
|
||||
typedef exprtk::expression<double> expression_t;
|
||||
typedef exprtk::parser<double> parser_t;
|
||||
|
||||
public:
|
||||
ExpressionParserImpl();
|
||||
|
||||
static std::vector<QString> detectReferencedVariables(const QString& expression);
|
||||
static std::vector<QString> detectReferencedVariables( const QString& expression );
|
||||
|
||||
void assignVector(const QString& variableName, std::vector<double>& vector);
|
||||
bool evaluate(const QString& expressionText, QString* errorText = nullptr);
|
||||
|
||||
static QString expandIfStatements(const QString& expressionText);
|
||||
void assignVector( const QString& variableName, std::vector<double>& vector );
|
||||
std::expected<void, QString> evaluate( const QString& expressionText );
|
||||
|
||||
static QString expandIfStatements( const QString& expressionText );
|
||||
|
||||
private:
|
||||
QString parserErrorText(parser_t& parser);
|
||||
QString parserErrorText( parser_t& parser );
|
||||
|
||||
private:
|
||||
symbol_table_t m_symbol_table;
|
||||
|
||||
Reference in new issue
Block a user