Debian Codegraph @

progs/arm_inp.adb

raw
1 with Ada.Text_IO,
2 Ada.Characters.Handling,
3 Ada.Strings.Fixed;
4 package body ARM_Input is
5
6 --
7 -- Ada reference manual formatter (ARM_Form).
8 --
9 -- This package contains the abstract definition of reading an input file
10 -- or other entity, and routines to lex the input entities.
11 --
12 -- ---------------------------------------
13 -- Copyright 2000, 2002, 2004, 2005, 2011, 2013, 2019
14 -- AXE Consultants. All rights reserved.
15 -- P.O. Box 1512, Madison WI 53701
16 -- E-Mail: [email protected]
17 --
18 -- ARM_Form is free software: you can redistribute it and/or modify
19 -- it under the terms of the GNU General Public License version 3
20 -- as published by the Free Software Foundation.
21 --
22 -- AXE CONSULTANTS MAKES THIS TOOL AND SOURCE CODE AVAILABLE ON AN "AS IS"
23 -- BASIS AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE ACCURACY,
24 -- CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR FUNCTIONING OF THIS TOOL.
25 -- IN NO EVENT WILL AXE CONSULTANTS BE LIABLE FOR ANY GENERAL,
26 -- CONSEQUENTIAL, INDIRECT, INCIDENTAL, EXEMPLARY, OR SPECIAL DAMAGES,
27 -- EVEN IF AXE CONSULTANTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
28 -- DAMAGES.
29 --
30 -- A copy of the GNU General Public License is available in the file
31 -- gpl-3-0.txt in the standard distribution of the ARM_Form tool.
32 -- Otherwise, see <http://www.gnu.org/licenses/>.
33 --
34 -- If the GPLv3 license is not satisfactory for your needs, a commercial
35 -- use license is available for this tool. Contact Randy at AXE Consultants
36 -- for more information.
37 --
38 -- ---------------------------------------
39 --
40 -- Edit History:
41 --
42 -- 5/15/00 - RLB - Created base package.
43 -- 5/16/00 - RLB - Added `' as a open/close pairing.
44 -- 7/18/02 - RLB - Added Check_One_of_Parameter_Names.
45 -- 12/06/04 - RLB - Expanded Check_One_of_Parameter_Names to take up to
46 -- five names.
47 -- 1/26/05 - RLB - Fixed so that quoted parameters can be skipped.
48 -- 8/16/11 - RLB - Added code so that looping operations stop when
49 -- the input is empty. (Otherwise, bad comments cause
50 -- an infinite loop.)
51 -- 10/18/11 - RLB - Changed to GPLv3 license.
52 -- 3/26/13 - RLB - Added %% as a bracket pairing, so the Google
53 -- Analytics snippett can be inserted.
54 -- 2/19/19 - RLB - Added code to have Skip_to_Close_Char to stop
55 -- if it reaches a hard paragraph break. Otherwise,
56 -- paragraphs are miscounted. (This should happen
57 -- rarely, if at all.) Added a parameter to control that
58 -- behavior.
59
60 function Is_Open_Char (Open_Char : in Character) return Boolean is
61 -- Return True if character is a parameter opening character
62 -- ('<', '[', '{', '(', '`'), and False otherwise.
63 begin
64 case Open_Char is
65 when '<' | '[' | '{' | '(' | '`' | '%' => return True;
66 when others => return False;
67 end case;
68 end Is_Open_Char;
69
70
71 function Get_Close_Char (Open_Char : in Character) return Character is
72 -- Return the parameter closing character for an opening character.
73 -- Raises Not_Valid_Error if Open_Char is not an opening character
74 -- ('<', '[', '{', '(', '`').
75 begin
76 case Open_Char is
77 when '<' => return '>';
78 when '[' => return ']';
79 when '{' => return '}';
80 when '(' => return ')';
81 when '`' => return ''';
82 when '%' => return '%';
83 when others =>
84 Ada.Text_IO.Put_Line ("** Not an Open_Char - " & Open_Char);
85 raise ARM_Input.Not_Valid_Error;
86 end case;
87 end Get_Close_Char;
88
89
90 function Get_Open_Char (Close_Char : in Character) return Character is
91 -- Return the parameter opening character for an closing character.
92 -- Raises Not_Valid_Error if Open_Char is not an closing character
93 -- ('>', ']', '}', ')', ''').
94 begin
95 case Close_Char is
96 when '>' => return '<';
97 when ']' => return '[';
98 when '}' => return '{';
99 when ')' => return '(';
100 when ''' => return '`';
101 when '%' => return '%';
102 when others =>
103 Ada.Text_IO.Put_Line ("** Not a Close_Char - " & Close_Char);
104 raise ARM_Input.Not_Valid_Error;
105 end case;
106 end Get_Open_Char;
107
108
109 procedure Get_Name (Input_Object : in out Input_Type'Class;
110 Name : out ARM_Input.Command_Name_Type;
111 Null_Name_Allowed : in Boolean := False) is
112 -- Get a name from the Input_Object. "Names" are sequences of
113 -- alphanumeric characters. If Null_Name_Allowed is False,
114 -- an error is produced if no name is found.
115 Ch : Character;
116 Len : Natural := 0;
117 begin
118 Name := (others => ' ');
119 loop
120 Get_Char (Input_Object, Ch);
121 if Ada.Characters.Handling.Is_Alphanumeric (Ch) then
122 Len := Len + 1;
123 Name(Len) := Ch;
124 else -- End of the name.
125 Replace_Char (Input_Object);
126 if Len = 0 and then (not Null_Name_Allowed) then
127 Ada.Text_IO.Put_Line (" ** Failed to find command name on line " & Line_String (Input_Object));
128 end if;
129 return;
130 end if;
131 end loop;
132 end Get_Name;
133
134
135 procedure Copy_to_String_until_Close_Char
136 (Input_Object : in out Input_Type'Class;
137 Close_Char : in Character;
138 Buffer : out String;
139 Len : out Natural) is
140 -- Copy text from Input_Object to Buffer until the matching
141 -- Close_Char is found. Len is the number of characters copied.
142 -- Use this when we only need a string; use recording when we
143 -- need the string *and* we still must process the type.
144 -- Note that we watch for matching opening characters, in
145 -- case a nested command uses one.
146 Ch : Character;
147 Start_Ch : Character;
148 Start_Ch_Count : Natural := 0;
149 begin
150 if Close_Char = '"' then
151 Start_Ch := Character'Val(128);
152 -- This character shouldn't occur in input text.
153 -- Buglet: We don't have a way to tell whether this is an inner
154 -- command or just the end; best to avoid nesting of
155 -- quoted parameters.
156 elsif Close_Char = '%' then
157 Start_Ch := Character'Val(129);
158 -- This character shouldn't occur in input text.
159 -- Buglet: We don't have a way to tell whether this is an inner
160 -- command or just the end; best to avoid nesting of
161 -- quoted parameters.
162 else
163 Start_Ch := ARM_Input.Get_Open_Char (Close_Char);
164 end if;
165 ARM_Input.Get_Char (Input_Object, Ch);
166 Len := 0;
167 loop
168 if Ch = Start_Ch then
169 -- In case an inner command uses the same
170 -- start/end character.
171 Start_Ch_Count := Start_Ch_Count + 1;
172 elsif Ch = Close_Char then
173 exit when Start_Ch_Count = 0;
174 Start_Ch_Count := Start_Ch_Count - 1;
175 elsif Ch = Ascii.SUB then -- End of file, quit immediately.
176 Ada.Text_IO.Put_Line (" ** End of file when recording string on line " &
177 ARM_Input.Line_String (Input_Object));
178 exit;
179 end if;
180 if Len >= Buffer'Length then
181 Ada.Text_IO.Put_Line (" ** String buffer overflow on line " &
182 ARM_Input.Line_String (Input_Object));
183 ----Debug:
184 --raise Program_Error; -- Where the heck are we??
185 else
186 Buffer (Buffer'First + Len) := Ch;
187 Len := Len + 1;
188 end if;
189 ARM_Input.Get_Char (Input_Object, Ch);
190 end loop;
191 end Copy_to_String_until_Close_Char;
192
193
194 procedure Skip_until_Close_Char
195 (Input_Object : in out Input_Type'Class;
196 Close_Char : in Character;
197 Exit_On_Para_End : in Boolean := True) is
198 -- Skip text from Input_Object until the matching Close_Char is found.
199 -- Exit this early if there is a paragraph end in the middle if
200 -- Exit_On_Para_End is True (otherwise, continue to the end).
201 -- Note that we watch for matching opening characters, in
202 -- case a nested command uses one.
203 Ch : Character;
204 Start_Ch : Character;
205 Start_Ch_Count : Natural := 0;
206 Start_Line : constant String := ARM_Input.Line_String (Input_Object);
207 -- Save this in case of severe error.
208 begin
209 if Close_Char = '"' then
210 Start_Ch := Character'Val(128);
211 -- This character shouldn't occur in input text.
212 -- Buglet: We don't have a way to tell whether this is an inner
213 -- command or just the end; best to avoid nesting of
214 -- quoted parameters.
215 elsif Close_Char = '%' then
216 Start_Ch := Character'Val(129);
217 -- This character shouldn't occur in input text.
218 -- Buglet: We don't have a way to tell whether this is an inner
219 -- command or just the end; best to avoid nesting of
220 -- quoted parameters.
221 else
222 Start_Ch := ARM_Input.Get_Open_Char (Close_Char);
223 end if;
224 --Ada.Text_IO.Put_Line ("?? Skip: Start=" & Start_Ch & "; Close=" & Close_Char & " on line " &
225 -- ARM_Input.Line_String (Input_Object));
226 ARM_Input.Get_Char (Input_Object, Ch);
227 loop
228 if Ch = Start_Ch then
229 -- In case an inner command uses the same
230 -- start/end character.
231 Start_Ch_Count := Start_Ch_Count + 1;
232 --Ada.Text_IO.Put_Line ("?? Skip: Start found, cnt=" & Natural'Image(Start_Ch_Count) & " on line " &
233 -- ARM_Input.Line_String (Input_Object));
234 elsif Ch = Close_Char then
235 --if Start_Ch_Count = 0 then
236 --Ada.Text_IO.Put_Line ("?? Skip: Close found on line " &
237 -- ARM_Input.Line_String (Input_Object));
238 --end if;
239 exit when Start_Ch_Count = 0;
240 Start_Ch_Count := Start_Ch_Count - 1;
241 --Ada.Text_IO.Put_Line ("?? Skip: Close found, cnt=" & Natural'Image(Start_Ch_Count) & " on line " &
242 -- ARM_Input.Line_String (Input_Object));
243 elsif Ch = Ascii.SUB then -- End of file, quit immediately.
244 Ada.Text_IO.Put_Line (" ** End of file when skipping to end, started on line " &
245 Start_Line);
246 exit;
247 elsif Ch = Ascii.LF and then
248 Exit_On_Para_End then -- Possible end of paragraph.
249 -- Check next character:
250 ARM_Input.Get_Char (Input_Object, Ch);
251 if Ch /= Ascii.LF then
252 goto Skip_Get; -- Soft line end, not interesting.
253 else -- Hard paragraph end!
254 -- Note: This is only known to occur in attribute definitions.
255 -- If we don't do this, we will not count the extra paragraphs,
256 -- meaning the numbers would be wrong.
257 --Ada.Text_IO.Put_Line ("?? Skip: Paragraph end found on line " &
258 -- ARM_Input.Line_String (Input_Object));
259 ARM_Input.Replace_Char (Input_Object);
260 exit;
261 end if;
262 end if;
263 -- Ignore everything until the end character
264 -- turns up (or the end of file).
265 ARM_Input.Get_Char (Input_Object, Ch);
266 <<Skip_Get>> null;
267 end loop;
268 end Skip_until_Close_Char;
269
270
271 procedure Check_Parameter_Name (Input_Object : in out Input_Type'Class;
272 Param_Name : in ARM_Input.Command_Name_Type;
273 Is_First : in Boolean;
274 Param_Close_Bracket : out Character) is
275 -- Check that the name of a parameter (if any) is Param_Name.
276 -- This is the first parameter is Is_First is True; otherwise
277 -- it is a later parameter. (For a later parameter, we'll skip
278 -- the comma and any whitespace.)
279 -- If the parameter has an argument, the opening character will
280 -- be read, and the closing character will be returned in
281 -- in Param_Close_Bracket. If the parameter wasn't found, an
282 -- error message will be produced, and Param_Close_Bracket will
283 -- be set to ' '.
284 Our_Param_Name : ARM_Input.Command_Name_Type;
285 Ch : Character;
286 begin
287 if not Is_First then
288 -- Skip over the comma and any whitespace:
289 ARM_Input.Get_Char (Input_Object, Ch);
290 if Ch /= ',' then
291 Ada.Text_IO.Put_Line (" ** Bad parameter separator for " &
292 Ada.Strings.Fixed.Trim (Param_Name, Ada.Strings.Right) &
293 ": " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
294 else
295 ARM_Input.Get_Char (Input_Object, Ch);
296 end if;
297 while (Ch = ' ' or else Ch = Ascii.LF) loop
298 ARM_Input.Get_Char (Input_Object, Ch);
299 end loop;
300 ARM_Input.Replace_Char (Input_Object);
301 -- else nothing to clean up.
302 end if;
303 Arm_Input.Get_Name (Input_Object, Our_Param_Name, Null_Name_Allowed => True);
304 if Ada.Characters.Handling.To_Lower (Param_Name) =
305 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
306 ARM_Input.Get_Char (Input_Object, Ch);
307 if Ch /= '=' then
308 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
309 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
310 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
311 end if;
312 elsif Ada.Strings.Fixed.Trim (Param_Name, Ada.Strings.Right) = "" then
313 null; -- No parameter name.
314 else
315 Ada.Text_IO.Put_Line (" ** Bad parameter name: " & Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
316 ", " & Ada.Strings.Fixed.Trim (Param_Name, Ada.Strings.Right) & " expected on line " &
317 ARM_Input.Line_String (Input_Object));
318 end if;
319 -- Now, open the parameter section:
320 ARM_Input.Get_Char (Input_Object, Ch);
321 if ARM_Input.Is_Open_Char (Ch) then -- Start parameter:
322 Param_Close_Bracket := ARM_Input.Get_Close_Char (Ch);
323 elsif Ch = '"' then -- Start quoted parameter:
324 Param_Close_Bracket := '"';
325 else -- No parameter. Weird.
326 ARM_Input.Replace_Char (Input_Object);
327 Ada.Text_IO.Put_Line (" ** Failed to find parameter text for " &
328 Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
329 ", line " & ARM_Input.Line_String (Input_Object));
330 Param_Close_Bracket := ' ';
331 end if;
332 end Check_Parameter_Name;
333
334
335 procedure Check_One_of_Parameter_Names (
336 Input_Object : in out Input_Type'Class;
337 Param_Name_1 : in ARM_Input.Command_Name_Type;
338 Param_Name_2 : in ARM_Input.Command_Name_Type;
339 Param_Name_3 : in ARM_Input.Command_Name_Type := (others => ' ');
340 Param_Name_4 : in ARM_Input.Command_Name_Type := (others => ' ');
341 Param_Name_5 : in ARM_Input.Command_Name_Type := (others => ' ');
342 Is_First : in Boolean;
343 Param_Close_Bracket : out Character;
344 Param_Found : out Param_Num) is
345 -- Check that the name of a parameter (if any) is one of the given
346 -- names. If the parameter is set to all blanks, it is not used.
347 -- This is the first parameter is Is_First is True;
348 -- otherwise it is a later parameter. (For a later parameter, we'll
349 -- skip the comma and any whitespace.)
350 -- Param_Found will be set to the number of the parameter that was
351 -- found.
352 -- If the parameter has an argument, the opening character will
353 -- be read, and the closing character will be returned in
354 -- in Param_Close_Bracket. If the parameter wasn't found, an
355 -- error message will be produced, Param_Close_Bracket will
356 -- be set to ' ', and Param_Found will be set to 0.
357 Our_Param_Name : ARM_Input.Command_Name_Type;
358 Ch : Character;
359 begin
360 if not Is_First then
361 -- Skip over the comma and any whitespace:
362 ARM_Input.Get_Char (Input_Object, Ch);
363 if Ch /= ',' then
364 Ada.Text_IO.Put_Line (" ** Bad parameter separator for " &
365 Ada.Strings.Fixed.Trim (Param_Name_1, Ada.Strings.Right) & " or " &
366 Ada.Strings.Fixed.Trim (Param_Name_2, Ada.Strings.Right) &
367 ": " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
368 else
369 ARM_Input.Get_Char (Input_Object, Ch);
370 end if;
371 while (Ch = ' ' or else Ch = Ascii.LF) loop
372 ARM_Input.Get_Char (Input_Object, Ch);
373 end loop;
374 ARM_Input.Replace_Char (Input_Object);
375 -- else nothing to clean up.
376 end if;
377 Arm_Input.Get_Name (Input_Object, Our_Param_Name, Null_Name_Allowed => True);
378 if Param_Name_1 /= ARM_Input.Command_Name_Type'(others => ' ') and then
379 Ada.Characters.Handling.To_Lower (Param_Name_1) =
380 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
381 ARM_Input.Get_Char (Input_Object, Ch);
382 if Ch /= '=' then
383 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
384 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
385 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
386 end if;
387 Param_Found := 1;
388 elsif Param_Name_2 /= ARM_Input.Command_Name_Type'(others => ' ') and then
389 Ada.Characters.Handling.To_Lower (Param_Name_2) =
390 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
391 ARM_Input.Get_Char (Input_Object, Ch);
392 if Ch /= '=' then
393 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
394 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
395 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
396 end if;
397 Param_Found := 2;
398 elsif Param_Name_3 /= ARM_Input.Command_Name_Type'(others => ' ') and then
399 Ada.Characters.Handling.To_Lower (Param_Name_3) =
400 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
401 ARM_Input.Get_Char (Input_Object, Ch);
402 if Ch /= '=' then
403 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
404 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
405 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
406 end if;
407 Param_Found := 3;
408 elsif Param_Name_4 /= ARM_Input.Command_Name_Type'(others => ' ') and then
409 Ada.Characters.Handling.To_Lower (Param_Name_4) =
410 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
411 ARM_Input.Get_Char (Input_Object, Ch);
412 if Ch /= '=' then
413 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
414 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
415 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
416 end if;
417 Param_Found := 4;
418 elsif Param_Name_5 /= ARM_Input.Command_Name_Type'(others => ' ') and then
419 Ada.Characters.Handling.To_Lower (Param_Name_5) =
420 Ada.Characters.Handling.To_Lower (Our_Param_Name) then
421 ARM_Input.Get_Char (Input_Object, Ch);
422 if Ch /= '=' then
423 Ada.Text_IO.Put_Line (" ** Bad parameter character for " &
424 Ada.Strings.Fixed.Trim(Our_Param_Name, Ada.Strings.Right) &
425 " parameter: " & Ch & " on line " & ARM_Input.Line_String (Input_Object));
426 end if;
427 Param_Found := 5;
428 else
429 if Param_Name_5 /= ARM_Input.Command_Name_Type'(others => ' ') then
430 Ada.Text_IO.Put_Line (" ** Bad parameter name: " & Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
431 "; but " & Ada.Strings.Fixed.Trim (Param_Name_1, Ada.Strings.Right) & ", " &
432 Ada.Strings.Fixed.Trim (Param_Name_2, Ada.Strings.Right) & ", " &
433 Ada.Strings.Fixed.Trim (Param_Name_3, Ada.Strings.Right) & ", " &
434 Ada.Strings.Fixed.Trim (Param_Name_4, Ada.Strings.Right) & ", or " &
435 Ada.Strings.Fixed.Trim (Param_Name_5, Ada.Strings.Right) & " expected on line " &
436 ARM_Input.Line_String (Input_Object));
437 elsif Param_Name_4 /= ARM_Input.Command_Name_Type'(others => ' ') then
438 Ada.Text_IO.Put_Line (" ** Bad parameter name: " & Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
439 "; but " & Ada.Strings.Fixed.Trim (Param_Name_1, Ada.Strings.Right) & ", " &
440 Ada.Strings.Fixed.Trim (Param_Name_2, Ada.Strings.Right) & ", " &
441 Ada.Strings.Fixed.Trim (Param_Name_3, Ada.Strings.Right) & ", or " &
442 Ada.Strings.Fixed.Trim (Param_Name_4, Ada.Strings.Right) & " expected on line " &
443 ARM_Input.Line_String (Input_Object));
444 elsif Param_Name_3 /= ARM_Input.Command_Name_Type'(others => ' ') then
445 Ada.Text_IO.Put_Line (" ** Bad parameter name: " & Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
446 "; but " & Ada.Strings.Fixed.Trim (Param_Name_1, Ada.Strings.Right) & ", " &
447 Ada.Strings.Fixed.Trim (Param_Name_2, Ada.Strings.Right) & ", or " &
448 Ada.Strings.Fixed.Trim (Param_Name_3, Ada.Strings.Right) & " expected on line " &
449 ARM_Input.Line_String (Input_Object));
450 else
451 Ada.Text_IO.Put_Line (" ** Bad parameter name: " & Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
452 "; but " & Ada.Strings.Fixed.Trim (Param_Name_1, Ada.Strings.Right) & " or " &
453 Ada.Strings.Fixed.Trim (Param_Name_2, Ada.Strings.Right) & " expected on line " &
454 ARM_Input.Line_String (Input_Object));
455 end if;
456 Param_Found := 0;
457 end if;
458 -- Now, open the parameter section:
459 ARM_Input.Get_Char (Input_Object, Ch);
460 if ARM_Input.Is_Open_Char (Ch) then -- Start parameter:
461 Param_Close_Bracket := ARM_Input.Get_Close_Char (Ch);
462 elsif Ch = '"' then -- Start quoted parameter:
463 Param_Close_Bracket := '"';
464 else -- No parameter. Weird.
465 ARM_Input.Replace_Char (Input_Object);
466 Ada.Text_IO.Put_Line (" ** Failed to find parameter text for " &
467 Ada.Strings.Fixed.Trim (Our_Param_Name, Ada.Strings.Right) &
468 ", line " & ARM_Input.Line_String (Input_Object));
469 Param_Close_Bracket := ' ';
470 end if;
471 end Check_One_of_Parameter_Names;
472
473
474 end ARM_Input;