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

with Ada.Exceptions;
with Yaml.Events.Queue;
with Yaml.Parser;

procedure Yaml.Inspect (Input : String) is
   type Error_Kind is (None, From_Lexer, From_Parser);

   P : Parser.Instance;
   Cur_Pos     : Positive := 1;
   Next_Pos    : Positive;
   Cur_Event   : Event;
   Read_Events : constant Events.Queue.Reference := Events.Queue.New_Queue;
   Occurred_Error : Error_Kind := None;
   Lexer_Token_Start, Lexer_Token_End : Mark;
   Exception_Message : access String;
begin
   P.Set_Input (Input);
   Start_Emitting;
   Start_Parsed_Input;
   begin
      loop
         Cur_Event := P.Next;
         Read_Events.Value.Append (Cur_Event);

         if Cur_Event.Start_Position.Index > Cur_Pos then
            Next_Pos := Cur_Pos;
            while Next_Pos <= Input'Last and then
              Next_Pos < Cur_Event.Start_Position.Index loop
               if Input (Next_Pos) = '#' then
                  if Cur_Pos < Next_Pos then
                     Emit_Whitespace (Input (Cur_Pos .. Next_Pos - 1));
                     Cur_Pos := Next_Pos;
                  end if;
                  while Next_Pos < Cur_Event.Start_Position.Index and then
                    Input (Next_Pos) /= Character'Val (10) loop
                     Next_Pos := Next_Pos + 1;
                  end loop;
                  Emit_Comment (Input (Cur_Pos .. Next_Pos - 1));
               end if;
               Next_Pos := Next_Pos + 1;
            end loop;
            if Cur_Pos < Next_Pos then
               Emit_Whitespace (Input (Cur_Pos .. Next_Pos - 1));
               Cur_Pos := Next_Pos;
            end if;
         end if;
         Start_Rendered_Event (Cur_Event);
         declare
            Content : constant String :=
              Input (Cur_Pos .. Cur_Event.End_Position.Index - 1);
            Cur : Positive := Content'First;
            Start : Positive;
         begin
            while Cur <= Content'Last loop
               if Content (Cur) in ' ' | Character'Val (10) then
                  Start := Cur;
                  loop
                     Cur := Cur + 1;
                     exit when Cur > Content'Last or else
                       Content (Cur) in ' ' | Character'Val (10);
                  end loop;
                  Emit_Whitespace (Content (Start .. Cur - 1));
                  exit when Cur > Content'Last;
               end if;
               Start := Cur;
               case Content (Cur) is
                  when '&' =>
                     loop
                        Cur := Cur + 1;
                        exit when Cur > Content'Last or else
                          Content (Cur) in ' ' | Character'Val (10);
                     end loop;
                     Emit_Anchor (Content (Start .. Cur - 1));
                  when '!' =>
                     loop
                        Cur := Cur + 1;
                        exit when Cur > Content'Last or else
                          Content (Cur) in ' ' | Character'Val (10);
                     end loop;
                     Emit_Tag (Content (Start .. Cur - 1));
                  when others =>
                     Emit_Event_Content (Content (Start .. Content'Last));
                     exit;
               end case;
            end loop;
         end;
         End_Rendered_Event;
         Cur_Pos := Cur_Event.End_Position.Index;
         exit when Cur_Event.Kind = Stream_End;
      end loop;
   exception
      when Error : Lexer_Error =>
         Emit_Unparseable (Input (Cur_Pos .. Input'Last));
         Occurred_Error := From_Lexer;
         Lexer_Token_Start := P.Current_Lexer_Token_Start;
         Lexer_Token_End := P.Current_Input_Character;
         Exception_Message := new String'(Ada.Exceptions.Exception_Message (Error));
      when Error : Parser_Error =>
         Emit_Unparseable (Input (Cur_Pos .. Input'Last));
         Occurred_Error := From_Parser;
         Lexer_Token_Start := P.Recent_Lexer_Token_Start;
         Lexer_Token_End := P.Recent_Lexer_Token_End;
         Exception_Message := new String'(Ada.Exceptions.Exception_Message (Error));
   end;
   End_Parsed_Input;
   Start_Parsed_Output;
   case Occurred_Error is
      when None =>
         declare
            Iterator : constant Events.Queue.Stream_Reference :=
              Events.Queue.As_Stream (Read_Events);
         begin
            loop
               Cur_Event := Iterator.Value.Next;
               Emit_Raw_Event (Cur_Event);
               exit when Cur_Event.Kind = Stream_End;
            end loop;
         end;
      when From_Lexer =>
         Emit_Lexer_Error (Lexer_Token_Start, Lexer_Token_End,
                           Exception_Message.all);
      when From_Parser =>
         Emit_Parser_Error (Lexer_Token_Start, Lexer_Token_End,
                            Exception_Message.all);
   end case;
   End_Parsed_Output;
   Finish_Emitting;
end Yaml.Inspect;