KiCad PCB EDA Suite
Loading...
Searching...
No Matches
text_eval_wrapper.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <kicommon.h>
23#include <wx/string.h>
24#include <unordered_map>
25#include <string>
26#include <memory>
27#include <variant>
28#include <vector>
29#include <functional>
30
31// Include EDA units support
32#include <eda_units.h>
33
34// Include the parser types
36
37// Forward declaration for NUMERIC_EVALUATOR compatibility
39
76 */
78{
79public:
80 // Callback function type for dynamic variable resolution
81 using VAR_CALLBACK = std::function<calc_parser::RESULT<calc_parser::VALUE>(const std::string& aVariableName)>;
82
83public:
88 explicit EXPRESSION_EVALUATOR( bool aClearVariablesOnEvaluate = false );
89
95 explicit EXPRESSION_EVALUATOR( EDA_UNITS aUnits, bool aClearVariablesOnEvaluate = false );
96
102 explicit EXPRESSION_EVALUATOR( VAR_CALLBACK aVariableCallback, bool aClearVariablesOnEvaluate = false );
103
110 explicit EXPRESSION_EVALUATOR( EDA_UNITS aUnits, VAR_CALLBACK aVariableCallback,
111 bool aClearVariablesOnEvaluate = false );
112
117
118 // Copy and move operations
121 EXPRESSION_EVALUATOR( EXPRESSION_EVALUATOR&& aOther ) noexcept;
123
132 void SetVariableCallback( VAR_CALLBACK aCallback );
133
140
145 bool HasVariableCallback() const;
146
154 void SetDefaultUnits( EDA_UNITS aUnits );
155
161
170 void SetVariable( const wxString& aName, double aValue );
171
180 void SetVariable( const wxString& aName, const wxString& aValue );
181
187 void SetVariable( const std::string& aName, const std::string& aValue );
188
194 bool RemoveVariable( const wxString& aName );
195
201 void ClearVariables();
202
210 bool HasVariable( const wxString& aName ) const;
211
219 wxString GetVariable( const wxString& aName ) const;
220
227 std::vector<wxString> GetVariableNames() const;
228
233 void SetVariables( const std::unordered_map<wxString, double>& aVariables );
234
239 void SetVariables( const std::unordered_map<wxString, wxString>& aVariables );
240
248 wxString Evaluate( const wxString& aInput );
249
259 wxString Evaluate( const wxString& aInput, const std::unordered_map<wxString, double>& aTempVariables );
260
270 wxString Evaluate( const wxString& aInput, const std::unordered_map<wxString, double>& aTempNumericVars,
271 const std::unordered_map<wxString, wxString>& aTempStringVars );
272
277 bool HasErrors() const;
278
283 size_t GetErrorCount() const;
284
289 wxString GetErrorSummary() const;
290
295 std::vector<wxString> GetErrors() const;
296
300 void ClearErrors();
301
308 void SetClearVariablesOnEvaluate( bool aEnable );
309
314 bool GetClearVariablesOnEvaluate() const;
315
323 bool TestExpression( const wxString& aExpression );
324
330 size_t CountExpressions( const wxString& aInput ) const;
331
337 std::vector<wxString> ExtractExpressions( const wxString& aInput ) const;
338
339private:
345 std::string wxStringToStdString( const wxString& aWxStr ) const;
346
352 wxString stdStringToWxString( const std::string& aStdStr ) const;
353
360 VAR_CALLBACK createCombinedCallback( const std::unordered_map<wxString, double>* aTempNumericVars = nullptr,
361 const std::unordered_map<wxString, wxString>* aTempStringVars = nullptr ) const;
362
369 std::pair<std::string, bool> evaluateWithParser( const std::string& aInput, VAR_CALLBACK aVariableCallback );
370
377 std::pair<std::string, bool> evaluateWithPartialErrorRecovery( const std::string& aInput,
378 VAR_CALLBACK aVariableCallback );
379
386 std::pair<std::string, bool> evaluateWithFullParser( const std::string& aInput, VAR_CALLBACK aVariableCallback );
387
395 wxString expandVariablesOutsideExpressions( const wxString& aInput,
396 const std::unordered_map<wxString, double>& aTempNumericVars,
397 const std::unordered_map<wxString, wxString>& aTempStringVars ) const;
398
399private:
400 std::unordered_map<std::string, calc_parser::VALUE> m_variables;
401 mutable std::unique_ptr<calc_parser::ERROR_COLLECTOR> m_lastErrors;
405 EDA_UNITS m_defaultUnits; // Default units for calculations
406};
407
430 */
432{
433public:
438 explicit NUMERIC_EVALUATOR_COMPAT( EDA_UNITS aUnits );
439
444
451 void Clear();
452
457 void SetDefaultUnits( EDA_UNITS aUnits );
458
465 void LocaleChanged();
466
471 bool IsValid() const;
472
477 wxString Result() const;
478
484 bool Process( const wxString& aString );
485
490 wxString OriginalText() const;
491
497 void SetVar( const wxString& aString, double aValue );
498
504 double GetVar( const wxString& aString );
505
510 void RemoveVar( const wxString& aString );
511
515 void ClearVar();
516
517private:
519 wxString m_lastInput;
520 wxString m_lastResult;
521 bool m_lastValid;
522};
double Evaluate(AM_PARAM_EVAL_STACK &aExp)
Evaluate an basic arithmetic expression (infix notation) with precedence The expression is a sequence...
Definition evaluate.cpp:98
High-level wrapper for evaluating mathematical and string expressions in wxString format.
EDA_UNITS GetDefaultUnits() const
Get the current default units.
bool HasVariableCallback() const
Check if a custom variable callback is set.
bool TestExpression(const wxString &aExpression)
Test if an expression can be parsed without evaluating it.
bool RemoveVariable(const wxString &aName)
Remove a variable from the evaluator.
bool HasErrors() const
Check if the last evaluation had errors.
wxString GetErrorSummary() const
Get detailed error information from the last evaluation.
void SetDefaultUnits(EDA_UNITS aUnits)
Set the default units for expressions.
bool GetClearVariablesOnEvaluate() const
Check if automatic variable clearing is enabled.
void SetVariables(const std::unordered_map< wxString, double > &aVariables)
Set multiple variables at once from a map.
std::pair< std::string, bool > evaluateWithFullParser(const std::string &aInput, VAR_CALLBACK aVariableCallback)
Full parser evaluation (original behavior) - fails completely on any error.
std::string wxStringToStdString(const wxString &aWxStr) const
Convert wxString to std::string using UTF-8 encoding.
std::unordered_map< std::string, calc_parser::VALUE > m_variables
std::pair< std::string, bool > evaluateWithParser(const std::string &aInput, VAR_CALLBACK aVariableCallback)
Parse and evaluate the input string using the expression parser.
size_t GetErrorCount() const
Get count of errors from the last evaluation.
std::vector< wxString > ExtractExpressions(const wxString &aInput) const
Extract all '@{}' expressions from input without evaluating.
void ClearErrors()
Clear any stored error information.
std::unique_ptr< calc_parser::ERROR_COLLECTOR > m_lastErrors
wxString stdStringToWxString(const std::string &aStdStr) const
Convert std::string to wxString using UTF-8 encoding.
std::vector< wxString > GetErrors() const
Get individual error messages from the last evaluation.
void SetVariableCallback(VAR_CALLBACK aCallback)
Set a custom variable resolver callback.
void ClearVariableCallback()
Clear the custom variable resolver callback.
void ClearVariables()
Clear all stored variables.
bool HasVariable(const wxString &aName) const
Check if a variable exists in stored variables.
std::vector< wxString > GetVariableNames() const
Get all stored variable names currently defined.
EXPRESSION_EVALUATOR & operator=(const EXPRESSION_EVALUATOR &aOther)
VAR_CALLBACK createCombinedCallback(const std::unordered_map< wxString, double > *aTempNumericVars=nullptr, const std::unordered_map< wxString, wxString > *aTempStringVars=nullptr) const
Create a callback function that combines all variable sources.
std::function< calc_parser::RESULT< calc_parser::VALUE >(const std::string &aVariableName)> VAR_CALLBACK
void SetVariable(const wxString &aName, double aValue)
Set a numeric variable for use in expressions.
void SetClearVariablesOnEvaluate(bool aEnable)
Enable or disable automatic variable clearing after evaluation.
wxString GetVariable(const wxString &aName) const
Get the current value of a stored variable.
std::pair< std::string, bool > evaluateWithPartialErrorRecovery(const std::string &aInput, VAR_CALLBACK aVariableCallback)
Parse and evaluate with partial error recovery - malformed expressions left unchanged.
size_t CountExpressions(const wxString &aInput) const
Count the number of '@{}' expressions in input string.
wxString expandVariablesOutsideExpressions(const wxString &aInput, const std::unordered_map< wxString, double > &aTempNumericVars, const std::unordered_map< wxString, wxString > &aTempStringVars) const
Expand ${variable} patterns that are outside '@{}' expressions.
EXPRESSION_EVALUATOR(bool aClearVariablesOnEvaluate=false)
Construct a new Expression Evaluator in static variable mode.
NUMERIC_EVALUATOR compatible wrapper around EXPRESSION_EVALUATOR.
void LocaleChanged()
Handle locale changes (for decimal separator)
void SetDefaultUnits(EDA_UNITS aUnits)
Set default units for evaluation.
bool Process(const wxString &aString)
Process and evaluate an expression.
wxString Result() const
Get the result of the last evaluation.
void RemoveVar(const wxString &aString)
Remove a single variable.
void SetVar(const wxString &aString, double aValue)
Set a variable value.
void ClearVar()
Remove all variables.
EXPRESSION_EVALUATOR m_evaluator
double GetVar(const wxString &aString)
Get a variable value.
void Clear()
Clear parser state but retain variables.
NUMERIC_EVALUATOR_COMPAT(EDA_UNITS aUnits)
Constructor with default units.
bool IsValid() const
Check if the last evaluation was successful.
wxString OriginalText() const
Get the original input text.
EDA_UNITS
Definition eda_units.h:44
#define KICOMMON_API
Definition kicommon.h:27