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

Add support for declarations in switch expressions #505

Merged
Merged
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
11 changes: 9 additions & 2 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -3147,11 +3147,18 @@ final class SwitchStatement : BaseNode
{
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(expression, statement));
mixin (visitIfNotNull!(condition, statement));
}
/** */ Expression expression;
/** */ IfCondition condition;
/** */ Statement statement;
mixin OpEquals;

deprecated("use condition.expression") inout(Expression) expression() inout @property @safe nothrow @nogc pure
{
if (!condition)
return null;
return condition.expression;
}
}

///
Expand Down
4 changes: 2 additions & 2 deletions src/dparse/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -2958,15 +2958,15 @@ class Formatter(Sink)
debug(verbose) writeln("SwitchStatement");

/**
Expression expression;
IfCondition condition;
Statement statement;
**/

with(switchStatement)
{
newThing(What.other);
isFinal ? put(" final switch(") : put("switch(");
format(expression);
format(condition);
put(")");

bool needBlock = statement.statementNoCaseNoDefault &&
Expand Down
14 changes: 5 additions & 9 deletions src/dparse/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -6755,17 +6755,13 @@ class Parser
// DCD #453
// with just `switch(stuff` returns a non null node,
// which allows DCD to gives completion on `stuff`.
if (auto e = parseExpression())
mixin(parseNodeQ!(`node.condition`, `IfCondition`));
if (currentIs(tok!")"))
{
node.expression = e;
if (currentIs(tok!")"))
{
advance();
// returns null only from here.
mixin(parseNodeQ!(`node.statement`, `Statement`));
}
advance();
// returns null only from here.
mixin(parseNodeQ!(`node.statement`, `Statement`));
}
else error("Expression expected after `switch(`", false);
node.tokens = tokens[startIndex .. index];
return node;
}
Expand Down
35 changes: 35 additions & 0 deletions test/ast_checks/switch_condition.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
void a()
{
switch (auto line = readln)
{
default:
line.strip;
}
}

void b()
{
switch (scope line = readln)
{
default:
line.strip;
}
}

void c()
{
switch (const line = readln)
{
default:
line.strip;
}
}

void d()
{
switch (const inout string line = readln)
{
default:
line.strip;
}
}
28 changes: 28 additions & 0 deletions test/ast_checks/switch_condition.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//functionDeclaration[name = 'a']//functionBody//switchStatement/condition/auto
not(//functionDeclaration[name = 'a']//functionBody//switchStatement/condition/scope)
//functionDeclaration[name = 'a']//functionBody//switchStatement/condition/identifier[text()='line']
//functionDeclaration[name = 'a']//functionBody//switchStatement/condition/unaryExpression//identifier[text()='readln']
//functionDeclaration[name = 'a']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='line']
//functionDeclaration[name = 'a']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='strip']
not(//functionDeclaration[name = 'b']//functionBody//switchStatement/condition/auto)
//functionDeclaration[name = 'b']//functionBody//switchStatement/condition/scope
//functionDeclaration[name = 'b']//functionBody//switchStatement/condition/identifier[text()='line']
//functionDeclaration[name = 'b']//functionBody//switchStatement/condition/unaryExpression//identifier[text()='readln']
//functionDeclaration[name = 'b']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='line']
//functionDeclaration[name = 'b']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='strip']
not(//functionDeclaration[name = 'c']//functionBody//switchStatement/condition/auto)
not(//functionDeclaration[name = 'c']//functionBody//switchStatement/condition/scope)
//functionDeclaration[name = 'c']//functionBody//switchStatement/condition/typeConstructor[text()='const']
//functionDeclaration[name = 'c']//functionBody//switchStatement/condition/identifier[text()='line']
//functionDeclaration[name = 'c']//functionBody//switchStatement/condition/unaryExpression//identifier[text()='readln']
//functionDeclaration[name = 'c']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='line']
//functionDeclaration[name = 'c']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='strip']
not(//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/auto)
not(//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/scope)
//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/typeConstructor[text()='const']
//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/typeConstructor[text()='inout']
//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/type[@pretty='string']
//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/identifier[text()='line']
//functionDeclaration[name = 'd']//functionBody//switchStatement/condition/unaryExpression//identifier[text()='readln']
//functionDeclaration[name = 'd']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='line']
//functionDeclaration[name = 'd']//functionBody//switchStatement/statement//defaultStatement//identifier[text()='strip']
14 changes: 14 additions & 0 deletions test/pass_files/switch_condition_assignment.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std : writeln, format;

int get()
{
return 1;
}

void main()
{
switch (auto x = get()) {
default:
writeln("inside switch: ", x);
}
}