jungerl /lib/otp.net/Otp/Erlang/Format.cs

Language C# Lines 296
MD5 Hash c3bc404e16a615bcac332c7921f5e99a Estimated Cost $6,064 (why?)
Repository https://github.com/gebi/jungerl.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
/*``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>
*/
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)
        {
            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 len = pos - start;
            return new string(fmt, start, len);
        }

        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;

            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;

            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] == '_') {
                        string s = pvariable(fmt, ref pos);
                        items.Add(new Erlang.Var(s));
                        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);
            if (fmt[pos++] != 'w')
                return State.ERL_FMT_ERR;
            object o = args[argc++];
            Type to = o.GetType();

            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(Erlang.Object))
                items.Add(o as Erlang.Object);
            else if (to == typeof(char))
                items.Add(new Erlang.Char((char)o));
            else
                throw new FormatException(
                    string.Format("Unknown type of argument #{0}: {1}", argc-1, to.ToString()));
            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();
                        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:
                    --pos;
                    char c = fmt[pos];
                    if (char.IsLower(c)) {         /* atom  ? */
                        string s = patom(fmt, ref pos);
                        result = new Erlang.Atom(s);
                    } else if (char.IsUpper(c) || c == '_') {
                        string s = pvariable(fmt, ref pos);
                        result = new Erlang.Var(s);
                    } 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));
                        else
                            result = new Erlang.Double(double.Parse(s));
                    } 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 = new Erlang.Atom(s);
                    }
                    break;
            }

            return result;
        }
    }
}
Back to Top