Skip to content

Commit 16522ec

Browse files
committed
feat(FindRules): Use find rules during parsing
1 parent fd672ed commit 16522ec

35 files changed

+215
-94
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
$var$ = $value1$;
2121
FindRules:
2222
- $sign$ In ("is", "==", "!=", "is not")
23-
- $value$ In ($value1$, $value2$)
2423
ReplaceTemplate: |-
2524
$var$ = $value1$ ?? $value2$;
2625
ReplaceRules:

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
$var$ = $value1$;
2020
FindRules:
2121
- $sign$ In ("is", "==", "!=", "is not")
22-
- $value$ In ($value1$, $value2$)
2322
ReplaceTemplate: |-
2423
$var$ = $value1$ ?? $value2$;
2524
ReplaceRules:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Tests.Examples;
5+
6+
public class Test
7+
{
8+
public int Method1(int value1, int value2)
9+
{
10+
return value1 + value2;
11+
}
12+
13+
public string Method2(int value1, string value2)
14+
{
15+
// some comments
16+
return value2 + value1;
17+
}
18+
19+
public void Method3()
20+
{
21+
22+
}
23+
24+
public Task Method4Async(CancellationToken cancellationToken)
25+
{
26+
return Task.CompletedTask;
27+
}
28+
29+
public int Test2(int Test2)
30+
{
31+
// some comments
32+
return Test2;
33+
}
34+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
$sign$ In ("is", "==", "!=", "is not")
2-
$value$ In ($value1$, $value2$)
1+
$sign$ In ("is", "==", "!=", "is not")

src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void FindRuleExprParsingShouldBeEqualsCustomResult(string ruleStr,
6262
}
6363

6464
[Theory]
65-
[InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"", "$value$ In $value1$,$value2$")]
65+
[InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"")]
6666
[InlineData("FindRule/AssignmentNullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"")]
6767
public static void FindRuleParsingFromFileShouldBeSuccess(string filePath, params string[] customResult)
6868
{

src/SimpleStateMachine.StructuralSearch.Tests/StructuralSearchTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
4+
using SimpleStateMachine.StructuralSearch;
5+
using SimpleStateMachine.StructuralSearch.Configurations;
36
using SimpleStateMachine.StructuralSearch.Extensions;
47
using Xunit;
58

@@ -37,4 +40,26 @@ public static void StructuralSearchShouldBeSuccess(string inputText, string temp
3740
}
3841
}
3942
};
43+
44+
45+
[Theory]
46+
[InlineData("ExamplesInput/Methods.cs")]
47+
public static void StructuralSearchShouldBeSuccess2(string filePath)
48+
{
49+
var configuration = new Configuration()
50+
{
51+
FindTemplate = "$Modificator$ $ReturnType$ $MethodName$($params$)",
52+
FindRules = new List<string>
53+
{
54+
"$Modificator$ in (\"public\", \"private\", \"internal\")",
55+
"$ReturnType$ is var",
56+
"$MethodName$ is var"
57+
}
58+
};
59+
60+
var parser = new StructuralSearchParser(configuration);
61+
IParsingContext context = new ParsingContext(new FileInput(new FileInfo(filePath)));
62+
var results = parser.Parse(ref context).ToList();
63+
// parser.ApplyFindRule(results);
64+
}
4065
}

src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public bool TryGetPlaceholder(string name, out IPlaceholder value)
1717
public void AddPlaceholder(IPlaceholder placeholder)
1818
=> throw new System.NotImplementedException();
1919

20+
public void RemovePlaceholder(IPlaceholder placeholder)
21+
=> throw new System.NotImplementedException();
22+
2023
public IPlaceholder GetPlaceholder(string name)
2124
=> Placeholder.CreateEmpty(this, name, string.Empty);
2225

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SimpleStateMachine.StructuralSearch.Rules;
2+
3+
internal static class IRuleParameterExtensions
4+
{
5+
public static bool IsApplicableForPlaceholder(this IRuleParameter parameter, string placeholderName)
6+
=> parameter is IPlaceholderRelatedRuleParameter relatedRuleParameter && relatedRuleParameter.Name == placeholderName;
7+
}

src/SimpleStateMachine.StructuralSearch/IParsingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IParsingContext
88

99
bool TryGetPlaceholder(string name, out IPlaceholder value);
1010
void AddPlaceholder(IPlaceholder placeholder);
11+
void RemovePlaceholder(IPlaceholder placeholder);
1112
IPlaceholder GetPlaceholder(string name);
1213

1314
void Fill(IReadOnlyDictionary<string, IPlaceholder>placeholders);

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Pidgin;
45
using SimpleStateMachine.StructuralSearch.Extensions;
6+
using SimpleStateMachine.StructuralSearch.Rules;
57

68
namespace SimpleStateMachine.StructuralSearch
79
{
810
public class PlaceholderParser : ParserWithLookahead<char, string>, IContextDependent
911
{
1012
private readonly string _name;
13+
private readonly IReadOnlyList<IFindRule> _findRules;
1114
private IParsingContext? _context;
12-
private IParsingContext Context => _context ?? throw new ArgumentNullException(nameof(_context));
1315

14-
public PlaceholderParser(string name)
16+
public PlaceholderParser(string name, IReadOnlyList<IFindRule> findRules)
1517
{
1618
_name = name;
19+
_findRules = findRules;
1720
}
21+
22+
private IParsingContext Context => _context ?? throw new ArgumentNullException(nameof(_context));
1823

1924
protected override Parser<char, string> BuildParser(Func<Parser<char, string>?> next,
2025
Func<Parser<char, string>?> nextNext)
@@ -28,7 +33,6 @@ protected override Parser<char, string> BuildParser(Func<Parser<char, string>?>
2833
Parser<char, Unit> lookahead;
2934
if (nextNextParser is not null)
3035
{
31-
3236
lookahead = Parser.Lookahead(nextParser.Then(nextNextParser, (s1, s2) =>
3337
{
3438
OnLookahead = () => new List<LookaheadResult<char, string>>
@@ -90,10 +94,19 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
9094
result = match.Value;
9195
if (res)
9296
{
93-
Context.AddPlaceholder(new Placeholder(
97+
var placeholderObj = new Placeholder
98+
(
9499
context: ref _context!,
95100
name: _name,
96-
match: match));
101+
match: match
102+
);
103+
104+
Context.AddPlaceholder(placeholderObj);
105+
106+
res = _findRules.All(r => r.Execute(ref _context));
107+
108+
if (!res)
109+
Context.RemovePlaceholder(placeholderObj);
97110
}
98111
}
99112

0 commit comments

Comments
 (0)