SQL解析器之词法分析--预扫描

我设计的在SQL语句解析之前,先必须进行预扫描,其作用是

  • 去除注释
  • 去除前导空格
  • 并且将各单词之间的多个空格压缩成一个
  • 并且去掉所有的换行符便于语法分析
 1 function TSCanner.Pro_Process(str: string): string;
2 var
3 sTemp, sTemp1: string;
4 sList: TStringList;
5 iPos, iPos1, i: Integer;
6 begin
7 sList := TStringList.Create;
8 sList :=SplitString(str,#13#10);
9 for i := 0 to sList.Count - 1 do
10 begin
11 sTemp := sList[i];
12 iPos := Pos('//', stemp);
13 if iPos > 0 then
14 begin
15 sTemp1 := Copy(sTemp, 1, iPos - 1);
16 sList[i] := sTemp1;
17 end;
18 end;
19
20 sTemp := sList.Text;
21 sList.Free;
22
23 sTemp := StringReplace(sTemp, #13#10, ' ', [rfReplaceAll]);
24 while Pos(' ', sTemp) > 0 do
25 sTemp := StringReplace(sTemp, ' ', ' ', [rfReplaceAll]);
26 sTemp := Trim(StringReplace(sTemp, ', ', ',', [rfReplaceAll]));
27 iPos :=Pos('/*',sTemp);
28 if iPos>0 then
29 begin
30 iPos1 :=Pos('*/',sTemp);
31 if iPos1>0 then
32 begin
33 sTemp :=Trim(Copy(sTemp, 1,iPos-1)+copy(sTemp,iPos1+2,Length(sTemp)-ipos1-1));
34 end;
35 end;
36 sTemp1 := UpdateAllKey(sTemp);
37 Result := sTemp1;
38 end;
39
40
41 function TSCanner.UpdateAllKey(sWords:string):string;
42 var
43 i,j:Integer;
44 sList:TStringList;
45 sTemp:string;
46 begin
47 sTemp :=sWords;
48 sList :=TStringList.Create;
49 sList.CommaText :=StringReplace(sTemp,' ',#13#10,[rfReplaceAll]);
50 for i:=0 to sList.Count -1 do
51 begin
52 for j:=Low(SQLKEY) to High(SQLKEY) do
53 begin
54 if LowerCase(sList[i])=LowerCase(SQLKEY[j].Content) then
55 begin
56 sTemp :=StringReplace(sTemp,sList[i],UpperCase(SQLKEY[j].Content),[rfReplaceAll]);
57 Break;
58 end;
59 end;
60 end;
61 sList.Free;
62 Result :=sTemp;
63 end;


 

上一篇:Word中使用正则表达式


下一篇:Redis学习(四) -- Redis的事务