Skip to content

Commit 3f990b8

Browse files
committed
UnarySubRule, fix rules parser
1 parent 8e25da0 commit 3f990b8

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Pidgin;
1+
using System.Text.RegularExpressions;
2+
using Pidgin;
23
using static Pidgin.Parser;
34

45
namespace SimpleStateMachine.StructuralSearch.Sandbox
@@ -7,11 +8,18 @@ internal static class Program
78
{
89
static void Main(string[] args)
910
{
10-
var result = StructuralSearch.ParseFindRule("$var$ Is int or equals \"test\"");
11-
var result1 = result.Execute("test");
1211

1312

14-
var t = ExprParser.ParseOrThrow("( 2 + 2 ) * 2");
13+
14+
var rule = StructuralSearch.ParseFindRule("$var$ Not (Is int or equals \"test\") and match \"^[0-9]+.[0-9]+$\"");
15+
var result1 = rule.Execute("test");
16+
var result2 = rule.Execute("125");
17+
var result3 = rule.Execute("5.3");
18+
19+
20+
21+
22+
var t = ExprParser.ParseOrThrow("2 + 2 + 2");
1523
var resw = t.Invoke();
1624
var test = String("return ")
1725
.Then(AnyCharExcept(';').ManyString())

src/SimpleStateMachine.StructuralSearch/Rules/UnarySubRule.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace SimpleStateMachine.StructuralSearch.Rules
1+
using System;
2+
using System.Text.RegularExpressions;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Rules
25
{
36
public class UnarySubRule : IRule
47
{
@@ -14,7 +17,17 @@ public UnarySubRule(SubRuleType type, IRuleParameter parameter)
1417

1518
public bool Execute(string value)
1619
{
17-
throw new System.NotImplementedException();
20+
var param = Parameter.GetValue();
21+
22+
return Type switch
23+
{
24+
SubRuleType.Equals => value.Equals(param),
25+
SubRuleType.Contains => value.Contains(param),
26+
SubRuleType.StartsWith => value.StartsWith(param),
27+
SubRuleType.EndsWith => value.EndsWith(param),
28+
SubRuleType.Match => Regex.IsMatch(value, param),
29+
_ => throw new ArgumentOutOfRangeException()
30+
};
1831
}
1932
}
2033
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/RulesParser.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ internal static Parser<char, Func<IRule, IRule>> Unary(Parser<char, UnaryRuleTyp
1717

1818

1919
internal static readonly Parser<char, Func<IRule, IRule, IRule>> And
20-
= Binary(Parsers.EnumValue(BinaryRuleType.And).Trim());
20+
= Binary(Parsers.EnumValue(BinaryRuleType.And, true).Trim());
2121

2222
internal static readonly Parser<char, Func<IRule, IRule, IRule>> Or
23-
= Binary(Parsers.EnumValue(BinaryRuleType.Or).Trim());
23+
= Binary(Parsers.EnumValue(BinaryRuleType.Or, true).Trim());
2424

2525
internal static readonly Parser<char, Func<IRule, IRule, IRule>> NOR
26-
= Binary(Parsers.EnumValue(BinaryRuleType.NOR).Trim());
26+
= Binary(Parsers.EnumValue(BinaryRuleType.NOR, true).Trim());
2727

2828
internal static readonly Parser<char, Func<IRule, IRule, IRule>> XOR
29-
= Binary(Parsers.EnumValue(BinaryRuleType.XOR).Trim());
29+
= Binary(Parsers.EnumValue(BinaryRuleType.XOR, true).Trim());
3030

3131
internal static readonly Parser<char, Func<IRule, IRule, IRule>> NAND
32-
= Binary(Parsers.EnumValue(BinaryRuleType.NAND).Trim());
32+
= Binary(Parsers.EnumValue(BinaryRuleType.NAND, true).Trim());
3333

3434
internal static readonly Parser<char, Func<IRule, IRule, IRule>> XNOR
35-
= Binary(Parsers.EnumValue(BinaryRuleType.XNOR).Trim());
35+
= Binary(Parsers.EnumValue(BinaryRuleType.XNOR, true).Trim());
3636

3737
internal static readonly Parser<char, Func<IRule, IRule>> Not
38-
= Unary(Parsers.EnumValue(UnaryRuleType.Not).Trim());
38+
= Unary(Parsers.EnumValue(UnaryRuleType.Not, true).Trim());
3939

4040
internal static readonly Parser<char, IRule> Expr = ExpressionParser.Build<char, IRule>(
4141
rule => (

0 commit comments

Comments
 (0)