界面库

抽象语法高亮类
This commit is contained in:
JianjunLiu
2022-09-15 14:27:24 +08:00
parent a89d92ad42
commit 1abe441ca6
3 changed files with 542 additions and 586 deletions
+20 -586
View File
@@ -1231,606 +1231,40 @@ type TINISynHigLighter = class(TSynHighLighter)
return r;
end
end
type TJsonSynHighLighter = class(TSynHighLighter)
function Create(AOwner);
type TJsonSynHighLighter = class(tcustomsynhighlighter)
function create(AOwner);
begin
inherited;
Clean();
FChangeDeal := true;
end
function Clean();override;
begin
// { "
FTokens := array();
FSates := array(0); //状态
FBBStates := array(new TBBState(")"));
FBEStates := array(new TBBState("}"));
FMBStates := array(new TBBState("]"));
FSatesCount := 1; //改变的行
end
function Recycling();override;
begin
inherited;
Clean();
end
function SetInValidateIndex(idx); override; //当前改变的行
begin
idx := max(1,idx);
if not(FChangeDeal) and idx>FSatesCount then return ;
fdolastline := idx-2;
if length(FSates)>=idx then
FSatesCount := idx;
else FSatesCount := length(FSates)-1;
if FSatesCount = 1 then Clean();
FChangeDeal := false;
end
function ParserTokenLines(s,b,e,cst,tokens);
begin
label L_XCDG;
if cst=0 then
begin
idx := b;
ttk := "";
while idx<=e do
begin
vi := s[idx];
case vi of
" ","\t":
begin
SetTToken(tokens,ttk,idx-1);
end
"(",")":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,")");
end
"{","}":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,"}");
end
"[","]":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,"]");
end
":",",":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,vi);
end
'"':
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,'"');
return ParserTokenLines(s,idx+1,e,'"',tokens);
end else
begin
ttk+=vi;
end
end ;
idx++;
end
SetTToken(tokens,ttk,idx-1);
end else //查找
begin
if b>e then return cst;
r := FindRightChar(cst,s,b,e,"\\");
if r=0 then //没找到
begin
Setttoken(tokens,s[b:],e,cst);
return cst;
end else //找到
if r<=e then
begin
if b<r then
begin
Setttoken(tokens,s[b:r-1],r-1,cst);
end
Setttoken(tokens,s[r:r],r,cst);
if r<e then
begin
//消除递归
b := r+1;
cst := 0;
goto L_XCDG;
//return ParserTokenLines(s,r+1,e,0,tokens); //递归方式
end
end
end
end
function InsureTokenParserd(LastLine);override; //解析
begin
ls := Lines;
FChangeDeal := true;
if fdolastline>=LastLine then return ;
fdolastline := LastLine;
for i:= FSatesCount-1 to LastLine do
begin
if i<0 then continue;
s := ls.GetStringByIndex(i);
cst := FSates[i];
tks := array();
FCBBState := FBBStates[i].Clone;
FCBEState := FBEStates[i].Clone;
FCMBState := FMBStates[i].Clone;
FSates[i+1] := ParserTokenLines(s,1,length(s),cst,tks);
FBBStates[i+1] :=FCBBState;//.Clone;
FBEStates[i+1] := FCBEState;//.Clone;
FMBStates[i+1] := FCMBState;
FSatesCount := i+1;
FTokens[i] := tks;
end
end
function GetLineTokens(idx);override;
begin
if idx<FSatesCount then
return FTokens[idx];
end
protected
function SetTToken(tokens,ttk,idx,ext);virtual; //设置token
begin
d := inherited;
if not d then return ;
tk := d.FValue;
if ext in array(")","]","}") then d.FFColor := 0xFF0000;
if ext="}" and tk="{" then
begin
FCBEState.GetLeft();
d.FMATe := FCBEState.GetSate();
end else
if ext="}" and tk="}" then
begin
FCBEState.GetRight();
d.FMATe := FCBEState.GetSate();
end else
if ext="]" and tk="[" then
begin
FCMBState.GetLeft();
d.FMATe := FCMBState.GetSate();
end else
if ext="]" and tk="]" then
begin
FCMBState.GetRight();
d.FMATe := FCMBState.GetSate();
end else
if ext=")" and tk="(" then
begin
FCBBState.GetLeft();
d.FMATe := FCBBState.GetSate();
end else
if ext=")" and tk=")" then
begin
FCBBState.GetRight();
d.FMATe := FCBBState.GetSate();
end else
if ext='"' then
begin
D.FFColor := 0x8B008B;
end else
if tk in array("true","false","null") then
begin
d.FFColor := 0x0000FF;
end else
if tk in array(":",",") then
begin
d.FFColor := 0x0000FF;
end else
if StrIsANumber(tk) then d.FFColor := 0x666666;
return d;
end
private
function StrIsANumber(s);
begin
if not s then return 0;
r := (1=ParseRegExpr("^[+-]?[\\d]+",s,"",result,MPos,Mlen)) ;
return r;
end
function FindRightChar(c,s,b,e,zy); //查找封闭的字符
begin
i := b;
while i<=e do
begin
si := s[i];
if si=zy then
begin
i+=2;
if i>e then return 0;
continue;
end
if s[i]=c then //找到了
begin
return i;
end
i++;
end
return 0; //没找到
for i := b to e do
begin
if s[i]=c then //找到了
begin
return i;
end
end
ExecuteCommand("string",array(('"',"\\")));
ExecuteCommand("keywords",array("null","true","false"));
ExecuteCommand("rowannotes",array());
ExecuteCommand("blockannotes",array());
ExecuteCommand("syms",array("{","}","[","]","<",">",":",",","="));
ExecuteCommand("pairs",array(("{","}"),("[","]")));
end
private
fdolastline;
FTokens;
FSates;
FSatesCount;
FCBBState;
FCBEState;
FCMBState;
FBBStates;
FBEStates;
FMBStates ;
end
type ttfmhighlighter = class(TSynHighLighter)
function Create(AOwner);
type ttfmhighlighter = class(tcustomsynhighlighter)
function create(AOwner);
begin
inherited;
Clean();
FChangeDeal := true;
end
function Clean();override;
begin
// { "
FTokens := array();
FSates := array(0); //状态
FBBStates := array(new TBBState(")"));
FBEStates := array(new TBBState("}"));
FMBStates := array(new TBBState("]"));
FbgedStates := array(new TBBState("end"));
FjkhStates := array(new TBBState(">"));
FSatesCount := 1; //改变的行
end
function Recycling();override;
begin
inherited;
Clean();
end
function SetInValidateIndex(idx); override; //当前改变的行
begin
idx := max(1,idx);
if not(FChangeDeal) and idx>FSatesCount then return ;
fdolastline := idx-2;
if length(FSates)>=idx then
FSatesCount := idx;
else FSatesCount := length(FSates)-1;
if FSatesCount = 1 then Clean();
FChangeDeal := false;
end
function ParserTokenLines(s,b,e,cst,tokens);
begin
label L_XCDG;
if cst=0 then
begin
idx := b;
ttk := "";
while idx<=e do
begin
vi := s[idx];
case vi of
" ","\t":
begin
SetTToken(tokens,ttk,idx-1);
end
"(",")":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,")");
end
"{","}":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,"}");
end
"[","]":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,"]");
end
"<",">":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,">");
end
":",",":
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,vi);
end
'"':
begin
SetTToken(tokens,ttk,idx-1);
SetTToken(tokens,vi,idx,'"');
return ParserTokenLines(s,idx+1,e,'"',tokens);
end else
begin
ttk+=vi;
end
end ;
idx++;
end
SetTToken(tokens,ttk,idx-1);
end else //查找
begin
if b>e then return cst;
r := FindRightChar(cst,s,b,e,"\\");
if r=0 then //没找到
begin
Setttoken(tokens,s[b:],e,cst);
return cst;
end else //找到
if r<=e then
begin
if b<r then
begin
Setttoken(tokens,s[b:r-1],r-1,cst);
end
Setttoken(tokens,s[r:r],r,cst);
if r<e then
begin
//消除递归
b := r+1;
cst := 0;
goto L_XCDG;
//return ParserTokenLines(s,r+1,e,0,tokens); //递归方式
end
end
end
end
function InsureTokenParserd(LastLine);override; //解析
begin
ls := Lines;
FChangeDeal := true;
if fdolastline>=LastLine then return ;
fdolastline := LastLine;
for i:= FSatesCount-1 to LastLine do
begin
if i<0 then continue;
s := ls.GetStringByIndex(i);
cst := FSates[i];
tks := array();
FCBBState := FBBStates[i].Clone;
FCBEState := FBEStates[i].Clone;
FCMBState := FMBStates[i].Clone;
fcbgesate := FbgedStates[i].Clone;
fcjkhState := FjkhStates[i].Clone;
FSates[i+1] := ParserTokenLines(s,1,length(s),cst,tks);
FBBStates[i+1] :=FCBBState;//.Clone;
FBEStates[i+1] := FCBEState;//.Clone;
FMBStates[i+1] := FCMBState;
FbgedStates[i+1] := fcbgesate;
FjkhStates[i+1] := fcjkhState;
FSatesCount := i+1;
FTokens[i] := tks;
end
end
function GetLineTokens(idx);override;
begin
if idx<FSatesCount then
return FTokens[idx];
end
protected
function SetTToken(tokens,ttk,idx,ext);virtual; //设置token
begin
d := inherited;
if not d then return ;
tk := d.FValue;
if ext in array(")","]","}",">") then d.FFColor := 0xFF0000;
if ext=">" and tk="<" then
begin
fcjkhState.GetLeft();
d.FMATe := fcjkhState.GetSate();
end else
if ext=">" and tk=">" then
begin
fcjkhState.GetRight();
d.FMATe := fcjkhState.GetSate();
end else
if ext="}" and tk="{" then
begin
FCBEState.GetLeft();
d.FMATe := FCBEState.GetSate();
end else
if ext="}" and tk="}" then
begin
FCBEState.GetRight();
d.FMATe := FCBEState.GetSate();
end else
if ext="]" and tk="[" then
begin
FCMBState.GetLeft();
d.FMATe := FCMBState.GetSate();
end else
if ext="]" and tk="]" then
begin
FCMBState.GetRight();
d.FMATe := FCMBState.GetSate();
end else
if ext=")" and tk="(" then
begin
FCBBState.GetLeft();
d.FMATe := FCBBState.GetSate();
end else
if ext=")" and tk=")" then
begin
FCBBState.GetRight();
d.FMATe := FCBBState.GetSate();
end else
if ext='"' then
begin
D.FFColor := 0x8B008B;
end else
if tk in array("true","false","null") then
begin
d.FFColor := 0x0000FF;
end else
if tk ="object" then
begin
d.FFColor := 0x0000FF;
fcbgesate.GetLeft();
d.FMATe := fcbgesate.GetSate();
end else
if tk="end" then
begin
d.FFColor := 0x0000FF;
fcbgesate.GetRight();
d.FMATe := fcbgesate.GetSate();
end else
if tk in array(":",",") then
begin
d.FFColor := 0x0000FF;
end else
if StrIsANumber(tk) then d.FFColor := 0x666666;
return d;
end
private
function StrIsANumber(s);
begin
if not s then return 0;
r := (1=ParseRegExpr("^[+-]?[\\d]+",s,"",result,MPos,Mlen)) ;
return r;
end
function FindRightChar(c,s,b,e,zy); //查找封闭的字符
begin
i := b;
while i<=e do
begin
si := s[i];
if si=zy then
begin
i+=2;
if i>e then return 0;
continue;
end
if s[i]=c then //找到了
begin
return i;
end
i++;
end
return 0; //没找到
for i := b to e do
begin
if s[i]=c then //找到了
begin
return i;
end
end
ExecuteCommand("string",array(('"',"\\")));
ExecuteCommand("keywords",array("object","end"));
ExecuteCommand("rowannotes",array());
ExecuteCommand("blockannotes",array());
ExecuteCommand("syms",array("{","}","[","]","<",">",":",",","="));
ExecuteCommand("pairs",array(("{","}"),("[","]"),("<",">"),("object","end")));
end
private
fdolastline;
FTokens;
FSates;
FSatesCount;
FCBBState;
FCBEState;
FCMBState;
FBBStates;
FBEStates;
FMBStates ;
FbgedStates;
FjkhStates;
fcbgesate;
fcjkhState;
end
implementation
type TBBState =class //括号状态
type TBBState =class(tpairstate) //括号状态
function Create(t);
begin
FType := ifstring(t)?t:"";
FCJ := 0;
FState := 0;
FIndexs := array(0);
FIndex := 0;
FCstate := false;
inherited;
end
function GetLeft();
begin
FCstate := true;
if FState then // 上一个是左括号
begin
FCJ++;
FIndex++;
FIndexs[FIndex]:=0;
end else //上一个非左括号
begin
FIndexs[FIndex]++;
end
FState := true;
end
function GetRight();
begin
FCstate := true;
//if FCJ=0 then return ;
if FState then // 上一个是左括号
begin
//FIndexs[FIndex]++;
end else //上一个非左括号
begin
if FCJ>0 then
begin
FCJ--;
DeleteIndex(FIndexs,FIndex);
FIndex--;
end else
begin
FCstate := false;
end
end
FState := false;
end
function GetSate();
begin
if FCstate then
begin
//return inttostr(FIndexs[FCJ])+FType;
return tostn(FIndexs)+FType;// inttostr(FCJ)+"+"+inttostr(FIndexs[FIndex])+FType; length(FIndexs)+FType ;//
end
end
function Show();
begin
echo "层级:",FCJ," 序号:",FIndexs[FIndex],"状态:", FCstate;
end
function Clone();
begin
r := new TBBState(FType);
r.FCstate := FCstate;
r.FState := FState;
r.FCJ := FCJ;
r.FIndexs := FIndexs;
r.FIndex := FIndex;
return r;
end
protected
FCstate;
FState;
FCJ;
FIndexs;
FIndex;
FType;
end
type TTsfFileParser = class //文件解析
private