amxmodx/editor/studio/UnitTextAnalyze.pas

454 lines
16 KiB
ObjectPascal
Raw Normal View History

2005-08-26 18:29:39 +00:00
unit UnitTextAnalyze;
interface
uses SysUtils, Classes, Windows, Forms;
type TPawnParseResult = class
2005-08-26 18:29:39 +00:00
public
Constants: TStringList;
Defined: TStringList;
CVars: TStringList;
Included: TStringList;
MethodsDefault: TStringList;
Events: TStringList;
Stocks: TStringList;
Natives: TStringList;
Forwards: TStringList;
Variables: TStringList;
CallTips: TStringList;
AutoComplete: TStringList;
HighlightKeywords: TStringList;
constructor Create; reintroduce;
procedure DestroyResult;
end;
function ParseCodePawn(eCode: TStringList; FileName: string; IsRecursive: Boolean = False): TPawnParseResult;
function UpdateIncPath(eInput: string): string;
2005-08-26 18:29:39 +00:00
var eCPUSpeed: Integer = 1;
implementation
uses UnitCodeExplorerUpdater, UnitCodeUtils, UnitfrmSettings,
UnitMainTools, UnitfrmMain;
var eLookedUpIncluded: TStringList;
function UpdateIncPath(eInput: string): string;
2005-08-26 18:29:39 +00:00
begin
eInput := StringReplace(Trim(eInput), '/', '\', [rfReplaceAll]);
if FileExists(ExtractFilePath(frmSettings.txtPawnCompilerPath.Text) + eInput + '.inc') then
Result := ExtractFilePath(frmSettings.txtPawnCompilerPath.Text) + eInput + '.inc'
else if FileExists(ExtractFilePath(frmSettings.txtPawnCompilerPath.Text) + 'include\' + eInput + '.inc') then
Result := ExtractFilePath(frmSettings.txtPawnCompilerPath.Text) + 'include\' + eInput + '.inc'
2005-11-12 15:11:14 +00:00
else if (FileExists(ExtractFilePath(ActiveDoc.FileName) + eInput + '.inc')) then
2005-08-26 18:29:39 +00:00
Result := ExtractFilePath(ActiveDoc.FileName) + eInput + '.inc'
else
Result := '';
end;
function ParseCodePawn(eCode: TStringList; FileName: string; IsRecursive: Boolean = False): TPawnParseResult;
2005-08-26 18:29:39 +00:00
var i, k: integer;
eString, eTemp, eBackup: string;
eStr, ePreEvents: TStringList;
eStartLine, eBracesOpen: Integer;
eTimeToSleep: Integer;
eAddingEnum: Integer;
eTempResult: TPawnParseResult;
eProcedureAdded: Boolean;
eCActive: Boolean;
2005-08-26 18:29:39 +00:00
begin
Result := TPawnParseResult.Create;
if not IsRecursive then
eLookedUpIncluded.Clear;
eStr := TStringList.Create;
ePreEvents := TStringList.Create;
eBracesOpen := 0;
eStartLine := -1;
eTimeToSleep := 0;
eAddingEnum := 0;
2005-11-01 03:07:47 +00:00
eCActive := False;
2005-08-26 18:29:39 +00:00
for i := 0 to eCode.Count - 1 do begin
if (Application.Terminated) or (not Started) or (frmMain.pnlLoading.Visible) or (not frmMain.trvExplorer.Visible) then exit;
2005-11-12 15:11:14 +00:00
eBackup := Trim(eCode[i]);
eString := RemoveStringsAndComments(Trim(eCode[i]), True, True);
2005-11-12 15:11:14 +00:00
if (Pos('/*', eBackup) = 1) or (Pos('*/', eBackup) <> 0) then begin
eCActive := (Pos('/*', eBackup) = 1);
if (eCActive) and (Pos('*/', eBackup) <> 0) then begin
eCActive := False;
2005-11-12 15:11:14 +00:00
continue
end;
2005-11-01 03:07:47 +00:00
end;
2005-11-12 15:11:14 +00:00
if (eBackup = '') or (Pos('//', eBackup) = 1) or (eCActive) then
2005-11-01 03:07:47 +00:00
continue;
2005-11-12 15:11:14 +00:00
2005-08-26 18:29:39 +00:00
eProcedureAdded := False;
Inc(eTimeToSleep, 1);
if eTimeToSleep = eCPUSpeed then begin
Sleep(1);
eTimeToSleep := 0;
end;
{ Constants and Variables }
if (IsAtStart('new', eString, False)) or (IsAtStart('stock', eString, False)) then begin // const or variable
if (eBracesOpen = 0) and (not IsRecursive) and (Pos('(', eString) = Pos(')', eString)) then begin
Delete(eString, 1, Pos(#32, eString));
eString := Trim(eString);
// we don't need braces so delete them...
while (CountChars(eString, '{') <> 0) and (CountChars(eString, '}') <> 0) and (Pos('{', eString) < Pos('}', eString)) do
eString := StringReplace(eString, '{' + Between(eString, '{', '}') + '}', '', [rfReplaceAll]);
while (CountChars(eString, '[') <> 0) and (CountChars(eString, ']') <> 0) and (Pos('[', eString) < Pos(']', eString)) do
eString := StringReplace(eString, '[' + Between(eString, '[', ']') + ']', '', [rfReplaceAll]);
// done? okay, split all items if there are more than one; and if not, it's okay...
eStr.Text := StringReplace(eString, ',', #13, [rfReplaceAll]);
for k := 0 to eStr.Count - 1 do begin
if (Trim(eStr[k]) <> '') and (eStr[k] <> '}') then begin
eTemp := Trim(RemoveSemicolon(eStr[k]));
if Pos(':', eTemp) <> 0 then
eTemp := Copy(eTemp, Pos(':', eTemp) + 1, Length(eTemp));
if Pos('=', eTemp) <> 0 then begin // constant
Result.Constants.AddObject(Copy(eTemp, 1, Pos('=', eTemp) - 1), TObject(i));
Result.AutoComplete.Add(Copy(eTemp, 1, Pos('=', eTemp) - 1));
end
else begin // variable
Result.Variables.AddObject(eTemp, TObject(i));
Result.AutoComplete.Add(eTemp);
end;
2005-08-26 18:29:39 +00:00
end;
end;
eString := RemoveStringsAndComments(Trim(eCode[i]), True, True);
continue;
2005-08-26 18:29:39 +00:00
end;
2005-11-01 03:07:47 +00:00
end;
2005-08-26 18:29:39 +00:00
{ Included }
2005-11-01 03:07:47 +00:00
if (IsAtStart('#include', eBackup)) then begin
eString := StringReplace(eBackup, '/', '\', [rfReplaceAll]);
2005-08-26 18:29:39 +00:00
if Between(eString, '<', '>') <> '' then begin
eString := Between(eString, '<', '>');
if ExtractFileExt(eString) <> '' then
ChangeFileExt(eString, '');
end
else if Between(eString, '"', '"') <> '' then begin
eString := Between(eString, '"', '"');
if ExtractFileExt(eString) <> '' then
ChangeFileExt(eString, '');
end
else begin
eString := Trim(eString);
2005-08-26 18:29:39 +00:00
if ExtractFileExt(eString) <> '' then
ChangeFileExt(eString, '');
end;
eString := Trim(eString);
Result.Included.AddObject(eString, TObject(i));
// Recursive update
if (eLookedUpIncluded.IndexOf(eString) = -1) then begin
eLookedUpIncluded.Add(eString);
eTemp := UpdateIncPath(eString);
if (eString <> '') and (FileExists(eTemp)) then begin
// Load code and parse
eTempResult := nil;
2005-08-26 18:29:39 +00:00
try
eStr.LoadFromFile(eTemp);
if Application.Terminated then exit;
eTempResult := ParseCodePawn(eStr, ExtractFileName(eTemp), True);
2005-08-26 18:29:39 +00:00
// Assign parsed values
Result.AutoComplete.AddStrings(eTempResult.AutoComplete);
Result.CallTips.AddStrings(eTempResult.CallTips);
Result.HighlightKeywords.AddStrings(eTempResult.HighlightKeywords);
// free
except
// mmmm.. burger
end;
if Assigned(eTempResult) then
eTempResult.DestroyResult;
2005-08-26 18:29:39 +00:00
// wait
Sleep(20);
end;
end;
2005-11-01 03:07:47 +00:00
continue;
end;
2005-08-26 18:29:39 +00:00
{ CVars }
2005-11-01 03:07:47 +00:00
if (IsAtStart('register_cvar', eString)) and (not IsRecursive) then begin
2005-08-26 18:29:39 +00:00
if Between(eString, '"', '"') <> '' then
Result.CVars.AddObject(Between(eBackup, '"', '"'), TObject(i));
2005-11-01 03:07:47 +00:00
continue;
end;
2005-08-26 18:29:39 +00:00
{ Defined }
2005-11-01 03:07:47 +00:00
if (IsAtStart('#define', eString)) then begin
eString := Copy(eString, 8, Length(eString));
2005-08-26 18:29:39 +00:00
eString := Trim(eString);
Result.CallTips.Add(eString + '-> ' + FileName + ', defined constant');
2005-08-26 18:29:39 +00:00
if Pos(#32, eString) <> 0 then
eString := Copy(eString, 1, Pos(#32, eString) - 1);
if Pos(' ', eString) <> 0 then
eString := Copy(eString, 1, Pos(' ', eString) - 1);
Result.Defined.AddObject(eString, TObject(i));
Result.AutoComplete.Add(eString);
2005-11-01 03:07:47 +00:00
continue;
end;
2005-08-26 18:29:39 +00:00
{ Events (Part 1) }
2005-11-01 03:07:47 +00:00
if (IsAtStart('register_event(', eString)) and (not IsRecursive) then begin
if CountChars(eBackup, '"') >= 4 then begin
eTemp := StringReplace(eBackup, '"' + Between(eBackup, '"', '"') + '"', '', []);
ePreEvents.Add(Between(eBackup, '"', '"'));
2005-08-26 18:29:39 +00:00
end;
2005-11-01 03:07:47 +00:00
continue;
2005-08-26 18:29:39 +00:00
end;
{ Functions (1), this is adapted from AMXX-Edit v2 [see TextAnalyze.pas] }
eBracesOpen := eBracesOpen + CountChars(eString, '{');
eBracesOpen := eBracesOpen - CountChars(eString, '}');
if Pos('{', eString) <> 0 then begin
{ Enums -> }
if eAddingEnum = 1 then begin
eAddingEnum := 2;
Delete(eString, 1, Pos('{', eString) + 1);
end
else begin
if eStartLine = -1 then begin
eProcedureAdded := True;
eStartLine := i;
end;
end;
{ <- Enums }
end;
if (Pos('}', eString) <> 0) then begin
2005-08-26 18:29:39 +00:00
{ Enums -> }
if eAddingEnum <> 0 then
eAddingEnum := 0;
{ <- Enums }
if (eStartLine <> -1) then begin
if (eBracesOpen = 0) and (not IsAtStart('new', Trim(eCode[eStartLine]))) then begin
if Trim(RemoveStringsAndComments(eCode[eStartLine], True, True)) = '{' then
eStartLine := eStartLine - 1;
eTemp := Trim(RemoveSemicolon(Trim(eCode[eStartLine])));
2005-08-26 18:29:39 +00:00
// Analyze type
k := 0;
if IsAtStart('public', eTemp) then
k := 1
else if IsAtStart('stock', eTemp) then
k := 2
else if IsAtStart('native', eTemp) then
k := 3
else if IsAtStart('forward', eTemp) then
k := 4
else if Pos('enum', LowerCase(eTemp)) = 1 then // no method
k := 5;
2005-08-26 18:29:39 +00:00
// Remove type
if Pos('@', eTemp) = 1 then begin
eTemp := Copy(eTemp, 2, Length(eTemp));
k := 1;
end
else begin
if (Pos(#32, eTemp) <> 0) and (Pos(#32, eTemp) < Pos('(', eTemp)) then
eTemp := Copy(eCode[eStartLine], Pos(#32, eCode[eStartLine]) + 1, Length(eCode[eStartLine]))
else if (Pos(#9, eTemp) <> 0) and (Pos(#9, eTemp) < Pos('(', eTemp)) then
eTemp := Copy(eTemp, Pos(#9, eTemp) + 1, Length(eTemp));
end;
if eTemp[Length(eTemp)] = '{' then
eTemp := Trim(Copy(eTemp, 1, Length(eTemp) - 1));
2005-08-26 18:29:39 +00:00
// Remove return-type
if (Pos(':', eTemp) <> 0) and (Pos(':', eTemp) < Pos('(', eTemp)) then
Delete(eTemp, 1, Pos(':', eTemp));
2005-08-26 18:29:39 +00:00
if Pos('operator', eTemp) = 1 then
k := 6;
2005-08-26 18:29:39 +00:00
if k < 5 then begin
case k of
0: Result.CallTips.Add(eTemp + '-> ' + FileName + ', function');
1: Result.CallTips.Add(eTemp + '-> ' + FileName + ', public function');
2: Result.CallTips.Add(eTemp + '-> ' + FileName + ', stock');
3: Result.CallTips.Add(eTemp + '-> ' + FileName + ', native');
4: Result.CallTips.Add(eTemp + '-> ' + FileName + ', forward');
end;
end;
2005-08-26 18:29:39 +00:00
// Copy function-name
if Pos('(', eTemp) <> 0 then
eTemp := Copy(eTemp, 1, Pos('(', eTemp) - 1);
eTemp := Trim(eTemp);
2005-11-13 10:40:59 +00:00
if (Result.AutoComplete.IndexOf(eTemp) <> -1) then begin
eStartLine := -1;
eBracesOpen := 0;
continue;
end;
2005-08-26 18:29:39 +00:00
if k < 5 then begin
Result.AutoComplete.Add(eTemp);
Result.HighlightKeywords.Add(eTemp);
end;
if eTemp <> '' then begin
case k of
0: begin
if not IsRecursive then
Result.MethodsDefault.AddObject(eTemp, TObject(eStartLine)); // Default Method
end;
1: begin
k := ePreEvents.IndexOf(eTemp);
if k <> -1 then begin
Result.Events.AddObject(eTemp, ePreEvents.Objects[k]);
ePreEvents.Delete(k);
end
else
Result.MethodsDefault.AddObject(eTemp, TObject(eStartLine));
end;
2: Result.Stocks.AddObject(eTemp, TObject(eStartLine));
3: Result.Natives.AddObject(eTemp, TObject(eStartLine));
4: Result.Forwards.AddObject(eTemp, TObject(eStartLine));
2005-08-26 18:29:39 +00:00
end;
end;
eStartLine := -1;
eBracesOpen := 0;
2005-08-26 18:29:39 +00:00
end;
end;
end
else if (eAddingEnum = 2) and (Pos('enum', LowerCase(eString)) <> 1) then begin
if Pos(' ', eString) <> 0 then
eString := Copy(eString, 1, Pos(' ', eString) - 1);
if Pos(',', eString) <> 0 then
eString := Copy(eString, 1, Pos(',', eString) - 1);
if Pos(' ', eString) <> 0 then
eString := Copy(eString, 1, Pos(' ', eString) - 1);
if Pos(':', eString) <> 0 then
eString := Copy(eString, 1, Pos(':', eString) - 1);
Result.AutoComplete.Add(eString);
end;
{ Enums }
if IsAtStart('enum', eString) then begin
if Pos('{', eString) <> 0 then
eAddingEnum := 2 // Add values immediately
else
eAddingEnum := 1; // Wait for next brace and add then
end;
{ Functions (2) }
if (IsAtStart('forward', eString)) or (IsAtStart('public', eString)) or (IsAtStart('native', eString)) or (IsAtStart('stock', eString)) then begin
if (not eProcedureAdded) and (Pos('(', eString) <> 0) and (Pos(')', eString) <> 0) then begin
eTemp := StringReplace(Trim(eBackup), #9, #32, [rfReplaceAll]);
eTemp := Trim(RemoveSemicolon(eTemp));
2005-08-26 18:29:39 +00:00
if eTemp[Length(eTemp)] = '{' then
eTemp := Trim(Copy(eTemp, 1, Length(eTemp) - 1));
2005-08-26 18:29:39 +00:00
// Remove type
if (Pos(#32, eTemp) <> 0) and (Pos(#32, eTemp) < Pos('(', eTemp)) then
eTemp := Copy(eTemp, Pos(#32, eTemp) + 1, Length(eTemp));
if (Pos(#9, eTemp) <> 0) and (Pos(#9, eTemp) < Pos('(', eTemp)) then
eTemp := Copy(eTemp, Pos(#9, eTemp) + 1, Length(eTemp));
// Remove return-type
if (Pos(':', eTemp) <> 0) and (Pos(':', eTemp) < Pos('(', eTemp)) then
Delete(eTemp, 1, Pos(':', eTemp));
if (Pos('enum', eTemp) = Pos('operator', eTemp)) and (Pos('enum', eTemp) = 0) then
Result.CallTips.Add(eTemp + '-> ' + FileName + ', ' + Trim(Copy(eString, 1, Pos(#32, eString) -1)));
2005-08-26 18:29:39 +00:00
// Copy function-name
if Pos('(', eTemp) <> 0 then
eTemp := Copy(eTemp, 1, Pos('(', eTemp) - 1);
eTemp := Trim(eTemp);
2005-08-26 18:29:39 +00:00
if (Pos('enum', eTemp) = Pos('operator', eTemp)) and (Pos('enum', eTemp) = 0) then begin
Result.AutoComplete.Add(eTemp);
Result.HighlightKeywords.Add(eTemp);
end;
if eTemp <> '' then begin
if IsAtStart('forward', eString) then
Result.Forwards.AddObject(eString, TObject(i))
else if IsAtStart('public', eString) then begin
k := ePreEvents.IndexOf(eTemp);
if k <> -1 then begin
Result.Events.AddObject(eTemp, ePreEvents.Objects[k]);
ePreEvents.Delete(k);
end
else
Result.MethodsDefault.AddObject(eTemp, TObject(i));
end
else if IsAtStart('native', eString) then
Result.Natives.AddObject(eTemp, TObject(i))
else if IsAtStart('stock', eString) then
Result.Stocks.AddObject(eTemp, TObject(i))
else if (Pos('enum', eTemp) = Pos('operator', eTemp)) and (Pos('enum', eTemp) = 0) then
Result.MethodsDefault.AddObject(eTemp, TObject(i));
end;
end;
end;
2005-08-26 18:29:39 +00:00
end;
ePreEvents.Free;
eStr.Free;
end;
{ TPawnParseResult }
2005-08-26 18:29:39 +00:00
constructor TPawnParseResult.Create;
2005-08-26 18:29:39 +00:00
begin
inherited Create;
Constants := TStringList.Create;
Defined := TStringList.Create;
CVars := TStringList.Create;
Included := TStringList.Create;
MethodsDefault := TStringList.Create;
Events := TStringList.Create;
Stocks := TStringList.Create;
Natives := TStringList.Create;
Forwards := TStringList.Create;
Variables := TStringList.Create;
CallTips := TStringList.Create;
AutoComplete := TStringList.Create;
HighlightKeywords := TStringList.Create;
end;
procedure TPawnParseResult.DestroyResult;
2005-08-26 18:29:39 +00:00
begin
AutoComplete.Free;
CallTips.Free;
2005-08-26 18:29:39 +00:00
Constants.Free;
CVars.Free;
Defined.Free;
2005-08-26 18:29:39 +00:00
Events.Free;
Forwards.Free;
HighlightKeywords.Free;
Included.Free;
MethodsDefault.Free;
Natives.Free;
Stocks.Free;
Variables.Free;
2005-08-26 18:29:39 +00:00
Free;
end;
initialization
eLookedUpIncluded := TStringList.Create;
finalization
eLookedUpIncluded.Free;
end.