原版的fpc/lazarus不支持中文变量,需然很多人对使用中文变量有不同看法(delphi等语言早就支持中文变量),但如果lazarus能支持中文也算给大家多一个选择,在网友“yoozaa和谐^_^沉迷LAZ”启发下,经debug发现,只需修改2个文件(fpc的scanner.pas和lazarus的keywordfunclists.pas),共5个位置(原理很简单,就是将IsIdentChar将字符范围扩大)就能实现fpc/lazarus支持中文变量/过程/函数,需要的就按以下方法修改就可以。
测试环境:
FPC:3.2.2
lazarus:Lazarus 2.2.2和2.2.4
本修改方案存在以下问题:
FPC完全支持中文变量、过程和函数,但lazarus的IDE使用中文变量、过程和函数时要用(英文半角的a..z,A..Z或_)开头(建议用英文小写的下划线_)。
如果你有更完美的解决方案请和我分享!
一、只需修改fpcsrc/compiler/scanner.pas以下4个位置,并重新编译fpcsrc源码(我用FPCUPdeluxe重新编译FPC,重新编译前需要在setup+设置FPC/Laz rebuild only打勾,然后点击Only FPC编译就可以)。
1、第4054行:
将'A'..'Z':修改为:'A'..'Z',#$80..#255 :
case c of '_', '0'..'9', 'A'..'Z',#$80..#255 ://2022.11.26 LBZ begin if i<255 then
2、第4729行:
将
if c in ['A'..'Z','a'..'z','_'] then
改为:
if (c in ['A'..'Z','a'..'z','_',#$80..#255]) then
if (c in ['A'..'Z','a'..'z','_',#$80..#255]) then //2022.11.26 LBZ begin readstring; token:=_ID; idtoken:=_ID;
3、第4822行:
将
if not (c in ['_','A'..'Z','a'..'z']) then
message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
改为:
if not ((c in ['_','A'..'Z','a'..'z',#$80..#255])) then
message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
if not ((c in ['_','A'..'Z','a'..'z',#$80..#255])) then //2022.11.26 LBZ message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
4、第5456行:
将
'a'..'z' :
改为:
'a'..'z' , #$80..#255:
case c of '_', 'A'..'Z', 'a'..'z' , #$80..#255://2022.11.26 LBZ begin readstring; optoken:=_ID;
二、修改lazarus\components\codetools\keywordfunclists.pas
将第852行:
IsIdentChar[c]:=c in ['a'..'z','A'..'Z','_','0'..'9'];
改为:
IsIdentChar[c]:=c in ['a'..'z','A'..'Z','_','0'..'9',#$80..#255];
procedure InternalInit; var c: char; w: word; begin for c:=Low(char) to High(char) do begin case c of 'a'..'z':CharToIHash[c]:=ord(c)-ord('a')+1; 'A'..'Z':CharToIHash[c]:=ord(c)-ord('A')+1; else CharToIHash[c]:=ord(c); end; UpChars[c]:=upcase(c); IsLineEndChar[c]:=c in [#10,#13]; IsSpaceChar[c]:=c in [#0..#32]; IsIdentStartChar[c]:=c in ['a'..'z','A'..'Z','_']; IsIdentChar[c]:=c in ['a'..'z','A'..'Z','_','0'..'9',#$80..#$FF];//2022.11.27 LBZ IsDottedIdentChar[c]:=c in ['.','a'..'z','A'..'Z','_','0'..'9']; IsNumberChar[c]:=c in ['0'..'9']; IsCommentStartChar[c]:=c in ['/','{','(']; IsCommentEndChar[c]:=c in ['}',')',#13,#10]; IsHexNumberChar[c]:=c in ['0'..'9','a'..'f','A'..'F']; IsOctNumberChar[c]:=c in ['0'..'7']; IsEqualOperatorStartChar[c]:=c in [':','+','-','/','*','<','>']; IsWordChar[c]:=c in ['a'..'z','A'..'Z']; IsNonWordChar[c]:=(c in [#0..#127]) and (not IsIdentChar[c]); IsAfterFloatPointChar[c]:=c in ['0'..'9','e','E']; end;
最后重新编译lazarus就可以不完美支持中文变量/过程/函数。
中文变量使用效果: