adayaml_0.3.0_ab19e387/util/src/yaml-to_dom.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
--  part of AdaYaml, (c) 2017 Felix Krause
--  released under the terms of the MIT license, see the file "copying.txt"

with Ada.Text_IO;
with Ada.Command_Line;

with Yaml.Source.Text_IO;
with Yaml.Source.File;
with Yaml.Dom.Loading;
with Yaml.Dom.Node;

pragma Unreferenced (Yaml.Dom.Node);

procedure Yaml.To_Dom is
   use type Dom.Node_Kind;

   Input : Source.Pointer;
begin
   if Ada.Command_Line.Argument_Count = 0 then
      Input := Source.Text_IO.As_Source (Ada.Text_IO.Standard_Input);
   else
      Input := Source.File.As_Source (Ada.Command_Line.Argument (1));
   end if;

   declare
      Document : constant Dom.Document_Reference :=
        Dom.Loading.From_Source (Input);
      Root_Node : constant not null access Dom.Node.Instance :=
        Document.Root.Value.Data;

      procedure Visit_Pair (Key, Value : not null access Dom.Node.Instance) is
      begin
         Ada.Text_IO.Put_Line ("Key: " & Key.Kind'Img);
         if Key.Kind = Dom.Scalar then
            Ada.Text_IO.Put_Line ("  """ & Key.Content.Value.Data.all & """");
         end if;
         Ada.Text_IO.Put_Line ("Value: " & Value.Kind'Img);
         if Value.Kind = Dom.Scalar then
            Ada.Text_IO.Put_Line ("  """ & Value.Content.Value.Data.all & """");
         end if;
      end Visit_Pair;
   begin
      Ada.Text_IO.Put_Line ("Root is " & Root_Node.Kind'Img);
      if Root_Node.Kind = Dom.Mapping then
         Root_Node.Pairs.Iterate (Visit_Pair'Access);
      end if;
   end;
end Yaml.To_Dom;