2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / scans.ads
blob7ff80e9c5cd2acb3a739d38670c36b01c0399a55
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C A N S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2001 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
34 with Types; use Types;
36 package Scans is
38 -- The scanner maintains a current state in the global variables defined
39 -- in this package. The call to the Scan routine advances this state to
40 -- the next token. The state is initialized by the call to one of the
41 -- initialization routines in Sinput.
43 -- The following type is used to identify token types returned by Scan.
44 -- The class column in this table indicates the token classes which
45 -- apply to the token, as defined by subsquent subtype declarations.
47 -- Note: the coding in SCN depends on the fact that the first entry in
48 -- this type declaration is *not* for a reserved word. For details on
49 -- why there is this requirement, see Scn.Initialize_Scanner.
51 type Token_Type is (
53 -- Token name Token type Class(es)
55 Tok_Integer_Literal, -- numeric lit Literal, Lit_Or_Name
57 Tok_Real_Literal, -- numeric lit Literal, Lit_Or_Name
59 Tok_String_Literal, -- string lit Literal. Lit_Or_Name
61 Tok_Char_Literal, -- char lit Name, Literal. Lit_Or_Name
63 Tok_Operator_Symbol, -- op symbol Name, Literal, Lit_Or_Name, Desig
65 Tok_Identifier, -- identifier Name, Lit_Or_Name, Desig
67 Tok_Double_Asterisk, -- **
69 Tok_Ampersand, -- & Binary_Addop
70 Tok_Minus, -- - Binary_Addop, Unary_Addop
71 Tok_Plus, -- + Binary_Addop, Unary_Addop
73 Tok_Asterisk, -- * Mulop
74 Tok_Mod, -- MOD Mulop
75 Tok_Rem, -- REM Mulop
76 Tok_Slash, -- / Mulop
78 Tok_New, -- NEW
80 Tok_Abs, -- ABS
81 Tok_Others, -- OTHERS
82 Tok_Null, -- NULL
84 Tok_Dot, -- . Namext
85 Tok_Apostrophe, -- ' Namext
87 Tok_Left_Paren, -- ( Namext, Consk
89 Tok_Delta, -- DELTA Atkwd, Sterm, Consk
90 Tok_Digits, -- DIGITS Atkwd, Sterm, Consk
91 Tok_Range, -- RANGE Atkwd, Sterm, Consk
93 Tok_Right_Paren, -- ) Sterm
94 Tok_Comma, -- , Sterm
96 Tok_And, -- AND Logop, Sterm
97 Tok_Or, -- OR Logop, Sterm
98 Tok_Xor, -- XOR Logop, Sterm
100 Tok_Less, -- < Relop, Sterm
101 Tok_Equal, -- = Relop, Sterm
102 Tok_Greater, -- > Relop, Sterm
103 Tok_Not_Equal, -- /= Relop, Sterm
104 Tok_Greater_Equal, -- >= Relop, Sterm
105 Tok_Less_Equal, -- <= Relop, Sterm
107 Tok_In, -- IN Relop, Sterm
108 Tok_Not, -- NOT Relop, Sterm
110 Tok_Box, -- <> Relop, Eterm, Sterm
111 Tok_Colon_Equal, -- := Eterm, Sterm
112 Tok_Colon, -- : Eterm, Sterm
113 Tok_Greater_Greater, -- >> Eterm, Sterm
115 Tok_Abstract, -- ABSTRACT Eterm, Sterm
116 Tok_Access, -- ACCESS Eterm, Sterm
117 Tok_Aliased, -- ALIASED Eterm, Sterm
118 Tok_All, -- ALL Eterm, Sterm
119 Tok_Array, -- ARRAY Eterm, Sterm
120 Tok_At, -- AT Eterm, Sterm
121 Tok_Body, -- BODY Eterm, Sterm
122 Tok_Constant, -- CONSTANT Eterm, Sterm
123 Tok_Do, -- DO Eterm, Sterm
124 Tok_Is, -- IS Eterm, Sterm
125 Tok_Limited, -- LIMITED Eterm, Sterm
126 Tok_Of, -- OF Eterm, Sterm
127 Tok_Out, -- OUT Eterm, Sterm
128 Tok_Record, -- RECORD Eterm, Sterm
129 Tok_Renames, -- RENAMES Eterm, Sterm
130 Tok_Reverse, -- REVERSE Eterm, Sterm
131 Tok_Tagged, -- TAGGED Eterm, Sterm
132 Tok_Then, -- THEN Eterm, Sterm
134 Tok_Less_Less, -- << Eterm, Sterm, After_SM
136 Tok_Abort, -- ABORT Eterm, Sterm, After_SM
137 Tok_Accept, -- ACCEPT Eterm, Sterm, After_SM
138 Tok_Case, -- CASE Eterm, Sterm, After_SM
139 Tok_Delay, -- DELAY Eterm, Sterm, After_SM
140 Tok_Else, -- ELSE Eterm, Sterm, After_SM
141 Tok_Elsif, -- ELSIF Eterm, Sterm, After_SM
142 Tok_End, -- END Eterm, Sterm, After_SM
143 Tok_Exception, -- EXCEPTION Eterm, Sterm, After_SM
144 Tok_Exit, -- EXIT Eterm, Sterm, After_SM
145 Tok_Goto, -- GOTO Eterm, Sterm, After_SM
146 Tok_If, -- IF Eterm, Sterm, After_SM
147 Tok_Pragma, -- PRAGMA Eterm, Sterm, After_SM
148 Tok_Raise, -- RAISE Eterm, Sterm, After_SM
149 Tok_Requeue, -- REQUEUE Eterm, Sterm, After_SM
150 Tok_Return, -- RETURN Eterm, Sterm, After_SM
151 Tok_Select, -- SELECT Eterm, Sterm, After_SM
152 Tok_Terminate, -- TERMINATE Eterm, Sterm, After_SM
153 Tok_Until, -- UNTIL Eterm, Sterm, After_SM
154 Tok_When, -- WHEN Eterm, Sterm, After_SM
156 Tok_Begin, -- BEGIN Eterm, Sterm, After_SM, Labeled_Stmt
157 Tok_Declare, -- DECLARE Eterm, Sterm, After_SM, Labeled_Stmt
158 Tok_For, -- FOR Eterm, Sterm, After_SM, Labeled_Stmt
159 Tok_Loop, -- LOOP Eterm, Sterm, After_SM, Labeled_Stmt
160 Tok_While, -- WHILE Eterm, Sterm, After_SM, Labeled_Stmt
162 Tok_Entry, -- ENTRY Eterm, Sterm, Declk, Deckn, After_SM
163 Tok_Protected, -- PROTECTED Eterm, Sterm, Declk, Deckn, After_SM
164 Tok_Task, -- TASK Eterm, Sterm, Declk, Deckn, After_SM
165 Tok_Type, -- TYPE Eterm, Sterm, Declk, Deckn, After_SM
166 Tok_Subtype, -- SUBTYPE Eterm, Sterm, Declk, Deckn, After_SM
167 Tok_Use, -- USE Eterm, Sterm, Declk, Deckn, After_SM
169 Tok_Function, -- FUNCTION Eterm, Sterm, Cunit, Declk, After_SM
170 Tok_Generic, -- GENERIC Eterm, Sterm, Cunit, Declk, After_SM
171 Tok_Package, -- PACKAGE Eterm, Sterm, Cunit, Declk, After_SM
172 Tok_Procedure, -- PROCEDURE Eterm, Sterm, Cunit, Declk, After_SM
174 Tok_Private, -- PRIVATE Eterm, Sterm, Cunit, After_SM
175 Tok_With, -- WITH Eterm, Sterm, Cunit, After_SM
176 Tok_Separate, -- SEPARATE Eterm, Sterm, Cunit, After_SM
178 Tok_EOF, -- End of file Eterm, Sterm, Cterm, After_SM
180 Tok_Semicolon, -- ; Eterm, Sterm, Cterm
182 Tok_Arrow, -- => Sterm, Cterm, Chtok
184 Tok_Vertical_Bar, -- | Cterm, Sterm, Chtok
186 Tok_Dot_Dot, -- .. Sterm, Chtok
188 -- The following three entries are used only when scanning
189 -- project files.
191 Tok_Project,
192 Tok_Extends,
193 Tok_External,
195 No_Token);
196 -- No_Token is used for initializing Token values to indicate that
197 -- no value has been set yet.
199 -- Note: in the RM, operator symbol is a special case of string literal.
200 -- We distinguish at the lexical level in this compiler, since there are
201 -- many syntactic situations in which only an operator symbol is allowed.
203 -- The following subtype declarations group the token types into classes.
204 -- These are used for class tests in the parser.
206 subtype Token_Class_Numeric_Literal is
207 Token_Type range Tok_Integer_Literal .. Tok_Real_Literal;
208 -- Numeric literal
210 subtype Token_Class_Literal is
211 Token_Type range Tok_Integer_Literal .. Tok_Operator_Symbol;
212 -- Literal
214 subtype Token_Class_Lit_Or_Name is
215 Token_Type range Tok_Integer_Literal .. Tok_Identifier;
217 subtype Token_Class_Binary_Addop is
218 Token_Type range Tok_Ampersand .. Tok_Plus;
219 -- Binary adding operator (& + -)
221 subtype Token_Class_Unary_Addop is
222 Token_Type range Tok_Minus .. Tok_Plus;
223 -- Unary adding operator (+ -)
225 subtype Token_Class_Mulop is
226 Token_Type range Tok_Asterisk .. Tok_Slash;
227 -- Multiplying operator
229 subtype Token_Class_Logop is
230 Token_Type range Tok_And .. Tok_Xor;
231 -- Logical operator (and, or, xor)
233 subtype Token_Class_Relop is
234 Token_Type range Tok_Less .. Tok_Box;
235 -- Relational operator (= /= < <= > >= not, in plus <> to catch misuse
236 -- of Pascal style not equal operator).
238 subtype Token_Class_Name is
239 Token_Type range Tok_Char_Literal .. Tok_Identifier;
240 -- First token of name (4.1),
241 -- (identifier, char literal, operator symbol)
243 subtype Token_Class_Desig is
244 Token_Type range Tok_Operator_Symbol .. Tok_Identifier;
245 -- Token which can be a Designator (identifier, operator symbol)
247 subtype Token_Class_Namext is
248 Token_Type range Tok_Dot .. Tok_Left_Paren;
249 -- Name extension tokens. These are tokens which can appear immediately
250 -- after a name to extend it recursively (period, quote, left paren)
252 subtype Token_Class_Consk is
253 Token_Type range Tok_Left_Paren .. Tok_Range;
254 -- Keywords which can start constraint
255 -- (left paren, delta, digits, range)
257 subtype Token_Class_Eterm is
258 Token_Type range Tok_Colon_Equal .. Tok_Semicolon;
259 -- Expression terminators. These tokens can never appear within a simple
260 -- expression. This is used for error recovery purposes (if we encounter
261 -- an error in an expression, we simply scan to the next Eterm token).
263 subtype Token_Class_Sterm is
264 Token_Type range Tok_Delta .. Tok_Dot_Dot;
265 -- Simple_Expression terminators. A Simple_Expression must be followed
266 -- by a token in this class, or an error message is issued complaining
267 -- about a missing binary operator.
269 subtype Token_Class_Atkwd is
270 Token_Type range Tok_Delta .. Tok_Range;
271 -- Attribute keywords. This class includes keywords which can be used
272 -- as an Attribute_Designator, namely DELTA, DIGITS and RANGE
274 subtype Token_Class_Cterm is
275 Token_Type range Tok_EOF .. Tok_Vertical_Bar;
276 -- Choice terminators. These tokens terminate a choice. This is used for
277 -- error recovery purposes (if we encounter an error in a Choice, we
278 -- simply scan to the next Cterm token).
280 subtype Token_Class_Chtok is
281 Token_Type range Tok_Arrow .. Tok_Dot_Dot;
282 -- Choice tokens. These tokens signal a choice when used in an Aggregate
284 subtype Token_Class_Cunit is
285 Token_Type range Tok_Function .. Tok_Separate;
286 -- Tokens which can begin a compilation unit
288 subtype Token_Class_Declk is
289 Token_Type range Tok_Entry .. Tok_Procedure;
290 -- Keywords which start a declaration
292 subtype Token_Class_Deckn is
293 Token_Type range Tok_Entry .. Tok_Use;
294 -- Keywords which start a declaration but can't start a compilation unit
296 subtype Token_Class_After_SM is
297 Token_Type range Tok_Less_Less .. Tok_EOF;
298 -- Tokens which always, or almost always, appear after a semicolon. Used
299 -- in the Resync_Past_Semicolon routine to avoid gobbling up stuff when
300 -- a semicolon is missing. Of significance only for error recovery.
302 subtype Token_Class_Labeled_Stmt is
303 Token_Type range Tok_Begin .. Tok_While;
304 -- Tokens which start labeled statements
306 type Token_Flag_Array is array (Token_Type) of Boolean;
307 Is_Reserved_Keyword : constant Token_Flag_Array := Token_Flag_Array'(
308 Tok_Mod .. Tok_Rem => True,
309 Tok_New .. Tok_Null => True,
310 Tok_Delta .. Tok_Range => True,
311 Tok_And .. Tok_Xor => True,
312 Tok_In .. Tok_Not => True,
313 Tok_Abstract .. Tok_Then => True,
314 Tok_Abort .. Tok_Separate => True,
315 others => False);
316 -- Flag array used to test for reserved word
318 --------------------------
319 -- Scan State Variables --
320 --------------------------
322 -- Note: these variables can only be referenced during the parsing of a
323 -- file. Reference to any of them from Sem or the expander is wrong.
325 Scan_Ptr : Source_Ptr;
326 -- Current scan pointer location. After a call to Scan, this points
327 -- just past the end of the token just scanned.
329 Token : Token_Type;
330 -- Type of current token
332 Token_Ptr : Source_Ptr;
333 -- Pointer to first character of current token
335 Current_Line_Start : Source_Ptr;
336 -- Pointer to first character of line containing current token
338 Start_Column : Column_Number;
339 -- Starting column number (zero origin) of the first non-blank character
340 -- on the line containing the current token. This is used for error
341 -- recovery circuits which depend on looking at the column line up.
343 Checksum : Word;
344 -- Used to accumulate a CRC representing the tokens in the source
345 -- file being compiled. This CRC includes only program tokens, and
346 -- excludes comments.
348 First_Non_Blank_Location : Source_Ptr;
349 -- Location of first non-blank character on the line containing the
350 -- current token (i.e. the location of the character whose column number
351 -- is stored in Start_Column).
353 Token_Node : Node_Id := Empty;
354 -- Node table Id for the current token. This is set only if the current
355 -- token is one for which the scanner constructs a node (i.e. it is an
356 -- identifier, operator symbol, or literal. For other token types,
357 -- Token_Node is undefined.
359 Token_Name : Name_Id := No_Name;
360 -- For identifiers, this is set to the Name_Id of the identifier scanned.
361 -- For all other tokens, Token_Name is set to Error_Name. Note that it
362 -- would be possible for the caller to extract this information from
363 -- Token_Node. We set Token_Name separately for two reasons. First it
364 -- allows a quicker test for a specific identifier. Second, it allows
365 -- a version of the parser to be built that does not build tree nodes,
366 -- usable as a syntax checker.
368 Prev_Token : Token_Type := No_Token;
369 -- Type of previous token
371 Prev_Token_Ptr : Source_Ptr;
372 -- Pointer to first character of previous token
374 Version_To_Be_Found : Boolean;
375 -- This flag is True if the scanner is still looking for an RCS version
376 -- number in a comment. Normally it is initialized to False so that this
377 -- circuit is not activated. If the -dv switch is set, then this flag is
378 -- initialized to True, and then reset when the version number is found.
379 -- We do things this way to minimize the impact on comment scanning.
381 --------------------------------------------------------
382 -- Procedures for Saving and Restoring the Scan State --
383 --------------------------------------------------------
385 -- The following procedures can be used to save and restore the entire
386 -- scan state. They are used in cases where it is necessary to backup
387 -- the scan during the parse.
389 type Saved_Scan_State is private;
390 -- Used for saving and restoring the scan state
392 procedure Save_Scan_State (Saved_State : out Saved_Scan_State);
393 pragma Inline (Save_Scan_State);
394 -- Saves the current scan state for possible later restoration. Note that
395 -- there is no harm in saving the state and then never restoring it.
397 procedure Restore_Scan_State (Saved_State : in Saved_Scan_State);
398 pragma Inline (Restore_Scan_State);
399 -- Restores a scan state saved by a call to Save_Scan_State.
400 -- The saved scan state must refer to the current source file.
402 private
403 type Saved_Scan_State is record
404 Save_Scan_Ptr : Source_Ptr;
405 Save_Token : Token_Type;
406 Save_Token_Ptr : Source_Ptr;
407 Save_Current_Line_Start : Source_Ptr;
408 Save_Start_Column : Column_Number;
409 Save_Checksum : Word;
410 Save_First_Non_Blank_Location : Source_Ptr;
411 Save_Token_Node : Node_Id;
412 Save_Token_Name : Name_Id;
413 Save_Prev_Token : Token_Type;
414 Save_Prev_Token_Ptr : Source_Ptr;
415 end record;
417 end Scans;