Skip to content

Commit

Permalink
Add scope to foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Oct 16, 2022
1 parent fc790d9 commit abeefa2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@ final class ForeachType : BaseNode
/** */ bool isAlias;
/** */ bool isEnum;
/** */ bool isRef;
/** */ bool isScope;
/** */ IdType[] typeConstructors;
/** */ Type type;
/** */ Token identifier;
Expand Down
4 changes: 4 additions & 0 deletions src/dparse/astprinter.d
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ class XMLPrinter : ASTVisitor
override void visit(const ForeachType foreachType)
{
output.writeln("<foreachType>");
if (foreachType.isAlias) output.writeln("<alias/>");
if (foreachType.isEnum) output.writeln("<enum/>");
if (foreachType.isRef) output.writeln("<ref/>");
if (foreachType.isScope) output.writeln("<scope/>");
foreach (constructor; foreachType.typeConstructors)
{
output.writeln("<typeConstructor>", str(constructor), "</typeConstructor>");
Expand Down
7 changes: 6 additions & 1 deletion src/dparse/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -3287,7 +3287,7 @@ class Parser
* Parses a ForeachType
*
* $(GRAMMAR $(RULEDEF foreachType):
* ($(LITERAL 'ref') | $(LITERAL 'alias') | $(LITERAL 'enum') | $(RULE typeConstructor))* $(RULE type)? $(LITERAL Identifier)
* ($(LITERAL 'ref') | $(LITERAL 'alias') | $(LITERAL 'enum') | $(LITERAL 'scope') | $(RULE typeConstructor))* $(RULE type)? $(LITERAL Identifier)
* ;)
*/
ForeachType parseForeachType()
Expand All @@ -3313,6 +3313,11 @@ class Parser
node.isEnum = true;
advance();
}
else if (currentIs(tok!"scope"))
{
node.isScope = true;
advance();
}
else if (tok!"" != (typeConstructor = parseTypeConstructor(false)))
{
trace("\033[01;36mType constructor");
Expand Down
4 changes: 4 additions & 0 deletions test/ast_checks/foreach.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void foo(T)(T[] arr)
{
foreach (enum ref scope const inout alias f; arr) {}
}
6 changes: 6 additions & 0 deletions test/ast_checks/foreach.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//functionDeclaration//foreachStatement//foreachType/alias
//functionDeclaration//foreachStatement//foreachType/enum
//functionDeclaration//foreachStatement//foreachType/ref
//functionDeclaration//foreachStatement//foreachType/scope
//functionDeclaration//foreachStatement//foreachType/typeConstructor[text()='const']
//functionDeclaration//foreachStatement//foreachType/typeConstructor[text()='inout']

0 comments on commit abeefa2

Please sign in to comment.