Skip to content
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
43 changes: 37 additions & 6 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ public:
/** */ void visit(const Finally finally_) { finally_.accept(this); }
/** */ void visit(const ForStatement forStatement) { forStatement.accept(this); }
/** */ void visit(const ForeachStatement foreachStatement) { foreachStatement.accept(this); }
/** */ void visit(const StaticForeachDeclaration staticForeachDeclaration) { staticForeachDeclaration.accept(this); }
/** */ void visit(const StaticForeachStatement staticForeachStatement) { staticForeachStatement.accept(this); }
/** */ void visit(const ForeachType foreachType) { foreachType.accept(this); }
/** */ void visit(const ForeachTypeList foreachTypeList) { foreachTypeList.accept(this); }
/** */ void visit(const FunctionAttribute functionAttribute) { functionAttribute.accept(this); }
Expand Down Expand Up @@ -1304,7 +1306,7 @@ public:
SharedStaticConstructor, SharedStaticDestructor, StaticAssertDeclaration,
StaticConstructor, StaticDestructor, StructDeclaration,
TemplateDeclaration, UnionDeclaration, Unittest, VariableDeclaration,
VersionSpecification);
VersionSpecification, StaticForeachDeclaration);

private Algebraic!(DeclarationTypes) storage;

Expand Down Expand Up @@ -1347,6 +1349,7 @@ public:
mixin(generateProperty("Unittest", "unittest_"));
mixin(generateProperty("VariableDeclaration", "variableDeclaration"));
mixin(generateProperty("VersionSpecification", "versionSpecification"));
mixin(generateProperty("StaticForeachDeclaration", "staticForeachDeclaration"));

override bool opEquals(Object other) const
{
Expand Down Expand Up @@ -1629,22 +1632,47 @@ public:
mixin OpEquals;
}


///
final class ForeachStatement : ASTNode
final class Foreach(bool declOnly) : ASTNode
{
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(foreachType, foreachTypeList, low, high,
declarationOrStatement));
mixin (visitIfNotNull!(foreachType, foreachTypeList, low, high));
static if (declOnly)
mixin (visitIfNotNull!(declarations));
else
mixin (visitIfNotNull!(declarationOrStatement));
}
/** */ IdType type;
/** */ ForeachTypeList foreachTypeList;
/** */ ForeachType foreachType;
/** */ Expression low;
/** */ Expression high;
/** */ DeclarationOrStatement declarationOrStatement;
/** */ size_t startIndex;
static if (declOnly)
/** */ Declaration[] declarations;
else
/** */ DeclarationOrStatement declarationOrStatement;
mixin OpEquals;
}

///
alias StaticForeachDeclaration = Foreach!true;

///
alias ForeachStatement = Foreach!false;

///
final class StaticForeachStatement : ASTNode
{
public:
override void accept(ASTVisitor visitor) const
{
mixin (visitIfNotNull!(foreachStatement));
}
/** */ ForeachStatement foreachStatement;
mixin OpEquals;
}

Expand All @@ -1656,6 +1684,8 @@ public:
{
mixin (visitIfNotNull!(type, identifier));
}
/** */ bool isAlias;
/** */ bool isEnum;
/** */ bool isRef;
/** */ IdType[] typeConstructors;
/** */ Type type;
Expand Down Expand Up @@ -2264,7 +2294,7 @@ public:
synchronizedStatement, tryStatement, throwStatement,
scopeGuardStatement, asmStatement, pragmaStatement,
conditionalStatement, staticAssertStatement, versionSpecification,
debugSpecification, expressionStatement));
debugSpecification, expressionStatement, staticForeachStatement));
}
/** */ LabeledStatement labeledStatement;
/** */ BlockStatement blockStatement;
Expand All @@ -2273,6 +2303,7 @@ public:
/** */ DoStatement doStatement;
/** */ ForStatement forStatement;
/** */ ForeachStatement foreachStatement;
/** */ StaticForeachStatement staticForeachStatement;
/** */ SwitchStatement switchStatement;
/** */ FinalSwitchStatement finalSwitchStatement;
/** */ ContinueStatement continueStatement;
Expand Down
118 changes: 98 additions & 20 deletions src/dparse/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,8 @@ class Parser
mixin (nullCheck!`node.conditionalDeclaration = parseConditionalDeclaration(strict)`);
else if (peekIs(tok!"assert"))
mixin(parseNodeQ!(`node.staticAssertDeclaration`, `StaticAssertDeclaration`));
else if (peekIs(tok!"foreach") || peekIs(tok!"foreach_reverse"))
mixin(parseNodeQ!(`node.staticForeachDeclaration`, `StaticForeachDeclaration`));
else
goto type;
break;
Expand Down Expand Up @@ -2741,6 +2743,37 @@ class Parser
return node;
}

/**
* Parses a StaticForeachDeclaration
*
* $(GRAMMAR $(RULEDEF staticForeachDeclaration):
* $(LITERAL 'static') ($(LITERAL 'foreach') | $(LITERAL 'foreach_reverse')) $(LITERAL '$(LPAREN)') $(RULE foreachTypeList) $(LITERAL ';') $(RULE expression) $(LITERAL '$(RPAREN)')
* ($(RULE declaration) | $(LITERAL '{') $(RULE declaration)* $(LITERAL '}'))
* | $(LITERAL 'static') ($(LITERAL 'foreach') | $(LITERAL 'foreach_reverse')) $(LITERAL '$(LPAREN)') $(RULE foreachType) $(LITERAL ';') $(RULE expression) $(LITERAL '..') $(RULE expression) $(LITERAL '$(RPAREN)')
* ($(RULE declaration) | $(LITERAL '{') $(RULE declaration)* $(LITERAL '}'))
* ;)
*/
StaticForeachDeclaration parseStaticForeachDeclaration()
{
mixin(traceEnterAndExit!(__FUNCTION__));
mixin(tokenCheck!"static");
return parseForeach!true();
}

/**
* Parses a StaticForeachStatement
*
* $(GRAMMAR $(RULEDEF staticForeachStatement):
* $(LITERAL 'static') $(RULE foreachStatement)
* ;)
*/
StaticForeachStatement parseStaticForeachStatement()
{
mixin(traceEnterAndExit!(__FUNCTION__));
mixin(simpleParse!(StaticForeachStatement,
tok!"static", "foreachStatement|parseForeachStatement"));
}

/**
* Parses a ForeachStatement
*
Expand All @@ -2752,7 +2785,13 @@ class Parser
ForeachStatement parseForeachStatement()
{
mixin(traceEnterAndExit!(__FUNCTION__));
ForeachStatement node = allocator.make!ForeachStatement;
return parseForeach!false();
}

Foreach!declOnly parseForeach(bool declOnly = false)()
{
mixin(traceEnterAndExit!(__FUNCTION__));
Foreach!declOnly node = allocator.make!(Foreach!declOnly);
if (currentIsOneOf(tok!"foreach", tok!"foreach_reverse"))
node.type = advance().type;
else
Expand Down Expand Up @@ -2791,37 +2830,72 @@ class Parser
error("Statement expected", false);
return node; // this line makes DCD better
}
mixin(parseNodeQ!(`node.declarationOrStatement`, `DeclarationOrStatement`));
static if (declOnly)
{
StackBuffer declarations;
if (currentIs(tok!"{"))
{
advance();
while (moreTokens() && !currentIs(tok!"}"))
{
immutable b = setBookmark();
immutable c = allocator.setCheckpoint();
if (declarations.put(parseDeclaration(true, true)))
abandonBookmark(b);
else
{
goToBookmark(b);
allocator.rollback(c);
return null;
}
}
mixin(tokenCheck!"}");
}
else if (!declarations.put(parseDeclaration(true, true)))
return null;
ownArray(node.declarations, declarations);
}
else
mixin(parseNodeQ!(`node.declarationOrStatement`, `DeclarationOrStatement`));
return node;
}

/**
* Parses a ForeachType
*
* $(GRAMMAR $(RULEDEF foreachType):
* $(LITERAL 'ref')? $(RULE typeConstructors)? $(RULE type)? $(LITERAL Identifier)
* | $(RULE typeConstructors)? $(LITERAL 'ref')? $(RULE type)? $(LITERAL Identifier)
* ($(LITERAL 'ref') | $(LITERAL 'alias') | $(LITERAL 'enum') | $(RULE typeConstructor))* $(RULE type)? $(LITERAL Identifier)
* ;)
*/
ForeachType parseForeachType()
{
mixin(traceEnterAndExit!(__FUNCTION__));
auto node = allocator.make!ForeachType;
if (currentIs(tok!"ref"))
{
node.isRef = true;
advance();
}
if (currentIsOneOf(tok!"const", tok!"immutable",
tok!"inout", tok!"shared") && !peekIs(tok!"("))
{
trace("\033[01;36mType constructor");
mixin(parseNodeQ!(`node.typeConstructors`, `TypeConstructors`));
}
if (currentIs(tok!"ref"))
while (moreTokens())
{
node.isRef = true;
advance();
IdType typeConstructor;
if (currentIs(tok!"ref"))
{
node.isRef = true;
advance();
}
else if (currentIs(tok!"alias"))
{
node.isAlias = true;
advance();
}
else if (currentIs(tok!"enum"))
{
node.isEnum = true;
advance();
}
else if (tok!"" != (typeConstructor = parseTypeConstructor(false)))
{
trace("\033[01;36mType constructor");
node.typeConstructors ~= typeConstructor;
}
else
break;
}
if (currentIs(tok!"identifier") && peekIsOneOf(tok!",", tok!";"))
{
Expand Down Expand Up @@ -4966,9 +5040,11 @@ class Parser
mixin(parseNodeQ!(`node.conditionalStatement`, `ConditionalStatement`));
else if (peekIs(tok!"assert"))
mixin(parseNodeQ!(`node.staticAssertStatement`, `StaticAssertStatement`));
else if (peekIs(tok!"foreach") || peekIs(tok!"foreach_reverse"))
mixin(parseNodeQ!(`node.staticForeachStatement`, `StaticForeachStatement`));
else
{
error("'if' or 'assert' expected.");
error("'if' or 'assert' or 'foreach' or 'foreach_reverse' expected.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error("'if', 'assert', 'foreach', or 'foreach_reverse' expected.");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a test case that checks that the new alias and enum attributes are parsed for the foreach statement.

Thank you @Hackerpilot for the review. I didn't know there were new type attributes.

return null;
}
break;
Expand Down Expand Up @@ -6861,6 +6937,8 @@ protected:
case tok!"static":
if (peekIs(tok!"if"))
return false;
else if (peekIs(tok!"foreach") || peekIs(tok!"foreach_reverse"))
goto default;
goto case;
case tok!"scope":
if (peekIs(tok!"("))
Expand Down Expand Up @@ -6975,7 +7053,7 @@ protected:
case tok!"scope":
return !peekIs(tok!"(");
case tok!"static":
return !peekIsOneOf(tok!"assert", tok!"this", tok!"if", tok!"~");
return !peekIsOneOf(tok!"assert", tok!"this", tok!"if", tok!"~", tok!"foreach", tok!"foreach_reverse");
case tok!"shared":
return !(startsWith(tok!"shared", tok!"static", tok!"this")
|| startsWith(tok!"shared", tok!"static", tok!"~")
Expand Down
15 changes: 15 additions & 0 deletions test/pass_files/declarations.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ idouble a = 4Li;
idouble a = 4i;
ifloat a = 4fi;
ifloat a = 4Fi;

static foreach (n; ['a', 'b', 'c'])
{
mixin("char " ~ n ~ ";");
}

static foreach_reverse (i; '0' .. '5')
{
mixin("int _" ~ i ~ ";");
}

static foreach (enum i, alias T; AliasSeq!(int, bool))
{
T a = i;
}
11 changes: 11 additions & 0 deletions test/pass_files/statements.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,15 @@ deprecated void foo()
i++;
}
label:


static foreach (n; ['a', 'b', 'c'])
{{
mixin(n ~ "++;");
}}

static foreach_reverse (i; 1 .. 10)
{
assert(i-- > 0);
}
}