summaryrefslogtreecommitdiff
path: root/gcc/ada/sfn_scan.adb
blob: 1d24ca227f39ae575631ed9b76179029c9c854f5 (plain)
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
724
725
726
727
728
729
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             S F N _ S C A N                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 2000-2010, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT 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.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT 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/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Exceptions; use Ada.Exceptions;

package body SFN_Scan is

   use ASCII;
   --  Allow easy access to control character definitions

   EOF : constant Character := ASCII.SUB;
   --  The character SUB (16#1A#) is used in DOS-derived systems, such as
   --  Windows to signal the end of a text file. If this character appears as
   --  the last character of a file scanned by a call to Scan_SFN_Pragmas, then
   --  it is ignored, otherwise it is treated as an illegal character.

   type String_Ptr is access String;

   S : String_Ptr;
   --  Points to the gnat.adc input file

   P : Natural;
   --  Subscript of next character to process in S

   Line_Num : Natural;
   --  Current line number

   Start_Of_Line : Natural;
   --  Subscript of first character at start of current line

   ----------------------
   -- Local Procedures --
   ----------------------

   function Acquire_Integer return Natural;
   --  This function skips white space, and then scans and returns
   --  an unsigned integer. Raises Error if no integer is present
   --  or if the integer is greater than 999.

   function Acquire_String (B : Natural; E : Natural) return String;
   --  This function takes a string scanned out by Scan_String, strips
   --  the enclosing quote characters and any internal doubled quote
   --  characters, and returns the result as a String. The arguments
   --  B and E are as returned from a call to Scan_String. The lower
   --  bound of the string returned is always 1.

   function Acquire_Unit_Name return String;
   --  Skips white space, and then scans and returns a unit name. The
   --  unit name is cased exactly as it appears in the source file.
   --  The terminating character must be white space, or a comma or
   --  a right parenthesis or end of file.

   function At_EOF return Boolean;
   pragma Inline (At_EOF);
   --  Returns True if at end of file, False if not. Note that this
   --  function does NOT skip white space, so P is always unchanged.

   procedure Check_Not_At_EOF;
   pragma Inline (Check_Not_At_EOF);
   --  Skips past white space if any, and then raises Error if at
   --  end of file. Otherwise returns with P skipped past whitespace.

   function Check_File_Type return Character;
   --  Skips white space if any, and then looks for any of the tokens
   --  Spec_File_Name, Body_File_Name, or Subunit_File_Name. If one
   --  of these is found then the value returned is 's', 'b' or 'u'
   --  respectively, and P is bumped past the token. If none of
   --  these tokens is found, then P is unchanged (except for
   --  possible skip of white space), and a space is returned.

   function Check_Token (T : String) return Boolean;
   --  Skips white space if any, and then checks if the string at the
   --  current location matches the given string T, and the character
   --  immediately following is non-alphabetic, non-numeric. If so,
   --  P is stepped past the token, and True is returned. If not,
   --  P is unchanged (except for possibly skipping past whitespace),
   --  and False is returned. S may contain only lower-case letters
   --  ('a' .. 'z').

   procedure Error (Err : String);
   --  Called if an error is detected. Raises Syntax_Error_In_GNAT_ADC
   --  with a message of the form gnat.adc:line:col: xxx, where xxx is
   --  the string Err passed as a parameter.

   procedure Require_Token (T : String);
   --  Skips white space if any, and then requires the given string
   --  to be present. If it is, the P is stepped past it, otherwise
   --  Error is raised, since this is a syntax error. Require_Token
   --  is used only for sequences of special characters, so there
   --  is no issue of terminators, or casing of letters.

   procedure Scan_String (B : out Natural; E : out Natural);
   --  Skips white space if any, then requires that a double quote
   --  or percent be present (start of string). Raises error if
   --  neither of these two characters is found. Otherwise scans
   --  out the string, and returns with P pointing past the
   --  closing quote and S (B .. E) contains the characters of the
   --  string (including the enclosing quotes, with internal quotes
   --  still doubled). Raises Error if the string is malformed.

   procedure Skip_WS;
   --  Skips P past any white space characters (end of line
   --  characters, spaces, comments, horizontal tab characters).

   ---------------------
   -- Acquire_Integer --
   ---------------------

   function Acquire_Integer return Natural is
      N : Natural := 0;

   begin
      Skip_WS;

      if S (P) not in '0' .. '9' then
         Error ("missing index parameter");
      end if;

      while S (P) in '0' .. '9' loop
         N := N * 10 + Character'Pos (S (P)) - Character'Pos ('0');

         if N > 999 then
            Error ("index value greater than 999");
         end if;

         P := P + 1;
      end loop;

      return N;
   end Acquire_Integer;

   --------------------
   -- Acquire_String --
   --------------------

   function Acquire_String (B : Natural; E : Natural) return String is
      Str : String (1 .. E - B - 1);
      Q   : constant Character := S (B);
      J   : Natural;
      Ptr : Natural;

   begin
      Ptr := B + 1;
      J := 0;
      while Ptr < E loop
         J := J + 1;
         Str (J) := S (Ptr);

         if S (Ptr) = Q and then S (Ptr + 1) = Q then
            Ptr := Ptr + 2;
         else
            Ptr := Ptr + 1;
         end if;
      end loop;

      return Str (1 .. J);
   end Acquire_String;

   -----------------------
   -- Acquire_Unit_Name --
   -----------------------

   function Acquire_Unit_Name return String is
      B : Natural;

   begin
      Check_Not_At_EOF;
      B := P;

      while not At_EOF loop
         exit when S (P) not in '0' .. '9'
           and then S (P) /= '.'
           and then S (P) /= '_'
           and then not (S (P) = '[' and then S (P + 1) = '"')
           and then not (S (P) = '"' and then S (P - 1) = '[')
           and then not (S (P) = '"' and then S (P + 1) = ']')
           and then not (S (P) = ']' and then S (P - 1) = '"')
           and then S (P) < 'A';
         P := P + 1;
      end loop;

      if P = B then
         Error ("null unit name");
      end if;

      return S (B .. P - 1);
   end Acquire_Unit_Name;

   ------------
   -- At_EOF --
   ------------

   function At_EOF return Boolean is
   begin
      --  Immediate return (False) if before last character of file

      if P < S'Last then
         return False;

      --  Special case: DOS EOF character as last character of file is
      --  allowed and treated as an end of file.

      elsif P = S'Last then
         return S (P) = EOF;

      --  If beyond last character of file, then definitely at EOF

      else
         return True;
      end if;
   end At_EOF;

   ---------------------
   -- Check_File_Type --
   ---------------------

   function Check_File_Type return Character is
   begin
      if Check_Token ("spec_file_name") then
         return 's';
      elsif Check_Token ("body_file_name") then
         return 'b';
      elsif Check_Token ("subunit_file_name") then
         return 'u';
      else
         return ' ';
      end if;
   end Check_File_Type;

   ----------------------
   -- Check_Not_At_EOF --
   ----------------------

   procedure Check_Not_At_EOF is
   begin
      Skip_WS;

      if At_EOF then
         Error ("unexpected end of file");
      end if;

      return;
   end Check_Not_At_EOF;

   -----------------
   -- Check_Token --
   -----------------

   function Check_Token (T : String) return Boolean is
      Save_P : Natural;
      C : Character;

   begin
      Skip_WS;
      Save_P := P;

      for K in T'Range loop
         if At_EOF then
            P := Save_P;
            return False;
         end if;

         C := S (P);

         if C in 'A' .. 'Z' then
            C := Character'Val (Character'Pos (C) +
                                 (Character'Pos ('a') - Character'Pos ('A')));
         end if;

         if C /= T (K) then
            P := Save_P;
            return False;
         end if;

         P := P + 1;
      end loop;

      if At_EOF then
         return True;
      end if;

      C := S (P);

      if C in '0' .. '9'
        or else C in 'a' .. 'z'
        or else C in 'A' .. 'Z'
        or else C > Character'Val (127)
      then
         P := Save_P;
         return False;

      else
         return True;
      end if;
   end Check_Token;

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

   procedure Error (Err : String) is
      C : Natural := 0;
      --  Column number

      M : String (1 .. 80);
      --  Buffer used to build resulting error msg

      LM : Natural := 0;
      --  Pointer to last set location in M

      procedure Add_Nat (N : Natural);
      --  Add chars of integer to error msg buffer

      -------------
      -- Add_Nat --
      -------------

      procedure Add_Nat (N : Natural) is
      begin
         if N > 9 then
            Add_Nat (N / 10);
         end if;

         LM := LM + 1;
         M (LM) := Character'Val (N mod 10 + Character'Pos ('0'));
      end Add_Nat;

   --  Start of processing for Error

   begin
      M (1 .. 9) := "gnat.adc:";
      LM := 9;
      Add_Nat (Line_Num);
      LM := LM + 1;
      M (LM) := ':';

      --  Determine column number

      for X in Start_Of_Line .. P loop
         C := C + 1;

         if S (X) = HT then
            C := (C + 7) / 8 * 8;
         end if;
      end loop;

      Add_Nat (C);
      M (LM + 1) := ':';
      LM := LM + 1;
      M (LM + 1) := ' ';
      LM := LM + 1;

      M (LM + 1 .. LM + Err'Length) := Err;
      LM := LM + Err'Length;

      Raise_Exception (Syntax_Error_In_GNAT_ADC'Identity, M (1 .. LM));
   end Error;

   -------------------
   -- Require_Token --
   -------------------

   procedure Require_Token (T : String) is
      SaveP : Natural;

   begin
      Skip_WS;
      SaveP := P;

      for J in T'Range loop

         if At_EOF or else S (P) /= T (J) then
            declare
               S : String (1 .. T'Length + 10);

            begin
               S (1 .. 9) := "missing """;
               S (10 .. T'Length + 9) := T;
               S (T'Length + 10) := '"';
               P := SaveP;
               Error (S);
            end;

         else
            P := P + 1;
         end if;
      end loop;
   end Require_Token;

   ----------------------
   -- Scan_SFN_Pragmas --
   ----------------------

   procedure Scan_SFN_Pragmas
     (Source   : String;
      SFN_Ptr  : Set_File_Name_Ptr;
      SFNP_Ptr : Set_File_Name_Pattern_Ptr)
   is
      B, E : Natural;
      Typ  : Character;
      Cas  : Character;

   begin
      Line_Num := 1;
      S := Source'Unrestricted_Access;
      P := Source'First;
      Start_Of_Line := P;

      --  Loop through pragmas in file

      Main_Scan_Loop : loop
         Skip_WS;
         exit Main_Scan_Loop when At_EOF;

         --  Error if something other than pragma

         if not Check_Token ("pragma") then
            Error ("non pragma encountered");
         end if;

         --  Source_File_Name pragma case

         if Check_Token ("source_file_name")
              or else
             Check_Token ("source_file_name_project")
         then
            Require_Token ("(");

            Typ := Check_File_Type;

            --  First format, with unit name first

            if Typ = ' ' then
               if Check_Token ("unit_name") then
                  Require_Token ("=>");
               end if;

               declare
                  U : constant String := Acquire_Unit_Name;

               begin
                  Require_Token (",");
                  Typ := Check_File_Type;

                  if Typ /= 's' and then Typ /= 'b' then
                     Error ("bad pragma");
                  end if;

                  Require_Token ("=>");
                  Scan_String (B, E);

                  declare
                     F : constant String := Acquire_String (B, E);
                     X : Natural;

                  begin
                     --  Scan Index parameter if present

                     if Check_Token (",") then
                        if Check_Token ("index") then
                           Require_Token ("=>");
                        end if;

                        X := Acquire_Integer;
                     else
                        X := 0;
                     end if;

                     Require_Token (")");
                     Require_Token (";");
                     SFN_Ptr.all (Typ, U, F, X);
                  end;
               end;

            --  Second format with pattern string

            else
               Require_Token ("=>");
               Scan_String (B, E);

               declare
                  Pat : constant String := Acquire_String (B, E);
                  Nas : Natural := 0;

               begin
                  --  Check exactly one asterisk

                  for J in Pat'Range loop
                     if Pat (J) = '*' then
                        Nas := Nas + 1;
                     end if;
                  end loop;

                  if Nas /= 1 then
                     Error ("** not allowed");
                  end if;

                  B := 0;
                  E := 0;
                  Cas := ' ';

                  --  Loop to scan out Casing or Dot_Replacement parameters

                  loop
                     Check_Not_At_EOF;
                     exit when S (P) = ')';
                     Require_Token (",");

                     if Check_Token ("casing") then
                        Require_Token ("=>");

                        if Cas /= ' ' then
                           Error ("duplicate casing argument");
                        elsif Check_Token ("lowercase") then
                           Cas := 'l';
                        elsif Check_Token ("uppercase") then
                           Cas := 'u';
                        elsif Check_Token ("mixedcase") then
                           Cas := 'm';
                        else
                           Error ("invalid casing argument");
                        end if;

                     elsif Check_Token ("dot_replacement") then
                        Require_Token ("=>");

                        if E /= 0 then
                           Error ("duplicate dot_replacement");
                        else
                           Scan_String (B, E);
                        end if;

                     else
                        Error ("invalid argument");
                     end if;
                  end loop;

                  Require_Token (")");
                  Require_Token (";");

                  if Cas = ' ' then
                     Cas := 'l';
                  end if;

                  if E = 0 then
                     SFNP_Ptr.all (Pat, Typ, ".", Cas);

                  else
                     declare
                        Dot : constant String := Acquire_String (B, E);

                     begin
                        SFNP_Ptr.all (Pat, Typ, Dot, Cas);
                     end;
                  end if;
               end;
            end if;

         --  Some other pragma, scan to semicolon at end of pragma

         else
            Skip_Loop : loop
               exit Main_Scan_Loop when At_EOF;
               exit Skip_Loop when S (P) = ';';

               if S (P) = '"' or else S (P) = '%' then
                  Scan_String (B, E);
               else
                  P := P + 1;
               end if;
            end loop Skip_Loop;

            --  We successfully skipped to semicolon, so skip past it

            P := P + 1;
         end if;
      end loop Main_Scan_Loop;

   exception
      when others =>
         Cursor := P - S'First + 1;
         raise;
   end Scan_SFN_Pragmas;

   -----------------
   -- Scan_String --
   -----------------

   procedure Scan_String (B : out Natural; E : out Natural) is
      Q : Character;

   begin
      Check_Not_At_EOF;

      if S (P) = '"' then
         Q := '"';
      elsif S (P) = '%' then
         Q := '%';
      else
         Error ("bad string");
         Q := '"';
      end if;

      --  Scan out the string, B points to first char

      B := P;
      P := P + 1;

      loop
         if At_EOF or else S (P) = LF or else S (P) = CR then
            Error -- CODEFIX
              ("missing string quote");

         elsif S (P) = HT then
            Error ("tab character in string");

         elsif S (P) /= Q then
            P := P + 1;

         --  We have a quote

         else
            P := P + 1;

            --  Check for doubled quote

            if not At_EOF and then S (P) = Q then
               P := P + 1;

            --  Otherwise this is the terminating quote

            else
               E := P - 1;
               return;
            end if;
         end if;
      end loop;
   end Scan_String;

   -------------
   -- Skip_WS --
   -------------

   procedure Skip_WS is
   begin
      WS_Scan : while not At_EOF loop
         case S (P) is

            --  End of physical line

            when CR | LF =>
               Line_Num := Line_Num + 1;
               P := P + 1;

               while not At_EOF
                 and then (S (P) = CR or else S (P) = LF)
               loop
                  Line_Num := Line_Num + 1;
                  P := P + 1;
               end loop;

               Start_Of_Line := P;

            --  All other cases of white space characters

            when ' ' | FF | VT | HT =>
               P := P + 1;

            --  Comment

            when '-' =>
               P := P + 1;

               if At_EOF then
                  Error ("bad comment");

               elsif S (P) = '-' then
                  P := P + 1;

                  while not At_EOF loop
                     case S (P) is
                        when CR | LF | FF | VT =>
                           exit;
                        when others =>
                           P := P + 1;
                     end case;
                  end loop;

               else
                  P := P - 1;
                  exit WS_Scan;
               end if;

            when others =>
               exit WS_Scan;

         end case;
      end loop WS_Scan;
   end Skip_WS;

end SFN_Scan;