Forum > Editor

[solution] Easy way to strip comments?

(1/7) > >>

indydev:
Maybe I missed something, but is there a way, in the Lazarus IDE to either copy, or save a unit stripped of all of its comments?

For the inevitable question 'why?':  Because sometimes I get units loaded with unhelpful comments; sometimes I need to send something with comments only for me that another person will not need; to help train AI models etc. The idea isn't to remove comments in a project, but to have a way to strip out for uses beyond the project you are working on.

cdbc:
Hi
I think I can whip something together in a jiffy, hang on...
edit:
I'm back...
With a quick'n'dirty console app, it's got some beauty-flaws, but it's a first try, so bear with me  :D
It could perhaps also do with a bit of debugging, but it's getting late, here in Denmark...
Let me know what you think...  8-)
Regards Benny

cdbc:
Hi
Right, couldn't help myself...  :D
Here's a new version, with some bug-fixes
Should be better, let me know, what you think  %)
edit: I call it like this:
 "./strip_pas_comments comment_states.pas > strip.pas"
/without the quotes/
Regards Benny

indydev:
Thanks.  I had some trouble getting back on here for some reason, and I decided to write my own. Will check yours. Here's my command line version that works "ok" for now.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program stripcmts; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes, SysUtils, CustApp,  RegExpr; type   { TMyApplication }   TMyApplication = class(TCustomApplication)  protected    procedure DoRun; override;  public    constructor Create(TheOwner: TComponent); override;    procedure WriteHelp; virtual;  end; var  FileName: String;  FileContent: TStringList;  RegEx: TRegExpr;  i: integer; { TMyApplication } procedure TMyApplication.DoRun;var  ErrorMsg: String;begin  // quick check parameters  ErrorMsg:=CheckOptions('h', 'help');  if ErrorMsg<>'' then begin    ShowException(Exception.Create(ErrorMsg));    Terminate;    Exit;  end;   // parse parameters  if HasOption('h', 'help') then begin    WriteHelp;    Terminate;    Exit;  end;   if ParamCount < 1 then  begin    WriteLn('Usage: ./stripcmts <filename>');    Terminate;    Exit;  end;   FileName := ParamStr(1);  FileContent := TStringList.Create;  try    FileContent.LoadFromFile(FileName);     RegEx := TRegExpr.Create;    RegEx.ModifierM:=TRUE;     try      RegEx.Expression := '\/\/.*';      RegEx.ModifierI := TRUE; // case-insensitive       for i := 0 to FileContent.Count - 1 do      begin        if RegEx.Exec(FileContent[i]) then FileContent[i] := StringReplace(FileContent[i], RegEx.Match[0], '', []);      end;       RegEx.Expression := '\{\s*[^\s\$].*?\}'; //'\{[^\$].*?\}';      RegEx.ModifierS := TRUE;      FileContent.Text := RegEx.Replace(FileContent.Text, '', FALSE);       finally      RegEx.Free;    end;     FileContent.SaveToFile(ChangeFileExt(FileName, '_CmtFree.pas'));  finally    FileContent.Free;  end;  // stop program loop  Terminate;end; constructor TMyApplication.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);  StopOnException:=True;end; procedure TMyApplication.WriteHelp;begin  { still need to write help code here }  writeln('Usage: ', ExeName, ' -h');end; var  Application: TMyApplication;begin  Application:=TMyApplication.Create(nil);  Application.Title:='Strip Comments';  Application.Run;  Application.Free;end.
need to add '(*' '*)' blocks to be more complete, and this doesn't handle nested blocks.

Thanks for writing a couple up.

indydev:

--- Quote from: cdbc on May 30, 2024, 03:06:34 am ---Hi
Right, couldn't help myself...  :D
Here's a new version, with some bug-fixes
Should be better, let me know, what you think  %)
edit: I call it like this:
 "./strip_pas_comments comment_states.pas > strip.pas"
/without the quotes/
Regards Benny

--- End quote ---


Ok. You went to town on this. I just put some regular expressions together. I didn't bother with a more advanced state machine. Thanks for your work!

Navigation

[0] Message Index

[#] Next page

Go to full version