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