KiCad PCB EDA Suite
LIBEVAL::COMPILER Class Reference

#include <libeval_compiler.h>

Inheritance diagram for LIBEVAL::COMPILER:
PCB_EXPR_COMPILER

Public Member Functions

 COMPILER ()
 
virtual ~COMPILER ()
 
void Clear ()
 
void parseError (const char *s)
 
void parseOk ()
 
int GetSourcePos () const
 
void setRoot (LIBEVAL::TREE_NODE *root)
 
void freeTree (LIBEVAL::TREE_NODE *tree)
 
bool Compile (const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
 
void SetErrorCallback (std::function< void(const wxString &aMessage, int aOffset)> aCallback)
 
bool IsErrorPending () const
 
const ERROR_STATUSGetError () const
 
void GcItem (TREE_NODE *aItem)
 
void GcItem (wxString *aItem)
 

Protected Types

enum  LEXER_STATE { LS_DEFAULT = 0, LS_STRING = 1 }
 

Protected Member Functions

bool generateUCode (UCODE *aCode, CONTEXT *aPreflightContext)
 
void reportError (COMPILATION_STAGE stage, const wxString &aErrorMsg, int aPos=-1)
 
void newString (const wxString &aString)
 
T_TOKEN getToken ()
 
bool lexDefault (T_TOKEN &aToken)
 
bool lexString (T_TOKEN &aToken)
 
int resolveUnits ()
 

Protected Attributes

LEXER_STATE m_lexerState
 
void * m_parser
 
TOKENIZER m_tokenizer
 
char m_localeDecimalSeparator
 
std::unique_ptr< UNIT_RESOLVERm_unitResolver
 
int m_sourcePos
 
bool m_parseFinished
 
ERROR_STATUS m_errorStatus
 
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback
 
TREE_NODEm_tree
 
std::vector< TREE_NODE * > m_gcItems
 
std::vector< wxString * > m_gcStrings
 

Detailed Description

Definition at line 435 of file libeval_compiler.h.

Member Enumeration Documentation

◆ LEXER_STATE

Enumerator
LS_DEFAULT 
LS_STRING 

Definition at line 470 of file libeval_compiler.h.

Constructor & Destructor Documentation

◆ COMPILER()

LIBEVAL::COMPILER::COMPILER ( )

Definition at line 218 of file libeval_compiler.cpp.

218  :
220 {
222  m_sourcePos = 0;
223  m_parseFinished = false;
224  m_unitResolver = std::make_unique<UNIT_RESOLVER>();
225  m_parser = LIBEVAL::ParseAlloc( malloc );
226  m_tree = nullptr;
227  m_errorStatus.pendingError = false;
228 }
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
LEXER_STATE m_lexerState
ERROR_STATUS m_errorStatus

References m_errorStatus, m_localeDecimalSeparator, m_parseFinished, m_parser, m_sourcePos, m_tree, m_unitResolver, and LIBEVAL::ERROR_STATUS::pendingError.

◆ ~COMPILER()

LIBEVAL::COMPILER::~COMPILER ( )
virtual

Definition at line 231 of file libeval_compiler.cpp.

232 {
233  LIBEVAL::ParseFree( m_parser, free );
234 
235  if( m_tree )
236  {
237  freeTree( m_tree );
238  }
239 
240  // Allow explicit call to destructor
241  m_parser = nullptr;
242 
243  Clear();
244 }
void freeTree(LIBEVAL::TREE_NODE *tree)

References Clear(), freeTree(), m_parser, and m_tree.

Member Function Documentation

◆ Clear()

void LIBEVAL::COMPILER::Clear ( )

Definition at line 247 of file libeval_compiler.cpp.

248 {
249  //free( current.token );
250  m_tokenizer.Clear();
251 
252  if( m_tree )
253  {
254  freeTree( m_tree );
255  m_tree = nullptr;
256  }
257 
258  m_tree = nullptr;
259 
260  for( auto tok : m_gcItems )
261  delete tok;
262 
263  for( auto tok: m_gcStrings )
264  delete tok;
265 
266  m_gcItems.clear();
267  m_gcStrings.clear();
268 }
std::vector< TREE_NODE * > m_gcItems
void freeTree(LIBEVAL::TREE_NODE *tree)
std::vector< wxString * > m_gcStrings

References LIBEVAL::TOKENIZER::Clear(), freeTree(), m_gcItems, m_gcStrings, m_tokenizer, and m_tree.

Referenced by newString(), and ~COMPILER().

◆ Compile()

bool LIBEVAL::COMPILER::Compile ( const wxString &  aString,
UCODE aCode,
CONTEXT aPreflightContext 
)

Definition at line 283 of file libeval_compiler.cpp.

284 {
285  // Feed parser token after token until end of input.
286 
287  newString( aString );
288 
289  if( m_tree )
290  {
291  freeTree( m_tree );
292  m_tree = nullptr;
293  }
294 
295  m_tree = nullptr;
296  m_parseFinished = false;
297  T_TOKEN tok( defaultToken );
298 
299  libeval_dbg(0, "str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
300 
301  if( aString.empty() )
302  {
303  m_parseFinished = true;
304  return generateUCode( aCode, aPreflightContext );
305  }
306 
307  do
308  {
310 
311  tok = getToken();
312 
313  if( tok.value.str )
314  GcItem( tok.value.str );
315 
316  libeval_dbg(10, "parse: tok %d valstr %p\n", tok.token, tok.value.str );
317  Parse( m_parser, tok.token, tok, this );
318 
320  return false;
321 
322  if( m_parseFinished || tok.token == G_ENDS )
323  {
324  // Reset parser by passing zero as token ID, value is ignored.
325  Parse( m_parser, 0, tok, this );
326  break;
327  }
328  } while( tok.token );
329 
330  return generateUCode( aCode, aPreflightContext );
331 }
bool generateUCode(UCODE *aCode, CONTEXT *aPreflightContext)
void freeTree(LIBEVAL::TREE_NODE *tree)
#define libeval_dbg(level, fmt,...)
constexpr T_TOKEN defaultToken
void GcItem(TREE_NODE *aItem)
void newString(const wxString &aString)
size_t GetPos() const
ERROR_STATUS m_errorStatus

References LIBEVAL::defaultToken, freeTree(), GcItem(), generateUCode(), LIBEVAL::TOKENIZER::GetPos(), getToken(), libeval_dbg, m_errorStatus, m_parseFinished, m_parser, m_sourcePos, m_tokenizer, m_tree, newString(), LIBEVAL::ERROR_STATUS::pendingError, LIBEVAL::T_TOKEN_VALUE::str, LIBEVAL::T_TOKEN::token, and LIBEVAL::T_TOKEN::value.

Referenced by DRC_RULE_CONDITION::Compile(), PCB_EXPR_EVALUATOR::Evaluate(), and testEvalExpr().

◆ freeTree()

void LIBEVAL::COMPILER::freeTree ( LIBEVAL::TREE_NODE tree)

Definition at line 677 of file libeval_compiler.cpp.

678 {
679  if ( tree->leaf[0] )
680  freeTree( tree->leaf[0] );
681 
682  if ( tree->leaf[1] )
683  freeTree( tree->leaf[1] );
684 
685  delete tree->uop;
686 }
void freeTree(LIBEVAL::TREE_NODE *tree)
TREE_NODE * leaf[2]

References LIBEVAL::TREE_NODE::leaf, and LIBEVAL::TREE_NODE::uop.

Referenced by Clear(), Compile(), and ~COMPILER().

◆ GcItem() [1/2]

void LIBEVAL::COMPILER::GcItem ( TREE_NODE aItem)
inline

Definition at line 466 of file libeval_compiler.h.

466 { m_gcItems.push_back( aItem ); }
std::vector< TREE_NODE * > m_gcItems

References m_gcItems.

Referenced by Compile(), and LIBEVAL::newNode().

◆ GcItem() [2/2]

void LIBEVAL::COMPILER::GcItem ( wxString *  aItem)
inline

Definition at line 467 of file libeval_compiler.h.

467 { m_gcStrings.push_back( aItem ); }
std::vector< wxString * > m_gcStrings

References m_gcStrings.

◆ generateUCode()

bool LIBEVAL::COMPILER::generateUCode ( UCODE aCode,
CONTEXT aPreflightContext 
)
protected

Definition at line 781 of file libeval_compiler.cpp.

782 {
783  std::vector<TREE_NODE*> stack;
784  wxString msg;
785 
786  if( !m_tree )
787  {
788  std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
789  // Empty expression returns true
790  aCode->AddOp( new UOP( TR_UOP_PUSH_VALUE, std::move(val) ) );
791  return true;
792  }
793 
794  prepareTree( m_tree );
795 
796  stack.push_back( m_tree );
797 
798  wxString dump;
799 
800  dumpNode( dump, m_tree, 0 );
801  libeval_dbg( 3, "Tree dump:\n%s\n\n", (const char*) dump.c_str() );
802 
803  while( !stack.empty() )
804  {
805  TREE_NODE* node = stack.back();
806 
807  libeval_dbg( 4, "process node %p [op %d] [stack %lu]\n",
808  node, node->op, (unsigned long)stack.size() );
809 
810  // process terminal nodes first
811  switch( node->op )
812  {
813  case TR_OP_FUNC_CALL:
814  // Function call's uop was generated inside TR_STRUCT_REF
815  if( !node->uop )
816  {
817  reportError( CST_CODEGEN, _( "Unknown parent of function parameters" ),
818  node->srcPos );
819  }
820 
821  node->isTerminal = true;
822  break;
823 
824  case TR_STRUCT_REF:
825  {
826  // leaf[0]: object
827  // leaf[1]: field (TR_IDENTIFIER) or TR_OP_FUNC_CALL
828 
829  if( node->leaf[0]->op != TR_IDENTIFIER )
830  {
831  reportError( CST_CODEGEN, _( "Unknown parent of property" ),
832  node->leaf[0]->srcPos - (int) node->leaf[0]->value.str->length() );
833  }
834 
835  switch( node->leaf[1]->op )
836  {
837  case TR_IDENTIFIER:
838  {
839  // leaf[0]: object
840  // leaf[1]: field
841 
842  wxString itemName = *node->leaf[0]->value.str;
843  wxString propName = *node->leaf[1]->value.str;
844  std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, propName );
845 
846  if( !vref )
847  {
848  msg.Printf( _( "Unrecognized item '%s'" ), itemName );
849  reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos - (int) itemName.length() );
850  }
851  else if( vref->GetType() == VT_PARSE_ERROR )
852  {
853  msg.Printf( _( "Unrecognized property '%s'" ), propName );
854  reportError( CST_CODEGEN, msg, node->leaf[1]->srcPos - (int) propName.length() );
855  }
856 
857  node->leaf[0]->isVisited = true;
858  node->leaf[1]->isVisited = true;
859 
860  node->SetUop( TR_UOP_PUSH_VAR, std::move( vref ) );
861  node->isTerminal = true;
862  break;
863  }
864  case TR_OP_FUNC_CALL:
865  {
866  // leaf[0]: object
867  // leaf[1]: TR_OP_FUNC_CALL
868  // leaf[0]: function name
869  // leaf[1]: parameter
870 
871  wxString itemName = *node->leaf[0]->value.str;
872  std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, "" );
873 
874  if( !vref )
875  {
876  msg.Printf( _( "Unrecognized item '%s'" ), itemName );
877  reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos - (int) itemName.length() );
878  }
879 
880  wxString functionName = *node->leaf[1]->leaf[0]->value.str;
881  auto func = aCode->CreateFuncCall( functionName );
882  std::vector<TREE_NODE*> params = squashParamList( node->leaf[1]->leaf[1] );
883 
884  libeval_dbg( 10, "emit func call: %s\n", functionName );
885 
886  if( !func )
887  {
888  msg.Printf( _( "Unrecognized function '%s'" ), functionName );
889  reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos + 1 );
890  }
891 
892  if( func )
893  {
894  // Preflight the function call
895 
896  for( TREE_NODE* pnode : params )
897  {
898  VALUE* param = aPreflightContext->AllocValue();
899  param->Set( *pnode->value.str );
900  aPreflightContext->Push( param );
901  }
902 
903  aPreflightContext->SetErrorCallback(
904  [&]( const wxString& aMessage, int aOffset )
905  {
906  size_t loc = node->leaf[1]->leaf[1]->srcPos;
907  reportError( CST_CODEGEN, aMessage, (int) loc - 1 );
908  } );
909 
910  try
911  {
912  func( aPreflightContext, vref.get() );
913  aPreflightContext->Pop(); // return value
914  }
915  catch( ... )
916  {
917  }
918  }
919 
920  node->leaf[0]->isVisited = true;
921  node->leaf[1]->isVisited = true;
922  node->leaf[1]->leaf[0]->isVisited = true;
923  node->leaf[1]->leaf[1]->isVisited = true;
924 
925  // Our non-terminal-node stacking algorithm can't handle doubly-nested
926  // structures so we need to pop a level by replacing the TR_STRUCT_REF with
927  // a TR_OP_FUNC_CALL and its function parameter
928  stack.pop_back();
929  stack.push_back( node->leaf[1] );
930 
931  for( TREE_NODE* pnode : params )
932  stack.push_back( pnode );
933 
934  node->leaf[1]->SetUop( TR_OP_METHOD_CALL, func, std::move( vref ) );
935  node->isTerminal = false;
936  break;
937  }
938 
939  default:
940  // leaf[0]: object
941  // leaf[1]: malformed syntax
942 
943  wxString itemName = *node->leaf[0]->value.str;
944  wxString propName = *node->leaf[1]->value.str;
945  std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, propName );
946 
947  if( !vref )
948  {
949  msg.Printf( _( "Unrecognized item '%s'" ), itemName );
950  reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos - (int) itemName.length() );
951  }
952 
953  msg.Printf( _( "Unrecognized property '%s'" ), propName );
954  reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos + 1 );
955 
956  node->leaf[0]->isVisited = true;
957  node->leaf[1]->isVisited = true;
958 
959  node->SetUop( TR_UOP_PUSH_VALUE, 0.0 );
960  node->isTerminal = true;
961  break;
962  }
963  break;
964  }
965 
966  case TR_NUMBER:
967  {
968  TREE_NODE* son = node->leaf[0];
969  double value;
970 
971  if( !node->value.str )
972  {
973  value = 0.0;
974  }
975  else if( son && son->op == TR_UNIT )
976  {
977  int units = son->value.idx;
978  value = m_unitResolver->Convert( *node->value.str, units );
979  son->isVisited = true;
980  }
981  else
982  {
983  msg.Printf( _( "Missing units for '%s'| (%s)" ),
984  *node->value.str,
985  m_unitResolver->GetSupportedUnitsMessage() );
986  reportError( CST_CODEGEN, msg, node->srcPos );
987 
988  value = DoubleValueFromString( EDA_UNITS::UNSCALED, *node->value.str );
989  }
990 
991  node->SetUop( TR_UOP_PUSH_VALUE, value );
992  node->isTerminal = true;
993  break;
994  }
995 
996  case TR_STRING:
997  {
998  wxString str = *node->value.str;
999  bool isWildcard = str.Contains("?") || str.Contains("*");
1000  node->SetUop( TR_UOP_PUSH_VALUE, str, isWildcard );
1001  node->isTerminal = true;
1002  break;
1003  }
1004 
1005  case TR_IDENTIFIER:
1006  {
1007  std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( *node->value.str, "" );
1008 
1009  if( !vref )
1010  {
1011  msg.Printf( _( "Unrecognized item '%s'" ), *node->value.str );
1012  reportError( CST_CODEGEN, msg, node->srcPos - (int) node->value.str->length() );
1013  }
1014 
1015  node->SetUop( TR_UOP_PUSH_VALUE, std::move( vref ) );
1016  node->isTerminal = true;
1017  break;
1018  }
1019 
1020  default:
1021  node->SetUop( node->op );
1022  node->isTerminal = ( !node->leaf[0] || node->leaf[0]->isVisited )
1023  && ( !node->leaf[1] || node->leaf[1]->isVisited );
1024  break;
1025  }
1026 
1027  if( !node->isTerminal )
1028  {
1029  if( node->leaf[0] && !node->leaf[0]->isVisited )
1030  {
1031  stack.push_back( node->leaf[0] );
1032  node->leaf[0]->isVisited = true;
1033  continue;
1034  }
1035  else if( node->leaf[1] && !node->leaf[1]->isVisited )
1036  {
1037  stack.push_back( node->leaf[1] );
1038  node->leaf[1]->isVisited = true;
1039  }
1040 
1041  continue;
1042  }
1043 
1044  node->isVisited = true;
1045 
1046  if( node->uop )
1047  {
1048  aCode->AddOp( node->uop );
1049  node->uop = nullptr;
1050  }
1051 
1052  stack.pop_back();
1053  }
1054 
1055  libeval_dbg(2,"dump: \n%s\n", aCode->Dump().c_str() );
1056 
1057  return true;
1058 }
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
#define TR_OP_FUNC_CALL
#define TR_OP_METHOD_CALL
#define libeval_dbg(level, fmt,...)
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
static void prepareTree(LIBEVAL::TREE_NODE *node)
Field Value of part, i.e. "3.3K".
void reportError(COMPILATION_STAGE stage, const wxString &aErrorMsg, int aPos=-1)
#define _(s)
Definition: 3d_actions.cpp:33
static std::vector< TREE_NODE * > squashParamList(TREE_NODE *root)
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:333
#define TR_UOP_PUSH_VAR
#define TR_UOP_PUSH_VALUE
void dumpNode(wxString &buf, TREE_NODE *tok, int depth=0)

References _, LIBEVAL::UCODE::AddOp(), LIBEVAL::CONTEXT::AllocValue(), LIBEVAL::UCODE::CreateFuncCall(), LIBEVAL::UCODE::CreateVarRef(), LIBEVAL::CST_CODEGEN, DoubleValueFromString(), dump(), LIBEVAL::UCODE::Dump(), LIBEVAL::dumpNode(), LIBEVAL::T_TOKEN_VALUE::idx, LIBEVAL::TREE_NODE::isTerminal, LIBEVAL::TREE_NODE::isVisited, LIBEVAL::TREE_NODE::leaf, libeval_dbg, m_tree, m_unitResolver, LIBEVAL::TREE_NODE::op, LIBEVAL::CONTEXT::Pop(), LIBEVAL::prepareTree(), LIBEVAL::CONTEXT::Push(), reportError(), LIBEVAL::VALUE::Set(), LIBEVAL::CONTEXT::SetErrorCallback(), LIBEVAL::TREE_NODE::SetUop(), LIBEVAL::squashParamList(), LIBEVAL::TREE_NODE::srcPos, LIBEVAL::T_TOKEN_VALUE::str, LIBEVAL::TR_IDENTIFIER, LIBEVAL::TR_NUMBER, TR_OP_FUNC_CALL, TR_OP_METHOD_CALL, LIBEVAL::TR_STRING, LIBEVAL::TR_STRUCT_REF, LIBEVAL::TR_UNIT, TR_UOP_PUSH_VALUE, TR_UOP_PUSH_VAR, UNSCALED, LIBEVAL::TREE_NODE::uop, LIBEVAL::TREE_NODE::value, and LIBEVAL::VT_PARSE_ERROR.

Referenced by Compile().

◆ GetError()

const ERROR_STATUS& LIBEVAL::COMPILER::GetError ( ) const
inline

Definition at line 464 of file libeval_compiler.h.

464 { return m_errorStatus; }
ERROR_STATUS m_errorStatus

References m_errorStatus.

Referenced by testEvalExpr().

◆ GetSourcePos()

int LIBEVAL::COMPILER::GetSourcePos ( ) const
inline

Definition at line 451 of file libeval_compiler.h.

451 { return m_sourcePos; }

References m_sourcePos.

Referenced by LIBEVAL::newNode().

◆ getToken()

T_TOKEN LIBEVAL::COMPILER::getToken ( )
protected

Definition at line 343 of file libeval_compiler.cpp.

344 {
345  T_TOKEN rv;
346  rv.value.str = nullptr;
347 
348  bool done = false;
349 
350  do
351  {
352  switch( m_lexerState )
353  {
354  case LS_DEFAULT:
355  done = lexDefault( rv );
356  break;
357  case LS_STRING:
358  done = lexString( rv );
359  break;
360  }
361  } while( !done );
362 
363  return rv;
364 }
bool lexString(T_TOKEN &aToken)
LEXER_STATE m_lexerState
bool lexDefault(T_TOKEN &aToken)

References lexDefault(), lexString(), LS_DEFAULT, LS_STRING, m_lexerState, LIBEVAL::T_TOKEN_VALUE::str, and LIBEVAL::T_TOKEN::value.

Referenced by Compile().

◆ IsErrorPending()

bool LIBEVAL::COMPILER::IsErrorPending ( ) const
inline

Definition at line 463 of file libeval_compiler.h.

References m_errorStatus, and LIBEVAL::ERROR_STATUS::pendingError.

◆ lexDefault()

bool LIBEVAL::COMPILER::lexDefault ( T_TOKEN aToken)
protected

Definition at line 400 of file libeval_compiler.cpp.

401 {
402  T_TOKEN retval;
403  wxString current;
404  int convertFrom;
405  wxString msg;
406 
407  retval.value.str = nullptr;
408  retval.token = G_ENDS;
409 
410  if( m_tokenizer.Done() )
411  {
412  aToken = retval;
413  return true;
414  }
415 
416  auto isDecimalSeparator =
417  [&]( wxUniChar ch ) -> bool
418  {
419  return ( ch == m_localeDecimalSeparator || ch == '.' || ch == ',' );
420  };
421 
422  // Lambda: get value as string, store into clToken.token and update current index.
423  auto extractNumber =
424  [&]()
425  {
426  bool haveSeparator = false;
427  wxUniChar ch = m_tokenizer.GetChar();
428 
429  do
430  {
431  if( isDecimalSeparator( ch ) && haveSeparator )
432  break;
433 
434  current.append( 1, ch );
435 
436  if( isDecimalSeparator( ch ) )
437  haveSeparator = true;
438 
440  ch = m_tokenizer.GetChar();
441  } while( isdigit( ch ) || isDecimalSeparator( ch ) );
442 
443  // Ensure that the systems decimal separator is used
444  for( int i = current.length(); i; i-- )
445  {
446  if( isDecimalSeparator( current[i - 1] ) )
447  current[i - 1] = m_localeDecimalSeparator;
448  }
449  };
450 
451 
452  int ch;
453 
454  // Start processing of first/next token: Remove whitespace
455  for( ;; )
456  {
457  ch = m_tokenizer.GetChar();
458 
459  if( ch == ' ' )
461  else
462  break;
463  }
464 
465  libeval_dbg(10, "LEX ch '%c' pos %lu\n", ch, (unsigned long)m_tokenizer.GetPos() );
466 
467  if( ch == 0 )
468  {
469  /* End of input */
470  }
471  else if( isdigit( ch ) )
472  {
473  // VALUE
474  extractNumber();
475  retval.token = G_VALUE;
476  retval.value.str = new wxString( current );
477  }
478  else if( ( convertFrom = resolveUnits() ) >= 0 )
479  {
480  // UNIT
481  // Units are appended to a VALUE.
482  // Determine factor to default unit if unit for value is given.
483  // Example: Default is mm, unit is inch: factor is 25.4
484  // The factor is assigned to the terminal UNIT. The actual
485  // conversion is done within a parser action.
486  retval.token = G_UNIT;
487  retval.value.idx = convertFrom;
488  }
489  else if( ch == '\'' ) // string literal
490  {
493  return false;
494  }
495  else if( isalpha( ch ) || ch == '_' )
496  {
497  current = m_tokenizer.GetChars( []( int c ) -> bool { return isalnum( c ) || c == '_'; } );
498  retval.token = G_IDENTIFIER;
499  retval.value.str = new wxString( current );
500  m_tokenizer.NextChar( current.length() );
501  }
502  else if( m_tokenizer.MatchAhead( "==", []( int c ) -> bool { return c != '='; } ) )
503  {
504  retval.token = G_EQUAL;
505  m_tokenizer.NextChar( 2 );
506  }
507  else if( m_tokenizer.MatchAhead( "!=", []( int c ) -> bool { return c != '='; } ) )
508  {
509  retval.token = G_NOT_EQUAL;
510  m_tokenizer.NextChar( 2 );
511  }
512  else if( m_tokenizer.MatchAhead( "<=", []( int c ) -> bool { return c != '='; } ) )
513  {
514  retval.token = G_LESS_EQUAL_THAN;
515  m_tokenizer.NextChar( 2 );
516  }
517  else if( m_tokenizer.MatchAhead( ">=", []( int c ) -> bool { return c != '='; } ) )
518  {
519  retval.token = G_GREATER_EQUAL_THAN;
520  m_tokenizer.NextChar( 2 );
521  }
522  else if( m_tokenizer.MatchAhead( "&&", []( int c ) -> bool { return c != '&'; } ) )
523  {
524  retval.token = G_BOOL_AND;
525  m_tokenizer.NextChar( 2 );
526  }
527  else if( m_tokenizer.MatchAhead( "||", []( int c ) -> bool { return c != '|'; } ) )
528  {
529  retval.token = G_BOOL_OR;
530  m_tokenizer.NextChar( 2 );
531  }
532  else
533  {
534  // Single char tokens
535  switch( ch )
536  {
537  case '+': retval.token = G_PLUS; break;
538  case '!': retval.token = G_BOOL_NOT; break;
539  case '-': retval.token = G_MINUS; break;
540  case '*': retval.token = G_MULT; break;
541  case '/': retval.token = G_DIVIDE; break;
542  case '<': retval.token = G_LESS_THAN; break;
543  case '>': retval.token = G_GREATER_THAN; break;
544  case '(': retval.token = G_PARENL; break;
545  case ')': retval.token = G_PARENR; break;
546  case ';': retval.token = G_SEMCOL; break;
547  case '.': retval.token = G_STRUCT_REF; break;
548  case ',': retval.token = G_COMMA; break;
549 
550 
551  default:
552  reportError( CST_PARSE, wxString::Format( _( "Unrecognized character '%c'" ),
553  (char) ch ) );
554  break;
555  }
556 
558  }
559 
560  aToken = retval;
561  return true;
562 }
wxString GetChars(const std::function< bool(wxUniChar)> &cond) const
void NextChar(int aAdvance=1)
#define libeval_dbg(level, fmt,...)
bool MatchAhead(const wxString &match, const std::function< bool(wxUniChar)> &stopCond) const
LEXER_STATE m_lexerState
void reportError(COMPILATION_STAGE stage, const wxString &aErrorMsg, int aPos=-1)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
size_t GetPos() const
#define _(s)
Definition: 3d_actions.cpp:33

References _, LIBEVAL::CST_PARSE, LIBEVAL::TOKENIZER::Done(), Format(), LIBEVAL::TOKENIZER::GetChar(), LIBEVAL::TOKENIZER::GetChars(), LIBEVAL::TOKENIZER::GetPos(), LIBEVAL::T_TOKEN_VALUE::idx, libeval_dbg, LS_STRING, m_lexerState, m_localeDecimalSeparator, m_tokenizer, LIBEVAL::TOKENIZER::MatchAhead(), LIBEVAL::TOKENIZER::NextChar(), reportError(), resolveUnits(), LIBEVAL::T_TOKEN_VALUE::str, LIBEVAL::T_TOKEN::token, and LIBEVAL::T_TOKEN::value.

Referenced by getToken().

◆ lexString()

bool LIBEVAL::COMPILER::lexString ( T_TOKEN aToken)
protected

Definition at line 367 of file libeval_compiler.cpp.

368 {
369  wxString str = m_tokenizer.GetChars( []( int c ) -> bool { return c != '\''; } );
370 
371  aToken.token = G_STRING;
372  aToken.value.str = new wxString( str );
373 
374  m_tokenizer.NextChar( str.length() + 1 );
376  return true;
377 }
wxString GetChars(const std::function< bool(wxUniChar)> &cond) const
void NextChar(int aAdvance=1)
LEXER_STATE m_lexerState

References LIBEVAL::TOKENIZER::GetChars(), LS_DEFAULT, m_lexerState, m_tokenizer, LIBEVAL::TOKENIZER::NextChar(), LIBEVAL::T_TOKEN_VALUE::str, LIBEVAL::T_TOKEN::token, and LIBEVAL::T_TOKEN::value.

Referenced by getToken().

◆ newString()

void LIBEVAL::COMPILER::newString ( const wxString &  aString)
protected

Definition at line 334 of file libeval_compiler.cpp.

335 {
336  Clear();
337 
339  m_tokenizer.Restart( aString );
340  m_parseFinished = false;
341 }
void Restart(const wxString &aStr)
LEXER_STATE m_lexerState

References Clear(), LS_DEFAULT, m_lexerState, m_parseFinished, m_tokenizer, and LIBEVAL::TOKENIZER::Restart().

Referenced by Compile().

◆ parseError()

void LIBEVAL::COMPILER::parseError ( const char *  s)

Definition at line 271 of file libeval_compiler.cpp.

272 {
273  reportError( CST_PARSE, s );
274 }
void reportError(COMPILATION_STAGE stage, const wxString &aErrorMsg, int aPos=-1)

References LIBEVAL::CST_PARSE, and reportError().

◆ parseOk()

void LIBEVAL::COMPILER::parseOk ( )

Definition at line 277 of file libeval_compiler.cpp.

278 {
279  m_parseFinished = true;
280 }

References m_parseFinished.

◆ reportError()

void LIBEVAL::COMPILER::reportError ( COMPILATION_STAGE  stage,
const wxString &  aErrorMsg,
int  aPos = -1 
)
protected

Definition at line 656 of file libeval_compiler.cpp.

657 {
658  if( aPos == -1 )
659  aPos = m_sourcePos;
660 
662  m_errorStatus.stage = stage;
663  m_errorStatus.message = aErrorMsg;
664  m_errorStatus.srcPos = aPos;
665 
666  if( m_errorCallback )
667  m_errorCallback( aErrorMsg, aPos );
668 }
COMPILATION_STAGE stage
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback
ERROR_STATUS m_errorStatus

References m_errorCallback, m_errorStatus, m_sourcePos, LIBEVAL::ERROR_STATUS::message, LIBEVAL::ERROR_STATUS::pendingError, LIBEVAL::ERROR_STATUS::srcPos, and LIBEVAL::ERROR_STATUS::stage.

Referenced by generateUCode(), lexDefault(), and parseError().

◆ resolveUnits()

int LIBEVAL::COMPILER::resolveUnits ( )
protected

Definition at line 380 of file libeval_compiler.cpp.

381 {
382  int unitId = 0;
383 
384  for( const wxString& unitName : m_unitResolver->GetSupportedUnits() )
385  {
386  if( m_tokenizer.MatchAhead( unitName, []( int c ) -> bool { return !isalnum( c ); } ) )
387  {
388  libeval_dbg(10, "Match unit '%s'\n", unitName.c_str() );
389  m_tokenizer.NextChar( unitName.length() );
390  return unitId;
391  }
392 
393  unitId++;
394  }
395 
396  return -1;
397 }
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
void NextChar(int aAdvance=1)
#define libeval_dbg(level, fmt,...)
bool MatchAhead(const wxString &match, const std::function< bool(wxUniChar)> &stopCond) const

References libeval_dbg, m_tokenizer, m_unitResolver, LIBEVAL::TOKENIZER::MatchAhead(), and LIBEVAL::TOKENIZER::NextChar().

Referenced by lexDefault().

◆ SetErrorCallback()

void LIBEVAL::COMPILER::SetErrorCallback ( std::function< void(const wxString &aMessage, int aOffset)>  aCallback)
inline

Definition at line 458 of file libeval_compiler.h.

459  {
460  m_errorCallback = std::move( aCallback );
461  }
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback

References m_errorCallback.

Referenced by DRC_RULE_CONDITION::Compile(), and PCB_EXPR_EVALUATOR::SetErrorCallback().

◆ setRoot()

void LIBEVAL::COMPILER::setRoot ( LIBEVAL::TREE_NODE root)

Definition at line 671 of file libeval_compiler.cpp.

672 {
673  m_tree = root;
674 }

References m_tree.

Member Data Documentation

◆ m_errorCallback

std::function<void( const wxString& aMessage, int aOffset )> LIBEVAL::COMPILER::m_errorCallback
protected

Definition at line 504 of file libeval_compiler.h.

Referenced by reportError(), and SetErrorCallback().

◆ m_errorStatus

ERROR_STATUS LIBEVAL::COMPILER::m_errorStatus
protected

Definition at line 502 of file libeval_compiler.h.

Referenced by Compile(), COMPILER(), GetError(), IsErrorPending(), and reportError().

◆ m_gcItems

std::vector<TREE_NODE*> LIBEVAL::COMPILER::m_gcItems
protected

Definition at line 508 of file libeval_compiler.h.

Referenced by Clear(), and GcItem().

◆ m_gcStrings

std::vector<wxString*> LIBEVAL::COMPILER::m_gcStrings
protected

Definition at line 509 of file libeval_compiler.h.

Referenced by Clear(), and GcItem().

◆ m_lexerState

LEXER_STATE LIBEVAL::COMPILER::m_lexerState
protected

Definition at line 476 of file libeval_compiler.h.

Referenced by getToken(), lexDefault(), lexString(), and newString().

◆ m_localeDecimalSeparator

char LIBEVAL::COMPILER::m_localeDecimalSeparator
protected

Definition at line 496 of file libeval_compiler.h.

Referenced by COMPILER(), and lexDefault().

◆ m_parseFinished

bool LIBEVAL::COMPILER::m_parseFinished
protected

Definition at line 501 of file libeval_compiler.h.

Referenced by Compile(), COMPILER(), newString(), and parseOk().

◆ m_parser

void* LIBEVAL::COMPILER::m_parser
protected

Definition at line 494 of file libeval_compiler.h.

Referenced by Compile(), COMPILER(), and ~COMPILER().

◆ m_sourcePos

int LIBEVAL::COMPILER::m_sourcePos
protected

Definition at line 500 of file libeval_compiler.h.

Referenced by Compile(), COMPILER(), GetSourcePos(), and reportError().

◆ m_tokenizer

TOKENIZER LIBEVAL::COMPILER::m_tokenizer
protected

Definition at line 495 of file libeval_compiler.h.

Referenced by Clear(), Compile(), lexDefault(), lexString(), newString(), and resolveUnits().

◆ m_tree

TREE_NODE* LIBEVAL::COMPILER::m_tree
protected

Definition at line 506 of file libeval_compiler.h.

Referenced by Clear(), Compile(), COMPILER(), generateUCode(), setRoot(), and ~COMPILER().

◆ m_unitResolver

std::unique_ptr<UNIT_RESOLVER> LIBEVAL::COMPILER::m_unitResolver
protected

The documentation for this class was generated from the following files: