[flang-commits] [flang] [flang] Implement conditional expressions parser/semantics (F2023) (PR #186489)
Caroline Newcombe via flang-commits
flang-commits at lists.llvm.org
Mon Apr 6 08:26:06 PDT 2026
================
@@ -61,6 +61,39 @@ TYPE_PARSER(parenthesized(
TYPE_PARSER(construct<AcImpliedDoControl>(
maybe(integerTypeSpec / "::"), loopBounds(scalarIntExpr)))
+// Conditional expression lookahead helper: checks if input starting with '('
+// contains '?' at nesting level 1. This avoids exponential backtracking when
+// parsing deeply nested parentheses that are not conditional expressions.
+struct ConditionalExprLookahead {
+ using resultType = Success;
+ constexpr ConditionalExprLookahead() {}
+ std::optional<Success> Parse(ParseState &state) const {
+ ParseState scan{state};
+ if (!attempt("("_tok).Parse(scan)) {
+ return std::nullopt;
+ }
+ int nestLevel{1};
+ while (!scan.IsAtEnd()) {
+ if (attempt(charLiteralConstant).Parse(scan)) {
+ // Skip character literals; don't check contents.
+ } else if (attempt("("_tok).Parse(scan)) {
+ ++nestLevel;
+ } else if (attempt(")"_tok).Parse(scan)) {
+ if (--nestLevel == 0) {
+ return std::nullopt;
+ }
+ } else if (attempt("?"_tok).Parse(scan)) {
+ if (nestLevel == 1) {
+ return {Success{}};
+ }
+ } else {
+ scan.UncheckedAdvance();
+ }
+ }
+ return std::nullopt;
+ }
+};
+
// R1001 primary ->
// literal-constant | designator | array-constructor |
----------------
cenewcombe wrote:
Done
https://github.com/llvm/llvm-project/pull/186489
More information about the flang-commits
mailing list