Skip to content

Commit

Permalink
Allow explicit public/internal access modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Sep 5, 2019
1 parent fabfef7 commit a7266dd
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PropertyGroup>
<IntegrationTestEnabled>true</IntegrationTestEnabled>
<IntegrationTestEnabled Condition="'$(BuildingInsideVisualStudio)' == 'true'">false</IntegrationTestEnabled>
<IntegrationTestEnabled Condition="'$(BuildingByReSharper)' == 'true'">false</IntegrationTestEnabled>
</PropertyGroup>

<PropertyGroup Condition="'$(IntegrationTestEnabled)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,7 @@ public void should_generate_internal_messages()
var code = Generate(new MessageDefinition
{
Name = "FooExecuted",
Options =
{
Internal = true
}
AccessModifier = AccessModifier.Internal
});

code.ShouldContain("internal sealed partial class FooExecuted : IEvent");
Expand All @@ -499,10 +496,7 @@ public void should_generate_internal_enums()
Name = "Default"
}
},
Options =
{
Internal = true
}
AccessModifier = AccessModifier.Internal
}
}
});
Expand Down
16 changes: 16 additions & 0 deletions src/Abc.Zebus.MessageDsl.Tests/MessageDsl/ParsedContractsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ public void should_parse_pragma_internal_and_public()
contracts.Messages[1].Options.Public.ShouldBeTrue();
}

[Test]
public void should_parse_access_modifiers()
{
var contracts = ParseValid("public MsgA(); internal MsgB();");
contracts.Messages[0].AccessModifier.ShouldEqual(AccessModifier.Public);
contracts.Messages[1].AccessModifier.ShouldEqual(AccessModifier.Internal);
}

[Test]
public void should_parse_access_modifiers_in_internal_scope()
{
var contracts = ParseValid("#pragma internal\r\npublic MsgA(); internal MsgB();");
contracts.Messages[0].AccessModifier.ShouldEqual(AccessModifier.Public);
contracts.Messages[1].AccessModifier.ShouldEqual(AccessModifier.Internal);
}

[Test]
public void should_parse_custom_types()
{
Expand Down
24 changes: 24 additions & 0 deletions src/Abc.Zebus.MessageDsl/Analysis/AstCreationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Abc.Zebus.MessageDsl.Ast;
using Abc.Zebus.MessageDsl.Dsl;
using Abc.Zebus.MessageDsl.Support;
using JetBrains.Annotations;
using static Abc.Zebus.MessageDsl.Dsl.MessageContractsParser;

namespace Abc.Zebus.MessageDsl.Analysis
Expand Down Expand Up @@ -97,6 +98,7 @@ public override AstNode VisitEnumDefinition(EnumDefinitionContext context)
Options = _currentMemberOptions
};

ProcessAccessModifier(enumDef, context.accessModifier());
ProcessAttributes(enumDef.Attributes, context.attributes());

if (context.underlyingType != null)
Expand Down Expand Up @@ -289,6 +291,8 @@ private void ProcessMessage(MessageDefinition message, MessageDefinitionContext
var nameContext = context.GetRuleContext<MessageNameContext>(0);
message.Name = GetId(nameContext.name);

ProcessAccessModifier(message, context.accessModifier());

foreach (var typeParamToken in nameContext._typeParams)
{
var paramId = GetId(typeParamToken);
Expand All @@ -312,6 +316,26 @@ private void ProcessMessage(MessageDefinition message, MessageDefinitionContext
}
}

private static void ProcessAccessModifier(IMemberNode member, [CanBeNull] AccessModifierContext accessModifier)
{
if (accessModifier == null)
{
member.AccessModifier = member.Options.GetAccessModifier();
return;
}

switch (accessModifier.type.Type)
{
case MessageContractsLexer.KW_PUBLIC:
member.AccessModifier = AccessModifier.Public;
break;

case MessageContractsLexer.KW_INTERNAL:
member.AccessModifier = AccessModifier.Internal;
break;
}
}

private void ProcessAttributes(AttributeSet attributeSet, AttributesContext context)
{
if (context == null || attributeSet == null)
Expand Down
8 changes: 8 additions & 0 deletions src/Abc.Zebus.MessageDsl/Ast/AccessModifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Abc.Zebus.MessageDsl.Ast
{
public enum AccessModifier
{
Public,
Internal
}
}
3 changes: 2 additions & 1 deletion src/Abc.Zebus.MessageDsl/Ast/EnumDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Abc.Zebus.MessageDsl.Ast
{
public class EnumDefinition : AstNode, INamedNode
public class EnumDefinition : AstNode, IMemberNode
{
private MemberOptions _options;

public string Name { get; set; }
public TypeName UnderlyingType { get; set; } = "int";
public AccessModifier AccessModifier { get; set; }
public AttributeSet Attributes { get; } = new AttributeSet();
public IList<EnumMemberDefinition> Members { get; } = new List<EnumMemberDefinition>();

Expand Down
8 changes: 8 additions & 0 deletions src/Abc.Zebus.MessageDsl/Ast/IMemberNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Abc.Zebus.MessageDsl.Ast
{
internal interface IMemberNode : INamedNode
{
AccessModifier AccessModifier { get; set; }
MemberOptions Options { get; }
}
}
9 changes: 7 additions & 2 deletions src/Abc.Zebus.MessageDsl/Ast/MemberOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Abc.Zebus.MessageDsl.Ast
using JetBrains.Annotations;

namespace Abc.Zebus.MessageDsl.Ast
{
public class MemberOptions : OptionsBase
{
Expand All @@ -10,9 +12,12 @@ public class MemberOptions : OptionsBase
public bool Public
{
get => !Internal;
set => Internal = !value;
[UsedImplicitly] set => Internal = !value;
}

public AccessModifier GetAccessModifier()
=> Internal ? AccessModifier.Internal : AccessModifier.Public;

public MemberOptions Clone() => (MemberOptions)MemberwiseClone();
}
}
3 changes: 2 additions & 1 deletion src/Abc.Zebus.MessageDsl/Ast/MessageDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Abc.Zebus.MessageDsl.Ast
{
public class MessageDefinition : AstNode, INamedNode
public class MessageDefinition : AstNode, IMemberNode
{
private MemberOptions _options;

public string Name { get; set; }
public AccessModifier AccessModifier { get; set; }
public IList<string> GenericParameters { get; } = new List<string>();
public IList<GenericConstraint> GenericConstraints { get; } = new List<GenericConstraint>();

Expand Down
14 changes: 11 additions & 3 deletions src/Abc.Zebus.MessageDsl/Dsl/MessageContracts.g4
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ usingDefinition
;

enumDefinition
: attributes 'public'? 'enum' name=id (':' underlyingType=typeName)? '{' (enumMember (',' enumMember)* ','?)? '}'
: attributes accessModifier? 'enum' name=id (':' underlyingType=typeName)? '{' (enumMember (',' enumMember)* ','?)? '}'
;

enumMember
Expand All @@ -61,7 +61,12 @@ enumValueAtom
;

messageDefinition
: attributes messageName customModifier='!'? parameterList interfaceList typeParamConstraintList
: attributes accessModifier? messageName customModifier='!'? parameterList interfaceList typeParamConstraintList
;

accessModifier
: type=KW_PUBLIC
| type=KW_INTERNAL
;

messageName
Expand Down Expand Up @@ -164,6 +169,9 @@ id

// --- LEXER ---

KW_PUBLIC : 'public';
KW_INTERNAL : 'internal';

ID
: [a-zA-Z_][a-zA-Z0-9_]*
;
Expand All @@ -174,7 +182,7 @@ STRING
;
CHAR
: '\'' (UNICODE_ESCAPE | '\\' ~[\r\n] | ~[\'\\\r\n]) '\''
: '\'' (UNICODE_ESCAPE | '\\' ~[\r\n] | ~['\\\r\n]) '\''
;
NUMBER
Expand Down
19 changes: 17 additions & 2 deletions src/Abc.Zebus.MessageDsl/Generator/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void WriteEnum(EnumDefinition enumDef)

Writer.Write(
"{0} enum {1}",
enumDef.Options.Internal ? "internal" : "public",
AccessModifier(enumDef.AccessModifier),
Identifier(enumDef.Name)
);

Expand Down Expand Up @@ -165,7 +165,7 @@ private void WriteMessage(MessageDefinition message)

Writer.Write(
"{0} sealed partial class {1}",
message.Options.Internal ? "internal" : "public",
AccessModifier(message.AccessModifier),
Identifier(message.Name)
);

Expand Down Expand Up @@ -360,6 +360,21 @@ private void WriteAttribute(AttributeDefinition attribute)
Writer.Write("({0})", attribute.Parameters);
}

private static string AccessModifier(AccessModifier accessModifier)
{
switch (accessModifier)
{
case Ast.AccessModifier.Public:
return "public";

case Ast.AccessModifier.Internal:
return "internal";

default:
throw new ArgumentOutOfRangeException(nameof(accessModifier), accessModifier, null);
}
}

private static string Identifier(string id) => CSharpSyntax.Identifier(id);
}
}

0 comments on commit a7266dd

Please sign in to comment.