[flang-commits] [flang] [flang] Implement conditional expressions parser/semantics (F2023) (PR #186489)
via flang-commits
flang-commits at lists.llvm.org
Tue Mar 31 06:29:50 PDT 2026
================
@@ -61,6 +61,34 @@ 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 {
+ if (std::optional<const char *> at{state.PeekAtNextChar()}) {
+ if (**at != '(') {
+ return std::nullopt;
+ }
+ const char *const start{*at};
+ const char *const limit{start + state.BytesRemaining()};
+ int nestLevel{0};
+ for (const char *p{start}; p < limit; ++p) {
+ if (*p == '(') {
+ ++nestLevel;
+ } else if (*p == ')' && --nestLevel == 0) {
----------------
jeanPerier wrote:
What if there are parentheses inside character literals?
```
integer function test(c)
character(*) :: c
test = (c.eq.")" ? 1 : 2)
end function
```
https://github.com/llvm/llvm-project/pull/186489
More information about the flang-commits
mailing list