/components/jcf2/Process/Obfuscate/RemoveComment.pas

http://github.com/graemeg/lazarus · Pascal · 108 lines · 53 code · 19 blank · 36 comment · 6 complexity · 27f16e9e907753eeb1d1a662b7af7d5d MD5 · raw file

  1. unit RemoveComment;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is RemoveComment, released May 2003.
  6. The Initial Developer of the Original Code is Anthony Steele.
  7. Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
  8. All Rights Reserved.
  9. Contributor(s): Anthony Steele.
  10. The contents of this file are subject to the Mozilla Public License Version 1.1
  11. (the "License"). you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at http://www.mozilla.org/NPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied.
  15. See the License for the specific language governing rights and limitations
  16. under the License.
  17. Alternatively, the contents of this file may be used under the terms of
  18. the GNU General Public License Version 2 or later (the "GPL")
  19. See http://www.gnu.org/licenses/gpl.html
  20. ------------------------------------------------------------------------------*)
  21. {*)}
  22. {$I JcfGlobal.inc}
  23. interface
  24. { AFS 28 Dec 2002
  25. Obfuscate by removing comments
  26. }
  27. uses SwitchableVisitor;
  28. type
  29. TRemoveComment = class(TSwitchableVisitor)
  30. protected
  31. function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
  32. public
  33. constructor Create; override;
  34. end;
  35. implementation
  36. uses
  37. JcfStringUtils,
  38. SourceToken, Tokens, ParseTreeNodeType, FormatFlags;
  39. function CommentMustStay(const pc: TSourceToken): boolean;
  40. var
  41. lsPrefix: string;
  42. begin
  43. Result := False;
  44. lsPrefix := StrLeft(pc.SourceCode, 2);
  45. if (lsPrefix = '{$') or (lsPrefix = '{%') then
  46. Result := True;
  47. { all curly backets in the uses clause of a program/library def
  48. must be respected as they link files to dfms, com classes 'n stuff }
  49. if (pc.CommentStyle in CURLY_COMMENTS) and
  50. (pc.HasParentNode(TopOfProgramSections)) and pc.HasParentNode(UsesClauses) and
  51. pc.IsOnRightOf(UsesClauses, UsesWords) then
  52. Result := True;
  53. { these comments are flags to the code format program, so leave them }
  54. if (pc.SourceCode = '{(*}') or (pc.SourceCode = '{*)}') then
  55. Result := True;
  56. // these are also flags
  57. if ((pc.CommentStyle = eDoubleSlash) and
  58. (StrLeft(pc.SourceCode, FORMAT_COMMENT_PREFIX_LEN) = FORMAT_COMMENT_PREFIX)) then
  59. Result := True;
  60. end;
  61. constructor TRemoveComment.Create;
  62. begin
  63. inherited;
  64. FormatFlags := FormatFlags + [eObfuscate];
  65. end;
  66. function TRemoveComment.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
  67. var
  68. lcSourceToken: TSourceToken;
  69. begin
  70. Result := False;
  71. lcSourceToken := TSourceToken(pcNode);
  72. (* turn comment to space - may be needed for token sep
  73. e.g. may be for a :=b{foo}to{bar}baz
  74. *)
  75. if lcSourceToken.TokenType = ttComment then
  76. begin
  77. if not CommentMustStay(lcSourceToken) then
  78. begin
  79. lcSourceToken.TokenType := ttWhiteSpace;
  80. lcSourceToken.SourceCode := ' ';
  81. end;
  82. end;
  83. end;
  84. end.