mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-15 20:19:24 +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,314 +413,303 @@ var
|
|||||||
Block: PBlock;
|
Block: PBlock;
|
||||||
Indent: Integer;
|
Indent: Integer;
|
||||||
begin
|
begin
|
||||||
Stack:=TBlockStack.Create;
|
p:=StartPos;
|
||||||
try
|
repeat
|
||||||
p:=1;
|
ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments);
|
||||||
repeat
|
DebugLn(['TFullyAutomaticBeautifier.ParseSource ',copy(Src,AtomStart,p-AtomStart)]);
|
||||||
ReadRawNextPascalAtom(Src,p,AtomStart,NestedComments);
|
if p>=EndPos then break;
|
||||||
DebugLn(['TFullyAutomaticBeautifier.ParseSource ',copy(Src,AtomStart,p-AtomStart)]);
|
|
||||||
if p>SrcLen then break;
|
|
||||||
|
|
||||||
if (Stack.Top>=0) then begin
|
if (Stack.Top>=0) then begin
|
||||||
Block:=@Stack.Stack[Stack.Top];
|
Block:=@Stack.Stack[Stack.Top];
|
||||||
if (Policies<>nil)
|
if (Policies<>nil)
|
||||||
and (not Policies.Indentations[Block^.Typ].IndentValid) then begin
|
and (not Policies.Indentations[Block^.Typ].IndentValid) then begin
|
||||||
// set block InnerIdent
|
// set block InnerIdent
|
||||||
if (Block^.InnerIdent<0)
|
if (Block^.InnerIdent<0)
|
||||||
and (not PositionsInSameLine(Src,Block^.StartPos,AtomStart)) then begin
|
and (not PositionsInSameLine(Src,Block^.StartPos,AtomStart)) then begin
|
||||||
Block^.InnerIdent:=GetLineIndentWithTabs(Src,AtomStart,DefaultTabWidth);
|
Block^.InnerIdent:=GetLineIndentWithTabs(Src,AtomStart,DefaultTabWidth);
|
||||||
if Block^.Typ in [bbtIfThen,bbtIfElse] then
|
if Block^.Typ in [bbtIfThen,bbtIfElse] then
|
||||||
Indent:=Block^.InnerIdent
|
Indent:=Block^.InnerIdent
|
||||||
-GetLineIndentWithTabs(Src,Stack.Stack[Stack.Top-1].StartPos,
|
-GetLineIndentWithTabs(Src,Stack.Stack[Stack.Top-1].StartPos,
|
||||||
DefaultTabWidth)
|
DefaultTabWidth)
|
||||||
else
|
else
|
||||||
Indent:=Block^.InnerIdent
|
Indent:=Block^.InnerIdent
|
||||||
-GetLineIndentWithTabs(Src,Block^.StartPos,DefaultTabWidth);
|
-GetLineIndentWithTabs(Src,Block^.StartPos,DefaultTabWidth);
|
||||||
Policies.Indentations[Block^.Typ].Indent:=Indent;
|
Policies.Indentations[Block^.Typ].Indent:=Indent;
|
||||||
Policies.Indentations[Block^.Typ].IndentValid:=true;
|
Policies.Indentations[Block^.Typ].IndentValid:=true;
|
||||||
{$IFDEF ShowCodeBeautifierParser}
|
{$IFDEF ShowCodeBeautifierParser}
|
||||||
DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' Indent=',Policies.Indentations[Block^.Typ].Indent]);
|
DebugLn([GetIndentStr(Stack.Top*2),'Indentation learned: ',FABBlockTypeNames[Block^.Typ],' Indent=',Policies.Indentations[Block^.Typ].Indent]);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
r:=@Src[AtomStart];
|
||||||
|
case UpChars[r^] of
|
||||||
|
'B':
|
||||||
|
if CompareIdentifiers('BEGIN',r)=0 then begin
|
||||||
|
while Stack.TopType in (bbtAllIdentifierSections+bbtAllCodeSections) do
|
||||||
|
EndBlock;
|
||||||
|
case Stack.TopType of
|
||||||
|
bbtNone:
|
||||||
|
BeginBlock(bbtMainBegin);
|
||||||
|
bbtProcedure,bbtFunction:
|
||||||
|
BeginBlock(bbtProcedureBegin);
|
||||||
|
bbtMainBegin:
|
||||||
|
BeginBlock(bbtCommentaryBegin);
|
||||||
|
bbtCaseElse,bbtCaseColon:
|
||||||
|
BeginBlock(bbtCaseBegin);
|
||||||
|
bbtIfThen,bbtIfElse:
|
||||||
|
BeginBlock(bbtIfBegin);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'C':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'A': // CA
|
||||||
|
if CompareIdentifiers('CASE',r)=0 then begin
|
||||||
|
if Stack.TopType in bbtAllStatements then
|
||||||
|
BeginBlock(bbtCase);
|
||||||
|
end;
|
||||||
|
'L': // CL
|
||||||
|
if CompareIdentifiers('CLASS',r)=0 then begin
|
||||||
|
if Stack.TopType=bbtTypeSection then
|
||||||
|
BeginBlock(bbtClass);
|
||||||
|
end;
|
||||||
|
'O': // CO
|
||||||
|
if CompareIdentifiers('CONST',r)=0 then
|
||||||
|
StartIdentifierSection(bbtConstSection);
|
||||||
|
end;
|
||||||
|
'E':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'L': // EL
|
||||||
|
if CompareIdentifiers('ELSE',r)=0 then begin
|
||||||
|
case Stack.TopType of
|
||||||
|
bbtCaseOf,bbtCaseColon:
|
||||||
|
begin
|
||||||
|
EndBlock;
|
||||||
|
BeginBlock(bbtCaseElse);
|
||||||
|
end;
|
||||||
|
bbtIfThen:
|
||||||
|
begin
|
||||||
|
EndBlock;
|
||||||
|
BeginBlock(bbtIfElse);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'N': // EN
|
||||||
|
if CompareIdentifiers('END',r)=0 then begin
|
||||||
|
// if statements can be closed by end without semicolon
|
||||||
|
while Stack.TopType in [bbtIf,bbtIfThen,bbtIfElse] do EndBlock;
|
||||||
|
if Stack.TopType=bbtClassSection then
|
||||||
|
EndBlock;
|
||||||
|
|
||||||
|
case Stack.TopType of
|
||||||
|
bbtMainBegin,bbtCommentaryBegin,
|
||||||
|
bbtRecord,bbtClass,bbtClassInterface,bbtTry,bbtFinally,bbtExcept,
|
||||||
|
bbtCase,bbtCaseBegin,bbtIfBegin:
|
||||||
|
EndBlock;
|
||||||
|
bbtCaseOf,bbtCaseElse,bbtCaseColon:
|
||||||
|
begin
|
||||||
|
EndBlock;
|
||||||
|
if Stack.TopType=bbtCase then
|
||||||
|
EndBlock;
|
||||||
|
end;
|
||||||
|
bbtProcedureBegin:
|
||||||
|
begin
|
||||||
|
EndBlock;
|
||||||
|
if Stack.TopType in [bbtProcedure,bbtFunction] then
|
||||||
|
EndBlock;
|
||||||
|
end;
|
||||||
|
bbtInterface,bbtImplementation,bbtInitialization,bbtFinalization:
|
||||||
|
EndBlock;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'X': // EX
|
||||||
|
if CompareIdentifiers('EXCEPT',r)=0 then begin
|
||||||
|
if Stack.TopType=bbtTry then begin
|
||||||
|
EndBlock;
|
||||||
|
BeginBlock(bbtExcept);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
'F':
|
||||||
r:=@Src[AtomStart];
|
case UpChars[r[1]] of
|
||||||
case UpChars[r^] of
|
'I': // FI
|
||||||
'B':
|
if CompareIdentifiers('FINALIZATION',r)=0 then begin
|
||||||
if CompareIdentifiers('BEGIN',r)=0 then begin
|
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
||||||
while Stack.TopType in (bbtAllIdentifierSections+bbtAllCodeSections) do
|
do
|
||||||
EndBlock;
|
EndBlock;
|
||||||
case Stack.TopType of
|
if Stack.TopType=bbtNone then
|
||||||
bbtNone:
|
BeginBlock(bbtInitialization);
|
||||||
BeginBlock(bbtMainBegin);
|
end else if CompareIdentifiers('FINALLY',r)=0 then begin
|
||||||
bbtProcedure,bbtFunction:
|
if Stack.TopType=bbtTry then begin
|
||||||
BeginBlock(bbtProcedureBegin);
|
EndBlock;
|
||||||
bbtMainBegin:
|
BeginBlock(bbtFinally);
|
||||||
BeginBlock(bbtCommentaryBegin);
|
|
||||||
bbtCaseElse,bbtCaseColon:
|
|
||||||
BeginBlock(bbtCaseBegin);
|
|
||||||
bbtIfThen,bbtIfElse:
|
|
||||||
BeginBlock(bbtIfBegin);
|
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
'C':
|
'O': // FO
|
||||||
case UpChars[r[1]] of
|
if CompareIdentifiers('FORWARD',r)=0 then begin
|
||||||
'A': // CA
|
if Stack.TopType in [bbtProcedure,bbtFunction] then begin
|
||||||
if CompareIdentifiers('CASE',r)=0 then begin
|
EndBlock;
|
||||||
if Stack.TopType in bbtAllStatements then
|
|
||||||
BeginBlock(bbtCase);
|
|
||||||
end;
|
|
||||||
'L': // CL
|
|
||||||
if CompareIdentifiers('CLASS',r)=0 then begin
|
|
||||||
if Stack.TopType=bbtTypeSection then
|
|
||||||
BeginBlock(bbtClass);
|
|
||||||
end;
|
|
||||||
'O': // CO
|
|
||||||
if CompareIdentifiers('CONST',r)=0 then
|
|
||||||
StartIdentifierSection(bbtConstSection);
|
|
||||||
end;
|
|
||||||
'E':
|
|
||||||
case UpChars[r[1]] of
|
|
||||||
'L': // EL
|
|
||||||
if CompareIdentifiers('ELSE',r)=0 then begin
|
|
||||||
case Stack.TopType of
|
|
||||||
bbtCaseOf,bbtCaseColon:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
|
||||||
BeginBlock(bbtCaseElse);
|
|
||||||
end;
|
|
||||||
bbtIfThen:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
|
||||||
BeginBlock(bbtIfElse);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'N': // EN
|
|
||||||
if CompareIdentifiers('END',r)=0 then begin
|
|
||||||
// if statements can be closed by end without semicolon
|
|
||||||
while Stack.TopType in [bbtIf,bbtIfThen,bbtIfElse] do EndBlock;
|
|
||||||
if Stack.TopType=bbtClassSection then
|
|
||||||
EndBlock;
|
|
||||||
|
|
||||||
case Stack.TopType of
|
|
||||||
bbtMainBegin,bbtCommentaryBegin,
|
|
||||||
bbtRecord,bbtClass,bbtClassInterface,bbtTry,bbtFinally,bbtExcept,
|
|
||||||
bbtCase,bbtCaseBegin,bbtIfBegin:
|
|
||||||
EndBlock;
|
|
||||||
bbtCaseOf,bbtCaseElse,bbtCaseColon:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
|
||||||
if Stack.TopType=bbtCase then
|
|
||||||
EndBlock;
|
|
||||||
end;
|
|
||||||
bbtProcedureBegin:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
|
||||||
if Stack.TopType in [bbtProcedure,bbtFunction] then
|
|
||||||
EndBlock;
|
|
||||||
end;
|
|
||||||
bbtInterface,bbtImplementation,bbtInitialization,bbtFinalization:
|
|
||||||
EndBlock;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'X': // EX
|
|
||||||
if CompareIdentifiers('EXCEPT',r)=0 then begin
|
|
||||||
if Stack.TopType=bbtTry then begin
|
|
||||||
EndBlock;
|
|
||||||
BeginBlock(bbtExcept);
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
'F':
|
'U': // FU
|
||||||
case UpChars[r[1]] of
|
if CompareIdentifiers('FUNCTION',r)=0 then
|
||||||
'I': // FI
|
StartProcedure(bbtFunction);
|
||||||
if CompareIdentifiers('FINALIZATION',r)=0 then begin
|
end;
|
||||||
|
'I':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'F': // IF
|
||||||
|
if p-AtomStart=2 then begin
|
||||||
|
// 'IF'
|
||||||
|
if Stack.TopType in bbtAllStatements then
|
||||||
|
BeginBlock(bbtIf);
|
||||||
|
end;
|
||||||
|
'N': // IN
|
||||||
|
case UpChars[r[2]] of
|
||||||
|
'I': // INI
|
||||||
|
if CompareIdentifiers('INITIALIZATION',r)=0 then begin
|
||||||
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
||||||
do
|
do
|
||||||
EndBlock;
|
EndBlock;
|
||||||
if Stack.TopType=bbtNone then
|
if Stack.TopType=bbtNone then
|
||||||
BeginBlock(bbtInitialization);
|
BeginBlock(bbtInitialization);
|
||||||
end else if CompareIdentifiers('FINALLY',r)=0 then begin
|
|
||||||
if Stack.TopType=bbtTry then begin
|
|
||||||
EndBlock;
|
|
||||||
BeginBlock(bbtFinally);
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
'O': // FO
|
'T': // INT
|
||||||
if CompareIdentifiers('FORWARD',r)=0 then begin
|
if CompareIdentifiers('INTERFACE',r)=0 then begin
|
||||||
if Stack.TopType in [bbtProcedure,bbtFunction] then begin
|
case Stack.TopType of
|
||||||
EndBlock;
|
bbtNone:
|
||||||
|
BeginBlock(bbtInterface);
|
||||||
|
bbtTypeSection:
|
||||||
|
BeginBlock(bbtClassInterface);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
'U': // FU
|
|
||||||
if CompareIdentifiers('FUNCTION',r)=0 then
|
|
||||||
StartProcedure(bbtFunction);
|
|
||||||
end;
|
|
||||||
'I':
|
|
||||||
case UpChars[r[1]] of
|
|
||||||
'F': // IF
|
|
||||||
if p-AtomStart=2 then begin
|
|
||||||
// 'IF'
|
|
||||||
if Stack.TopType in bbtAllStatements then
|
|
||||||
BeginBlock(bbtIf);
|
|
||||||
end;
|
|
||||||
'N': // IN
|
|
||||||
case UpChars[r[2]] of
|
|
||||||
'I': // INI
|
|
||||||
if CompareIdentifiers('INITIALIZATION',r)=0 then begin
|
|
||||||
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
|
||||||
do
|
|
||||||
EndBlock;
|
|
||||||
if Stack.TopType=bbtNone then
|
|
||||||
BeginBlock(bbtInitialization);
|
|
||||||
end;
|
|
||||||
'T': // INT
|
|
||||||
if CompareIdentifiers('INTERFACE',r)=0 then begin
|
|
||||||
case Stack.TopType of
|
|
||||||
bbtNone:
|
|
||||||
BeginBlock(bbtInterface);
|
|
||||||
bbtTypeSection:
|
|
||||||
BeginBlock(bbtClassInterface);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'M': // IM
|
|
||||||
if CompareIdentifiers('IMPLEMENTATION',r)=0 then begin
|
|
||||||
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
|
||||||
do
|
|
||||||
EndBlock;
|
|
||||||
if Stack.TopType=bbtNone then
|
|
||||||
BeginBlock(bbtImplementation);
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
'L':
|
'M': // IM
|
||||||
if CompareIdentifiers('LABEL',r)=0 then
|
if CompareIdentifiers('IMPLEMENTATION',r)=0 then begin
|
||||||
StartIdentifierSection(bbtLabelSection);
|
while Stack.TopType in (bbtAllCodeSections+bbtAllIdentifierSections+bbtAllStatements)
|
||||||
'O':
|
do
|
||||||
if CompareIdentifiers('OF',r)=0 then begin
|
|
||||||
case Stack.TopType of
|
|
||||||
bbtCase:
|
|
||||||
BeginBlock(bbtCaseOf);
|
|
||||||
bbtClass,bbtClassInterface:
|
|
||||||
EndBlock;
|
EndBlock;
|
||||||
end;
|
if Stack.TopType=bbtNone then
|
||||||
|
BeginBlock(bbtImplementation);
|
||||||
end;
|
end;
|
||||||
'P':
|
end;
|
||||||
case UpChars[r[1]] of
|
'L':
|
||||||
'R': // PR
|
if CompareIdentifiers('LABEL',r)=0 then
|
||||||
case UpChars[r[2]] of
|
StartIdentifierSection(bbtLabelSection);
|
||||||
'I': // PRI
|
'O':
|
||||||
if (CompareIdentifiers('PRIVATE',r)=0) then
|
if CompareIdentifiers('OF',r)=0 then begin
|
||||||
StartClassSection;
|
|
||||||
'O': // PRO
|
|
||||||
case UpChars[r[3]] of
|
|
||||||
'T': // PROT
|
|
||||||
if (CompareIdentifiers('PROTECTED',r)=0) then
|
|
||||||
StartClassSection;
|
|
||||||
'C': // PROC
|
|
||||||
if CompareIdentifiers('PROCEDURE',r)=0 then
|
|
||||||
StartProcedure(bbtProcedure);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'U': // PU
|
|
||||||
if (CompareIdentifiers('PUBLIC',r)=0)
|
|
||||||
or (CompareIdentifiers('PUBLISHED',r)=0) then
|
|
||||||
StartClassSection;
|
|
||||||
end;
|
|
||||||
'R':
|
|
||||||
case UpChars[r[1]] of
|
|
||||||
'E': // RE
|
|
||||||
case UpChars[r[2]] of
|
|
||||||
'C': // REC
|
|
||||||
if CompareIdentifiers('RECORD',r)=0 then
|
|
||||||
BeginBlock(bbtRecord);
|
|
||||||
'P': // REP
|
|
||||||
if CompareIdentifiers('REPEAT',r)=0 then
|
|
||||||
if Stack.TopType in bbtAllStatements then
|
|
||||||
BeginBlock(bbtRepeat);
|
|
||||||
'S': // RES
|
|
||||||
if CompareIdentifiers('RESOURCESTRING',r)=0 then
|
|
||||||
StartIdentifierSection(bbtResourceStringSection);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'T':
|
|
||||||
case UpChars[r[1]] of
|
|
||||||
'H': // TH
|
|
||||||
if CompareIdentifiers('THEN',r)=0 then begin
|
|
||||||
if Stack.TopType=bbtIf then
|
|
||||||
BeginBlock(bbtIfThen);
|
|
||||||
end;
|
|
||||||
'R': // TR
|
|
||||||
if CompareIdentifiers('TRY',r)=0 then begin
|
|
||||||
if Stack.TopType in bbtAllStatements then
|
|
||||||
BeginBlock(bbtTry);
|
|
||||||
end;
|
|
||||||
'Y': // TY
|
|
||||||
if CompareIdentifiers('TYPE',r)=0 then begin
|
|
||||||
StartIdentifierSection(bbtTypeSection);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'U':
|
|
||||||
case UpChars[r[1]] of
|
|
||||||
'S': // US
|
|
||||||
if CompareIdentifiers('USES',r)=0 then begin
|
|
||||||
if Stack.TopType in [bbtNone,bbtInterface,bbtImplementation] then
|
|
||||||
BeginBlock(bbtUsesSection);
|
|
||||||
end;
|
|
||||||
'N': // UN
|
|
||||||
if CompareIdentifiers('UNTIL',r)=0 then begin
|
|
||||||
EndTopMostBlock(bbtRepeat);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
'V':
|
|
||||||
if CompareIdentifiers('VAR',r)=0 then begin
|
|
||||||
StartIdentifierSection(bbtVarSection);
|
|
||||||
end;
|
|
||||||
';':
|
|
||||||
case Stack.TopType of
|
case Stack.TopType of
|
||||||
bbtUsesSection:
|
bbtCase:
|
||||||
|
BeginBlock(bbtCaseOf);
|
||||||
|
bbtClass,bbtClassInterface:
|
||||||
EndBlock;
|
EndBlock;
|
||||||
bbtCaseColon:
|
end;
|
||||||
|
end;
|
||||||
|
'P':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'R': // PR
|
||||||
|
case UpChars[r[2]] of
|
||||||
|
'I': // PRI
|
||||||
|
if (CompareIdentifiers('PRIVATE',r)=0) then
|
||||||
|
StartClassSection;
|
||||||
|
'O': // PRO
|
||||||
|
case UpChars[r[3]] of
|
||||||
|
'T': // PROT
|
||||||
|
if (CompareIdentifiers('PROTECTED',r)=0) then
|
||||||
|
StartClassSection;
|
||||||
|
'C': // PROC
|
||||||
|
if CompareIdentifiers('PROCEDURE',r)=0 then
|
||||||
|
StartProcedure(bbtProcedure);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'U': // PU
|
||||||
|
if (CompareIdentifiers('PUBLIC',r)=0)
|
||||||
|
or (CompareIdentifiers('PUBLISHED',r)=0) then
|
||||||
|
StartClassSection;
|
||||||
|
end;
|
||||||
|
'R':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'E': // RE
|
||||||
|
case UpChars[r[2]] of
|
||||||
|
'C': // REC
|
||||||
|
if CompareIdentifiers('RECORD',r)=0 then
|
||||||
|
BeginBlock(bbtRecord);
|
||||||
|
'P': // REP
|
||||||
|
if CompareIdentifiers('REPEAT',r)=0 then
|
||||||
|
if Stack.TopType in bbtAllStatements then
|
||||||
|
BeginBlock(bbtRepeat);
|
||||||
|
'S': // RES
|
||||||
|
if CompareIdentifiers('RESOURCESTRING',r)=0 then
|
||||||
|
StartIdentifierSection(bbtResourceStringSection);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'T':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'H': // TH
|
||||||
|
if CompareIdentifiers('THEN',r)=0 then begin
|
||||||
|
if Stack.TopType=bbtIf then
|
||||||
|
BeginBlock(bbtIfThen);
|
||||||
|
end;
|
||||||
|
'R': // TR
|
||||||
|
if CompareIdentifiers('TRY',r)=0 then begin
|
||||||
|
if Stack.TopType in bbtAllStatements then
|
||||||
|
BeginBlock(bbtTry);
|
||||||
|
end;
|
||||||
|
'Y': // TY
|
||||||
|
if CompareIdentifiers('TYPE',r)=0 then begin
|
||||||
|
StartIdentifierSection(bbtTypeSection);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'U':
|
||||||
|
case UpChars[r[1]] of
|
||||||
|
'S': // US
|
||||||
|
if CompareIdentifiers('USES',r)=0 then begin
|
||||||
|
if Stack.TopType in [bbtNone,bbtInterface,bbtImplementation] then
|
||||||
|
BeginBlock(bbtUsesSection);
|
||||||
|
end;
|
||||||
|
'N': // UN
|
||||||
|
if CompareIdentifiers('UNTIL',r)=0 then begin
|
||||||
|
EndTopMostBlock(bbtRepeat);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
'V':
|
||||||
|
if CompareIdentifiers('VAR',r)=0 then begin
|
||||||
|
StartIdentifierSection(bbtVarSection);
|
||||||
|
end;
|
||||||
|
';':
|
||||||
|
case Stack.TopType of
|
||||||
|
bbtUsesSection:
|
||||||
|
EndBlock;
|
||||||
|
bbtCaseColon:
|
||||||
|
begin
|
||||||
|
EndBlock;
|
||||||
|
BeginBlock(bbtCaseOf);
|
||||||
|
end;
|
||||||
|
bbtIfThen,bbtIfElse:
|
||||||
|
while Stack.TopType in [bbtIf,bbtIfThen,bbtIfElse] do
|
||||||
|
EndBlock;
|
||||||
|
end;
|
||||||
|
':':
|
||||||
|
if p-AtomStart=1 then begin
|
||||||
|
// colon
|
||||||
|
case Stack.TopType of
|
||||||
|
bbtCaseOf:
|
||||||
begin
|
begin
|
||||||
EndBlock;
|
EndBlock;
|
||||||
BeginBlock(bbtCaseOf);
|
BeginBlock(bbtCaseColon);
|
||||||
end;
|
end;
|
||||||
|
bbtIf:
|
||||||
|
EndBlock;
|
||||||
bbtIfThen,bbtIfElse:
|
bbtIfThen,bbtIfElse:
|
||||||
while Stack.TopType in [bbtIf,bbtIfThen,bbtIfElse] do
|
begin
|
||||||
EndBlock;
|
EndBlock;
|
||||||
end;
|
if Stack.TopType=bbtIf then
|
||||||
':':
|
|
||||||
if p-AtomStart=1 then begin
|
|
||||||
// colon
|
|
||||||
case Stack.TopType of
|
|
||||||
bbtCaseOf:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
EndBlock;
|
||||||
BeginBlock(bbtCaseColon);
|
|
||||||
end;
|
|
||||||
bbtIf:
|
|
||||||
EndBlock;
|
|
||||||
bbtIfThen,bbtIfElse:
|
|
||||||
begin
|
|
||||||
EndBlock;
|
|
||||||
if Stack.TopType=bbtIf then
|
|
||||||
EndBlock;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
until false;
|
end;
|
||||||
finally
|
until false;
|
||||||
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