Skip to content

Commit 0f347e9

Browse files
committed
Fix #484, support multiple message parts in assert
Also modified the AST for regular asserts, for future proofing and easier implementation.
1 parent d42ed27 commit 0f347e9

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

src/dparse/ast.d

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,11 +874,21 @@ final class AssertArguments : BaseNode
874874
{
875875
override void accept(ASTVisitor visitor) const
876876
{
877-
mixin (visitIfNotNull!(assertion, message));
877+
mixin (visitIfNotNull!(assertion, messageParts));
878878
}
879+
879880
/** */ ExpressionNode assertion;
880-
/** */ ExpressionNode message;
881+
/** */ ExpressionNode[] messageParts;
881882
mixin OpEquals;
883+
884+
deprecated("use firstMessage or process all messageParts instead")
885+
alias message = firstMessage;
886+
887+
/// Returns `messageParts[0]` or `null` if no messageParts.
888+
inout(ExpressionNode) firstMessage() inout nothrow pure @nogc @safe
889+
{
890+
return messageParts.length ? messageParts[0] : null;
891+
}
882892
}
883893

884894
///

src/dparse/formatter.d

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,13 @@ class Formatter(Sink)
406406
{
407407
debug(verbose) writeln("AssertExpression");
408408

409-
/**
410-
AssignExpression assertion;
411-
AssignExpression message;
412-
**/
413-
414409
with(assertArguments)
415410
{
416411
format(assertion);
417-
if (message)
412+
foreach (part; messageParts)
418413
{
419414
put(", ");
420-
format(message);
415+
format(part);
421416
}
422417
}
423418
}

src/dparse/parser.d

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class Parser
350350
* Parses an ArgumentList.
351351
*
352352
* $(GRAMMAR $(RULEDEF argumentList):
353-
* $(RULE assignExpression) ($(LITERAL ',') $(RULE assignExpression)?)*
353+
* $(RULE assignExpression) ($(LITERAL ',') $(RULE assignExpression)?)* $(LITERAL ',')?
354354
* ;)
355355
*/
356356
ArgumentList parseArgumentList()
@@ -1037,24 +1037,25 @@ class Parser
10371037
* Parses an AssertArguments
10381038
*
10391039
* $(GRAMMAR $(RULEDEF assertArguments):
1040-
* $(RULE assignExpression) ($(LITERAL ',') $(RULE assignExpression))? $(LITERAL ',')?
1040+
* $(RULE assignExpression) ($(LITERAL ',') $(RULE assignExpression))* $(LITERAL ',')?
10411041
* ;)
10421042
*/
10431043
AssertArguments parseAssertArguments()
10441044
{
10451045
auto startIndex = index;
10461046
auto node = allocator.make!AssertArguments;
10471047
mixin(parseNodeQ!(`node.assertion`, `AssignExpression`));
1048-
if (currentIs(tok!","))
1049-
advance();
1050-
if (currentIs(tok!")"))
1048+
1049+
StackBuffer items;
1050+
while (currentIs(tok!","))
10511051
{
1052-
node.tokens = tokens[startIndex .. index];
1053-
return node;
1054-
}
1055-
mixin(parseNodeQ!(`node.message`, `AssignExpression`));
1056-
if (currentIs(tok!","))
10571052
advance();
1053+
if (currentIs(tok!")"))
1054+
break;
1055+
if (!items.put(parseAssignExpression()))
1056+
return null;
1057+
}
1058+
ownArray(node.messageParts, items);
10581059
node.tokens = tokens[startIndex .. index];
10591060
return node;
10601061
}

test/ast_checks/assert_args.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static assert(foo, "a", b, "c");

test/ast_checks/assert_args.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
./module/declaration/staticAssertDeclaration//assertArguments/unaryExpression[1]//identifierOrTemplateInstance/identifier[text()="foo"]
2+
./module/declaration/staticAssertDeclaration//assertArguments/unaryExpression[2]/primaryExpression/stringLiteral[text()='"a"']
3+
./module/declaration/staticAssertDeclaration//assertArguments/unaryExpression[3]//identifierOrTemplateInstance/identifier[text()="b"]
4+
./module/declaration/staticAssertDeclaration//assertArguments/unaryExpression[4]/primaryExpression/stringLiteral[text()='"c"']

0 commit comments

Comments
 (0)