Skip to content

Commit 00ab0e3

Browse files
committed
push
1 parent 7ab8b74 commit 00ab0e3

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class PlaceholderParserTests
1212
[InlineData("($test$)", "(value (test) )", "value (test) ")]
1313
[InlineData("$var$;", "test;;", "test")]
1414
[InlineData("$var$;.", "test;;;.", "test;;")]
15+
[InlineData("$value2$(123)", "temp1(123)", "temp1")]
1516
[InlineData("$value2$", "temp1 ?? temp2", "temp1 ?? temp2")]
1617
[InlineData("$value2$;", "temp1 ?? temp2;", "temp1 ?? temp2")]
1718
public static void PlaceholderParsingShouldBeSuccess(string template, string source, string expectedResult)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Pidgin;
2+
using SimpleStateMachine.StructuralSearch.CustomParsers;
3+
using SimpleStateMachine.StructuralSearch.Extensions;
4+
using Xunit;
5+
6+
namespace SimpleStateMachine.StructuralSearch.Tests;
7+
8+
public static class Tests
9+
{
10+
[Theory]
11+
[InlineData("temp1 ?? temp2;", ";", "temp1 ?? temp2")]
12+
[InlineData("(value )", null, "value ")]
13+
[InlineData("(value )", null, "value")]
14+
[InlineData("(value (test))", null, "value (test)")]
15+
[InlineData("(value (test) )", null, "value (test) ")]
16+
[InlineData("test;;", ";", "test;")]
17+
[InlineData("test;;;.", ".", "test;;")]
18+
[InlineData( "temp1(123)", null,"temp1")]
19+
[InlineData("temp1 ?? temp2", null, "temp1 ?? temp2")]
20+
public static void PlaceholderParsingShouldBeSuccess(string input, string? terminator, string expectedResult)
21+
{
22+
var terminatorParser = terminator is null ? Parser.EndOfLine : Parser.String(terminator);
23+
var result = Parser.AnyCharExcept().AtLeastOnceAsStringUntil(terminatorParser).ParseOrThrow(expectedResult);
24+
Assert.Equal(expectedResult, result);
25+
}
26+
27+
[Theory]
28+
[InlineData("temp1;", ";", "temp1 ?? temp2")]
29+
public static void PlaceholderParsingShouldBeSuccess2(string input, string terminator, string expectedResult)
30+
{
31+
var terminatorParser = Parser.String(terminator).Try().Select(x => Unit.Value);
32+
var result = PlaceholderParser.CreateParser(terminatorParser).ParseToEnd(input);
33+
Assert.Equal(expectedResult, result);
34+
}
35+
}

src/SimpleStateMachine.StructuralSearch/CustomParsers/FindTemplateParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace SimpleStateMachine.StructuralSearch.CustomParsers;
77

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

@@ -31,7 +31,7 @@ private void InitializeLookaheadParsers()
3131
}
3232
}
3333

34-
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected, out IEnumerable<string> result)
34+
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected, out List<string> result)
3535
{
3636
var results = new List<string>();
3737
var count = _parsers.Count;

src/SimpleStateMachine.StructuralSearch/CustomParsers/PlaceholderParser.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ 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)
13+
internal static readonly IReadOnlySet<char> InvalidStringLiteralChars = new HashSet<char>(Constant.AllParenthesis)
1414
{
1515
Constant.CarriageReturn,
1616
Constant.LineFeed,
1717
Constant.Space
1818
};
1919

20-
private static readonly Parser<char, char> StringLiteralChar = Parser.AnyCharExcept(InvalidStringLiteralChars);
20+
internal static readonly Parser<char, char> StringLiteralChar = Parser.AnyCharExcept(InvalidStringLiteralChars);
2121

2222
private readonly string _name;
2323
private IParsingContext? _context;
@@ -62,13 +62,46 @@ protected override Parser<char, string> BuildParser(Func<Parser<char, string>?>
6262
}).Try());
6363
}
6464

65-
var anyString = StringLiteralChar.AtLeastOnceAsStringUntil(lookahead);
66-
var simpleString = StringLiteralChar.AtLeastOnceString();
65+
return CreateParser(lookahead);
66+
67+
68+
//parenthesised and tokens and whiteSpaces
69+
// var prdsAndTokens = Parser.OneOf(parenthesised, token)
70+
// .AtLeastOnceUntil(lookahead)
71+
// .JoinToString()
72+
// .Try();
73+
74+
// var parser = prdsAndTokens.Or(anyString);
75+
}
76+
77+
// internal static Parser<char, string> CreateParser(Parser<char, Unit> terminator)
78+
// {
79+
// var simpleString = StringLiteralChar.AtLeastOnceAsStringUntil(terminator).Select(x => x);
80+
// var whitespaces = Parser.OneOf(Constant.WhitespaceChars).AtLeastOnceAsStringUntil(terminator).Select(x => x);
81+
// var token = Parser.OneOf(simpleString.Try(), whitespaces).Select(x => x);
82+
//
83+
// Parser<char, string>? parser = null;
84+
//
85+
// // var parserBetweenParentheses = Parser.Rec(() => parser ?? throw new ArgumentNullException(nameof(parser)))
86+
// // .BetweenAnyParentheses((c1, s, c2) => $"{c1}{s}{c2}");
87+
//
88+
// // parser = Parser.OneOf(token, parserBetweenParentheses).AtLeastOnceUntil(terminator).JoinToString();
89+
// parser = token.Try().Many().JoinToString();
90+
// return parser;
91+
// }
92+
93+
internal static Parser<char, string> CreateParser(Parser<char, Unit> terminator)
94+
{
95+
var anyString = InvalidStringLiteralChars.AnyCharWithPlshd
96+
.AtLeastOnceAsStringUntil(lookahead);
97+
98+
var simpleString = CommonTemplateParser.StringWithPlshd;
6799
var token = Parser.OneOf(simpleString, Grammar.WhiteSpaces).Try();
68100
Parser<char, string>? term = null;
69101

70-
var parenthesised = Parser.Rec(() => term ?? throw new ArgumentNullException(nameof(term)))
71-
.BetweenAnyParentheses((c1, s, c2) => $"{c1}{s}{c2}");
102+
var parenthesised = Parsers.BetweenOneOfChars(x => Parser.Char(x).AsString(),
103+
expr: Parser.Rec(() => term ?? throw new ArgumentNullException(nameof(term))),
104+
Constant.AllParenthesised).JoinToString();
72105

73106
term = Parser.OneOf(parenthesised, token).Many().JoinToString();
74107

@@ -81,7 +114,7 @@ protected override Parser<char, string> BuildParser(Func<Parser<char, string>?>
81114
var parser = prdsAndTokens.Or(anyString);
82115
return parser;
83116
}
84-
117+
85118
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
86119
out string result)
87120
{

src/SimpleStateMachine.StructuralSearch/StructuralSearch/StructuralSearch.cs

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

0 commit comments

Comments
 (0)