2008-03-25 08:17:24 +00:00
|
|
|
{
|
|
|
|
Project1.pas
|
|
|
|
|
|
|
|
Copyright (C) 2008 Dmitry 'Skalogryz' Boyarintsev
|
|
|
|
|
|
|
|
main parser unit
|
|
|
|
}
|
2008-03-04 23:34:41 +00:00
|
|
|
program Project1;
|
|
|
|
|
2008-03-31 12:39:54 +00:00
|
|
|
{$ifdef fpc}
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
{$else}
|
|
|
|
{$APPTYPE CONSOLE}
|
|
|
|
{$endif}
|
2008-03-04 23:34:41 +00:00
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, ObjCParserUtils, ObjCParserTypes;
|
|
|
|
|
2008-03-27 15:27:00 +00:00
|
|
|
type
|
|
|
|
// this object is used only for precomile directives handling
|
2008-03-28 10:25:55 +00:00
|
|
|
|
|
|
|
{ TPrecompileHandler }
|
|
|
|
|
2008-03-27 15:27:00 +00:00
|
|
|
TPrecompileHandler = class(TObject)
|
|
|
|
public
|
|
|
|
hdr : TObjCHeader;
|
|
|
|
procedure OnPrecompile(Sender: TObject);
|
2008-03-28 10:25:55 +00:00
|
|
|
procedure OnComment(Sender: TObject; const Comment: AnsiString);
|
2008-03-27 15:27:00 +00:00
|
|
|
constructor Create(AHeader: TObjCHeader);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TPrecompileHandler.OnPrecompile(Sender: TObject);
|
|
|
|
var
|
|
|
|
parser : TTextParser;
|
|
|
|
preEntity : TPrecompiler;
|
|
|
|
lst : TEntity;
|
|
|
|
prc : TNotifyEvent;
|
|
|
|
begin
|
|
|
|
parser := Sender as TTextParser;
|
|
|
|
//todo: change for something nicier =)
|
|
|
|
prc := parser.OnPrecompile;
|
|
|
|
parser.OnPrecompile := nil;
|
|
|
|
try
|
|
|
|
if parser.Stack.Count > 0 then
|
|
|
|
lst := TEntity(parser.Stack[parser.Stack.Count-1])
|
|
|
|
else
|
|
|
|
lst := nil;
|
|
|
|
|
|
|
|
preEntity := TPrecompiler.Create(lst);
|
|
|
|
preEntity.Parse(parser);
|
|
|
|
lst.Items.Add(preEntity);
|
|
|
|
finally
|
|
|
|
parser.OnPrecompile := prc;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-03-28 10:25:55 +00:00
|
|
|
procedure TPrecompileHandler.OnComment(Sender: TObject; const Comment: AnsiString);
|
|
|
|
var
|
|
|
|
parser : TTextParser;
|
|
|
|
cmt : TComment;
|
|
|
|
ent : TEntity;
|
|
|
|
begin
|
|
|
|
if length(Comment) < 2 then Exit;
|
|
|
|
parser := TTextParser(Sender);
|
|
|
|
|
|
|
|
if parser.Stack.Count > 0
|
|
|
|
then ent := TEntity(parser.Stack[parser.Stack.Count-1])
|
|
|
|
else ent := nil;
|
|
|
|
|
|
|
|
if not Assigned(ent) then Exit;
|
|
|
|
cmt := TComment.Create(ent);
|
|
|
|
cmt._Comment := Comment;
|
|
|
|
if IsSubStr('/*', cmt._Comment, 1) then begin
|
|
|
|
cmt._Comment[1] := '(';
|
|
|
|
if isSubStr('*/', cmt._Comment, length(cmt._Comment) - 1) then
|
|
|
|
cmt._Comment[ length(cmt._Comment)] := ')';
|
|
|
|
end;
|
|
|
|
ent.Items.Add(cmt);
|
|
|
|
end;
|
|
|
|
|
2008-03-27 15:27:00 +00:00
|
|
|
constructor TPrecompileHandler.Create(AHeader: TObjCHeader);
|
|
|
|
begin
|
|
|
|
hdr := AHeader;
|
|
|
|
end;
|
|
|
|
|
2008-03-04 23:34:41 +00:00
|
|
|
procedure ReadAndParseFile(const FileName: AnsiString; outdata: TStrings);
|
|
|
|
var
|
2008-03-28 10:25:55 +00:00
|
|
|
hdr : TObjCHeader;
|
|
|
|
parser : TTextParser;
|
|
|
|
prec : TPrecompileHandler;
|
|
|
|
s : AnsiString;
|
2008-03-04 23:34:41 +00:00
|
|
|
begin
|
2008-03-25 08:17:24 +00:00
|
|
|
if not FileExists(FileName) then
|
|
|
|
Exit;
|
|
|
|
|
2008-03-04 23:34:41 +00:00
|
|
|
s := StrFromFile(FileName);
|
|
|
|
hdr := TObjCHeader.Create;
|
2008-03-27 15:27:00 +00:00
|
|
|
prec := TPrecompileHandler.Create(hdr);
|
2008-03-28 10:25:55 +00:00
|
|
|
parser := TTextParser.Create;
|
|
|
|
parser.TokenTable := CreateObjCTokenTable;
|
2008-03-27 15:27:00 +00:00
|
|
|
|
2008-03-04 23:34:41 +00:00
|
|
|
try
|
2008-03-28 10:25:55 +00:00
|
|
|
parser.Buf := s;
|
2008-03-04 23:34:41 +00:00
|
|
|
try
|
2008-03-28 10:25:55 +00:00
|
|
|
parser.TokenTable.Precompile := '#';
|
|
|
|
parser.OnPrecompile := prec.OnPrecompile;
|
|
|
|
parser.OnComment := prec.OnComment;
|
2008-03-25 08:17:24 +00:00
|
|
|
hdr._FileName := ExtractFileName(FileName);
|
2008-03-28 10:25:55 +00:00
|
|
|
hdr.Parse(parser);
|
2008-03-04 23:34:41 +00:00
|
|
|
except
|
|
|
|
end;
|
2008-03-25 08:17:24 +00:00
|
|
|
WriteOutIncludeFile(hdr, outdata);
|
2008-03-04 23:34:41 +00:00
|
|
|
finally
|
|
|
|
hdr.Free;
|
2008-03-28 10:25:55 +00:00
|
|
|
parser.TokenTable.Free;
|
|
|
|
parser.Free;
|
2008-03-27 15:27:00 +00:00
|
|
|
prec.Free;
|
2008-03-04 23:34:41 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2008-03-25 08:17:24 +00:00
|
|
|
inpf : AnsiString;
|
2008-03-31 12:39:54 +00:00
|
|
|
st : TStrings;
|
|
|
|
i : integer;
|
2008-03-04 23:34:41 +00:00
|
|
|
begin
|
|
|
|
try
|
2008-03-31 12:39:54 +00:00
|
|
|
inpf := ParamStr(1);
|
|
|
|
if not FileExists(inpf) then begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
st := TStringList.Create;
|
|
|
|
try
|
|
|
|
ReadAndParseFile(inpf, st);
|
|
|
|
for i := 0 to st.Count - 1 do
|
|
|
|
writeln(st[i]);
|
|
|
|
except
|
|
|
|
end;
|
|
|
|
st.Free;
|
2008-03-25 08:17:24 +00:00
|
|
|
except
|
2008-03-31 12:39:54 +00:00
|
|
|
on e: exception do
|
|
|
|
writeln(e.Message);
|
2008-03-04 23:34:41 +00:00
|
|
|
end;
|
|
|
|
end.
|
|
|
|
|