Debian Codegraph @

deps/ada-toml/src/toml.ads

raw
1 with Ada.Finalization;
2 with Ada.Strings.Unbounded;
3
4 with Interfaces;
5
6 package TOML with Preelaborate is
7
8 pragma Warnings (Off);
9 use type Ada.Strings.Unbounded.Unbounded_String;
10 pragma Warnings (On);
11
12 Version : constant String := "0.1";
13 -- Version for the ada-toml project
14
15 subtype Unbounded_UTF8_String is Ada.Strings.Unbounded.Unbounded_String;
16
17 type Any_Value_Kind is
18 (TOML_Table,
19 TOML_Array,
20 TOML_String,
21 TOML_Integer,
22 TOML_Float,
23 TOML_Boolean,
24 TOML_Offset_Datetime,
25 TOML_Local_Datetime,
26 TOML_Local_Date,
27 TOML_Local_Time);
28
29 subtype Composite_Value_Kind is
30 Any_Value_Kind range TOML_Table .. TOML_Array;
31
32 subtype Atom_Value_Kind is
33 Any_Value_Kind range TOML_String .. TOML_Local_Time;
34
35 type TOML_Value is new Ada.Finalization.Controlled with private;
36 No_TOML_Value : constant TOML_Value;
37
38 -- TODO: create TOML_Value subtypes for the various kinds
39
40 type Any_Integer is new Interfaces.Integer_64;
41 -- TOML supports any integer that can be encoded in a 64-bit signed
42 -- integer.
43
44 type Valid_Float is new Interfaces.IEEE_Float_64;
45 type Float_Kind is (Regular, NaN, Infinity);
46 subtype Special_Float_Kind is Float_Kind range NaN .. Infinity;
47 type Any_Float (Kind : Float_Kind := Regular) is record
48 case Kind is
49 when Regular =>
50 Value : Valid_Float;
51 when Special_Float_Kind =>
52 Positive : Boolean;
53 end case;
54 end record;
55 -- TOML advises to implement its float values as IEEE 754 binary64 values,
56 -- however Ada does not provide a standard way to represent infinities and
57 -- NaN (Not a Number), so fallback to a discriminated record to reliably
58 -- represent TOML float.
59
60 type Any_Year is range 1 .. 9999;
61 type Any_Month is range 1 .. 12;
62 type Any_Day is range 1 .. 31;
63
64 type Any_Local_Date is record
65 Year : Any_Year;
66 Month : Any_Month;
67 Day : Any_Day;
68 end record;
69
70 type Any_Hour is range 0 .. 23;
71 type Any_Minute is range 0 .. 59;
72 type Any_Second is range 0 .. 60;
73 type Any_Millisecond is range 0 .. 999;
74
75 type Any_Local_Offset is range -(23 * 60 + 59) .. 23 * 60 + 59;
76 -- Offset between local time and UTC, in minutes. We allow from -23:59 to
77 -- +23:59.
78
79 type Any_Local_Time is record
80 Hour : Any_Hour;
81 Minute : Any_Minute;
82 Second : Any_Second;
83 Millisecond : Any_Millisecond;
84 end record;
85
86 type Any_Local_Datetime is record
87 Date : Any_Local_Date;
88 Time : Any_Local_Time;
89 end record;
90
91 type Any_Offset_Datetime is record
92 Datetime : Any_Local_Datetime;
93 Offset : Any_Local_Offset;
94 Unknown_Offset : Boolean;
95 end record
96 with Dynamic_Predicate => not Any_Offset_Datetime.Unknown_Offset
97 or else Any_Offset_Datetime.Offset = 0;
98
99 -----------------------
100 -- Generic accessors --
101 -----------------------
102
103 function Is_Null (Value : TOML_Value) return Boolean;
104 -- Return whether Value is a null reference
105
106 function Is_Present (Value : TOML_Value) return Boolean
107 is (not Value.Is_Null);
108
109 function Kind (Value : TOML_Value) return Any_Value_Kind
110 with Pre => Value.Is_Present;
111 -- Return the kind of TOML node for the given Value
112
113 function Equals (Left, Right : TOML_Value) return Boolean
114 with Pre => Left.Is_Present and then Right.Is_Present;
115 -- Return whether Left and Right refer to equivalent TOML documents.
116 --
117 -- Note that this is very different from the built-in "=" operator:
118 -- the TOML_Value type has by-reference meaning, so "=" compares identity,
119 -- not structural equivalence.
120
121 function Clone (Value : TOML_Value) return TOML_Value
122 with Pre => Value.Is_Present;
123 -- Return a reference to a deep copy for Value
124
125 --------------------
126 -- Atom accessors --
127 --------------------
128
129 function As_Boolean (Value : TOML_Value) return Boolean
130 with Pre => Value.Kind = TOML_Boolean;
131 -- Return the boolean that Value represents
132
133 function As_Integer (Value : TOML_Value) return Any_Integer
134 with Pre => Value.Kind = TOML_Integer;
135 -- Return the integer that Value represents
136
137 function As_Float (Value : TOML_Value) return Any_Float
138 with Pre => Value.Kind = TOML_Float;
139 -- Return the float that Value represents
140
141 function As_String (Value : TOML_Value) return String
142 with Pre => Value.Kind = TOML_String;
143 -- Return the string that Value represents
144
145 function As_Unbounded_String
146 (Value : TOML_Value) return Unbounded_UTF8_String
147 with Pre => Value.Kind = TOML_String;
148 -- Likewise, but return an unbounded string
149
150 function As_Offset_Datetime (Value : TOML_Value) return Any_Offset_Datetime
151 with Pre => Value.Kind = TOML_Offset_Datetime;
152 -- Return the offset datetime that Value represents
153
154 function As_Local_Datetime (Value : TOML_Value) return Any_Local_Datetime
155 with Pre => Value.Kind = TOML_Local_Datetime;
156 -- Return the local datetime that Value represents
157
158 function As_Local_Date (Value : TOML_Value) return Any_Local_Date
159 with Pre => Value.Kind = TOML_Local_Date;
160 -- Return the local date that Value represents
161
162 function As_Local_Time (Value : TOML_Value) return Any_Local_Time
163 with Pre => Value.Kind = TOML_Local_Time;
164 -- Return the local time that Value represents
165
166 ---------------------
167 -- Table accessors --
168 ---------------------
169
170 function Has (Value : TOML_Value; Key : String) return Boolean
171 with Pre => Value.Kind = TOML_Table;
172 -- Return whether Value contains an entry for the given Key
173
174 function Has
175 (Value : TOML_Value; Key : Unbounded_UTF8_String) return Boolean
176 with Pre => Value.Kind = TOML_Table;
177 -- Likewise, but take an unbounded string
178
179 type Key_Array is array (Positive range <>) of Unbounded_UTF8_String;
180
181 function Keys (Value : TOML_Value) return Key_Array
182 with Pre => Value.Kind = TOML_Table;
183 -- Return a list for all keys in the given table. Note that the result is
184 -- sorted.
185
186 function Get (Value : TOML_Value; Key : String) return TOML_Value
187 with Pre => Value.Has (Key);
188 -- Return the value for the entry in Value corresponding to Key
189
190 function Get
191 (Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value
192 with Pre => Value.Has (Key);
193 -- Likewise, but take an unbounded string
194
195 function Get_Or_Null (Value : TOML_Value; Key : String) return TOML_Value
196 with Pre => Value.Kind = TOML_Table;
197 -- If there is an entry in the Value table, return its value. Return
198 -- No_TOML_Value otherwise.
199
200 function Get_Or_Null
201 (Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value
202 with Pre => Value.Kind = TOML_Table;
203 -- Likewise, but take an unbounded string
204
205 -- The following types and primitive allow one to iterate on key/value
206 -- entries conveniently in a simple FOR loop.
207
208 type Table_Entry is record
209 Key : Unbounded_UTF8_String;
210 Value : TOML_Value;
211 end record;
212
213 type Table_Entry_Array is array (Positive range <>) of Table_Entry;
214
215 function Iterate_On_Table (Value : TOML_Value) return Table_Entry_Array
216 with Pre => Value.Kind = TOML_Table;
217 -- Return an array of key/value pairs for all entries in Value. The result
218 -- is sorted by key.
219
220 ---------------------
221 -- Array accessors --
222 ---------------------
223
224 function Length (Value : TOML_Value) return Natural
225 with Pre => Value.Kind = TOML_Array;
226 -- Return the number of items in Value
227
228 function Item (Value : TOML_Value; Index : Positive) return TOML_Value
229 with Pre => Value.Kind = TOML_Array and then Index <= Value.Length;
230 -- Return the item in Value at the given Index
231
232 -------------------
233 -- Atom creators --
234 -------------------
235
236 function Create_Boolean (Value : Boolean) return TOML_Value
237 with Post => Create_Boolean'Result.Kind = TOML_Boolean
238 and then Create_Boolean'Result.As_Boolean = Value;
239 -- Create a TOML boolean value
240
241 function Create_Integer (Value : Any_Integer) return TOML_Value
242 with Post => Create_Integer'Result.Kind = TOML_Integer
243 and then Create_Integer'Result.As_Integer = Value;
244 -- Create a TOML integer value
245
246 function Create_Float (Value : Any_Float) return TOML_Value
247 with Post => Create_Float'Result.Kind = TOML_Float
248 and then Create_Float'Result.As_Float = Value;
249 -- Create a TOML integer value
250
251 function Create_String (Value : String) return TOML_Value
252 with Post => Create_String'Result.Kind = TOML_String
253 and then Create_String'Result.As_String = Value;
254 -- Create a TOML string value. Value must be a valid UTF-8 string.
255
256 function Create_String (Value : Unbounded_UTF8_String) return TOML_Value
257 with Post => Create_String'Result.Kind = TOML_String
258 and then Create_String'Result.As_Unbounded_String = Value;
259 -- Create a TOML string value
260
261 function Create_Offset_Datetime
262 (Value : Any_Offset_Datetime) return TOML_Value
263 with Post =>
264 Create_Offset_Datetime'Result.Kind = TOML_Offset_Datetime
265 and then Create_Offset_Datetime'Result.As_Offset_Datetime = Value;
266 -- Create a TOML offset datetime value
267
268 function Create_Local_Datetime
269 (Value : Any_Local_Datetime) return TOML_Value
270 with Post =>
271 Create_Local_Datetime'Result.Kind = TOML_Local_Datetime
272 and then Create_Local_Datetime'Result.As_Local_Datetime = Value;
273 -- Create a TOML local datetime value
274
275 function Create_Local_Date (Value : Any_Local_Date) return TOML_Value
276 with Post => Create_Local_Date'Result.Kind = TOML_Local_Date
277 and then Create_Local_Date'Result.As_Local_Date = Value;
278 -- Create a TOML local date value
279
280 function Create_Local_Time (Value : Any_Local_Time) return TOML_Value
281 with Post => Create_Local_Time'Result.Kind = TOML_Local_Time
282 and then Create_Local_Time'Result.As_Local_Time = Value;
283 -- Create a TOML local date value
284
285 ---------------------
286 -- Table modifiers --
287 ---------------------
288
289 function Create_Table return TOML_Value
290 with Post => Create_Table'Result.Kind = TOML_Table;
291 -- Create an empty TOML table
292
293 procedure Set (Value : TOML_Value; Key : String; Entry_Value : TOML_Value)
294 with Pre => Value.Kind = TOML_Table;
295 -- Create an entry in Value to bind Key to Entry_Value. If Value already
296 -- has an entry for Key, replace it.
297
298 procedure Set
299 (Value : TOML_Value;
300 Key : Unbounded_UTF8_String;
301 Entry_Value : TOML_Value)
302 with Pre => Value.Kind = TOML_Table;
303 -- Likewise, but take an unbounded string
304
305 procedure Set_Default
306 (Value : TOML_Value; Key : String; Entry_Value : TOML_Value)
307 with Pre => Value.Kind = TOML_Table;
308 -- If Value has an entry for Key, do nothing. Otherwise, create an entry
309 -- binding Key to Entry_Value.
310
311 procedure Set_Default
312 (Value : TOML_Value;
313 Key : Unbounded_UTF8_String;
314 Entry_Value : TOML_Value)
315 with Pre => Value.Kind = TOML_Table;
316 -- Likewise, but take an unbounded string
317
318 procedure Unset (Value : TOML_Value; Key : String)
319 with Pre => Value.Kind = TOML_Table and then Value.Has (Key);
320 -- Remove the Key entry in Value
321
322 procedure Unset (Value : TOML_Value; Key : Unbounded_UTF8_String)
323 with Pre => Value.Kind = TOML_Table and then Value.Has (Key);
324 -- Likewise, but take an unbounded string
325
326 function Merge (L, R : TOML_Value) return TOML_Value
327 with Pre => L.Kind = TOML_Table and then R.Kind = TOML_Table,
328 Post => Merge'Result.Kind = TOML_Table;
329 -- Merge two tables. If a key is present in both, Constraint_Error is
330 -- raised. The operation is shallow, so the result table shares values with
331 -- L and R.
332
333 function Merge
334 (L, R : TOML_Value;
335 Merge_Entries : not null access function
336 (Key : Unbounded_UTF8_String; L, R : TOML_Value) return TOML_Value)
337 return TOML_Value
338 with Pre => L.Kind = TOML_Table and then R.Kind = TOML_Table,
339 Post => Merge'Result.Kind = TOML_Table;
340 -- Merge two tables. If a key is present in both, call Merge_Entries to
341 -- resolve the conflict: use its return value for the entry in the returned
342 -- table.
343
344 ---------------------
345 -- Array modifiers --
346 ---------------------
347
348 function Create_Array return TOML_Value
349 with Post => Create_Array'Result.Kind = TOML_Array;
350 -- Create a TOML array
351
352 procedure Set (Value : TOML_Value; Index : Positive; Item : TOML_Value)
353 with Pre => Value.Kind = TOML_Array and then Index <= Value.Length;
354 -- Replace the Index'th item in Value with Item
355
356 procedure Append (Value, Item : TOML_Value)
357 with Pre => Value.Kind = TOML_Array;
358 -- Append Item to the Value array
359
360 procedure Insert_Before
361 (Value : TOML_Value; Index : Positive; Item : TOML_Value)
362 with Pre => Value.Kind = TOML_Array and then Index < Value.Length + 1;
363 -- Insert Item before the Item'th element in the Value array
364
365 ------------------
366 -- Input/Output --
367 ------------------
368
369 type Source_Location is record
370 Line, Column : Natural;
371 end record;
372
373 No_Location : constant Source_Location := (0, 0);
374
375 -- Result of TOML document parsing. If the parsing was successful, contains
376 -- the corresponding TOML value, otherwise, contains an error message that
377 -- describes why parsing failed.
378
379 type Read_Result (Success : Boolean := True) is record
380 case Success is
381 when False =>
382 Message : Unbounded_UTF8_String;
383 Location : Source_Location;
384 when True =>
385 Value : TOML_Value;
386 end case;
387 end record;
388
389 function Load_String (Content : String) return Read_Result;
390 -- Parse Content as a TOML document
391
392 function Dump_As_String (Value : TOML_Value) return String
393 with Pre => Value.Kind = TOML_Table;
394 -- Serialize Value as a valid TOML document
395
396 function Dump_As_Unbounded
397 (Value : TOML_Value) return Unbounded_UTF8_String
398 with Pre => Value.Kind = TOML_Table;
399 -- Likewise, but return an unbounded string
400
401 -- To keep this package preelaborable, subprograms that perform I/O on files
402 -- are found in TOML.File_IO
403
404 function Format_Error (Result : Read_Result) return String
405 with Pre => not Result.Success;
406 -- Format the error information in Result into a GNU-style diagnostic
407
408 private
409
410 type TOML_Value_Record;
411 type TOML_Value_Record_Access is access all TOML_Value_Record;
412
413 type TOML_Value is new Ada.Finalization.Controlled with record
414 Value : TOML_Value_Record_Access;
415 end record;
416
417 overriding procedure Adjust (Self : in out TOML_Value);
418 overriding procedure Finalize (Self : in out TOML_Value);
419
420 No_TOML_Value : constant TOML_Value := (Ada.Finalization.Controlled
421 with Value => null);
422
423 function Create_Error
424 (Message : String; Location : Source_Location) return Read_Result;
425 -- Create an unsuccessful Read_Result value with the provided error
426 -- information.
427
428 function Implicitly_Created (Self : TOML_Value'Class) return Boolean
429 with Pre => Self.Kind in TOML_Table | TOML_Array;
430 -- Helper for parsing. Return whether Self was created implicitly
431
432 procedure Set_Implicitly_Created (Self : TOML_Value'Class)
433 with Pre => Self.Kind in TOML_Table | TOML_Array;
434 -- Make future calls to Implicitly_Created return True for Self
435
436 procedure Set_Explicitly_Created (Self : TOML_Value'Class)
437 with Pre => Self.Kind in TOML_Table | TOML_Array;
438 -- Make future calls to Implicitly_Created return False for Self
439
440 end TOML;