PageRenderTime 24ms CodeModel.GetById 21ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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