@ysharp_design/

Parsing Lisp S-Expressions with known schema in C#

C#

No description

fork
loading
Files
  • main.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
	Cf.
	
	http://stackoverflow.com/questions/3051254/parsing-lisp-s-expressions-with-known-schema-in-c-sharp
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

public delegate object Acceptor(Token token, string match);

public class Symbol
{
    public Symbol(string id) { Id = id ?? Guid.NewGuid().ToString("P"); }
    public override string ToString() => Id;
    public string Id { get; private set; }
}

public class Token : Symbol
{
    internal Token(string id) : base(id) { }
    public Token(string pattern, Acceptor acceptor) : base(pattern) { Regex = new Regex(string.Format("^({0})", !string.IsNullOrEmpty(Pattern = pattern) ? Pattern : ".*"), RegexOptions.Compiled); ValueOf = acceptor; }
    public string Pattern { get; private set; }
    public Regex Regex { get; private set; }
    public Acceptor ValueOf { get; private set; }
}

public class SExpressionSyntax
{
    private readonly Token Space = Token("\\s+", Echo);
    private readonly Token Open = Token("\\(", Echo);
    private readonly Token Close = Token("\\)", Echo);
    private readonly Token Quote = Token("\\'", Echo);
    private Token comment;

    private static Exception Error(string message, params object[] arguments) => new Exception(string.Format(message, arguments));

    private static object Echo(Token token, string match) => new Token(token.Id);

    private static object Quoting(Token token, string match) => NewSymbol(token, match);

    private Tuple<Token, string, object> Read(ref string input)
    {
        if (!string.IsNullOrEmpty(input))
        {
            var found = null as Match;
            var sofar = input;
            var tuple = Lexicon.FirstOrDefault(current => (found = current.Item2.Regex.Match(sofar)).Success && (found.Length > 0));
            var token = tuple != null ? tuple.Item2 : null;
            var match = token != null ? found.Value : null;
            input = match != null ? input.Substring(match.Length) : input;
            return token != null ? Tuple.Create(token, match, token.ValueOf(token, match)) : null;
        }
        return null;
    }

    private Tuple<Token, string, object> Next(ref string input)
    {
        Tuple<Token, string, object> read;
        while (((read = Read(ref input)) != null) && ((read.Item1 == Comment) || (read.Item1 == Space))) ;
        return read;
    }

    public object Parse(ref string input, Tuple<Token, string, object> next)
    {
        var value = null as object;
        if (next != null)
        {
            var token = next.Item1;
            if (token == Open)
            {
                var list = new List<object>();
                while (((next = Next(ref input)) != null) && (next.Item1 != Close))
                {
                    list.Add(Parse(ref input, next));
                }
                if (next == null)
                {
                    throw Error("unexpected EOF");
                }
                value = list.ToArray();
            }
            else if (token == Quote)
            {
                var quote = next.Item3;
                next = Next(ref input);
                value = new[] { quote, Parse(ref input, next) };
            }
            else
            {
                value = next.Item3;
            }
        }
        else
        {
            throw Error("unexpected EOF");
        }
        return value;
    }

    protected Token TokenOf(Acceptor acceptor)
    {
        var found = Lexicon.FirstOrDefault(pair => pair.Item2.ValueOf == acceptor);
        var token = found != null ? found.Item2 : null;
        if ((token == null) && (acceptor != Commenting))
        {
            throw Error("missing required token definition: {0}", acceptor.Method.Name);
        }
        return token;
    }

    protected IList<Tuple<string, Token>> Lexicon { get; private set; }

    protected Token Comment { get { return comment = comment ?? TokenOf(Commenting); } }

    public static Token Token(string pattern, Acceptor acceptor) => new Token(pattern, acceptor);

    public static object Commenting(Token token, string match) => Echo(token, match);

    public static object NewSymbol(Token token, string match) => new Symbol(match);

    public static Symbol Symbol(object value) => value as Symbol;

    public static string Moniker(object value) => Symbol(value) != null ? Symbol(value).Id : null;

    public static string ToString(object value)
    {
        return
            value is object[] ?
            (
                ((object[])value).Length > 0 ?
                ((object[])value).Aggregate(new StringBuilder("("), (result, obj) => result.AppendFormat(" {0}", ToString(obj))).Append(" )").ToString()
                :
                "( )"
            )
            :
            (value != null ? (value is string ? string.Concat('"', (string)value, '"') : (value is bool ? value.ToString().ToLower() : value.ToString())).Replace("\\\r\n", "\r\n").Replace("\\\n", "\n").Replace("\\t", "\t").Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\\"", "\"") : null) ?? "(null)";
    }

    public SExpressionSyntax()
    {
        Lexicon = new List<Tuple<string, Token>>();
        Include(Space, Open, Close, Quote);
    }

    public SExpressionSyntax Include(params Token[] tokens)
    {
        foreach (var token in tokens)
        {
            Lexicon.Add(new Tuple<string, Token>(token.Id, token));
        }
        return this;
    }

    public object Parse(string input)
    {
        var next = Next(ref input);
        var value = Parse(ref input, next);
        if ((next = Next(ref input)) != null)
        {
            throw Error("unexpected ", next.Item1);
        }
        return value;
    }
}

public class CustomSExpressionSyntax : SExpressionSyntax
{
    public CustomSExpressionSyntax()
        : base()
    {
        Include
        (
            // "//" comments
            Token("\\/\\/.*", SExpressionSyntax.Commenting),

            // Obvious
            Token("false", (token, match) => false),
            Token("true", (token, match) => true),
            Token("null", (token, match) => null),
            Token("\\-?[0-9]+\\.[0-9]+", (token, match) => double.Parse(match)),
            Token("\\-?[0-9]+", (token, match) => int.Parse(match)),

            // String literals
            Token("\\\"(\\\\\\n|\\\\t|\\\\n|\\\\r|\\\\\\\"|[^\\\"])*\\\"", (token, match) => match.Substring(1, match.Length - 2)),

            // Identifiers
            Token("[_A-Za-z][_0-9A-Za-z]*", NewSymbol)
        );
    }
}

public class Node { }

public class HearPerceptorState : Node
{
    public string Ident { get; set; }
    public double Value { get; set; }
}

public class HingeJointState : Node
{
    public string Ident { get; set; }
    public double Value { get; set; }
}

public class Polar : Tuple<double, double, double>
{
    public Polar(double a, double b, double c) : base(a, b, c) { }
}

public class ForceResistancePerceptorState : Node
{
    public string Ident { get; set; }
    public Polar Polar { get; set; }
}

public class Test
{
    public static void Main()
    {
        var input = @"
            (
                (Hear 12.3 HelloWorld)
                (HJ LAJ1 -0.42)
                (FRP lf (pos 2.3 1.7 0.4))
            )
        ";

        // visit DRY helpers
        Func<object, object[]> asRecord = value => (object[])value;
        Func<object, Symbol> symbol = value => SExpressionSyntax.Symbol(value);
        Func<object, string> identifier = value => symbol(value).Id;

        // the SExpr visit, proper
        Func<object[], Node[]> visitAll = null;
        Func<object[], Node> visitHear = null;
        Func<object[], Node> visitHJ = null;
        Func<object[], Node> visitFRP = null;

        visitAll =
            all =>
                all.
                Select
                (
                    item =>
                        symbol(asRecord(item)[0]).Id != "Hear" ?
                        (
                            symbol(asRecord(item)[0]).Id != "HJ" ?
                            visitFRP(asRecord(item))
                            :
                            visitHJ(asRecord(item))
                        )
                        :
                        visitHear(asRecord(item))
                ).
                ToArray();

        visitHear =
            item =>
                new HearPerceptorState { Value = (double)asRecord(item)[1], Ident = identifier(asRecord(item)[2]) };

        visitHJ =
            item =>
                new HingeJointState { Ident = identifier(asRecord(item)[1]), Value = (double)asRecord(item)[2] };

        visitFRP =
            item =>
                new ForceResistancePerceptorState
                {
                    Ident = identifier(asRecord(item)[1]),
                    Polar =
                        new Polar
                        (
                            (double)asRecord(asRecord(item)[2])[1],
                            (double)asRecord(asRecord(item)[2])[2],
                            (double)asRecord(asRecord(item)[2])[3]
                        )
                };

        var syntax = new CustomSExpressionSyntax();

        var sexpr = syntax.Parse(input);

        var nodes = visitAll(asRecord(sexpr));

        Console.WriteLine("SO_3051254");
        Console.WriteLine();
        Console.WriteLine(nodes.Length == 3);
        Console.WriteLine(nodes[0] is HearPerceptorState);
        Console.WriteLine(nodes[1] is HingeJointState);
        Console.WriteLine(nodes[2] is ForceResistancePerceptorState);
    }
}