Skip to content

Commit

Permalink
Allow discards in enums (syntax only)
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed May 9, 2024
1 parent 980fab9 commit b3f0037
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Abc.Zebus.MessageDsl/Analysis/AstCreationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ public AstCreationVisitor(ParsedContracts contracts)

foreach (var enumMemberContext in context.enumMember())
{
if (enumMemberContext.discard is not null)
{
enumDef.Members.Add(
new EnumMemberDefinition
{
ParseContext = enumMemberContext,
Name = "_",
IsDiscarded = true
}
);

continue;
}

var memberDef = new EnumMemberDefinition
{
ParseContext = enumMemberContext,
Expand Down
12 changes: 12 additions & 0 deletions src/Abc.Zebus.MessageDsl/Analysis/AstProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void Cleanup()
{
foreach (var message in _contracts.Messages)
RemoveDiscardedParameters(message);

foreach (var enumDef in _contracts.Enums)
RemoveDiscardedMembers(enumDef);
}

private static void AddInterfaces(MessageDefinition message)
Expand Down Expand Up @@ -146,6 +149,15 @@ private static void RemoveDiscardedParameters(MessageDefinition message)
}
}

private static void RemoveDiscardedMembers(EnumDefinition enumDef)
{
for (var i = enumDef.Members.Count - 1; i >= 0; --i)
{
if (enumDef.Members[i].IsDiscarded)
enumDef.Members.RemoveAt(i);
}
}

private static void AddReservations(MessageDefinition message)
{
var currentReservation = ReservationRange.None;
Expand Down
3 changes: 3 additions & 0 deletions src/Abc.Zebus.MessageDsl/Analysis/AstValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ private void ValidateEnum(EnumDefinition enumDef)

foreach (var member in enumDef.Members)
{
if (member.IsDiscarded)
continue;

if (!definedMembers.Add(member.Name))
_contracts.AddError(member.ParseContext, $"Duplicate enum member: {member.Name}");

Expand Down
4 changes: 4 additions & 0 deletions src/Abc.Zebus.MessageDsl/Ast/EnumMemberDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public class EnumMemberDefinition : AstNode, INamedNode
public string? Value { get; set; }
public AttributeSet Attributes { get; } = new();
internal int? ProtoValue { get; set; }
internal bool IsDiscarded { get; set; }

public override string ToString()
=> Name;
}
1 change: 1 addition & 0 deletions src/Abc.Zebus.MessageDsl/Dsl/MessageContracts.g4
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enumDefinition

enumMember
: attributes name=id ('=' value=enumValue)?
| discard=DISCARD
;

enumValue
Expand Down

0 comments on commit b3f0037

Please sign in to comment.