File: FormatFlags.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (265 lines) | stat: -rw-r--r-- 8,124 bytes parent folder | download
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
254
255
256
257
258
259
260
261
262
263
264
265
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
------------------------------------------------------------------------------*)
{*)}

{$mode delphi}

interface

uses
  SysUtils;

type

  TFormatFlag = (
    eParse,      //must be the first item.
    eAllFormat,  //must be the second item.
    eObfuscate,
    eAddSpace, eRemoveSpace,
    eAddReturn, eRemoveReturn,
    eAlignVars, eAlignConst, eAlignTypeDef, eAlignAssign, eAlignComment, eAlignField,
    eCapsReservedWord, eCapsSpecificWord,
    eIndent, eLineBreaking, eBlockStyle,
    eWarning, eWarnAssignToFunctionName, eWarnCaseNoElse, eWarnEmptyBlock, eWarnUnusedParam,
    eFindReplace, eFindReplaceUses, eRemoveComments);


  { these flags control:

    parse: stop parsing tokens - to turn it on again need jcf:parse=on
    AllFormat: all clarify processes - turn the formatter as a whole on or off (except parse)
    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 = [eAllFormat..High(TFormatFlag)];

implementation

uses
  { local }
  JcfStringUtils, jcfbaseConsts;

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

const
  FORMAT_FLAG_NAMES: array[1..34] of TRFlagNameData =
    (
    (sName: 'parse'; eFlags: [eParse]),
    (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: 'warnassigntofunctionname'; eFlags: [eWarnAssignToFunctionName]),
    (sName: 'warncasenoelse'; eFlags: [eWarnCaseNoElse]),
    (sName: 'warnemptyblock'; eFlags: [eWarnEmptyBlock]),
    (sName: 'warnunusedparam'; eFlags: [eWarnUnusedParam]),
    (sName: 'findreplace'; eFlags: [eFindReplace]),
    (sName: 'findreplaceuses'; eFlags: [eFindReplaceUses]),

    (sName: 'removecomments'; 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 ps = 'on' then
    Result := True
  else if 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
  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;

  if length(psComment) <= FORMAT_COMMENT_PREFIX_LEN then
    exit;
  if (psComment[3] <> 'j') and (psComment[3] <> 'J') then
    exit;
  psComment := LowerCase(psComment);

  { all comments without the required prefix are of no import to this code
    if it's not one, then exit without error }
  if not CompareMem(@psComment[1], @FORMAT_COMMENT_PREFIX[1], FORMAT_COMMENT_PREFIX_LEN) 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 := Format(lisMsgCommentHasPrefixButCannotBeParsed, [psComment]);
    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 := Format(lisMsgInCommentStateCannotBeParsedToOnOff, [psComment, lsState]);
      exit;
    end
    else
      raise;
  end;

  lbFlagFound := False;

  // accept jcf:all=on to reset state to normal by removing all flags
  if 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 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 := Format(lisMsgInCommentSettingIsNotKnown, [psComment, lsSetting]);
    exit;
  end;
end;

end.