📄 cplusbase.h
字号:
#if !defined(_CPLUSBASE_H)
#define _CPLUSBASE_H
#include "PubHeader.h"
#include "Base.h"
#include "Callback.h"
#include "Varient.h"
#include "Utility.h"
#include "Lex.h"
#include "ObGrid.h"
////////////////////////////////////////////////////////
#define SHOW_WHEN_COMMENT_NOT_FOUND "unknow"
#define NW_TRAN_MODE_L_TO_H 1
#define NW_TRAN_MODE_H_TO_L 0
#define DEFAULT_NW_TRAN_MODE NW_TRAN_MODE_L_TO_H
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// GetTokenEx(nExStyle)
enum
{
EN_PROCIDENT = 1 , // 允许函数调用类型值, 例如 Func(...)
EN_LINEREMARKRET= 2 ,
EN_VALUESET = 4 , // 允许 { ... }
EN_DOLLARITEM = 8 , // 允许 $Num
};
#ifdef _DLL_PROJECT
class CLASS_EXPORT CPlusLex: public CLex
#else
class CPlusLex: public CLex
#endif
{
public:
enum register_type
{
REG_KEYWORD =0, // KEYWORD允许两个单词的复合词
REG_OPERATOR=1, // Operator和RelateOperator支持双字符
};
public:
CPlusLex();
~CPlusLex();
bool OpenFile(char* szfilename,bool bAsTextFile=true);
char* GetName() { return m_BufFile.getFilename(); }
char* GetPath() { return m_BufFile.getFilepath(); }
char* GetAbsoluteFilename() { return m_BufFile.getAbsoluteFilename(); }
void Reset(bool bResetBuffer);
/////////////////////////////////////////////////////////////////////
// Extend Function, implement through GetToken()
int GetToken(LexToken& Token, bool bJustPreview=false); // KeyWord
int GetToken(LexToken& Token, WORD nExStyle, bool bJustPreview);
int GetTokenEx(LexToken& Token, WORD nExStyle=0); // 扩展: 支持Ident 加 . () [] ::
// 不返回REMARKS
bool GetSpecificToken(LexToken& Token,long TokenId); // 支持基本TokenId和KeyWord, 如果TokenId!=Remark, 则跳过Remark
// 如果不成功,则位置指针停留在原地
bool GetSpecificToken(LexToken& Token,char* szTokenName,bool bcase=false);
bool Fetch_string(char* szLeftOperator, char* szRightOperator, // 考虑了配对的() []
bool bReturnBetween, OTSTR& strReturn); // 返回串包含Left,RightOperator
bool ForwardLocatTo(long nLocat);
Location& getLocation();
bool setLocation(Location& locat);
private:
BufferFile m_BufFile;
bool m_bTokenValid;
LexToken m_Token; // 最多可保存一个元素, 用于Preview情况
///////////////////////////////
static LexRules m_Rules;
static StringArray m_KeyWord_List;
static bool Register(register_type type, char* lpstring);
public:
//////////////////////////////////////////////////
static void Initial(); // 程序开始时调用一次
static void Destroy(); // 程序结束时调用一次
static bool isValue(LexToken& Token);
static bool isValueSet(LexToken& Token);
static bool isKeyWord(LexToken& Token);
static bool isIdent(LexToken& Token); // 包含扩展的Ident
static bool isOperator(LexToken& Token);
static bool isProcIdent(LexToken& Token);
static bool VarstringParser(char* varString,
OTSTR& strVar,OTSTR& strRestrict,OTSTR& strNext,
long* pRestrictLocat=NULL,long* pNextLocat=NULL);
};
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
#ifdef _DLL_PROJECT
class CLASS_EXPORT ExprNode: public LexToken
#else
class ExprNode: public LexToken
#endif
{
public:
enum { MAX_CHILD_NUM=3 };
public:
//////////////////////////////////
ExprNode* m_pChildList[MAX_CHILD_NUM]; // 单目 0, 双目 0,1, 三目 0,1,2
public:
ExprNode();
~ExprNode();
bool isLeaf();
int GetHeight();
void operator=(const ExprNode& right);
private:
friend class ExprAnalyser;
int Height(ExprNode* pNode);
};
////////////////////
enum {
EN_VALUSET_ASSIGN = 1,
EN_FIRST_VALUESET_ITEM = 2,
};
#ifdef _DLL_PROJECT
class CLASS_EXPORT ExprAnalyser
#else
class ExprAnalyser
#endif
{
public:
class TResult: public CVarient
{
public:
bool bisIdent; // true CVarient保存的是变量名(szIdent)
// false CVarient保存的是具体的值
public:
TResult() { bisIdent=false; }
~TResult() {}
bool clone(TResult& ResultObject)
{
bisIdent=ResultObject.bisIdent;
return CVarient::clone(ResultObject);
}
bool isIdent()
{
return bisIdent;
}
void setIdent(char* szIdent)
{
bisIdent=true;
set(szIdent);
}
char* getIdent()
{
char* szIdent;
if (get(&szIdent)) return szIdent;
return NULL;
}
};
public:
ExprAnalyser();
~ExprAnalyser();
ExprNode* GetExpression(char* lpstr_expression,WORD nFlag=0);
ExprNode* GetExpression(CPlusLex& Parser, WORD nFlag=0); // 分析取得最大的表达式串, 不得为空表达式
// bEnableValueSetAssign指数组赋值 {...}
///////////////////////////////////////////////////////////////////
void setCallback(void* handler, cCallback::tFunction pFunctionPointer);
void setCallback(cCallback* pCallback);
cCallback* getCallback();
///////////////////////////////////////////////////////////////////
bool calculate(char* lpstr_expression,long& result); // 表达式求值(不允许数组或结构类型的变量,因无法运算)
bool calculate(char* lpstr_expression,OTSTR& result);
bool calculate(char* lpstr_expression,CVarient& result,bool bNullResultAllowed=false); // bNullResultAllowed用于void函数返回
bool calculate(ExprNode* pNode,long& result);
bool calculate(ExprNode* pNode,CVarient& result);
bool execute(char* lpstr_expression);
bool execute(ExprNode* pNode);
bool execute(ExprNode* pNode,TResult& result); // 返回值或结果变量名
///////////////////////////////////////////////////////////////////
static OTSTR GetExprstring(ExprNode* pNode,char* szParentOperator="#");
static Location& GetExprLocat(ExprNode* pNode);
static void OffsetExprLocat(ExprNode* pNode,Location& offset);
static void GetExprNodes(ExprNode* pNode,TObArray<ExprNode>& List, bool bPrefixMode=true); // 取得归属pNode的所有ExprNode
static void GetExprSuffixstring(ExprNode* pNode,OTSTR& str);
static void DelExprNode(ExprNode* pNode);
char* GetLastError() { return m_strLastError.GetBuffer(); }
private:
OTSTR m_strLastError;
cCallback m_Callback;
bool m_bEnableValueSetAssign;
bool m_bEnableFirstValueSetItem;
//////////////////////////////////////
ExprNode* NewExprNode();
ExprNode* BuildExprTree(CPlusLex& Parser,char* szPrecedeOperator,bool bFirstChildNode=true);
ExprNode* ExtendNode (CPlusLex& Parser,char* szPrecedeOperator, ExprNode* pNode); // if Found error, return NULL
static int GetOperatorType(char* pOperator,bool bAfterIdent=true);
static int GetOperatorPri (char* pOperator,bool bAfterIdent=true);
bool GetValue (LexToken* pToken,TResult& result);
bool GetValue (char* szIdent,TResult& result);
bool AssignValue(LexToken* pToken,TResult& result);
bool AssignValue(char* szIdent,TResult& result);
bool AssignValueData(char* szIdent,char* szData); // 可用于结构赋值
bool calculate(char* lpstr_expression,TResult& result,bool bNullResultAllowed=false); // bNullResultAllowed用于void函数返回
bool calculate(ExprNode* pNode,TResult& result);
bool Check(ExprNode* pNode,bool bValueSetAllowed,Location* plocationOffset=NULL); //### also should check ident validate
};
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Var Definition Object
#define CHOICE_TYPE "Choice"
#define ANY_TYPE "dynamic"
#ifdef _DLL_PROJECT
class CLASS_EXPORT VariableItemAttr: public TName
#else
class VariableItemAttr: public TName
#endif
{
public:
enum type_
{
// basic type
T_INT1 = 0, T_INT2 = 1, T_INT4 = 2,
T_UINT1 = 3, T_UINT2 = 4, T_UINT4 = 5,
T_OCTET = 7, T_DOUBLE = 8,
T_PVOID = 10,
T_PSTRUCT= 128, T_PCHOICE = 129, // 从T_PVOID演伸而来, PCHOICE是含有动态个数变量的结构
T_NULL = -1,
T_ANYTYPE= -2, // 表示T_NULL以外的任意类型(只在procedure定义头的引用变量和返回值中支持)
// ex type
T_BYTE = 20, T_WORD = 21, T_DWORD = 22,
T_STRING = 23, // VisiableString
T_SCHAR = 24, // char[n] used as VisiableString
T_MEMO = 25 // Large octet
};
protected:
type_ m_type;
long m_size;
type_ m_Extype;
OTSTR m_strTypeName;
int m_BitLen; // 对该类型数据赋值, 先(type)Data, 之后取相应Bits
int m_BeginBit;
bool m_bisBitTail;
public:
VariableItemAttr();
virtual ~VariableItemAttr();
bool setAttr(char* varname,VariableItemAttr::type_ type,long size);
VariableItemAttr::type_ getType();
int getSize();
void clone(VariableItemAttr& Object);
void clear();
//////////////////////////////////////////////////////
// Used for struct_typename and user defined typename
void setTypeName(char* typeName,VariableItemAttr::type_ extype);
char* getTypeName();
VariableItemAttr::type_ getExType();
////////////////////////////////////////////////////////////
bool setBitLen(int BitLen);
int getBitLen();
bool setBeginBit(int BeginBit);
int getBeginBit();
void setBitTailFlag(bool bTail);
bool getBitTailFlag();
////////////////////////////////////////////////////////////
static bool isValidType(int type, bool bIncludeAnyType=true);
static bool isBasicType(int type, bool bIncludeNull, bool bIncludePVOID=true);
static bool isBasicType(char* szType, bool bIncludeNull, type_* returnType=NULL, type_* returnExType=NULL);
static VariableItemAttr::type_ getBasicType(VariableItemAttr::type_ ExType);
static char* getTypeDefaultName(int type);
};
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
class StructDefinition;
#ifdef _DLL_PROJECT
class CLASS_EXPORT VariableDefinition: public VariableItemAttr, public TObject
#else
class VariableDefinition: public VariableItemAttr, public TObject
#endif
{
public:
WORD m_nIdentifierId;
bool m_isNetWorkTranMode_L_TO_H;
protected:
StructDefinition* m_pParent;
DWORD m_wParam; // 可用于保存其他属性(指针)
StructDefinition* m_pStructDefinitionObject; // 结构类型的变量,此值有效
bool m_bPassAddress; // Used for procedure params define
OTSTR m_strInitialValueData; // 赋初值
OTSTR m_strDescriptionEnd;
long m_Id;
public:
VariableDefinition();
VariableDefinition(StructDefinition* pParent,WORD nIdentifierId);
~VariableDefinition();
int ObjectTypeId() { return 4; }
OTSTR GetVariableName(bool bShowBeginBitLocat=true);
StructDefinition* GetParent() { return m_pParent; }
void setParam(DWORD param) { m_wParam=param; }
DWORD getParam() { return m_wParam; }
///////////////////////////////////////////////////
void setPassAddressFlag(bool bPassAddress) { m_bPassAddress=bPassAddress; }
bool getPassAddressFlag() { return m_bPassAddress; }
void setInitialValueData(char* szInitialValueData) { m_strInitialValueData=szInitialValueData; }
OTSTR& getInitialValueData() { return m_strInitialValueData; }
void setDescriptionEnd(char* szDescription) { m_strDescriptionEnd=szDescription; }
OTSTR& getDescriptionEnd() { return m_strDescriptionEnd; }
void setStructDefinitionObject(StructDefinition* pStructDefinition); // 如果该变量是结构类型, 有效
StructDefinition* getStructDefinitionObject();
virtual StructDefinition* GetTypeOf() { return NULL; } // 只能用于简单变量
protected:
friend class CVariableItem;
};
#ifdef _DLL_PROJECT
template class CLASS_EXPORT TObArray<VariableDefinition>;
#endif
#ifdef _DLL_PROJECT
class CLASS_EXPORT StructDefinition: public TName, public TObject
#else
class StructDefinition: public TName, public TObject
#endif
{
public:
WORD m_nIdentifierId;
bool m_isNetWorkTranMode_L_TO_H;
protected:
DWORD m_wParam;
TObArray<VariableDefinition> m_VariableDefinitionList;
public:
StructDefinition();
StructDefinition(WORD nIdentifierId);
~StructDefinition();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -