| 1 |
with Ada.Containers.Generic_Array_Sort; |
| 2 |
with Ada.Containers.Hashed_Maps; |
| 3 |
with Ada.Containers.Vectors; |
| 4 |
with Ada.Strings.Unbounded.Hash; |
| 5 |
with Ada.Unchecked_Deallocation; |
| 6 |
|
| 7 |
with TOML.Generic_Dump; |
| 8 |
with TOML.Generic_Parse; |
| 9 |
|
| 10 |
package body TOML is |
| 11 |
|
| 12 |
use Ada.Strings.Unbounded; |
| 13 |
|
| 14 |
procedure Dump_To_String is new TOML.Generic_Dump |
| 15 |
(Output_Stream => Unbounded_UTF8_String, |
| 16 |
Put => Append); |
| 17 |
|
| 18 |
procedure Sort_Keys is new Ada.Containers.Generic_Array_Sort |
| 19 |
(Index_Type => Positive, |
| 20 |
Element_Type => Unbounded_UTF8_String, |
| 21 |
Array_Type => Key_Array); |
| 22 |
|
| 23 |
package TOML_Maps is new Ada.Containers.Hashed_Maps |
| 24 |
(Key_Type => Unbounded_String, |
| 25 |
Element_Type => TOML_Value, |
| 26 |
Hash => Hash, |
| 27 |
Equivalent_Keys => "="); |
| 28 |
|
| 29 |
package TOML_Vectors is new Ada.Containers.Vectors (Positive, TOML_Value); |
| 30 |
|
| 31 |
type TOML_Value_Record (Kind : Any_Value_Kind) is limited record |
| 32 |
Ref_Count : Natural; |
| 33 |
|
| 34 |
case Kind is |
| 35 |
when TOML_Table => |
| 36 |
Map_Value : TOML_Maps.Map; |
| 37 |
|
| 38 |
Table_Implicitly_Created : Boolean; |
| 39 |
-- Helper for parsing: true iff this table was created implictly |
| 40 |
-- when parsing a sub-table. For instance, this is true for the |
| 41 |
-- table "a" if we read "[a.b]" but not yet "[a]". |
| 42 |
|
| 43 |
when TOML_Array => |
| 44 |
Array_Value : TOML_Vectors.Vector; |
| 45 |
-- List of values for all items |
| 46 |
|
| 47 |
Array_Implicitly_Created : Boolean; |
| 48 |
-- Same as Table_Implicitly_Created |
| 49 |
|
| 50 |
when TOML_String => |
| 51 |
String_Value : Unbounded_String; |
| 52 |
|
| 53 |
when TOML_Integer => |
| 54 |
Integer_Value : Any_Integer; |
| 55 |
|
| 56 |
when TOML_Float => |
| 57 |
Float_Value : Any_Float; |
| 58 |
|
| 59 |
when TOML_Boolean => |
| 60 |
Boolean_Value : Boolean; |
| 61 |
|
| 62 |
when TOML_Offset_Datetime => |
| 63 |
Offset_Datetime_Value : Any_Offset_Datetime; |
| 64 |
|
| 65 |
when TOML_Local_Datetime => |
| 66 |
Local_Datetime_Value : Any_Local_Datetime; |
| 67 |
|
| 68 |
when TOML_Local_Date => |
| 69 |
Local_Date_Value : Any_Local_Date; |
| 70 |
|
| 71 |
when TOML_Local_Time => |
| 72 |
Local_Time_Value : Any_Local_Time; |
| 73 |
end case; |
| 74 |
end record; |
| 75 |
|
| 76 |
procedure Free is new Ada.Unchecked_Deallocation |
| 77 |
(TOML_Value_Record, TOML_Value_Record_Access); |
| 78 |
|
| 79 |
function Create_Value (Rec : TOML_Value_Record_Access) return TOML_Value; |
| 80 |
-- Wrap a value record in a value. This resets its ref-count to 1. |
| 81 |
|
| 82 |
------------------ |
| 83 |
-- Create_Value -- |
| 84 |
------------------ |
| 85 |
|
| 86 |
function Create_Value (Rec : TOML_Value_Record_Access) return TOML_Value is |
| 87 |
begin |
| 88 |
return Result : TOML_Value do |
| 89 |
Rec.Ref_Count := 1; |
| 90 |
Result.Value := Rec; |
| 91 |
end return; |
| 92 |
end Create_Value; |
| 93 |
|
| 94 |
------------------ |
| 95 |
-- Create_Error -- |
| 96 |
------------------ |
| 97 |
|
| 98 |
function Create_Error |
| 99 |
(Message : String; Location : Source_Location) return Read_Result is |
| 100 |
begin |
| 101 |
return |
| 102 |
(Success => False, |
| 103 |
Message => To_Unbounded_String (Message), |
| 104 |
Location => Location); |
| 105 |
end Create_Error; |
| 106 |
|
| 107 |
------------- |
| 108 |
-- Is_Null -- |
| 109 |
------------- |
| 110 |
|
| 111 |
function Is_Null (Value : TOML_Value) return Boolean is |
| 112 |
begin |
| 113 |
return Value.Value = null; |
| 114 |
end Is_Null; |
| 115 |
|
| 116 |
---------- |
| 117 |
-- Kind -- |
| 118 |
---------- |
| 119 |
|
| 120 |
function Kind (Value : TOML_Value) return Any_Value_Kind is |
| 121 |
begin |
| 122 |
return Value.Value.Kind; |
| 123 |
end Kind; |
| 124 |
|
| 125 |
------------ |
| 126 |
-- Equals -- |
| 127 |
------------ |
| 128 |
|
| 129 |
function Equals (Left, Right : TOML_Value) return Boolean is |
| 130 |
begin |
| 131 |
-- If Left and Right refer to the same document, they are obviously |
| 132 |
-- equivalent (X is equivalent to X). If they don't have the same kind, |
| 133 |
-- they are obviously not equivalent. |
| 134 |
|
| 135 |
if Left = Right then |
| 136 |
return True; |
| 137 |
elsif Left.Kind /= Right.Kind then |
| 138 |
return False; |
| 139 |
end if; |
| 140 |
|
| 141 |
case Left.Kind is |
| 142 |
when TOML_Table => |
| 143 |
declare |
| 144 |
Left_Keys : constant Key_Array := Left.Keys; |
| 145 |
Right_Keys : constant Key_Array := Right.Keys; |
| 146 |
begin |
| 147 |
if Left_Keys /= Right_Keys then |
| 148 |
return False; |
| 149 |
end if; |
| 150 |
|
| 151 |
for K of Left_Keys loop |
| 152 |
if not Equals (Left.Get (K), Right.Get (K)) then |
| 153 |
return False; |
| 154 |
end if; |
| 155 |
end loop; |
| 156 |
end; |
| 157 |
|
| 158 |
when TOML_Array => |
| 159 |
if Left.Length /= Right.Length then |
| 160 |
return False; |
| 161 |
end if; |
| 162 |
|
| 163 |
for I in 1 .. Left.Length loop |
| 164 |
if not Equals (Left.Item (I), Right.Item (I)) then |
| 165 |
return False; |
| 166 |
end if; |
| 167 |
end loop; |
| 168 |
|
| 169 |
when TOML_String => |
| 170 |
return Left.Value.String_Value = Right.Value.String_Value; |
| 171 |
|
| 172 |
when TOML_Integer => |
| 173 |
return Left.Value.Integer_Value = Right.Value.Integer_Value; |
| 174 |
|
| 175 |
when TOML_Boolean => |
| 176 |
return Left.Value.Boolean_Value = Right.Value.Boolean_Value; |
| 177 |
|
| 178 |
when TOML_Offset_Datetime => |
| 179 |
return Left.Value.Offset_Datetime_Value |
| 180 |
= Right.Value.Offset_Datetime_Value; |
| 181 |
|
| 182 |
when TOML_Local_Datetime => |
| 183 |
return Left.Value.Local_Datetime_Value |
| 184 |
= Right.Value.Local_Datetime_Value; |
| 185 |
|
| 186 |
when TOML_Local_Date => |
| 187 |
return Left.Value.Local_Date_Value = Right.Value.Local_Date_Value; |
| 188 |
|
| 189 |
when TOML_Local_Time => |
| 190 |
return Left.Value.Local_Time_Value = Right.Value.Local_Time_Value; |
| 191 |
|
| 192 |
when TOML_Float => |
| 193 |
return Left.Value.Float_Value = Right.Value.Float_Value; |
| 194 |
end case; |
| 195 |
|
| 196 |
return True; |
| 197 |
end Equals; |
| 198 |
|
| 199 |
----------- |
| 200 |
-- Clone -- |
| 201 |
----------- |
| 202 |
|
| 203 |
function Clone (Value : TOML_Value) return TOML_Value is |
| 204 |
Result : TOML_Value; |
| 205 |
begin |
| 206 |
case Value.Kind is |
| 207 |
when TOML_Table => |
| 208 |
Result := Create_Table; |
| 209 |
for Key of Value.Keys loop |
| 210 |
Result.Set (Key, Value.Get (Key).Clone); |
| 211 |
end loop; |
| 212 |
|
| 213 |
when TOML_Array => |
| 214 |
Result := Create_Array; |
| 215 |
for I in 1 .. Value.Length loop |
| 216 |
Result.Append (Value.Item (I)); |
| 217 |
end loop; |
| 218 |
|
| 219 |
when TOML_String => |
| 220 |
Result := Create_String (Value.Value.String_Value); |
| 221 |
|
| 222 |
when TOML_Integer => |
| 223 |
Result := Create_Integer (Value.Value.Integer_Value); |
| 224 |
|
| 225 |
when TOML_Boolean => |
| 226 |
Result := Create_Boolean (Value.Value.Boolean_Value); |
| 227 |
|
| 228 |
when TOML_Offset_Datetime => |
| 229 |
Result := |
| 230 |
Create_Offset_Datetime (Value.Value.Offset_Datetime_Value); |
| 231 |
|
| 232 |
when TOML_Local_Datetime => |
| 233 |
Result := Create_Local_Datetime (Value.Value.Local_Datetime_Value); |
| 234 |
|
| 235 |
when TOML_Local_Date => |
| 236 |
Result := Create_Local_Date (Value.Value.Local_Date_Value); |
| 237 |
|
| 238 |
when TOML_Local_Time => |
| 239 |
Result := Create_Local_Time (Value.Value.Local_Time_Value); |
| 240 |
|
| 241 |
when TOML_Float => |
| 242 |
Result := Create_Float (Value.Value.Float_Value); |
| 243 |
end case; |
| 244 |
|
| 245 |
return Result; |
| 246 |
end Clone; |
| 247 |
|
| 248 |
---------------- |
| 249 |
-- As_Boolean -- |
| 250 |
---------------- |
| 251 |
|
| 252 |
function As_Boolean (Value : TOML_Value) return Boolean is |
| 253 |
begin |
| 254 |
return Value.Value.Boolean_Value; |
| 255 |
end As_Boolean; |
| 256 |
|
| 257 |
---------------- |
| 258 |
-- As_Integer -- |
| 259 |
---------------- |
| 260 |
|
| 261 |
function As_Integer (Value : TOML_Value) return Any_Integer is |
| 262 |
begin |
| 263 |
return Value.Value.Integer_Value; |
| 264 |
end As_Integer; |
| 265 |
|
| 266 |
-------------- |
| 267 |
-- As_Float -- |
| 268 |
-------------- |
| 269 |
|
| 270 |
function As_Float (Value : TOML_Value) return Any_Float is |
| 271 |
begin |
| 272 |
return Value.Value.Float_Value; |
| 273 |
end As_Float; |
| 274 |
|
| 275 |
--------------- |
| 276 |
-- As_String -- |
| 277 |
--------------- |
| 278 |
|
| 279 |
function As_String (Value : TOML_Value) return String is |
| 280 |
begin |
| 281 |
return To_String (Value.As_Unbounded_String); |
| 282 |
end As_String; |
| 283 |
|
| 284 |
------------------------- |
| 285 |
-- As_Unbounded_String -- |
| 286 |
------------------------- |
| 287 |
|
| 288 |
function As_Unbounded_String |
| 289 |
(Value : TOML_Value) return Ada.Strings.Unbounded.Unbounded_String is |
| 290 |
begin |
| 291 |
return Value.Value.String_Value; |
| 292 |
end As_Unbounded_String; |
| 293 |
|
| 294 |
------------------------ |
| 295 |
-- As_Offset_Datetime -- |
| 296 |
------------------------ |
| 297 |
|
| 298 |
function As_Offset_Datetime (Value : TOML_Value) return Any_Offset_Datetime |
| 299 |
is |
| 300 |
begin |
| 301 |
return Value.Value.Offset_Datetime_Value; |
| 302 |
end As_Offset_Datetime; |
| 303 |
|
| 304 |
----------------------- |
| 305 |
-- As_Local_Datetime -- |
| 306 |
----------------------- |
| 307 |
|
| 308 |
function As_Local_Datetime (Value : TOML_Value) return Any_Local_Datetime is |
| 309 |
begin |
| 310 |
return Value.Value.Local_Datetime_Value; |
| 311 |
end As_Local_Datetime; |
| 312 |
|
| 313 |
------------------- |
| 314 |
-- As_Local_Date -- |
| 315 |
------------------- |
| 316 |
|
| 317 |
function As_Local_Date (Value : TOML_Value) return Any_Local_Date is |
| 318 |
begin |
| 319 |
return Value.Value.Local_Date_Value; |
| 320 |
end As_Local_Date; |
| 321 |
|
| 322 |
------------------- |
| 323 |
-- As_Local_Time -- |
| 324 |
------------------- |
| 325 |
|
| 326 |
function As_Local_Time (Value : TOML_Value) return Any_Local_Time is |
| 327 |
begin |
| 328 |
return Value.Value.Local_Time_Value; |
| 329 |
end As_Local_Time; |
| 330 |
|
| 331 |
--------- |
| 332 |
-- Has -- |
| 333 |
--------- |
| 334 |
|
| 335 |
function Has (Value : TOML_Value; Key : String) return Boolean is |
| 336 |
begin |
| 337 |
return Value.Has (To_Unbounded_String (Key)); |
| 338 |
end Has; |
| 339 |
|
| 340 |
--------- |
| 341 |
-- Has -- |
| 342 |
--------- |
| 343 |
|
| 344 |
function Has |
| 345 |
(Value : TOML_Value; Key : Unbounded_UTF8_String) return Boolean is |
| 346 |
begin |
| 347 |
return Value.Value.Map_Value.Contains (Key); |
| 348 |
end Has; |
| 349 |
|
| 350 |
---------- |
| 351 |
-- Keys -- |
| 352 |
---------- |
| 353 |
|
| 354 |
function Keys (Value : TOML_Value) return Key_Array is |
| 355 |
use TOML_Maps; |
| 356 |
Map : TOML_Maps.Map renames Value.Value.Map_Value; |
| 357 |
I : Positive := 1; |
| 358 |
begin |
| 359 |
return Result : Key_Array (1 .. Natural (Map.Length)) do |
| 360 |
for Position in Map.Iterate loop |
| 361 |
Result (I) := Key (Position); |
| 362 |
I := I + 1; |
| 363 |
end loop; |
| 364 |
Sort_Keys (Result); |
| 365 |
end return; |
| 366 |
end Keys; |
| 367 |
|
| 368 |
--------- |
| 369 |
-- Get -- |
| 370 |
--------- |
| 371 |
|
| 372 |
function Get (Value : TOML_Value; Key : String) return TOML_Value is |
| 373 |
begin |
| 374 |
return Value.Get (To_Unbounded_String (Key)); |
| 375 |
end Get; |
| 376 |
|
| 377 |
--------- |
| 378 |
-- Get -- |
| 379 |
--------- |
| 380 |
|
| 381 |
function Get |
| 382 |
(Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value is |
| 383 |
begin |
| 384 |
return Value.Value.Map_Value.Element (Key); |
| 385 |
end Get; |
| 386 |
|
| 387 |
----------------- |
| 388 |
-- Get_Or_Null -- |
| 389 |
----------------- |
| 390 |
|
| 391 |
function Get_Or_Null (Value : TOML_Value; Key : String) return TOML_Value |
| 392 |
is |
| 393 |
begin |
| 394 |
return Value.Get_Or_Null (To_Unbounded_String (Key)); |
| 395 |
end Get_Or_Null; |
| 396 |
|
| 397 |
----------------- |
| 398 |
-- Get_Or_Null -- |
| 399 |
----------------- |
| 400 |
|
| 401 |
function Get_Or_Null |
| 402 |
(Value : TOML_Value; Key : Unbounded_UTF8_String) return TOML_Value |
| 403 |
is |
| 404 |
use TOML_Maps; |
| 405 |
Position : constant Cursor := Value.Value.Map_Value.Find (Key); |
| 406 |
begin |
| 407 |
return (if Has_Element (Position) |
| 408 |
then Element (Position) |
| 409 |
else No_TOML_Value); |
| 410 |
end Get_Or_Null; |
| 411 |
|
| 412 |
---------------------- |
| 413 |
-- Iterate_On_Table -- |
| 414 |
---------------------- |
| 415 |
|
| 416 |
function Iterate_On_Table (Value : TOML_Value) return Table_Entry_Array is |
| 417 |
Keys : constant Key_Array := Value.Keys; |
| 418 |
begin |
| 419 |
return Result : Table_Entry_Array (Keys'Range) do |
| 420 |
for I In Keys'Range loop |
| 421 |
Result (I) := (Keys (I), Value.Get (Keys (I))); |
| 422 |
end loop; |
| 423 |
end return; |
| 424 |
end Iterate_On_Table; |
| 425 |
|
| 426 |
------------ |
| 427 |
-- Length -- |
| 428 |
------------ |
| 429 |
|
| 430 |
function Length (Value : TOML_Value) return Natural is |
| 431 |
begin |
| 432 |
return Natural (Value.Value.Array_Value.Length); |
| 433 |
end Length; |
| 434 |
|
| 435 |
---------- |
| 436 |
-- Item -- |
| 437 |
---------- |
| 438 |
|
| 439 |
function Item (Value : TOML_Value; Index : Positive) return TOML_Value is |
| 440 |
begin |
| 441 |
return Value.Value.Array_Value.Element (Index); |
| 442 |
end Item; |
| 443 |
|
| 444 |
-------------------- |
| 445 |
-- Create_Boolean -- |
| 446 |
-------------------- |
| 447 |
|
| 448 |
function Create_Boolean (Value : Boolean) return TOML_Value is |
| 449 |
begin |
| 450 |
return Create_Value (new TOML_Value_Record' |
| 451 |
(Kind => TOML_Boolean, Ref_Count => 1, Boolean_Value => Value)); |
| 452 |
end Create_Boolean; |
| 453 |
|
| 454 |
-------------------- |
| 455 |
-- Create_Integer -- |
| 456 |
-------------------- |
| 457 |
|
| 458 |
function Create_Integer (Value : Any_Integer) return TOML_Value is |
| 459 |
begin |
| 460 |
return Create_Value (new TOML_Value_Record' |
| 461 |
(Kind => TOML_Integer, Ref_Count => 1, Integer_Value => Value)); |
| 462 |
end Create_Integer; |
| 463 |
|
| 464 |
------------------ |
| 465 |
-- Create_Float -- |
| 466 |
------------------ |
| 467 |
|
| 468 |
function Create_Float (Value : Any_Float) return TOML_Value is |
| 469 |
begin |
| 470 |
return Create_Value (new TOML_Value_Record' |
| 471 |
(Kind => TOML_Float, Ref_Count => 1, Float_Value => Value)); |
| 472 |
end Create_Float; |
| 473 |
|
| 474 |
------------------- |
| 475 |
-- Create_String -- |
| 476 |
------------------- |
| 477 |
|
| 478 |
function Create_String (Value : String) return TOML_Value is |
| 479 |
begin |
| 480 |
return Create_String (To_Unbounded_String (Value)); |
| 481 |
end Create_String; |
| 482 |
|
| 483 |
------------------- |
| 484 |
-- Create_String -- |
| 485 |
------------------- |
| 486 |
|
| 487 |
function Create_String (Value : Unbounded_UTF8_String) return TOML_Value is |
| 488 |
begin |
| 489 |
return Create_Value (new TOML_Value_Record' |
| 490 |
(Kind => TOML_String, |
| 491 |
Ref_Count => 1, |
| 492 |
String_Value => Value)); |
| 493 |
end Create_String; |
| 494 |
|
| 495 |
---------------------------- |
| 496 |
-- Create_Offset_Datetime -- |
| 497 |
---------------------------- |
| 498 |
|
| 499 |
function Create_Offset_Datetime |
| 500 |
(Value : Any_Offset_Datetime) return TOML_Value is |
| 501 |
begin |
| 502 |
return Create_Value (new TOML_Value_Record' |
| 503 |
(Kind => TOML_Offset_Datetime, |
| 504 |
Ref_Count => 1, |
| 505 |
Offset_Datetime_Value => Value)); |
| 506 |
end Create_Offset_Datetime; |
| 507 |
|
| 508 |
--------------------------- |
| 509 |
-- Create_Local_Datetime -- |
| 510 |
--------------------------- |
| 511 |
|
| 512 |
function Create_Local_Datetime |
| 513 |
(Value : Any_Local_Datetime) return TOML_Value is |
| 514 |
begin |
| 515 |
return Create_Value (new TOML_Value_Record' |
| 516 |
(Kind => TOML_Local_Datetime, |
| 517 |
Ref_Count => 1, |
| 518 |
Local_Datetime_Value => Value)); |
| 519 |
end Create_Local_Datetime; |
| 520 |
|
| 521 |
----------------------- |
| 522 |
-- Create_Local_Date -- |
| 523 |
----------------------- |
| 524 |
|
| 525 |
function Create_Local_Date (Value : Any_Local_Date) return TOML_Value is |
| 526 |
begin |
| 527 |
return Create_Value (new TOML_Value_Record' |
| 528 |
(Kind => TOML_Local_Date, |
| 529 |
Ref_Count => 1, |
| 530 |
Local_Date_Value => Value)); |
| 531 |
end Create_Local_Date; |
| 532 |
|
| 533 |
----------------------- |
| 534 |
-- Create_Local_Time -- |
| 535 |
----------------------- |
| 536 |
|
| 537 |
function Create_Local_Time (Value : Any_Local_Time) return TOML_Value is |
| 538 |
begin |
| 539 |
return Create_Value (new TOML_Value_Record' |
| 540 |
(Kind => TOML_Local_Time, |
| 541 |
Ref_Count => 1, |
| 542 |
Local_Time_Value => Value)); |
| 543 |
end Create_Local_Time; |
| 544 |
|
| 545 |
------------------ |
| 546 |
-- Create_Table -- |
| 547 |
------------------ |
| 548 |
|
| 549 |
function Create_Table return TOML_Value is |
| 550 |
begin |
| 551 |
return Create_Value (new TOML_Value_Record' |
| 552 |
(Kind => TOML_Table, |
| 553 |
Ref_Count => 1, |
| 554 |
Map_Value => <>, |
| 555 |
Table_Implicitly_Created => False)); |
| 556 |
end Create_Table; |
| 557 |
|
| 558 |
--------- |
| 559 |
-- Set -- |
| 560 |
--------- |
| 561 |
|
| 562 |
procedure Set (Value : TOML_Value; Key : String; Entry_Value : TOML_Value) |
| 563 |
is |
| 564 |
begin |
| 565 |
Value.Set (To_Unbounded_String (Key), Entry_Value); |
| 566 |
end Set; |
| 567 |
|
| 568 |
--------- |
| 569 |
-- Set -- |
| 570 |
--------- |
| 571 |
|
| 572 |
procedure Set |
| 573 |
(Value : TOML_Value; |
| 574 |
Key : Unbounded_UTF8_String; |
| 575 |
Entry_Value : TOML_Value) |
| 576 |
is |
| 577 |
begin |
| 578 |
Value.Value.Map_Value.Include (Key, Entry_Value); |
| 579 |
end Set; |
| 580 |
|
| 581 |
----------------- |
| 582 |
-- Set_Default -- |
| 583 |
----------------- |
| 584 |
|
| 585 |
procedure Set_Default |
| 586 |
(Value : TOML_Value; Key : String; Entry_Value : TOML_Value) |
| 587 |
is |
| 588 |
begin |
| 589 |
Value.Set_Default (To_Unbounded_String (Key), Entry_Value); |
| 590 |
end Set_Default; |
| 591 |
|
| 592 |
----------------- |
| 593 |
-- Set_Default -- |
| 594 |
----------------- |
| 595 |
|
| 596 |
procedure Set_Default |
| 597 |
(Value : TOML_Value; |
| 598 |
Key : Unbounded_UTF8_String; |
| 599 |
Entry_Value : TOML_Value) |
| 600 |
is |
| 601 |
use TOML_Maps; |
| 602 |
Dummy_Position : Cursor; |
| 603 |
Dummy_Inserted : Boolean; |
| 604 |
begin |
| 605 |
Value.Value.Map_Value.Insert |
| 606 |
(Key, Entry_Value, Dummy_Position, Dummy_Inserted); |
| 607 |
end Set_Default; |
| 608 |
|
| 609 |
----------- |
| 610 |
-- Unset -- |
| 611 |
----------- |
| 612 |
|
| 613 |
procedure Unset (Value : TOML_Value; Key : String) is |
| 614 |
begin |
| 615 |
Value.Unset (To_Unbounded_String (Key)); |
| 616 |
end Unset; |
| 617 |
|
| 618 |
----------- |
| 619 |
-- Unset -- |
| 620 |
----------- |
| 621 |
|
| 622 |
procedure Unset (Value : TOML_Value; Key : Unbounded_UTF8_String) is |
| 623 |
begin |
| 624 |
Value.Value.Map_Value.Delete (Key); |
| 625 |
end Unset; |
| 626 |
|
| 627 |
----------- |
| 628 |
-- Merge -- |
| 629 |
----------- |
| 630 |
|
| 631 |
function Merge (L, R : TOML_Value) return TOML_Value is |
| 632 |
function Merge_Entries |
| 633 |
(Key : Unbounded_UTF8_String; |
| 634 |
Dummy_L, Dummy_R : TOML_Value) return TOML_Value |
| 635 |
is (raise Constraint_Error with "duplicate key: " & To_String (Key)); |
| 636 |
begin |
| 637 |
return Merge (L, R, Merge_Entries'Access); |
| 638 |
end Merge; |
| 639 |
|
| 640 |
----------- |
| 641 |
-- Merge -- |
| 642 |
----------- |
| 643 |
|
| 644 |
function Merge |
| 645 |
(L, R : TOML_Value; |
| 646 |
Merge_Entries : not null access function |
| 647 |
(Key : Unbounded_UTF8_String; L, R : TOML_Value) return TOML_Value) |
| 648 |
return TOML_Value |
| 649 |
is |
| 650 |
Table : constant TOML_Value := Create_Table; |
| 651 |
begin |
| 652 |
for Key of L.Keys loop |
| 653 |
Table.Set (Key, L.Get (Key)); |
| 654 |
end loop; |
| 655 |
|
| 656 |
for Key of R.Keys loop |
| 657 |
declare |
| 658 |
Value : TOML_Value := R.Get (Key); |
| 659 |
begin |
| 660 |
if Table.Has (Key) then |
| 661 |
Value := Merge_Entries (Key, Table.Get (Key), Value); |
| 662 |
end if; |
| 663 |
Table.Set (Key, Value); |
| 664 |
end; |
| 665 |
end loop; |
| 666 |
|
| 667 |
return Table; |
| 668 |
end Merge; |
| 669 |
|
| 670 |
------------------ |
| 671 |
-- Create_Array -- |
| 672 |
------------------ |
| 673 |
|
| 674 |
function Create_Array return TOML_Value is |
| 675 |
begin |
| 676 |
return Create_Value (new TOML_Value_Record' |
| 677 |
(Kind => TOML_Array, |
| 678 |
Ref_Count => 1, |
| 679 |
Array_Value => <>, |
| 680 |
Array_Implicitly_Created => False)); |
| 681 |
end Create_Array; |
| 682 |
|
| 683 |
--------- |
| 684 |
-- Set -- |
| 685 |
--------- |
| 686 |
|
| 687 |
procedure Set (Value : TOML_Value; Index : Positive; Item : TOML_Value) is |
| 688 |
begin |
| 689 |
Value.Value.Array_Value (Index) := Item; |
| 690 |
end Set; |
| 691 |
|
| 692 |
------------ |
| 693 |
-- Append -- |
| 694 |
------------ |
| 695 |
|
| 696 |
procedure Append (Value, Item : TOML_Value) is |
| 697 |
begin |
| 698 |
Value.Value.Array_Value.Append (Item); |
| 699 |
end Append; |
| 700 |
|
| 701 |
------------------- |
| 702 |
-- Insert_Before -- |
| 703 |
------------------- |
| 704 |
|
| 705 |
procedure Insert_Before |
| 706 |
(Value : TOML_Value; Index : Positive; Item : TOML_Value) |
| 707 |
is |
| 708 |
begin |
| 709 |
Value.Value.Array_Value.Insert (Index, Item); |
| 710 |
end Insert_Before; |
| 711 |
|
| 712 |
----------------- |
| 713 |
-- Load_String -- |
| 714 |
----------------- |
| 715 |
|
| 716 |
function Load_String (Content : String) return Read_Result is |
| 717 |
type Input_Stream is record |
| 718 |
Next_Character : Positive; |
| 719 |
-- Index of the next character in Content that Get must return |
| 720 |
end record; |
| 721 |
|
| 722 |
procedure Get |
| 723 |
(Stream : in out Input_Stream; |
| 724 |
EOF : out Boolean; |
| 725 |
Byte : out Character); |
| 726 |
-- Callback for Parse_String |
| 727 |
|
| 728 |
--------- |
| 729 |
-- Get -- |
| 730 |
--------- |
| 731 |
|
| 732 |
procedure Get |
| 733 |
(Stream : in out Input_Stream; |
| 734 |
EOF : out Boolean; |
| 735 |
Byte : out Character) is |
| 736 |
begin |
| 737 |
if Stream.Next_Character > Content'Length then |
| 738 |
EOF := True; |
| 739 |
else |
| 740 |
EOF := False; |
| 741 |
Byte := Content (Stream.Next_Character); |
| 742 |
Stream.Next_Character := Stream.Next_Character + 1; |
| 743 |
end if; |
| 744 |
end Get; |
| 745 |
|
| 746 |
function Parse_String is new TOML.Generic_Parse (Input_Stream, Get); |
| 747 |
|
| 748 |
Stream : Input_Stream := (Next_Character => Content'First); |
| 749 |
begin |
| 750 |
return Parse_String (Stream); |
| 751 |
end Load_String; |
| 752 |
|
| 753 |
-------------------- |
| 754 |
-- Dump_As_String -- |
| 755 |
-------------------- |
| 756 |
|
| 757 |
function Dump_As_String (Value : TOML_Value) return String is |
| 758 |
begin |
| 759 |
return To_String (Dump_As_Unbounded (Value)); |
| 760 |
end Dump_As_String; |
| 761 |
|
| 762 |
----------------------- |
| 763 |
-- Dump_As_Unbounded -- |
| 764 |
----------------------- |
| 765 |
|
| 766 |
function Dump_As_Unbounded |
| 767 |
(Value : TOML_Value) return Unbounded_UTF8_String is |
| 768 |
begin |
| 769 |
return Result : Unbounded_UTF8_String do |
| 770 |
Dump_To_String (Result, Value); |
| 771 |
end return; |
| 772 |
end Dump_As_Unbounded; |
| 773 |
|
| 774 |
------------------ |
| 775 |
-- Format_Error -- |
| 776 |
------------------ |
| 777 |
|
| 778 |
function Format_Error (Result : Read_Result) return String is |
| 779 |
Formatted : Unbounded_UTF8_String; |
| 780 |
begin |
| 781 |
if Result.Location.Line /= 0 then |
| 782 |
declare |
| 783 |
L : constant String := Result.Location.Line'Image; |
| 784 |
C : constant String := Result.Location.Column'Image; |
| 785 |
begin |
| 786 |
Append (Formatted, L (L'First + 1 .. L'Last)); |
| 787 |
Append (Formatted, ":"); |
| 788 |
Append (Formatted, C (C'First + 1 .. C'Last)); |
| 789 |
Append (Formatted, ": "); |
| 790 |
end; |
| 791 |
end if; |
| 792 |
|
| 793 |
Append (Formatted, Result.Message); |
| 794 |
return To_String (Formatted); |
| 795 |
end Format_Error; |
| 796 |
|
| 797 |
------------ |
| 798 |
-- Adjust -- |
| 799 |
------------ |
| 800 |
|
| 801 |
overriding procedure Adjust (Self : in out TOML_Value) is |
| 802 |
begin |
| 803 |
if Self.Value = null then |
| 804 |
return; |
| 805 |
end if; |
| 806 |
|
| 807 |
Self.Value.Ref_Count := Self.Value.Ref_Count + 1; |
| 808 |
end Adjust; |
| 809 |
|
| 810 |
-------------- |
| 811 |
-- Finalize -- |
| 812 |
-------------- |
| 813 |
|
| 814 |
overriding procedure Finalize (Self : in out TOML_Value) is |
| 815 |
begin |
| 816 |
if Self.Value = null then |
| 817 |
return; |
| 818 |
end if; |
| 819 |
|
| 820 |
declare |
| 821 |
V : TOML_Value_Record renames Self.Value.all; |
| 822 |
begin |
| 823 |
-- Decrement the ref-count. If no-one references V anymore, |
| 824 |
-- deallocate it. |
| 825 |
|
| 826 |
V.Ref_Count := V.Ref_Count - 1; |
| 827 |
if V.Ref_Count > 0 then |
| 828 |
return; |
| 829 |
end if; |
| 830 |
end; |
| 831 |
|
| 832 |
Free (Self.Value); |
| 833 |
end Finalize; |
| 834 |
|
| 835 |
------------------------ |
| 836 |
-- Implicitly_Created -- |
| 837 |
------------------------ |
| 838 |
|
| 839 |
function Implicitly_Created (Self : TOML_Value'Class) return Boolean is |
| 840 |
begin |
| 841 |
if Self.Kind = TOML_Table then |
| 842 |
return Self.Value.Table_Implicitly_Created; |
| 843 |
else |
| 844 |
return Self.Value.Array_Implicitly_Created; |
| 845 |
end if; |
| 846 |
end Implicitly_Created; |
| 847 |
|
| 848 |
---------------------------- |
| 849 |
-- Set_Implicitly_Created -- |
| 850 |
---------------------------- |
| 851 |
|
| 852 |
procedure Set_Implicitly_Created (Self : TOML_Value'Class) is |
| 853 |
begin |
| 854 |
if Self.Kind = TOML_Table then |
| 855 |
Self.Value.Table_Implicitly_Created := True; |
| 856 |
else |
| 857 |
Self.Value.Array_Implicitly_Created := True; |
| 858 |
end if; |
| 859 |
end Set_Implicitly_Created; |
| 860 |
|
| 861 |
---------------------------- |
| 862 |
-- Set_Explicitly_Created -- |
| 863 |
---------------------------- |
| 864 |
|
| 865 |
procedure Set_Explicitly_Created (Self : TOML_Value'Class) is |
| 866 |
begin |
| 867 |
if Self.Kind = TOML_Table then |
| 868 |
Self.Value.Table_Implicitly_Created := False; |
| 869 |
else |
| 870 |
Self.Value.Array_Implicitly_Created := False; |
| 871 |
end if; |
| 872 |
end Set_Explicitly_Created; |
| 873 |
|
| 874 |
end TOML; |