Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix final and abstract access modifiers #34

Merged
merged 5 commits into from
Jul 7, 2020
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
27 changes: 21 additions & 6 deletions src/TcBlack/ObjectDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private TcObject TokenizeFunctionBlock()

List<string> interfaces = new List<string>();
List<string> parents = new List<string>();
List<string> accessModifiers = new List<string>();
bool implements = false;
bool extends = false;
foreach (string part in splitDefinition)
Expand All @@ -123,12 +124,21 @@ private TcObject TokenizeFunctionBlock()
extends = true;
implements = false;
}
else if (
part.ToLower() == "abstract"
|| part.ToLower() == "final"
|| part.ToLower() == "internal"
|| part.ToLower() == "public")
Roald87 marked this conversation as resolved.
Show resolved Hide resolved
{
accessModifiers.Add(part);
}
}
string name = splitDefinition[1 + accessModifiers.Count];

return new TcObject(
objectType: "FUNCTION_BLOCK",
accessModifier: "",
name: splitDefinition[1],
accessModifier: string.Join(" ", accessModifiers.ToArray()),
name: name,
dataType: "",
extends: string.Join(", ", parents.ToArray()),
implements: string.Join(", ", interfaces.ToArray())
Expand All @@ -138,7 +148,8 @@ private TcObject TokenizeFunctionBlock()
private TcObject TokenizeMethodOrProperty()
{
string entityType = @"\s*(FUNCTION|METHOD|PROPERTY)\s*";
string accessModifier = @"(PRIVATE|PUBLIC|PROTECTED|INTERNAL)?\s*";
string accessModifier =
@"(PRIVATE|PUBLIC|PROTECTED|INTERNAL)?(?:(?: *)(FINAL|ABSTRACT))?\s*";
string name = @"(\w+)\s*:?";
string dataType = @"\s*(.*[^\s+;])?";

Expand All @@ -148,11 +159,15 @@ private TcObject TokenizeMethodOrProperty()
if (matches.Count > 0)
{
Match match = matches[0];
bool twoModifers = match.Groups[2].Value.Length > 0
&& match.Groups[3].Value.Length > 0;
return new TcObject(
objectType: match.Groups[1].Value,
accessModifier: match.Groups[2].Value,
name: match.Groups[3].Value,
dataType: match.Groups[4].Value,
accessModifier: match.Groups[2].Value
+ (twoModifers ? " " : "")
+ match.Groups[3].Value,
name: match.Groups[4].Value,
dataType: match.Groups[5].Value,
extends: "",
implements: ""
);
Expand Down
63 changes: 63 additions & 0 deletions src/TcBlackTests/ObjectDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,69 @@ public void MethodsWithVariousAccessModifiersAndWhiteSpaces(
Assert.Equal(expectedCode, var.Format(ref indents));
}

[Theory]
[InlineData(
"FUNCTION_BLOCK FINAL Sum IMPLEMENTS Interface",
"FUNCTION_BLOCK FINAL Sum IMPLEMENTS Interface"
)]
[InlineData(
"FUNCTION_BLOCK ABSTRACT Sum IMPLEMENTS Interface, interface2",
"FUNCTION_BLOCK ABSTRACT Sum IMPLEMENTS Interface, interface2"
)]
[InlineData(
"FUNCTION_BLOCK INTERNAL FINAL Sum EXTENDS FB_SumParent IMPLEMENTS I_ab",
"FUNCTION_BLOCK INTERNAL FINAL Sum EXTENDS FB_SumParent IMPLEMENTS I_ab"
)]
[InlineData(
"FUNCTION_BLOCK PUBLIC ABSTRACT Sum IMPLEMENTS Interface, interface2",
"FUNCTION_BLOCK PUBLIC ABSTRACT Sum IMPLEMENTS Interface, interface2"
)]
[InlineData(
" PROPERTY PUBLIC ABSTRACT Test:BOOL",
"PROPERTY PUBLIC ABSTRACT Test : BOOL"
)]
[InlineData(
" PROPERTY FINAL Test:BOOL",
"PROPERTY FINAL Test : BOOL"
)]
[InlineData(
"METHOD PROTECTED FINAL Sum : BOOL",
"METHOD PROTECTED FINAL Sum : BOOL"
)]
[InlineData(
"METHOD PROTECTED ABSTRACT Sum : BOOL",
"METHOD PROTECTED ABSTRACT Sum : BOOL"
)]
[InlineData(
"METHOD PRIVATE ABSTRACT Sum : BOOL",
"METHOD PRIVATE ABSTRACT Sum : BOOL"
)]
[InlineData(
"METHOD INTERNAL ABSTRACT Sum : BOOL",
"METHOD INTERNAL ABSTRACT Sum : BOOL"
)]
[InlineData(
"METHOD PROTECTED FINAL Sum : BOOL",
"METHOD PROTECTED FINAL Sum : BOOL"
)]
[InlineData(
"METHOD FINAL Sum : BOOL",
"METHOD FINAL Sum : BOOL"
)]
[InlineData(
"METHOD PROTECTED Sum : BOOL",
"METHOD PROTECTED Sum : BOOL"
)]
public void AbstractAndFinalModifiersForMethodsAndFunctionBlocks(
string originalCode, string expectedCode
)
{
ObjectDefinition var =
new ObjectDefinition(originalCode, " ", "\n");
uint indents = 0;
Assert.Equal(expectedCode, var.Format(ref indents));
}

[Theory]
[InlineData(
"METHOD PUBLIC Close : SysFile.SysTypes.RTS_IEC_RESULT;",
Expand Down