Debian Codegraph @

progs/arm_syn.adb

raw
1 with Ada.Unchecked_Deallocation,
2 Ada.Strings.Fixed;
3 package body ARM_Syntax is
4
5 --
6 -- Ada reference manual formatter (ARM_Form).
7 --
8 -- This package contains the database to collect the syntax summary and
9 -- cross-reference.
10 --
11 -- ---------------------------------------
12 -- Copyright 2000, 2004, 2006, 2011
13 -- AXE Consultants. All rights reserved.
14 -- P.O. Box 1512, Madison WI 53701
15 -- E-Mail: [email protected]
16 --
17 -- ARM_Form is free software: you can redistribute it and/or modify
18 -- it under the terms of the GNU General Public License version 3
19 -- as published by the Free Software Foundation.
20 --
21 -- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS"
22 -- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY,
23 -- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL.
24 -- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL,
25 -- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES,
26 -- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
27 -- DAMAGES.
28 --
29 -- A copy of the GNU General Public License is available in the file
30 -- gpl-3-0.txt in the standard distribution of the ARM_Form tool.
31 -- Otherwise, see <http://www.gnu.org/licenses/>.
32 --
33 -- If the GPLv3 license is not satisfactory for your needs, a commercial
34 -- use license is available for this tool. Contact Randy at AXE Consultants
35 -- for more information.
36 --
37 -- ---------------------------------------
38 --
39 -- Edit History:
40 --
41 -- 5/17/00 - RLB - Created package.
42 -- 5/24/00 - RLB - Updated to use revised Tabset.
43 -- 5/26/00 - RLB - Added a Tabset parameter.
44 -- 8/ 4/00 - RLB - Changed style to make font smaller (per Duff).
45 -- 8/16/00 - RLB - Added NoParaNum; removed junk space (which caused
46 -- blank lines and paragraph numbers).
47 -- 9/26/00 - RLB - Revised to use SyntaxDisplay format to get more
48 -- control over the formating of this section.
49 -- 9/27/00 - RLB - Revised XRef to decrease white space.
50 -- 9/28/00 - RLB - Added code to make links in HTML version.
51 -- 9/09/04 - RLB - Removed unused junk noted by Stephen Leake.
52 -- 6/22/06 - RLB - Added additional information to improve the links.
53 -- Changed the cross-reference table to use the Ada 83
54 -- format (which adds missing section references).
55 -- 10/13/06 - RLB - Added Defined flag to cross-references to eliminate
56 -- junk errors from not-quite-non-terminals.
57 -- 10/18/11 - RLB - Changed to GPLv3 license.
58
59 type String_Ptr is access String;
60 type Rule_Type;
61 type Rule_Ptr is access Rule_Type;
62 type Rule_Type is record
63 Clause : String (1..10);
64 Clause_Len : Natural;
65 Rule : String_Ptr;
66 Tabset : String (1..40);
67 Next : Rule_Ptr;
68 end record;
69
70 Rule_List : Rule_Ptr := null;
71 Rule_List_Tail : Rule_Ptr := null;
72
73 type XRef_Type;
74 type XRef_Ptr is access XRef_Type;
75 type XRef_Type is record
76 Clause : String (1..10);
77 Clause_Len : Natural;
78 Name : String (1..40);
79 Name_Len : Natural;
80 Used_In : String (1..40);
81 Used_In_Len : Natural;
82 Defined : Boolean;
83 Next : XRef_Ptr;
84 end record;
85
86 XRef_List : XRef_Ptr := null;
87 XRef_Count : Natural := 0;
88
89 type NT_Type;
90 type NT_Ptr is access NT_Type;
91 type NT_Type is record
92 Name : String (1..40);
93 Name_Len : Natural;
94 Clause : String (1..10);
95 Clause_Len : Natural;
96 Link_Target : Target_Type;
97 Next : NT_Ptr;
98 end record;
99
100 NT_List : NT_Ptr := null;
101 NT_Count : Natural := 0;
102
103 procedure Free is new Ada.Unchecked_Deallocation (Rule_Type, Rule_Ptr);
104 -- procedure Free is new Ada.Unchecked_Deallocation (XRef_Type, XRef_Ptr); not referenced
105 -- procedure Free is new Ada.Unchecked_Deallocation (NT_Type, NT_Ptr);
106 procedure Free is new Ada.Unchecked_Deallocation (String, String_Ptr);
107
108 procedure Create is
109 -- Initialize the syntax database.
110 begin
111 Rule_List := null;
112 Rule_List_Tail := null;
113 end Create;
114
115
116 procedure Destroy is
117 -- Destroy the syntax database.
118 RTemp : Rule_Ptr;
119 begin
120 while Rule_List /= null loop
121 RTemp := Rule_List;
122 Rule_List := RTemp.Next;
123 Free (RTemp.Rule);
124 Free (RTemp);
125 end loop;
126
127 end Destroy;
128
129
130 procedure Insert_Rule (
131 For_Clause : in String;
132 Rule : in String;
133 Tabset : in String := "") is
134 -- Add a rule for the syntax summary. The rule appears in For_Clause.
135 -- Tabset provides any needed tab settings.
136 Temp_Rule : Rule_Type;
137 begin
138 Ada.Strings.Fixed.Move (Target => Temp_Rule.Clause,
139 Source => For_Clause,
140 Drop => Ada.Strings.Error,
141 Pad => ' ');
142 Temp_Rule.Clause_Len := For_Clause'Length;
143 Temp_Rule.Rule := new String'(Rule);
144 Ada.Strings.Fixed.Move (Target => Temp_Rule.Tabset,
145 Source => Tabset,
146 Drop => Ada.Strings.Error,
147 Pad => ' ');
148 Temp_Rule.Next := null;
149 if Rule_List_Tail = null then
150 Rule_List := new Rule_Type'(Temp_Rule);
151 Rule_List_Tail := Rule_List;
152 else
153 Rule_List_Tail.Next := new Rule_Type'(Temp_Rule);
154 Rule_List_Tail := Rule_List_Tail.Next;
155 end if;
156 end Insert_Rule;
157
158
159 procedure Add_Non_Terminal (
160 NT_Name : in String;
161 For_Clause : in String;
162 Link_Target : out ARM_Syntax.Target_Type) is
163 -- Add a non-terminal to the syntax list. Returns a new Link_Target
164 -- for the Non-Terminal.
165 Temp_NT : NT_Type;
166 begin
167 Ada.Strings.Fixed.Move (Target => Temp_NT.Clause,
168 Source => For_Clause,
169 Drop => Ada.Strings.Error,
170 Pad => ' ');
171 Temp_NT.Clause_Len := For_Clause'Length;
172 Ada.Strings.Fixed.Move (Target => Temp_NT.Name,
173 Source => NT_Name,
174 Drop => Ada.Strings.Error,
175 Pad => ' ');
176 Temp_NT.Name_Len := NT_Name'Length;
177
178 declare
179 Val : constant String := Natural'Image(NT_Count);
180 begin
181 Temp_NT.Link_Target := "S0000";
182 if Val'Length <= 5 then
183 Temp_NT.Link_Target (5-(Val'Length-2)..5) :=
184 Val(2..Val'Last);
185 else
186 raise Program_Error; -- Too many.
187 end if;
188 Link_Target := Temp_NT.Link_Target;
189 end;
190
191 if NT_List = null then
192 Temp_NT.Next := null;
193 NT_List := new NT_Type'(Temp_NT);
194 else
195 Temp_NT.Next := NT_List;
196 NT_List := new NT_Type'(Temp_NT);
197 end if;
198 NT_Count := NT_Count + 1;
199
200 end Add_Non_Terminal;
201
202
203 function Non_Terminal_Clause (NT_Name : in String) return String is
204 -- Return the clause where NT_Name is declared.
205 -- Returns "" if NT_Name is not a declared Non_Terminal.
206 Loc : NT_Ptr;
207 begin
208 Loc := NT_List;
209 while Loc /= null loop
210 if NT_Name = Loc.Name(1..Loc.Name_Len) then
211 return Loc.Clause(1..Loc.Clause_Len);
212 end if;
213 Loc := Loc.Next;
214 end loop;
215 return ""; -- Not found.
216 end Non_Terminal_Clause;
217
218
219 function Non_Terminal_Link_Target (NT_Name : in String) return Target_Type is
220 -- Return the link target for NT_Name.
221 -- Returns " " if NT_Name is not a declared Non_Terminal.
222 Loc : NT_Ptr;
223 begin
224 Loc := NT_List;
225 while Loc /= null loop
226 if NT_Name = Loc.Name(1..Loc.Name_Len) then
227 return Loc.Link_Target;
228 end if;
229 Loc := Loc.Next;
230 end loop;
231 return Target_Type'(others => ' '); -- Not found.
232 end Non_Terminal_Link_Target;
233
234
235 procedure Add_Xref (
236 Name : in String;
237 Used_In : in String;
238 Clause : in String;
239 Defined : in Boolean) is
240 -- Add a cross-reference entry.
241 -- The item referenced is Name, and it is referenced in the production
242 -- for Used_In, in Clause. It is a defined non-terminal if Defined
243 -- is True (thus it can be linked).
244 Temp_XRef : XRef_Type;
245 begin
246 Ada.Strings.Fixed.Move (Target => Temp_XRef.Clause,
247 Source => Clause,
248 Drop => Ada.Strings.Error,
249 Pad => ' ');
250 Temp_XRef.Clause_Len := Clause'Length;
251 Ada.Strings.Fixed.Move (Target => Temp_XRef.Name,
252 Source => Name,
253 Drop => Ada.Strings.Error,
254 Pad => ' ');
255 Temp_XRef.Name_Len := Name'Length;
256 Ada.Strings.Fixed.Move (Target => Temp_XRef.Used_In,
257 Source => Used_In,
258 Drop => Ada.Strings.Error,
259 Pad => ' ');
260 Temp_XRef.Used_In_Len := Used_In'Length;
261 Temp_XRef.Defined := Defined;
262
263 -- Check for an identical record already loaded:
264 declare
265 Temp : XRef_Ptr := XRef_List;
266 begin
267 -- We assume that all of the items from the current clause
268 -- are together at the top of the list. If the list is inserted
269 -- in reverse order (the default), that will be true.
270 while Temp /= null and then
271 (Temp.Clause_Len = Temp_XRef.Clause_Len) and then
272 (Temp.Clause = Temp_XRef.Clause) loop
273 if (Temp.Name_Len = Temp_XRef.Name_Len) and then
274 (Temp.Used_In_Len = Temp_XRef.Used_In_Len) and then
275 (Temp.Name = Temp_XRef.Name) and then
276 (Temp.Used_In = Temp_XRef.Used_In) then
277 -- Identical to an existing item, forget it.
278 -- (We do this to eliminate multiple items from one production.
279 return;
280 end if;
281 Temp := Temp.Next;
282 end loop;
283 end;
284
285 if XRef_List = null then
286 Temp_XRef.Next := null;
287 XRef_List := new XRef_Type'(Temp_XRef);
288 else
289 Temp_XRef.Next := XRef_List;
290 XRef_List := new XRef_Type'(Temp_XRef);
291 end if;
292 XRef_Count := XRef_Count + 1;
293 end Add_Xref;
294
295
296 --generic
297 -- with procedure Format_Text (Text : in String;
298 -- Text_Name : in String);
299 procedure Report is
300 -- Output the fully formatted syntax summary to the
301 -- "Format_Text" routine. "Format_Text" allows all commands
302 -- for the full formatter. (Text_Name is an identifying name
303 -- for error messages).
304 Temp : Rule_Ptr;
305 begin
306 Format_Text ("@begin(syntaxdisplay)" & Ascii.LF, "Prefix");
307 Temp := Rule_List;
308 while Temp /= null loop
309 if Ada.Strings.Fixed.Trim (Temp.Tabset, Ada.Strings.Right) = "" then
310 Format_Text ("@noparanum@RefSecbyNum{" &
311 Temp.Clause(1..Temp.Clause_Len) & "}:" & Ascii.LF &
312 Temp.Rule.all & Ascii.LF & Ascii.LF,
313 Temp.Clause(1..Temp.Clause_Len));
314 else
315 Format_Text ("@noparanum@tabclear{}@tabset{" &
316 Ada.Strings.Fixed.Trim (Temp.Tabset, Ada.Strings.Right) &
317 "}@RefSecbyNum{" & Temp.Clause(1..Temp.Clause_Len) & "}:" & Ascii.LF &
318 Temp.Rule.all & Ascii.LF & Ascii.LF,
319 Temp.Clause(1..Temp.Clause_Len));
320 end if;
321 Temp := Temp.Next;
322 end loop;
323 Format_Text ("@end(syntaxdisplay)" & Ascii.LF, "Suffix");
324 end Report;
325
326
327 --generic
328 -- with procedure Format_Text (Text : in String;
329 -- Text_Name : in String);
330 procedure XRef is
331 -- Output the fully formatted syntax cross-reference to the
332 -- "Format_Text" routine. "Format_Text" allows all commands
333 -- for the full formatter. (Text_Name is an identifying name
334 -- for error messages).
335 Temp : XRef_Ptr;
336 Last : XRef_Ptr := null;
337 Items : array (1..XRef_Count) of XRef_Ptr;
338 begin
339 -- Sort the items:
340
341 -- Load the items:
342 Temp := XRef_List;
343 for I in Items'range loop
344 Items(I) := Temp;
345 Temp := Temp.Next;
346 end loop;
347
348 -- Sort the items array (use an insertion sort):
349 declare
350 Left : Natural; -- Left sorting stop
351
352 function "<" (Left, Right : XRef_Ptr) return Boolean is
353 begin
354 -- We sort first on "Name", then on "Used_In".
355 if Left.Name (1..Left.Name_Len) < Right.Name (1..Right.Name_Len) then
356 return True;
357 elsif Left.Name (1..Left.Name_Len) > Right.Name (1..Right.Name_Len) then
358 return False;
359 else
360 return Left.Used_In (1..Left.Used_In_Len) < Right.Used_In (1..Right.Used_In_Len);
361 end if;
362 end "<";
363
364 begin
365 for Right In Items'First+1 .. Items'Last loop -- Right sorting stop
366 Temp := Items(Right);
367 Left := Right - 1;
368 while Temp < Items(Left) loop -- Switch items
369 Items(Left + 1) := Items(Left);
370 Left := Left - 1;
371 exit when Left = 0;
372 end loop;
373 Items(Left + 1) := Temp;
374 end loop;
375 end;
376
377 -- Relink the items in the sorted order:
378 for I in Items'First .. Items'Last - 1 loop
379 Items(I).Next := Items(I+1);
380 end loop;
381 if Items'Length > 0 then
382 Items(Items'Last).Next := null;
383 XRef_List := Items(1);
384 else
385 XRef_List := null;
386 end if;
387
388
389 Format_Text ("@begin(syntaxdisplay)" & Ascii.LF, "Prefix");
390 Format_Text ("@tabclear()@tabset(P4, P38)" & Ascii.LF, "Prefix");
391 Format_Text ("@begin(twocol)" & Ascii.LF, "Prefix");
392 Temp := XRef_List;
393 while Temp /= null loop
394 if Last = null or else
395 Last.Name (1..Last.Name_Len) /= Temp.Name (1..Temp.Name_Len) then
396 -- New header:
397 declare
398 Clause : constant String :=
399 Non_Terminal_Clause (Temp.Name (1..Temp.Name_Len));
400 begin
401 if Temp.Defined then
402 if Clause /= "" then
403 Format_Text ("@noparanum@trailing@nt{" & Temp.Name (1..Temp.Name_Len) &
404 "}@\@RefSecbyNum{" & Clause & "}" & Ascii.LF,
405 Temp.Name (1..Temp.Name_Len) & " header");
406 else -- Undefined? Weird, but don't break, just use the
407 -- Ada 83 ellipsis.
408 Format_Text ("@noparanum@trailing@nt{" & Temp.Name (1..Temp.Name_Len) &
409 "}@\..." & Ascii.LF,
410 Temp.Name (1..Temp.Name_Len) & " header");
411 end if;
412 else
413 if Clause /= "" then
414 Format_Text ("@noparanum@trailing@ntf{" & Temp.Name (1..Temp.Name_Len) &
415 "}@\@RefSecbyNum{" & Clause & "}" & Ascii.LF,
416 Temp.Name (1..Temp.Name_Len) & " header");
417 else -- Undefined? Weird, but don't break, just use the
418 -- Ada 83 ellipsis.
419 Format_Text ("@noparanum@trailing@ntf{" & Temp.Name (1..Temp.Name_Len) &
420 "}@\..." & Ascii.LF,
421 Temp.Name (1..Temp.Name_Len) & " header");
422 end if;
423 end if;
424 end;
425 -- Original:
426 --Format_Text ("@noparanum@trailing@nt{" & Temp.Name (1..Temp.Name_Len) &
427 -- "}" & Ascii.LF,
428 -- Temp.Name (1..Temp.Name_Len) & " header");
429 Last := Temp;
430 end if;
431 if Temp.Next = null or else
432 Temp.Name (1..Temp.Name_Len) /= Temp.Next.Name (1..Temp.Next.Name_Len) then
433 -- Last item of a set.
434 Format_Text ("@\@nt{" & Temp.Used_In(1..Temp.Used_In_Len) & "}@\" &
435 "@RefSecbyNum{" & Temp.Clause(1..Temp.Clause_Len) & '}' & Ascii.LF & Ascii.LF,
436 Temp.Name (1..Temp.Name_Len) & " ref " & Temp.Clause(1..Temp.Clause_Len));
437 else -- Not an end item.
438 Format_Text ("@\@nt{" & Temp.Used_In(1..Temp.Used_In_Len) & "}@\" &
439 "@RefSecbyNum{" & Temp.Clause(1..Temp.Clause_Len) & '}' & Ascii.LF,
440 Temp.Name (1..Temp.Name_Len) & " ref " & Temp.Clause(1..Temp.Clause_Len));
441 end if;
442 Temp := Temp.Next;
443 end loop;
444 Format_Text ("@end(twocol)" & Ascii.LF, "Suffix");
445 Format_Text ("@end(syntaxdisplay)" & Ascii.LF, "Suffix");
446 -- Should free the XRef list here, but we won't do anything
447 -- afterwards, so doing so doesn't matter.
448 end XRef;
449
450 end ARM_Syntax;
451
452