Skip to content

[flang][runtime] Extension: NAMELIST input may omit terminal '/' #76476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ end
* When a file included via an `INCLUDE` line or `#include` directive
has a continuation marker at the end of its last line in free form,
Fortran line continuation works.
* A `NAMELIST` input group may omit its trailing `/` character if
it is followed by another `NAMELIST` input group.
* A `NAMELIST` input group may begin with either `&` or `$`.

### Extensions supported when enabled by options

Expand Down
7 changes: 6 additions & 1 deletion flang/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace Fortran::runtime::io {
static inline bool IsCharValueSeparator(const DataEdit &edit, char32_t ch) {
char32_t comma{
edit.modes.editingFlags & decimalComma ? char32_t{';'} : char32_t{','}};
return ch == ' ' || ch == '\t' || ch == '/' || ch == comma;
return ch == ' ' || ch == '\t' || ch == comma || ch == '/' ||
(edit.IsNamelist() && (ch == '&' || ch == '$'));
}

static bool CheckCompleteListDirectedField(
Expand Down Expand Up @@ -917,6 +918,10 @@ static bool EditListDirectedCharacterInput(
case '/':
isSep = true;
break;
case '&':
case '$':
isSep = edit.IsNamelist();
break;
case ',':
isSep = !(edit.modes.editingFlags & decimalComma);
break;
Expand Down
6 changes: 6 additions & 0 deletions flang/runtime/io-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ std::optional<char32_t> IoStatementState::NextInField(
case '*':
case '\n': // for stream access
return std::nullopt;
case '&':
case '$':
if (edit.IsNamelist()) {
return std::nullopt;
}
break;
case ',':
if (!(edit.modes.editingFlags & decimalComma)) {
return std::nullopt;
Expand Down
25 changes: 15 additions & 10 deletions flang/runtime/namelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool IONAME(OutputNamelist)(Cookie cookie, const NamelistGroup &group) {

static constexpr bool IsLegalIdStart(char32_t ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_' ||
ch == '@' || ch == '$';
ch == '@';
}

static constexpr bool IsLegalIdChar(char32_t ch) {
Expand Down Expand Up @@ -378,12 +378,13 @@ static bool HandleComponent(IoStatementState &io, Descriptor &desc,
return false;
}

// Advance to the terminal '/' of a namelist group.
// Advance to the terminal '/' of a namelist group or leading '&'/'$'
// of the next.
static void SkipNamelistGroup(IoStatementState &io) {
std::size_t byteCount{0};
while (auto ch{io.GetNextNonBlank(byteCount)}) {
io.HandleRelativePosition(byteCount);
if (*ch == '/') {
if (*ch == '/' || *ch == '&' || *ch == '$') {
break;
} else if (*ch == '\'' || *ch == '"') {
// Skip quoted character literal
Expand Down Expand Up @@ -418,7 +419,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
std::size_t byteCount{0};
while (true) {
next = io.GetNextNonBlank(byteCount);
while (next && *next != '&') {
while (next && *next != '&' && *next != '$') {
// Extension: comment lines without ! before namelist groups
if (!io.AdvanceRecord()) {
next.reset();
Expand All @@ -430,9 +431,10 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
handler.SignalEnd();
return false;
}
if (*next != '&') {
if (*next != '&' && *next != '$') {
handler.SignalError(
"NAMELIST input group does not begin with '&' (at '%lc')", *next);
"NAMELIST input group does not begin with '&' or '$' (at '%lc')",
*next);
return false;
}
io.HandleRelativePosition(byteCount);
Expand All @@ -448,7 +450,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
// Read the group's items
while (true) {
next = io.GetNextNonBlank(byteCount);
if (!next || *next == '/') {
if (!next || *next == '/' || *next == '&' || *next == '$') {
break;
}
if (!GetLowerCaseName(io, name, sizeof name)) {
Expand Down Expand Up @@ -540,12 +542,15 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
io.HandleRelativePosition(byteCount);
}
}
if (!next || *next != '/') {
if (next && *next == '/') {
io.HandleRelativePosition(byteCount);
} else if (*next && (*next == '&' || *next == '$')) {
// stop at beginning of next group
} else {
handler.SignalError(
"No '/' found after NAMELIST group '%s'", group.groupName);
return false;
}
io.HandleRelativePosition(byteCount);
return true;
}

Expand All @@ -565,7 +570,7 @@ bool IsNamelistNameOrSlash(IoStatementState &io) {
// TODO: how to deal with NaN(...) ambiguity?
return ch && (*ch == '=' || *ch == '(' || *ch == '%');
} else {
return *ch == '/';
return *ch == '/' || *ch == '&' || *ch == '$';
}
}
}
Expand Down