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;
|
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, ObjCParserUtils, ObjCParserTypes;
|
|
|
|
|
2008-03-27 15:27:00 +00:00
|
|
|
type
|
|
|
|
// this object is used only for precomile directives handling
|
|
|
|
TPrecompileHandler = class(TObject)
|
|
|
|
public
|
|
|
|
hdr : TObjCHeader;
|
|
|
|
procedure OnPrecompile(Sender: TObject);
|
|
|
|
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);
|
|
|
|
//writeln('added to: ', lst.ClassName, ' ', preEntity._Directive + preEntity._Params);
|
|
|
|
lst.Items.Add(preEntity);
|
|
|
|
//write('// debug: ');
|
|
|
|
//writeln('precompile: ', preEntity._Directive, ', params:', preEntity._Params);
|
|
|
|
finally
|
|
|
|
parser.OnPrecompile := prc;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
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-27 15:27:00 +00:00
|
|
|
hdr : TObjCHeader;
|
|
|
|
txt : 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-04 23:34:41 +00:00
|
|
|
txt := TTextParser.Create;
|
|
|
|
txt.TokenTable := CreateObjCTokenTable;
|
2008-03-27 15:27:00 +00:00
|
|
|
|
2008-03-04 23:34:41 +00:00
|
|
|
try
|
|
|
|
txt.Buf := s;
|
|
|
|
try
|
2008-03-27 15:27:00 +00:00
|
|
|
txt.TokenTable.Precompile := '#';
|
|
|
|
txt.OnPrecompile := prec.OnPrecompile;
|
2008-03-25 08:17:24 +00:00
|
|
|
hdr._FileName := ExtractFileName(FileName);
|
2008-03-04 23:34:41 +00:00
|
|
|
hdr.Parse(txt);
|
|
|
|
except
|
|
|
|
end;
|
2008-03-25 08:17:24 +00:00
|
|
|
WriteOutIncludeFile(hdr, outdata);
|
2008-03-04 23:34:41 +00:00
|
|
|
finally
|
|
|
|
hdr.Free;
|
|
|
|
txt.TokenTable.Free;
|
|
|
|
txt.Free;
|
2008-03-27 15:27:00 +00:00
|
|
|
prec.Free;
|
2008-03-04 23:34:41 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure GetParams(var InpFile, OutFile: AnsiString);
|
|
|
|
begin
|
|
|
|
InpFile := ParamStr(1);
|
|
|
|
OutFile := ParamStr(2);
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2008-03-25 08:17:24 +00:00
|
|
|
inpf : AnsiString;
|
2008-03-04 23:34:41 +00:00
|
|
|
f : Text;
|
|
|
|
st : TStrings;
|
|
|
|
fn : AnsiString;
|
|
|
|
i : integer;
|
|
|
|
begin
|
2008-03-25 08:17:24 +00:00
|
|
|
inpf := ParamStr(1);
|
2008-03-04 23:34:41 +00:00
|
|
|
if not FileExists(inpf) then begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
st := TStringList.Create;
|
|
|
|
try
|
2008-03-25 08:17:24 +00:00
|
|
|
ReadAndParseFile(inpf, st);
|
|
|
|
for i := 0 to st.Count - 1 do
|
|
|
|
writeln(st[i]);
|
|
|
|
except
|
2008-03-04 23:34:41 +00:00
|
|
|
end;
|
2008-03-25 08:17:24 +00:00
|
|
|
st.Free;
|
|
|
|
|
2008-03-04 23:34:41 +00:00
|
|
|
end.
|
|
|
|
|