File: FormatFlags.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (253 lines) | stat: -rw-r--r-- 7,651 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
unit FormatFlags;

{ AFS 20 July 2001

  Code formatter exclusions flags
  These flags are used to switch off formatting based on special comments }

{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is FormatFlags, released May 2003.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

type

  TFormatFlag = (eAllFormat,
    eObfuscate,
    eAddSpace, eRemoveSpace,
    eAddReturn, eRemoveReturn,
    eAlignVars, eAlignConst, eAlignTypeDef, eAlignAssign, eAlignComment, eAlignField,
    eCapsReservedWord, eCapsSpecificWord,
    eIndent, eLineBreaking, eBlockStyle,
    eWarning,
    eFindReplace, eFindReplaceUses, eRemoveComments);


  { these flags control:

    AllFormat: all clarify processes - turn the formatter as a whole on or off
    space: all processes that insert or remove spaces
    indent: inenting of code blocks etc
    return: all processes that insert or remove returns - note tat there is some overlap with
    eAlign: alignment of vars, assigns etc
    eLineBreaking: spliting long lines into 2 or more
    eBlockStyle - where to put begins & ends, else, etc
    eWarning: supress warnings
  }

  TFormatFlags = set of TFormatFlag;

{ read a comment for comment enabled flag data }
function ReadCommentJcfFlags(psComment: string; out psError: string;
  out peFlags: TFormatFlags; out pbOn: boolean): boolean;

const
  FORMAT_COMMENT_PREFIX     = '//jcf:';
  FORMAT_COMMENT_PREFIX_LEN = 6;

  ALL_FLAGS: TFormatFlags = [Low(TFormatFlag)..High(TFormatFlag)];

implementation

uses
  { delphi }
  {$ifndef fpc}Windows,{$endif} SysUtils,
  { local }
  JcfStringUtils;

type
  TRFlagNameData = record
    sName: string;
    eFlags: TFormatFlags;
  end;

const
  FORMAT_FLAG_NAMES: array[1..29] of TRFlagNameData =
    (
    (sName: 'format'; eFlags: [eAllFormat]),
    (sName: 'obfuscate'; eFlags: [eObfuscate]),


    (sName: 'space'; eFlags: [eAddSpace, eRemoveSpace]),
    (sName: 'addspace'; eFlags: [eAddSpace]),
    (sName: 'removespace'; eFlags: [eRemoveSpace]),


    (sName: 'return'; eFlags: [eAddReturn, eRemoveReturn]),
    (sName: 'addreturn'; eFlags: [eAddReturn]),
    (sName: 'removereturn'; eFlags: [eRemoveReturn]),

    (sName: 'add'; eFlags: [eAddReturn, eAddSpace]),
    (sName: 'remove'; eFlags: [eRemoveReturn, eRemoveSpace]),


    (sName: 'align'; eFlags: [eAlignVars, eAlignConst, eAlignTypeDef,
    eAlignAssign, eAlignComment]),
    (sName: 'aligndef'; eFlags: [eAlignVars, eAlignConst, eAlignTypeDef]),
    (sName: 'alignfn'; eFlags: [eAlignVars, eAlignAssign]),

    (sName: 'alignvars'; eFlags: [eAlignVars]),
    (sName: 'alignconst'; eFlags: [eAlignConst]),
    (sName: 'aligntypedef'; eFlags: [eAlignTypeDef]),
    (sName: 'alignassign'; eFlags: [eAlignAssign]),
    (sName: 'aligncomment'; eFlags: [eAlignComment]),
    (sName: 'alignfield'; eFlags: [eAlignField]),


    (sName: 'indent'; eFlags: [eIndent]),

    (sName: 'caps'; eFlags: [eCapsReservedWord, eCapsSpecificWord]),
    (sName: 'capsreservedwords'; eFlags: [eCapsReservedWord]),
    (sName: 'capsspecificword'; eFlags: [eCapsSpecificWord]),


    (sName: 'linebreaking'; eFlags: [eLineBreaking]),
    (sName: 'blockstyle'; eFlags: [eBlockStyle]),

    (sName: 'warnings'; eFlags: [eWarning]),
    (sName: 'findreplace'; eFlags: [eFindReplace]),
    (sName: 'findreplaceuses'; eFlags: [eFindReplaceUses]),

    (sName: 'reovecomments'; eFlags: [eRemoveComments])
    );


{ can stop and restart formating using these comments
 from DelForExp - Egbbert Van Nes's program }
const
  OLD_NOFORMAT_ON  = '{(*}';
  OLD_NOFORMAT_OFF = '{*)}';

  NOFORMAT_ON  = FORMAT_COMMENT_PREFIX + 'format=off';
  NOFORMAT_OFF = FORMAT_COMMENT_PREFIX + 'format=on';

{ like StrToBoolean, but recognises 'on' and 'off' too }
function LStrToBoolean(const ps: string): boolean;
begin
  if AnsiSameText(ps, 'on') then
    Result := True
  else if AnsiSameText(ps, 'off') then
    Result := False
  else
    Result := StrToBoolean(ps);
end;

{ this function works as follows
  Give it a comment text (psComment)
  and it returns
  - True if the comment is a special JCF flags comment
  psError is empty if the flags could be parsed, else contains an error message
  psFlags returns the set of flags referenced
  pbOn tells if they were turned on or off
}
function ReadCommentJcfFlags(psComment: string; out psError: string;
  out peFlags: TFormatFlags; out pbOn: boolean): boolean;
var
  lsPrefix, lsRest: string;
  lsSetting, lsState: string;
  lbFlagFound: boolean;
  liLoop:      integer;
begin
  Result  := False;
  psError := '';

  // translate {(*} comments to jcf:format=on comments
  if psComment = OLD_NOFORMAT_ON then
    psComment := NOFORMAT_ON
  else if psComment = OLD_NOFORMAT_OFF then
    psComment := NOFORMAT_OFF;

  { all comments without the required prefix are of no import to this code
    if it's not one, then exit without error }
  lsPrefix := StrLeft(psComment, 6);
  if not (AnsiSameText(lsPrefix, FORMAT_COMMENT_PREFIX)) then
    exit;

  // should be a valid jcf flag directive after here
  Result := True;
  lsRest := Trim(StrRestOf(psComment, 7));

  { rest should read <setting>=<state>
    where the setting is one of the format flags, and the state is 'on' or 'off'
  }
  lsSetting := Trim(StrBefore('=', lsRest));
  lsState   := Trim(StrAfter('=', lsRest));

  { is the comment well formed? }
  if (lsSetting = '') or (lsState = '') then
  begin
    psError := 'Comment ' + StrDoubleQuote(psComment) +
      ' has prefix but cannot be parsed';
    exit;
  end;

  { try and get a state flag from the string, abort if it fails }
  try
    pbOn := LStrToBoolean(lsState);
  except
    On EJcfConversionError do
    begin
      psError := 'In comment ' + StrDoubleQuote(psComment) + ' , ' +
        ' state ' + StrDoubleQuote(lsState) + ' cannot be parsed to either on or off';
      exit;
    end
    else
      raise;
  end;

  lbFlagFound := False;

  // accept jcf:all=on to reset state to normal by removing all flags
  if AnsiSameText(lsSetting, 'all') then
  begin
    peFlags     := ALL_FLAGS;
    lbFlagFound := True;
  end
  else
  begin
    { match the setting from the table }
    for liLoop := low(FORMAT_FLAG_NAMES) to high(FORMAT_FLAG_NAMES) do
    begin
      if AnsiSameText(lsSetting, FORMAT_FLAG_NAMES[liLoop].sName) then
      begin
        peFlags     := FORMAT_FLAG_NAMES[liLoop].eFlags;
        lbFlagFound := True;
        break;
      end;
    end;
  end;

  if not lbFlagFound then
  begin
    // unknown setting - nothing to do except log a message
    psError := 'In comment ' + StrDoubleQuote(psComment) + ' , ' +
      ' setting ' + StrDoubleQuote(lsSetting) + ' is not known';
    exit;
  end;
end;

end.