📄 identifiers.cpp
字号:
#include "stdafx.h"
#include "Identifiers.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define IDEN_HASH_SIZE 10240 //符号表初始大小及增长单位
CIdentifiers::CIdentifiers()
:
mMap(IDEN_HASH_SIZE)
{
Init();
}
CIdentifiers::~CIdentifiers()
{
ClearContent();
}
BOOL CIdentifiers::AddIdentifier(LPCTSTR iName, UINT iType/*=ID_UNDEFINED*/,
CIdentifier* ipIden/*=NULL*/, KEYWORD iKeyIndex/*=KEY_NOT*/)
{
CIdenItem* pItem = Search(iName);
if(pItem != NULL)
{
//if(iType != ID_MACRO) //宏可以重定义
// return FALSE;
DeleteIdentifier(iName);
}
//在析构函数中删除
pItem = new CIdenItem();
pItem->type = iType;
pItem->keyIndex = iKeyIndex;
pItem->pIden = ipIden;
mMap.SetAt(iName, pItem);
return TRUE;
}
BOOL CIdentifiers::AddIdentifier(LPCTSTR iName,
UINT iType,
CIdentifier* ipIden,
LPCTSTR iScope)
{
CString name = iName;
if(iScope != NULL)
name.Insert(0, iScope);
ASSERT(!name.IsEmpty());
CIdenItem* pItem = Search(name);
if(pItem != NULL)
{
if(pItem->type == ID_VARIABLE && iType == ID_VARIABLE)
{
CVariable* pVar = (CVariable*)pItem->pIden;
ASSERT(pVar != NULL);
pVar->UniteVariable((CVariable*)ipIden);
return FALSE;
}
else if(pItem->type == ID_FUNCTION && iType == ID_FUNCTION)
{
CExFunction* pFunc = (CExFunction*)pItem->pIden;
ASSERT(pFunc != NULL);
pFunc->UniteFunction((CExFunction*)ipIden);
return FALSE;
}
else
{
//ASSERT(FALSE);
delete ipIden;
return FALSE;
}
}
//在析构函数中删除
pItem = new CIdenItem();
pItem->type = iType;
pItem->keyIndex = KEY_NOT;
pItem->pIden = ipIden;
mMap.SetAt(iName, pItem);
return TRUE;
}
void CIdentifiers::DeleteIdentifier(LPCTSTR iName)
{
TRACE1("Delete ID: %s \n", iName);
CIdenItem* pItem = Search(iName);
if(pItem == NULL)
return;
delete pItem;
mMap.RemoveKey(iName);
}
void CIdentifiers::Init()
{
ClearContent();
mMap.InitHashTable(IDEN_HASH_SIZE);
//将关字键保存到影射表, 0为KEY_NOT
for(int i=1; i<KEY_COUNT; i++)
{
UINT type = ID_UNDEFINED;
switch(i)
{
case KEY_INT:
type = ID_BUILTIN;
break;
}
AddIdentifier(gKeywords[i], type, NULL, (KEYWORD)i);
}
}
void CIdentifiers::ClearContent()
{
POSITION pos = mMap.GetStartPosition();
CString key;
CIdenItem* pItem = NULL;
while (pos != NULL)
{
mMap.GetNextAssoc(pos, key, pItem);
delete pItem;
}
mMap.RemoveAll();
}
void CIdentifiers::AddGlobalMacro(LPCTSTR iMacros)
{
CString name;
for(UINT i=0; i<strlen(iMacros); i++)
{
char ch = iMacros[i];
if(ch == ';')
{
if(!name.IsEmpty())
{
//AddIdentifier(name, ID_MACRO);
CMacro::ParseMacroFormCode(name);
name.Empty();
}
}
else
{
name += ch;
}
}
//最后一个
if(!name.IsEmpty())
CMacro::ParseMacroFormCode(name);
//AddIdentifier(name, ID_MACRO);
}
CIdenItem* CIdentifiers::Search(LPCTSTR iName)
{
CIdenItem* pItem = NULL;
mMap.Lookup(iName, pItem);
return pItem;
}
KEYWORD CIdentifiers::QueryKeyword(LPCTSTR iWord)
{
CIdenItem* pItem = Search(iWord);
if(pItem == NULL)
return KEY_NOT;
return pItem->keyIndex;
}
BOOL CIdentifiers::IsDefinedMacro(LPCTSTR iName, CPage* ipPage/*=NULL*/)
{
CIdenItem* pItem = Search(iName);
if(pItem == NULL || pItem->type != ID_MACRO)
return FALSE;
if(ipPage == NULL) //不考虑引用者来自什么代码页
return TRUE;
CMacro* pMacro = (CMacro*)pItem->pIden;
if(pMacro == NULL) //全局宏
return TRUE;
return pMacro->IsVisibleByPage(ipPage);
}
CIdenItem* CIdentifiers::UnwindMacro(LPCTSTR iWord,
CPage* ipPage,
CTokenList& ioList,
POSITION& ioPos)
{
CIdenItem* pItem = Search(iWord);
if(pItem == NULL) return NULL;
if(pItem->type == ID_MACRO)
{
CMacro* pMacro = (CMacro*)pItem->pIden;
if(pMacro != NULL/* && (ipPage == NULL || pMacro->IsVisibleByPage(ipPage))*/)
{
pMacro->Unwind(ioList, ioPos);
}
}
return pItem;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -