Skip to content

Commit 9c2edb4

Browse files
committed
string format in parameters
1 parent 3d46eba commit 9c2edb4

File tree

7 files changed

+133
-75
lines changed

7 files changed

+133
-75
lines changed

src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Text.RegularExpressions;
1+
using System;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
24
using Pidgin;
35
using SimpleStateMachine.StructuralSearch.Extensions;
46
using SimpleStateMachine.StructuralSearch.Rules;
@@ -11,6 +13,7 @@ internal static class Program
1113
{
1214
static void Main(string[] args)
1315
{
16+
var tesds = ParametersParser.StringFormatParameter.ParseOrThrow("\"tfasdfa\\\"sd$var$.Lenght\"");
1417
var rule = StructuralSearch.ParseFindRule("$var$ equals $var$.Lenght and Not StartsWith \"123\"");
1518
var rule2 = StructuralSearch.ParseFindRule("$var$ equals $var$.Offset.Start and Not StartsWith \"123\"");
1619
var result1 = rule.Execute("test");

src/SimpleStateMachine.StructuralSearch/Constant.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public static partial class Constant
7777
/// </summary>
7878
public static readonly char DoubleQuotes = '\"';
7979

80+
/// <summary>
81+
/// Char: '\"'
82+
/// </summary>
83+
public static readonly char BackSlash = '\\';
84+
8085
/// <summary>
8186
/// Char: '.'
8287
/// </summary>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Rules
5+
{
6+
public class StringFormatParameter : IRuleParameter
7+
{
8+
public IEnumerable<IRuleParameter> Parameters { get; }
9+
10+
public StringFormatParameter(IEnumerable<IRuleParameter> parameters)
11+
{
12+
Parameters = parameters;
13+
}
14+
15+
public string GetValue()
16+
{
17+
return string.Join(string.Empty, Parameters.Select(x => x.GetValue()));
18+
}
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using Pidgin;
3+
using SimpleStateMachine.StructuralSearch.Extensions;
4+
using SimpleStateMachine.StructuralSearch.Rules;
5+
6+
namespace SimpleStateMachine.StructuralSearch
7+
{
8+
public static class ParametersParser
9+
{
10+
public static readonly Parser<char, IRuleParameter> PlaceholderParameter =
11+
CommonTemplateParser.Placeholder
12+
.Select(x => new PlaceholderParameter(x))
13+
.As<char, PlaceholderParameter, IRuleParameter>()
14+
.TrimStart()
15+
.Try();
16+
17+
public static readonly Parser<char, IRuleParameter> Parameter =
18+
Parser.OneOf(PlaceholderPropertyParser.PlaceholderPropertyParameter, PlaceholderParameter);
19+
20+
public static readonly Parser<char, IEnumerable<IRuleParameter>> Parameters =
21+
Parameter.AtLeastOnce();
22+
23+
public static readonly Parser<char, IRuleParameter> StringParameter =
24+
CommonParser.Escaped(Constant.DoubleQuotes, Constant.PlaceholderSeparator)
25+
.Or(Parser.AnyCharExcept(Constant.DoubleQuotes, Constant.PlaceholderSeparator))
26+
.AtLeastOnceString()
27+
.Select(x => new StringParameter(x))
28+
.As<char, StringParameter, IRuleParameter>()
29+
.Try();
30+
31+
public static readonly Parser<char, IRuleParameter> StringFormatParameter =
32+
Parser.OneOf(StringParameter, PlaceholderPropertyParser.PlaceholderPropertyParameter, PlaceholderParameter)
33+
.AtLeastOnce()
34+
.Between(CommonParser.DoubleQuotes)
35+
.Select(parameters => new StringFormatParameter(parameters))
36+
.As<char, StringFormatParameter, IRuleParameter>()
37+
.TrimStart()
38+
.Try();
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using Pidgin;
3+
using SimpleStateMachine.StructuralSearch.Extensions;
4+
using SimpleStateMachine.StructuralSearch.Rules;
5+
6+
namespace SimpleStateMachine.StructuralSearch
7+
{
8+
9+
public static class PlaceholderPropertyParser
10+
{
11+
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> File =
12+
Parsers.EnumValue(PlaceholderProperty.File, true)
13+
.Then(CommonParser.Dote)
14+
.Then(Parsers.Enum<FileProperty>(true))
15+
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
16+
new PlaceholderFileParameter(placeholder, property)))
17+
.Try();
18+
19+
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Column =
20+
Parsers.EnumValue(PlaceholderProperty.Column, true)
21+
.Then(CommonParser.Dote)
22+
.Then(Parsers.Enum<ColumnProperty>(true))
23+
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
24+
new PlaceholderColumnParameter(placeholder, property)))
25+
.Try();
26+
27+
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Line =
28+
Parsers.EnumValue(PlaceholderProperty.Line, true)
29+
.Then(CommonParser.Dote)
30+
.Then(Parsers.Enum<LineProperty>(true))
31+
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
32+
new PlaceholderLineParameter(placeholder, property)))
33+
.Try();
34+
35+
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Offset =
36+
Parsers.EnumValue(PlaceholderProperty.Offset, true)
37+
.Then(CommonParser.Dote)
38+
.Then(Parsers.Enum<OffsetProperty>(true))
39+
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
40+
new PlaceholderOffsetParameter(placeholder, property)))
41+
.Try();
42+
43+
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Lenght =
44+
Parsers.EnumValue(PlaceholderProperty.Lenght, true)
45+
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
46+
new PlaceholderLenghtParameter(placeholder, property)))
47+
.Try();
48+
49+
public static readonly Parser<char, IRuleParameter> PlaceholderPropertyParameter =
50+
CommonTemplateParser.Placeholder.Before(CommonParser.Dote)
51+
.Select(name => new PlaceholderParameter(name))
52+
.Then(Parser.OneOf(Lenght, File, Column, Offset, Line),
53+
(placeholder, func) => func(placeholder))
54+
.TrimStart()
55+
.Try();
56+
}
57+
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,10 @@ internal static Parser<char, T> Parenthesised<T>(Parser<char, T> parser, Func<Pa
4646
return parser.Between(custom(Parsers.Stringc(Constant.LeftParenthesis)),
4747
custom(Parsers.Stringc(Constant.RightParenthesis)));
4848
}
49+
50+
internal static Parser<char, char> Escaped(params char [] chars)
51+
{
52+
return Char(Constant.BackSlash).Then(OneOf(chars));
53+
}
4954
}
5055
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/SubFindRuleParser.cs

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,13 @@ namespace SimpleStateMachine.StructuralSearch.Rules
77
{
88
public static class SubRuleParser
99
{
10-
public static readonly Parser<char, IRuleParameter> PlaceholderParameter =
11-
CommonTemplateParser.Placeholder
12-
.Select(x => new PlaceholderParameter(x))
13-
.As<char, PlaceholderParameter, IRuleParameter>()
14-
.TrimStart()
15-
.Try();
16-
17-
public static readonly Parser<char, IRuleParameter> StringParameter =
18-
Parser.AnyCharExcept(Constant.DoubleQuotes)
19-
.AtLeastOnceString()
20-
.Between(CommonParser.DoubleQuotes)
21-
.Select(x => new StringParameter(x))
22-
.As<char, StringParameter, IRuleParameter>()
23-
.TrimStart()
24-
.Try();
25-
26-
27-
public static readonly Parser<char, IRuleParameter> Parameter =
28-
Parser.OneOf(PlaceholderPropertyParser.PlaceholderPropertyParameter, PlaceholderParameter, StringParameter);
29-
30-
public static readonly Parser<char, IEnumerable<IRuleParameter>> Parameters =
31-
Parameter.AtLeastOnce();
32-
3310
public static readonly Parser<char, SubRuleType> SubRuleType =
3411
Parsers.EnumExcept(true, Rules.SubRuleType.Is, Rules.SubRuleType.In)
3512
.TrimStart();
3613

3714
public static readonly Parser<char, IRule> UnarySubRule =
3815
Parser.Map((type, param) => new UnarySubRule(type, param),
39-
SubRuleType, Parameter)
16+
SubRuleType, ParametersParser.Parameter)
4017
.As<char, UnarySubRule, IRule>()
4118
.Try();
4219

@@ -54,57 +31,8 @@ public static class SubRuleParser
5431
public static readonly Parser<char, IRule> InSubRule =
5532
Parser.Map((type, param) => new InRule(type, param),
5633
Parsers.EnumValue(Rules.SubRuleType.In, true)
57-
.TrimStart(), Parameters)
34+
.TrimStart(), ParametersParser.Parameters)
5835
.As<char, InRule, IRule>()
5936
.Try();
6037
}
61-
62-
public static class PlaceholderPropertyParser
63-
{
64-
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> File =
65-
Parsers.EnumValue(PlaceholderProperty.File, true)
66-
.Then(CommonParser.Dote)
67-
.Then(Parsers.Enum<FileProperty>(true))
68-
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
69-
new PlaceholderFileParameter(placeholder, property)))
70-
.Try();
71-
72-
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Column =
73-
Parsers.EnumValue(PlaceholderProperty.Column, true)
74-
.Then(CommonParser.Dote)
75-
.Then(Parsers.Enum<ColumnProperty>(true))
76-
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
77-
new PlaceholderColumnParameter(placeholder, property)))
78-
.Try();
79-
80-
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Line =
81-
Parsers.EnumValue(PlaceholderProperty.Line, true)
82-
.Then(CommonParser.Dote)
83-
.Then(Parsers.Enum<LineProperty>(true))
84-
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
85-
new PlaceholderLineParameter(placeholder, property)))
86-
.Try();
87-
88-
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Offset =
89-
Parsers.EnumValue(PlaceholderProperty.Offset, true)
90-
.Then(CommonParser.Dote)
91-
.Then(Parsers.Enum<OffsetProperty>(true))
92-
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
93-
new PlaceholderOffsetParameter(placeholder, property)))
94-
.Try();
95-
96-
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Lenght =
97-
Parsers.EnumValue(PlaceholderProperty.Lenght, true)
98-
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
99-
new PlaceholderLenghtParameter(placeholder, property)))
100-
.Try();
101-
102-
public static readonly Parser<char, IRuleParameter> PlaceholderPropertyParameter =
103-
CommonTemplateParser.Placeholder.Before(CommonParser.Dote)
104-
.Select(name => new PlaceholderParameter(name))
105-
.Then(Parser.OneOf(Lenght, File, Column, Offset, Line),
106-
(placeholder, func) => func(placeholder))
107-
.TrimStart()
108-
.Try();
109-
}
11038
}

0 commit comments

Comments
 (0)