Skip to content

Commit 28f661d

Browse files
committed
Support replace
1 parent 306282f commit 28f661d

File tree

8 files changed

+85
-57
lines changed

8 files changed

+85
-57
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.IO;
2-
using System.Linq;
32
using SimpleStateMachine.StructuralSearch.Tests.Mock;
43
using Xunit;
54

@@ -17,8 +16,8 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex
1716

1817
var fileInfo = new FileInfo(exampleFilePath);
1918
var input = Input.Input.File(fileInfo);
20-
var matches = parser.Parse(input);
21-
Assert.Equal(matchesCount, matches.Count());
19+
var matches = parser.StructuralSearch(input);
20+
Assert.Equal(matchesCount, matches.Count);
2221
}
2322

2423
// [Theory]

src/SimpleStateMachine.StructuralSearch.Tests/StructuralSearchTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void StructuralSearchFileParsingShouldBeSuccess(string filePath)
5656
};
5757

5858
var parser = new StructuralSearchParser(configuration);
59-
var results = parser.Parse(new FileInput(new FileInfo(filePath))).ToList();
59+
var results = parser.StructuralSearch(new FileInput(new FileInfo(filePath))).ToList();
6060
// parser.ApplyFindRule(results);
6161
}
6262
}

src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Pidgin;
33
using System.Collections.Generic;
4-
using SimpleStateMachine.StructuralSearch.Parsing;
54

65
namespace SimpleStateMachine.StructuralSearch.Extensions;
76

src/SimpleStateMachine.StructuralSearch/Placeholder.cs renamed to src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs

File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace SimpleStateMachine.StructuralSearch;
2+
3+
internal class PlaceholderReplace : IPlaceholder
4+
{
5+
private readonly IPlaceholder _placeholder;
6+
private readonly string _newValue;
7+
8+
public PlaceholderReplace(IPlaceholder placeholder, string newValue)
9+
{
10+
_placeholder = placeholder;
11+
_newValue = newValue;
12+
}
13+
14+
public string Name => _placeholder.Name;
15+
public string Value => _newValue;
16+
public int Length => _newValue.Length;
17+
public LinePosition Line => _placeholder.Line;
18+
public ColumnPosition Column => _placeholder.Column;
19+
public OffsetPosition Offset => _placeholder.Offset;
20+
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
using SimpleStateMachine.StructuralSearch.Parameters;
1+
using SimpleStateMachine.StructuralSearch.Context;
2+
using SimpleStateMachine.StructuralSearch.Parameters;
23

34
namespace SimpleStateMachine.StructuralSearch.Replace;
45

56
internal class Assignment
67
{
7-
private readonly PlaceholderParameter _placeholder;
8+
private readonly PlaceholderParameter _placeholderParameter;
89
private readonly IParameter _newValue;
910

10-
public Assignment(PlaceholderParameter placeholder, IParameter newValue)
11+
public Assignment(PlaceholderParameter placeholderParameter, IParameter newValue)
1112
{
12-
_placeholder = placeholder;
13+
_placeholderParameter = placeholderParameter;
1314
_newValue = newValue;
1415
}
1516

17+
public IPlaceholder GetPlaceholder(ref IParsingContext context) => _placeholderParameter.GetPlaceholder(ref context);
18+
19+
public string GetValue(ref IParsingContext context)
20+
=> _newValue.GetValue(ref context);
21+
1622
public override string ToString()
17-
=> $"{_placeholder}{Constant.Space}{Constant.Should}{Constant.Space}{_newValue}";
23+
=> $"{_placeholderParameter}{Constant.Space}{Constant.Should}{Constant.Space}{_newValue}";
1824
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace SimpleStateMachine.StructuralSearch.Replace;
4+
5+
public class ReplaceResult
6+
{
7+
public ReplaceResult(FindParserResult findResult, IReadOnlyDictionary<string, IPlaceholder> newPlaceholders, string newValue)
8+
{
9+
FindResult = findResult;
10+
NewPlaceholders = newPlaceholders;
11+
Value = newValue;
12+
}
13+
14+
public FindParserResult FindResult { get; }
15+
public IReadOnlyDictionary<string, IPlaceholder> NewPlaceholders { get; }
16+
public string Value { get; }
17+
}
Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using SimpleStateMachine.StructuralSearch.Context;
34
using SimpleStateMachine.StructuralSearch.Extensions;
45
using SimpleStateMachine.StructuralSearch.Input;
56
using SimpleStateMachine.StructuralSearch.Operator.Logical;
@@ -19,61 +20,47 @@ public StructuralSearchParser(Configuration configuration)
1920
_findRules = configuration.FindRules
2021
.EmptyIfNull()
2122
.Select(Parsing.StructuralSearch.ParseFindRule).ToArray();
22-
23+
2324
_findParser = Parsing.StructuralSearch.ParseFindTemplate(configuration.FindTemplate);
24-
25+
2526
_replaceBuilder = Parsing.StructuralSearch.ParseReplaceTemplate(configuration.ReplaceTemplate);
26-
27+
2728
_replaceRules = configuration.ReplaceRules
2829
.EmptyIfNull()
2930
.Select(Parsing.StructuralSearch.ParseReplaceRule).ToList();
3031
}
3132

32-
public IEnumerable<FindParserResult> Parse(IInput input)
33+
public List<FindParserResult> StructuralSearch(IInput input)
3334
=> _findParser.Parse(input, _findRules);
3435

35-
// public IEnumerable<FindParserResult> ApplyReplaceRule(IEnumerable<FindParserResult> matches)
36-
// {
37-
// var result = new List<FindParserResult>();
38-
//
39-
// foreach (var match in matches)
40-
// {
41-
// var placeholders = match.Placeholders
42-
// .ToDictionary(x => x.Key,
43-
// x => x.Value);
44-
//
45-
// IParsingContext context = new ParsingContext(match.Placeholders.First().Value.Input, match.Placeholders);
46-
// List<ReplaceSubRule> rules = new();
47-
//
48-
// foreach (var replaceRule in _replaceRules)
49-
// {
50-
// if (replaceRule.IsMatch(ref context))
51-
// {
52-
// rules.AddRange(replaceRule.Rules);
53-
// }
54-
// }
55-
//
56-
// foreach (var rule in rules)
57-
// {
58-
// var name = rule.Placeholder.Name;
59-
// var placeholder = placeholders[name];
60-
// var value = rule.Parameter.GetValue(ref context);
61-
// placeholders[name] = new ReplacedPlaceholder(placeholder, value);
62-
// }
63-
//
64-
// result.Add(match with { Placeholders = placeholders });
65-
// }
66-
//
67-
// return result;
68-
// }
36+
public List<ReplaceResult> StructuralSearchAndReplace(IInput input)
37+
{
38+
var results = StructuralSearch(input);
39+
return results.Select(r => Replace(input, r)).ToList();
40+
}
6941

70-
// public ReplaceMatch GetReplaceMatch(FindParserResult parserResult)
71-
// {
72-
// IParsingContext context = new ParsingContext(parserResult.Placeholders);
73-
// var replaceText = _replaceBuilder.Build(ref context);
74-
// return new ReplaceMatch(parserResult.Match, replaceText);
75-
// }
76-
//
77-
// public IEnumerable<ReplaceMatch> GetReplaceMatches(IEnumerable<FindParserResult> parserResults)
78-
// => parserResults.Select(GetReplaceMatch);
42+
public ReplaceResult Replace(IInput input, FindParserResult findResult)
43+
{
44+
IParsingContext context = new ParsingContext(input, []);
45+
46+
foreach (var placeholder in findResult.Placeholders)
47+
context.Add(placeholder.Key, placeholder.Value);
48+
49+
foreach (var replaceRule in _replaceRules)
50+
{
51+
if (!replaceRule.IsMatch(ref context))
52+
continue;
53+
54+
foreach (var assignment in replaceRule.Assignments)
55+
{
56+
var placeholder = assignment.GetPlaceholder(ref context);
57+
var newValue = assignment.GetValue(ref context);
58+
context[placeholder.Name] = new PlaceholderReplace(placeholder, newValue);
59+
}
60+
}
61+
62+
var newPlaceholders = context.ToDictionary();
63+
var result = _replaceBuilder.Build(ref context);
64+
return new ReplaceResult(findResult, newPlaceholders, result);
65+
}
7966
}

0 commit comments

Comments
 (0)