libadalang_tools_24.0.0_d864b5a8/src/tgen/tgen-json-unparse.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
------------------------------------------------------------------------------
--                                                                          --
--                                  TGen                                    --
--                                                                          --
--                        Copyright (C) 2023, AdaCore                       --
--                                                                          --
-- TGen  is  free software; you can redistribute it and/or modify it  under --
-- under  terms of  the  GNU General  Public License  as  published by  the --
-- Free  Software  Foundation;  either version 3, or  (at your option)  any --
-- later version. This software  is distributed in the hope that it will be --
-- useful but  WITHOUT  ANY  WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY  or  FITNESS  FOR A PARTICULAR PURPOSE.                  --
--                                                                          --
-- As a special  exception  under  Section 7  of  GPL  version 3,  you are  --
-- granted additional  permissions described in the  GCC  Runtime  Library  --
-- Exception, version 3.1, as published by the Free Software Foundation.    --
--                                                                          --
-- You should have received a copy of the GNU General Public License and a  --
-- copy of the GCC Runtime Library Exception along with this program;  see  --
-- the files COPYING3 and COPYING.RUNTIME respectively.  If not, see        --
-- <http://www.gnu.org/licenses/>.                                          --
------------------------------------------------------------------------------

with Ada.Strings;           use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Maps;      use Ada.Strings.Maps;

package body TGen.JSON.Unparse is

   procedure Remove_Trailing_Comma_And_Spaces
     (Text : in out Unbounded_String);

   function Unparse_Array
     (Sizes : JSON_Array;
      Val   : JSON_Array) return Unbounded_String;

   function Unparse_Unconstrained_Array (Val : JSON_Value) return JSON_Value;

   function Unparse_Constrained_Array (Val : JSON_Value) return JSON_Value;

   function Unparse_Record (Val : JSON_Value) return Unbounded_String;

   function Unparse_Non_Discriminated_Record
     (Val : JSON_Value) return JSON_Value;

   function Unparse_Discriminated_Record (Val : JSON_Value) return JSON_Value;

   function Unparse_Quotient (Val : JSON_Value) return JSON_Value;

   --------------------------------------
   -- Remove_Trailing_Comma_And_Spaces --
   --------------------------------------

   procedure Remove_Trailing_Comma_And_Spaces (Text : in out Unbounded_String)
   is
   begin
      Trim (Text, Right);
      Trim (Text, Null_Set, To_Set (','));
   end Remove_Trailing_Comma_And_Spaces;

   -------------------
   -- Unparse_Array --
   -------------------

   function Unparse_Array
     (Sizes : JSON_Array;
      Val   : JSON_Array) return Unbounded_String
   is
      type Nat_Array is array (Natural range <>) of Natural;

      function Pp_Arr
        (Current_Index : in out Positive;
         Sizes         : Nat_Array) return Unbounded_String;
      --  Unflatten the generated array

      ------------
      -- Pp_Arr --
      ------------

      function Pp_Arr
        (Current_Index : in out Positive;
         Sizes         : Nat_Array) return Unbounded_String
      is
         Unparsed_Value   : Unbounded_String;
         Current_Arr_Size : constant Natural := Sizes (Sizes'First);
      begin
         Append (Unparsed_Value, "(");

         --  Special cases for array of size 0 and array of size 1

         if Current_Arr_Size = 0 then

            --  Print an empty aggregate

            Append (Unparsed_Value, "others => <>");

         elsif Current_Arr_Size = 1 then

            --  Print an aggregate with the others keyword as we can't
            --  have an array declared e.g. (1) but (others => 1), or
            --  (<index> => 1) is allowed. As we don't have the index
            --  here, pick the former.

            Append (Unparsed_Value, "others => ");
         end if;

         for I in 1 .. Current_Arr_Size loop

            --  We have reached the last index type: unparse the component
            --  value

            if Sizes'Length = 1 then
               --  An array component can't be of an unconstrained type.

               Append
                 (Unparsed_Value,
                  UTF8_String'
                    (Unparse (Get (Val, Current_Index)).Get ("value")));
               Current_Index := @ + 1;
            else
               --  Otherwise, generate the nested array recursively

               Append
                 (Unparsed_Value,
                  Pp_Arr
                    (Current_Index,
                     Sizes (Sizes'First + 1 .. Sizes'Last)));
            end if;
            Append (Unparsed_Value, ", ");
         end loop;
         Trim (Unparsed_Value, Right);
         Trim (Unparsed_Value, Null_Set, To_Set (','));
         Append (Unparsed_Value, ")");
         return Unparsed_Value;
      end Pp_Arr;

      Sizes_Arr   : Nat_Array (1 .. Length (Sizes));
      Dummy_Index : Positive := 1;
   begin

      --  Start by getting all of the sizes

      for I in Sizes_Arr'Range loop
         Sizes_Arr (I) := Natural'Value (Get (Array_Element (Sizes, I)));
      end loop;

      return Pp_Arr (Dummy_Index, Sizes_Arr);
   end Unparse_Array;

   ---------------------------------
   -- Unparse_Unconstrained_Array --
   ---------------------------------

   function Unparse_Unconstrained_Array (Val : JSON_Value) return JSON_Value
   is
      Result : constant JSON_Value := Create_Object;
      Constraints : Unbounded_String;
      Dimensions : constant JSON_Array := Val.Get ("dimensions");
   begin
      for Dimension of Dimensions loop
         Append (Constraints, "(");
         Append (Constraints, UTF8_String'(Dimension.Get ("First")));
         Append (Constraints, " .. ");
         Append (Constraints, UTF8_String'(Dimension.Get ("Last")));
         Append (Constraints, ") ");
      end loop;
      Trim (Constraints, Left);
      Set_Field (Result, "constraints", Constraints);

      Set_Field
        (Result,
         "value",
         Unparse_Array (Val.Get ("sizes"), Val.Get ("array")));
      return Result;
   end Unparse_Unconstrained_Array;

   -------------------------------
   -- Unparse_Constrained_Array --
   -------------------------------

   function Unparse_Constrained_Array (Val : JSON_Value) return JSON_Value
   is
      Result : constant JSON_Value := Create_Object;
   begin
      Set_Field
        (Result,
         "value",
         Unparse_Array (Val.Get ("sizes"), Val.Get ("array")));
      return Result;
   end Unparse_Constrained_Array;

   --------------------
   -- Unparse_Record --
   --------------------

   function Unparse_Record (Val : JSON_Value) return Unbounded_String
   is
      Unparsed_Value : Unbounded_String;

      procedure Process_Component (Name : UTF8_String; Value : JSON_Value);

      procedure Process_Component
        (Name  : UTF8_String; Value : JSON_Value) is
      begin
         Append (Unparsed_Value, Name);
         Append (Unparsed_Value, " => ");

         --  A record component can't be of an unconstrained type

         Append (Unparsed_Value,
                 UTF8_String'(Get (Unparse (Value), "value")));
         Append (Unparsed_Value, ", ");
      end Process_Component;

   begin
      Append (Unparsed_Value, "(");
      Map_JSON_Object (Val, Process_Component'Access);

      --  If this is a null record, explicitly generate an empty aggregate

      if Length (Unparsed_Value) = 1 then
         Append (Unparsed_Value, "others => <>");

      else
         --  Otherwise, we have to trim the resulting value

         Remove_Trailing_Comma_And_Spaces (Unparsed_Value);
      end if;
      Append (Unparsed_Value, ")");
      return Unparsed_Value;
   end Unparse_Record;

   --------------------------------------
   -- Unparse_Non_Discriminated_Record --
   --------------------------------------

   function Unparse_Non_Discriminated_Record
     (Val : JSON_Value) return JSON_Value
   is
      Result : constant JSON_Value := Create_Object;
   begin
      Set_Field (Result, "value", Unparse_Record (Get (Val, "components")));
      return Result;
   end Unparse_Non_Discriminated_Record;

   ----------------------------------
   -- Unparse_Discriminated_Record --
   ----------------------------------

   function Unparse_Discriminated_Record (Val : JSON_Value) return JSON_Value
   is
      Result      : constant JSON_Value := Create_Object;
      Constraints : Unbounded_String;
      Components  : constant Unbounded_String :=
        Unparse_Record (Get (Val, "components"));

      procedure Process_Discr (Name : UTF8_String; Value : JSON_Value);

      -------------------
      -- Process_Discr --
      -------------------

      procedure Process_Discr (Name : UTF8_String; Value : JSON_Value) is
      begin
         Append (Constraints, Name);
         Append (Constraints, " => ");

         --  A record discriminant can't be of an unconstrained type

         Append (Constraints, UTF8_String'(Unparse (Value).Get ("value")));
         Append (Constraints, ", ");
      end Process_Discr;
   begin
      --  Deal with the discriminants

      Append (Constraints, "(");
      Map_JSON_Object (Get (Val, "discriminants"), Process_Discr'Access);
      Remove_Trailing_Comma_And_Spaces (Constraints);
      Append (Constraints, ")");
      Set_Field (Result, "constraints", Constraints);

      --  Also add the discriminant values to the aggregate expression, as it
      --  is required to put the discriminant value in a discriminated record
      --  aggregate expression.

      Set_Field
        (Result, "value",
         Slice (Constraints, 1, Length (Constraints) - 1)
         & ", "
         & Slice (Components, 2, Length (Components)));
      return Result;
   end Unparse_Discriminated_Record;

   ----------------------
   -- Unparse_Quotient --
   ----------------------

   function Unparse_Quotient (Val : JSON_Value) return JSON_Value
   is
      Result          : constant JSON_Value := Create_Object;
      Quotient_String : constant Unbounded_String := Get (Val, "value");
      Div_Index       : constant Natural :=
        Index (Quotient_String, To_Set ('/'));
   begin
      --  A quotient string is e.g. "2 / 4"

      Set_Field
        (Result,
         "value",
         Slice (Quotient_String, 1, Div_Index - 2) & ".0 "
         & Slice (Quotient_String, Div_Index, Length (Quotient_String))
         & ".0");
      return Result;
   end Unparse_Quotient;

   -------------
   -- Unparse --
   -------------

   function Unparse (Val : JSON_Value) return JSON_Value
   is
      Result : constant JSON_Value := Create_Object;
      Val_Str : constant String := Val.Write;
      pragma Unreferenced (Val_Str);
   begin
      --  Dispatch on the right unparsing function

      case Kind (Val) is

      when JSON_Object_Type =>

         if Has_Field (Val, "discriminants") then

            --  Discriminated record case

            return Unparse_Discriminated_Record (Val);

         elsif Has_Field (Val, "components") then

            --  Non discriminated record case

            return Unparse_Non_Discriminated_Record (Val);

         elsif Has_Field (Val, "dimensions") then

            --  Unconstrained array case

            return Unparse_Unconstrained_Array (Val);

         elsif Has_Field (Val, "array") then

            --  Constrained array case

            return Unparse_Constrained_Array (Val);

         elsif Has_Field (Val, "quotient") then

            --  Unparse floating point / fixed point value stored in a
            --  quotient string.

            return Unparse_Quotient (Val);

         else
            --  Defensive code

            raise Program_Error with "Unknown value representation";
         end if;

      when others =>
         Set_Field (Result, "value", Val);
         return Result;
      end case;
   end Unparse;

end TGen.JSON.Unparse;