From 7406eebb2743648621fe8ab904135dd9c9823308 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Wed, 25 May 2022 01:11:19 +0300 Subject: [PATCH 1/5] Apply replace rules and replace for files --- .../Mock/EmptyParsingContext.cs | 2 +- .../StructurSearchParserTests.cs | 20 ++++++ .../FindParser.cs | 6 +- .../FindParserResult.cs | 2 +- .../IFindParser.cs | 2 +- .../IParsingContext.cs | 2 +- .../{ => Input}/EmptyInput.cs | 2 +- .../{ => Input}/FileInput.cs | 18 +++-- .../{ => Input}/IInput.cs | 2 +- .../{ => Input}/StringInput.cs | 2 +- .../ParsingContext.cs | 2 +- .../Placeholder/ReplacedPlaceholder.cs | 23 ++++++ .../Rules/ReplaceRule/ReplaceRule.cs | 8 +-- .../StructuralSearchParser.cs | 72 ++++++++++++------- 14 files changed, 115 insertions(+), 48 deletions(-) rename src/SimpleStateMachine.StructuralSearch/{ => Input}/EmptyInput.cs (88%) rename src/SimpleStateMachine.StructuralSearch/{ => Input}/FileInput.cs (61%) rename src/SimpleStateMachine.StructuralSearch/{ => Input}/IInput.cs (84%) rename src/SimpleStateMachine.StructuralSearch/{ => Input}/StringInput.cs (91%) create mode 100644 src/SimpleStateMachine.StructuralSearch/Placeholder/ReplacedPlaceholder.cs diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs b/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs index 6aec55c..4c1d77b 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs @@ -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.Tests/StructurSearchParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs index 0a341b0..b5d3c3f 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs @@ -23,5 +23,25 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex var matches = parser.Parse(ref context); Assert.Equal(matches.Count(), matchesCount); } + + [Theory] + // [InlineData("AssignmentNullUnionOperator")] + [InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)] + [InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)] + public static void StructuralSearchShouldBe(string exampleName, string exampleFilePath, int matchesCount) + { + var config = ConfigurationMock.GetConfigurationFromFiles(exampleName); + var parser = new StructuralSearchParser(config); + + var fileInfo = new FileInfo(exampleFilePath); + var input = Input.File(fileInfo); + IParsingContext context = new ParsingContext(input); + var matches = parser.Parse(ref context); + matches = parser.ApplyFindRule(ref context, matches); + matches = parser.ApplyReplaceRule(ref context, matches); + parser.Replace(ref context, matches); + + Assert.Equal(matches.Count(), matchesCount); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/FindParser.cs b/src/SimpleStateMachine.StructuralSearch/FindParser.cs index 106aa6a..50366ef 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParser.cs @@ -17,9 +17,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 +29,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/IFindParser.cs b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs index 04ce8a2..244cdd1 100644 --- a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs @@ -7,6 +7,6 @@ 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 88% rename from src/SimpleStateMachine.StructuralSearch/EmptyInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs index 1649fb4..6b60c12 100644 --- a/src/SimpleStateMachine.StructuralSearch/EmptyInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs @@ -9,7 +9,7 @@ public Result ParseBy(Parser parser) throw new System.NotImplementedException(); } - public void Replace(Match match, string value) + public void ReplaceAsync(Match match, string value) { throw new System.NotImplementedException(); } diff --git a/src/SimpleStateMachine.StructuralSearch/FileInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs similarity index 61% rename from src/SimpleStateMachine.StructuralSearch/FileInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs index d6d66c7..f1af583 100644 --- a/src/SimpleStateMachine.StructuralSearch/FileInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs @@ -19,14 +19,18 @@ public Result ParseBy(Parser parser) return parser.Parse(FileInfo.OpenText()); } - public void Replace(Match match, string value) + public void ReplaceAsync(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 84% rename from src/SimpleStateMachine.StructuralSearch/IInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/IInput.cs index 7350a93..4c00320 100644 --- a/src/SimpleStateMachine.StructuralSearch/IInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs @@ -5,7 +5,7 @@ namespace SimpleStateMachine.StructuralSearch public interface IInput { Result ParseBy(Parser parser); - void Replace(Match match, string value); + void ReplaceAsync(Match match, string value); string Extension { get; } string Path { get; } diff --git a/src/SimpleStateMachine.StructuralSearch/StringInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs similarity index 91% rename from src/SimpleStateMachine.StructuralSearch/StringInput.cs rename to src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs index 7f30966..13bec1a 100644 --- a/src/SimpleStateMachine.StructuralSearch/StringInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs @@ -16,7 +16,7 @@ public Result ParseBy(Parser parser) return parser.Parse(Input); } - public void Replace(Match match, string value) + public void ReplaceAsync(Match match, string value) { throw new System.NotImplementedException(); } diff --git a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs index 0736c79..414e5dc 100644 --- a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs @@ -37,7 +37,7 @@ public IReadOnlyDictionary SwitchOnNew() return placeholders; } - public void Set(IReadOnlyDictionary placeholders) + public void Fill(IReadOnlyDictionary placeholders) { Clear(); 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/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 88de028..558a329 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -8,19 +8,19 @@ namespace SimpleStateMachine.StructuralSearch { public class ReplaceRule { - 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)}"; } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 86b0f96..2e26d94 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -34,18 +34,18 @@ public StructuralSearchParser(Configuration configuration) .Select(StructuralSearch.ParseReplaceRule); } - 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(); foreach (var match in matches) { - context.Set(match.Placeholders); + context.Fill(match.Placeholders); var all = FindRules.All(x => x.Execute()); if (all) { @@ -55,34 +55,54 @@ public IEnumerable ApplyFindRule(ref IParsingContext context, I 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; + 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 => x.ConditionRule.Execute()) + .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 }); + } + + return result; } - public void Replace(FindParserMatch context) + public void Replace(ref IParsingContext context, FindParserResult parserResult) { + context.Fill(parserResult.Placeholders); + var replaceText = ReplaceBuilder.Build(context); + context.Input.ReplaceAsync(parserResult.Match, replaceText); + //ReplaceBuilder.Build(context); + // context. + //FindParser.Parse(context, context.File.Data); + } + public void Replace(ref IParsingContext context, IEnumerable parserResults) + { + foreach (var parserResult in parserResults) + { + Replace(ref context, parserResult); + } + //ReplaceBuilder.Build(context); // context. //FindParser.Parse(context, context.File.Data); } - } } \ No newline at end of file From fb78bd7bd440eded6cf58a3e62a5d9fca1465bc6 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Wed, 25 May 2022 03:18:13 +0300 Subject: [PATCH 2/5] fix tests --- .../ConfigurationFile/FullConfig.yml | 6 +++--- .../ConfigurationFile/ShortConfig.yml | 4 ++-- .../FindRule/AssignmentNullUnionOperator.txt | 2 +- .../FindRule/NullUnionOperator.txt | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml index 2f78c66..fcd1c6a 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,8 +19,8 @@ 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$, "$value$\.Value", $value2$, "$value2$\.Value") ReplaceTemplate: |- $var$ = $value1$ ?? $value2$; ReplaceRules: diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml index 673dc0e..4204fca 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,7 +18,7 @@ else $var$ = $value1$; FindRules: - - $sign$ In ("Is", "==", "!=", "is not") + - $sign$ In ("is", "==", "!=", "is not") - $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") ReplaceTemplate: |- $var$ = $value1$ ?? $value2$; 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..a915aa5 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") +$sign$ In ("is", "==", "!=", "is not") $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") \ No newline at end of file From 2a714a6aa739b84ad48b7ecc33e075a0253e2c81 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Wed, 25 May 2022 03:20:55 +0300 Subject: [PATCH 3/5] Output interface, All rules and parameters is IContextDependent --- .../FindRuleParserTests.cs | 4 +- .../StructurSearchParserTests.cs | 17 ++++--- .../EmptyParsingContext.cs | 2 +- .../Input/EmptyInput.cs | 2 +- .../Input/FileInput.cs | 2 +- .../Input/IInput.cs | 2 +- .../{ => Input}/Input.cs | 0 .../Input/StringInput.cs | 2 +- .../Output/FileOutput.cs | 28 +++++++++++ .../Output/IOutput.cs | 8 +++ .../Output/Output.cs | 8 +++ .../ParsingContext.cs | 2 + .../RepalceMatch.cs | 3 ++ .../Rules/FindRule/BinaryRule.cs | 8 ++- .../Rules/FindRule/BinarySubRule.cs | 8 ++- .../Rules/FindRule/EmptySubRule.cs | 7 ++- .../Rules/FindRule/IRule.cs | 2 +- .../Rules/FindRule/InSubRule.cs | 20 +++++++- .../Rules/FindRule/IsSubRule.cs | 7 ++- .../Rules/FindRule/UnaryRule.cs | 7 ++- .../Rules/Parameters/IRuleParameter.cs | 2 +- .../Parameters/ParenthesisedParameter.cs | 8 +++ .../Parameters/PlaceholderColumnParameter.cs | 7 ++- .../Parameters/PlaceholderFileParameter.cs | 7 ++- .../Parameters/PlaceholderLenghtParameter.cs | 7 ++- .../Parameters/PlaceholderLineParameter.cs | 7 ++- .../Parameters/PlaceholderOffsetParameter.cs | 7 ++- .../Rules/Parameters/PlaceholderParameter.cs | 2 +- .../Rules/Parameters/StringFormatParameter.cs | 10 +++- .../Rules/Parameters/StringParameter.cs | 7 ++- .../Rules/ReplaceRule/ChangeParameter.cs | 9 +++- .../Rules/ReplaceRule/ReplaceRule.cs | 14 +++++- .../Rules/ReplaceRule/ReplaceSubRule.cs | 10 +++- .../StructuralSearchParser.cs | 49 ++++++++++++++----- 34 files changed, 237 insertions(+), 48 deletions(-) rename src/{SimpleStateMachine.StructuralSearch.Tests/Mock => SimpleStateMachine.StructuralSearch}/EmptyParsingContext.cs (94%) rename src/SimpleStateMachine.StructuralSearch/{ => Input}/Input.cs (100%) create mode 100644 src/SimpleStateMachine.StructuralSearch/Output/FileOutput.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Output/IOutput.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Output/Output.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/RepalceMatch.cs diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs index 7fd2678..3cef0c0 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs @@ -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$,\"$value1$\\.Value\",$value2$,\"$value2$\\.Value\"")] + [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/StructurSearchParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs index b5d3c3f..41e33ce 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs @@ -26,21 +26,26 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex [Theory] // [InlineData("AssignmentNullUnionOperator")] - [InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)] - [InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)] - public static void StructuralSearchShouldBe(string exampleName, string exampleFilePath, int matchesCount) + [InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", "Examples/Test.cs", 2)] + [InlineData("TernaryOperator", "Examples/TernaryOperator.cs", "", 3)] + public static void StructuralSearchShouldBe(string exampleName, string inputFilePath, string outputFilePath, int matchesCount) { + // TODO fix NullUnion example + var config = ConfigurationMock.GetConfigurationFromFiles(exampleName); var parser = new StructuralSearchParser(config); - var fileInfo = new FileInfo(exampleFilePath); - var input = Input.File(fileInfo); + 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); - parser.Replace(ref context, matches); + var replaceMatches = parser.GetReplaceMatches(ref context, matches); + var outputFileInfo = new FileInfo(outputFilePath); + var output = Output.File(outputFileInfo); + output.Replace(input, replaceMatches); Assert.Equal(matches.Count(), matchesCount); } } diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs similarity index 94% rename from src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs rename to src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs index 4c1d77b..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 { diff --git a/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs index 6b60c12..1649fb4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs @@ -9,7 +9,7 @@ public Result ParseBy(Parser parser) throw new System.NotImplementedException(); } - public void ReplaceAsync(Match match, string value) + public void Replace(Match match, string value) { throw new System.NotImplementedException(); } diff --git a/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs index f1af583..aab3dbe 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs @@ -19,7 +19,7 @@ public Result ParseBy(Parser parser) return parser.Parse(FileInfo.OpenText()); } - public void ReplaceAsync(Match match, string value) + public void Replace(Match match, string value) { var text = File.ReadAllText(Path); text = text.Replace(match.Value, value); diff --git a/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs index 4c00320..7350a93 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/IInput.cs @@ -5,7 +5,7 @@ namespace SimpleStateMachine.StructuralSearch public interface IInput { Result ParseBy(Parser parser); - void ReplaceAsync(Match match, string value); + void Replace(Match match, string value); string Extension { get; } string Path { get; } 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/Input/StringInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs index 13bec1a..7f30966 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs @@ -16,7 +16,7 @@ public Result ParseBy(Parser parser) return parser.Parse(Input); } - public void ReplaceAsync(Match match, string value) + public void Replace(Match match, string value) { throw new System.NotImplementedException(); } 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/ParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs index 414e5dc..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; 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..4819cf9 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs @@ -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..9baf912 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs @@ -21,6 +21,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/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/ChangeParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs index b5c4b1e..41613f2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -3,7 +3,7 @@ namespace SimpleStateMachine.StructuralSearch { - public class ChangeParameter : IRuleParameter + public class ChangeParameter : IRuleParameter, IContextDependent { public IRuleParameter Parameter { get; } public ChangeType Type { get; } @@ -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/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 558a329..406d1f9 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -6,7 +6,7 @@ namespace SimpleStateMachine.StructuralSearch { - public class ReplaceRule + public class ReplaceRule: IContextDependent { public IRule ConditionRule { get; } @@ -21,6 +21,16 @@ public ReplaceRule(IRule conditionRule, IEnumerable rules) public override string ToString() { 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/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 2e26d94..69c4a11 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -13,11 +13,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,13 +25,13 @@ public StructuralSearchParser(Configuration configuration) FindRules = configuration.FindRules .EmptyIfNull() - .Select(StructuralSearch.ParseFindRule); + .Select(StructuralSearch.ParseFindRule).ToList(); ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate); ReplaceRules = configuration.ReplaceRules .EmptyIfNull() - .Select(StructuralSearch.ParseReplaceRule); + .Select(StructuralSearch.ParseReplaceRule).ToList(); } public IEnumerable Parse(ref IParsingContext context) @@ -43,6 +43,7 @@ public IEnumerable Parse(ref IParsingContext context) public IEnumerable ApplyFindRule(ref IParsingContext context, IEnumerable matches) { var result = new List(); + SetFindRulesContext(ref context); foreach (var match in matches) { context.Fill(match.Placeholders); @@ -53,10 +54,12 @@ public IEnumerable ApplyFindRule(ref IParsingContext context, } } + SetFindRulesContext(ref ParsingContext.Empty); return result; } public IEnumerable ApplyReplaceRule(ref IParsingContext context, IEnumerable matches) { + SetReplaceRulesContext(ref context); var result = new List(); foreach (var match in matches) @@ -82,27 +85,49 @@ public IEnumerable ApplyReplaceRule(ref IParsingContext contex result.Add(match with { Placeholders = placeholders }); } + SetReplaceRulesContext(ref ParsingContext.Empty); + return result; } - public void Replace(ref IParsingContext context, FindParserResult parserResult) + public ReplaceMatch GetReplaceMatch(ref IParsingContext context, FindParserResult parserResult) { context.Fill(parserResult.Placeholders); var replaceText = ReplaceBuilder.Build(context); - context.Input.ReplaceAsync(parserResult.Match, replaceText); + return new ReplaceMatch(parserResult.Match, replaceText); + // context.Input.Replace(parserResult.Match, replaceText); //ReplaceBuilder.Build(context); // context. //FindParser.Parse(context, context.File.Data); } - public void Replace(ref IParsingContext context, IEnumerable parserResults) + public IEnumerable GetReplaceMatches(ref IParsingContext context, IEnumerable parserResults) { + var replaceMatches = new List(); foreach (var parserResult in parserResults) { - Replace(ref context, parserResult); + 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); } - - //ReplaceBuilder.Build(context); - // context. - //FindParser.Parse(context, context.File.Data); } } } \ No newline at end of file From df40c8a9b9f979f66fa5c0268ba751edeea82a79 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Thu, 26 May 2022 02:59:04 +0300 Subject: [PATCH 4/5] Fix NullUnion example and rulls, new parameter functions --- .../ConfigurationFile/FullConfig.yml | 6 +- .../ConfigurationFile/ShortConfig.yml | 7 +- .../Examples/NullUnionOperator.cs | 53 ---------- .../{Examples => ExamplesInput}/Common.cs | 0 .../{Examples => ExamplesInput}/Common2.cs | 0 .../ExamplesInput/NullUnionOperator.cs | 40 ++++++++ .../TernaryOperator.cs | 2 +- .../ExamplesOutput/NullUnionOperator.txt | 34 +++++++ .../ExamplesOutput/TernaryOperator.txt | 45 +++++++++ .../FindRule/NullUnionOperator.txt | 2 +- .../FindRuleParserTests.cs | 5 +- .../ParameterParserTests.cs | 16 +++ .../ReplaceRule/NullUnionOperator.txt | 2 +- ...StateMachine.StructuralSearch.Tests.csproj | 5 +- .../StructurSearchParserTests.cs | 20 ++-- .../Parsers/EmptyStringParser.cs | 9 +- .../Rules/FindRule/BinarySubRule.cs | 4 +- .../Parameters/PlaceholderPropertyParser.cs | 15 +-- .../ReplaceRule/ChangeBinaryParameter.cs | 40 ++++++++ .../Rules/ReplaceRule/ChangeBinaryType.cs | 6 ++ .../Rules/ReplaceRule/ChangeParameter.cs | 2 +- .../Rules/ReplaceRule/ChangeUnaryParameter.cs | 40 ++++++++ .../Rules/ReplaceRule/ChangeUnaryType.cs | 6 ++ .../StructuralSearch/ParametersParser.cs | 98 ++++++++++++++----- .../StructuralSearch/ReplaceTemplateParser.cs | 2 +- .../StructuralSearchParser.cs | 6 +- 26 files changed, 351 insertions(+), 114 deletions(-) delete mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/Examples/NullUnionOperator.cs rename src/SimpleStateMachine.StructuralSearch.Tests/{Examples => ExamplesInput}/Common.cs (100%) rename src/SimpleStateMachine.StructuralSearch.Tests/{Examples => ExamplesInput}/Common2.cs (100%) create mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/NullUnionOperator.cs rename src/SimpleStateMachine.StructuralSearch.Tests/{Examples => ExamplesInput}/TernaryOperator.cs (93%) create mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt create mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/TernaryOperator.txt create mode 100644 src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryType.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryType.cs diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml index fcd1c6a..21dc313 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml @@ -20,11 +20,11 @@ $var$ = $value1$; FindRules: - $sign$ In ("is", "==", "!=", "is not") - - $value$ In ($value1$, "$value$\.Value", $value2$, "$value2$\.Value") + - $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/ShortConfig.yml b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml index 4204fca..ad1671e 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml @@ -19,11 +19,12 @@ $var$ = $value1$; FindRules: - $sign$ In ("is", "==", "!=", "is not") - - $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value") + - $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/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 100% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common.cs diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common2.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/Common2.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs 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 93% rename from src/SimpleStateMachine.StructuralSearch.Tests/Examples/TernaryOperator.cs rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs index 8cd797b..e0ff7fa 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/Examples/TernaryOperator.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs @@ -1,6 +1,6 @@ using System; -namespace SimpleStateMachine.StructuralSearch.Tests.Examples +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput { public class TernaryOperator { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt new file mode 100644 index 0000000..f996818 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt @@ -0,0 +1,34 @@ +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput; + +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/NullUnionOperator.txt b/src/SimpleStateMachine.StructuralSearch.Tests/FindRule/NullUnionOperator.txt index a915aa5..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 +$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 3cef0c0..33ae085 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs @@ -25,9 +25,10 @@ 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$")] public void FindRuleExprParsingShouldBeSuccess(string ruleStr) { var rule = FindRuleParser.Expr.ParseOrThrow(ruleStr); @@ -62,7 +63,7 @@ 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/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) { 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/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/SimpleStateMachine.StructuralSearch.Tests.csproj b/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj index 59c8bae..159ced3 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 41e33ce..f7cdfeb 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs @@ -9,8 +9,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); @@ -26,13 +26,15 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex [Theory] // [InlineData("AssignmentNullUnionOperator")] - [InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", "Examples/Test.cs", 2)] - [InlineData("TernaryOperator", "Examples/TernaryOperator.cs", "", 3)] - public static void StructuralSearchShouldBe(string exampleName, string inputFilePath, string outputFilePath, int matchesCount) + [InlineData("NullUnionOperator", 2)] + [InlineData("TernaryOperator", 3)] + public static void StructuralSearchShouldBe(string exampleName, int matchesCount) { - // TODO fix NullUnion example - 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); @@ -47,6 +49,10 @@ public static void StructuralSearchShouldBe(string exampleName, string inputFile var output = Output.File(outputFileInfo); output.Replace(input, replaceMatches); Assert.Equal(matches.Count(), matchesCount); + + var resultStr = File.ReadAllText(resultFilePath); + var outputStr = File.ReadAllText(outputFilePath); + Assert.Equal(resultStr, outputStr); } } } \ 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/Rules/FindRule/BinarySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs index 4819cf9..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 { 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/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 41613f2..7c4edd2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -3,7 +3,7 @@ namespace SimpleStateMachine.StructuralSearch { - public class ChangeParameter : IRuleParameter, IContextDependent + public class ChangeParameter : IRuleParameter { public IRuleParameter Parameter { get; } public ChangeType Type { get; } 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/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/ReplaceTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs index 5439307..4cd4f49 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs @@ -24,7 +24,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/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 69c4a11..de0ba71 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -71,7 +71,11 @@ public IEnumerable ApplyReplaceRule(ref IParsingContext contex context.Fill(match.Placeholders); var rules = ReplaceRules - .Where(x => x.ConditionRule.Execute()) + .Where(x => + { + var result = x.ConditionRule.Execute(); + return result; + }) .SelectMany(x => x.Rules); foreach (var rule in rules) From 721588731c10a72d68368d409e7d145544eba377 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Sat, 3 Sep 2022 23:23:56 +0400 Subject: [PATCH 5/5] fix error, remove unused namespaces --- .../Program.cs | 4 +- .../Expr.cs | 4 +- .../ExprParser.cs | 3 +- .../Program.cs | 14 +--- .../ConfigurationFile/If.yml | 6 ++ .../ConfigurationFileTests.cs | 7 +- .../ExamplesInput/Common.cs | 6 +- .../ExamplesInput/Common2.cs | 6 +- .../ExamplesInput/TernaryOperator.cs | 4 +- .../ExamplesInput/Test.txt | 0 ...UnionOperator.txt => NullUnionOperator.cs} | 2 +- .../FindRuleParserTests.cs | 5 +- .../PlaceholderParserTests.cs | 4 +- .../ReplaceRuleParserTests.cs | 3 +- .../ReplaceTemplateTests.cs | 4 +- ...StateMachine.StructuralSearch.Tests.csproj | 2 +- .../StructurSearchParserTests.cs | 71 +++++++++++++++++-- .../Configurations/Configuration.cs | 1 - .../Constant.cs | 5 +- .../Custom/StreamReplacer.cs | 5 +- .../FindParser.cs | 5 +- .../Helper/StreamExtensions.cs | 1 - .../IFindParser.cs | 3 - .../Input/FileInput.cs | 2 - .../Parsers/LookaheadParser.cs | 3 +- .../Parsers/ParserWithLookahead.cs | 2 - .../Placeholder/MatchPosition.cs | 4 +- .../Parameters/PlaceholderLenghtParameter.cs | 4 +- .../Rules/ReplaceRule/ReplaceRule.cs | 3 - .../SeriesParser.cs | 3 +- .../StructuralSearch/CommonParser.cs | 1 - .../StructuralSearch/FindRulesParser.cs | 1 - .../StructuralSearch/ReplaceRuleParser.cs | 4 +- .../StructuralSearch/ReplaceTemplateParser.cs | 5 +- .../StructuralSearch/StructuralSearch.cs | 3 +- .../StructuralSearch/SubRuleParser.cs | 4 +- .../StructuralSearchParser.cs | 4 +- 37 files changed, 98 insertions(+), 110 deletions(-) create mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/If.yml create mode 100644 src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Test.txt rename src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/{NullUnionOperator.txt => NullUnionOperator.cs} (88%) 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/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/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/ExamplesInput/Common.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common.cs index d8fe2f4..a05456c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/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/ExamplesInput/Common2.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/Common2.cs index 74b39f4..813da5c 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/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/TernaryOperator.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs index e0ff7fa..7ee333a 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesInput/TernaryOperator.cs @@ -1,6 +1,4 @@ -using System; - -namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput +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.txt b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs similarity index 88% rename from src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt rename to src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs index f996818..7f32c29 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.txt +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ExamplesOutput/NullUnionOperator.cs @@ -1,4 +1,4 @@ -namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesInput; +namespace SimpleStateMachine.StructuralSearch.Tests.ExamplesOutput; public class NullUnionOperator { diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs index 33ae085..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 @@ -29,6 +27,7 @@ public class FindRuleParserTests [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); 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/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 159ced3..c1d25e0 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj +++ b/src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj @@ -28,7 +28,7 @@ Always - + Always diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs index f7cdfeb..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; @@ -30,6 +33,7 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex [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"); @@ -44,15 +48,68 @@ public static void StructuralSearchShouldBe(string exampleName, int matchesCount 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); - Assert.Equal(matches.Count(), matchesCount); - - var resultStr = File.ReadAllText(resultFilePath); - var outputStr = File.ReadAllText(outputFilePath); - Assert.Equal(resultStr, outputStr); + 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/FindParser.cs b/src/SimpleStateMachine.StructuralSearch/FindParser.cs index 50366ef..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 { 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 244cdd1..4396976 100644 --- a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs @@ -1,7 +1,4 @@ using System.Collections.Generic; -using System.IO; -using Pidgin; -using SimpleStateMachine.StructuralSearch.ReplaceTemplate; namespace SimpleStateMachine.StructuralSearch { diff --git a/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs index aab3dbe..63384e5 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/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 { 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/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/Rules/Parameters/PlaceholderLenghtParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs index 9baf912..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 { diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 406d1f9..71eca54 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -1,7 +1,4 @@ using System.Collections.Generic; -using System.Reflection.Metadata; -using Pidgin; -using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Rules; namespace SimpleStateMachine.StructuralSearch 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/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 4cd4f49..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; 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 de0ba71..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; @@ -27,7 +26,8 @@ public StructuralSearchParser(Configuration configuration) .EmptyIfNull() .Select(StructuralSearch.ParseFindRule).ToList(); - ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate); + if(!string.IsNullOrEmpty(configuration.ReplaceTemplate)) + ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate); ReplaceRules = configuration.ReplaceRules .EmptyIfNull()