Skip to content

Commit

Permalink
remove support for in the interpolated expression sequence by popular…
Browse files Browse the repository at this point in the history
… demaand
  • Loading branch information
adamdruppe committed Nov 14, 2023
1 parent 52c960e commit d758ccb
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 47 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dmd/astbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -4910,12 +4910,12 @@ struct ASTBase

extern (C++) final class InterpExp : Expression
{
InterpolatedSet interpolatedSet;
InterpolatedSet* interpolatedSet;
char postfix = 0; // 'c', 'w', 'd'

extern (D) this(const ref Loc loc, InterpolatedSet* interpolatedSet, char postfix = 0)
{
super(loc, EXP.interpolatedSet, __traits(classInstanceSize, InterpExp));
super(loc, EXP.interpolated, __traits(classInstanceSize, InterpExp));
this.interpolatedSet = interpolatedSet;
this.postfix = postfix;
}
Expand Down
5 changes: 0 additions & 5 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -1900,11 +1900,6 @@ extern (C++) final class InterpExp : Expression
this.postfix = postfix;
}

static InterpExp create(const ref Loc loc, InterpolatedSet* set)
{
return new InterpExp(loc, set);
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down
6 changes: 1 addition & 5 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,8 @@ class InterpExp final : public Expression
OwnedBy ownedByCtfe;
void* interpolatedSet;

static InterpExp *create(const Loc &loc, void* set);

void accept(Visitor* v) override { v->visit(this); }
}


};

// Tuple

Expand Down
20 changes: 0 additions & 20 deletions compiler/src/dmd/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -1846,28 +1846,8 @@ class Lexer
// otherwise returns false, indicating to treat it as just part of a normal string
private bool handleInterpolatedSegment(Token* token, Loc start)
{
bool wasSpecial = true;
switch(*p)
{
case '_':
case 'a': .. case 'z':
case 'A': .. case 'Z':

// always put the string part in first
token.appendInterpolatedPart(stringbuffer);
stringbuffer.setsize(0);

// identifier, scan it with the lexer to follow all rules
auto pstart = p;
Token tok;
scan(&tok);

// then put the interpolated string segment
token.appendInterpolatedPart(pstart[0 .. p - pstart]);

stringbuffer.setsize(0);

return true;
case '(':
// expression, at this level we need to scan until the closing ')'

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ nothrow:
*/
void setString(const(char)* ptr, size_t length)
{
assert(value == TOK.string_);
value = TOK.string_;
auto s = cast(char*)mem.xmalloc_noscan(length + 1);
memcpy(s, ptr, length);
s[length] = 0;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ struct Token
{
utf8_t *ustring; // UTF8 string
void *interpolatedSet;
}
};
unsigned len;
unsigned char postfix; // 'c', 'w', 'd'
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* TEST_OUTPUT:
---
interpolatedexpressionsequence_postfix.d(10): Error: String postfixes on interpolated expression sequences are not allowed.
interpolatedexpressionsequence_postfix.d(11): Error: String postfixes on interpolated expression sequences are not allowed.
interpolatedexpressionsequence_postfix.d(12): Error: String postfixes on interpolated expression sequences are not allowed.
fail_compilation/interpolatedexpressionsequence_postfix.d(10): Error: String postfixes on interpolated expression sequences are not allowed.
fail_compilation/interpolatedexpressionsequence_postfix.d(11): Error: String postfixes on interpolated expression sequences are not allowed.
fail_compilation/interpolatedexpressionsequence_postfix.d(12): Error: String postfixes on interpolated expression sequences are not allowed.
---
*/
void main() {
Expand Down
10 changes: 6 additions & 4 deletions compiler/test/runnable/interpolatedexpressionsequence.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
string b = "one";
// parser won't permit alias = i".." directly; i"..." is meant to
// be used as a function/template parameter at this time.
alias expr = AliasSeq!i"$a $(b)";
alias expr = AliasSeq!i"$(a) $(b)";
// elements from the source code are available at compile time, so
// we static assert those, but the values, of course, are different
static assert(expr[0] == InterpolationHeader());
Expand All @@ -38,12 +38,14 @@ void main() {
// it does currently allow `auto` to be used, it creates a value tuple
// you can embed any D expressions inside the parenthesis, and the
// token is not ended until you get the *outer* ) and ".
auto thing = i"$b $("$" ~ ')' ~ `"`)";
auto thing = i"$(b) $("$" ~ ')' ~ `"`)";
assert(simpleToString(thing) == "one $)\"");
assert(simpleToString(i"$b") == "$b"); // support for $ident removed by popular demand
// i`` and iq{} should also work
assert(simpleToString(i` $b is $(b)!`) == " one is one!");
assert(simpleToString(iq{ $b is $(b)!}) == " one is one!");
assert(simpleToString(i` $(b) is $(b)!`) == " one is one!");
assert(simpleToString(iq{ $(b) is $(b)!}) == " one is one!");
assert(simpleToString(i`\$('$')`) == "\\$"); // no \ escape there
assert(simpleToString(iq{{$('$')}}) == "{$}"); // {} needs to work
}
1 change: 1 addition & 0 deletions compiler/test/unit/lexer/location_offset.d
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ enum ignoreTokens
showCtfeContext,
objcClassReference,
vectorArray,
interpolated,

wchar_tLiteral,
endOfLine,
Expand Down
10 changes: 4 additions & 6 deletions druntime/src/core/interpolation.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
int num;
// the compiler uses this module to implement the
// i"..." literal used here.
auto a = i"$str has $num items.";
auto a = i"$(str) has $(num) items.";
---
The variable `a` is a sequence of expressions:
Expand All @@ -33,7 +33,7 @@
In the example:
---
auto a = i"$(str) has $num items.";
auto a = i"$(str) has $(num) items.";
---
We will find:
Expand All @@ -50,11 +50,9 @@
---
You can see the correspondence with the original
input: when you write `$(expression)`, or the shorthand
equivalent when you only want an expression consisting of
a single identifier, `$identifier`, the string of the
input: when you write `$(expression)`, the string of the
code is passed as `InterpolatedExpression!ThatString`,
(excluding any parenthesis around the expression, if present),
(excluding any parenthesis around the expression),
and everything else is passed as `InterpolatedLiteral!str`,
in the same sequence as they appeared in the source.
Expand Down

0 comments on commit d758ccb

Please sign in to comment.