Skip to content

Commit

Permalink
Remove whitespace sensitivity for * and &
Browse files Browse the repository at this point in the history
Undo commit 30c79a0

Updates comment on partly-related #152
Closes #319
Closes #989
  • Loading branch information
hsutter committed Mar 21, 2024
1 parent 38efdc7 commit 5663493
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
6 changes: 5 additions & 1 deletion regression-tests/mixed-test-parens.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ auto f(auto, auto) -> void { }
constexpr int a = 1;

main: () -> int = {
p := new<int>(11);
std::cout << p * << "\n";

v : std::vector<int> = ( 1, 2, 3 );
std::cout << (1+2) * (3+v[0]);
std::cout << (1+2)*(3+v[0]);
std::cout << "\n13*14 is (13*14)$\n";
f<(1>2)>(3,4);
f< a+a >(5,6);
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
12
11
12
13*14 is 182
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
12
11
12
13*14 is 182
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
12
11
12
13*14 is 182
4 changes: 4 additions & 0 deletions regression-tests/test-results/mixed-test-parens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ constexpr int a = 1;

#line 8 "mixed-test-parens.cpp2"
[[nodiscard]] auto main() -> int{
auto p {cpp2_new<int>(11)};
std::cout << *cpp2::impl::assert_not_null(cpp2::move(p)) << "\n";

std::vector<int> v {1, 2, 3};
std::cout << (1 + 2) * (3 + CPP2_ASSERT_IN_BOUNDS_LITERAL(cpp2::move(v), 0));
std::cout << ("\n13*14 is " + cpp2::to_string(13 * 14) + "\n");
f<(cpp2::impl::cmp_greater(1,2))>(3, 4);
f<a + a>(5, 6);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
12
11
12
13*14 is 182
23 changes: 3 additions & 20 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5771,7 +5771,7 @@ class parser

//G postfix-expression:
//G primary-expression
//G postfix-expression postfix-operator // Note: without whitespace before the operator
//G postfix-expression postfix-operator
//G postfix-expression '[' expression-list? ','? ']'
//G postfix-expression '(' expression-list? ','? ')'
//G postfix-expression '.' id-expression
Expand All @@ -5788,23 +5788,18 @@ class parser
while (
!done()
&& (
(is_postfix_operator(curr().type())
// Postfix operators must be lexically adjacent
&& curr().position().lineno == peek(-1)->position().lineno
&& curr().position().colno == peek(-1)->position().colno + peek(-1)->length()
)
is_postfix_operator(curr().type())
|| curr().type() == lexeme::LeftBracket
|| curr().type() == lexeme::LeftParen
|| curr().type() == lexeme::Dot
)
)
{
// these can't be unary operators if followed by a (, identifier, or literal
// * and & can't be unary operators if followed by a (, identifier, or literal
if (
(
curr().type() == lexeme::Multiply
|| curr().type() == lexeme::Ampersand
|| curr().type() == lexeme::Tilde
)
&& peek(1)
&& (
Expand All @@ -5814,18 +5809,6 @@ class parser
)
)
{
auto op = curr().to_string();
auto msg = "postfix unary " + op;
if (curr().type() == lexeme::Multiply ) { msg += " (dereference)" ; }
else if (curr().type() == lexeme::Ampersand) { msg += " (address-of)" ; }
else if (curr().type() == lexeme::Tilde ) { msg += " (unary bit-complement)" ; }
msg += " cannot be immediately followed by a (, identifier, or literal - add whitespace before "
+ op + " here if you meant binary " + op;
if (curr().type() == lexeme::Multiply ) { msg += " (multiplication)" ; }
else if (curr().type() == lexeme::Ampersand) { msg += " (bitwise and)" ; }
else if (curr().type() == lexeme::Tilde ) { msg += " (binary bit-complement)"; }

error(msg, false);
break;
}

Expand Down

0 comments on commit 5663493

Please sign in to comment.