codetools: started h2pastool

git-svn-id: trunk@14407 -
This commit is contained in:
mattias 2008-03-04 13:23:11 +00:00
parent 14ae0b57cc
commit 58e4529589
6 changed files with 104 additions and 21 deletions

1
.gitattributes vendored
View File

@ -142,6 +142,7 @@ components/codetools/extractproctool.pas svneol=native#text/pascal
components/codetools/fileprocs.pas svneol=native#text/pascal
components/codetools/finddeclarationcache.pas svneol=native#text/pascal
components/codetools/finddeclarationtool.pas svneol=native#text/pascal
components/codetools/h2pastool.pas svneol=native#text/plain
components/codetools/identcompletiontool.pas svneol=native#text/pascal
components/codetools/keywordfunclists.pas svneol=native#text/pascal
components/codetools/languages/README.txt svneol=native#text/plain

View File

@ -22,7 +22,7 @@ uses
CodeCompletionTool, LinkScanner, FindDeclarationCache, BasicCodeTools,
NonPascalCodeTools, CodeTree, CodeAtom, SourceChanger, CodeToolMemManager,
CodeCache, KeywordFuncLists, SourceLog, ExprEval, DefineTemplates, FileProcs,
CodeToolsStrConsts, DirectoryCacher, CCodeParserTool,
CodeToolsStrConsts, DirectoryCacher, CCodeParserTool, H2PasTool,
MultiKeyWordListTool, ResourceCodeTool, CodeToolsStructs, CacheCodeTools,
// fast xml units, changes not merged in current fpc
Laz_DOM, Laz_XMLCfg, Laz_XMLRead, Laz_XMLWrite, Laz_XMLStreaming;

View File

@ -104,7 +104,7 @@ type
FResourceTool: TResourceCodeTool;
FSetPropertyVariablename: string;
FSourceExtensions: string; // default is '.pp;.pas;.lpr;.dpr;.dpk'
FSourceTools: TAVLTree; // tree of TCustomCodeTool sorted TCustomCodeTool(Data).Scanner.MainCode
FPascalTools: TAVLTree; // tree of TCustomCodeTool sorted TCustomCodeTool(Data).Scanner.MainCode
FTabWidth: integer;
FVisibleEditorLines: integer;
FWriteExceptions: boolean;
@ -774,7 +774,7 @@ begin
FSourceExtensions:='.pp;.pas;.p;.lpr;.lpk;.dpr;.dpk';
FVisibleEditorLines:=20;
FWriteExceptions:=true;
FSourceTools:=TAVLTree.Create(@CompareCodeToolMainSources);
FPascalTools:=TAVLTree.Create(@CompareCodeToolMainSources);
FDirectivesTools:=TAVLTree.Create(@CompareDirectivesTreeSources);
IdentifierList:=TIdentifierList.Create;
IdentifierHistory:=TIdentifierHistoryList.Create;
@ -793,8 +793,8 @@ begin
FreeAndNil(Positions);
FreeAndNil(IdentifierHistory);
FreeAndNil(IdentifierList);
FSourceTools.FreeAndClear;
FreeAndNil(FSourceTools);
FPascalTools.FreeAndClear;
FreeAndNil(FPascalTools);
FDirectivesTools.FreeAndClear;
FreeAndNil(FDirectivesTools);
FreeAndNil(FResourceTool);
@ -4442,7 +4442,7 @@ var
ANode: TAVLTreeNode;
CurSrc, SearchedSrc: Pointer;
begin
ANode:=FSourceTools.Root;
ANode:=FPascalTools.Root;
SearchedSrc:=Pointer(Code);
while (ANode<>nil) do begin
CurSrc:=Pointer(TCustomCodeTool(ANode.Data).Scanner.MainCode);
@ -4493,7 +4493,7 @@ begin
end;
Result:=TCodeTool.Create;
Result.Scanner:=Code.Scanner;
FSourceTools.Add(Result);
FPascalTools.Add(Result);
TCodeTool(Result).OnGetCodeToolForBuffer:=@OnGetCodeToolForBuffer;
TCodeTool(Result).OnGetDirectoryCache:=@OnGetDirectoryCache;
TCodeTool(Result).OnFindUsedUnit:=@DoOnFindUsedUnit;
@ -4671,14 +4671,14 @@ begin
RootCodeTreeNode:=ANode.GetRoot;
// search in codetools
AToolNode:=FSourceTools.FindLowest;
AToolNode:=FPascalTools.FindLowest;
while (AToolNode<>nil) do begin
CurTool:=TCustomCodeTool(AToolNode.Data);
if (CurTool.Tree<>nil) and (CurTool.Tree.Root=RootCodeTreeNode) then begin
Result:=CurTool;
exit;
end;
AToolNode:=FSourceTools.FindSuccessor(AToolNode);
AToolNode:=FPascalTools.FindSuccessor(AToolNode);
end;
// search in directivestools
@ -4757,7 +4757,7 @@ begin
if Result<>0 then begin
dec(Result,60000); exit;
end;
Result:=FSourceTools.ConsistencyCheck;
Result:=FPascalTools.ConsistencyCheck;
if Result<>0 then begin
dec(Result,70000); exit;
end;

View File

@ -26,10 +26,10 @@
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="LCL"/>
<PackageName Value="CodeTools"/>
</Item1>
<Item2>
<PackageName Value="CodeTools"/>
<PackageName Value="LCL"/>
</Item2>
</RequiredPackages>
<Units Count="2">

View File

@ -32,15 +32,18 @@ program H2PasTest;
uses
Classes, SysUtils, CodeCache, CodeToolManager, FileProcs,
CodeAtom, CodeTree, NonPascalCodeTools, CCodeParserTool;
CodeAtom, CodeTree, NonPascalCodeTools, CCodeParserTool,
H2PasTool;
const
ConfigFilename = 'codetools.config';
var
Filename: String;
Code: TCodeBuffer;
Tool: TCCodeParserTool;
CCode, PasCode: TCodeBuffer;
Tool: TH2PasTool;
Caret: TCodeXYPosition;
OutputFilename: String;
CCodeTool: TCCodeParserTool;
begin
try
CodeToolBoss.SimpleInit(ConfigFilename);
@ -49,18 +52,23 @@ begin
Filename:=ParamStr(1);
// Step 1: load the file
Code:=CodeToolBoss.LoadFile(Filename,false,false);
if Code=nil then
CCode:=CodeToolBoss.LoadFile(Filename,false,false);
if CCode=nil then
raise Exception.Create('loading failed '+Filename);
// Step 2: create the output file
OutputFilename:='h2pasoutput.pas';
PasCode:=CodeToolBoss.CreateFile(OutputFilename);
if PasCode=nil then
raise Exception.Create('creating failed '+OutputFilename);
Tool:=TCCodeParserTool.Create;
Tool.Parse(Code);
Tool:=TH2PasTool.Create;
Tool.Convert(CCode,PasCode);
Tool.WriteDebugReport;
Tool.Free;
except
on E: ECCodeParserException do begin
Tool:=ECCodeParserException(E).Sender;
Tool.CleanPosToCaret(Tool.LastErrorReportPos,Caret);
CCodeTool:=ECCodeParserException(E).Sender;
CCodeTool.CleanPosToCaret(CCodeTool.LastErrorReportPos,Caret);
writeln(Caret.Code.Filename+'('+IntToStr(Caret.Y)+','+IntToStr(Caret.X)+')'+' Error: '+E.Message);
end;
on E: Exception do begin

View File

@ -0,0 +1,74 @@
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
A tool to help converting C header files to pascal bindings.
}
unit H2PasTool;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileProcs,CCodeParserTool, NonPascalCodeTools,
CodeCache;
type
{ TH2PasTool }
TH2PasTool = class
public
CTool: TCCodeParserTool;
function Convert(CCode, PascalCode: TCodeBuffer): boolean;
procedure WriteDebugReport;
end;
implementation
{ TH2PasTool }
function TH2PasTool.Convert(CCode, PascalCode: TCodeBuffer): boolean;
begin
Result:=false;
CTool:=TCCodeParserTool.Create;
try
CTool.Parse(CCode);
CTool.WriteDebugReport;
finally
CTool.Free;
end;
Result:=true;
end;
procedure TH2PasTool.WriteDebugReport;
begin
DebugLn(['TH2PasTool.WriteDebugReport ']);
if CTool<>nil then
CTool.WriteDebugReport;
end;
end.