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