| 1 |
//===-- lib/Parser/misc-parsers.h -------------------------------*- C++ -*-===// |
| 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 |
// Parser templates and constexpr parsers shared by multiple |
| 10 |
// per-type parser implementation source files. |
| 11 |
|
| 12 |
#ifndef FORTRAN_PARSER_MISC_PARSERS_H_ |
| 13 |
#define FORTRAN_PARSER_MISC_PARSERS_H_ |
| 14 |
|
| 15 |
#include "basic-parsers.h" |
| 16 |
#include "token-parsers.h" |
| 17 |
#include "type-parsers.h" |
| 18 |
#include "flang/Parser/message.h" |
| 19 |
#include "flang/Parser/parse-tree.h" |
| 20 |
|
| 21 |
namespace Fortran::parser { |
| 22 |
|
| 23 |
// R401 xzy-list -> xzy [, xzy]... |
| 24 |
template <typename PA> inline constexpr auto nonemptyList(const PA &p) { |
| 25 |
return nonemptySeparated(p, ","_tok); // p-list |
| 26 |
} |
| 27 |
|
| 28 |
template <typename PA> |
| 29 |
inline constexpr auto nonemptyList(MessageFixedText error, const PA &p) { |
| 30 |
return withMessage(error, nonemptySeparated(p, ","_tok)); // p-list |
| 31 |
} |
| 32 |
|
| 33 |
template <typename PA> inline constexpr auto optionalList(const PA &p) { |
| 34 |
return defaulted(nonemptySeparated(p, ","_tok)); // [p-list] |
| 35 |
} |
| 36 |
|
| 37 |
// R402 xzy-name -> name |
| 38 |
|
| 39 |
// R516 keyword -> name |
| 40 |
constexpr auto keyword{construct<Keyword>(name)}; |
| 41 |
|
| 42 |
// R1101 block -> [execution-part-construct]... |
| 43 |
constexpr auto block{many(executionPartConstruct)}; |
| 44 |
|
| 45 |
constexpr auto listOfNames{nonemptyList("expected names"_err_en_US, name)}; |
| 46 |
|
| 47 |
constexpr auto star{construct<Star>("*"_tok)}; |
| 48 |
constexpr auto allocatable{construct<Allocatable>("ALLOCATABLE"_tok)}; |
| 49 |
constexpr auto contiguous{construct<Contiguous>("CONTIGUOUS"_tok)}; |
| 50 |
constexpr auto optional{construct<Optional>("OPTIONAL"_tok)}; |
| 51 |
constexpr auto pointer{construct<Pointer>("POINTER"_tok)}; |
| 52 |
constexpr auto protectedAttr{construct<Protected>("PROTECTED"_tok)}; |
| 53 |
constexpr auto save{construct<Save>("SAVE"_tok)}; |
| 54 |
|
| 55 |
template <typename A> common::IfNoLvalue<std::list<A>, A> singletonList(A &&x) { |
| 56 |
std::list<A> result; |
| 57 |
result.emplace_back(std::move(x)); |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
template <typename A> |
| 62 |
common::IfNoLvalue<std::optional<A>, A> presentOptional(A &&x) { |
| 63 |
return std::make_optional(std::move(x)); |
| 64 |
} |
| 65 |
} // namespace Fortran::parser |
| 66 |
#endif |