Skip to content

Commit 30c79a0

Browse files
committed
Require whitespace before binary *, &, ~, closes #152
Before this I had been trying out making `a*b` work without whitespace by making `a****b` interpret the final `*` as binary if followed by something that wouldn't make sense if it was unary, but that's a bit subtle. (And similarly for `&` and should also include `~`) See additional discussion in #152.
1 parent 9a319fd commit 30c79a0

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

regression-tests/mixed-test-parens.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ constexpr int a = 1;
77

88
main: () -> int = {
99
v : std::vector<int> = ( 1, 2, 3 );
10-
std::cout << (1+2)*(3+v[0]);
10+
std::cout << (1+2) * (3+v[0]);
1111
f<(1>2)>(3,4);
1212
f< a+a >(5,6);
1313
}

source/parse.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,9 +1375,9 @@ class parser
13751375
errors.emplace_back( curr().position(), m );
13761376
}
13771377

1378-
auto error(std::string const& msg, bool = true) const -> void
1378+
auto error(std::string const& msg, bool include_curr_token = true) const -> void
13791379
{
1380-
error(msg.c_str());
1380+
error(msg.c_str(), include_curr_token);
13811381
}
13821382

13831383

@@ -1536,11 +1536,23 @@ class parser
15361536
curr().type() == lexeme::Dot
15371537
)
15381538
{
1539-
// * and & can't be a unary operator if followed by a (, identifier, or literal
1540-
if ((curr().type() == lexeme::Multiply || curr().type() == lexeme::Ampersand) &&
1539+
// these can't be unary operators if followed by a (, identifier, or literal
1540+
if ((curr().type() == lexeme::Multiply || curr().type() == lexeme::Ampersand || curr().type() == lexeme::Tilde) &&
15411541
peek(1) &&
15421542
(peek(1)->type() == lexeme::LeftParen || peek(1)->type() == lexeme::Identifier || is_literal(peek(1)->type())))
15431543
{
1544+
auto op = curr().to_string(true);
1545+
auto msg = "postfix unary " + op;
1546+
if (curr().type() == lexeme::Multiply ) { op += " (dereference)" ; }
1547+
else if (curr().type() == lexeme::Ampersand) { op += " (address-of)" ; }
1548+
else if (curr().type() == lexeme::Tilde ) { op += " (unary bit-complement)"; }
1549+
msg += " cannot be immediately followed by a (, identifier, or literal - add whitespace before "
1550+
+ op + " here if you meant binary " + op;
1551+
if (curr().type() == lexeme::Multiply ) { op += " (multiplication)" ; }
1552+
else if (curr().type() == lexeme::Ampersand) { op += " (bitwise and)" ; }
1553+
else if (curr().type() == lexeme::Tilde ) { op += " (binarybit-complement)"; }
1554+
1555+
error(msg, false);
15441556
break;
15451557
}
15461558

0 commit comments

Comments
 (0)