{-------------------------------------------------------------------------------
  过程名:    TUnitKeeper.DetectGlobalVar
  作者:      hongk 2011.10.12
  参数:      const AUnit: TUnitKeeper; const AFilePreProcesser: TFilePreProcess
  返回值:    string
  功能:     检测全局变量的命名规范
-------------------------------------------------------------------------------}
function TUnitKeeper.DetectGlobalVar(const AUnit: TUnitKeeper; const
    AFilePreProcesser: TFilePreProcess): string;
var
  I, J, K, L: Integer;
  nLineNum: Integer;
  sGlobolVar: string;
  oRegExpr: TRegExpr;
  oNonStdName: TNonStandardName;
  nVarEndIndex: Integer;
  nImpIndex: Integer; //implementation的index
begin
  nImpIndex := 0;        //初始化
  nVarEndIndex := 0;     //初始化
  with AFilePreProcesser do
  begin
    for I := 0 to Count - 1 do
    begin
      if WordTypeOf[I] = CWordTypeOfImplementation then
        nImpIndex := I;
        continue;
    end;
  end;
  with AUnit do
  begin
    with AFilePreProcesser do
    begin
      J := 0;
      while J < nImpIndex do
      begin
        if WordTypeOf[J] in [CWordTypeOfFunction, CWordTypeOfProcedure,
          CWordTypeOfConstructor, CWordTypeOfDestructor] then
        begin                                            //这段代码以后重构
          if WordValueOf[J + CTwoMoveLen] = ';' then     //与前面代码重复
            Inc(J, CThreeMoveLen);                  //为了跳过此检测,则向后跳
          if WordValueOf[J + CTwoMoveLen] = '(' then
          begin
            while WordValueOf[J] <> ')' do
              Inc(J);
            while WordValueOf[J] <> ';' do
              Inc(J);
            Inc(J);
          end;
          if WordValueOf[J + CTwoMoveLen] = ':'then
          begin
            while WordValueOf[J] <> ';' do
              Inc(J);
          end;
          if WordValueOf[J + COneMoveLen] = '(' then
          begin
            while WordValueOf[J] <> ')' do
              Inc(J);
            while WordValueOf[J] <> ';' do
              Inc(J);
            Inc(J);
          end;
        end
        else if WordTypeOf[J] = CWordTypeOfClass then
        begin
          if WordValueOf[J - 1] = '=' then      //跳过所有类
          begin
            for I := 0 to ClassCount - 1 do
            begin
              if WordValueOf[J - 2] = ClassItems[I].Name then
              begin
                J := ClassItems[I].EndIndex;
                Inc(J);
              end
            end;
          end
          else
          begin
            Inc(J); //这条语句基本也执行不到 ,完善if...else块
          end;
        end
        else if WordTypeOf[J] = CWordTypeOfVar then  //检测到关键字var
        begin
          for K := J to nImpIndex do
          begin
            if WordTypeOf[K] in [CWordTypeOfType, CWordTypeOfUses, CWordTypeOfConst,
              CWordTypeOfResourcestring, CWordTypeOfImplementation] then
            begin
              nVarEndIndex := K;                 //找到var声明的结束
            end;
          end;          for L := J to nVarEndIndex - 1 do      //循环遍历全局变量声明部分
          begin
            if WordValueOf[L + 3] = ':' then
              begin
                oRegExpr := TRegExpr.Create;
                oRegExpr.Expression := CCheckGlbVarRegExpr;
                try
                  sGlobolVar := WordValueOf[J + CTwoMoveLen];
                  if oRegExpr.Exec(sGlobolVar) then
                  begin
                    Result := '';
                    Inc(J);
                  end
                  else begin
                    nLineNum := LineNumberOf[J + CTwoMoveLen];
                    oNonStdName := TNonStandardName.Create(AUnit.Name, sGlobolVar,
                      SGlobolVarNoStd, nLineNum);
                    AddNoStdNameClass(oNonStdName);
                    Inc(J, COneMoveLen);
                  end
                finally
                  FreeAndNil(oRegExpr);
                end;
              end
            else
              Inc(J);
          end;        end
        else
          Inc(J);
      end;
    end;
  end;
end;