diff --git a/SimpleStateMachine.StructuralSearch.Action/Program.cs b/SimpleStateMachine.StructuralSearch.Action/Program.cs index 3e72de0..11b3660 100644 --- a/SimpleStateMachine.StructuralSearch.Action/Program.cs +++ b/SimpleStateMachine.StructuralSearch.Action/Program.cs @@ -1,6 +1,4 @@ -using System.Text; -using SimpleStateMachine.StructuralSearch.Action; -using System.Web; +using SimpleStateMachine.StructuralSearch.Action; using CommandLine; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileSystemGlobbing; diff --git a/src/SimpleStateMachine.StructuralSearch.Sandbox/Expr.cs b/src/SimpleStateMachine.StructuralSearch.Sandbox/Expr.cs index f72d2f4..8a7abb4 100644 --- a/src/SimpleStateMachine.StructuralSearch.Sandbox/Expr.cs +++ b/src/SimpleStateMachine.StructuralSearch.Sandbox/Expr.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Immutable; -using System.Linq; +using System.Collections.Immutable; namespace SimpleStateMachine.StructuralSearch.Sandbox { diff --git a/src/SimpleStateMachine.StructuralSearch.Sandbox/ExprParser.cs b/src/SimpleStateMachine.StructuralSearch.Sandbox/ExprParser.cs index 908866f..ab1502c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Sandbox/ExprParser.cs +++ b/src/SimpleStateMachine.StructuralSearch.Sandbox/ExprParser.cs @@ -1,5 +1,4 @@ -using System; -using Pidgin; +using Pidgin; using Pidgin.Expression; using static Pidgin.Parser; diff --git a/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs b/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs index e434048..a8a6d8a 100644 --- a/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs +++ b/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs @@ -1,19 +1,7 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Text.Json; -using System.Text.RegularExpressions; -using Pidgin; -using SimpleStateMachine.StructuralSearch.Configurations; +using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Helper; -using SimpleStateMachine.StructuralSearch.Rules; -using YamlDotNet.Serialization; -using YamlDotNet.Serialization.NamingConventions; using static Pidgin.Parser; -using String = System.String; namespace SimpleStateMachine.StructuralSearch.Sandbox { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml index 2f78c66..21dc313 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml @@ -6,7 +6,7 @@ $var$ = $value$; } FindRules: - - $sign$ In ("Is", "==", "!=", "is not") + - $sign$ In ("is", "==", "!=", "is not") ReplaceTemplate: |- $var$ ??= $value$; ReplaceRules: @@ -19,12 +19,12 @@ else $var$ = $value1$; FindRules: - - $sign$ In ("Is", "==", "!=", "is not") - - $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") + - $sign$ In ("is", "==", "!=", "is not") + - $value$ In ($value1$, $value2$) ReplaceTemplate: |- $var$ = $value1$ ?? $value2$; - ReplaceRules: - - $sign$ In ("!=", "is not") then $var2$ => $var1$, $var1$ => $var2$ + ReplaceRules: + - $sign$ In ("!=", "is not") then $value2$ => $value1$, $value1$ => $value2$ # TernaryOperator diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/If.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/If.yml new file mode 100644 index 0000000..0de08f3 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/If.yml @@ -0,0 +1,6 @@ +Configurations: + - FindTemplate: if($var1$ == $var2$) + FindRules: + - $var1$ Is Var + - $var2$ is Var + ReplaceTemplate: $var1$, $var2$ \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml index 673dc0e..ad1671e 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml @@ -6,7 +6,7 @@ $var$ = $value$; } FindRules: - - $sign$ In ("Is", "==", "!=", "is not") + - $sign$ In ("is", "==", "!=", "is not") ReplaceTemplate: |- $var$ ??= $value$; @@ -18,12 +18,13 @@ else $var$ = $value1$; FindRules: - - $sign$ In ("Is", "==", "!=", "is not") - - $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") + - $sign$ In ("is", "==", "!=", "is not") + - $value$ In ($value1$, $value2$) ReplaceTemplate: |- $var$ = $value1$ ?? $value2$; - ReplaceRules: - - $sign$ In ("!=", "is not") then $var2$ => $var1$, $var1$ => $var2$ + ReplaceRules: + - $sign$ In ("!=", "is not") then $value2$ => $value1$, $value1$ => $value2$ + # TernaryOperator - FindTemplate: |- diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFileTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFileTests.cs index c889b69..4f4b747 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFileTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFileTests.cs @@ -1,17 +1,12 @@ using System.Collections.Generic; -using System.IO; -using System.Text.Json; -using Pidgin; using SimpleStateMachine.StructuralSearch.Configurations; using SimpleStateMachine.StructuralSearch.Helper; using SimpleStateMachine.StructuralSearch.Tests.Mock; using Xunit; -using YamlDotNet.Serialization; -using YamlDotNet.Serialization.NamingConventions; namespace SimpleStateMachine.StructuralSearch.Tests { - public class ConfigurationFileTests + public class ConfigurationFileParserTests { [Theory] [InlineData("ConfigurationFile/ShortConfig.yml")] diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/NullUnionOperator.cs b/src/SimpleStateMachine.StructuralSearch.Tests/Examples/NullUnionOperator.cs deleted file mode 100644 index 41be29b..0000000 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/NullUnionOperator.cs +++ /dev/null @@ -1,53 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Tests.Examples; - -public class NullUnionOperator -{ - public int Test1() - { - int? temp = 1; - - if(temp is null) - return 3; - else - return 4; - } - - public void Test2() - { - int? result; - int? temp1 = 5; - int? temp2 = 5; - if(temp1 is null) - result = temp2; - else - result = temp1; - } - - public void Test3() - { - int result; - int? temp1 = 5; - int? temp2 = 5; - if(temp1 is null) - result = temp2.Value; - else - result = temp1.Value; - } - - public int Test4() - { - int? temp3 = 5; - - if(temp3 is null) - return 7; - else if (temp3 == 8) - return 9; - else - return 10; - } - - public void Test5() - { - - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common.cs similarity index 81% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common.cs index d8fe2f4..a05456c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common.cs @@ -1,8 +1,4 @@ -using System.Collections.Generic; -using System.IO; -using System.Text.Json; - -namespace SimpleStateMachine.StructuralSearch.Tests.Examples; +namespace SimpleStateMachine.StructuralSearch.Tests.Examples; public class Common { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common2.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs similarity index 75% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common2.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs index 74b39f4..813da5c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common2.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs @@ -1,8 +1,4 @@ -using System.Collections.Generic; -using System.IO; -using System.Text.Json; - -namespace SimpleStateMachine.StructuralSearch.Tests.Examples; +namespace SimpleStateMachine.StructuralSearch.Tests.Examples; public class Common2 { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/NullUnionOperator.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/NullUnionOperator.cs new file mode 100644 index 0000000..b9607aa --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/NullUnionOperator.cs @@ -0,0 +1,40 @@ +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput; + +public class NullUnionOperator +{ + public void Test1() + { + int? result; + int? temp1 = 5; + int? temp2 = 5; + + if(temp1 is null) + result = temp2; + else + result = temp1; + } + + public void Test2() + { + + int? result; + int? temp1 = 6; + int? temp2 = 7; + if(temp1 is not null) + result = temp2; + else + result = temp1; + } + + public int Test3() + { + int? temp3 = 5; + + if(temp3 is null) + return 7; + else if (temp3 == 8) + return 9; + else + return 10; + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/TernaryOperator.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs similarity index 92% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/TernaryOperator.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs index 8cd797b..7ee333a 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/TernaryOperator.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs @@ -1,6 +1,4 @@ -using System; - -namespace SimpleStateMachine.StructuralSearch.Tests.Examples +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput { public class TernaryOperator { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Test.txt b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Test.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs new file mode 100644 index 0000000..7f32c29 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs @@ -0,0 +1,34 @@ +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesOutput; + +public class NullUnionOperator +{ + public void Test1() + { + int? result; + int? temp1 = 5; + int? temp2 = 5; + + result = temp1 ?? temp2; + } + + public void Test2() + { + + int? result; + int? temp1 = 6; + int? temp2 = 7; + result = temp2 ?? temp1; + } + + public int Test3() + { + int? temp3 = 5; + + if(temp3 is null) + return 7; + else if (temp3 == 8) + return 9; + else + return 10; + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/TernaryOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/TernaryOperator.txt new file mode 100644 index 0000000..769b318 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/TernaryOperator.txt @@ -0,0 +1,45 @@ +using System; + +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput +{ + public class TernaryOperator + { + public int Test1() + { + var temp = 1; + + return temp == 2? 3 : 4; + } + + public int Test2() + { + var temp = 5; + + return temp == 6? 7 : 8; + } + + public int Test3() + { + var temp2 = 1; + + return temp2 == 2? 3 : 4; + } + + public int Test4() + { + var temp3 = 5; + + if(temp3 == 6) + return 7; + else if (temp3 == 8) + return 9; + else + return 10; + } + + public void Test5() + { + + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/AssignmentNullUnionOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/AssignmentNullUnionOperator.txt index 3afde7b..3b4ba68 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/AssignmentNullUnionOperator.txt +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/AssignmentNullUnionOperator.txt @@ -1 +1 @@ -$sign$ In ("Is", "==", "!=", "is not") \ No newline at end of file +$sign$ In ("is", "==", "!=", "is not") \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/NullUnionOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/NullUnionOperator.txt index c626822..856b04e 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/NullUnionOperator.txt +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/NullUnionOperator.txt @@ -1,2 +1,2 @@ -$sign$ In ("Is", "==", "!=", "is not") -$value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") \ No newline at end of file +$sign$ In ("is", "==", "!=", "is not") +$value$ In ($value1$, $value2$) \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs index 7fd2678..d03aad3 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs @@ -1,8 +1,6 @@ -using System; -using System.IO; +using System.IO; using System.Linq; using Pidgin; -using SimpleStateMachine.StructuralSearch.Rules; using Xunit; namespace SimpleStateMachine.StructuralSearch.Tests @@ -25,9 +23,11 @@ public class FindRuleParserTests [InlineData("$var$ StartsWith \"123\"")] [InlineData("$var$ EndsWith $var$.Lenght")] [InlineData("$var$ EndsWith \"123\"")] - [InlineData("$var$ Match $var$.Lenght")] + // [InlineData("$var$ Match $var$.Lenght")] [InlineData("$var$ Is Int")] [InlineData("$var$ Is DateTime")] + [InlineData("$var$ equals $var1$ or $var2$ equals $var1$")] + [InlineData("$var$ match \"[a-b]+\"")] public void FindRuleExprParsingShouldBeSuccess(string ruleStr) { var rule = FindRuleParser.Expr.ParseOrThrow(ruleStr); @@ -62,8 +62,8 @@ public void FindRuleExprParsingShouldBeEqualsCustomResult(int number, string rul Assert.Equal(_ruleStr, customResult.ToLower()); } [Theory] - [InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"Is\",\"==\",\"!=\",\"is not\"", "$value$ In $value1$,\"$value1$\\.Value\",$value2$,\"$value2$\\.Value\"")] - [InlineData("FindRule/AssignmentNullUnionOperator.txt", "$sign$ In \"Is\",\"==\",\"!=\",\"is not\"")] + [InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"", "$value$ In $value1$,$value2$")] + [InlineData("FindRule/AssignmentNullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"")] public void FindRuleParsingFromFileShouldBeSuccess(string filePath, params string[] customResult) { var ruleStr = File.ReadAllText(filePath); diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ParameterParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ParameterParserTests.cs index ac51d46..fccc50e 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ParameterParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ParameterParserTests.cs @@ -55,4 +55,20 @@ public void StringFormatParameterParsingShouldBeFail(string str) { Assert.Throws(() => ParametersParser.StringFormatParameter.ParseOrThrow(str)); } + + [Theory] + [InlineData("$var$")] + [InlineData("$var$.Trim")] + [InlineData("$var$.Lenght")] + [InlineData("$var$.Lenght.Trim")] + [InlineData("$var$.RemoveSubStr(\"123\")")] + [InlineData("$var$.Lenght.RemoveSubStr(\"123\")")] + [InlineData("$var$.Lenght.Trim.RemoveSubStr(\"123\")")] + public void ParameterParsingShouldBeSuccess(string str) + { + var parameter = ParametersParser.Parameter.ParseOrThrow(str); + var parameterStr = parameter.ToString().ToLower(); + Assert.Equal(parameterStr.ToLower(), str.ToLower()); + } + } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs index 1ce567c..85441fe 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using Microsoft.VisualBasic; +using System.Linq; using Pidgin; using Xunit; diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRule/NullUnionOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRule/NullUnionOperator.txt index 082363d..580587c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRule/NullUnionOperator.txt +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRule/NullUnionOperator.txt @@ -1 +1 @@ -$sign$ In ("!=", "is not") then $var2$ => $var1$, $var1$ => $var2$ \ No newline at end of file +$sign$ In ("!=", "is not") then $value2$ => $value1$, $value1$ => $value2$ \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs index 0100154..f8a95b0 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs @@ -1,5 +1,4 @@ -using System.Linq; -using Pidgin; +using Pidgin; using Xunit; namespace SimpleStateMachine.StructuralSearch.Tests diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs index 6395ef0..c294ed7 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs @@ -1,8 +1,6 @@ -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Linq; using Pidgin; -using SimpleStateMachine.StructuralSearch.Tests.Mock; using Xunit; namespace SimpleStateMachine.StructuralSearch.Tests diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj b/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj index 59c8bae..c1d25e0 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj +++ b/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj @@ -25,7 +25,10 @@ - + + Always + + Always diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs index 0a341b0..0fc0a77 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs @@ -1,5 +1,8 @@ -using System.IO; +using System; +using System.IO; using System.Linq; +using SimpleStateMachine.StructuralSearch.Configurations; +using SimpleStateMachine.StructuralSearch.Helper; using SimpleStateMachine.StructuralSearch.Tests.Mock; using Xunit; @@ -9,8 +12,8 @@ public class StructuralSearchParserTests { [Theory] // [InlineData("AssignmentNullUnionOperator")] - [InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)] - [InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)] + [InlineData("NullUnionOperator", "ExamplesInput/NullUnionOperator.cs", 2)] + [InlineData("TernaryOperator", "ExamplesInput/TernaryOperator.cs", 3)] public static void StructuralSearchShouldBeSuccess(string exampleName, string exampleFilePath, int matchesCount) { var config = ConfigurationMock.GetConfigurationFromFiles(exampleName); @@ -23,5 +26,90 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex var matches = parser.Parse(ref context); Assert.Equal(matches.Count(), matchesCount); } + + [Theory] + // [InlineData("AssignmentNullUnionOperator")] + [InlineData("NullUnionOperator", 2)] + [InlineData("TernaryOperator", 3)] + public static void StructuralSearchShouldBe(string exampleName, int matchesCount) + { + var startTime = System.Diagnostics.Stopwatch.StartNew(); + var config = ConfigurationMock.GetConfigurationFromFiles(exampleName); + var inputFilePath = Path.Combine($"ExamplesInput/{exampleName}.cs"); + var resultFilePath = Path.Combine($"ExamplesOutput/{exampleName}.txt"); + var outputFilePath = Path.Combine($"{exampleName}.cs"); + + var parser = new StructuralSearchParser(config); + + var inputFileInfo = new FileInfo(inputFilePath); + var input = Input.File(inputFileInfo); + IParsingContext context = new ParsingContext(input); + var matches = parser.Parse(ref context); + matches = parser.ApplyFindRule(ref context, matches); + matches = parser.ApplyReplaceRule(ref context, matches); + var replaceMatches = parser.GetReplaceMatches(ref context, matches); + var outputFileInfo = new FileInfo(outputFilePath); + var output = Output.File(outputFileInfo); + output.Replace(input, replaceMatches); + startTime.Stop(); + var resultTime = startTime.Elapsed; + + // Assert.Equal(matches.Count(), matchesCount); + // + // var resultStr = File.ReadAllText(resultFilePath); + // var outputStr = File.ReadAllText(outputFilePath); + // Assert.Equal(resultStr, outputStr); + } + + [Theory] + [InlineData("ConfigurationFile/if.yml")] + public static void StructuralSearchFromConfig(string configPath) + { + var config = YmlHelper.Parse(configPath).Configurations.First(); + var inputFileInfo = new FileInfo("ExamplesInput/Test.txt"); + + int count = 100; + long result = 0; + for (int i = 0; i < count; i++) + { + var res = Test(config, inputFileInfo); + Console.WriteLine(res); + result += res; + } + var t = result/count; + Console.WriteLine($"SR: {t}"); + } + + private static long Test(Configuration config, FileInfo inputFileInfo) + { + var startTime = System.Diagnostics.Stopwatch.StartNew(); + var input = Input.File(inputFileInfo); + var parser = new StructuralSearchParser(config); + + IParsingContext context = new ParsingContext(input); + var matches = parser.Parse(ref context); + matches = parser.ApplyFindRule(ref context, matches); + matches = parser.ApplyReplaceRule(ref context, matches); + var replaceMatches = parser.GetReplaceMatches(ref context, matches); + startTime.Stop(); + var resultTime = startTime.Elapsed; + return resultTime.Ticks; + } + + // private static long Test(Configuration config, FileInfo inputFileInfo) + // { + // var startTime = System.Diagnostics.Stopwatch.StartNew(); + // var input = Input.File(inputFileInfo); + // var parser = new StructuralSearchParser(config); + // + // IParsingContext context = new ParsingContext(input); + // var matches = parser.Parse(ref context); + // matches = parser.ApplyFindRule(ref context, matches); + // matches = parser.ApplyReplaceRule(ref context, matches); + // var replaceMatches = parser.GetReplaceMatches(ref context, matches); + // startTime.Stop(); + // var resultTime = startTime.Elapsed; + // return resultTime.Ticks; + // } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs b/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs index 4cdf031..244bd77 100644 --- a/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs +++ b/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using SimpleStateMachine.StructuralSearch.Helper; namespace SimpleStateMachine.StructuralSearch.Configurations diff --git a/src/SimpleStateMachine.StructuralSearch/Constant.cs b/src/SimpleStateMachine.StructuralSearch/Constant.cs index 452e6a3..4b446e2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Constant.cs +++ b/src/SimpleStateMachine.StructuralSearch/Constant.cs @@ -1,7 +1,4 @@ -using System; -using System.Linq; - -namespace SimpleStateMachine.StructuralSearch +namespace SimpleStateMachine.StructuralSearch { public static partial class Constant { diff --git a/src/SimpleStateMachine.StructuralSearch/Custom/StreamReplacer.cs b/src/SimpleStateMachine.StructuralSearch/Custom/StreamReplacer.cs index 474e309..20260ce 100644 --- a/src/SimpleStateMachine.StructuralSearch/Custom/StreamReplacer.cs +++ b/src/SimpleStateMachine.StructuralSearch/Custom/StreamReplacer.cs @@ -1,7 +1,4 @@ -using System.IO; -using System.Threading.Tasks; - -namespace SimpleStateMachine.StructuralSearch.Custom; +namespace SimpleStateMachine.StructuralSearch.Custom; // public class StreamReplacer : Stream // { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs similarity index 86% rename from src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs rename to src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs index 6aec55c..c80ddd0 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace SimpleStateMachine.StructuralSearch.Tests.Mock +namespace SimpleStateMachine.StructuralSearch { public class EmptyParsingContext : IParsingContext { @@ -26,7 +26,7 @@ public IReadOnlyDictionary SwitchOnNew() throw new System.NotImplementedException(); } - public void Set(IReadOnlyDictionary placeholders) + public void Fill(IReadOnlyDictionary placeholders) { throw new System.NotImplementedException(); } diff --git a/src/SimpleStateMachine.StructuralSearch/FindParser.cs b/src/SimpleStateMachine.StructuralSearch/FindParser.cs index 106aa6a..af066c5 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParser.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; -using System.IO; +using System.Collections.Generic; using System.Linq; using System.Text; using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; -using YamlDotNet.Core.Events; namespace SimpleStateMachine.StructuralSearch { @@ -17,9 +14,9 @@ public FindParser(SeriesParser parser) Parser = parser; } - public IEnumerable Parse(ref IParsingContext context) + public IEnumerable Parse(ref IParsingContext context) { - List matches = new(); + List matches = new(); StringBuilder res = new(); Parser.SetContext(ref context); @@ -29,7 +26,7 @@ public IEnumerable Parse(ref IParsingContext context) .ThenInvoke(match => { var placeholders= parsingContext.SwitchOnNew(); - matches.Add(new FindParserMatch(match, placeholders)); + matches.Add(new FindParserResult(match, placeholders)); }) .ThenReturn(Unit.Value) .Try(); diff --git a/src/SimpleStateMachine.StructuralSearch/FindParserResult.cs b/src/SimpleStateMachine.StructuralSearch/FindParserResult.cs index bba7b74..4ec3e63 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParserResult.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParserResult.cs @@ -2,4 +2,4 @@ namespace SimpleStateMachine.StructuralSearch; -public readonly record struct FindParserMatch(Match Match, IReadOnlyDictionary Placeholders); \ No newline at end of file +public readonly record struct FindParserResult(Match Match, IReadOnlyDictionary Placeholders); \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/StreamExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Helper/StreamExtensions.cs index 4d9015b..41bdc22 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/StreamExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/StreamExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.Contracts; using System.IO; namespace SimpleStateMachine.StructuralSearch.Helper; diff --git a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs index 04ce8a2..4396976 100644 --- a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs @@ -1,12 +1,9 @@ using System.Collections.Generic; -using System.IO; -using Pidgin; -using SimpleStateMachine.StructuralSearch.ReplaceTemplate; namespace SimpleStateMachine.StructuralSearch { public interface IFindParser { - IEnumerable Parse(ref IParsingContext context); + IEnumerable Parse(ref IParsingContext context); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/IParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/IParsingContext.cs index 65740f7..689b032 100644 --- a/src/SimpleStateMachine.StructuralSearch/IParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/IParsingContext.cs @@ -13,7 +13,7 @@ public interface IParsingContext IPlaceholder GetPlaceholder(string name); IReadOnlyDictionary SwitchOnNew(); - void Set(IReadOnlyDictionaryplaceholders); + void Fill(IReadOnlyDictionaryplaceholders); void Clear(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/EmptyInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/EmptyInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs diff --git a/src/SimpleStateMachine.StructuralSearch/FileInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs similarity index 64% rename from src/SimpleStateMachine.StructuralSearch/FileInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs index d6d66c7..63384e5 100644 --- a/src/SimpleStateMachine.StructuralSearch/FileInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs @@ -1,7 +1,5 @@ using System.IO; -using System.Threading; using Pidgin; -using SimpleStateMachine.StructuralSearch.Helper; namespace SimpleStateMachine.StructuralSearch { @@ -21,12 +19,16 @@ public Result ParseBy(Parser parser) public void Replace(Match match, string value) { - using var valueReader = new StringReader(value); - using var streamWriter = new StreamWriter(FileInfo.FullName); - var emptyStart = match.Offset.Start + match.Lenght; - var emptyEnd = match.Offset.End; - valueReader.CopyPartTo(streamWriter, match.Offset.Start, match.Lenght); - + var text = File.ReadAllText(Path); + text = text.Replace(match.Value, value); + File.WriteAllText(Path, text); + + // using var valueReader = new StringReader(value); + // using var streamWriter = new StreamWriter(FileInfo.FullName); + // var emptyStart = match.Offset.Start + match.Lenght; + // var emptyEnd = match.Offset.End; + // valueReader.CopyPartTo(streamWriter, match.Offset.Start, match.Lenght); + //return (emptyStart, emptyEnd); } diff --git a/src/SimpleStateMachine.StructuralSearch/IInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/IInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/IInput.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Input.cs b/src/SimpleStateMachine.StructuralSearch/Input/Input.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Input.cs rename to src/SimpleStateMachine.StructuralSearch/Input/Input.cs diff --git a/src/SimpleStateMachine.StructuralSearch/StringInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/StringInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Output/FileOutput.cs b/src/SimpleStateMachine.StructuralSearch/Output/FileOutput.cs new file mode 100644 index 0000000..8d119ad --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Output/FileOutput.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace SimpleStateMachine.StructuralSearch; + +public class FileOutput : IOutput +{ + public readonly FileInfo FileInfo; + + public FileOutput(FileInfo fileInfo) + { + FileInfo = fileInfo; + } + + public void Replace(IInput input, IEnumerable replaceMatches) + { + var text = replaceMatches + .Aggregate(input.Data, (current, replaceMatch) => + current.Replace(replaceMatch.Match.Value, replaceMatch.Value)); + + File.WriteAllText(Path, text); + } + + public string Extension => FileInfo.Extension; + public string Path => System.IO.Path.GetFullPath(FileInfo.FullName); + public string Name => System.IO.Path.GetFileNameWithoutExtension(FileInfo.Name); +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Output/IOutput.cs b/src/SimpleStateMachine.StructuralSearch/Output/IOutput.cs new file mode 100644 index 0000000..3354bc8 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Output/IOutput.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; + +namespace SimpleStateMachine.StructuralSearch; + +public interface IOutput +{ + void Replace(IInput input, IEnumerable replaceMatches); +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Output/Output.cs b/src/SimpleStateMachine.StructuralSearch/Output/Output.cs new file mode 100644 index 0000000..b77c590 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Output/Output.cs @@ -0,0 +1,8 @@ +using System.IO; + +namespace SimpleStateMachine.StructuralSearch; + +public static class Output +{ + public static IOutput File(FileInfo fileInfo) => new FileOutput(fileInfo); +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs index f5b8a16..590d324 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs @@ -2,16 +2,17 @@ namespace SimpleStateMachine.StructuralSearch { - public class EmptyStringParser:Parser + public class EmptyStringParser : Parser { public bool Value { get; } - + public EmptyStringParser(bool value) { Value = value; } - - public override bool TryParse(ref ParseState state, ref PooledList> expecteds, out string result) + + public override bool TryParse(ref ParseState state, ref PooledList> expecteds, + out string result) { result = string.Empty; return Value; diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/LookaheadParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/LookaheadParser.cs index 14df294..40910ab 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/LookaheadParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/LookaheadParser.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using Pidgin; +using Pidgin; namespace SimpleStateMachine.StructuralSearch { diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs index df34438..9f7da64 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using Pidgin; -using SimpleStateMachine.StructuralSearch.Extensions; namespace SimpleStateMachine.StructuralSearch { diff --git a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs index 0736c79..e90bc0e 100644 --- a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs @@ -5,6 +5,8 @@ namespace SimpleStateMachine.StructuralSearch { public class ParsingContext : IParsingContext { + public static IParsingContext Empty = new EmptyParsingContext(); + public ParsingContext(IInput input) { Input = input; @@ -37,7 +39,7 @@ public IReadOnlyDictionary SwitchOnNew() return placeholders; } - public void Set(IReadOnlyDictionary placeholders) + public void Fill(IReadOnlyDictionary placeholders) { Clear(); diff --git a/src/SimpleStateMachine.StructuralSearch/Placeholder/MatchPosition.cs b/src/SimpleStateMachine.StructuralSearch/Placeholder/MatchPosition.cs index d80ed3a..b22ac81 100644 --- a/src/SimpleStateMachine.StructuralSearch/Placeholder/MatchPosition.cs +++ b/src/SimpleStateMachine.StructuralSearch/Placeholder/MatchPosition.cs @@ -1,6 +1,4 @@ -using System; - -namespace SimpleStateMachine.StructuralSearch +namespace SimpleStateMachine.StructuralSearch { public readonly record struct Match(T Value, int Lenght, ColumnPosition Column, LinePosition Line, OffsetPosition Offset); diff --git a/src/SimpleStateMachine.StructuralSearch/Placeholder/ReplacedPlaceholder.cs b/src/SimpleStateMachine.StructuralSearch/Placeholder/ReplacedPlaceholder.cs new file mode 100644 index 0000000..b73ba57 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Placeholder/ReplacedPlaceholder.cs @@ -0,0 +1,23 @@ +namespace SimpleStateMachine.StructuralSearch; + +public class ReplacedPlaceholder : IPlaceholder +{ + public ReplacedPlaceholder(IPlaceholder placeholder, string value) + { + Name = placeholder.Name; + Value = value; + Lenght = value.Length; + Line = placeholder.Line; + Column = placeholder.Column; + Offset = placeholder.Offset; + Input = placeholder.Input; + } + + public string Name { get; } + public string Value { get; } + public int Lenght { get; } + public LinePosition Line { get; } + public ColumnPosition Column { get; } + public OffsetPosition Offset { get; } + public IInput Input { get; } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/RepalceMatch.cs b/src/SimpleStateMachine.StructuralSearch/RepalceMatch.cs new file mode 100644 index 0000000..506388f --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/RepalceMatch.cs @@ -0,0 +1,3 @@ +namespace SimpleStateMachine.StructuralSearch; + +public readonly record struct ReplaceMatch(Match Match, string Value); \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs index 925df7f..be73e89 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs @@ -28,6 +28,12 @@ public bool Execute() public override string ToString() { return $"{Left}{Constant.Space}{Type}{Constant.Space}{Right}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Left.SetContext(ref context); + Right.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs index e4d5f5e..4697df0 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs @@ -20,8 +20,8 @@ public BinarySubRule(SubRuleType type, IRuleParameter left, IRuleParameter right public bool Execute() { - var left = Right.GetValue(); - var right = Left.GetValue(); + var left = Left.GetValue(); + var right = Right.GetValue(); return Type switch { @@ -37,6 +37,12 @@ public bool Execute() public override string ToString() { return $"{Left}{Constant.Space}{Type}{Constant.Space}{Right}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Left.SetContext(ref context); + Right.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs index 143e65e..d18eafe 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs @@ -10,5 +10,10 @@ public bool Execute() public override string ToString() { return $"{Constant.Underscore}"; - } + } + + public void SetContext(ref IParsingContext context) + { + + } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs index 1ef83c1..1d2f9b3 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs @@ -1,6 +1,6 @@ namespace SimpleStateMachine.StructuralSearch.Rules { - public interface IRule + public interface IRule : IContextDependent { bool Execute(); } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs index 1b74255..ce8b2ae 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs @@ -19,12 +19,28 @@ public InSubRule(IRuleParameter parameter, IEnumerable arguments public bool Execute() { var value = Parameter.GetValue(); - return Arguments.Any(parameter => Equals(value, parameter.GetValue())); + var result = Arguments.Any(parameter => + { + var value_ = parameter.GetValue(); + var equal = Equals(value, value_); + return equal; + }); + return result; } public override string ToString() { return $"{Parameter}{Constant.Space}{SubRuleType.In}{Constant.Space}{string.Join(Constant.Comma, Arguments.Select(x=>x.ToString()))}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + + foreach (var argument in Arguments) + { + argument.SetContext(ref context); + } + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs index e076ac1..636450f 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs @@ -33,6 +33,11 @@ public bool Execute() public override string ToString() { return $"{Parameter}{Constant.Space}{SubRuleType.Is}{Constant.Space}{Argument}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs index d7af616..7d1ca29 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs @@ -28,6 +28,11 @@ public bool Execute() public override string ToString() { return $"{Type}{Constant.Space}{Parameter}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs index 889dd2e..ecb0836 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs @@ -1,6 +1,6 @@ namespace SimpleStateMachine.StructuralSearch.Rules { - public interface IRuleParameter + public interface IRuleParameter : IContextDependent { string GetValue(); } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs index bfb0c61..f8915a4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs @@ -26,6 +26,14 @@ public override string ToString() return string.Format(Template, string.Join(string.Empty, Parameters.Select(x=> x.ToString()))); } + public void SetContext(ref IParsingContext context) + { + foreach (var parameter in Parameters) + { + parameter.SetContext(ref context); + } + } + private static string GetTemplate(ParenthesisType parenthesisType) { return parenthesisType switch diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs index e9d79ff..b88e6f7 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs @@ -30,6 +30,11 @@ public string GetValue() public override string ToString() { return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Column}{Constant.Dote}{Property}"; - } + } + + public void SetContext(ref IParsingContext context) + { + PlaceholderParameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs index c1a6746..9545ddd 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs @@ -30,6 +30,11 @@ public string GetValue() public override string ToString() { return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.File}{Constant.Dote}{Property}"; - } + } + + public void SetContext(ref IParsingContext context) + { + PlaceholderParameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs index 9a45b67..621e520 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs @@ -1,6 +1,4 @@ -using System; - -namespace SimpleStateMachine.StructuralSearch.Rules +namespace SimpleStateMachine.StructuralSearch.Rules { public class PlaceholderLenghtParameter: IRuleParameter { @@ -21,6 +19,11 @@ public string GetValue() public override string ToString() { return $"{PlaceholderParameter}{Constant.Dote}{Property}"; - } + } + + public void SetContext(ref IParsingContext context) + { + PlaceholderParameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs index eef3e0d..7b8cefc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs @@ -30,6 +30,11 @@ public string GetValue() public override string ToString() { return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Line}{Constant.Dote}{Property}"; - } + } + + public void SetContext(ref IParsingContext context) + { + PlaceholderParameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs index 8cb9b4a..2f6454f 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs @@ -29,6 +29,11 @@ public string GetValue() public override string ToString() { return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Offset}{Constant.Dote}{Property}"; - } + } + + public void SetContext(ref IParsingContext context) + { + PlaceholderParameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs index 02d2d5f..8393359 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs @@ -1,6 +1,6 @@ namespace SimpleStateMachine.StructuralSearch.Rules { - public class PlaceholderParameter : IRuleParameter, IContextDependent + public class PlaceholderParameter : IRuleParameter { private IParsingContext _context; diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs index 7daafea..3a0f9de 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs @@ -11,7 +11,7 @@ public static class PlaceholderPropertyParser public static readonly Parser> File = Parsers.EnumValue(PlaceholderProperty.File, true) .Then(CommonParser.Dote) - .Then(Parser.CIEnum()) + .Then(Parser.CIEnum()) .Select(property => new Func(placeholder => new PlaceholderFileParameter(placeholder, property))) .Try(); @@ -19,7 +19,7 @@ public static class PlaceholderPropertyParser public static readonly Parser> Column = Parsers.EnumValue(PlaceholderProperty.Column, true) .Then(CommonParser.Dote) - .Then(Parser.CIEnum()) + .Then(Parser.CIEnum()) .Select(property => new Func(placeholder => new PlaceholderColumnParameter(placeholder, property))) .Try(); @@ -27,7 +27,7 @@ public static class PlaceholderPropertyParser public static readonly Parser> Line = Parsers.EnumValue(PlaceholderProperty.Line, true) .Then(CommonParser.Dote) - .Then(Parser.CIEnum()) + .Then(Parser.CIEnum()) .Select(property => new Func(placeholder => new PlaceholderLineParameter(placeholder, property))) .Try(); @@ -35,7 +35,7 @@ public static class PlaceholderPropertyParser public static readonly Parser> Offset = Parsers.EnumValue(PlaceholderProperty.Offset, true) .Then(CommonParser.Dote) - .Then(Parser.CIEnum()) + .Then(Parser.CIEnum()) .Select(property => new Func(placeholder => new PlaceholderOffsetParameter(placeholder, property))) .Try(); @@ -47,10 +47,11 @@ public static class PlaceholderPropertyParser .Try(); public static readonly Parser> PlaceholderPropertyParameter = - CommonParser.Dote.Then(Parser.OneOf(Lenght, File, Column, Offset, Line)).Optional() + CommonParser.Dote.Then(Parser.OneOf(Lenght, File, Column, Offset, Line)) + .Try() + .Optional() .Select(property => new Func(placeholder => - property.HasValue ? property.Value(placeholder) : placeholder)) - .Try(); + property.HasValue ? property.Value(placeholder) : placeholder)); // public static readonly Parser PlaceholderPropertyParameter = // CommonTemplateParser.Placeholder.Before(CommonParser.Dote) diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs index d2dd438..77275cd 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs @@ -20,6 +20,14 @@ public string GetValue() public override string ToString() { return $"{Constant.DoubleQuotes}{string.Join(string.Empty, Parameters.Select(x=> x.ToString()))}{Constant.DoubleQuotes}"; - } + } + + public void SetContext(ref IParsingContext context) + { + foreach (var parameter in Parameters) + { + parameter.SetContext(ref context); + } + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs index b94bf9e..0ca302e 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs @@ -19,6 +19,11 @@ public override string ToString() var value = EscapeHelper.EscapeChars(Value, c => $"{Constant.BackSlash}{c}", Constant.Parameter.Escape); return $"{value}"; - } + } + + public void SetContext(ref IParsingContext context) + { + + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs new file mode 100644 index 0000000..e450325 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs @@ -0,0 +1,40 @@ +using System; +using SimpleStateMachine.StructuralSearch.Rules; + +namespace SimpleStateMachine.StructuralSearch; + +public class ChangeBinaryParameter : IRuleParameter +{ + public IRuleParameter Parameter { get; } + public ChangeType Type { get; } + + public ChangeBinaryParameter(IRuleParameter parameter, ChangeType type) + { + Parameter = parameter; + Type = type; + } + + public string GetValue() + { + var value = Parameter.GetValue(); + return Type switch + { + ChangeType.Trim => value.Trim(), + ChangeType.TrimEnd => value.TrimEnd(), + ChangeType.TrimStart => value.TrimStart(), + ChangeType.ToUpper => value.ToUpper(), + ChangeType.ToLower => value.ToLower(), + _ => throw new ArgumentOutOfRangeException() + }; + } + + public override string ToString() + { + return $"{Parameter}{Constant.Dote}{Type}"; + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryType.cs new file mode 100644 index 0000000..efbca09 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryType.cs @@ -0,0 +1,6 @@ +namespace SimpleStateMachine.StructuralSearch; + +public enum ChangeBinaryType +{ + Replace = 0 +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs index b5c4b1e..7c4edd2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -31,6 +31,11 @@ public string GetValue() public override string ToString() { return $"{Parameter}{Constant.Dote}{Type}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs new file mode 100644 index 0000000..b6293e6 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs @@ -0,0 +1,40 @@ +using System; +using SimpleStateMachine.StructuralSearch.Rules; + +namespace SimpleStateMachine.StructuralSearch; + +public class ChangeUnaryParameter : IRuleParameter +{ + public IRuleParameter Parameter { get; } + public ChangeUnaryType Type { get; } + public IRuleParameter Arg { get; } + + public ChangeUnaryParameter(IRuleParameter parameter, ChangeUnaryType type, IRuleParameter arg) + { + Parameter = parameter; + Type = type; + Arg = arg; + } + + public string GetValue() + { + var parameter = Parameter.GetValue(); + var arg = Arg.GetValue(); + return Type switch + { + ChangeUnaryType.RemoveSubStr => parameter.Replace(arg, string.Empty), + _ => throw new ArgumentOutOfRangeException() + }; + } + + public override string ToString() + { + return $"{Parameter}{Constant.Dote}{Type}{Constant.LeftParenthesis}{Arg}{Constant.RightParenthesis}"; + } + + public void SetContext(ref IParsingContext context) + { + Parameter.SetContext(ref context); + Arg.SetContext(ref context); + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryType.cs new file mode 100644 index 0000000..441468e --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryType.cs @@ -0,0 +1,6 @@ +namespace SimpleStateMachine.StructuralSearch; + +public enum ChangeUnaryType +{ + RemoveSubStr = 0 +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 88de028..71eca54 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -1,26 +1,33 @@ using System.Collections.Generic; -using System.Reflection.Metadata; -using Pidgin; -using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Rules; namespace SimpleStateMachine.StructuralSearch { - public class ReplaceRule + public class ReplaceRule: IContextDependent { - public IRule FindRule { get; } + public IRule ConditionRule { get; } public IEnumerable Rules { get; } - public ReplaceRule(IRule findRule, IEnumerable rules) + public ReplaceRule(IRule conditionRule, IEnumerable rules) { - FindRule = findRule; + ConditionRule = conditionRule; Rules = rules; } public override string ToString() { - return $"{FindRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; - } + return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; + } + + public void SetContext(ref IParsingContext context) + { + ConditionRule.SetContext(ref context); + + foreach (var rule in Rules) + { + rule.SetContext(ref context); + } + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs index ef09293..4a93579 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs @@ -2,7 +2,7 @@ namespace SimpleStateMachine.StructuralSearch; -public class ReplaceSubRule +public class ReplaceSubRule: IContextDependent { public PlaceholderParameter Placeholder { get; } public IRuleParameter Parameter { get; } @@ -16,5 +16,11 @@ public ReplaceSubRule(PlaceholderParameter placeholder, IRuleParameter parameter public override string ToString() { return $"{Placeholder}{Constant.Space}{Constant.Should}{Constant.Space}{Parameter}"; - } + } + + public void SetContext(ref IParsingContext context) + { + Placeholder.SetContext(ref context); + Parameter.SetContext(ref context); + } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs index 85bf00a..283e996 100644 --- a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Pidgin; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs index 5d5a6e9..0788510 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs @@ -1,6 +1,5 @@ using System; using Pidgin; -using SimpleStateMachine.StructuralSearch.Extensions; using static Pidgin.Parser; namespace SimpleStateMachine.StructuralSearch { diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs index 78982fd..1653bd3 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; using Pidgin; using Pidgin.Expression; using SimpleStateMachine.StructuralSearch.Extensions; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs index 31a1135..795ff7e 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs @@ -13,11 +13,11 @@ static ParametersParser() { // TODO optimize Parenthesised = Parsers.BetweenOneOfChars(x => Parser.Char(x).Try(), - Parser.Rec(() => StringWithParenthesised), - Constant.AllParenthesised) + Parser.Rec(() => StringWithParenthesised), + Constant.AllParenthesised) .Try() .Labelled($"{nameof(ParametersParser)}.{nameof(Parenthesised)}"); - + StringWithParenthesised = Parser.OneOf(Parenthesised, String) .Optional() .Select(x => x.HasValue ? x.Value : Enumerable.Empty()) @@ -31,9 +31,11 @@ static ParametersParser() .As() .Try() .Labelled($"{nameof(ParametersParser)}.{nameof(StringParameter)}"); - + + var placeholderOrPropertyRuleParameter = Parser.Rec(() => PlaceholderOrPropertyRuleParameter); + StringFormatParameter = - Parser.OneOf(PlaceholderOrPropertyRuleParameter, StringParameter) + Parser.OneOf(placeholderOrPropertyRuleParameter, StringParameter) .AtLeastOnce() .Between(CommonParser.DoubleQuotes) .Select(parameters => new StringFormatParameter(parameters)) @@ -41,11 +43,31 @@ static ParametersParser() // .TrimStart() .Try(); - Parameter = Parser.OneOf(StringFormatParameter, PlaceholderOrPropertyRuleParameter) - // .TrimStart() - .Try(); + Parameter = Parser.OneOf(StringFormatParameter, placeholderOrPropertyRuleParameter) + // .TrimStart() + .Try(); Parameters = Parameter.Trim().SeparatedAtLeastOnce(CommonParser.Comma.Trim()); + + ChangeUnaryParameter = Parser.Map((type, arg) => (type, arg), + Parser.CIEnum(), + CommonParser.Parenthesised(Parameter, Parser.Char)) + .Select(pair => new Func(placeholder => + new ChangeUnaryParameter(placeholder, pair.type, pair.arg))) + .Try() + .Labelled($"{nameof(ParametersParser)}.{nameof(ChangeUnaryParameter)}") + ; + + Change = CommonParser.Dote.Then(Parser.OneOf(ChangeParameter, ChangeUnaryParameter)) + .Many() + .Select(funcs => + new Func(placeholder => + funcs.Aggregate(placeholder, (parameter, func) => func(parameter)))); ; + + PlaceholderOrPropertyRuleParameter = PlaceholderParameter + .Then(PlaceholderPropertyParser.PlaceholderPropertyParameter, (placeholder, func) => func(placeholder)) + .Then(Change, (parameter, func) => func(parameter)) + .Try(); } public static readonly Parser> String = @@ -53,34 +75,58 @@ static ParametersParser() .Or(Parser.AnyCharExcept(Constant.Parameter.Excluded)) .AtLeastOnce() .Labelled($"{nameof(ParametersParser)}.{nameof(String)}"); - + public static readonly Parser StringParameter; - + public static readonly Parser PlaceholderParameter = CommonTemplateParser.Placeholder .Select(x => new PlaceholderParameter(x)) // .TrimStart() .Try() .Labelled($"{nameof(ParametersParser)}.{nameof(PlaceholderParameter)}"); - - public static readonly Parser> ChangeParameter = - CommonParser.Dote.Then(Parser.CIEnum()) - .Optional() - .Select(changeType => new Func(placeholder => - changeType.HasValue ? new ChangeParameter(placeholder, changeType.Value) : placeholder)) - .Try() - .Labelled($"{nameof(ParametersParser)}.{nameof(ChangeParameter)}"); - - public static readonly Parser PlaceholderOrPropertyRuleParameter = - PlaceholderParameter.Then(PlaceholderPropertyParser.PlaceholderPropertyParameter, - (placeholder, func) => func(placeholder)) - .Then(ChangeParameter, (parameter, func) => func(parameter)) + + + // public static readonly Parser> ChangeParameter = + // CommonParser.Dote.Then(Parser.CIEnum()) + // .Optional() + // .Select(changeType => new Func(placeholder => + // changeType.HasValue ? new ChangeParameter(placeholder, changeType.Value) : placeholder)) + // .Try() + // .Labelled($"{nameof(ParametersParser)}.{nameof(ChangeParameter)}"); + // + // public static readonly Parser> ChangeUnaryParameter = + // CommonParser.Dote.Then(Parser.Map( + // (type, arg) => (type, arg), + // Parser.CIEnum(), + // CommonParser.Parenthesised(Parser.Rec(() => PlaceholderOrPropertyRuleParameter), Parser.Char))) + // .Optional() + // .Select(pair => new Func(placeholder => + // pair.HasValue + // ? new ChangeUnaryParameter(placeholder, pair.Value.type, pair.Value.arg) + // : placeholder)) + // .Try() + // .Labelled($"{nameof(ParametersParser)}.{nameof(ChangeUnaryParameter)}"); + + private static readonly Parser> ChangeParameter = + Parser.CIEnum() + .Select(changeType => + new Func( + placeholder => new ChangeParameter(placeholder, changeType))) .Try() - .Labelled($"{nameof(ParametersParser)}.{nameof(PlaceholderOrPropertyRuleParameter)}"); + .Labelled($"{nameof(ParametersParser)}.{nameof(ChangeParameter)}") + ; + + private static readonly Parser> ChangeUnaryParameter; + + public static readonly Parser> Change; + + public static readonly Parser PlaceholderOrProperty; + + public static readonly Parser PlaceholderOrPropertyRuleParameter; + - public static readonly Parser> StringWithParenthesised; - + public static readonly Parser> Parenthesised; public static readonly Parser StringFormatParameter; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs index 947279a..27b9261 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using Pidgin; +using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Rules; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs index 5439307..3be7771 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; +using System.Collections.Generic; using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Helper; @@ -24,7 +21,7 @@ static ReplaceTemplateParser() .Try(); Parameter = Parser.OneOf(ParenthesisedParameter, ParametersParser.Parameter, ParametersParser.StringParameter) - .Then(ParametersParser.ChangeParameter, (parameter, func) => func(parameter)) + .Then(ParametersParser.Change, (parameter, func) => func(parameter)) .Try(); diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs index f2904e8..4b38916 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs @@ -1,5 +1,4 @@ -using Pidgin; -using SimpleStateMachine.StructuralSearch.ReplaceTemplate; +using SimpleStateMachine.StructuralSearch.ReplaceTemplate; using SimpleStateMachine.StructuralSearch.Rules; namespace SimpleStateMachine.StructuralSearch diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs index 6fe66de..820a826 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using Pidgin; +using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; namespace SimpleStateMachine.StructuralSearch.Rules diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 86b0f96..f0d41bc 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -1,6 +1,5 @@  using System.Collections.Generic; -using System.IO; using System.Linq; using SimpleStateMachine.StructuralSearch.Configurations; using SimpleStateMachine.StructuralSearch.Extensions; @@ -13,11 +12,11 @@ public class StructuralSearchParser { public IFindParser FindParser { get; set; } - public IEnumerable FindRules { get; set; } + public IReadOnlyList FindRules { get; set; } public IReplaceBuilder ReplaceBuilder { get; set; } - public IEnumerable ReplaceRules { get; set; } + public IReadOnlyList ReplaceRules { get; set; } public StructuralSearchParser(Configuration configuration) { @@ -25,27 +24,29 @@ public StructuralSearchParser(Configuration configuration) FindRules = configuration.FindRules .EmptyIfNull() - .Select(StructuralSearch.ParseFindRule); + .Select(StructuralSearch.ParseFindRule).ToList(); - ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate); + if(!string.IsNullOrEmpty(configuration.ReplaceTemplate)) + ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate); ReplaceRules = configuration.ReplaceRules .EmptyIfNull() - .Select(StructuralSearch.ParseReplaceRule); + .Select(StructuralSearch.ParseReplaceRule).ToList(); } - public IEnumerable Parse(ref IParsingContext context) + public IEnumerable Parse(ref IParsingContext context) { var matches = FindParser.Parse(ref context); return matches; } - public IEnumerable ApplyFindRule(ref IParsingContext context, IEnumerable matches) + public IEnumerable ApplyFindRule(ref IParsingContext context, IEnumerable matches) { - var result = new List(); + var result = new List(); + SetFindRulesContext(ref context); foreach (var match in matches) { - context.Set(match.Placeholders); + context.Fill(match.Placeholders); var all = FindRules.All(x => x.Execute()); if (all) { @@ -53,36 +54,84 @@ public IEnumerable ApplyFindRule(ref IParsingContext context, I } } + SetFindRulesContext(ref ParsingContext.Empty); return result; } - public void ApplyReplaceRule(ref IParsingContext context, IEnumerable matches) + public IEnumerable ApplyReplaceRule(ref IParsingContext context, IEnumerable matches) { - // var result = new List(); - // foreach (var match in matches) - // { - // context.Set(match.Placeholders); - // - // var rules = ReplaceRules - // .Where(x => x.FindRule.Execute()) - // .SelectMany(x => x.Rules); - // - // var placeHolder = match.Placeholders - // .ToDictionary(x=> x.Key, x=> x.Value); - // - // foreach (var rule in rules) - // { - // placeHolder[rule.Placeholder.Name].Value - // } - // } - // - // return result; + SetReplaceRulesContext(ref context); + var result = new List(); + + foreach (var match in matches) + { + var placeholders = match.Placeholders + .ToDictionary(x => x.Key, + x => x.Value); + + context.Fill(match.Placeholders); + + var rules = ReplaceRules + .Where(x => + { + var result = x.ConditionRule.Execute(); + return result; + }) + .SelectMany(x => x.Rules); + + foreach (var rule in rules) + { + var name = rule.Placeholder.Name; + var placeholder = placeholders[name]; + var value = rule.Parameter.GetValue(); + placeholders[name] = new ReplacedPlaceholder(placeholder, value); + } + + result.Add(match with { Placeholders = placeholders }); + } + + SetReplaceRulesContext(ref ParsingContext.Empty); + + return result; } - public void Replace(FindParserMatch context) + public ReplaceMatch GetReplaceMatch(ref IParsingContext context, FindParserResult parserResult) { + context.Fill(parserResult.Placeholders); + var replaceText = ReplaceBuilder.Build(context); + return new ReplaceMatch(parserResult.Match, replaceText); + // context.Input.Replace(parserResult.Match, replaceText); //ReplaceBuilder.Build(context); // context. //FindParser.Parse(context, context.File.Data); } + public IEnumerable GetReplaceMatches(ref IParsingContext context, IEnumerable parserResults) + { + var replaceMatches = new List(); + foreach (var parserResult in parserResults) + { + var replaceMatch = GetReplaceMatch(ref context, parserResult); + replaceMatches.Add(replaceMatch); + } + + return replaceMatches; + } + + + private void SetFindRulesContext(ref IParsingContext context) + { + foreach (var findRule in FindRules) + { + if(findRule is IContextDependent contextDependent) + contextDependent.SetContext(ref context); + } + } + private void SetReplaceRulesContext(ref IParsingContext context) + { + foreach (var replaceRule in ReplaceRules) + { + if(replaceRule is IContextDependent contextDependent) + contextDependent.SetContext(ref context); + } + } } } \ No newline at end of file