Skip to content

Commit e89b9e2

Browse files
committed
fix enum parser, try parse for string, isRule implementation
1 parent 74be7e5 commit e89b9e2

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ internal static class Program
1010
{
1111
static void Main(string[] args)
1212
{
13-
var result = FindRuleParser.ParseTemplate("$var$ Equals \"125\"");
13+
var result = FindRuleParser.ParseTemplate("$var$ Is int");
14+
var result1 = result.Execute("125");
15+
1416

1517
var t = ExprParser.ParseOrThrow("( 2 + 2 ) * 2");
1618
var resw = t.Invoke();

src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,22 @@ public static Parser<TToken, IEnumerable<T>> UntilNot<TToken, T, U>(this Parser<
4343
? parser.Until(Not(terminator))
4444
: throw new ArgumentNullException(nameof(parser));
4545
}
46+
47+
public static bool TryParse(this Parser<char, string> parser, string value, out string? result)
48+
{
49+
if(parser is null)
50+
throw new ArgumentNullException(nameof(parser));
4651

52+
var res = parser.Try().Optional().ParseOrThrow(value, null);
53+
result = res.HasValue ? res.Value : default;
54+
return res.HasValue;
55+
}
56+
public static Parser<TToken, bool> Contains<TToken, T>(this Parser<TToken, T> parser)
57+
{
58+
return parser != null
59+
? parser.Optional().Select(x => x.HasValue)
60+
: throw new ArgumentNullException(nameof(parser));
61+
}
4762

4863
// public static Parser<TToken, TOut> WithResult<TToken, TOut>(this Parser<TToken, TOut> parser, Func<TToken, SourcePos, TOut> transformResult)
4964
// {

src/SimpleStateMachine.StructuralSearch/Extensions/StringParserExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public static Parser<char, T> Trim<T>(this Parser<char, T> parser)
6363
return parser.Before(SkipWhitespaces);
6464
}
6565

66-
public static Parser<char, TEnum> AsEnum<TEnum>(this Parser<char, string> parser)
66+
public static Parser<char, TEnum> AsEnum<TEnum>(this Parser<char, string> parser, bool ignoreCase)
6767
where TEnum: struct, Enum
6868
{
69-
return parser.Select(Enum.Parse<TEnum>);
69+
return parser.Select(value => Enum.Parse<TEnum>(value, ignoreCase));
7070
}
7171
}
7272
}

src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded)
1616
_parser = Parser.OneOf(EnumHelper.GetNamesExcept(excluded)
1717
.Select(value => Parsers.String(value, ignoreCase))
1818
.Select(Parser.Try))
19-
.AsEnum<TEnum>();
19+
.AsEnum<TEnum>(ignoreCase);
2020
}
2121

2222
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds,

src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ public static Parser<char, string> Stringc(char character, bool ignoreCase = fal
1616

1717
public static Parser<char, string> String(string value, bool ignoreCase)
1818
{
19-
return ignoreCase ? Parser.String(value) : Parser.CIString(value);
19+
return ignoreCase ? Parser.CIString(value): Parser.String(value);
2020
}
2121

22-
public static Parser<char, bool> Contains(string value, bool ignoreCase = false)
23-
{
24-
return String(value, ignoreCase).Optional().Select(x => x.HasValue);
25-
}
26-
2722
// public static Parser<char, string> EnumValue<TEnum>(TEnum value, bool ignoreCase = false)
2823
// where TEnum : struct, Enum
2924
// {
@@ -133,7 +128,7 @@ public static Parser<char, TEnum> EnumExcept<TEnum>(bool ignoreCase = false, par
133128
public static Parser<char, TEnum> EnumValue<TEnum>(TEnum value, bool ignoreCase = false)
134129
where TEnum : struct, Enum
135130
{
136-
return Parsers.String(value.ToString(), ignoreCase).AsEnum<TEnum>();
131+
return Parsers.String(value.ToString(), ignoreCase).AsEnum<TEnum>(ignoreCase);
137132
}
138133
}
139134
}

src/SimpleStateMachine.StructuralSearch/Rules/IsRule.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace SimpleStateMachine.StructuralSearch.Rules
1+
using System;
2+
using Pidgin;
3+
using SimpleStateMachine.StructuralSearch.Extensions;
4+
5+
namespace SimpleStateMachine.StructuralSearch.Rules
26
{
37
public class IsRule : IRule
48
{
@@ -14,7 +18,15 @@ public IsRule(SubRuleType type, PlaceholderType placeholderType)
1418

1519
public bool Execute(string value)
1620
{
17-
throw new System.NotImplementedException();
21+
return PlaceholderType switch
22+
{
23+
PlaceholderType.Var => CommonParser.Identifier.TryParse(value, out _),
24+
PlaceholderType.Int => int.TryParse(value, out _),
25+
PlaceholderType.Double => double.TryParse(value, out _),
26+
PlaceholderType.DateTime => DateTime.TryParse(value, out _),
27+
PlaceholderType.Guid => Guid.TryParse(value, out _),
28+
_ => throw new ArgumentOutOfRangeException()
29+
};
1830
}
1931
}
2032
}

src/SimpleStateMachine.StructuralSearch/StructuralSearch/RulesParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ static FindRuleParser()
6666

6767
public static readonly Parser<char, IRule> IsSubRule =
6868
Parser.Map((type, param) => new IsRule(type, param),
69-
Parsers.EnumValue(Rules.SubRuleType.Is), PlaceholderType)
69+
Parsers.EnumValue(Rules.SubRuleType.Is, true)
70+
.Trim(),
71+
PlaceholderType)
7072
.As<char, IsRule, IRule>()
7173
.Try();
7274

7375
public static readonly Parser<char, IRule> InSubRule =
7476
Parser.Map((type, param) => new InRule(type, param),
75-
Parsers.EnumValue(Rules.SubRuleType.In), Parameters)
77+
Parsers.EnumValue(Rules.SubRuleType.In, true), Parameters)
7678
.As<char, InRule, IRule>()
7779
.Try();
7880

0 commit comments

Comments
 (0)