1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                                S C A N S                                 --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32with Namet;  use Namet;
33with Types;  use Types;
34with Uintp;  use Uintp;
35with Urealp; use Urealp;
36
37package Scans is
38
39--  The scanner maintains a current state in the global variables defined
40--  in this package. The call to the Scan routine advances this state to
41--  the next token. The state is initialized by the call to one of the
42--  initialization routines in Sinput.
43
44   --  The following type is used to identify token types returned by Scan.
45   --  The class column in this table indicates the token classes which
46   --  apply to the token, as defined by subsequent subtype declarations.
47
48   --  Note: Namet.Is_Keyword_Name depends on the fact that the first entry in
49   --  this type declaration is *not* for a reserved word. For details on why
50   --  there is this requirement, see Initialize_Ada_Keywords below.
51
52   type Token_Type is (
53
54      --  Token name          Token type   Class(es)
55
56      Tok_Integer_Literal, -- numeric lit  Literal, Lit_Or_Name
57
58      Tok_Real_Literal,    -- numeric lit  Literal, Lit_Or_Name
59
60      Tok_String_Literal,  -- string lit   Literal. Lit_Or_Name
61
62      Tok_Char_Literal,    -- char lit     Name, Literal. Lit_Or_Name
63
64      Tok_Operator_Symbol, -- op symbol    Name, Literal, Lit_Or_Name, Desig
65
66      Tok_Identifier,      -- identifier   Name, Lit_Or_Name, Desig
67
68      Tok_Double_Asterisk, -- **
69
70      Tok_Ampersand,       -- &            Binary_Addop
71      Tok_Minus,           -- -            Binary_Addop, Unary_Addop
72      Tok_Plus,            -- +            Binary_Addop, Unary_Addop
73
74      Tok_Asterisk,        -- *            Mulop
75      Tok_Mod,             -- MOD          Mulop
76      Tok_Rem,             -- REM          Mulop
77      Tok_Slash,           -- /            Mulop
78
79      Tok_New,             -- NEW
80
81      Tok_Abs,             -- ABS
82      Tok_Others,          -- OTHERS
83      Tok_Null,            -- NULL
84
85      Tok_Dot,             -- .            Namext
86      Tok_Apostrophe,      -- '            Namext
87
88      Tok_Left_Paren,      -- (            Namext, Consk
89
90      Tok_Delta,           -- DELTA        Atkwd, Sterm, Consk
91      Tok_Digits,          -- DIGITS       Atkwd, Sterm, Consk
92      Tok_Range,           -- RANGE        Atkwd, Sterm, Consk
93
94      Tok_Right_Paren,     -- )            Sterm
95      Tok_Comma,           -- ,            Sterm
96
97      Tok_And,             -- AND          Logop, Sterm
98      Tok_Or,              -- OR           Logop, Sterm
99      Tok_Xor,             -- XOR          Logop, Sterm
100
101      Tok_Less,            -- <            Relop, Sterm
102      Tok_Equal,           -- =            Relop, Sterm
103      Tok_Greater,         -- >            Relop, Sterm
104      Tok_Not_Equal,       -- /=           Relop, Sterm
105      Tok_Greater_Equal,   -- >=           Relop, Sterm
106      Tok_Less_Equal,      -- <=           Relop, Sterm
107
108      Tok_In,              -- IN           Relop, Sterm
109      Tok_Not,             -- NOT          Relop, Sterm
110
111      Tok_Box,             -- <>           Relop, Eterm, Sterm
112      Tok_Colon_Equal,     -- :=           Eterm, Sterm
113      Tok_Colon,           -- :            Eterm, Sterm
114      Tok_Greater_Greater, -- >>           Eterm, Sterm
115
116      Tok_Abstract,        -- ABSTRACT     Eterm, Sterm
117      Tok_Access,          -- ACCESS       Eterm, Sterm
118      Tok_Aliased,         -- ALIASED      Eterm, Sterm
119      Tok_All,             -- ALL          Eterm, Sterm
120      Tok_Array,           -- ARRAY        Eterm, Sterm
121      Tok_At,              -- AT           Eterm, Sterm
122      Tok_Body,            -- BODY         Eterm, Sterm
123      Tok_Constant,        -- CONSTANT     Eterm, Sterm
124      Tok_Do,              -- DO           Eterm, Sterm
125      Tok_Is,              -- IS           Eterm, Sterm
126      Tok_Interface,       -- INTERFACE    Eterm, Sterm
127      Tok_Limited,         -- LIMITED      Eterm, Sterm
128      Tok_Of,              -- OF           Eterm, Sterm
129      Tok_Out,             -- OUT          Eterm, Sterm
130      Tok_Record,          -- RECORD       Eterm, Sterm
131      Tok_Renames,         -- RENAMES      Eterm, Sterm
132      Tok_Reverse,         -- REVERSE      Eterm, Sterm
133      Tok_Some,            -- SOME         Eterm, Sterm
134      Tok_Tagged,          -- TAGGED       Eterm, Sterm
135      Tok_Then,            -- THEN         Eterm, Sterm
136
137      Tok_Less_Less,       -- <<           Eterm, Sterm, After_SM
138
139      Tok_Abort,           -- ABORT        Eterm, Sterm, After_SM
140      Tok_Accept,          -- ACCEPT       Eterm, Sterm, After_SM
141      Tok_Case,            -- CASE         Eterm, Sterm, After_SM
142      Tok_Delay,           -- DELAY        Eterm, Sterm, After_SM
143      Tok_Else,            -- ELSE         Eterm, Sterm, After_SM
144      Tok_Elsif,           -- ELSIF        Eterm, Sterm, After_SM
145      Tok_End,             -- END          Eterm, Sterm, After_SM
146      Tok_Exception,       -- EXCEPTION    Eterm, Sterm, After_SM
147      Tok_Exit,            -- EXIT         Eterm, Sterm, After_SM
148      Tok_Goto,            -- GOTO         Eterm, Sterm, After_SM
149      Tok_If,              -- IF           Eterm, Sterm, After_SM
150      Tok_Pragma,          -- PRAGMA       Eterm, Sterm, After_SM
151      Tok_Raise,           -- RAISE        Eterm, Sterm, After_SM
152      Tok_Requeue,         -- REQUEUE      Eterm, Sterm, After_SM
153      Tok_Return,          -- RETURN       Eterm, Sterm, After_SM
154      Tok_Select,          -- SELECT       Eterm, Sterm, After_SM
155      Tok_Terminate,       -- TERMINATE    Eterm, Sterm, After_SM
156      Tok_Until,           -- UNTIL        Eterm, Sterm, After_SM
157      Tok_When,            -- WHEN         Eterm, Sterm, After_SM
158
159      Tok_Begin,           -- BEGIN        Eterm, Sterm, After_SM, Labeled_Stmt
160      Tok_Declare,         -- DECLARE      Eterm, Sterm, After_SM, Labeled_Stmt
161      Tok_For,             -- FOR          Eterm, Sterm, After_SM, Labeled_Stmt
162      Tok_Loop,            -- LOOP         Eterm, Sterm, After_SM, Labeled_Stmt
163      Tok_While,           -- WHILE        Eterm, Sterm, After_SM, Labeled_Stmt
164
165      Tok_Entry,           -- ENTRY        Eterm, Sterm, Declk, Deckn, After_SM
166      Tok_Protected,       -- PROTECTED    Eterm, Sterm, Declk, Deckn, After_SM
167      Tok_Task,            -- TASK         Eterm, Sterm, Declk, Deckn, After_SM
168      Tok_Type,            -- TYPE         Eterm, Sterm, Declk, Deckn, After_SM
169      Tok_Subtype,         -- SUBTYPE      Eterm, Sterm, Declk, Deckn, After_SM
170      Tok_Overriding,      -- OVERRIDING   Eterm, Sterm, Declk, Declk, After_SM
171      Tok_Synchronized,    -- SYNCHRONIZED Eterm, Sterm, Declk, Deckn, After_SM
172      Tok_Use,             -- USE          Eterm, Sterm, Declk, Deckn, After_SM
173
174      Tok_Function,        -- FUNCTION     Eterm, Sterm, Cunit, Declk, After_SM
175      Tok_Generic,         -- GENERIC      Eterm, Sterm, Cunit, Declk, After_SM
176      Tok_Package,         -- PACKAGE      Eterm, Sterm, Cunit, Declk, After_SM
177      Tok_Procedure,       -- PROCEDURE    Eterm, Sterm, Cunit, Declk, After_SM
178
179      Tok_Private,         -- PRIVATE      Eterm, Sterm, Cunit, After_SM
180      Tok_With,            -- WITH         Eterm, Sterm, Cunit, After_SM
181      Tok_Separate,        -- SEPARATE     Eterm, Sterm, Cunit, After_SM
182
183      Tok_EOF,             -- End of file  Eterm, Sterm, Cterm, After_SM
184
185      Tok_Semicolon,       -- ;            Eterm, Sterm, Cterm
186
187      Tok_Arrow,           -- =>           Sterm, Cterm, Chtok
188
189      Tok_Vertical_Bar,    -- |            Cterm, Sterm, Chtok
190
191      Tok_Dot_Dot,         -- ..           Sterm, Chtok
192
193      Tok_Project,
194      Tok_Extends,
195      Tok_External,
196      Tok_External_As_List,
197      --  These four entries represent keywords for the project file language
198      --  and can be returned only in the case of scanning project files.
199
200      Tok_Comment,
201      --  This entry is used when scanning project files (where it represents
202      --  an entire comment), and in preprocessing with the -C switch set
203      --  (where it represents just the "--" of a comment). For the project
204      --  file case, the text of the comment is stored in Comment_Id.
205
206      Tok_End_Of_Line,
207      --  Represents an end of line. Not used during normal compilation scans
208      --  where end of line is ignored. Active for preprocessor scanning and
209      --  also when scanning project files (where it is needed because of ???)
210
211      Tok_Special,
212      --  Used only in preprocessor scanning (to represent one of the
213      --  characters '#', '$', '?', '@', '`', '\', '^', '~', or '_'. The
214      --  character value itself is stored in Scans.Special_Character.
215
216      Tok_SPARK_Hide,
217      --  HIDE directive in SPARK
218
219      No_Token);
220      --  No_Token is used for initializing Token values to indicate that
221      --  no value has been set yet.
222
223   --  Note: in the RM, operator symbol is a special case of string literal.
224   --  We distinguish at the lexical level in this compiler, since there are
225   --  many syntactic situations in which only an operator symbol is allowed.
226
227   --  The following subtype declarations group the token types into classes.
228   --  These are used for class tests in the parser.
229
230   subtype Token_Class_Numeric_Literal is
231     Token_Type range Tok_Integer_Literal .. Tok_Real_Literal;
232   --  Numeric literal
233
234   subtype Token_Class_Literal is
235     Token_Type range Tok_Integer_Literal .. Tok_Operator_Symbol;
236   --  Literal
237
238   subtype Token_Class_Lit_Or_Name is
239     Token_Type range Tok_Integer_Literal .. Tok_Identifier;
240
241   subtype Token_Class_Binary_Addop is
242     Token_Type range Tok_Ampersand .. Tok_Plus;
243   --  Binary adding operator (& + -)
244
245   subtype Token_Class_Unary_Addop is
246     Token_Type range Tok_Minus .. Tok_Plus;
247   --  Unary adding operator (+ -)
248
249   subtype Token_Class_Mulop is
250     Token_Type range Tok_Asterisk .. Tok_Slash;
251   --  Multiplying operator
252
253   subtype Token_Class_Logop is
254     Token_Type range Tok_And .. Tok_Xor;
255   --  Logical operator (and, or, xor)
256
257   subtype Token_Class_Relop is
258     Token_Type range Tok_Less .. Tok_Box;
259   --  Relational operator (= /= < <= > >= not, in plus <> to catch misuse
260   --  of Pascal style not equal operator).
261
262   subtype Token_Class_Name is
263     Token_Type range Tok_Char_Literal .. Tok_Identifier;
264   --  First token of name (4.1),
265   --    (identifier, char literal, operator symbol)
266
267   subtype Token_Class_Desig is
268     Token_Type range Tok_Operator_Symbol .. Tok_Identifier;
269   --  Token which can be a Designator (identifier, operator symbol)
270
271   subtype Token_Class_Namext is
272     Token_Type range Tok_Dot .. Tok_Left_Paren;
273   --  Name extension tokens. These are tokens which can appear immediately
274   --  after a name to extend it recursively (period, quote, left paren)
275
276   subtype Token_Class_Consk is
277     Token_Type range Tok_Left_Paren .. Tok_Range;
278   --  Keywords which can start constraint
279   --    (left paren, delta, digits, range)
280
281   subtype Token_Class_Eterm is
282     Token_Type range Tok_Colon_Equal .. Tok_Semicolon;
283   --  Expression terminators. These tokens can never appear within a simple
284   --  expression. This is used for error recovery purposes (if we encounter
285   --  an error in an expression, we simply scan to the next Eterm token).
286
287   subtype Token_Class_Sterm is
288     Token_Type range Tok_Delta .. Tok_Dot_Dot;
289   --  Simple_Expression terminators. A Simple_Expression must be followed
290   --  by a token in this class, or an error message is issued complaining
291   --  about a missing binary operator.
292
293   subtype Token_Class_Atkwd is
294     Token_Type range Tok_Delta .. Tok_Range;
295   --  Attribute keywords. This class includes keywords which can be used
296   --  as an Attribute_Designator, namely DELTA, DIGITS and RANGE
297
298   subtype Token_Class_Cterm is
299     Token_Type range Tok_EOF .. Tok_Vertical_Bar;
300   --  Choice terminators. These tokens terminate a choice. This is used for
301   --  error recovery purposes (if we encounter an error in a Choice, we
302   --  simply scan to the next Cterm token).
303
304   subtype Token_Class_Chtok is
305     Token_Type range Tok_Arrow .. Tok_Dot_Dot;
306   --  Choice tokens. These tokens signal a choice when used in an Aggregate
307
308   subtype Token_Class_Cunit is
309     Token_Type range Tok_Function .. Tok_Separate;
310   --  Tokens which can begin a compilation unit
311
312   subtype Token_Class_Declk is
313     Token_Type range Tok_Entry .. Tok_Procedure;
314   --  Keywords which start a declaration
315
316   subtype Token_Class_Deckn is
317     Token_Type range Tok_Entry .. Tok_Use;
318   --  Keywords which start a declaration but can't start a compilation unit
319
320   subtype Token_Class_After_SM is
321     Token_Type range Tok_Less_Less .. Tok_EOF;
322   --  Tokens which always, or almost always, appear after a semicolon. Used
323   --  in the Resync_Past_Semicolon routine to avoid gobbling up stuff when
324   --  a semicolon is missing. Of significance only for error recovery.
325
326   subtype Token_Class_Labeled_Stmt is
327     Token_Type range Tok_Begin .. Tok_While;
328   --  Tokens which start labeled statements
329
330   type Token_Flag_Array is array (Token_Type) of Boolean;
331   Is_Reserved_Keyword : constant Token_Flag_Array :=
332                           Token_Flag_Array'
333                             (Tok_Mod      .. Tok_Rem      => True,
334                              Tok_New      .. Tok_Null     => True,
335                              Tok_Delta    .. Tok_Range    => True,
336                              Tok_And      .. Tok_Xor      => True,
337                              Tok_In       .. Tok_Not      => True,
338                              Tok_Abstract .. Tok_Then     => True,
339                              Tok_Abort    .. Tok_Separate => True,
340                              others                       => False);
341   --  Flag array used to test for reserved word
342
343   procedure Initialize_Ada_Keywords;
344   --  Set up Token_Type values in Names table entries for Ada reserved
345   --  words. This ignores Ada_Version; Ada_Version is taken into account in
346   --  Snames.Is_Keyword_Name.
347
348   --------------------------
349   -- Scan State Variables --
350   --------------------------
351
352   --  Note: these variables can only be referenced during the parsing of a
353   --  file. Reference to any of them from Sem or the expander is wrong.
354
355   --  These variables are initialized as required by Scn.Initialize_Scanner,
356   --  and should not be referenced before such a call. However, there are
357   --  situations in which these variables are saved and restored, and this
358   --  may happen before the first Initialize_Scanner call, resulting in the
359   --  assignment of invalid values. To avoid this, and allow building with
360   --  the -gnatVa switch, we initialize some variables to known valid values.
361
362   Scan_Ptr : Source_Ptr := No_Location; -- init for -gnatVa
363   --  Current scan pointer location. After a call to Scan, this points
364   --  just past the end of the token just scanned.
365
366   Token : Token_Type := No_Token; -- init for -gnatVa
367   --  Type of current token
368
369   Token_Ptr : Source_Ptr := No_Location; -- init for -gnatVa
370   --  Pointer to first character of current token
371
372   Current_Line_Start : Source_Ptr := No_Location; -- init for -gnatVa
373   --  Pointer to first character of line containing current token
374
375   Start_Column : Column_Number := No_Column_Number; -- init for -gnatVa
376   --  Starting column number (zero origin) of the first non-blank character
377   --  on the line containing the current token. This is used for error
378   --  recovery circuits which depend on looking at the column line up.
379
380   Type_Token_Location : Source_Ptr := No_Location; -- init for -gnatVa
381   --  Within a type declaration, gives the location of the TYPE keyword that
382   --  opened the type declaration. Used in checking the end column of a record
383   --  declaration, which can line up either with the TYPE keyword, or with the
384   --  start of the line containing the RECORD keyword.
385
386   Checksum : Word := 0; -- init for -gnatVa
387   --  Used to accumulate a CRC representing the tokens in the source
388   --  file being compiled. This CRC includes only program tokens, and
389   --  excludes comments.
390
391   First_Non_Blank_Location : Source_Ptr := No_Location; -- init for -gnatVa
392   --  Location of first non-blank character on the line containing the
393   --  current token (i.e. the location of the character whose column number
394   --  is stored in Start_Column).
395
396   Token_Node : Node_Id := Empty;
397   --  Node table Id for the current token. This is set only if the current
398   --  token is one for which the scanner constructs a node (i.e. it is an
399   --  identifier, operator symbol, or literal. For other token types,
400   --  Token_Node is undefined.
401
402   Token_Name : Name_Id := No_Name;
403   --  For identifiers, this is set to the Name_Id of the identifier scanned.
404   --  For all other tokens, Token_Name is set to Error_Name. Note that it
405   --  would be possible for the caller to extract this information from
406   --  Token_Node. We set Token_Name separately for two reasons. First it
407   --  allows a quicker test for a specific identifier. Second, it allows
408   --  a version of the parser to be built that does not build tree nodes,
409   --  usable as a syntax checker.
410
411   Prev_Token : Token_Type := No_Token;
412   --  Type of previous token
413
414   Prev_Token_Ptr : Source_Ptr;
415   --  Pointer to first character of previous token
416
417   Version_To_Be_Found : Boolean;
418   --  This flag is True if the scanner is still looking for an RCS version
419   --  number in a comment. Normally it is initialized to False so that this
420   --  circuit is not activated. If the -dv switch is set, then this flag is
421   --  initialized to True, and then reset when the version number is found.
422   --  We do things this way to minimize the impact on comment scanning.
423
424   Character_Code : Char_Code;
425   --  Valid only when Token is Tok_Char_Literal. Contains the value of the
426   --  scanned literal.
427
428   Real_Literal_Value : Ureal;
429   --  Valid only when Token is Tok_Real_Literal, contains the value of the
430   --  scanned literal.
431
432   Int_Literal_Value : Uint;
433   --  Valid only when Token = Tok_Integer_Literal, contains the value of the
434   --  scanned literal.
435
436   Based_Literal_Uses_Colon : Boolean;
437   --  Valid only when Token = Tok_Integer_Literal or Tok_Real_Literal. Set
438   --  True only for the case of a based literal using ':' instead of '#'.
439
440   String_Literal_Id : String_Id;
441   --  Valid only when Token = Tok_String_Literal or Tok_Operator_Symbol.
442   --  Contains the Id for currently scanned string value.
443
444   Wide_Character_Found : Boolean := False;
445   --  Valid only when Token = Tok_String_Literal. Set True if wide character
446   --  found (i.e. a character that does not fit in Character, but fits in
447   --  Wide_Wide_Character).
448
449   Wide_Wide_Character_Found : Boolean := False;
450   --  Valid only when Token = Tok_String_Literal. Set True if wide wide
451   --  character found (i.e. a character that does not fit in Character or
452   --  Wide_Character).
453
454   Special_Character : Character;
455   --  Valid only when Token = Tok_Special. Returns one of the characters
456   --  '#', '$', '?', '@', '`', '\', '^', '~', or '_'.
457   --
458   --  Why only this set? What about wide characters???
459
460   Comment_Id : Name_Id := No_Name;
461   --  Valid only when Token = Tok_Comment. Store the string that follows
462   --  the "--" of a comment when scanning project files.
463   --
464   --  Is it really right for this to be a Name rather than a String, what
465   --  about the case of Wide_Wide_Characters???
466
467   Inside_If_Expression : Nat := 0;
468   --  This is a counter that is set non-zero while scanning out an if
469   --  expression (incremented on entry, decremented on exit). It is used to
470   --  disconnect format checks that normally apply to keywords THEN, ELSE etc.
471
472   --------------------------------------------------------
473   -- Procedures for Saving and Restoring the Scan State --
474   --------------------------------------------------------
475
476   --  The following procedures can be used to save and restore the entire
477   --  scan state. They are used in cases where it is necessary to backup
478   --  the scan during the parse.
479
480   type Saved_Scan_State is private;
481   --  Used for saving and restoring the scan state
482
483   procedure Save_Scan_State (Saved_State : out Saved_Scan_State);
484   pragma Inline (Save_Scan_State);
485   --  Saves the current scan state for possible later restoration. Note that
486   --  there is no harm in saving the state and then never restoring it.
487
488   procedure Restore_Scan_State (Saved_State : Saved_Scan_State);
489   pragma Inline (Restore_Scan_State);
490   --  Restores a scan state saved by a call to Save_Scan_State.
491   --  The saved scan state must refer to the current source file.
492
493private
494   type Saved_Scan_State is record
495      Save_Scan_Ptr                 : Source_Ptr;
496      Save_Token                    : Token_Type;
497      Save_Token_Ptr                : Source_Ptr;
498      Save_Current_Line_Start       : Source_Ptr;
499      Save_Start_Column             : Column_Number;
500      Save_Checksum                 : Word;
501      Save_First_Non_Blank_Location : Source_Ptr;
502      Save_Token_Node               : Node_Id;
503      Save_Token_Name               : Name_Id;
504      Save_Prev_Token               : Token_Type;
505      Save_Prev_Token_Ptr           : Source_Ptr;
506   end record;
507
508end Scans;
509