/components/jcf2/Process/Obfuscate/RemoveReturn.pas

http://github.com/graemeg/lazarus · Pascal · 81 lines · 35 code · 17 blank · 29 comment · 3 complexity · 625f1446a9520acd97ea72325c3e36fe MD5 · raw file

  1. unit RemoveReturn;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is RemoveReturn, 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. uses SwitchableVisitor;
  25. type
  26. TRemoveReturn = class(TSwitchableVisitor)
  27. protected
  28. function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
  29. public
  30. constructor Create; override;
  31. end;
  32. implementation
  33. uses SourceToken, Tokens, ParseTreeNodeType, FormatFlags;
  34. constructor TRemoveReturn.Create;
  35. begin
  36. inherited;
  37. FormatFlags := FormatFlags + [eObfuscate];
  38. end;
  39. function TRemoveReturn.EnabledVisitSourceToken(const pcNode: TObject): boolean;
  40. var
  41. lcSourceToken, lcPrev: TSourceToken;
  42. begin
  43. Result := False;
  44. lcSourceToken := TSourceToken(pcNode);
  45. // only act on returns
  46. if lcSourceToken.TokenType <> ttReturn then
  47. exit;
  48. { not in asm }
  49. if lcSourceToken.HasParentNode(nAsm) then
  50. exit;
  51. // never remove the return after a comment like this
  52. lcPrev := lcSourceToken.PriorTokenWithExclusions([ttWhiteSpace]);
  53. if (lcPrev <> nil) and (lcPrev.TokenType = ttComment) and
  54. (lcPrev.CommentStyle = eDoubleSlash) then
  55. exit;
  56. // transmute to white space - may be needed as seperator
  57. lcSourceToken.SourceCode := ' ';
  58. lcSourceToken.TokenType := ttWhiteSpace;
  59. end;
  60. end.