[flang] Fix parsing of WRITE(I+J) with more accurate look-ahead

The parsing of I/O units uses look-ahead to discriminate between
keywords, variables and expressions as part of distinguishing internal
from external I/O.  The look-ahead was inaccurate for variables that
appear as the initial parts of expressions.

Differential Revision: https://reviews.llvm.org/D95743

GitOrigin-RevId: f0ffc690d5bcb3d8c373cbe63ac9a2a3a10deee8
diff --git a/lib/Parser/Fortran-parsers.cpp b/lib/Parser/Fortran-parsers.cpp
index a21acd0..de59b26 100644
--- a/lib/Parser/Fortran-parsers.cpp
+++ b/lib/Parser/Fortran-parsers.cpp
@@ -1084,7 +1084,8 @@
 // R924 image-selector ->
 //        lbracket cosubscript-list [, image-selector-spec-list] rbracket
 TYPE_CONTEXT_PARSER("image selector"_en_US,
-    construct<ImageSelector>("[" >> nonemptyList(cosubscript / !"="_tok),
+    construct<ImageSelector>(
+        "[" >> nonemptyList(cosubscript / lookAhead(space / ",]"_ch)),
         defaulted("," >> nonemptyList(Parser<ImageSelectorSpec>{})) / "]"))
 
 // R926 image-selector-spec ->
diff --git a/lib/Parser/io-parsers.cpp b/lib/Parser/io-parsers.cpp
index 6294e5c..a4cf697 100644
--- a/lib/Parser/io-parsers.cpp
+++ b/lib/Parser/io-parsers.cpp
@@ -24,7 +24,7 @@
 // R905 char-variable -> variable
 // "char-variable" is attempted first since it's not type constrained but
 // syntactically ambiguous with "file-unit-number", which is constrained.
-TYPE_PARSER(construct<IoUnit>(variable / !"="_tok) ||
+TYPE_PARSER(construct<IoUnit>(variable / lookAhead(space / ",);\n"_ch)) ||
     construct<IoUnit>(fileUnitNumber) || construct<IoUnit>(star))
 
 // R1202 file-unit-number -> scalar-int-expr