Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpolated strings, v2 #510

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 129 additions & 2 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
typeMap[typeid(TypeofExpression)] = 46;
typeMap[typeid(UnaryExpression)] = 47;
typeMap[typeid(XorExpression)] = 48;
typeMap[typeid(InterpolatedStringExpression)] = 49;
typeMap[typeid(InterpolatedStringText)] = 50;
typeMap[typeid(InterpolatedStringVariable)] = 51;
}

/// Describes which syntax was used in a list of declarations in the containing AST node
Expand Down Expand Up @@ -167,6 +170,19 @@
case 46: visit(cast(TypeofExpression) n); break;
case 47: visit(cast(UnaryExpression) n); break;
case 48: visit(cast(XorExpression) n); break;
// skip 49, 50, 51 (used for InterpolatedStringPart)
default: assert(false, __MODULE__ ~ " has a bug");
}
}

/// ditto
void dynamicDispatch(const InterpolatedStringPart n)
{
switch (typeMap.get(typeid(n), 0))
{
case 49: visit(cast(InterpolatedStringExpression) n); break;
case 50: visit(cast(InterpolatedStringText) n); break;
case 51: visit(cast(InterpolatedStringVariable) n); break;
default: assert(false, __MODULE__ ~ " has a bug");
}
}
Expand Down Expand Up @@ -289,6 +305,10 @@
/** */ void visit(const Initialize initialize) { initialize.accept(this); }
/** */ void visit(const Initializer initializer) { initializer.accept(this); }
/** */ void visit(const InterfaceDeclaration interfaceDeclaration) { interfaceDeclaration.accept(this); }
/** */ void visit(const InterpolatedString interpolatedString) { interpolatedString.accept(this); }
/** */ void visit(const InterpolatedStringExpression interpolatedStringExpression) { interpolatedStringExpression.accept(this); }
/** */ void visit(const InterpolatedStringText interpolatedStringText) { interpolatedStringText.accept(this); }
/** */ void visit(const InterpolatedStringVariable interpolatedStringVariable) { interpolatedStringVariable.accept(this); }
/** */ void visit(const Invariant invariant_) { invariant_.accept(this); }
/** */ void visit(const IsExpression isExpression) { isExpression.accept(this); }
/** */ void visit(const KeyValuePair keyValuePair) { keyValuePair.accept(this); }
Expand Down Expand Up @@ -426,7 +446,7 @@
}
}

mixin template OpEquals(bool print = false)
private mixin template OpEquals(extraFields...)
{
override bool opEquals(Object other) const
{
Expand All @@ -443,6 +463,9 @@
if (field != obj.tupleof[i])
return false;
}
static foreach (field; extraFields)
if (mixin("this." ~ field ~ " != obj." ~ field))
return false;

Check warning on line 468 in src/dparse/ast.d

View check run for this annotation

Codecov / codecov/patch

src/dparse/ast.d#L467-L468

Added lines #L467 - L468 were not covered by tests
return true;
}
return false;
Expand Down Expand Up @@ -2318,6 +2341,109 @@
mixin OpEquals;
}

///
final class InterpolatedString : BaseNode
{
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(parts));
}

/** */ InterpolatedStringPart[] parts;

inout(Token) startQuote() inout pure nothrow @nogc @safe scope
{
return tokens.length ? tokens[0] : Token.init;
}

inout(Token) endQuote() inout pure nothrow @nogc @safe scope
{
return tokens.length && tokens[$ - 1].type == tok!"istringLiteralEnd"
? tokens[$ - 1]
: Token.init;
}

/// '\0'/'c'/'w'/'d' for `i""`, `i""c`, `i""w` and `i""d` respectively.
char postfixType() inout pure nothrow @nogc @safe scope
{
auto end = endQuote.text;
auto endChar = end.length ? end[$ - 1] : ' ';
switch (endChar)

Check warning on line 2371 in src/dparse/ast.d

View check run for this annotation

Codecov / codecov/patch

src/dparse/ast.d#L2369-L2371

Added lines #L2369 - L2371 were not covered by tests
{
case 'c':
case 'w':
case 'd':
return endChar;
default:
return '\0';

Check warning on line 2378 in src/dparse/ast.d

View check run for this annotation

Codecov / codecov/patch

src/dparse/ast.d#L2373-L2378

Added lines #L2373 - L2378 were not covered by tests
}
}

mixin OpEquals!("startQuote.text", "postfixType");
}

///
abstract class InterpolatedStringPart : BaseNode
{
}

///
final class InterpolatedStringText : InterpolatedStringPart
{
override void accept(ASTVisitor visitor) const
{
}

/// The token containing the plain text part in its `.text` property.
inout(Token) text() inout pure nothrow @nogc @safe scope
{
return tokens.length ? tokens[0] : Token.init;
}

mixin OpEquals!("text.text");
}

///
final class InterpolatedStringVariable : InterpolatedStringPart
{
override void accept(ASTVisitor visitor) const
{
}

/// The dollar token.
inout(Token) dollar() inout pure nothrow @nogc @safe scope
{
return tokens.length == 2 ? tokens[0] : Token.init;

Check warning on line 2416 in src/dparse/ast.d

View check run for this annotation

Codecov / codecov/patch

src/dparse/ast.d#L2416

Added line #L2416 was not covered by tests
}

/// The variable name token.
inout(Token) name() inout pure nothrow @nogc @safe scope
{
return tokens.length == 2 ? tokens[1] : Token.init;
}

mixin OpEquals!("name.text");
}

///
final class InterpolatedStringExpression : InterpolatedStringPart
{
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(expression));
}

/** */ Expression expression;

/// The dollar token.
inout(Token) dollar() inout pure nothrow @nogc @safe scope
{
return tokens.length ? tokens[0] : Token.init;

Check warning on line 2441 in src/dparse/ast.d

View check run for this annotation

Codecov / codecov/patch

src/dparse/ast.d#L2441

Added line #L2441 was not covered by tests
}

mixin OpEquals;
}

///
final class Invariant : BaseNode
{
Expand Down Expand Up @@ -2798,7 +2924,7 @@
typeofExpression, typeidExpression, arrayLiteral, assocArrayLiteral,
expression, dot, identifierOrTemplateInstance, isExpression,
functionLiteralExpression,traitsExpression, mixinExpression,
importExpression, vector, arguments));
importExpression, vector, arguments, interpolatedString));
}
/** */ Token dot;
/** */ Token primary;
Expand All @@ -2818,6 +2944,7 @@
/** */ Type type;
/** */ Token typeConstructor;
/** */ Arguments arguments;
/** */ InterpolatedString interpolatedString;
mixin OpEquals;
}

Expand Down
27 changes: 27 additions & 0 deletions src/dparse/astprinter.d
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,33 @@ class XMLPrinter : ASTVisitor
output.writeln("</interfaceDeclaration>");
}

override void visit(const InterpolatedString interpolatedString)
{
output.writeln("<interpolatedString startQuote=\"",
xmlAttributeEscape(interpolatedString.startQuote.text),
"\" endQuote=\"",
xmlAttributeEscape(interpolatedString.endQuote.text),
"\">");
foreach (part; interpolatedString.parts)
dynamicDispatch(part);
output.writeln("</interpolatedString>");
}

override void visit(const InterpolatedStringText interpolatedStringText)
{
output.writeln("<text>", xmlEscape(interpolatedStringText.text.text), "</text>");
}

override void visit(const InterpolatedStringVariable interpolatedStringVariable)
{
output.writeln("<variable>", xmlEscape(interpolatedStringVariable.name.text), "</variable>");
}

override void visit(const InterpolatedStringExpression interpolatedStringExpression)
{
visit(interpolatedStringExpression.expression);
}

override void visit(const Invariant invariant_)
{
output.writeln("<invariant>");
Expand Down
37 changes: 37 additions & 0 deletions src/dparse/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,36 @@ class Formatter(Sink)
}
}

void format(const InterpolatedString interpolatedString)
{
put(interpolatedString.startQuote.text);
foreach (part; interpolatedString.parts)
{
if (cast(InterpolatedStringText) part) format(cast(InterpolatedStringText) part);
else if (cast(InterpolatedStringVariable) part) format(cast(InterpolatedStringVariable) part);
else if (cast(InterpolatedStringExpression) part) format(cast(InterpolatedStringExpression) part);
}
put(interpolatedString.endQuote.text);
}

void format(const InterpolatedStringText interpolatedStringText)
{
put(interpolatedStringText.text.text);
}

void format(const InterpolatedStringVariable interpolatedStringVariable)
{
put("$");
put(interpolatedStringVariable.name.text);
}

void format(const InterpolatedStringExpression interpolatedStringExpression)
{
put("$(");
format(interpolatedStringExpression.expression);
put(")");
}

void format(const Invariant invariant_, const Attribute[] attrs = null)
{
debug(verbose) writeln("Invariant");
Expand Down Expand Up @@ -2571,6 +2601,7 @@ class Formatter(Sink)
Type type;
Token typeConstructor;
Arguments arguments;
InterpolatedString interpolatedString;
**/

with(primaryExpression)
Expand Down Expand Up @@ -2605,6 +2636,7 @@ class Formatter(Sink)
else if (vector) format(vector);
else if (type) format(type);
else if (arguments) format(arguments);
else if (interpolatedString) format(interpolatedString);
}
}

Expand Down Expand Up @@ -4348,4 +4380,9 @@ do
{
}
}}, `a == b && c == d`);
testFormatNode!(VariableDeclaration)(`T x = i"hello";`);
testFormatNode!(VariableDeclaration)(`T x = i" hello ";`);
testFormatNode!(VariableDeclaration)(`T x = i" hello $name ";`);
testFormatNode!(VariableDeclaration)(`T x = i" hello $(name) ";`);
testFormatNode!(VariableDeclaration)(`T x = i" hello $( name ) ";`, `T x = i" hello $(name) ";`);
}
Loading
Loading