Skip to content

Commit d549739

Browse files
committed
with source match
1 parent c720d93 commit d549739

File tree

13 files changed

+331
-75
lines changed

13 files changed

+331
-75
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System.Linq;
2+
3+
namespace SimpleStateMachine.StructuralSearch.Sandbox
4+
{
5+
public static class Constant
6+
{
7+
/// <summary>
8+
/// Parenthesis char: '('
9+
/// </summary>
10+
public static readonly char LeftParenthesis = '(';
11+
12+
/// <summary>
13+
/// Parenthesis char: ')'
14+
/// </summary>
15+
public static readonly char RightParenthesis = ')';
16+
17+
/// <summary>
18+
/// Parenthesis char: '['
19+
/// </summary>
20+
public static readonly char LeftSquareParenthesis = '[';
21+
22+
/// <summary>
23+
/// Parenthesis char: ']'
24+
/// </summary>
25+
public static readonly char RightSquareParenthesis = ']';
26+
27+
/// <summary>
28+
/// Parenthesis char: '{'
29+
/// </summary>
30+
public static readonly char LeftCurlyParenthesis = '{';
31+
32+
/// <summary>
33+
/// Parenthesis char: '}'
34+
/// </summary>
35+
public static readonly char RightCurlyParenthesis = '}';
36+
37+
/// <summary>
38+
/// Char: '$'
39+
/// </summary>
40+
public static readonly char PlaceholderSeparator = '$';
41+
42+
/// <summary>
43+
/// Char: '\r'
44+
/// </summary>
45+
public static readonly char CarriageReturn = '\r';
46+
47+
/// <summary>
48+
/// Char: '\n'
49+
/// </summary>
50+
public static readonly char LineFeed = '\n';
51+
52+
/// <summary>
53+
/// Char: ' '
54+
/// </summary>
55+
public static readonly char Space = ' ';
56+
57+
/// <summary>
58+
/// Parenthesis chars: '(' and ')'
59+
/// </summary>
60+
public static readonly (char, char) Parenthesis = (LeftParenthesis, RightParenthesis);
61+
62+
/// <summary>
63+
/// Parenthesis chars: '[' and ']'
64+
/// </summary>
65+
public static readonly (char, char) SquareParenthesis = (LeftSquareParenthesis, RightSquareParenthesis);
66+
67+
/// <summary>
68+
/// Parenthesis chars: '{ and '}'
69+
/// </summary>
70+
public static readonly (char, char) CurlyParenthesis = (LeftCurlyParenthesis, RightCurlyParenthesis);
71+
72+
/// <summary>
73+
/// Parenthesis chars: '(' and ')', '{ and '}', '{ and '}'
74+
/// </summary>
75+
public static readonly (char, char)[] AllParenthesised = { Parenthesis, SquareParenthesis, CurlyParenthesis };
76+
77+
public static char[] All()
78+
{
79+
var all = new char[]{
80+
LeftParenthesis,
81+
RightParenthesis,
82+
LeftSquareParenthesis,
83+
RightSquareParenthesis,
84+
LeftCurlyParenthesis,
85+
RightCurlyParenthesis,
86+
PlaceholderSeparator,
87+
CarriageReturn,
88+
LineFeed,
89+
Space
90+
};
91+
92+
return all;
93+
}
94+
95+
public static char[] AllExclude(params char[] excluded)
96+
{
97+
return All().Where(x => !excluded.Contains(x)).ToArray();
98+
}
99+
}
100+
}

src/SimpleStateMachine.StructuralSearch.Sandbox/Parsers.cs renamed to src/SimpleStateMachine.StructuralSearch.Sandbox/Custom/Parsers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,5 @@ public static Parser<TToken, R> Series<TToken, T, R>(IEnumerable<Parser<TToken,
5252

5353
return new SeriesParser<TToken, T, R>(parsers, func);
5454
}
55-
5655
}
5756
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Pidgin;
2+
3+
namespace SimpleStateMachine.StructuralSearch.Sandbox.Custom
4+
{
5+
public class PlaceholderParser: Parser<char, SourceMatch>
6+
{
7+
private Parser<char, SourceMatch> _parser;
8+
private string _name;
9+
public PlaceholderParser(string name, Parser<char, SourceMatch> parser)
10+
{
11+
_name = name;
12+
_parser = parser;
13+
}
14+
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds, out SourceMatch result)
15+
{
16+
var isCorrect = _parser.TryParse(ref state, ref expecteds, out result);
17+
18+
return isCorrect;
19+
}
20+
}
21+
}

src/SimpleStateMachine.StructuralSearch.Sandbox/Custom/MapManyParser.cs renamed to src/SimpleStateMachine.StructuralSearch.Sandbox/Custom/SeriesParser.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ public SeriesParser(IEnumerable<Parser<TToken, T>> parsers, Func<IEnumerable<T>,
1515
this.parsers = parsers;
1616
}
1717

18+
// public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expecteds, out R result)
19+
// {
20+
// var results = new List<T>();
21+
// foreach (var parser in parsers)
22+
// {
23+
// if (!parser.TryParse(ref state, ref expecteds, out var _result))
24+
// {
25+
// result = default (R);
26+
// return false;
27+
// }
28+
// Console.WriteLine($"R: {_result}");
29+
// results.Add(_result);
30+
// }
31+
//
32+
// result = _func(results);
33+
// return true;
34+
// }
35+
36+
1837
public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expecteds, out R result)
1938
{
2039
var results = new List<T>();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Sandbox.Extensions
5+
{
6+
public static class EnumerableStringExtensions
7+
{
8+
public static string JoinToString(this IEnumerable<string> enumerable, string separator = null)
9+
{
10+
return string.Join(separator, enumerable);
11+
}
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text;
4+
5+
namespace SimpleStateMachine.StructuralSearch.Sandbox.Extensions
6+
{
7+
public static class IEnumerableSourceMatchExtensions
8+
{
9+
public static SourceMatch Concatenate(this IEnumerable<SourceMatch> matches)
10+
{
11+
int start = matches.First().Start;
12+
int end = matches.Last().End;
13+
var value = string.Join(string.Empty, matches.Select(x => x.Value));
14+
return new SourceMatch(value, start, end);
15+
}
16+
}
17+
}

src/SimpleStateMachine.StructuralSearch.Sandbox/Extensions/ParserExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static Parser<TToken, T> WithDebug<TToken, T>(this Parser<TToken, T> pars
6565
return u;
6666
}, parser, Parser<TToken>.CurrentPos, Parser<TToken>.CurrentSourcePosDelta);
6767
}
68+
6869
public static Parser<TToken, T> BetweenAsThen<TToken, T, U, V>(this Parser<TToken, T> parser,
6970
Parser<TToken, U> parser1,
7071
Parser<TToken, V> parser2, Func<U, T, V, T> func)

src/SimpleStateMachine.StructuralSearch.Sandbox/Extensions/StringParserExtensions.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using Pidgin;
4+
using static Pidgin.Parser<char>;
5+
using static Pidgin.Parser;
46

57
namespace SimpleStateMachine.StructuralSearch.Sandbox.Extensions
68
{
@@ -38,9 +40,16 @@ public static Parser<TToken, IEnumerable<string>> ToMany<TToken>(this Parser<TTo
3840
{
3941
return parser.Select(x => new List<string>() { x }).ToIEnumerable();
4042
}
41-
// public static Parser<TToken, List<string>> ToMany<TToken>(this Parser<TToken, string> parser)
42-
// {
43-
// return parser.Select(x => new List<string>{ x });
44-
// }
43+
44+
public static Parser<TToken, string> JoinToString<TToken>(this Parser<TToken, IEnumerable<string>> parser, string separator = null)
45+
{
46+
separator ??= string.Empty;
47+
return parser.Select(x => string.Join(separator, x));
48+
}
49+
50+
public static Parser<TToken, SourceMatch> AsMatch<TToken>(this Parser<TToken, string> parser)
51+
{
52+
return parser.Then(Parser<TToken>.CurrentOffset, (s, offset) => new SourceMatch(s, offset - s.Length, offset));
53+
}
4554
}
4655
}

src/SimpleStateMachine.StructuralSearch.Sandbox/ParserToParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Pidgin;
2+
using SimpleStateMachine.StructuralSearch.Sandbox.Extensions;
23

34
namespace SimpleStateMachine.StructuralSearch.Sandbox
45
{
@@ -14,5 +15,11 @@ public static Parser<char, Parser<char, string>> Stringc(char token)
1415
var _token = token.ToString();
1516
return Parser.String(_token).Select(x => Parser.String(_token));
1617
}
18+
19+
public static Parser<char, Parser<char, SourceMatch>> StringcMatch(char token)
20+
{
21+
var _token = token.ToString();
22+
return Parser.String(_token).Select(x => Parser.String(_token).AsMatch());
23+
}
1724
}
1825
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SimpleStateMachine.StructuralSearch.Sandbox
2+
{
3+
public class Placeholder
4+
{
5+
public static Placeholder Not => null;
6+
private PlaceholdersMaster _master;
7+
public Placeholder(PlaceholdersMaster master, string name, SourceMatch match)
8+
{
9+
_master = master;
10+
Name = name;
11+
Match = match;
12+
}
13+
14+
public string Name { get; }
15+
public SourceMatch Match { get; }
16+
}
17+
}

0 commit comments

Comments
 (0)