PageRenderTime 30ms CodeModel.GetById 23ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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