/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

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