IronAHK /Scripting/Parser/Construct/Expressions/Object.cs

Language C# Lines 119
MD5 Hash 09b9f601d13c618be7cb6697ddb81b34 Estimated Cost $2,005 (why?)
Repository git://github.com/polyethene/IronAHK.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
using System.CodeDom;
using System.Collections.Generic;

namespace IronAHK.Scripting
{
    partial class Parser
    {
        void ParseObject(List<object> parts, out CodePrimitiveExpression[] keys, out CodeExpression[] values)
        {
            var names = new List<CodePrimitiveExpression>();
            var entries = new List<CodeExpression>();

            for (int i = 0; i < parts.Count; i++)
            {
                CodeExpression value = null;

                #region Name

                if (!(parts[i] is string))
                    throw new ParseException(ExUnexpected);

                var name = (string)parts[i];

                if (name.Length > 2 && name[0] == StringBound && name[name.Length - 1] == StringBound)
                    name = name.Substring(1, name.Length - 2);

                if (name.Length == 0 || !IsIdentifier(name))
                    throw new ParseException(ExInvalidVarName);

                i++;
                if (i == parts.Count)
                    goto collect;

                #endregion

                #region Assign

                if (!(parts[i] is string))
                    throw new ParseException(ExUnexpected);

                var assign = (string)parts[i];

                if (assign.Length == 1 && assign[0] == Multicast)
                    goto collect;

                if (!(assign.Length == 1 && (assign[0] == Equal || assign[0] == HotkeyBound)))
                    throw new ParseException(ExUnexpected);

                i++;
                if (i == parts.Count)
                    goto collect;

                #endregion

                #region Value

                var sub = new List<object>();
                int next = Set(parts, i);

                if (next == 0) // no enclosing set (...){...}[...] so scan until next bounary
                {
                    for (next = i; next < parts.Count; next++)
                    {
                        if (parts[next] is string && ((string)parts[next])[0] == Multicast)
                            break;
                    }
                }
                else
                    next++; // set function returns n-1 index

                for (; i < next; i++)
                    sub.Add(parts[i]);
                i--;

                value = ParseExpression(sub);

                i++;
                if (i == parts.Count)
                    goto collect;

                #endregion

                #region Delimiter

                if (!(parts[i] is string))
                    throw new ParseException(ExUnexpected);

                var delim = (string)parts[i];

                if (!(delim.Length == 1 && delim[0] == Multicast))
                    throw new ParseException(ExUnexpected);

                #endregion

                #region Collect

            collect:
                names.Add(new CodePrimitiveExpression(name));
                entries.Add(value ?? new CodePrimitiveExpression(null));

                #endregion
            }

            keys = names.ToArray();
            values = entries.ToArray();
        }

        bool IsJsonObject(object item)
        {
            return item is CodeMethodInvokeExpression && ((CodeMethodInvokeExpression)item).Method.MethodName == InternalMethods.Index.MethodName;
        }

        bool IsArrayExtension(object item)
        {
            return item is CodeMethodInvokeExpression && ((CodeMethodInvokeExpression)item).Method.MethodName == InternalMethods.ExtendArray.MethodName;
        }
    }
}
Back to Top