Skip to content

Commit f5d3935

Browse files
committed
Optimization replace rule
1 parent ca43648 commit f5d3935

File tree

8 files changed

+80
-38
lines changed

8 files changed

+80
-38
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class ReplaceRuleParserTests
99
[Theory]
1010
[InlineData("$var1$ equals $var2$ then $var1$ => \"test $var3$\"")]
1111
[InlineData("$var1$ equals \"\\$\" then $var1$ => \"\\$\",$var2$ => \"132\"")]
12+
[InlineData("_ then $var1$ => \"test $var3$.Lenght\"")]
13+
[InlineData("_ then $var1$ => \"\\$\",$var2$ => \"132\"")]
1214
[InlineData("Not $var1$ equals $var$.Lenght then $var1$ => $var$.Lenght")]
1315
[InlineData("Not $var1$ equals $var$.offset.Start then $var1$ => $var$.offset.Start")]
1416
[InlineData("$var1$ equals $var$.Lenght and Not $var1$ StartsWith \"123\" then $var1$ => $var$.offset.Start.Trim")]

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ReplaceTemplateTests
1212
[InlineData("ReplaceTemplate/NullUnionOperator.txt", 10)]
1313
[InlineData("ReplaceTemplate/AssignmentNullUnionOperator.txt", 6)]
1414
[InlineData("ReplaceTemplate/TernaryOperator.txt", 11)]
15-
public void TemplateParsingShouldHaveStepCount(string templatePath, int stepsCount)
15+
public void ReplaceTemplateParsingShouldHaveStepCount(string templatePath, int stepsCount)
1616
{
1717
var replaceTemplate = File.ReadAllText(templatePath);
1818
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate);
@@ -50,5 +50,17 @@ public void ReplaceBuildShouldBeSuccess(string templatePath, string resultPath,
5050
Assert.NotNull(replaceResult);
5151
Assert.Equal(replaceResult, result);
5252
}
53+
54+
[Theory]
55+
[InlineData("test $var1$.Lenght")]
56+
public void ReplaceTemplateParsingShouldBeSuccess(string templateStr)
57+
{
58+
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(templateStr);
59+
var t = replaceBuilder.ToString();
60+
var result = replaceBuilder.Build(new EmptyParsingContext());
61+
62+
// Assert.NotNull(replaceTemplate);
63+
// Assert.Equal(replaceBuilder.Steps.Count(), stepsCount);
64+
}
5365
}
5466
}

src/SimpleStateMachine.StructuralSearch/Constant.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,102 +10,107 @@ public static partial class Constant
1010
/// <summary>
1111
/// String: "Not"
1212
/// </summary>
13-
public static readonly string Not ="Not";
13+
public const string Not ="Not";
1414

1515
/// <summary>
1616
/// String: "Then"
1717
/// </summary>
18-
public static readonly string Then ="Then";
18+
public const string Then ="Then";
1919

2020
/// <summary>
2121
/// Parenthesis char: '('
2222
/// </summary>
23-
public static readonly char LeftParenthesis = '(';
23+
public const char LeftParenthesis = '(';
2424

2525
/// <summary>
2626
/// Parenthesis char: ')'
2727
/// </summary>
28-
public static readonly char RightParenthesis = ')';
28+
public const char RightParenthesis = ')';
2929

3030
/// <summary>
3131
/// Parenthesis char: '['
3232
/// </summary>
33-
public static readonly char LeftSquareParenthesis = '[';
33+
public const char LeftSquareParenthesis = '[';
3434

3535
/// <summary>
3636
/// Parenthesis char: ']'
3737
/// </summary>
38-
public static readonly char RightSquareParenthesis = ']';
38+
public const char RightSquareParenthesis = ']';
3939

4040
/// <summary>
4141
/// Parenthesis char: '{'
4242
/// </summary>
43-
public static readonly char LeftCurlyParenthesis = '{';
43+
public const char LeftCurlyParenthesis = '{';
4444

4545
/// <summary>
4646
/// Parenthesis char: '}'
4747
/// </summary>
48-
public static readonly char RightCurlyParenthesis = '}';
48+
public const char RightCurlyParenthesis = '}';
4949

5050
/// <summary>
5151
/// Char: '$'
5252
/// </summary>
53-
public static readonly char PlaceholderSeparator = '$';
53+
public const char PlaceholderSeparator = '$';
5454

5555
/// <summary>
5656
/// Char: '\r'
5757
/// </summary>
58-
public static readonly char CarriageReturn = '\r';
58+
public const char CarriageReturn = '\r';
5959

6060
/// <summary>
6161
/// Char: '\n'
6262
/// </summary>
63-
public static readonly char LineFeed = '\n';
63+
public const char LineFeed = '\n';
6464

6565
/// <summary>
6666
/// Char: ' '
6767
/// </summary>
68-
public static readonly char Space = ' ';
68+
public const char Space = ' ';
6969

7070
/// <summary>
7171
/// Char: ','
7272
/// </summary>
73-
public static readonly char Comma = ',';
73+
public const char Comma = ',';
7474

7575
/// <summary>
7676
/// Char: '\''
7777
/// </summary>
78-
public static readonly char SingleQuotes = '\'';
78+
public const char SingleQuotes = '\'';
7979

8080
/// <summary>
8181
/// Char: '\"'
8282
/// </summary>
83-
public static readonly char DoubleQuotes = '\"';
83+
public const char DoubleQuotes = '\"';
8484

8585
/// <summary>
8686
/// Char: '\"'
8787
/// </summary>
88-
public static readonly char BackSlash = '\\';
88+
public const char BackSlash = '\\';
8989

9090
/// <summary>
9191
/// Char: '.'
9292
/// </summary>
93-
public static readonly char Dote = '.';
93+
public const char Dote = '.';
9494

9595
/// <summary>
9696
/// Char: '='
9797
/// </summary>
98-
public static readonly char Equals = '=';
98+
public const char Equals = '=';
9999

100100
/// <summary>
101101
/// Char: '>'
102102
/// </summary>
103-
public static readonly char More = '>';
103+
public const char More = '>';
104+
105+
/// <summary>
106+
/// Char: '_'
107+
/// </summary>
108+
public const char Underscore = '_';
104109

105110
/// <summary>
106111
/// Char: ':'
107112
/// </summary>
108-
public static readonly char Colon = ':';
113+
public const char Colon = ':';
109114

110115
/// <summary>
111116
/// String: "=>"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace SimpleStateMachine.StructuralSearch.Rules;
2+
3+
public class EmptySubRule : IRule
4+
{
5+
public bool Execute()
6+
{
7+
return true;
8+
}
9+
10+
public override string ToString()
11+
{
12+
return $"{Constant.Underscore}";
13+
}
14+
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ internal static readonly Parser<char, char> SingleQuotes
4545

4646
internal static readonly Parser<char, char> Dote
4747
= Char(Constant.Dote);
48+
49+
internal static readonly Parser<char, char> Underscore
50+
= Char(Constant.Underscore);
4851

4952
internal static Parser<char, T> Parenthesised<T>(Parser<char, T> parser, Func<Parser<char, string>, Parser<char, string>> custom)
5053
{

src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,40 @@ namespace SimpleStateMachine.StructuralSearch
66
{
77
public static class ReplaceRuleParser
88
{
9-
internal static readonly Parser<char, IRuleParameter> ChangeParameter =
10-
Parser.Map((parameter, changeType) => new ChangeParameter(parameter, changeType),
11-
ParametersParser.Parameter.Before(CommonParser.Dote),
12-
Parser.CIEnum<ChangeType>())
13-
.As<char, ChangeParameter, IRuleParameter>()
14-
.Try()
15-
.TrimStart();
16-
179
internal static readonly Parser<char, string> Then =
1810
Parser.CIString(Constant.Then)
1911
.Try()
2012
.TrimStart();
21-
13+
14+
internal static readonly Parser<char, IRuleParameter> ReplaceSubRuleParameter =
15+
Parser.Map((parameter, changeType) =>
16+
changeType.HasValue ? new ChangeParameter(parameter, changeType.Value) : parameter,
17+
ParametersParser.Parameter,
18+
CommonParser.Dote.Then(Parser.CIEnum<ChangeType>()).Optional())
19+
.Try()
20+
.TrimStart();
21+
2222
internal static readonly Parser<char, ReplaceSubRule> ReplaceSubRule =
23-
Parser.Map((placeholder, parameter) => new ReplaceSubRule(placeholder, parameter),
24-
ParametersParser.PlaceholderParameter.Before(CommonTemplateParser.Should.TrimStart()),
25-
Parser.OneOf(ChangeParameter.Try(), ParametersParser.Parameter.Try()))
23+
Parser.Map((placeholder, _, parameter) => new ReplaceSubRule(placeholder, parameter),
24+
ParametersParser.PlaceholderParameter.TrimStart(),
25+
CommonTemplateParser.Should.TrimStart(),
26+
ReplaceSubRuleParameter)
27+
.Try()
28+
.TrimStart();
29+
30+
internal static readonly Parser<char, IRule> EmptySubRule =
31+
CommonParser.Underscore.ThenReturn(new EmptySubRule())
32+
.As<char, EmptySubRule, IRule>()
2633
.Try()
2734
.TrimStart();
2835

2936
internal static readonly Parser<char, ReplaceRule> ReplaceRule =
3037
Parser.Map((rule, subRules) => new ReplaceRule(rule, subRules),
31-
FindRuleParser.Expr,
38+
Parser.OneOf(EmptySubRule, FindRuleParser.Expr),
3239
Then.Then(ReplaceSubRule.SeparatedAtLeastOnce(CommonParser.Comma)))
3340
.Try()
3441
.TrimStart();
35-
42+
3643
internal static ReplaceRule ParseTemplate(string str)
3744
{
3845
return ReplaceRule.ParseOrThrow(str);

src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static ReplaceTemplateParser()
4646

4747
internal static readonly Parser<char, IEnumerable<IReplaceStep>> Parenthesised;
4848

49-
private static readonly Parser<char, IEnumerable<IReplaceStep>> TemplateParser;
49+
internal static readonly Parser<char, IEnumerable<IReplaceStep>> TemplateParser;
5050

5151
internal static IReplaceBuilder ParseTemplate(string str)
5252
{

src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public static Parser<char, IRule> InSubRule(IRuleParameter left, SubRuleType rul
3030
.Select(args => new InSubRule(left, args))
3131
.As<char, InSubRule, IRule>()
3232
.Try();
33-
34-
33+
3534
public static readonly Parser<char, IRule> OneOfSubRule =
3635
Parser.Map((left, ruleType) => (left, ruleType),
3736
ParametersParser.Parameter, SubRuleType)

0 commit comments

Comments
 (0)