21#include <fmt/format.h>
25#include <fast_float/fast_float.h>
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wunused-variable"
36#pragma GCC diagnostic ignored "-Wsign-compare"
37#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
40#include <text_eval/text_eval.c>
43#pragma GCC diagnostic pop
85 concept Utf8Byte = std::same_as<T, char> || std::same_as<T, unsigned char> || std::same_as<T, std::byte>;
92 static constexpr bool is_ascii( std::byte b )
noexcept
94 return ( b & std::byte{ 0x80 } ) == std::byte{ 0x00 };
99 return ( b & std::byte{ 0xC0 } ) == std::byte{ 0x80 };
106 if( ( first & std::byte{ 0xE0 } ) == std::byte{ 0xC0 } )
108 if( ( first & std::byte{ 0xF0 } ) == std::byte{ 0xE0 } )
110 if( ( first & std::byte{ 0xF8 } ) == std::byte{ 0xF0 } )
117 static std::u32string
to_utf32( std::string_view utf8 )
120 result.reserve( utf8.size() );
122 auto bytes = std::as_bytes( std::span{ utf8.data(), utf8.size() } );
124 for(
size_t i = 0; i < bytes.size(); )
126 std::byte first = bytes[i];
129 if( len == 0 || i + len > bytes.size() )
132 result.push_back( U
'\uFFFD' );
137 char32_t codepoint = 0;
141 case 1: codepoint = std::to_integer<char32_t>( first );
break;
147 result.push_back( U
'\uFFFD' );
151 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x1F } ) << 6 )
152 | std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } );
160 result.push_back( U
'\uFFFD' );
164 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x0F } ) << 12 )
165 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 6 )
166 | std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } );
175 result.push_back( U
'\uFFFD' );
179 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x07 } ) << 18 )
180 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 12 )
181 | ( std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } ) << 6 )
182 | std::to_integer<char32_t>( bytes[i + 3] & std::byte{ 0x3F } );
188 if( codepoint > 0x10FFFF || ( codepoint >= 0xD800 && codepoint <= 0xDFFF ) )
190 result.push_back( U
'\uFFFD' );
192 else if( len == 2 && codepoint < 0x80 )
194 result.push_back( U
'\uFFFD' );
196 else if( len == 3 && codepoint < 0x800 )
198 result.push_back( U
'\uFFFD' );
200 else if( len == 4 && codepoint < 0x10000 )
202 result.push_back( U
'\uFFFD' );
206 result.push_back( codepoint );
216 static std::string
to_utf8( std::u32string_view utf32 )
219 result.reserve( utf32.size() * 4 );
221 for(
char32_t cp : utf32 )
226 result.push_back(
static_cast<char>( cp ) );
228 else if( cp <= 0x7FF )
231 result.push_back(
static_cast<char>( 0xC0 | ( cp >> 6 ) ) );
232 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
234 else if( cp <= 0xFFFF )
237 if( cp >= 0xD800 && cp <= 0xDFFF )
240 result.append(
"\uFFFD" );
244 result.push_back(
static_cast<char>( 0xE0 | ( cp >> 12 ) ) );
245 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
246 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
249 else if( cp <= 0x10FFFF )
252 result.push_back(
static_cast<char>( 0xF0 | ( cp >> 18 ) ) );
253 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 12 ) & 0x3F ) ) );
254 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
255 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
260 result.append(
"\uFFFD" );
274 return cp == U
' ' || cp == U
'\t' || cp == U
'\r' || cp == U
'\n' ||
275 cp == U
'\f' || cp == U
'\v' || cp == U
'\u00A0' ||
276 cp == U
'\u2000' || cp == U
'\u2001' || cp == U
'\u2002' || cp == U
'\u2003' ||
277 cp == U
'\u2004' || cp == U
'\u2005' || cp == U
'\u2006' || cp == U
'\u2007' ||
278 cp == U
'\u2008' || cp == U
'\u2009' || cp == U
'\u200A' || cp == U
'\u2028' ||
279 cp == U
'\u2029' || cp == U
'\u202F' || cp == U
'\u205F' || cp == U
'\u3000';
283 return cp >= U
'0' && cp <= U
'9';
287 return (cp >= U
'a' && cp <= U
'z') || (cp >= U
'A' && cp <= U
'Z');
292 return is_ascii_alpha(cp) || (cp >= 0x80 && cp <= 0x10FFFF && cp != 0xFFFD);
308 static constexpr std::array<PREFIX, 18>
prefixes = { {
313 {U
'u', 1e-6}, {U
'µ', 1e-6}, {U
'μ', 1e-6},
315 {U
'k', 1e3}, {U
'K', 1e3},
325 return std::ranges::any_of(
prefixes,
334 auto it = std::ranges::find_if(
prefixes,
339 return it !=
prefixes.end() ? it->multiplier : 1.0;
414 if( aFromUnit == aToUnit )
477 [[nodiscard]]
constexpr char32_t peek_char(
size_t offset = 1 ) const noexcept
479 size_t peek_pos =
m_pos + offset;
480 return peek_pos <
m_text.size() ?
m_text[peek_pos] : U
'\0';
485 for(
size_t i = 0; i < count &&
m_pos <
m_text.size(); ++i )
510 auto error_msg = fmt::format(
"Line {}, Column {}: {}",
m_line,
m_column, message );
519 std::strncpy( token.text, value.c_str(),
sizeof( token.text ) - 1 );
520 token.text[
sizeof( token.text ) - 1] =
'\0';
529 token.dValue = value;
537 std::u32string content;
538 content.reserve( 64 );
551 case U
'n': content.push_back( U
'\n' );
break;
552 case U
't': content.push_back( U
'\t' );
break;
553 case U
'r': content.push_back( U
'\r' );
break;
554 case U
'\\': content.push_back( U
'\\' );
break;
555 case U
'"': content.push_back( U
'"' );
break;
556 case U
'\'': content.push_back( U
'\'' );
break;
557 case U
'0': content.push_back( U
'\0' );
break;
563 for(
int i = 0; i < 2 &&
m_pos <
m_text.size(); ++i )
566 if( ( hex_char >= U
'0' && hex_char <= U
'9' )
567 || ( hex_char >= U
'A' && hex_char <= U
'F' )
568 || ( hex_char >= U
'a' && hex_char <= U
'f' ) )
570 hex.push_back( hex_char );
584 auto value = std::stoul( hex_str,
nullptr, 16 );
586 if( value <= 0x10FFFF )
587 content.push_back(
static_cast<char32_t>( value ) );
589 content.push_back( U
'\uFFFD' );
593 content.push_back( U
'\uFFFD' );
598 content.append( U
"\\x" );
604 content.push_back( U
'\\' );
605 content.push_back( escaped );
609 else if( c == U
'\n' )
611 add_error(
"Unterminated string literal" );
616 content.push_back( c );
624 add_error(
"Missing closing quote in string literal");
632 std::u32string number_text;
633 number_text.reserve( 32 );
635 bool has_decimal =
false;
636 double multiplier = 1.0;
654 number_text.push_back( U
'.' );
662 std::u32string potential_unit;
663 size_t temp_pos =
m_pos;
665 while( temp_pos <
m_text.size() )
667 char32_t unit_char =
m_text[temp_pos];
671 potential_unit.push_back( unit_char );
681 if( !potential_unit.empty() )
695 number_text.push_back( U
'.' );
704 number_text.push_back( U
'.' );
713 number_text.push_back( U
'.' );
731 if( !number_str.empty() && number_str !=
"." )
733 auto result = fast_float::from_chars( number_str.data(), number_str.data() + number_str.size(), value );
735 if(
result.ec != std::errc() ||
result.ptr != number_str.data() + number_str.size() )
736 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number", number_str ) );
740 if( !std::isfinite( value ) )
747 catch(
const std::exception& e )
749 add_error( fmt::format(
"Invalid number format: {}", e.what() ) );
757 size_t whitespace_start =
m_pos;
764 std::u32string unit_text;
774 unit_text.push_back( c );
783 if( !unit_text.empty() )
793 value = converted_value;
798 m_pos = whitespace_start;
804 m_pos = whitespace_start;
812 std::u32string identifier;
813 identifier.reserve(64);
833 if ((current == U
'@' &&
next == U
'{') ||
834 (current == U
'$' &&
next == U
'{')) {
838 text.push_back(current);
878 if( current == U
'@' &&
next == U
'{' )
887 if( current == U
'$' &&
next == U
'{' )
897 if( current == U
'}' )
911 if( current == U
'<' &&
next == U
'=' )
917 if( current == U
'>' &&
next == U
'=' )
923 if( current == U
'=' &&
next == U
'=' )
929 if( current == U
'!' &&
next == U
'=' )
940 static constexpr std::array<std::pair<char32_t, TextEvalToken>, 11> single_char_tokens{{
954 if(
auto it = std::ranges::find_if( single_char_tokens,
955 [current](
const auto& pair ) {
return pair.first == current; } );
956 it != single_char_tokens.end() )
965 if( current == U
'"' || current == U
'\'' )
1009 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1013 bool aClearVariablesOnEvaluate ) :
1019 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1027 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1031 bool aClearVariablesOnEvaluate ) :
1037 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1049 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1060 if(
this != &aOther )
1068 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1090 if(
this != &aOther )
1170 if( std::holds_alternative<double>( it->second ) )
1172 double val = std::get<double>( it->second );
1174 if( val == std::floor( val ) &&
std::abs( val ) < 1e15 )
1175 return wxString::Format(
"%.0f", val );
1177 return wxString::Format(
"%g", val );
1189 std::vector<wxString> names;
1200 for(
const auto& [
name, value] : aVariables )
1206 for(
const auto& [
name, value] : aVariables )
1212 std::unordered_map<wxString, double> emptyNumVars;
1213 std::unordered_map<wxString, wxString> emptyStringVars;
1214 return Evaluate( aInput, emptyNumVars, emptyStringVars );
1218 const std::unordered_map<wxString, double>& aTempVariables )
1220 std::unordered_map<wxString, wxString> emptyStringVars;
1221 return Evaluate( aInput, aTempVariables, emptyStringVars );
1225 const std::unordered_map<wxString, double>& aTempNumericVars,
1226 const std::unordered_map<wxString, wxString>& aTempStringVars )
1244 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1282 std::vector<wxString>
result;
1287 result.reserve( errors.size() );
1289 for(
const auto& error : errors )
1315 wxString testInput =
"@{" + aExpression +
"}";
1332 for(
const auto& error : errors )
1334 if( error.find(
"Syntax error" ) != std::string::npos ||
1335 error.find(
"Parser failed" ) != std::string::npos )
1350 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1361 std::vector<wxString> expressions;
1364 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1366 size_t start = pos + 2;
1367 size_t end = aInput.find(
"}", start );
1369 if(
end != wxString::npos )
1371 expressions.push_back( aInput.substr( start,
end - start ) );
1385 return aWxStr.ToStdString( wxConvUTF8 );
1390 return wxString( aStdStr.c_str(), wxConvUTF8 );
1394 const wxString& aInput,
1395 const std::unordered_map<wxString, double>& aTempNumericVars,
1396 const std::unordered_map<wxString, wxString>& aTempStringVars )
const
1398 wxString
result = aInput;
1402 std::vector<std::pair<size_t, size_t>> expressionRanges;
1405 while( (pos =
result.find(
"@{", pos )) != std::string::npos )
1408 size_t braceCount = 1;
1409 size_t searchPos = start + 2;
1412 while( searchPos <
result.length() && braceCount > 0 )
1414 if(
result[searchPos] ==
'{' )
1416 else if(
result[searchPos] ==
'}' )
1421 if( braceCount == 0 )
1423 expressionRanges.emplace_back( start, searchPos );
1431 while( (pos =
result.find(
"${", pos )) != std::string::npos )
1434 bool insideExpression =
false;
1435 for(
const auto& range : expressionRanges )
1437 if( pos >= range.first && pos < range.second )
1439 insideExpression =
true;
1444 if( insideExpression )
1448 size_t closePos =
result.find(
"}", pos + 2 );
1449 if( closePos != std::string::npos )
1452 size_t afterBrace = closePos + 1;
1453 bool followedByUnit =
false;
1455 if( afterBrace <
result.length() )
1459 for(
const auto& unit : units )
1461 if( afterBrace + unit.length() <=
result.length() &&
1462 result.substr( afterBrace, unit.length() ) == unit )
1464 followedByUnit =
true;
1470 if( !followedByUnit )
1485 size_t closePos =
result.find(
"}", pos + 2 );
1486 if( closePos == std::string::npos )
1493 wxString varName =
result.substr( pos + 2, closePos - pos - 2 );
1494 wxString replacement;
1498 auto stringIt = aTempStringVars.find( varName );
1499 if( stringIt != aTempStringVars.end() )
1501 replacement = stringIt->second;
1507 auto numIt = aTempNumericVars.find( varName );
1508 if( numIt != aTempNumericVars.end() )
1510 replacement = wxString::FromDouble( numIt->second );
1521 if( std::holds_alternative<std::string>( value ) )
1526 else if( std::holds_alternative<double>( value ) )
1528 replacement = wxString::FromDouble( std::get<double>( value ) );
1538 result.replace( pos, closePos - pos + 1, replacement );
1539 pos += replacement.length();
1545 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1555 const std::unordered_map<wxString, double>* aTempNumericVars,
1556 const std::unordered_map<wxString, wxString>* aTempStringVars )
const
1564 if( customResult.HasValue() )
1565 return customResult;
1573 if( aTempStringVars )
1576 if(
auto it = aTempStringVars->find( wxVarName ); it != aTempStringVars->end() )
1584 if( aTempNumericVars )
1587 if(
auto it = aTempNumericVars->find( wxVarName ); it != aTempNumericVars->end() )
1603 wxString testString = wxString::Format(
"${%s}", varName );
1606 bool wasResolved =
false;
1607 std::function<bool( wxString* )>
resolver =
1608 [&wasResolved]( wxString* token ) ->
bool
1622 std::function<bool( wxString* )> valueResolver =
1623 []( wxString* token ) ->
bool
1630 wxString resolvedValue =
ExpandTextVars( testString, &valueResolver );
1633 if( resolvedValue != testString )
1641 auto result = fast_float::from_chars( resolvedStd.data(), resolvedStd.data() + resolvedStd.size(), numValue );
1643 if(
result.ec != std::errc() ||
result.ptr != resolvedStd.data() + resolvedStd.size() )
1644 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number", resolvedStd ) );
1656 catch(
const std::exception& e )
1669 fmt::format(
"Undefined variable: {}", aVarName ) );
1674 const std::string& aInput,
1682 if (partialResult != aInput) {
1684 return {std::move(partialResult), partialHadErrors};
1690 }
catch (
const std::bad_alloc&) {
1694 return {aInput,
true};
1695 }
catch (
const std::exception& e) {
1697 m_lastErrors->AddError(fmt::format(
"Exception: {}", e.what()));
1699 return {aInput,
true};
1704 const std::string& aInput,
1707 std::string
result = aInput;
1708 bool hadAnyErrors =
false;
1712 std::vector<std::pair<size_t, size_t>> expressionRanges;
1715 while( ( pos =
result.find(
"@{", pos ) ) != std::string::npos )
1718 size_t exprStart = pos + 2;
1719 size_t braceCount = 1;
1720 size_t searchPos = exprStart;
1723 while( searchPos <
result.length() && braceCount > 0 )
1725 if(
result[searchPos] ==
'{' )
1729 else if(
result[searchPos] ==
'}' )
1736 if( braceCount == 0 )
1738 size_t end = searchPos;
1739 expressionRanges.emplace_back( start,
end );
1749 for(
auto it = expressionRanges.rbegin(); it != expressionRanges.rend(); ++it )
1751 auto [start,
end] = *it;
1752 std::string fullExpr =
result.substr( start,
end - start );
1753 std::string innerExpr =
result.substr( start + 2,
end - start - 3 );
1759 std::string testExpr =
"@{" + innerExpr +
"}";
1762 auto tempErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1769 if( !evalHadErrors )
1772 result.replace( start,
end - start, evalResult );
1777 hadAnyErrors =
true;
1781 oldErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1783 oldErrors->AddError( fmt::format(
"Failed to evaluate expression: {}", fullExpr ) );
1793 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1795 m_lastErrors->AddError( fmt::format(
"Exception in expression: {}", fullExpr ) );
1796 hadAnyErrors =
true;
1800 return { std::move(
result ), hadAnyErrors };
1806 if( aInput.empty() )
1808 return { std::string{},
false };
1812 struct ErrorCollectorGuard
1832 auto parser_deleter = [](
void* p )
1834 KI_EVAL::ParseFree( p, free );
1838 std::unique_ptr<void,
decltype( parser_deleter )> parser{ KI_EVAL::ParseAlloc( malloc ), parser_deleter };
1846 return { aInput,
true };
1857 token_type = tokenizer.get_next_token( token_value );
1860 KI_EVAL::Parse( parser.get(),
static_cast<int>( token_type ), token_value, &document );
1877 auto [
result, had_errors] = processor.
Process( *document, std::move( aVariableCallback ) );
1884 return { aInput,
true };
1888 return { std::move(
result ), had_errors };
1893 return { aInput,
true };
1895 catch(
const std::bad_alloc& )
1901 return { aInput,
true };
1903 catch(
const std::exception& e )
1907 m_lastErrors->AddError( fmt::format(
"Exception: {}", e.what() ) );
1909 return { aInput,
true };
1955 wxString processedExpression = aString;
1961 std::sort( varNames.begin(), varNames.end(),
1962 [](
const wxString& a,
const wxString& b ) { return a.length() > b.length(); } );
1965 for(
const auto& varName : varNames )
1969 wxString pattern =
"\\b" + varName +
"\\b";
1970 wxString replacement =
"${" + varName +
"}";
1976 while( ( pos = processedExpression.find( varName, pos ) ) != wxString::npos )
1979 bool isWholeWord =
true;
1984 wxChar before = processedExpression[pos - 1];
1985 if( wxIsalnum( before ) || before ==
'_' || before ==
'$' )
1986 isWholeWord =
false;
1990 if( isWholeWord && pos + varName.length() < processedExpression.length() )
1992 wxChar after = processedExpression[pos + varName.length()];
1993 if( wxIsalnum( after ) || after ==
'_' )
1994 isWholeWord =
false;
1999 processedExpression.replace( pos, varName.length(), replacement );
2000 pos += replacement.length();
2004 pos += varName.length();
2010 wxString wrappedExpression =
"@{" + processedExpression +
"}";
2048 wxString value =
m_evaluator.GetVariable( aString );
2053 if( !value.ToDouble( &
result ) )
EDA_UNITS GetDefaultUnits() const
Get the current default units.
bool HasVariableCallback() const
Check if a custom variable callback is set.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
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.
bool m_clearVariablesOnEvaluate
EXPRESSION_EVALUATOR(bool aClearVariablesOnEvaluate=false)
Construct a new Expression Evaluator in static variable mode.
TOKENIZER_CONTEXT m_context
constexpr char32_t current_char() const noexcept
calc_parser::TOKEN_TYPE parse_number()
void add_error(std::string_view message) const
static constexpr calc_parser::TOKEN_TYPE make_number_token(double value) noexcept
utf8_utils::SI_PREFIX_HANDLER SI_HANDLER
constexpr size_t get_column() const noexcept
void skip_whitespace() noexcept
constexpr bool has_more_tokens() const noexcept
calc_parser::ERROR_COLLECTOR * m_errorCollector
calc_parser::TOKEN_TYPE parse_string_literal(char32_t quote_char)
KIEVAL_TEXT_TOKENIZER(std::string_view input, calc_parser::ERROR_COLLECTOR *error_collector=nullptr, EDA_UNITS default_units=EDA_UNITS::MM)
TextEvalToken get_next_token(calc_parser::TOKEN_TYPE &token_value)
utf8_utils::CHARACTER_CLASSIFIER CLASSIFIER
calc_parser::TOKEN_TYPE parse_text_content()
constexpr void advance_position(size_t count=1) noexcept
calc_parser::TOKEN_TYPE parse_identifier()
constexpr size_t get_line() const noexcept
constexpr char32_t peek_char(size_t offset=1) const noexcept
static calc_parser::TOKEN_TYPE make_string_token(std::string value) noexcept
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.
static auto Process(const DOC &aDoc, VariableCallback aVariableCallback) -> std::pair< std::string, bool >
Process document using callback for variable resolution.
auto GetErrors() const -> const std::vector< std::string > &
static constexpr Unit parseUnit(std::string_view unitStr) noexcept
Parse a unit string and return the corresponding Unit enum.
static double convertToEdaUnits(double value, std::string_view unitStr, EDA_UNITS targetUnits)
Convert a value with unit string to target EDA_UNITS.
static std::vector< std::string > getAllUnitStrings()
Get all unit strings in parsing order (longest first)
static constexpr int sequence_length(std::byte first) noexcept
static std::string to_utf8(std::u32string_view utf32)
static constexpr bool is_ascii(std::byte b) noexcept
static std::u32string to_utf32(std::string_view utf8)
static constexpr bool is_continuation(std::byte b) noexcept
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
static FILENAME_RESOLVER * resolver
double convertToDefaultUnits(double aValue, const std::string &aUnitStr, EDA_UNITS aDefaultUnits)
Unit parseUnit(const std::string &aUnitStr)
double getConversionFactor(Unit aFromUnit, Unit aToUnit)
Unit edaUnitsToInternal(EDA_UNITS aUnits)
thread_local ERROR_COLLECTOR * g_errorCollector
auto MakeValue(T aVal) -> Result< T >
std::variant< double, std::string > Value
auto MakeError(std::string aMsg) -> Result< T >
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
double fromMM(double aMMValue)
double toMM(double aIUValue)
static constexpr bool is_digit(UnicodeCodepoint auto cp) noexcept
static constexpr bool is_alpha(UnicodeCodepoint auto cp) noexcept
static constexpr bool is_whitespace(UnicodeCodepoint auto cp) noexcept
static constexpr bool is_ascii_alpha(UnicodeCodepoint auto cp) noexcept
static constexpr bool is_alnum(UnicodeCodepoint auto cp) noexcept
static constexpr double get_multiplier(UnicodeCodepoint auto cp) noexcept
static constexpr std::array< PREFIX, 18 > prefixes
static constexpr bool is_si_prefix(UnicodeCodepoint auto cp) noexcept
wxString result
Test unit parsing edge cases and error handling.