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
87concept Utf8Byte = std::same_as<T, char> || std::same_as<T, unsigned char> || std::same_as<T, std::byte>;
94 static constexpr bool is_ascii( std::byte b )
noexcept {
return ( b & std::byte{ 0x80 } ) == std::byte{ 0x00 }; }
98 return ( b & std::byte{ 0xC0 } ) == std::byte{ 0x80 };
105 if( ( first & std::byte{ 0xE0 } ) == std::byte{ 0xC0 } )
107 if( ( first & std::byte{ 0xF0 } ) == std::byte{ 0xE0 } )
109 if( ( first & std::byte{ 0xF8 } ) == std::byte{ 0xF0 } )
116 static std::u32string
to_utf32( std::string_view utf8 )
119 result.reserve( utf8.size() );
121 auto bytes = std::as_bytes( std::span{ utf8.data(), utf8.size() } );
123 for(
size_t i = 0; i < bytes.size(); )
125 std::byte first = bytes[i];
128 if( len == 0 || i + len > bytes.size() )
131 result.push_back( U
'\uFFFD' );
136 char32_t codepoint = 0;
140 case 1: codepoint = std::to_integer<char32_t>( first );
break;
146 result.push_back( U
'\uFFFD' );
150 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x1F } ) << 6 )
151 | std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } );
159 result.push_back( U
'\uFFFD' );
163 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x0F } ) << 12 )
164 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 6 )
165 | std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } );
174 result.push_back( U
'\uFFFD' );
178 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x07 } ) << 18 )
179 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 12 )
180 | ( std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } ) << 6 )
181 | std::to_integer<char32_t>( bytes[i + 3] & std::byte{ 0x3F } );
187 if( codepoint > 0x10FFFF || ( codepoint >= 0xD800 && codepoint <= 0xDFFF ) )
189 result.push_back( U
'\uFFFD' );
191 else if( len == 2 && codepoint < 0x80 )
193 result.push_back( U
'\uFFFD' );
195 else if( len == 3 && codepoint < 0x800 )
197 result.push_back( U
'\uFFFD' );
199 else if( len == 4 && codepoint < 0x10000 )
201 result.push_back( U
'\uFFFD' );
205 result.push_back( codepoint );
215 static std::string
to_utf8( std::u32string_view utf32 )
218 result.reserve( utf32.size() * 4 );
220 for(
char32_t cp : utf32 )
225 result.push_back(
static_cast<char>( cp ) );
227 else if( cp <= 0x7FF )
230 result.push_back(
static_cast<char>( 0xC0 | ( cp >> 6 ) ) );
231 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
233 else if( cp <= 0xFFFF )
236 if( cp >= 0xD800 && cp <= 0xDFFF )
239 result.append(
"\uFFFD" );
243 result.push_back(
static_cast<char>( 0xE0 | ( cp >> 12 ) ) );
244 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
245 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
248 else if( cp <= 0x10FFFF )
251 result.push_back(
static_cast<char>( 0xF0 | ( cp >> 18 ) ) );
252 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 12 ) & 0x3F ) ) );
253 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
254 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
259 result.append(
"\uFFFD" );
275 return cp == U
' ' || cp == U
'\t' || cp == U
'\r' || cp == U
'\n' || cp == U
'\f' || cp == U
'\v' || cp == U
'\u00A0'
277 cp == U
'\u2000' || cp == U
'\u2001' || cp == U
'\u2002' || cp == U
'\u2003' || cp == U
'\u2004'
278 || cp == U
'\u2005' || cp == U
'\u2006' || cp == U
'\u2007' || cp == U
'\u2008' || cp == U
'\u2009'
279 || cp == U
'\u200A' || cp == U
'\u2028' || cp == U
'\u2029' || cp == U
'\u202F' || cp == U
'\u205F'
287 return ( cp >= U
'a' && cp <= U
'z' ) || ( cp >= U
'A' && cp <= U
'Z' );
293 return is_ascii_alpha( cp ) || ( cp >= 0x80 && cp <= 0x10FFFF && cp != 0xFFFD );
307 static constexpr std::array<PREFIX, 18>
prefixes = { { { U
'a', 1e-18 },
325 return std::ranges::any_of(
prefixes,
334 auto it = std::ranges::find_if(
prefixes,
339 return it !=
prefixes.end() ? it->multiplier : 1.0;
416 if( aFromUnit == aToUnit )
476 [[nodiscard]]
char32_t peek_char(
size_t offset = 1 ) const noexcept
478 size_t peek_pos =
m_pos + offset;
479 return peek_pos <
m_text.size() ?
m_text[peek_pos] : U
'\0';
484 for(
size_t i = 0; i < count &&
m_pos <
m_text.size(); ++i )
509 auto error_msg = fmt::format(
"Line {}, Column {}: {}",
m_line,
m_column, message );
518 std::strncpy( token.text, value.c_str(),
sizeof( token.text ) - 1 );
519 token.text[
sizeof( token.text ) - 1] =
'\0';
528 token.dValue = value;
536 std::u32string content;
537 content.reserve( 64 );
550 case U
'n': content.push_back( U
'\n' );
break;
551 case U
't': content.push_back( U
'\t' );
break;
552 case U
'r': content.push_back( U
'\r' );
break;
553 case U
'\\': content.push_back( U
'\\' );
break;
554 case U
'"': content.push_back( U
'"' );
break;
555 case U
'\'': content.push_back( U
'\'' );
break;
556 case U
'0': content.push_back( U
'\0' );
break;
562 for(
int i = 0; i < 2 &&
m_pos <
m_text.size(); ++i )
565 if( ( hex_char >= U
'0' && hex_char <= U
'9' ) || ( hex_char >= U
'A' && hex_char <= U
'F' )
566 || ( hex_char >= U
'a' && hex_char <= U
'f' ) )
568 hex.push_back( hex_char );
582 auto value = std::stoul( hex_str,
nullptr, 16 );
584 if( value <= 0x10FFFF )
585 content.push_back(
static_cast<char32_t>( value ) );
587 content.push_back( U
'\uFFFD' );
591 content.push_back( U
'\uFFFD' );
596 content.append( U
"\\x" );
602 content.push_back( U
'\\' );
603 content.push_back( escaped );
607 else if( c == U
'\n' )
609 add_error(
"Unterminated string literal" );
614 content.push_back( c );
625 add_error(
"Missing closing quote in string literal" );
633 std::u32string number_text;
634 number_text.reserve( 32 );
636 double multiplier = 1.0;
654 number_text.push_back( U
'.' );
661 std::u32string potential_unit;
662 size_t temp_pos =
m_pos;
664 while( temp_pos <
m_text.size() )
666 char32_t unit_char =
m_text[temp_pos];
670 potential_unit.push_back( unit_char );
680 if( !potential_unit.empty() )
724 if( c == U
'e' || c == U
'E' )
727 size_t temp_pos =
m_pos + 1;
728 bool is_scientific =
false;
730 if( temp_pos <
m_text.size() )
735 is_scientific =
true;
742 number_text.push_back( c );
764 add_error(
"Invalid scientific notation: missing exponent digits" );
778 std::u32string potential_unit;
779 size_t temp_pos =
m_pos;
781 while( temp_pos <
m_text.size() )
783 char32_t unit_char =
m_text[temp_pos];
787 potential_unit.push_back( unit_char );
796 if( !potential_unit.empty() )
823 if( !number_str.empty() && number_str !=
"." )
825 auto result = fast_float::from_chars( number_str.data(), number_str.data() + number_str.size(), value );
827 if(
result.ec != std::errc() ||
result.ptr != number_str.data() + number_str.size() )
828 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number", number_str ) );
832 if( !std::isfinite( value ) )
839 catch(
const std::exception& e )
841 add_error( fmt::format(
"Invalid number format: {}", e.what() ) );
849 size_t whitespace_start =
m_pos;
856 std::u32string unit_text;
866 unit_text.push_back( c );
875 if( !unit_text.empty() )
885 value = converted_value;
890 m_pos = whitespace_start;
896 m_pos = whitespace_start;
905 std::u32string identifier;
906 identifier.reserve( 64 );
928 if( ( current == U
'@' &&
next == U
'{' ) || ( current == U
'$' &&
next == U
'{' ) )
933 text.push_back( current );
973 if( current == U
'@' &&
next == U
'{' )
982 if( current == U
'$' &&
next == U
'{' )
992 if( current == U
'}' )
1006 if( current == U
'<' &&
next == U
'=' )
1012 if( current == U
'>' &&
next == U
'=' )
1018 if( current == U
'=' &&
next == U
'=' )
1024 if( current == U
'!' &&
next == U
'=' )
1035 static constexpr std::array<std::pair<char32_t, TextEvalToken>, 11> single_char_tokens{
1049 if(
auto it = std::ranges::find_if( single_char_tokens,
1050 [current](
const auto& pair )
1052 return pair.first == current;
1054 it != single_char_tokens.end() )
1063 if( current == U
'"' || current == U
'\'' )
1107 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1116 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1124 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1128 bool aClearVariablesOnEvaluate ) :
1134 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1146 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1157 if(
this != &aOther )
1165 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1187 if(
this != &aOther )
1267 if( std::holds_alternative<double>( it->second ) )
1269 double val = std::get<double>( it->second );
1271 if( val == std::floor( val ) &&
std::abs( val ) < 1e15 )
1272 return wxString::Format(
"%.0f", val );
1274 return wxString::Format(
"%g", val );
1286 std::vector<wxString> names;
1297 for(
const auto& [
name, value] : aVariables )
1303 for(
const auto& [
name, value] : aVariables )
1309 std::unordered_map<wxString, double> emptyNumVars;
1310 std::unordered_map<wxString, wxString> emptyStringVars;
1311 return Evaluate( aInput, emptyNumVars, emptyStringVars );
1315 const std::unordered_map<wxString, double>& aTempVariables )
1317 std::unordered_map<wxString, wxString> emptyStringVars;
1318 return Evaluate( aInput, aTempVariables, emptyStringVars );
1322 const std::unordered_map<wxString, double>& aTempNumericVars,
1323 const std::unordered_map<wxString, wxString>& aTempStringVars )
1341 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1379 std::vector<wxString>
result;
1384 result.reserve( errors.size() );
1386 for(
const auto& error : errors )
1412 wxString testInput =
"@{" + aExpression +
"}";
1429 for(
const auto& error : errors )
1431 if( error.find(
"Syntax error" ) != std::string::npos
1432 || error.find(
"Parser failed" ) != std::string::npos )
1447 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1458 std::vector<wxString> expressions;
1461 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1463 size_t start = pos + 2;
1464 size_t end = aInput.find(
"}", start );
1466 if(
end != wxString::npos )
1468 expressions.push_back( aInput.substr( start,
end - start ) );
1482 return aWxStr.ToStdString( wxConvUTF8 );
1487 return wxString( aStdStr.c_str(), wxConvUTF8 );
1491 const wxString& aInput,
const std::unordered_map<wxString, double>& aTempNumericVars,
1492 const std::unordered_map<wxString, wxString>& aTempStringVars )
const
1494 wxString
result = aInput;
1498 std::vector<std::pair<size_t, size_t>> expressionRanges;
1501 while( ( pos =
result.find(
"@{", pos ) ) != std::string::npos )
1504 size_t braceCount = 1;
1505 size_t searchPos = start + 2;
1508 while( searchPos <
result.length() && braceCount > 0 )
1510 if(
result[searchPos] ==
'{' )
1512 else if(
result[searchPos] ==
'}' )
1517 if( braceCount == 0 )
1519 expressionRanges.emplace_back( start, searchPos );
1527 while( ( pos =
result.find(
"${", pos ) ) != std::string::npos )
1530 bool insideExpression =
false;
1531 for(
const auto& range : expressionRanges )
1533 if( pos >= range.first && pos < range.second )
1535 insideExpression =
true;
1540 if( insideExpression )
1544 size_t closePos =
result.find(
"}", pos + 2 );
1545 if( closePos != std::string::npos )
1548 size_t afterBrace = closePos + 1;
1549 bool followedByUnit =
false;
1551 if( afterBrace <
result.length() )
1555 for(
const auto& unit : units )
1557 if( afterBrace + unit.length() <=
result.length()
1558 &&
result.substr( afterBrace, unit.length() ) == unit )
1560 followedByUnit =
true;
1566 if( !followedByUnit )
1581 size_t closePos =
result.find(
"}", pos + 2 );
1582 if( closePos == std::string::npos )
1589 wxString varName =
result.substr( pos + 2, closePos - pos - 2 );
1590 wxString replacement;
1594 auto stringIt = aTempStringVars.find( varName );
1595 if( stringIt != aTempStringVars.end() )
1597 replacement = stringIt->second;
1603 auto numIt = aTempNumericVars.find( varName );
1604 if( numIt != aTempNumericVars.end() )
1606 replacement = wxString::FromDouble( numIt->second );
1617 if( std::holds_alternative<std::string>( value ) )
1622 else if( std::holds_alternative<double>( value ) )
1624 replacement = wxString::FromDouble( std::get<double>( value ) );
1634 result.replace( pos, closePos - pos + 1, replacement );
1635 pos += replacement.length();
1641 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1652 const std::unordered_map<wxString, wxString>* aTempStringVars )
const
1654 return [
this, aTempNumericVars,
1661 if( customResult.HasValue() )
1662 return customResult;
1670 if( aTempStringVars )
1673 if(
auto it = aTempStringVars->find( wxVarName ); it != aTempStringVars->end() )
1681 if( aTempNumericVars )
1684 if(
auto it = aTempNumericVars->find( wxVarName ); it != aTempNumericVars->end() )
1700 wxString testString = wxString::Format(
"${%s}", varName );
1703 bool wasResolved =
false;
1704 std::function<bool( wxString* )>
resolver = [&wasResolved]( wxString* token ) ->
bool
1718 std::function<bool( wxString* )> valueResolver = []( wxString* token ) ->
bool
1725 wxString resolvedValue =
ExpandTextVars( testString, &valueResolver );
1728 if( resolvedValue != testString )
1736 auto result = fast_float::from_chars( resolvedStd.data(),
1737 resolvedStd.data() + resolvedStd.size(), numValue );
1739 if(
result.ec != std::errc() ||
result.ptr != resolvedStd.data() + resolvedStd.size() )
1740 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number", resolvedStd ) );
1752 catch(
const std::exception& e )
1777 if( partialResult != aInput )
1780 return { std::move( partialResult ), partialHadErrors };
1786 catch(
const std::bad_alloc& )
1792 return { aInput,
true };
1794 catch(
const std::exception& e )
1798 m_lastErrors->AddError( fmt::format(
"Exception: {}", e.what() ) );
1800 return { aInput,
true };
1804std::pair<std::string, bool>
1807 std::string
result = aInput;
1808 bool hadAnyErrors =
false;
1812 std::vector<std::pair<size_t, size_t>> expressionRanges;
1815 while( ( pos =
result.find(
"@{", pos ) ) != std::string::npos )
1818 size_t exprStart = pos + 2;
1819 size_t braceCount = 1;
1820 size_t searchPos = exprStart;
1823 while( searchPos <
result.length() && braceCount > 0 )
1825 if(
result[searchPos] ==
'{' )
1829 else if(
result[searchPos] ==
'}' )
1836 if( braceCount == 0 )
1838 size_t end = searchPos;
1839 expressionRanges.emplace_back( start,
end );
1849 for(
auto it = expressionRanges.rbegin(); it != expressionRanges.rend(); ++it )
1851 auto [start,
end] = *it;
1852 std::string fullExpr =
result.substr( start,
end - start );
1853 std::string innerExpr =
result.substr( start + 2,
end - start - 3 );
1859 std::string testExpr =
"@{" + innerExpr +
"}";
1862 auto tempErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1869 if( !evalHadErrors )
1872 result.replace( start,
end - start, evalResult );
1877 hadAnyErrors =
true;
1881 oldErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1883 oldErrors->AddError( fmt::format(
"Failed to evaluate expression: {}", fullExpr ) );
1893 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1895 m_lastErrors->AddError( fmt::format(
"Exception in expression: {}", fullExpr ) );
1896 hadAnyErrors =
true;
1900 return { std::move(
result ), hadAnyErrors };
1906 if( aInput.empty() )
1908 return { std::string{},
false };
1912 struct ErrorCollectorGuard
1932 auto parser_deleter = [](
void* p )
1934 KI_EVAL::ParseFree( p, free );
1938 std::unique_ptr<void,
decltype( parser_deleter )> parser{ KI_EVAL::ParseAlloc( malloc ), parser_deleter };
1946 return { aInput,
true };
1957 token_type = tokenizer.get_next_token( token_value );
1960 KI_EVAL::Parse( parser.get(),
static_cast<int>( token_type ), token_value, &document );
1977 auto [
result, had_errors] = processor.
Process( *document, std::move( aVariableCallback ) );
1984 return { aInput,
true };
1988 return { std::move(
result ), had_errors };
1993 return { aInput,
true };
1995 catch(
const std::bad_alloc& )
2001 return { aInput,
true };
2003 catch(
const std::exception& e )
2007 m_lastErrors->AddError( fmt::format(
"Exception: {}", e.what() ) );
2009 return { aInput,
true };
2056 wxString processedExpression = aString;
2062 std::sort( varNames.begin(), varNames.end(),
2063 [](
const wxString& a,
const wxString& b )
2065 return a.length() > b.length();
2069 for(
const auto& varName : varNames )
2073 wxString pattern =
"\\b" + varName +
"\\b";
2074 wxString replacement =
"${" + varName +
"}";
2080 while( ( pos = processedExpression.find( varName, pos ) ) != wxString::npos )
2083 bool isWholeWord =
true;
2088 wxChar before = processedExpression[pos - 1];
2089 if( wxIsalnum( before ) || before ==
'_' || before ==
'$' )
2090 isWholeWord =
false;
2094 if( isWholeWord && pos + varName.length() < processedExpression.length() )
2096 wxChar after = processedExpression[pos + varName.length()];
2097 if( wxIsalnum( after ) || after ==
'_' )
2098 isWholeWord =
false;
2103 processedExpression.replace( pos, varName.length(), replacement );
2104 pos += replacement.length();
2108 pos += varName.length();
2114 wxString wrappedExpression =
"@{" + processedExpression +
"}";
2152 wxString value =
m_evaluator.GetVariable( aString );
2157 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
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
void advance_position(size_t count=1) noexcept
utf8_utils::SI_PREFIX_HANDLER SI_HANDLER
constexpr size_t get_column() const noexcept
void skip_whitespace() noexcept
bool has_more_tokens() const noexcept
calc_parser::ERROR_COLLECTOR * m_errorCollector
char32_t peek_char(size_t offset=1) const noexcept
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()
calc_parser::TOKEN_TYPE parse_identifier()
constexpr size_t get_line() const noexcept
static calc_parser::TOKEN_TYPE make_string_token(std::string value) noexcept
char32_t current_char() const 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.