第二部分:基类 TCodeColor
一、本章说明 暂时先公开基类代码,和子类的部分代码,子类中字符分析函数 Analyzer 的实现暂时不公开,主要出于这样的考虑: 1、基类已实现大部分共用功能,但是还不完善,代码也不规范。特别是各个版本的 Analyzer 中都会用到这些功能,所以待听取路高手的意见,将基类改进完善后,将重写 Analyzer 并在第三部分中公开; 2、如果您急于使用本程序,有两种方法:一是您可以留下邮箱地址,我会寄给您一份程序;二是利用并不完善基类 TCodeColor 继承出一个子类,自己实现 Analyzer ,字符分析的方法可以参考 李马 的 行云流水网站 提供的 C 语言版 《Pascal 词法分析器》源代码; 3、欢迎大家提出宝贵意见、想法,同时尽可能的留下相应的解决方案。
二、用户端 很简单,不做过多说明:
//┏━━━━━━━━━━━━━━┓ //┃代码着色:CodeColor v1.0 ┃ //┃来自:悄然无声的 Blog ┃ //┗━━━━━━━━━━━━━━┛
procedure TfrmMain.Button1Click(Sender: TObject); var cc:TCodeColor; begin cc:=TCC_Delphi.Create('gainsboro',720); cc.Source:=Memo1.Text; cc.Processor; Memo1.Text:=cc.OutPut; Memo1.Lines.SaveToFile('result.htm'); end; |
二、基类 TCodeColor 也很简单,在注释中已有详细说明。IsAlpha 和 IsNumberic 都用的土方法,或许还有更好的实现方法。IsMBCSChar 是经过滚龙的三次指点才变成现在这个样子的,原先叫 IsHZ ,所以欢迎大家指点一二。
//┏━━━━━━━━━━━━━━┓ //┃代码着色:CodeColor v1.0 ┃ //┃来自:悄然无声的 Blog ┃ //┗━━━━━━━━━━━━━━┛
unit CodeColor;
interface
uses Classes,SysUtils;
type TCodeColor = class(TObject) protected FOutPut: string; //处理后的 Html 文本 FSource: string; //处理前的源文本 FPosition: Integer; //当前被处理字符的位置 FBGColor:string; //Html 中表格的背景颜色 FWidth:string;//Html 中表格的宽度 ReserveWords: TStrings;//关键字 //抽象的字符分析函数,便于在子类中有不同的实现 procedure Analyzer; virtual; abstract;
//下面四个函数在子类中的 Analyzer 中使用 function GetNextChar: Char;//返回下一个字符 function IsAlpha(const ch: Char): Boolean; //判断是否是字母 function IsMBCSChar(const ch: Char): Boolean;//判断字符是否为多字节字符 function IsNumberic(const ch: Char): Boolean; //判断是否是数字 public constructor Create(const bgColor:string;width:Integer); property Source: string write FSource;//属性:设置源文本 procedure Processor;//处理机 property OutPut: string read FOutPut; //属性:返回处理后的 Html 文本 end; implementation
{ ********************************** TCodeColor ********************************** }
constructor TCodeColor.Create(const bgColor: string; width: Integer); begin FBGColor:=BGColor; FWidth:=InttoStr(width); end;
//处理机 procedure TCodeColor.Processor; begin FPosition:=0; FOutput:='';
while(FPosition<Length(FSource)) do begin Analyzer; end;
FOutput:='<TABLE BGCOLOR="' + FBGColor + '" ' + 'BORDER="1" CELLSPACING="1" CELLPADDING="1" ' + 'width="' + FWidth + '">' + '<TR><TD>' + '<font face="Courier New" size="2">' + FOutput + '</font>' + '</TD></TR></TABLE>'; end;
//返回下一个字符 function TCodeColor.GetNextChar: Char; begin inc(FPosition); result:=FSource[FPosition]; end;
//判断是否是字母 function TCodeColor.IsAlpha(const ch: Char): Boolean; var i: Integer; begin i:=ord(ch); if( ((i>=65)and(i<=90)) or ((i>=97)and(i<=122)) ) then result:=true else result:=false; end;
//判断字符是否为多字节字符 function TCodeColor.IsMBCSChar(const ch: Char): Boolean; begin Result := (ByteType(ch, 1) <> mbSingleByte); end;
//判断是否是数字 function TCodeColor.IsNumberic(const ch: Char): Boolean; var i: Integer; begin i:=ord(ch); if(i>57) or (i<48) then result:=false else result:=true; end;
end. |
三、子类 TCC_Delphi 这里仅给出一个结构,Analyzer 在这暂不实现,后面会专门发一篇帖子介绍。
//┏━━━━━━━━━━━━━━┓ //┃代码着色:CodeColor v1.0 ┃ //┃来自:悄然无声的 Blog ┃ //┗━━━━━━━━━━━━━━┛
unit CC_Delphi;
interface
uses CodeColor,StrUtils,Classes;
type TCC_Delphi = class(TCodeColor) protected procedure Analyzer; override; public constructor Create(const bgColor:string;width:Integer); end;
implementation
{ ********************************** TCC_Delphi ********************************** }
constructor TCC_Delphi.Create(const bgColor:string;width:Integer); begin inherited Create(bgColor,width); ReserveWords:=TStringList.Create(); ReserveWords.LoadFromFile('Delphi_ReserveWords.txt'); end;
//字符分析函数 procedure TCC_Delphi.Analyzer; var strToken:string; ch:Char; begin //此处暂不实现,给出部分样例仅供参考
//.........................
//判断标识符和关键字的情况 if(IsAlpha(ch) or IsMBCSChar(ch)) then begin while (IsAlpha(ch) or IsNumberic(ch) or (ch = '_') or IsMBCSChar(ch)) do begin strToken:=strToken+ch; ch := GetNextChar; end; dec(FPosition); if(ReserveWords.IndexOf(strToken)>0) then FOutput:=FOutput + '<strong><font color="blue">' + strToken + '</font></strong>' else FOutput:=FOutput + strToken; exit; end;
//......................... end; end. | 
|