From 519bc80394470f72f16d3145eed5db361f575aa6 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 13 Aug 2020 18:47:41 +0100 Subject: [PATCH] Replace error reporting in DRC rule compiler. --- common/libeval_compiler/libeval_compiler.cpp | 14 +------------- include/libeval_compiler/libeval_compiler.h | 12 ++++++++---- pcbnew/drc/drc_rule.cpp | 16 +++++++++++++++- pcbnew/drc/drc_rule_parser.cpp | 18 +++++++++++++++--- pcbnew/pcb_expr_evaluator.h | 8 +++++++- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/common/libeval_compiler/libeval_compiler.cpp b/common/libeval_compiler/libeval_compiler.cpp index 3dec8c25cd..de7ab61339 100644 --- a/common/libeval_compiler/libeval_compiler.cpp +++ b/common/libeval_compiler/libeval_compiler.cpp @@ -615,25 +615,13 @@ void COMPILER::reportError( COMPILATION_STAGE stage, const wxString& aErrorMsg, if( aPos == -1 ) aPos = m_sourcePos; -// fixme: no HTML or anything UI-related here. - -#if 0 - wxString rest; - wxString first = aErrorMsg.BeforeFirst( '|', &rest ); - wxString msg = wxString::Format( _( "ERROR: %s%s" ), - m_originLine, - m_originOffset + aPos, - first, - rest ); -#endif - m_errorStatus.pendingError = true; m_errorStatus.stage = stage; m_errorStatus.message = aErrorMsg; m_errorStatus.srcPos = aPos; if( m_errorCallback ) - m_errorCallback( m_errorStatus ); + m_errorCallback( aErrorMsg, aPos ); } diff --git a/include/libeval_compiler/libeval_compiler.h b/include/libeval_compiler/libeval_compiler.h index f4cd1fa85b..4c33e1e6a5 100644 --- a/include/libeval_compiler/libeval_compiler.h +++ b/include/libeval_compiler/libeval_compiler.h @@ -429,7 +429,11 @@ public: bool Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflightContext ); - void SetErrorCallback( std::function aCallback ); + void SetErrorCallback( std::function aCallback ) + { + m_errorCallback = aCallback; + } + bool IsErrorPending() const { return m_errorStatus.pendingError; } const ERROR_STATUS& GetError() const { return m_errorStatus; } @@ -454,8 +458,8 @@ protected: /* Tokenizer: Next token/value taken from input string. */ T_TOKEN getToken(); - bool lexDefault( T_TOKEN& aToken ); - bool lexString( T_TOKEN& aToken ); + bool lexDefault( T_TOKEN& aToken ); + bool lexString( T_TOKEN& aToken ); int resolveUnits(); @@ -474,7 +478,7 @@ protected: ERROR_STATUS m_errorStatus; std::vector m_gcItems; std::vector m_gcStrings; - std::function m_errorCallback; + std::function m_errorCallback; }; diff --git a/pcbnew/drc/drc_rule.cpp b/pcbnew/drc/drc_rule.cpp index a3865a9d41..7acbc3fecf 100644 --- a/pcbnew/drc/drc_rule.cpp +++ b/pcbnew/drc/drc_rule.cpp @@ -25,7 +25,7 @@ #include #include #include - +#include #include #include @@ -119,7 +119,21 @@ bool DRC_RULE_CONDITION::EvaluateFor( const BOARD_ITEM* aItemA, const BOARD_ITEM bool DRC_RULE_CONDITION::Compile( REPORTER* aReporter, int aSourceLine, int aSourceOffset ) { + auto errorHandler = [&]( const wxString& aMessage, int aOffset ) + { + wxString rest; + wxString first = aMessage.BeforeFirst( '|', &rest ); + wxString msg = wxString::Format( _( "ERROR: %s%s" ), + aSourceLine, + aSourceOffset + aOffset, + first, + rest ); + + aReporter->Report( msg, RPT_SEVERITY_ERROR ); + }; + PCB_EXPR_COMPILER compiler; + compiler.SetErrorCallback( errorHandler ); if (!m_ucode) m_ucode = new PCB_EXPR_UCODE; diff --git a/pcbnew/drc/drc_rule_parser.cpp b/pcbnew/drc/drc_rule_parser.cpp index 4c4675d50f..bf7a599d36 100644 --- a/pcbnew/drc/drc_rule_parser.cpp +++ b/pcbnew/drc/drc_rule_parser.cpp @@ -376,10 +376,22 @@ void DRC_RULES_PARSER::parseConstraint( DRC_RULE* aRule ) void DRC_RULES_PARSER::parseValueWithUnits( const wxString& aExpr, int& aResult ) { - PCB_EXPR_EVALUATOR evaluator; - - // m_reporter, CurLineNumber(), CurOffset() ); + auto errorHandler = [&]( const wxString& aMessage, int aOffset ) + { + wxString rest; + wxString first = aMessage.BeforeFirst( '|', &rest ); + wxString msg = wxString::Format( _( "ERROR: %s%s" ), + CurLineNumber(), + CurOffset() + aOffset, + first, + rest ); + m_reporter->Report( msg, RPT_SEVERITY_ERROR ); + }; + + PCB_EXPR_EVALUATOR evaluator; + evaluator.SetErrorCallback( errorHandler ); + evaluator.Evaluate( aExpr ); aResult = evaluator.Result(); } diff --git a/pcbnew/pcb_expr_evaluator.h b/pcbnew/pcb_expr_evaluator.h index f55d08b9fa..260e71c8cd 100644 --- a/pcbnew/pcb_expr_evaluator.h +++ b/pcbnew/pcb_expr_evaluator.h @@ -75,7 +75,7 @@ public: } private: - BOARD_ITEM* m_items[2]; + BOARD_ITEM* m_items[2]; PCB_LAYER_ID m_layer; }; @@ -159,6 +159,12 @@ public: bool Evaluate( const wxString& aExpr ); int Result() const { return m_result; } + + void SetErrorCallback( std::function aCallback ) + { + m_compiler.SetErrorCallback( aCallback ); + } + bool IsErrorPending() const { return m_errorStatus.pendingError; } const LIBEVAL::ERROR_STATUS& GetError() const { return m_errorStatus; }