123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116 |
- #include "stdafx.h"
- #include "syntax.h"
- CString g_strInString; //定义一个字符串值除了包含字符数字、下划线_外,还包含什么字符常量, C++还会包含#, 如#include
- TCHAR *g_BeginCommentExStr[2];//当前语言的多行注释规则:引起多行注释的字符串
- TCHAR *g_EndCommentExStr[2]; //当前语言的多行注释规则:结束多行注释的字符串
- TCHAR *g_LineCommentStr[2]; //当前语言的单行注释规则, 以行末为中止
- TCHAR *g_BeginStringStr[1]; //当前语言的字符规则, 引起字符串变量的字符串
- TCHAR *g_EndStringStr[1]; //当前语言的字符规则, 结束字符串变量的字符串
- TCHAR *g_BeginCharStr[1]; //当前语言的字符规则, 结束字符型常量的字符串
- TCHAR *g_EndCharStr[1]; //当前语言的字符规则, 结束字符型常量的字符串
- BOOL g_bControl; //控制字符,如C++中的\",因为控制字符\的存在其表示的是一个引号
- BOOL g_bCase; //在进行关键字比较时是否区分大小写
- int g_nstrInStringCount;
- int g_nBeginCommentExStrCount;
- int g_nEndCommentExStrCount;
- int g_nLineCommentStrCount;
- int g_nBeginStringStrCount;
- int g_nEndStringStrCount;
- int g_nBeginCharStrCount;
- int g_nEndCharStrCount;
- int g_nCurLanguage;
- void SetCurLanguage(int nLanguage)
- {
- g_nCurLanguage = nLanguage;
- }
- BOOL IsSynWord(CStringArray &m_strArrayKeyWords, CString &strReadyToTest)
- {
- int val;
- int low=0,hig=m_strArrayKeyWords.GetSize()-1;
- int mid=(low+hig)/2;
-
- while(low<=hig) {
- CString str = m_strArrayKeyWords[mid];
- if(g_bCase)
- val=strReadyToTest.Compare(m_strArrayKeyWords[mid]);
- else
- val=strReadyToTest.CompareNoCase(m_strArrayKeyWords[mid]);
- if(val>0)
- low=mid+1;
- else if(val<0)
- hig=mid-1;
- else
- return TRUE;
- mid=(low+hig)/2;
- }
- return FALSE;
- }
- BOOL IsNumber(CString &strReadyToTest)
- {
- int nLength = strReadyToTest.GetLength();
- //*
- switch (g_nCurLanguage)
- {
- case _BASIC:
- {
- if (nLength > 2 && strReadyToTest[0] == _T('&') &&
- (strReadyToTest[1] == _T('O') || strReadyToTest[1] == _T('H')))
- {
- for (int I = 2; I < nLength; I++)
- {
- if (_istdigit(strReadyToTest[I]) ||(strReadyToTest[I] >= _T('A') &&
- strReadyToTest[I] <= _T('F')) ||(strReadyToTest[I] >= _T('a') &&
- strReadyToTest[I] <= _T('f')))
- continue;
- return FALSE;
- }
- return TRUE;
- }
- }
- break;
- default:
- break;
- } // switch
-
- if (!_istdigit(strReadyToTest[0]))
- return FALSE;
-
- for (int I = 1; I < nLength; I++)
- {
- if (!_istdigit(strReadyToTest[I]) && strReadyToTest[I] != _T('+') &&
- strReadyToTest[I] != _T('-') && strReadyToTest[I] != _T('.') &&
- strReadyToTest[I] != _T('e') && strReadyToTest[I] != _T('E'))
- return FALSE;
- }
- //*/
- return TRUE;
- }
- //装入关键字列表及语法解析规则
- void LoadSynWord(CStringArray &m_strArrayKeyWords, int nLanguage)
- {
- LoadParseRule(nLanguage);
- CString strAllSynWord;
- m_strArrayKeyWords.RemoveAll();
-
- CWinApp *app = AfxGetApp();
- switch ( nLanguage ) {
- case _BASIC:
- strAllSynWord = _T(" Alias,And,Any,As,Base,Boolean,Byref,Byte,Byval,Call,Case,Close,Compare,Const,Currency,Data,Date,Declare,Defbool,Defbyte,Defcur,Defdate,Defdbl,Defdec,Defint,Deflng,Defobj,Defsng,Defstr,Defvar,Dim,Do,Double,Each,Else,Elseif,End,Enum,Eqv,Erase,Error,Event,Exit,Explicit,False,For,Function,Get,Global,Gosub,Goto,If,IIf,Imp,Implements,In,Integer,Is,Let,Lib,Line,Lock,Long,Loop,Lset,New,Next,Not,Object,On,Open,Option,Optional,Or,Preserve,Print,Private,Property,Public,Put,Raiseevent,Redim,Resume,Return,Rset,Select,Set,Single,Static,Stop,String,Sub,Then,To,True,Type,Ubound,Unload,Unlock,Var,Variant,Wend,While,With,Write,Xor,#if,#End,#else,Abs,Array,Asc,Atn,CBool,CByte,CCur,CDate,CDbl,Chr,CInt,CLng,Cos,CreateObject,CSng,CStr,Date,DateAdd,DateDiff,DatePart,DateSerial,DateValue,Day,Exp,Filter,Fix,FormatCurrency,FormatDateTime,FormatNumber,FormatPercent,GetObject,Hex,Hour,InputBox,InStr,InStrRev,Int,IsArray,IsDate,IsEmpty,IsNull,IsNumeric,IsObject,Join,LBound,LCase,Left,Len,LoadPicture,Log,LTrim,Mid,Minute,Month,MonthName,MsgBox,Now,Oct,Replace,RGB,Right,Rnd,Round,RTrim,ScriptEngine,ScriptEngineBuildVersion,ScriptEngineMajorVersion,ScriptEngineMinorVersion,Second,Sgn,Sin,Space,Split,Sqr,StrComp,StrReverse,String,Tan,Time,TimeSerial,TimeValue,Trim,TypeName,UBound,UCase,VarType,Weekday,WeekdayName,Year,SelectYK,ExecuteYK,MannulSetYX,MannulSetYC,IsMainServer,SelectYT,ExecuteYT,CancelMansetYX,CancelMansetYC,Sleeps,");
- strAllSynWord = app->GetProfileString(_T("Keywords"), _T("_BASIC"), strAllSynWord);
- break;
-
- default: //is NONE
- return;
- }
- if(!g_bCase) {
- strAllSynWord.MakeLower();
- }
- CString strTemp;
- int nPosPrior = 0;
- int nPos;
- nPos = strAllSynWord.Find(_T(","), nPosPrior);
- while(nPos!=-1)
- {
- strTemp = strAllSynWord.Mid(nPosPrior+1 , nPos - nPosPrior - 1);
- m_strArrayKeyWords.Add(strTemp);
-
- nPosPrior = nPos;
- nPos = strAllSynWord.Find(_T(","), nPosPrior + 1);
- }
- SortSynWordArray(m_strArrayKeyWords);
- }
- void SortSynWordArray(CStringArray &m_strArrayKeyWords)
- {
- int i,j,x, count = m_strArrayKeyWords.GetSize();
- CString strtmp;
- for(i=1;i<count;i++)
- {
- for(x=0;x<i;x++)
- {
- if(m_strArrayKeyWords.GetAt(i).Compare(m_strArrayKeyWords.GetAt(x))<=0)
- break;
- }
- strtmp=m_strArrayKeyWords.GetAt(i);
- for(j=i;j>x;j--)
- m_strArrayKeyWords.SetAt(j, m_strArrayKeyWords.GetAt(j-1));
- m_strArrayKeyWords.SetAt(x, strtmp);
- }
- }
- /*///////////////////////////////////////////////////////////////////////////////////////////////
- ParseLine(m_strArrayKeyWords, dwCookie, strLine, ColorInfo, nActualItems) );
- 功能:根据所给定的语言解析一行字符串,返回用颜色指示的字符串块,以及解析后的当前行的解析结果,
- 指示解析后当前行处于什么状态,是处于字符串中、注释中、多行注释中还是正常状态。
- 入口参数:
- m_strArrayKeyWords: 关键字列表
- dwCookie: 传入前一行的解析结果,如果前一行是多行注释的开始或者前一行是一软回车时有用
- strLine: 被解析的一行字符串
- ColorInfo: 解析后的颜色值,指定是关键字、字符串、注释还是正常的字符颜色
- nActualItems strLine被解析后的块数
- /////////////////////////////////////////////////////////////////////////////////////////////////*/
- DWORD ParseLine(CStringArray &m_strArrayKeyWords,
- DWORD dwCookie,
- CString &strLine,
- COLORINFO *ColorInfo,
- int &nActualItems)
- {
- //*
- if(g_nCurLanguage ==_HTML || g_nCurLanguage ==_XML) {
- int dwCookieTmp = ParseLineForHtml(m_strArrayKeyWords, dwCookie, strLine, ColorInfo, nActualItems);
- return dwCookieTmp;
- }
- //*/
- int nLength = strLine.GetLength();
- int nIdentBegin = -1;
- int i;
- BOOL bMatch;
- BOOL bInString; //定义一个字符串的界限
- BOOL bRedefineBlock = TRUE;
- BOOL bDecIndex = FALSE;
- BOOL bIsInTheControlChar = FALSE; //指示是否在\的控制中, strControl为空时无效, 初始化时必须为FALSE, 原因见结束定义字符串的处理代码
- if(nLength == 0)
- return dwCookie;
- int I = 0;
- for ( I = 0; ; I++)
- {
- if (bRedefineBlock) //如果开始定义颜色块向下做
- {
- int nPos = I;
- if (bDecIndex)
- nPos--;
- if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
- {
- DEFINE_BLOCK(nPos, COLORINDEX_COMMENT);
- }
- else {
- if (dwCookie & COOKIE_STRING)
- {
- DEFINE_BLOCK(nPos, COLORINDEX_STRING);
- }
- else
- {
- DEFINE_BLOCK(nPos, COLORINDEX_NORMAL);
- }
- }
- bRedefineBlock = FALSE;
- bDecIndex = FALSE;
- }
-
- if (I >= nLength)
- goto Out;
-
- //遇到双字节字体如中文直接跳过
- if(IsDBCSLeadByte(strLine[I])) //检查是不是中文字符
- {
- I += 2;
- if (I >= nLength)
- goto Out;
- else
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- //////////////////////////////
-
- if (dwCookie & COOKIE_COMMENT)
- {
- DEFINE_BLOCK(I, COLORINDEX_COMMENT);
- dwCookie |= COOKIE_COMMENT;
- goto Out;
- }
-
- //如果当前正处于字符串变量中,则应检查是否有结束字符串变量的字符串
- if (dwCookie & COOKIE_STRING)
- {
- if(g_bControl) { //处理控制字符,至少C++会用到(例\"表示引号而不是两个字符)
- if(I > 0) {
- if(strLine[I-1] != '\\') {
- if(strLine[I] != '\\')
- bIsInTheControlChar = FALSE;
- else
- bIsInTheControlChar = TRUE;
- }
- else {
- if(strLine[I] == '\\') {
- if(!bIsInTheControlChar)
- bIsInTheControlChar = TRUE;
- else
- bIsInTheControlChar = FALSE;
- }
- }
- }
- }
-
- int nlen;
- for (int i = 0; i < g_nEndStringStrCount; i++ ) {
- if(g_EndStringStr[i]==NULL)
- continue;
- nlen = strlen(g_EndStringStr[i]);
- bMatch = TRUE;
- //如果strLine的当前字符串值与结束定义字符规则中的一项相同则中止定义字符串
- if (I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) { //向前搜索字符串检查是否与结束定义的字符串相同
- if(strLine[I-nlen+1+k]!=g_EndStringStr[i][k]) {
- bMatch = FALSE; //如果不匹配,中断比较
- break;
- }
- }
- if(bMatch && !bIsInTheControlChar) {
- dwCookie &= ~COOKIE_STRING;
- bRedefineBlock = TRUE;
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- }
- }
- goto cmpnextchar; //如果没有结束定义字符串继续处理strLine的下一个字符
- }
-
- //如果当前正处于字符变量中,则应检查是否有结束字符变量的字符串
- if (dwCookie & COOKIE_CHAR)
- {
- if(g_bControl) { //处理控制字符,至少C++会用到(例\"表示引号而不是两个字符)
- if(I > 0) {
- if(strLine[I-1] != '\\') {
- if(strLine[I] != '\\')
- bIsInTheControlChar = FALSE;
- else
- bIsInTheControlChar = TRUE;
- }
- else {
- if(strLine[I] == '\\') {
- if(!bIsInTheControlChar)
- bIsInTheControlChar = TRUE;
- else
- bIsInTheControlChar = FALSE;
- }
- }
- }
- }
-
- int nlen;
- for (int i = 0; i < g_nEndCharStrCount; i++ ) {
- if(g_EndCharStr[i]==NULL)
- continue;
- nlen = strlen(g_EndCharStr[i]);
- bMatch = TRUE;
- //如果strLine的当前字符值与结束定义字符规则中的一项相同则中止定义字符串
- if (I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) { //向前搜索字符串检查是否与结束定义的字符相同
- if(strLine[I-nlen+1+k]!=g_EndCharStr[i][k]) {
- bMatch = FALSE; //如果不匹配,中断比较
- break;
- }
- }
- if(bMatch && !bIsInTheControlChar) {
- dwCookie &= ~COOKIE_CHAR;
- bRedefineBlock = TRUE;
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- }
- }
- goto cmpnextchar; //如果没有结束定义字符继续处理strLine的下一个字符
- }
- //如果当前正处于多行注释中,则应检查是否有结束多行注释的字符串
- if (dwCookie & COOKIE_EXT_COMMENT) {
- for (int i = 0; i < g_nEndCommentExStrCount; i++ ) {
- if(g_EndCommentExStr[i]==NULL)
- continue;
- bMatch = TRUE;
- int nlen = strlen(g_EndCommentExStr[i]);
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_EndCommentExStr[i][k]) {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- dwCookie &= ~COOKIE_EXT_COMMENT;
- bRedefineBlock = TRUE;
- goto cmpnextchar;
- }
- }
- }
- goto cmpnextchar;
- }
-
- //处理单行注释
- for (i = 0; i < g_nLineCommentStrCount; i++ ) {
- if(g_LineCommentStr[i]==NULL)
- continue;
- int nlen = strlen(g_LineCommentStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_LineCommentStr[i][k]) {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_COMMENT);
- dwCookie |= COOKIE_COMMENT;
- goto Out; //只要发现有单行注释的字符串则退出
- }
- }
- }
- //处理字符串变量的开始
- for (i = 0; i < g_nBeginStringStrCount; i++ ) {
- if(g_BeginStringStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginStringStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_BeginStringStr[i][k]) {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_STRING);
- dwCookie |= COOKIE_STRING;
- goto cmpnextchar;
- }
- }
- }
- //处理字符型变量的开始
- for (i = 0; i < g_nBeginCharStrCount; i++ ) {
- if(g_BeginCharStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginCharStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_BeginCharStr[i][k]) {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_CHAR);
- dwCookie |= COOKIE_CHAR;
- goto cmpnextchar;
- }
- }
- }
- //处理多行注释的开始
- for (i = 0; i < g_nBeginCommentExStrCount; i++ ) {
- if(g_BeginCommentExStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginCommentExStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_BeginCommentExStr[i][k]) {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_COMMENT);
- dwCookie |= COOKIE_EXT_COMMENT;
- if(nlen>1) {
- if ( ++I>= nLength) // 考虑"/*/"的情况
- goto Out;
- }
- goto cmpnextchar;
- }
- }
- }
-
- bInString = FALSE;
- for ( i = 0; i < g_nstrInStringCount; i++ ) {
- BOOL b = ( strLine[I] == g_strInString.GetAt(i) ) ;
- bInString |= b;
- if(bInString)
- break;
- }
- if (isalnum(strLine[I]) || strLine[I] == '_' || bInString)
- {
- if (nIdentBegin == -1)
- nIdentBegin = I;
- }
- else
- {
- if (nIdentBegin >= 0)
- {
- CString strtmp= strLine.Mid(nIdentBegin, I - nIdentBegin);
- if (IsSynWord(m_strArrayKeyWords, strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_SYNTAX);
- }
- else if (IsNumber(strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
- }
- bRedefineBlock = TRUE;
- bDecIndex = TRUE;
- nIdentBegin = -1;
- }
- }
- cmpnextchar:
- i=0; //没有意义,为编译通过而准备
- }
-
- Out: //当遇到单行注释时会直接跳到这儿
- if (nIdentBegin >= 0)
- {
- CString strtmp = strLine.Mid(nIdentBegin, I - nIdentBegin);
- if (IsSynWord(m_strArrayKeyWords, strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_SYNTAX);
- }
- else if (IsNumber(strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
- }
- }
- return dwCookie;
- }
- DWORD ParseLineForHtml(CStringArray &m_strArrayKeyWords,
- DWORD dwCookie,
- CString &strLine,
- COLORINFO *ColorInfo,
- int &nActualItems)
- {
- //对于无多行注释的语法在只取当前行的解析结果时可直接返回
- if(g_nBeginCommentExStrCount==0 && ColorInfo==NULL)
- return dwCookie;
- int nLength = strLine.GetLength();
- int nIdentBegin = -1;
- int i;
- BOOL bMatch;
- BOOL bInString; //定义一个字符串的界限
- BOOL bRedefineBlock = TRUE;
- BOOL bDecIndex = FALSE;
- if(nLength == 0)
- return dwCookie;
- int I = 0;
- for ( I = 0; ; I++)
- {
- if (bRedefineBlock) //如果开始定义颜色块向下做
- {
- int nPos = I;
- if (bDecIndex)
- nPos--;
- if (dwCookie & COOKIE_EXT_COMMENT)
- {
- DEFINE_BLOCK(nPos, COLORINDEX_COMMENT);
- }
- else if (dwCookie & COOKIE_STRING)
- {
- DEFINE_BLOCK(nPos, COLORINDEX_STRING);
- }
- else if (dwCookie & COOKIE_SCRIPT)
- {
- DEFINE_BLOCK(nPos, COLORINDEX_STRING);
- }
- else
- {
- DEFINE_BLOCK(nPos, COLORINDEX_NORMAL);
- }
- bRedefineBlock = FALSE;
- bDecIndex = FALSE;
- }
-
- if (I >= nLength)
- goto Out;
-
- //遇到双字节字体如中文直接跳过
- if(IsDBCSLeadByte(strLine[I])) //检查是不是中文字符
- {
- I += 2;
- if (I >= nLength)
- goto Out;
- else
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- //////////////////////////////
- if ( !(dwCookie & COOKIE_TAG) )
- {
- if (dwCookie & COOKIE_SCRIPT)
- {
- if ( I + 7 < nLength)
- {// the string "</script" length is 7;
- if (strLine[I] != '<')
- continue;
- if (strLine[I+1] != '/')
- continue;
- if (strLine[I+2] != 's' && strLine[I+2] != 'S')
- continue;
- if (strLine[I+3] != 'c' && strLine[I+3] != 'C')
- continue;
- if (strLine[I+4] != 'r' && strLine[I+4] != 'R')
- continue;
- if (strLine[I+5] != 'i' && strLine[I+5] != 'I')
- continue;
- if (strLine[I+6] != 'p' && strLine[I+6] != 'P')
- continue;
- if (strLine[I+7] != 't' && strLine[I+7] != 'T')
- continue;
- dwCookie &= ~COOKIE_SCRIPT;
- goto con;
- }
- continue;
- }
- }
- con:
- if (strLine[I] == '<')
- {
- dwCookie |= COOKIE_TAG;
- DEFINE_BLOCK(I, COLORINDEX_SYNTAX);
- //DEFINE_BLOCK(I+1, COLORINDEX_NORMAL);
- }
-
- if (dwCookie & COOKIE_TAG)
- {
- //如果当前正处于字符串变量中,则应检查是否有结束字符串变量的字符串
- if ( dwCookie & COOKIE_STRING )
- {
- int nlen;
- for (int i = 0; i < g_nEndStringStrCount; i++ )
- {
- if(g_EndStringStr[i]==NULL)
- continue;
- nlen = strlen(g_EndStringStr[i]);
- bMatch = TRUE;
- //如果strLine的当前字符串值与结束定义字符规则中的一项相同则中止定义字符串
- if (I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) { //向前搜索字符串检查是否与结束定义的字符串相同
- if(strLine[I-nlen+1+k]!=g_EndStringStr[i][k])
- {
- bMatch = FALSE; //如果不匹配,中断比较
- break;
- }
- }
- if(bMatch) {
- dwCookie &= ~COOKIE_STRING;
- bRedefineBlock = TRUE;
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- }
- }
- goto cmpnextchar; //如果没有结束定义字符串继续处理strLine的下一个字符
- }
-
- //如果当前正处于字符变量中,则应检查是否有结束字符变量的字符串
- if ( dwCookie & COOKIE_CHAR )
- {
- int nlen;
- for (i = 0; i < g_nEndCharStrCount; i++ )
- {
- if(g_EndCharStr[i]==NULL)
- continue;
- nlen = strlen(g_EndCharStr[i]);
- bMatch = TRUE;
- //如果strLine的当前字符值与结束定义字符规则中的一项相同则中止定义字符串
- if (I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) { //向前搜索字符串检查是否与结束定义的字符相同
- if(strLine[I-nlen+1+k]!=g_EndCharStr[i][k])
- {
- bMatch = FALSE; //如果不匹配,中断比较
- break;
- }
- }
- if(bMatch) {
- dwCookie &= ~COOKIE_CHAR;
- bRedefineBlock = TRUE;
- goto cmpnextchar; //继续处理strLine的下一个字符
- }
- }
- }
- goto cmpnextchar; //如果没有结束定义字符继续处理strLine的下一个字符
- }
-
- //如果当前正处于多行注释中,则应检查是否有结束多行注释的字符串
- if ( dwCookie & COOKIE_EXT_COMMENT ) {
- for (i = 0; i < g_nEndCommentExStrCount; i++ )
- {
- if(g_EndCommentExStr[i]==NULL)
- continue;
- bMatch = TRUE;
- int nlen = strlen(g_EndCommentExStr[i]);
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_EndCommentExStr[i][k])
- {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- dwCookie &= ~COOKIE_EXT_COMMENT;
- bRedefineBlock = TRUE;
- goto cmpnextchar;
- }
- }
- }
- goto cmpnextchar;
- }
-
- //处理字符串变量的开始
- for (i = 0; i < g_nBeginStringStrCount; i++ ) {
- if(g_BeginStringStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginStringStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++)
- {
- if(strLine[I-nlen+1+k]!=g_BeginStringStr[i][k])
- {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_STRING);
- dwCookie |= COOKIE_STRING;
- goto cmpnextchar;
- }
- }
- }
-
- //处理字符型变量的开始
- for (i = 0; i < g_nBeginCharStrCount; i++ )
- {
- if(g_BeginCharStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginCharStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++)
- {
- if(strLine[I-nlen+1+k]!=g_BeginCharStr[i][k])
- {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch) {
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_CHAR);
- dwCookie |= COOKIE_CHAR;
- goto cmpnextchar;
- }
- }
- }
-
- //处理多行注释的开始
- for (i = 0; i < g_nBeginCommentExStrCount; i++ )
- {
- if(g_BeginCommentExStr[i]==NULL)
- continue;
- int nlen = strlen(g_BeginCommentExStr[i]);
- bMatch = TRUE;
- if(I >= nlen - 1)
- {
- for(int k=0; k<nlen; k++) {
- if(strLine[I-nlen+1+k]!=g_BeginCommentExStr[i][k])
- {
- bMatch = FALSE;
- break;
- }
- }
- if(bMatch)
- {
- nActualItems--;
- DEFINE_BLOCK(I - nlen + 1, COLORINDEX_COMMENT);
- dwCookie |= COOKIE_EXT_COMMENT;
- goto cmpnextchar;
- }
- }
- }
-
- bInString = FALSE;
- for ( i = 0; i < g_nstrInStringCount; i++ )
- {
- BOOL b = ( strLine[I] == g_strInString.GetAt(i) );
- bInString |= b;
- if(bInString)
- break;
- }
- if (isalnum(strLine[I]) || strLine[I] == '_' || bInString)
- {
- if (nIdentBegin == -1)
- nIdentBegin = I;
- }
- else
- {
- if (nIdentBegin >= 0)
- {
- CString strtmp= strLine.Mid(nIdentBegin, I - nIdentBegin);
- if (IsNumber(strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
- }//*
- else
- {
- if(strtmp.CompareNoCase(_T("script"))==0)
- {
- if( nIdentBegin > 0 && strLine[nIdentBegin-1] == '<')
- {
- dwCookie |= COOKIE_SCRIPT;
- }
- else if( nIdentBegin > 1 && strLine[nIdentBegin-1] == '/' && strLine[nIdentBegin-2] == '<')
- {
- dwCookie &= ~COOKIE_SCRIPT;
- DEFINE_BLOCK(nIdentBegin-2, COLORINDEX_SYNTAX);
- }
- }
- if( (nIdentBegin > 0) && (strLine[nIdentBegin-1] == '=') )
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_NORMAL);
- }
- else
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_SYNTAX);
- }
- }
- bRedefineBlock = TRUE;
- bDecIndex = TRUE;
- nIdentBegin = -1;
- }
- }
- }
- if (strLine[I] == '>')
- {
- bRedefineBlock = FALSE;
- dwCookie &= ~COOKIE_TAG;
- DEFINE_BLOCK(I, COLORINDEX_SYNTAX);
- if(dwCookie & COOKIE_SCRIPT)
- {
- DEFINE_BLOCK(I+1, COLORINDEX_STRING);
- }
- else if( (I > 1) && (strLine[I-1]=='-') && (strLine[I-2]=='-') )
- {
- DEFINE_BLOCK(I, COLORINDEX_COMMENT);
- }
- else
- {
- DEFINE_BLOCK(I+1, COLORINDEX_NORMAL);
- }
- }
- cmpnextchar:
- i = 0;
- }
-
- Out: //当遇到单行注释时会直接跳到这儿
- if (dwCookie & COOKIE_TAG) {
- if (nIdentBegin >= 0)
- {
- CString strtmp = strLine.Mid(nIdentBegin, I - nIdentBegin);
- if (IsSynWord(m_strArrayKeyWords, strtmp))
- {
- if( (nIdentBegin > 0) && (strLine[nIdentBegin-1]=='/') ) {
- DEFINE_BLOCK(nIdentBegin-1, COLORINDEX_SYNTAX);
- }
- else {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_SYNTAX);
- }
- if(strtmp.CompareNoCase(_T("script"))==0) {
- if( nIdentBegin > 0 && strLine[nIdentBegin-1] == '<') {
- dwCookie |= COOKIE_SCRIPT;
- }
- else if( nIdentBegin > 1 && strLine[nIdentBegin-1] == '/' && strLine[nIdentBegin-12] == '<') {
- dwCookie &= ~COOKIE_SCRIPT;
- DEFINE_BLOCK(nIdentBegin-2, COLORINDEX_NORMAL);
- }
- }
- }
- else if (IsNumber(strtmp))
- {
- DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
- }
- }
- }
- return dwCookie;
- }
- void LoadParseRule(int nlanguage)
- {
- g_strInString = _T("");
- g_nstrInStringCount = 0;
- g_nBeginCommentExStrCount = 0;
- g_nEndCommentExStrCount = 0;
- g_nLineCommentStrCount = 0;
- g_nBeginStringStrCount = 0;
- g_nEndStringStrCount = 0;
- g_nBeginCharStrCount = 0;
- g_nEndCharStrCount = 0;
- g_bControl = FALSE;
- g_bCase = FALSE;
- /////////////////////////////////////////////////////////////////////////////
- if(g_BeginCommentExStr[0]!= NULL) {delete g_BeginCommentExStr[0]; g_BeginCommentExStr[0] = NULL;}
- if(g_BeginCommentExStr[1]!= NULL) {delete g_BeginCommentExStr[1]; g_BeginCommentExStr[1] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_EndCommentExStr[0]!= NULL) {delete g_EndCommentExStr[0]; g_EndCommentExStr[0] = NULL;}
- if(g_EndCommentExStr[1]!= NULL) {delete g_EndCommentExStr[1]; g_EndCommentExStr[1] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_LineCommentStr[0]!= NULL) {delete g_LineCommentStr[0]; g_LineCommentStr[0] = NULL;}
- if(g_LineCommentStr[1]!= NULL) {delete g_LineCommentStr[1]; g_LineCommentStr[1] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_BeginStringStr[0]!= NULL) {delete g_BeginStringStr[0]; g_BeginStringStr[0] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_EndStringStr[0]!= NULL) {delete g_EndStringStr[0]; g_EndStringStr[0] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_BeginCharStr[0]!= NULL) {delete g_BeginCharStr[0]; g_BeginCharStr[0] = NULL;}
- /////////////////////////////////////////////////////////////////////////////
- if(g_EndCharStr[0]!= NULL) {delete g_EndCharStr[0]; g_EndCharStr[0] = NULL;}
- CString strInString = _T("");
- CString BeginCommentExStr0 = _T("");
- CString EndCommentExStr0 = _T("");
- CString BeginCommentExStr1 = _T("");
- CString EndCommentExStr1 = _T("");
- CString LineCommentStr0 = _T("");
- CString LineCommentStr1 = _T("");
- CString BeginStringStr0 = _T("");
- CString EndStringStr0 = _T("");
- CString BeginCharStr0 = _T("");
- CString EndCharStr0 = _T("");
- switch ( nlanguage ) {
- case _BASIC:
- strInString = _T("&#");
- LineCommentStr0=_T("\'");
- LineCommentStr1=_T("rem");
- BeginStringStr0=_T("\"");
- EndStringStr0=_T("\"");
- break;
- default: //is NONE
- return;
- }
- CString strLanguagePos = _T("icrEdit__ParseRule");
- strLanguagePos += GetLanguageString(nlanguage);
- CString strtmp;
- CWinApp *app = AfxGetApp();
- g_bCase = app->GetProfileInt( strLanguagePos, _T("g_bCase"), g_bCase);
- //一个单词包含的字符常量
- strtmp = app->GetProfileString( strLanguagePos, _T("strInString"), strInString);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_strInString = strtmp;
- g_nstrInStringCount = g_strInString.GetLength();
- }
- //一个单词包含的字符常量
- //开始多行注释的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("BeginCommentExStr0"), BeginCommentExStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_BeginCommentExStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_BeginCommentExStr[0], strtmp);
- g_nBeginCommentExStrCount = 1;
- }
- strtmp = app->GetProfileString( strLanguagePos, _T("BeginCommentExStr1"), BeginCommentExStr1);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_BeginCommentExStr[1] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_BeginCommentExStr[1], strtmp);
- g_nBeginCommentExStrCount = 2;
- }
- //开始多行注释的字符串
- //结束多行注释的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("EndCommentExStr0"), EndCommentExStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_EndCommentExStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_EndCommentExStr[0], strtmp);
- g_nEndCommentExStrCount = 1;
- }
- strtmp = app->GetProfileString( strLanguagePos, _T("EndCommentExStr1"), EndCommentExStr1);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_EndCommentExStr[1] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_EndCommentExStr[1], strtmp);
- g_nEndCommentExStrCount = 2;
- }
- //结束多行注释的字符串
- //开始单行注释的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("LineCommentStr0"), LineCommentStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_LineCommentStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_LineCommentStr[0], strtmp);
- g_nLineCommentStrCount = 1;
- }
- strtmp = app->GetProfileString( strLanguagePos, _T("LineCommentStr1"), LineCommentStr1);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_LineCommentStr[1] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_LineCommentStr[1], strtmp);
- g_nLineCommentStrCount = 2;
- }
- //开始单行注释的字符串
- //开始字符串变量的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("BeginStringStr0"), BeginStringStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_BeginStringStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_BeginStringStr[0], strtmp);
- g_nBeginStringStrCount = 1;
- }
- //开始字符串变量的字符串
- //结束字符串变量的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("EndStringStr0"), EndStringStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_EndStringStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_EndStringStr[0], strtmp);
- g_nEndStringStrCount = 1;
- }
- //结束字符串变量的字符串
- //开始字符型常量的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("BeginCharStr0"), BeginCharStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_BeginCharStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_BeginCharStr[0], strtmp);
- g_nBeginCharStrCount = 1;
- }
- //开始字符型常量的字符串
- //结束字符型常量的字符串
- strtmp = app->GetProfileString( strLanguagePos, _T("EndCharStr0"), EndCharStr0);
- strtmp.TrimLeft();strtmp.TrimRight();strtmp.TrimLeft(0x9);strtmp.TrimRight(0x9);
- if(!strtmp.IsEmpty()) {
- g_EndCharStr[0] = new TCHAR[strtmp.GetLength()+1];
- strcpy(g_EndCharStr[0], strtmp);
- g_nEndCharStrCount = 1;
- }
- //结束字符型常量的字符串
- }
- CString GetLanguageString(int nlanguage)
- {
- switch ( nlanguage ) {
- case _BASIC:
- return _T("_BASIC");
- default: //is NONE
- return _T("Plain text");
- }
- }
- void DeleteAllocString()
- {
- //删除分配的语法解析规则字符串指针以防止内存泄漏
- if(g_BeginCommentExStr[0]!= NULL) {delete g_BeginCommentExStr[0]; g_BeginCommentExStr[0] = NULL;}
- if(g_BeginCommentExStr[1]!= NULL) {delete g_BeginCommentExStr[1]; g_BeginCommentExStr[1] = NULL;}
- if(g_EndCommentExStr[0]!= NULL) {delete g_EndCommentExStr[0]; g_EndCommentExStr[0] = NULL;}
- if(g_EndCommentExStr[1]!= NULL) {delete g_EndCommentExStr[1]; g_EndCommentExStr[1] = NULL;}
- if(g_LineCommentStr[0]!= NULL) {delete g_LineCommentStr[0]; g_LineCommentStr[0] = NULL;}
- if(g_LineCommentStr[1]!= NULL) {delete g_LineCommentStr[1]; g_LineCommentStr[1] = NULL;}
- if(g_BeginStringStr[0]!= NULL) {delete g_BeginStringStr[0]; g_BeginStringStr[0] = NULL;}
- if(g_EndStringStr[0]!= NULL) {delete g_EndStringStr[0]; g_EndStringStr[0] = NULL;}
- if(g_BeginCharStr[0]!= NULL) {delete g_BeginCharStr[0]; g_BeginCharStr[0] = NULL;}
- if(g_EndCharStr[0]!= NULL) {delete g_EndCharStr[0]; g_EndCharStr[0] = NULL;}
- }
- CString GetLineCommentString(int nlanguage)
- {
- CString LineCommentStr0 = _T("");
- switch ( nlanguage ) {
- case _BASIC:
- LineCommentStr0=_T("\'");
- break;
- default: //is NONE
- return LineCommentStr0;
- }
- CString strLanguagePos = _T("icrEdit__ParseRule");
- strLanguagePos += GetLanguageString(nlanguage);
- CWinApp *app = AfxGetApp();
- LineCommentStr0 = app->GetProfileString( strLanguagePos, _T("LineCommentStr0"), LineCommentStr0);
- LineCommentStr0.TrimLeft();
- LineCommentStr0.TrimRight();
- LineCommentStr0.TrimLeft(0x9);
- LineCommentStr0.TrimRight(0x9);
- if(!LineCommentStr0.IsEmpty())
- return LineCommentStr0;
- LineCommentStr0 = app->GetProfileString( strLanguagePos, _T("LineCommentStr1"), LineCommentStr0);
- LineCommentStr0.TrimLeft();
- LineCommentStr0.TrimRight();
- LineCommentStr0.TrimLeft(0x9);
- LineCommentStr0.TrimRight(0x9);
- return LineCommentStr0;
- }
- //根据显示给用户看的字符串取出语言
- int GetLanguageByStringShow(CString strlanguage)
- {
- if(strlanguage==_T("Basic"))
- return _BASIC;
- return 0;
- }
- //根据语言返回给用户看的字符串
- CString GetStringShowByLanguage(int nlanguage)
- {
- switch(nlanguage) {
- case _BASIC:
- return _T("Basic");
- default:
- return _T("Plain text");
- }
- }
|