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
90concept Utf8Byte = std::same_as<T, char> || std::same_as<T, unsigned char> || std::same_as<T, std::byte>;
98 static constexpr bool is_ascii( std::byte b )
noexcept
100 return ( b & std::byte{ 0x80 } ) == std::byte{ 0x00 };
105 return ( b & std::byte{ 0xC0 } ) == std::byte{ 0x80 };
113 if( ( first & std::byte{ 0xE0 } ) == std::byte{ 0xC0 } )
116 if( ( first & std::byte{ 0xF0 } ) == std::byte{ 0xE0 } )
119 if( ( first & std::byte{ 0xF8 } ) == std::byte{ 0xF0 } )
127 static std::u32string
to_utf32( std::string_view utf8 )
130 result.reserve( utf8.size() );
132 auto bytes = std::as_bytes( std::span{ utf8.data(), utf8.size() } );
134 for(
size_t i = 0; i < bytes.size(); )
136 std::byte first = bytes[i];
139 if( len == 0 || i + len > bytes.size() )
142 result.push_back( U
'\uFFFD' );
147 char32_t codepoint = 0;
151 case 1: codepoint = std::to_integer<char32_t>( first );
break;
157 result.push_back( U
'\uFFFD' );
161 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x1F } ) << 6 )
162 | std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } );
170 result.push_back( U
'\uFFFD' );
174 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x0F } ) << 12 )
175 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 6 )
176 | std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } );
185 result.push_back( U
'\uFFFD' );
189 codepoint = ( std::to_integer<char32_t>( first & std::byte{ 0x07 } ) << 18 )
190 | ( std::to_integer<char32_t>( bytes[i + 1] & std::byte{ 0x3F } ) << 12 )
191 | ( std::to_integer<char32_t>( bytes[i + 2] & std::byte{ 0x3F } ) << 6 )
192 | std::to_integer<char32_t>( bytes[i + 3] & std::byte{ 0x3F } );
198 if( codepoint > 0x10FFFF || ( codepoint >= 0xD800 && codepoint <= 0xDFFF ) )
200 result.push_back( U
'\uFFFD' );
202 else if( len == 2 && codepoint < 0x80 )
204 result.push_back( U
'\uFFFD' );
206 else if( len == 3 && codepoint < 0x800 )
208 result.push_back( U
'\uFFFD' );
210 else if( len == 4 && codepoint < 0x10000 )
212 result.push_back( U
'\uFFFD' );
216 result.push_back( codepoint );
226 static std::string
to_utf8( std::u32string_view utf32 )
229 result.reserve( utf32.size() * 4 );
231 for(
char32_t cp : utf32 )
236 result.push_back(
static_cast<char>( cp ) );
238 else if( cp <= 0x7FF )
241 result.push_back(
static_cast<char>( 0xC0 | ( cp >> 6 ) ) );
242 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
244 else if( cp <= 0xFFFF )
247 if( cp >= 0xD800 && cp <= 0xDFFF )
250 result.append(
"\uFFFD" );
254 result.push_back(
static_cast<char>( 0xE0 | ( cp >> 12 ) ) );
255 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
256 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
259 else if( cp <= 0x10FFFF )
262 result.push_back(
static_cast<char>( 0xF0 | ( cp >> 18 ) ) );
263 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 12 ) & 0x3F ) ) );
264 result.push_back(
static_cast<char>( 0x80 | ( ( cp >> 6 ) & 0x3F ) ) );
265 result.push_back(
static_cast<char>( 0x80 | ( cp & 0x3F ) ) );
270 result.append(
"\uFFFD" );
288 return cp == U
' ' || cp == U
'\t' || cp == U
'\r' || cp == U
'\n' || cp == U
'\f' || cp == U
'\v' || cp == U
'\u00A0'
290 cp == U
'\u2000' || cp == U
'\u2001' || cp == U
'\u2002' || cp == U
'\u2003' || cp == U
'\u2004'
291 || cp == U
'\u2005' || cp == U
'\u2006' || cp == U
'\u2007' || cp == U
'\u2008' || cp == U
'\u2009'
292 || cp == U
'\u200A' || cp == U
'\u2028' || cp == U
'\u2029' || cp == U
'\u202F' || cp == U
'\u205F'
298 return cp >= U
'0' && cp <= U
'9';
303 return ( cp >= U
'a' && cp <= U
'z' ) || ( cp >= U
'A' && cp <= U
'Z' );
327 static constexpr std::array<PREFIX, 18>
prefixes = { { { U
'a', 1e-18 },
345 return std::ranges::any_of(
prefixes,
354 auto it = std::ranges::find_if(
prefixes,
359 return it !=
prefixes.end() ? it->multiplier : 1.0;
440 if( aFromUnit == aToUnit )
493 size_t peek_pos =
m_pos + offset;
494 return peek_pos <
m_text.size() ?
m_text[peek_pos] : U
'\0';
499 for(
size_t i = 0; i < count &&
m_pos <
m_text.size(); ++i )
534 std::strncpy( token.text, value.c_str(),
sizeof( token.text ) - 1 );
535 token.text[
sizeof( token.text ) - 1] =
'\0';
544 token.dValue = value;
552 std::u32string content;
553 content.reserve( 64 );
567 content.push_back( U
'\n' );
570 content.push_back( U
'\t' );
573 content.push_back( U
'\r' );
576 content.push_back( U
'\\' );
579 content.push_back( U
'"' );
582 content.push_back( U
'\'' );
585 content.push_back( U
'\0' );
592 for(
int i = 0; i < 2 &&
m_pos <
m_text.size(); ++i )
595 if( ( hex_char >= U
'0' && hex_char <= U
'9' )
596 || ( hex_char >= U
'A' && hex_char <= U
'F' )
597 || ( hex_char >= U
'a' && hex_char <= U
'f' ) )
599 hex.push_back( hex_char );
613 unsigned long value = std::stoul( hex_str,
nullptr, 16 );
615 if( value <= 0x10FFFF )
616 content.push_back(
static_cast<char32_t>( value ) );
618 content.push_back( U
'\uFFFD' );
622 content.push_back( U
'\uFFFD' );
627 content.append( U
"\\x" );
633 content.push_back( U
'\\' );
634 content.push_back( escaped );
638 else if( c == U
'\n' )
640 add_error(
"Unterminated string literal" );
645 content.push_back( c );
656 add_error(
"Missing closing quote in string literal" );
664 std::u32string number_text;
665 number_text.reserve( 32 );
667 double multiplier = 1.0;
685 number_text.push_back( U
'.' );
692 std::u32string potential_unit;
693 size_t temp_pos =
m_pos;
695 while( temp_pos <
m_text.size() )
697 char32_t unit_char =
m_text[temp_pos];
701 potential_unit.push_back( unit_char );
711 if( !potential_unit.empty() )
755 if( c == U
'e' || c == U
'E' )
758 size_t temp_pos =
m_pos + 1;
759 bool is_scientific =
false;
761 if( temp_pos <
m_text.size() )
766 is_scientific =
true;
772 number_text.push_back( c );
794 add_error(
"Invalid scientific notation: missing exponent digits" );
808 std::u32string potential_unit;
809 size_t temp_pos =
m_pos;
811 while( temp_pos <
m_text.size() )
813 char32_t unit_char =
m_text[temp_pos];
817 potential_unit.push_back( unit_char );
826 if( !potential_unit.empty() )
853 if( !number_str.empty() && number_str !=
"." )
855 auto result = fast_float::from_chars( number_str.data(), number_str.data() + number_str.size(), value );
857 if(
result.ec != std::errc() ||
result.ptr != number_str.data() + number_str.size() )
858 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number", number_str ) );
862 if( !std::isfinite( value ) )
869 catch(
const std::exception& e )
871 add_error( fmt::format(
"Invalid number format: {}", e.what() ) );
879 size_t whitespace_start =
m_pos;
885 std::u32string unit_text;
895 unit_text.push_back( c );
904 if( !unit_text.empty() )
914 value = converted_value;
919 m_pos = whitespace_start;
925 m_pos = whitespace_start;
934 std::u32string identifier;
935 identifier.reserve( 64 );
957 if( ( current == U
'@' &&
next == U
'{' ) || ( current == U
'$' &&
next == U
'{' ) )
960 text.push_back( current );
996 if( current == U
'@' &&
next == U
'{' )
1005 if( current == U
'$' &&
next == U
'{' )
1015 if( current == U
'}' )
1031 if( current == U
'<' &&
next == U
'=' )
1037 if( current == U
'>' &&
next == U
'=' )
1043 if( current == U
'=' &&
next == U
'=' )
1049 if( current == U
'!' &&
next == U
'=' )
1060 static constexpr std::array<std::pair<char32_t, TEXT_EVAL_TOKEN>, 11> single_char_tokens{
1074 if(
auto it = std::ranges::find_if( single_char_tokens,
1075 [current](
const auto& pair )
1077 return pair.first == current;
1079 it != single_char_tokens.end() )
1088 if( current == U
'"' || current == U
'\'' )
1149 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1159 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1168 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1173 bool aClearVariablesOnEvaluate ) :
1179 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1193 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1206 if(
this != &aOther )
1214 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1238 if(
this != &aOther )
1332 if( std::holds_alternative<double>( it->second ) )
1334 double val = std::get<double>( it->second );
1337 if( val == std::floor( val ) &&
std::abs( val ) < 1e15 )
1338 return wxString::Format(
"%.0f", val );
1341 return wxString::Format(
"%.10g", val );
1355 std::vector<wxString> names;
1367 for(
const auto& [
name, value] : aVariables )
1374 for(
const auto& [
name, value] : aVariables )
1381 std::unordered_map<wxString, double> emptyNumVars;
1382 std::unordered_map<wxString, wxString> emptyStringVars;
1383 return Evaluate( aInput, emptyNumVars, emptyStringVars );
1388 const std::unordered_map<wxString, double>& aTempVariables )
1390 std::unordered_map<wxString, wxString> emptyStringVars;
1391 return Evaluate( aInput, aTempVariables, emptyStringVars );
1396 const std::unordered_map<wxString, double>& aTempNumericVars,
1397 const std::unordered_map<wxString, wxString>& aTempStringVars )
1414 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1454 std::vector<wxString>
result;
1458 const std::vector<std::string>& errors =
m_lastErrors->GetErrors();
1459 result.reserve( errors.size() );
1461 for(
const std::string& error : errors )
1491 wxString testInput =
"@{" + aExpression +
"}";
1509 for(
const std::string& error :
m_lastErrors->GetErrors() )
1511 if( error.find(
"Syntax error" ) != std::string::npos
1512 || error.find(
"Parser failed" ) != std::string::npos )
1528 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1540 std::vector<wxString> expressions;
1543 while( ( pos = aInput.find(
"@{", pos ) ) != wxString::npos )
1545 size_t start = pos + 2;
1546 size_t end = aInput.find(
"}", start );
1548 if(
end != wxString::npos )
1550 expressions.push_back( aInput.substr( start,
end - start ) );
1565 return aWxStr.ToStdString( wxConvUTF8 );
1571 return wxString( aStdStr.c_str(), wxConvUTF8 );
1576 const wxString& aInput,
1577 const std::unordered_map<wxString, double>& aTempNumericVars,
1578 const std::unordered_map<wxString, wxString>& aTempStringVars )
const
1580 wxString
result = aInput;
1584 std::vector<std::pair<size_t, size_t>> expressionRanges;
1587 while( ( pos =
result.find(
"@{", pos ) ) != std::string::npos )
1590 size_t braceCount = 1;
1591 size_t searchPos = start + 2;
1594 while( searchPos <
result.length() && braceCount > 0 )
1596 if(
result[searchPos] ==
'{' )
1598 else if(
result[searchPos] ==
'}' )
1603 if( braceCount == 0 )
1604 expressionRanges.emplace_back( start, searchPos );
1612 while( ( pos =
result.find(
"${", pos ) ) != std::string::npos )
1615 bool insideExpression =
false;
1617 for(
const auto& range : expressionRanges )
1619 if( pos >= range.first && pos < range.second )
1621 insideExpression =
true;
1626 if( insideExpression )
1630 size_t closePos =
result.find(
"}", pos + 2 );
1632 if( closePos != std::string::npos )
1635 size_t afterBrace = closePos + 1;
1636 bool followedByUnit =
false;
1638 if( afterBrace <
result.length() )
1643 for(
const auto& unit : units )
1645 if( afterBrace + unit.length() <=
result.length()
1646 &&
result.substr( afterBrace, unit.length() ) == unit )
1648 followedByUnit =
true;
1654 if( !followedByUnit )
1669 size_t closePos =
result.find(
"}", pos + 2 );
1671 if( closePos == std::string::npos )
1678 wxString varName =
result.substr( pos + 2, closePos - pos - 2 );
1679 wxString replacement;
1683 auto stringIt = aTempStringVars.find( varName );
1685 if( stringIt != aTempStringVars.end() )
1687 replacement = stringIt->second;
1693 auto numIt = aTempNumericVars.find( varName );
1695 if( numIt != aTempNumericVars.end() )
1697 replacement = wxString::FromDouble( numIt->second );
1710 if( std::holds_alternative<std::string>( value ) )
1715 else if( std::holds_alternative<double>( value ) )
1717 replacement = wxString::FromDouble( std::get<double>( value ) );
1727 result.replace( pos, closePos - pos + 1, replacement );
1728 pos += replacement.length();
1734 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1747 const std::unordered_map<wxString, wxString>* aTempStringVars )
const
1749 return [
this, aTempNumericVars, aTempStringVars]
1757 if( customResult.HasValue() )
1758 return customResult;
1766 if( aTempStringVars )
1770 if(
auto it = aTempStringVars->find( wxVarName ); it != aTempStringVars->end() )
1778 if( aTempNumericVars )
1782 if(
auto it = aTempNumericVars->find( wxVarName ); it != aTempNumericVars->end() )
1796 wxString testString = wxString::Format(
"${%s}", varName );
1799 bool wasResolved =
false;
1800 std::function<bool( wxString* )> resolver =
1801 [&wasResolved]( wxString* token ) ->
bool
1815 std::function<bool( wxString* )> valueResolver =
1816 []( wxString* token ) ->
bool
1826 if( resolvedValue != testString )
1834 auto result = fast_float::from_chars( resolvedStd.data(),
1835 resolvedStd.data() + resolvedStd.size(),
1838 if(
result.ec != std::errc()
1839 ||
result.ptr != resolvedStd.data() + resolvedStd.size() )
1841 throw std::invalid_argument( fmt::format(
"Cannot convert '{}' to number",
1855 catch(
const std::exception& )
1881 if( partialResult != aInput )
1884 return { std::move( partialResult ), partialHadErrors };
1890 catch(
const std::bad_alloc& )
1895 return { aInput,
true };
1897 catch(
const std::exception& e )
1900 m_lastErrors->AddError( fmt::format(
"Exception: {}", e.what() ) );
1902 return { aInput,
true };
1907std::pair<std::string, bool>
1910 std::string
result = aInput;
1911 bool hadAnyErrors =
false;
1915 std::vector<std::pair<size_t, size_t>> expressionRanges;
1918 while( ( pos =
result.find(
"@{", pos ) ) != std::string::npos )
1921 size_t exprStart = pos + 2;
1922 size_t braceCount = 1;
1923 size_t searchPos = exprStart;
1926 while( searchPos <
result.length() && braceCount > 0 )
1928 if(
result[searchPos] ==
'{' )
1930 else if(
result[searchPos] ==
'}' )
1936 if( braceCount == 0 )
1938 size_t end = searchPos;
1939 expressionRanges.emplace_back( start,
end );
1949 for(
auto it = expressionRanges.rbegin(); it != expressionRanges.rend(); ++it )
1951 auto [start,
end] = *it;
1952 std::string fullExpr =
result.substr( start,
end - start );
1953 std::string innerExpr =
result.substr( start + 2,
end - start - 3 );
1959 std::string testExpr =
"@{" + innerExpr +
"}";
1962 auto tempErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1969 if( !evalHadErrors )
1972 result.replace( start,
end - start, evalResult );
1977 hadAnyErrors =
true;
1981 oldErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1983 oldErrors->AddError( fmt::format(
"Failed to evaluate expression: {}", fullExpr ) );
1993 m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
1995 m_lastErrors->AddError( fmt::format(
"Exception in expression: {}", fullExpr ) );
1996 hadAnyErrors =
true;
2000 return { std::move(
result ), hadAnyErrors };
2007 if( aInput.empty() )
2009 return { std::string{},
false };
2018 m_document( aDocument )
2028 } guard( document );
2043 auto parser_deleter =
2046 KI_EVAL::ParseFree( p, free );
2050 std::unique_ptr<void,
decltype( parser_deleter )> parser{ KI_EVAL::ParseAlloc( malloc ), parser_deleter };
2057 return { aInput,
true };
2066 token_type = tokenizer.get_next_token( token_value );
2069 KI_EVAL::Parse( parser.get(), (
int) token_type, token_value, &document );
2084 std::move( aVariableCallback ) );
2089 return { aInput,
true };
2091 return { std::move(
result ), had_errors };
2095 return { aInput,
true };
2097 catch(
const std::bad_alloc& )
2102 return { aInput,
true };
2104 catch(
const std::exception& e )
2107 m_lastErrors->AddError( fmt::format(
"Exception: {}", e.what() ) );
2109 return { aInput,
true };
2164 wxString processedExpression = aString;
2167 std::vector<wxString> varNames =
m_evaluator.GetVariableNames();
2170 std::sort( varNames.begin(), varNames.end(),
2171 [](
const wxString& a,
const wxString& b )
2173 return a.length() > b.length();
2177 for(
const wxString& varName : varNames )
2181 wxString pattern =
"\\b" + varName +
"\\b";
2182 wxString replacement =
"${" + varName +
"}";
2188 while( ( pos = processedExpression.find( varName, pos ) ) != wxString::npos )
2191 bool isWholeWord =
true;
2196 wxChar before = processedExpression[pos - 1];
2198 if( wxIsalnum( before ) || before ==
'_' || before ==
'$' )
2199 isWholeWord =
false;
2203 if( isWholeWord && pos + varName.length() < processedExpression.length() )
2205 wxChar after = processedExpression[pos + varName.length()];
2207 if( wxIsalnum( after ) || after ==
'_' )
2208 isWholeWord =
false;
2213 processedExpression.replace( pos, varName.length(), replacement );
2214 pos += replacement.length();
2218 pos += varName.length();
2224 wxString wrappedExpression =
"@{" + processedExpression +
"}";
2265 wxString value =
m_evaluator.GetVariable( aString );
2270 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.
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()
Destructor.
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.
VAR_CALLBACK m_customCallback
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.
bool m_clearVariablesOnEvaluate
EXPRESSION_EVALUATOR(bool aClearVariablesOnEvaluate=false)
Construct a new Expression Evaluator in static variable mode.
TOKENIZER_CONTEXT m_context
static calc_parser::TOKEN_TYPE make_string_token(const std::string &value) noexcept
calc_parser::TOKEN_TYPE parse_number()
TEXT_EVAL_TOKEN get_next_token(calc_parser::TOKEN_TYPE &token_value)
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)
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
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 std::pair< std::string, bool > Process(const DOC &aDoc, VAR_CALLBACK aVariableCallback)
Process document using callback for variable resolution.
const std::vector< std::string > & GetErrors() const
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, RESOLUTION_CONTEXT aContext)
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
RESULT< T > MakeError(std::string aMsg)
std::variant< double, std::string > VALUE
RESULT< T > MakeValue(T aVal)
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
static const long long MM
wxString result
Test unit parsing edge cases and error handling.