Skip to content

Commit 1a7d7b2

Browse files
committed
push
1 parent d4e514f commit 1a7d7b2

File tree

16 files changed

+121
-110
lines changed

16 files changed

+121
-110
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
using System.IO;
2-
using System.Linq;
32
using Pidgin;
43
using SimpleStateMachine.StructuralSearch.Context;
5-
using SimpleStateMachine.StructuralSearch.Templates.ReplaceTemplate;
64
using Xunit;
75

86
namespace SimpleStateMachine.StructuralSearch.Tests;
97

108
public static class ReplaceTemplateTests
119
{
12-
[Theory]
13-
[InlineData("ReplaceTemplate/NullUnionOperator.txt", 6)]
14-
[InlineData("ReplaceTemplate/AssignmentNullUnionOperator.txt", 4)]
15-
[InlineData("ReplaceTemplate/TernaryOperator.txt", 7)]
16-
public static void ReplaceTemplateParsingShouldHaveStepCount(string templatePath, int stepsCount)
17-
{
18-
var replaceTemplate = File.ReadAllText(templatePath);
19-
var replaceBuilder = StructuralSearch.StructuralSearch.ParseReplaceTemplate(replaceTemplate);
20-
21-
Assert.NotNull(replaceTemplate);
22-
Assert.Equal(((ReplaceBuilder)replaceBuilder).Steps.Count(), stepsCount);
23-
}
24-
2510
[Theory]
2611
[InlineData("ReplaceTemplate/NullUnionOperator.txt", "ReplaceResult/NullUnionOperator.txt",
2712
new[] { "var", "sign", "value1", "value2"},
@@ -54,21 +39,28 @@ public static void ReplaceBuildShouldBeSuccess(string templatePath, string resul
5439
// TODO validation parenthesis for parameters
5540

5641
[Theory]
42+
[InlineData("test")]
43+
[InlineData("()")]
44+
[InlineData("$var1$")]
45+
[InlineData("test () test")]
46+
[InlineData("test $var1$ test")]
47+
[InlineData("($var1$)")]
48+
[InlineData("($var1$ test)")]
49+
[InlineData("test (test)")]
50+
[InlineData("test ($var1$)")]
51+
[InlineData("test ($var1$) test")]
52+
5753
[InlineData("test $var1$.Lenght")]
5854
[InlineData("(test) $var1$.Lenght")]
5955
[InlineData("test ($var1$.Lenght)")]
6056
[InlineData("(test $var1$.Lenght)")]
61-
[InlineData("test ")]
6257
[InlineData("($var1$.Lenght)")]
6358
[InlineData(" ($var1$.Lenght)")]
64-
[InlineData(" ( )")]
65-
[InlineData("test ( )")]
6659
[InlineData(" (test $var1$.Lenght)")]
6760
[InlineData("(test) ($var1$.Lenght)")]
6861
[InlineData("((test) $var1$.Lenght)")]
6962
[InlineData("(test ($var1$.Lenght))")]
7063
[InlineData("((test) ($var1$.Lenght))")]
71-
[InlineData("()")]
7264
[InlineData("(test ($var1$.Lenght) test2)")]
7365
public static void ReplaceTemplateParsingShouldBeSuccess(string templateStr)
7466
{

src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
namespace SimpleStateMachine.StructuralSearch.Helper;
65

src/SimpleStateMachine.StructuralSearch/Parsers/FindTemplateParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace SimpleStateMachine.StructuralSearch.Parsers;
77

88
internal class FindTemplateParser : Parser<char, IEnumerable<string>>, IContextDependent
99
{
10-
private readonly IEnumerable<Parser<char, string>> _parsers;
10+
private readonly List<Parser<char, string>> _parsers;
1111

12-
public FindTemplateParser(IEnumerable<Parser<char, string>> parsers)
12+
public FindTemplateParser(List<Parser<char, string>> parsers)
1313
{
1414
_parsers = parsers;
1515

@@ -18,7 +18,7 @@ public FindTemplateParser(IEnumerable<Parser<char, string>> parsers)
1818

1919
private void InitializeLookaheadParsers()
2020
{
21-
var count = _parsers.Count();
21+
var count = _parsers.Count;
2222

2323
for (var i = count-1; i >= 0 ; i--)
2424
{
@@ -34,7 +34,7 @@ private void InitializeLookaheadParsers()
3434
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected, out IEnumerable<string> result)
3535
{
3636
var results = new List<string>();
37-
var count = _parsers.Count();
37+
var count = _parsers.Count;
3838

3939
for (int i = 0; i < count; i++)
4040
{

src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using Pidgin;
54
using SimpleStateMachine.StructuralSearch.Extensions;

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ protected override Parser<char, string> BuildParser(Func<Parser<char, string>?>
5353
}).Try());
5454
}
5555

56-
// Was changed from StringLiteralChar
57-
var anyString = Grammar.NonLanguageSyntaxChar
56+
var anyString = Grammar.StringLiteralChar
5857
.AtLeastOnceAsStringUntil(lookahead);
5958

6059
var simpleString = Grammar.StringLiteral;
@@ -101,7 +100,7 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
101100
match: match
102101
);
103102

104-
Context.Add(_name, placeholderObj);
103+
Context[_name] = placeholderObj;
105104

106105
res = Context.FindRules
107106
.Where(r => r.IsApplicableForPlaceholder(_name))

src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@ namespace SimpleStateMachine.StructuralSearch.Rules.Parameters;
66

77
internal class StringFormatParameter : IRuleParameter
88
{
9-
private readonly IEnumerable<IRuleParameter> _parameters;
9+
private readonly List<IRuleParameter> _parameters;
1010

11-
public StringFormatParameter(IEnumerable<IRuleParameter> parameters)
11+
public StringFormatParameter(List<IRuleParameter> parameters)
1212
{
1313
_parameters = parameters;
1414
}
1515

1616
public string GetValue(ref IParsingContext context)
1717
{
18-
var values = new List<string>();
19-
foreach (var parameter in _parameters)
20-
{
21-
values.Add(parameter.GetValue(ref context));
22-
}
23-
24-
return string.Join(string.Empty, values);
18+
var localContext = context;
19+
return string.Join(string.Empty, _parameters.Select(parameter => parameter.GetValue(ref localContext)));
2520
}
2621

2722
public override string ToString()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using SimpleStateMachine.StructuralSearch.Context;
4+
5+
namespace SimpleStateMachine.StructuralSearch.Rules.Parameters;
6+
7+
internal class StringJoinParameter : IRuleParameter
8+
{
9+
private readonly List<IRuleParameter> _parameters;
10+
11+
public StringJoinParameter(List<IRuleParameter> parameters)
12+
{
13+
_parameters = parameters;
14+
}
15+
16+
public string GetValue(ref IParsingContext context)
17+
{
18+
var localContext = context;
19+
var values = _parameters.Select(parameter => parameter.GetValue(ref localContext)).ToList();
20+
return string.Join(string.Empty, values);
21+
}
22+
23+
public override string ToString()
24+
=> string.Join(string.Empty, _parameters.Select(x => x.ToString()));
25+
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static class FindRuleParser
2323
(
2424
Parser.OneOf
2525
(
26-
SubRuleParser.OneOfSubRule,
26+
FindSubRuleParser.OneOfSubRule,
2727
CommonParser.Parenthesised(rule, x => Parser.Char(x).Trim())
2828
),
2929
[

src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubRuleParser.cs renamed to src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindSubRuleParser.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace SimpleStateMachine.StructuralSearch.StructuralSearch;
88

9-
internal static class SubRuleParser
9+
internal static class FindSubRuleParser
1010
{
1111
private static readonly Parser<char, SubRuleType> SubRuleType = Parser.CIEnum<SubRuleType>().Trim();
12-
private static readonly Parser<char, PlaceholderType> PlaceholderType = Parser.CIEnum<PlaceholderType>().TrimStart();
12+
13+
private static readonly Parser<char, PlaceholderType>
14+
PlaceholderType = Parser.CIEnum<PlaceholderType>().TrimStart();
1315

1416
private static Parser<char, IFindRule> BinarySubRule(IRuleParameter left, SubRuleType ruleType) =>
1517
ParametersParser.Parameter
@@ -18,30 +20,30 @@ private static Parser<char, IFindRule> BinarySubRule(IRuleParameter left, SubRul
1820
.As<char, BinarySubRule, IFindRule>()
1921
.Try();
2022

21-
private static Parser<char, IFindRule> IsSubRule(IRuleParameter left, SubRuleType ruleType) =>
23+
private static Parser<char, IFindRule> IsSubRule(IRuleParameter left) =>
2224
PlaceholderType.Select(arg => new IsSubRule(left, arg))
2325
.As<char, IsSubRule, IFindRule>()
2426
.Try();
2527

26-
private static Parser<char, IFindRule> InSubRule(IRuleParameter left, SubRuleType ruleType) =>
28+
private static Parser<char, IFindRule> InSubRule(IRuleParameter left) =>
2729
ParametersParser.Parameters
2830
.ParenthesisedOptional(x => Parser.Char(x).Trim())
2931
.TrimStart()
3032
.Select(args => new InSubRule(left, args))
3133
.As<char, InSubRule, IFindRule>()
3234
.Try();
3335

34-
public static readonly Parser<char, IFindRule> OneOfSubRule =
36+
internal static readonly Parser<char, IFindRule> OneOfSubRule =
3537
Parser.Map
3638
(
37-
func: (left, ruleType) => (left, ruleType),
39+
func: (left, ruleType) => (value: left, type: ruleType),
3840
parser1: ParametersParser.Parameter.Trim(),
3941
parser2: SubRuleType
4042
)
41-
.Then(x => x.ruleType switch
43+
.Then(x => x.type switch
4244
{
43-
Rules.FindRules.Types.SubRuleType.In => InSubRule(x.left, x.ruleType),
44-
Rules.FindRules.Types.SubRuleType.Is => IsSubRule(x.left, x.ruleType),
45-
_ => BinarySubRule(x.left, x.ruleType)
45+
Rules.FindRules.Types.SubRuleType.In => InSubRule(x.value),
46+
Rules.FindRules.Types.SubRuleType.Is => IsSubRule(x.value),
47+
_ => BinarySubRule(x.value, x.type)
4648
});
4749
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal static class FindTemplateParser
4343
Parser.OneOf(TokenInParentheses, Token.AsMany())
4444
.AtLeastOnceUntil(CommonParser.Eof)
4545
.Select(x => x.SelectMany(y => y))
46-
.Select(parsers => new Parsers.FindTemplateParser(parsers));
46+
.Select(parsers => new Parsers.FindTemplateParser(parsers.ToList()));
4747

4848
internal static IFindParser ParseTemplate(string? str) =>
4949
string.IsNullOrEmpty(str)

0 commit comments

Comments
 (0)