Browse Source

Move to fmt

pull/19/head
Seth Hillbrand 3 months ago
parent
commit
44cc5b8e93
  1. 2
      common/text_eval/text_eval.lemon
  2. 29
      common/text_eval/text_eval_parser.cpp
  3. 22
      common/text_eval/text_eval_wrapper.cpp
  4. 8
      include/text_eval/text_eval_parser.h
  5. 8
      include/text_eval/text_eval_types.h

2
common/text_eval/text_eval.lemon

@ -147,7 +147,7 @@ factor(F) ::= NUMBER(N). {
{
if (g_errorCollector)
{
g_errorCollector->AddError( std::format( "Invalid number format: {}", GetTokenString( N ) ) );
g_errorCollector->AddError( fmt::format( "Invalid number format: {}", GetTokenString( N ) ) );
}
F = NODE::CreateNumberRaw( 0.0 );
}

29
common/text_eval/text_eval_parser.cpp

@ -18,6 +18,7 @@
*/
#include <text_eval/text_eval_parser.h>
#include <fmt/format.h>
#include <array>
#include <cctype>
@ -274,23 +275,23 @@ public:
auto [year, month, day] = DaysToYmd( aDaysSinceEpoch );
if( aFormat == "ISO" || aFormat == "iso" )
return std::format( "{:04d}-{:02d}-{:02d}", year, month, day );
return fmt::format( "{:04d}-{:02d}-{:02d}", year, month, day );
else if( aFormat == "US" || aFormat == "us" )
return std::format( "{:02d}/{:02d}/{:04d}", month, day, year );
return fmt::format( "{:02d}/{:02d}/{:04d}", month, day, year );
else if( aFormat == "EU" || aFormat == "european" )
return std::format( "{:02d}/{:02d}/{:04d}", day, month, year );
return fmt::format( "{:02d}/{:02d}/{:04d}", day, month, year );
else if( aFormat == "long" )
return std::format( "{} {}, {}", monthNames[month-1], day, year );
return fmt::format( "{} {}, {}", monthNames[month-1], day, year );
else if( aFormat == "short" )
return std::format( "{} {}, {}", monthAbbrev[month-1], day, year );
return fmt::format( "{} {}, {}", monthAbbrev[month-1], day, year );
else if( aFormat == "Chinese" || aFormat == "chinese" || aFormat == "CN" || aFormat == "cn" || aFormat == "中文" )
return std::format( "{}年{:02d}月{:02d}日", year, month, day );
return fmt::format( "{}年{:02d}月{:02d}日", year, month, day );
else if( aFormat == "Japanese" || aFormat == "japanese" || aFormat == "JP" || aFormat == "jp" || aFormat == "日本語" )
return std::format( "{}年{:02d}月{:02d}日", year, month, day );
return fmt::format( "{}年{:02d}月{:02d}日", year, month, day );
else if( aFormat == "Korean" || aFormat == "korean" || aFormat == "KR" || aFormat == "kr" || aFormat == "한국어" )
return std::format( "{}년 {:02d}월 {:02d}일", year, month, day );
return fmt::format( "{}년 {:02d}월 {:02d}일", year, month, day );
else
return std::format( "{:04d}-{:02d}-{:02d}", year, month, day );
return fmt::format( "{:04d}-{:02d}-{:02d}", year, month, day );
}
static auto GetWeekdayName( int aDaysSinceEpoch ) -> std::string
@ -342,7 +343,7 @@ auto EVAL_VISITOR::operator()( const NODE& aNode ) const -> Result<Value>
if( m_variableCallback )
return m_variableCallback( varName );
return MakeError<Value>( std::format( "No variable resolver configured for: {}", varName ) );
return MakeError<Value>( fmt::format( "No variable resolver configured for: {}", varName ) );
}
case NodeType::BinOp:
@ -436,7 +437,7 @@ auto EVAL_VISITOR::evaluateFunction( const FUNC_DATA& aFunc ) const -> Result<Va
decimals = static_cast<int>( decResult.GetValue() );
}
return MakeValue<Value>( std::format( "{:.{}f}", value, decimals ) );
return MakeValue<Value>( fmt::format( "{:.{}f}", value, decimals ) );
}
else if( name == "currency" && argc >= 1 )
{
@ -447,7 +448,7 @@ auto EVAL_VISITOR::evaluateFunction( const FUNC_DATA& aFunc ) const -> Result<Va
const auto amount = numResult.GetValue();
const auto symbol = argc > 1 ? VALUE_UTILS::ToString( argValues[1] ) : "$";
return MakeValue<Value>( std::format( "{}{:.2f}", symbol, amount ) );
return MakeValue<Value>( fmt::format( "{}{:.2f}", symbol, amount ) );
}
else if( name == "fixed" && argc >= 1 )
{
@ -464,7 +465,7 @@ auto EVAL_VISITOR::evaluateFunction( const FUNC_DATA& aFunc ) const -> Result<Va
decimals = static_cast<int>( decResult.GetValue() );
}
return MakeValue<Value>( std::format( "{:.{}f}", value, decimals ) );
return MakeValue<Value>( fmt::format( "{:.{}f}", value, decimals ) );
}
// Date formatting functions (return strings!)
@ -579,7 +580,7 @@ auto EVAL_VISITOR::evaluateFunction( const FUNC_DATA& aFunc ) const -> Result<Va
return MakeValue<Value>( sum / static_cast<double>( argc ) );
}
return MakeError<Value>( std::format( "Unknown function: {} with {} arguments", name, argc ) );
return MakeError<Value>( fmt::format( "Unknown function: {} with {} arguments", name, argc ) );
}
auto DOC_PROCESSOR::Process( const DOC& aDoc, VariableCallback aVariableCallback )

22
common/text_eval/text_eval_wrapper.cpp

@ -18,6 +18,7 @@
*/
#include <text_eval/text_eval_wrapper.h>
#include <fmt/format.h>
// Include the KiCad common functionality
#include <common.h>
@ -45,7 +46,6 @@ namespace KI_EVAL
#include <wx/log.h>
#include <algorithm>
#include <format>
#include <regex>
// // Token type enum matching the generated parser
@ -507,7 +507,7 @@ private:
{
if( m_errorCollector )
{
auto error_msg = std::format( "Line {}, Column {}: {}", m_line, m_column, message );
auto error_msg = fmt::format( "Line {}, Column {}: {}", m_line, m_column, message );
m_errorCollector->AddError( error_msg );
}
}
@ -733,7 +733,7 @@ private:
auto result = fast_float::from_chars( number_str.data(), number_str.data() + number_str.size(), value );
if( result.ec != std::errc() || result.ptr != number_str.data() + number_str.size() )
throw std::invalid_argument( std::format( "Cannot convert '{}' to number", number_str ) );
throw std::invalid_argument( fmt::format( "Cannot convert '{}' to number", number_str ) );
value *= multiplier;
@ -746,7 +746,7 @@ private:
}
catch( const std::exception& e )
{
add_error( std::format( "Invalid number format: {}", e.what() ) );
add_error( fmt::format( "Invalid number format: {}", e.what() ) );
value = 0.0;
}
@ -1544,7 +1544,7 @@ wxString EXPRESSION_EVALUATOR::expandVariablesOutsideExpressions(
// Variable not found, record error but leave ${variable} unchanged
if( !m_lastErrors )
m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
m_lastErrors->AddError( std::format( "Undefined variable: {}", wxStringToStdString( varName ) ) );
m_lastErrors->AddError( fmt::format( "Undefined variable: {}", wxStringToStdString( varName ) ) );
pos = closePos + 1;
}
}
@ -1642,7 +1642,7 @@ EXPRESSION_EVALUATOR::VariableCallback EXPRESSION_EVALUATOR::createCombinedCallb
auto result = fast_float::from_chars( resolvedStd.data(), resolvedStd.data() + resolvedStd.size(), numValue );
if( result.ec != std::errc() || result.ptr != resolvedStd.data() + resolvedStd.size() )
throw std::invalid_argument( std::format( "Cannot convert '{}' to number", resolvedStd ) );
throw std::invalid_argument( fmt::format( "Cannot convert '{}' to number", resolvedStd ) );
return calc_parser::MakeValue<calc_parser::Value>( numValue );
}
@ -1667,7 +1667,7 @@ EXPRESSION_EVALUATOR::VariableCallback EXPRESSION_EVALUATOR::createCombinedCallb
// No variable found anywhere
return calc_parser::MakeError<calc_parser::Value>(
std::format( "Undefined variable: {}", aVarName ) );
fmt::format( "Undefined variable: {}", aVarName ) );
};
}
@ -1695,7 +1695,7 @@ std::pair<std::string, bool> EXPRESSION_EVALUATOR::evaluateWithParser(
return {aInput, true};
} catch (const std::exception& e) {
if (m_lastErrors) {
m_lastErrors->AddError(std::format("Exception: {}", e.what()));
m_lastErrors->AddError(fmt::format("Exception: {}", e.what()));
}
return {aInput, true};
}
@ -1781,7 +1781,7 @@ std::pair<std::string, bool> EXPRESSION_EVALUATOR::evaluateWithPartialErrorRecov
if( !oldErrors )
oldErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
oldErrors->AddError( std::format( "Failed to evaluate expression: {}", fullExpr ) );
oldErrors->AddError( fmt::format( "Failed to evaluate expression: {}", fullExpr ) );
}
// Restore the main error collector
@ -1793,7 +1793,7 @@ std::pair<std::string, bool> EXPRESSION_EVALUATOR::evaluateWithPartialErrorRecov
if( !m_lastErrors )
m_lastErrors = std::make_unique<calc_parser::ERROR_COLLECTOR>();
m_lastErrors->AddError( std::format( "Exception in expression: {}", fullExpr ) );
m_lastErrors->AddError( fmt::format( "Exception in expression: {}", fullExpr ) );
hadAnyErrors = true;
}
}
@ -1905,7 +1905,7 @@ std::pair<std::string, bool> EXPRESSION_EVALUATOR::evaluateWithFullParser( const
{
if( m_lastErrors )
{
m_lastErrors->AddError( std::format( "Exception: {}", e.what() ) );
m_lastErrors->AddError( fmt::format( "Exception: {}", e.what() ) );
}
return { aInput, true };
}

8
include/text_eval/text_eval_parser.h

@ -27,7 +27,7 @@
#include <variant>
#include <concepts>
#include <ranges>
#include <format>
#include <fmt/format.h>
#include <optional>
#include <cassert>
#include <cmath>
@ -110,7 +110,7 @@ namespace calc_parser
}
catch( ... )
{
return MakeError<double>( std::format( "Cannot convert '{}' to number", str ) );
return MakeError<double>( fmt::format( "Cannot convert '{}' to number", str ) );
}
}
@ -128,9 +128,9 @@ namespace calc_parser
// If the number is very close to a whole number, treat it as such
if( std::abs( num - rounded ) < tolerance && std::abs( rounded ) < 1e15 )
return std::format( "{:.0f}", rounded );
return fmt::format( "{:.0f}", rounded );
return std::format( "{}", num );
return fmt::format( "{}", num );
}
// Check if Value represents a "truthy" value for conditionals

8
include/text_eval/text_eval_types.h

@ -22,7 +22,7 @@
#include <string>
#include <variant>
#include <vector>
#include <format>
#include <fmt/format.h>
namespace calc_parser
{
@ -79,7 +79,7 @@ namespace calc_parser
auto AddSyntaxError( int aLine = -1, int aColumn = -1 ) -> void
{
if( aLine >= 0 && aColumn >= 0 )
AddError( std::format( "Syntax error at line {}, column {}", aLine, aColumn ) );
AddError( fmt::format( "Syntax error at line {}, column {}", aLine, aColumn ) );
else
AddError( "Syntax error in calculation expression" );
}
@ -98,10 +98,10 @@ namespace calc_parser
{
std::string result;
for( const auto& error : m_errors )
result += std::format( "Error: {}\n", error );
result += fmt::format( "Error: {}\n", error );
for( const auto& warning : m_warnings )
result += std::format( "Warning: {}\n", warning );
result += fmt::format( "Warning: {}\n", warning );
return result;
}

Loading…
Cancel
Save