| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- P A R . S Y N C -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, 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. 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 COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
separate (Par) |
| 27 |
package body Sync is |
| 28 |
|
| 29 |
procedure Resync_Init; |
| 30 |
-- This routine is called on initiating a resynchronization action |
| 31 |
|
| 32 |
procedure Resync_Resume; |
| 33 |
-- This routine is called on completing a resynchronization action |
| 34 |
|
| 35 |
------------------- |
| 36 |
-- Resync_Choice -- |
| 37 |
------------------- |
| 38 |
|
| 39 |
procedure Resync_Choice is |
| 40 |
begin |
| 41 |
Resync_Init; |
| 42 |
|
| 43 |
-- Loop till we get a token that terminates a choice. Note that EOF is |
| 44 |
-- one such token, so we are sure to get out of this loop eventually. |
| 45 |
|
| 46 |
while Token not in Token_Class_Cterm loop |
| 47 |
Scan; |
| 48 |
end loop; |
| 49 |
|
| 50 |
Resync_Resume; |
| 51 |
end Resync_Choice; |
| 52 |
|
| 53 |
------------------ |
| 54 |
-- Resync_Cunit -- |
| 55 |
------------------ |
| 56 |
|
| 57 |
procedure Resync_Cunit is |
| 58 |
begin |
| 59 |
Resync_Init; |
| 60 |
|
| 61 |
while Token not in Token_Class_Cunit | Tok_EOF loop |
| 62 |
Scan; |
| 63 |
end loop; |
| 64 |
|
| 65 |
Resync_Resume; |
| 66 |
end Resync_Cunit; |
| 67 |
|
| 68 |
----------------------- |
| 69 |
-- Resync_Expression -- |
| 70 |
----------------------- |
| 71 |
|
| 72 |
procedure Resync_Expression is |
| 73 |
Paren_Count : Int; |
| 74 |
|
| 75 |
begin |
| 76 |
Resync_Init; |
| 77 |
Paren_Count := 0; |
| 78 |
|
| 79 |
loop |
| 80 |
-- Terminating tokens are those in class Eterm and also RANGE, |
| 81 |
-- DIGITS or DELTA if not preceded by an apostrophe (if they are |
| 82 |
-- preceded by an apostrophe, then they are attributes). In addition, |
| 83 |
-- at the outer parentheses level only, we also consider a comma, |
| 84 |
-- right parenthesis or vertical bar to terminate an expression. |
| 85 |
|
| 86 |
if Token in Token_Class_Eterm |
| 87 |
|
| 88 |
or else (Token in Token_Class_Atkwd |
| 89 |
and then Prev_Token /= Tok_Apostrophe) |
| 90 |
|
| 91 |
or else (Paren_Count = 0 |
| 92 |
and then |
| 93 |
Token in Tok_Comma | Tok_Right_Paren | Tok_Vertical_Bar) |
| 94 |
then |
| 95 |
-- A special check: if we stop on the ELSE of OR ELSE or the |
| 96 |
-- THEN of AND THEN, keep going, because this is not really an |
| 97 |
-- expression terminator after all. Also, keep going past WITH |
| 98 |
-- since this can be part of an extension aggregate |
| 99 |
|
| 100 |
if (Token = Tok_Else and then Prev_Token = Tok_Or) |
| 101 |
or else (Token = Tok_Then and then Prev_Token = Tok_And) |
| 102 |
or else Token = Tok_With |
| 103 |
then |
| 104 |
null; |
| 105 |
else |
| 106 |
exit; |
| 107 |
end if; |
| 108 |
end if; |
| 109 |
|
| 110 |
if Token = Tok_Left_Paren then |
| 111 |
Paren_Count := Paren_Count + 1; |
| 112 |
|
| 113 |
elsif Token = Tok_Right_Paren then |
| 114 |
Paren_Count := Paren_Count - 1; |
| 115 |
|
| 116 |
end if; |
| 117 |
|
| 118 |
Scan; -- past token to be skipped |
| 119 |
end loop; |
| 120 |
|
| 121 |
Resync_Resume; |
| 122 |
end Resync_Expression; |
| 123 |
|
| 124 |
----------------- |
| 125 |
-- Resync_Init -- |
| 126 |
----------------- |
| 127 |
|
| 128 |
procedure Resync_Init is |
| 129 |
begin |
| 130 |
-- The following check makes sure we do not get stuck in an infinite |
| 131 |
-- loop resynchronizing and getting nowhere. If we are called to do a |
| 132 |
-- resynchronize and we are exactly at the same point that we left off |
| 133 |
-- on the last resynchronize call, then we force at least one token to |
| 134 |
-- be skipped so that we make progress. |
| 135 |
|
| 136 |
if Token_Ptr = Last_Resync_Point then |
| 137 |
Scan; -- to skip at least one token |
| 138 |
end if; |
| 139 |
|
| 140 |
-- Output extra error message if debug R flag is set |
| 141 |
|
| 142 |
if Debug_Flag_R then |
| 143 |
Error_Msg_SC ("resynchronizing!"); |
| 144 |
end if; |
| 145 |
end Resync_Init; |
| 146 |
|
| 147 |
---------------------------------- |
| 148 |
-- Resync_Past_Malformed_Aspect -- |
| 149 |
---------------------------------- |
| 150 |
|
| 151 |
procedure Resync_Past_Malformed_Aspect is |
| 152 |
begin |
| 153 |
Resync_Init; |
| 154 |
|
| 155 |
loop |
| 156 |
-- A comma may separate two aspect specifications, but it may also |
| 157 |
-- delimit multiple arguments of a single aspect. |
| 158 |
|
| 159 |
if Token = Tok_Comma then |
| 160 |
declare |
| 161 |
Scan_State : Saved_Scan_State; |
| 162 |
|
| 163 |
begin |
| 164 |
Save_Scan_State (Scan_State); |
| 165 |
Scan; -- past comma |
| 166 |
|
| 167 |
-- The identifier following the comma is a valid aspect, the |
| 168 |
-- current malformed aspect has been successfully skipped. |
| 169 |
|
| 170 |
if Token = Tok_Identifier |
| 171 |
and then Is_Aspect_Id (Token_Name) |
| 172 |
then |
| 173 |
Restore_Scan_State (Scan_State); |
| 174 |
exit; |
| 175 |
|
| 176 |
-- The comma is delimiting multiple arguments of an aspect |
| 177 |
|
| 178 |
else |
| 179 |
Restore_Scan_State (Scan_State); |
| 180 |
end if; |
| 181 |
end; |
| 182 |
|
| 183 |
-- An IS signals the last aspect specification when the related |
| 184 |
-- context is a body. |
| 185 |
|
| 186 |
elsif Token = Tok_Is then |
| 187 |
exit; |
| 188 |
|
| 189 |
-- A semicolon signals the last aspect specification |
| 190 |
|
| 191 |
elsif Token = Tok_Semicolon then |
| 192 |
exit; |
| 193 |
|
| 194 |
-- In the case of a mistyped semicolon, any token which follows a |
| 195 |
-- semicolon signals the last aspect specification. |
| 196 |
|
| 197 |
elsif Token in Token_Class_After_SM then |
| 198 |
exit; |
| 199 |
end if; |
| 200 |
|
| 201 |
-- Keep on resyncing |
| 202 |
|
| 203 |
Scan; |
| 204 |
end loop; |
| 205 |
|
| 206 |
-- Fall out of loop with resynchronization complete |
| 207 |
|
| 208 |
Resync_Resume; |
| 209 |
end Resync_Past_Malformed_Aspect; |
| 210 |
|
| 211 |
--------------------------- |
| 212 |
-- Resync_Past_Semicolon -- |
| 213 |
--------------------------- |
| 214 |
|
| 215 |
procedure Resync_Past_Semicolon is |
| 216 |
begin |
| 217 |
Resync_Init; |
| 218 |
|
| 219 |
loop |
| 220 |
-- Done if we are at a semicolon |
| 221 |
|
| 222 |
if Token = Tok_Semicolon then |
| 223 |
Scan; -- past semicolon |
| 224 |
exit; |
| 225 |
|
| 226 |
-- Done if we are at a token which normally appears only after |
| 227 |
-- a semicolon. One special glitch is that the keyword private is |
| 228 |
-- in this category only if it does NOT appear after WITH. |
| 229 |
|
| 230 |
elsif Token in Token_Class_After_SM |
| 231 |
and then (Token /= Tok_Private or else Prev_Token /= Tok_With) |
| 232 |
then |
| 233 |
exit; |
| 234 |
|
| 235 |
-- Otherwise keep going |
| 236 |
|
| 237 |
else |
| 238 |
Scan; |
| 239 |
end if; |
| 240 |
end loop; |
| 241 |
|
| 242 |
-- Fall out of loop with resynchronization complete |
| 243 |
|
| 244 |
Resync_Resume; |
| 245 |
end Resync_Past_Semicolon; |
| 246 |
|
| 247 |
---------------------------------------------- |
| 248 |
-- Resync_Past_Semicolon_Or_To_Loop_Or_Then -- |
| 249 |
---------------------------------------------- |
| 250 |
|
| 251 |
procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then is |
| 252 |
begin |
| 253 |
Resync_Init; |
| 254 |
|
| 255 |
loop |
| 256 |
-- Done if at semicolon |
| 257 |
|
| 258 |
if Token = Tok_Semicolon then |
| 259 |
Scan; -- past the semicolon |
| 260 |
exit; |
| 261 |
|
| 262 |
-- Done if we are at a token which normally appears only after |
| 263 |
-- a semicolon. One special glitch is that the keyword private is |
| 264 |
-- in this category only if it does NOT appear after WITH. |
| 265 |
|
| 266 |
elsif Token in Token_Class_After_SM |
| 267 |
and then (Token /= Tok_Private or else Prev_Token /= Tok_With) |
| 268 |
then |
| 269 |
exit; |
| 270 |
|
| 271 |
-- Done if we are at THEN or LOOP |
| 272 |
|
| 273 |
elsif Token in Tok_Then | Tok_Loop then |
| 274 |
exit; |
| 275 |
|
| 276 |
-- Otherwise keep going |
| 277 |
|
| 278 |
else |
| 279 |
Scan; |
| 280 |
end if; |
| 281 |
end loop; |
| 282 |
|
| 283 |
-- Fall out of loop with resynchronization complete |
| 284 |
|
| 285 |
Resync_Resume; |
| 286 |
end Resync_Past_Semicolon_Or_To_Loop_Or_Then; |
| 287 |
|
| 288 |
------------------- |
| 289 |
-- Resync_Resume -- |
| 290 |
------------------- |
| 291 |
|
| 292 |
procedure Resync_Resume is |
| 293 |
begin |
| 294 |
-- Save resync point (see special test in Resync_Init) |
| 295 |
|
| 296 |
Last_Resync_Point := Token_Ptr; |
| 297 |
|
| 298 |
if Debug_Flag_R then |
| 299 |
Error_Msg_SC ("resuming here!"); |
| 300 |
end if; |
| 301 |
end Resync_Resume; |
| 302 |
|
| 303 |
--------------------------- |
| 304 |
-- Resync_Semicolon_List -- |
| 305 |
--------------------------- |
| 306 |
|
| 307 |
procedure Resync_Semicolon_List is |
| 308 |
Paren_Count : Int; |
| 309 |
|
| 310 |
begin |
| 311 |
Resync_Init; |
| 312 |
Paren_Count := 0; |
| 313 |
|
| 314 |
loop |
| 315 |
if Token in Tok_EOF | Tok_Semicolon | Tok_Is | Token_Class_After_SM |
| 316 |
then |
| 317 |
exit; |
| 318 |
|
| 319 |
elsif Token = Tok_Left_Paren then |
| 320 |
Paren_Count := Paren_Count + 1; |
| 321 |
|
| 322 |
elsif Token = Tok_Right_Paren then |
| 323 |
if Paren_Count = 0 then |
| 324 |
exit; |
| 325 |
else |
| 326 |
Paren_Count := Paren_Count - 1; |
| 327 |
end if; |
| 328 |
end if; |
| 329 |
|
| 330 |
Scan; |
| 331 |
end loop; |
| 332 |
|
| 333 |
Resync_Resume; |
| 334 |
end Resync_Semicolon_List; |
| 335 |
|
| 336 |
------------------------- |
| 337 |
-- Resync_To_Semicolon -- |
| 338 |
------------------------- |
| 339 |
|
| 340 |
procedure Resync_To_Semicolon is |
| 341 |
begin |
| 342 |
Resync_Init; |
| 343 |
|
| 344 |
loop |
| 345 |
-- Done if we are at a semicolon |
| 346 |
|
| 347 |
if Token = Tok_Semicolon then |
| 348 |
exit; |
| 349 |
|
| 350 |
-- Done if we are at a token which normally appears only after |
| 351 |
-- a semicolon. One special glitch is that the keyword private is |
| 352 |
-- in this category only if it does NOT appear after WITH. |
| 353 |
|
| 354 |
elsif Token in Token_Class_After_SM |
| 355 |
and then (Token /= Tok_Private or else Prev_Token /= Tok_With) |
| 356 |
then |
| 357 |
exit; |
| 358 |
|
| 359 |
-- Otherwise keep going |
| 360 |
|
| 361 |
else |
| 362 |
Scan; |
| 363 |
end if; |
| 364 |
end loop; |
| 365 |
|
| 366 |
-- Fall out of loop with resynchronization complete |
| 367 |
|
| 368 |
Resync_Resume; |
| 369 |
end Resync_To_Semicolon; |
| 370 |
|
| 371 |
-------------------- |
| 372 |
-- Resync_To_When -- |
| 373 |
-------------------- |
| 374 |
|
| 375 |
procedure Resync_To_When is |
| 376 |
begin |
| 377 |
Resync_Init; |
| 378 |
|
| 379 |
loop |
| 380 |
-- Done if at semicolon, WHEN or IS |
| 381 |
|
| 382 |
if Token in Tok_Semicolon | Tok_When | Tok_Is then |
| 383 |
exit; |
| 384 |
|
| 385 |
-- Otherwise keep going |
| 386 |
|
| 387 |
else |
| 388 |
Scan; |
| 389 |
end if; |
| 390 |
end loop; |
| 391 |
|
| 392 |
-- Fall out of loop with resynchronization complete |
| 393 |
|
| 394 |
Resync_Resume; |
| 395 |
end Resync_To_When; |
| 396 |
|
| 397 |
end Sync; |