diff --git a/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs b/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs index b15e60b..e80b601 100644 --- a/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs +++ b/src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs @@ -13,12 +13,14 @@ internal static class Program { static void Main(string[] args) { - var tesds = ParametersParser.StringFormatParameter.ParseOrThrow("\"tfasdfa\\\"sd$var$.Lenght\""); - var rule = StructuralSearch.ParseFindRule("$var$ equals $var$.Lenght and Not StartsWith \"123\""); + //var tesds = ParametersParser.StringFormatParameter.ParseOrThrow("\"tfasdfa\\\"sd$var$.Lenght\""); + var replaceRule = StructuralSearch.ParseReplaceRule("$var$ equals $var$ => $var$.Trim"); + + var rule = StructuralSearch.ParseFindRule("$var$ equals $var$.Lenght and Not StartsWith \"123\\\" $var$ \\\"\""); var rule2 = StructuralSearch.ParseFindRule("$var$ equals $var$.Offset.Start and Not StartsWith \"123\""); - var result1 = rule.Execute("test"); - var result2 = rule.Execute("10"); - var result3 = rule.Execute("5.3"); + // var result1 = rule.Execute("test"); + // var result2 = rule.Execute("10"); + // var result3 = rule.Execute("5.3"); var t = ExprParser.ParseOrThrow("2 + 2 + 2"); var resw = t.Invoke(); diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs new file mode 100644 index 0000000..4ba60ab --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs @@ -0,0 +1,24 @@ +using System.Linq; +using Pidgin; +using Xunit; + +namespace SimpleStateMachine.StructuralSearch.Tests +{ + public class FindRuleParserTests + { + [Theory] + [InlineData("equals $var$")] + [InlineData("equals \"\\$\"")] + [InlineData("Not equals $var$.Lenght")] + [InlineData("Not equals $var$.offset.Start")] + [InlineData("equals $var$.Lenght and Not StartsWith \"123\"")] + [InlineData("equals $var$.Lenght and Not StartsWith \"\\\"Test\"")] + public void FindRuleParsingShouldBeSuccess(string ruleStr) + { + var rule = FindRuleParser.Expr.ParseOrThrow(ruleStr); + var _ruleStr = rule.ToString()?.ToLower(); + Assert.NotNull(rule); + Assert.Equal(_ruleStr, ruleStr.ToLower()); + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs new file mode 100644 index 0000000..d9a0d95 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs @@ -0,0 +1,26 @@ +using System.Linq; +using Pidgin; +using Xunit; + +namespace SimpleStateMachine.StructuralSearch.Tests +{ + public class ReplaceRuleParserTests + { + [Theory] + [InlineData("equals $var$", "$var$")] + [InlineData("equals \"\\$\"", "\"\\$\"")] + [InlineData("Not equals $var$.Lenght", "$var$.Lenght")] + [InlineData("Not equals $var$.offset.Start", "$var$.offset.Start")] + [InlineData("equals $var$.Lenght and Not StartsWith \"123\"", "$var$.offset.Start.Trim")] + [InlineData("equals $var$.Lenght and Not StartsWith \"\\\"Test\"", "$var$.offset.Start.ToUpper")] + public void FindRuleParsingShouldBeSuccess(string findRule, string replaceRule) + { + var placeholder = "$var$"; + var replaceRuleStr = $"{placeholder} {findRule} => {replaceRule}"; + var rule = StructuralSearch.ParseReplaceRule(replaceRuleStr); + var _ruleStr = rule.ToString().ToLower(); + Assert.NotNull(rule); + Assert.Equal(_ruleStr, replaceRuleStr.ToLower()); + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/RuleParserTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/RuleParserTests.cs deleted file mode 100644 index 6bb2cc2..0000000 --- a/src/SimpleStateMachine.StructuralSearch.Tests/RuleParserTests.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Tests -{ - public class RuleParserTests - { - - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Constant.cs b/src/SimpleStateMachine.StructuralSearch/Constant.cs index 4b66283..5a3ead5 100644 --- a/src/SimpleStateMachine.StructuralSearch/Constant.cs +++ b/src/SimpleStateMachine.StructuralSearch/Constant.cs @@ -87,6 +87,21 @@ public static partial class Constant /// public static readonly char Dote = '.'; + /// + /// Char: '=' + /// + public static readonly char Equals = '='; + + /// + /// Char: '>' + /// + public static readonly char More = '>'; + + /// + /// String: "=>" + /// + public static readonly string Should = $"{Equals}{More}"; + /// /// Parenthesis chars: '(' and ')' /// diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/CharHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/CharHelper.cs deleted file mode 100644 index ddb44bc..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Helper/CharHelper.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Helper -{ - public static class CharHelper - { - // public static char Escape(char сh) - // { - // return сh switch - // { - // 'n' => '\n', - // 't' - // }; - // } - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs new file mode 100644 index 0000000..e1c0fc4 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; + +namespace SimpleStateMachine.StructuralSearch.Helper +{ + public static class EscapeHelper + { + public static string Escape(string str, Func replaceRule) + { + return new string(str.Select(replaceRule).ToArray()); + } + + public static string EscapeChars(string str, Func replaceRule, params char[] filter) + { + return new string(str.Select(c => filter.Contains(c) ? replaceRule(c) : c).ToArray()); + } + + public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) + { + return new string(str.Select(c => excluded.Contains(c) ? c : replaceRule(c)).ToArray()); + } + + public static string Escape(string str, Func replaceRule) + { + return string.Join(string.Empty, str.Select(replaceRule)); + } + + public static string EscapeChars(string str, Func replaceRule, params char[] filter) + { + return string.Join(string.Empty, str.Select(c => filter.Contains(c) ? replaceRule(c) : c.ToString())); + } + + public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) + { + return string.Join(string.Empty, str.Select(c => excluded.Contains(c) ? c.ToString() : replaceRule(c))); + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/Rule.cs b/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/Rule.cs deleted file mode 100644 index a3db319..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/Rule.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Rules -{ - public class Rule:IRule - { - public string Placeholder { get; } - - private IRule _rule { get; } - - public Rule(string placeholder, IRule rule) - { - Placeholder = placeholder; - _rule = rule; - } - - public bool Execute(string value) - { - return _rule.Execute(value); - } - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringParameter.cs deleted file mode 100644 index f95db57..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringParameter.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Rules -{ - public class StringParameter : IRuleParameter - { - public string Value { get; } - public StringParameter(string value) - { - Value = value; - } - public string GetValue() - { - return Value; - } - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/RuleParser.cs b/src/SimpleStateMachine.StructuralSearch/Rule/RuleParser.cs deleted file mode 100644 index a951370..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Rule/RuleParser.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch -{ - public class RuleParser - { - - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/BinaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs similarity index 82% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/BinaryRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs index fe08ca8..3003b1a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/BinaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs @@ -24,5 +24,10 @@ public bool Execute(string value) return LogicalHelper.Calculate(Type, left, right); } + + public override string ToString() + { + return $"{Left}{Constant.Space}{Type}{Constant.Space}{Right}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/BinaryRuleType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRuleType.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/BinaryRuleType.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRuleType.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/FindRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/FindRule.cs new file mode 100644 index 0000000..0570d4b --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/FindRule.cs @@ -0,0 +1,20 @@ +namespace SimpleStateMachine.StructuralSearch.Rules +{ + public class FindRule + { + public IRuleParameter Placeholder { get; } + + private IRule _rule { get; } + + public FindRule(IRuleParameter placeholder, IRule rule) + { + Placeholder = placeholder; + _rule = rule; + } + + public override string ToString() + { + return $"{Placeholder}{Constant.Space}{_rule}"; + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/InRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InRule.cs similarity index 76% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/InRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InRule.cs index cba6722..9fa285f 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/InRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InRule.cs @@ -20,5 +20,10 @@ public bool Execute(string value) { return Parameters.Any(parameter => Equals(value, parameter.GetValue())); } + + public override string ToString() + { + return $"{Type}{Constant.Space}{string.Join(Constant.Space, Parameters.Select(x=>x.ToString()))}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IsRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsRule.cs similarity index 88% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IsRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsRule.cs index 48d7051..f5f3eb1 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IsRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsRule.cs @@ -27,5 +27,10 @@ public bool Execute(string value) _ => throw new ArgumentOutOfRangeException() }; } + + public override string ToString() + { + return $"{Type}{Constant.Space}{PlaceholderType}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/LineProperty.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/LineProperty.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/LineProperty.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/LineProperty.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/OffsetProperty.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/OffsetProperty.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/OffsetProperty.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/OffsetProperty.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/PlaceholderType.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderType.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/PlaceholderType.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/SubRuleType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/SubRuleType.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/SubRuleType.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/SubRuleType.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs similarity index 83% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnaryRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs index 3e1986c..a0c8649 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs @@ -24,5 +24,10 @@ public bool Execute(string value) _ => throw new ArgumentOutOfRangeException() }; } + + public override string ToString() + { + return $"{Type}{Constant.Space}{Parameter}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnaryRuleType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRuleType.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnaryRuleType.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRuleType.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnarySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnarySubRule.cs similarity index 88% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnarySubRule.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnarySubRule.cs index 097d568..d86f6b6 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/UnarySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnarySubRule.cs @@ -29,5 +29,10 @@ public bool Execute(string value) _ => throw new ArgumentOutOfRangeException() }; } + + public override string ToString() + { + return $"{Type}{Constant.Space}{Parameter}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/ColumnProperty.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ColumnProperty.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/ColumnProperty.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ColumnProperty.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/FileProperty.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/FileProperty.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/FileProperty.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/FileProperty.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IRuleParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/IRuleParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/ParametersParser.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParametersParser.cs similarity index 92% rename from src/SimpleStateMachine.StructuralSearch/Rule/ParametersParser.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParametersParser.cs index c48227a..a6fa3c4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/ParametersParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParametersParser.cs @@ -14,18 +14,13 @@ public static class ParametersParser .TrimStart() .Try(); - public static readonly Parser Parameter = - Parser.OneOf(PlaceholderPropertyParser.PlaceholderPropertyParameter, PlaceholderParameter); - - public static readonly Parser> Parameters = - Parameter.AtLeastOnce(); - public static readonly Parser StringParameter = CommonParser.Escaped(Constant.DoubleQuotes, Constant.PlaceholderSeparator) .Or(Parser.AnyCharExcept(Constant.DoubleQuotes, Constant.PlaceholderSeparator)) .AtLeastOnceString() .Select(x => new StringParameter(x)) .As() + .TrimStart() .Try(); public static readonly Parser StringFormatParameter = @@ -36,5 +31,14 @@ public static class ParametersParser .As() .TrimStart() .Try(); + + public static readonly Parser Parameter = + Parser.OneOf(PlaceholderPropertyParser.PlaceholderPropertyParameter, PlaceholderParameter, StringFormatParameter) + .TrimStart() + .Try(); + + public static readonly Parser> Parameters = + Parameter.AtLeastOnce(); + } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderColumnParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs similarity index 75% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderColumnParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs index bd68015..3eee816 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderColumnParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs @@ -15,5 +15,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Column}{Constant.Dote}{Property}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderFileParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs similarity index 74% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderFileParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs index 8bc999c..fc01cf9 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderFileParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs @@ -15,5 +15,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.File}{Constant.Dote}{Property}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLenghtParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs similarity index 80% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLenghtParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs index 0c082a9..13ddd83 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLenghtParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs @@ -17,5 +17,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{PlaceholderParameter}{Constant.Dote}{Property}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLineParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs similarity index 75% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLineParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs index 090a9da..e9e04f4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderLineParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs @@ -15,5 +15,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Line}{Constant.Dote}{Property}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderOffsetParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs similarity index 74% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderOffsetParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs index bfef946..0fb9c17 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderOffsetParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs @@ -14,5 +14,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{PlaceholderParameter}{Constant.Dote}{PlaceholderProperty.Offset}{Constant.Dote}{Property}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs similarity index 69% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs index 778a6b4..2abf798 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/PlaceholderParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs @@ -13,5 +13,10 @@ public string GetValue() { throw new System.NotImplementedException(); } + + public override string ToString() + { + return $"{Constant.PlaceholderSeparator}{Name}{Constant.PlaceholderSeparator}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/PlaceholderProperty.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderProperty.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/PlaceholderProperty.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderProperty.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/PlaceholderPropertyParser.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/Rule/PlaceholderPropertyParser.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringFormatParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs similarity index 76% rename from src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringFormatParameter.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs index 2606881..4bd1363 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/FindRule/StringFormatParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs @@ -16,5 +16,10 @@ public string GetValue() { return string.Join(string.Empty, Parameters.Select(x => x.GetValue())); } + + public override string ToString() + { + return $"{string.Join(Constant.Space, Parameters.Select(x=> x.ToString()))}"; + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs new file mode 100644 index 0000000..f4b12c8 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs @@ -0,0 +1,25 @@ +using SimpleStateMachine.StructuralSearch.Helper; + +namespace SimpleStateMachine.StructuralSearch.Rules +{ + public class StringParameter : IRuleParameter + { + public string Value { get; } + public StringParameter(string value) + { + Value = value; + } + public string GetValue() + { + return Value; + } + + public override string ToString() + { + var value = EscapeHelper.EscapeChars(Value, c => $"{Constant.BackSlash}{c}", Constant.PlaceholderSeparator, + Constant.DoubleQuotes); + + return $"{Constant.DoubleQuotes}{value}{Constant.DoubleQuotes}"; + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs new file mode 100644 index 0000000..b5c4b1e --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -0,0 +1,36 @@ +using System; +using SimpleStateMachine.StructuralSearch.Rules; + +namespace SimpleStateMachine.StructuralSearch +{ + public class ChangeParameter : IRuleParameter + { + public IRuleParameter Parameter { get; } + public ChangeType Type { get; } + + public ChangeParameter(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}"; + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rule/ReplaceRule/ChangeType.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeType.cs similarity index 80% rename from src/SimpleStateMachine.StructuralSearch/Rule/ReplaceRule/ChangeType.cs rename to src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeType.cs index d1fef1a..3cdbebf 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rule/ReplaceRule/ChangeType.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeType.cs @@ -2,8 +2,7 @@ { public enum ChangeType { - NotCorrect = 0, - Trim, + Trim = 0, TrimEnd, TrimStart, ToUpper, diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs new file mode 100644 index 0000000..ee49fbe --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -0,0 +1,24 @@ +using Pidgin; +using SimpleStateMachine.StructuralSearch.Extensions; +using SimpleStateMachine.StructuralSearch.Rules; + +namespace SimpleStateMachine.StructuralSearch +{ + public class ReplaceRule + { + public FindRule FindRule { get; } + + public IRuleParameter Parameter { get; } + + public ReplaceRule(FindRule findRule, IRuleParameter parameter) + { + FindRule = findRule; + Parameter = parameter; + } + + public override string ToString() + { + return $"{FindRule}{Constant.Space}{Constant.Should}{Constant.Space}{Parameter}"; + } + } +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj b/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj index b2a59d1..38b6024 100644 --- a/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj +++ b/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj @@ -10,4 +10,9 @@ + + + <_Parameter1>SimpleStateMachine.StructuralSearch.Tests + + diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonTemplateParser.cs index df5e38c..6df70d8 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonTemplateParser.cs @@ -16,6 +16,9 @@ internal static readonly Parser Placeholder internal static readonly Parser StringWithPlshd = AnyCharWithPlshd.AtLeastOnceString(); + internal static readonly Parser Should + = String(Constant.Should); + //can be contains one $ internal static readonly Parser StringWithoutPlaceholder = Any.AtLeastOnceAsStringUntilNot(Placeholder); diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs index 9531835..7d6b8af 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs @@ -1,13 +1,13 @@ using System; +using System.Runtime.CompilerServices; using Pidgin; using Pidgin.Expression; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Rules; - namespace SimpleStateMachine.StructuralSearch { - internal static class FindRuleParser + public static class FindRuleParser { internal static Parser> Binary(Parser op) => op.Select>(type => (l, r) => new BinaryRule(type, l, r)); @@ -16,27 +16,41 @@ internal static Parser> Unary(Parser op.Select>(type => param => new UnaryRule(type, param)); internal static readonly Parser> And - = Binary(Parsers.EnumValue(BinaryRuleType.And, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.And, true) + .TrimStart() + .Try()); internal static readonly Parser> Or - = Binary(Parsers.EnumValue(BinaryRuleType.Or, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.Or, true) + .TrimStart() + .Try()); internal static readonly Parser> NOR - = Binary(Parsers.EnumValue(BinaryRuleType.NOR, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.NOR, true) + .TrimStart() + .Try()); internal static readonly Parser> XOR - = Binary(Parsers.EnumValue(BinaryRuleType.XOR, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.XOR, true) + .TrimStart() + .Try()); internal static readonly Parser> NAND - = Binary(Parsers.EnumValue(BinaryRuleType.NAND, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.NAND, true) + .TrimStart() + .Try()); internal static readonly Parser> XNOR - = Binary(Parsers.EnumValue(BinaryRuleType.XNOR, true).TrimStart()); + = Binary(Parsers.EnumValue(BinaryRuleType.XNOR, true) + .TrimStart() + .Try()); internal static readonly Parser> Not - = Unary(Parsers.EnumValue(UnaryRuleType.Not, true).TrimStart()); + = Unary(Parsers.EnumValue(UnaryRuleType.Not, true) + .TrimStart() + .Try()); - internal static readonly Parser Expr = ExpressionParser.Build( + public static readonly Parser Expr = ExpressionParser.Build( rule => ( Parser.OneOf( SubRuleParser.UnarySubRule, @@ -57,13 +71,12 @@ internal static readonly Parser> Not ) ); - internal static readonly Parser Rule = - Parser.Map((name, rule) => new Rule(name, rule), - CommonTemplateParser.Placeholder.TrimStart(), - Expr.TrimStart()) - .As(); + internal static readonly Parser Rule = + Parser.Map((parameter, rule) => new FindRule(parameter, rule), + ParametersParser.PlaceholderParameter, + Expr.TrimStart()); - internal static IRule ParseTemplate(string str) + internal static FindRule ParseTemplate(string str) { return Rule.ParseOrThrow(str); } diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs index a0d053b..c1d7e79 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs @@ -1,7 +1,29 @@ -namespace SimpleStateMachine.StructuralSearch +using Pidgin; +using SimpleStateMachine.StructuralSearch.Extensions; +using SimpleStateMachine.StructuralSearch.Rules; + +namespace SimpleStateMachine.StructuralSearch { - public class ReplaceRuleParser + public static class ReplaceRuleParser { + internal static readonly Parser ChangeParameter = + Parser.Map((parameter, changeType) => new ChangeParameter(parameter, changeType), + ParametersParser.Parameter.Before(CommonParser.Dote), + Parsers.Enum(true)) + .As() + .Try() + .TrimStart(); + internal static readonly Parser ReplaceRule = + Parser.Map((rule, parameter) => new ReplaceRule(rule, parameter), + FindRuleParser.Rule.Before(CommonTemplateParser.Should.TrimStart()), + Parser.OneOf(ChangeParameter.Try(), ParametersParser.Parameter.Try())) + .Try() + .TrimStart(); + + internal static ReplaceRule ParseTemplate(string str) + { + return ReplaceRule.ParseOrThrow(str); + } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs index 9e3e2b5..8d3cafa 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs @@ -12,7 +12,10 @@ public static Parser ParseFindTemplate(string template) public static IReplaceBuilder ParseReplaceTemplate(string template) => ReplaceTemplateParser.ParseTemplate(template); - public static IRule ParseFindRule(string template) + public static FindRule ParseFindRule(string template) => FindRuleParser.ParseTemplate(template); + + public static ReplaceRule ParseReplaceRule(string template) + => ReplaceRuleParser.ParseTemplate(template); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/FindTemplate/ConstantFindTemplate.cs b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/FindTemplate/ConstantFindTemplate.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs diff --git a/src/SimpleStateMachine.StructuralSearch/FindTemplate/ParserToParser.cs b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/FindTemplate/ParserToParser.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/IReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/IReplaceBuilder.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/IReplaceStep.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/IReplaceStep.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/ParserToReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/ParserToReplace.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/PlaceholderReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/PlaceholderReplace.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/ReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/ReplaceBuilder.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs diff --git a/src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/TokenReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs similarity index 100% rename from src/SimpleStateMachine.StructuralSearch/ReplaceTemplate/TokenReplace.cs rename to src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs