Jeg er lidt på skideren. Jeg vil gerne lave en php parser som finder alle variabler, includes og funktioner.
Dog bruger min kode en del ram, hvilket jeg gerne vil have at den ikke gør.
Her er hele min kode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SynEdit, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
path: TEdit;
Editor: TSynEdit;
Memo1: TMemo;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
type
TPHPType = (tNone, tCodeStart, tCodeEnd, tFunction, tVar, tInclude);
var
LastPHPType, PHPType: TPHPType;
LastPHPData, PHPData: String;
PHPCode: String;
InsidePHPCode: Boolean;
procedure ParseNextPHP;
begin
LastPHPType := PHPType;
LastPHPData := PHPData;
PHPType := tNone;
PHPData := '';
PHPCode := Trim(PHPCode);
if (PHPCode = '') OR (Length(PHPCode) = 0) then
begin
Exit;
end;
{if Copy(PHPCode, 1, 1) = '''' then
begin
end else
begin}
if Copy(PHPCode, 1, 2) = '<?' then
begin
InsidePHPCode := True;
PHPData := '<?';
PHPType := tCodeStart;
Delete(PHPCode, 1, 2);
end else
if Copy(PHPCode, 1, 2) = '?>' then
begin
InsidePHPCode := False;
PHPData := '?>';
PHPType := tCodeEnd;
Delete(PHPCode, 1, 2);
end else
if Copy(PHPCode, 1, 1) = '$' then
begin
PHPData := '$';
PHPType := tVar;
Delete(PHPCode, 1, 1);
end else
if PHPCode[1] in ['a'..'z', 'A'..'Z', '_'] then
begin
//Text
while (Length(PHPCode) > 0) and (PHPCode[1] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) do
begin
PHPData := PHPData + PHPCode[1];
Delete(PHPCode, 1, 1);
end;
end else
begin
PHPData := Copy(PHPCode, 1, 1);
Delete(PHPCode, 1, 1);
end;
end;
begin
Memo1.Lines.Clear;
InsidePHPCode := False;
PHPCode := Editor.Text;
ParseNextPHP;
while PHPData <> '' do
begin
//ShowMessage(PHPData);
if LastPHPType = tVar then
begin
if Memo1.Lines.IndexOf(PHPData) = -1 then
Memo1.Lines.Add(PHPData);
//ShowMessage('En variable er fundet! Navnet er:' + #13#10 + PHPData);
end;
ParseNextPHP;
end;
end;
end.
Koden bliver udført hvert sekundt og "lakker" en del.
Er der en som kan gør så den bruger mindre ram eller har en anden metode hvorpå jeg kan parse disse ting?
Indlæg senest redigeret d. 10.05.2007 16:39 af Bruger #8254