diff --git a/src/TcBlack/ObjectDefinition.cs b/src/TcBlack/ObjectDefinition.cs index b048e4d..8994114 100644 --- a/src/TcBlack/ObjectDefinition.cs +++ b/src/TcBlack/ObjectDefinition.cs @@ -97,6 +97,7 @@ private TcObject TokenizeFunctionBlock() List interfaces = new List(); List parents = new List(); + List accessModifiers = new List(); bool implements = false; bool extends = false; foreach (string part in splitDefinition) @@ -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") + { + 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()) @@ -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+;])?"; @@ -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: "" ); diff --git a/src/TcBlackTests/ObjectDefinitionTests.cs b/src/TcBlackTests/ObjectDefinitionTests.cs index 0ffeb19..0aba038 100644 --- a/src/TcBlackTests/ObjectDefinitionTests.cs +++ b/src/TcBlackTests/ObjectDefinitionTests.cs @@ -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;",