Debian Codegraph @

progs/arm_indx.adb

raw
1 with -- ARM_Output, redundant with spec
2 ARM_Contents,
3 Ada.Exceptions,
4 Ada.Characters.Handling,
5 Ada.Strings.Fixed,
6 Ada.Text_IO,
7 Ada.Calendar,
8 Ada.Unchecked_Deallocation;
9 package body ARM_Index is
10
11 --
12 -- Ada reference manual formatter (ARM_Form).
13 --
14 -- This package contains the routines to manage and generate the index.
15 --
16 -- ---------------------------------------
17 -- Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2010, 2011, 2012
18 -- AXE Consultants. All rights reserved.
19 -- P.O. Box 1512, Madison WI 53701
20 -- E-Mail: [email protected]
21 --
22 -- ARM_Form is free software: you can redistribute it and/or modify
23 -- it under the terms of the GNU General Public License version 3
24 -- as published by the Free Software Foundation.
25 --
26 -- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS"
27 -- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY,
28 -- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL.
29 -- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL,
30 -- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES,
31 -- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
32 -- DAMAGES.
33 --
34 -- A copy of the GNU General Public License is available in the file
35 -- gpl-3-0.txt in the standard distribution of the ARM_Form tool.
36 -- Otherwise, see <http://www.gnu.org/licenses/>.
37 --
38 -- If the GPLv3 license is not satisfactory for your needs, a commercial
39 -- use license is available for this tool. Contact Randy at AXE Consultants
40 -- for more information.
41 --
42 -- ---------------------------------------
43 --
44 -- Edit History:
45 --
46 -- 5/28/00 - RLB - Created package.
47 -- 8/ 7/00 - RLB - Improved sorting of entries.
48 -- 1/31/02 - RLB - Added missing with of Unchecked_Deallocation.
49 -- 4/11/03 - RLB - Added a hard space in the indexing routine, in order
50 -- that the empty paragraph isn't ignored.
51 -- 9/09/04 - RLB - Removed unused junk noted by Stephen Leake.
52 -- 10/28/05 - RLB - Added key reuse.
53 -- 10/30/05 - RLB - Added subtype declaration.
54 -- 1/16/06 - RLB - Fixed so a letter header is output if the first
55 -- indexed item starts with a letter.
56 -- 2/17/06 - RLB - Added Remove_Soft_Hyphens flag to Clean (for output).
57 -- 9/22/06 - RLB - Changed to use Clause_Number_Type.
58 -- 2/13/07 - RLB - Changed Start_Paragraph to use explicit indents.
59 -- 12/19/07 - RLB - Revised Text_Format calls.
60 -- 3/31/10 - RLB - Fixed sorting to ignore embedded commands (like
61 -- soft hyphens).
62 -- 10/18/11 - RLB - Changed to GPLv3 license.
63 -- 3/12/12 - RLB - Lengthened indexing so that
64 -- "Ada.Strings.Wide_Wide_@!Unbounded.Wide_Wide_@!Equal_Case_Insensitive"
65 -- would fit. (Don't ask.)
66 -- 8/31/12 - RLB - Fixed to avoid reading uninitialized section numbers.
67
68 Next_Index_Key : Index_Key;
69
70 type Term_Type;
71 type Term_Ptr is access Term_Type;
72 type Term_Type is record
73 Clause : String (1..10);
74 Clause_Len : Natural;
75 Clause_Number : ARM_Contents.Clause_Number_Type;
76 Paragraph : String (1..10);
77 Paragraph_Len : Natural;
78 Term : String (1..72);
79 Term_Len : Natural;
80 Subterm : String (1..80);
81 Subterm_Len : Natural;
82 Kind : Index_Item_Kind_Type;
83 Key : Index_Key;
84 Next : Term_Ptr;
85 end record;
86
87 procedure Free is new Ada.Unchecked_Deallocation (Term_Type, Term_Ptr);
88
89 Index_List : Term_Ptr;
90 Term_Count : Natural := 0;
91
92 procedure Create is
93 -- Initialize this package.
94 begin
95 Index_List := null;
96 Next_Index_Key := 1001;
97 Term_Count := 0;
98 end Create;
99
100
101 procedure Destroy is
102 -- Finalize this package; make sure the index is empty.
103 TTemp : Term_Ptr;
104 begin
105 while Index_List /= null loop
106 TTemp := Index_List;
107 Index_List := TTemp.Next;
108 Free (TTemp);
109 end loop;
110 end Destroy;
111
112
113 function Clean (Item : in String;
114 Remove_Soft_Hyphens : in Boolean) return String is
115 -- Remove any commands from Item. (Except for soft hyphens
116 -- if Remove_Soft_Hyphens is False.) We keep the contents of any
117 -- commands (we assume the commands are basic formatting).
118 Result : String (1 .. Item'Length);
119 Len : Natural := 0;
120 In_Command : Boolean := False;
121 Skip_Next_Char : Boolean := False;
122 Close_Char : Character := ' ';
123 begin
124 for I in Item'Range loop
125 if Item(I) = '@' then
126 if I < Item'Last and then
127 (Item(I+1) = '!' or else -- Soft hyphen
128 Item(I+1) = '|' or else -- Soft linebreak
129 Item(I+1) = ';' or else -- "nothing"
130 Item(I+1) = '\' or else -- tab
131 Item(I+1) = ' ' or else -- hard space
132 Item(I+1) = '*') then -- hard return
133 if Remove_Soft_Hyphens then
134 Skip_Next_Char := True;
135 else
136 -- Allow soft hyphens and other simple commands.
137 Len := Len + 1;
138 Result(Len) := Item(I);
139 end if;
140 elsif I < Item'Last and then
141 Item(I+1) = '@' then
142 -- Literal '@' command.
143 if Remove_Soft_Hyphens then
144 Len := Len + 1;
145 Result(Len) := '@';
146 Skip_Next_Char := True;
147 else
148 -- Leave these markers, as we'll format the
149 -- resulting string.
150 Len := Len + 1;
151 Result(Len) := Item(I);
152 end if;
153 else
154 In_Command := True;
155 Close_Char := ' ';
156 -- Skip it.
157 end if;
158 elsif Skip_Next_Char then
159 Skip_Next_Char := False;
160 elsif In_Command then
161 if Item(I) = '{' then
162 Close_Char := '}';
163 In_Command := False; -- Finished command header.
164 elsif Item(I) = '[' then
165 Close_Char := ']';
166 In_Command := False;
167 elsif Item(I) = '(' then
168 Close_Char := ')';
169 In_Command := False;
170 elsif Item(I) = '<' then
171 Close_Char := '>';
172 In_Command := False;
173 elsif Item(I) = '`' then
174 Close_Char := ''';
175 In_Command := False;
176 elsif Ada.Characters.Handling.Is_Alphanumeric (Item(I)) then
177 -- Skip character (part of the command name).
178 null;
179 else -- End of parameterless command (note: '@' already
180 -- was recognized).
181 Close_Char := ' ';
182 In_Command := False;
183 Len := Len + 1;
184 Result(Len) := Item(I);
185 end if;
186 elsif Close_Char /= ' ' and then
187 Close_Char = Item(I) then
188 -- Skip the close character.
189 Close_Char := ' ';
190 elsif Item(I) = Ascii.LF then
191 Len := Len + 1;
192 Result(Len) := ' ';
193 else
194 Len := Len + 1;
195 Result(Len) := Item(I);
196 end if;
197 end loop;
198 return Result (1..Len);
199 end Clean;
200
201
202 function Get_Key return Index_Key is
203 -- Returns a Key value to refer to one or more index entries
204 -- (for a single entity).
205 Temp : Index_Key := Next_Index_Key;
206 begin
207 Next_Index_Key := Next_Index_Key + 1;
208 return Temp;
209 end Get_Key;
210
211
212 procedure Add_Reusing_Key (Term : in String;
213 Subterm : in String := "";
214 Kind : in Index_Item_Kind_Type := Primary_Term;
215 Clause : in String := "";
216 Paragraph : in String := "";
217 Key : in Index_Key) is
218 -- Add an index reference to the index, (re)using the specified Key
219 -- to refer to this index entry. Key must have previously
220 -- returned by Add or Get_Key.
221 Temp_Term : Term_Type;
222 CTerm : constant String := Clean(Term, Remove_Soft_Hyphens => False);
223 CSubterm : constant String := Clean(Subterm, Remove_Soft_Hyphens => False);
224 begin
225 Temp_Term.Kind := Kind;
226 Ada.Strings.Fixed.Move (Target => Temp_Term.Term,
227 Source => CTerm,
228 Drop => Ada.Strings.Error,
229 Pad => ' ');
230 Temp_Term.Term_Len := CTerm'Length;
231 if Kind /= Primary_Term_and_Subterm and then
232 Kind /= Partial_Term_with_Subterm and then
233 Kind /= Child_Unit_Parent and then
234 Kind /= Declaration_in_Package and then
235 Kind /= Subdeclaration_in_Package and then
236 Kind /= Subtype_Declaration_in_Package and then
237 Kind /= See_Term and then
238 Kind /= See_Also_Term and then
239 Kind /= See_Other_Term and then
240 Kind /= See_Also_Other_Term then
241 if Subterm /= "" then
242 Ada.Exceptions.Raise_Exception (Not_Valid_Error'Identity,
243 "Subterm used in kind without subterm");
244 else
245 Temp_Term.Subterm_Len := 0;
246 end if;
247 else
248 Ada.Strings.Fixed.Move (Target => Temp_Term.Subterm,
249 Source => CSubterm,
250 Drop => Ada.Strings.Error,
251 Pad => ' ');
252 Temp_Term.Subterm_Len := CSubterm'Length;
253 end if;
254 if (Kind = See_Other_Term or else
255 Kind = See_Also_Other_Term) then
256 if Clause /= "" or else Paragraph /= "" then
257 raise Not_Valid_Error with
258 "Clause used in kind without reference";
259 else
260 Temp_Term.Clause_Len := 0;
261 Temp_Term.Paragraph_Len := 0;
262 -- Note: These have no clause numbers.
263 end if;
264 else
265 Ada.Strings.Fixed.Move (Target => Temp_Term.Clause,
266 Source => Clause,
267 Drop => Ada.Strings.Error,
268 Pad => ' ');
269 Temp_Term.Clause_Len := Clause'Length;
270 Ada.Strings.Fixed.Move (Target => Temp_Term.Paragraph,
271 Source => Paragraph,
272 Drop => Ada.Strings.Error,
273 Pad => ' ');
274 Temp_Term.Paragraph_Len := Paragraph'Length;
275 ARM_Contents.Make_Clause (Clause, Temp_Term.Clause_Number);
276
277 end if;
278 Temp_Term.Key := Key;
279
280 Temp_Term.Next := Index_List;
281 Index_List := new Term_Type'(Temp_Term);
282 Term_Count := Term_Count + 1;
283 exception
284 when Ada.Strings.Length_Error =>
285 Ada.Text_IO.Put_Line ("**** Index doesn't fit: Term: " & CTerm &
286 " [Subterm: " & CSubterm & "]");
287 end Add_Reusing_Key;
288
289
290 procedure Add (Term : in String;
291 Subterm : in String := "";
292 Kind : in Index_Item_Kind_Type := Primary_Term;
293 Clause : in String := "";
294 Paragraph : in String := "";
295 Key : out Index_Key) is
296 -- Add an index reference to the index. Returns a Key value to
297 -- refer to this index entry.
298 -- Raises Not_Valid_Error if Subterm, Clause, or Paragraph is not
299 -- empty when the kind does not use it.
300 begin
301 Key := Get_Key;
302 Add_Reusing_Key (Term, Subterm, Kind, Clause, Paragraph, Key);
303 end Add;
304
305
306 procedure Generate_Index_Body (Output_Object : in out ARM_Output.Output_Type'Class;
307 Use_Paragraphs : in Boolean := True) is
308 -- Generate the index body. (The section header has already been
309 -- generated). References include paragraph numbers if Use_Paragraphs
310 -- is true.
311 -- Note: This should not leave us in a paragraph.
312
313 Temp : Term_Ptr;
314 Last : Term_Ptr := null;
315 Items : array (1..Term_Count) of Term_Ptr;
316 Keep_Set : Boolean := False;
317
318 Start : Ada.Calendar.Time := Ada.Calendar.Clock;
319
320 function To_Lower (A : in String) return String renames
321 Ada.Characters.Handling.To_Lower;
322 function To_Lower (A : in Character) return Character renames
323 Ada.Characters.Handling.To_Lower;
324
325 procedure Clause_Ref (Item : Term_Ptr) is
326 -- Generate a clause reference:
327 begin
328 if Item.Clause_Len > 5 and then Item.Clause (1..5) = "Annex" then
329 -- Strip off the "Annex".
330 if Use_Paragraphs and then Item.Paragraph_Len /= 0 then
331 ARM_Output.Index_Reference (Output_Object,
332 Text => Item.Clause (Item.Clause_Len) & '(' &
333 Item.Paragraph (1..Item.Paragraph_Len) & ')',
334 Index_Key => Item.Key,
335 Clause_Number => Item.Clause (1..Item.Clause_Len));
336 else
337 ARM_Output.Index_Reference (Output_Object,
338 Text => Item.Clause (Item.Clause_Len) & "",
339 Index_Key => Item.Key,
340 Clause_Number => Item.Clause (1..Item.Clause_Len));
341 end if;
342 elsif Use_Paragraphs and then Item.Paragraph_Len /= 0 then
343 ARM_Output.Index_Reference (Output_Object,
344 Text => Item.Clause (1..Item.Clause_Len) & '(' &
345 Item.Paragraph (1..Item.Paragraph_Len) & ')',
346 Index_Key => Item.Key,
347 Clause_Number => Item.Clause (1..Item.Clause_Len));
348 else
349 ARM_Output.Index_Reference (Output_Object,
350 Text => Item.Clause (1..Item.Clause_Len),
351 Index_Key => Item.Key,
352 Clause_Number => Item.Clause (1..Item.Clause_Len));
353 end if;
354 end Clause_Ref;
355
356 procedure Italic_Text (Text : in String) is
357 begin
358 ARM_Output.Text_Format (Output_Object,
359 Format => (Bold => False, Italic => True,
360 Font => ARM_Output.Default,
361 Size => 0, Color => ARM_Output.Default,
362 Change => ARM_Output.None,
363 Version | Added_Version => '0',
364 Location => ARM_Output.Normal));
365 ARM_Output.Ordinary_Text (Output_Object, Text);
366 ARM_Output.Text_Format (Output_Object,
367 Format => (Bold => False, Italic => False,
368 Font => ARM_Output.Default,
369 Size => 0, Color => ARM_Output.Default,
370 Change => ARM_Output.None,
371 Version | Added_Version => '0',
372 Location => ARM_Output.Normal));
373 end Italic_Text;
374
375 procedure Term_Text (Text : in String) is
376 A_Soft_Hyphen : Natural := Ada.Strings.Fixed.Index (Text, "@!");
377 begin
378 if A_Soft_Hyphen = 0 then
379 ARM_Output.Ordinary_Text (Output_Object, Text);
380 else
381 ARM_Output.Ordinary_Text (Output_Object, Text(Text'First .. A_Soft_Hyphen-1));
382 ARM_Output.Soft_Hyphen_Break (Output_Object);
383 Term_Text (Text(A_Soft_Hyphen+2 .. Text'Last)); -- In case there is more than one soft hyphen.
384 end if;
385 end Term_Text;
386
387 procedure New_Kind (Item : Term_Ptr; Reset_Keep : in Boolean) is
388 -- Generate and item of a new kind. Note that the term has already
389 -- been generated (at some point).
390 begin
391 case Item.Kind is
392 when Primary_Term =>
393 -- ** Must be first, so can't get here.
394 Italic_Text ("*SORT ERROR*");
395 ARM_Output.Hard_Space (Output_Object);
396 ARM_Output.Hard_Space (Output_Object);
397 ARM_Output.Ordinary_Character (Output_Object, ' ');
398 Clause_Ref (Item);
399
400 when Partial_Term =>
401 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
402 ARM_Output.Hard_Space (Output_Object);
403 ARM_Output.Hard_Space (Output_Object);
404 ARM_Output.Hard_Space (Output_Object);
405 ARM_Output.Ordinary_Character (Output_Object, '[');
406 Italic_Text ("partial");
407 ARM_Output.Ordinary_Character (Output_Object, ']');
408 ARM_Output.Hard_Space (Output_Object);
409 ARM_Output.Hard_Space (Output_Object);
410 ARM_Output.Ordinary_Character (Output_Object, ' ');
411 Clause_Ref (Item);
412
413 when Primary_Term_and_Subterm =>
414 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
415 ARM_Output.Hard_Space (Output_Object);
416 ARM_Output.Hard_Space (Output_Object);
417 ARM_Output.Hard_Space (Output_Object);
418 Term_Text (Item.Subterm (1..Item.Subterm_Len));
419 ARM_Output.Hard_Space (Output_Object);
420 ARM_Output.Hard_Space (Output_Object);
421 ARM_Output.Ordinary_Character (Output_Object, ' ');
422 Clause_Ref (Item);
423
424 when Partial_Term_with_Subterm =>
425 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
426 ARM_Output.Hard_Space (Output_Object);
427 ARM_Output.Hard_Space (Output_Object);
428 ARM_Output.Hard_Space (Output_Object);
429 Term_Text (Item.Subterm (1..Item.Subterm_Len));
430 --[The "partial" does not seem to appear on subterms.]
431 --ARM_Output.Hard_Space (Output_Object);
432 --ARM_Output.Ordinary_Character (Output_Object, '[');
433 --Italic_Text ("partial");
434 --ARM_Output.Ordinary_Character (Output_Object, ']');
435 ARM_Output.Hard_Space (Output_Object);
436 ARM_Output.Hard_Space (Output_Object);
437 ARM_Output.Ordinary_Character (Output_Object, ' ');
438 Clause_Ref (Item);
439
440 when Syntax_NT_Used =>
441 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
442 ARM_Output.Hard_Space (Output_Object);
443 ARM_Output.Hard_Space (Output_Object);
444 ARM_Output.Hard_Space (Output_Object);
445 Italic_Text ("used");
446 ARM_Output.Hard_Space (Output_Object);
447 ARM_Output.Hard_Space (Output_Object);
448 ARM_Output.Ordinary_Character (Output_Object, ' ');
449 Clause_Ref (Item);
450
451 when Child_Unit_Parent =>
452 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
453 ARM_Output.Hard_Space (Output_Object);
454 ARM_Output.Hard_Space (Output_Object);
455 ARM_Output.Hard_Space (Output_Object);
456 Italic_Text ("child of");
457 ARM_Output.Ordinary_Character (Output_Object, ' ');
458 Term_Text (Item.Subterm (1..Item.Subterm_Len));
459 ARM_Output.Hard_Space (Output_Object);
460 ARM_Output.Hard_Space (Output_Object);
461 ARM_Output.Ordinary_Character (Output_Object, ' ');
462 Clause_Ref (Item);
463
464 when Declaration_In_Package =>
465 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
466 ARM_Output.Hard_Space (Output_Object);
467 ARM_Output.Hard_Space (Output_Object);
468 ARM_Output.Hard_Space (Output_Object);
469 Italic_Text ("in");
470 ARM_Output.Ordinary_Character (Output_Object, ' ');
471 Term_Text (Item.Subterm (1..Item.Subterm_Len));
472 ARM_Output.Hard_Space (Output_Object);
473 ARM_Output.Hard_Space (Output_Object);
474 ARM_Output.Ordinary_Character (Output_Object, ' ');
475 Clause_Ref (Item);
476
477 when SubDeclaration_In_Package =>
478 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
479 ARM_Output.Hard_Space (Output_Object);
480 ARM_Output.Hard_Space (Output_Object);
481 ARM_Output.Hard_Space (Output_Object);
482 declare
483 In_Loc : Natural :=
484 Ada.Strings.Fixed.Index (Item.Subterm (1..Item.Subterm_Len),
485 " in ");
486 begin
487 if In_Loc = 0 then
488 -- Weird, "in" not found.
489 Term_Text (Item.Subterm (1..Item.Subterm_Len));
490 else
491 Term_Text (Item.Subterm (1 .. In_Loc));
492 Italic_Text ("in");
493 Term_Text (Item.Subterm (In_Loc+3 .. Item.Subterm_Len));
494 end if;
495 end;
496 ARM_Output.Hard_Space (Output_Object);
497 ARM_Output.Hard_Space (Output_Object);
498 ARM_Output.Ordinary_Character (Output_Object, ' ');
499 Clause_Ref (Item);
500
501 when Subtype_Declaration_In_Package =>
502 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
503 ARM_Output.Hard_Space (Output_Object);
504 ARM_Output.Hard_Space (Output_Object);
505 ARM_Output.Hard_Space (Output_Object);
506 Italic_Text ("in");
507 ARM_Output.Ordinary_Character (Output_Object, ' ');
508 Term_Text (Item.Subterm (1..Item.Subterm_Len));
509 ARM_Output.Hard_Space (Output_Object);
510 ARM_Output.Hard_Space (Output_Object);
511 ARM_Output.Ordinary_Character (Output_Object, ' ');
512 Clause_Ref (Item);
513
514 when See_Term =>
515 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
516 ARM_Output.Hard_Space (Output_Object);
517 ARM_Output.Hard_Space (Output_Object);
518 ARM_Output.Hard_Space (Output_Object);
519 Italic_Text ("See");
520 ARM_Output.Ordinary_Character (Output_Object, ' ');
521 Term_Text (Item.Subterm (1..Item.Subterm_Len));
522 ARM_Output.Hard_Space (Output_Object);
523 ARM_Output.Hard_Space (Output_Object);
524 ARM_Output.Ordinary_Character (Output_Object, ' ');
525 Clause_Ref (Item);
526
527 when See_Also_Term =>
528 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
529 ARM_Output.Hard_Space (Output_Object);
530 ARM_Output.Hard_Space (Output_Object);
531 ARM_Output.Hard_Space (Output_Object);
532 Italic_Text ("See also");
533 ARM_Output.Ordinary_Character (Output_Object, ' ');
534 Term_Text (Item.Subterm (1..Item.Subterm_Len));
535 ARM_Output.Hard_Space (Output_Object);
536 ARM_Output.Hard_Space (Output_Object);
537 ARM_Output.Ordinary_Character (Output_Object, ' ');
538 Clause_Ref (Item);
539
540 when See_Other_Term =>
541 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
542 ARM_Output.Hard_Space (Output_Object);
543 ARM_Output.Hard_Space (Output_Object);
544 ARM_Output.Hard_Space (Output_Object);
545 Italic_Text ("See");
546 ARM_Output.Ordinary_Character (Output_Object, ' ');
547 Term_Text (Item.Subterm (1..Item.Subterm_Len));
548 -- No clause reference here.
549
550 when See_Also_Other_Term =>
551 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => Reset_Keep);
552 ARM_Output.Hard_Space (Output_Object);
553 ARM_Output.Hard_Space (Output_Object);
554 ARM_Output.Hard_Space (Output_Object);
555 Italic_Text ("See also");
556 ARM_Output.Ordinary_Character (Output_Object, ' ');
557 Term_Text (Item.Subterm (1..Item.Subterm_Len));
558 -- No clause reference here.
559 end case;
560 end New_Kind;
561
562 function Is_Last_for_Term (Item : in Term_Ptr) return Boolean is
563 -- Returns True if this is the last line for Item's Term.
564 begin
565 if Item.Next = null then
566 return True;
567 elsif To_Lower (Item.Term (1..Item.Term_Len)) /= To_Lower (Item.Next.Term (1..Item.Next.Term_Len)) then
568 -- The next item has a different term.
569 return True;
570 elsif Temp.Kind /= Temp.Next.Kind then
571 -- The next item has a different kind, so another line will
572 -- be generated.
573 return False;
574 elsif To_Lower (Item.Subterm (1..Item.Subterm_Len)) /= To_Lower (Item.Next.Subterm (1..Item.Next.Subterm_Len)) then
575 -- The next item has a different subterm, so another line will
576 -- be generated.
577 return False;
578 else
579 -- The following term will just add another clause reference.
580 -- So we must look at the term following that:
581 return Is_Last_for_Term (Item.Next);
582 end if;
583 end Is_Last_for_Term;
584
585 begin
586 Ada.Text_IO.Put_Line (" -- Start index sorting - " & Natural'Image(Term_Count) & " items.");
587
588 -- Sort the items:
589
590 -- Load the items:
591 Temp := Index_List;
592 for I in Items'range loop
593 Items(I) := Temp;
594 Temp := Temp.Next;
595 end loop;
596
597 -- Sort the items array:
598 declare
599
600 function "<" (Left, Right : Term_Ptr) return Boolean is
601 function To_Lower (A : in String) return String renames
602 Ada.Characters.Handling.To_Lower;
603 use type ARM_Contents.Clause_Number_Type;
604
605 type Compare_Result is (Less, Greater, Equal);
606 function Compare (Left, Right : in String) return Compare_Result is
607 -- Compare two items; the compare is case insensitive and
608 -- ignores embedded commands.
609 begin
610 -- Simple cases in order to increase performance.
611 -- Check for null cases so we don't have to worry about them
612 -- later.
613 if Left = Right then
614 return Equal;
615 elsif Left'Length = 0 then
616 return Less; -- Right can't be null or they'd be equal.
617 elsif Right'Length = 0 then
618 return Greater;
619 end if;
620 -- Both strings are non-null.
621 if Left(Left'First) in '0'..'9' or else
622 Left(Left'First) in 'A'..'Z' then
623 if Right(Right'First) in '0'..'9' or else
624 Right(Right'First) in 'A'..'Z' then
625 if Left(Left'First) < Right(Right'First) then
626 return Less;
627 elsif Left(Left'First) > Right(Right'First) then
628 return Greater;
629 -- else continue below.
630 end if;
631 elsif Right(Right'First) in 'a'..'z' then
632 if Left(Left'First) < Character'Val(Character'Pos(Right(Right'First))+Character'Pos('A')-Character'Pos('a')) then
633 return Less;
634 elsif Left(Left'First) > Character'Val(Character'Pos(Right(Right'First))+Character'Pos('A')-Character'Pos('a')) then
635 return Greater;
636 end if;
637 -- else continue below.
638 end if;
639 elsif Left(Left'First) in 'a'..'z' then
640 if Right(Right'First) in '0'..'9' or else
641 Right(Right'First) in 'A'..'Z' then
642 if Character'Val(Character'Pos(Left(Left'First))+Character'Pos('A')-Character'Pos('a')) < Right(Right'First) then
643 return Less;
644 elsif Character'Val(Character'Pos(Left(Left'First))+Character'Pos('A')-Character'Pos('a')) > Right(Right'First) then
645 return Greater;
646 -- else continue below.
647 end if;
648 elsif Right(Right'First) in 'a'..'z' then -- Both lower.
649 if Left(Left'First) < Right(Right'First) then
650 return Less;
651 elsif Left(Left'First) > Right(Right'First) then
652 return Greater;
653 end if;
654 -- else continue below.
655 end if;
656 -- else continue below.
657 end if;
658
659 -- OK, do a full compare:
660 declare
661 -- %% Warning: This has terrible performance, too much heap use.
662 L : constant String := Clean (To_Lower (Left), Remove_Soft_Hyphens => True);
663 R : constant String := Clean (To_Lower (Right), Remove_Soft_Hyphens => True);
664 begin
665 --Ada.Text_IO.Put_Line("Full compare: Left=" & Left & "; Right=" & Right);
666 --Ada.Text_IO.Put_Line(" Clean Left=" & L & "; Clean Right=" & R);
667 if L < R then
668 --Ada.Text_IO.Put_Line(" Result=Less");
669 return Less;
670 elsif L > R then
671 --Ada.Text_IO.Put_Line(" Result=Greater");
672 return Greater;
673 else
674 --Ada.Text_IO.Put_Line(" Result=Equal");
675 return Equal;
676 end if;
677 end;
678 end Compare;
679 begin
680 -- We sort first on "Term", then on "Kind", then on "Subterm",
681 -- then on "Clause", and finally on "Paragraph".
682 case Compare (Left.Term (1..Left.Term_Len), Right.Term (1..Right.Term_Len)) is
683 when Less => return True;
684 when Greater => return False;
685 when Equal => null; -- Continue to next compare.
686 end case;
687 -- Partial_Term_with_Subterm and Primary_Term_and_Subterm
688 -- look identical, so they should compare equal for this
689 -- purpose:
690 if Left.Kind = Right.Kind or else
691 (Left.Kind = Partial_Term_with_Subterm and then
692 Right.Kind = Primary_Term_and_Subterm) or else
693 (Left.Kind = Primary_Term_and_Subterm and then
694 Right.Kind = Partial_Term_with_Subterm) then
695 null; -- Continue to next compare.
696 elsif Left.Kind < Right.Kind then
697 return True;
698 else --if Left.Kind > Right.Kind then
699 return False;
700 end if;
701 case Compare (Left.Subterm (1..Left.Subterm_Len), Right.Subterm (1..Right.Subterm_Len)) is
702 when Less => return True;
703 when Greater => return False;
704 when Equal => null; -- Continue to next compare.
705 end case;
706 if Left.Kind = See_Other_Term or else
707 Left.Kind = See_Also_Other_Term then
708 -- These have no clause or paragraph number to compare,
709 -- and the Kinds must match, so they are equal.
710 -- Since this is "<" and not "<=", the result is False.
711 return False;
712 end if;
713 -- Note: We use the numbers, because the references don't
714 -- sort right (11.1 comes before 2.8, etc.)
715 if Left.Clause_Number < Right.Clause_Number then
716 return True;
717 elsif Left.Clause_Number = Right.Clause_Number then
718 -- Make sure that single digit paragraph numbers sort before
719 -- multiple digit ones:
720 if Left.Paragraph_Len <= 1 or else Left.Paragraph(2) = '.' or else Left.Paragraph(2) = '/' then
721 -- Single digit number:
722 if Right.Paragraph_Len <= 1 or else Right.Paragraph(2) = '.' or else Right.Paragraph(2) = '/' then
723 -- Single digit number, normal compare:
724 return Left.Paragraph (1..Left.Paragraph_Len) < Right.Paragraph (1..Right.Paragraph_Len);
725 else
726 -- Single digit is always less than multiple digits:
727 return True;
728 end if;
729 else -- Not single digit number:
730 if Right.Paragraph_Len <= 1 or else Right.Paragraph(2) = '.' or else Right.Paragraph(2) = '/' then
731 -- Single digit number, always less than multiple digits:
732 return False;
733 else
734 -- Else both multiple, use normal compare:
735 return Left.Paragraph (1..Left.Paragraph_Len) < Right.Paragraph (1..Right.Paragraph_Len);
736 end if;
737 end if;
738 else -- Left.Clause_Number > Right.Clause_Number
739 return False;
740 end if;
741 end "<";
742
743 --procedure Final_Sort_Slice (Left, Right : in Natural) is
744 -- -- Use an insertion sort to sort the slice between Left and Right.
745 -- Working_Left : Natural; -- Working Left sorting stop
746 -- Temp : Term_Ptr;
747 --begin
748 -- for Working_Right In Left+1 .. Right loop -- Right sorting stop
749 -- Temp := Items(Working_Right);
750 -- Working_Left := Working_Right - 1;
751 -- while Temp < Items(Working_Left) loop -- Switch items
752 -- Items(Working_Left + 1) := Items(Working_Left);
753 -- Working_Left := Working_Left - 1;
754 -- exit when Working_Left = 0;
755 -- end loop;
756 -- Items(Working_Left + 1) := Temp;
757 -- end loop;
758 --end Final_Sort_Slice;
759
760
761 procedure Partition_Sort_Slice (Start_Index, End_Index : Natural) is
762 -- Use quicksort partition sort to sort the slice between
763 -- Start_Index and End_Index.
764 Temp_Item : Term_Ptr;
765 begin
766 case ((End_Index - Start_Index) + 1) is
767 when 0 | 1 => null; -- A single element is obviously sorted (trivially).
768 when 2 =>
769 -- Unrolled Insertion Sort.
770 if Items(Start_Index+1) < Items(Start_Index) then
771 -- Move the record down.
772 Temp_Item := Items(Start_Index+1);
773 Items(Start_Index+1) := Items(Start_Index );
774 Items(Start_Index ) := Temp_Item; -- Put at beginning.
775 -- else Doesn't need to move.
776 end if;
777 when 3 =>
778 -- Unrolled Insertion Sort.
779 if Items(Start_Index+1) < Items(Start_Index) then
780 -- Move the record down.
781 Temp_Item := Items(Start_Index+1);
782 Items(Start_Index+1) := Items(Start_Index );
783 Items(Start_Index ) := Temp_Item; -- Put at beginning.
784 -- else Doesn't need to move.
785 end if;
786 if Items(Start_Index+2) < Items(Start_Index+1) then
787 -- Move the record down.
788 Temp_Item := Items(Start_Index+2);
789 Items(Start_Index+2) := Items(Start_Index+1);
790 if Temp_Item < Items(Start_Index) then
791 -- Move the record down.
792 Items(Start_Index+1) := Items(Start_Index);
793 Items(Start_Index) := Temp_Item; -- Put at beginning.
794 else
795 -- Put the record here.
796 Items(Start_Index+1) := Temp_Item;
797 end if;
798 -- else Doesn't need to move.
799 end if;
800 when 4 =>
801 -- Unrolled Insertion Sort.
802 if Items(Start_Index+1) < Items(Start_Index) then
803 -- Move the record down.
804 Temp_Item := Items(Start_Index+1);
805 Items(Start_Index+1) := Items(Start_Index );
806 Items(Start_Index ) := Temp_Item; -- Put at beginning.
807 -- else Doesn't need to move.
808 end if;
809 if Items(Start_Index+2) < Items(Start_Index+1) then
810 -- Move the record down.
811 Temp_Item := Items(Start_Index+2);
812 Items(Start_Index+2) := Items(Start_Index+1);
813 if Temp_Item < Items(Start_Index) then
814 -- Move the record down.
815 Items(Start_Index+1) := Items(Start_Index);
816 Items(Start_Index) := Temp_Item; -- Put at beginning.
817 else
818 -- Put the record here.
819 Items(Start_Index+1) := Temp_Item;
820 end if;
821 -- else Doesn't need to move.
822 end if;
823 if Items(Start_Index+3) < Items(Start_Index+2) then
824 -- Move the record down.
825 Temp_Item := Items(Start_Index+3);
826 Items(Start_Index+3) := Items(Start_Index+2);
827 if Temp_Item < Items(Start_Index+1) then
828 -- Move the record down.
829 Items(Start_Index+2) := Items(Start_Index+1);
830 if Temp_Item < Items(Start_Index) then
831 -- Move the record down.
832 Items(Start_Index+1) := Items(Start_Index);
833 Items(Start_Index) := Temp_Item; -- Put at beginning.
834 else -- Put the record here.
835 Items(Start_Index+1) := Temp_Item;
836 end if;
837 else
838 -- Put the record here.
839 Items(Start_Index+2) := Temp_Item;
840 end if;
841 -- else Don't move the record.
842 end if;
843 when others => -- Longer partitions, quicksort.
844 declare
845 Left_Index, Right_Index : Natural;
846 Pivot_Item : Term_Ptr;
847 begin
848 -- Split into partitions, and sort them.
849 Left_Index := Start_Index;
850 Right_Index := End_Index;
851 -- Use the middle element for the pivot, in case the items are
852 -- somewhat sorted.
853 Pivot_Item := Items ((End_Index - Start_Index) / 2 + Start_Index);
854 loop
855 loop
856 exit when not (Items(Left_Index) < Pivot_Item); -- >=
857 Left_Index := Left_Index + 1;
858 end loop;
859 loop
860 exit when not (Pivot_Item < Items(Right_Index));
861 Right_Index := Right_Index - 1;
862 end loop;
863 if Left_Index <= Right_Index then
864 if Left_Index < Right_Index then
865 Temp_Item := Items(Left_Index);
866 Items(Left_Index) := Items(Right_Index);
867 Items(Right_Index) := Temp_Item;
868 end if;
869 Left_Index := Left_Index + 1;
870 Right_Index := Right_Index - 1;
871 end if;
872 exit when Left_Index > Right_Index;
873 end loop; -- Repeat Loop
874 -- Recursive calls on partitions.
875 Partition_Sort_Slice (Left_Index, End_Index);
876 Partition_Sort_Slice (Start_Index, Right_Index);
877 end;
878 end case;
879 end Partition_Sort_Slice;
880
881 begin
882 -- Debug: (This should only print Kinds that have no clause number).
883 --for Idx in Items'range loop
884 -- if Arm_Contents."=" (Items(Idx).Clause_Number.Section, Arm_Contents.UNKNOWN) then
885 -- Ada.Text_IO.Put_Line ("For index item" & Natural'Image(Idx) &
886 -- " Clause.Section=" & Arm_Contents.Section_Number_Type'Image(Items(Idx).Clause_Number.Section) &
887 -- " Kind=" & Index_Item_Kind_Type'Image(Items(Idx).Kind));
888 -- -- else OK.
889 -- end if;
890 --end loop;
891
892 -- Use quicksort to handle most of the array, then
893 -- insertion sort for the final slices.
894 Partition_Sort_Slice (Items'First, Items'Last);
895 end;
896
897 -- Relink the items in the sorted order:
898 for I in Items'First .. Items'Last - 1 loop
899 Items(I).Next := Items(I+1);
900 end loop;
901 if Items'Length > 0 then
902 Items(Items'Last).Next := null;
903 Index_List := Items(1);
904 else
905 Index_List := null;
906 end if;
907
908 Ada.Text_IO.Put_Line (" -- Finish index sorting - " & Duration'Image (
909 Ada.Calendar."-" (Ada.Calendar.Clock, Start)) & " secs.");
910
911 ARM_Output.Start_Paragraph (Output_Object, ARM_Output.Index, Indent => 0,
912 Number => "", No_Breaks => True);
913 Keep_Set := False;
914
915 Temp := Index_List;
916 while Temp /= null loop
917 -- First, check if we've changed to a new group:
918 if (Last /= null and then
919 To_Lower (Last.Term(1)) /= To_Lower(Temp.Term(1))) or else
920 (Last = null and then To_Lower(Temp.Term(1)) in 'a' .. 'z') then
921 -- The first character has changed, or this is the first item.
922 -- We only generate letters, so we try not to come here for
923 -- non-letters.
924 ARM_Output.End_Paragraph (Output_Object);
925 ARM_Output.Start_Paragraph (Output_Object, ARM_Output.Index,
926 Indent => 0, Number => "");
927 Keep_Set := False;
928 if To_Lower(Temp.Term(1)) in 'a' .. 'z' then
929 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => False);
930 ARM_Output.Text_Format (Output_Object,
931 Format => (Bold => True, Italic => False,
932 Font => ARM_Output.Default,
933 Size => 2, Color => ARM_Output.Default,
934 Change => ARM_Output.None,
935 Version | Added_Version => '0',
936 Location => ARM_Output.Normal));
937 ARM_Output.Ordinary_Character (Output_Object,
938 Ada.Characters.Handling.To_Upper(Temp.Term(1)));
939 ARM_Output.Text_Format (Output_Object,
940 Format => (Bold => False, Italic => False,
941 Font => ARM_Output.Default,
942 Size => 0, Color => ARM_Output.Default,
943 Change => ARM_Output.None,
944 Version | Added_Version => '0',
945 Location => ARM_Output.Normal));
946 ARM_Output.Index_Line_Break (Output_Object, Clear_Keep_with_Next => False);
947 else
948 ARM_Output.Hard_Space (Output_Object);
949 -- So the paragraph isn't empty (which causes it to be
950 -- ignored in HTML).
951 end if;
952 end if;
953
954 if Last = null or else
955 To_Lower(Last.Term (1..Last.Term_Len)) /= To_Lower(Temp.Term (1..Temp.Term_Len)) then
956 -- New term: (Note that we ignore case differences here. Perhaps
957 -- there ought to be a warning?)
958 if Last /= null then
959 ARM_Output.End_Paragraph (Output_Object);
960 if Temp.Kind = Primary_Term then
961 ARM_Output.Start_Paragraph (Output_Object, ARM_Output.Index,
962 Indent => 0, Number => "",
963 No_Breaks => True);
964 Keep_Set := False;
965 else -- The item has at least two lines; keep them together.
966 ARM_Output.Start_Paragraph (Output_Object, ARM_Output.Index,
967 Indent => 0, Number => "",
968 No_Breaks => True, Keep_with_Next => True);
969 Keep_Set := True;
970 end if;
971 end if;
972 if Temp.Kind /= Subtype_Declaration_in_Package then
973 Term_Text (Temp.Term (1..Temp.Term_Len));
974 else
975 declare
976 Of_Loc : Natural :=
977 Ada.Strings.Fixed.Index (Temp.Term (1..Temp.Term_Len),
978 " subtype of ");
979 begin
980 if Of_Loc = 0 then
981 -- Weird, "subtype of" not found.
982 Term_Text (Temp.Term (1..Temp.Term_Len));
983 else
984 Term_Text (Temp.Term (1 .. Of_Loc));
985 Italic_Text ("subtype of");
986 Term_Text (Temp.Term (Of_Loc+11 .. Temp.Term_Len));
987 end if;
988 end;
989 end if;
990 if Temp.Kind = Primary_Term then
991 ARM_Output.Hard_Space (Output_Object);
992 ARM_Output.Hard_Space (Output_Object);
993 ARM_Output.Ordinary_Character (Output_Object, ' ');
994 Clause_Ref (Temp);
995 else
996 if Is_Last_for_Term(Temp) then
997 -- Last (only) item of this term, always clear Keep:
998 New_Kind (Temp, Reset_Keep => True);
999 Keep_Set := False;
1000 else -- Leave keep set:
1001 New_Kind (Temp, Reset_Keep => False);
1002 end if;
1003 end if;
1004 elsif Last.Kind /= Temp.Kind then
1005 New_Kind (Temp, Reset_Keep => Keep_Set);
1006 Keep_Set := False;
1007 elsif (Temp.Kind = Primary_Term_and_Subterm or else
1008 Temp.Kind = Partial_Term_with_Subterm or else
1009 Temp.Kind = Syntax_NT_Used or else
1010 Temp.Kind = Child_Unit_Parent or else
1011 Temp.Kind = Declaration_in_Package or else
1012 Temp.Kind = SubDeclaration_in_Package or else
1013 Temp.Kind = Subtype_Declaration_in_Package) and then
1014 Last.Subterm (1..Last.Subterm_Len) /= Temp.Subterm (1..Temp.Subterm_Len) then
1015 New_Kind (Temp, Reset_Keep => Keep_Set);
1016 Keep_Set := False;
1017 elsif (Temp.Kind = See_Other_Term or else
1018 Temp.Kind = See_Also_Other_Term) then
1019 -- Just add the next reference.
1020 ARM_Output.Ordinary_Character (Output_Object, ',');
1021 ARM_Output.Ordinary_Character (Output_Object, ' ');
1022 ARM_Output.Ordinary_Text (Output_Object,
1023 Temp.Subterm (1..Temp.Subterm_Len));
1024 -- No clause references here.
1025 elsif (Temp.Kind = See_Term or else
1026 Temp.Kind = See_Also_Term) then
1027 -- Just add the next reference. We'll just use New_Kind for
1028 -- this, so we don't get the formats slightly different.
1029 New_Kind (Temp, Reset_Keep => Keep_Set);
1030 Keep_Set := False;
1031 elsif Last.Clause (1..Last.Clause_Len) = Temp.Clause (1..Temp.Clause_Len) and then
1032 Last.Paragraph (1..Last.Paragraph_Len) = Temp.Paragraph (1..Temp.Paragraph_Len) then
1033 -- The reference and everything else is the same, so just
1034 -- forget this item.
1035 null;
1036 else
1037 -- Just add the next clause.
1038 ARM_Output.Ordinary_Character (Output_Object, ',');
1039 ARM_Output.Ordinary_Character (Output_Object, ' ');
1040 Clause_Ref (Temp);
1041 end if;
1042 Last := Temp;
1043 Temp := Temp.Next;
1044 end loop;
1045
1046 ARM_Output.End_Paragraph (Output_Object);
1047
1048 end Generate_Index_Body;
1049
1050 end ARM_Index;