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,82 @@
1
+ #ifndef _PARSER_H_
2
+ #define _PARSER_H_
3
+
4
+ #include "ruby.h"
5
+
6
+ #if HAVE_RE_H
7
+ #include "re.h"
8
+ #endif
9
+
10
+ #ifdef HAVE_RUBY_ENCODING_H
11
+ #include "ruby/encoding.h"
12
+ #define FORCE_UTF8(obj) ((obj) = rb_enc_associate(rb_str_dup(obj), rb_utf8_encoding()))
13
+ #else
14
+ #define FORCE_UTF8(obj)
15
+ #endif
16
+ #ifdef HAVE_RUBY_ST_H
17
+ #include "ruby/st.h"
18
+ #else
19
+ #include "st.h"
20
+ #endif
21
+
22
+ #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
23
+
24
+ /* unicode */
25
+
26
+ typedef unsigned long UTF32; /* at least 32 bits */
27
+ typedef unsigned short UTF16; /* at least 16 bits */
28
+ typedef unsigned char UTF8; /* typically 8 bits */
29
+
30
+ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
31
+ #define UNI_SUR_HIGH_START (UTF32)0xD800
32
+ #define UNI_SUR_HIGH_END (UTF32)0xDBFF
33
+ #define UNI_SUR_LOW_START (UTF32)0xDC00
34
+ #define UNI_SUR_LOW_END (UTF32)0xDFFF
35
+
36
+ typedef struct JSON_ParserStruct {
37
+ VALUE dwrapped_parser;
38
+ VALUE Vsource;
39
+ const char *source;
40
+ long len;
41
+ const char *memo;
42
+ VALUE create_id;
43
+ int max_nesting;
44
+ int current_nesting;
45
+ int allow_nan;
46
+ int parsing_name;
47
+ int symbolize_names;
48
+ VALUE object_class;
49
+ VALUE array_class;
50
+ int create_additions;
51
+ VALUE match_string;
52
+ } JSON_Parser;
53
+
54
+ #define GET_PARSER \
55
+ GET_PARSER_INIT; \
56
+ if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
57
+ #define GET_PARSER_INIT \
58
+ JSON_Parser *json; \
59
+ Data_Get_Struct(self, JSON_Parser, json)
60
+
61
+ #define MinusInfinity "-Infinity"
62
+ #define EVIL 0x666
63
+
64
+ static UTF32 unescape_unicode(const unsigned char *p);
65
+ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch);
66
+ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
67
+ static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
68
+ static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
69
+ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
70
+ static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
71
+ static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd);
72
+ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
73
+ static VALUE convert_encoding(VALUE source);
74
+ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
75
+ static VALUE cParser_parse(VALUE self);
76
+ static JSON_Parser *JSON_allocate();
77
+ static void JSON_mark(JSON_Parser *json);
78
+ static void JSON_free(JSON_Parser *json);
79
+ static VALUE cJSON_parser_s_allocate(VALUE klass);
80
+ static VALUE cParser_source(VALUE self);
81
+
82
+ #endif
@@ -0,0 +1,853 @@
1
+ #include "parser.h"
2
+
3
+ /* unicode */
4
+
5
+ static const char digit_values[256] = {
6
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
8
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
9
+ -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
10
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
11
+ 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -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
+ -1, -1, -1, -1, -1, -1, -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
20
+ };
21
+
22
+ static UTF32 unescape_unicode(const unsigned char *p)
23
+ {
24
+ char b;
25
+ UTF32 result = 0;
26
+ b = digit_values[p[0]];
27
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
28
+ result = (result << 4) | b;
29
+ b = digit_values[p[1]];
30
+ result = (result << 4) | b;
31
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
32
+ b = digit_values[p[2]];
33
+ result = (result << 4) | b;
34
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
35
+ b = digit_values[p[3]];
36
+ result = (result << 4) | b;
37
+ if (b < 0) return UNI_REPLACEMENT_CHAR;
38
+ return result;
39
+ }
40
+
41
+ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
42
+ {
43
+ int len = 1;
44
+ if (ch <= 0x7F) {
45
+ buf[0] = (char) ch;
46
+ } else if (ch <= 0x07FF) {
47
+ buf[0] = (char) ((ch >> 6) | 0xC0);
48
+ buf[1] = (char) ((ch & 0x3F) | 0x80);
49
+ len++;
50
+ } else if (ch <= 0xFFFF) {
51
+ buf[0] = (char) ((ch >> 12) | 0xE0);
52
+ buf[1] = (char) (((ch >> 6) & 0x3F) | 0x80);
53
+ buf[2] = (char) ((ch & 0x3F) | 0x80);
54
+ len += 2;
55
+ } else if (ch <= 0x1fffff) {
56
+ buf[0] =(char) ((ch >> 18) | 0xF0);
57
+ buf[1] =(char) (((ch >> 12) & 0x3F) | 0x80);
58
+ buf[2] =(char) (((ch >> 6) & 0x3F) | 0x80);
59
+ buf[3] =(char) ((ch & 0x3F) | 0x80);
60
+ len += 3;
61
+ } else {
62
+ buf[0] = '?';
63
+ }
64
+ return len;
65
+ }
66
+
67
+ #ifdef HAVE_RUBY_ENCODING_H
68
+ static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8, CEncoding_UTF_16BE,
69
+ CEncoding_UTF_16LE, CEncoding_UTF_32BE, CEncoding_UTF_32LE;
70
+ static ID i_encoding, i_encode;
71
+ #else
72
+ static ID i_iconv;
73
+ #endif
74
+
75
+ static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
76
+ static VALUE CNaN, CInfinity, CMinusInfinity;
77
+
78
+ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
79
+ i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
80
+ i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
81
+
82
+ %%{
83
+ machine JSON_common;
84
+
85
+ cr = '\n';
86
+ cr_neg = [^\n];
87
+ ws = [ \t\r\n];
88
+ c_comment = '/*' ( any* - (any* '*/' any* ) ) '*/';
89
+ cpp_comment = '//' cr_neg* cr;
90
+ comment = c_comment | cpp_comment;
91
+ ignore = ws | comment;
92
+ name_separator = ':';
93
+ value_separator = ',';
94
+ Vnull = 'null';
95
+ Vfalse = 'false';
96
+ Vtrue = 'true';
97
+ VNaN = 'NaN';
98
+ VInfinity = 'Infinity';
99
+ VMinusInfinity = '-Infinity';
100
+ begin_value = [nft\"\-\[\{NI] | digit;
101
+ begin_object = '{';
102
+ end_object = '}';
103
+ begin_array = '[';
104
+ end_array = ']';
105
+ begin_string = '"';
106
+ begin_name = begin_string;
107
+ begin_number = digit | '-';
108
+ }%%
109
+
110
+ %%{
111
+ machine JSON_object;
112
+ include JSON_common;
113
+
114
+ write data;
115
+
116
+ action parse_value {
117
+ VALUE v = Qnil;
118
+ const char *np = JSON_parse_value(json, fpc, pe, &v);
119
+ if (np == NULL) {
120
+ fhold; fbreak;
121
+ } else {
122
+ if (NIL_P(json->object_class)) {
123
+ rb_hash_aset(*result, last_name, v);
124
+ } else {
125
+ rb_funcall(*result, i_aset, 2, last_name, v);
126
+ }
127
+ fexec np;
128
+ }
129
+ }
130
+
131
+ action parse_name {
132
+ const char *np;
133
+ json->parsing_name = 1;
134
+ np = JSON_parse_string(json, fpc, pe, &last_name);
135
+ json->parsing_name = 0;
136
+ if (np == NULL) { fhold; fbreak; } else fexec np;
137
+ }
138
+
139
+ action exit { fhold; fbreak; }
140
+
141
+ a_pair = ignore* begin_name >parse_name
142
+ ignore* name_separator ignore*
143
+ begin_value >parse_value;
144
+
145
+ main := begin_object
146
+ (a_pair (ignore* value_separator a_pair)*)?
147
+ ignore* end_object @exit;
148
+ }%%
149
+
150
+ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
151
+ {
152
+ int cs = EVIL;
153
+ VALUE last_name = Qnil;
154
+ VALUE object_class = json->object_class;
155
+
156
+ if (json->max_nesting && json->current_nesting > json->max_nesting) {
157
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
158
+ }
159
+
160
+ *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
161
+
162
+ %% write init;
163
+ %% write exec;
164
+
165
+ if (cs >= JSON_object_first_final) {
166
+ if (json->create_additions) {
167
+ VALUE klassname = rb_hash_aref(*result, json->create_id);
168
+ if (!NIL_P(klassname)) {
169
+ VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
170
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
171
+ *result = rb_funcall(klass, i_json_create, 1, *result);
172
+ }
173
+ }
174
+ }
175
+ return p + 1;
176
+ } else {
177
+ return NULL;
178
+ }
179
+ }
180
+
181
+ %%{
182
+ machine JSON_value;
183
+ include JSON_common;
184
+
185
+ write data;
186
+
187
+ action parse_null {
188
+ *result = Qnil;
189
+ }
190
+ action parse_false {
191
+ *result = Qfalse;
192
+ }
193
+ action parse_true {
194
+ *result = Qtrue;
195
+ }
196
+ action parse_nan {
197
+ if (json->allow_nan) {
198
+ *result = CNaN;
199
+ } else {
200
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
201
+ }
202
+ }
203
+ action parse_infinity {
204
+ if (json->allow_nan) {
205
+ *result = CInfinity;
206
+ } else {
207
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
208
+ }
209
+ }
210
+ action parse_string {
211
+ const char *np = JSON_parse_string(json, fpc, pe, result);
212
+ if (np == NULL) { fhold; fbreak; } else fexec np;
213
+ }
214
+
215
+ action parse_number {
216
+ const char *np;
217
+ if(pe > fpc + 9 && !strncmp(MinusInfinity, fpc, 9)) {
218
+ if (json->allow_nan) {
219
+ *result = CMinusInfinity;
220
+ fexec p + 10;
221
+ fhold; fbreak;
222
+ } else {
223
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
224
+ }
225
+ }
226
+ np = JSON_parse_float(json, fpc, pe, result);
227
+ if (np != NULL) fexec np;
228
+ np = JSON_parse_integer(json, fpc, pe, result);
229
+ if (np != NULL) fexec np;
230
+ fhold; fbreak;
231
+ }
232
+
233
+ action parse_array {
234
+ const char *np;
235
+ json->current_nesting++;
236
+ np = JSON_parse_array(json, fpc, pe, result);
237
+ json->current_nesting--;
238
+ if (np == NULL) { fhold; fbreak; } else fexec np;
239
+ }
240
+
241
+ action parse_object {
242
+ const char *np;
243
+ json->current_nesting++;
244
+ np = JSON_parse_object(json, fpc, pe, result);
245
+ json->current_nesting--;
246
+ if (np == NULL) { fhold; fbreak; } else fexec np;
247
+ }
248
+
249
+ action exit { fhold; fbreak; }
250
+
251
+ main := (
252
+ Vnull @parse_null |
253
+ Vfalse @parse_false |
254
+ Vtrue @parse_true |
255
+ VNaN @parse_nan |
256
+ VInfinity @parse_infinity |
257
+ begin_number >parse_number |
258
+ begin_string >parse_string |
259
+ begin_array >parse_array |
260
+ begin_object >parse_object
261
+ ) %*exit;
262
+ }%%
263
+
264
+ static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
265
+ {
266
+ int cs = EVIL;
267
+
268
+ %% write init;
269
+ %% write exec;
270
+
271
+ if (cs >= JSON_value_first_final) {
272
+ return p;
273
+ } else {
274
+ return NULL;
275
+ }
276
+ }
277
+
278
+ %%{
279
+ machine JSON_integer;
280
+
281
+ write data;
282
+
283
+ action exit { fhold; fbreak; }
284
+
285
+ main := '-'? ('0' | [1-9][0-9]*) (^[0-9] @exit);
286
+ }%%
287
+
288
+ static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
289
+ {
290
+ int cs = EVIL;
291
+
292
+ %% write init;
293
+ json->memo = p;
294
+ %% write exec;
295
+
296
+ if (cs >= JSON_integer_first_final) {
297
+ long len = p - json->memo;
298
+ *result = rb_Integer(rb_str_new(json->memo, len));
299
+ return p + 1;
300
+ } else {
301
+ return NULL;
302
+ }
303
+ }
304
+
305
+ %%{
306
+ machine JSON_float;
307
+ include JSON_common;
308
+
309
+ write data;
310
+
311
+ action exit { fhold; fbreak; }
312
+
313
+ main := '-'? (
314
+ (('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
315
+ | (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
316
+ ) (^[0-9Ee.\-] @exit );
317
+ }%%
318
+
319
+ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
320
+ {
321
+ int cs = EVIL;
322
+
323
+ %% write init;
324
+ json->memo = p;
325
+ %% write exec;
326
+
327
+ if (cs >= JSON_float_first_final) {
328
+ long len = p - json->memo;
329
+ *result = rb_Float(rb_str_new(json->memo, len));
330
+ return p + 1;
331
+ } else {
332
+ return NULL;
333
+ }
334
+ }
335
+
336
+
337
+ %%{
338
+ machine JSON_array;
339
+ include JSON_common;
340
+
341
+ write data;
342
+
343
+ action parse_value {
344
+ VALUE v = Qnil;
345
+ const char *np = JSON_parse_value(json, fpc, pe, &v);
346
+ if (np == NULL) {
347
+ fhold; fbreak;
348
+ } else {
349
+ if (NIL_P(json->array_class)) {
350
+ rb_ary_push(*result, v);
351
+ } else {
352
+ rb_funcall(*result, i_leftshift, 1, v);
353
+ }
354
+ fexec np;
355
+ }
356
+ }
357
+
358
+ action exit { fhold; fbreak; }
359
+
360
+ next_element = value_separator ignore* begin_value >parse_value;
361
+
362
+ main := begin_array ignore*
363
+ ((begin_value >parse_value ignore*)
364
+ (ignore* next_element ignore*)*)?
365
+ end_array @exit;
366
+ }%%
367
+
368
+ static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
369
+ {
370
+ int cs = EVIL;
371
+ VALUE array_class = json->array_class;
372
+
373
+ if (json->max_nesting && json->current_nesting > json->max_nesting) {
374
+ rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
375
+ }
376
+ *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
377
+
378
+ %% write init;
379
+ %% write exec;
380
+
381
+ if(cs >= JSON_array_first_final) {
382
+ return p + 1;
383
+ } else {
384
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
385
+ return NULL;
386
+ }
387
+ }
388
+
389
+ static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd)
390
+ {
391
+ const char *p = string, *pe = string, *unescape;
392
+ int unescape_len;
393
+
394
+ while (pe < stringEnd) {
395
+ if (*pe == '\\') {
396
+ unescape = (char *) "?";
397
+ unescape_len = 1;
398
+ if (pe > p) rb_str_buf_cat(result, p, pe - p);
399
+ switch (*++pe) {
400
+ case 'n':
401
+ unescape = (char *) "\n";
402
+ break;
403
+ case 'r':
404
+ unescape = (char *) "\r";
405
+ break;
406
+ case 't':
407
+ unescape = (char *) "\t";
408
+ break;
409
+ case '"':
410
+ unescape = (char *) "\"";
411
+ break;
412
+ case '\\':
413
+ unescape = (char *) "\\";
414
+ break;
415
+ case 'b':
416
+ unescape = (char *) "\b";
417
+ break;
418
+ case 'f':
419
+ unescape = (char *) "\f";
420
+ break;
421
+ case 'u':
422
+ if (pe > stringEnd - 4) {
423
+ return Qnil;
424
+ } else {
425
+ char buf[4];
426
+ UTF32 ch = unescape_unicode((unsigned char *) ++pe);
427
+ pe += 3;
428
+ if (UNI_SUR_HIGH_START == (ch & 0xFC00)) {
429
+ pe++;
430
+ if (pe > stringEnd - 6) return Qnil;
431
+ if (pe[0] == '\\' && pe[1] == 'u') {
432
+ UTF32 sur = unescape_unicode((unsigned char *) pe + 2);
433
+ ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
434
+ | (sur & 0x3FF));
435
+ pe += 5;
436
+ } else {
437
+ unescape = (char *) "?";
438
+ break;
439
+ }
440
+ }
441
+ unescape_len = convert_UTF32_to_UTF8(buf, ch);
442
+ unescape = buf;
443
+ }
444
+ break;
445
+ default:
446
+ p = pe;
447
+ continue;
448
+ }
449
+ rb_str_buf_cat(result, unescape, unescape_len);
450
+ p = ++pe;
451
+ } else {
452
+ pe++;
453
+ }
454
+ }
455
+ rb_str_buf_cat(result, p, pe - p);
456
+ return result;
457
+ }
458
+
459
+ %%{
460
+ machine JSON_string;
461
+ include JSON_common;
462
+
463
+ write data;
464
+
465
+ action parse_string {
466
+ *result = json_string_unescape(*result, json->memo + 1, p);
467
+ if (NIL_P(*result)) {
468
+ fhold;
469
+ fbreak;
470
+ } else {
471
+ FORCE_UTF8(*result);
472
+ fexec p + 1;
473
+ }
474
+ }
475
+
476
+ action exit { fhold; fbreak; }
477
+
478
+ main := '"' ((^([\"\\] | 0..0x1f) | '\\'[\"\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^([\"\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit;
479
+ }%%
480
+
481
+ static int
482
+ match_i(VALUE regexp, VALUE klass, VALUE memo)
483
+ {
484
+ if (regexp == Qundef) return ST_STOP;
485
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
486
+ RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
487
+ rb_ary_push(memo, klass);
488
+ return ST_STOP;
489
+ }
490
+ return ST_CONTINUE;
491
+ }
492
+
493
+ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
494
+ {
495
+ int cs = EVIL;
496
+ *result = rb_str_buf_new(0);
497
+ %% write init;
498
+ json->memo = p;
499
+ %% write exec;
500
+
501
+ if (json->create_additions) {
502
+ VALUE match_string = json->match_string;
503
+ if (RTEST(match_string)) {
504
+ VALUE klass;
505
+ VALUE memo = rb_ary_new2(2);
506
+ rb_ary_push(memo, *result);
507
+ rb_hash_foreach(match_string, match_i, memo);
508
+ klass = rb_ary_entry(memo, 1);
509
+ if (RTEST(klass)) {
510
+ *result = rb_funcall(klass, i_json_create, 1, *result);
511
+ }
512
+ }
513
+ }
514
+
515
+ if (json->symbolize_names && json->parsing_name) {
516
+ *result = rb_str_intern(*result);
517
+ }
518
+ if (cs >= JSON_string_first_final) {
519
+ return p + 1;
520
+ } else {
521
+ return NULL;
522
+ }
523
+ }
524
+
525
+
526
+ %%{
527
+ machine JSON;
528
+
529
+ write data;
530
+
531
+ include JSON_common;
532
+
533
+ action parse_object {
534
+ const char *np;
535
+ json->current_nesting = 1;
536
+ np = JSON_parse_object(json, fpc, pe, &result);
537
+ if (np == NULL) { fhold; fbreak; } else fexec np;
538
+ }
539
+
540
+ action parse_array {
541
+ const char *np;
542
+ json->current_nesting = 1;
543
+ np = JSON_parse_array(json, fpc, pe, &result);
544
+ if (np == NULL) { fhold; fbreak; } else fexec np;
545
+ }
546
+
547
+ main := ignore* (
548
+ begin_object >parse_object |
549
+ begin_array >parse_array
550
+ ) ignore*;
551
+ }%%
552
+
553
+ /*
554
+ * Document-class: JSON::Ext::Parser
555
+ *
556
+ * This is the JSON parser implemented as a C extension. It can be configured
557
+ * to be used by setting
558
+ *
559
+ * JSON.parser = JSON::Ext::Parser
560
+ *
561
+ * with the method parser= in JSON.
562
+ *
563
+ */
564
+
565
+ static VALUE convert_encoding(VALUE source)
566
+ {
567
+ const char *ptr = RSTRING_PTR(source);
568
+ long len = RSTRING_LEN(source);
569
+ if (len < 2) {
570
+ rb_raise(eParserError, "A JSON text must at least contain two octets!");
571
+ }
572
+ #ifdef HAVE_RUBY_ENCODING_H
573
+ {
574
+ VALUE encoding = rb_funcall(source, i_encoding, 0);
575
+ if (encoding == CEncoding_ASCII_8BIT) {
576
+ if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
577
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32BE);
578
+ } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
579
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16BE);
580
+ } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
581
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32LE);
582
+ } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
583
+ source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16LE);
584
+ } else {
585
+ source = rb_str_dup(source);
586
+ FORCE_UTF8(source);
587
+ }
588
+ } else {
589
+ source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
590
+ }
591
+ }
592
+ #else
593
+ if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
594
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32be"), source);
595
+ } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
596
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16be"), source);
597
+ } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
598
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32le"), source);
599
+ } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
600
+ source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16le"), source);
601
+ }
602
+ #endif
603
+ return source;
604
+ }
605
+
606
+ static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v)
607
+ {
608
+ // store reference to v in a Ruby instVar to keep v alive
609
+ // without using a gc_mark function in Data_Wrap_Struct calls
610
+ rb_iv_set(json->dwrapped_parser, iv_name, v);
611
+ }
612
+
613
+ /*
614
+ * call-seq: new(source, opts => {})
615
+ *
616
+ * Creates a new JSON::Ext::Parser instance for the string _source_.
617
+ *
618
+ * Creates a new JSON::Ext::Parser instance for the string _source_.
619
+ *
620
+ * It will be configured by the _opts_ hash. _opts_ can have the following
621
+ * keys:
622
+ *
623
+ * _opts_ can have the following keys:
624
+ * * *max_nesting*: The maximum depth of nesting allowed in the parsed data
625
+ * structures. Disable depth checking with :max_nesting => false|nil|0, it
626
+ * defaults to 19.
627
+ * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
628
+ * defiance of RFC 4627 to be parsed by the Parser. This option defaults to
629
+ * false.
630
+ * * *symbolize_names*: If set to true, returns symbols for the names
631
+ * (keys) in a JSON object. Otherwise strings are returned, which is also
632
+ * the default.
633
+ * * *create_additions*: If set to false, the Parser doesn't create
634
+ * additions even if a matchin class and create_id was found. This option
635
+ * defaults to true.
636
+ * * *object_class*: Defaults to Hash
637
+ * * *array_class*: Defaults to Array
638
+ */
639
+
640
+ static int init_count = 0;
641
+ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
642
+ {
643
+ const char *ptr;
644
+ long len;
645
+ VALUE source, opts;
646
+
647
+ GET_PARSER_INIT;
648
+ init_count += 1;
649
+
650
+ #ifndef MAGLEV
651
+ if (json->Vsource) {
652
+ rb_raise(rb_eTypeError, "already initialized instance");
653
+ }
654
+ #endif
655
+ rb_scan_args(argc, argv, "11", &source, &opts);
656
+ source = convert_encoding(StringValue(source));
657
+ ptr = RSTRING_PTR(source);
658
+ len = RSTRING_LEN(source);
659
+ if (!NIL_P(opts)) {
660
+ opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
661
+ if (NIL_P(opts)) {
662
+ rb_raise(rb_eArgError, "opts needs to be like a hash");
663
+ } else {
664
+ VALUE tmp = ID2SYM(i_max_nesting);
665
+ if (option_given_p(opts, tmp)) {
666
+ VALUE max_nesting = rb_hash_aref(opts, tmp);
667
+ if (RTEST(max_nesting)) {
668
+ Check_Type(max_nesting, T_FIXNUM);
669
+ json->max_nesting = FIX2INT(max_nesting);
670
+ } else {
671
+ json->max_nesting = 0;
672
+ }
673
+ } else {
674
+ json->max_nesting = 19;
675
+ }
676
+ tmp = ID2SYM(i_allow_nan);
677
+ if (option_given_p(opts, tmp)) {
678
+ json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
679
+ } else {
680
+ json->allow_nan = 0;
681
+ }
682
+ tmp = ID2SYM(i_symbolize_names);
683
+ if (option_given_p(opts, tmp)) {
684
+ json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
685
+ } else {
686
+ json->symbolize_names = 0;
687
+ }
688
+ tmp = ID2SYM(i_create_additions);
689
+ if (option_given_p(opts, tmp)) {
690
+ json->create_additions = RTEST(rb_hash_aref(opts, tmp));
691
+ } else {
692
+ json->create_additions = 1;
693
+ }
694
+ tmp = ID2SYM(i_create_id);
695
+ if (option_given_p(opts, tmp)) {
696
+ json->create_id = rb_hash_aref(opts, tmp);
697
+ } else {
698
+ json->create_id = rb_funcall(mJSON, i_create_id, 0);
699
+ }
700
+ parser_iv_set(json, "@create_id", json->create_id);
701
+ tmp = ID2SYM(i_object_class);
702
+ if (option_given_p(opts, tmp)) {
703
+ json->object_class = rb_hash_aref(opts, tmp);
704
+ parser_iv_set(json, "@object_class", json->object_class);
705
+ }
706
+ tmp = ID2SYM(i_array_class);
707
+ if (option_given_p(opts, tmp)) {
708
+ json->array_class = rb_hash_aref(opts, tmp);
709
+ parser_iv_set(json, "@array_class", json->array_class);
710
+ }
711
+ tmp = ID2SYM(i_match_string);
712
+ if (option_given_p(opts, tmp)) {
713
+ VALUE match_string = rb_hash_aref(opts, tmp);
714
+ json->match_string = RTEST(match_string) ? match_string : Qnil;
715
+ parser_iv_set(json, "@match_string", json->match_string);
716
+ }
717
+ }
718
+ } else {
719
+ json->max_nesting = 19;
720
+ json->allow_nan = 0;
721
+ json->create_additions = 1;
722
+ json->create_id = rb_funcall(mJSON, i_create_id, 0);
723
+ parser_iv_set(json, "@create_id", json->create_id);
724
+ }
725
+ json->current_nesting = 0;
726
+ json->len = len;
727
+ json->source = ptr;
728
+ json->Vsource = source;
729
+ parser_iv_set(json, "@vsource", json->Vsource);
730
+ return self;
731
+ }
732
+
733
+ /*
734
+ * call-seq: parse()
735
+ *
736
+ * Parses the current JSON text _source_ and returns the complete data
737
+ * structure as a result.
738
+ */
739
+ static VALUE cParser_parse(VALUE self)
740
+ {
741
+ const char *p, *pe;
742
+ int cs = EVIL;
743
+ VALUE result = Qnil;
744
+ GET_PARSER;
745
+
746
+ %% write init;
747
+ p = json->source;
748
+ pe = p + json->len;
749
+ %% write exec;
750
+
751
+ if (cs >= JSON_first_final && p == pe) {
752
+ return result;
753
+ } else {
754
+ rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
755
+ return Qnil;
756
+ }
757
+ }
758
+
759
+ static JSON_Parser *JSON_allocate()
760
+ {
761
+ JSON_Parser *json = ALLOC(JSON_Parser);
762
+ MEMZERO(json, JSON_Parser, 1);
763
+ json->dwrapped_parser = Qnil;
764
+ json->Vsource = Qnil;
765
+ json->create_id = Qnil;
766
+ json->object_class = Qnil;
767
+ json->array_class = Qnil;
768
+ json->match_string = Qnil;
769
+
770
+ return json;
771
+ }
772
+
773
+ /* deleted JSON_mark function */
774
+
775
+ static void JSON_free(JSON_Parser *json)
776
+ {
777
+ ruby_xfree(json);
778
+ }
779
+
780
+ static VALUE cJSON_parser_s_allocate(VALUE klass)
781
+ {
782
+ JSON_Parser *json = JSON_allocate();
783
+ VALUE data_obj = Data_Wrap_Struct(klass, NULL, JSON_free, json);
784
+ json->dwrapped_parser = data_obj;
785
+ return data_obj;
786
+ }
787
+
788
+ /*
789
+ * call-seq: source()
790
+ *
791
+ * Returns a copy of the current _source_ string, that was used to construct
792
+ * this Parser.
793
+ */
794
+ static VALUE cParser_source(VALUE self)
795
+ {
796
+ GET_PARSER;
797
+ return rb_str_dup(json->Vsource);
798
+ }
799
+
800
+ void Init_parser()
801
+ {
802
+ rb_require("json/common");
803
+ mJSON = rb_define_module("JSON");
804
+ mExt = rb_define_module_under(mJSON, "Ext");
805
+ cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
806
+ eParserError = rb_path2class("JSON::ParserError");
807
+ eNestingError = rb_path2class("JSON::NestingError");
808
+ rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
809
+ rb_define_method(cParser, "initialize", cParser_initialize, -1);
810
+ rb_define_method(cParser, "parse", cParser_parse, 0);
811
+ rb_define_method(cParser, "source", cParser_source, 0);
812
+
813
+ CNaN = rb_const_get(mJSON, rb_intern("NaN"));
814
+ CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
815
+ CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
816
+
817
+ i_json_creatable_p = rb_intern("json_creatable?");
818
+ i_json_create = rb_intern("json_create");
819
+ i_create_id = rb_intern("create_id");
820
+ i_create_additions = rb_intern("create_additions");
821
+ i_chr = rb_intern("chr");
822
+ i_max_nesting = rb_intern("max_nesting");
823
+ i_allow_nan = rb_intern("allow_nan");
824
+ i_symbolize_names = rb_intern("symbolize_names");
825
+ i_object_class = rb_intern("object_class");
826
+ i_array_class = rb_intern("array_class");
827
+ i_match = rb_intern("match");
828
+ i_match_string = rb_intern("match_string");
829
+ i_key_p = rb_intern("key?");
830
+ i_deep_const_get = rb_intern("deep_const_get");
831
+ i_aset = rb_intern("[]=");
832
+ i_leftshift = rb_intern("<<");
833
+ #ifdef HAVE_RUBY_ENCODING_H
834
+ CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
835
+ CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
836
+ CEncoding_UTF_16LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16le"));
837
+ CEncoding_UTF_32BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32be"));
838
+ CEncoding_UTF_32LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32le"));
839
+ CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit"));
840
+ i_encoding = rb_intern("encoding");
841
+ i_encode = rb_intern("encode");
842
+ #else
843
+ i_iconv = rb_intern("iconv");
844
+ #endif
845
+ }
846
+
847
+ /*
848
+ * Local variables:
849
+ * mode: c
850
+ * c-file-style: ruby
851
+ * indent-tabs-mode: nil
852
+ * End:
853
+ */