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 along
17 * with this program. If not, see <http://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
75{
76public:
77 // Callback function type for dynamic variable resolution
78 using VariableCallback = std::function<calc_parser::Result<calc_parser::Value>(const std::string& aVariableName)>;
79
80private:
81 std::unordered_map<std::string, calc_parser::Value> m_variables;
82 mutable std::unique_ptr<calc_parser::ERROR_COLLECTOR> m_lastErrors;
86 EDA_UNITS m_defaultUnits; // Default units for calculations
87
88public:
93 explicit EXPRESSION_EVALUATOR( bool aClearVariablesOnEvaluate = false );
94
100 explicit EXPRESSION_EVALUATOR( EDA_UNITS aUnits, bool aClearVariablesOnEvaluate = false );
101
107 explicit EXPRESSION_EVALUATOR( VariableCallback aVariableCallback,
108 bool aClearVariablesOnEvaluate = false );
109
116 explicit EXPRESSION_EVALUATOR( EDA_UNITS aUnits, VariableCallback aVariableCallback,
117 bool aClearVariablesOnEvaluate = false );
118
123
124 // Copy and move operations
127 EXPRESSION_EVALUATOR( EXPRESSION_EVALUATOR&& aOther ) noexcept;
129
138 void SetVariableCallback( VariableCallback aCallback );
139
146
151 bool HasVariableCallback() const;
152
160 void SetDefaultUnits( EDA_UNITS aUnits );
161
167
176 void SetVariable( const wxString& aName, double aValue );
177
186 void SetVariable( const wxString& aName, const wxString& aValue );
187
193 void SetVariable( const std::string& aName, const std::string& aValue );
194
200 bool RemoveVariable( const wxString& aName );
201
207 void ClearVariables();
208
216 bool HasVariable( const wxString& aName ) const;
217
225 wxString GetVariable( const wxString& aName ) const;
226
233 std::vector<wxString> GetVariableNames() const;
234
239 void SetVariables( const std::unordered_map<wxString, double>& aVariables );
240
245 void SetVariables( const std::unordered_map<wxString, wxString>& aVariables );
246
254 wxString Evaluate( const wxString& aInput );
255
265 wxString Evaluate( const wxString& aInput,
266 const std::unordered_map<wxString, double>& aTempVariables );
267
277 wxString Evaluate( const wxString& aInput,
278 const std::unordered_map<wxString, double>& aTempNumericVars,
279 const std::unordered_map<wxString, wxString>& aTempStringVars );
280
285 bool HasErrors() const;
286
291 size_t GetErrorCount() const;
292
297 wxString GetErrorSummary() const;
298
303 std::vector<wxString> GetErrors() const;
304
308 void ClearErrors();
309
316 void SetClearVariablesOnEvaluate( bool aEnable );
317
322 bool GetClearVariablesOnEvaluate() const;
323
331 bool TestExpression( const wxString& aExpression );
332
338 size_t CountExpressions( const wxString& aInput ) const;
339
345 std::vector<wxString> ExtractExpressions( const wxString& aInput ) const;
346
347private:
353 std::string wxStringToStdString( const wxString& aWxStr ) const;
354
360 wxString stdStringToWxString( const std::string& aStdStr ) const;
361
369 const std::unordered_map<wxString, double>* aTempNumericVars = nullptr,
370 const std::unordered_map<wxString, wxString>* aTempStringVars = nullptr ) const;
371
378 std::pair<std::string, bool> evaluateWithParser(
379 const std::string& aInput,
380 VariableCallback aVariableCallback );
381
388 std::pair<std::string, bool> evaluateWithPartialErrorRecovery(
389 const std::string& aInput,
390 VariableCallback aVariableCallback );
391
398 std::pair<std::string, bool> evaluateWithFullParser(
399 const std::string& aInput,
400 VariableCallback aVariableCallback );
401
410 const wxString& aInput,
411 const std::unordered_map<wxString, double>& aTempNumericVars,
412 const std::unordered_map<wxString, wxString>& aTempStringVars ) const;
413};
414
439{
440private:
442 wxString m_lastInput;
443 wxString m_lastResult;
445
446public:
451 explicit NUMERIC_EVALUATOR_COMPAT( EDA_UNITS aUnits );
452
457
464 void Clear();
465
470 void SetDefaultUnits( EDA_UNITS aUnits );
471
478 void LocaleChanged();
479
484 bool IsValid() const;
485
490 wxString Result() const;
491
497 bool Process( const wxString& aString );
498
503 wxString OriginalText() const;
504
510 void SetVar( const wxString& aString, double aValue );
511
517 double GetVar( const wxString& aString );
518
523 void RemoveVar( const wxString& aString );
524
528 void ClearVar();
529};
double Evaluate(AM_PARAM_EVAL_STACK &aExp)
Evaluate an basic arithmetic expression (infix notation) with precedence The expression is a sequence...
Definition evaluate.cpp:102
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.
std::pair< std::string, bool > evaluateWithFullParser(const std::string &aInput, VariableCallback aVariableCallback)
Full parser evaluation (original behavior) - fails completely on any error.
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.
std::unordered_map< std::string, calc_parser::Value > m_variables
void SetVariables(const std::unordered_map< wxString, double > &aVariables)
Set multiple variables at once from a map.
std::pair< std::string, bool > evaluateWithPartialErrorRecovery(const std::string &aInput, VariableCallback aVariableCallback)
Parse and evaluate with partial error recovery - malformed expressions left unchanged.
std::string wxStringToStdString(const wxString &aWxStr) const
Convert wxString to std::string using UTF-8 encoding.
size_t GetErrorCount() const
Get count of errors from the last evaluation.
VariableCallback 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::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 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()
Destructor.
EXPRESSION_EVALUATOR & operator=(const EXPRESSION_EVALUATOR &aOther)
std::function< calc_parser::Result< calc_parser::Value >(const std::string &aVariableName)> VariableCallback
std::pair< std::string, bool > evaluateWithParser(const std::string &aInput, VariableCallback aVariableCallback)
Parse and evaluate the input string using the expression parser.
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.
void SetVariableCallback(VariableCallback aCallback)
Set a custom variable resolver callback.
wxString GetVariable(const wxString &aName) const
Get the current value of a stored variable.
size_t CountExpressions(const wxString &aInput) const
Count the number of} expressions in input string.
VariableCallback m_customCallback
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)
~NUMERIC_EVALUATOR_COMPAT()
Destructor.
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:48
#define KICOMMON_API
Definition kicommon.h:28