mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-12 14:29:36 +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;
|
||||
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 = class
|
||||
private
|
||||
FOnGetExamples: TOnGetFABExamples;
|
||||
procedure ParseSource(const Src: string; SrcLen: integer;
|
||||
NestedComments: boolean; Policies: TFABPolicies);
|
||||
procedure FindPolicies(Types: TFABBlockTypes; Policies: TFABPolicies);
|
||||
procedure ParseSource(const Src: string; StartPos, EndPos: integer;
|
||||
NestedComments: boolean;
|
||||
Stack: TFABBlockStack; Policies: TFABPolicies);
|
||||
public
|
||||
DefaultTabWidth: integer;
|
||||
constructor Create;
|
||||
@ -207,42 +235,14 @@ const
|
||||
|
||||
implementation
|
||||
|
||||
type
|
||||
TBlock = record
|
||||
Typ: TFABBlockType;
|
||||
StartPos: integer;
|
||||
InnerIdent: integer;
|
||||
end;
|
||||
PBlock = ^TBlock;
|
||||
{ TFABBlockStack }
|
||||
|
||||
{ TBlockStack }
|
||||
|
||||
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;
|
||||
constructor TFABBlockStack.Create;
|
||||
begin
|
||||
Top:=-1;
|
||||
end;
|
||||
|
||||
destructor TBlockStack.Destroy;
|
||||
destructor TFABBlockStack.Destroy;
|
||||
begin
|
||||
ReAllocMem(Stack,0);
|
||||
Capacity:=0;
|
||||
@ -250,7 +250,7 @@ begin
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TBlockStack.BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
||||
procedure TFABBlockStack.BeginBlock(Typ: TFABBlockType; StartPos: integer);
|
||||
var
|
||||
Block: PBlock;
|
||||
begin
|
||||
@ -263,7 +263,7 @@ begin
|
||||
ReAllocMem(Stack,SizeOf(TBlock)*Capacity);
|
||||
end;
|
||||
{$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}
|
||||
Block:=@Stack[Top];
|
||||
Block^.Typ:=Typ;
|
||||
@ -272,10 +272,10 @@ begin
|
||||
TopType:=Typ;
|
||||
end;
|
||||
|
||||
procedure TBlockStack.EndBlock;
|
||||
procedure TFABBlockStack.EndBlock;
|
||||
begin
|
||||
{$IFDEF ShowCodeBeautifier}
|
||||
DebugLn([GetIndentStr(Top*2),'TBlockStack.EndBlock ',FABBlockTypeNames[TopType]]);
|
||||
DebugLn([GetIndentStr(Top*2),'TFABBlockStack.EndBlock ',FABBlockTypeNames[TopType]]);
|
||||
{$ENDIF}
|
||||
dec(Top);
|
||||
if Top>=0 then
|
||||
@ -284,13 +284,13 @@ begin
|
||||
TopType:=bbtNone;
|
||||
end;
|
||||
|
||||
function TBlockStack.TopMostIndexOf(Typ: TFABBlockType): integer;
|
||||
function TFABBlockStack.TopMostIndexOf(Typ: TFABBlockType): integer;
|
||||
begin
|
||||
Result:=Top;
|
||||
while (Result>=0) and (Stack[Result].Typ<>Typ) do dec(Result);
|
||||
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
|
||||
var
|
||||
i: LongInt;
|
||||
@ -302,7 +302,7 @@ begin
|
||||
end;
|
||||
|
||||
{$IFDEF ShowCodeBeautifier}
|
||||
function TBlockStack.PosToStr(p: integer): string;
|
||||
function TFABBlockStack.PosToStr(p: integer): string;
|
||||
var
|
||||
X: integer;
|
||||
Y: LongInt;
|
||||
@ -317,9 +317,9 @@ end;
|
||||
{ TFullyAutomaticBeautifier }
|
||||
|
||||
procedure TFullyAutomaticBeautifier.ParseSource(const Src: string;
|
||||
SrcLen: integer; NestedComments: boolean; Policies: TFABPolicies);
|
||||
StartPos, EndPos: integer; NestedComments: boolean; Stack: TFABBlockStack;
|
||||
Policies: TFABPolicies);
|
||||
var
|
||||
Stack: TBlockStack;
|
||||
p: Integer;
|
||||
AtomStart: integer;
|
||||
|
||||
@ -413,13 +413,11 @@ var
|
||||
Block: PBlock;
|
||||
Indent: Integer;
|
||||
begin
|
||||
Stack:=TBlockStack.Create;
|
||||
try
|
||||
p:=1;
|
||||
p:=StartPos;
|
||||
repeat
|
||||
ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments);
|
||||
DebugLn(['TFullyAutomaticBeautifier.ParseSource ',copy(Src,AtomStart,p-AtomStart)]);
|
||||
if p>SrcLen then break;
|
||||
if p>=EndPos then break;
|
||||
|
||||
if (Stack.Top>=0) then begin
|
||||
Block:=@Stack.Stack[Stack.Top];
|
||||
@ -712,15 +710,6 @@ begin
|
||||
end;
|
||||
end;
|
||||
until false;
|
||||
finally
|
||||
Stack.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TFullyAutomaticBeautifier.FindPolicies(Types: TFABBlockTypes;
|
||||
Policies: TFABPolicies);
|
||||
begin
|
||||
|
||||
end;
|
||||
|
||||
constructor TFullyAutomaticBeautifier.Create;
|
||||
@ -744,16 +733,19 @@ function TFullyAutomaticBeautifier.GetIndent(const Source: string;
|
||||
out Indent: TFABIndentationPolicy): boolean;
|
||||
var
|
||||
Policies: TFABPolicies;
|
||||
Stack: TFABBlockStack;
|
||||
begin
|
||||
Result:=false;
|
||||
FillByte(Indent,SizeOf(Indent),0);
|
||||
|
||||
Policies:=TFABPolicies.Create;
|
||||
Stack:=TFABBlockStack.Create;
|
||||
try
|
||||
// parse source
|
||||
ParseSource(Source,length(Source),NewNestedComments,Policies);
|
||||
ParseSource(Source,1,length(Source)+1,NewNestedComments,Stack,Policies);
|
||||
|
||||
finally
|
||||
Stack.Free;
|
||||
Policies.Free;
|
||||
end;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user