Skip to content

Commit 4780118

Browse files
committed
Support IsApplicableForPlaceholder
1 parent 6db14f0 commit 4780118

20 files changed

+68
-10
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/StructuralSearchTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void StructuralSearchShouldBeSuccess(string inputText, string temp
4242

4343
[Theory]
4444
[InlineData("ExamplesInput/Methods.cs")]
45-
public static void StructuralSearchShouldBeSuccess2(string filePath)
45+
public static void StructuralSearchFileParsingShouldBeSuccess(string filePath)
4646
{
4747
var configuration = new Configuration
4848
{

src/SimpleStateMachine.StructuralSearch/CustomParsers/PlaceholderParser.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ namespace SimpleStateMachine.StructuralSearch.CustomParsers;
1010

1111
internal class PlaceholderParser : ParserWithLookahead<char, string>, IContextDependent
1212
{
13+
private static readonly IReadOnlySet<char> InvalidStringLiteralChars = new HashSet<char>(Constant.AllParenthesis)
14+
{
15+
Constant.CarriageReturn,
16+
Constant.LineFeed,
17+
Constant.Space
18+
};
19+
20+
internal static readonly Parser<char, char> StringLiteralChar = Parser.AnyCharExcept(Constant.InvalidStringLiteralChars);
21+
1322
private readonly string _name;
1423
private IParsingContext? _context;
1524

@@ -53,10 +62,8 @@ protected override Parser<char, string> BuildParser(Func<Parser<char, string>?>
5362
}).Try());
5463
}
5564

56-
var anyString = Parser.AnyCharExcept(Constant.InvalidStringLiteralChars)
57-
.AtLeastOnceAsStringUntil(lookahead);
58-
59-
var simpleString = Grammar.StringLiteral;
65+
var anyString = StringLiteralChar.AtLeastOnceAsStringUntil(lookahead);
66+
var simpleString = StringLiteralChar.AtLeastOnceString();
6067
var token = Parser.OneOf(simpleString, Grammar.WhiteSpaces).Try();
6168
Parser<char, string>? term = null;
6269

@@ -100,6 +107,7 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
100107
Context[_name] = placeholderObj;
101108

102109
res = Context.FindRules
110+
.Where(r => r.IsApplicableForPlaceholder(_name))
103111
.All(r => r.Execute(ref _context!));
104112

105113
if (!res)

src/SimpleStateMachine.StructuralSearch/Operator/Logical/BinaryOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public BinaryOperation(ILogicalOperation left, LogicalBinaryOperator type, ILogi
1717
_right = right;
1818
}
1919

20+
public bool IsApplicableForPlaceholder(string placeholderName)
21+
=> _left.IsApplicableForPlaceholder(placeholderName) || _right.IsApplicableForPlaceholder(placeholderName);
22+
2023
public bool Execute(ref IParsingContext context)
2124
{
2225
var left = _left.Execute(ref context);

src/SimpleStateMachine.StructuralSearch/Operator/Logical/EmptyLogicalOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace SimpleStateMachine.StructuralSearch.Operator.Logical;
44

55
public class EmptyLogicalOperation : ILogicalOperation
66
{
7+
public bool IsApplicableForPlaceholder(string placeholderName)
8+
=> false;
9+
710
public bool Execute(ref IParsingContext context)
811
=> true;
912

src/SimpleStateMachine.StructuralSearch/Operator/Logical/ILogicalOperation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace SimpleStateMachine.StructuralSearch.Operator.Logical;
44

55
public interface ILogicalOperation
66
{
7+
bool IsApplicableForPlaceholder(string placeholderName);
78
bool Execute(ref IParsingContext context);
89
}

src/SimpleStateMachine.StructuralSearch/Operator/Logical/InOperation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using SimpleStateMachine.StructuralSearch.Context;
34
using SimpleStateMachine.StructuralSearch.Parameters;
45

@@ -15,6 +16,9 @@ public InOperation(IParameter parameter, List<IParameter> arguments)
1516
_arguments = arguments;
1617
}
1718

19+
public bool IsApplicableForPlaceholder(string placeholderName)
20+
=> _parameter.IsApplicableForPlaceholder(placeholderName) || _arguments.Any(a => a.IsApplicableForPlaceholder(placeholderName));
21+
1822
public bool Execute(ref IParsingContext context)
1923
{
2024
var parameter = _parameter.GetValue(ref context);

src/SimpleStateMachine.StructuralSearch/Operator/Logical/IsOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public IsOperation(IParameter parameter, ParameterType type)
1919
_type = type;
2020
}
2121

22+
public bool IsApplicableForPlaceholder(string placeholderName)
23+
=> _parameter.IsApplicableForPlaceholder(placeholderName);
24+
2225
public bool Execute(ref IParsingContext context)
2326
{
2427
var value = _parameter.GetValue(ref context);

src/SimpleStateMachine.StructuralSearch/Operator/Logical/MatchOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public MatchOperation(IParameter stringParameter, string regex)
1515
_regex = regex;
1616
}
1717

18+
public bool IsApplicableForPlaceholder(string placeholderName)
19+
=> _stringParameter.IsApplicableForPlaceholder(placeholderName);
20+
1821
public bool Execute(ref IParsingContext context)
1922
{
2023
var value = _stringParameter.GetValue(ref context);

src/SimpleStateMachine.StructuralSearch/Operator/Logical/NotOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public NotOperation(ILogicalOperation parameter)
1111
_parameter = parameter;
1212
}
1313

14+
public bool IsApplicableForPlaceholder(string placeholderName)
15+
=> _parameter.IsApplicableForPlaceholder(placeholderName);
16+
1417
public bool Execute(ref IParsingContext context)
1518
{
1619
var value = _parameter.Execute(ref context);

src/SimpleStateMachine.StructuralSearch/Operator/Logical/StringCompareOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public StringCompareOperation(IParameter left, StringCompareOperator @operator,
1919
_right = right;
2020
}
2121

22+
public bool IsApplicableForPlaceholder(string placeholderName)
23+
=> _left.IsApplicableForPlaceholder(placeholderName) || _right.IsApplicableForPlaceholder(placeholderName);
24+
2225
public bool Execute(ref IParsingContext context)
2326
{
2427
var left = _left.GetValue(ref context);

0 commit comments

Comments
 (0)