2021-09-02 16:14:01 +08:00
|
|
|
//===-- runtime/namelist.cpp ----------------------------------------------===//
|
2021-05-06 02:37:49 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "namelist.h"
|
|
|
|
#include "descriptor-io.h"
|
|
|
|
#include "io-stmt.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/io-api.h"
|
2021-10-22 05:13:21 +08:00
|
|
|
#include <algorithm>
|
2021-05-06 02:37:49 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2021-06-18 04:13:19 +08:00
|
|
|
// Max size of a group, symbol or component identifier that can appear in
|
|
|
|
// NAMELIST input, plus a byte for NUL termination.
|
|
|
|
static constexpr std::size_t nameBufferSize{201};
|
|
|
|
|
2022-01-20 08:25:41 +08:00
|
|
|
static inline char32_t GetComma(IoStatementState &io) {
|
|
|
|
return io.mutableModes().editingFlags & decimalComma ? char32_t{';'}
|
|
|
|
: char32_t{','};
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
bool IONAME(OutputNamelist)(Cookie cookie, const NamelistGroup &group) {
|
|
|
|
IoStatementState &io{*cookie};
|
|
|
|
io.CheckFormattedStmtType<Direction::Output>("OutputNamelist");
|
2022-01-28 04:09:07 +08:00
|
|
|
io.mutableModes().inNamelist = true;
|
2022-01-20 08:25:41 +08:00
|
|
|
char comma{static_cast<char>(GetComma(io))};
|
2022-01-28 04:09:07 +08:00
|
|
|
ConnectionState &connection{io.GetConnectionState()};
|
2021-05-06 02:37:49 +08:00
|
|
|
// Internal functions to advance records and convert case
|
|
|
|
const auto EmitWithAdvance{[&](char ch) -> bool {
|
|
|
|
return (!connection.NeedAdvance(1) || io.AdvanceRecord()) &&
|
|
|
|
io.Emit(&ch, 1);
|
|
|
|
}};
|
|
|
|
const auto EmitUpperCase{[&](const char *str) -> bool {
|
|
|
|
if (connection.NeedAdvance(std::strlen(str)) &&
|
|
|
|
!(io.AdvanceRecord() && io.Emit(" ", 1))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; *str; ++str) {
|
|
|
|
char up{*str >= 'a' && *str <= 'z' ? static_cast<char>(*str - 'a' + 'A')
|
|
|
|
: *str};
|
|
|
|
if (!io.Emit(&up, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
// &GROUP
|
|
|
|
if (!(EmitWithAdvance('&') && EmitUpperCase(group.groupName))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (std::size_t j{0}; j < group.items; ++j) {
|
|
|
|
// [,]ITEM=...
|
|
|
|
const NamelistGroup::Item &item{group.item[j]};
|
2022-01-20 08:25:41 +08:00
|
|
|
if (!(EmitWithAdvance(j == 0 ? ' ' : comma) && EmitUpperCase(item.name) &&
|
2021-05-06 02:37:49 +08:00
|
|
|
EmitWithAdvance('=') &&
|
|
|
|
descr::DescriptorIO<Direction::Output>(io, item.descriptor))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// terminal /
|
|
|
|
return EmitWithAdvance('/');
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:13:19 +08:00
|
|
|
static constexpr bool IsLegalIdStart(char32_t ch) {
|
|
|
|
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_' ||
|
|
|
|
ch == '@' || ch == '$';
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr bool IsLegalIdChar(char32_t ch) {
|
|
|
|
return IsLegalIdStart(ch) || (ch >= '0' && ch <= '9');
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr char NormalizeIdChar(char32_t ch) {
|
|
|
|
return static_cast<char>(ch >= 'A' && ch <= 'Z' ? ch - 'A' + 'a' : ch);
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
static bool GetLowerCaseName(
|
|
|
|
IoStatementState &io, char buffer[], std::size_t maxLength) {
|
2021-06-18 04:13:19 +08:00
|
|
|
if (auto ch{io.GetNextNonBlank()}) {
|
2021-05-06 02:37:49 +08:00
|
|
|
if (IsLegalIdStart(*ch)) {
|
|
|
|
std::size_t j{0};
|
|
|
|
do {
|
2021-06-18 04:13:19 +08:00
|
|
|
buffer[j] = NormalizeIdChar(*ch);
|
2021-05-06 02:37:49 +08:00
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
2021-06-18 04:13:19 +08:00
|
|
|
} while (++j < maxLength && ch && IsLegalIdChar(*ch));
|
2021-05-06 02:37:49 +08:00
|
|
|
buffer[j++] = '\0';
|
|
|
|
if (j <= maxLength) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
io.GetIoErrorHandler().SignalError(
|
|
|
|
"Identifier '%s...' in NAMELIST input group is too long", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::optional<SubscriptValue> GetSubscriptValue(IoStatementState &io) {
|
|
|
|
std::optional<SubscriptValue> value;
|
|
|
|
std::optional<char32_t> ch{io.GetCurrentChar()};
|
|
|
|
bool negate{ch && *ch == '-'};
|
2022-01-13 07:10:20 +08:00
|
|
|
if ((ch && *ch == '+') || negate) {
|
2021-05-06 02:37:49 +08:00
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
|
|
|
}
|
|
|
|
bool overflow{false};
|
|
|
|
while (ch && *ch >= '0' && *ch <= '9') {
|
|
|
|
SubscriptValue was{value.value_or(0)};
|
|
|
|
overflow |= was >= std::numeric_limits<SubscriptValue>::max() / 10;
|
|
|
|
value = 10 * was + *ch - '0';
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
|
|
|
}
|
|
|
|
if (overflow) {
|
|
|
|
io.GetIoErrorHandler().SignalError(
|
|
|
|
"NAMELIST input subscript value overflow");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
if (negate) {
|
|
|
|
if (value) {
|
|
|
|
return -*value;
|
|
|
|
} else {
|
|
|
|
io.HandleRelativePosition(-1); // give back '-' with no digits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool HandleSubscripts(IoStatementState &io, Descriptor &desc,
|
|
|
|
const Descriptor &source, const char *name) {
|
|
|
|
IoErrorHandler &handler{io.GetIoErrorHandler()};
|
|
|
|
io.HandleRelativePosition(1); // skip '('
|
2021-06-18 04:13:19 +08:00
|
|
|
// Allow for blanks in subscripts; they're nonstandard, but not
|
|
|
|
// ambiguous within the parentheses.
|
2021-05-06 02:37:49 +08:00
|
|
|
SubscriptValue lower[maxRank], upper[maxRank], stride[maxRank];
|
|
|
|
int j{0};
|
2021-10-22 05:13:21 +08:00
|
|
|
std::size_t contiguousStride{source.ElementBytes()};
|
2021-05-06 02:37:49 +08:00
|
|
|
bool ok{true};
|
|
|
|
std::optional<char32_t> ch{io.GetNextNonBlank()};
|
2022-01-20 08:25:41 +08:00
|
|
|
char32_t comma{GetComma(io)};
|
2021-05-06 02:37:49 +08:00
|
|
|
for (; ch && *ch != ')'; ++j) {
|
|
|
|
SubscriptValue dimLower{0}, dimUpper{0}, dimStride{0};
|
|
|
|
if (j < maxRank && j < source.rank()) {
|
|
|
|
const Dimension &dim{source.GetDimension(j)};
|
|
|
|
dimLower = dim.LowerBound();
|
|
|
|
dimUpper = dim.UpperBound();
|
2021-10-22 05:13:21 +08:00
|
|
|
dimStride =
|
|
|
|
dim.ByteStride() / std::max<SubscriptValue>(contiguousStride, 1);
|
|
|
|
contiguousStride *= dim.Extent();
|
2021-05-06 02:37:49 +08:00
|
|
|
} else if (ok) {
|
|
|
|
handler.SignalError(
|
|
|
|
"Too many subscripts for rank-%d NAMELIST group item '%s'",
|
|
|
|
source.rank(), name);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
if (auto low{GetSubscriptValue(io)}) {
|
|
|
|
if (*low < dimLower || (dimUpper >= dimLower && *low > dimUpper)) {
|
|
|
|
if (ok) {
|
|
|
|
handler.SignalError("Subscript %jd out of range %jd..%jd in NAMELIST "
|
|
|
|
"group item '%s' dimension %d",
|
|
|
|
static_cast<std::intmax_t>(*low),
|
|
|
|
static_cast<std::intmax_t>(dimLower),
|
|
|
|
static_cast<std::intmax_t>(dimUpper), name, j + 1);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dimLower = *low;
|
|
|
|
}
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
if (ch && *ch == ':') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
if (auto high{GetSubscriptValue(io)}) {
|
|
|
|
if (*high > dimUpper) {
|
|
|
|
if (ok) {
|
|
|
|
handler.SignalError(
|
|
|
|
"Subscript triplet upper bound %jd out of range (>%jd) in "
|
|
|
|
"NAMELIST group item '%s' dimension %d",
|
|
|
|
static_cast<std::intmax_t>(*high),
|
|
|
|
static_cast<std::intmax_t>(dimUpper), name, j + 1);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dimUpper = *high;
|
|
|
|
}
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
if (ch && *ch == ':') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
if (auto str{GetSubscriptValue(io)}) {
|
|
|
|
dimStride = *str;
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // scalar
|
|
|
|
dimUpper = dimLower;
|
|
|
|
dimStride = 0;
|
|
|
|
}
|
2022-01-20 08:25:41 +08:00
|
|
|
if (ch && *ch == comma) {
|
2021-05-06 02:37:49 +08:00
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
lower[j] = dimLower;
|
|
|
|
upper[j] = dimUpper;
|
|
|
|
stride[j] = dimStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
if (ch && *ch == ')') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (desc.EstablishPointerSection(source, lower, upper, stride)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
handler.SignalError(
|
|
|
|
"Bad subscripts for NAMELIST input group item '%s'", name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handler.SignalError(
|
|
|
|
"Bad subscripts (missing ')') for NAMELIST input group item '%s'",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-08 05:39:28 +08:00
|
|
|
static bool HandleSubstring(
|
|
|
|
IoStatementState &io, Descriptor &desc, const char *name) {
|
|
|
|
IoErrorHandler &handler{io.GetIoErrorHandler()};
|
|
|
|
auto pair{desc.type().GetCategoryAndKind()};
|
|
|
|
if (!pair || pair->first != TypeCategory::Character) {
|
|
|
|
handler.SignalError("Substring reference to non-character item '%s'", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int kind{pair->second};
|
|
|
|
SubscriptValue chars{static_cast<SubscriptValue>(desc.ElementBytes()) / kind};
|
|
|
|
// Allow for blanks in substring bounds; they're nonstandard, but not
|
|
|
|
// ambiguous within the parentheses.
|
|
|
|
io.HandleRelativePosition(1); // skip '('
|
|
|
|
std::optional<SubscriptValue> lower, upper;
|
|
|
|
std::optional<char32_t> ch{io.GetNextNonBlank()};
|
|
|
|
if (ch) {
|
|
|
|
if (*ch == ':') {
|
|
|
|
lower = 1;
|
|
|
|
} else {
|
|
|
|
lower = GetSubscriptValue(io);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch && ch == ':') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
if (ch) {
|
|
|
|
if (*ch == ')') {
|
|
|
|
upper = chars;
|
|
|
|
} else {
|
|
|
|
upper = GetSubscriptValue(io);
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch && *ch == ')') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (lower && upper) {
|
|
|
|
if (*lower > *upper) {
|
|
|
|
// An empty substring, whatever the values are
|
|
|
|
desc.raw().elem_len = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (*lower >= 1 || *upper <= chars) {
|
|
|
|
// Offset the base address & adjust the element byte length
|
|
|
|
desc.raw().elem_len = (*upper - *lower + 1) * kind;
|
|
|
|
desc.set_base_addr(reinterpret_cast<void *>(
|
|
|
|
reinterpret_cast<char *>(desc.raw().base_addr) +
|
|
|
|
kind * (*lower - 1)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler.SignalError(
|
|
|
|
"Bad substring bounds for NAMELIST input group item '%s'", name);
|
|
|
|
} else {
|
|
|
|
handler.SignalError(
|
|
|
|
"Bad substring (missing ')') for NAMELIST input group item '%s'", name);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:13:19 +08:00
|
|
|
static bool HandleComponent(IoStatementState &io, Descriptor &desc,
|
|
|
|
const Descriptor &source, const char *name) {
|
|
|
|
IoErrorHandler &handler{io.GetIoErrorHandler()};
|
|
|
|
io.HandleRelativePosition(1); // skip '%'
|
|
|
|
char compName[nameBufferSize];
|
|
|
|
if (GetLowerCaseName(io, compName, sizeof compName)) {
|
|
|
|
const DescriptorAddendum *addendum{source.Addendum()};
|
|
|
|
if (const typeInfo::DerivedType *
|
|
|
|
type{addendum ? addendum->derivedType() : nullptr}) {
|
|
|
|
if (const typeInfo::Component *
|
|
|
|
comp{type->FindDataComponent(compName, std::strlen(compName))}) {
|
2021-10-21 07:01:52 +08:00
|
|
|
comp->CreatePointerDescriptor(desc, source, handler);
|
2021-06-18 04:13:19 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
handler.SignalError(
|
|
|
|
"NAMELIST component reference '%%%s' of input group item %s is not "
|
|
|
|
"a component of its derived type",
|
|
|
|
compName, name);
|
|
|
|
}
|
2021-10-21 07:01:52 +08:00
|
|
|
} else if (source.type().IsDerived()) {
|
|
|
|
handler.Crash("Derived type object '%s' in NAMELIST is missing its "
|
|
|
|
"derived type information!",
|
|
|
|
name);
|
2021-06-18 04:13:19 +08:00
|
|
|
} else {
|
|
|
|
handler.SignalError("NAMELIST component reference '%%%s' of input group "
|
|
|
|
"item %s for non-derived type",
|
|
|
|
compName, name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handler.SignalError("NAMELIST component reference of input group item %s "
|
|
|
|
"has no name after '%'",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-13 09:34:52 +08:00
|
|
|
// Advance to the terminal '/' of a namelist group.
|
|
|
|
static void SkipNamelistGroup(IoStatementState &io) {
|
|
|
|
while (auto ch{io.GetNextNonBlank()}) {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (*ch == '/') {
|
|
|
|
break;
|
|
|
|
} else if (*ch == '\'' || *ch == '"') {
|
|
|
|
// Skip quoted character literal
|
|
|
|
char32_t quote{*ch};
|
|
|
|
while (true) {
|
|
|
|
if ((ch = io.GetCurrentChar())) {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (*ch == quote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!io.AdvanceRecord()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
|
|
|
|
IoStatementState &io{*cookie};
|
|
|
|
io.CheckFormattedStmtType<Direction::Input>("InputNamelist");
|
2022-01-28 04:09:07 +08:00
|
|
|
io.mutableModes().inNamelist = true;
|
2021-05-06 02:37:49 +08:00
|
|
|
IoErrorHandler &handler{io.GetIoErrorHandler()};
|
2021-07-22 04:07:04 +08:00
|
|
|
auto *listInput{io.get_if<ListDirectedStatementState<Direction::Input>>()};
|
|
|
|
RUNTIME_CHECK(handler, listInput != nullptr);
|
2022-01-13 09:34:52 +08:00
|
|
|
// Find this namelist group's header in the input
|
2021-08-14 02:57:58 +08:00
|
|
|
io.BeginReadingRecord();
|
2022-01-13 09:34:52 +08:00
|
|
|
std::optional<char32_t> next;
|
2021-06-18 04:13:19 +08:00
|
|
|
char name[nameBufferSize];
|
2021-05-06 02:37:49 +08:00
|
|
|
RUNTIME_CHECK(handler, group.groupName != nullptr);
|
2022-01-20 08:25:41 +08:00
|
|
|
char32_t comma{GetComma(io)};
|
2022-01-13 09:34:52 +08:00
|
|
|
while (true) {
|
|
|
|
next = io.GetNextNonBlank();
|
|
|
|
while (next && *next != '&') {
|
|
|
|
// Extension: comment lines without ! before namelist groups
|
|
|
|
if (!io.AdvanceRecord()) {
|
|
|
|
next.reset();
|
|
|
|
} else {
|
|
|
|
next = io.GetNextNonBlank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!next || *next != '&') {
|
|
|
|
handler.SignalError(
|
|
|
|
"NAMELIST input group does not begin with '&' (at '%lc')", *next);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (!GetLowerCaseName(io, name, sizeof name)) {
|
|
|
|
handler.SignalError("NAMELIST input group has no name");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (std::strcmp(group.groupName, name) == 0) {
|
|
|
|
break; // found it
|
|
|
|
}
|
|
|
|
SkipNamelistGroup(io);
|
2021-05-06 02:37:49 +08:00
|
|
|
}
|
|
|
|
// Read the group's items
|
|
|
|
while (true) {
|
|
|
|
next = io.GetNextNonBlank();
|
|
|
|
if (!next || *next == '/') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!GetLowerCaseName(io, name, sizeof name)) {
|
|
|
|
handler.SignalError(
|
2022-01-20 08:25:41 +08:00
|
|
|
"NAMELIST input group '%s' was not terminated at '%c'",
|
|
|
|
group.groupName, static_cast<char>(*next));
|
2021-05-06 02:37:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::size_t itemIndex{0};
|
|
|
|
for (; itemIndex < group.items; ++itemIndex) {
|
|
|
|
if (std::strcmp(name, group.item[itemIndex].name) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (itemIndex >= group.items) {
|
|
|
|
handler.SignalError(
|
|
|
|
"'%s' is not an item in NAMELIST group '%s'", name, group.groupName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Handle indexing and components, if any. No spaces are allowed.
|
|
|
|
// A copy of the descriptor is made if necessary.
|
|
|
|
const Descriptor &itemDescriptor{group.item[itemIndex].descriptor};
|
|
|
|
const Descriptor *useDescriptor{&itemDescriptor};
|
|
|
|
StaticDescriptor<maxRank, true, 16> staticDesc[2];
|
|
|
|
int whichStaticDesc{0};
|
|
|
|
next = io.GetCurrentChar();
|
2022-01-08 05:39:28 +08:00
|
|
|
bool hadSubscripts{false};
|
|
|
|
bool hadSubstring{false};
|
2021-05-06 02:37:49 +08:00
|
|
|
if (next && (*next == '(' || *next == '%')) {
|
|
|
|
do {
|
2021-06-18 04:13:19 +08:00
|
|
|
Descriptor &mutableDescriptor{staticDesc[whichStaticDesc].descriptor()};
|
|
|
|
whichStaticDesc ^= 1;
|
2021-05-06 02:37:49 +08:00
|
|
|
if (*next == '(') {
|
2022-01-08 05:39:28 +08:00
|
|
|
if (!hadSubstring && (hadSubscripts || useDescriptor->rank() == 0)) {
|
|
|
|
mutableDescriptor = *useDescriptor;
|
|
|
|
mutableDescriptor.raw().attribute = CFI_attribute_pointer;
|
|
|
|
if (!HandleSubstring(io, mutableDescriptor, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hadSubstring = true;
|
|
|
|
} else if (hadSubscripts) {
|
|
|
|
handler.SignalError("Multiple sets of subscripts for item '%s' in "
|
|
|
|
"NAMELIST group '%s'",
|
|
|
|
name, group.groupName);
|
|
|
|
return false;
|
|
|
|
} else if (!HandleSubscripts(
|
|
|
|
io, mutableDescriptor, *useDescriptor, name)) {
|
2021-10-21 07:01:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-08 05:39:28 +08:00
|
|
|
hadSubscripts = true;
|
2021-05-06 02:37:49 +08:00
|
|
|
} else {
|
2021-10-21 07:01:52 +08:00
|
|
|
if (!HandleComponent(io, mutableDescriptor, *useDescriptor, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-08 05:39:28 +08:00
|
|
|
hadSubscripts = false;
|
|
|
|
hadSubstring = false;
|
2021-05-06 02:37:49 +08:00
|
|
|
}
|
2021-06-18 04:13:19 +08:00
|
|
|
useDescriptor = &mutableDescriptor;
|
2021-05-06 02:37:49 +08:00
|
|
|
next = io.GetCurrentChar();
|
|
|
|
} while (next && (*next == '(' || *next == '%'));
|
|
|
|
}
|
|
|
|
// Skip the '='
|
|
|
|
next = io.GetNextNonBlank();
|
|
|
|
if (!next || *next != '=') {
|
|
|
|
handler.SignalError("No '=' found after item '%s' in NAMELIST group '%s'",
|
|
|
|
name, group.groupName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
io.HandleRelativePosition(1);
|
2021-10-21 04:56:47 +08:00
|
|
|
// Read the values into the descriptor. An array can be short.
|
2021-07-22 04:07:04 +08:00
|
|
|
listInput->ResetForNextNamelistItem();
|
2021-05-06 02:37:49 +08:00
|
|
|
if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
next = io.GetNextNonBlank();
|
2022-01-20 08:25:41 +08:00
|
|
|
if (next && *next == comma) {
|
2021-05-06 02:37:49 +08:00
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!next || *next != '/') {
|
|
|
|
handler.SignalError(
|
|
|
|
"No '/' found after NAMELIST group '%s'", group.groupName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-21 04:56:47 +08:00
|
|
|
bool IsNamelistName(IoStatementState &io) {
|
|
|
|
if (io.get_if<ListDirectedStatementState<Direction::Input>>()) {
|
2022-01-28 04:09:07 +08:00
|
|
|
if (io.mutableModes().inNamelist) {
|
2021-11-04 06:33:29 +08:00
|
|
|
SavedPosition savedPosition{io};
|
2021-10-21 04:56:47 +08:00
|
|
|
if (auto ch{io.GetNextNonBlank()}) {
|
|
|
|
if (IsLegalIdStart(*ch)) {
|
|
|
|
do {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
ch = io.GetCurrentChar();
|
|
|
|
} while (ch && IsLegalIdChar(*ch));
|
|
|
|
ch = io.GetNextNonBlank();
|
|
|
|
// TODO: how to deal with NaN(...) ambiguity?
|
2021-11-04 06:33:29 +08:00
|
|
|
return ch && (*ch == '=' || *ch == '(' || *ch == '%');
|
2021-10-21 04:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:37:49 +08:00
|
|
|
} // namespace Fortran::runtime::io
|