spdx_0.2.0_2df9b118/src/spdx.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
with Ada.Strings;
with Ada.Strings.Fixed;
with Ada.Characters.Handling; use Ada.Characters.Handling;

with SPDX.Licenses;
with SPDX.Exceptions;

package body SPDX is

   function Token_Str (This : Expression; Loc : Location) return String;

   function Is_Custom_Id (Str : String) return Boolean;
   procedure Parse_License (This : in out Expression);
   procedure Parse_Compound_Expression (This : in out Expression);
   procedure Parse_Simple_Expression (This : in out Expression);
   procedure Parse_Exception (This : in out Expression);

   ---------------
   -- Token_Str --
   ---------------

   function Token_Str (This : Expression; Loc : Location) return String is
   begin
      if Loc.From not in This.Str'Range
        or else
          Loc.To not in This.Str'Range
      then
         return "";
      else
         return This.Str (Loc.From .. Loc.To);
      end if;
   end Token_Str;

   ------------------
   -- Is_Custom_Id --
   ------------------

   function Is_Custom_Id (Str : String) return Boolean is
      Lower  : constant String := To_Lower (Str);
      Prefix : constant String := "custom-";
   begin
      return Lower'Length > Prefix'Length
        and then
          Lower (Lower'First .. Lower'First + Prefix'Length - 1) = Prefix;
   end Is_Custom_Id;

   -------------------
   -- Parse_License --
   -------------------

   procedure Parse_License (This : in out Expression) is
   begin
      Parse_Compound_Expression (This);

      if This.Error /= None then
         return;
      end if;

      if not This.Tokens.Is_Empty then
         This.Error := Unexpected_Token;
         This.Err_Loc := This.Tokens.First_Element.Loc;
      end if;
   end Parse_License;

   -------------------------------
   -- Parse_Compound_Expression --
   -------------------------------

   procedure Parse_Compound_Expression (This : in out Expression) is
   begin
      --  compound = ( compound )
      --             | simple
      --             | simple AND compound
      --             | simple OR compound

      if This.Tokens.Is_Empty then
         This.Error := Empty_Expression;
         This.Err_Loc := (This.Str'Last, This.Str'Last);
         return;
      end if;

      case This.Tokens.First_Element.Kind is

      when Paren_Open =>

         This.Tokens.Delete_First;

         Parse_Compound_Expression (This);

         if This.Error /= None then
            return;
         end if;

         if This.Tokens.Is_Empty then
            This.Error := Paren_Close_Expected;
            This.Err_Loc := (This.Str'Last, This.Str'Last);
            return;
         end if;

         if This.Tokens.First_Element.Kind /= Paren_Close then
            This.Error := Paren_Close_Expected;
            This.Err_Loc := This.Tokens.First_Element.Loc;
            return;
         end if;

         --  Delete the Paren_Close
         This.Tokens.Delete_First;

      when Id_Str =>
         Parse_Simple_Expression (This);

         if This.Error /= None then
            return;
         end if;

      when others =>
         This.Error := Unexpected_Token;
         This.Err_Loc := This.Tokens.First_Element.Loc;
         return;
      end case;

      if This.Tokens.Is_Empty then
         --  End of expression
         return;
      end if;

      if This.Tokens.First_Element.Kind in Op_And | Op_Or then
         --  Just skip operator as we do not build the AST
         This.Tokens.Delete_First;

         Parse_Compound_Expression (This);
      end if;
   end Parse_Compound_Expression;

   -----------------------------
   -- Parse_Simple_Expression --
   -----------------------------

   procedure Parse_Simple_Expression (This : in out Expression) is
   begin
      --  simple =   id
      --           | id+
      --           | id exception
      --           | id+ exception

      if This.Tokens.Is_Empty then
         This.Error := License_Id_Expected;
         This.Err_Loc := (This.Str'Last, This.Str'Last);
         return;
      end if;

      if This.Tokens.First_Element.Kind /= Id_Str then
         This.Error := License_Id_Expected;
         This.Err_Loc := This.Tokens.First_Element.Loc;
         return;
      end if;

      declare
         License_Id : constant String :=
           Token_Str (This, This.Tokens.First_Element.Loc);
      begin

         if This.Allow_Custom and then Is_Custom_Id (License_Id) then
            This.Has_Custom_Id := True;
         elsif not SPDX.Licenses.Valid_Id (License_Id) then
            This.Error := Invalid_License_Id;
            This.Err_Loc := This.Tokens.First_Element.Loc;
         end if;

         This.Tokens.Delete_First;

         if This.Tokens.Is_Empty then
            return;
         end if;

         --  + operator
         if This.Tokens.First_Element.Kind = Op_Or_Later then
            This.Tokens.Delete_First;
         end if;

         if This.Tokens.Is_Empty then
            return;
         end if;

         Parse_Exception (This);
      end;

   end Parse_Simple_Expression;

   ---------------------
   -- Parse_Exception --
   ---------------------

   procedure Parse_Exception (This : in out Expression) is
   begin
      --  exception =   <nothing>
      --              | WITH id

      if This.Tokens.First_Element.Kind = Op_With then

         This.Tokens.Delete_First;

         if This.Tokens.Is_Empty then
            This.Error := Exception_Id_Expected;
            This.Err_Loc := (This.Str'Last, This.Str'Last);
            return;
         end if;

         if This.Tokens.First_Element.Kind /= Id_Str then
            This.Error := Exception_Id_Expected;
            This.Err_Loc := This.Tokens.First_Element.Loc;
            return;
         end if;

         declare
            Exception_Id : constant String :=
              Token_Str (This, This.Tokens.First_Element.Loc);
         begin
            if not SPDX.Exceptions.Valid_Id (Exception_Id) then
               This.Error := Invalid_Exception_Id;
               This.Err_Loc := This.Tokens.First_Element.Loc;
            end if;
               This.Tokens.Delete_First;
         end;
      end if;
   end Parse_Exception;

   -----------
   -- Parse --
   -----------

   function Parse (Str          : String;
                   Allow_Custom : Boolean := False)
                   return Expression
   is
      Exp : Expression (Str'Length);
   begin

      Exp.Str := Str;
      Exp.Allow_Custom := Allow_Custom;

      Tokenize (Exp);

      if Exp.Error /= None then
         return Exp;
      end if;

      Parse_License (Exp);

      return Exp;
   end Parse;

   -----------
   -- Error --
   -----------

   function Error (This : Expression) return String is

      function Img (N : Natural) return String;
      function Img (Loc : Location) return String;

      ---------
      -- Img --
      ---------

      function Img (N : Natural) return String
      is (Ada.Strings.Fixed.Trim (N'Img, Ada.Strings.Left));

      ---------
      -- Img --
      ---------

      function Img (Loc : Location) return String
      is ((Img (Loc.From) & ":" & Img (Loc.To)));

   begin
      case This.Error is
         when None =>
            return "";

         when Or_Later_Misplaced =>
            return "+ operator must follow and indentifier without " &
              "whitespace (" & Img (This.Err_Loc) & ")";

         when Invalid_Char =>
            return "Invalid character at " & Img (This.Err_Loc.From);

         when Operator_Lowcase =>
            return "Operator must be uppercase at (" & Img (This.Err_Loc) & ")";

         when Unexpected_Token =>
            return "Unexpected token at (" & Img (This.Err_Loc) & ")";

         when Paren_Close_Expected =>
            return "Missing closing parentheses ')' at (" &
              Img (This.Err_Loc) & ")";

         when License_Id_Expected =>
            return "License id expected at (" & Img (This.Err_Loc) & ")";

         when Invalid_License_Id =>
            return "Invalid license ID: '" &
              Token_Str (This, This.Err_Loc) & "' (" & Img (This.Err_Loc) & ")";

         when Exception_Id_Expected =>
            return "License exception id expected at (" &
              Img (This.Err_Loc) & ")";

         when Invalid_Exception_Id =>
            return "Invalid license exception ID: '" &
              Token_Str (This, This.Err_Loc) &
              "' (" & Img (This.Err_Loc) & ")";

         when Empty_Expression =>
            return "Empty license expression at (" & Img (This.Err_Loc) & ")";

      end case;
   end Error;

   -----------
   -- Valid --
   -----------

   function Valid (This : Expression) return Boolean is
   begin
      return This.Error = None;
   end Valid;

   ---------
   -- Img --
   ---------

   function Img (This : Expression) return String is
   begin
      return This.Str;
   end Img;

   ----------------
   -- Has_Custom --
   ----------------

   function Has_Custom (This : Expression) return Boolean
   is (This.Has_Custom_Id);

   --------------
   -- Tokenize --
   --------------

   procedure Tokenize (This : in out Expression) is

      Tokens : Token_Vector.Vector renames This.Tokens;
      Str : String renames This.Str;

      Index : Natural := Str'First;

   begin
      while Index in Str'Range loop

         if Str (Index) in Whitespace_Characters then
            Index := Index + 1; -- Skip whitespace

         elsif Str (Index) = '(' then
            Tokens.Append ((Paren_Open, (Index, Index)));
            Index := Index + 1;

         elsif Str (Index) = ')' then
            Tokens.Append ((Paren_Close, (Index, Index)));
            Index := Index + 1;

         elsif Str (Index) = '+' then
            This.Error := Or_Later_Misplaced;
            This.Err_Loc := (Index, Index);
            return;

         elsif Str (Index) in Id_Characters then

            --  Operator or identifier

            declare
               From : constant Natural := Index;
            begin
               while Index in Str'Range
                 and then Str (Index) in Id_Characters | '+'
               loop
                  Index := Index + 1;
               end loop;

               declare
                  To     : constant Natural := Index - 1;
                  Substr : constant String := Str (From .. To);
               begin
                  if Substr = "WITH" then
                     Tokens.Append ((Op_With, (From, To)));

                  elsif Substr = "OR" then
                     Tokens.Append ((Op_Or, (From, To)));

                  elsif Substr = "AND" then
                     Tokens.Append ((Op_And, (From, To)));

                  elsif To_Lower (Substr) = "with"
                    or else To_Lower (Substr) = "or"
                    or else To_Lower (Substr) = "and"
                  then
                     This.Error := Operator_Lowcase;
                     This.Err_Loc := (From, To);
                     return;

                  else
                     if Str (To) = '+' then
                        --  + operator can be found after and id (without
                        --  whitespace).
                        Tokens.Append ((Id_Str, (From, To - 1)));
                        Tokens.Append ((Op_Or_Later, (To, To)));
                     else
                        Tokens.Append ((Id_Str, (From, To)));
                     end if;
                  end if;
               end;
            end;

         else
            This.Error := Invalid_Char;
            This.Err_Loc := (Index, Index);
            return;

         end if;
      end loop;
   end Tokenize;
end SPDX;