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

/components/jcf2/Process/SwitchableVisitor.pas

http://github.com/graemeg/lazarus
Pascal | 128 lines | 61 code | 29 blank | 38 comment | 5 complexity | 9f46e010236c7b34443402e817e560fb MD5 | raw file
  1unit SwitchableVisitor;
  2
  3{ AFS 22 Feb 02
  4  this visitor respects the special comments "//jcf:"
  5  that can turn sertain processes off and on again
  6}
  7
  8{(*}
  9(*------------------------------------------------------------------------------
 10 Delphi Code formatter source code 
 11
 12The Original Code is SwitchableVisitor, released May 2003.
 13The Initial Developer of the Original Code is Anthony Steele. 
 14Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
 15All Rights Reserved. 
 16Contributor(s): Anthony Steele. 
 17
 18The contents of this file are subject to the Mozilla Public License Version 1.1
 19(the "License"). you may not use this file except in compliance with the License.
 20You may obtain a copy of the License at http://www.mozilla.org/NPL/
 21
 22Software distributed under the License is distributed on an "AS IS" basis,
 23WITHOUT WARRANTY OF ANY KIND, either express or implied.
 24See the License for the specific language governing rights and limitations 
 25under the License.
 26
 27Alternatively, the contents of this file may be used under the terms of
 28the GNU General Public License Version 2 or later (the "GPL") 
 29See http://www.gnu.org/licenses/gpl.html
 30------------------------------------------------------------------------------*)
 31{*)}
 32
 33{$I JcfGlobal.inc}
 34
 35interface
 36
 37uses BaseVisitor, FormatFlags;
 38
 39type
 40
 41  TSwitchableVisitor = class(TBaseTreeNodeVisitor)
 42  private
 43    // is this processs on?
 44    fbEnabled: boolean;
 45    // on/off flags that this processor responds to
 46    feFormatFlags: TFormatFlags;
 47
 48  protected
 49    // enabled state may be changed by this token
 50    procedure CheckEnabled(const pcToken: TObject); virtual;
 51
 52    // every token is inspected, even when the visitor is disabled
 53    procedure InspectSourceToken(const {%H-}pcToken: TObject); virtual;
 54    // this is only called when the processor is enabled
 55    function EnabledVisitSourceToken(const {%H-}pcToken: TObject): Boolean; virtual;
 56
 57  public
 58    constructor Create; override;
 59
 60    function VisitSourceToken(const pcToken: TObject): Boolean; override;
 61
 62    property FormatFlags: TFormatFlags Read feFormatFlags Write feFormatFlags;
 63  end;
 64
 65implementation
 66
 67uses SourceToken, Tokens, ParseError;
 68
 69constructor TSwitchableVisitor.Create;
 70begin
 71  inherited;
 72  fbEnabled := True;
 73
 74  //by default, format unless all processors are turned off
 75  feFormatFlags := [eAllFormat];
 76end;
 77
 78procedure TSwitchableVisitor.CheckEnabled(const pcToken: TObject);
 79var
 80  lcToken: TSourceToken;
 81  leFlags: TFormatFlags;
 82  lsError: string;
 83  lbHasFlags: boolean;
 84  lbOn: boolean;
 85begin
 86  lcToken := TSourceToken(pcToken);
 87
 88  if lcToken.TokenType <> ttComment then
 89    exit;
 90
 91  lbHasFlags := ReadCommentJcfFlags(lcToken.SourceCode, lsError, leFlags, lbOn);
 92
 93  if not lbHasFlags then
 94    exit;
 95
 96  if lsError <> '' then
 97    raise TEParseError.Create(lsError, lcToken);
 98
 99  // does this flag affect us? 
100  if (FormatFlags * leFlags) <> [] then
101    fbEnabled := lbOn;
102end;
103
104
105function TSwitchableVisitor.EnabledVisitSourceToken(const pcToken: TObject): Boolean;
106begin
107  // here for override
108  Result := False;
109end;
110
111procedure TSwitchableVisitor.InspectSourceToken(const pcToken: TObject);
112begin
113  // here for override
114end;
115
116function TSwitchableVisitor.VisitSourceToken(const pcToken: TObject): Boolean;
117begin
118  CheckEnabled(pcToken);
119
120  InspectSourceToken(pcToken);
121
122  if fbEnabled then
123    Result := EnabledVisitSourceToken(pcToken)
124  else
125    Result:= False;
126end;
127
128end.