Skip to content

Commit

Permalink
Fix primary-expression ( suffix parsing bug, closes #860
Browse files Browse the repository at this point in the history
If a primary-expression is followed by `(` but -not- a parameter-list and `)`, then don't treat it as part of the primary-expression, it's something else (likely a local statement/block parameter list)
  • Loading branch information
hsutter committed Dec 18, 2023
1 parent 479a345 commit 759c3b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.3.0 Build 8C17:1133
cppfront compiler v0.3.0 Build 8C17:1632
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8C17:1133"
"8C17:1632"
28 changes: 19 additions & 9 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5686,6 +5686,9 @@ class parser
n->cap_grp->add(n.get());
}

// Remember current position, in case we need to backtrack
auto term_pos = pos;

auto term = postfix_expression_node::term{&curr()};
next();

Expand All @@ -5708,18 +5711,25 @@ class parser
}
else if (term.op->type() == lexeme::LeftParen)
{
// Next should be an expression-list followed by a ')'
// If not, then this wasn't a call expression so backtrack to
// the '(' which will be part of the next grammar production

term.expr_list = expression_list(term.op);
if (!term.expr_list) {
error("( is not followed by a valid expression list");
return {};
if (
term.expr_list
&& curr().type() == lexeme::RightParen
)
{
term.expr_list->close_paren = &curr();
term.op_close = &curr();
next();
}
if (curr().type() != lexeme::RightParen) {
error("unexpected text - ( is not properly matched by )", true, {}, true);
return {};
else
{
pos = term_pos; // backtrack
break;
}
term.expr_list->close_paren = &curr();
term.op_close = &curr();
next();
}
else if (term.op->type() == lexeme::Dot)
{
Expand Down

0 comments on commit 759c3b3

Please sign in to comment.