json-maglev- 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. data/CHANGES +200 -0
  2. data/COPYING +58 -0
  3. data/COPYING-json-jruby +57 -0
  4. data/GPL +340 -0
  5. data/Gemfile +7 -0
  6. data/README-json-jruby.markdown +33 -0
  7. data/README.rdoc +358 -0
  8. data/Rakefile +407 -0
  9. data/TODO +1 -0
  10. data/VERSION +1 -0
  11. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  12. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  13. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  14. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  15. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  16. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  17. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  18. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  19. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  20. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  21. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  22. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  23. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  24. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  25. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  26. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  27. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  28. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  29. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  30. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  31. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  32. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  33. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  34. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  35. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  36. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  37. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  38. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  39. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  40. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  41. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  42. data/benchmarks/generator2_benchmark.rb +222 -0
  43. data/benchmarks/generator_benchmark.rb +224 -0
  44. data/benchmarks/ohai.json +1216 -0
  45. data/benchmarks/ohai.ruby +1 -0
  46. data/benchmarks/parser2_benchmark.rb +251 -0
  47. data/benchmarks/parser_benchmark.rb +259 -0
  48. data/bin/edit_json.rb +9 -0
  49. data/bin/prettify_json.rb +48 -0
  50. data/data/example.json +1 -0
  51. data/data/index.html +38 -0
  52. data/data/prototype.js +4184 -0
  53. data/ext/json/ext/generator/extconf.rb +20 -0
  54. data/ext/json/ext/generator/generator.c +1414 -0
  55. data/ext/json/ext/generator/generator.h +196 -0
  56. data/ext/json/ext/parser/extconf.rb +16 -0
  57. data/ext/json/ext/parser/parser.c +1996 -0
  58. data/ext/json/ext/parser/parser.h +82 -0
  59. data/ext/json/ext/parser/parser.rl +853 -0
  60. data/install.rb +26 -0
  61. data/java/lib/bytelist-1.0.6.jar +0 -0
  62. data/java/lib/jcodings.jar +0 -0
  63. data/java/src/json/ext/ByteListTranscoder.java +167 -0
  64. data/java/src/json/ext/Generator.java +437 -0
  65. data/java/src/json/ext/GeneratorMethods.java +232 -0
  66. data/java/src/json/ext/GeneratorService.java +43 -0
  67. data/java/src/json/ext/GeneratorState.java +473 -0
  68. data/java/src/json/ext/OptionsReader.java +119 -0
  69. data/java/src/json/ext/Parser.java +2314 -0
  70. data/java/src/json/ext/Parser.rl +844 -0
  71. data/java/src/json/ext/ParserService.java +35 -0
  72. data/java/src/json/ext/RuntimeInfo.java +121 -0
  73. data/java/src/json/ext/StringDecoder.java +166 -0
  74. data/java/src/json/ext/StringEncoder.java +106 -0
  75. data/java/src/json/ext/Utils.java +89 -0
  76. data/json-java.gemspec +22 -0
  77. data/json.gemspec +41 -0
  78. data/json_pure.gemspec +46 -0
  79. data/lib/json.rb +62 -0
  80. data/lib/json/Array.xpm +21 -0
  81. data/lib/json/FalseClass.xpm +21 -0
  82. data/lib/json/Hash.xpm +21 -0
  83. data/lib/json/Key.xpm +73 -0
  84. data/lib/json/NilClass.xpm +21 -0
  85. data/lib/json/Numeric.xpm +28 -0
  86. data/lib/json/String.xpm +96 -0
  87. data/lib/json/TrueClass.xpm +21 -0
  88. data/lib/json/add/core.rb +243 -0
  89. data/lib/json/add/rails.rb +8 -0
  90. data/lib/json/common.rb +423 -0
  91. data/lib/json/editor.rb +1369 -0
  92. data/lib/json/ext.rb +28 -0
  93. data/lib/json/json.xpm +1499 -0
  94. data/lib/json/pure.rb +15 -0
  95. data/lib/json/pure/generator.rb +442 -0
  96. data/lib/json/pure/parser.rb +320 -0
  97. data/lib/json/version.rb +8 -0
  98. data/tests/fixtures/fail1.json +1 -0
  99. data/tests/fixtures/fail10.json +1 -0
  100. data/tests/fixtures/fail11.json +1 -0
  101. data/tests/fixtures/fail12.json +1 -0
  102. data/tests/fixtures/fail13.json +1 -0
  103. data/tests/fixtures/fail14.json +1 -0
  104. data/tests/fixtures/fail18.json +1 -0
  105. data/tests/fixtures/fail19.json +1 -0
  106. data/tests/fixtures/fail2.json +1 -0
  107. data/tests/fixtures/fail20.json +1 -0
  108. data/tests/fixtures/fail21.json +1 -0
  109. data/tests/fixtures/fail22.json +1 -0
  110. data/tests/fixtures/fail23.json +1 -0
  111. data/tests/fixtures/fail24.json +1 -0
  112. data/tests/fixtures/fail25.json +1 -0
  113. data/tests/fixtures/fail27.json +2 -0
  114. data/tests/fixtures/fail28.json +2 -0
  115. data/tests/fixtures/fail3.json +1 -0
  116. data/tests/fixtures/fail4.json +1 -0
  117. data/tests/fixtures/fail5.json +1 -0
  118. data/tests/fixtures/fail6.json +1 -0
  119. data/tests/fixtures/fail7.json +1 -0
  120. data/tests/fixtures/fail8.json +1 -0
  121. data/tests/fixtures/fail9.json +1 -0
  122. data/tests/fixtures/pass1.json +56 -0
  123. data/tests/fixtures/pass15.json +1 -0
  124. data/tests/fixtures/pass16.json +1 -0
  125. data/tests/fixtures/pass17.json +1 -0
  126. data/tests/fixtures/pass2.json +1 -0
  127. data/tests/fixtures/pass26.json +1 -0
  128. data/tests/fixtures/pass3.json +6 -0
  129. data/tests/setup_variant.rb +11 -0
  130. data/tests/test_json.rb +424 -0
  131. data/tests/test_json_addition.rb +167 -0
  132. data/tests/test_json_encoding.rb +65 -0
  133. data/tests/test_json_fixtures.rb +35 -0
  134. data/tests/test_json_generate.rb +180 -0
  135. data/tests/test_json_string_matching.rb +40 -0
  136. data/tests/test_json_unicode.rb +72 -0
  137. data/tools/fuzz.rb +139 -0
  138. data/tools/server.rb +61 -0
  139. metadata +265 -0
@@ -0,0 +1,196 @@
1
+ #ifndef _GENERATOR_H_
2
+ #define _GENERATOR_H_
3
+
4
+ #include <string.h>
5
+ #include <assert.h>
6
+ #include <math.h>
7
+
8
+ #include "ruby.h"
9
+
10
+ #if HAVE_RUBY_RE_H
11
+ #include "ruby/re.h"
12
+ #endif
13
+
14
+ #if HAVE_RE_H
15
+ #include "re.h"
16
+ #endif
17
+
18
+ #ifdef HAVE_RUBY_ENCODING_H
19
+ #include "ruby/encoding.h"
20
+ #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
21
+ #else
22
+ #define FORCE_UTF8(obj)
23
+ #endif
24
+
25
+ #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
26
+
27
+ #ifndef RHASH_SIZE
28
+ #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
29
+ #endif
30
+
31
+ #ifndef RFLOAT_VALUE
32
+ #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
33
+ #endif
34
+
35
+ #ifndef RARRAY_PTR
36
+ #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
37
+ #endif
38
+ #ifndef RARRAY_LEN
39
+ #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
40
+ #endif
41
+ #ifndef RSTRING_PTR
42
+ #define RSTRING_PTR(string) RSTRING(string)->ptr
43
+ #endif
44
+ #ifndef RSTRING_LEN
45
+ #define RSTRING_LEN(string) RSTRING(string)->len
46
+ #endif
47
+
48
+ #define RSTRING_PAIR(string) RSTRING_PTR(string), RSTRING_LEN(string)
49
+
50
+ /* fbuffer implementation */
51
+
52
+ typedef struct FBufferStruct {
53
+ unsigned long initial_length;
54
+ char *ptr;
55
+ unsigned long len;
56
+ unsigned long capa;
57
+ } FBuffer;
58
+
59
+ #define FBUFFER_INITIAL_LENGTH 4096
60
+
61
+ #define FBUFFER_PTR(fb) (fb->ptr)
62
+ #define FBUFFER_LEN(fb) (fb->len)
63
+ #define FBUFFER_CAPA(fb) (fb->capa)
64
+ #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
65
+
66
+ static char *fstrndup(const char *ptr, unsigned long len);
67
+ static FBuffer *fbuffer_alloc();
68
+ static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length);
69
+ static void fbuffer_free(FBuffer *fb);
70
+ static void fbuffer_clear(FBuffer *fb);
71
+ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
72
+ static void fbuffer_append_long(FBuffer *fb, long number);
73
+ static void fbuffer_append_char(FBuffer *fb, char newchr);
74
+ static FBuffer *fbuffer_dup(FBuffer *fb);
75
+ static VALUE fbuffer_to_s(FBuffer *fb);
76
+
77
+ /* unicode defintions */
78
+
79
+ #define UNI_STRICT_CONVERSION 1
80
+
81
+ typedef unsigned long UTF32; /* at least 32 bits */
82
+ typedef unsigned short UTF16; /* at least 16 bits */
83
+ typedef unsigned char UTF8; /* typically 8 bits */
84
+
85
+ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
86
+ #define UNI_MAX_BMP (UTF32)0x0000FFFF
87
+ #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
88
+ #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
89
+ #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
90
+
91
+ #define UNI_SUR_HIGH_START (UTF32)0xD800
92
+ #define UNI_SUR_HIGH_END (UTF32)0xDBFF
93
+ #define UNI_SUR_LOW_START (UTF32)0xDC00
94
+ #define UNI_SUR_LOW_END (UTF32)0xDFFF
95
+
96
+ static const int halfShift = 10; /* used for shifting by 10 bits */
97
+
98
+ static const UTF32 halfBase = 0x0010000UL;
99
+ static const UTF32 halfMask = 0x3FFUL;
100
+
101
+ static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
102
+ static void unicode_escape(char *buf, UTF16 character);
103
+ static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
104
+ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
105
+ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
106
+
107
+ /* ruby api and some helpers */
108
+
109
+ typedef struct JSON_Generator_StateStruct {
110
+ char *indent;
111
+ long indent_len;
112
+ char *space;
113
+ long space_len;
114
+ char *space_before;
115
+ long space_before_len;
116
+ char *object_nl;
117
+ long object_nl_len;
118
+ char *array_nl;
119
+ long array_nl_len;
120
+ FBuffer *array_delim;
121
+ FBuffer *object_delim;
122
+ FBuffer *object_delim2;
123
+ long max_nesting;
124
+ char allow_nan;
125
+ char ascii_only;
126
+ long depth;
127
+ } JSON_Generator_State;
128
+
129
+ #define GET_STATE(self) \
130
+ JSON_Generator_State *state; \
131
+ Data_Get_Struct(self, JSON_Generator_State, state)
132
+
133
+ #define GENERATE_JSON(type) \
134
+ FBuffer *buffer; \
135
+ VALUE Vstate; \
136
+ JSON_Generator_State *state; \
137
+ \
138
+ rb_scan_args(argc, argv, "01", &Vstate); \
139
+ Vstate = cState_from_state_s(cState, Vstate); \
140
+ Data_Get_Struct(Vstate, JSON_Generator_State, state); \
141
+ buffer = cState_prepare_buffer(Vstate); \
142
+ generate_json_##type(buffer, Vstate, state, self); \
143
+ return fbuffer_to_s(buffer)
144
+
145
+ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
146
+ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
147
+ static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
148
+ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
149
+ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
150
+ static VALUE mString_included_s(VALUE self, VALUE modul);
151
+ static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
152
+ static VALUE mString_to_json_raw_object(VALUE self);
153
+ static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
154
+ static VALUE mString_Extend_json_create(VALUE self, VALUE o);
155
+ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
156
+ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
157
+ static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
158
+ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
159
+ static void State_free(JSON_Generator_State *state);
160
+ static JSON_Generator_State *State_allocate();
161
+ static VALUE cState_s_allocate(VALUE klass);
162
+ static VALUE cState_configure(VALUE self, VALUE opts);
163
+ static VALUE cState_to_h(VALUE self);
164
+ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
165
+ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
166
+ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
167
+ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
168
+ static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
169
+ static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
170
+ static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
171
+ static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
172
+ static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
173
+ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
174
+ static VALUE cState_partial_generate(VALUE self, VALUE obj);
175
+ static VALUE cState_generate(VALUE self, VALUE obj);
176
+ static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
177
+ static VALUE cState_from_state_s(VALUE self, VALUE opts);
178
+ static VALUE cState_indent(VALUE self);
179
+ static VALUE cState_indent_set(VALUE self, VALUE indent);
180
+ static VALUE cState_space(VALUE self);
181
+ static VALUE cState_space_set(VALUE self, VALUE space);
182
+ static VALUE cState_space_before(VALUE self);
183
+ static VALUE cState_space_before_set(VALUE self, VALUE space_before);
184
+ static VALUE cState_object_nl(VALUE self);
185
+ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
186
+ static VALUE cState_array_nl(VALUE self);
187
+ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
188
+ static VALUE cState_max_nesting(VALUE self);
189
+ static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
190
+ static VALUE cState_allow_nan_p(VALUE self);
191
+ static VALUE cState_ascii_only_p(VALUE self);
192
+ static VALUE cState_depth(VALUE self);
193
+ static VALUE cState_depth_set(VALUE self, VALUE depth);
194
+ static FBuffer *cState_prepare_buffer(VALUE self);
195
+
196
+ #endif
@@ -0,0 +1,16 @@
1
+ require 'mkmf'
2
+ require 'rbconfig'
3
+
4
+ unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
5
+ $CFLAGS << ' -O3'
6
+ end
7
+ if CONFIG['CC'] =~ /gcc/
8
+ $CFLAGS << ' -Wall'
9
+ #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
10
+ # $CFLAGS << ' -O0 -ggdb'
11
+ #end
12
+ end
13
+
14
+ have_header("re.h")
15
+ have_header("ruby/st.h")
16
+ create_makefile 'json/ext/parser'
@@ -0,0 +1,1996 @@
1
+
2
+ #line 1 "parser.rl"
3
+ #include "parser.h"
4
+
5
+ /* unicode */
6
+
7
+ static const char digit_values[256] = {
8
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
9
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
10
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
11
+ -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
12
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
13
+ 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
14
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
16
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
18
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
19
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
20
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
21
+ -1, -1, -1, -1, -1, -1, -1
22
+ };
23
+
24
+ static UTF32 unescape_unicode(const unsigned char *p)
25
+ {
26
+ char b;
27
+ UTF32 result = 0;
28
+ b = digit_values[p[0]];
29
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
30
+ result = (result << 4) | b;
31
+ b = digit_values[p[1]];
32
+ result = (result << 4) | b;
33
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
34
+ b = digit_values[p[2]];
35
+ result = (result << 4) | b;
36
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
37
+ b = digit_values[p[3]];
38
+ result = (result << 4) | b;
39
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
40
+ return result;
41
+ }
42
+
43
+ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
44
+ {
45
+ int len = 1;
46
+ if (ch <= 0x7F) {
47
+ buf[0] = (char) ch;
48
+ } else if (ch <= 0x07FF) {
49
+ buf[0] = (char) ((ch >> 6) | 0xC0);
50
+ buf[1] = (char) ((ch & 0x3F) | 0x80);
51
+ len++;
52
+ } else if (ch <= 0xFFFF) {
53
+ buf[0] = (char) ((ch >> 12) | 0xE0);
54
+ buf[1] = (char) (((ch >> 6) & 0x3F) | 0x80);
55
+ buf[2] = (char) ((ch & 0x3F) | 0x80);
56
+ len += 2;
57
+ } else if (ch <= 0x1fffff) {
58
+ buf[0] =(char) ((ch >> 18) | 0xF0);
59
+ buf[1] =(char) (((ch >> 12) & 0x3F) | 0x80);
60
+ buf[2] =(char) (((ch >> 6) & 0x3F) | 0x80);
61
+ buf[3] =(char) ((ch & 0x3F) | 0x80);
62
+ len += 3;
63
+ } else {
64
+ buf[0] = '?';
65
+ }
66
+ return len;
67
+ }
68
+
69
+ #ifdef HAVE_RUBY_ENCODING_H
70
+ static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8, CEncoding_UTF_16BE,
71
+ CEncoding_UTF_16LE, CEncoding_UTF_32BE, CEncoding_UTF_32LE;
72
+ static ID i_encoding, i_encode;
73
+ #else
74
+ static ID i_iconv;
75
+ #endif
76
+
77
+ static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
78
+ static VALUE CNaN, CInfinity, CMinusInfinity;
79
+
80
+ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
81
+ i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
82
+ i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
83
+
84
+
85
+ #line 108 "parser.rl"
86
+
87
+
88
+
89
+ #line 90 "parser.c"
90
+ static const int JSON_object_start = 1;
91
+ static const int JSON_object_first_final = 27;
92
+ static const int JSON_object_error = 0;
93
+
94
+ static const int JSON_object_en_main = 1;
95
+
96
+
97
+ #line 148 "parser.rl"
98
+
99
+
100
+ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
101
+ {
102
+ int cs = EVIL;
103
+ VALUE last_name = Qnil;
104
+ VALUE object_class = json->object_class;
105
+
106
+ if (json->max_nesting && json->current_nesting > json->max_nesting) {
107
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
108
+ }
109
+
110
+ *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
111
+
112
+
113
+ #line 114 "parser.c"
114
+ {
115
+ cs = JSON_object_start;
116
+ }
117
+
118
+ #line 163 "parser.rl"
119
+
120
+ #line 121 "parser.c"
121
+ {
122
+ if ( p == pe )
123
+ goto _test_eof;
124
+ switch ( cs )
125
+ {
126
+ case 1:
127
+ if ( (*p) == 123 )
128
+ goto st2;
129
+ goto st0;
130
+ st0:
131
+ cs = 0;
132
+ goto _out;
133
+ st2:
134
+ if ( ++p == pe )
135
+ goto _test_eof2;
136
+ case 2:
137
+ switch( (*p) ) {
138
+ case 13: goto st2;
139
+ case 32: goto st2;
140
+ case 34: goto tr2;
141
+ case 47: goto st23;
142
+ case 125: goto tr4;
143
+ }
144
+ if ( 9 <= (*p) && (*p) <= 10 )
145
+ goto st2;
146
+ goto st0;
147
+ tr2:
148
+ #line 131 "parser.rl"
149
+ {
150
+ const char *np;
151
+ json->parsing_name = 1;
152
+ np = JSON_parse_string(json, p, pe, &last_name);
153
+ json->parsing_name = 0;
154
+ if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {p = (( np))-1;}
155
+ }
156
+ goto st3;
157
+ st3:
158
+ if ( ++p == pe )
159
+ goto _test_eof3;
160
+ case 3:
161
+ #line 162 "parser.c"
162
+ switch( (*p) ) {
163
+ case 13: goto st3;
164
+ case 32: goto st3;
165
+ case 47: goto st4;
166
+ case 58: goto st8;
167
+ }
168
+ if ( 9 <= (*p) && (*p) <= 10 )
169
+ goto st3;
170
+ goto st0;
171
+ st4:
172
+ if ( ++p == pe )
173
+ goto _test_eof4;
174
+ case 4:
175
+ switch( (*p) ) {
176
+ case 42: goto st5;
177
+ case 47: goto st7;
178
+ }
179
+ goto st0;
180
+ st5:
181
+ if ( ++p == pe )
182
+ goto _test_eof5;
183
+ case 5:
184
+ if ( (*p) == 42 )
185
+ goto st6;
186
+ goto st5;
187
+ st6:
188
+ if ( ++p == pe )
189
+ goto _test_eof6;
190
+ case 6:
191
+ switch( (*p) ) {
192
+ case 42: goto st6;
193
+ case 47: goto st3;
194
+ }
195
+ goto st5;
196
+ st7:
197
+ if ( ++p == pe )
198
+ goto _test_eof7;
199
+ case 7:
200
+ if ( (*p) == 10 )
201
+ goto st3;
202
+ goto st7;
203
+ st8:
204
+ if ( ++p == pe )
205
+ goto _test_eof8;
206
+ case 8:
207
+ switch( (*p) ) {
208
+ case 13: goto st8;
209
+ case 32: goto st8;
210
+ case 34: goto tr11;
211
+ case 45: goto tr11;
212
+ case 47: goto st19;
213
+ case 73: goto tr11;
214
+ case 78: goto tr11;
215
+ case 91: goto tr11;
216
+ case 102: goto tr11;
217
+ case 110: goto tr11;
218
+ case 116: goto tr11;
219
+ case 123: goto tr11;
220
+ }
221
+ if ( (*p) > 10 ) {
222
+ if ( 48 <= (*p) && (*p) <= 57 )
223
+ goto tr11;
224
+ } else if ( (*p) >= 9 )
225
+ goto st8;
226
+ goto st0;
227
+ tr11:
228
+ #line 116 "parser.rl"
229
+ {
230
+ VALUE v = Qnil;
231
+ const char *np = JSON_parse_value(json, p, pe, &v);
232
+ if (np == NULL) {
233
+ p--; {p++; cs = 9; goto _out;}
234
+ } else {
235
+ if (NIL_P(json->object_class)) {
236
+ rb_hash_aset(*result, last_name, v);
237
+ } else {
238
+ rb_funcall(*result, i_aset, 2, last_name, v);
239
+ }
240
+ {p = (( np))-1;}
241
+ }
242
+ }
243
+ goto st9;
244
+ st9:
245
+ if ( ++p == pe )
246
+ goto _test_eof9;
247
+ case 9:
248
+ #line 249 "parser.c"
249
+ switch( (*p) ) {
250
+ case 13: goto st9;
251
+ case 32: goto st9;
252
+ case 44: goto st10;
253
+ case 47: goto st15;
254
+ case 125: goto tr4;
255
+ }
256
+ if ( 9 <= (*p) && (*p) <= 10 )
257
+ goto st9;
258
+ goto st0;
259
+ st10:
260
+ if ( ++p == pe )
261
+ goto _test_eof10;
262
+ case 10:
263
+ switch( (*p) ) {
264
+ case 13: goto st10;
265
+ case 32: goto st10;
266
+ case 34: goto tr2;
267
+ case 47: goto st11;
268
+ }
269
+ if ( 9 <= (*p) && (*p) <= 10 )
270
+ goto st10;
271
+ goto st0;
272
+ st11:
273
+ if ( ++p == pe )
274
+ goto _test_eof11;
275
+ case 11:
276
+ switch( (*p) ) {
277
+ case 42: goto st12;
278
+ case 47: goto st14;
279
+ }
280
+ goto st0;
281
+ st12:
282
+ if ( ++p == pe )
283
+ goto _test_eof12;
284
+ case 12:
285
+ if ( (*p) == 42 )
286
+ goto st13;
287
+ goto st12;
288
+ st13:
289
+ if ( ++p == pe )
290
+ goto _test_eof13;
291
+ case 13:
292
+ switch( (*p) ) {
293
+ case 42: goto st13;
294
+ case 47: goto st10;
295
+ }
296
+ goto st12;
297
+ st14:
298
+ if ( ++p == pe )
299
+ goto _test_eof14;
300
+ case 14:
301
+ if ( (*p) == 10 )
302
+ goto st10;
303
+ goto st14;
304
+ st15:
305
+ if ( ++p == pe )
306
+ goto _test_eof15;
307
+ case 15:
308
+ switch( (*p) ) {
309
+ case 42: goto st16;
310
+ case 47: goto st18;
311
+ }
312
+ goto st0;
313
+ st16:
314
+ if ( ++p == pe )
315
+ goto _test_eof16;
316
+ case 16:
317
+ if ( (*p) == 42 )
318
+ goto st17;
319
+ goto st16;
320
+ st17:
321
+ if ( ++p == pe )
322
+ goto _test_eof17;
323
+ case 17:
324
+ switch( (*p) ) {
325
+ case 42: goto st17;
326
+ case 47: goto st9;
327
+ }
328
+ goto st16;
329
+ st18:
330
+ if ( ++p == pe )
331
+ goto _test_eof18;
332
+ case 18:
333
+ if ( (*p) == 10 )
334
+ goto st9;
335
+ goto st18;
336
+ tr4:
337
+ #line 139 "parser.rl"
338
+ { p--; {p++; cs = 27; goto _out;} }
339
+ goto st27;
340
+ st27:
341
+ if ( ++p == pe )
342
+ goto _test_eof27;
343
+ case 27:
344
+ #line 345 "parser.c"
345
+ goto st0;
346
+ st19:
347
+ if ( ++p == pe )
348
+ goto _test_eof19;
349
+ case 19:
350
+ switch( (*p) ) {
351
+ case 42: goto st20;
352
+ case 47: goto st22;
353
+ }
354
+ goto st0;
355
+ st20:
356
+ if ( ++p == pe )
357
+ goto _test_eof20;
358
+ case 20:
359
+ if ( (*p) == 42 )
360
+ goto st21;
361
+ goto st20;
362
+ st21:
363
+ if ( ++p == pe )
364
+ goto _test_eof21;
365
+ case 21:
366
+ switch( (*p) ) {
367
+ case 42: goto st21;
368
+ case 47: goto st8;
369
+ }
370
+ goto st20;
371
+ st22:
372
+ if ( ++p == pe )
373
+ goto _test_eof22;
374
+ case 22:
375
+ if ( (*p) == 10 )
376
+ goto st8;
377
+ goto st22;
378
+ st23:
379
+ if ( ++p == pe )
380
+ goto _test_eof23;
381
+ case 23:
382
+ switch( (*p) ) {
383
+ case 42: goto st24;
384
+ case 47: goto st26;
385
+ }
386
+ goto st0;
387
+ st24:
388
+ if ( ++p == pe )
389
+ goto _test_eof24;
390
+ case 24:
391
+ if ( (*p) == 42 )
392
+ goto st25;
393
+ goto st24;
394
+ st25:
395
+ if ( ++p == pe )
396
+ goto _test_eof25;
397
+ case 25:
398
+ switch( (*p) ) {
399
+ case 42: goto st25;
400
+ case 47: goto st2;
401
+ }
402
+ goto st24;
403
+ st26:
404
+ if ( ++p == pe )
405
+ goto _test_eof26;
406
+ case 26:
407
+ if ( (*p) == 10 )
408
+ goto st2;
409
+ goto st26;
410
+ }
411
+ _test_eof2: cs = 2; goto _test_eof;
412
+ _test_eof3: cs = 3; goto _test_eof;
413
+ _test_eof4: cs = 4; goto _test_eof;
414
+ _test_eof5: cs = 5; goto _test_eof;
415
+ _test_eof6: cs = 6; goto _test_eof;
416
+ _test_eof7: cs = 7; goto _test_eof;
417
+ _test_eof8: cs = 8; goto _test_eof;
418
+ _test_eof9: cs = 9; goto _test_eof;
419
+ _test_eof10: cs = 10; goto _test_eof;
420
+ _test_eof11: cs = 11; goto _test_eof;
421
+ _test_eof12: cs = 12; goto _test_eof;
422
+ _test_eof13: cs = 13; goto _test_eof;
423
+ _test_eof14: cs = 14; goto _test_eof;
424
+ _test_eof15: cs = 15; goto _test_eof;
425
+ _test_eof16: cs = 16; goto _test_eof;
426
+ _test_eof17: cs = 17; goto _test_eof;
427
+ _test_eof18: cs = 18; goto _test_eof;
428
+ _test_eof27: cs = 27; goto _test_eof;
429
+ _test_eof19: cs = 19; goto _test_eof;
430
+ _test_eof20: cs = 20; goto _test_eof;
431
+ _test_eof21: cs = 21; goto _test_eof;
432
+ _test_eof22: cs = 22; goto _test_eof;
433
+ _test_eof23: cs = 23; goto _test_eof;
434
+ _test_eof24: cs = 24; goto _test_eof;
435
+ _test_eof25: cs = 25; goto _test_eof;
436
+ _test_eof26: cs = 26; goto _test_eof;
437
+
438
+ _test_eof: {}
439
+ _out: {}
440
+ }
441
+
442
+ #line 164 "parser.rl"
443
+
444
+ if (cs >= JSON_object_first_final) {
445
+ if (json->create_additions) {
446
+ VALUE klassname = rb_hash_aref(*result, json->create_id);
447
+ if (!NIL_P(klassname)) {
448
+ VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
449
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
450
+ *result = rb_funcall(klass, i_json_create, 1, *result);
451
+ }
452
+ }
453
+ }
454
+ return p + 1;
455
+ } else {
456
+ return NULL;
457
+ }
458
+ }
459
+
460
+
461
+ #line 462 "parser.c"
462
+ static const int JSON_value_start = 1;
463
+ static const int JSON_value_first_final = 21;
464
+ static const int JSON_value_error = 0;
465
+
466
+ static const int JSON_value_en_main = 1;
467
+
468
+
469
+ #line 262 "parser.rl"
470
+
471
+
472
+ static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
473
+ {
474
+ int cs = EVIL;
475
+
476
+
477
+ #line 478 "parser.c"
478
+ {
479
+ cs = JSON_value_start;
480
+ }
481
+
482
+ #line 269 "parser.rl"
483
+
484
+ #line 485 "parser.c"
485
+ {
486
+ if ( p == pe )
487
+ goto _test_eof;
488
+ switch ( cs )
489
+ {
490
+ case 1:
491
+ switch( (*p) ) {
492
+ case 34: goto tr0;
493
+ case 45: goto tr2;
494
+ case 73: goto st2;
495
+ case 78: goto st9;
496
+ case 91: goto tr5;
497
+ case 102: goto st11;
498
+ case 110: goto st15;
499
+ case 116: goto st18;
500
+ case 123: goto tr9;
501
+ }
502
+ if ( 48 <= (*p) && (*p) <= 57 )
503
+ goto tr2;
504
+ goto st0;
505
+ st0:
506
+ cs = 0;
507
+ goto _out;
508
+ tr0:
509
+ #line 210 "parser.rl"
510
+ {
511
+ const char *np = JSON_parse_string(json, p, pe, result);
512
+ if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
513
+ }
514
+ goto st21;
515
+ tr2:
516
+ #line 215 "parser.rl"
517
+ {
518
+ const char *np;
519
+ if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) {
520
+ if (json->allow_nan) {
521
+ *result = CMinusInfinity;
522
+ {p = (( p + 10))-1;}
523
+ p--; {p++; cs = 21; goto _out;}
524
+ } else {
525
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
526
+ }
527
+ }
528
+ np = JSON_parse_float(json, p, pe, result);
529
+ if (np != NULL) {p = (( np))-1;}
530
+ np = JSON_parse_integer(json, p, pe, result);
531
+ if (np != NULL) {p = (( np))-1;}
532
+ p--; {p++; cs = 21; goto _out;}
533
+ }
534
+ goto st21;
535
+ tr5:
536
+ #line 233 "parser.rl"
537
+ {
538
+ const char *np;
539
+ json->current_nesting++;
540
+ np = JSON_parse_array(json, p, pe, result);
541
+ json->current_nesting--;
542
+ if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
543
+ }
544
+ goto st21;
545
+ tr9:
546
+ #line 241 "parser.rl"
547
+ {
548
+ const char *np;
549
+ json->current_nesting++;
550
+ np = JSON_parse_object(json, p, pe, result);
551
+ json->current_nesting--;
552
+ if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
553
+ }
554
+ goto st21;
555
+ tr16:
556
+ #line 203 "parser.rl"
557
+ {
558
+ if (json->allow_nan) {
559
+ *result = CInfinity;
560
+ } else {
561
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
562
+ }
563
+ }
564
+ goto st21;
565
+ tr18:
566
+ #line 196 "parser.rl"
567
+ {
568
+ if (json->allow_nan) {
569
+ *result = CNaN;
570
+ } else {
571
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
572
+ }
573
+ }
574
+ goto st21;
575
+ tr22:
576
+ #line 190 "parser.rl"
577
+ {
578
+ *result = Qfalse;
579
+ }
580
+ goto st21;
581
+ tr25:
582
+ #line 187 "parser.rl"
583
+ {
584
+ *result = Qnil;
585
+ }
586
+ goto st21;
587
+ tr28:
588
+ #line 193 "parser.rl"
589
+ {
590
+ *result = Qtrue;
591
+ }
592
+ goto st21;
593
+ st21:
594
+ if ( ++p == pe )
595
+ goto _test_eof21;
596
+ case 21:
597
+ #line 249 "parser.rl"
598
+ { p--; {p++; cs = 21; goto _out;} }
599
+ #line 600 "parser.c"
600
+ goto st0;
601
+ st2:
602
+ if ( ++p == pe )
603
+ goto _test_eof2;
604
+ case 2:
605
+ if ( (*p) == 110 )
606
+ goto st3;
607
+ goto st0;
608
+ st3:
609
+ if ( ++p == pe )
610
+ goto _test_eof3;
611
+ case 3:
612
+ if ( (*p) == 102 )
613
+ goto st4;
614
+ goto st0;
615
+ st4:
616
+ if ( ++p == pe )
617
+ goto _test_eof4;
618
+ case 4:
619
+ if ( (*p) == 105 )
620
+ goto st5;
621
+ goto st0;
622
+ st5:
623
+ if ( ++p == pe )
624
+ goto _test_eof5;
625
+ case 5:
626
+ if ( (*p) == 110 )
627
+ goto st6;
628
+ goto st0;
629
+ st6:
630
+ if ( ++p == pe )
631
+ goto _test_eof6;
632
+ case 6:
633
+ if ( (*p) == 105 )
634
+ goto st7;
635
+ goto st0;
636
+ st7:
637
+ if ( ++p == pe )
638
+ goto _test_eof7;
639
+ case 7:
640
+ if ( (*p) == 116 )
641
+ goto st8;
642
+ goto st0;
643
+ st8:
644
+ if ( ++p == pe )
645
+ goto _test_eof8;
646
+ case 8:
647
+ if ( (*p) == 121 )
648
+ goto tr16;
649
+ goto st0;
650
+ st9:
651
+ if ( ++p == pe )
652
+ goto _test_eof9;
653
+ case 9:
654
+ if ( (*p) == 97 )
655
+ goto st10;
656
+ goto st0;
657
+ st10:
658
+ if ( ++p == pe )
659
+ goto _test_eof10;
660
+ case 10:
661
+ if ( (*p) == 78 )
662
+ goto tr18;
663
+ goto st0;
664
+ st11:
665
+ if ( ++p == pe )
666
+ goto _test_eof11;
667
+ case 11:
668
+ if ( (*p) == 97 )
669
+ goto st12;
670
+ goto st0;
671
+ st12:
672
+ if ( ++p == pe )
673
+ goto _test_eof12;
674
+ case 12:
675
+ if ( (*p) == 108 )
676
+ goto st13;
677
+ goto st0;
678
+ st13:
679
+ if ( ++p == pe )
680
+ goto _test_eof13;
681
+ case 13:
682
+ if ( (*p) == 115 )
683
+ goto st14;
684
+ goto st0;
685
+ st14:
686
+ if ( ++p == pe )
687
+ goto _test_eof14;
688
+ case 14:
689
+ if ( (*p) == 101 )
690
+ goto tr22;
691
+ goto st0;
692
+ st15:
693
+ if ( ++p == pe )
694
+ goto _test_eof15;
695
+ case 15:
696
+ if ( (*p) == 117 )
697
+ goto st16;
698
+ goto st0;
699
+ st16:
700
+ if ( ++p == pe )
701
+ goto _test_eof16;
702
+ case 16:
703
+ if ( (*p) == 108 )
704
+ goto st17;
705
+ goto st0;
706
+ st17:
707
+ if ( ++p == pe )
708
+ goto _test_eof17;
709
+ case 17:
710
+ if ( (*p) == 108 )
711
+ goto tr25;
712
+ goto st0;
713
+ st18:
714
+ if ( ++p == pe )
715
+ goto _test_eof18;
716
+ case 18:
717
+ if ( (*p) == 114 )
718
+ goto st19;
719
+ goto st0;
720
+ st19:
721
+ if ( ++p == pe )
722
+ goto _test_eof19;
723
+ case 19:
724
+ if ( (*p) == 117 )
725
+ goto st20;
726
+ goto st0;
727
+ st20:
728
+ if ( ++p == pe )
729
+ goto _test_eof20;
730
+ case 20:
731
+ if ( (*p) == 101 )
732
+ goto tr28;
733
+ goto st0;
734
+ }
735
+ _test_eof21: cs = 21; goto _test_eof;
736
+ _test_eof2: cs = 2; goto _test_eof;
737
+ _test_eof3: cs = 3; goto _test_eof;
738
+ _test_eof4: cs = 4; goto _test_eof;
739
+ _test_eof5: cs = 5; goto _test_eof;
740
+ _test_eof6: cs = 6; goto _test_eof;
741
+ _test_eof7: cs = 7; goto _test_eof;
742
+ _test_eof8: cs = 8; goto _test_eof;
743
+ _test_eof9: cs = 9; goto _test_eof;
744
+ _test_eof10: cs = 10; goto _test_eof;
745
+ _test_eof11: cs = 11; goto _test_eof;
746
+ _test_eof12: cs = 12; goto _test_eof;
747
+ _test_eof13: cs = 13; goto _test_eof;
748
+ _test_eof14: cs = 14; goto _test_eof;
749
+ _test_eof15: cs = 15; goto _test_eof;
750
+ _test_eof16: cs = 16; goto _test_eof;
751
+ _test_eof17: cs = 17; goto _test_eof;
752
+ _test_eof18: cs = 18; goto _test_eof;
753
+ _test_eof19: cs = 19; goto _test_eof;
754
+ _test_eof20: cs = 20; goto _test_eof;
755
+
756
+ _test_eof: {}
757
+ _out: {}
758
+ }
759
+
760
+ #line 270 "parser.rl"
761
+
762
+ if (cs >= JSON_value_first_final) {
763
+ return p;
764
+ } else {
765
+ return NULL;
766
+ }
767
+ }
768
+
769
+
770
+ #line 771 "parser.c"
771
+ static const int JSON_integer_start = 1;
772
+ static const int JSON_integer_first_final = 5;
773
+ static const int JSON_integer_error = 0;
774
+
775
+ static const int JSON_integer_en_main = 1;
776
+
777
+
778
+ #line 286 "parser.rl"
779
+
780
+
781
+ static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
782
+ {
783
+ int cs = EVIL;
784
+
785
+
786
+ #line 787 "parser.c"
787
+ {
788
+ cs = JSON_integer_start;
789
+ }
790
+
791
+ #line 293 "parser.rl"
792
+ json->memo = p;
793
+
794
+ #line 795 "parser.c"
795
+ {
796
+ if ( p == pe )
797
+ goto _test_eof;
798
+ switch ( cs )
799
+ {
800
+ case 1:
801
+ switch( (*p) ) {
802
+ case 45: goto st2;
803
+ case 48: goto st3;
804
+ }
805
+ if ( 49 <= (*p) && (*p) <= 57 )
806
+ goto st4;
807
+ goto st0;
808
+ st0:
809
+ cs = 0;
810
+ goto _out;
811
+ st2:
812
+ if ( ++p == pe )
813
+ goto _test_eof2;
814
+ case 2:
815
+ if ( (*p) == 48 )
816
+ goto st3;
817
+ if ( 49 <= (*p) && (*p) <= 57 )
818
+ goto st4;
819
+ goto st0;
820
+ st3:
821
+ if ( ++p == pe )
822
+ goto _test_eof3;
823
+ case 3:
824
+ if ( 48 <= (*p) && (*p) <= 57 )
825
+ goto st0;
826
+ goto tr4;
827
+ tr4:
828
+ #line 283 "parser.rl"
829
+ { p--; {p++; cs = 5; goto _out;} }
830
+ goto st5;
831
+ st5:
832
+ if ( ++p == pe )
833
+ goto _test_eof5;
834
+ case 5:
835
+ #line 836 "parser.c"
836
+ goto st0;
837
+ st4:
838
+ if ( ++p == pe )
839
+ goto _test_eof4;
840
+ case 4:
841
+ if ( 48 <= (*p) && (*p) <= 57 )
842
+ goto st4;
843
+ goto tr4;
844
+ }
845
+ _test_eof2: cs = 2; goto _test_eof;
846
+ _test_eof3: cs = 3; goto _test_eof;
847
+ _test_eof5: cs = 5; goto _test_eof;
848
+ _test_eof4: cs = 4; goto _test_eof;
849
+
850
+ _test_eof: {}
851
+ _out: {}
852
+ }
853
+
854
+ #line 295 "parser.rl"
855
+
856
+ if (cs >= JSON_integer_first_final) {
857
+ long len = p - json->memo;
858
+ *result = rb_Integer(rb_str_new(json->memo, len));
859
+ return p + 1;
860
+ } else {
861
+ return NULL;
862
+ }
863
+ }
864
+
865
+
866
+ #line 867 "parser.c"
867
+ static const int JSON_float_start = 1;
868
+ static const int JSON_float_first_final = 10;
869
+ static const int JSON_float_error = 0;
870
+
871
+ static const int JSON_float_en_main = 1;
872
+
873
+
874
+ #line 317 "parser.rl"
875
+
876
+
877
+ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
878
+ {
879
+ int cs = EVIL;
880
+
881
+
882
+ #line 883 "parser.c"
883
+ {
884
+ cs = JSON_float_start;
885
+ }
886
+
887
+ #line 324 "parser.rl"
888
+ json->memo = p;
889
+
890
+ #line 891 "parser.c"
891
+ {
892
+ if ( p == pe )
893
+ goto _test_eof;
894
+ switch ( cs )
895
+ {
896
+ case 1:
897
+ switch( (*p) ) {
898
+ case 45: goto st2;
899
+ case 48: goto st3;
900
+ }
901
+ if ( 49 <= (*p) && (*p) <= 57 )
902
+ goto st9;
903
+ goto st0;
904
+ st0:
905
+ cs = 0;
906
+ goto _out;
907
+ st2:
908
+ if ( ++p == pe )
909
+ goto _test_eof2;
910
+ case 2:
911
+ if ( (*p) == 48 )
912
+ goto st3;
913
+ if ( 49 <= (*p) && (*p) <= 57 )
914
+ goto st9;
915
+ goto st0;
916
+ st3:
917
+ if ( ++p == pe )
918
+ goto _test_eof3;
919
+ case 3:
920
+ switch( (*p) ) {
921
+ case 46: goto st4;
922
+ case 69: goto st6;
923
+ case 101: goto st6;
924
+ }
925
+ goto st0;
926
+ st4:
927
+ if ( ++p == pe )
928
+ goto _test_eof4;
929
+ case 4:
930
+ if ( 48 <= (*p) && (*p) <= 57 )
931
+ goto st5;
932
+ goto st0;
933
+ st5:
934
+ if ( ++p == pe )
935
+ goto _test_eof5;
936
+ case 5:
937
+ switch( (*p) ) {
938
+ case 69: goto st6;
939
+ case 101: goto st6;
940
+ }
941
+ if ( (*p) > 46 ) {
942
+ if ( 48 <= (*p) && (*p) <= 57 )
943
+ goto st5;
944
+ } else if ( (*p) >= 45 )
945
+ goto st0;
946
+ goto tr7;
947
+ tr7:
948
+ #line 311 "parser.rl"
949
+ { p--; {p++; cs = 10; goto _out;} }
950
+ goto st10;
951
+ st10:
952
+ if ( ++p == pe )
953
+ goto _test_eof10;
954
+ case 10:
955
+ #line 956 "parser.c"
956
+ goto st0;
957
+ st6:
958
+ if ( ++p == pe )
959
+ goto _test_eof6;
960
+ case 6:
961
+ switch( (*p) ) {
962
+ case 43: goto st7;
963
+ case 45: goto st7;
964
+ }
965
+ if ( 48 <= (*p) && (*p) <= 57 )
966
+ goto st8;
967
+ goto st0;
968
+ st7:
969
+ if ( ++p == pe )
970
+ goto _test_eof7;
971
+ case 7:
972
+ if ( 48 <= (*p) && (*p) <= 57 )
973
+ goto st8;
974
+ goto st0;
975
+ st8:
976
+ if ( ++p == pe )
977
+ goto _test_eof8;
978
+ case 8:
979
+ switch( (*p) ) {
980
+ case 69: goto st0;
981
+ case 101: goto st0;
982
+ }
983
+ if ( (*p) > 46 ) {
984
+ if ( 48 <= (*p) && (*p) <= 57 )
985
+ goto st8;
986
+ } else if ( (*p) >= 45 )
987
+ goto st0;
988
+ goto tr7;
989
+ st9:
990
+ if ( ++p == pe )
991
+ goto _test_eof9;
992
+ case 9:
993
+ switch( (*p) ) {
994
+ case 46: goto st4;
995
+ case 69: goto st6;
996
+ case 101: goto st6;
997
+ }
998
+ if ( 48 <= (*p) && (*p) <= 57 )
999
+ goto st9;
1000
+ goto st0;
1001
+ }
1002
+ _test_eof2: cs = 2; goto _test_eof;
1003
+ _test_eof3: cs = 3; goto _test_eof;
1004
+ _test_eof4: cs = 4; goto _test_eof;
1005
+ _test_eof5: cs = 5; goto _test_eof;
1006
+ _test_eof10: cs = 10; goto _test_eof;
1007
+ _test_eof6: cs = 6; goto _test_eof;
1008
+ _test_eof7: cs = 7; goto _test_eof;
1009
+ _test_eof8: cs = 8; goto _test_eof;
1010
+ _test_eof9: cs = 9; goto _test_eof;
1011
+
1012
+ _test_eof: {}
1013
+ _out: {}
1014
+ }
1015
+
1016
+ #line 326 "parser.rl"
1017
+
1018
+ if (cs >= JSON_float_first_final) {
1019
+ long len = p - json->memo;
1020
+ *result = rb_Float(rb_str_new(json->memo, len));
1021
+ return p + 1;
1022
+ } else {
1023
+ return NULL;
1024
+ }
1025
+ }
1026
+
1027
+
1028
+
1029
+ #line 1030 "parser.c"
1030
+ static const int JSON_array_start = 1;
1031
+ static const int JSON_array_first_final = 17;
1032
+ static const int JSON_array_error = 0;
1033
+
1034
+ static const int JSON_array_en_main = 1;
1035
+
1036
+
1037
+ #line 366 "parser.rl"
1038
+
1039
+
1040
+ static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
1041
+ {
1042
+ int cs = EVIL;
1043
+ VALUE array_class = json->array_class;
1044
+
1045
+ if (json->max_nesting && json->current_nesting > json->max_nesting) {
1046
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
1047
+ }
1048
+ *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
1049
+
1050
+
1051
+ #line 1052 "parser.c"
1052
+ {
1053
+ cs = JSON_array_start;
1054
+ }
1055
+
1056
+ #line 379 "parser.rl"
1057
+
1058
+ #line 1059 "parser.c"
1059
+ {
1060
+ if ( p == pe )
1061
+ goto _test_eof;
1062
+ switch ( cs )
1063
+ {
1064
+ case 1:
1065
+ if ( (*p) == 91 )
1066
+ goto st2;
1067
+ goto st0;
1068
+ st0:
1069
+ cs = 0;
1070
+ goto _out;
1071
+ st2:
1072
+ if ( ++p == pe )
1073
+ goto _test_eof2;
1074
+ case 2:
1075
+ switch( (*p) ) {
1076
+ case 13: goto st2;
1077
+ case 32: goto st2;
1078
+ case 34: goto tr2;
1079
+ case 45: goto tr2;
1080
+ case 47: goto st13;
1081
+ case 73: goto tr2;
1082
+ case 78: goto tr2;
1083
+ case 91: goto tr2;
1084
+ case 93: goto tr4;
1085
+ case 102: goto tr2;
1086
+ case 110: goto tr2;
1087
+ case 116: goto tr2;
1088
+ case 123: goto tr2;
1089
+ }
1090
+ if ( (*p) > 10 ) {
1091
+ if ( 48 <= (*p) && (*p) <= 57 )
1092
+ goto tr2;
1093
+ } else if ( (*p) >= 9 )
1094
+ goto st2;
1095
+ goto st0;
1096
+ tr2:
1097
+ #line 343 "parser.rl"
1098
+ {
1099
+ VALUE v = Qnil;
1100
+ const char *np = JSON_parse_value(json, p, pe, &v);
1101
+ if (np == NULL) {
1102
+ p--; {p++; cs = 3; goto _out;}
1103
+ } else {
1104
+ if (NIL_P(json->array_class)) {
1105
+ rb_ary_push(*result, v);
1106
+ } else {
1107
+ rb_funcall(*result, i_leftshift, 1, v);
1108
+ }
1109
+ {p = (( np))-1;}
1110
+ }
1111
+ }
1112
+ goto st3;
1113
+ st3:
1114
+ if ( ++p == pe )
1115
+ goto _test_eof3;
1116
+ case 3:
1117
+ #line 1118 "parser.c"
1118
+ switch( (*p) ) {
1119
+ case 13: goto st3;
1120
+ case 32: goto st3;
1121
+ case 44: goto st4;
1122
+ case 47: goto st9;
1123
+ case 93: goto tr4;
1124
+ }
1125
+ if ( 9 <= (*p) && (*p) <= 10 )
1126
+ goto st3;
1127
+ goto st0;
1128
+ st4:
1129
+ if ( ++p == pe )
1130
+ goto _test_eof4;
1131
+ case 4:
1132
+ switch( (*p) ) {
1133
+ case 13: goto st4;
1134
+ case 32: goto st4;
1135
+ case 34: goto tr2;
1136
+ case 45: goto tr2;
1137
+ case 47: goto st5;
1138
+ case 73: goto tr2;
1139
+ case 78: goto tr2;
1140
+ case 91: goto tr2;
1141
+ case 102: goto tr2;
1142
+ case 110: goto tr2;
1143
+ case 116: goto tr2;
1144
+ case 123: goto tr2;
1145
+ }
1146
+ if ( (*p) > 10 ) {
1147
+ if ( 48 <= (*p) && (*p) <= 57 )
1148
+ goto tr2;
1149
+ } else if ( (*p) >= 9 )
1150
+ goto st4;
1151
+ goto st0;
1152
+ st5:
1153
+ if ( ++p == pe )
1154
+ goto _test_eof5;
1155
+ case 5:
1156
+ switch( (*p) ) {
1157
+ case 42: goto st6;
1158
+ case 47: goto st8;
1159
+ }
1160
+ goto st0;
1161
+ st6:
1162
+ if ( ++p == pe )
1163
+ goto _test_eof6;
1164
+ case 6:
1165
+ if ( (*p) == 42 )
1166
+ goto st7;
1167
+ goto st6;
1168
+ st7:
1169
+ if ( ++p == pe )
1170
+ goto _test_eof7;
1171
+ case 7:
1172
+ switch( (*p) ) {
1173
+ case 42: goto st7;
1174
+ case 47: goto st4;
1175
+ }
1176
+ goto st6;
1177
+ st8:
1178
+ if ( ++p == pe )
1179
+ goto _test_eof8;
1180
+ case 8:
1181
+ if ( (*p) == 10 )
1182
+ goto st4;
1183
+ goto st8;
1184
+ st9:
1185
+ if ( ++p == pe )
1186
+ goto _test_eof9;
1187
+ case 9:
1188
+ switch( (*p) ) {
1189
+ case 42: goto st10;
1190
+ case 47: goto st12;
1191
+ }
1192
+ goto st0;
1193
+ st10:
1194
+ if ( ++p == pe )
1195
+ goto _test_eof10;
1196
+ case 10:
1197
+ if ( (*p) == 42 )
1198
+ goto st11;
1199
+ goto st10;
1200
+ st11:
1201
+ if ( ++p == pe )
1202
+ goto _test_eof11;
1203
+ case 11:
1204
+ switch( (*p) ) {
1205
+ case 42: goto st11;
1206
+ case 47: goto st3;
1207
+ }
1208
+ goto st10;
1209
+ st12:
1210
+ if ( ++p == pe )
1211
+ goto _test_eof12;
1212
+ case 12:
1213
+ if ( (*p) == 10 )
1214
+ goto st3;
1215
+ goto st12;
1216
+ tr4:
1217
+ #line 358 "parser.rl"
1218
+ { p--; {p++; cs = 17; goto _out;} }
1219
+ goto st17;
1220
+ st17:
1221
+ if ( ++p == pe )
1222
+ goto _test_eof17;
1223
+ case 17:
1224
+ #line 1225 "parser.c"
1225
+ goto st0;
1226
+ st13:
1227
+ if ( ++p == pe )
1228
+ goto _test_eof13;
1229
+ case 13:
1230
+ switch( (*p) ) {
1231
+ case 42: goto st14;
1232
+ case 47: goto st16;
1233
+ }
1234
+ goto st0;
1235
+ st14:
1236
+ if ( ++p == pe )
1237
+ goto _test_eof14;
1238
+ case 14:
1239
+ if ( (*p) == 42 )
1240
+ goto st15;
1241
+ goto st14;
1242
+ st15:
1243
+ if ( ++p == pe )
1244
+ goto _test_eof15;
1245
+ case 15:
1246
+ switch( (*p) ) {
1247
+ case 42: goto st15;
1248
+ case 47: goto st2;
1249
+ }
1250
+ goto st14;
1251
+ st16:
1252
+ if ( ++p == pe )
1253
+ goto _test_eof16;
1254
+ case 16:
1255
+ if ( (*p) == 10 )
1256
+ goto st2;
1257
+ goto st16;
1258
+ }
1259
+ _test_eof2: cs = 2; goto _test_eof;
1260
+ _test_eof3: cs = 3; goto _test_eof;
1261
+ _test_eof4: cs = 4; goto _test_eof;
1262
+ _test_eof5: cs = 5; goto _test_eof;
1263
+ _test_eof6: cs = 6; goto _test_eof;
1264
+ _test_eof7: cs = 7; goto _test_eof;
1265
+ _test_eof8: cs = 8; goto _test_eof;
1266
+ _test_eof9: cs = 9; goto _test_eof;
1267
+ _test_eof10: cs = 10; goto _test_eof;
1268
+ _test_eof11: cs = 11; goto _test_eof;
1269
+ _test_eof12: cs = 12; goto _test_eof;
1270
+ _test_eof17: cs = 17; goto _test_eof;
1271
+ _test_eof13: cs = 13; goto _test_eof;
1272
+ _test_eof14: cs = 14; goto _test_eof;
1273
+ _test_eof15: cs = 15; goto _test_eof;
1274
+ _test_eof16: cs = 16; goto _test_eof;
1275
+
1276
+ _test_eof: {}
1277
+ _out: {}
1278
+ }
1279
+
1280
+ #line 380 "parser.rl"
1281
+
1282
+ if(cs >= JSON_array_first_final) {
1283
+ return p + 1;
1284
+ } else {
1285
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1286
+ return NULL;
1287
+ }
1288
+ }
1289
+
1290
+ static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd)
1291
+ {
1292
+ const char *p = string, *pe = string, *unescape;
1293
+ int unescape_len;
1294
+
1295
+ while (pe < stringEnd) {
1296
+ if (*pe == '\\') {
1297
+ unescape = (char *) "?";
1298
+ unescape_len = 1;
1299
+ if (pe > p) rb_str_buf_cat(result, p, pe - p);
1300
+ switch (*++pe) {
1301
+ case 'n':
1302
+ unescape = (char *) "\n";
1303
+ break;
1304
+ case 'r':
1305
+ unescape = (char *) "\r";
1306
+ break;
1307
+ case 't':
1308
+ unescape = (char *) "\t";
1309
+ break;
1310
+ case '"':
1311
+ unescape = (char *) "\"";
1312
+ break;
1313
+ case '\\':
1314
+ unescape = (char *) "\\";
1315
+ break;
1316
+ case 'b':
1317
+ unescape = (char *) "\b";
1318
+ break;
1319
+ case 'f':
1320
+ unescape = (char *) "\f";
1321
+ break;
1322
+ case 'u':
1323
+ if (pe > stringEnd - 4) {
1324
+ return Qnil;
1325
+ } else {
1326
+ char buf[4];
1327
+ UTF32 ch = unescape_unicode((unsigned char *) ++pe);
1328
+ pe += 3;
1329
+ if (UNI_SUR_HIGH_START == (ch & 0xFC00)) {
1330
+ pe++;
1331
+ if (pe > stringEnd - 6) return Qnil;
1332
+ if (pe[0] == '\\' && pe[1] == 'u') {
1333
+ UTF32 sur = unescape_unicode((unsigned char *) pe + 2);
1334
+ ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
1335
+ | (sur & 0x3FF));
1336
+ pe += 5;
1337
+ } else {
1338
+ unescape = (char *) "?";
1339
+ break;
1340
+ }
1341
+ }
1342
+ unescape_len = convert_UTF32_to_UTF8(buf, ch);
1343
+ unescape = buf;
1344
+ }
1345
+ break;
1346
+ default:
1347
+ p = pe;
1348
+ continue;
1349
+ }
1350
+ rb_str_buf_cat(result, unescape, unescape_len);
1351
+ p = ++pe;
1352
+ } else {
1353
+ pe++;
1354
+ }
1355
+ }
1356
+ rb_str_buf_cat(result, p, pe - p);
1357
+ return result;
1358
+ }
1359
+
1360
+
1361
+ #line 1362 "parser.c"
1362
+ static const int JSON_string_start = 1;
1363
+ static const int JSON_string_first_final = 8;
1364
+ static const int JSON_string_error = 0;
1365
+
1366
+ static const int JSON_string_en_main = 1;
1367
+
1368
+
1369
+ #line 479 "parser.rl"
1370
+
1371
+
1372
+ static int
1373
+ match_i(VALUE regexp, VALUE klass, VALUE memo)
1374
+ {
1375
+ if (regexp == Qundef) return ST_STOP;
1376
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
1377
+ RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
1378
+ rb_ary_push(memo, klass);
1379
+ return ST_STOP;
1380
+ }
1381
+ return ST_CONTINUE;
1382
+ }
1383
+
1384
+ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
1385
+ {
1386
+ int cs = EVIL;
1387
+ *result = rb_str_buf_new(0);
1388
+
1389
+ #line 1390 "parser.c"
1390
+ {
1391
+ cs = JSON_string_start;
1392
+ }
1393
+
1394
+ #line 498 "parser.rl"
1395
+ json->memo = p;
1396
+
1397
+ #line 1398 "parser.c"
1398
+ {
1399
+ if ( p == pe )
1400
+ goto _test_eof;
1401
+ switch ( cs )
1402
+ {
1403
+ case 1:
1404
+ if ( (*p) == 34 )
1405
+ goto st2;
1406
+ goto st0;
1407
+ st0:
1408
+ cs = 0;
1409
+ goto _out;
1410
+ st2:
1411
+ if ( ++p == pe )
1412
+ goto _test_eof2;
1413
+ case 2:
1414
+ switch( (*p) ) {
1415
+ case 34: goto tr2;
1416
+ case 92: goto st3;
1417
+ }
1418
+ if ( 0 <= (*p) && (*p) <= 31 )
1419
+ goto st0;
1420
+ goto st2;
1421
+ tr2:
1422
+ #line 465 "parser.rl"
1423
+ {
1424
+ *result = json_string_unescape(*result, json->memo + 1, p);
1425
+ if (NIL_P(*result)) {
1426
+ p--;
1427
+ {p++; cs = 8; goto _out;}
1428
+ } else {
1429
+ FORCE_UTF8(*result);
1430
+ {p = (( p + 1))-1;}
1431
+ }
1432
+ }
1433
+ #line 476 "parser.rl"
1434
+ { p--; {p++; cs = 8; goto _out;} }
1435
+ goto st8;
1436
+ st8:
1437
+ if ( ++p == pe )
1438
+ goto _test_eof8;
1439
+ case 8:
1440
+ #line 1441 "parser.c"
1441
+ goto st0;
1442
+ st3:
1443
+ if ( ++p == pe )
1444
+ goto _test_eof3;
1445
+ case 3:
1446
+ if ( (*p) == 117 )
1447
+ goto st4;
1448
+ if ( 0 <= (*p) && (*p) <= 31 )
1449
+ goto st0;
1450
+ goto st2;
1451
+ st4:
1452
+ if ( ++p == pe )
1453
+ goto _test_eof4;
1454
+ case 4:
1455
+ if ( (*p) < 65 ) {
1456
+ if ( 48 <= (*p) && (*p) <= 57 )
1457
+ goto st5;
1458
+ } else if ( (*p) > 70 ) {
1459
+ if ( 97 <= (*p) && (*p) <= 102 )
1460
+ goto st5;
1461
+ } else
1462
+ goto st5;
1463
+ goto st0;
1464
+ st5:
1465
+ if ( ++p == pe )
1466
+ goto _test_eof5;
1467
+ case 5:
1468
+ if ( (*p) < 65 ) {
1469
+ if ( 48 <= (*p) && (*p) <= 57 )
1470
+ goto st6;
1471
+ } else if ( (*p) > 70 ) {
1472
+ if ( 97 <= (*p) && (*p) <= 102 )
1473
+ goto st6;
1474
+ } else
1475
+ goto st6;
1476
+ goto st0;
1477
+ st6:
1478
+ if ( ++p == pe )
1479
+ goto _test_eof6;
1480
+ case 6:
1481
+ if ( (*p) < 65 ) {
1482
+ if ( 48 <= (*p) && (*p) <= 57 )
1483
+ goto st7;
1484
+ } else if ( (*p) > 70 ) {
1485
+ if ( 97 <= (*p) && (*p) <= 102 )
1486
+ goto st7;
1487
+ } else
1488
+ goto st7;
1489
+ goto st0;
1490
+ st7:
1491
+ if ( ++p == pe )
1492
+ goto _test_eof7;
1493
+ case 7:
1494
+ if ( (*p) < 65 ) {
1495
+ if ( 48 <= (*p) && (*p) <= 57 )
1496
+ goto st2;
1497
+ } else if ( (*p) > 70 ) {
1498
+ if ( 97 <= (*p) && (*p) <= 102 )
1499
+ goto st2;
1500
+ } else
1501
+ goto st2;
1502
+ goto st0;
1503
+ }
1504
+ _test_eof2: cs = 2; goto _test_eof;
1505
+ _test_eof8: cs = 8; goto _test_eof;
1506
+ _test_eof3: cs = 3; goto _test_eof;
1507
+ _test_eof4: cs = 4; goto _test_eof;
1508
+ _test_eof5: cs = 5; goto _test_eof;
1509
+ _test_eof6: cs = 6; goto _test_eof;
1510
+ _test_eof7: cs = 7; goto _test_eof;
1511
+
1512
+ _test_eof: {}
1513
+ _out: {}
1514
+ }
1515
+
1516
+ #line 500 "parser.rl"
1517
+
1518
+ if (json->create_additions) {
1519
+ VALUE match_string = json->match_string;
1520
+ if (RTEST(match_string)) {
1521
+ VALUE klass;
1522
+ VALUE memo = rb_ary_new2(2);
1523
+ rb_ary_push(memo, *result);
1524
+ rb_hash_foreach(match_string, match_i, memo);
1525
+ klass = rb_ary_entry(memo, 1);
1526
+ if (RTEST(klass)) {
1527
+ *result = rb_funcall(klass, i_json_create, 1, *result);
1528
+ }
1529
+ }
1530
+ }
1531
+
1532
+ if (json->symbolize_names && json->parsing_name) {
1533
+ *result = rb_str_intern(*result);
1534
+ }
1535
+ if (cs >= JSON_string_first_final) {
1536
+ return p + 1;
1537
+ } else {
1538
+ return NULL;
1539
+ }
1540
+ }
1541
+
1542
+
1543
+
1544
+ #line 1545 "parser.c"
1545
+ static const int JSON_start = 1;
1546
+ static const int JSON_first_final = 10;
1547
+ static const int JSON_error = 0;
1548
+
1549
+ static const int JSON_en_main = 1;
1550
+
1551
+
1552
+ #line 551 "parser.rl"
1553
+
1554
+
1555
+ /*
1556
+ * Document-class: JSON::Ext::Parser
1557
+ *
1558
+ * This is the JSON parser implemented as a C extension. It can be configured
1559
+ * to be used by setting
1560
+ *
1561
+ * JSON.parser = JSON::Ext::Parser
1562
+ *
1563
+ * with the method parser= in JSON.
1564
+ *
1565
+ */
1566
+
1567
+ static VALUE convert_encoding(VALUE source)
1568
+ {
1569
+ const char *ptr = RSTRING_PTR(source);
1570
+ long len = RSTRING_LEN(source);
1571
+ if (len < 2) {
1572
+ rb_raise(eParserError, "A JSON text must at least contain two octets!");
1573
+ }
1574
+ #ifdef HAVE_RUBY_ENCODING_H
1575
+ {
1576
+ VALUE encoding = rb_funcall(source, i_encoding, 0);
1577
+ if (encoding == CEncoding_ASCII_8BIT) {
1578
+ if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
1579
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32BE);
1580
+ } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
1581
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16BE);
1582
+ } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
1583
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32LE);
1584
+ } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
1585
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16LE);
1586
+ } else {
1587
+ source = rb_str_dup(source);
1588
+ FORCE_UTF8(source);
1589
+ }
1590
+ } else {
1591
+ source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
1592
+ }
1593
+ }
1594
+ #else
1595
+ if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
1596
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32be"), source);
1597
+ } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
1598
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16be"), source);
1599
+ } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
1600
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32le"), source);
1601
+ } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
1602
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16le"), source);
1603
+ }
1604
+ #endif
1605
+ return source;
1606
+ }
1607
+
1608
+ static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v)
1609
+ {
1610
+ // store reference to v in a Ruby instVar to keep v alive
1611
+ // without using a gc_mark function in Data_Wrap_Struct calls
1612
+ rb_iv_set(json->dwrapped_parser, iv_name, v);
1613
+ }
1614
+
1615
+ /*
1616
+ * call-seq: new(source, opts => {})
1617
+ *
1618
+ * Creates a new JSON::Ext::Parser instance for the string _source_.
1619
+ *
1620
+ * Creates a new JSON::Ext::Parser instance for the string _source_.
1621
+ *
1622
+ * It will be configured by the _opts_ hash. _opts_ can have the following
1623
+ * keys:
1624
+ *
1625
+ * _opts_ can have the following keys:
1626
+ * * *max_nesting*: The maximum depth of nesting allowed in the parsed data
1627
+ * structures. Disable depth checking with :max_nesting => false|nil|0, it
1628
+ * defaults to 19.
1629
+ * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
1630
+ * defiance of RFC 4627 to be parsed by the Parser. This option defaults to
1631
+ * false.
1632
+ * * *symbolize_names*: If set to true, returns symbols for the names
1633
+ * (keys) in a JSON object. Otherwise strings are returned, which is also
1634
+ * the default.
1635
+ * * *create_additions*: If set to false, the Parser doesn't create
1636
+ * additions even if a matchin class and create_id was found. This option
1637
+ * defaults to true.
1638
+ * * *object_class*: Defaults to Hash
1639
+ * * *array_class*: Defaults to Array
1640
+ */
1641
+
1642
+ static int init_count = 0;
1643
+ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1644
+ {
1645
+ const char *ptr;
1646
+ long len;
1647
+ VALUE source, opts;
1648
+
1649
+ GET_PARSER_INIT;
1650
+ init_count += 1;
1651
+
1652
+ #ifndef MAGLEV
1653
+ if (json->Vsource) {
1654
+ rb_raise(rb_eTypeError, "already initialized instance");
1655
+ }
1656
+ #endif
1657
+ rb_scan_args(argc, argv, "11", &source, &opts);
1658
+ source = convert_encoding(StringValue(source));
1659
+ ptr = RSTRING_PTR(source);
1660
+ len = RSTRING_LEN(source);
1661
+ if (!NIL_P(opts)) {
1662
+ opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
1663
+ if (NIL_P(opts)) {
1664
+ rb_raise(rb_eArgError, "opts needs to be like a hash");
1665
+ } else {
1666
+ VALUE tmp = ID2SYM(i_max_nesting);
1667
+ if (option_given_p(opts, tmp)) {
1668
+ VALUE max_nesting = rb_hash_aref(opts, tmp);
1669
+ if (RTEST(max_nesting)) {
1670
+ Check_Type(max_nesting, T_FIXNUM);
1671
+ json->max_nesting = FIX2INT(max_nesting);
1672
+ } else {
1673
+ json->max_nesting = 0;
1674
+ }
1675
+ } else {
1676
+ json->max_nesting = 19;
1677
+ }
1678
+ tmp = ID2SYM(i_allow_nan);
1679
+ if (option_given_p(opts, tmp)) {
1680
+ json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
1681
+ } else {
1682
+ json->allow_nan = 0;
1683
+ }
1684
+ tmp = ID2SYM(i_symbolize_names);
1685
+ if (option_given_p(opts, tmp)) {
1686
+ json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
1687
+ } else {
1688
+ json->symbolize_names = 0;
1689
+ }
1690
+ tmp = ID2SYM(i_create_additions);
1691
+ if (option_given_p(opts, tmp)) {
1692
+ json->create_additions = RTEST(rb_hash_aref(opts, tmp));
1693
+ } else {
1694
+ json->create_additions = 1;
1695
+ }
1696
+ tmp = ID2SYM(i_create_id);
1697
+ if (option_given_p(opts, tmp)) {
1698
+ json->create_id = rb_hash_aref(opts, tmp);
1699
+ } else {
1700
+ json->create_id = rb_funcall(mJSON, i_create_id, 0);
1701
+ }
1702
+ parser_iv_set(json, "@create_id", json->create_id);
1703
+ tmp = ID2SYM(i_object_class);
1704
+ if (option_given_p(opts, tmp)) {
1705
+ json->object_class = rb_hash_aref(opts, tmp);
1706
+ parser_iv_set(json, "@object_class", json->object_class);
1707
+ }
1708
+ tmp = ID2SYM(i_array_class);
1709
+ if (option_given_p(opts, tmp)) {
1710
+ json->array_class = rb_hash_aref(opts, tmp);
1711
+ parser_iv_set(json, "@array_class", json->array_class);
1712
+ }
1713
+ tmp = ID2SYM(i_match_string);
1714
+ if (option_given_p(opts, tmp)) {
1715
+ VALUE match_string = rb_hash_aref(opts, tmp);
1716
+ json->match_string = RTEST(match_string) ? match_string : Qnil;
1717
+ parser_iv_set(json, "@match_string", json->match_string);
1718
+ }
1719
+ }
1720
+ } else {
1721
+ json->max_nesting = 19;
1722
+ json->allow_nan = 0;
1723
+ json->create_additions = 1;
1724
+ json->create_id = rb_funcall(mJSON, i_create_id, 0);
1725
+ parser_iv_set(json, "@create_id", json->create_id);
1726
+ }
1727
+ json->current_nesting = 0;
1728
+ json->len = len;
1729
+ json->source = ptr;
1730
+ json->Vsource = source;
1731
+ parser_iv_set(json, "@vsource", json->Vsource);
1732
+ return self;
1733
+ }
1734
+
1735
+ /*
1736
+ * call-seq: parse()
1737
+ *
1738
+ * Parses the current JSON text _source_ and returns the complete data
1739
+ * structure as a result.
1740
+ */
1741
+ static VALUE cParser_parse(VALUE self)
1742
+ {
1743
+ const char *p, *pe;
1744
+ int cs = EVIL;
1745
+ VALUE result = Qnil;
1746
+ GET_PARSER;
1747
+
1748
+
1749
+ #line 1748 "parser.c"
1750
+ {
1751
+ cs = JSON_start;
1752
+ }
1753
+
1754
+ #line 745 "parser.rl"
1755
+ p = json->source;
1756
+ pe = p + json->len;
1757
+
1758
+ #line 1757 "parser.c"
1759
+ {
1760
+ if ( p == pe )
1761
+ goto _test_eof;
1762
+ switch ( cs )
1763
+ {
1764
+ st1:
1765
+ if ( ++p == pe )
1766
+ goto _test_eof1;
1767
+ case 1:
1768
+ switch( (*p) ) {
1769
+ case 13: goto st1;
1770
+ case 32: goto st1;
1771
+ case 47: goto st2;
1772
+ case 91: goto tr3;
1773
+ case 123: goto tr4;
1774
+ }
1775
+ if ( 9 <= (*p) && (*p) <= 10 )
1776
+ goto st1;
1777
+ goto st0;
1778
+ st0:
1779
+ cs = 0;
1780
+ goto _out;
1781
+ st2:
1782
+ if ( ++p == pe )
1783
+ goto _test_eof2;
1784
+ case 2:
1785
+ switch( (*p) ) {
1786
+ case 42: goto st3;
1787
+ case 47: goto st5;
1788
+ }
1789
+ goto st0;
1790
+ st3:
1791
+ if ( ++p == pe )
1792
+ goto _test_eof3;
1793
+ case 3:
1794
+ if ( (*p) == 42 )
1795
+ goto st4;
1796
+ goto st3;
1797
+ st4:
1798
+ if ( ++p == pe )
1799
+ goto _test_eof4;
1800
+ case 4:
1801
+ switch( (*p) ) {
1802
+ case 42: goto st4;
1803
+ case 47: goto st1;
1804
+ }
1805
+ goto st3;
1806
+ st5:
1807
+ if ( ++p == pe )
1808
+ goto _test_eof5;
1809
+ case 5:
1810
+ if ( (*p) == 10 )
1811
+ goto st1;
1812
+ goto st5;
1813
+ tr3:
1814
+ #line 540 "parser.rl"
1815
+ {
1816
+ const char *np;
1817
+ json->current_nesting = 1;
1818
+ np = JSON_parse_array(json, p, pe, &result);
1819
+ if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
1820
+ }
1821
+ goto st10;
1822
+ tr4:
1823
+ #line 533 "parser.rl"
1824
+ {
1825
+ const char *np;
1826
+ json->current_nesting = 1;
1827
+ np = JSON_parse_object(json, p, pe, &result);
1828
+ if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
1829
+ }
1830
+ goto st10;
1831
+ st10:
1832
+ if ( ++p == pe )
1833
+ goto _test_eof10;
1834
+ case 10:
1835
+ #line 1834 "parser.c"
1836
+ switch( (*p) ) {
1837
+ case 13: goto st10;
1838
+ case 32: goto st10;
1839
+ case 47: goto st6;
1840
+ }
1841
+ if ( 9 <= (*p) && (*p) <= 10 )
1842
+ goto st10;
1843
+ goto st0;
1844
+ st6:
1845
+ if ( ++p == pe )
1846
+ goto _test_eof6;
1847
+ case 6:
1848
+ switch( (*p) ) {
1849
+ case 42: goto st7;
1850
+ case 47: goto st9;
1851
+ }
1852
+ goto st0;
1853
+ st7:
1854
+ if ( ++p == pe )
1855
+ goto _test_eof7;
1856
+ case 7:
1857
+ if ( (*p) == 42 )
1858
+ goto st8;
1859
+ goto st7;
1860
+ st8:
1861
+ if ( ++p == pe )
1862
+ goto _test_eof8;
1863
+ case 8:
1864
+ switch( (*p) ) {
1865
+ case 42: goto st8;
1866
+ case 47: goto st10;
1867
+ }
1868
+ goto st7;
1869
+ st9:
1870
+ if ( ++p == pe )
1871
+ goto _test_eof9;
1872
+ case 9:
1873
+ if ( (*p) == 10 )
1874
+ goto st10;
1875
+ goto st9;
1876
+ }
1877
+ _test_eof1: cs = 1; goto _test_eof;
1878
+ _test_eof2: cs = 2; goto _test_eof;
1879
+ _test_eof3: cs = 3; goto _test_eof;
1880
+ _test_eof4: cs = 4; goto _test_eof;
1881
+ _test_eof5: cs = 5; goto _test_eof;
1882
+ _test_eof10: cs = 10; goto _test_eof;
1883
+ _test_eof6: cs = 6; goto _test_eof;
1884
+ _test_eof7: cs = 7; goto _test_eof;
1885
+ _test_eof8: cs = 8; goto _test_eof;
1886
+ _test_eof9: cs = 9; goto _test_eof;
1887
+
1888
+ _test_eof: {}
1889
+ _out: {}
1890
+ }
1891
+
1892
+ #line 748 "parser.rl"
1893
+
1894
+ if (cs >= JSON_first_final && p == pe) {
1895
+ return result;
1896
+ } else {
1897
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1898
+ return Qnil;
1899
+ }
1900
+ }
1901
+
1902
+ static JSON_Parser *JSON_allocate()
1903
+ {
1904
+ JSON_Parser *json = ALLOC(JSON_Parser);
1905
+ MEMZERO(json, JSON_Parser, 1);
1906
+ json->dwrapped_parser = Qnil;
1907
+ json->Vsource = Qnil;
1908
+ json->create_id = Qnil;
1909
+ json->object_class = Qnil;
1910
+ json->array_class = Qnil;
1911
+ json->match_string = Qnil;
1912
+
1913
+ return json;
1914
+ }
1915
+
1916
+ /* deleted JSON_mark function */
1917
+
1918
+ static void JSON_free(JSON_Parser *json)
1919
+ {
1920
+ ruby_xfree(json);
1921
+ }
1922
+
1923
+ static VALUE cJSON_parser_s_allocate(VALUE klass)
1924
+ {
1925
+ JSON_Parser *json = JSON_allocate();
1926
+ VALUE data_obj = Data_Wrap_Struct(klass, NULL, JSON_free, json);
1927
+ json->dwrapped_parser = data_obj;
1928
+ return data_obj;
1929
+ }
1930
+
1931
+ /*
1932
+ * call-seq: source()
1933
+ *
1934
+ * Returns a copy of the current _source_ string, that was used to construct
1935
+ * this Parser.
1936
+ */
1937
+ static VALUE cParser_source(VALUE self)
1938
+ {
1939
+ GET_PARSER;
1940
+ return rb_str_dup(json->Vsource);
1941
+ }
1942
+
1943
+ void Init_parser()
1944
+ {
1945
+ rb_require("json/common");
1946
+ mJSON = rb_define_module("JSON");
1947
+ mExt = rb_define_module_under(mJSON, "Ext");
1948
+ cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
1949
+ eParserError = rb_path2class("JSON::ParserError");
1950
+ eNestingError = rb_path2class("JSON::NestingError");
1951
+ rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
1952
+ rb_define_method(cParser, "initialize", cParser_initialize, -1);
1953
+ rb_define_method(cParser, "parse", cParser_parse, 0);
1954
+ rb_define_method(cParser, "source", cParser_source, 0);
1955
+
1956
+ CNaN = rb_const_get(mJSON, rb_intern("NaN"));
1957
+ CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
1958
+ CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
1959
+
1960
+ i_json_creatable_p = rb_intern("json_creatable?");
1961
+ i_json_create = rb_intern("json_create");
1962
+ i_create_id = rb_intern("create_id");
1963
+ i_create_additions = rb_intern("create_additions");
1964
+ i_chr = rb_intern("chr");
1965
+ i_max_nesting = rb_intern("max_nesting");
1966
+ i_allow_nan = rb_intern("allow_nan");
1967
+ i_symbolize_names = rb_intern("symbolize_names");
1968
+ i_object_class = rb_intern("object_class");
1969
+ i_array_class = rb_intern("array_class");
1970
+ i_match = rb_intern("match");
1971
+ i_match_string = rb_intern("match_string");
1972
+ i_key_p = rb_intern("key?");
1973
+ i_deep_const_get = rb_intern("deep_const_get");
1974
+ i_aset = rb_intern("[]=");
1975
+ i_leftshift = rb_intern("<<");
1976
+ #ifdef HAVE_RUBY_ENCODING_H
1977
+ CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1978
+ CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
1979
+ CEncoding_UTF_16LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16le"));
1980
+ CEncoding_UTF_32BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32be"));
1981
+ CEncoding_UTF_32LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32le"));
1982
+ CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit"));
1983
+ i_encoding = rb_intern("encoding");
1984
+ i_encode = rb_intern("encode");
1985
+ #else
1986
+ i_iconv = rb_intern("iconv");
1987
+ #endif
1988
+ }
1989
+
1990
+ /*
1991
+ * Local variables:
1992
+ * mode: c
1993
+ * c-file-style: ruby
1994
+ * indent-tabs-mode: nil
1995
+ * End:
1996
+ */