Skip to content

Commit

Permalink
Auto upper standard data types
Browse files Browse the repository at this point in the history
  • Loading branch information
kdorsel committed Jul 1, 2020
1 parent dec0c0c commit f1bf61c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/TcBlack/ObjectDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

Expand All @@ -10,6 +11,48 @@ namespace TcBlack
/// </summary>
public class ObjectDefinition : CodeLineBase
{
private static readonly HashSet<string> DataTypes = new HashSet<string>
(StringComparer.OrdinalIgnoreCase)
{
"bool",
"byte",
"word",
"dword",
"lword",
"sint",
"usint",
"int",
"uint",
"dint",
"udint",
"lint",
"ulint",
"real",
"lreal",
"time",
"ltime",
"time_of_day",
"tod",
"date",
"date_and_time",
"dt",
"xint",
"uxint",
"xword",
"pvoid",
"any",
"any_date",
"any_num",
"any_sintrg",
"any_real",
"any_int",
};

private static readonly Regex ComplexDataTypeRegex =
new Regex(
@"(w?string(?:\(\d+\))?|array\s*\[[\d.]*]\s*of\s+|(?:pointer|reference)\s+to\s+)(.*)"
);

public struct TcObject
{
public TcObject(
Expand Down Expand Up @@ -135,6 +178,24 @@ private TcObject TokenizeFunctionBlock()
);
}

private string UpperDataType(string data)
{
if (DataTypes.Contains(data))
{
return data.ToUpper();
}

MatchCollection matches = ComplexDataTypeRegex.Matches(data);
if (matches.Count > 0)
{
Match match = matches[0];
return match.Groups[1].Value.ToUpper()
+ UpperDataType(match.Groups[2].Value);
}

return data;
}

private TcObject TokenizeMethodOrProperty()
{
string entityType = @"\s*(FUNCTION|METHOD|PROPERTY)\s*";
Expand All @@ -152,7 +213,7 @@ private TcObject TokenizeMethodOrProperty()
objectType: match.Groups[1].Value,
accessModifier: match.Groups[2].Value,
name: match.Groups[3].Value,
dataType: match.Groups[4].Value,
dataType: UpperDataType(match.Groups[4].Value),
extends: "",
implements: ""
);
Expand Down
43 changes: 43 additions & 0 deletions src/TcBlackTests/ObjectDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,48 @@ public void MethodsWithReturnTypesWithFullPath(
Assert.Equal(expectedCode, var.Format(ref indents));
}

[Theory]
[InlineData(
"METHOD PUBLIC Close : uint;",
"METHOD PUBLIC Close : UINT"
)]
[InlineData(
"METHOD Read:word ",
"METHOD Read : WORD"
)]
[InlineData(
"METHOD Read : string(10) ",
"METHOD Read : STRING(10)"
)]
[InlineData(
"METHOD Read : pointer to int ",
"METHOD Read : POINTER TO INT"
)]
[InlineData(
"METHOD Read : pointer to Custom_Type ",
"METHOD Read : POINTER TO Custom_Type"
)]
[InlineData(
"METHOD Read : pointer to string(3456) ",
"METHOD Read : POINTER TO STRING(3456)"
)]
[InlineData(
"METHOD Read : array[1..100] of udint ",
"METHOD Read : ARRAY[1..100] OF UDINT"
)]
[InlineData(
"METHOD Read : pointer to array[1..100] of udint ",
"METHOD Read : POINTER TO ARRAY[1..100] OF UDINT"
)]
public void MethodsWithStandardReturnTypes(
string originalCode, string expectedCode
)
{
ObjectDefinition var =
new ObjectDefinition(originalCode, " ", "\n");
uint indents = 0;
Assert.Equal(expectedCode, var.Format(ref indents));
}

}
}

0 comments on commit f1bf61c

Please sign in to comment.