Skip to content

Commit bff4e31

Browse files
committed
Some for StructuralSearchParser
1 parent f43c0f5 commit bff4e31

File tree

19 files changed

+307
-34
lines changed

19 files changed

+307
-34
lines changed

src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Text;
56
using System.Text.Json;
67
using System.Text.RegularExpressions;
78
using Pidgin;
@@ -20,6 +21,24 @@ internal static class Program
2021
{
2122
static void Main(string[] args)
2223
{
24+
25+
var source = "test;;;test;;;.";
26+
var parser = Parser.OneOf(Parser<char>.Any.ThenReturn(Unit.Value), Parser<char>.End);
27+
28+
29+
var t = Parser<char>.Any.AtLeastOnceAsStringUntil(Lookahead(String(";").Then(Not(String(";"))).Try())).ParseOrThrow(source);
30+
31+
32+
var path = "Test.txt";
33+
var oldText = "0123456789";
34+
var text = "test";
35+
File.WriteAllText(path, oldText);
36+
37+
using var stringReader = text.AsStream();
38+
using var streamWriter = File.OpenWrite(path);
39+
40+
stringReader.CopyPartTo(streamWriter, 0, 7);
41+
2342
// var config = YmlHelper.Parse(
2443
// @"C:\Users\roman\GitHub\SimpleStateMachine.StructuralSearch\src\SimpleStateMachine.StructuralSearch.Tests\ConfigurationFile\FullConfig.yml");
2544
//

src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public Placeholder GetPlaceholder(string name)
2121
return Placeholder.CreateEmpty(this, name, string.Empty);
2222
}
2323

24-
public IReadOnlyDictionary<string, Placeholder> Switch()
24+
public IReadOnlyDictionary<string, Placeholder> SwitchOnNew()
25+
{
26+
throw new System.NotImplementedException();
27+
}
28+
29+
public void Set(IReadOnlyDictionary<string, Placeholder> placeholders)
30+
{
31+
throw new System.NotImplementedException();
32+
}
33+
34+
public void Clear()
2535
{
2636
throw new System.NotImplementedException();
2737
}

src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ public void TemplateParsingShouldBeSuccess(string template, string source, strin
3535
}
3636

3737
[Theory]
38-
[InlineData("$var$;", "test;;;", "test;;")]
38+
[InlineData("$var$;", "test;;", "test")]
3939
[InlineData("$var$;.", "test;;;.", "test;;")]
40-
[InlineData("$var$;$var2$;", "test;;;test;;;", "test",";;test;;")]
4140
public void TemplateParsingShouldBeSuccess2(string template, string source, params string[] values)
4241
{
4342
var input = Input.String(source);

src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs

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

@@ -9,8 +10,8 @@ public class StructuralSearchParserTests
910
[Theory]
1011
// [InlineData("AssignmentNullUnionOperator")]
1112
// [InlineData("NullUnionOperator")]
12-
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs")]
13-
public static void StructuralSearchShouldBeSuccess(string exampleName, string exampleFilePath)
13+
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)]
14+
public static void StructuralSearchShouldBeSuccess(string exampleName, string exampleFilePath, int matchesCount)
1415
{
1516
var config = ConfigurationMock.GetConfigurationFromFiles(exampleName);
1617
var parser = new StructuralSearchParser(config);
@@ -19,7 +20,13 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex
1920
var input = Input.File(fileInfo);
2021
IParsingContext context = new ParsingContext(input);
2122

22-
parser.Parse(ref context);
23+
var matches = parser.Parse(ref context);
24+
Assert.Equal(matches.Count(), matchesCount);
25+
// foreach (var match in matches)
26+
// {
27+
// input.Replace(match.Match);
28+
// }
29+
2330
}
2431
}
2532
}

src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace SimpleStateMachine.StructuralSearch.Configurations
77
{
88
public class Configuration : IEquatable<Configuration>
99
{
10-
public string FindTemplate { get; set; }
10+
public string FindTemplate { get; init; }
1111

12-
public List<string>? FindRules { get; set; }
12+
public List<string>? FindRules { get; init; }
1313

14-
public string ReplaceTemplate { get; set; }
14+
public string ReplaceTemplate { get; init; }
1515

16-
public List<string>? ReplaceRules { get; set; }
16+
public List<string>? ReplaceRules { get; init; }
1717

1818
public bool Equals(Configuration? other)
1919
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Custom;
5+
6+
// public class StreamReplacer : Stream
7+
// {
8+
//
9+
// private readonly Stream _baseStream;
10+
//
11+
// public StreamReplacer(Stream baseStream)
12+
// {
13+
// _baseStream = baseStream;
14+
// }
15+
//
16+
// public Task Replace(Stream stream, int fromStart, int fromEnd, int toStart, int toEnd)
17+
// {
18+
// stream.SetLength();
19+
// stream.Position = fromStart;
20+
// stream.
21+
// }
22+
//
23+
// public override void Flush()
24+
// {
25+
// throw new System.NotImplementedException();
26+
// }
27+
//
28+
// public override int Read(byte[] buffer, int offset, int count)
29+
// {
30+
// throw new System.NotImplementedException();
31+
// }
32+
//
33+
// public override long Seek(long offset, SeekOrigin origin)
34+
// {
35+
// throw new System.NotImplementedException();
36+
// }
37+
//
38+
// public override void SetLength(long value)
39+
// {
40+
// throw new System.NotImplementedException();
41+
// }
42+
//
43+
// public override void Write(byte[] buffer, int offset, int count)
44+
// {
45+
// throw new System.NotImplementedException();
46+
// }
47+
//
48+
// public override bool CanRead { get; }
49+
// public override bool CanSeek { get; }
50+
// public override bool CanWrite { get; }
51+
// public override long Length { get; }
52+
// public override long Position { get; set; }
53+
// }

src/SimpleStateMachine.StructuralSearch/EmptyInput.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
99
throw new System.NotImplementedException();
1010
}
1111

12+
public void Replace(Match<string> match, string value)
13+
{
14+
throw new System.NotImplementedException();
15+
}
16+
1217
public string Extension => string.Empty;
1318
public string Path => string.Empty;
1419
public string Name => string.Empty;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.IO;
2+
using System.Text;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Extensions;
5+
6+
public static class StringExtensions
7+
{
8+
public static MemoryStream AsStream(this string str)
9+
{
10+
return new MemoryStream(Encoding.UTF8.GetBytes(str));
11+
}
12+
}

src/SimpleStateMachine.StructuralSearch/FileInput.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
using System.IO;
2+
using System.Threading;
23
using Pidgin;
4+
using SimpleStateMachine.StructuralSearch.Helper;
35

46
namespace SimpleStateMachine.StructuralSearch
57
{
68
public class FileInput : IInput
79
{
10+
public readonly FileInfo FileInfo;
11+
812
public FileInput(FileInfo fileInfo)
913
{
1014
FileInfo = fileInfo;
1115
}
12-
13-
public readonly FileInfo FileInfo;
14-
16+
1517
public Result<char, T> ParseBy<T>(Parser<char, T> parser)
1618
{
1719
return parser.Parse(FileInfo.OpenText());
1820
}
1921

22+
public void Replace(Match<string> match, string value)
23+
{
24+
using var valueReader = new StringReader(value);
25+
using var streamWriter = new StreamWriter(FileInfo.FullName);
26+
var emptyStart = match.Offset.Start + match.Lenght;
27+
var emptyEnd = match.Offset.End;
28+
valueReader.CopyPartTo(streamWriter, match.Offset.Start, match.Lenght);
29+
30+
//return (emptyStart, emptyEnd);
31+
}
32+
2033
public string Extension => FileInfo.Extension;
2134
public string Path => System.IO.Path.GetFullPath(FileInfo.FullName);
2235
public string Name => System.IO.Path.GetFileNameWithoutExtension(FileInfo.Name);

src/SimpleStateMachine.StructuralSearch/FindParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public IEnumerable<FindParserMatch> Parse(ref IParsingContext context)
2828
.Match()
2929
.ThenInvoke(match =>
3030
{
31-
var placeholders= parsingContext.Switch();
31+
var placeholders= parsingContext.SwitchOnNew();
3232
matches.Add(new FindParserMatch(match, placeholders));
3333
})
3434
.ThenReturn(Unit.Value)
@@ -38,7 +38,7 @@ public IEnumerable<FindParserMatch> Parse(ref IParsingContext context)
3838
.ThenInvoke(x =>
3939
{
4040
res.Append(x);
41-
parsingContext.Switch();
41+
parsingContext.SwitchOnNew();
4242
})
4343
.ThenReturn(Unit.Value);
4444

0 commit comments

Comments
 (0)