WSL/SLF GitLab Repository

expressions.cc 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*****************************************************************************/
/*  Copyright 2019 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell 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.

   INIshell 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 for more details.

   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "expressions.h"
#include "inishell.h"
#include "src/gui_elements/Atomic.h"

#include "lib/tinyexpr.h"

#include <QRegularExpression>

27
28
namespace expr {

29
30
31
32
33
34
35
36
37
/**
 * @brief Evaluate a string according to special syntax tokens.
 * @details This function checks for arithmetic expressions which are in accordance with SLF software,
 * environment variables, and INI keys, and tries to evaluate them.
 * @param[in] expression The string to check.
 * @param[out] evaluation_success True if the expression could be evaluated.
 * @param[in] needs_prefix If true, check arithmetic expression only if ${{...}}. If false, check without prefix.
 * @return True if the syntax to indicate an expression is correct.
 */
38
39
bool checkExpression(const QString &expression, bool &evaluation_success,
    const std::vector< std::pair<QString, QString> > &substitutions, const bool &needs_prefix)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
	static const QString regex_envvar(R"(\${env:(.+)})");   //ex.: ${env:USER}
	QString regex_expression;
	if (needs_prefix)
		regex_expression = R"(\${{(.+)}})"; //ex.: ${{sin(pi)}}
	else
		regex_expression = R"((.+))"; //enable checks without ${{}
	static const QString regex_inikey(R"(\${(.+)})");       //ex.: ${GENERAL::BUFFER_SIZE}
	static const QRegularExpression rex_envvar(regex_envvar);
	const QRegularExpression rex_expression(regex_expression);
	static const QRegularExpression rex_inikey(regex_inikey);
	const QRegularExpressionMatch match_envvar(rex_envvar.match(expression));
	const QRegularExpressionMatch match_expression(rex_expression.match(expression));
	const QRegularExpressionMatch match_inikey(rex_inikey.match(expression));
54
55
56
	static const QString regex_scientific( R"(-?[\d.]+(?:[Ee]-?\d+)?)" );
	static const QRegularExpression rex_scientific( regex_scientific );
	const QRegularExpressionMatch match_scientific(rex_scientific.match(expression));
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	static const size_t idx_total = 0;
	static const size_t idx_check = 1;

	if (expression.isEmpty()) {
		evaluation_success = false;
		return false;
	}

	if (match_envvar.captured(idx_total) == expression) {
		/* check if environment variable is set */
		const QByteArray envvar( qgetenv(match_envvar.captured(idx_check).toLocal8Bit()) );
		evaluation_success = !envvar.isNull();
		return true;
	} else if (match_expression.captured(idx_total) == expression) {
		/* check if arithmetic expression is valid */
72
73
		QString sub_expr( match_expression.captured(idx_check) );
		expr::doMetaSubstitutions(substitutions, sub_expr);
74
		int status_code;
75
		te_interp(sub_expr.toStdString().c_str(), &status_code);
76
77
78
79
80
81
82
83
		evaluation_success = (status_code == 0);
		return true;
	} else if (match_inikey.captured(idx_total) == expression) {
		/* check if INI key is in XML file */
		const QList<Atomic *> found_panels( getMainWindow()->
		    getPanelsForKey(match_inikey.captured(idx_check)) );
		evaluation_success = !found_panels.isEmpty();
		return true;
84
85
86
	} else if (match_scientific.captured(idx_total) == expression) {
		evaluation_success = true;
		return true;
87
88
89
90
	}
	evaluation_success = false;
	return false;
}
91

92
93
94
95
96
97
/**
 * @brief Parse "substitutions" option into container.
 * @details This is a helper function to translate XML settings to an internal container.
 * @param[in] options XML options for the panel.
 * @return Vector of desired. substitutions and their replacements.
 */
98
99
100
101
102
103
104
std::vector< std::pair<QString, QString> > parseSubstitutions(const QDomNode &options)
{
	/*
	 * Regex substitutions can be given in the XML because some processing elements
	 * will prepare the expression for tinyexpr first, enabling e. g. substitutions.
	 * In order not to hard code a list of substitutions which may differ for different
	 * software, these replacements can be user-set.
105
106
107
108
109
110
	 *
	 * Consider the Particle Filter as an example. The PF knows the variable "xx" but tinyexpr
	 * does not. It is replaced by a numeric value by the PF before passing the expression to
	 * tinyexpr. With substitutions you can mimick this and replace all "xx" with "1" (or
	 * whatever fits your case) so that INIshell can correctly style it as valid - because
	 * it is valid now for tinyexpr.
111
112
113
114
115
116
117
	 */
	std::vector<std::pair<QString, QString>> substitutions;
	for (QDomElement op = options.firstChildElement("substitution"); !op.isNull();
	    op = op.nextSiblingElement("substitution")) {
		std::pair<QString, QString> sub(op.attribute("find"), op.attribute("replace"));
		substitutions.emplace_back(sub);
	}
Michael Reisecker's avatar
Michael Reisecker committed
118
	return substitutions;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
}

/**
 * @brief Mimic MeteoIO's data and meta data substitutions.
 * @details This way, the expression element in INIshell can style it as correct.
 * @param[in] substitutions The requested subsitutions a panel has read and stored from XML.
 * @param[in] expression Expression as entered by the user.
 * Will be transformed so that tinyexpr understands it.
 */
void doMetaSubstitutions(const std::vector<std::pair<QString, QString> > &substitutions, QString &expression)
{
	for (auto &sub : substitutions)
		expression.replace(QRegularExpression(sub.first), sub.second);
}


} //namespace expr