Skip to content

Commit d3ae8ce

Browse files
committed
fix config parser
1 parent 5c1262f commit d3ae8ce

File tree

8 files changed

+60
-52
lines changed

8 files changed

+60
-52
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
# NullUnionOperator
1515

1616
- FindTemplate: |-
17-
if($value1$ $sign$ null)
18-
{
17+
if($value$ $sign$ null)
1918
$var$ = $value2$;
20-
}
2119
else
22-
{
2320
$var$ = $value1$;
24-
}
2521
FindRules:
2622
- $sign$ In ("Is", "==", "!=", "is not")
23+
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
2724
ReplaceTemplate: |-
2825
$var$ = $value1$ ?? $value2$;
2926
ReplaceRules:

src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
# NullUnionOperator
1414

1515
- FindTemplate: |-
16-
if($value1$ $sign$ null)
17-
{
16+
if($value$ $sign$ null)
1817
$var$ = $value2$;
19-
}
2018
else
21-
{
2219
$var$ = $value1$;
23-
}
2420
FindRules:
2521
- $sign$ In ("Is", "==", "!=", "is not")
22+
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
2623
ReplaceTemplate: |-
2724
$var$ = $value1$ ?? $value2$;
2825
ReplaceRules:

src/SimpleStateMachine.StructuralSearch.Tests/Examples/NullUnionOperator.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ public int Test1()
1212
return 4;
1313
}
1414

15-
public int Test2()
15+
public void Test2()
1616
{
17-
int? temp = 5;
18-
19-
if(temp is null)
20-
return 7;
17+
int? result;
18+
int? temp1 = 5;
19+
int? temp2 = 5;
20+
if(temp1 is null)
21+
result = temp2;
2122
else
22-
return 8;
23+
result = temp1;
2324
}
2425

25-
public int Test3()
26+
public void Test3()
2627
{
27-
int? temp2 = 1;
28-
29-
if(temp2 is null)
30-
return 3;
28+
int result;
29+
int? temp1 = 5;
30+
int? temp2 = 5;
31+
if(temp1 is null)
32+
result = temp2.Value;
3133
else
32-
return 4;
34+
result = temp1.Value;
3335
}
3436

3537
public int Test4()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
$sign$ In ("Is", "==", "!=", "is not")
1+
$sign$ In ("Is", "==", "!=", "is not")
2+
$value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
if($value1$ $sign$ null)
2-
{
1+
if($value$ $sign$ null)
32
$var$ = $value2$;
4-
}
53
else
6-
{
7-
$var$ = $value1$;
8-
}
4+
$var$ = $value1$;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
if(var1 is null)
2-
{
32
temp = var2;
4-
}
53
else
6-
{
7-
temp = var1;
8-
}
4+
temp = var1;

src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class StructuralSearchParserTests
99
{
1010
[Theory]
1111
// [InlineData("AssignmentNullUnionOperator")]
12-
// [InlineData("NullUnionOperator")]
12+
[InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)]
1313
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)]
1414
public static void StructuralSearchShouldBeSuccess(string exampleName, string exampleFilePath, int matchesCount)
1515
{

src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class StructuralSearchParser
1717

1818
public IReplaceBuilder ReplaceBuilder { get; set; }
1919

20-
public Dictionary<string, ReplaceRule> ReplaceRules { get; set; }
20+
public IEnumerable<ReplaceRule> ReplaceRules { get; set; }
2121

2222
public StructuralSearchParser(Configuration configuration)
2323
{
@@ -29,41 +29,60 @@ public StructuralSearchParser(Configuration configuration)
2929

3030
ReplaceBuilder = StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate);
3131

32-
// ReplaceRules = configuration.ReplaceRules
33-
// .EmptyIfNull()
34-
// .Select(StructuralSearch.ParseReplaceRule)
35-
// .ToDictionary(x => x.FindRule.Placeholder.Name, x => x);
32+
ReplaceRules = configuration.ReplaceRules
33+
.EmptyIfNull()
34+
.Select(StructuralSearch.ParseReplaceRule);
3635
}
3736

3837
public IEnumerable<FindParserMatch> Parse(ref IParsingContext context)
3938
{
4039
var matches = FindParser.Parse(ref context);
40+
return matches;
41+
}
42+
43+
public IEnumerable<FindParserMatch> ApplyFindRule(ref IParsingContext context, IEnumerable<FindParserMatch> matches)
44+
{
4145
var result = new List<FindParserMatch>();
42-
4346
foreach (var match in matches)
4447
{
45-
context.Set(match.Placeholders);
46-
if (AllRulesCompleted(ref context))
47-
{
48+
context.Set(match.Placeholders);
49+
var all = FindRules.All(x => x.Execute());
50+
if (all)
51+
{
4852
result.Add(match);
49-
}
53+
}
5054
}
5155

5256
return result;
5357
}
54-
58+
public void ApplyReplaceRule(ref IParsingContext context, IEnumerable<FindParserMatch> matches)
59+
{
60+
// var result = new List<FindParserMatch>();
61+
// foreach (var match in matches)
62+
// {
63+
// context.Set(match.Placeholders);
64+
//
65+
// var rules = ReplaceRules
66+
// .Where(x => x.FindRule.Execute())
67+
// .SelectMany(x => x.Rules);
68+
//
69+
// var placeHolder = match.Placeholders
70+
// .ToDictionary(x=> x.Key, x=> x.Value);
71+
//
72+
// foreach (var rule in rules)
73+
// {
74+
// placeHolder[rule.Placeholder.Name].Value
75+
// }
76+
// }
77+
//
78+
// return result;
79+
}
5580
public void Replace(FindParserMatch context)
5681
{
5782
//ReplaceBuilder.Build(context);
5883
// context.
5984
//FindParser.Parse(context, context.File.Data);
6085
}
6186

62-
public bool AllRulesCompleted(ref IParsingContext context)
63-
{
64-
//return FindRules.Values.All(x => x.Execute());
65-
66-
return true;
67-
}
6887
}
6988
}

0 commit comments

Comments
 (0)