Skip to content

Commit 5f830ce

Browse files
committed
use parsing context
1 parent b0cce27 commit 5f830ce

File tree

14 files changed

+178
-150
lines changed

14 files changed

+178
-150
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFileTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ public void ConfigurationFileParsingShouldBeSuccess(string filePath)
2121
.WithNamingConvention(PascalCaseNamingConvention.Instance)
2222
.Build();
2323

24-
var cfg = deserializer.Deserialize<ConfigurationsFile>(yml);
24+
var cfg = deserializer.Deserialize<ConfigurationFile>(yml);
2525
var mock = Mock();
2626
Assert.Equal(mock, cfg);
2727
}
2828

29-
private ConfigurationsFile Mock()
29+
private ConfigurationFile Mock()
3030
{
3131
var names = new[] { "AssignmentNullUnionOperator", "NullUnionOperator", "TernaryOperator"};
3232

33-
var configurationFile = new ConfigurationsFile
33+
var configurationFile = new ConfigurationFile
3434
{
3535
Configurations = new List<Configuration>()
3636
};

src/SimpleStateMachine.StructuralSearch.Tests/FindTemplateTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class FindTemplateTests
1414
public void TemplateParsingShouldBeSuccess(string templatePath)
1515
{
1616
var findTemplate = File.ReadAllText(templatePath);
17-
var template = StructuralSearch.ParseFindTemplate(findTemplate, new ParsingContext());
17+
var template = StructuralSearch.ParseFindTemplate(findTemplate);
1818

1919
Assert.NotNull(template);
2020
}
@@ -28,12 +28,11 @@ public void SourceParsingBeFindTemplateShouldBeSuccess(string templatePath, stri
2828
{
2929
var findTemplate = File.ReadAllText(templatePath);
3030
var source = File.ReadAllText(sourcePath);
31-
32-
var context = new ParsingContext();
33-
var template = StructuralSearch.ParseFindTemplate(findTemplate, context);
34-
var result = template.ParseOrThrow(source);
31+
var findParser = StructuralSearch.ParseFindTemplate(findTemplate);
32+
var parsingContext = new ParsingContext();
33+
var result = findParser.Parse(parsingContext, source);
3534

36-
Assert.NotNull(template);
35+
Assert.NotNull(findParser);
3736
Assert.NotNull(result);
3837
Assert.NotNull(result.Value);
3938
Assert.Equal(result.Lenght, source.Length);

src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class PlaceholderParserTests
88
[Fact]
99
public void FindTemplateShouldBeNotEmpty()
1010
{
11-
Assert.Throws<ParseException>(() => StructuralSearch.ParseFindTemplate(string.Empty, new ParsingContext()));
11+
Assert.Throws<ParseException>(() => StructuralSearch.ParseFindTemplate(string.Empty));
1212
}
1313

1414
[Theory]
@@ -18,10 +18,10 @@ public void FindTemplateShouldBeNotEmpty()
1818
[InlineData("($test$)", "(value (test) )", "value (test) ")]
1919
public void TemplateParsingShouldBeSuccess(string template, string source, string result)
2020
{
21-
var context = new ParsingContext();
22-
var templateParser = StructuralSearch.ParseFindTemplate(template, context);
23-
var res = templateParser.ParseOrThrow(source);
24-
var value = context.GetPlaceholder("test");
21+
var parsingContext = new ParsingContext();
22+
var templateParser = StructuralSearch.ParseFindTemplate(template);
23+
var res = templateParser.Parse(parsingContext, source);
24+
var value = parsingContext.GetPlaceholder("test");
2525

2626
Assert.Equal(value, result);
2727
}
@@ -30,9 +30,9 @@ public void TemplateParsingShouldBeSuccess(string template, string source, strin
3030
[InlineData("$var$;$var2$;", "test;;;test;;;", "value ")]
3131
public void TemplateParsingShouldBeSuccess2(string template, string source, string result)
3232
{
33-
var context = new ParsingContext();
34-
var templateParser = StructuralSearch.ParseFindTemplate(template, context);
35-
var res = templateParser.ParseOrThrow(source);
33+
var parsingContext = new ParsingContext();
34+
var templateParser = StructuralSearch.ParseFindTemplate(template);
35+
var res = templateParser.Parse(parsingContext, source);
3636

3737

3838
// var templateStr = File.ReadAllText(templatePath);

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ReplaceRuleParserTests
1313
[InlineData("Not equals $var$.offset.Start", "$var$.offset.Start")]
1414
[InlineData("equals $var$.Lenght and Not StartsWith \"123\"", "$var$.offset.Start.Trim")]
1515
[InlineData("equals $var$.Lenght and Not StartsWith \"\\\"Test\"", "$var$.offset.Start.ToUpper")]
16-
public void FindRuleParsingShouldBeSuccess(string findRule, string replaceRule)
16+
public void ReplaceRuleParsingShouldBeSuccess(string findRule, string replaceRule)
1717
{
1818
var placeholder = "$var$";
1919
var replaceRuleStr = $"{placeholder} {findRule} => {replaceRule}";

src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationsFile.cs renamed to src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
namespace SimpleStateMachine.StructuralSearch.Configurations
66
{
7-
public class ConfigurationsFile: IEquatable<ConfigurationsFile>
7+
public class ConfigurationFile: IEquatable<ConfigurationFile>
88
{
99
public List<Configuration> Configurations { get; set; }
1010

11-
public bool Equals(ConfigurationsFile? other)
11+
public bool Equals(ConfigurationFile? other)
1212
{
1313
return Configurations.SequenceEqual(other.Configurations);
1414
}
1515

1616
public override bool Equals(object? obj)
1717
{
1818
if (obj.GetType() != this.GetType()) return false;
19-
return Equals((ConfigurationsFile)obj);
19+
return Equals((ConfigurationFile)obj);
2020
}
2121

2222
public override int GetHashCode()

src/SimpleStateMachine.StructuralSearch/Extensions/SourceMatchManyParserExtensions.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using Pidgin;
3+
using SimpleStateMachine.StructuralSearch.Extensions;
4+
5+
namespace SimpleStateMachine.StructuralSearch
6+
{
7+
public class FindParser : IFindParser
8+
{
9+
private SeriesParser Parser { get; }
10+
public FindParser(SeriesParser parser)
11+
{
12+
Parser = parser;
13+
}
14+
15+
public SourceMatch Parse(IParsingContext context, string input)
16+
{
17+
Parser.SetContext(context);
18+
var result = Parser.Select(x => string.Join(string.Empty, x)).AsMatch().Parse(input);
19+
20+
return result.Success ? result.Value : SourceMatch.Empty;
21+
}
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using Pidgin;
3+
using SimpleStateMachine.StructuralSearch.ReplaceTemplate;
4+
5+
namespace SimpleStateMachine.StructuralSearch
6+
{
7+
public interface IFindParser
8+
{
9+
SourceMatch Parse(IParsingContext context, string input);
10+
}
11+
}

src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ public static Parser<TToken, IEnumerable<T>> MapToMany<TToken, T>(Parser<TToken,
7777
}, parser1, parser2);
7878
}
7979

80-
81-
public static Parser<TToken, R> Series<TToken, T, R>(IParsingContext context, IEnumerable<Parser<TToken, T>> parsers,
82-
Func<IEnumerable<T>, R> func)
83-
{
84-
if (parsers == null)
85-
throw new ArgumentNullException(nameof(parsers));
86-
if (func == null)
87-
throw new ArgumentNullException(nameof(func));
88-
89-
return new SeriesParser<TToken, T, R>(context, parsers, func);
90-
}
91-
9280
public static Parser<char, IEnumerable<T>> BetweenChars<T>(char left, char right,
9381
Func<char, Parser<char, T>> leftRight,
9482
Parser<char, IEnumerable<T>> expr)

src/SimpleStateMachine.StructuralSearch/Parsers/SeriesParser.cs

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)