adayaml_0.3.0_ab19e387/util/src/yaml-to_events.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
--  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.Parser;
with Yaml.Transformator.Annotation_Processor;

procedure Yaml.To_Events is
   use type Source.Pointer;

   Input : Source.Pointer;
   P     : Parser.Instance;
   Cur   : Event;

   Process_Annotations : Boolean := False;
begin
   for Cur_Argument in 1 .. Ada.Command_Line.Argument_Count loop
      declare
         Argument : constant String := Ada.Command_Line.Argument (Cur_Argument);
      begin
         if Argument'Length > 0 then
            if Argument (Argument'First) = '-' then
               if Argument = "-a" or Argument = "--process-annotations" then
                  Process_Annotations := True;
               else
                  Ada.Text_IO.Put_Line (Ada.Text_IO.Standard_Error,
                                        "Unknown option: " & Argument);
                  return;
               end if;
            elsif Input /= null then
               Ada.Text_IO.Put_Line (Ada.Text_IO.Standard_Error,
                                     "Too many files given!");
               Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
            else
               Input := Source.File.As_Source (Argument);
            end if;
         end if;
      end;
   end loop;
   if Input = null then
      Input := Source.Text_IO.As_Source (Ada.Text_IO.Standard_Input);
   end if;
   P.Set_Input (Input);

   if Process_Annotations then
      declare
         Processor : constant Yaml.Transformator.Pointer
           := Yaml.Transformator.Annotation_Processor.New_Processor
             (P.Pool);
      begin
         loop
            Cur := P.Next;
            Processor.Put (Cur);
            while Processor.Has_Next loop
               Ada.Text_IO.Put_Line (To_String (Processor.Next));
            end loop;
            exit when Cur.Kind = Stream_End;
         end loop;
      end;
   else
      loop
         Cur := P.Next;
         Ada.Text_IO.Put_Line (To_String (Cur));
         exit when Cur.Kind = Stream_End;
      end loop;
   end if;

end Yaml.To_Events;