larsklevan-json 1.5.1

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