Debian Codegraph @

deps/ada-toml/src/toml-generic_dump.adb

raw
1 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
2
3 procedure TOML.Generic_Dump (Stream : in out Output_Stream; Value : TOML_Value)
4 is
5
6 procedure Put (Bytes : String);
7 -- Shortcut for Put (Stream, Bytes)
8
9 type Map_Pair is record
10 Key : Unbounded_UTF8_String;
11 Value : TOML_Value;
12 end record;
13
14 type Map_Pair_Array is array (Positive range <>) of Map_Pair;
15
16 function Format_String
17 (S : Unbounded_UTF8_String) return Unbounded_UTF8_String;
18 -- Format a valid TOML representation of the given string (S)
19
20 function Format_Key
21 (Key : Unbounded_UTF8_String) return Unbounded_UTF8_String;
22 -- Format a valid TOML representation of the given Key
23
24 function Append_Key
25 (Prefix, Suffix : Unbounded_UTF8_String) return Unbounded_UTF8_String;
26 -- Return a key corresponding to "Prefix.Suffix" (if Prefix is not empty)
27 -- or "Suffix" (if Prefix is empty).
28
29 procedure Separate_Pairs
30 (Pairs : in out Map_Pair_Array;
31 Last_Table_Pair : out Natural;
32 Last_Array_Pair : out Natural)
33 with Pre => Pairs'First = 1;
34 -- Shuffle Pairs so that, after the procedure returns:
35 --
36 -- * Pairs (Pairs'First .. Last_Table_Pair) contains only table values;
37 -- * Pairs (Last_Table_Pair + 1 .. Last_Array_Pair) contains only
38 -- array-of-tables values;
39 -- * Pairs (Last_Array_Pair + 1 .. Pairs'Last) contains remaining values.
40 --
41 -- The three array sub-ranges are sorted by key.
42
43 procedure Put_Table_Header
44 (Prefix, Suffix : Unbounded_UTF8_String;
45 Nested_Key : out Unbounded_UTF8_String);
46 -- Set Nested_Key to Append_Key (Prefix, Suffix). Then write a table header
47 -- for Nested_Key to Stream.
48
49 procedure Put_Array_Header (Nested_Key : Unbounded_UTF8_String);
50 -- Write an array of tables header for Nested_Key to Stream
51
52 procedure Dump_Toplevel_Table
53 (Key : Unbounded_UTF8_String; Value : TOML_Value)
54 with Pre => Value.Kind = TOML_Table;
55 -- Dump the given table (Value) as a top-level table, under the given key
56
57 procedure Dump_Toplevel_Array
58 (Parent_Key, Array_Key : Unbounded_UTF8_String; Array_Value : TOML_Value)
59 with Pre => Array_Value.Kind = TOML_Array;
60 -- Dump the given array (Array_Value) as a top-level array of tables, under
61 -- the given keys (Parent_Key "." Array_Key).
62
63 function Strip_Number (Image : String) return String;
64 -- If the first character in Image is a space, return the rest of Image
65
66 function Pad_Number (Image : String; Digit_Count : Positive) return String;
67 -- Return Strip_Number (Image) left-padded with 0 so that the result is
68 -- Digit_Count long.
69
70 procedure Dump_Inline (Value : TOML_Value)
71 with Pre => Value /= No_TOML_Value;
72 -- Dump the given value using the inline format
73
74 ---------
75 -- Put --
76 ---------
77
78 procedure Put (Bytes : String) is
79 begin
80 Put (Stream, Bytes);
81 end Put;
82
83 -------------------
84 -- Format_String --
85 -------------------
86
87 function Format_String
88 (S : Unbounded_UTF8_String) return Unbounded_UTF8_String
89 is
90 Result : Unbounded_UTF8_String;
91 begin
92 Append (Result, """");
93 for I in 1 .. Length (S) loop
94 -- Forward printable ASCII bytes (except quotes and backslashes)
95 -- and non-ASCII bytes, but emit escapes for quotes and control
96 -- characters.
97 declare
98 Char : constant Character := Element (S, I);
99 begin
100 case Char is
101 -- The only way to represent the following control characters
102 -- is to use the unicode escape (\uXXXX).
103 when ASCII.NUL .. ASCII.BEL
104 | ASCII.VT
105 | ASCII.SO .. ASCII.US
106 | ASCII.DEL
107 =>
108 declare
109 use type Interfaces.Unsigned_8;
110
111 Byte : Interfaces.Unsigned_8 := Character'Pos (Char);
112 Repr : String (1 .. 6) := "\u0000";
113 I : Natural range 3 .. 6 := 6;
114 begin
115 while Byte /= 0 loop
116 declare
117 Nibble : constant Interfaces.Unsigned_8 :=
118 Byte mod 16;
119 Digit : Interfaces.Unsigned_8;
120 begin
121 if Nibble <= 9 then
122 Digit := Character'Pos ('0') + Nibble;
123 else
124 Digit := Character'Pos ('a') - 10 + Nibble;
125 end if;
126 Repr (I) := Character'Val (Digit);
127 Byte := Byte / 16;
128 I := I - 1;
129 end;
130 end loop;
131 Append (Result, Repr);
132 end;
133
134 -- The following control characters have dedicated escape
135 -- sequences:
136
137 when ASCII.BS => Append (Result, "\b");
138 when ASCII.HT => Append (Result, "\t");
139 when ASCII.LF => Append (Result, "\n");
140 when ASCII.FF => Append (Result, "\f");
141 when ASCII.CR => Append (Result, "\r");
142
143 -- Quotes and backslashes must be escaped
144
145 when '"' => Append (Result, "\""");
146 when '\' => Append (Result, "\\");
147
148 -- All other bytes can be directly forwarded to the string
149 -- literal.
150
151 when ' ' | '!'
152 | '#' .. '['
153 | ']' .. '~'
154 | Character'Val (128) .. Character'Val(255)
155 =>
156 Append (Result, Char);
157 end case;
158 end;
159 end loop;
160 Append (Result, """");
161 return Result;
162 end Format_String;
163
164 ----------------
165 -- Format_Key --
166 ----------------
167
168 function Format_Key
169 (Key : Unbounded_UTF8_String) return Unbounded_UTF8_String is
170 begin
171 -- Determine if we need to quote Key, and if so, do it
172
173 for I in 1 .. Length (Key) loop
174 if Element (Key, I) not in
175 '0' .. '9' | 'A' .. 'Z' | 'a' .. 'z' | '-' | '_'
176 then
177 return Format_String (Key);
178 end if;
179 end loop;
180
181 if Length (Key) = 0 then
182 return Format_String (Key);
183 end if;
184
185 -- Otherwise, we can return the key as-is (without quoting)
186
187 return Key;
188 end Format_Key;
189
190 ----------------
191 -- Append_Key --
192 ----------------
193
194 function Append_Key
195 (Prefix, Suffix : Unbounded_UTF8_String) return Unbounded_UTF8_String
196 is
197 Result : Unbounded_UTF8_String := Prefix;
198 begin
199 if Length (Result) > 0 then
200 Append (Result, ".");
201 end if;
202 Append (Result, Format_Key (Suffix));
203 return Result;
204 end Append_Key;
205
206 --------------------
207 -- Separate_Pairs --
208 --------------------
209
210 procedure Separate_Pairs
211 (Pairs : in out Map_Pair_Array;
212 Last_Table_Pair : out Natural;
213 Last_Array_Pair : out Natural)
214 is
215 type Pair_List is record
216 Pairs : Map_Pair_Array (Separate_Pairs.Pairs'Range);
217 Last : Natural := 0;
218 end record;
219
220 procedure Append (Pair : Map_Pair; List : in out Pair_List);
221 -- Append Pair to the given List
222
223 procedure Append
224 (List : Pair_List; First_Pair : Positive; Last_Pair : out Natural);
225 -- Append all pairs in List to Pairs. The first one goes to:
226 --
227 -- Pairs (First_Pair)
228 --
229 -- and the last one is copied to:
230 --
231 -- Pairs (Last_Pair)
232
233 ------------
234 -- Append --
235 ------------
236
237 procedure Append (Pair : Map_Pair; List : in out Pair_List) is
238 begin
239 List.Last := List.Last + 1;
240 List.Pairs (List.Last) := Pair;
241 end Append;
242
243 ------------
244 -- Append --
245 ------------
246
247 procedure Append
248 (List : Pair_List; First_Pair : Positive; Last_Pair : out Natural)
249 is
250 begin
251 Last_Pair := First_Pair - 1;
252 for P of List.Pairs (1 .. List.Last) loop
253 Last_Pair := Last_Pair + 1;
254 Pairs (Last_Pair) := P;
255 end loop;
256 end Append;
257
258 Table_Pairs, Array_Pairs, Other_Pairs : Pair_List;
259 Last : Natural;
260 begin
261 -- Put pairs in separate lists. Note that this preserves sorting.
262
263 for P of Pairs loop
264 case P.Value.Kind is
265 when TOML_Table =>
266 Append (P, Table_Pairs);
267 when TOML_Array =>
268 if (for all I in 1 .. P.Value.Length =>
269 P.Value.Item (I).Kind = TOML_Table)
270 then
271 Append (P, Array_Pairs);
272 else
273 Append (P, Other_Pairs);
274 end if;
275 when others =>
276 Append (P, Other_Pairs);
277 end case;
278 end loop;
279
280 -- Then put back each category of pairs to Pairs
281
282 Append (Table_Pairs, Pairs'First, Last_Table_Pair);
283 Append (Array_Pairs, Last_Table_Pair + 1, Last_Array_Pair);
284 Append (Other_Pairs, Last_Array_Pair + 1, Last);
285 pragma Assert (Last = Pairs'Last);
286 end Separate_Pairs;
287
288 ----------------------
289 -- Put_Table_Header --
290 ----------------------
291
292 procedure Put_Table_Header
293 (Prefix, Suffix : Unbounded_UTF8_String;
294 Nested_Key : out Unbounded_UTF8_String) is
295 begin
296 Nested_Key := Append_Key (Prefix, Suffix);
297 Put ("[" & To_String (Nested_Key) & "]" & ASCII.LF);
298 end Put_Table_Header;
299
300 ----------------------
301 -- Put_Array_Header --
302 ----------------------
303
304 procedure Put_Array_Header (Nested_Key : Unbounded_UTF8_String) is
305 begin
306 Put ("[[" & To_String (Nested_Key) & "]]" & ASCII.LF);
307 end Put_Array_Header;
308
309 -------------------------
310 -- Dump_Toplevel_Table --
311 -------------------------
312
313 procedure Dump_Toplevel_Table
314 (Key : Unbounded_UTF8_String; Value : TOML_Value)
315 is
316 Keys : constant Key_Array := Value.Keys;
317 Pairs : Map_Pair_Array (Keys'Range);
318 Last_Table_Pair, Last_Array_Pair : Natural;
319 Nested_Key : Unbounded_UTF8_String;
320 begin
321 -- Initialize Pairs from Keys and Value and split pairs by value kind
322
323 for I in Keys'Range loop
324 Pairs (I) := (Key => Keys (I),
325 Value => Value.Get (Keys (I)));
326 end loop;
327 Separate_Pairs (Pairs, Last_Table_Pair, Last_Array_Pair);
328
329 declare
330 Table_Pairs : Map_Pair_Array renames
331 Pairs (Pairs'First .. Last_Table_Pair);
332 Array_Pairs : Map_Pair_Array renames
333 Pairs (Last_Table_Pair + 1 .. Last_Array_Pair);
334 Other_Pairs : Map_Pair_Array renames
335 Pairs (Last_Array_Pair + 1 .. Pairs'Last);
336 begin
337 -- Dump non-table and non-array map pairs with inline style:
338 -- key = value
339
340 for Pair of Other_Pairs loop
341 Put (To_String (Format_Key (Pair.Key)));
342 Put (" = ");
343 Dump_Inline (Pair.Value);
344 Put ((1 => ASCII.LF));
345 end loop;
346
347 -- Dump tables as top-level ones: [section]
348
349 for Pair of Table_Pairs loop
350 Put_Table_Header (Key, Pair.Key, Nested_Key);
351 Dump_Toplevel_Table (Nested_Key, Pair.Value);
352 end loop;
353
354 -- Dump arrays as top-level ones: [[section]]
355
356 for Pair of Array_Pairs loop
357 Dump_Toplevel_Array (Key, Pair.Key, Pair.Value);
358 end loop;
359 end;
360 end Dump_Toplevel_Table;
361
362 -------------------------
363 -- Dump_Toplevel_Array --
364 -------------------------
365
366 procedure Dump_Toplevel_Array
367 (Parent_Key, Array_Key : Unbounded_UTF8_String; Array_Value : TOML_Value)
368 is
369 Nested_Key : constant Unbounded_UTF8_String :=
370 Append_Key (Parent_Key, Array_Key);
371 begin
372 for I in 1 .. Array_Value.Length loop
373 Put_Array_Header (Nested_Key);
374 Dump_Toplevel_Table (Nested_Key, Array_Value.Item (I));
375 end loop;
376 end Dump_Toplevel_Array;
377
378 ------------------
379 -- Strip_Number --
380 ------------------
381
382 function Strip_Number (Image : String) return String is
383 begin
384 if Image'Length > 0 and then Image (Image'First) = ' ' then
385 return Image (Image'First + 1 .. Image'Last);
386 else
387 return Image;
388 end if;
389 end Strip_Number;
390
391 ----------------
392 -- Pad_Number --
393 ----------------
394
395 function Pad_Number (Image : String; Digit_Count : Positive) return String
396 is
397 Result : constant String := Strip_Number (Image);
398 begin
399 pragma Assert (Result'Length <= Digit_Count);
400 return (Result'Length + 1 .. Digit_Count => '0') & Result;
401 end Pad_Number;
402
403 -----------------
404 -- Dump_Inline --
405 -----------------
406
407 procedure Dump_Inline (Value : TOML_Value) is
408
409 procedure Put (Datetime : TOML.Any_Local_Datetime);
410 procedure Put (Date : TOML.Any_Local_Date);
411 procedure Put (Time : TOML.Any_Local_Time);
412
413 ---------
414 -- Put --
415 ---------
416
417 procedure Put (Datetime : TOML.Any_Local_Datetime) is
418 begin
419 Put (Datetime.Date);
420 Put ("T");
421 Put (Datetime.Time);
422 end Put;
423
424 procedure Put (Date : TOML.Any_Local_Date) is
425 begin
426 Put (Pad_Number (Date.Year'Image, 4)
427 & "-" & Pad_Number (Date.Month'Image, 2)
428 & "-" & Pad_Number (Date.Day'Image, 2));
429 end Put;
430
431 procedure Put (Time : TOML.Any_Local_Time) is
432 begin
433 Put (Pad_Number (Time.Hour'Image, 2)
434 & ":" & Pad_Number (Time.Minute'Image, 2)
435 & ":" & Pad_Number (Time.Second'Image, 2)
436 & "." & Pad_Number (Time.Millisecond'Image, 3));
437 end Put;
438
439 begin
440 case Value.Kind is
441 when TOML_Boolean =>
442 Put ((if Value.As_Boolean then "true" else "false"));
443
444 when TOML_Integer =>
445 Put (Strip_Number (Any_Integer'Image (Value.As_Integer)));
446
447 when TOML_Float =>
448 declare
449 V : constant Any_Float := Value.As_Float;
450 begin
451 case V.Kind is
452 when Regular =>
453 Put (Strip_Number (V.Value'Image));
454 when NaN | Infinity =>
455 Put (if V.Positive then "" else "-");
456 Put (if V.Kind = NaN then "nan" else "inf");
457 end case;
458 end;
459
460 when TOML_String =>
461 -- TODO: escape strings when needed
462
463 Put (To_String (Format_String (Value.As_Unbounded_String)));
464
465 when TOML_Offset_Datetime =>
466 declare
467 V : constant Any_Offset_Datetime :=
468 Value.As_Offset_Datetime;
469 Absolute_Offset : constant Any_Local_Offset :=
470 (if V.Offset < 0
471 then -V.Offset
472 else V.Offset);
473 Hour_Offset : constant Any_Local_Offset :=
474 Absolute_Offset / 60;
475 Minute_Offset : constant Any_Local_Offset :=
476 Absolute_Offset mod 60;
477 begin
478 Put (V.Datetime);
479 if V.Offset < 0 or else V.Unknown_Offset then
480 Put ("-");
481 else
482 Put ("+");
483 end if;
484 Put (Pad_Number (Hour_Offset'Image, 2)
485 & ":" & Pad_Number (Minute_Offset'Image, 2));
486 end;
487
488 when TOML_Local_Datetime =>
489 Put (Value.As_Local_Datetime);
490
491 when TOML_Local_Date =>
492 Put (Value.As_Local_Date);
493
494 when TOML_Local_Time =>
495 Put (Value.As_Local_Time);
496
497 when TOML_Array =>
498 Put ("[" & ASCII.LF);
499 for I in 1 .. Value.Length loop
500 Dump_Inline (Value.Item (I));
501 Put ("," & ASCII.LF);
502 end loop;
503 Put ("]");
504
505 when TOML_Table =>
506 Put ("{");
507 declare
508 First : Boolean := True;
509 begin
510 for E of Value.Iterate_On_Table loop
511 if not First then
512 Put (", ");
513 end if;
514 Put (To_String (Format_String (E.Key)));
515 Put (" = ");
516 Dump_Inline (E.Value);
517 First := False;
518 end loop;
519 end;
520 Put ("}");
521 end case;
522 end Dump_Inline;
523
524 begin
525 Dump_Toplevel_Table (Null_Unbounded_String, Value);
526 end TOML.Generic_Dump;