Debian Codegraph @

deps/ada-toml/src-checkers/ada_toml_decode.adb

raw
1 -- Test program. Read bytes on the standard input as a TOML document.
2 --
3 -- If it's a valid TOML document, parse it and emit on the standard output a
4 -- JSON representation of it.
5 --
6 -- If it's not a valid TOML document, print an error message on the standard
7 -- output.
8
9 with Ada.Command_Line;
10 with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
11 with Ada.Strings.Unbounded;
12 with Ada.Text_IO;
13
14 with Interfaces;
15
16 with TOML;
17 with TOML.Generic_Parse;
18
19 procedure Ada_TOML_Decode is
20
21 use type TOML.Any_Value_Kind;
22
23 package Cmd renames Ada.Command_Line;
24 package IO renames Ada.Text_IO;
25
26 type Stdin_Stream is null record;
27
28 procedure Get
29 (Stream : in out Stdin_Stream; EOF : out Boolean; Byte : out Character);
30 -- Callback for TOML.Generic_Parse
31
32 subtype Wrapped_Kind is TOML.Any_Value_Kind
33 with Static_Predicate =>
34 Wrapped_Kind in TOML.TOML_Array .. TOML.TOML_Boolean
35 | TOML.TOML_Offset_Datetime | TOML.TOML_Local_Datetime
36 | TOML.TOML_Local_Date | TOML.TOML_Local_Time
37 | TOML.TOML_Float;
38
39 function Kind_Name (Kind : Wrapped_Kind) return String;
40 -- Return the name expected in the JSON output for the given kind
41
42 function Strip_Number (Image : String) return String;
43 -- If the first character in Image is a space, return the rest of Image
44
45 function Pad_Number (Image : String; Digit_Count : Positive) return String;
46 -- Return Strip_Number (Image) left-padded with 0 so that the result is
47 -- Digit_Count long.
48
49 procedure Dump_String (Value : TOML.Unbounded_UTF8_String);
50 -- Dump the given string as a JSON string literal
51
52 procedure Dump_Array (Value : TOML.TOML_Value)
53 with Pre => TOML."=" (Value.Kind, TOML.TOML_Array);
54 -- Dump the given TOML array as a JSON array
55
56 procedure Dump (Value : TOML.TOML_Value);
57 -- Dump the given TOML value using the expected JSON output format.
58 -- Toplevel must be true for the root table, root table children and table
59 -- arrays.
60
61 ---------------
62 -- Kind_Name --
63 ---------------
64
65 function Kind_Name (Kind : Wrapped_Kind) return String is
66 begin
67 return (case Kind is
68 when TOML.TOML_Array => "array",
69 when TOML.TOML_String => "string",
70 when TOML.TOML_Integer => "integer",
71 when TOML.TOML_Float => "float",
72 when TOML.TOML_Boolean => "bool",
73 when TOML.TOML_Offset_Datetime => "datetime",
74 when TOML.TOML_Local_Datetime => "datetime-local",
75 when TOML.TOML_Local_Date => "date-local",
76 when TOML.TOML_Local_Time => "time-local");
77 end Kind_Name;
78
79 ------------------
80 -- Strip_Number --
81 ------------------
82
83 function Strip_Number (Image : String) return String is
84 begin
85 if Image'Length > 0 and then Image (Image'First) = ' ' then
86 return Image (Image'First + 1 .. Image'Last);
87 else
88 return Image;
89 end if;
90 end Strip_Number;
91
92 ----------------
93 -- Pad_Number --
94 ----------------
95
96 function Pad_Number (Image : String; Digit_Count : Positive) return String
97 is
98 Result : constant String := Strip_Number (Image);
99 begin
100 pragma Assert (Result'Length <= Digit_Count);
101 return (Result'Length + 1 .. Digit_Count => '0') & Result;
102 end Pad_Number;
103
104 -----------------
105 -- Dump_String --
106 -----------------
107
108 procedure Dump_String (Value : TOML.Unbounded_UTF8_String) is
109 use Ada.Strings.Unbounded;
110 begin
111 IO.Put ("""");
112 declare
113 S : constant Wide_Wide_String :=
114 Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Decode
115 (To_String (Value));
116 begin
117 for C of S loop
118 if C in '"' | '\' then
119 IO.Put ("\" & Character'Val (Wide_Wide_Character'Pos (C)));
120 elsif C in ' ' .. '~' then
121 IO.Put ((1 => Character'Val (Wide_Wide_Character'Pos (C))));
122 else
123 declare
124 use type Interfaces.Unsigned_32;
125
126 Codepoint : Interfaces.Unsigned_32 :=
127 Wide_Wide_Character'Pos (C);
128 Digits_Count : constant Positive :=
129 (if Codepoint <= 16#FFFF# then 4 else 8);
130 CP_Digits : String (1 .. Digits_Count);
131 begin
132 if Digits_Count = 4 then
133 IO.Put ("\u");
134 else
135 IO.Put ("\U");
136 end if;
137 for D of reverse CP_Digits loop
138 declare
139 subtype Hex_Digit is
140 Interfaces.Unsigned_32 range 0 .. 15;
141 Digit : constant Hex_Digit := Codepoint mod 16;
142 begin
143 case Digit is
144 when 0 .. 9 =>
145 D := Character'Val (Character'Pos ('0') + Digit);
146 when 10 .. 15 =>
147 D := Character'Val
148 (Character'Pos ('A') + Digit - 10);
149 end case;
150 Codepoint := Codepoint / 16;
151 end;
152 end loop;
153 IO.Put (CP_Digits);
154 end;
155 end if;
156 end loop;
157 end;
158 IO.Put ("""");
159 end Dump_String;
160
161 ----------------
162 -- Dump_Array --
163 ----------------
164
165 procedure Dump_Array (Value : TOML.TOML_Value) is
166 begin
167 IO.Put_Line ("[");
168 for I in 1 .. Value.Length loop
169 if I > 1 then
170 IO.Put_Line (",");
171 end if;
172 Dump (Value.Item (I));
173 end loop;
174 IO.Put_Line ("]");
175 end Dump_Array;
176
177 ----------
178 -- Dump --
179 ----------
180
181 procedure Dump (Value : TOML.TOML_Value) is
182 use all type TOML.Any_Value_Kind;
183
184 procedure Put (Datetime : TOML.Any_Local_Datetime);
185 procedure Put (Date : TOML.Any_Local_Date);
186 procedure Put (Time : TOML.Any_Local_Time);
187
188 ---------
189 -- Put --
190 ---------
191
192 procedure Put (Datetime : TOML.Any_Local_Datetime) is
193 begin
194 Put (Datetime.Date);
195 IO.Put ("T");
196 Put (Datetime.Time);
197 end Put;
198
199 procedure Put (Date : TOML.Any_Local_Date) is
200 begin
201 IO.Put (Pad_Number (Date.Year'Image, 4)
202 & "-" & Pad_Number (Date.Month'Image, 2)
203 & "-" & Pad_Number (Date.Day'Image, 2));
204 end Put;
205
206 procedure Put (Time : TOML.Any_Local_Time) is
207 use type TOML.Any_Millisecond;
208 begin
209 IO.Put (Pad_Number (Time.Hour'Image, 2)
210 & ":" & Pad_Number (Time.Minute'Image, 2)
211 & ":" & Pad_Number (Time.Second'Image, 2));
212 if Time.Millisecond /= 0 then
213 IO.Put ("." & Pad_Number (Time.Millisecond'Image, 3));
214 end if;
215 end Put;
216
217 begin
218 if Value.Kind = TOML_Table then
219 IO.Put_Line ("{");
220 declare
221 Keys : constant TOML.Key_Array := Value.Keys;
222 begin
223 for I in Keys'Range loop
224 if I > Keys'First then
225 IO.Put_Line (",");
226 end if;
227
228 Dump_String (Keys (I));
229 IO.Put_Line (":");
230 Dump (Value.Get (Keys (I)));
231 end loop;
232 end;
233 IO.Put_Line ("}");
234
235 elsif Value.Kind = TOML.TOML_Array
236 and then (for all I in 1 .. Value.Length =>
237 Value.Item (I).Kind = TOML_Table)
238 then
239 Dump_Array (Value);
240
241 else
242 IO.Put_Line
243 ("{""type"": """ & Kind_Name (Value.Kind) & """, ""value"":");
244 case Wrapped_Kind (Value.Kind) is
245 when TOML_Array =>
246 Dump_Array (Value);
247
248 when TOML_String =>
249 Dump_String (Value.As_Unbounded_String);
250 IO.New_Line;
251
252 when TOML_Integer =>
253 IO.Put_Line ("""" & Strip_Number (Value.As_Integer'Image)
254 & """");
255
256 when TOML_Float =>
257 declare
258 V : constant TOML.Any_Float := Value.As_Float;
259 begin
260 IO.Put ("""");
261 case V.Kind is
262 when TOML.Regular =>
263 IO.Put (Strip_Number (V.Value'Image));
264 when TOML.NaN =>
265 IO.Put (if V.Positive then "+" else "-");
266 IO.Put ("nan");
267 when TOML.Infinity =>
268 IO.Put (if V.Positive then "+" else "-");
269 IO.Put ("inf");
270 end case;
271 IO.Put_Line ("""");
272 end;
273
274 when TOML_Boolean =>
275 if Value.As_Boolean then
276 IO.Put_Line ("""true""");
277 else
278 IO.Put_Line ("""false""");
279 end if;
280
281 when TOML_Offset_Datetime =>
282 declare
283 use type TOML.Any_Local_Offset;
284
285 V : constant TOML.Any_Offset_Datetime :=
286 Value.As_Offset_Datetime;
287 Absolute_Offset : constant TOML.Any_Local_Offset :=
288 (if V.Offset < 0
289 then -V.Offset
290 else V.Offset);
291 Hour_Offset : constant TOML.Any_Local_Offset :=
292 Absolute_Offset / 60;
293 Minute_Offset : constant TOML.Any_Local_Offset :=
294 Absolute_Offset mod 60;
295 begin
296 IO.Put ("""");
297 Put (V.Datetime);
298 if V.Offset = 0 and then not V.Unknown_Offset then
299 IO.Put ("Z");
300 else
301 if V.Offset <= 0 then
302 IO.Put ("-");
303 else
304 IO.Put ("+");
305 end if;
306 IO.Put (Pad_Number (Hour_Offset'Image, 2)
307 & ":" & Pad_Number (Minute_Offset'Image, 2));
308 end if;
309 IO.Put_Line ("""");
310 end;
311
312 when TOML_Local_Datetime =>
313 IO.Put ("""");
314 Put (Value.As_Local_Datetime);
315 IO.Put_Line ("""");
316
317 when TOML_Local_Date =>
318 IO.Put ("""");
319 Put (Value.As_Local_Date);
320 IO.Put_Line ("""");
321
322 when TOML_Local_Time =>
323 IO.Put ("""");
324 Put (Value.As_Local_Time);
325 IO.Put_Line ("""");
326 end case;
327 IO.Put_Line ("}");
328 end if;
329 end Dump;
330
331 ---------
332 -- Get --
333 ---------
334
335 procedure Get
336 (Stream : in out Stdin_Stream; EOF : out Boolean; Byte : out Character)
337 is
338 pragma Unreferenced (Stream);
339 Available : Boolean;
340 begin
341 IO.Get_Immediate (Byte, Available);
342 EOF := not Available;
343 exception
344 when IO.End_Error =>
345 EOF := True;
346 end Get;
347
348 function Parse_File is new TOML.Generic_Parse (Stdin_Stream, Get);
349
350 Stdin : Stdin_Stream := (null record);
351 Result : constant TOML.Read_Result := Parse_File (Stdin);
352 begin
353 if Result.Success then
354 Dump (Result.Value);
355 else
356 IO.Put_Line (TOML.Format_Error (Result));
357 Cmd.Set_Exit_Status (Cmd.Failure);
358 end if;
359 end Ada_TOML_Decode;