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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541 | ------------------------------------------------------------------------------
-- --
-- GNATPP COMPONENTS --
-- --
-- G N A T P P . D I C T I O N A R I E S --
-- --
-- B o d y --
-- --
-- Copyright (C) 2001-2017, AdaCore --
-- --
-- GNATPP is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNATPP is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI- --
-- LITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with GNAT; see file COPYING. If not, --
-- write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
-- Boston, --
-- --
-- GNATPP is maintained by AdaCore (http://www.adacore.com) --
-- --
------------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Containers.Hashed_Sets; use Ada.Containers;
package body Pp.Formatting.Dictionaries is
package Syms renames Utils.Symbols;
subtype Symbol is Syms.Symbol;
subtype Opt_Symbol is Syms.Opt_Symbol;
package Name_Sets is new Ada.Containers.Hashed_Sets (Symbol,
Syms.Hash_Symbol, Syms.Case_Insensitive_Equal, Syms."=");
use Name_Sets;
subtype Name_Set is Name_Sets.Set;
Whole_Word_Exceptions, Subword_Exceptions : Name_Set;
type Opt_Casing_Exception_Kinds is
(Not_A_Casing_Exception, -- Wrong syntax of the exception string
Whole_Word, -- Name to be replaced as a whole
Subword);
subtype Casing_Exception_Kinds is
Opt_Casing_Exception_Kinds range Whole_Word .. Subword;
-- Subword is a part of the name delimited by '_' or by the beginning or
-- end of the word and which does not contain any '_' inside.
-----------------------
-- Local subprograms --
-----------------------
procedure Add_To_Dictionary
(Name : String; Exception_Kind : Casing_Exception_Kinds);
-- If Name does not exist in the dictionary, adds the corresponding
-- dictionary entry. Otherwise replace the casing defined by the
-- existing occurrence of this name by the casing given by Name
function Find_In_Dictionary
(Name : String; Exception_Kind : Casing_Exception_Kinds)
return Opt_Symbol;
-- Tries to find in the dictionary the entry which corresponds to Name
-- without taking into account the character casing. (Exception_Kind
-- is used to limit the search by the corresponding kind of dictionary
-- entries). Return the Id of the corresponding dictionary entry, returns
-- No_String if the dictionary does not contain such an entry.
procedure Scan_Dictionary (Dictionary_Name : String);
-- Scans the named dictionary file
-----------------------
-- Add_To_Dictionary --
-----------------------
procedure Add_To_Dictionary
(Name : String; Exception_Kind : Casing_Exception_Kinds)
is
Id : constant Symbol := Syms.Intern (Name);
begin
case Exception_Kind is
when Whole_Word =>
Include (Whole_Word_Exceptions, Id);
when Subword =>
Include (Subword_Exceptions, Id);
end case;
end Add_To_Dictionary;
---------------------------
-- Check_With_Dictionary --
---------------------------
procedure Check_With_Dictionary
(Ada_Name : in out Wide_String; Casing : PP_Casing)
is
Name : String := To_String (Ada_Name);
Name_Last : constant Natural := Name'Last;
SW_Start : Integer := Name'First;
SW_End : Integer := Name_Last;
-- Indexes of a subword in the Name
Dictionary_String : Opt_Symbol;
procedure Set_Subword;
-- Provided that Name has subwords, and that the current settings of
-- SW_Start and SW_End point to some subword, sets these indexes to
-- point to the next subword. Set SW_Start and SW_End to 0 if there
-- are no more subwords.
-- This procedure does not check if we have one more subword to move
-- these indexes to.
function Capitalize_Subword
(SW : String; Casing : PP_Casing) return String;
-- Supposing that SW is a (sub)word having no '_' inside, returns
-- the capitalized version of this subword according to the casing
-- represented by Casing.
-----------------
-- Set_Subword --
-----------------
procedure Set_Subword is
begin
if SW_End = Name_Last then
-- There is no more subwords
SW_Start := 0;
SW_End := 0;
else
SW_Start := SW_End + 2;
SW_End := Name_Last;
for J in SW_Start + 1 .. SW_End loop
if Name (J) = '_' then
SW_End := J - 1;
exit;
end if;
end loop;
end if;
end Set_Subword;
------------------------
-- Capitalize_Subword --
------------------------
function Capitalize_Subword
(SW : String; Casing : PP_Casing) return String
is
Result : String := SW;
First_Idx : constant Natural := Result'First;
begin
case Casing is
when Lower_Case =>
Result := To_Lower (Result);
when Upper_Case =>
Result := To_Upper (Result);
when Mixed =>
Result := To_Lower (Result);
Result (First_Idx) := To_Upper (Result (First_Idx));
when As_Declared =>
-- Nothing to do!
null;
end case;
return Result;
end Capitalize_Subword;
begin -- Check_With_Dictionary
for J in Name'Range loop
if Name (J) = '_' then
SW_End := J - 1;
exit;
end if;
end loop;
Dictionary_String :=
Find_In_Dictionary (Name => Name, Exception_Kind => Whole_Word);
if not Syms.Present (Dictionary_String) then
-- May be we can apply the subword exception to the whole word
Dictionary_String :=
Find_In_Dictionary (Name => Name, Exception_Kind => Subword);
end if;
if Syms.Present (Dictionary_String) then
Name := Syms.Str (Dictionary_String).S;
else
if SW_End < Name'Last then
-- That is, the whole word is not in the dictionary and it has at
-- least two subwords, and Name (SW_Start .. SW) is the first one
while SW_End /= 0 loop
Dictionary_String :=
Find_In_Dictionary
(Name => Name (SW_Start .. SW_End),
Exception_Kind => Subword);
if Syms.Present (Dictionary_String) then
Name (SW_Start .. SW_End) := Syms.Str (Dictionary_String).S;
else
Name (SW_Start .. SW_End) :=
Capitalize_Subword (Name (SW_Start .. SW_End), Casing);
end if;
Set_Subword;
end loop;
else
-- The case of a word with no subwords, the word is not in the
-- dictionary
Name := Capitalize_Subword (Name, Casing);
end if;
end if;
Ada_Name := To_Wide_String (Name);
end Check_With_Dictionary;
------------------------
-- Find_In_Dictionary --
------------------------
function Find_In_Dictionary
(Name : String; Exception_Kind : Casing_Exception_Kinds) return Opt_Symbol
is
Id : constant Symbol := Syms.Intern (Name);
C : constant Name_Sets.Cursor :=
(case Exception_Kind is
when Whole_Word => Find (Whole_Word_Exceptions, Id),
when Subword => Find (Subword_Exceptions, Id));
begin
return (if Has_Element (C) then Element (C) else Syms.No_Symbol);
end Find_In_Dictionary;
-----------------------
-- Scan_Dictionaries --
-----------------------
procedure Scan_Dictionaries (Dictionary_File_Names : String_Ref_Array) is
pragma Assert (Is_Empty (Whole_Word_Exceptions));
pragma Assert (Is_Empty (Subword_Exceptions));
begin
for D_Name of Dictionary_File_Names loop
if D_Name.all /= "-" then -- "--dictionary-" means no predef casing
Dictionaries.Scan_Dictionary (D_Name.all);
end if;
end loop;
end Scan_Dictionaries;
---------------------
-- Scan_Dictionary --
---------------------
procedure Scan_Dictionary (Dictionary_Name : String) is
String_Buffer_Max_Len : constant Natural := 1_024;
-- Should be enough, I hope...
String_Buffer : String (1 .. String_Buffer_Max_Len);
Len : Natural range 0 .. String_Buffer_Max_Len := 0;
-- The length of the dictionary file line which is being processed
Line_Num : Natural := 0;
-- The number of the currently processed line
Dictionary_File : File_Type;
procedure Process_Dictionary_File_Line;
-- Reads the next line from the dictionary file, parses it, and
-- if founds the new definition of the casing exception, puts the
-- corresponding word in the exception table
----------------------------------
-- Process_Dictionary_File_Line --
----------------------------------
procedure Process_Dictionary_File_Line is
Start_Word : Natural := 0;
End_Word : Natural := 0;
Exc_Kind : Opt_Casing_Exception_Kinds;
function Skip_White_Spaces (Idx : Natural) return Natural;
-- Starting from Idx (which is treated as an index in String_Buffer
-- bounded by the current value of Len), computes the index of the
-- first non-blank character. If there is no non-blank character on
-- the right from Idx, or if the actual is greater than Len returns
-- zero.
function Skip_Non_Space_Chars (Idx : Natural) return Natural;
-- Starting from Idx (which is treated as an index in String_Buffer
-- bounded by the current value of Len, Idx is supposed to point to
-- non-blank character), return the index of the character preceding
-- the first white space character (or Ada comment beginning) being
-- on the right of Idx (returns Len if there is no such white space).
function Get_Exception_Kind return Opt_Casing_Exception_Kinds;
-- Checks if String_Buffer (Start_Word .. End_Word) has a syntax of
-- a casing exception and return the corresponding exception kind.
-- Returns Not_A_Casing_Exception if this word cannot be interpreted
-- as a casing exception. As a side effect, this function may correct
-- the values of Start_Word and End_Word to skip '*' in case of a
-- subword.
function Is_White_Space (Ch : Character) return Boolean;
-- Checks if Ch is a white space
--------------------
-- Is_White_Space --
--------------------
function Is_White_Space (Ch : Character) return Boolean is
begin
return False or else Ch = ' ' or else Ch = ASCII.HT;
end Is_White_Space;
-----------------------
-- Skip_White_Spaces --
-----------------------
function Skip_White_Spaces (Idx : Natural) return Natural is
Result : Natural := Idx;
begin
while Is_White_Space (String_Buffer (Result)) and then Result < Len
loop
Result := Result + 1;
end loop;
if Result > Len
or else
(Result = Len and then Is_White_Space (String_Buffer (Result)))
then
Result := 0;
end if;
return Result;
end Skip_White_Spaces;
--------------------------
-- Skip_Non_Space_Chars --
--------------------------
function Skip_Non_Space_Chars (Idx : Natural) return Natural is
Result : Natural := Idx;
begin
while Result < Len
and then not
(Is_White_Space (String_Buffer (Result))
or else
(String_Buffer (Result) = '-'
and then String_Buffer (Result + 1) = '-'))
loop
Result := Result + 1;
end loop;
if Is_White_Space (String_Buffer (Result))
or else String_Buffer (Result) = '-'
then
Result := Result - 1;
end if;
return Result;
end Skip_Non_Space_Chars;
------------------------
-- Get_Exception_Kind --
------------------------
function Get_Exception_Kind return Opt_Casing_Exception_Kinds is
Result : Opt_Casing_Exception_Kinds;
Prev_Char_Is_Underline : Boolean := False;
begin
if String_Buffer (Start_Word) = '*'
and then String_Buffer (End_Word) = '*'
then
Result := Subword;
Start_Word := Start_Word + 1;
End_Word := End_Word - 1;
else
Result := Whole_Word;
end if;
-- And now we have to check that String_Buffer (First_Idx ..
-- Last_Idx) has a syntax of an identifier
if Start_Word > End_Word or else String_Buffer (Start_Word) = '_'
or else String_Buffer (End_Word) = '_'
then
Result := Not_A_Casing_Exception;
else
for J in Start_Word .. End_Word loop
if Is_Alphanumeric (String_Buffer (J)) then
Prev_Char_Is_Underline := False;
elsif String_Buffer (J) = '_' then
if not Prev_Char_Is_Underline then
Prev_Char_Is_Underline := True;
if Result = Subword then
Result := Not_A_Casing_Exception;
exit;
end if;
else
Result := Not_A_Casing_Exception;
exit;
end if;
else
Result := Not_A_Casing_Exception;
exit;
end if;
end loop;
end if;
return Result;
end Get_Exception_Kind;
begin -- Process_Dictionary_File_Line
Get_Line (Dictionary_File, String_Buffer, Len);
if Len = 0 then
-- This is an empty line
return;
end if;
Start_Word := Skip_White_Spaces (1);
if Start_Word = 0
or else
(Start_Word < Len and then String_Buffer (Start_Word) = '-'
and then String_Buffer (Start_Word + 1) = '-')
then
-- blank or comment line
return;
end if;
End_Word := Skip_Non_Space_Chars (Start_Word);
Exc_Kind := Get_Exception_Kind;
if Exc_Kind = Not_A_Casing_Exception then
Ada.Text_IO.Put_Line
(Standard_Error,
Dictionary_Name & ':' & Image (Line_Num) & ':' &
Image (Start_Word) &
": wrong syntax of a casing exception, line ignored");
else
Add_To_Dictionary
(String_Buffer (Start_Word .. End_Word), Exc_Kind);
-- We have to check if we have something else in the dictionary
-- file line. The only possible things are blank characters and
-- comments
if End_Word < Len and then String_Buffer (End_Word + 1) = '*' then
-- Taking into account the side effect of Get_Exception_Kind
End_Word := End_Word + 1;
end if;
if End_Word < Len then
-- We have something else in this line
Start_Word := Skip_White_Spaces (End_Word + 1);
if not
(Start_Word = 0
or else
(Start_Word < Len and then String_Buffer (Start_Word) = '-'
and then String_Buffer (Start_Word + 1) = '-'))
then
Ada.Text_IO.Put_Line
(Standard_Error,
Dictionary_Name & ':' & Image (Line_Num) & ':' &
Image (Start_Word) &
": only one casing exception per line is allowed");
Ada.Text_IO.Put_Line
(Standard_Error,
Dictionary_Name & ':' & Image (Line_Num) & ':' &
Image (Start_Word) & ": end of line ignored");
end if;
end if;
end if;
end Process_Dictionary_File_Line;
begin -- Scan_Dictionary
-- First trying to open the dictionary file: ???It would be cleaner to
-- keep the file opening and error message handling in gnatpp.
begin
Open
(File => Dictionary_File, Mode => In_File, Name => Dictionary_Name);
exception
when Name_Error =>
Ada.Text_IO.Put_Line
(Standard_Error,
"gnatpp: dictionary file not found: " & Dictionary_Name);
return;
when Status_Error =>
Ada.Text_IO.Put_Line
(Standard_Error,
"gnatpp: cannot open dictionary file: " & Dictionary_Name);
Ada.Text_IO.Put_Line
(Standard_Error,
" the file may be used by another process");
return;
end;
while not End_Of_File (Dictionary_File) loop
Line_Num := Line_Num + 1;
Process_Dictionary_File_Line;
end loop;
if Is_Open (Dictionary_File) then
Close (Dictionary_File);
end if;
end Scan_Dictionary;
end Pp.Formatting.Dictionaries;
|