Debian Codegraph @

flang/lib/Parser/Fortran-parsers.cpp

raw
1 //===-- lib/Parser/Fortran-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 // Top-level grammar specification for Fortran. These parsers drive
10 // the tokenization parsers in cooked-tokens.h to consume characters,
11 // recognize the productions of Fortran, and to construct a parse tree.
12 // See ParserCombinators.md for documentation on the parser combinator
13 // library used here to implement an LL recursive descent recognizer.
14
15 // The productions that follow are derived from the draft Fortran 2018
16 // standard, with some necessary modifications to remove left recursion
17 // and some generalization in order to defer cases where parses depend
18 // on the definitions of symbols. The "Rxxx" numbers that appear in
19 // comments refer to these numbered requirements in the Fortran standard.
20
21 // The whole Fortran grammar originally constituted one header file,
22 // but that turned out to require more memory to compile with current
23 // C++ compilers than some people were willing to accept, so now the
24 // various per-type parsers are partitioned into several C++ source
25 // files. This file contains parsers for constants, types, declarations,
26 // and misfits (mostly clauses 7, 8, & 9 of Fortran 2018). The others:
27 // executable-parsers.cpp Executable statements
28 // expr-parsers.cpp Expressions
29 // io-parsers.cpp I/O statements and FORMAT
30 // openmp-parsers.cpp OpenMP directives
31 // program-parsers.cpp Program units
32
33 #include "basic-parsers.h"
34 #include "expr-parsers.h"
35 #include "misc-parsers.h"
36 #include "stmt-parser.h"
37 #include "token-parsers.h"
38 #include "type-parser-implementation.h"
39 #include "flang/Parser/parse-tree.h"
40 #include "flang/Parser/user-state.h"
41
42 namespace Fortran::parser {
43
44 // R601 alphanumeric-character -> letter | digit | underscore
45 // R603 name -> letter [alphanumeric-character]...
46 constexpr auto nonDigitIdChar{letter || otherIdChar};
47 constexpr auto rawName{nonDigitIdChar >> many(nonDigitIdChar || digit)};
48 TYPE_PARSER(space >> sourced(rawName >> construct<Name>()))
49
50 // R608 intrinsic-operator ->
51 // power-op | mult-op | add-op | concat-op | rel-op |
52 // not-op | and-op | or-op | equiv-op
53 // R610 extended-intrinsic-op -> intrinsic-operator
54 // These parsers must be ordered carefully to avoid misrecognition.
55 constexpr auto namedIntrinsicOperator{
56 ".LT." >> pure(DefinedOperator::IntrinsicOperator::LT) ||
57 ".LE." >> pure(DefinedOperator::IntrinsicOperator::LE) ||
58 ".EQ." >> pure(DefinedOperator::IntrinsicOperator::EQ) ||
59 ".NE." >> pure(DefinedOperator::IntrinsicOperator::NE) ||
60 ".GE." >> pure(DefinedOperator::IntrinsicOperator::GE) ||
61 ".GT." >> pure(DefinedOperator::IntrinsicOperator::GT) ||
62 ".NOT." >> pure(DefinedOperator::IntrinsicOperator::NOT) ||
63 ".AND." >> pure(DefinedOperator::IntrinsicOperator::AND) ||
64 ".OR." >> pure(DefinedOperator::IntrinsicOperator::OR) ||
65 ".EQV." >> pure(DefinedOperator::IntrinsicOperator::EQV) ||
66 ".NEQV." >> pure(DefinedOperator::IntrinsicOperator::NEQV) ||
67 extension<LanguageFeature::XOROperator>(
68 "nonstandard usage: .XOR. spelling of .NEQV."_port_en_US,
69 ".XOR." >> pure(DefinedOperator::IntrinsicOperator::NEQV)) ||
70 extension<LanguageFeature::LogicalAbbreviations>(
71 "nonstandard usage: abbreviated logical operator"_port_en_US,
72 ".N." >> pure(DefinedOperator::IntrinsicOperator::NOT) ||
73 ".A." >> pure(DefinedOperator::IntrinsicOperator::AND) ||
74 ".O." >> pure(DefinedOperator::IntrinsicOperator::OR) ||
75 extension<LanguageFeature::XOROperator>(
76 "nonstandard usage: .X. spelling of .NEQV."_port_en_US,
77 ".X." >> pure(DefinedOperator::IntrinsicOperator::NEQV)))};
78
79 constexpr auto intrinsicOperator{
80 "**" >> pure(DefinedOperator::IntrinsicOperator::Power) ||
81 "*" >> pure(DefinedOperator::IntrinsicOperator::Multiply) ||
82 "//" >> pure(DefinedOperator::IntrinsicOperator::Concat) ||
83 "/=" >> pure(DefinedOperator::IntrinsicOperator::NE) ||
84 "/" >> pure(DefinedOperator::IntrinsicOperator::Divide) ||
85 "+" >> pure(DefinedOperator::IntrinsicOperator::Add) ||
86 "-" >> pure(DefinedOperator::IntrinsicOperator::Subtract) ||
87 "<=" >> pure(DefinedOperator::IntrinsicOperator::LE) ||
88 extension<LanguageFeature::AlternativeNE>(
89 "nonstandard usage: <> spelling of /= or .NE."_port_en_US,
90 "<>" >> pure(DefinedOperator::IntrinsicOperator::NE)) ||
91 "<" >> pure(DefinedOperator::IntrinsicOperator::LT) ||
92 "==" >> pure(DefinedOperator::IntrinsicOperator::EQ) ||
93 ">=" >> pure(DefinedOperator::IntrinsicOperator::GE) ||
94 ">" >> pure(DefinedOperator::IntrinsicOperator::GT) ||
95 namedIntrinsicOperator};
96
97 // R609 defined-operator ->
98 // defined-unary-op | defined-binary-op | extended-intrinsic-op
99 TYPE_PARSER(construct<DefinedOperator>(intrinsicOperator) ||
100 construct<DefinedOperator>(definedOpName))
101
102 // R505 implicit-part -> [implicit-part-stmt]... implicit-stmt
103 // N.B. PARAMETER, FORMAT, & ENTRY statements that appear before any
104 // other kind of declaration-construct will be parsed into the
105 // implicit-part.
106 TYPE_CONTEXT_PARSER("implicit part"_en_US,
107 construct<ImplicitPart>(many(Parser<ImplicitPartStmt>{})))
108
109 // R506 implicit-part-stmt ->
110 // implicit-stmt | parameter-stmt | format-stmt | entry-stmt
111 TYPE_PARSER(first(
112 construct<ImplicitPartStmt>(statement(indirect(Parser<ImplicitStmt>{}))),
113 construct<ImplicitPartStmt>(statement(indirect(parameterStmt))),
114 construct<ImplicitPartStmt>(statement(indirect(oldParameterStmt))),
115 construct<ImplicitPartStmt>(statement(indirect(formatStmt))),
116 construct<ImplicitPartStmt>(statement(indirect(entryStmt))),
117 construct<ImplicitPartStmt>(indirect(compilerDirective)),
118 construct<ImplicitPartStmt>(indirect(openaccDeclarativeConstruct))))
119
120 // R512 internal-subprogram -> function-subprogram | subroutine-subprogram
121 // Internal subprograms are not program units, so their END statements
122 // can be followed by ';' and another statement on the same line.
123 TYPE_CONTEXT_PARSER("internal subprogram"_en_US,
124 (construct<InternalSubprogram>(indirect(functionSubprogram)) ||
125 construct<InternalSubprogram>(indirect(subroutineSubprogram))) /
126 forceEndOfStmt ||
127 construct<InternalSubprogram>(indirect(compilerDirective)))
128
129 // R511 internal-subprogram-part -> contains-stmt [internal-subprogram]...
130 TYPE_CONTEXT_PARSER("internal subprogram part"_en_US,
131 construct<InternalSubprogramPart>(statement(containsStmt),
132 many(StartNewSubprogram{} >> Parser<InternalSubprogram>{})))
133
134 // R605 literal-constant ->
135 // int-literal-constant | real-literal-constant |
136 // complex-literal-constant | logical-literal-constant |
137 // char-literal-constant | boz-literal-constant
138 TYPE_PARSER(
139 first(construct<LiteralConstant>(Parser<HollerithLiteralConstant>{}),
140 construct<LiteralConstant>(realLiteralConstant),
141 construct<LiteralConstant>(intLiteralConstant),
142 construct<LiteralConstant>(Parser<ComplexLiteralConstant>{}),
143 construct<LiteralConstant>(Parser<BOZLiteralConstant>{}),
144 construct<LiteralConstant>(charLiteralConstant),
145 construct<LiteralConstant>(Parser<LogicalLiteralConstant>{})))
146
147 // R606 named-constant -> name
148 TYPE_PARSER(construct<NamedConstant>(name))
149
150 // R701 type-param-value -> scalar-int-expr | * | :
151 TYPE_PARSER(construct<TypeParamValue>(scalarIntExpr) ||
152 construct<TypeParamValue>(star) ||
153 construct<TypeParamValue>(construct<TypeParamValue::Deferred>(":"_tok)))
154
155 // R702 type-spec -> intrinsic-type-spec | derived-type-spec
156 // N.B. This type-spec production is one of two instances in the Fortran
157 // grammar where intrinsic types and bare derived type names can clash;
158 // the other is below in R703 declaration-type-spec. Look-ahead is required
159 // to disambiguate the cases where a derived type name begins with the name
160 // of an intrinsic type, e.g., REALITY.
161 TYPE_CONTEXT_PARSER("type spec"_en_US,
162 construct<TypeSpec>(intrinsicTypeSpec / lookAhead("::"_tok || ")"_tok)) ||
163 construct<TypeSpec>(derivedTypeSpec))
164
165 // R703 declaration-type-spec ->
166 // intrinsic-type-spec | TYPE ( intrinsic-type-spec ) |
167 // TYPE ( derived-type-spec ) | CLASS ( derived-type-spec ) |
168 // CLASS ( * ) | TYPE ( * )
169 // N.B. It is critical to distribute "parenthesized()" over the alternatives
170 // for TYPE (...), rather than putting the alternatives within it, which
171 // would fail on "TYPE(real_derived)" with a misrecognition of "real" as an
172 // intrinsic-type-spec.
173 // N.B. TYPE(x) is a derived type if x is a one-word extension intrinsic
174 // type (BYTE or DOUBLECOMPLEX), not the extension intrinsic type.
175 TYPE_CONTEXT_PARSER("declaration type spec"_en_US,
176 construct<DeclarationTypeSpec>(intrinsicTypeSpec) ||
177 "TYPE" >>
178 (parenthesized(construct<DeclarationTypeSpec>(
179 !"DOUBLECOMPLEX"_tok >> !"BYTE"_tok >> intrinsicTypeSpec)) ||
180 parenthesized(construct<DeclarationTypeSpec>(
181 construct<DeclarationTypeSpec::Type>(derivedTypeSpec))) ||
182 construct<DeclarationTypeSpec>(
183 "( * )" >> construct<DeclarationTypeSpec::TypeStar>())) ||
184 "CLASS" >> parenthesized(construct<DeclarationTypeSpec>(
185 construct<DeclarationTypeSpec::Class>(
186 derivedTypeSpec)) ||
187 construct<DeclarationTypeSpec>("*" >>
188 construct<DeclarationTypeSpec::ClassStar>())) ||
189 extension<LanguageFeature::DECStructures>(
190 "nonstandard usage: STRUCTURE"_port_en_US,
191 construct<DeclarationTypeSpec>(
192 // As is also done for the STRUCTURE statement, the name of
193 // the structure includes the surrounding slashes to avoid
194 // name clashes.
195 construct<DeclarationTypeSpec::Record>(
196 "RECORD" >> sourced("/" >> name / "/")))) ||
197 construct<DeclarationTypeSpec>(vectorTypeSpec))
198
199 // R704 intrinsic-type-spec ->
200 // integer-type-spec | REAL [kind-selector] | DOUBLE PRECISION |
201 // COMPLEX [kind-selector] | CHARACTER [char-selector] |
202 // LOGICAL [kind-selector]
203 // Extensions: DOUBLE COMPLEX, BYTE
204 TYPE_CONTEXT_PARSER("intrinsic type spec"_en_US,
205 first(construct<IntrinsicTypeSpec>(integerTypeSpec),
206 construct<IntrinsicTypeSpec>(
207 construct<IntrinsicTypeSpec::Real>("REAL" >> maybe(kindSelector))),
208 construct<IntrinsicTypeSpec>("DOUBLE PRECISION" >>
209 construct<IntrinsicTypeSpec::DoublePrecision>()),
210 construct<IntrinsicTypeSpec>(construct<IntrinsicTypeSpec::Complex>(
211 "COMPLEX" >> maybe(kindSelector))),
212 construct<IntrinsicTypeSpec>(construct<IntrinsicTypeSpec::Character>(
213 "CHARACTER" >> maybe(Parser<CharSelector>{}))),
214 construct<IntrinsicTypeSpec>(construct<IntrinsicTypeSpec::Logical>(
215 "LOGICAL" >> maybe(kindSelector))),
216 extension<LanguageFeature::DoubleComplex>(
217 "nonstandard usage: DOUBLE COMPLEX"_port_en_US,
218 construct<IntrinsicTypeSpec>("DOUBLE COMPLEX"_sptok >>
219 construct<IntrinsicTypeSpec::DoubleComplex>())),
220 extension<LanguageFeature::Byte>("nonstandard usage: BYTE"_port_en_US,
221 construct<IntrinsicTypeSpec>(construct<IntegerTypeSpec>(
222 "BYTE" >> construct<std::optional<KindSelector>>(pure(1)))))))
223
224 // Extension: Vector type
225 // VECTOR(intrinsic-type-spec) | __VECTOR_PAIR | __VECTOR_QUAD
226 TYPE_CONTEXT_PARSER("vector type spec"_en_US,
227 extension<LanguageFeature::PPCVector>(
228 "nonstandard usage: Vector type"_port_en_US,
229 first(construct<VectorTypeSpec>(intrinsicVectorTypeSpec),
230 construct<VectorTypeSpec>("__VECTOR_PAIR" >>
231 construct<VectorTypeSpec::PairVectorTypeSpec>()),
232 construct<VectorTypeSpec>("__VECTOR_QUAD" >>
233 construct<VectorTypeSpec::QuadVectorTypeSpec>()))))
234
235 // VECTOR(integer-type-spec) | VECTOR(real-type-spec) |
236 // VECTOR(unsigend-type-spec) |
237 TYPE_PARSER(construct<IntrinsicVectorTypeSpec>("VECTOR" >>
238 parenthesized(construct<VectorElementType>(integerTypeSpec) ||
239 construct<VectorElementType>(unsignedTypeSpec) ||
240 construct<VectorElementType>(construct<IntrinsicTypeSpec::Real>(
241 "REAL" >> maybe(kindSelector))))))
242
243 // UNSIGNED type
244 TYPE_PARSER(construct<UnsignedTypeSpec>("UNSIGNED" >> maybe(kindSelector)))
245
246 // R705 integer-type-spec -> INTEGER [kind-selector]
247 TYPE_PARSER(construct<IntegerTypeSpec>("INTEGER" >> maybe(kindSelector)))
248
249 // R706 kind-selector -> ( [KIND =] scalar-int-constant-expr )
250 // Legacy extension: kind-selector -> * digit-string
251 TYPE_PARSER(construct<KindSelector>(
252 parenthesized(maybe("KIND ="_tok) >> scalarIntConstantExpr)) ||
253 extension<LanguageFeature::StarKind>(
254 "nonstandard usage: TYPE*KIND syntax"_port_en_US,
255 construct<KindSelector>(construct<KindSelector::StarSize>(
256 "*" >> digitString64 / spaceCheck))))
257
258 constexpr auto noSpace{
259 recovery(withMessage("invalid space"_err_en_US, !" "_ch), space)};
260
261 // R707 signed-int-literal-constant -> [sign] int-literal-constant
262 TYPE_PARSER(sourced(
263 construct<SignedIntLiteralConstant>(SignedIntLiteralConstantWithoutKind{},
264 maybe(noSpace >> underscore >> noSpace >> kindParam))))
265
266 // R708 int-literal-constant -> digit-string [_ kind-param]
267 // The negated look-ahead for a trailing underscore prevents misrecognition
268 // when the digit string is a numeric kind parameter of a character literal.
269 TYPE_PARSER(construct<IntLiteralConstant>(space >> digitString,
270 maybe(underscore >> noSpace >> kindParam) / !underscore))
271
272 // R709 kind-param -> digit-string | scalar-int-constant-name
273 TYPE_PARSER(construct<KindParam>(digitString64) ||
274 construct<KindParam>(
275 scalar(integer(constant(sourced(rawName >> construct<Name>()))))))
276
277 // R712 sign -> + | -
278 // N.B. A sign constitutes a whole token, so a space is allowed in free form
279 // after the sign and before a real-literal-constant or
280 // complex-literal-constant. A sign is not a unary operator in these contexts.
281 constexpr auto sign{
282 "+"_tok >> pure(Sign::Positive) || "-"_tok >> pure(Sign::Negative)};
283
284 // R713 signed-real-literal-constant -> [sign] real-literal-constant
285 constexpr auto signedRealLiteralConstant{
286 construct<SignedRealLiteralConstant>(maybe(sign), realLiteralConstant)};
287
288 // R714 real-literal-constant ->
289 // significand [exponent-letter exponent] [_ kind-param] |
290 // digit-string exponent-letter exponent [_ kind-param]
291 // R715 significand -> digit-string . [digit-string] | . digit-string
292 // R716 exponent-letter -> E | D
293 // Extension: Q
294 // R717 exponent -> signed-digit-string
295 constexpr auto exponentPart{
296 ("ed"_ch ||
297 extension<LanguageFeature::QuadPrecision>(
298 "nonstandard usage: Q exponent"_port_en_US, "q"_ch)) >>
299 SignedDigitString{}};
300
301 TYPE_CONTEXT_PARSER("REAL literal constant"_en_US,
302 space >>
303 construct<RealLiteralConstant>(
304 sourced((digitString >> "."_ch >>
305 !(some(letter) >>
306 "."_ch /* don't misinterpret 1.AND. */) >>
307 maybe(digitString) >> maybe(exponentPart) >> ok ||
308 "."_ch >> digitString >> maybe(exponentPart) >> ok ||
309 digitString >> exponentPart >> ok) >>
310 construct<RealLiteralConstant::Real>()),
311 maybe(noSpace >> underscore >> noSpace >> kindParam)))
312
313 // R718 complex-literal-constant -> ( real-part , imag-part )
314 TYPE_CONTEXT_PARSER("COMPLEX literal constant"_en_US,
315 parenthesized(construct<ComplexLiteralConstant>(
316 Parser<ComplexPart>{} / ",", Parser<ComplexPart>{})))
317
318 // PGI/Intel extension: signed complex literal constant
319 TYPE_PARSER(construct<SignedComplexLiteralConstant>(
320 sign, Parser<ComplexLiteralConstant>{}))
321
322 // R719 real-part ->
323 // signed-int-literal-constant | signed-real-literal-constant |
324 // named-constant
325 // R720 imag-part ->
326 // signed-int-literal-constant | signed-real-literal-constant |
327 // named-constant
328 TYPE_PARSER(construct<ComplexPart>(signedRealLiteralConstant) ||
329 construct<ComplexPart>(signedIntLiteralConstant) ||
330 construct<ComplexPart>(namedConstant))
331
332 // R721 char-selector ->
333 // length-selector |
334 // ( LEN = type-param-value , KIND = scalar-int-constant-expr ) |
335 // ( type-param-value , [KIND =] scalar-int-constant-expr ) |
336 // ( KIND = scalar-int-constant-expr [, LEN = type-param-value] )
337 TYPE_PARSER(construct<CharSelector>(Parser<LengthSelector>{}) ||
338 parenthesized(construct<CharSelector>(
339 "LEN =" >> typeParamValue, ", KIND =" >> scalarIntConstantExpr)) ||
340 parenthesized(construct<CharSelector>(
341 typeParamValue / ",", maybe("KIND ="_tok) >> scalarIntConstantExpr)) ||
342 parenthesized(construct<CharSelector>(
343 "KIND =" >> scalarIntConstantExpr, maybe(", LEN =" >> typeParamValue))))
344
345 // R722 length-selector -> ( [LEN =] type-param-value ) | * char-length [,]
346 // N.B. The trailing [,] in the production is permitted by the Standard
347 // only in the context of a type-declaration-stmt, but even with that
348 // limitation, it would seem to be unnecessary and buggy to consume the comma
349 // here.
350 TYPE_PARSER(construct<LengthSelector>(
351 parenthesized(maybe("LEN ="_tok) >> typeParamValue)) ||
352 construct<LengthSelector>("*" >> charLength /* / maybe(","_tok) */))
353
354 // R723 char-length -> ( type-param-value ) | digit-string
355 TYPE_PARSER(construct<CharLength>(parenthesized(typeParamValue)) ||
356 construct<CharLength>(space >> digitString64 / spaceCheck))
357
358 // R724 char-literal-constant ->
359 // [kind-param _] ' [rep-char]... ' |
360 // [kind-param _] " [rep-char]... "
361 // "rep-char" is any non-control character. Doubled interior quotes are
362 // combined. Backslash escapes can be enabled.
363 // N.B. the parsing of "kind-param" takes care to not consume the '_'.
364 TYPE_CONTEXT_PARSER("CHARACTER literal constant"_en_US,
365 construct<CharLiteralConstant>(
366 kindParam / underscore, charLiteralConstantWithoutKind) ||
367 construct<CharLiteralConstant>(construct<std::optional<KindParam>>(),
368 space >> charLiteralConstantWithoutKind))
369
370 TYPE_CONTEXT_PARSER(
371 "Hollerith"_en_US, construct<HollerithLiteralConstant>(rawHollerithLiteral))
372
373 // R725 logical-literal-constant ->
374 // .TRUE. [_ kind-param] | .FALSE. [_ kind-param]
375 // Also accept .T. and .F. as extensions.
376 TYPE_PARSER(construct<LogicalLiteralConstant>(logicalTRUE,
377 maybe(noSpace >> underscore >> noSpace >> kindParam)) ||
378 construct<LogicalLiteralConstant>(
379 logicalFALSE, maybe(noSpace >> underscore >> noSpace >> kindParam)))
380
381 // R726 derived-type-def ->
382 // derived-type-stmt [type-param-def-stmt]...
383 // [private-or-sequence]... [component-part]
384 // [type-bound-procedure-part] end-type-stmt
385 // R735 component-part -> [component-def-stmt]...
386 TYPE_CONTEXT_PARSER("derived type definition"_en_US,
387 construct<DerivedTypeDef>(statement(Parser<DerivedTypeStmt>{}),
388 many(unambiguousStatement(Parser<TypeParamDefStmt>{})),
389 many(statement(Parser<PrivateOrSequence>{})),
390 many(inContext("component"_en_US,
391 unambiguousStatement(Parser<ComponentDefStmt>{}))),
392 maybe(Parser<TypeBoundProcedurePart>{}),
393 statement(Parser<EndTypeStmt>{})))
394
395 // R727 derived-type-stmt ->
396 // TYPE [[, type-attr-spec-list] ::] type-name [(
397 // type-param-name-list )]
398 TYPE_CONTEXT_PARSER("TYPE statement"_en_US,
399 construct<DerivedTypeStmt>(
400 "TYPE" >> optionalListBeforeColons(Parser<TypeAttrSpec>{}), name,
401 defaulted(parenthesized(nonemptyList(name)))))
402
403 // R728 type-attr-spec ->
404 // ABSTRACT | access-spec | BIND(C) | EXTENDS ( parent-type-name )
405 TYPE_PARSER(construct<TypeAttrSpec>(construct<Abstract>("ABSTRACT"_tok)) ||
406 construct<TypeAttrSpec>(construct<TypeAttrSpec::BindC>("BIND ( C )"_tok)) ||
407 construct<TypeAttrSpec>(
408 construct<TypeAttrSpec::Extends>("EXTENDS" >> parenthesized(name))) ||
409 construct<TypeAttrSpec>(accessSpec))
410
411 // R729 private-or-sequence -> private-components-stmt | sequence-stmt
412 TYPE_PARSER(construct<PrivateOrSequence>(Parser<PrivateStmt>{}) ||
413 construct<PrivateOrSequence>(Parser<SequenceStmt>{}))
414
415 // R730 end-type-stmt -> END TYPE [type-name]
416 TYPE_PARSER(construct<EndTypeStmt>(
417 recovery("END TYPE" >> maybe(name), namedConstructEndStmtErrorRecovery)))
418
419 // R731 sequence-stmt -> SEQUENCE
420 TYPE_PARSER(construct<SequenceStmt>("SEQUENCE"_tok))
421
422 // R732 type-param-def-stmt ->
423 // integer-type-spec , type-param-attr-spec :: type-param-decl-list
424 // R734 type-param-attr-spec -> KIND | LEN
425 constexpr auto kindOrLen{"KIND" >> pure(common::TypeParamAttr::Kind) ||
426 "LEN" >> pure(common::TypeParamAttr::Len)};
427 TYPE_PARSER(construct<TypeParamDefStmt>(integerTypeSpec / ",", kindOrLen,
428 "::" >> nonemptyList("expected type parameter declarations"_err_en_US,
429 Parser<TypeParamDecl>{})))
430
431 // R733 type-param-decl -> type-param-name [= scalar-int-constant-expr]
432 TYPE_PARSER(construct<TypeParamDecl>(name, maybe("=" >> scalarIntConstantExpr)))
433
434 // R736 component-def-stmt -> data-component-def-stmt |
435 // proc-component-def-stmt
436 // Accidental extension not enabled here: PGI accepts type-param-def-stmt in
437 // component-part of derived-type-def.
438 TYPE_PARSER(recovery(
439 withMessage("expected component definition"_err_en_US,
440 first(construct<ComponentDefStmt>(Parser<DataComponentDefStmt>{}),
441 construct<ComponentDefStmt>(Parser<ProcComponentDefStmt>{}),
442 construct<ComponentDefStmt>(indirect(compilerDirective)))),
443 construct<ComponentDefStmt>(inStmtErrorRecovery)))
444
445 // R737 data-component-def-stmt ->
446 // declaration-type-spec [[, component-attr-spec-list] ::]
447 // component-decl-list
448 // N.B. The standard requires double colons if there's an initializer.
449 TYPE_PARSER(construct<DataComponentDefStmt>(declarationTypeSpec,
450 optionalListBeforeColons(Parser<ComponentAttrSpec>{}),
451 nonemptyList("expected component declarations"_err_en_US,
452 Parser<ComponentOrFill>{})))
453
454 // R738 component-attr-spec ->
455 // access-spec | ALLOCATABLE |
456 // CODIMENSION lbracket coarray-spec rbracket |
457 // CONTIGUOUS | DIMENSION ( component-array-spec ) | POINTER |
458 // CUDA-data-attr
459 TYPE_PARSER(construct<ComponentAttrSpec>(accessSpec) ||
460 construct<ComponentAttrSpec>(allocatable) ||
461 construct<ComponentAttrSpec>("CODIMENSION" >> coarraySpec) ||
462 construct<ComponentAttrSpec>(contiguous) ||
463 construct<ComponentAttrSpec>("DIMENSION" >> Parser<ComponentArraySpec>{}) ||
464 construct<ComponentAttrSpec>(pointer) ||
465 extension<LanguageFeature::CUDA>(
466 construct<ComponentAttrSpec>(Parser<common::CUDADataAttr>{})) ||
467 construct<ComponentAttrSpec>(recovery(
468 fail<ErrorRecovery>(
469 "type parameter definitions must appear before component declarations"_err_en_US),
470 kindOrLen >> construct<ErrorRecovery>())))
471
472 // R739 component-decl ->
473 // component-name [( component-array-spec )]
474 // [lbracket coarray-spec rbracket] [* char-length]
475 // [component-initialization]
476 TYPE_CONTEXT_PARSER("component declaration"_en_US,
477 construct<ComponentDecl>(name, maybe(Parser<ComponentArraySpec>{}),
478 maybe(coarraySpec), maybe("*" >> charLength), maybe(initialization)))
479 // The source field of the Name will be replaced with a distinct generated name.
480 TYPE_CONTEXT_PARSER("%FILL item"_en_US,
481 extension<LanguageFeature::DECStructures>(
482 "nonstandard usage: %FILL"_port_en_US,
483 construct<FillDecl>(space >> sourced("%FILL" >> construct<Name>()),
484 maybe(Parser<ComponentArraySpec>{}), maybe("*" >> charLength))))
485 TYPE_PARSER(construct<ComponentOrFill>(Parser<ComponentDecl>{}) ||
486 construct<ComponentOrFill>(Parser<FillDecl>{}))
487
488 // R740 component-array-spec ->
489 // explicit-shape-spec-list | deferred-shape-spec-list
490 // N.B. Parenthesized here rather than around references to this production.
491 TYPE_PARSER(construct<ComponentArraySpec>(parenthesized(
492 nonemptyList("expected explicit shape specifications"_err_en_US,
493 explicitShapeSpec))) ||
494 construct<ComponentArraySpec>(parenthesized(deferredShapeSpecList)))
495
496 // R741 proc-component-def-stmt ->
497 // PROCEDURE ( [proc-interface] ) , proc-component-attr-spec-list
498 // :: proc-decl-list
499 TYPE_CONTEXT_PARSER("PROCEDURE component definition statement"_en_US,
500 construct<ProcComponentDefStmt>(
501 "PROCEDURE" >> parenthesized(maybe(procInterface)),
502 localRecovery("expected PROCEDURE component attributes"_err_en_US,
503 "," >> nonemptyList(Parser<ProcComponentAttrSpec>{}), ok),
504 localRecovery("expected PROCEDURE declarations"_err_en_US,
505 "::" >> nonemptyList(procDecl), SkipTo<'\n'>{})))
506
507 // R742 proc-component-attr-spec ->
508 // access-spec | NOPASS | PASS [(arg-name)] | POINTER
509 constexpr auto noPass{construct<NoPass>("NOPASS"_tok)};
510 constexpr auto pass{construct<Pass>("PASS" >> maybe(parenthesized(name)))};
511 TYPE_PARSER(construct<ProcComponentAttrSpec>(accessSpec) ||
512 construct<ProcComponentAttrSpec>(noPass) ||
513 construct<ProcComponentAttrSpec>(pass) ||
514 construct<ProcComponentAttrSpec>(pointer))
515
516 // R744 initial-data-target -> designator
517 constexpr auto initialDataTarget{indirect(designator)};
518
519 // R743 component-initialization ->
520 // = constant-expr | => null-init | => initial-data-target
521 // R805 initialization ->
522 // = constant-expr | => null-init | => initial-data-target
523 // Universal extension: initialization -> / data-stmt-value-list /
524 TYPE_PARSER(construct<Initialization>("=>" >> nullInit) ||
525 construct<Initialization>("=>" >> initialDataTarget) ||
526 construct<Initialization>("=" >> constantExpr) ||
527 extension<LanguageFeature::SlashInitialization>(
528 "nonstandard usage: /initialization/"_port_en_US,
529 construct<Initialization>(
530 "/" >> nonemptyList("expected values"_err_en_US,
531 indirect(Parser<DataStmtValue>{})) /
532 "/")))
533
534 // R745 private-components-stmt -> PRIVATE
535 // R747 binding-private-stmt -> PRIVATE
536 TYPE_PARSER(construct<PrivateStmt>("PRIVATE"_tok))
537
538 // R746 type-bound-procedure-part ->
539 // contains-stmt [binding-private-stmt] [type-bound-proc-binding]...
540 TYPE_CONTEXT_PARSER("type bound procedure part"_en_US,
541 construct<TypeBoundProcedurePart>(statement(containsStmt),
542 maybe(statement(Parser<PrivateStmt>{})),
543 many(statement(Parser<TypeBoundProcBinding>{}))))
544
545 // R748 type-bound-proc-binding ->
546 // type-bound-procedure-stmt | type-bound-generic-stmt |
547 // final-procedure-stmt
548 TYPE_CONTEXT_PARSER("type bound procedure binding"_en_US,
549 recovery(
550 first(construct<TypeBoundProcBinding>(Parser<TypeBoundProcedureStmt>{}),
551 construct<TypeBoundProcBinding>(Parser<TypeBoundGenericStmt>{}),
552 construct<TypeBoundProcBinding>(Parser<FinalProcedureStmt>{})),
553 construct<TypeBoundProcBinding>(
554 !"END"_tok >> SkipTo<'\n'>{} >> construct<ErrorRecovery>())))
555
556 // R749 type-bound-procedure-stmt ->
557 // PROCEDURE [[, bind-attr-list] ::] type-bound-proc-decl-list |
558 // PROCEDURE ( interface-name ) , bind-attr-list :: binding-name-list
559 // The "::" is required by the standard (C768) in the first production if
560 // any type-bound-proc-decl has a "=>', but it's not strictly necessary to
561 // avoid a bad parse.
562 TYPE_CONTEXT_PARSER("type bound PROCEDURE statement"_en_US,
563 "PROCEDURE" >>
564 (construct<TypeBoundProcedureStmt>(
565 construct<TypeBoundProcedureStmt::WithInterface>(
566 parenthesized(name),
567 localRecovery("expected list of binding attributes"_err_en_US,
568 "," >> nonemptyList(Parser<BindAttr>{}), ok),
569 localRecovery("expected list of binding names"_err_en_US,
570 "::" >> listOfNames, SkipTo<'\n'>{}))) ||
571 construct<TypeBoundProcedureStmt>(construct<
572 TypeBoundProcedureStmt::WithoutInterface>(
573 pure<std::list<BindAttr>>(),
574 nonemptyList(
575 "expected type bound procedure declarations"_err_en_US,
576 construct<TypeBoundProcDecl>(name,
577 maybe(extension<LanguageFeature::MissingColons>(
578 "type-bound procedure statement should have '::' if it has '=>'"_port_en_US,
579 "=>" >> name)))))) ||
580 construct<TypeBoundProcedureStmt>(
581 construct<TypeBoundProcedureStmt::WithoutInterface>(
582 optionalListBeforeColons(Parser<BindAttr>{}),
583 nonemptyList(
584 "expected type bound procedure declarations"_err_en_US,
585 Parser<TypeBoundProcDecl>{})))))
586
587 // R750 type-bound-proc-decl -> binding-name [=> procedure-name]
588 TYPE_PARSER(construct<TypeBoundProcDecl>(name, maybe("=>" >> name)))
589
590 // R751 type-bound-generic-stmt ->
591 // GENERIC [, access-spec] :: generic-spec => binding-name-list
592 TYPE_CONTEXT_PARSER("type bound GENERIC statement"_en_US,
593 construct<TypeBoundGenericStmt>("GENERIC" >> maybe("," >> accessSpec),
594 "::" >> indirect(genericSpec), "=>" >> listOfNames))
595
596 // R752 bind-attr ->
597 // access-spec | DEFERRED | NON_OVERRIDABLE | NOPASS | PASS [(arg-name)]
598 TYPE_PARSER(construct<BindAttr>(accessSpec) ||
599 construct<BindAttr>(construct<BindAttr::Deferred>("DEFERRED"_tok)) ||
600 construct<BindAttr>(
601 construct<BindAttr::Non_Overridable>("NON_OVERRIDABLE"_tok)) ||
602 construct<BindAttr>(noPass) || construct<BindAttr>(pass))
603
604 // R753 final-procedure-stmt -> FINAL [::] final-subroutine-name-list
605 TYPE_CONTEXT_PARSER("FINAL statement"_en_US,
606 construct<FinalProcedureStmt>("FINAL" >> maybe("::"_tok) >> listOfNames))
607
608 // R754 derived-type-spec -> type-name [(type-param-spec-list)]
609 TYPE_PARSER(construct<DerivedTypeSpec>(name,
610 defaulted(parenthesized(nonemptyList(
611 "expected type parameters"_err_en_US, Parser<TypeParamSpec>{})))))
612
613 // R755 type-param-spec -> [keyword =] type-param-value
614 TYPE_PARSER(construct<TypeParamSpec>(maybe(keyword / "="), typeParamValue))
615
616 // R756 structure-constructor -> derived-type-spec ( [component-spec-list] )
617 TYPE_PARSER((construct<StructureConstructor>(derivedTypeSpec,
618 parenthesized(optionalList(Parser<ComponentSpec>{}))) ||
619 // This alternative corrects misrecognition of the
620 // component-spec-list as the type-param-spec-list in
621 // derived-type-spec.
622 construct<StructureConstructor>(
623 construct<DerivedTypeSpec>(
624 name, construct<std::list<TypeParamSpec>>()),
625 parenthesized(optionalList(Parser<ComponentSpec>{})))) /
626 !"("_tok)
627
628 // R757 component-spec -> [keyword =] component-data-source
629 TYPE_PARSER(construct<ComponentSpec>(
630 maybe(keyword / "="), Parser<ComponentDataSource>{}))
631
632 // R758 component-data-source -> expr | data-target | proc-target
633 TYPE_PARSER(construct<ComponentDataSource>(indirect(expr)))
634
635 // R759 enum-def ->
636 // enum-def-stmt enumerator-def-stmt [enumerator-def-stmt]...
637 // end-enum-stmt
638 TYPE_CONTEXT_PARSER("enum definition"_en_US,
639 construct<EnumDef>(statement(Parser<EnumDefStmt>{}),
640 some(unambiguousStatement(Parser<EnumeratorDefStmt>{})),
641 statement(Parser<EndEnumStmt>{})))
642
643 // R760 enum-def-stmt -> ENUM, BIND(C)
644 TYPE_PARSER(construct<EnumDefStmt>("ENUM , BIND ( C )"_tok))
645
646 // R761 enumerator-def-stmt -> ENUMERATOR [::] enumerator-list
647 TYPE_CONTEXT_PARSER("ENUMERATOR statement"_en_US,
648 construct<EnumeratorDefStmt>("ENUMERATOR" >> maybe("::"_tok) >>
649 nonemptyList("expected enumerators"_err_en_US, Parser<Enumerator>{})))
650
651 // R762 enumerator -> named-constant [= scalar-int-constant-expr]
652 TYPE_PARSER(
653 construct<Enumerator>(namedConstant, maybe("=" >> scalarIntConstantExpr)))
654
655 // R763 end-enum-stmt -> END ENUM
656 TYPE_PARSER(recovery("END ENUM"_tok, constructEndStmtErrorRecovery) >>
657 construct<EndEnumStmt>())
658
659 // R801 type-declaration-stmt ->
660 // declaration-type-spec [[, attr-spec]... ::] entity-decl-list
661 constexpr auto entityDeclWithoutEqInit{construct<EntityDecl>(name,
662 maybe(arraySpec), maybe(coarraySpec), maybe("*" >> charLength),
663 !"="_tok >> maybe(initialization))}; // old-style REAL A/0/ still works
664 TYPE_PARSER(
665 construct<TypeDeclarationStmt>(declarationTypeSpec,
666 defaulted("," >> nonemptyList(Parser<AttrSpec>{})) / "::",
667 nonemptyList("expected entity declarations"_err_en_US, entityDecl)) ||
668 // C806: no initializers allowed without colons ("REALA=1" is ambiguous)
669 construct<TypeDeclarationStmt>(declarationTypeSpec,
670 construct<std::list<AttrSpec>>(),
671 nonemptyList("expected entity declarations"_err_en_US,
672 entityDeclWithoutEqInit)) ||
673 // PGI-only extension: comma in place of doubled colons
674 extension<LanguageFeature::MissingColons>(
675 "nonstandard usage: ',' in place of '::'"_port_en_US,
676 construct<TypeDeclarationStmt>(declarationTypeSpec,
677 defaulted("," >> nonemptyList(Parser<AttrSpec>{})),
678 withMessage("expected entity declarations"_err_en_US,
679 "," >> nonemptyList(entityDecl)))))
680
681 // R802 attr-spec ->
682 // access-spec | ALLOCATABLE | ASYNCHRONOUS |
683 // CODIMENSION lbracket coarray-spec rbracket | CONTIGUOUS |
684 // DIMENSION ( array-spec ) | EXTERNAL | INTENT ( intent-spec ) |
685 // INTRINSIC | language-binding-spec | OPTIONAL | PARAMETER | POINTER |
686 // PROTECTED | SAVE | TARGET | VALUE | VOLATILE |
687 // CUDA-data-attr
688 TYPE_PARSER(construct<AttrSpec>(accessSpec) ||
689 construct<AttrSpec>(allocatable) ||
690 construct<AttrSpec>(construct<Asynchronous>("ASYNCHRONOUS"_tok)) ||
691 construct<AttrSpec>("CODIMENSION" >> coarraySpec) ||
692 construct<AttrSpec>(contiguous) ||
693 construct<AttrSpec>("DIMENSION" >> arraySpec) ||
694 construct<AttrSpec>(construct<External>("EXTERNAL"_tok)) ||
695 construct<AttrSpec>("INTENT" >> parenthesized(intentSpec)) ||
696 construct<AttrSpec>(construct<Intrinsic>("INTRINSIC"_tok)) ||
697 construct<AttrSpec>(languageBindingSpec) || construct<AttrSpec>(optional) ||
698 construct<AttrSpec>(construct<Parameter>("PARAMETER"_tok)) ||
699 construct<AttrSpec>(pointer) || construct<AttrSpec>(protectedAttr) ||
700 construct<AttrSpec>(save) ||
701 construct<AttrSpec>(construct<Target>("TARGET"_tok)) ||
702 construct<AttrSpec>(construct<Value>("VALUE"_tok)) ||
703 construct<AttrSpec>(construct<Volatile>("VOLATILE"_tok)) ||
704 extension<LanguageFeature::CUDA>(
705 construct<AttrSpec>(Parser<common::CUDADataAttr>{})))
706
707 // CUDA-data-attr ->
708 // CONSTANT | DEVICE | MANAGED | PINNED | SHARED | TEXTURE | UNIFIED
709 TYPE_PARSER("CONSTANT" >> pure(common::CUDADataAttr::Constant) ||
710 "DEVICE" >> pure(common::CUDADataAttr::Device) ||
711 "MANAGED" >> pure(common::CUDADataAttr::Managed) ||
712 "PINNED" >> pure(common::CUDADataAttr::Pinned) ||
713 "SHARED" >> pure(common::CUDADataAttr::Shared) ||
714 "TEXTURE" >> pure(common::CUDADataAttr::Texture) ||
715 "UNIFIED" >> pure(common::CUDADataAttr::Unified))
716
717 // R804 object-name -> name
718 constexpr auto objectName{name};
719
720 // R803 entity-decl ->
721 // object-name [( array-spec )] [lbracket coarray-spec rbracket]
722 // [* char-length] [initialization] |
723 // function-name [* char-length]
724 TYPE_PARSER(construct<EntityDecl>(objectName, maybe(arraySpec),
725 maybe(coarraySpec), maybe("*" >> charLength), maybe(initialization)))
726
727 // R806 null-init -> function-reference ... which must resolve to NULL()
728 TYPE_PARSER(lookAhead(name / "( )") >> construct<NullInit>(expr))
729
730 // R807 access-spec -> PUBLIC | PRIVATE
731 TYPE_PARSER(construct<AccessSpec>("PUBLIC" >> pure(AccessSpec::Kind::Public)) ||
732 construct<AccessSpec>("PRIVATE" >> pure(AccessSpec::Kind::Private)))
733
734 // R808 language-binding-spec ->
735 // BIND ( C [, NAME = scalar-default-char-constant-expr] )
736 // R1528 proc-language-binding-spec -> language-binding-spec
737 TYPE_PARSER(construct<LanguageBindingSpec>(
738 "BIND ( C" >> maybe(", NAME =" >> scalarDefaultCharConstantExpr),
739 (", CDEFINED" >> pure(true) || pure(false)) / ")"))
740
741 // R809 coarray-spec -> deferred-coshape-spec-list | explicit-coshape-spec
742 // N.B. Bracketed here rather than around references, for consistency with
743 // array-spec.
744 TYPE_PARSER(
745 construct<CoarraySpec>(bracketed(Parser<DeferredCoshapeSpecList>{})) ||
746 construct<CoarraySpec>(bracketed(Parser<ExplicitCoshapeSpec>{})))
747
748 // R810 deferred-coshape-spec -> :
749 // deferred-coshape-spec-list - just a list of colons
750 inline int listLength(std::list<Success> &&xs) { return xs.size(); }
751
752 TYPE_PARSER(construct<DeferredCoshapeSpecList>(
753 applyFunction(listLength, nonemptyList(":"_tok))))
754
755 // R811 explicit-coshape-spec ->
756 // [[lower-cobound :] upper-cobound ,]... [lower-cobound :] *
757 // R812 lower-cobound -> specification-expr
758 // R813 upper-cobound -> specification-expr
759 TYPE_PARSER(construct<ExplicitCoshapeSpec>(
760 many(explicitShapeSpec / ","), maybe(specificationExpr / ":") / "*"))
761
762 // R815 array-spec ->
763 // explicit-shape-spec-list | assumed-shape-spec-list |
764 // deferred-shape-spec-list | assumed-size-spec | implied-shape-spec |
765 // implied-shape-or-assumed-size-spec | assumed-rank-spec
766 // N.B. Parenthesized here rather than around references to avoid
767 // a need for forced look-ahead.
768 // Shape specs that could be deferred-shape-spec or assumed-shape-spec
769 // (e.g. '(:,:)') are parsed as the former.
770 TYPE_PARSER(
771 construct<ArraySpec>(parenthesized(nonemptyList(explicitShapeSpec))) ||
772 construct<ArraySpec>(parenthesized(deferredShapeSpecList)) ||
773 construct<ArraySpec>(
774 parenthesized(nonemptyList(Parser<AssumedShapeSpec>{}))) ||
775 construct<ArraySpec>(parenthesized(Parser<AssumedSizeSpec>{})) ||
776 construct<ArraySpec>(parenthesized(Parser<ImpliedShapeSpec>{})) ||
777 construct<ArraySpec>(parenthesized(Parser<AssumedRankSpec>{})))
778
779 // R816 explicit-shape-spec -> [lower-bound :] upper-bound
780 // R817 lower-bound -> specification-expr
781 // R818 upper-bound -> specification-expr
782 TYPE_PARSER(construct<ExplicitShapeSpec>(
783 maybe(specificationExpr / ":"), specificationExpr))
784
785 // R819 assumed-shape-spec -> [lower-bound] :
786 TYPE_PARSER(construct<AssumedShapeSpec>(maybe(specificationExpr) / ":"))
787
788 // R820 deferred-shape-spec -> :
789 // deferred-shape-spec-list - just a list of colons
790 TYPE_PARSER(construct<DeferredShapeSpecList>(
791 applyFunction(listLength, nonemptyList(":"_tok))))
792
793 // R821 assumed-implied-spec -> [lower-bound :] *
794 TYPE_PARSER(construct<AssumedImpliedSpec>(maybe(specificationExpr / ":") / "*"))
795
796 // R822 assumed-size-spec -> explicit-shape-spec-list , assumed-implied-spec
797 TYPE_PARSER(construct<AssumedSizeSpec>(
798 nonemptyList(explicitShapeSpec) / ",", assumedImpliedSpec))
799
800 // R823 implied-shape-or-assumed-size-spec -> assumed-implied-spec
801 // R824 implied-shape-spec -> assumed-implied-spec , assumed-implied-spec-list
802 // I.e., when the assumed-implied-spec-list has a single item, it constitutes an
803 // implied-shape-or-assumed-size-spec; otherwise, an implied-shape-spec.
804 TYPE_PARSER(construct<ImpliedShapeSpec>(nonemptyList(assumedImpliedSpec)))
805
806 // R825 assumed-rank-spec -> ..
807 TYPE_PARSER(construct<AssumedRankSpec>(".."_tok))
808
809 // R826 intent-spec -> IN | OUT | INOUT
810 TYPE_PARSER(construct<IntentSpec>("IN OUT" >> pure(IntentSpec::Intent::InOut) ||
811 "IN" >> pure(IntentSpec::Intent::In) ||
812 "OUT" >> pure(IntentSpec::Intent::Out)))
813
814 // R827 access-stmt -> access-spec [[::] access-id-list]
815 TYPE_PARSER(construct<AccessStmt>(accessSpec,
816 defaulted(maybe("::"_tok) >>
817 nonemptyList("expected names and generic specifications"_err_en_US,
818 Parser<AccessId>{}))))
819
820 // R828 access-id -> access-name | generic-spec
821 // "access-name" is ambiguous with "generic-spec"
822 TYPE_PARSER(construct<AccessId>(indirect(genericSpec)))
823
824 // R829 allocatable-stmt -> ALLOCATABLE [::] allocatable-decl-list
825 TYPE_PARSER(construct<AllocatableStmt>("ALLOCATABLE" >> maybe("::"_tok) >>
826 nonemptyList(
827 "expected object declarations"_err_en_US, Parser<ObjectDecl>{})))
828
829 // R830 allocatable-decl ->
830 // object-name [( array-spec )] [lbracket coarray-spec rbracket]
831 // R860 target-decl ->
832 // object-name [( array-spec )] [lbracket coarray-spec rbracket]
833 TYPE_PARSER(
834 construct<ObjectDecl>(objectName, maybe(arraySpec), maybe(coarraySpec)))
835
836 // R831 asynchronous-stmt -> ASYNCHRONOUS [::] object-name-list
837 TYPE_PARSER(construct<AsynchronousStmt>("ASYNCHRONOUS" >> maybe("::"_tok) >>
838 nonemptyList("expected object names"_err_en_US, objectName)))
839
840 // R832 bind-stmt -> language-binding-spec [::] bind-entity-list
841 TYPE_PARSER(construct<BindStmt>(languageBindingSpec / maybe("::"_tok),
842 nonemptyList("expected bind entities"_err_en_US, Parser<BindEntity>{})))
843
844 // R833 bind-entity -> entity-name | / common-block-name /
845 TYPE_PARSER(construct<BindEntity>(pure(BindEntity::Kind::Object), name) ||
846 construct<BindEntity>("/" >> pure(BindEntity::Kind::Common), name / "/"))
847
848 // R834 codimension-stmt -> CODIMENSION [::] codimension-decl-list
849 TYPE_PARSER(construct<CodimensionStmt>("CODIMENSION" >> maybe("::"_tok) >>
850 nonemptyList("expected codimension declarations"_err_en_US,
851 Parser<CodimensionDecl>{})))
852
853 // R835 codimension-decl -> coarray-name lbracket coarray-spec rbracket
854 TYPE_PARSER(construct<CodimensionDecl>(name, coarraySpec))
855
856 // R836 contiguous-stmt -> CONTIGUOUS [::] object-name-list
857 TYPE_PARSER(construct<ContiguousStmt>("CONTIGUOUS" >> maybe("::"_tok) >>
858 nonemptyList("expected object names"_err_en_US, objectName)))
859
860 // R837 data-stmt -> DATA data-stmt-set [[,] data-stmt-set]...
861 TYPE_CONTEXT_PARSER("DATA statement"_en_US,
862 construct<DataStmt>(
863 "DATA" >> nonemptySeparated(Parser<DataStmtSet>{}, maybe(","_tok))))
864
865 // R838 data-stmt-set -> data-stmt-object-list / data-stmt-value-list /
866 TYPE_PARSER(construct<DataStmtSet>(
867 nonemptyList(
868 "expected DATA statement objects"_err_en_US, Parser<DataStmtObject>{}),
869 withMessage("expected DATA statement value list"_err_en_US,
870 "/"_tok >> nonemptyList("expected DATA statement values"_err_en_US,
871 Parser<DataStmtValue>{})) /
872 "/"))
873
874 // R839 data-stmt-object -> variable | data-implied-do
875 TYPE_PARSER(construct<DataStmtObject>(indirect(variable)) ||
876 construct<DataStmtObject>(dataImpliedDo))
877
878 // R840 data-implied-do ->
879 // ( data-i-do-object-list , [integer-type-spec ::] data-i-do-variable
880 // = scalar-int-constant-expr , scalar-int-constant-expr
881 // [, scalar-int-constant-expr] )
882 // R842 data-i-do-variable -> do-variable
883 TYPE_PARSER(parenthesized(construct<DataImpliedDo>(
884 nonemptyList(Parser<DataIDoObject>{} / lookAhead(","_tok)) / ",",
885 maybe(integerTypeSpec / "::"), loopBounds(scalarIntConstantExpr))))
886
887 // R841 data-i-do-object ->
888 // array-element | scalar-structure-component | data-implied-do
889 TYPE_PARSER(construct<DataIDoObject>(scalar(indirect(designator))) ||
890 construct<DataIDoObject>(indirect(dataImpliedDo)))
891
892 // R843 data-stmt-value -> [data-stmt-repeat *] data-stmt-constant
893 TYPE_PARSER(construct<DataStmtValue>(
894 maybe(Parser<DataStmtRepeat>{} / "*"), Parser<DataStmtConstant>{}))
895
896 // R847 constant-subobject -> designator
897 // R846 int-constant-subobject -> constant-subobject
898 constexpr auto constantSubobject{constant(indirect(designator))};
899
900 // R844 data-stmt-repeat -> scalar-int-constant | scalar-int-constant-subobject
901 // R607 int-constant -> constant
902 // Factored into: constant -> literal-constant -> int-literal-constant
903 // The named-constant alternative of constant is subsumed by constant-subobject
904 TYPE_PARSER(construct<DataStmtRepeat>(intLiteralConstant) ||
905 construct<DataStmtRepeat>(scalar(integer(constantSubobject))))
906
907 // R845 data-stmt-constant ->
908 // scalar-constant | scalar-constant-subobject |
909 // signed-int-literal-constant | signed-real-literal-constant |
910 // null-init | initial-data-target |
911 // constant-structure-constructor
912 // N.B. scalar-constant and scalar-constant-subobject are ambiguous with
913 // initial-data-target; null-init and structure-constructor are ambiguous
914 // in the absence of parameters and components; structure-constructor with
915 // components can be ambiguous with a scalar-constant-subobject.
916 // So we parse literal constants, designator, null-init, and
917 // structure-constructor, so that semantics can figure things out later
918 // with the symbol table.
919 TYPE_PARSER(sourced(first(construct<DataStmtConstant>(literalConstant),
920 construct<DataStmtConstant>(signedRealLiteralConstant),
921 construct<DataStmtConstant>(signedIntLiteralConstant),
922 extension<LanguageFeature::SignedComplexLiteral>(
923 "nonstandard usage: signed COMPLEX literal"_port_en_US,
924 construct<DataStmtConstant>(Parser<SignedComplexLiteralConstant>{})),
925 construct<DataStmtConstant>(nullInit),
926 construct<DataStmtConstant>(indirect(designator) / !"("_tok),
927 construct<DataStmtConstant>(Parser<StructureConstructor>{}))))
928
929 // R848 dimension-stmt ->
930 // DIMENSION [::] array-name ( array-spec )
931 // [, array-name ( array-spec )]...
932 TYPE_CONTEXT_PARSER("DIMENSION statement"_en_US,
933 construct<DimensionStmt>("DIMENSION" >> maybe("::"_tok) >>
934 nonemptyList("expected array specifications"_err_en_US,
935 construct<DimensionStmt::Declaration>(name, arraySpec))))
936
937 // R849 intent-stmt -> INTENT ( intent-spec ) [::] dummy-arg-name-list
938 TYPE_CONTEXT_PARSER("INTENT statement"_en_US,
939 construct<IntentStmt>(
940 "INTENT" >> parenthesized(intentSpec) / maybe("::"_tok), listOfNames))
941
942 // R850 optional-stmt -> OPTIONAL [::] dummy-arg-name-list
943 TYPE_PARSER(
944 construct<OptionalStmt>("OPTIONAL" >> maybe("::"_tok) >> listOfNames))
945
946 // R851 parameter-stmt -> PARAMETER ( named-constant-def-list )
947 // Legacy extension: omitted parentheses, no implicit typing from names
948 TYPE_CONTEXT_PARSER("PARAMETER statement"_en_US,
949 construct<ParameterStmt>(
950 "PARAMETER" >> parenthesized(nonemptyList(Parser<NamedConstantDef>{}))))
951 TYPE_CONTEXT_PARSER("old style PARAMETER statement"_en_US,
952 extension<LanguageFeature::OldStyleParameter>(
953 "nonstandard usage: PARAMETER without parentheses"_port_en_US,
954 construct<OldParameterStmt>(
955 "PARAMETER" >> nonemptyList(Parser<NamedConstantDef>{}))))
956
957 // R852 named-constant-def -> named-constant = constant-expr
958 TYPE_PARSER(construct<NamedConstantDef>(namedConstant, "=" >> constantExpr))
959
960 // R853 pointer-stmt -> POINTER [::] pointer-decl-list
961 TYPE_PARSER(construct<PointerStmt>("POINTER" >> maybe("::"_tok) >>
962 nonemptyList(
963 "expected pointer declarations"_err_en_US, Parser<PointerDecl>{})))
964
965 // R854 pointer-decl ->
966 // object-name [( deferred-shape-spec-list )] | proc-entity-name
967 TYPE_PARSER(
968 construct<PointerDecl>(name, maybe(parenthesized(deferredShapeSpecList))))
969
970 // R855 protected-stmt -> PROTECTED [::] entity-name-list
971 TYPE_PARSER(
972 construct<ProtectedStmt>("PROTECTED" >> maybe("::"_tok) >> listOfNames))
973
974 // R856 save-stmt -> SAVE [[::] saved-entity-list]
975 TYPE_PARSER(construct<SaveStmt>(
976 "SAVE" >> defaulted(maybe("::"_tok) >>
977 nonemptyList("expected SAVE entities"_err_en_US,
978 Parser<SavedEntity>{}))))
979
980 // R857 saved-entity -> object-name | proc-pointer-name | / common-block-name /
981 // R858 proc-pointer-name -> name
982 TYPE_PARSER(construct<SavedEntity>(pure(SavedEntity::Kind::Entity), name) ||
983 construct<SavedEntity>("/" >> pure(SavedEntity::Kind::Common), name / "/"))
984
985 // R859 target-stmt -> TARGET [::] target-decl-list
986 TYPE_PARSER(construct<TargetStmt>("TARGET" >> maybe("::"_tok) >>
987 nonemptyList("expected objects"_err_en_US, Parser<ObjectDecl>{})))
988
989 // R861 value-stmt -> VALUE [::] dummy-arg-name-list
990 TYPE_PARSER(construct<ValueStmt>("VALUE" >> maybe("::"_tok) >> listOfNames))
991
992 // R862 volatile-stmt -> VOLATILE [::] object-name-list
993 TYPE_PARSER(construct<VolatileStmt>("VOLATILE" >> maybe("::"_tok) >>
994 nonemptyList("expected object names"_err_en_US, objectName)))
995
996 // R866 implicit-name-spec -> EXTERNAL | TYPE
997 constexpr auto implicitNameSpec{
998 "EXTERNAL" >> pure(ImplicitStmt::ImplicitNoneNameSpec::External) ||
999 "TYPE" >> pure(ImplicitStmt::ImplicitNoneNameSpec::Type)};
1000
1001 // R863 implicit-stmt ->
1002 // IMPLICIT implicit-spec-list |
1003 // IMPLICIT NONE [( [implicit-name-spec-list] )]
1004 TYPE_CONTEXT_PARSER("IMPLICIT statement"_en_US,
1005 construct<ImplicitStmt>(
1006 "IMPLICIT" >> nonemptyList("expected IMPLICIT specifications"_err_en_US,
1007 Parser<ImplicitSpec>{})) ||
1008 construct<ImplicitStmt>("IMPLICIT NONE"_sptok >>
1009 defaulted(parenthesized(optionalList(implicitNameSpec)))))
1010
1011 // R864 implicit-spec -> declaration-type-spec ( letter-spec-list )
1012 // The variant form of declarationTypeSpec is meant to avoid misrecognition
1013 // of a letter-spec as a simple parenthesized expression for kind or character
1014 // length, e.g., PARAMETER(I=5,N=1); IMPLICIT REAL(I-N)(O-Z) vs.
1015 // IMPLICIT REAL(I-N). The variant form needs to attempt to reparse only
1016 // types with optional parenthesized kind/length expressions, so derived
1017 // type specs, DOUBLE PRECISION, and DOUBLE COMPLEX need not be considered.
1018 constexpr auto noKindSelector{construct<std::optional<KindSelector>>()};
1019 constexpr auto implicitSpecDeclarationTypeSpecRetry{
1020 construct<DeclarationTypeSpec>(first(
1021 construct<IntrinsicTypeSpec>(
1022 construct<IntegerTypeSpec>("INTEGER" >> noKindSelector)),
1023 construct<IntrinsicTypeSpec>(
1024 construct<IntrinsicTypeSpec::Real>("REAL" >> noKindSelector)),
1025 construct<IntrinsicTypeSpec>(
1026 construct<IntrinsicTypeSpec::Complex>("COMPLEX" >> noKindSelector)),
1027 construct<IntrinsicTypeSpec>(construct<IntrinsicTypeSpec::Character>(
1028 "CHARACTER" >> construct<std::optional<CharSelector>>())),
1029 construct<IntrinsicTypeSpec>(construct<IntrinsicTypeSpec::Logical>(
1030 "LOGICAL" >> noKindSelector))))};
1031
1032 TYPE_PARSER(construct<ImplicitSpec>(declarationTypeSpec,
1033 parenthesized(nonemptyList(Parser<LetterSpec>{}))) ||
1034 construct<ImplicitSpec>(implicitSpecDeclarationTypeSpecRetry,
1035 parenthesized(nonemptyList(Parser<LetterSpec>{}))))
1036
1037 // R865 letter-spec -> letter [- letter]
1038 TYPE_PARSER(space >> (construct<LetterSpec>(letter, maybe("-" >> letter)) ||
1039 construct<LetterSpec>(otherIdChar,
1040 construct<std::optional<const char *>>())))
1041
1042 // R867 import-stmt ->
1043 // IMPORT [[::] import-name-list] |
1044 // IMPORT , ONLY : import-name-list | IMPORT , NONE | IMPORT , ALL
1045 TYPE_CONTEXT_PARSER("IMPORT statement"_en_US,
1046 construct<ImportStmt>(
1047 "IMPORT , ONLY :" >> pure(common::ImportKind::Only), listOfNames) ||
1048 construct<ImportStmt>(
1049 "IMPORT , NONE" >> pure(common::ImportKind::None)) ||
1050 construct<ImportStmt>(
1051 "IMPORT , ALL" >> pure(common::ImportKind::All)) ||
1052 construct<ImportStmt>(
1053 "IMPORT" >> maybe("::"_tok) >> optionalList(name)))
1054
1055 // R868 namelist-stmt ->
1056 // NAMELIST / namelist-group-name / namelist-group-object-list
1057 // [[,] / namelist-group-name / namelist-group-object-list]...
1058 // R869 namelist-group-object -> variable-name
1059 TYPE_PARSER(construct<NamelistStmt>("NAMELIST" >>
1060 nonemptySeparated(
1061 construct<NamelistStmt::Group>("/" >> name / "/", listOfNames),
1062 maybe(","_tok))))
1063
1064 // R870 equivalence-stmt -> EQUIVALENCE equivalence-set-list
1065 // R871 equivalence-set -> ( equivalence-object , equivalence-object-list )
1066 TYPE_PARSER(construct<EquivalenceStmt>("EQUIVALENCE" >>
1067 nonemptyList(
1068 parenthesized(nonemptyList("expected EQUIVALENCE objects"_err_en_US,
1069 Parser<EquivalenceObject>{})))))
1070
1071 // R872 equivalence-object -> variable-name | array-element | substring
1072 TYPE_PARSER(construct<EquivalenceObject>(indirect(designator)))
1073
1074 // R873 common-stmt ->
1075 // COMMON [/ [common-block-name] /] common-block-object-list
1076 // [[,] / [common-block-name] / common-block-object-list]...
1077 TYPE_PARSER(
1078 construct<CommonStmt>("COMMON" >> defaulted("/" >> maybe(name) / "/"),
1079 nonemptyList("expected COMMON block objects"_err_en_US,
1080 Parser<CommonBlockObject>{}),
1081 many(maybe(","_tok) >>
1082 construct<CommonStmt::Block>("/" >> maybe(name) / "/",
1083 nonemptyList("expected COMMON block objects"_err_en_US,
1084 Parser<CommonBlockObject>{})))))
1085
1086 // R874 common-block-object -> variable-name [( array-spec )]
1087 TYPE_PARSER(construct<CommonBlockObject>(name, maybe(arraySpec)))
1088
1089 // R901 designator -> object-name | array-element | array-section |
1090 // coindexed-named-object | complex-part-designator |
1091 // structure-component | substring
1092 // The Standard's productions for designator and its alternatives are
1093 // ambiguous without recourse to a symbol table. Many of the alternatives
1094 // for designator (viz., array-element, coindexed-named-object,
1095 // and structure-component) are all syntactically just data-ref.
1096 // What designator boils down to is this:
1097 // It starts with either a name or a character literal.
1098 // If it starts with a character literal, it must be a substring.
1099 // If it starts with a name, it's a sequence of %-separated parts;
1100 // each part is a name, maybe a (section-subscript-list), and
1101 // maybe an [image-selector].
1102 // If it's a substring, it ends with (substring-range).
1103 TYPE_CONTEXT_PARSER("designator"_en_US,
1104 sourced(construct<Designator>(substring) || construct<Designator>(dataRef)))
1105
1106 constexpr auto percentOrDot{"%"_tok ||
1107 // legacy VAX extension for RECORD field access
1108 extension<LanguageFeature::DECStructures>(
1109 "nonstandard usage: component access with '.' in place of '%'"_port_en_US,
1110 "."_tok / lookAhead(OldStructureComponentName{}))};
1111
1112 // R902 variable -> designator | function-reference
1113 // This production appears to be left-recursive in the grammar via
1114 // function-reference -> procedure-designator -> proc-component-ref ->
1115 // scalar-variable
1116 // and would be so if we were to allow functions to be called via procedure
1117 // pointer components within derived type results of other function references
1118 // (a reasonable extension, esp. in the case of procedure pointer components
1119 // that are NOPASS). However, Fortran constrains the use of a variable in a
1120 // proc-component-ref to be a data-ref without coindices (C1027).
1121 // Some array element references will be misrecognized as function references.
1122 constexpr auto noMoreAddressing{!"("_tok >> !"["_tok >> !percentOrDot};
1123 TYPE_CONTEXT_PARSER("variable"_en_US,
1124 construct<Variable>(indirect(functionReference / noMoreAddressing)) ||
1125 construct<Variable>(indirect(designator)))
1126
1127 // R908 substring -> parent-string ( substring-range )
1128 // R909 parent-string ->
1129 // scalar-variable-name | array-element | coindexed-named-object |
1130 // scalar-structure-component | scalar-char-literal-constant |
1131 // scalar-named-constant
1132 TYPE_PARSER(
1133 construct<Substring>(dataRef, parenthesized(Parser<SubstringRange>{})))
1134
1135 TYPE_PARSER(construct<CharLiteralConstantSubstring>(
1136 charLiteralConstant, parenthesized(Parser<SubstringRange>{})))
1137
1138 TYPE_PARSER(sourced(construct<SubstringInquiry>(Parser<Substring>{}) /
1139 ("%LEN"_tok || "%KIND"_tok)))
1140
1141 // R910 substring-range -> [scalar-int-expr] : [scalar-int-expr]
1142 TYPE_PARSER(construct<SubstringRange>(
1143 maybe(scalarIntExpr), ":" >> maybe(scalarIntExpr)))
1144
1145 // R911 data-ref -> part-ref [% part-ref]...
1146 // R914 coindexed-named-object -> data-ref
1147 // R917 array-element -> data-ref
1148 TYPE_PARSER(
1149 construct<DataRef>(nonemptySeparated(Parser<PartRef>{}, percentOrDot)))
1150
1151 // R912 part-ref -> part-name [( section-subscript-list )] [image-selector]
1152 TYPE_PARSER(construct<PartRef>(name,
1153 defaulted(
1154 parenthesized(nonemptyList(Parser<SectionSubscript>{})) / !"=>"_tok),
1155 maybe(Parser<ImageSelector>{})))
1156
1157 // R913 structure-component -> data-ref
1158 // The final part-ref in the data-ref is not allowed to have subscripts.
1159 TYPE_CONTEXT_PARSER("component"_en_US,
1160 construct<StructureComponent>(
1161 construct<DataRef>(some(Parser<PartRef>{} / percentOrDot)), name))
1162
1163 // R919 subscript -> scalar-int-expr
1164 constexpr auto subscript{scalarIntExpr};
1165
1166 // R920 section-subscript -> subscript | subscript-triplet | vector-subscript
1167 // R923 vector-subscript -> int-expr
1168 // N.B. The distinction that needs to be made between "subscript" and
1169 // "vector-subscript" is deferred to semantic analysis.
1170 TYPE_PARSER(construct<SectionSubscript>(Parser<SubscriptTriplet>{}) ||
1171 construct<SectionSubscript>(intExpr))
1172
1173 // R921 subscript-triplet -> [subscript] : [subscript] [: stride]
1174 TYPE_PARSER(construct<SubscriptTriplet>(
1175 maybe(subscript), ":" >> maybe(subscript), maybe(":" >> subscript)))
1176
1177 // R925 cosubscript -> scalar-int-expr
1178 constexpr auto cosubscript{scalarIntExpr};
1179
1180 // R924 image-selector ->
1181 // lbracket cosubscript-list [, image-selector-spec-list] rbracket
1182 TYPE_CONTEXT_PARSER("image selector"_en_US,
1183 construct<ImageSelector>(
1184 "[" >> nonemptyList(cosubscript / lookAhead(space / ",]"_ch)),
1185 defaulted("," >> nonemptyList(Parser<ImageSelectorSpec>{})) / "]"))
1186
1187 // R926 image-selector-spec ->
1188 // STAT = stat-variable | TEAM = team-value |
1189 // TEAM_NUMBER = scalar-int-expr
1190 TYPE_PARSER(construct<ImageSelectorSpec>(construct<ImageSelectorSpec::Stat>(
1191 "STAT =" >> scalar(integer(indirect(variable))))) ||
1192 construct<ImageSelectorSpec>(construct<TeamValue>("TEAM =" >> teamValue)) ||
1193 construct<ImageSelectorSpec>(construct<ImageSelectorSpec::Team_Number>(
1194 "TEAM_NUMBER =" >> scalarIntExpr)))
1195
1196 // R927 allocate-stmt ->
1197 // ALLOCATE ( [type-spec ::] allocation-list [, alloc-opt-list] )
1198 TYPE_CONTEXT_PARSER("ALLOCATE statement"_en_US,
1199 construct<AllocateStmt>("ALLOCATE (" >> maybe(typeSpec / "::"),
1200 nonemptyList(Parser<Allocation>{}),
1201 defaulted("," >> nonemptyList(Parser<AllocOpt>{})) / ")"))
1202
1203 // R928 alloc-opt ->
1204 // ERRMSG = errmsg-variable | MOLD = source-expr |
1205 // SOURCE = source-expr | STAT = stat-variable |
1206 // (CUDA) STREAM = scalar-int-expr
1207 // PINNED = scalar-logical-variable
1208 // R931 source-expr -> expr
1209 TYPE_PARSER(construct<AllocOpt>(
1210 construct<AllocOpt::Mold>("MOLD =" >> indirect(expr))) ||
1211 construct<AllocOpt>(
1212 construct<AllocOpt::Source>("SOURCE =" >> indirect(expr))) ||
1213 construct<AllocOpt>(statOrErrmsg) ||
1214 extension<LanguageFeature::CUDA>(
1215 construct<AllocOpt>(construct<AllocOpt::Stream>(
1216 "STREAM =" >> indirect(scalarIntExpr))) ||
1217 construct<AllocOpt>(construct<AllocOpt::Pinned>(
1218 "PINNED =" >> indirect(scalarLogicalVariable)))))
1219
1220 // R929 stat-variable -> scalar-int-variable
1221 TYPE_PARSER(construct<StatVariable>(scalar(integer(variable))))
1222
1223 // R932 allocation ->
1224 // allocate-object [( allocate-shape-spec-list )]
1225 // [lbracket allocate-coarray-spec rbracket]
1226 TYPE_PARSER(construct<Allocation>(Parser<AllocateObject>{},
1227 defaulted(parenthesized(nonemptyList(Parser<AllocateShapeSpec>{}))),
1228 maybe(bracketed(Parser<AllocateCoarraySpec>{}))))
1229
1230 // R933 allocate-object -> variable-name | structure-component
1231 TYPE_PARSER(construct<AllocateObject>(structureComponent) ||
1232 construct<AllocateObject>(name / !"="_tok))
1233
1234 // R934 allocate-shape-spec -> [lower-bound-expr :] upper-bound-expr
1235 // R938 allocate-coshape-spec -> [lower-bound-expr :] upper-bound-expr
1236 TYPE_PARSER(construct<AllocateShapeSpec>(maybe(boundExpr / ":"), boundExpr))
1237
1238 // R937 allocate-coarray-spec ->
1239 // [allocate-coshape-spec-list ,] [lower-bound-expr :] *
1240 TYPE_PARSER(construct<AllocateCoarraySpec>(
1241 defaulted(nonemptyList(Parser<AllocateShapeSpec>{}) / ","),
1242 maybe(boundExpr / ":") / "*"))
1243
1244 // R939 nullify-stmt -> NULLIFY ( pointer-object-list )
1245 TYPE_CONTEXT_PARSER("NULLIFY statement"_en_US,
1246 "NULLIFY" >> parenthesized(construct<NullifyStmt>(
1247 nonemptyList(Parser<PointerObject>{}))))
1248
1249 // R940 pointer-object ->
1250 // variable-name | structure-component | proc-pointer-name
1251 TYPE_PARSER(construct<PointerObject>(structureComponent) ||
1252 construct<PointerObject>(name))
1253
1254 // R941 deallocate-stmt ->
1255 // DEALLOCATE ( allocate-object-list [, dealloc-opt-list] )
1256 TYPE_CONTEXT_PARSER("DEALLOCATE statement"_en_US,
1257 construct<DeallocateStmt>(
1258 "DEALLOCATE (" >> nonemptyList(Parser<AllocateObject>{}),
1259 defaulted("," >> nonemptyList(statOrErrmsg)) / ")"))
1260
1261 // R942 dealloc-opt -> STAT = stat-variable | ERRMSG = errmsg-variable
1262 // R1165 sync-stat -> STAT = stat-variable | ERRMSG = errmsg-variable
1263 TYPE_PARSER(construct<StatOrErrmsg>("STAT =" >> statVariable) ||
1264 construct<StatOrErrmsg>("ERRMSG =" >> msgVariable))
1265
1266 // Directives, extensions, and deprecated statements
1267 // !DIR$ IGNORE_TKR [ [(tkrdmac...)] name ]...
1268 // !DIR$ LOOP COUNT (n1[, n2]...)
1269 // !DIR$ name[=value] [, name[=value]]...
1270 // !DIR$ <anything else>
1271 constexpr auto ignore_tkr{
1272 "IGNORE_TKR" >> optionalList(construct<CompilerDirective::IgnoreTKR>(
1273 maybe(parenthesized(many(letter))), name))};
1274 constexpr auto loopCount{
1275 "LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
1276 parenthesized(nonemptyList(digitString64)))};
1277 constexpr auto assumeAligned{"ASSUME_ALIGNED" >>
1278 optionalList(construct<CompilerDirective::AssumeAligned>(
1279 indirect(designator), ":"_tok >> digitString64))};
1280 constexpr auto vectorAlways{
1281 "VECTOR ALWAYS" >> construct<CompilerDirective::VectorAlways>()};
1282 TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
1283 sourced((construct<CompilerDirective>(ignore_tkr) ||
1284 construct<CompilerDirective>(loopCount) ||
1285 construct<CompilerDirective>(assumeAligned) ||
1286 construct<CompilerDirective>(vectorAlways) ||
1287 construct<CompilerDirective>(
1288 many(construct<CompilerDirective::NameValue>(
1289 name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
1290 endOfStmt ||
1291 construct<CompilerDirective>(pure<CompilerDirective::Unrecognized>()) /
1292 SkipTo<'\n'>{}))
1293
1294 TYPE_PARSER(extension<LanguageFeature::CrayPointer>(
1295 "nonstandard usage: based POINTER"_port_en_US,
1296 construct<BasedPointerStmt>(
1297 "POINTER" >> nonemptyList("expected POINTER associations"_err_en_US,
1298 construct<BasedPointer>("(" >> objectName / ",",
1299 objectName, maybe(Parser<ArraySpec>{}) / ")")))))
1300
1301 // CUDA-attributes-stmt -> ATTRIBUTES (CUDA-data-attr) [::] name-list
1302 TYPE_PARSER(extension<LanguageFeature::CUDA>(construct<CUDAAttributesStmt>(
1303 "ATTRIBUTES" >> parenthesized(Parser<common::CUDADataAttr>{}),
1304 defaulted(
1305 maybe("::"_tok) >> nonemptyList("expected names"_err_en_US, name)))))
1306
1307 // Subtle: A structure's name includes the surrounding slashes, which avoids
1308 // clashes with other uses of the name in the same scope.
1309 constexpr auto structureName{maybe(sourced("/" >> name / "/"))};
1310
1311 // Note that Parser<StructureStmt>{} has a mandatory list of entity-decls
1312 // and is used only by NestedStructureStmt{}.Parse() in user-state.cpp.
1313 TYPE_PARSER(construct<StructureStmt>("STRUCTURE" >> structureName,
1314 localRecovery(
1315 "entity declarations are required on a nested structure"_err_en_US,
1316 nonemptyList(entityDecl), ok)))
1317
1318 constexpr auto nestedStructureDef{
1319 CONTEXT_PARSER("nested STRUCTURE definition"_en_US,
1320 construct<StructureDef>(statement(NestedStructureStmt{}),
1321 many(Parser<StructureField>{}),
1322 statement(construct<StructureDef::EndStructureStmt>(
1323 "END STRUCTURE"_tok))))};
1324
1325 TYPE_PARSER(construct<StructureField>(statement(StructureComponents{})) ||
1326 construct<StructureField>(indirect(Parser<Union>{})) ||
1327 construct<StructureField>(indirect(nestedStructureDef)))
1328
1329 TYPE_CONTEXT_PARSER("STRUCTURE definition"_en_US,
1330 extension<LanguageFeature::DECStructures>(
1331 "nonstandard usage: STRUCTURE"_port_en_US,
1332 construct<StructureDef>(
1333 statement(construct<StructureStmt>(
1334 "STRUCTURE" >> structureName, optionalList(entityDecl))),
1335 many(Parser<StructureField>{}),
1336 statement(construct<StructureDef::EndStructureStmt>(
1337 "END STRUCTURE"_tok)))))
1338
1339 TYPE_CONTEXT_PARSER("UNION definition"_en_US,
1340 construct<Union>(statement(construct<Union::UnionStmt>("UNION"_tok)),
1341 many(Parser<Map>{}),
1342 statement(construct<Union::EndUnionStmt>("END UNION"_tok))))
1343
1344 TYPE_CONTEXT_PARSER("MAP definition"_en_US,
1345 construct<Map>(statement(construct<Map::MapStmt>("MAP"_tok)),
1346 many(Parser<StructureField>{}),
1347 statement(construct<Map::EndMapStmt>("END MAP"_tok))))
1348
1349 TYPE_CONTEXT_PARSER("arithmetic IF statement"_en_US,
1350 deprecated<LanguageFeature::ArithmeticIF>(construct<ArithmeticIfStmt>(
1351 "IF" >> parenthesized(expr), label / ",", label / ",", label)))
1352
1353 TYPE_CONTEXT_PARSER("ASSIGN statement"_en_US,
1354 deprecated<LanguageFeature::Assign>(
1355 construct<AssignStmt>("ASSIGN" >> label, "TO" >> name)))
1356
1357 TYPE_CONTEXT_PARSER("assigned GOTO statement"_en_US,
1358 deprecated<LanguageFeature::AssignedGOTO>(construct<AssignedGotoStmt>(
1359 "GO TO" >> name,
1360 defaulted(maybe(","_tok) >>
1361 parenthesized(nonemptyList("expected labels"_err_en_US, label))))))
1362
1363 TYPE_CONTEXT_PARSER("PAUSE statement"_en_US,
1364 deprecated<LanguageFeature::Pause>(
1365 construct<PauseStmt>("PAUSE" >> maybe(Parser<StopCode>{}))))
1366
1367 // These requirement productions are defined by the Fortran standard but never
1368 // used directly by the grammar:
1369 // R620 delimiter -> ( | ) | / | [ | ] | (/ | /)
1370 // R1027 numeric-expr -> expr
1371 // R1031 int-constant-expr -> int-expr
1372 // R1221 dtv-type-spec -> TYPE ( derived-type-spec ) |
1373 // CLASS ( derived-type-spec )
1374 //
1375 // These requirement productions are defined and used, but need not be
1376 // defined independently here in this file:
1377 // R771 lbracket -> [
1378 // R772 rbracket -> ]
1379 //
1380 // Further note that:
1381 // R607 int-constant -> constant
1382 // is used only once via R844 scalar-int-constant
1383 // R904 logical-variable -> variable
1384 // is used only via scalar-logical-variable
1385 // R906 default-char-variable -> variable
1386 // is used only via scalar-default-char-variable
1387 // R907 int-variable -> variable
1388 // is used only via scalar-int-variable
1389 // R915 complex-part-designator -> designator % RE | designator % IM
1390 // %RE and %IM are initially recognized as structure components
1391 // R916 type-param-inquiry -> designator % type-param-name
1392 // is occulted by structure component designators
1393 // R918 array-section ->
1394 // data-ref [( substring-range )] | complex-part-designator
1395 // is not used because parsing is not sensitive to rank
1396 // R1030 default-char-constant-expr -> default-char-expr
1397 // is only used via scalar-default-char-constant-expr
1398 } // namespace Fortran::parser