Skip to content

Commit

Permalink
Auto upper standard data types (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roald87 committed Jul 12, 2020
2 parents 846613b + 4f271de commit 12ee756
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 7 deletions.
185 changes: 185 additions & 0 deletions src/TcBlack/Keywords.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using System.Text.RegularExpressions;

namespace TcBlack
{
class Keywords
{
protected static MatchEvaluator KeywordsEval = new MatchEvaluator(UpText);
protected static string[] KeywordList =
{
"abs",
"acos",
"add",
"adr",
"and",
"andn",
"any_date",
"any_int",
"any_num",
"any_real",
"any_sintrg",
"any",
"array",
"asin",
"at",
"atan",
"bitadr",
"bool",
"by",
"byte",
"cal",
"calc",
"calcn",
"case",
"constant",
"cos",
"date_and_time",
"date",
"dint",
"div",
"do",
"dt",
"dword",
"else",
"elsif",
"end_case",
"end_for",
"end_if",
"end_repeat",
"end_struct",
"end_type",
"end_var",
"end_while",
"eq",
"exit",
"exp",
"expt",
"false",
"for",
"function_block",
"function",
"ge",
"gt",
"if",
"indexof",
"int",
"jmp",
"jmpc",
"jmpcn",
"ld",
"ldn",
"le",
"lint",
"ln",
"log",
"lreal",
"lt",
"ltime",
"lword",
"max",
"method",
"min",
"mod",
"move",
"mul",
"mux",
"ne",
"not",
"of",
"or",
"orn",
"params",
"persistent",
"pointer",
"program",
"r",
"read_only",
"read_write",
"real",
"reference",
"repeat",
"ret",
"retain",
"retc",
"retcn",
"return",
"rol",
"ror",
"s",
"sel",
"shl",
"shr",
"sin",
"sint",
"sizeof",
"sqrt",
"st",
"stn",
"string",
"struct",
"sub",
"super",
"super",
"tan",
"then",
"this",
"time_of_day",
"time",
"to",
"tod",
"true",
"trunc",
"type",
@"t#(?:\d+(?:ms|s|m|h|d))+",
"udint",
"uint",
"ulint",
"until",
"usint",
"uxint",
"var_config",
"var_external",
"var_global",
"var_in_out",
"var_input",
"var_output",
"var_stat",
"var_temp",
"var",
"while",
"word",
"wstring",
"xint",
"xor",
"xorn",
"xword",
"pvoid",
};
protected static Regex KeywordsRegex = new Regex(
@"(?<!\w)(" + string.Join("|", KeywordList) + @")(?!\w)",
RegexOptions.IgnoreCase
);

protected static Regex SpacingRegex = new Regex(@"\s+");

public Keywords()
{
}

protected static string UpText(Match m)
{
return m.ToString().ToUpper();
}

public static string Upper(string data)
{
return SpacingRegex.Replace(
KeywordsRegex.Replace(
data,
KeywordsEval
),
" "
);
}
}
}
2 changes: 1 addition & 1 deletion src/TcBlack/ObjectDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private TcObject TokenizeMethodOrProperty()
+ (twoModifers ? " " : "")
+ match.Groups[3].Value,
name: match.Groups[4].Value,
dataType: match.Groups[5].Value,
dataType: Keywords.Upper(match.Groups[5].Value),
extends: "",
implements: ""
);
Expand Down
1 change: 1 addition & 0 deletions src/TcBlack/TcBlack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Backup.cs" />
<Compile Include="CompositeCode.cs" />
<Compile Include="EmptyLine.cs" />
<Compile Include="Keywords.cs" />
<Compile Include="ObjectDefinition.cs" />
<Compile Include="ICodeLineOperations.cs" />
<Compile Include="Program.cs" />
Expand Down
14 changes: 11 additions & 3 deletions src/TcBlack/VariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,27 @@ public TcDeclaration Tokenize()
_unformattedCode = Regex.Replace(_unformattedCode, strInitRegex, "");
}

MatchCollection matches = Regex.Matches(_unformattedCode, pattern);
MatchCollection matches = Regex.Matches(
_unformattedCode,
pattern,
RegexOptions.IgnoreCase
);
TcDeclaration variable;
if (matches.Count > 0)
{
match = matches[0];
if (strInit.Length == 0)
{
strInit = RemoveWhiteSpaceIfPossible(match.Groups[4].Value);
strInit = Keywords.Upper(
RemoveWhiteSpaceIfPossible(match.Groups[4].Value)
);
}
variable = new TcDeclaration(
name: RemoveWhiteSpaceIfPossible(match.Groups[1].Value),
allocation: RemoveWhiteSpaceIfPossible(match.Groups[2].Value),
dataType: RemoveWhiteSpaceIfPossible(match.Groups[3].Value),
dataType: Keywords.Upper(
RemoveWhiteSpaceIfPossible(match.Groups[3].Value)
),
initialization: strInit,
comment: match.Groups[5].Value.Trim()
);
Expand Down
52 changes: 51 additions & 1 deletion src/TcBlackTests/ObjectDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,57 @@ 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"
)]
[InlineData(
"METHOD Read : pointer to array[1..nNumber] of int ",
"METHOD Read : POINTER TO ARRAY[1..nNumber] OF INT"
)]
[InlineData(
"METHOD Read : String(nInt) ",
"METHOD Read : STRING(nInt)"
)]
public void MethodsWithStandardReturnTypes(
string originalCode, string expectedCode
)
{
ObjectDefinition var =
new ObjectDefinition(originalCode, " ", "\n");
uint indents = 0;
Assert.Equal(expectedCode, var.Format(ref indents));
}

[Theory]
[InlineData(
"INTERFACE ITF_1",
Expand Down Expand Up @@ -196,6 +247,5 @@ public void FormatInterfaces(
uint indents = 0;
Assert.Equal(expectedCode, var.Format(ref indents));
}

}
}
38 changes: 36 additions & 2 deletions src/TcBlackTests/VariableDeclarationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VariableDeclarationTests
[InlineData("var ", " LREAL ")]
[InlineData(" number ", " DINT")]
[InlineData(" _internalVar4", "DWORD ")]
[InlineData(" SHOUTING ", " int ")]
[InlineData(" SHOUTING ", " INT ")]
[InlineData("aSample1 ", "ARRAY[1..5] OF INT")]
[InlineData("aSample1 ", "ARRAY[1..nInt - 1] OF INT")]
[InlineData("aSample1 ", "ARRAY[nInt + 1..3] OF INT")]
Expand Down Expand Up @@ -179,7 +179,7 @@ private void AssertEquals(TcDeclaration expected, TcDeclaration actual)
"deviceDown AT %QX0.2 : BOOL ; ",
"deviceDown AT %QX0.2 : BOOL;"
)]
[InlineData("devSpeed:TIME:=T#10ms;", "devSpeed : TIME := T#10ms;")]
[InlineData("devSpeed:TIME:=T#10ms;", "devSpeed : TIME := T#10MS;")]
[InlineData(
"fbSample : FB_Sample(nId_Init := 11, fIn_Init := 33.44) ;",
"fbSample : FB_Sample(nId_Init:=11, fIn_Init:=33.44);"
Expand Down Expand Up @@ -305,5 +305,39 @@ public void SpecialCharStringInitialization(string unformattedCode, string expec
uint indents = 0;
Assert.Equal(expected, variable.Format(ref indents));
}

[Theory]
[InlineData(
"devSpeed:TIME:=T#10ms;",
"devSpeed : TIME := T#10MS;"
)]
[InlineData(
"devSpeed:time:=T#2d5h6m1s10ms;",
"devSpeed : TIME := T#2D5H6M1S10MS;"
)]
[InlineData(
"MSG : int := 253;",
"MSG : INT := 253;"
)]
[InlineData(
"SomeArray : array[1..(n * (end + 1) - 4) / initial] OF real;",
"SomeArray : ARRAY[1..(n * (end + 1) - 4) / initial] OF REAL;"
)]
[InlineData(
"ptr : pointer to dword ; ",
"ptr : POINTER TO DWORD;"
)]
[InlineData(
"ptr : pointer to pointer to dword ; ",
"ptr : POINTER TO POINTER TO DWORD;"
)]
public void UpperCaseKeywords(string unformattedCode, string expected)
{
VariableDeclaration variable = new VariableDeclaration(
unformattedCode: unformattedCode, singleIndent: " ", lineEnding: "\n"
);
uint indents = 0;
Assert.Equal(expected, variable.Format(ref indents));
}
}
}

0 comments on commit 12ee756

Please sign in to comment.