You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 5中如何精准检测保留关键字的大小写问题?

解决Delphi 5中.pas文件保留关键字大写检测的误匹配问题

嘿,我太懂你这种被旧版本Delphi捆住手脚的无奈了——雇主不肯升级,咱只能在有限的工具里找最优解!你之前遇到的误匹配问题,核心是没判断关键字是否作为独立单词存在,而不是其他单词的一部分。

针对Delphi 5的特性(没有原生正则支持),我们可以用一个简洁的词法判断思路:验证关键字的前后字符是否属于Delphi标识符的合法字符(字母、数字、下划线)——如果不是,那这个关键字就是独立的,不会是其他单词的一部分。

具体实现步骤

  1. 先写一个辅助函数,判断某个字符是否是Delphi标识符的组成部分:
function IsIdentifierChar(C: Char): Boolean;
begin
  // Delphi标识符只能包含字母、数字、下划线
  Result := (C in ['a'..'z', 'A'..'Z', '0'..'9', '_']);
end;
  1. 再写一个函数,验证字符串中某个位置的子串是否是独立的关键字:
function IsKeywordAtPos(const S: string; const Keyword: string; Pos: Integer): Boolean;
var
  KeywordLen: Integer;
  IsValidBefore, IsValidAfter: Boolean;
begin
  KeywordLen := Length(Keyword);
  // 先检查位置是否在字符串范围内
  if (Pos < 1) or (Pos + KeywordLen - 1 > Length(S)) then
  begin
    Result := False;
    Exit;
  end;

  // 检查关键字前的字符:如果是字符串开头,或者前一个字符不是标识符字符
  IsValidBefore := (Pos = 1) or not IsIdentifierChar(S[Pos - 1]);
  // 检查关键字后的字符:如果是字符串结尾,或者后一个字符不是标识符字符
  IsValidAfter := (Pos + KeywordLen - 1 = Length(S)) or not IsIdentifierChar(S[Pos + KeywordLen]);

  // 前后都符合条件,才是独立的关键字
  Result := IsValidBefore and IsValidAfter;
end;
  1. 最后是主检测逻辑,逐行扫描文件,找到关键字后先验证是否为独立单词,再判断大小写:
procedure CheckKeywordUppercase(const FilePath: string; const Keywords: array of string);
var
  LineList: TStringList;
  LineIdx, KeyIdx, LowerPos, ExactPos: Integer;
  CurrentLine: string;
begin
  LineList := TStringList.Create;
  try
    LineList.LoadFromFile(FilePath);
    for LineIdx := 0 to LineList.Count - 1 do
    begin
      CurrentLine := LineList[LineIdx];
      for KeyIdx := Low(Keywords) to High(Keywords) do
      begin
        LowerPos := PosIgnoreCase(Keywords[KeyIdx], CurrentLine);
        // 循环找当前行中所有该关键字的匹配位置
        while LowerPos <> 0 do
        begin
          // 先确认是独立关键字,再检查大小写
          if IsKeywordAtPos(CurrentLine, Keywords[KeyIdx], LowerPos) then
          begin
            ExactPos := Pos(Keywords[KeyIdx], CurrentLine);
            // 如果完全匹配的位置不存在,或者和忽略大小写的位置不一致,说明有大写
            if (ExactPos = 0) or (ExactPos <> LowerPos) then
            begin
              Writeln(Format('Line %d: Keyword "%s" contains uppercase characters', [LineIdx + 1, Keywords[KeyIdx]]));
              // 这里可以替换成你需要的错误触发逻辑,比如记录日志、弹窗提示等
            end;
          end;
          // 跳到下一个可能的匹配位置,避免重复检测同一个关键字
          LowerPos := PosIgnoreCase(Keywords[KeyIdx], CurrentLine, LowerPos + Length(Keywords[KeyIdx]));
        end;
      end;
    end;
  finally
    LineList.Free;
  end;
end;

使用说明

  • 确保你的KEYWORDS列表里的关键字都是小写形式(比如['if', 'then', 'else', 'and']),这样PosIgnoreCase能找到所有大小写变体,Pos能精准匹配纯小写的情况。
  • 这个方法不需要枚举所有可能的分隔符(括号、逗号、分号、空格等),只要关键字前后不是标识符字符,就会被判定为独立关键字,完美避免了像MODIFICATION中IF的误匹配。

内容的提问来源于stack exchange,提问作者P.S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:53:15