/
scanner.adb
2232 lines (2089 loc) · 77.3 KB
/
scanner.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- VHDL lexical scanner.
-- Copyright (C) 2002 - 2014 Tristan Gingold
--
-- GHDL is free software; you can redistribute it and/or modify it under
-- the terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 2, or (at your option) any later
-- version.
--
-- GHDL 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. See the GNU General Public License
-- for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with GHDL; see the file COPYING. If not, write to the Free
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Errorout; use Errorout;
with Name_Table;
with Files_Map; use Files_Map;
with Std_Names;
with Str_Table;
with Flags; use Flags;
package body Scanner is
-- This classification is a simplification of the categories of LRM93 13.1
-- LRM93 13.1
-- The only characters allowed in the text of a VHDL description are the
-- graphic characters and format effector.
type Character_Kind_Type is
(
-- Neither a format effector nor a graphic character.
Invalid,
Format_Effector,
Lower_Case_Letter,
Upper_Case_Letter,
Digit,
Special_Character,
Space_Character,
Other_Special_Character
);
-- LRM93 13.1
-- basic_graphic_character ::=
-- upper_case_letter | digit | special_character | space_character
--
--subtype Basic_Graphic_Character is
-- Character_Kind_Type range Upper_Case_Letter .. Space_Character;
-- LRM93 13.1
-- graphic_character ::=
-- basic_graphic_character | lower_case_letter | other_special_character
--
-- Note: There are 191 graphic characters.
subtype Graphic_Character is
Character_Kind_Type range Lower_Case_Letter .. Other_Special_Character;
-- letter ::= upper_case_letter | lower_case_letter
subtype Letter is
Character_Kind_Type range Lower_Case_Letter .. Upper_Case_Letter;
-- LRM93 13.1
-- The characters included in each of the categories of basic graphic
-- characters are defined as follows:
type Character_Array is array (Character) of Character_Kind_Type;
pragma Suppress_Initialization (Character_Array);
Characters_Kind : constant Character_Array :=
(NUL .. BS => Invalid,
-- Format effectors are the ISO (and ASCII) characters called horizontal
-- tabulation, vertical tabulation, carriage return, line feed, and form
-- feed.
HT | LF | VT | FF | CR => Format_Effector,
SO .. US => Invalid,
-- 1. upper case letters
'A' .. 'Z' | UC_A_Grave .. UC_O_Diaeresis |
UC_O_Oblique_Stroke .. UC_Icelandic_Thorn => Upper_Case_Letter,
-- 2. digits
'0' .. '9' => Digit,
-- 3. special characters
'"' | '#' | '&' | ''' | '(' | ')' | '+' | ',' | '-' | '.' | '/'
| ':' | ';' | '<' | '=' | '>' | '[' | ']'
| '_' | '|' | '*' => Special_Character,
-- 4. the space characters
' ' | NBSP => Space_Character,
-- 5. lower case letters
'a' .. 'z' | LC_German_Sharp_S .. LC_O_Diaeresis |
LC_O_Oblique_Stroke .. LC_Y_Diaeresis => Lower_Case_Letter,
-- 6. other special characters
'!' | '$' | '%' | '@' | '?' | '\' | '^' | '`' | '{' | '}' | '~'
| Inverted_Exclamation .. Inverted_Question | Multiplication_Sign |
Division_Sign => Other_Special_Character,
-- '¡' -- INVERTED EXCLAMATION MARK
-- '¢' -- CENT SIGN
-- '£' -- POUND SIGN
-- '¤' -- CURRENCY SIGN
-- '¥' -- YEN SIGN
-- '¦' -- BROKEN BAR
-- '§' -- SECTION SIGN
-- '¨' -- DIAERESIS
-- '©' -- COPYRIGHT SIGN
-- 'ª' -- FEMININE ORDINAL INDICATOR
-- '«' -- LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
-- '¬' -- NOT SIGN
-- '' -- SOFT HYPHEN
-- '®' -- REGISTERED SIGN
-- '¯' -- MACRON
-- '°' -- DEGREE SIGN
-- '±' -- PLUS-MINUS SIGN
-- '²' -- SUPERSCRIPT TWO
-- '³' -- SUPERSCRIPT THREE
-- '´' -- ACUTE ACCENT
-- 'µ' -- MICRO SIGN
-- '¶' -- PILCROW SIGN
-- '·' -- MIDDLE DOT
-- '¸' -- CEDILLA
-- '¹' -- SUPERSCRIPT ONE
-- 'º' -- MASCULINE ORDINAL INDICATOR
-- '»' -- RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
-- '¼' -- VULGAR FRACTION ONE QUARTER
-- '½' -- VULGAR FRACTION ONE HALF
-- '¾' -- VULGAR FRACTION THREE QUARTERS
-- '¿' -- INVERTED QUESTION MARK
-- '×' -- MULTIPLICATION SIGN
-- '÷' -- DIVISION SIGN
DEL .. APC => Invalid);
-- The context contains the whole internal state of the scanner, ie
-- it can be used to push/pop a lexical analysis, to restart the
-- scanner from a context marking a previous point.
type Scan_Context is record
Source: File_Buffer_Acc;
Source_File: Source_File_Entry;
Line_Number: Natural;
Line_Pos: Source_Ptr;
Pos: Source_Ptr;
Token_Pos: Source_Ptr;
File_Len: Source_Ptr;
Token: Token_Type;
Prev_Token: Token_Type;
Bit_Str_Base : Character;
Bit_Str_Sign : Character;
Str_Id : String8_Id;
Str_Len : Nat32;
Identifier: Name_Id;
Int64: Iir_Int64;
Fp64: Iir_Fp64;
end record;
pragma Suppress_Initialization (Scan_Context);
-- The current context.
-- Default value is an invalid context.
Current_Context: Scan_Context := (Source => null,
Source_File => No_Source_File_Entry,
Line_Number => 0,
Line_Pos => 0,
Pos => 0,
Token_Pos => 0,
File_Len => 0,
Token => Tok_Invalid,
Prev_Token => Tok_Invalid,
Identifier => Null_Identifier,
Bit_Str_Base => ' ',
Bit_Str_Sign => ' ',
Str_Id => Null_String8,
Str_Len => 0,
Int64 => 0,
Fp64 => 0.0);
Source: File_Buffer_Acc renames Current_Context.Source;
Pos: Source_Ptr renames Current_Context.Pos;
-- When CURRENT_TOKEN is an identifier, its name_id is stored into
-- this global variable.
-- Function current_text can be used to convert it into an iir.
function Current_Identifier return Name_Id is
begin
return Current_Context.Identifier;
end Current_Identifier;
procedure Invalidate_Current_Identifier is
begin
Current_Context.Identifier := Null_Identifier;
end Invalidate_Current_Identifier;
procedure Invalidate_Current_Token is
begin
if Current_Token /= Tok_Invalid then
Current_Context.Prev_Token := Current_Token;
Current_Token := Tok_Invalid;
end if;
end Invalidate_Current_Token;
function Current_String_Id return String8_Id is
begin
return Current_Context.Str_Id;
end Current_String_Id;
function Current_String_Length return Nat32 is
begin
return Current_Context.Str_Len;
end Current_String_Length;
function Get_Bit_String_Base return Character is
begin
return Current_Context.Bit_Str_Base;
end Get_Bit_String_Base;
function Get_Bit_String_Sign return Character is
begin
return Current_Context.Bit_Str_Sign;
end Get_Bit_String_Sign;
function Current_Iir_Int64 return Iir_Int64 is
begin
return Current_Context.Int64;
end Current_Iir_Int64;
function Current_Iir_Fp64 return Iir_Fp64 is
begin
return Current_Context.Fp64;
end Current_Iir_Fp64;
function Get_Current_Source_File return Source_File_Entry is
begin
return Current_Context.Source_File;
end Get_Current_Source_File;
function Get_Current_Line return Natural is
begin
return Current_Context.Line_Number;
end Get_Current_Line;
function Get_Current_Column return Natural
is
Col : Natural;
Name : Name_Id;
begin
Coord_To_Position
(Current_Context.Source_File,
Current_Context.Line_Pos,
Integer (Current_Context.Pos - Current_Context.Line_Pos),
Name, Col);
return Col;
end Get_Current_Column;
function Get_Token_Column return Natural
is
Col : Natural;
Name : Name_Id;
begin
Coord_To_Position
(Current_Context.Source_File,
Current_Context.Line_Pos,
Integer (Current_Context.Token_Pos - Current_Context.Line_Pos),
Name, Col);
return Col;
end Get_Token_Column;
function Get_Token_Position return Source_Ptr is
begin
return Current_Context.Token_Pos;
end Get_Token_Position;
function Get_Position return Source_Ptr is
begin
return Current_Context.Pos;
end Get_Position;
procedure Set_File (Source_File : Source_File_Entry)
is
N_Source: File_Buffer_Acc;
begin
pragma Assert (Current_Context.Source = null);
pragma Assert (Source_File /= No_Source_File_Entry);
N_Source := Get_File_Source (Source_File);
Current_Context := (Source => N_Source,
Source_File => Source_File,
Line_Number => 1,
Line_Pos => 0,
Pos => N_Source'First,
Token_Pos => 0, -- should be invalid,
File_Len => Get_File_Length (Source_File),
Token => Tok_Invalid,
Prev_Token => Tok_Invalid,
Identifier => Null_Identifier,
Bit_Str_Base => ' ',
Bit_Str_Sign => ' ',
Str_Id => Null_String8,
Str_Len => 0,
Int64 => -1,
Fp64 => 0.0);
Current_Token := Tok_Invalid;
end Set_File;
function Detect_Encoding_Errors return Boolean
is
C : constant Character := Source (Pos);
begin
-- No need to check further if first character is plain ASCII-7
if C >= ' ' and C < Character'Val (127) then
return False;
end if;
-- UTF-8 BOM is EF BB BF
if Source (Pos + 0) = Character'Val (16#ef#)
and then Source (Pos + 1) = Character'Val (16#bb#)
and then Source (Pos + 2) = Character'Val (16#bf#)
then
Error_Msg_Scan
("source encoding must be latin-1 (UTF-8 BOM detected)");
return True;
end if;
-- UTF-16 BE BOM is FE FF
if Source (Pos + 0) = Character'Val (16#fe#)
and then Source (Pos + 1) = Character'Val (16#ff#)
then
Error_Msg_Scan
("source encoding must be latin-1 (UTF-16 BE BOM detected)");
return True;
end if;
-- UTF-16 LE BOM is FF FE
if Source (Pos + 0) = Character'Val (16#ff#)
and then Source (Pos + 1) = Character'Val (16#fe#)
then
Error_Msg_Scan
("source encoding must be latin-1 (UTF-16 LE BOM detected)");
return True;
end if;
-- Certainly weird, but scanner/parser will catch it.
return False;
end Detect_Encoding_Errors;
procedure Set_Current_Position (Position: Source_Ptr)
is
Loc : Location_Type;
Offset: Natural;
File_Entry : Source_File_Entry;
begin
-- Scanner must have been initialized.
pragma Assert (Current_Context.Source /= null);
Current_Token := Tok_Invalid;
Current_Context.Pos := Position;
Loc := File_Pos_To_Location (Current_Context.Source_File,
Current_Context.Pos);
Location_To_Coord (Loc,
File_Entry, Current_Context.Line_Pos,
Current_Context.Line_Number, Offset);
end Set_Current_Position;
procedure Close_File is
begin
Current_Context.Source := null;
end Close_File;
-- Emit an error when a character above 128 was found.
-- This must be called only in vhdl87.
procedure Error_8bit is
begin
Error_Msg_Scan ("8 bits characters not allowed in vhdl87");
end Error_8bit;
-- Emit an error when a separator is expected.
procedure Error_Separator is
begin
Error_Msg_Scan ("a separator is required here");
end Error_Separator;
-- scan a decimal literal or a based literal.
--
-- LRM93 13.4.1
-- DECIMAL_LITERAL ::= INTEGER [ . INTEGER ] [ EXPONENT ]
-- EXPONENT ::= E [ + ] INTEGER | E - INTEGER
--
-- LRM93 13.4.2
-- BASED_LITERAL ::= BASE # BASED_INTEGER [ . BASED_INTEGER ] # EXPONENT
-- BASE ::= INTEGER
procedure Scan_Literal is separate;
-- Scan a string literal.
--
-- LRM93 13.6 / LRM08 15.7
-- A string literal is formed by a sequence of graphic characters
-- (possibly none) enclosed between two quotation marks used as string
-- brackets.
-- STRING_LITERAL ::= " { GRAPHIC_CHARACTER } "
--
-- IN: for a string, at the call of this procedure, the current character
-- must be either '"' or '%'.
procedure Scan_String
is
-- The quotation character (can be " or %).
Mark: Character;
-- Current character.
C : Character;
-- Current length.
Length : Nat32;
begin
-- String delimiter.
Mark := Source (Pos);
pragma Assert (Mark = '"' or else Mark = '%');
Pos := Pos + 1;
Length := 0;
Current_Context.Str_Id := Str_Table.Create_String8;
loop
C := Source (Pos);
if C = Mark then
-- LRM93 13.6
-- If a quotation mark value is to be represented in the sequence
-- of character values, then a pair of adjacent quoatation
-- characters marks must be written at the corresponding place
-- within the string literal.
-- LRM93 13.10
-- Any pourcent sign within the sequence of characters must then
-- be doubled, and each such doubled percent sign is interpreted
-- as a single percent sign value.
-- The same replacement is allowed for a bit string literal,
-- provieded that both bit string brackets are replaced.
Pos := Pos + 1;
exit when Source (Pos) /= Mark;
end if;
case Characters_Kind (C) is
when Format_Effector =>
if Mark = '%' then
-- No matching '%' has been found. Consider '%' was used
-- as the remainder operator, instead of 'rem'. This will
-- improve the error message.
Error_Msg_Scan
(Get_Token_Location,
"'%%' is not a vhdl operator, use 'rem'");
Current_Token := Tok_Rem;
Pos := Current_Context.Token_Pos + 1;
return;
end if;
Error_Msg_Scan ("format effector not allowed in a string");
exit;
when Invalid =>
Error_Msg_Scan
("invalid character not allowed, even in a string");
when Graphic_Character =>
if Vhdl_Std = Vhdl_87 and then C > Character'Val (127) then
Error_8bit;
end if;
end case;
if C = '"' and Mark = '%' then
-- LRM93 13.10
-- The quotation marks (") used as string brackets at both ends of
-- a string literal can be replaced by percent signs (%), provided
-- that the enclosed sequence of characters constains no quotation
-- marks, and provided that both string brackets are replaced.
Error_Msg_Scan
("'""' cannot be used in a string delimited with '%%'");
end if;
Length := Length + 1;
Str_Table.Append_String8 (Character'Pos (C));
Pos := Pos + 1;
end loop;
Current_Token := Tok_String;
Current_Context.Str_Len := Length;
end Scan_String;
-- Scan a bit string literal.
--
-- LRM93 13.7
-- A bit string literal is formed by a sequence of extended digits
-- (possibly none) enclosed between two quotations used as bit string
-- brackets, preceded by a base specifier.
-- BIT_STRING_LITERAL ::= BASE_SPECIFIER " [ BIT_VALUE ] "
-- BIT_VALUE ::= EXTENDED_DIGIT { [ UNDERLINE ] EXTENDED_DIGIT }
--
-- The current character must be a base specifier, followed by '"' or '%'.
-- The base must be valid.
procedure Scan_Bit_String (Base_Log : Nat32)
is
-- Position of character '0'.
Pos_0 : constant Nat8 := Character'Pos ('0');
-- Used for the base.
subtype Nat4 is Natural range 1 .. 4;
Base : constant Nat32 := 2 ** Nat4 (Base_Log);
-- The quotation character (can be " or %).
Orig_Pos : constant Source_Ptr := Pos;
Mark : constant Character := Source (Orig_Pos);
-- Current character.
C : Character;
-- Current length.
Length : Nat32;
-- Digit value.
V, D : Nat8;
-- True if invalid character already found, to avoid duplicate message.
Has_Invalid : Boolean;
begin
pragma Assert (Mark = '"' or else Mark = '%');
Pos := Pos + 1;
Length := 0;
Has_Invalid := False;
Current_Context.Str_Id := Str_Table.Create_String8;
loop
<< Again >> null;
C := Source (Pos);
Pos := Pos + 1;
exit when C = Mark;
-- LRM93 13.7
-- If the base specifier is 'B', the extended digits in the bit
-- value are restricted to 0 and 1.
-- If the base specifier is 'O', the extended digits int the bit
-- value are restricted to legal digits in the octal number
-- system, ie, the digits 0 through 7.
-- If the base specifier is 'X', the extended digits are all digits
-- together with the letters A through F.
case C is
when '0' .. '9' =>
V := Character'Pos (C) - Character'Pos ('0');
when 'A' .. 'F' =>
V := Character'Pos (C) - Character'Pos ('A') + 10;
when 'a' .. 'f' =>
-- LRM93 13.7
-- A letter in a bit string literal (...) can be written either
-- in lowercase or in upper case, with the same meaning.
V := Character'Pos (C) - Character'Pos ('a') + 10;
when '_' =>
if Source (Pos) = '_' then
Error_Msg_Scan
("double underscore not allowed in a bit string");
end if;
if Source (Pos - 2) = Mark then
Error_Msg_Scan
("underscore not allowed at the start of a bit string");
elsif Source (Pos) = Mark then
Error_Msg_Scan
("underscore not allowed at the end of a bit string");
end if;
goto Again;
when '"' =>
pragma Assert (Mark = '%');
Error_Msg_Scan
("'""' cannot close a bit string opened by '%%'");
exit;
when '%' =>
pragma Assert (Mark = '"');
Error_Msg_Scan
("'%%' cannot close a bit string opened by '""'");
exit;
when others =>
if Characters_Kind (C) in Graphic_Character then
if Vhdl_Std >= Vhdl_08 then
V := Nat8'Last;
else
if not Has_Invalid then
Error_Msg_Scan ("invalid character in bit string");
Has_Invalid := True;
end if;
-- Continue the bit string
V := 0;
end if;
else
if Mark = '%' then
Error_Msg_Scan
(File_Pos_To_Location
(Current_Context.Source_File, Orig_Pos),
"'%%' is not a vhdl operator, use 'rem'");
Current_Token := Tok_Rem;
Pos := Orig_Pos + 1;
return;
else
Error_Msg_Scan ("bit string not terminated");
Pos := Pos - 1;
end if;
exit;
end if;
end case;
-- Expand bit value.
if Vhdl_Std >= Vhdl_08 and V > Base then
-- Expand as graphic character.
for I in 1 .. Base_Log loop
Str_Table.Append_String8_Char (C);
end loop;
else
-- Expand as extended digits.
case Base_Log is
when 1 =>
if V > 1 then
Error_Msg_Scan
("invalid character in a binary bit string");
V := 1;
end if;
Str_Table.Append_String8 (Pos_0 + V);
when 3 =>
if V > 7 then
Error_Msg_Scan
("invalid character in a octal bit string");
V := 7;
end if;
for I in 1 .. 3 loop
D := V / 4;
Str_Table.Append_String8 (Pos_0 + D);
V := (V - 4 * D) * 2;
end loop;
when 4 =>
for I in 1 .. 4 loop
D := V / 8;
Str_Table.Append_String8 (Pos_0 + D);
V := (V - 8 * D) * 2;
end loop;
when others =>
raise Internal_Error;
end case;
end if;
Length := Length + Base_Log;
end loop;
-- Note: the length of the bit string may be 0.
Current_Token := Tok_Bit_String;
Current_Context.Str_Len := Length;
end Scan_Bit_String;
-- Scan a decimal bit string literal. For base specifier D the algorithm
-- is rather different: all the graphic characters shall be digits, and we
-- need to use a (not very efficient) arbitrary precision multiplication.
procedure Scan_Dec_Bit_String
is
use Str_Table;
Id : String8_Id;
-- Position of character '0'.
Pos_0 : constant Nat8 := Character'Pos ('0');
-- Current character.
C : Character;
-- Current length.
Length : Nat32;
-- Digit value.
V, D : Nat8;
type Carries_Type is array (0 .. 3) of Nat8;
Carries : Carries_Type;
No_Carries : constant Carries_Type := (others => Pos_0);
-- Shift right carries. Note the Carries (0) is the LSB.
procedure Shr_Carries is
begin
Carries := (Carries (1), Carries (2), Carries (3), Pos_0);
end Shr_Carries;
procedure Append_Carries is
begin
-- Expand the bit string. Note that position 1 of the string8 is
-- the MSB.
while Carries /= No_Carries loop
Append_String8 (Pos_0);
Length := Length + 1;
for I in reverse 2 .. Length loop
Set_Element_String8 (Id, I, Element_String8 (Id, I - 1));
end loop;
Set_Element_String8 (Id, 1, Carries (0));
Shr_Carries;
end loop;
end Append_Carries;
-- Add 1 to Carries. Overflow is not allowed and should be prevented by
-- construction.
procedure Add_One_To_Carries is
begin
for I in Carries'Range loop
if Carries (I) = Pos_0 then
Carries (I) := Pos_0 + 1;
-- End of propagation.
exit;
else
Carries (I) := Pos_0;
-- Continue propagation.
pragma Assert (I < Carries'Last);
end if;
end loop;
end Add_One_To_Carries;
begin
pragma Assert (Source (Pos) = '"');
Pos := Pos + 1;
Length := 0;
Id := Create_String8;
Current_Context.Str_Id := Id;
loop
<< Again >> null;
C := Source (Pos);
Pos := Pos + 1;
exit when C = '"';
if C in '0' .. '9' then
V := Character'Pos (C) - Character'Pos ('0');
elsif C = '_' then
if Source (Pos) = '_' then
Error_Msg_Scan
("double underscore not allowed in a bit string");
end if;
if Source (Pos - 2) = '"' then
Error_Msg_Scan
("underscore not allowed at the start of a bit string");
elsif Source (Pos) = '"' then
Error_Msg_Scan
("underscore not allowed at the end of a bit string");
end if;
goto Again;
else
if Characters_Kind (C) in Graphic_Character then
Error_Msg_Scan
("graphic character not allowed in decimal bit string");
-- Continue the bit string
V := 0;
else
Error_Msg_Scan ("bit string not terminated");
Pos := Pos - 1;
exit;
end if;
end if;
-- Multiply by 10.
Carries := (others => Pos_0);
for I in reverse 1 .. Length loop
-- Shift by 1 (*2).
D := Element_String8 (Id, I);
Set_Element_String8 (Id, I, Carries (0));
Shr_Carries;
-- Add D and D * 4.
if D /= Pos_0 then
Add_One_To_Carries;
-- Add_Four_To_Carries:
for I in 2 .. 3 loop
if Carries (I) = Pos_0 then
Carries (I) := Pos_0 + 1;
-- End of propagation.
exit;
else
Carries (I) := Pos_0;
-- Continue propagation.
end if;
end loop;
end if;
end loop;
Append_Carries;
-- Add V.
for I in Carries'Range loop
D := V / 2;
Carries (I) := Pos_0 + (V - 2 * D);
V := D;
end loop;
for I in reverse 1 .. Length loop
D := Element_String8 (Id, I);
if D /= Pos_0 then
Add_One_To_Carries;
end if;
Set_Element_String8 (Id, I, Carries (0));
Shr_Carries;
exit when Carries = No_Carries;
end loop;
Append_Carries;
end loop;
Current_Token := Tok_Bit_String;
Current_Context.Str_Len := Length;
end Scan_Dec_Bit_String;
-- LRM08 15.2 Character set
-- For each uppercase letter, there is a corresponding lowercase letter;
-- and for each lowercase letter except [y diaeresis] and [german sharp s],
-- there is a corresponding uppercase letter.
type Character_Map is array (Character) of Character;
To_Lower_Map : constant Character_Map :=
(
-- Uppercase ASCII letters.
'A' => 'a',
'B' => 'b',
'C' => 'c',
'D' => 'd',
'E' => 'e',
'F' => 'f',
'G' => 'g',
'H' => 'h',
'I' => 'i',
'J' => 'j',
'K' => 'k',
'L' => 'l',
'M' => 'm',
'N' => 'n',
'O' => 'o',
'P' => 'p',
'Q' => 'q',
'R' => 'r',
'S' => 's',
'T' => 't',
'U' => 'u',
'V' => 'v',
'W' => 'w',
'X' => 'x',
'Y' => 'y',
'Z' => 'z',
-- Lowercase ASCII letters.
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
'e' => 'e',
'f' => 'f',
'g' => 'g',
'h' => 'h',
'i' => 'i',
'j' => 'j',
'k' => 'k',
'l' => 'l',
'm' => 'm',
'n' => 'n',
'o' => 'o',
'p' => 'p',
'q' => 'q',
'r' => 'r',
's' => 's',
't' => 't',
'u' => 'u',
'v' => 'v',
'w' => 'w',
'x' => 'x',
'y' => 'y',
'z' => 'z',
-- Uppercase Latin-1 letters.
UC_A_Grave => LC_A_Grave,
UC_A_Acute => LC_A_Acute,
UC_A_Circumflex => LC_A_Circumflex,
UC_A_Tilde => LC_A_Tilde,
UC_A_Diaeresis => LC_A_Diaeresis,
UC_A_Ring => LC_A_Ring,
UC_AE_Diphthong => LC_AE_Diphthong,
UC_C_Cedilla => LC_C_Cedilla,
UC_E_Grave => LC_E_Grave,
UC_E_Acute => LC_E_Acute,
UC_E_Circumflex => LC_E_Circumflex,
UC_E_Diaeresis => LC_E_Diaeresis,
UC_I_Grave => LC_I_Grave,
UC_I_Acute => LC_I_Acute,
UC_I_Circumflex => LC_I_Circumflex,
UC_I_Diaeresis => LC_I_Diaeresis,
UC_Icelandic_Eth => LC_Icelandic_Eth,
UC_N_Tilde => LC_N_Tilde,
UC_O_Grave => LC_O_Grave,
UC_O_Acute => LC_O_Acute,
UC_O_Circumflex => LC_O_Circumflex,
UC_O_Tilde => LC_O_Tilde,
UC_O_Diaeresis => LC_O_Diaeresis,
UC_O_Oblique_Stroke => LC_O_Oblique_Stroke,
UC_U_Grave => LC_U_Grave,
UC_U_Acute => LC_U_Acute,
UC_U_Circumflex => LC_U_Circumflex,
UC_U_Diaeresis => LC_U_Diaeresis,
UC_Y_Acute => LC_Y_Acute,
UC_Icelandic_Thorn => LC_Icelandic_Thorn,
-- Lowercase Latin-1 letters.
LC_A_Grave => LC_A_Grave,
LC_A_Acute => LC_A_Acute,
LC_A_Circumflex => LC_A_Circumflex,
LC_A_Tilde => LC_A_Tilde,
LC_A_Diaeresis => LC_A_Diaeresis,
LC_A_Ring => LC_A_Ring,
LC_AE_Diphthong => LC_AE_Diphthong,
LC_C_Cedilla => LC_C_Cedilla,
LC_E_Grave => LC_E_Grave,
LC_E_Acute => LC_E_Acute,
LC_E_Circumflex => LC_E_Circumflex,
LC_E_Diaeresis => LC_E_Diaeresis,
LC_I_Grave => LC_I_Grave,
LC_I_Acute => LC_I_Acute,
LC_I_Circumflex => LC_I_Circumflex,
LC_I_Diaeresis => LC_I_Diaeresis,
LC_Icelandic_Eth => LC_Icelandic_Eth,
LC_N_Tilde => LC_N_Tilde,
LC_O_Grave => LC_O_Grave,
LC_O_Acute => LC_O_Acute,
LC_O_Circumflex => LC_O_Circumflex,
LC_O_Tilde => LC_O_Tilde,
LC_O_Diaeresis => LC_O_Diaeresis,
LC_O_Oblique_Stroke => LC_O_Oblique_Stroke,
LC_U_Grave => LC_U_Grave,
LC_U_Acute => LC_U_Acute,
LC_U_Circumflex => LC_U_Circumflex,
LC_U_Diaeresis => LC_U_Diaeresis,
LC_Y_Acute => LC_Y_Acute,
LC_Icelandic_Thorn => LC_Icelandic_Thorn,
-- Lowercase latin-1 characters without corresponding uppercase one.
LC_Y_Diaeresis => LC_Y_Diaeresis,
LC_German_Sharp_S => LC_German_Sharp_S,
-- Not a letter.
others => NUL);
-- LRM93 13.3.1
-- Basic Identifiers
-- A basic identifier consists only of letters, digits, and underlines.
-- BASIC_IDENTIFIER ::= LETTER { [ UNDERLINE ] LETTER_OR_DIGIT }
-- LETTER_OR_DIGIT ::= LETTER | DIGIT
-- LETTER ::= UPPER_CASE_LETTER | LOWER_CASE_LETTER
--
-- NB: At the call of this procedure, the current character must be a legal
-- character for a basic identifier.
procedure Scan_Identifier (Allow_PSL : Boolean)
is
use Name_Table;
Buffer : String (1 .. Max_Name_Length);
C : Character;
Len : Natural;
begin
-- This is an identifier or a key word.
Len := 0;
loop
-- Source (pos) is correct.
-- LRM93 13.3.1
-- All characters if a basic identifier are signifiant, including
-- any underline character inserted between a letter or digit and
-- an adjacent letter or digit.
-- Basic identifiers differing only in the use of the corresponding
-- upper and lower case letters are considered as the same.
-- This is achieved by converting all upper case letters into
-- equivalent lower case letters.
-- The opposite (converting in lower case letters) is not possible,
-- because two characters have no upper-case equivalent.
C := Source (Pos);
case C is
when 'A' .. 'Z' =>
C := Character'Val
(Character'Pos (C)
+ Character'Pos ('a') - Character'Pos ('A'));
when 'a' .. 'z' | '0' .. '9' =>
null;
when '_' =>
if Source (Pos + 1) = '_' then
Error_Msg_Scan ("two underscores can't be consecutive");
end if;
when ' ' | ')' | '.' | ';' | ':' =>
exit;
when others =>
-- Non common case.
case Characters_Kind (C) is
when Upper_Case_Letter | Lower_Case_Letter =>
if Vhdl_Std = Vhdl_87 then
Error_8bit;
end if;
C := To_Lower_Map (C);
pragma Assert (C /= NUL);
when Digit =>
raise Internal_Error;
when others =>
exit;
end case;
end case;
-- Put character in name buffer. FIXME: compute the hash at the same
-- time ?
Len := Len + 1;
Buffer (Len) := C;
-- Next character.
Pos := Pos + 1;
end loop;
if Source (Pos - 1) = '_' then
if not Allow_PSL then
-- Some PSL reserved words finish with '_'. This case is handled
-- later.
Error_Msg_Scan ("identifier cannot finish with '_'");
end if;
Pos := Pos - 1;
Len := Len - 1;