2020-06-04 11:04:03 +00:00
|
|
|
/*
|
|
|
|
This file is part of libeval, a simple math expression evaluator
|
|
|
|
|
|
|
|
Copyright (C) 2007 Michael Geselbracht, mgeselbracht3@gmail.com
|
2020-07-03 16:36:33 +00:00
|
|
|
Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LIBEVAL_COMPILER_H
|
|
|
|
#define __LIBEVAL_COMPILER_H
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
2020-07-24 14:16:15 +00:00
|
|
|
#include <map>
|
2020-06-04 11:04:03 +00:00
|
|
|
#include <string>
|
2020-07-25 11:58:17 +00:00
|
|
|
#include <stack>
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
#include <base_units.h>
|
|
|
|
|
2020-09-22 12:39:05 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
// This gets leaked by python headers on MSVC only and will cause chaos
|
|
|
|
#undef COMPILER
|
|
|
|
#endif
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
#define TR_OP_BINARY_MASK 0x200
|
|
|
|
#define TR_OP_UNARY_MASK 0x100
|
|
|
|
|
|
|
|
#define TR_OP_MUL 0x201
|
|
|
|
#define TR_OP_DIV 0x202
|
|
|
|
#define TR_OP_ADD 0x203
|
|
|
|
#define TR_OP_SUB 0x204
|
|
|
|
#define TR_OP_LESS 0x25
|
|
|
|
#define TR_OP_GREATER 0x206
|
|
|
|
#define TR_OP_LESS_EQUAL 0x207
|
|
|
|
#define TR_OP_GREATER_EQUAL 0x208
|
|
|
|
#define TR_OP_EQUAL 0x209
|
|
|
|
#define TR_OP_NOT_EQUAL 0x20a
|
|
|
|
#define TR_OP_BOOL_AND 0x20b
|
|
|
|
#define TR_OP_BOOL_OR 0x20c
|
|
|
|
#define TR_OP_BOOL_NOT 0x100
|
|
|
|
#define TR_OP_FUNC_CALL 24
|
2020-06-17 22:36:02 +00:00
|
|
|
#define TR_OP_METHOD_CALL 25
|
2020-06-04 11:04:03 +00:00
|
|
|
#define TR_UOP_PUSH_VAR 1
|
|
|
|
#define TR_UOP_PUSH_VALUE 2
|
|
|
|
|
|
|
|
// This namespace is used for the lemon parser
|
|
|
|
namespace LIBEVAL
|
|
|
|
{
|
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
class COMPILER;
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
enum COMPILATION_STAGE
|
|
|
|
{
|
|
|
|
CST_PARSE = 0,
|
|
|
|
CST_CODEGEN,
|
|
|
|
CST_RUNTIME
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ERROR_STATUS
|
|
|
|
{
|
|
|
|
bool pendingError = false;
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
COMPILATION_STAGE stage;
|
|
|
|
wxString message;
|
|
|
|
int srcPos;
|
2020-08-08 22:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
enum VAR_TYPE_T
|
|
|
|
{
|
|
|
|
VT_STRING = 1,
|
|
|
|
VT_NUMERIC,
|
2020-07-30 11:24:29 +00:00
|
|
|
VT_UNDEFINED,
|
|
|
|
VT_PARSE_ERROR
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TOKEN_TYPE_T
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
TR_UNDEFINED = 0,
|
2020-07-25 11:58:17 +00:00
|
|
|
TR_NUMBER = 1,
|
2020-06-04 11:04:03 +00:00
|
|
|
TR_IDENTIFIER = 2,
|
|
|
|
TR_ASSIGN = 3,
|
|
|
|
TR_STRUCT_REF = 4,
|
|
|
|
TR_STRING = 5,
|
2020-08-26 22:00:42 +00:00
|
|
|
TR_UNIT = 6,
|
2020-09-22 12:56:54 +00:00
|
|
|
TR_ARG_LIST = 7,
|
|
|
|
TR_NULL = 8
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
class UOP;
|
2020-08-10 12:47:01 +00:00
|
|
|
class UCODE;
|
|
|
|
class CONTEXT;
|
|
|
|
class VAR_REF;
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
typedef std::function<void( CONTEXT*, void* )> FUNC_CALL_REF;
|
|
|
|
|
|
|
|
struct T_TOKEN_VALUE
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
wxString* str;
|
|
|
|
double num;
|
|
|
|
int idx;
|
2020-08-10 12:47:01 +00:00
|
|
|
};
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
// Lemon can't handle c'tors and d'tors, so we provide a poor-man's version.
|
2020-08-10 12:47:01 +00:00
|
|
|
constexpr T_TOKEN_VALUE defaultTokenValue = { nullptr, 0.0, 0 };
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
struct T_TOKEN
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
int token;
|
2020-08-10 12:47:01 +00:00
|
|
|
T_TOKEN_VALUE value;
|
|
|
|
};
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
// Lemon can't handle c'tors and d'tors, so we provide a poor-man's version.
|
|
|
|
constexpr T_TOKEN defaultToken = { TR_UNDEFINED, defaultTokenValue };
|
|
|
|
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
class TREE_NODE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
T_TOKEN_VALUE value;
|
2020-07-18 10:40:56 +00:00
|
|
|
|
|
|
|
int op;
|
|
|
|
TREE_NODE* leaf[2];
|
|
|
|
UOP* uop;
|
|
|
|
bool valid;
|
|
|
|
bool isTerminal;
|
2020-08-13 14:15:43 +00:00
|
|
|
bool isVisited;
|
2020-07-19 21:22:49 +00:00
|
|
|
int srcPos;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
|
|
|
void SetUop( int aOp, double aValue );
|
2020-09-23 21:44:50 +00:00
|
|
|
void SetUop( int aOp, const wxString& aValue, bool aStringIsWildcard );
|
2020-08-10 12:47:01 +00:00
|
|
|
void SetUop( int aOp, std::unique_ptr<VAR_REF> aRef = nullptr );
|
|
|
|
void SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef = nullptr );
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
TREE_NODE* newNode( LIBEVAL::COMPILER* compiler, int op,
|
|
|
|
const T_TOKEN_VALUE& value = defaultTokenValue );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
class UNIT_RESOLVER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UNIT_RESOLVER()
|
|
|
|
{
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
virtual ~UNIT_RESOLVER()
|
|
|
|
{
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
virtual const std::vector<wxString>& GetSupportedUnits() const
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
static const std::vector<wxString> nullUnits;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
return nullUnits;
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
virtual double Convert( const wxString& aString, int unitType ) const
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
};
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
class VALUE
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-06-04 11:04:03 +00:00
|
|
|
public:
|
2020-09-27 18:43:44 +00:00
|
|
|
VALUE() :
|
|
|
|
m_type( VT_UNDEFINED ),
|
2020-09-23 21:44:50 +00:00
|
|
|
m_valueDbl( 0 ),
|
|
|
|
m_stringIsWildcard( false )
|
2020-07-19 21:22:49 +00:00
|
|
|
{};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-09-23 21:44:50 +00:00
|
|
|
VALUE( const wxString& aStr, bool aIsWildcard = false ) :
|
2020-06-04 11:04:03 +00:00
|
|
|
m_type( VT_STRING ),
|
|
|
|
m_valueDbl( 0 ),
|
2020-09-23 21:44:50 +00:00
|
|
|
m_valueStr( aStr ),
|
|
|
|
m_stringIsWildcard( aIsWildcard )
|
2020-07-19 21:22:49 +00:00
|
|
|
{};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
VALUE( const double aVal ) :
|
|
|
|
m_type( VT_NUMERIC ),
|
2020-09-23 21:44:50 +00:00
|
|
|
m_valueDbl( aVal ),
|
|
|
|
m_stringIsWildcard( false )
|
2020-07-19 21:22:49 +00:00
|
|
|
{};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-09-27 18:43:44 +00:00
|
|
|
virtual ~VALUE()
|
|
|
|
{};
|
|
|
|
|
|
|
|
virtual double AsDouble() const
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
return m_valueDbl;
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-09-27 18:43:44 +00:00
|
|
|
virtual const wxString& AsString() const
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
return m_valueStr;
|
|
|
|
}
|
|
|
|
|
2020-09-27 18:43:44 +00:00
|
|
|
virtual bool EqualTo( const VALUE* b ) const;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
VAR_TYPE_T GetType() const { return m_type; };
|
|
|
|
|
|
|
|
void Set( double aValue )
|
|
|
|
{
|
|
|
|
m_type = VT_NUMERIC;
|
|
|
|
m_valueDbl = aValue;
|
|
|
|
}
|
|
|
|
|
2020-07-25 11:58:17 +00:00
|
|
|
void Set( const wxString& aValue )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
m_type = VT_STRING;
|
|
|
|
m_valueStr = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Set( const VALUE &val )
|
|
|
|
{
|
|
|
|
m_type = val.m_type;
|
|
|
|
m_valueDbl = val.m_valueDbl;
|
|
|
|
|
|
|
|
if( m_type == VT_STRING )
|
|
|
|
m_valueStr = val.m_valueStr;
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
private:
|
2020-07-19 21:22:49 +00:00
|
|
|
VAR_TYPE_T m_type;
|
|
|
|
double m_valueDbl;
|
2020-07-25 11:58:17 +00:00
|
|
|
wxString m_valueStr;
|
2020-09-23 21:44:50 +00:00
|
|
|
bool m_stringIsWildcard;
|
2020-07-19 21:22:49 +00:00
|
|
|
};
|
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
class VAR_REF
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-10 12:47:01 +00:00
|
|
|
VAR_REF() {};
|
|
|
|
virtual ~VAR_REF() {};
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
virtual VAR_TYPE_T GetType() = 0;
|
2020-07-23 20:47:38 +00:00
|
|
|
virtual VALUE GetValue( CONTEXT* aCtx ) = 0;
|
2020-07-19 21:22:49 +00:00
|
|
|
};
|
2020-06-13 23:19:22 +00:00
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-07-22 13:08:57 +00:00
|
|
|
class CONTEXT
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-12 15:26:10 +00:00
|
|
|
virtual ~CONTEXT()
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-07-25 11:58:17 +00:00
|
|
|
for( VALUE* value : m_ownedValues )
|
|
|
|
delete value;
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE* AllocValue()
|
|
|
|
{
|
2020-07-25 11:58:17 +00:00
|
|
|
VALUE* value = new VALUE();
|
|
|
|
m_ownedValues.push_back( value );
|
|
|
|
return value;
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 11:58:17 +00:00
|
|
|
void Push( VALUE* v )
|
2020-07-22 13:08:57 +00:00
|
|
|
{
|
2020-07-25 11:58:17 +00:00
|
|
|
m_stack.push( v );
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE* Pop()
|
|
|
|
{
|
2020-09-18 23:09:53 +00:00
|
|
|
if( m_stack.size() == 0 )
|
|
|
|
{
|
|
|
|
ReportError( _( "Malformed expression" ) );
|
|
|
|
return AllocValue();
|
|
|
|
}
|
|
|
|
|
2020-07-25 11:58:17 +00:00
|
|
|
VALUE* value = m_stack.top();
|
|
|
|
m_stack.pop();
|
|
|
|
return value;
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-22 13:08:57 +00:00
|
|
|
int SP() const
|
|
|
|
{
|
2020-07-25 11:58:17 +00:00
|
|
|
return m_stack.size();
|
2020-08-08 22:39:05 +00:00
|
|
|
};
|
2020-07-22 13:08:57 +00:00
|
|
|
|
2020-08-26 11:55:43 +00:00
|
|
|
void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
|
|
|
|
{
|
|
|
|
m_errorCallback = std::move( aCallback );
|
|
|
|
}
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
void ReportError( const wxString& aErrorMsg );
|
|
|
|
bool IsErrorPending() const { return m_errorStatus.pendingError; }
|
|
|
|
const ERROR_STATUS& GetError() const { return m_errorStatus; }
|
2020-07-22 13:08:57 +00:00
|
|
|
|
|
|
|
private:
|
2020-07-25 11:58:17 +00:00
|
|
|
std::vector<VALUE*> m_ownedValues;
|
|
|
|
std::stack<VALUE*> m_stack;
|
2020-08-08 22:39:05 +00:00
|
|
|
ERROR_STATUS m_errorStatus;
|
2020-08-26 11:55:43 +00:00
|
|
|
|
|
|
|
std::function<void( const wxString& aMessage, int aOffset )> m_errorCallback;
|
2020-07-22 13:08:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class UCODE
|
|
|
|
{
|
|
|
|
public:
|
2020-07-29 21:11:30 +00:00
|
|
|
virtual ~UCODE();
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
void AddOp( UOP* uop )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-06-17 22:36:02 +00:00
|
|
|
m_ucode.push_back(uop);
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
VALUE* Run( CONTEXT* ctx );
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString Dump() const;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
virtual std::unique_ptr<VAR_REF> CreateVarRef( const wxString& var, const wxString& field )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-06-13 23:19:22 +00:00
|
|
|
return nullptr;
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
virtual FUNC_CALL_REF CreateFuncCall( const wxString& name )
|
2020-06-17 22:36:02 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
protected:
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
std::vector<UOP*> m_ucode;
|
|
|
|
};
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
|
|
|
|
class UOP
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-10 12:47:01 +00:00
|
|
|
UOP( int op, std::unique_ptr<VALUE> value ) :
|
|
|
|
m_op( op ),
|
|
|
|
m_ref(nullptr),
|
|
|
|
m_value( std::move( value ) )
|
|
|
|
{};
|
|
|
|
|
|
|
|
UOP( int op, std::unique_ptr<VAR_REF> vref ) :
|
2020-07-19 21:22:49 +00:00
|
|
|
m_op( op ),
|
2020-08-10 12:47:01 +00:00
|
|
|
m_ref( std::move( vref ) ),
|
|
|
|
m_value(nullptr)
|
2020-07-19 21:22:49 +00:00
|
|
|
{};
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
UOP( int op, FUNC_CALL_REF func, std::unique_ptr<VAR_REF> vref = nullptr ) :
|
2020-07-19 21:22:49 +00:00
|
|
|
m_op( op ),
|
2020-08-10 12:47:01 +00:00
|
|
|
m_func( std::move( func ) ),
|
|
|
|
m_ref( std::move( vref ) ),
|
|
|
|
m_value(nullptr)
|
2020-07-19 21:22:49 +00:00
|
|
|
{};
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
~UOP()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
void Exec( CONTEXT* ctx );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString Format() const;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
private:
|
2020-08-14 11:34:44 +00:00
|
|
|
int m_op;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
FUNC_CALL_REF m_func;
|
2020-08-10 12:47:01 +00:00
|
|
|
std::unique_ptr<VAR_REF> m_ref;
|
|
|
|
std::unique_ptr<VALUE> m_value;
|
2020-07-19 21:22:49 +00:00
|
|
|
};
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
class TOKENIZER
|
|
|
|
{
|
|
|
|
public:
|
2020-08-08 22:39:05 +00:00
|
|
|
void Restart( const wxString& aStr )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
m_str = aStr;
|
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_str = "";
|
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetChar() const
|
|
|
|
{
|
|
|
|
if( m_pos >= m_str.length() )
|
|
|
|
return 0;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return m_str[m_pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Done() const
|
|
|
|
{
|
|
|
|
return m_pos >= m_str.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NextChar( int aAdvance = 1 )
|
|
|
|
{
|
|
|
|
m_pos += aAdvance;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetPos() const
|
|
|
|
{
|
|
|
|
return m_pos;
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
wxString GetChars( const std::function<bool( wxUniChar )>& cond ) const;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
bool MatchAhead( const wxString& match,
|
|
|
|
const std::function<bool( wxUniChar )>& stopCond ) const;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
private:
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString m_str;
|
2020-08-14 11:34:44 +00:00
|
|
|
size_t m_pos = 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class COMPILER
|
|
|
|
{
|
|
|
|
public:
|
2020-08-08 22:39:05 +00:00
|
|
|
COMPILER();
|
2020-06-04 11:04:03 +00:00
|
|
|
virtual ~COMPILER();
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
/*
|
|
|
|
* clear() should be invoked by the client if a new input string is to be processed. It
|
2020-06-04 11:04:03 +00:00
|
|
|
* will reset the parser. User defined variables are retained.
|
|
|
|
*/
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
/* Used by the lemon parser */
|
|
|
|
void parseError( const char* s );
|
|
|
|
void parseOk();
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
int GetSourcePos() const { return m_sourcePos; }
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void setRoot( LIBEVAL::TREE_NODE *root );
|
2020-07-24 14:16:15 +00:00
|
|
|
void freeTree( LIBEVAL::TREE_NODE *tree );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
bool Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflightContext );
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-08-13 17:47:41 +00:00
|
|
|
void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
m_errorCallback = std::move( aCallback );
|
2020-08-13 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
bool IsErrorPending() const { return m_errorStatus.pendingError; }
|
|
|
|
const ERROR_STATUS& GetError() const { return m_errorStatus; }
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void GcItem( TREE_NODE* aItem ) { m_gcItems.push_back( aItem ); }
|
|
|
|
void GcItem( wxString* aItem ) { m_gcStrings.push_back( aItem ); }
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
protected:
|
|
|
|
enum LEXER_STATE
|
|
|
|
{
|
|
|
|
LS_DEFAULT = 0,
|
|
|
|
LS_STRING = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
LEXER_STATE m_lexerState;
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
bool generateUCode( UCODE* aCode, CONTEXT* aPreflightContext );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
void reportError( COMPILATION_STAGE stage, const wxString& aErrorMsg, int aPos = -1 );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
/* Begin processing of a new input string */
|
2020-08-08 22:39:05 +00:00
|
|
|
void newString( const wxString& aString );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
/* Tokenizer: Next token/value taken from input string. */
|
|
|
|
T_TOKEN getToken();
|
2020-08-13 17:47:41 +00:00
|
|
|
bool lexDefault( T_TOKEN& aToken );
|
|
|
|
bool lexString( T_TOKEN& aToken );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
int resolveUnits();
|
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
protected:
|
2020-06-04 11:04:03 +00:00
|
|
|
/* Token state for input string. */
|
2020-07-19 21:22:49 +00:00
|
|
|
void* m_parser; // the current lemon parser state machine
|
|
|
|
TOKENIZER m_tokenizer;
|
|
|
|
char m_localeDecimalSeparator;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
std::unique_ptr<UNIT_RESOLVER> m_unitResolver;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
int m_sourcePos;
|
|
|
|
bool m_parseFinished;
|
2020-08-14 11:34:44 +00:00
|
|
|
ERROR_STATUS m_errorStatus;
|
|
|
|
|
|
|
|
std::function<void( const wxString& aMessage, int aOffset )> m_errorCallback;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
TREE_NODE* m_tree;
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
std::vector<TREE_NODE*> m_gcItems;
|
2020-08-14 11:34:44 +00:00
|
|
|
std::vector<wxString*> m_gcStrings;
|
2020-06-04 11:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace LIBEVAL
|
|
|
|
|
|
|
|
#endif /* LIBEVAL_COMPILER_H_ */
|