Skip to content

Commit 2ff71ce

Browse files
committed
fix replace rule
1 parent a73f874 commit 2ff71ce

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ internal static class Program
1313
{
1414
static void Main(string[] args)
1515
{
16-
var tesds = ParametersParser.StringFormatParameter.ParseOrThrow("\"tfasdfa\\\"sd$var$.Lenght\"");
17-
var replaceRule = StructuralSearch.ParseReplaceRule("$var$ equals $var$ => $var$");
16+
//var tesds = ParametersParser.StringFormatParameter.ParseOrThrow("\"tfasdfa\\\"sd$var$.Lenght\"");
17+
var replaceRule = StructuralSearch.ParseReplaceRule("$var$ equals $var$ => $var$.Trim");
1818

1919
var rule = StructuralSearch.ParseFindRule("$var$ equals $var$.Lenght and Not StartsWith \"123\\\" $var$ \\\"\"");
2020
var rule2 = StructuralSearch.ParseFindRule("$var$ equals $var$.Offset.Start and Not StartsWith \"123\"");

src/SimpleStateMachine.StructuralSearch/Rule/ReplaceRule/ChangeParameter.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SimpleStateMachine.StructuralSearch.Rules;
1+
using System;
2+
using SimpleStateMachine.StructuralSearch.Rules;
23

34
namespace SimpleStateMachine.StructuralSearch
45
{
@@ -15,7 +16,21 @@ public ChangeParameter(IRuleParameter parameter, ChangeType type)
1516

1617
public string GetValue()
1718
{
18-
throw new System.NotImplementedException();
19+
var value = Parameter.GetValue();
20+
return Type switch
21+
{
22+
ChangeType.Trim => value.Trim(),
23+
ChangeType.TrimEnd => value.TrimEnd(),
24+
ChangeType.TrimStart => value.TrimStart(),
25+
ChangeType.ToUpper => value.ToUpper(),
26+
ChangeType.ToLower => value.ToLower(),
27+
_ => throw new ArgumentOutOfRangeException()
28+
};
1929
}
30+
31+
public override string ToString()
32+
{
33+
return $"{Parameter}{Constant.Dote}{Type}";
34+
}
2035
}
2136
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,39 @@ internal static Parser<char, Func<IRule, IRule>> Unary(Parser<char, UnaryRuleTyp
1616
=> op.Select<Func<IRule, IRule>>(type => param => new UnaryRule(type, param));
1717

1818
internal static readonly Parser<char, Func<IRule, IRule, IRule>> And
19-
= Binary(Parsers.EnumValue(BinaryRuleType.And, true).TrimStart());
19+
= Binary(Parsers.EnumValue(BinaryRuleType.And, true)
20+
.TrimStart()
21+
.Try());
2022

2123
internal static readonly Parser<char, Func<IRule, IRule, IRule>> Or
22-
= Binary(Parsers.EnumValue(BinaryRuleType.Or, true).TrimStart());
24+
= Binary(Parsers.EnumValue(BinaryRuleType.Or, true)
25+
.TrimStart()
26+
.Try());
2327

2428
internal static readonly Parser<char, Func<IRule, IRule, IRule>> NOR
25-
= Binary(Parsers.EnumValue(BinaryRuleType.NOR, true).TrimStart());
29+
= Binary(Parsers.EnumValue(BinaryRuleType.NOR, true)
30+
.TrimStart()
31+
.Try());
2632

2733
internal static readonly Parser<char, Func<IRule, IRule, IRule>> XOR
28-
= Binary(Parsers.EnumValue(BinaryRuleType.XOR, true).TrimStart());
34+
= Binary(Parsers.EnumValue(BinaryRuleType.XOR, true)
35+
.TrimStart()
36+
.Try());
2937

3038
internal static readonly Parser<char, Func<IRule, IRule, IRule>> NAND
31-
= Binary(Parsers.EnumValue(BinaryRuleType.NAND, true).TrimStart());
39+
= Binary(Parsers.EnumValue(BinaryRuleType.NAND, true)
40+
.TrimStart()
41+
.Try());
3242

3343
internal static readonly Parser<char, Func<IRule, IRule, IRule>> XNOR
34-
= Binary(Parsers.EnumValue(BinaryRuleType.XNOR, true).TrimStart());
44+
= Binary(Parsers.EnumValue(BinaryRuleType.XNOR, true)
45+
.TrimStart()
46+
.Try());
3547

3648
internal static readonly Parser<char, Func<IRule, IRule>> Not
37-
= Unary(Parsers.EnumValue(UnaryRuleType.Not, true).TrimStart());
49+
= Unary(Parsers.EnumValue(UnaryRuleType.Not, true)
50+
.TrimStart()
51+
.Try());
3852

3953
public static readonly Parser<char, IRule> Expr = ExpressionParser.Build<char, IRule>(
4054
rule => (

src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ public static class ReplaceRuleParser
1616

1717
internal static readonly Parser<char, ReplaceRule> ReplaceRule =
1818
Parser.Map((rule, parameter) => new ReplaceRule(rule, parameter),
19-
FindRuleParser.Rule.Before(CommonTemplateParser.Should),
20-
Parser.OneOf(ChangeParameter, ParametersParser.Parameter));
19+
FindRuleParser.Rule.Before(CommonTemplateParser.Should.TrimStart()),
20+
Parser.OneOf(ChangeParameter.Try(), ParametersParser.Parameter.Try()))
21+
.Try()
22+
.TrimStart();
2123

2224
internal static ReplaceRule ParseTemplate(string str)
2325
{

0 commit comments

Comments
 (0)