#line 1 "parser.rl" /* vim: set cin et sw=4 ts=4: */ #include "ruby.h" #include "re.h" #include "unicode.h" #ifndef swap16 #define swap16(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) #endif #define EVIL 0x666 static VALUE mJSON, mExt, cParser, eParserError; static ID i_json_creatable_p, i_json_create, i_create_id, i_chr; typedef struct JSON_ParserStruct { VALUE Vsource; char *source; long len; char *memo; VALUE create_id; } JSON_Parser; static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result); static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result); static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result); static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result); static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result); static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result); #define GET_STRUCT \ JSON_Parser *json; \ Data_Get_Struct(self, JSON_Parser, json); #line 59 "parser.rl" #line 42 "parser.c" static const int JSON_object_start = 0; static const int JSON_object_first_final = 27; static const int JSON_object_error = 1; #line 92 "parser.rl" static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; VALUE last_name = Qnil; *result = rb_hash_new(); #line 59 "parser.c" { cs = JSON_object_start; } #line 101 "parser.rl" #line 65 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: if ( (*p) == 123 ) goto st2; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: switch( (*p) ) { case 13: goto st2; case 32: goto st2; case 34: goto tr13; case 47: goto st23; case 125: goto tr10; } if ( 9 <= (*p) && (*p) <= 10 ) goto st2; goto st1; tr13: #line 78 "parser.rl" { char *np = JSON_parse_string(json, p, pe, &last_name); if (np == NULL) goto _out3; else {p = (( np))-1;} } goto st3; st3: if ( ++p == pe ) goto _out3; case 3: #line 102 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; case 47: goto st4; case 58: goto st8; } if ( 9 <= (*p) && (*p) <= 10 ) goto st3; goto st1; st4: if ( ++p == pe ) goto _out4; case 4: switch( (*p) ) { case 42: goto st5; case 47: goto st7; } goto st1; st5: if ( ++p == pe ) goto _out5; case 5: if ( (*p) == 42 ) goto st6; goto st5; st6: if ( ++p == pe ) goto _out6; case 6: switch( (*p) ) { case 42: goto st6; case 47: goto st3; } goto st5; st7: if ( ++p == pe ) goto _out7; case 7: if ( (*p) == 10 ) goto st3; goto st7; st8: if ( ++p == pe ) goto _out8; case 8: switch( (*p) ) { case 13: goto st8; case 32: goto st8; case 34: goto tr11; case 45: goto tr11; case 47: goto st19; case 91: goto tr11; case 102: goto tr11; case 110: goto tr11; case 116: goto tr11; case 123: goto tr11; } if ( (*p) > 10 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr11; } else if ( (*p) >= 9 ) goto st8; goto st1; tr11: #line 67 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { goto _out9; } else { rb_hash_aset(*result, last_name, v); {p = (( np))-1;} } } goto st9; st9: if ( ++p == pe ) goto _out9; case 9: #line 183 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; case 44: goto st10; case 47: goto st15; case 125: goto tr10; } if ( 9 <= (*p) && (*p) <= 10 ) goto st9; goto st1; st10: if ( ++p == pe ) goto _out10; case 10: switch( (*p) ) { case 13: goto st10; case 32: goto st10; case 34: goto tr13; case 47: goto st11; } if ( 9 <= (*p) && (*p) <= 10 ) goto st10; goto st1; st11: if ( ++p == pe ) goto _out11; case 11: switch( (*p) ) { case 42: goto st12; case 47: goto st14; } goto st1; st12: if ( ++p == pe ) goto _out12; case 12: if ( (*p) == 42 ) goto st13; goto st12; st13: if ( ++p == pe ) goto _out13; case 13: switch( (*p) ) { case 42: goto st13; case 47: goto st10; } goto st12; st14: if ( ++p == pe ) goto _out14; case 14: if ( (*p) == 10 ) goto st10; goto st14; st15: if ( ++p == pe ) goto _out15; case 15: switch( (*p) ) { case 42: goto st16; case 47: goto st18; } goto st1; st16: if ( ++p == pe ) goto _out16; case 16: if ( (*p) == 42 ) goto st17; goto st16; st17: if ( ++p == pe ) goto _out17; case 17: switch( (*p) ) { case 42: goto st17; case 47: goto st9; } goto st16; st18: if ( ++p == pe ) goto _out18; case 18: if ( (*p) == 10 ) goto st9; goto st18; tr10: #line 83 "parser.rl" { goto _out27; } goto st27; st27: if ( ++p == pe ) goto _out27; case 27: #line 279 "parser.c" goto st1; st19: if ( ++p == pe ) goto _out19; case 19: switch( (*p) ) { case 42: goto st20; case 47: goto st22; } goto st1; st20: if ( ++p == pe ) goto _out20; case 20: if ( (*p) == 42 ) goto st21; goto st20; st21: if ( ++p == pe ) goto _out21; case 21: switch( (*p) ) { case 42: goto st21; case 47: goto st8; } goto st20; st22: if ( ++p == pe ) goto _out22; case 22: if ( (*p) == 10 ) goto st8; goto st22; st23: if ( ++p == pe ) goto _out23; case 23: switch( (*p) ) { case 42: goto st24; case 47: goto st26; } goto st1; st24: if ( ++p == pe ) goto _out24; case 24: if ( (*p) == 42 ) goto st25; goto st24; st25: if ( ++p == pe ) goto _out25; case 25: switch( (*p) ) { case 42: goto st25; case 47: goto st2; } goto st24; st26: if ( ++p == pe ) goto _out26; case 26: if ( (*p) == 10 ) goto st2; goto st26; } _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out8: cs = 8; goto _out; _out9: cs = 9; goto _out; _out10: cs = 10; goto _out; _out11: cs = 11; goto _out; _out12: cs = 12; goto _out; _out13: cs = 13; goto _out; _out14: cs = 14; goto _out; _out15: cs = 15; goto _out; _out16: cs = 16; goto _out; _out17: cs = 17; goto _out; _out18: cs = 18; goto _out; _out27: cs = 27; goto _out; _out19: cs = 19; goto _out; _out20: cs = 20; goto _out; _out21: cs = 21; goto _out; _out22: cs = 22; goto _out; _out23: cs = 23; goto _out; _out24: cs = 24; goto _out; _out25: cs = 25; goto _out; _out26: cs = 26; goto _out; _out: {} } #line 102 "parser.rl" if (cs >= JSON_object_first_final) { VALUE klassname = rb_hash_aref(*result, json->create_id); if (!NIL_P(klassname)) { VALUE klass = rb_path2class(StringValueCStr(klassname)); if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) { *result = rb_funcall(klass, i_json_create, 1, *result); } } return p + 1; } else { return NULL; } } #line 393 "parser.c" static const int JSON_value_start = 0; static const int JSON_value_first_final = 12; static const int JSON_value_error = 1; #line 167 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; #line 408 "parser.c" { cs = JSON_value_start; } #line 174 "parser.rl" #line 414 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: switch( (*p) ) { case 34: goto tr9; case 45: goto tr10; case 91: goto tr11; case 102: goto st2; case 110: goto st6; case 116: goto st9; case 123: goto tr15; } if ( 48 <= (*p) && (*p) <= 57 ) goto tr10; goto st1; st1: goto _out1; tr5: #line 123 "parser.rl" { *result = Qnil; } goto st12; tr6: #line 126 "parser.rl" { *result = Qfalse; } goto st12; tr7: #line 129 "parser.rl" { *result = Qtrue; } goto st12; tr9: #line 132 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) goto _out12; else {p = (( np))-1;} } goto st12; tr10: #line 137 "parser.rl" { char *np; np = JSON_parse_float(json, p, pe, result); if (np != NULL) {p = (( np))-1;} np = JSON_parse_integer(json, p, pe, result); if (np != NULL) {p = (( np))-1;} goto _out12; } goto st12; tr11: #line 146 "parser.rl" { char *np = JSON_parse_array(json, p, pe, result); if (np == NULL) goto _out12; else {p = (( np))-1;} } goto st12; tr15: #line 151 "parser.rl" { char *np = JSON_parse_object(json, p, pe, result); if (np == NULL) goto _out12; else {p = (( np))-1;} } goto st12; st12: if ( ++p == pe ) goto _out12; case 12: #line 156 "parser.rl" { goto _out12; } #line 491 "parser.c" goto st1; st2: if ( ++p == pe ) goto _out2; case 2: if ( (*p) == 97 ) goto st3; goto st1; st3: if ( ++p == pe ) goto _out3; case 3: if ( (*p) == 108 ) goto st4; goto st1; st4: if ( ++p == pe ) goto _out4; case 4: if ( (*p) == 115 ) goto st5; goto st1; st5: if ( ++p == pe ) goto _out5; case 5: if ( (*p) == 101 ) goto tr6; goto st1; st6: if ( ++p == pe ) goto _out6; case 6: if ( (*p) == 117 ) goto st7; goto st1; st7: if ( ++p == pe ) goto _out7; case 7: if ( (*p) == 108 ) goto st8; goto st1; st8: if ( ++p == pe ) goto _out8; case 8: if ( (*p) == 108 ) goto tr5; goto st1; st9: if ( ++p == pe ) goto _out9; case 9: if ( (*p) == 114 ) goto st10; goto st1; st10: if ( ++p == pe ) goto _out10; case 10: if ( (*p) == 117 ) goto st11; goto st1; st11: if ( ++p == pe ) goto _out11; case 11: if ( (*p) == 101 ) goto tr7; goto st1; } _out1: cs = 1; goto _out; _out12: cs = 12; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out8: cs = 8; goto _out; _out9: cs = 9; goto _out; _out10: cs = 10; goto _out; _out11: cs = 11; goto _out; _out: {} } #line 175 "parser.rl" if (cs >= JSON_value_first_final) { return p; } else { return NULL; } } #line 589 "parser.c" static const int JSON_integer_start = 0; static const int JSON_integer_first_final = 5; static const int JSON_integer_error = 1; #line 191 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; #line 604 "parser.c" { cs = JSON_integer_start; } #line 198 "parser.rl" json->memo = p; #line 611 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: switch( (*p) ) { case 45: goto st2; case 48: goto st3; } if ( 49 <= (*p) && (*p) <= 57 ) goto st4; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: if ( (*p) == 48 ) goto st3; if ( 49 <= (*p) && (*p) <= 57 ) goto st4; goto st1; st3: if ( ++p == pe ) goto _out3; case 3: if ( 48 <= (*p) && (*p) <= 57 ) goto st1; goto tr4; tr4: #line 188 "parser.rl" { goto _out5; } goto st5; st5: if ( ++p == pe ) goto _out5; case 5: #line 651 "parser.c" goto st1; st4: if ( ++p == pe ) goto _out4; case 4: if ( 48 <= (*p) && (*p) <= 57 ) goto st4; goto tr4; } _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out5: cs = 5; goto _out; _out4: cs = 4; goto _out; _out: {} } #line 200 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; *result = rb_Integer(rb_str_new(json->memo, len)); return p + 1; } else { return NULL; } } #line 681 "parser.c" static const int JSON_float_start = 0; static const int JSON_float_first_final = 10; static const int JSON_float_error = 1; #line 222 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; #line 696 "parser.c" { cs = JSON_float_start; } #line 229 "parser.rl" json->memo = p; #line 703 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: switch( (*p) ) { case 45: goto st2; case 48: goto st3; } if ( 49 <= (*p) && (*p) <= 57 ) goto st9; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: if ( (*p) == 48 ) goto st3; if ( 49 <= (*p) && (*p) <= 57 ) goto st9; goto st1; st3: if ( ++p == pe ) goto _out3; case 3: if ( (*p) == 46 ) goto st4; goto st1; st4: if ( ++p == pe ) goto _out4; case 4: if ( 48 <= (*p) && (*p) <= 57 ) goto st5; goto st1; st5: if ( ++p == pe ) goto _out5; case 5: switch( (*p) ) { case 69: goto st6; case 101: goto st6; } if ( (*p) > 46 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st5; } else if ( (*p) >= 45 ) goto st1; goto tr8; tr8: #line 216 "parser.rl" { goto _out10; } goto st10; st10: if ( ++p == pe ) goto _out10; case 10: #line 764 "parser.c" goto st1; st6: if ( ++p == pe ) goto _out6; case 6: switch( (*p) ) { case 43: goto st7; case 45: goto st7; } if ( 48 <= (*p) && (*p) <= 57 ) goto st8; goto st1; st7: if ( ++p == pe ) goto _out7; case 7: if ( 48 <= (*p) && (*p) <= 57 ) goto st8; goto st1; st8: if ( ++p == pe ) goto _out8; case 8: switch( (*p) ) { case 69: goto st1; case 101: goto st1; } if ( (*p) > 46 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st8; } else if ( (*p) >= 45 ) goto st1; goto tr8; st9: if ( ++p == pe ) goto _out9; case 9: switch( (*p) ) { case 46: goto st4; case 69: goto st6; case 101: goto st6; } if ( 48 <= (*p) && (*p) <= 57 ) goto st9; goto st1; } _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out10: cs = 10; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out8: cs = 8; goto _out; _out9: cs = 9; goto _out; _out: {} } #line 231 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; *result = rb_Float(rb_str_new(json->memo, len)); return p + 1; } else { return NULL; } } #line 837 "parser.c" static const int JSON_array_start = 0; static const int JSON_array_first_final = 17; static const int JSON_array_error = 1; #line 267 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; *result = rb_ary_new(); #line 853 "parser.c" { cs = JSON_array_start; } #line 275 "parser.rl" #line 859 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: if ( (*p) == 91 ) goto st2; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: switch( (*p) ) { case 13: goto st2; case 32: goto st2; case 34: goto tr8; case 45: goto tr8; case 47: goto st13; case 91: goto tr8; case 93: goto tr7; case 102: goto tr8; case 110: goto tr8; case 116: goto tr8; case 123: goto tr8; } if ( (*p) > 10 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr8; } else if ( (*p) >= 9 ) goto st2; goto st1; tr8: #line 248 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { goto _out3; } else { rb_ary_push(*result, v); {p = (( np))-1;} } } goto st3; st3: if ( ++p == pe ) goto _out3; case 3: #line 911 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; case 44: goto st4; case 47: goto st9; case 93: goto tr7; } if ( 9 <= (*p) && (*p) <= 10 ) goto st3; goto st1; st4: if ( ++p == pe ) goto _out4; case 4: switch( (*p) ) { case 13: goto st4; case 32: goto st4; case 34: goto tr8; case 45: goto tr8; case 47: goto st5; case 91: goto tr8; case 102: goto tr8; case 110: goto tr8; case 116: goto tr8; case 123: goto tr8; } if ( (*p) > 10 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto tr8; } else if ( (*p) >= 9 ) goto st4; goto st1; st5: if ( ++p == pe ) goto _out5; case 5: switch( (*p) ) { case 42: goto st6; case 47: goto st8; } goto st1; st6: if ( ++p == pe ) goto _out6; case 6: if ( (*p) == 42 ) goto st7; goto st6; st7: if ( ++p == pe ) goto _out7; case 7: switch( (*p) ) { case 42: goto st7; case 47: goto st4; } goto st6; st8: if ( ++p == pe ) goto _out8; case 8: if ( (*p) == 10 ) goto st4; goto st8; st9: if ( ++p == pe ) goto _out9; case 9: switch( (*p) ) { case 42: goto st10; case 47: goto st12; } goto st1; st10: if ( ++p == pe ) goto _out10; case 10: if ( (*p) == 42 ) goto st11; goto st10; st11: if ( ++p == pe ) goto _out11; case 11: switch( (*p) ) { case 42: goto st11; case 47: goto st3; } goto st10; st12: if ( ++p == pe ) goto _out12; case 12: if ( (*p) == 10 ) goto st3; goto st12; tr7: #line 259 "parser.rl" { goto _out17; } goto st17; st17: if ( ++p == pe ) goto _out17; case 17: #line 1016 "parser.c" goto st1; st13: if ( ++p == pe ) goto _out13; case 13: switch( (*p) ) { case 42: goto st14; case 47: goto st16; } goto st1; st14: if ( ++p == pe ) goto _out14; case 14: if ( (*p) == 42 ) goto st15; goto st14; st15: if ( ++p == pe ) goto _out15; case 15: switch( (*p) ) { case 42: goto st15; case 47: goto st2; } goto st14; st16: if ( ++p == pe ) goto _out16; case 16: if ( (*p) == 10 ) goto st2; goto st16; } _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out8: cs = 8; goto _out; _out9: cs = 9; goto _out; _out10: cs = 10; goto _out; _out11: cs = 11; goto _out; _out12: cs = 12; goto _out; _out17: cs = 17; goto _out; _out13: cs = 13; goto _out; _out14: cs = 14; goto _out; _out15: cs = 15; goto _out; _out16: cs = 16; goto _out; _out: {} } #line 276 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; } else { rb_raise(eParserError, "unexpected token at '%s'", p); } } static VALUE json_string_escape(char *p, char *pe) { VALUE result = rb_str_buf_new(pe - p + 1); while (p < pe) { if (*p == '\\') { p++; if (p >= pe) return Qnil; /* raise an exception later, \ at end */ switch (*p) { case '"': case '\\': rb_str_buf_cat(result, p, 1); p++; break; case 'b': rb_str_buf_cat2(result, "\b"); p++; break; case 'f': rb_str_buf_cat2(result, "\f"); p++; break; case 'n': rb_str_buf_cat2(result, "\n"); p++; break; case 'r': rb_str_buf_cat2(result, "\r"); p++; break; case 't': rb_str_buf_cat2(result, "\t"); p++; break; case 'u': if (p > pe - 4) { return Qnil; } else { p = JSON_convert_UTF16_to_UTF8(result, p, pe, strictConversion); } break; } } else { char *q = p; while (*q != '\\' && q < pe) q++; rb_str_buf_cat(result, p, q - p); p = q; } } return result; } #line 1133 "parser.c" static const int JSON_string_start = 0; static const int JSON_string_first_final = 8; static const int JSON_string_error = 1; #line 350 "parser.rl" static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; *result = rb_str_new("", 0); #line 1149 "parser.c" { cs = JSON_string_start; } #line 358 "parser.rl" json->memo = p; #line 1156 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { case 0: if ( (*p) == 34 ) goto st2; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: switch( (*p) ) { case 34: goto tr4; case 92: goto st3; } if ( 0 <= (*p) && (*p) <= 31 ) goto st1; goto st2; tr4: #line 342 "parser.rl" { *result = json_string_escape(json->memo + 1, p); if (NIL_P(*result)) goto _out8; else {p = (( p + 1))-1;} } #line 347 "parser.rl" { goto _out8; } goto st8; st8: if ( ++p == pe ) goto _out8; case 8: #line 1192 "parser.c" goto st1; st3: if ( ++p == pe ) goto _out3; case 3: switch( (*p) ) { case 34: goto st2; case 47: goto st2; case 92: goto st2; case 98: goto st2; case 102: goto st2; case 110: goto st2; case 114: goto st2; case 116: goto st2; case 117: goto st4; } goto st1; st4: if ( ++p == pe ) goto _out4; case 4: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st5; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto st5; } else goto st5; goto st1; st5: if ( ++p == pe ) goto _out5; case 5: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st6; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto st6; } else goto st6; goto st1; st6: if ( ++p == pe ) goto _out6; case 6: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st7; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto st7; } else goto st7; goto st1; st7: if ( ++p == pe ) goto _out7; case 7: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st2; } else if ( (*p) > 70 ) { if ( 97 <= (*p) && (*p) <= 102 ) goto st2; } else goto st2; goto st1; } _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out8: cs = 8; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out: {} } #line 360 "parser.rl" if (cs >= JSON_string_first_final) { return p + 1; } else { return NULL; } } #line 1285 "parser.c" static const int JSON_start = 0; static const int JSON_first_final = 10; static const int JSON_error = 1; #line 390 "parser.rl" /* * Document-class: JSON::Ext::Parser * * This is the JSON parser implemented as a C extension. It can be configured * to be used by setting * * JSON.parser = JSON::Ext::Parser * * with the method parser= in JSON. * */ /* * call-seq: new(source) * * Creates a new JSON::Ext::Parser instance for the string _source_. */ static VALUE cParser_initialize(VALUE self, VALUE source) { char *ptr; long len; GET_STRUCT; source = StringValue(source); ptr = RSTRING(source)->ptr; len = RSTRING(source)->len; if (len < 2) { rb_raise(eParserError, "A JSON text must at least contain two octets!"); } /* Convert these? if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) { rb_raise(eParserError, "Only UTF8 octet streams are supported atm!"); } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) { rb_raise(eParserError, "Only UTF8 octet streams are supported atm!"); } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) { rb_raise(eParserError, "Only UTF8 octet streams are supported atm!"); } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) { rb_raise(eParserError, "Only UTF8 octet streams are supported atm!"); } */ json->len = len; json->source = ptr; json->Vsource = source; json->create_id = rb_funcall(mJSON, i_create_id, 0); return self; } /* * call-seq: parse() * * Parses the current JSON text _source_ and returns the complete data * structure as a result. */ static VALUE cParser_parse(VALUE self) { GET_STRUCT; char *p, *pe; int cs = EVIL; VALUE result = Qnil; #line 1356 "parser.c" { cs = JSON_start; } #line 453 "parser.rl" p = json->source; pe = p + json->len; #line 1364 "parser.c" { if ( p == pe ) goto _out; switch ( cs ) { st0: if ( ++p == pe ) goto _out0; case 0: switch( (*p) ) { case 13: goto st0; case 32: goto st0; case 47: goto st2; case 91: goto tr7; case 123: goto tr8; } if ( 9 <= (*p) && (*p) <= 10 ) goto st0; goto st1; st1: goto _out1; st2: if ( ++p == pe ) goto _out2; case 2: switch( (*p) ) { case 42: goto st3; case 47: goto st5; } goto st1; st3: if ( ++p == pe ) goto _out3; case 3: if ( (*p) == 42 ) goto st4; goto st3; st4: if ( ++p == pe ) goto _out4; case 4: switch( (*p) ) { case 42: goto st4; case 47: goto st0; } goto st3; st5: if ( ++p == pe ) goto _out5; case 5: if ( (*p) == 10 ) goto st0; goto st5; tr7: #line 381 "parser.rl" { char *np = JSON_parse_array(json, p, pe, &result); if (np == NULL) goto _out10; else {p = (( np))-1;} } goto st10; tr8: #line 376 "parser.rl" { char *np = JSON_parse_object(json, p, pe, &result); if (np == NULL) goto _out10; else {p = (( np))-1;} } goto st10; st10: if ( ++p == pe ) goto _out10; case 10: #line 1436 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; case 47: goto st6; } if ( 9 <= (*p) && (*p) <= 10 ) goto st10; goto st1; st6: if ( ++p == pe ) goto _out6; case 6: switch( (*p) ) { case 42: goto st7; case 47: goto st9; } goto st1; st7: if ( ++p == pe ) goto _out7; case 7: if ( (*p) == 42 ) goto st8; goto st7; st8: if ( ++p == pe ) goto _out8; case 8: switch( (*p) ) { case 42: goto st8; case 47: goto st10; } goto st7; st9: if ( ++p == pe ) goto _out9; case 9: if ( (*p) == 10 ) goto st10; goto st9; } _out0: cs = 0; goto _out; _out1: cs = 1; goto _out; _out2: cs = 2; goto _out; _out3: cs = 3; goto _out; _out4: cs = 4; goto _out; _out5: cs = 5; goto _out; _out10: cs = 10; goto _out; _out6: cs = 6; goto _out; _out7: cs = 7; goto _out; _out8: cs = 8; goto _out; _out9: cs = 9; goto _out; _out: {} } #line 456 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; } else { rb_raise(eParserError, "unexpected token at '%s'", p); } } static JSON_Parser *JSON_allocate() { JSON_Parser *json = ALLOC(JSON_Parser); MEMZERO(json, JSON_Parser, 1); return json; } static void JSON_mark(JSON_Parser *json) { rb_gc_mark_maybe(json->Vsource); rb_gc_mark_maybe(json->create_id); } static void JSON_free(JSON_Parser *json) { free(json); } static VALUE cJSON_parser_s_allocate(VALUE klass) { JSON_Parser *json = JSON_allocate(); return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json); } /* * call-seq: source() * * Returns a copy of the current _source_ string, that was used to construct * this Parser. */ static VALUE cParser_source(VALUE self) { GET_STRUCT; return rb_str_dup(json->Vsource); } void Init_parser() { mJSON = rb_define_module("JSON"); mExt = rb_define_module_under(mJSON, "Ext"); cParser = rb_define_class_under(mExt, "Parser", rb_cObject); eParserError = rb_path2class("JSON::ParserError"); rb_define_alloc_func(cParser, cJSON_parser_s_allocate); rb_define_method(cParser, "initialize", cParser_initialize, 1); rb_define_method(cParser, "parse", cParser_parse, 0); rb_define_method(cParser, "source", cParser_source, 0); i_json_creatable_p = rb_intern("json_creatable?"); i_json_create = rb_intern("json_create"); i_create_id = rb_intern("create_id"); i_chr = rb_intern("chr"); }