1 //===-- lib/Parser/executable-parsers.cpp ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // Per-type parsers for executable statements
10 
11 #include "basic-parsers.h"
12 #include "debug-parser.h"
13 #include "expr-parsers.h"
14 #include "misc-parsers.h"
15 #include "stmt-parser.h"
16 #include "token-parsers.h"
17 #include "type-parser-implementation.h"
18 #include "flang/Parser/characters.h"
19 #include "flang/Parser/parse-tree.h"
20 
21 namespace Fortran::parser {
22 
23 // Fortran allows the statement with the corresponding label at the end of
24 // a do-construct that begins with an old-style label-do-stmt to be a
25 // new-style END DO statement; e.g., DO 10 I=1,N; ...; 10 END DO.  Usually,
26 // END DO statements appear only at the ends of do-constructs that begin
27 // with a nonlabel-do-stmt, so care must be taken to recognize this case and
28 // essentially treat them like CONTINUE statements.
29 
30 // R514 executable-construct ->
31 //        action-stmt | associate-construct | block-construct |
32 //        case-construct | change-team-construct | critical-construct |
33 //        do-construct | if-construct | select-rank-construct |
34 //        select-type-construct | where-construct | forall-construct
35 constexpr auto executableConstruct{
36     first(construct<ExecutableConstruct>(CapturedLabelDoStmt{}),
37         construct<ExecutableConstruct>(EndDoStmtForCapturedLabelDoStmt{}),
38         construct<ExecutableConstruct>(indirect(Parser<DoConstruct>{})),
39         // Attempt DO statements before assignment statements for better
40         // error messages in cases like "DO10I=1,(error)".
41         construct<ExecutableConstruct>(statement(actionStmt)),
42         construct<ExecutableConstruct>(indirect(Parser<AssociateConstruct>{})),
43         construct<ExecutableConstruct>(indirect(Parser<BlockConstruct>{})),
44         construct<ExecutableConstruct>(indirect(Parser<CaseConstruct>{})),
45         construct<ExecutableConstruct>(indirect(Parser<ChangeTeamConstruct>{})),
46         construct<ExecutableConstruct>(indirect(Parser<CriticalConstruct>{})),
47         construct<ExecutableConstruct>(indirect(Parser<IfConstruct>{})),
48         construct<ExecutableConstruct>(indirect(Parser<SelectRankConstruct>{})),
49         construct<ExecutableConstruct>(indirect(Parser<SelectTypeConstruct>{})),
50         construct<ExecutableConstruct>(indirect(whereConstruct)),
51         construct<ExecutableConstruct>(indirect(forallConstruct)),
52         construct<ExecutableConstruct>(indirect(ompEndLoopDirective)),
53         construct<ExecutableConstruct>(indirect(openaccConstruct)),
54         construct<ExecutableConstruct>(indirect(openmpConstruct)),
55         construct<ExecutableConstruct>(indirect(compilerDirective)))};
56 
57 // R510 execution-part-construct ->
58 //        executable-construct | format-stmt | entry-stmt | data-stmt
59 // Extension (PGI/Intel): also accept NAMELIST in execution part
60 constexpr auto obsoleteExecutionPartConstruct{recovery(ignoredStatementPrefix >>
61         fail<ExecutionPartConstruct>(
62             "obsolete legacy extension is not supported"_err_en_US),
63     construct<ExecutionPartConstruct>(construct<ErrorRecovery>(ok /
64         statement("REDIMENSION" >> name /
65                 parenthesized(nonemptyList(Parser<AllocateShapeSpec>{}))))))};
66 
67 TYPE_PARSER(recovery(
68     withMessage("expected execution part construct"_err_en_US,
69         CONTEXT_PARSER("execution part construct"_en_US,
70             first(construct<ExecutionPartConstruct>(executableConstruct),
71                 construct<ExecutionPartConstruct>(
72                     statement(indirect(formatStmt))),
73                 construct<ExecutionPartConstruct>(
74                     statement(indirect(entryStmt))),
75                 construct<ExecutionPartConstruct>(
76                     statement(indirect(dataStmt))),
77                 extension<LanguageFeature::ExecutionPartNamelist>(
78                     construct<ExecutionPartConstruct>(
79                         statement(indirect(Parser<NamelistStmt>{})))),
80                 obsoleteExecutionPartConstruct))),
81     construct<ExecutionPartConstruct>(executionPartErrorRecovery)))
82 
83 // R509 execution-part -> executable-construct [execution-part-construct]...
84 TYPE_CONTEXT_PARSER("execution part"_en_US,
85     construct<ExecutionPart>(many(executionPartConstruct)))
86 
87 // R515 action-stmt ->
88 //        allocate-stmt | assignment-stmt | backspace-stmt | call-stmt |
89 //        close-stmt | continue-stmt | cycle-stmt | deallocate-stmt |
90 //        endfile-stmt | error-stop-stmt | event-post-stmt | event-wait-stmt |
91 //        exit-stmt | fail-image-stmt | flush-stmt | form-team-stmt |
92 //        goto-stmt | if-stmt | inquire-stmt | lock-stmt | nullify-stmt |
93 //        open-stmt | pointer-assignment-stmt | print-stmt | read-stmt |
94 //        return-stmt | rewind-stmt | stop-stmt | sync-all-stmt |
95 //        sync-images-stmt | sync-memory-stmt | sync-team-stmt | unlock-stmt |
96 //        wait-stmt | where-stmt | write-stmt | computed-goto-stmt | forall-stmt
97 // R1159 continue-stmt -> CONTINUE
98 // R1163 fail-image-stmt -> FAIL IMAGE
99 TYPE_PARSER(first(construct<ActionStmt>(indirect(Parser<AllocateStmt>{})),
100     construct<ActionStmt>(indirect(assignmentStmt)),
101     construct<ActionStmt>(indirect(pointerAssignmentStmt)),
102     construct<ActionStmt>(indirect(Parser<BackspaceStmt>{})),
103     construct<ActionStmt>(indirect(Parser<CallStmt>{})),
104     construct<ActionStmt>(indirect(Parser<CloseStmt>{})),
105     construct<ActionStmt>(construct<ContinueStmt>("CONTINUE"_tok)),
106     construct<ActionStmt>(indirect(Parser<CycleStmt>{})),
107     construct<ActionStmt>(indirect(Parser<DeallocateStmt>{})),
108     construct<ActionStmt>(indirect(Parser<EndfileStmt>{})),
109     construct<ActionStmt>(indirect(Parser<EventPostStmt>{})),
110     construct<ActionStmt>(indirect(Parser<EventWaitStmt>{})),
111     construct<ActionStmt>(indirect(Parser<ExitStmt>{})),
112     construct<ActionStmt>(construct<FailImageStmt>("FAIL IMAGE"_sptok)),
113     construct<ActionStmt>(indirect(Parser<FlushStmt>{})),
114     construct<ActionStmt>(indirect(Parser<FormTeamStmt>{})),
115     construct<ActionStmt>(indirect(Parser<GotoStmt>{})),
116     construct<ActionStmt>(indirect(Parser<IfStmt>{})),
117     construct<ActionStmt>(indirect(Parser<InquireStmt>{})),
118     construct<ActionStmt>(indirect(Parser<LockStmt>{})),
119     construct<ActionStmt>(indirect(Parser<NullifyStmt>{})),
120     construct<ActionStmt>(indirect(Parser<OpenStmt>{})),
121     construct<ActionStmt>(indirect(Parser<PrintStmt>{})),
122     construct<ActionStmt>(indirect(Parser<ReadStmt>{})),
123     construct<ActionStmt>(indirect(Parser<ReturnStmt>{})),
124     construct<ActionStmt>(indirect(Parser<RewindStmt>{})),
125     construct<ActionStmt>(indirect(Parser<StopStmt>{})), // & error-stop-stmt
126     construct<ActionStmt>(indirect(Parser<SyncAllStmt>{})),
127     construct<ActionStmt>(indirect(Parser<SyncImagesStmt>{})),
128     construct<ActionStmt>(indirect(Parser<SyncMemoryStmt>{})),
129     construct<ActionStmt>(indirect(Parser<SyncTeamStmt>{})),
130     construct<ActionStmt>(indirect(Parser<UnlockStmt>{})),
131     construct<ActionStmt>(indirect(Parser<WaitStmt>{})),
132     construct<ActionStmt>(indirect(whereStmt)),
133     construct<ActionStmt>(indirect(Parser<WriteStmt>{})),
134     construct<ActionStmt>(indirect(Parser<ComputedGotoStmt>{})),
135     construct<ActionStmt>(indirect(forallStmt)),
136     construct<ActionStmt>(indirect(Parser<ArithmeticIfStmt>{})),
137     construct<ActionStmt>(indirect(Parser<AssignStmt>{})),
138     construct<ActionStmt>(indirect(Parser<AssignedGotoStmt>{})),
139     construct<ActionStmt>(indirect(Parser<PauseStmt>{}))))
140 
141 // R1102 associate-construct -> associate-stmt block end-associate-stmt
142 TYPE_CONTEXT_PARSER("ASSOCIATE construct"_en_US,
143     construct<AssociateConstruct>(statement(Parser<AssociateStmt>{}), block,
144         statement(Parser<EndAssociateStmt>{})))
145 
146 // R1103 associate-stmt ->
147 //        [associate-construct-name :] ASSOCIATE ( association-list )
148 TYPE_CONTEXT_PARSER("ASSOCIATE statement"_en_US,
149     construct<AssociateStmt>(maybe(name / ":"),
150         "ASSOCIATE" >> parenthesized(nonemptyList(Parser<Association>{}))))
151 
152 // R1104 association -> associate-name => selector
153 TYPE_PARSER(construct<Association>(name, "=>" >> selector))
154 
155 // R1105 selector -> expr | variable
156 TYPE_PARSER(construct<Selector>(variable) / lookAhead(","_tok || ")"_tok) ||
157     construct<Selector>(expr))
158 
159 // R1106 end-associate-stmt -> END ASSOCIATE [associate-construct-name]
160 TYPE_PARSER(construct<EndAssociateStmt>(
161     recovery("END ASSOCIATE" >> maybe(name), endStmtErrorRecovery)))
162 
163 // R1107 block-construct ->
164 //         block-stmt [block-specification-part] block end-block-stmt
165 TYPE_CONTEXT_PARSER("BLOCK construct"_en_US,
166     construct<BlockConstruct>(statement(Parser<BlockStmt>{}),
167         Parser<BlockSpecificationPart>{}, // can be empty
168         block, statement(Parser<EndBlockStmt>{})))
169 
170 // R1108 block-stmt -> [block-construct-name :] BLOCK
171 TYPE_PARSER(construct<BlockStmt>(maybe(name / ":") / "BLOCK"))
172 
173 // R1109 block-specification-part ->
174 //         [use-stmt]... [import-stmt]... [implicit-part]
175 //         [[declaration-construct]... specification-construct]
176 // C1107 prohibits COMMON, EQUIVALENCE, INTENT, NAMELIST, OPTIONAL, VALUE,
177 // and statement function definitions.  C1108 prohibits SAVE /common/.
178 // C1570 indirectly prohibits ENTRY.  These constraints are best enforced later.
179 // The odd grammar rule above would have the effect of forcing any
180 // trailing FORMAT and DATA statements after the last specification-construct
181 // to be recognized as part of the block-construct's block part rather than
182 // its block-specification-part, a distinction without any apparent difference.
183 TYPE_PARSER(construct<BlockSpecificationPart>(specificationPart))
184 
185 // R1110 end-block-stmt -> END BLOCK [block-construct-name]
186 TYPE_PARSER(construct<EndBlockStmt>(
187     recovery("END BLOCK" >> maybe(name), endStmtErrorRecovery)))
188 
189 // R1111 change-team-construct -> change-team-stmt block end-change-team-stmt
190 TYPE_CONTEXT_PARSER("CHANGE TEAM construct"_en_US,
191     construct<ChangeTeamConstruct>(statement(Parser<ChangeTeamStmt>{}), block,
192         statement(Parser<EndChangeTeamStmt>{})))
193 
194 // R1112 change-team-stmt ->
195 //         [team-construct-name :] CHANGE TEAM
196 //         ( team-value [, coarray-association-list] [, sync-stat-list] )
197 TYPE_CONTEXT_PARSER("CHANGE TEAM statement"_en_US,
198     construct<ChangeTeamStmt>(maybe(name / ":"),
199         "CHANGE TEAM"_sptok >> "("_tok >> teamValue,
200         defaulted("," >> nonemptyList(Parser<CoarrayAssociation>{})),
201         defaulted("," >> nonemptyList(statOrErrmsg))) /
202         ")")
203 
204 // R1113 coarray-association -> codimension-decl => selector
205 TYPE_PARSER(
206     construct<CoarrayAssociation>(Parser<CodimensionDecl>{}, "=>" >> selector))
207 
208 // R1114 end-change-team-stmt ->
209 //         END TEAM [( [sync-stat-list] )] [team-construct-name]
210 TYPE_CONTEXT_PARSER("END TEAM statement"_en_US,
211     construct<EndChangeTeamStmt>(
212         "END TEAM" >> defaulted(parenthesized(optionalList(statOrErrmsg))),
213         maybe(name)))
214 
215 // R1117 critical-stmt ->
216 //         [critical-construct-name :] CRITICAL [( [sync-stat-list] )]
217 TYPE_CONTEXT_PARSER("CRITICAL statement"_en_US,
218     construct<CriticalStmt>(maybe(name / ":"),
219         "CRITICAL" >> defaulted(parenthesized(optionalList(statOrErrmsg)))))
220 
221 // R1116 critical-construct -> critical-stmt block end-critical-stmt
222 TYPE_CONTEXT_PARSER("CRITICAL construct"_en_US,
223     construct<CriticalConstruct>(statement(Parser<CriticalStmt>{}), block,
224         statement(Parser<EndCriticalStmt>{})))
225 
226 // R1118 end-critical-stmt -> END CRITICAL [critical-construct-name]
227 TYPE_PARSER(construct<EndCriticalStmt>(
228     recovery("END CRITICAL" >> maybe(name), endStmtErrorRecovery)))
229 
230 // R1119 do-construct -> do-stmt block end-do
231 // R1120 do-stmt -> nonlabel-do-stmt | label-do-stmt
232 TYPE_CONTEXT_PARSER("DO construct"_en_US,
233     construct<DoConstruct>(
234         statement(Parser<NonLabelDoStmt>{}) / EnterNonlabelDoConstruct{}, block,
235         statement(Parser<EndDoStmt>{}) / LeaveDoConstruct{}))
236 
237 // R1125 concurrent-header ->
238 //         ( [integer-type-spec ::] concurrent-control-list
239 //         [, scalar-mask-expr] )
240 TYPE_PARSER(parenthesized(construct<ConcurrentHeader>(
241     maybe(integerTypeSpec / "::"), nonemptyList(Parser<ConcurrentControl>{}),
242     maybe("," >> scalarLogicalExpr))))
243 
244 // R1126 concurrent-control ->
245 //         index-name = concurrent-limit : concurrent-limit [: concurrent-step]
246 // R1127 concurrent-limit -> scalar-int-expr
247 // R1128 concurrent-step -> scalar-int-expr
248 TYPE_PARSER(construct<ConcurrentControl>(name / "=", scalarIntExpr / ":",
249     scalarIntExpr, maybe(":" >> scalarIntExpr)))
250 
251 // R1130 locality-spec ->
252 //         LOCAL ( variable-name-list ) | LOCAL_INIT ( variable-name-list ) |
253 //         SHARED ( variable-name-list ) | DEFAULT ( NONE )
254 TYPE_PARSER(construct<LocalitySpec>(construct<LocalitySpec::Local>(
255                 "LOCAL" >> parenthesized(listOfNames))) ||
256     construct<LocalitySpec>(construct<LocalitySpec::LocalInit>(
257         "LOCAL_INIT"_sptok >> parenthesized(listOfNames))) ||
258     construct<LocalitySpec>(construct<LocalitySpec::Shared>(
259         "SHARED" >> parenthesized(listOfNames))) ||
260     construct<LocalitySpec>(
261         construct<LocalitySpec::DefaultNone>("DEFAULT ( NONE )"_tok)))
262 
263 // R1123 loop-control ->
264 //         [,] do-variable = scalar-int-expr , scalar-int-expr
265 //           [, scalar-int-expr] |
266 //         [,] WHILE ( scalar-logical-expr ) |
267 //         [,] CONCURRENT concurrent-header concurrent-locality
268 // R1129 concurrent-locality -> [locality-spec]...
269 TYPE_CONTEXT_PARSER("loop control"_en_US,
270     maybe(","_tok) >>
271         (construct<LoopControl>(loopBounds(scalarExpr)) ||
272             construct<LoopControl>(
273                 "WHILE" >> parenthesized(scalarLogicalExpr)) ||
274             construct<LoopControl>(construct<LoopControl::Concurrent>(
275                 "CONCURRENT" >> concurrentHeader,
276                 many(Parser<LocalitySpec>{})))))
277 
278 // R1121 label-do-stmt -> [do-construct-name :] DO label [loop-control]
279 TYPE_CONTEXT_PARSER("label DO statement"_en_US,
280     construct<LabelDoStmt>(
281         maybe(name / ":"), "DO" >> label, maybe(loopControl)))
282 
283 // R1122 nonlabel-do-stmt -> [do-construct-name :] DO [loop-control]
284 TYPE_CONTEXT_PARSER("nonlabel DO statement"_en_US,
285     construct<NonLabelDoStmt>(maybe(name / ":"), "DO" >> maybe(loopControl)))
286 
287 // R1132 end-do-stmt -> END DO [do-construct-name]
288 TYPE_CONTEXT_PARSER("END DO statement"_en_US,
289     construct<EndDoStmt>(
290         recovery("END DO" >> maybe(name), endStmtErrorRecovery)))
291 
292 // R1133 cycle-stmt -> CYCLE [do-construct-name]
293 TYPE_CONTEXT_PARSER(
294     "CYCLE statement"_en_US, construct<CycleStmt>("CYCLE" >> maybe(name)))
295 
296 // R1134 if-construct ->
297 //         if-then-stmt block [else-if-stmt block]...
298 //         [else-stmt block] end-if-stmt
299 // R1135 if-then-stmt -> [if-construct-name :] IF ( scalar-logical-expr )
300 // THEN R1136 else-if-stmt ->
301 //         ELSE IF ( scalar-logical-expr ) THEN [if-construct-name]
302 // R1137 else-stmt -> ELSE [if-construct-name]
303 // R1138 end-if-stmt -> END IF [if-construct-name]
304 TYPE_CONTEXT_PARSER("IF construct"_en_US,
305     construct<IfConstruct>(
306         statement(construct<IfThenStmt>(maybe(name / ":"),
307             "IF" >> parenthesized(scalarLogicalExpr) / "THEN")),
308         block,
309         many(construct<IfConstruct::ElseIfBlock>(
310             unambiguousStatement(construct<ElseIfStmt>(
311                 "ELSE IF" >> parenthesized(scalarLogicalExpr),
312                 "THEN" >> maybe(name))),
313             block)),
314         maybe(construct<IfConstruct::ElseBlock>(
315             statement(construct<ElseStmt>("ELSE" >> maybe(name))), block)),
316         statement(construct<EndIfStmt>(
317             recovery("END IF" >> maybe(name), endStmtErrorRecovery)))))
318 
319 // R1139 if-stmt -> IF ( scalar-logical-expr ) action-stmt
320 TYPE_CONTEXT_PARSER("IF statement"_en_US,
321     construct<IfStmt>("IF" >> parenthesized(scalarLogicalExpr),
322         unlabeledStatement(actionStmt)))
323 
324 // R1140 case-construct ->
325 //         select-case-stmt [case-stmt block]... end-select-stmt
326 TYPE_CONTEXT_PARSER("SELECT CASE construct"_en_US,
327     construct<CaseConstruct>(statement(Parser<SelectCaseStmt>{}),
328         many(construct<CaseConstruct::Case>(
329             unambiguousStatement(Parser<CaseStmt>{}), block)),
330         statement(endSelectStmt)))
331 
332 // R1141 select-case-stmt -> [case-construct-name :] SELECT CASE ( case-expr
333 // ) R1144 case-expr -> scalar-expr
334 TYPE_CONTEXT_PARSER("SELECT CASE statement"_en_US,
335     construct<SelectCaseStmt>(
336         maybe(name / ":"), "SELECT CASE" >> parenthesized(scalar(expr))))
337 
338 // R1142 case-stmt -> CASE case-selector [case-construct-name]
339 TYPE_CONTEXT_PARSER("CASE statement"_en_US,
340     construct<CaseStmt>("CASE" >> Parser<CaseSelector>{}, maybe(name)))
341 
342 // R1143 end-select-stmt -> END SELECT [case-construct-name]
343 // R1151 end-select-rank-stmt -> END SELECT [select-construct-name]
344 // R1155 end-select-type-stmt -> END SELECT [select-construct-name]
345 TYPE_PARSER(construct<EndSelectStmt>(
346     recovery("END SELECT" >> maybe(name), endStmtErrorRecovery)))
347 
348 // R1145 case-selector -> ( case-value-range-list ) | DEFAULT
349 constexpr auto defaultKeyword{construct<Default>("DEFAULT"_tok)};
350 TYPE_PARSER(parenthesized(construct<CaseSelector>(
351                 nonemptyList(Parser<CaseValueRange>{}))) ||
352     construct<CaseSelector>(defaultKeyword))
353 
354 // R1147 case-value -> scalar-constant-expr
355 constexpr auto caseValue{scalar(constantExpr)};
356 
357 // R1146 case-value-range ->
358 //         case-value | case-value : | : case-value | case-value : case-value
359 TYPE_PARSER(construct<CaseValueRange>(construct<CaseValueRange::Range>(
360                 construct<std::optional<CaseValue>>(caseValue),
361                 ":" >> maybe(caseValue))) ||
362     construct<CaseValueRange>(
363         construct<CaseValueRange::Range>(construct<std::optional<CaseValue>>(),
364             ":" >> construct<std::optional<CaseValue>>(caseValue))) ||
365     construct<CaseValueRange>(caseValue))
366 
367 // R1148 select-rank-construct ->
368 //         select-rank-stmt [select-rank-case-stmt block]...
369 //         end-select-rank-stmt
370 TYPE_CONTEXT_PARSER("SELECT RANK construct"_en_US,
371     construct<SelectRankConstruct>(statement(Parser<SelectRankStmt>{}),
372         many(construct<SelectRankConstruct::RankCase>(
373             unambiguousStatement(Parser<SelectRankCaseStmt>{}), block)),
374         statement(endSelectStmt)))
375 
376 // R1149 select-rank-stmt ->
377 //         [select-construct-name :] SELECT RANK
378 //         ( [associate-name =>] selector )
379 TYPE_CONTEXT_PARSER("SELECT RANK statement"_en_US,
380     construct<SelectRankStmt>(maybe(name / ":"),
381         "SELECT RANK"_sptok >> "("_tok >> maybe(name / "=>"), selector / ")"))
382 
383 // R1150 select-rank-case-stmt ->
384 //         RANK ( scalar-int-constant-expr ) [select-construct-name] |
385 //         RANK ( * ) [select-construct-name] |
386 //         RANK DEFAULT [select-construct-name]
387 TYPE_CONTEXT_PARSER("RANK case statement"_en_US,
388     "RANK" >> (construct<SelectRankCaseStmt>(
389                   parenthesized(construct<SelectRankCaseStmt::Rank>(
390                                     scalarIntConstantExpr) ||
391                       construct<SelectRankCaseStmt::Rank>(star)) ||
392                       construct<SelectRankCaseStmt::Rank>(defaultKeyword),
393                   maybe(name))))
394 
395 // R1152 select-type-construct ->
396 //         select-type-stmt [type-guard-stmt block]... end-select-type-stmt
397 TYPE_CONTEXT_PARSER("SELECT TYPE construct"_en_US,
398     construct<SelectTypeConstruct>(statement(Parser<SelectTypeStmt>{}),
399         many(construct<SelectTypeConstruct::TypeCase>(
400             unambiguousStatement(Parser<TypeGuardStmt>{}), block)),
401         statement(endSelectStmt)))
402 
403 // R1153 select-type-stmt ->
404 //         [select-construct-name :] SELECT TYPE
405 //         ( [associate-name =>] selector )
406 TYPE_CONTEXT_PARSER("SELECT TYPE statement"_en_US,
407     construct<SelectTypeStmt>(maybe(name / ":"),
408         "SELECT TYPE (" >> maybe(name / "=>"), selector / ")"))
409 
410 // R1154 type-guard-stmt ->
411 //         TYPE IS ( type-spec ) [select-construct-name] |
412 //         CLASS IS ( derived-type-spec ) [select-construct-name] |
413 //         CLASS DEFAULT [select-construct-name]
414 TYPE_CONTEXT_PARSER("type guard statement"_en_US,
415     construct<TypeGuardStmt>("TYPE IS"_sptok >>
416                 parenthesized(construct<TypeGuardStmt::Guard>(typeSpec)) ||
417             "CLASS IS"_sptok >> parenthesized(construct<TypeGuardStmt::Guard>(
418                                     derivedTypeSpec)) ||
419             construct<TypeGuardStmt::Guard>("CLASS" >> defaultKeyword),
420         maybe(name)))
421 
422 // R1156 exit-stmt -> EXIT [construct-name]
423 TYPE_CONTEXT_PARSER(
424     "EXIT statement"_en_US, construct<ExitStmt>("EXIT" >> maybe(name)))
425 
426 // R1157 goto-stmt -> GO TO label
427 TYPE_CONTEXT_PARSER(
428     "GOTO statement"_en_US, construct<GotoStmt>("GO TO" >> label))
429 
430 // R1158 computed-goto-stmt -> GO TO ( label-list ) [,] scalar-int-expr
431 TYPE_CONTEXT_PARSER("computed GOTO statement"_en_US,
432     construct<ComputedGotoStmt>("GO TO" >> parenthesized(nonemptyList(label)),
433         maybe(","_tok) >> scalarIntExpr))
434 
435 // R1160 stop-stmt -> STOP [stop-code] [, QUIET = scalar-logical-expr]
436 // R1161 error-stop-stmt ->
437 //         ERROR STOP [stop-code] [, QUIET = scalar-logical-expr]
438 TYPE_CONTEXT_PARSER("STOP statement"_en_US,
439     construct<StopStmt>("STOP" >> pure(StopStmt::Kind::Stop) ||
440             "ERROR STOP"_sptok >> pure(StopStmt::Kind::ErrorStop),
441         maybe(Parser<StopCode>{}), maybe(", QUIET =" >> scalarLogicalExpr)))
442 
443 // R1162 stop-code -> scalar-default-char-expr | scalar-int-expr
444 // The two alternatives for stop-code can't be distinguished at
445 // parse time.
446 TYPE_PARSER(construct<StopCode>(scalar(expr)))
447 
448 // R1164 sync-all-stmt -> SYNC ALL [( [sync-stat-list] )]
449 TYPE_CONTEXT_PARSER("SYNC ALL statement"_en_US,
450     construct<SyncAllStmt>("SYNC ALL"_sptok >>
451         defaulted(parenthesized(optionalList(statOrErrmsg)))))
452 
453 // R1166 sync-images-stmt -> SYNC IMAGES ( image-set [, sync-stat-list] )
454 // R1167 image-set -> int-expr | *
455 TYPE_CONTEXT_PARSER("SYNC IMAGES statement"_en_US,
456     "SYNC IMAGES"_sptok >> parenthesized(construct<SyncImagesStmt>(
457                                construct<SyncImagesStmt::ImageSet>(intExpr) ||
458                                    construct<SyncImagesStmt::ImageSet>(star),
459                                defaulted("," >> nonemptyList(statOrErrmsg)))))
460 
461 // R1168 sync-memory-stmt -> SYNC MEMORY [( [sync-stat-list] )]
462 TYPE_CONTEXT_PARSER("SYNC MEMORY statement"_en_US,
463     construct<SyncMemoryStmt>("SYNC MEMORY"_sptok >>
464         defaulted(parenthesized(optionalList(statOrErrmsg)))))
465 
466 // R1169 sync-team-stmt -> SYNC TEAM ( team-value [, sync-stat-list] )
467 TYPE_CONTEXT_PARSER("SYNC TEAM statement"_en_US,
468     construct<SyncTeamStmt>("SYNC TEAM"_sptok >> "("_tok >> teamValue,
469         defaulted("," >> nonemptyList(statOrErrmsg)) / ")"))
470 
471 // R1170 event-post-stmt -> EVENT POST ( event-variable [, sync-stat-list] )
472 // R1171 event-variable -> scalar-variable
473 TYPE_CONTEXT_PARSER("EVENT POST statement"_en_US,
474     construct<EventPostStmt>("EVENT POST"_sptok >> "("_tok >> scalar(variable),
475         defaulted("," >> nonemptyList(statOrErrmsg)) / ")"))
476 
477 // R1172 event-wait-stmt ->
478 //         EVENT WAIT ( event-variable [, event-wait-spec-list] )
479 TYPE_CONTEXT_PARSER("EVENT WAIT statement"_en_US,
480     construct<EventWaitStmt>("EVENT WAIT"_sptok >> "("_tok >> scalar(variable),
481         defaulted("," >> nonemptyList(Parser<EventWaitStmt::EventWaitSpec>{})) /
482             ")"))
483 
484 // R1174 until-spec -> UNTIL_COUNT = scalar-int-expr
485 constexpr auto untilSpec{"UNTIL_COUNT =" >> scalarIntExpr};
486 
487 // R1173 event-wait-spec -> until-spec | sync-stat
488 TYPE_PARSER(construct<EventWaitStmt::EventWaitSpec>(untilSpec) ||
489     construct<EventWaitStmt::EventWaitSpec>(statOrErrmsg))
490 
491 // R1177 team-variable -> scalar-variable
492 constexpr auto teamVariable{scalar(variable)};
493 
494 // R1175 form-team-stmt ->
495 //         FORM TEAM ( team-number , team-variable [, form-team-spec-list] )
496 // R1176 team-number -> scalar-int-expr
497 TYPE_CONTEXT_PARSER("FORM TEAM statement"_en_US,
498     construct<FormTeamStmt>("FORM TEAM"_sptok >> "("_tok >> scalarIntExpr,
499         "," >> teamVariable,
500         defaulted("," >> nonemptyList(Parser<FormTeamStmt::FormTeamSpec>{})) /
501             ")"))
502 
503 // R1178 form-team-spec -> NEW_INDEX = scalar-int-expr | sync-stat
504 TYPE_PARSER(
505     construct<FormTeamStmt::FormTeamSpec>("NEW_INDEX =" >> scalarIntExpr) ||
506     construct<FormTeamStmt::FormTeamSpec>(statOrErrmsg))
507 
508 // R1182 lock-variable -> scalar-variable
509 constexpr auto lockVariable{scalar(variable)};
510 
511 // R1179 lock-stmt -> LOCK ( lock-variable [, lock-stat-list] )
512 TYPE_CONTEXT_PARSER("LOCK statement"_en_US,
513     construct<LockStmt>("LOCK (" >> lockVariable,
514         defaulted("," >> nonemptyList(Parser<LockStmt::LockStat>{})) / ")"))
515 
516 // R1180 lock-stat -> ACQUIRED_LOCK = scalar-logical-variable | sync-stat
517 TYPE_PARSER(
518     construct<LockStmt::LockStat>("ACQUIRED_LOCK =" >> scalarLogicalVariable) ||
519     construct<LockStmt::LockStat>(statOrErrmsg))
520 
521 // R1181 unlock-stmt -> UNLOCK ( lock-variable [, sync-stat-list] )
522 TYPE_CONTEXT_PARSER("UNLOCK statement"_en_US,
523     construct<UnlockStmt>("UNLOCK (" >> lockVariable,
524         defaulted("," >> nonemptyList(statOrErrmsg)) / ")"))
525 
526 } // namespace Fortran::parser
527