otp.net /Otp/Erlang/Format.cs

Language C# Lines 406
MD5 Hash fa0adcfca9b91e661d944717520de6b2 Estimated Cost $8,650 (why?)
Repository https://github.com/saleyn/otp.net.git View Raw File View Project SPDX
  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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
* 
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
*
* Copyright (c) 2011 Serge Aleynikov <saleyn@gmail.com>
*/
using System.Globalization;

namespace Otp.Erlang
{
    using System;

    /*
    * Provides a C# representation of Erlang strings. 
    **/
    internal class Formatter
    {
        private enum State {
              ERL_OK                = 0
            , ERL_FMT_ERR           = -1
            , ERL_MAX_ENTRIES       = 255  /* Max entries in a tuple/list term */
            , ERL_MAX_NAME_LENGTH   = 255  /* Max length of variable names */
        };

        private static int skip_null_chars(char[] fmt, int pos) {
            for (char c = fmt[pos]; pos < fmt.Length && (c == ' ' || c == '\t' || c == '\n'); c = fmt[++pos]);
            return pos;
        }

        private static string pvariable(char[] fmt, ref int pos, out TermType type)
        {
            int start = pos;

            for (pos = skip_null_chars(fmt, pos); pos < fmt.Length; pos++)
            {
                char c = fmt[pos];
                if (char.IsLetterOrDigit(c) || (c == '_'))
                    continue;
                else
                    break;
            }
            
            int i = pos;
            int end = pos;

            if (fmt.Length > i + 1 && fmt[i] == ':' && fmt[i + 1] == ':')
            {
                i = pos + 2;
                int tps = i;

                for (char c = fmt[i]; char.IsLetter(c) && i < fmt.Length - 1; c = fmt[++i]) ;

                if (fmt[i] == '(' && i < fmt.Length - 1 && fmt[i + 1] == ')')
                {
                    pos = i + 2;

                    string tp = new string(fmt, tps, i - tps);

                    switch (tp)
                    {
                        case "int":
                        case "integer":
                            type = TermType.Int;
                            break;
                        case "str":
                        case "string":
                            type = TermType.String;
                            break;
                        case "atom":
                            type = TermType.Atom;
                            break;
                        case "float":
                        case "double":
                            type = TermType.Double;
                            break;
                        case "binary":
                            type = TermType.Binary;
                            break;
                        case "bool":
                        case "boolean":
                            type = TermType.Boolean;
                            break;
                        case "byte":
                            type = TermType.Byte;
                            break;
                        case "char":
                            type = TermType.Char;
                            break;
                        case "list":
                            type = TermType.List;
                            break;
                        case "tuple":
                            type = TermType.Tuple;
                            break;
                        case "pid":
                            type = TermType.Pid;
                            break;
                        case "ref":
                        case "reference":
                            type = TermType.Ref;
                            break;
                        case "port":
                            type = TermType.Port;
                            break;
                        default:
                            throw new ArgumentException("Type '" + tps + "' is not supported!");
                    }
                }
                else
                    throw new ArgumentException("Invalid variable type specification: " +
                        new string(fmt, start, pos - start));
            }
            else
                type = TermType.Object;

            int len = end - start;
            return new string(fmt, start, len);
        }

        private static Object createAtom(string s)
        {
            bool is_true = s == "true";
            if (is_true || s == "false")
                return new Erlang.Boolean(is_true);
            return new Erlang.Atom(s);
        }

        private static string patom(char[] fmt, ref int pos)
        {
            int start = pos;

            for (pos = skip_null_chars(fmt, pos); pos < fmt.Length; pos++)
            {
                char c = fmt[pos];
                if (char.IsLetterOrDigit(c) || (c == '_') || (c == '@'))
                    continue;
                else
                    break;
            }
            int len = pos - start;
            return new string(fmt, start, len);
        }

        private static string pdigit(char[] fmt, ref int pos)
        {
            int start = pos;
            bool dotp = false;

            for (pos = skip_null_chars(fmt, pos); pos < fmt.Length; pos++)
            {
                char c = fmt[pos];
                if (char.IsDigit(c) || c == '-')
                    continue;
                else if (!dotp && c == '.')
                {
                    dotp = true;
                    continue;
                }
                else
                    break;
            }
            int len = pos - start;
            return new string(fmt, start, len);
        }

        private static string pstring(char[] fmt, ref int pos)
        {
            int start = ++pos; // skip the first double quote

            for (; pos < fmt.Length; pos++)
            {
                char c = fmt[pos];
                if (c == '"') {
                    if (fmt[pos - 1] == '\\')
                        continue;
                    else
                        break;
                }
            }
            int len = pos++ - start;
            return new string(fmt, start, len);
        }

        private static string pquotedAtom(char[] fmt, ref int pos)
        {
            int start = ++pos; // skip first quote

            for (pos = skip_null_chars(fmt, pos); pos < fmt.Length; pos++)
            {
                char c = fmt[pos];
                if (c == '\'')
                {
                    if (fmt[pos-1] == '\\')
                        continue;
                    else
                        break;
                }
            }
            int len = pos++ - start;
            return new string(fmt, start, len);
        }

        private static State ptuple(char[] fmt, ref int pos,
            ref System.Collections.Generic.List<Erlang.Object> items, ref int argc, params object[] args)
        {
            int start = pos;
            State rc = State.ERL_FMT_ERR;

            if (pos < fmt.Length)
            {
                pos = skip_null_chars(fmt, pos);
                switch (fmt[pos++]) {
                    case '}':
                        rc = State.ERL_OK;
                        break;

                    case ',':
                        rc = ptuple(fmt, ref pos, ref items, ref argc, args);
                        break;

                    default: {
                        --pos;
                        Erlang.Object obj = create(fmt, ref pos, ref argc, args);
                        items.Add(obj);
                        rc = ptuple(fmt, ref pos, ref items, ref argc, args);
                        break;
                    }
                }
            }
            return rc;
        }

        private static State plist(char[] fmt, ref int pos,
            ref System.Collections.Generic.List<Erlang.Object> items, ref int argc, params object[] args)
        {
            State rc = State.ERL_FMT_ERR;

            if (pos < fmt.Length)
            {
                pos = skip_null_chars(fmt, pos);

                switch (fmt[pos++]) {
                    case ']':
                        rc = State.ERL_OK;
                        break;

                    case ',':
                        rc = plist(fmt, ref pos, ref items, ref argc, args);
                        break;

                    case '|':
                        pos = skip_null_chars(fmt, pos);
                        if (char.IsUpper(fmt[pos]) || fmt[pos] == '_') {
                            TermType t;
                            string s = pvariable(fmt, ref pos, out t);
                            items.Add(new Erlang.Var(s, t));
                            pos = skip_null_chars(fmt, pos);
                            if (fmt[pos] == ']')
                                rc = State.ERL_OK;
                            break;
                        }
                        break;

                    default: {
                        --pos;
                        Erlang.Object obj = create(fmt, ref pos, ref argc, args);
                        items.Add(obj);
                        rc = plist(fmt, ref pos, ref items, ref argc, args);
                        break;
                    }

                }
            }
            return rc;
        }

        private static State pformat(char[] fmt, ref int pos,
            ref System.Collections.Generic.List<Erlang.Object> items, ref int argc, params object[] args)
        {
            pos = skip_null_chars(fmt, pos);

            char c = fmt[pos++];

            if (args.Length < argc+1)
                throw new FormatException(string.Format("Missing argument for argument #{0}: ~{1}", argc, c));

            object o = args[argc++];

            switch (c)
            {
                case 'i':
                case 'd': items.Add(new Erlang.Long(o is int ? (int)o : (long)o)); break;
                case 'f': items.Add(new Erlang.Double((double)o));  break;
                case 's': items.Add(new Erlang.String((string)o));  break;
                case 'b': items.Add(new Erlang.Boolean((bool)o));   break;
                case 'c': items.Add(new Erlang.Char((char)o));      break;
                case 'w':
                    Type to = o.GetType();

                    // Native types
                    if (to == typeof(int))
                        items.Add(new Erlang.Long((int)o));
                    else if (to == typeof(long))
                        items.Add(new Erlang.Long((long)o));
                    else if (to == typeof(double))
                        items.Add(new Erlang.Double((double)o));
                    else if (to == typeof(string))
                        items.Add(new Erlang.String((string)o));
                    else if (to == typeof(bool))
                        items.Add(new Erlang.Boolean((bool)o));
                    else if (to == typeof(char))
                        items.Add(new Erlang.Char((char)o));
                    // Erlang terms
                    else if (typeof(Erlang.Object).IsAssignableFrom(o.GetType()))
                        items.Add(o as Erlang.Object);
                    else
                        throw new FormatException(
                            string.Format("Unknown type of argument #{0}: {1}", argc-1, to.ToString()));
                    break;
                default:
                    throw new FormatException(string.Format("Unsupported type ~{0} for argument #{1}", c, argc-1));
            }
            return State.ERL_OK;
        }

        internal static Erlang.Object create(
            char[] fmt, ref int pos, ref int argc, params object[] args)
        {
            var items = new System.Collections.Generic.List<Erlang.Object>();
            Erlang.Object result = null;
            pos = skip_null_chars(fmt, pos);

            switch (fmt[pos++]) {
                case '{':
                    if (State.ERL_OK != ptuple(fmt, ref pos, ref items, ref argc, args))
                        throw new FormatException(
                            string.Format("Error parsing tuple at pos {0}", pos));
                    result = new Erlang.Tuple(items.ToArray());
                    break;

                case '[':
                    if (fmt[pos] == ']') {
                        result = new Erlang.List();
                        pos++;
                        break;
                    }
                    else if (State.ERL_OK == plist(fmt, ref pos, ref items, ref argc, args))
                    {
                        result = new Erlang.List(items.ToArray());
                        break;
                    }
                    throw new FormatException(
                        string.Format("Error parsing list at pos {0}", pos));

                case '$': /* char-value? */
                    result = new Erlang.Char(fmt[pos++]);
                    break;

                case '~':
                    if (State.ERL_OK != pformat(fmt, ref pos, ref items, ref argc, args))
                        throw new FormatException(
                            string.Format("Error parsing term at pos {0}", pos));
                    result = items[0];
                    break;

                default:
                    char c = fmt[--pos];
                    if (char.IsLower(c)) {         /* atom  ? */
                        string s = patom(fmt, ref pos);
                        result = createAtom(s);
                    } else if (char.IsUpper(c) || c == '_') {
                        TermType t;
                        string s = pvariable(fmt, ref pos, out t);
                        result = new Erlang.Var(s, t);
                    } else if (char.IsDigit(c) || c == '-') {    /* integer/float ? */
                        string s = pdigit(fmt, ref pos);
                        if (s.IndexOf('.') < 0)
                            result = new Erlang.Long(long.Parse(s, CultureInfo.InvariantCulture));
                        else
                            result = new Erlang.Double(double.Parse(s, CultureInfo.InvariantCulture));
                    } else if (c == '"') {      /* string ? */
                        string s = pstring(fmt, ref pos);
                        result = new Erlang.String(s);
                    } else if (c == '\'') {     /* quoted atom ? */
                        string s = pquotedAtom(fmt, ref pos);
                        result = createAtom(s);
                    }
                    break;
            }

            return result;
        }
    }
}
Back to Top