I am writing an IDE to use with Delphi DWScript and now have a simple debuggable script. I now want to highlight the executable lines in my source (like the blue dots at the left of the Delphi source). Digging for examples / information I see that there is a program 'SymbolDictionary' where I can call 'FindSymbolUsage( suReference)' - this seems to give me positions of symbols 'being referred to', and I guess I could call this again with 'suImplementation' to get the lines where there is an assignment. This has made me realise though that I could do with understanding what the structure and purpose of the ContextMap and the SymbolDictionary actually are. Does anyone have an example of listing the executable line numbers of the script?
My fledgling code is reproduced below and is awaiting critical analysis :-) Thanks
TExecutableLines = class( TObject )
constructor Create;
destructor Destroy; override;
PRIVATE
FLines : TBits;
function GetIsExecutable(ALineNumber: integer): boolean;
procedure SetIsExecutable(ALineNumber: integer; const Value: boolean);
PUBLIC
procedure Clear;
procedure Evaluate( AProgram : IdwsProgram; const AUnitName : string );
property IsExecutable[ALineNumber : integer] : boolean
read GetIsExecutable
write SetIsExecutable;
end;
{ TExecutableLines }
procedure TExecutableLines.Clear;
begin
FLines.Size := 0;
FLines.Size := 1024;
end;
constructor TExecutableLines.Create;
begin
inherited;
FLines := TBits.Create;
end;
destructor TExecutableLines.Destroy;
begin
FreeAndnil( FLines );
inherited;
end;
procedure TExecutableLines.Evaluate(AProgram: IdwsProgram; const AUnitName : string);
var
I : integer;
Pos : TSymbolPosition;
begin
Clear;
For I := 0 to AProgram.SymbolDictionary.Count-1 do
begin
Pos := AProgram.SymbolDictionary.FindSymbolPosList(
AProgram.SymbolDictionary[I].Symbol ).FindUsage( suReference);
if Pos <> nil then
If Pos.ScriptPos.IsMainModule then
IsExecutable[ Pos.ScriptPos.Line ] := True
else
if SameText( Pos.UnitName, AUnitName ) then
IsExecutable[ Pos.ScriptPos.Line ] := True
end;
end;
function TExecutableLines.GetIsExecutable(ALineNumber: integer): boolean;
begin
if ALineNumber = 0 then
raise Exception.Create('Lines numbers are 1..n' );
if ALineNumber < FLines.Size then
Result := FLines[ALineNumber]
else
Result := False;
end;
procedure TExecutableLines.SetIsExecutable(ALineNumber: integer;
const Value: boolean);
begin
if ALineNumber = 0 then
raise Exception.Create('Lines numbers are 1..n' );
if ALineNumber >= FLines.Size then
FLines.Size := ALineNumber+1;
FLines[ALineNumber] := Value;
end;