1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- #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;
- for (int 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;
- for (int 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");
- }
- }
|