FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Finished string interpolation v1 · feiyunwill/cppfront@f742798 · GitHub

Commit f742798

Browse files
committed
Finished string interpolation v1
Improved `*`/`~`/`&` disambiguation Add `to_string` fallback "no interpolation exists, customize me" message
1 parent 6f7e642 commit f742798

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎include/cpp2util.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ auto to_string(std::optional<T> const& o) -> std::string {
549549
return "(empty)";
550550
}
551551

552+
auto to_string(...) -> std::string {
553+
return "(customize me - no cpp2::to_string overload exists for this type)";
554+
}
555+
552556

553557
}
554558

‎source/cppfront.cpp‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@ class cppfront
890890
// Scan back to find the matching (
891891
auto paren_depth = 1;
892892
auto open = pos - 2;
893+
894+
// "next" in the string is the "last" one encountered in the backwards scan
895+
auto last_nonwhitespace = '\0';
896+
893897
for( ; text[open] != '"'; --open)
894898
{
895899
if (text[open] == ')') {
@@ -902,7 +906,7 @@ class cppfront
902906
}
903907
}
904908
else if (
905-
(text[open] == '-' && text[open - 1] == '-') ||
909+
(text[open] == '+' && text[open - 1] == '+') ||
906910
(text[open] == '-' && text[open - 1] == '-')
907911
)
908912
{
@@ -913,17 +917,21 @@ class cppfront
913917
return "";
914918
}
915919
else if (
916-
(text[open] == '*' && !isspace(text[open - 1])) ||
917-
(text[open] == '&' && !isspace(text[open - 1])) ||
918-
(text[open] == '!' && !isspace(text[open - 1]))
920+
(text[open] == '*' || text[open] == '&' || text[open] == '~') &&
921+
!isspace(text[open - 1]) &&
922+
!isalnum(last_nonwhitespace) && last_nonwhitespace != '('
919923
)
920924
{
921925
errors.emplace_back(
922926
source_position( n.position().lineno, n.position().colno + pos ),
923-
"a string interpolation expression may not contain unary *, &, or ~ (if you meant binary, add a space before the operator)"
927+
"a string interpolation expression may not contain unary & * or ~"
924928
);
925929
return "";
926930
}
931+
932+
if (!std::isspace(text[open])) {
933+
last_nonwhitespace = text[open];
934+
}
927935
}
928936
if (text[open] == '"')
929937
{

‎source/parse.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,13 @@ class parser
13011301
curr().type() == lexeme::Dot
13021302
)
13031303
{
1304+
// * & and ~ can't be a unary operator if followed by ( or identifier
1305+
if ((curr().type() == lexeme::Multiply || curr().type() == lexeme::Ampersand || curr().type() == lexeme::Tilde) &&
1306+
(peek(1)->type() == lexeme::LeftParen || peek(1)->type() == lexeme::Identifier))
1307+
{
1308+
break;
1309+
}
1310+
13041311
if (curr().type() == lexeme::Dollar) {
13051312
// cap_grp must not already be set, or this is a multi-$ postfix-expression
13061313
if (n->cap_grp) {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL