mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-17 22:41:42 +02:00
codetools: indentations: made parser more generic
git-svn-id: trunk@20308 -
This commit is contained in:
parent
ed48fb1d69
commit
baa2686a42
@ -139,14 +139,42 @@ type
|
|||||||
procedure Clear;
|
procedure Clear;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
type
|
||||||
|
TBlock = record
|
||||||
|
Typ: TFABBlockType;
|
||||||
|
StartPos: integer;
|
||||||
|
InnerIdent: integer;
|
||||||
|
end;
|
||||||
|
PBlock = ^TBlock;
|
||||||
|
|
||||||
|
{ TFABBlockStack }
|
||||||
|
|
||||||
|
TFABBlockStack = class
|
||||||
|
public
|
||||||
|
Stack: PBlock;
|
||||||
|
Capacity: integer;
|
||||||
|
Top: integer;
|
||||||
|
TopType: TFABBlockType;
|
||||||
|
constructor Create;
|
||||||
|
destructor Destroy; override;
|
||||||
|
procedure BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
||||||
|
procedure EndBlock;
|
||||||
|
function TopMostIndexOf(Typ: TFABBlockType): integer;
|
||||||
|
function EndTopMostBlock(Typ: TFABBlockType): boolean;
|
||||||
|
{$IFDEF ShowCodeBeautifier}
|
||||||
|
Src: string;
|
||||||
|
function PosToStr(p: integer): string;
|
||||||
|
{$ENDIF}
|
||||||
|
end;
|
||||||
|
|
||||||
{ TFullyAutomaticBeautifier }
|
{ TFullyAutomaticBeautifier }
|
||||||
|
|
||||||
TFullyAutomaticBeautifier = class
|
TFullyAutomaticBeautifier = class
|
||||||
private
|
private
|
||||||
FOnGetExamples: TOnGetFABExamples;
|
FOnGetExamples: TOnGetFABExamples;
|
||||||
procedure ParseSource(const Src: string; SrcLen: integer;
|
procedure ParseSource(const Src: string; StartPos, EndPos: integer;
|
||||||
NestedComments: boolean; Policies: TFABPolicies);
|
NestedComments: boolean;
|
||||||
procedure FindPolicies(Types: TFABBlockTypes; Policies: TFABPolicies);
|
Stack: TFABBlockStack; Policies: TFABPolicies);
|
||||||
public
|
public
|
||||||
DefaultTabWidth: integer;
|
DefaultTabWidth: integer;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
@ -207,42 +235,14 @@ const
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
type
|
{ TFABBlockStack }
|
||||||
TBlock = record
|
|
||||||
Typ: TFABBlockType;
|
|
||||||
StartPos: integer;
|
|
||||||
InnerIdent: integer;
|
|
||||||
end;
|
|
||||||
PBlock = ^TBlock;
|
|
||||||
|
|
||||||
{ TBlockStack }
|
constructor TFABBlockStack.Create;
|
||||||
|
|
||||||
TBlockStack = class
|
|
||||||
public
|
|
||||||
Stack: PBlock;
|
|
||||||
Capacity: integer;
|
|
||||||
Top: integer;
|
|
||||||
TopType: TFABBlockType;
|
|
||||||
constructor Create;
|
|
||||||
destructor Destroy; override;
|
|
||||||
procedure BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
|
||||||
procedure EndBlock;
|
|
||||||
function TopMostIndexOf(Typ: TFABBlockType): integer;
|
|
||||||
function EndTopMostBlock(Typ: TFABBlockType): boolean;
|
|
||||||
{$IFDEF ShowCodeBeautifier}
|
|
||||||
Src: string;
|
|
||||||
function PosToStr(p: integer): string;
|
|
||||||
{$ENDIF}
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TBlockStack }
|
|
||||||
|
|
||||||
constructor TBlockStack.Create;
|
|
||||||
begin
|
begin
|
||||||
Top:=-1;
|
Top:=-1;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TBlockStack.Destroy;
|
destructor TFABBlockStack.Destroy;
|
||||||
begin
|
begin
|
||||||
ReAllocMem(Stack,0);
|
ReAllocMem(Stack,0);
|
||||||
Capacity:=0;
|
Capacity:=0;
|
||||||
@ -250,7 +250,7 @@ begin
|
|||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBlockStack.BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
procedure TFABBlockStack.BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
||||||
var
|
var
|
||||||
Block: PBlock;
|
Block: PBlock;
|
||||||
begin
|
begin
|
||||||
@ -263,7 +263,7 @@ begin
|
|||||||
ReAllocMem(Stack,SizeOf(TBlock)*Capacity);
|
ReAllocMem(Stack,SizeOf(TBlock)*Capacity);
|
||||||
end;
|
end;
|
||||||
{$IFDEF ShowCodeBeautifier}
|
{$IFDEF ShowCodeBeautifier}
|
||||||
DebugLn([GetIndentStr(Top*2),'TBlockStack.BeginBlock ',FABBlockTypeNames[Typ],' ',StartPos,' at ',PosToStr(StartPos)]);
|
DebugLn([GetIndentStr(Top*2),'TFABBlockStack.BeginBlock ',FABBlockTypeNames[Typ],' ',StartPos,' at ',PosToStr(StartPos)]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
Block:=@Stack[Top];
|
Block:=@Stack[Top];
|
||||||
Block^.Typ:=Typ;
|
Block^.Typ:=Typ;
|
||||||
@ -272,10 +272,10 @@ begin
|
|||||||
TopType:=Typ;
|
TopType:=Typ;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TBlockStack.EndBlock;
|
procedure TFABBlockStack.EndBlock;
|
||||||
begin
|
begin
|
||||||
{$IFDEF ShowCodeBeautifier}
|
{$IFDEF ShowCodeBeautifier}
|
||||||
DebugLn([GetIndentStr(Top*2),'TBlockStack.EndBlock ',FABBlockTypeNames[TopType]]);
|
DebugLn([GetIndentStr(Top*2),'TFABBlockStack.EndBlock ',FABBlockTypeNames[TopType]]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
dec(Top);
|
dec(Top);
|
||||||
if Top>=0 then
|
if Top>=0 then
|
||||||
@ -284,13 +284,13 @@ begin
|
|||||||
TopType:=bbtNone;
|
TopType:=bbtNone;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TBlockStack.TopMostIndexOf(Typ: TFABBlockType): integer;
|
function TFABBlockStack.TopMostIndexOf(Typ: TFABBlockType): integer;
|
||||||
begin
|
begin
|
||||||
Result:=Top;
|
Result:=Top;
|
||||||
while (Result>=0) and (Stack[Result].Typ<>Typ) do dec(Result);
|
while (Result>=0) and (Stack[Result].Typ<>Typ) do dec(Result);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TBlockStack.EndTopMostBlock(Typ: TFABBlockType): boolean;
|
function TFABBlockStack.EndTopMostBlock(Typ: TFABBlockType): boolean;
|
||||||
// check if there is this type on the stack and if yes, end it
|
// check if there is this type on the stack and if yes, end it
|
||||||
var
|
var
|
||||||
i: LongInt;
|
i: LongInt;
|
||||||
@ -302,7 +302,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
{$IFDEF ShowCodeBeautifier}
|
{$IFDEF ShowCodeBeautifier}
|
||||||
function TBlockStack.PosToStr(p: integer): string;
|
function TFABBlockStack.PosToStr(p: integer): string;
|
||||||
var
|
var
|
||||||
X: integer;
|
X: integer;
|
||||||
Y: LongInt;
|
Y: LongInt;
|
||||||
@ -317,9 +317,9 @@ end;
|
|||||||
{ TFullyAutomaticBeautifier }
|
{ TFullyAutomaticBeautifier }
|
||||||
|
|
||||||
procedure TFullyAutomaticBeautifier.ParseSource(const Src: string;
|
procedure TFullyAutomaticBeautifier.ParseSource(const Src: string;
|
||||||
SrcLen: integer; NestedComments: boolean; Policies: TFABPolicies);
|
StartPos, EndPos: integer; NestedComments: boolean; Stack: TFABBlockStack;
|
||||||
|
Policies: TFABPolicies);
|
||||||
var
|
var
|
||||||
Stack: TBlockStack;
|
|
||||||
p: Integer;
|
p: Integer;
|
||||||
AtomStart: integer;
|
AtomStart: integer;
|
||||||
|
|
||||||
@ -413,13 +413,11 @@ var
|
|||||||
Block: PBlock;
|
Block: PBlock;
|
||||||
Indent: Integer;
|
Indent: Integer;
|
||||||
begin
|
begin
|
||||||
Stack:=TBlockStack.Create;
|
p:=StartPos;
|
||||||
try
|
|
||||||
p:=1;
|
|
||||||
repeat
|
repeat
|
||||||
ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments);
|
ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments);
|
||||||
DebugLn(['TFullyAutomaticBeautifier.ParseSource ',copy(Src,AtomStart,p-AtomStart)]);
|
DebugLn(['TFullyAutomaticBeautifier.ParseSource ',copy(Src,AtomStart,p-AtomStart)]);
|
||||||
if p>SrcLen then break;
|
if p>=EndPos then break;
|
||||||
|
|
||||||
if (Stack.Top>=0) then begin
|
if (Stack.Top>=0) then begin
|
||||||
Block:=@Stack.Stack[Stack.Top];
|
Block:=@Stack.Stack[Stack.Top];
|
||||||
@ -712,15 +710,6 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
until false;
|
until false;
|
||||||
finally
|
|
||||||
Stack.Free;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TFullyAutomaticBeautifier.FindPolicies(Types: TFABBlockTypes;
|
|
||||||
Policies: TFABPolicies);
|
|
||||||
begin
|
|
||||||
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TFullyAutomaticBeautifier.Create;
|
constructor TFullyAutomaticBeautifier.Create;
|
||||||
@ -744,16 +733,19 @@ function TFullyAutomaticBeautifier.GetIndent(const Source: string;
|
|||||||
out Indent: TFABIndentationPolicy): boolean;
|
out Indent: TFABIndentationPolicy): boolean;
|
||||||
var
|
var
|
||||||
Policies: TFABPolicies;
|
Policies: TFABPolicies;
|
||||||
|
Stack: TFABBlockStack;
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
FillByte(Indent,SizeOf(Indent),0);
|
FillByte(Indent,SizeOf(Indent),0);
|
||||||
|
|
||||||
Policies:=TFABPolicies.Create;
|
Policies:=TFABPolicies.Create;
|
||||||
|
Stack:=TFABBlockStack.Create;
|
||||||
try
|
try
|
||||||
// parse source
|
// parse source
|
||||||
ParseSource(Source,length(Source),NewNestedComments,Policies);
|
ParseSource(Source,1,length(Source)+1,NewNestedComments,Stack,Policies);
|
||||||
|
|
||||||
finally
|
finally
|
||||||
|
Stack.Free;
|
||||||
Policies.Free;
|
Policies.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user