4 * This code is copyrighted work by Daniel Luz <dev at mernen dot com>.
6 * Distributed under the Ruby and GPLv2 licenses; see COPYING and GPL files
11 import org.jruby.Ruby;
12 import org.jruby.RubyArray;
13 import org.jruby.RubyClass;
14 import org.jruby.RubyEncoding;
15 import org.jruby.RubyFloat;
16 import org.jruby.RubyHash;
17 import org.jruby.RubyInteger;
18 import org.jruby.RubyModule;
19 import org.jruby.RubyNumeric;
20 import org.jruby.RubyObject;
21 import org.jruby.RubyString;
22 import org.jruby.anno.JRubyMethod;
23 import org.jruby.exceptions.JumpException;
24 import org.jruby.exceptions.RaiseException;
25 import org.jruby.runtime.Block;
26 import org.jruby.runtime.ObjectAllocator;
27 import org.jruby.runtime.ThreadContext;
28 import org.jruby.runtime.Visibility;
29 import org.jruby.runtime.builtin.IRubyObject;
30 import org.jruby.util.ByteList;
33 * The <code>JSON::Ext::Parser</code> class.
35 * <p>This is the JSON parser implemented as a Java class. To use it as the
36 * standard parser, set
37 * <pre>JSON.parser = JSON::Ext::Parser</pre>
38 * This is performed for you when you <code>include "json/ext"</code>.
40 * <p>This class does not perform the actual parsing, just acts as an interface
41 * to Ruby code. When the {@link #parse()} method is invoked, a
42 * Parser.ParserSession object is instantiated, which handles the process.
46 public class Parser extends RubyObject {
47 private final RuntimeInfo info;
48 private RubyString vSource;
49 private RubyString createId;
50 private boolean createAdditions;
51 private int maxNesting;
52 private boolean allowNaN;
53 private boolean symbolizeNames;
54 private boolean quirksMode;
55 private RubyClass objectClass;
56 private RubyClass arrayClass;
57 private RubyHash match_string;
59 private static final int DEFAULT_MAX_NESTING = 19;
61 private static final String JSON_MINUS_INFINITY = "-Infinity";
62 // constant names in the JSON module containing those values
63 private static final String CONST_NAN = "NaN";
64 private static final String CONST_INFINITY = "Infinity";
65 private static final String CONST_MINUS_INFINITY = "MinusInfinity";
67 static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
68 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
69 return new Parser(runtime, klazz);
74 * Multiple-value return for internal parser methods.
76 * <p>All the <code>parse<var>Stuff</var></code> methods return instances of
77 * <code>ParserResult</code> when successful, or <code>null</code> when
78 * there's a problem with the input data.
80 static final class ParserResult {
82 * The result of the successful parsing. Should never be
85 final IRubyObject result;
87 * The point where the parser returned.
91 ParserResult(IRubyObject result, int p) {
97 public Parser(Ruby runtime, RubyClass metaClass) {
98 super(runtime, metaClass);
99 info = RuntimeInfo.forRuntime(runtime);
103 * <code>Parser.new(source, opts = {})</code>
105 * <p>Creates a new <code>JSON::Ext::Parser</code> instance for the string
106 * <code>source</code>.
107 * It will be configured by the <code>opts</code> Hash.
108 * <code>opts</code> can have the following keys:
111 * <dt><code>:max_nesting</code>
112 * <dd>The maximum depth of nesting allowed in the parsed data
113 * structures. Disable depth checking with <code>:max_nesting => false|nil|0</code>,
116 * <dt><code>:allow_nan</code>
117 * <dd>If set to <code>true</code>, allow <code>NaN</code>,
118 * <code>Infinity</code> and <code>-Infinity</code> in defiance of RFC 4627
119 * to be parsed by the Parser. This option defaults to <code>false</code>.
121 * <dt><code>:symbolize_names</code>
122 * <dd>If set to <code>true</code>, returns symbols for the names (keys) in
123 * a JSON object. Otherwise strings are returned, which is also the default.
125 * <dt><code>:quirks_mode?</code>
126 * <dd>If set to <code>true</code>, if the parse is in quirks_mode, false
129 * <dt><code>:create_additions</code>
130 * <dd>If set to <code>false</code>, the Parser doesn't create additions
131 * even if a matchin class and <code>create_id</code> was found. This option
132 * defaults to <code>true</code>.
134 * <dt><code>:object_class</code>
135 * <dd>Defaults to Hash.
137 * <dt><code>:array_class</code>
138 * <dd>Defaults to Array.
141 @JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
142 public static IRubyObject newInstance(IRubyObject clazz, IRubyObject[] args, Block block) {
143 Parser parser = (Parser)((RubyClass)clazz).allocate();
145 parser.callInit(args, block);
150 @JRubyMethod(required = 1, optional = 1, visibility = Visibility.PRIVATE)
151 public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
152 Ruby runtime = context.getRuntime();
153 if (this.vSource != null) {
154 throw runtime.newTypeError("already initialized instance");
157 OptionsReader opts = new OptionsReader(context, args.length > 1 ? args[1] : null);
158 this.maxNesting = opts.getInt("max_nesting", DEFAULT_MAX_NESTING);
159 this.allowNaN = opts.getBool("allow_nan", false);
160 this.symbolizeNames = opts.getBool("symbolize_names", false);
161 this.quirksMode = opts.getBool("quirks_mode", false);
162 this.createId = opts.getString("create_id", getCreateId(context));
163 this.createAdditions = opts.getBool("create_additions", false);
164 this.objectClass = opts.getClass("object_class", runtime.getHash());
165 this.arrayClass = opts.getClass("array_class", runtime.getArray());
166 this.match_string = opts.getHash("match_string");
168 this.vSource = args[0].convertToString();
169 if (!quirksMode) this.vSource = convertEncoding(context, vSource);
175 * Checks the given string's encoding. If a non-UTF-8 encoding is detected,
176 * a converted copy is returned.
177 * Returns the source string if no conversion is needed.
179 private RubyString convertEncoding(ThreadContext context, RubyString source) {
180 ByteList bl = source.getByteList();
181 int len = bl.length();
183 throw Utils.newException(context, Utils.M_PARSER_ERROR,
184 "A JSON text must at least contain two octets!");
187 if (info.encodingsSupported()) {
188 RubyEncoding encoding = (RubyEncoding)source.encoding(context);
189 if (encoding != info.ascii8bit.get()) {
190 return (RubyString)source.encode(context, info.utf8.get());
193 String sniffedEncoding = sniffByteList(bl);
194 if (sniffedEncoding == null) return source; // assume UTF-8
195 return reinterpretEncoding(context, source, sniffedEncoding);
198 String sniffedEncoding = sniffByteList(bl);
199 if (sniffedEncoding == null) return source; // assume UTF-8
200 Ruby runtime = context.getRuntime();
201 return (RubyString)info.jsonModule.get().
202 callMethod(context, "iconv",
204 runtime.newString("utf-8"),
205 runtime.newString(sniffedEncoding),
210 * Checks the first four bytes of the given ByteList to infer its encoding,
211 * using the principle demonstrated on section 3 of RFC 4627 (JSON).
213 private static String sniffByteList(ByteList bl) {
214 if (bl.length() < 4) return null;
215 if (bl.get(0) == 0 && bl.get(2) == 0) {
216 return bl.get(1) == 0 ? "utf-32be" : "utf-16be";
218 if (bl.get(1) == 0 && bl.get(3) == 0) {
219 return bl.get(2) == 0 ? "utf-32le" : "utf-16le";
225 * Assumes the given (binary) RubyString to be in the given encoding, then
226 * converts it to UTF-8.
228 private RubyString reinterpretEncoding(ThreadContext context,
229 RubyString str, String sniffedEncoding) {
230 RubyEncoding actualEncoding = info.getEncoding(context, sniffedEncoding);
231 RubyEncoding targetEncoding = info.utf8.get();
232 RubyString dup = (RubyString)str.dup();
233 dup.force_encoding(context, actualEncoding);
234 return (RubyString)dup.encode_bang(context, targetEncoding);
238 * <code>Parser#parse()</code>
240 * <p>Parses the current JSON text <code>source</code> and returns the
241 * complete data structure as a result.
244 public IRubyObject parse(ThreadContext context) {
245 return new ParserSession(this, context).parse();
249 * <code>Parser#source()</code>
251 * <p>Returns a copy of the current <code>source</code> string, that was
252 * used to construct this Parser.
254 @JRubyMethod(name = "source")
255 public IRubyObject source_get() {
256 return checkAndGetSource().dup();
260 * <code>Parser#quirks_mode?()</code>
262 * <p>If set to <code>true</code>, if the parse is in quirks_mode, false
265 @JRubyMethod(name = "quirks_mode?")
266 public IRubyObject quirks_mode_p(ThreadContext context) {
267 return context.getRuntime().newBoolean(quirksMode);
270 public RubyString checkAndGetSource() {
271 if (vSource != null) {
274 throw getRuntime().newTypeError("uninitialized instance");
279 * Queries <code>JSON.create_id</code>. Returns <code>null</code> if it is
280 * set to <code>nil</code> or <code>false</code>, and a String if not.
282 private RubyString getCreateId(ThreadContext context) {
283 IRubyObject v = info.jsonModule.get().callMethod(context, "create_id");
284 return v.isTrue() ? v.convertToString() : null;
288 * A string parsing session.
290 * <p>Once a ParserSession is instantiated, the source string should not
291 * change until the parsing is complete. The ParserSession object assumes
292 * the source {@link RubyString} is still associated to its original
293 * {@link ByteList}, which in turn must still be bound to the same
294 * <code>byte[]</code> value (and on the same offset).
296 // Ragel uses lots of fall-through
297 @SuppressWarnings("fallthrough")
298 private static class ParserSession {
299 private final Parser parser;
300 private final ThreadContext context;
301 private final ByteList byteList;
302 private final byte[] data;
303 private final StringDecoder decoder;
304 private int currentNesting = 0;
306 // initialization value for all state variables.
307 // no idea about the origins of this value, ask Flori ;)
308 private static final int EVIL = 0x666;
310 private ParserSession(Parser parser, ThreadContext context) {
311 this.parser = parser;
312 this.context = context;
313 this.byteList = parser.checkAndGetSource().getByteList();
314 this.data = byteList.unsafeBytes();
315 this.decoder = new StringDecoder(context);
318 private RaiseException unexpectedToken(int absStart, int absEnd) {
319 RubyString msg = getRuntime().newString("unexpected token at '")
320 .cat(data, absStart, absEnd - absStart)
322 return newException(Utils.M_PARSER_ERROR, msg);
325 private Ruby getRuntime() {
326 return context.getRuntime();
330 // line 353 "Parser.rl"
334 // line 335 "Parser.java"
335 private static byte[] init__JSON_value_actions_0()
338 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
339 5, 1, 6, 1, 7, 1, 8, 1, 9
343 private static final byte _JSON_value_actions[] = init__JSON_value_actions_0();
346 private static byte[] init__JSON_value_key_offsets_0()
349 0, 0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
350 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
354 private static final byte _JSON_value_key_offsets[] = init__JSON_value_key_offsets_0();
357 private static char[] init__JSON_value_trans_keys_0()
360 34, 45, 73, 78, 91, 102, 110, 116, 123, 48, 57, 110,
361 102, 105, 110, 105, 116, 121, 97, 78, 97, 108, 115, 101,
362 117, 108, 108, 114, 117, 101, 0
366 private static final char _JSON_value_trans_keys[] = init__JSON_value_trans_keys_0();
369 private static byte[] init__JSON_value_single_lengths_0()
372 0, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
373 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
377 private static final byte _JSON_value_single_lengths[] = init__JSON_value_single_lengths_0();
380 private static byte[] init__JSON_value_range_lengths_0()
383 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
388 private static final byte _JSON_value_range_lengths[] = init__JSON_value_range_lengths_0();
391 private static byte[] init__JSON_value_index_offsets_0()
394 0, 0, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29,
395 31, 33, 35, 37, 39, 41, 43, 45, 47, 49
399 private static final byte _JSON_value_index_offsets[] = init__JSON_value_index_offsets_0();
402 private static byte[] init__JSON_value_trans_targs_0()
405 21, 21, 2, 9, 21, 11, 15, 18, 21, 21, 0, 3,
406 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 21,
407 0, 10, 0, 21, 0, 12, 0, 13, 0, 14, 0, 21,
408 0, 16, 0, 17, 0, 21, 0, 19, 0, 20, 0, 21,
413 private static final byte _JSON_value_trans_targs[] = init__JSON_value_trans_targs_0();
416 private static byte[] init__JSON_value_trans_actions_0()
419 13, 11, 0, 0, 15, 0, 0, 0, 17, 11, 0, 0,
420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
421 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 3,
422 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 5,
427 private static final byte _JSON_value_trans_actions[] = init__JSON_value_trans_actions_0();
430 private static byte[] init__JSON_value_from_state_actions_0()
433 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
434 0, 0, 0, 0, 0, 0, 0, 0, 0, 19
438 private static final byte _JSON_value_from_state_actions[] = init__JSON_value_from_state_actions_0();
441 static final int JSON_value_start = 1;
442 static final int JSON_value_first_final = 21;
443 static final int JSON_value_error = 0;
445 static final int JSON_value_en_main = 1;
448 // line 459 "Parser.rl"
451 ParserResult parseValue(int p, int pe) {
453 IRubyObject result = null;
456 // line 457 "Parser.java"
458 cs = JSON_value_start;
461 // line 466 "Parser.rl"
463 // line 464 "Parser.java"
472 _goto: while (true) {
473 switch ( _goto_targ ) {
484 _acts = _JSON_value_from_state_actions[cs];
485 _nacts = (int) _JSON_value_actions[_acts++];
486 while ( _nacts-- > 0 ) {
487 switch ( _JSON_value_actions[_acts++] ) {
489 // line 444 "Parser.rl"
492 { p += 1; _goto_targ = 5; if (true) continue _goto;}
495 // line 496 "Parser.java"
500 _keys = _JSON_value_key_offsets[cs];
501 _trans = _JSON_value_index_offsets[cs];
502 _klen = _JSON_value_single_lengths[cs];
506 int _upper = _keys + _klen - 1;
508 if ( _upper < _lower )
511 _mid = _lower + ((_upper-_lower) >> 1);
512 if ( data[p] < _JSON_value_trans_keys[_mid] )
514 else if ( data[p] > _JSON_value_trans_keys[_mid] )
517 _trans += (_mid - _keys);
525 _klen = _JSON_value_range_lengths[cs];
529 int _upper = _keys + (_klen<<1) - 2;
531 if ( _upper < _lower )
534 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
535 if ( data[p] < _JSON_value_trans_keys[_mid] )
537 else if ( data[p] > _JSON_value_trans_keys[_mid+1] )
540 _trans += ((_mid - _keys)>>1);
548 cs = _JSON_value_trans_targs[_trans];
550 if ( _JSON_value_trans_actions[_trans] != 0 ) {
551 _acts = _JSON_value_trans_actions[_trans];
552 _nacts = (int) _JSON_value_actions[_acts++];
553 while ( _nacts-- > 0 )
555 switch ( _JSON_value_actions[_acts++] )
558 // line 361 "Parser.rl"
560 result = getRuntime().getNil();
564 // line 364 "Parser.rl"
566 result = getRuntime().getFalse();
570 // line 367 "Parser.rl"
572 result = getRuntime().getTrue();
576 // line 370 "Parser.rl"
578 if (parser.allowNaN) {
579 result = getConstant(CONST_NAN);
581 throw unexpectedToken(p - 2, pe);
586 // line 377 "Parser.rl"
588 if (parser.allowNaN) {
589 result = getConstant(CONST_INFINITY);
591 throw unexpectedToken(p - 7, pe);
596 // line 384 "Parser.rl"
598 if (pe > p + 9 - (parser.quirksMode ? 1 : 0) &&
599 absSubSequence(p, p + 9).toString().equals(JSON_MINUS_INFINITY)) {
601 if (parser.allowNaN) {
602 result = getConstant(CONST_MINUS_INFINITY);
605 { p += 1; _goto_targ = 5; if (true) continue _goto;}
607 throw unexpectedToken(p, pe);
610 ParserResult res = parseFloat(p, pe);
615 res = parseInteger(p, pe);
621 { p += 1; _goto_targ = 5; if (true) continue _goto;}
625 // line 410 "Parser.rl"
627 ParserResult res = parseString(p, pe);
630 { p += 1; _goto_targ = 5; if (true) continue _goto;}
638 // line 420 "Parser.rl"
641 ParserResult res = parseArray(p, pe);
645 { p += 1; _goto_targ = 5; if (true) continue _goto;}
653 // line 432 "Parser.rl"
656 ParserResult res = parseObject(p, pe);
660 { p += 1; _goto_targ = 5; if (true) continue _goto;}
667 // line 668 "Parser.java"
687 // line 467 "Parser.rl"
689 if (cs >= JSON_value_first_final && result != null) {
690 return new ParserResult(result, p);
697 // line 698 "Parser.java"
698 private static byte[] init__JSON_integer_actions_0()
705 private static final byte _JSON_integer_actions[] = init__JSON_integer_actions_0();
708 private static byte[] init__JSON_integer_key_offsets_0()
715 private static final byte _JSON_integer_key_offsets[] = init__JSON_integer_key_offsets_0();
718 private static char[] init__JSON_integer_trans_keys_0()
721 45, 48, 49, 57, 48, 49, 57, 48, 57, 48, 57, 0
725 private static final char _JSON_integer_trans_keys[] = init__JSON_integer_trans_keys_0();
728 private static byte[] init__JSON_integer_single_lengths_0()
735 private static final byte _JSON_integer_single_lengths[] = init__JSON_integer_single_lengths_0();
738 private static byte[] init__JSON_integer_range_lengths_0()
745 private static final byte _JSON_integer_range_lengths[] = init__JSON_integer_range_lengths_0();
748 private static byte[] init__JSON_integer_index_offsets_0()
755 private static final byte _JSON_integer_index_offsets[] = init__JSON_integer_index_offsets_0();
758 private static byte[] init__JSON_integer_indicies_0()
761 0, 2, 3, 1, 2, 3, 1, 1, 4, 1, 3, 4,
766 private static final byte _JSON_integer_indicies[] = init__JSON_integer_indicies_0();
769 private static byte[] init__JSON_integer_trans_targs_0()
776 private static final byte _JSON_integer_trans_targs[] = init__JSON_integer_trans_targs_0();
779 private static byte[] init__JSON_integer_trans_actions_0()
786 private static final byte _JSON_integer_trans_actions[] = init__JSON_integer_trans_actions_0();
789 static final int JSON_integer_start = 1;
790 static final int JSON_integer_first_final = 3;
791 static final int JSON_integer_error = 0;
793 static final int JSON_integer_en_main = 1;
796 // line 486 "Parser.rl"
799 ParserResult parseInteger(int p, int pe) {
803 // line 804 "Parser.java"
805 cs = JSON_integer_start;
808 // line 492 "Parser.rl"
811 // line 812 "Parser.java"
820 _goto: while (true) {
821 switch ( _goto_targ ) {
833 _keys = _JSON_integer_key_offsets[cs];
834 _trans = _JSON_integer_index_offsets[cs];
835 _klen = _JSON_integer_single_lengths[cs];
839 int _upper = _keys + _klen - 1;
841 if ( _upper < _lower )
844 _mid = _lower + ((_upper-_lower) >> 1);
845 if ( data[p] < _JSON_integer_trans_keys[_mid] )
847 else if ( data[p] > _JSON_integer_trans_keys[_mid] )
850 _trans += (_mid - _keys);
858 _klen = _JSON_integer_range_lengths[cs];
862 int _upper = _keys + (_klen<<1) - 2;
864 if ( _upper < _lower )
867 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
868 if ( data[p] < _JSON_integer_trans_keys[_mid] )
870 else if ( data[p] > _JSON_integer_trans_keys[_mid+1] )
873 _trans += ((_mid - _keys)>>1);
881 _trans = _JSON_integer_indicies[_trans];
882 cs = _JSON_integer_trans_targs[_trans];
884 if ( _JSON_integer_trans_actions[_trans] != 0 ) {
885 _acts = _JSON_integer_trans_actions[_trans];
886 _nacts = (int) _JSON_integer_actions[_acts++];
887 while ( _nacts-- > 0 )
889 switch ( _JSON_integer_actions[_acts++] )
892 // line 480 "Parser.rl"
895 { p += 1; _goto_targ = 5; if (true) continue _goto;}
898 // line 899 "Parser.java"
918 // line 494 "Parser.rl"
920 if (cs < JSON_integer_first_final) {
924 ByteList num = absSubSequence(memo, p);
925 // note: this is actually a shared string, but since it is temporary and
926 // read-only, it doesn't really matter
927 RubyString expr = RubyString.newStringLight(getRuntime(), num);
928 RubyInteger number = RubyNumeric.str2inum(getRuntime(), expr, 10, true);
929 return new ParserResult(number, p + 1);
933 // line 934 "Parser.java"
934 private static byte[] init__JSON_float_actions_0()
941 private static final byte _JSON_float_actions[] = init__JSON_float_actions_0();
944 private static byte[] init__JSON_float_key_offsets_0()
947 0, 0, 4, 7, 10, 12, 16, 18, 23, 29, 29
951 private static final byte _JSON_float_key_offsets[] = init__JSON_float_key_offsets_0();
954 private static char[] init__JSON_float_trans_keys_0()
957 45, 48, 49, 57, 48, 49, 57, 46, 69, 101, 48, 57,
958 43, 45, 48, 57, 48, 57, 46, 69, 101, 48, 57, 69,
959 101, 45, 46, 48, 57, 69, 101, 45, 46, 48, 57, 0
963 private static final char _JSON_float_trans_keys[] = init__JSON_float_trans_keys_0();
966 private static byte[] init__JSON_float_single_lengths_0()
969 0, 2, 1, 3, 0, 2, 0, 3, 2, 0, 2
973 private static final byte _JSON_float_single_lengths[] = init__JSON_float_single_lengths_0();
976 private static byte[] init__JSON_float_range_lengths_0()
979 0, 1, 1, 0, 1, 1, 1, 1, 2, 0, 2
983 private static final byte _JSON_float_range_lengths[] = init__JSON_float_range_lengths_0();
986 private static byte[] init__JSON_float_index_offsets_0()
989 0, 0, 4, 7, 11, 13, 17, 19, 24, 29, 30
993 private static final byte _JSON_float_index_offsets[] = init__JSON_float_index_offsets_0();
996 private static byte[] init__JSON_float_indicies_0()
999 0, 2, 3, 1, 2, 3, 1, 4, 5, 5, 1, 6,
1000 1, 7, 7, 8, 1, 8, 1, 4, 5, 5, 3, 1,
1001 5, 5, 1, 6, 9, 1, 1, 1, 1, 8, 9, 0
1005 private static final byte _JSON_float_indicies[] = init__JSON_float_indicies_0();
1008 private static byte[] init__JSON_float_trans_targs_0()
1010 return new byte [] {
1011 2, 0, 3, 7, 4, 5, 8, 6, 10, 9
1015 private static final byte _JSON_float_trans_targs[] = init__JSON_float_trans_targs_0();
1018 private static byte[] init__JSON_float_trans_actions_0()
1020 return new byte [] {
1021 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1025 private static final byte _JSON_float_trans_actions[] = init__JSON_float_trans_actions_0();
1028 static final int JSON_float_start = 1;
1029 static final int JSON_float_first_final = 8;
1030 static final int JSON_float_error = 0;
1032 static final int JSON_float_en_main = 1;
1035 // line 522 "Parser.rl"
1038 ParserResult parseFloat(int p, int pe) {
1042 // line 1043 "Parser.java"
1044 cs = JSON_float_start;
1047 // line 528 "Parser.rl"
1050 // line 1051 "Parser.java"
1059 _goto: while (true) {
1060 switch ( _goto_targ ) {
1072 _keys = _JSON_float_key_offsets[cs];
1073 _trans = _JSON_float_index_offsets[cs];
1074 _klen = _JSON_float_single_lengths[cs];
1078 int _upper = _keys + _klen - 1;
1080 if ( _upper < _lower )
1083 _mid = _lower + ((_upper-_lower) >> 1);
1084 if ( data[p] < _JSON_float_trans_keys[_mid] )
1086 else if ( data[p] > _JSON_float_trans_keys[_mid] )
1089 _trans += (_mid - _keys);
1097 _klen = _JSON_float_range_lengths[cs];
1101 int _upper = _keys + (_klen<<1) - 2;
1103 if ( _upper < _lower )
1106 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
1107 if ( data[p] < _JSON_float_trans_keys[_mid] )
1109 else if ( data[p] > _JSON_float_trans_keys[_mid+1] )
1112 _trans += ((_mid - _keys)>>1);
1120 _trans = _JSON_float_indicies[_trans];
1121 cs = _JSON_float_trans_targs[_trans];
1123 if ( _JSON_float_trans_actions[_trans] != 0 ) {
1124 _acts = _JSON_float_trans_actions[_trans];
1125 _nacts = (int) _JSON_float_actions[_acts++];
1126 while ( _nacts-- > 0 )
1128 switch ( _JSON_float_actions[_acts++] )
1131 // line 513 "Parser.rl"
1134 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1137 // line 1138 "Parser.java"
1157 // line 530 "Parser.rl"
1159 if (cs < JSON_float_first_final) {
1163 ByteList num = absSubSequence(memo, p);
1164 // note: this is actually a shared string, but since it is temporary and
1165 // read-only, it doesn't really matter
1166 RubyString expr = RubyString.newStringLight(getRuntime(), num);
1167 RubyFloat number = RubyNumeric.str2fnum(getRuntime(), expr, true);
1168 return new ParserResult(number, p + 1);
1172 // line 1173 "Parser.java"
1173 private static byte[] init__JSON_string_actions_0()
1175 return new byte [] {
1180 private static final byte _JSON_string_actions[] = init__JSON_string_actions_0();
1183 private static byte[] init__JSON_string_key_offsets_0()
1185 return new byte [] {
1186 0, 0, 1, 5, 8, 14, 20, 26, 32
1190 private static final byte _JSON_string_key_offsets[] = init__JSON_string_key_offsets_0();
1193 private static char[] init__JSON_string_trans_keys_0()
1195 return new char [] {
1196 34, 34, 92, 0, 31, 117, 0, 31, 48, 57, 65, 70,
1197 97, 102, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70,
1198 97, 102, 48, 57, 65, 70, 97, 102, 0
1202 private static final char _JSON_string_trans_keys[] = init__JSON_string_trans_keys_0();
1205 private static byte[] init__JSON_string_single_lengths_0()
1207 return new byte [] {
1208 0, 1, 2, 1, 0, 0, 0, 0, 0
1212 private static final byte _JSON_string_single_lengths[] = init__JSON_string_single_lengths_0();
1215 private static byte[] init__JSON_string_range_lengths_0()
1217 return new byte [] {
1218 0, 0, 1, 1, 3, 3, 3, 3, 0
1222 private static final byte _JSON_string_range_lengths[] = init__JSON_string_range_lengths_0();
1225 private static byte[] init__JSON_string_index_offsets_0()
1227 return new byte [] {
1228 0, 0, 2, 6, 9, 13, 17, 21, 25
1232 private static final byte _JSON_string_index_offsets[] = init__JSON_string_index_offsets_0();
1235 private static byte[] init__JSON_string_indicies_0()
1237 return new byte [] {
1238 0, 1, 2, 3, 1, 0, 4, 1, 0, 5, 5, 5,
1239 1, 6, 6, 6, 1, 7, 7, 7, 1, 0, 0, 0,
1244 private static final byte _JSON_string_indicies[] = init__JSON_string_indicies_0();
1247 private static byte[] init__JSON_string_trans_targs_0()
1249 return new byte [] {
1250 2, 0, 8, 3, 4, 5, 6, 7
1254 private static final byte _JSON_string_trans_targs[] = init__JSON_string_trans_targs_0();
1257 private static byte[] init__JSON_string_trans_actions_0()
1259 return new byte [] {
1260 0, 0, 1, 0, 0, 0, 0, 0
1264 private static final byte _JSON_string_trans_actions[] = init__JSON_string_trans_actions_0();
1267 static final int JSON_string_start = 1;
1268 static final int JSON_string_first_final = 8;
1269 static final int JSON_string_error = 0;
1271 static final int JSON_string_en_main = 1;
1274 // line 574 "Parser.rl"
1277 ParserResult parseString(int p, int pe) {
1279 IRubyObject result = null;
1282 // line 1283 "Parser.java"
1284 cs = JSON_string_start;
1287 // line 581 "Parser.rl"
1290 // line 1291 "Parser.java"
1299 _goto: while (true) {
1300 switch ( _goto_targ ) {
1312 _keys = _JSON_string_key_offsets[cs];
1313 _trans = _JSON_string_index_offsets[cs];
1314 _klen = _JSON_string_single_lengths[cs];
1318 int _upper = _keys + _klen - 1;
1320 if ( _upper < _lower )
1323 _mid = _lower + ((_upper-_lower) >> 1);
1324 if ( data[p] < _JSON_string_trans_keys[_mid] )
1326 else if ( data[p] > _JSON_string_trans_keys[_mid] )
1329 _trans += (_mid - _keys);
1337 _klen = _JSON_string_range_lengths[cs];
1341 int _upper = _keys + (_klen<<1) - 2;
1343 if ( _upper < _lower )
1346 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
1347 if ( data[p] < _JSON_string_trans_keys[_mid] )
1349 else if ( data[p] > _JSON_string_trans_keys[_mid+1] )
1352 _trans += ((_mid - _keys)>>1);
1360 _trans = _JSON_string_indicies[_trans];
1361 cs = _JSON_string_trans_targs[_trans];
1363 if ( _JSON_string_trans_actions[_trans] != 0 ) {
1364 _acts = _JSON_string_trans_actions[_trans];
1365 _nacts = (int) _JSON_string_actions[_acts++];
1366 while ( _nacts-- > 0 )
1368 switch ( _JSON_string_actions[_acts++] )
1371 // line 549 "Parser.rl"
1373 int offset = byteList.begin();
1374 ByteList decoded = decoder.decode(byteList, memo + 1 - offset,
1376 result = getRuntime().newString(decoded);
1377 if (result == null) {
1379 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1386 // line 562 "Parser.rl"
1389 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1392 // line 1393 "Parser.java"
1412 // line 583 "Parser.rl"
1414 if (parser.createAdditions) {
1415 RubyHash match_string = parser.match_string;
1416 if (match_string != null) {
1417 final IRubyObject[] memoArray = { result, null };
1419 match_string.visitAll(new RubyHash.Visitor() {
1421 public void visit(IRubyObject pattern, IRubyObject klass) {
1422 if (pattern.callMethod(context, "===", memoArray[0]).isTrue()) {
1423 memoArray[1] = klass;
1424 throw JumpException.SPECIAL_JUMP;
1428 } catch (JumpException e) { }
1429 if (memoArray[1] != null) {
1430 RubyClass klass = (RubyClass) memoArray[1];
1431 if (klass.respondsTo("json_creatable?") &&
1432 klass.callMethod(context, "json_creatable?").isTrue()) {
1433 result = klass.callMethod(context, "json_create", result);
1439 if (cs >= JSON_string_first_final && result != null) {
1440 return new ParserResult(result, p + 1);
1447 // line 1448 "Parser.java"
1448 private static byte[] init__JSON_array_actions_0()
1450 return new byte [] {
1455 private static final byte _JSON_array_actions[] = init__JSON_array_actions_0();
1458 private static byte[] init__JSON_array_key_offsets_0()
1460 return new byte [] {
1461 0, 0, 1, 18, 25, 41, 43, 44, 46, 47, 49, 50,
1462 52, 53, 55, 56, 58, 59
1466 private static final byte _JSON_array_key_offsets[] = init__JSON_array_key_offsets_0();
1469 private static char[] init__JSON_array_trans_keys_0()
1471 return new char [] {
1472 91, 13, 32, 34, 45, 47, 73, 78, 91, 93, 102, 110,
1473 116, 123, 9, 10, 48, 57, 13, 32, 44, 47, 93, 9,
1474 10, 13, 32, 34, 45, 47, 73, 78, 91, 102, 110, 116,
1475 123, 9, 10, 48, 57, 42, 47, 42, 42, 47, 10, 42,
1476 47, 42, 42, 47, 10, 42, 47, 42, 42, 47, 10, 0
1480 private static final char _JSON_array_trans_keys[] = init__JSON_array_trans_keys_0();
1483 private static byte[] init__JSON_array_single_lengths_0()
1485 return new byte [] {
1486 0, 1, 13, 5, 12, 2, 1, 2, 1, 2, 1, 2,
1491 private static final byte _JSON_array_single_lengths[] = init__JSON_array_single_lengths_0();
1494 private static byte[] init__JSON_array_range_lengths_0()
1496 return new byte [] {
1497 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0,
1502 private static final byte _JSON_array_range_lengths[] = init__JSON_array_range_lengths_0();
1505 private static byte[] init__JSON_array_index_offsets_0()
1507 return new byte [] {
1508 0, 0, 2, 18, 25, 40, 43, 45, 48, 50, 53, 55,
1509 58, 60, 63, 65, 68, 70
1513 private static final byte _JSON_array_index_offsets[] = init__JSON_array_index_offsets_0();
1516 private static byte[] init__JSON_array_indicies_0()
1518 return new byte [] {
1519 0, 1, 0, 0, 2, 2, 3, 2, 2, 2, 4, 2,
1520 2, 2, 2, 0, 2, 1, 5, 5, 6, 7, 4, 5,
1521 1, 6, 6, 2, 2, 8, 2, 2, 2, 2, 2, 2,
1522 2, 6, 2, 1, 9, 10, 1, 11, 9, 11, 6, 9,
1523 6, 10, 12, 13, 1, 14, 12, 14, 5, 12, 5, 13,
1524 15, 16, 1, 17, 15, 17, 0, 15, 0, 16, 1, 0
1528 private static final byte _JSON_array_indicies[] = init__JSON_array_indicies_0();
1531 private static byte[] init__JSON_array_trans_targs_0()
1533 return new byte [] {
1534 2, 0, 3, 13, 17, 3, 4, 9, 5, 6, 8, 7,
1535 10, 12, 11, 14, 16, 15
1539 private static final byte _JSON_array_trans_targs[] = init__JSON_array_trans_targs_0();
1542 private static byte[] init__JSON_array_trans_actions_0()
1544 return new byte [] {
1545 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0,
1550 private static final byte _JSON_array_trans_actions[] = init__JSON_array_trans_actions_0();
1553 static final int JSON_array_start = 1;
1554 static final int JSON_array_first_final = 17;
1555 static final int JSON_array_error = 0;
1557 static final int JSON_array_en_main = 1;
1560 // line 653 "Parser.rl"
1563 ParserResult parseArray(int p, int pe) {
1566 if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) {
1567 throw newException(Utils.M_NESTING_ERROR,
1568 "nesting of " + currentNesting + " is too deep");
1571 // this is guaranteed to be a RubyArray due to the earlier
1572 // allocator test at OptionsReader#getClass
1574 (RubyArray)parser.arrayClass.newInstance(context,
1575 IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
1578 // line 1579 "Parser.java"
1580 cs = JSON_array_start;
1583 // line 670 "Parser.rl"
1585 // line 1586 "Parser.java"
1594 _goto: while (true) {
1595 switch ( _goto_targ ) {
1607 _keys = _JSON_array_key_offsets[cs];
1608 _trans = _JSON_array_index_offsets[cs];
1609 _klen = _JSON_array_single_lengths[cs];
1613 int _upper = _keys + _klen - 1;
1615 if ( _upper < _lower )
1618 _mid = _lower + ((_upper-_lower) >> 1);
1619 if ( data[p] < _JSON_array_trans_keys[_mid] )
1621 else if ( data[p] > _JSON_array_trans_keys[_mid] )
1624 _trans += (_mid - _keys);
1632 _klen = _JSON_array_range_lengths[cs];
1636 int _upper = _keys + (_klen<<1) - 2;
1638 if ( _upper < _lower )
1641 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
1642 if ( data[p] < _JSON_array_trans_keys[_mid] )
1644 else if ( data[p] > _JSON_array_trans_keys[_mid+1] )
1647 _trans += ((_mid - _keys)>>1);
1655 _trans = _JSON_array_indicies[_trans];
1656 cs = _JSON_array_trans_targs[_trans];
1658 if ( _JSON_array_trans_actions[_trans] != 0 ) {
1659 _acts = _JSON_array_trans_actions[_trans];
1660 _nacts = (int) _JSON_array_actions[_acts++];
1661 while ( _nacts-- > 0 )
1663 switch ( _JSON_array_actions[_acts++] )
1666 // line 622 "Parser.rl"
1668 ParserResult res = parseValue(p, pe);
1671 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1673 if (!parser.arrayClass.getName().equals("Array")) {
1674 result.callMethod(context, "<<", res.result);
1676 result.append(res.result);
1683 // line 637 "Parser.rl"
1686 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1689 // line 1690 "Parser.java"
1709 // line 671 "Parser.rl"
1711 if (cs >= JSON_array_first_final) {
1712 return new ParserResult(result, p + 1);
1714 throw unexpectedToken(p, pe);
1719 // line 1720 "Parser.java"
1720 private static byte[] init__JSON_object_actions_0()
1722 return new byte [] {
1727 private static final byte _JSON_object_actions[] = init__JSON_object_actions_0();
1730 private static byte[] init__JSON_object_key_offsets_0()
1732 return new byte [] {
1733 0, 0, 1, 8, 14, 16, 17, 19, 20, 36, 43, 49,
1734 51, 52, 54, 55, 57, 58, 60, 61, 63, 64, 66, 67,
1739 private static final byte _JSON_object_key_offsets[] = init__JSON_object_key_offsets_0();
1742 private static char[] init__JSON_object_trans_keys_0()
1744 return new char [] {
1745 123, 13, 32, 34, 47, 125, 9, 10, 13, 32, 47, 58,
1746 9, 10, 42, 47, 42, 42, 47, 10, 13, 32, 34, 45,
1747 47, 73, 78, 91, 102, 110, 116, 123, 9, 10, 48, 57,
1748 13, 32, 44, 47, 125, 9, 10, 13, 32, 34, 47, 9,
1749 10, 42, 47, 42, 42, 47, 10, 42, 47, 42, 42, 47,
1750 10, 42, 47, 42, 42, 47, 10, 42, 47, 42, 42, 47,
1755 private static final char _JSON_object_trans_keys[] = init__JSON_object_trans_keys_0();
1758 private static byte[] init__JSON_object_single_lengths_0()
1760 return new byte [] {
1761 0, 1, 5, 4, 2, 1, 2, 1, 12, 5, 4, 2,
1762 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
1767 private static final byte _JSON_object_single_lengths[] = init__JSON_object_single_lengths_0();
1770 private static byte[] init__JSON_object_range_lengths_0()
1772 return new byte [] {
1773 0, 0, 1, 1, 0, 0, 0, 0, 2, 1, 1, 0,
1774 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1779 private static final byte _JSON_object_range_lengths[] = init__JSON_object_range_lengths_0();
1782 private static byte[] init__JSON_object_index_offsets_0()
1784 return new byte [] {
1785 0, 0, 2, 9, 15, 18, 20, 23, 25, 40, 47, 53,
1786 56, 58, 61, 63, 66, 68, 71, 73, 76, 78, 81, 83,
1791 private static final byte _JSON_object_index_offsets[] = init__JSON_object_index_offsets_0();
1794 private static byte[] init__JSON_object_indicies_0()
1796 return new byte [] {
1797 0, 1, 0, 0, 2, 3, 4, 0, 1, 5, 5, 6,
1798 7, 5, 1, 8, 9, 1, 10, 8, 10, 5, 8, 5,
1799 9, 7, 7, 11, 11, 12, 11, 11, 11, 11, 11, 11,
1800 11, 7, 11, 1, 13, 13, 14, 15, 4, 13, 1, 14,
1801 14, 2, 16, 14, 1, 17, 18, 1, 19, 17, 19, 14,
1802 17, 14, 18, 20, 21, 1, 22, 20, 22, 13, 20, 13,
1803 21, 23, 24, 1, 25, 23, 25, 7, 23, 7, 24, 26,
1804 27, 1, 28, 26, 28, 0, 26, 0, 27, 1, 0
1808 private static final byte _JSON_object_indicies[] = init__JSON_object_indicies_0();
1811 private static byte[] init__JSON_object_trans_targs_0()
1813 return new byte [] {
1814 2, 0, 3, 23, 27, 3, 4, 8, 5, 7, 6, 9,
1815 19, 9, 10, 15, 11, 12, 14, 13, 16, 18, 17, 20,
1820 private static final byte _JSON_object_trans_targs[] = init__JSON_object_trans_targs_0();
1823 private static byte[] init__JSON_object_trans_actions_0()
1825 return new byte [] {
1826 0, 0, 3, 0, 5, 0, 0, 0, 0, 0, 0, 1,
1827 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1832 private static final byte _JSON_object_trans_actions[] = init__JSON_object_trans_actions_0();
1835 static final int JSON_object_start = 1;
1836 static final int JSON_object_first_final = 27;
1837 static final int JSON_object_error = 0;
1839 static final int JSON_object_en_main = 1;
1842 // line 730 "Parser.rl"
1845 ParserResult parseObject(int p, int pe) {
1847 IRubyObject lastName = null;
1849 if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) {
1850 throw newException(Utils.M_NESTING_ERROR,
1851 "nesting of " + currentNesting + " is too deep");
1854 // this is guaranteed to be a RubyHash due to the earlier
1855 // allocator test at OptionsReader#getClass
1857 (RubyHash)parser.objectClass.newInstance(context,
1858 IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
1861 // line 1862 "Parser.java"
1863 cs = JSON_object_start;
1866 // line 748 "Parser.rl"
1868 // line 1869 "Parser.java"
1877 _goto: while (true) {
1878 switch ( _goto_targ ) {
1890 _keys = _JSON_object_key_offsets[cs];
1891 _trans = _JSON_object_index_offsets[cs];
1892 _klen = _JSON_object_single_lengths[cs];
1896 int _upper = _keys + _klen - 1;
1898 if ( _upper < _lower )
1901 _mid = _lower + ((_upper-_lower) >> 1);
1902 if ( data[p] < _JSON_object_trans_keys[_mid] )
1904 else if ( data[p] > _JSON_object_trans_keys[_mid] )
1907 _trans += (_mid - _keys);
1915 _klen = _JSON_object_range_lengths[cs];
1919 int _upper = _keys + (_klen<<1) - 2;
1921 if ( _upper < _lower )
1924 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
1925 if ( data[p] < _JSON_object_trans_keys[_mid] )
1927 else if ( data[p] > _JSON_object_trans_keys[_mid+1] )
1930 _trans += ((_mid - _keys)>>1);
1938 _trans = _JSON_object_indicies[_trans];
1939 cs = _JSON_object_trans_targs[_trans];
1941 if ( _JSON_object_trans_actions[_trans] != 0 ) {
1942 _acts = _JSON_object_trans_actions[_trans];
1943 _nacts = (int) _JSON_object_actions[_acts++];
1944 while ( _nacts-- > 0 )
1946 switch ( _JSON_object_actions[_acts++] )
1949 // line 685 "Parser.rl"
1951 ParserResult res = parseValue(p, pe);
1954 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1956 if (!parser.objectClass.getName().equals("Hash")) {
1957 result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result });
1959 result.op_aset(context, lastName, res.result);
1966 // line 700 "Parser.rl"
1968 ParserResult res = parseString(p, pe);
1971 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1973 RubyString name = (RubyString)res.result;
1974 if (parser.symbolizeNames) {
1975 lastName = context.getRuntime().is1_9()
1986 // line 718 "Parser.rl"
1989 { p += 1; _goto_targ = 5; if (true) continue _goto;}
1992 // line 1993 "Parser.java"
2012 // line 749 "Parser.rl"
2014 if (cs < JSON_object_first_final) {
2018 IRubyObject returnedResult = result;
2020 // attempt to de-serialize object
2021 if (parser.createAdditions) {
2022 IRubyObject vKlassName = result.op_aref(context, parser.createId);
2023 if (!vKlassName.isNil()) {
2024 // might throw ArgumentError, we let it propagate
2025 IRubyObject klass = parser.info.jsonModule.get().
2026 callMethod(context, "deep_const_get", vKlassName);
2027 if (klass.respondsTo("json_creatable?") &&
2028 klass.callMethod(context, "json_creatable?").isTrue()) {
2030 returnedResult = klass.callMethod(context, "json_create", result);
2034 return new ParserResult(returnedResult, p + 1);
2038 // line 2039 "Parser.java"
2039 private static byte[] init__JSON_actions_0()
2041 return new byte [] {
2046 private static final byte _JSON_actions[] = init__JSON_actions_0();
2049 private static byte[] init__JSON_key_offsets_0()
2051 return new byte [] {
2052 0, 0, 7, 9, 10, 12, 13, 15, 16, 18, 19
2056 private static final byte _JSON_key_offsets[] = init__JSON_key_offsets_0();
2059 private static char[] init__JSON_trans_keys_0()
2061 return new char [] {
2062 13, 32, 47, 91, 123, 9, 10, 42, 47, 42, 42, 47,
2063 10, 42, 47, 42, 42, 47, 10, 13, 32, 47, 9, 10,
2068 private static final char _JSON_trans_keys[] = init__JSON_trans_keys_0();
2071 private static byte[] init__JSON_single_lengths_0()
2073 return new byte [] {
2074 0, 5, 2, 1, 2, 1, 2, 1, 2, 1, 3
2078 private static final byte _JSON_single_lengths[] = init__JSON_single_lengths_0();
2081 private static byte[] init__JSON_range_lengths_0()
2083 return new byte [] {
2084 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
2088 private static final byte _JSON_range_lengths[] = init__JSON_range_lengths_0();
2091 private static byte[] init__JSON_index_offsets_0()
2093 return new byte [] {
2094 0, 0, 7, 10, 12, 15, 17, 20, 22, 25, 27
2098 private static final byte _JSON_index_offsets[] = init__JSON_index_offsets_0();
2101 private static byte[] init__JSON_indicies_0()
2103 return new byte [] {
2104 0, 0, 2, 3, 4, 0, 1, 5, 6, 1, 7, 5,
2105 7, 0, 5, 0, 6, 8, 9, 1, 10, 8, 10, 11,
2106 8, 11, 9, 11, 11, 12, 11, 1, 0
2110 private static final byte _JSON_indicies[] = init__JSON_indicies_0();
2113 private static byte[] init__JSON_trans_targs_0()
2115 return new byte [] {
2116 1, 0, 2, 10, 10, 3, 5, 4, 7, 9, 8, 10,
2121 private static final byte _JSON_trans_targs[] = init__JSON_trans_targs_0();
2124 private static byte[] init__JSON_trans_actions_0()
2126 return new byte [] {
2127 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0,
2132 private static final byte _JSON_trans_actions[] = init__JSON_trans_actions_0();
2135 static final int JSON_start = 1;
2136 static final int JSON_first_final = 10;
2137 static final int JSON_error = 0;
2139 static final int JSON_en_main = 1;
2142 // line 807 "Parser.rl"
2145 public IRubyObject parseStrict() {
2148 IRubyObject result = null;
2151 // line 2152 "Parser.java"
2156 // line 815 "Parser.rl"
2157 p = byteList.begin();
2158 pe = p + byteList.length();
2160 // line 2161 "Parser.java"
2169 _goto: while (true) {
2170 switch ( _goto_targ ) {
2182 _keys = _JSON_key_offsets[cs];
2183 _trans = _JSON_index_offsets[cs];
2184 _klen = _JSON_single_lengths[cs];
2188 int _upper = _keys + _klen - 1;
2190 if ( _upper < _lower )
2193 _mid = _lower + ((_upper-_lower) >> 1);
2194 if ( data[p] < _JSON_trans_keys[_mid] )
2196 else if ( data[p] > _JSON_trans_keys[_mid] )
2199 _trans += (_mid - _keys);
2207 _klen = _JSON_range_lengths[cs];
2211 int _upper = _keys + (_klen<<1) - 2;
2213 if ( _upper < _lower )
2216 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
2217 if ( data[p] < _JSON_trans_keys[_mid] )
2219 else if ( data[p] > _JSON_trans_keys[_mid+1] )
2222 _trans += ((_mid - _keys)>>1);
2230 _trans = _JSON_indicies[_trans];
2231 cs = _JSON_trans_targs[_trans];
2233 if ( _JSON_trans_actions[_trans] != 0 ) {
2234 _acts = _JSON_trans_actions[_trans];
2235 _nacts = (int) _JSON_actions[_acts++];
2236 while ( _nacts-- > 0 )
2238 switch ( _JSON_actions[_acts++] )
2241 // line 779 "Parser.rl"
2244 ParserResult res = parseObject(p, pe);
2247 { p += 1; _goto_targ = 5; if (true) continue _goto;}
2249 result = res.result;
2255 // line 791 "Parser.rl"
2258 ParserResult res = parseArray(p, pe);
2261 { p += 1; _goto_targ = 5; if (true) continue _goto;}
2263 result = res.result;
2268 // line 2269 "Parser.java"
2288 // line 818 "Parser.rl"
2290 if (cs >= JSON_first_final && p == pe) {
2293 throw unexpectedToken(p, pe);
2298 // line 2299 "Parser.java"
2299 private static byte[] init__JSON_quirks_mode_actions_0()
2301 return new byte [] {
2306 private static final byte _JSON_quirks_mode_actions[] = init__JSON_quirks_mode_actions_0();
2309 private static byte[] init__JSON_quirks_mode_key_offsets_0()
2311 return new byte [] {
2312 0, 0, 16, 18, 19, 21, 22, 24, 25, 27, 28
2316 private static final byte _JSON_quirks_mode_key_offsets[] = init__JSON_quirks_mode_key_offsets_0();
2319 private static char[] init__JSON_quirks_mode_trans_keys_0()
2321 return new char [] {
2322 13, 32, 34, 45, 47, 73, 78, 91, 102, 110, 116, 123,
2323 9, 10, 48, 57, 42, 47, 42, 42, 47, 10, 42, 47,
2324 42, 42, 47, 10, 13, 32, 47, 9, 10, 0
2328 private static final char _JSON_quirks_mode_trans_keys[] = init__JSON_quirks_mode_trans_keys_0();
2331 private static byte[] init__JSON_quirks_mode_single_lengths_0()
2333 return new byte [] {
2334 0, 12, 2, 1, 2, 1, 2, 1, 2, 1, 3
2338 private static final byte _JSON_quirks_mode_single_lengths[] = init__JSON_quirks_mode_single_lengths_0();
2341 private static byte[] init__JSON_quirks_mode_range_lengths_0()
2343 return new byte [] {
2344 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1
2348 private static final byte _JSON_quirks_mode_range_lengths[] = init__JSON_quirks_mode_range_lengths_0();
2351 private static byte[] init__JSON_quirks_mode_index_offsets_0()
2353 return new byte [] {
2354 0, 0, 15, 18, 20, 23, 25, 28, 30, 33, 35
2358 private static final byte _JSON_quirks_mode_index_offsets[] = init__JSON_quirks_mode_index_offsets_0();
2361 private static byte[] init__JSON_quirks_mode_indicies_0()
2363 return new byte [] {
2364 0, 0, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2,
2365 0, 2, 1, 4, 5, 1, 6, 4, 6, 7, 4, 7,
2366 5, 8, 9, 1, 10, 8, 10, 0, 8, 0, 9, 7,
2371 private static final byte _JSON_quirks_mode_indicies[] = init__JSON_quirks_mode_indicies_0();
2374 private static byte[] init__JSON_quirks_mode_trans_targs_0()
2376 return new byte [] {
2377 1, 0, 10, 6, 3, 5, 4, 10, 7, 9, 8, 2
2381 private static final byte _JSON_quirks_mode_trans_targs[] = init__JSON_quirks_mode_trans_targs_0();
2384 private static byte[] init__JSON_quirks_mode_trans_actions_0()
2386 return new byte [] {
2387 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
2391 private static final byte _JSON_quirks_mode_trans_actions[] = init__JSON_quirks_mode_trans_actions_0();
2394 static final int JSON_quirks_mode_start = 1;
2395 static final int JSON_quirks_mode_first_final = 10;
2396 static final int JSON_quirks_mode_error = 0;
2398 static final int JSON_quirks_mode_en_main = 1;
2401 // line 846 "Parser.rl"
2404 public IRubyObject parseQuirksMode() {
2407 IRubyObject result = null;
2410 // line 2411 "Parser.java"
2412 cs = JSON_quirks_mode_start;
2415 // line 854 "Parser.rl"
2416 p = byteList.begin();
2417 pe = p + byteList.length();
2419 // line 2420 "Parser.java"
2428 _goto: while (true) {
2429 switch ( _goto_targ ) {
2441 _keys = _JSON_quirks_mode_key_offsets[cs];
2442 _trans = _JSON_quirks_mode_index_offsets[cs];
2443 _klen = _JSON_quirks_mode_single_lengths[cs];
2447 int _upper = _keys + _klen - 1;
2449 if ( _upper < _lower )
2452 _mid = _lower + ((_upper-_lower) >> 1);
2453 if ( data[p] < _JSON_quirks_mode_trans_keys[_mid] )
2455 else if ( data[p] > _JSON_quirks_mode_trans_keys[_mid] )
2458 _trans += (_mid - _keys);
2466 _klen = _JSON_quirks_mode_range_lengths[cs];
2470 int _upper = _keys + (_klen<<1) - 2;
2472 if ( _upper < _lower )
2475 _mid = _lower + (((_upper-_lower) >> 1) & ~1);
2476 if ( data[p] < _JSON_quirks_mode_trans_keys[_mid] )
2478 else if ( data[p] > _JSON_quirks_mode_trans_keys[_mid+1] )
2481 _trans += ((_mid - _keys)>>1);
2489 _trans = _JSON_quirks_mode_indicies[_trans];
2490 cs = _JSON_quirks_mode_trans_targs[_trans];
2492 if ( _JSON_quirks_mode_trans_actions[_trans] != 0 ) {
2493 _acts = _JSON_quirks_mode_trans_actions[_trans];
2494 _nacts = (int) _JSON_quirks_mode_actions[_acts++];
2495 while ( _nacts-- > 0 )
2497 switch ( _JSON_quirks_mode_actions[_acts++] )
2500 // line 832 "Parser.rl"
2502 ParserResult res = parseValue(p, pe);
2505 { p += 1; _goto_targ = 5; if (true) continue _goto;}
2507 result = res.result;
2512 // line 2513 "Parser.java"
2532 // line 857 "Parser.rl"
2534 if (cs >= JSON_quirks_mode_first_final && p == pe) {
2537 throw unexpectedToken(p, pe);
2541 public IRubyObject parse() {
2542 if (parser.quirksMode) {
2543 return parseQuirksMode();
2545 return parseStrict();
2551 * Returns a subsequence of the source ByteList, based on source
2552 * array byte offsets (i.e., the ByteList's own begin offset is not
2553 * automatically added).
2557 private ByteList absSubSequence(int absStart, int absEnd) {
2558 int offset = byteList.begin();
2559 return (ByteList)byteList.subSequence(absStart - offset,
2564 * Retrieves a constant directly descended from the <code>JSON</code> module.
2565 * @param name The constant name
2567 private IRubyObject getConstant(String name) {
2568 return parser.info.jsonModule.get().getConstant(name);
2571 private RaiseException newException(String className, String message) {
2572 return Utils.newException(context, className, message);
2575 private RaiseException newException(String className, RubyString message) {
2576 return Utils.newException(context, className, message);
2579 private RaiseException newException(String className,
2580 String messageBegin, ByteList messageEnd) {
2581 return newException(className,
2582 getRuntime().newString(messageBegin).cat(messageEnd));