| 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 |
#ifndef MAYBE_UNUSED |
| 17 |
# define MAYBE_UNUSED(x) x |
| 18 |
#endif |
| 19 |
|
| 20 |
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key)) |
| 21 |
|
| 22 |
/* unicode */ |
| 23 |
|
| 24 |
typedef unsigned long UTF32; /* at least 32 bits */ |
| 25 |
typedef unsigned short UTF16; /* at least 16 bits */ |
| 26 |
typedef unsigned char UTF8; /* typically 8 bits */ |
| 27 |
|
| 28 |
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD |
| 29 |
#define UNI_SUR_HIGH_START (UTF32)0xD800 |
| 30 |
#define UNI_SUR_HIGH_END (UTF32)0xDBFF |
| 31 |
#define UNI_SUR_LOW_START (UTF32)0xDC00 |
| 32 |
#define UNI_SUR_LOW_END (UTF32)0xDFFF |
| 33 |
|
| 34 |
typedef struct JSON_ParserStruct { |
| 35 |
VALUE Vsource; |
| 36 |
char *source; |
| 37 |
long len; |
| 38 |
char *memo; |
| 39 |
VALUE create_id; |
| 40 |
int max_nesting; |
| 41 |
int allow_nan; |
| 42 |
int parsing_name; |
| 43 |
int symbolize_names; |
| 44 |
int freeze; |
| 45 |
VALUE object_class; |
| 46 |
VALUE array_class; |
| 47 |
VALUE decimal_class; |
| 48 |
int create_additions; |
| 49 |
VALUE match_string; |
| 50 |
FBuffer *fbuffer; |
| 51 |
} JSON_Parser; |
| 52 |
|
| 53 |
#define GET_PARSER \ |
| 54 |
GET_PARSER_INIT; \ |
| 55 |
if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance") |
| 56 |
#define GET_PARSER_INIT \ |
| 57 |
JSON_Parser *json; \ |
| 58 |
TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json) |
| 59 |
|
| 60 |
#define MinusInfinity "-Infinity" |
| 61 |
#define EVIL 0x666 |
| 62 |
|
| 63 |
static UTF32 unescape_unicode(const unsigned char *p); |
| 64 |
static int convert_UTF32_to_UTF8(char *buf, UTF32 ch); |
| 65 |
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting); |
| 66 |
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting); |
| 67 |
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result); |
| 68 |
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result); |
| 69 |
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting); |
| 70 |
static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int symbolize); |
| 71 |
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result); |
| 72 |
static VALUE convert_encoding(VALUE source); |
| 73 |
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self); |
| 74 |
static VALUE cParser_parse(VALUE self); |
| 75 |
static void JSON_mark(void *json); |
| 76 |
static void JSON_free(void *json); |
| 77 |
static VALUE cJSON_parser_s_allocate(VALUE klass); |
| 78 |
static VALUE cParser_source(VALUE self); |
| 79 |
#ifndef ZALLOC |
| 80 |
#define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type))) |
| 81 |
static inline void *ruby_zalloc(size_t n) |
| 82 |
{ |
| 83 |
void *p = ruby_xmalloc(n); |
| 84 |
memset(p, 0, n); |
| 85 |
return p; |
| 86 |
} |
| 87 |
#endif |
| 88 |
#ifdef TypedData_Make_Struct |
| 89 |
static const rb_data_type_t JSON_Parser_type; |
| 90 |
#define NEW_TYPEDDATA_WRAPPER 1 |
| 91 |
#else |
| 92 |
#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, JSON_free, json) |
| 93 |
#define TypedData_Get_Struct(self, JSON_Parser, ignore, json) Data_Get_Struct(self, JSON_Parser, json) |
| 94 |
#endif |
| 95 |
|
| 96 |
#endif |