ada_language_server_22.0.0_ef4bdf41/source/uri/uris.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
------------------------------------------------------------------------------
--                         Language Server Protocol                         --
--                                                                          --
--                     Copyright (C) 2018-2019, AdaCore                     --
--                                                                          --
-- This is free software;  you can redistribute it  and/or modify it  under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for  more details.  You should have  received  a copy of the GNU --
-- General  Public  License  distributed  with  this  software;   see  file --
-- COPYING3.  If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license.                                                          --
------------------------------------------------------------------------------

with Ada.Characters.Handling;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Wide_Wide_Characters.Handling;
with GNAT.Regpat;

package body URIs is

   package body Conversions is

      UNC : constant GNAT.Regpat.Pattern_Matcher :=  --  \\host\share\path
        GNAT.Regpat.Compile ("^\\\\([^\\]*)\\");

      Slash : constant String :=
        GNAT.Regpat.Quote ((1 => GNAT.OS_Lib.Directory_Separator));

      Segment : constant GNAT.Regpat.Pattern_Matcher :=
        GNAT.Regpat.Compile (Slash);

      ---------------
      -- From_File --
      ---------------

      function From_File (Full_Path : String) return URI_String is

         use type GNAT.Regpat.Match_Location;

         procedure Add_All_Paths
           (URI  : in out URIs.URI;
            Path : String);
         --  Add each directory of Path to URI.Path

         -------------------
         -- Add_All_Paths --
         -------------------

         procedure Add_All_Paths
           (URI  : in out URIs.URI;
            Path : String)
         is
            Found : GNAT.Regpat.Match_Array (0 .. 0);
         begin
            GNAT.Regpat.Match (Segment, Path, Found);

            if Found (0) = GNAT.Regpat.No_Match then
               URI.Add_Path_Segment (Path);

               return;

            elsif Found (0).First > Path'First then
               URI.Add_Path_Segment (Path (Path'First .. Found (0).First - 1));
            end if;

            Add_All_Paths (URI, Path (Found (0).Last + 1 .. Path'Last));
         end Add_All_Paths;

         URI   : URIs.URI;
         Found : GNAT.Regpat.Match_Array (0 .. 1);
      begin
         GNAT.Regpat.Match (UNC, Full_Path, Found);
         --  Check if we have path in form of UNC:  \\host\share\path

         if Found (0) = GNAT.Regpat.No_Match then
            Add_All_Paths (URI, Full_Path);

         else  --  UNC form, extract Host
            declare
               Host : constant String :=
                 Full_Path (Found (1).First .. Found (1).Last);
            begin
               URI.Set_Host (Host);
               Add_All_Paths
                 (URI, Full_Path (Found (1).Last + 1 .. Full_Path'Last));
            end;
         end if;

         URI.Set_Scheme ("file");

         return URI.To_String;
      end From_File;

      -------------
      -- To_File --
      -------------

      function To_File (URI : URI_String; Normalize : Boolean) return String is

         procedure Append_Path (Path : String);
         --  Append Path to Result

         Result : Ada.Strings.Unbounded.Unbounded_String;

         -----------------
         -- Append_Path --
         -----------------

         procedure Append_Path (Path : String) is
            use Ada.Strings.Unbounded;
         begin
            --  We skip empty path segments, for URI like "file:///a//b/"
            --  converts to "/a/b"
            if Path /= "" then
               if Result = Null_Unbounded_String then
                  --  Special case for the first element of the PATH:
                  --  under Windows it should be "<drive_letter>:", on
                  --  other systems it should be "/"
                  if GNAT.OS_Lib.Directory_Separator = '\' then
                     Result := To_Unbounded_String (Path);
                  else
                     Result := To_Unbounded_String ('/' & Path);
                  end if;
               else
                  Result := Result & GNAT.OS_Lib.Directory_Separator & Path;
               end if;
            end if;
         end Append_Path;

         Value  : URIs.URI;
         Ok     : Boolean;
      begin
         Value.Parse (URI, Ok);

         if Ok and then Scheme (Value) = "file" then
            if Host (Value) not in "" | "localhost" then
               Append_Path ("\\" & Host (Value));
            end if;

            for J in Value.Each_Path_Segment loop
               Append_Path (J.To_String);
            end loop;
         else
            raise Constraint_Error with "Invalid URI: " & URI;
         end if;

         if Normalize then
            return GNAT.OS_Lib.Normalize_Pathname
              (Ada.Strings.Unbounded.To_String (Result));
         else
            return Ada.Strings.Unbounded.To_String (Result);
         end if;
      end To_File;

   end Conversions;

   package Iterators is
      type Iterator is new Iterator_Interfaces.Reversible_Iterator with record
         Parent : access constant URI;
      end record;

      overriding function First (Self : Iterator) return Path_Cursor;
      overriding function Last (Self : Iterator) return Path_Cursor;

      overriding function Next
        (Self     : Iterator;
         Position : Path_Cursor) return Path_Cursor;

      overriding function Previous
        (Self     : Iterator;
         Position : Path_Cursor) return Path_Cursor;
   end Iterators;

   package body Iterators is

      overriding function First (Self : Iterator) return Path_Cursor is
      begin
         return (Value => Self.Parent.Path.First);
      end First;

      overriding function Last (Self : Iterator) return Path_Cursor is
      begin
         return (Value => Self.Parent.Path.Last);
      end Last;

      overriding function Next
        (Self     : Iterator;
         Position : Path_Cursor) return Path_Cursor
      is
         pragma Unreferenced (Self);
      begin
         return (Value => Unbounded_String_Lists.Next (Position.Value));
      end Next;

      overriding function Previous
        (Self     : Iterator;
         Position : Path_Cursor) return Path_Cursor
      is
         pragma Unreferenced (Self);
      begin
         return (Value => Unbounded_String_Lists.Previous (Position.Value));
      end Previous;

   end Iterators;

   subtype Unreserved is Character
     with Static_Predicate => Unreserved
       in 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-' | '.' |  '_' | '~';

   subtype Sub_Delims is Character
     with Static_Predicate => Sub_Delims
       in '!' | '$' | '&' | ''' | '(' | ')' | '*' | '+' | ',' | ';' | '=';

   ----------------------
   -- Add_Path_Segment --
   ----------------------

   procedure Add_Path_Segment
     (Self : in out URI'Class; Value : UTF_8_String) is
   begin
      Self.Path.Append (Ada.Strings.Unbounded.To_Unbounded_String (Value));
   end Add_Path_Segment;

   -----------------------
   -- Each_Path_Segment --
   -----------------------

   function Each_Path_Segment
     (Self : URI'Class)
      return Iterator_Interfaces.Reversible_Iterator'Class
   is
   begin
      return Iterators.Iterator'(Parent => Self'Unchecked_Access);
   end Each_Path_Segment;

   -----------------
   -- Has_Element --
   -----------------

   function Has_Element (Position : Path_Cursor) return Boolean is
   begin
      return Unbounded_String_Lists.Has_Element (Position.Value);
   end Has_Element;

   ----------
   -- Host --
   ----------

   function Host (Self : URI'Class) return UTF_8_String is
   begin
      return Ada.Strings.Unbounded.To_String (Self.Host);
   end Host;

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

   procedure Parse
     (Self    : out URI'Class;
      Text    : String;
      Success : out Boolean)
   is
      use Ada.Strings.Unbounded;
      use Ada.Characters.Handling;

      function Parse_Authority
        (Userinfo : out Unbounded_String;
         Host     : out Unbounded_String;
         Port     : out Natural) return Boolean;

      function Parse_Scheme (Scheme : out Unbounded_String)  return Boolean;
      function Parse_User (Userinfo : out Unbounded_String) return Boolean;
      function Parse_Host (Host : out Unbounded_String) return Boolean;
      function Parse_IP_Literal (Host : out Unbounded_String) return Boolean;
      function Parse_Reg_Name (Host : out Unbounded_String) return Boolean;
      function Parse_Port (Port : out Natural) return Boolean;
      function Parse_Segment (Segment : out Unbounded_String) return Boolean;
      function Parse_Path_Abempty
        (Path : out Unbounded_String_Lists.List) return Boolean;

      Index    : Positive := Text'First;

      ---------------------
      -- Parse_Authority --
      ---------------------

      function Parse_Authority
        (Userinfo : out Unbounded_String;
         Host     : out Unbounded_String;
         Port     : out Natural) return Boolean
      is
         Has_Userinfo : Boolean;
         pragma Unreferenced (Has_Userinfo);
         Has_Host     : Boolean;
         Has_Port     : Boolean;
         pragma Unreferenced (Has_Port);
      begin
         Port := 0;

         if Index + 1 <= Text'Last
           and then Text (Index .. Index + 1) = "//"
         then
            Index := Index + 2;
         else
            return False;
         end if;

         Has_Userinfo := Parse_User (Userinfo);
         Has_Host := Parse_Host (Host);

         if Has_Host then
            Has_Port := Parse_Port (Port);
         end if;

         return Has_Host;
      end Parse_Authority;

      ----------------
      -- Parse_Host --
      ----------------

      function Parse_Host (Host : out Unbounded_String) return Boolean is
      begin
         return Parse_IP_Literal (Host) or else Parse_Reg_Name (Host);
      end Parse_Host;

      ----------------------
      -- Parse_IP_Literal --
      ----------------------

      function Parse_IP_Literal (Host : out Unbounded_String) return Boolean is
         Result : Unbounded_String;
      begin
         if Index > Text'Last or else Text (Index) /= '[' then
            return False;
         end if;

         Append (Result, '[');

         for J in Index + 1 .. Text'Last loop
            case Text (J) is
               when Unreserved | Sub_Delims | ':' =>
                  Append (Result, Text (J));
               when ']' =>
                  Append (Result, Text (J));
                  Host := Result;
                  Index := J + 1;
                  return True;
               when others =>
                  return False;
            end case;
         end loop;

         return False;
      end Parse_IP_Literal;

      ------------------------
      -- Parse_Path_Abempty --
      ------------------------

      function Parse_Path_Abempty
        (Path : out Unbounded_String_Lists.List) return Boolean is
      begin
         while Index <= Text'Last
           and then Text (Index) = '/'
         loop
            declare
               Segment : Unbounded_String;
            begin
               Index := Index + 1;

               if Parse_Segment (Segment) then
                  Path.Append (Segment);
               else
                  return False;
               end if;
            end;
         end loop;

         return True;
      end Parse_Path_Abempty;

      ----------------
      -- Parse_Port --
      ----------------

      function Parse_Port (Port : out Natural) return Boolean is
         Result : Unbounded_String;
      begin
         if Index > Text'Last or else Text (Index) /= ':' then
            Port := 0;
            return False;
         end if;

         for J in Index + 1 .. Text'Last loop
            case Text (J) is
               when '0' .. '9' =>
                  Append (Result, Text (J));
               when others =>
                  Index := J;
                  exit;
            end case;
         end loop;

         if Length (Result) = 0 then
            Port := 0;
         else
            Port := Natural'Value (To_String (Result));
         end if;

         return True;
      end Parse_Port;

      --------------------
      -- Parse_Reg_Name --
      --------------------

      function Parse_Reg_Name (Host : out Unbounded_String) return Boolean is
         Result : Unbounded_String;
         J      : Positive := Index;
      begin
         Index := Text'Last + 1;

         while J <= Text'Last loop
            case Text (J) is
               when Unreserved | Sub_Delims =>
                  Append (Result, Text (J));
                  J := J + 1;
               when '%' =>
                  if J + 2 > Text'Last
                    or else not Is_Hexadecimal_Digit (Text (J + 1))
                    or else not Is_Hexadecimal_Digit (Text (J + 2))
                  then
                     return False;
                  end if;

                  Append
                    (Result,
                     Character'Val
                       (Natural'Value ("16#" & Text (J + 1 .. J + 2) & "#")));

                  J := J + 3;
               when others =>
                  Index := J;
                  exit;
            end case;
         end loop;

         Host := Result;
         return True;
      end Parse_Reg_Name;

      ------------------
      -- Parse_Scheme --
      ------------------

      function Parse_Scheme (Scheme : out Unbounded_String)  return Boolean is
         Result : Unbounded_String;
      begin
         if Index > Text'Last
           or else Text (Index) not in 'a' .. 'z' | 'A' .. 'Z'
         then
            return False;
         end if;

         for J in Index .. Text'Last loop
            case Text (J) is
               when 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '+' | '-' | '.' =>
                  Append (Result, Text (J));
               when ':' =>
                  Scheme := Result;
                  Index := J + 1;
                  return True;
               when others =>
                  return False;
            end case;
         end loop;

         return False;
      end Parse_Scheme;

      -------------------
      -- Parse_Segment --
      -------------------

      function Parse_Segment (Segment : out Unbounded_String) return Boolean is
         Result : Unbounded_String;
         J      : Positive := Index;
      begin
         while J <= Text'Last loop
            case Text (J) is
               when Unreserved | Sub_Delims | ':' | '@' =>
                  Append (Result, Text (J));
                  J := J + 1;
               when '%' =>
                  if J + 2 > Text'Last
                    or else not Is_Hexadecimal_Digit (Text (J + 1))
                    or else not Is_Hexadecimal_Digit (Text (J + 2))
                  then
                     return False;
                  end if;

                  Append
                    (Result,
                     Character'Val
                       (Natural'Value ("16#" & Text (J + 1 .. J + 2) & "#")));

                  J := J + 3;
               when others =>
                  Index := J;
                  Segment := Result;
                  return True;
            end case;
         end loop;

         Index := Text'Last + 1;
         Segment := Result;
         return True;
      end Parse_Segment;

      ----------------
      -- Parse_User --
      ----------------

      function Parse_User (Userinfo : out Unbounded_String) return Boolean is
         Result : Unbounded_String;
         J      : Positive := Index;
      begin
         while J <= Text'Last loop
            case Text (J) is
               when Unreserved | Sub_Delims | ':' =>
                  Append (Result, Text (J));
                  J := J + 1;
               when '%' =>
                  if J + 2 > Text'Last
                    or else not Is_Hexadecimal_Digit (Text (J + 1))
                    or else not Is_Hexadecimal_Digit (Text (J + 2))
                  then
                     return False;
                  end if;

                  Append
                    (Result,
                     Character'Val
                       (Natural'Value ("16#" & Text (J + 1 .. J + 2) & "#")));

                  J := J + 3;
               when '@' =>
                  Index := J + 1;
                  Userinfo := Result;
                  return True;

               when others =>
                  return False;
            end case;
         end loop;

         return False;
      end Parse_User;

      Has_Scheme : Boolean;
      Scheme     : Ada.Strings.Unbounded.Unbounded_String;
      User_Info  : Ada.Strings.Unbounded.Unbounded_String;
      Host       : Ada.Strings.Unbounded.Unbounded_String;
      Port       : Natural := 0;
      Path       : Unbounded_String_Lists.List;
   begin
      Has_Scheme := Parse_Scheme (Scheme);

      if Has_Scheme
        and then Parse_Authority (User_Info, Host, Port)
      then
         Success := Parse_Path_Abempty (Path) and then Index > Text'Last;
      else
         Success := False;
      end if;

   --  query and fragment parts are not implemented yet

      if Success then
         Self.Set_Scheme (To_String (Scheme));
         Self.User_Info := User_Info;
         Self.Set_Host (To_String (Host));
         Self.Port := Port;
         Self.Path := Path;
      end if;
   end Parse;

   ------------
   -- Scheme --
   ------------

   function Scheme (Self : URI'Class) return String is
   begin
      return Ada.Strings.Unbounded.To_String (Self.Scheme);
   end Scheme;

   --------------
   -- Set_Host --
   --------------

   procedure Set_Host (Self : in out URI'Class; Value : UTF_8_String) is
      Wide : constant Wide_Wide_String :=
        Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Decode (Value);
      Lower : constant Wide_Wide_String :=
        Ada.Wide_Wide_Characters.Handling.To_Lower (Wide);
      UTF_8 : constant UTF_8_String :=
        Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode (Lower);
   begin
      Self.Host := Ada.Strings.Unbounded.To_Unbounded_String (UTF_8);
   end Set_Host;

   ----------------
   -- Set_Scheme --
   ----------------

   procedure Set_Scheme (Self : in out URI'Class; Value : String) is
   begin
      Self.Scheme := Ada.Strings.Unbounded.To_Unbounded_String
        (Ada.Characters.Handling.To_Lower (Value));
   end Set_Scheme;

   ---------------
   -- To_String --
   ---------------

   function To_String (Position : Path_Cursor) return String is
      Value : constant Ada.Strings.Unbounded.Unbounded_String :=
        Unbounded_String_Lists.Element (Position.Value);
   begin
      return Ada.Strings.Unbounded.To_String (Value);
   end To_String;

   ---------------
   -- To_String --
   ---------------

   function To_String (Self : URI'Class) return URI_String is

      procedure Append (Char : Character);
      procedure Append (Text : String);
      procedure Append_Hex (Char : Character);

      Result : Ada.Strings.Unbounded.Unbounded_String;

      ------------
      -- Append --
      ------------

      procedure Append (Char : Character) is
      begin
         Ada.Strings.Unbounded.Append (Result, Char);
      end Append;

      ------------
      -- Append --
      ------------

      procedure Append (Text : String) is
      begin
         for J of Text loop
            case J is
               when Unreserved =>
                  Append (J);
               when others =>
                  Append_Hex (J);
            end case;
         end loop;
      end Append;

      ----------------
      -- Append_Hex --
      ----------------

      procedure Append_Hex (Char : Character) is
         Pos   : constant Natural := Character'Pos (Char);
         Map   : constant String := "0123456789ABCDEF";
         Upper : constant Character := Map (1 + Pos / 16);
         Lower : constant Character := Map (1 + Pos mod 16);
      begin
         Append ('%');
         Append (Upper);
         Append (Lower);
      end Append_Hex;

      User : constant String :=
        Ada.Strings.Unbounded.To_String (Self.User_Info);
      Host : constant String := Ada.Strings.Unbounded.To_String (Self.Host);
      Port : String := Natural'Image (Self.Port);
   begin
      Ada.Strings.Unbounded.Append (Result, Self.Scheme);
      Ada.Strings.Unbounded.Append (Result, "://");

      if User /= "" then
         Append (User);
         Append ('@');
      end if;

      if Host'Length > 0 and then Host (1) = '[' then
         Ada.Strings.Unbounded.Append (Result, Host);
      else
         Append (Host);
      end if;

      if Self.Port > 0 then
         Port (1) := ':';
         Ada.Strings.Unbounded.Append (Result, Port);
      end if;

      for Segment in Self.Each_Path_Segment loop
         Append ('/');

         Append (Segment.To_String);
      end loop;

      return Ada.Strings.Unbounded.To_String (Result);
   end To_String;

end URIs;