ada_toml_0.3.0_862d230b/src/toml.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
with Ada.Containers.Generic_Array_Sort;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded.Hash;
with Ada.Unchecked_Deallocation;

with TOML.Generic_Dump;
with TOML.Generic_Parse;

package body TOML is

   use Ada.Strings.Unbounded;

   procedure Dump_To_String is new TOML.Generic_Dump
     (Output_Stream => Unbounded_UTF8_String,
      Put           => Append);

   procedure Sort_Keys is new Ada.Containers.Generic_Array_Sort
     (Index_Type   => Positive,
      Element_Type => Unbounded_UTF8_String,
      Array_Type   => Key_Array);

   package TOML_Maps is new Ada.Containers.Hashed_Maps
     (Key_Type        => Unbounded_String,
      Element_Type    => TOML_Value,
      Hash            => Hash,
      Equivalent_Keys => "=");

   package TOML_Vectors is new Ada.Containers.Vectors (Positive, TOML_Value);

   type TOML_Value_Record (Kind : Any_Value_Kind) is limited record
      Ref_Count : Natural;

      case Kind is
         when TOML_Table =>
            Map_Value : TOML_Maps.Map;

            Table_Implicitly_Created : Boolean;
            --  Helper for parsing: true iff this table was created implictly
            --  when parsing a sub-table. For instance, this is true for the
            --  table "a" if we read "[a.b]" but not yet "[a]".

         when TOML_Array =>
            Array_Value : TOML_Vectors.Vector;
            --  List of values for all items

            Array_Implicitly_Created : Boolean;
            --  Same as Table_Implicitly_Created

         when TOML_String =>
            String_Value : Unbounded_String;

         when TOML_Integer =>
            Integer_Value : Any_Integer;

         when TOML_Float =>
            Float_Value : Any_Float;

         when TOML_Boolean =>
            Boolean_Value : Boolean;

         when TOML_Offset_Datetime =>
            Offset_Datetime_Value : Any_Offset_Datetime;

         when TOML_Local_Datetime =>
            Local_Datetime_Value : Any_Local_Datetime;

         when TOML_Local_Date =>
            Local_Date_Value : Any_Local_Date;

         when TOML_Local_Time =>
            Local_Time_Value : Any_Local_Time;
      end case;
   end record;

   procedure Free is new Ada.Unchecked_Deallocation
     (TOML_Value_Record, TOML_Value_Record_Access);

   function Create_Value (Rec : TOML_Value_Record_Access) return TOML_Value;
   --  Wrap a value record in a value. This resets its ref-count to 1.

   ------------------
   -- Create_Value --
   ------------------

   function Create_Value (Rec : TOML_Value_Record_Access) return TOML_Value is
   begin
      return Result : TOML_Value do
         Rec.Ref_Count := 1;
         Result.Value := Rec;
      end return;
   end Create_Value;

   ------------------
   -- Create_Error --
   ------------------

   function Create_Error
     (Message : String; Location : Source_Location) return Read_Result is
   begin
      return
        (Success  => False,
         Message  => To_Unbounded_String (Message),
         Location => Location);
   end Create_Error;

   -------------
   -- Is_Null --
   -------------

   function Is_Null (Value : TOML_Value) return Boolean is
   begin
      return Value.Value = null;
   end Is_Null;

   ----------
   -- Kind --
   ----------

   function Kind (Value : TOML_Value) return Any_Value_Kind is
   begin
      return Value.Value.Kind;
   end Kind;

   ------------
   -- Equals --
   ------------

   function Equals (Left, Right : TOML_Value) return Boolean is
   begin
      --  If Left and Right refer to the same document, they are obviously
      --  equivalent (X is equivalent to X). If they don't have the same kind,
      --  they are obviously not equivalent.

      if Left = Right then
         return True;
      elsif Left.Kind /= Right.Kind then
         return False;
      end if;

      case Left.Kind is
         when TOML_Table =>
            declare
               Left_Keys  : constant Key_Array := Left.Keys;
               Right_Keys : constant Key_Array := Right.Keys;
            begin
               if Left_Keys /= Right_Keys then
                  return False;
               end if;

               for K of Left_Keys loop
                  if not Equals (Left.Get (K), Right.Get (K)) then
                     return False;
                  end if;
               end loop;
            end;

         when TOML_Array =>
            if Left.Length /= Right.Length then
               return False;
            end if;

            for I in 1 .. Left.Length loop
               if not Equals (Left.Item (I), Right.Item (I)) then
                  return False;
               end if;
            end loop;

         when TOML_String =>
            return Left.Value.String_Value = Right.Value.String_Value;

         when TOML_Integer =>
            return Left.Value.Integer_Value = Right.Value.Integer_Value;

         when TOML_Boolean =>
            return Left.Value.Boolean_Value = Right.Value.Boolean_Value;

         when TOML_Offset_Datetime =>
            return Left.Value.Offset_Datetime_Value
                   = Right.Value.Offset_Datetime_Value;

         when TOML_Local_Datetime =>
            return Left.Value.Local_Datetime_Value
                   = Right.Value.Local_Datetime_Value;

         when TOML_Local_Date =>
            return Left.Value.Local_Date_Value = Right.Value.Local_Date_Value;

         when TOML_Local_Time =>
            return Left.Value.Local_Time_Value = Right.Value.Local_Time_Value;

         when TOML_Float =>
            return Left.Value.Float_Value = Right.Value.Float_Value;
      end case;

      return True;
   end Equals;

   -----------
   -- Clone --
   -----------

   function Clone (Value : TOML_Value) return TOML_Value is
      Result : TOML_Value;
   begin
      case Value.Kind is
         when TOML_Table =>
            Result := Create_Table;
            for Key of Value.Keys loop
               Result.Set (Key, Value.Get (Key).Clone);
            end loop;

         when TOML_Array =>
            Result := Create_Array;
            for I in 1 .. Value.Length loop
               Result.Append (Value.Item (I));
            end loop;

         when TOML_String =>
            Result := Create_String (Value.Value.String_Value);

         when TOML_Integer =>
            Result := Create_Integer (Value.Value.Integer_Value);

         when TOML_Boolean =>
            Result := Create_Boolean (Value.Value.Boolean_Value);

         when TOML_Offset_Datetime =>
            Result :=
               Create_Offset_Datetime (Value.Value.Offset_Datetime_Value);

         when TOML_Local_Datetime =>
            Result := Create_Local_Datetime (Value.Value.Local_Datetime_Value);

         when TOML_Local_Date =>
            Result := Create_Local_Date (Value.Value.Local_Date_Value);

         when TOML_Local_Time =>
            Result := Create_Local_Time (Value.Value.Local_Time_Value);

         when TOML_Float =>
            Result := Create_Float (Value.Value.Float_Value);
      end case;

      return Result;
   end Clone;

   ----------------
   -- As_Boolean --
   ----------------

   function As_Boolean (Value : TOML_Value) return Boolean is
   begin
      return Value.Value.Boolean_Value;
   end As_Boolean;

   ----------------
   -- As_Integer --
   ----------------

   function As_Integer (Value : TOML_Value) return Any_Integer is
   begin
      return Value.Value.Integer_Value;
   end As_Integer;

   --------------
   -- As_Float --
   --------------

   function As_Float (Value : TOML_Value) return Any_Float is
   begin
      return Value.Value.Float_Value;
   end As_Float;

   ---------------
   -- As_String --
   ---------------

   function As_String (Value : TOML_Value) return String is
   begin
      return To_String (Value.As_Unbounded_String);
   end As_String;

   -------------------------
   -- As_Unbounded_String --
   -------------------------

   function As_Unbounded_String
     (Value : TOML_Value) return Ada.Strings.Unbounded.Unbounded_String is
   begin
      return Value.Value.String_Value;
   end As_Unbounded_String;

   ------------------------
   -- As_Offset_Datetime --
   ------------------------

   function As_Offset_Datetime (Value : TOML_Value) return Any_Offset_Datetime
   is
   begin
      return Value.Value.Offset_Datetime_Value;
   end As_Offset_Datetime;

   -----------------------
   -- As_Local_Datetime --
   -----------------------

   function As_Local_Datetime (Value : TOML_Value) return Any_Local_Datetime is
   begin
      return Value.Value.Local_Datetime_Value;
   end As_Local_Datetime;

   -------------------
   -- As_Local_Date --
   -------------------

   function As_Local_Date (Value : TOML_Value) return Any_Local_Date is
   begin
      return Value.Value.Local_Date_Value;
   end As_Local_Date;

   -------------------
   -- As_Local_Time --
   -------------------

   function As_Local_Time (Value : TOML_Value) return Any_Local_Time is
   begin
      return Value.Value.Local_Time_Value;
   end As_Local_Time;

   ---------
   -- Has --
   ---------

   function Has (Value : TOML_Value; Key : String) return Boolean is
   begin
      return Value.Has (To_Unbounded_String (Key));
   end Has;

   ---------
   -- Has --
   ---------

   function Has
     (Value : TOML_Value; Key : Unbounded_UTF8_String) return Boolean is
   begin
      return Value.Value.Map_Value.Contains (Key);
   end Has;

   ----------
   -- Keys --
   ----------

   function Keys (Value : TOML_Value) return Key_Array is
      use TOML_Maps;
      Map : TOML_Maps.Map renames Value.Value.Map_Value;
      I   : Positive := 1;
   begin
      return Result : Key_Array (1 .. Natural (Map.Length)) do
         for Position in Map.Iterate loop
            Result (I) := Key (Position);
            I := I + 1;
         end loop;
         Sort_Keys (Result);
      end return;
   end Keys;

   ---------
   -- Get --
   ---------

   function Get (Value : TOML_Value; Key : String) return TOML_Value is
   begin
      return Value.Get (To_Unbounded_String (Key));
   end Get;

   ---------
   -- Get --
   ---------

   function Get
     (Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value is
   begin
      return Value.Value.Map_Value.Element (Key);
   end Get;

   -----------------
   -- Get_Or_Null --
   -----------------

   function Get_Or_Null (Value : TOML_Value; Key : String) return TOML_Value
   is
   begin
      return Value.Get_Or_Null (To_Unbounded_String (Key));
   end Get_Or_Null;

   -----------------
   -- Get_Or_Null --
   -----------------

   function Get_Or_Null
     (Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value
   is
      use TOML_Maps;
      Position : constant Cursor := Value.Value.Map_Value.Find (Key);
   begin
      return (if Has_Element (Position)
              then Element (Position)
              else No_TOML_Value);
   end Get_Or_Null;

   ----------------------
   -- Iterate_On_Table --
   ----------------------

   function Iterate_On_Table (Value : TOML_Value) return Table_Entry_Array is
      Keys : constant Key_Array := Value.Keys;
   begin
      return Result : Table_Entry_Array (Keys'Range) do
         for I In Keys'Range loop
            Result (I) := (Keys (I), Value.Get (Keys (I)));
         end loop;
      end return;
   end Iterate_On_Table;

   ------------
   -- Length --
   ------------

   function Length (Value : TOML_Value) return Natural is
   begin
      return Natural (Value.Value.Array_Value.Length);
   end Length;

   ----------
   -- Item --
   ----------

   function Item (Value : TOML_Value; Index : Positive) return TOML_Value is
   begin
      return Value.Value.Array_Value.Element (Index);
   end Item;

   --------------------
   -- Create_Boolean --
   --------------------

   function Create_Boolean (Value : Boolean) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind => TOML_Boolean, Ref_Count => 1, Boolean_Value => Value));
   end Create_Boolean;

   --------------------
   -- Create_Integer --
   --------------------

   function Create_Integer (Value : Any_Integer) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind => TOML_Integer, Ref_Count => 1, Integer_Value => Value));
   end Create_Integer;

   ------------------
   -- Create_Float --
   ------------------

   function Create_Float (Value : Any_Float) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind => TOML_Float, Ref_Count => 1, Float_Value => Value));
   end Create_Float;

   -------------------
   -- Create_String --
   -------------------

   function Create_String (Value : String) return TOML_Value is
   begin
      return Create_String (To_Unbounded_String (Value));
   end Create_String;

   -------------------
   -- Create_String --
   -------------------

   function Create_String (Value : Unbounded_UTF8_String) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind         => TOML_String,
         Ref_Count    => 1,
         String_Value => Value));
   end Create_String;

   ----------------------------
   -- Create_Offset_Datetime --
   ----------------------------

   function Create_Offset_Datetime
     (Value : Any_Offset_Datetime) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind                  => TOML_Offset_Datetime,
         Ref_Count             => 1,
         Offset_Datetime_Value => Value));
   end Create_Offset_Datetime;

   ---------------------------
   -- Create_Local_Datetime --
   ---------------------------

   function Create_Local_Datetime
     (Value : Any_Local_Datetime) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind                 => TOML_Local_Datetime,
         Ref_Count            => 1,
         Local_Datetime_Value => Value));
   end Create_Local_Datetime;

   -----------------------
   -- Create_Local_Date --
   -----------------------

   function Create_Local_Date (Value : Any_Local_Date) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind             => TOML_Local_Date,
         Ref_Count        => 1,
         Local_Date_Value => Value));
   end Create_Local_Date;

   -----------------------
   -- Create_Local_Time --
   -----------------------

   function Create_Local_Time (Value : Any_Local_Time) return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind             => TOML_Local_Time,
         Ref_Count        => 1,
         Local_Time_Value => Value));
   end Create_Local_Time;

   ------------------
   -- Create_Table --
   ------------------

   function Create_Table return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind                     => TOML_Table,
         Ref_Count                => 1,
         Map_Value                => <>,
         Table_Implicitly_Created => False));
   end Create_Table;

   ---------
   -- Set --
   ---------

   procedure Set (Value : TOML_Value; Key : String; Entry_Value : TOML_Value)
   is
   begin
      Value.Set (To_Unbounded_String (Key), Entry_Value);
   end Set;

   ---------
   -- Set --
   ---------

   procedure Set
     (Value       : TOML_Value;
      Key         : Unbounded_UTF8_String;
      Entry_Value : TOML_Value)
   is
   begin
      Value.Value.Map_Value.Include (Key, Entry_Value);
   end Set;

   -----------------
   -- Set_Default --
   -----------------

   procedure Set_Default
     (Value : TOML_Value; Key : String; Entry_Value : TOML_Value)
   is
   begin
      Value.Set_Default (To_Unbounded_String (Key), Entry_Value);
   end Set_Default;

   -----------------
   -- Set_Default --
   -----------------

   procedure Set_Default
     (Value       : TOML_Value;
      Key         : Unbounded_UTF8_String;
      Entry_Value : TOML_Value)
   is
      use TOML_Maps;
      Dummy_Position : Cursor;
      Dummy_Inserted : Boolean;
   begin
      Value.Value.Map_Value.Insert
        (Key, Entry_Value, Dummy_Position, Dummy_Inserted);
   end Set_Default;

   -----------
   -- Unset --
   -----------

   procedure Unset (Value : TOML_Value; Key : String) is
   begin
      Value.Unset (To_Unbounded_String (Key));
   end Unset;

   -----------
   -- Unset --
   -----------

   procedure Unset (Value : TOML_Value; Key : Unbounded_UTF8_String) is
   begin
      Value.Value.Map_Value.Delete (Key);
   end Unset;

   -----------
   -- Merge --
   -----------

   function Merge (L, R : TOML_Value) return TOML_Value is
      function Merge_Entries
        (Key              : Unbounded_UTF8_String;
         Dummy_L, Dummy_R : TOML_Value) return TOML_Value
      is (raise Constraint_Error with "duplicate key: " & To_String (Key));
   begin
      return Merge (L, R, Merge_Entries'Access);
   end Merge;

   -----------
   -- Merge --
   -----------

   function Merge
     (L, R          : TOML_Value;
      Merge_Entries : not null access function
        (Key : Unbounded_UTF8_String; L, R : TOML_Value) return TOML_Value)
      return TOML_Value
   is
      Table : constant TOML_Value := Create_Table;
   begin
      for Key of L.Keys loop
         Table.Set (Key, L.Get (Key));
      end loop;

      for Key of R.Keys loop
         declare
            Value : TOML_Value := R.Get (Key);
         begin
            if Table.Has (Key) then
               Value := Merge_Entries (Key, Table.Get (Key), Value);
            end if;
            Table.Set (Key, Value);
         end;
      end loop;

      return Table;
   end Merge;

   ------------------
   -- Create_Array --
   ------------------

   function Create_Array return TOML_Value is
   begin
      return Create_Value (new TOML_Value_Record'
        (Kind                     => TOML_Array,
         Ref_Count                => 1,
         Array_Value              => <>,
         Array_Implicitly_Created => False));
   end Create_Array;

   ---------
   -- Set --
   ---------

   procedure Set (Value : TOML_Value; Index : Positive; Item : TOML_Value) is
   begin
      Value.Value.Array_Value (Index) := Item;
   end Set;

   ------------
   -- Append --
   ------------

   procedure Append (Value, Item : TOML_Value) is
   begin
      Value.Value.Array_Value.Append (Item);
   end Append;

   -------------------
   -- Insert_Before --
   -------------------

   procedure Insert_Before
     (Value : TOML_Value; Index : Positive; Item : TOML_Value)
   is
   begin
      Value.Value.Array_Value.Insert (Index, Item);
   end Insert_Before;

   -----------------
   -- Load_String --
   -----------------

   function Load_String (Content : String) return Read_Result is
      type Input_Stream is record
         Next_Character : Positive;
         --  Index of the next character in Content that Get must return
      end record;

      procedure Get
        (Stream : in out Input_Stream;
         EOF    : out Boolean;
         Byte   : out Character);
      --  Callback for Parse_String

      ---------
      -- Get --
      ---------

      procedure Get
        (Stream : in out Input_Stream;
         EOF    : out Boolean;
         Byte   : out Character) is
      begin
         if Stream.Next_Character > Content'Length then
            EOF := True;
         else
            EOF := False;
            Byte := Content (Stream.Next_Character);
            Stream.Next_Character := Stream.Next_Character + 1;
         end if;
      end Get;

      function Parse_String is new TOML.Generic_Parse (Input_Stream, Get);

      Stream : Input_Stream := (Next_Character => Content'First);
   begin
      return Parse_String (Stream);
   end Load_String;

   --------------------
   -- Dump_As_String --
   --------------------

   function Dump_As_String (Value : TOML_Value) return String is
   begin
      return To_String (Dump_As_Unbounded (Value));
   end Dump_As_String;

   -----------------------
   -- Dump_As_Unbounded --
   -----------------------

   function Dump_As_Unbounded
     (Value : TOML_Value) return Unbounded_UTF8_String is
   begin
      return Result : Unbounded_UTF8_String do
         Dump_To_String (Result, Value);
      end return;
   end Dump_As_Unbounded;

   ------------------
   -- Format_Error --
   ------------------

   function Format_Error (Result : Read_Result) return String is
      Formatted : Unbounded_UTF8_String;
   begin
      if Result.Location.Line /= 0 then
         declare
            L : constant String := Result.Location.Line'Image;
            C : constant String := Result.Location.Column'Image;
         begin
            Append (Formatted, L (L'First + 1 .. L'Last));
            Append (Formatted, ":");
            Append (Formatted, C (C'First + 1 .. C'Last));
            Append (Formatted, ": ");
         end;
      end if;

      Append (Formatted, Result.Message);
      return To_String (Formatted);
   end Format_Error;

   ------------
   -- Adjust --
   ------------

   overriding procedure Adjust (Self : in out TOML_Value) is
   begin
      if Self.Value = null then
         return;
      end if;

      Self.Value.Ref_Count := Self.Value.Ref_Count + 1;
   end Adjust;

   --------------
   -- Finalize --
   --------------

   overriding procedure Finalize (Self : in out TOML_Value) is
   begin
      if Self.Value = null then
         return;
      end if;

      declare
         V : TOML_Value_Record renames Self.Value.all;
      begin
         --  Decrement the ref-count. If no-one references V anymore,
         --  deallocate it.

         V.Ref_Count := V.Ref_Count - 1;
         if V.Ref_Count > 0 then
            return;
         end if;
      end;

      Free (Self.Value);
   end Finalize;

   ------------------------
   -- Implicitly_Created --
   ------------------------

   function Implicitly_Created (Self : TOML_Value'Class) return Boolean is
   begin
      if Self.Kind = TOML_Table then
         return Self.Value.Table_Implicitly_Created;
      else
         return Self.Value.Array_Implicitly_Created;
      end if;
   end Implicitly_Created;

   ----------------------------
   -- Set_Implicitly_Created --
   ----------------------------

   procedure Set_Implicitly_Created (Self : TOML_Value'Class) is
   begin
      if Self.Kind = TOML_Table then
         Self.Value.Table_Implicitly_Created := True;
      else
         Self.Value.Array_Implicitly_Created := True;
      end if;
   end Set_Implicitly_Created;

   ----------------------------
   -- Set_Explicitly_Created --
   ----------------------------

   procedure Set_Explicitly_Created (Self : TOML_Value'Class) is
   begin
      if Self.Kind = TOML_Table then
         Self.Value.Table_Implicitly_Created := False;
      else
         Self.Value.Array_Implicitly_Created := False;
      end if;
   end Set_Explicitly_Created;

end TOML;