Example #1
0
        public Parser(RubyScope scope, MutableString source, Hash options)
        {
            InitializeLibrary(scope);

            _json = new ParserEngineState(this, scope, source);
            if (options.Count > 0) {
                if (options.ContainsKey(_maxNesting)) {
                    _json.MaxNesting = options[_maxNesting] is int ? (int)options[_maxNesting] : 0;
                }
                _json.AllowNaN = options.ContainsKey(_allowNan) ? (bool)options[_allowNan] : ParserEngineState.DEFAULT_ALLOW_NAN;
                // TODO: check needed, create_id could be TrueClass, FalseClass, NilClass or String
                _json.CreateID = options.ContainsKey(_createAdditions) && (bool)options[_createAdditions] ? Helpers.GetCreateId(scope) : null;
            }
        }
Example #2
0
        public Parser(RubyScope scope, MutableString source, Hash options)
        {
            InitializeLibrary(scope);

            _json = new ParserEngineState(this, scope, source);
            if (options.Count > 0)
            {
                if (options.ContainsKey(_maxNesting))
                {
                    _json.MaxNesting = options[_maxNesting] is int?(int)options[_maxNesting] : 0;
                }
                _json.AllowNaN = options.ContainsKey(_allowNan) ? (bool)options[_allowNan] : ParserEngineState.DEFAULT_ALLOW_NAN;
                // TODO: check needed, create_id could be TrueClass, FalseClass, NilClass or String
                _json.CreateID = options.ContainsKey(_createAdditions) && (bool)options[_createAdditions] ? Helpers.GetCreateId(scope) : null;
            }
        }
Example #3
0
        static int? JSON_parse_object(ParserEngineState json, ref int p, ref int pe, ref Object result)
        {
            int cs = EVIL;
            var source = json.Source;
            Object last_name = null;

            if (json.MaxNesting > 0 && json.CurrentNesting > json.MaxNesting) {
                Helpers.ThrowNestingException("nesting of {0:d} is to deep", json.CurrentNesting);
            }

            result = new Hash(json.Context);

            #region ** ragel generated code **

            {
                cs = JSON_object_start;
            }

            {
                sbyte _klen;
                sbyte _trans;
                sbyte _acts;
                sbyte _nacts;
                sbyte _keys;

                if (p == pe)
                    goto _test_eof;
                if (cs == 0)
                    goto _out;
            _resume:
                _keys = _JSON_object_key_offsets[cs];
                _trans = (sbyte)_JSON_object_index_offsets[cs];

                _klen = _JSON_object_single_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + _klen - 1);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + ((_upper - _lower) >> 1));
                        if (source[p] < _JSON_object_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 1);
                        else if (source[p] > _JSON_object_trans_keys[_mid])
                            _lower = (sbyte)(_mid + 1);
                        else {
                            _trans += (sbyte)(_mid - _keys);
                            goto _match;
                        }
                    }
                    _keys += (sbyte)_klen;
                    _trans += (sbyte)_klen;
                }

                _klen = _JSON_object_range_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + (_klen << 1) - 2);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + (((_upper - _lower) >> 1) & ~1));
                        if (source[p] < _JSON_object_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 2);
                        else if (source[p] > _JSON_object_trans_keys[_mid + 1])
                            _lower = (sbyte)(_mid + 2);
                        else {
                            _trans += (sbyte)((_mid - _keys) >> 1);
                            goto _match;
                        }
                    }
                    _trans += (sbyte)_klen;
                }

            _match:
                _trans = (sbyte)_JSON_object_indicies[_trans];
                cs = _JSON_object_trans_targs[_trans];

                if (_JSON_object_trans_actions[_trans] == 0)
                    goto _again;

                _acts = _JSON_object_trans_actions[_trans];
                _nacts = _JSON_object_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_object_actions[_acts++]) {
                        case 0: {
                                Object v = null;
                                int? np = JSON_parse_value(json, ref p, ref pe, ref v);
                                if (!np.HasValue) {
                                    p--; { p++; if (true) goto _out; }
                                }
                                else {
                                    (result as Hash).Add(last_name, v); { p = ((np.Value)) - 1; }
                                }
                            }
                            break;
                        case 1: {
                                int? np = JSON_parse_string(json, ref p, ref pe, ref last_name);
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        case 2: { p--; { p++; if (true) goto _out; } }
                            break;
                        default: break;
                    }
                }

            _again:
                if (cs == 0)
                    goto _out;
                if (++p != pe)
                    goto _resume;
            _test_eof: { }
            _out: { }
            }

            #endregion

            if (cs >= JSON_object_first_final) {
                if (json.CreateID != null && Helpers.IsJsonClass(result as Hash)) {
                    Object classNameT = (result as Hash)[json.CreateID];
                    if (classNameT != null) {
                        RubyModule classClass;
                        String className = (classNameT as MutableString).ToString();
                        if (!json.Context.TryGetModule(json.Scope.GlobalScope, className, out classClass)) {
                            throw RubyExceptions.CreateArgumentError(String.Format("can't find const {0}", className));
                        }
                        if (json.Context.RespondTo(classClass, "json_creatable?")) {
                            bool creatable = (bool) _jsonCreatableCallSite.Target(_jsonCreatableCallSite, json.Context, classClass);
                            if (creatable) {
                                result = _jsonCreateCallSite.Target(_jsonCreateCallSite, json.Context, classClass, result);
                            }
                        }
                    }
                }

                return p + 1;
            }
            else {
                return null;
            }
        }
Example #4
0
        static int? JSON_parse_integer(ParserEngineState json, ref int p, ref int pe, ref Object result)
        {
            int cs = EVIL;

            #region ** ragel generated code **
            {
                cs = JSON_integer_start;
            }
            #endregion

            json.Memo = p;
            var source = json.Source;

            #region ** ragel generated code **
            {
                sbyte _klen;
                sbyte _trans;
                sbyte _acts;
                sbyte _nacts;
                sbyte _keys;

                if (p == pe)
                    goto _test_eof;
                if (cs == 0)
                    goto _out;
            _resume:
                _keys = _JSON_integer_key_offsets[cs];
                _trans = (sbyte)_JSON_integer_index_offsets[cs];

                _klen = _JSON_integer_single_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + _klen - 1);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + ((_upper - _lower) >> 1));
                        if (source[p] < _JSON_integer_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 1);
                        else if (source[p] > _JSON_integer_trans_keys[_mid])
                            _lower = (sbyte)(_mid + 1);
                        else {
                            _trans += (sbyte)(_mid - _keys);
                            goto _match;
                        }
                    }
                    _keys += (sbyte)_klen;
                    _trans += (sbyte)_klen;
                }

                _klen = _JSON_integer_range_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + (_klen << 1) - 2);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + (((_upper - _lower) >> 1) & ~1));
                        if (source[p] < _JSON_integer_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 2);
                        else if (source[p] > _JSON_integer_trans_keys[_mid + 1])
                            _lower = (sbyte)(_mid + 2);
                        else {
                            _trans += (sbyte)((_mid - _keys) >> 1);
                            goto _match;
                        }
                    }
                    _trans += (sbyte)_klen;
                }

            _match:
                _trans = (sbyte)_JSON_integer_indicies[_trans];
                cs = _JSON_integer_trans_targs[_trans];

                if (_JSON_integer_trans_actions[_trans] == 0)
                    goto _again;

                _acts = _JSON_integer_trans_actions[_trans];
                _nacts = _JSON_integer_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_integer_actions[_acts++]) {
                        case 0: { p--; { p++; if (true) goto _out; } }
                            break;
                        default: break;
                    }
                }

            _again:
                if (cs == 0)
                    goto _out;
                if (++p != pe)
                    goto _resume;
            _test_eof: { }
            _out: { }
            }

            #endregion

            if (cs >= JSON_integer_first_final) {
                int len = p - json.Memo;
                result = Helpers.ToInteger(source.Substring(json.Memo, len));
                return p + 1;
            }
            else {
                return null;
            }
        }
Example #5
0
        static int? JSON_parse_array(ParserEngineState json, ref int p, ref int pe, ref Object result)
        {
            int cs = EVIL;
            var source = json.Source;

            if (json.MaxNesting > 0 && json.CurrentNesting > json.MaxNesting) {
                Helpers.ThrowNestingException("nesting of {0:d} is to deep", json.CurrentNesting);
            }
            result = new RubyArray();

            #region ** ragel generated code **

            {
                cs = JSON_array_start;
            }

            {
                sbyte _klen;
                sbyte _trans;
                sbyte _acts;
                sbyte _nacts;
                sbyte _keys;

                if (p == pe)
                    goto _test_eof;
                if (cs == 0)
                    goto _out;
            _resume:
                _keys = _JSON_array_key_offsets[cs];
                _trans = (sbyte)_JSON_array_index_offsets[cs];

                _klen = _JSON_array_single_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + _klen - 1);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + ((_upper - _lower) >> 1));
                        if (source[p] < _JSON_array_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 1);
                        else if (source[p] > _JSON_array_trans_keys[_mid])
                            _lower = (sbyte)(_mid + 1);
                        else {
                            _trans += (sbyte)(_mid - _keys);
                            goto _match;
                        }
                    }
                    _keys += (sbyte)_klen;
                    _trans += (sbyte)_klen;
                }

                _klen = _JSON_array_range_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + (_klen << 1) - 2);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + (((_upper - _lower) >> 1) & ~1));
                        if (source[p] < _JSON_array_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 2);
                        else if (source[p] > _JSON_array_trans_keys[_mid + 1])
                            _lower = (sbyte)(_mid + 2);
                        else {
                            _trans += (sbyte)((_mid - _keys) >> 1);
                            goto _match;
                        }
                    }
                    _trans += (sbyte)_klen;
                }

            _match:
                _trans = (sbyte)_JSON_array_indicies[_trans];
                cs = _JSON_array_trans_targs[_trans];

                if (_JSON_array_trans_actions[_trans] == 0)
                    goto _again;

                _acts = _JSON_array_trans_actions[_trans];
                _nacts = _JSON_array_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_array_actions[_acts++]) {
                        case 0: {
                                Object v = null;
                                int? np = JSON_parse_value(json, ref p, ref pe, ref v);
                                if (!np.HasValue) {
                                    p--; { p++; if (true) goto _out; }
                                }
                                else {
                                    (result as IList<Object>).Add(v); { p = ((np.Value)) - 1; }
                                }
                            }
                            break;
                        case 1: { p--; { p++; if (true) goto _out; } }
                            break;
                        default: break;
                    }
                }

            _again:
                if (cs == 0)
                    goto _out;
                if (++p != pe)
                    goto _resume;
            _test_eof: { }
            _out: { }
            }

            #endregion

            if (cs >= JSON_array_first_final) {
                return p + 1;
            }
            else {
                Helpers.ThrowParserException("unexpected token at '{0}'", Helpers.GetMessageForException(source, p, pe));
            }
            return null;
        }
Example #6
0
        public static Object Parse(ParserEngineState json)
        {
            int p, pe;
            Object result = null;
            int cs = EVIL;

            #region ** ragel generated code **

            {
                cs = JSON_start;
            }

            #endregion

            var source = json.OriginalSource.ToString();
            p = 0;
            pe = p + json.Length;

            #region ** ragel generated code **

            {
                sbyte _klen;
                sbyte _trans;
                sbyte _acts;
                sbyte _nacts;
                sbyte _keys;

                if (p == pe)
                    goto _test_eof;
                if (cs == 0)
                    goto _out;
            _resume:
                _keys = _JSON_key_offsets[cs];
                _trans = (sbyte)_JSON_index_offsets[cs];

                _klen = _JSON_single_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + _klen - 1);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + ((_upper - _lower) >> 1));
                        if (source[p] < _JSON_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 1);
                        else if (source[p] > _JSON_trans_keys[_mid])
                            _lower = (sbyte)(_mid + 1);
                        else {
                            _trans += (sbyte)(_mid - _keys);
                            goto _match;
                        }
                    }
                    _keys += (sbyte)_klen;
                    _trans += (sbyte)_klen;
                }

                _klen = _JSON_range_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + (_klen << 1) - 2);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + (((_upper - _lower) >> 1) & ~1));
                        if (source[p] < _JSON_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 2);
                        else if (source[p] > _JSON_trans_keys[_mid + 1])
                            _lower = (sbyte)(_mid + 2);
                        else {
                            _trans += (sbyte)((_mid - _keys) >> 1);
                            goto _match;
                        }
                    }
                    _trans += (sbyte)_klen;
                }

            _match:
                _trans = (sbyte)_JSON_indicies[_trans];
                cs = _JSON_trans_targs[_trans];

                if (_JSON_trans_actions[_trans] == 0)
                    goto _again;

                _acts = _JSON_trans_actions[_trans];
                _nacts = _JSON_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_actions[_acts++]) {
                        case 0: {
                                int? np;
                                json.CurrentNesting = 1;
                                np = JSON_parse_object(json, ref p, ref pe, ref result);
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        case 1: {
                                int? np;
                                json.CurrentNesting = 1;
                                np = JSON_parse_array(json, ref p, ref pe, ref result);
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        default: break;
                    }
                }

            _again:
                if (cs == 0)
                    goto _out;
                if (++p != pe)
                    goto _resume;
            _test_eof: { }
            _out: { }
            }

            #endregion

            if (cs >= JSON_first_final && p == pe) {
                return result;
            }
            else {
                Helpers.ThrowParserException("unexpected token at '{0}'", Helpers.GetMessageForException(source, p, pe));
            }
            return null;
        }
Example #7
0
        static int? JSON_parse_value(ParserEngineState json, ref int p, ref int pe, ref Object result)
        {
            int cs = EVIL;
            var source = json.Source;

            #region ** ragel generated code **

            {
                cs = JSON_value_start;
            }

            {
                sbyte _klen;
                sbyte _trans;
                sbyte _acts;
                sbyte _nacts;
                sbyte _keys;

                if (p == pe)
                    goto _test_eof;
                if (cs == 0)
                    goto _out;
            _resume:
                _acts = _JSON_value_from_state_actions[cs];
                _nacts = _JSON_value_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_value_actions[_acts++]) {
                        case 9: { p--; { p++; if (true) goto _out; } }
                            break;
                        default: break;
                    }
                }

                _keys = _JSON_value_key_offsets[cs];
                _trans = (sbyte)_JSON_value_index_offsets[cs];

                _klen = _JSON_value_single_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + _klen - 1);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + ((_upper - _lower) >> 1));
                        if (source[p] < _JSON_value_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 1);
                        else if (source[p] > _JSON_value_trans_keys[_mid])
                            _lower = (sbyte)(_mid + 1);
                        else {
                            _trans += (sbyte)(_mid - _keys);
                            goto _match;
                        }
                    }
                    _keys += (sbyte)_klen;
                    _trans += (sbyte)_klen;
                }

                _klen = _JSON_value_range_lengths[cs];
                if (_klen > 0) {
                    sbyte _lower = _keys;
                    sbyte _mid;
                    sbyte _upper = (sbyte)(_keys + (_klen << 1) - 2);
                    while (true) {
                        if (_upper < _lower)
                            break;

                        _mid = (sbyte)(_lower + (((_upper - _lower) >> 1) & ~1));
                        if (source[p] < _JSON_value_trans_keys[_mid])
                            _upper = (sbyte)(_mid - 2);
                        else if (source[p] > _JSON_value_trans_keys[_mid + 1])
                            _lower = (sbyte)(_mid + 2);
                        else {
                            _trans += (sbyte)((_mid - _keys) >> 1);
                            goto _match;
                        }
                    }
                    _trans += (sbyte)_klen;
                }

            _match:
                cs = _JSON_value_trans_targs[_trans];

                if (_JSON_value_trans_actions[_trans] == 0)
                    goto _again;

                _acts = _JSON_value_trans_actions[_trans];
                _nacts = _JSON_value_actions[_acts++];
                while (_nacts-- > 0) {
                    switch (_JSON_value_actions[_acts++]) {
                        case 0: {
                                result = null;
                            }
                            break;
                        case 1: {
                                result = false;
                            }
                            break;
                        case 2: {
                                result = true;
                            }
                            break;
                        case 3: {
                                if (json.AllowNaN) {
                                    result = CONSTANT_NAN;
                                }
                                else {
                                    Helpers.ThrowParserException("unexpected token at '{0}'", Helpers.GetMessageForException(source, p - 2, pe));
                                }
                            }
                            break;
                        case 4: {
                                if (json.AllowNaN) {
                                    result = CONSTANT_INFINITY;
                                }
                                else {
                                    Helpers.ThrowParserException("unexpected token at '{0}'", Helpers.GetMessageForException(source, p - 8, pe));
                                }
                            }
                            break;
                        case 5: {
                                int? np = JSON_parse_string(json, ref p, ref pe, ref result);
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        case 6: {
                                int? np;
                                if (pe > p + 9 && String.CompareOrdinal(JSON_MINUS_INFINITY, 0, source, p, 9) == 0) {
                                    if (json.AllowNaN) {
                                        result = CONSTANT_MINUS_INFINITY; { p = ((p + 10)) - 1; }
                                        p--; { p++; if (true) goto _out; }
                                    }
                                    else {
                                        Helpers.ThrowParserException("unexpected token at '{0}'", Helpers.GetMessageForException(source, p, pe));
                                    }
                                }
                                np = JSON_parse_float(json, ref p, ref pe, ref result);
                                if (np.HasValue) { p = ((np.Value)) - 1; }
                                np = JSON_parse_integer(json, ref p, ref pe, ref result);
                                if (np.HasValue) { p = ((np.Value)) - 1; }
                                p--; { p++; if (true) goto _out; }
                            }
                            break;
                        case 7: {
                                int? np;
                                json.CurrentNesting++;
                                np = JSON_parse_array(json, ref p, ref pe, ref result);
                                json.CurrentNesting--;
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        case 8: {
                                int? np;
                                json.CurrentNesting++;
                                np = JSON_parse_object(json, ref p, ref pe, ref result);
                                json.CurrentNesting--;
                                if (!np.HasValue) { p--; { p++; if (true) goto _out; } } else { p = ((np.Value)) - 1; }
                            }
                            break;
                        default: break;
                    }
                }

            _again:
                if (cs == 0)
                    goto _out;
                if (++p != pe)
                    goto _resume;
            _test_eof: { }
            _out: { }
            }
            #endregion

            if (cs >= JSON_value_first_final) {
                return p;
            }
            else {
                return null;
            }
        }