OSDN Git Service

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