Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]
Return to module

File Contents

Contents of /Public_Parser_JSON2-0.9/json_number.c:

#line 1 "src/json_number.rl"
// vim:syntax=ragel

#include <stdio.h>

// p_wchar2 is INT32
#if defined(HAVE_WCHAR_H) && 4 == SIZEOF_WCHAR_T
# define USE_WCHAR_SCANF
#endif

#ifdef USE_WCHAR_SCANF
# include <wchar.h>
#endif


#line 30 "src/json_number.rl"


static p_wchar2 *_parse_JSON_number(p_wchar2 *p, p_wchar2 *pe, struct parser_state *state) {
    p_wchar2 *i = p;
    int cs;
    INT_TYPE d = 0;
    FLOAT_TYPE f;

    
#line 27 "work/json_number.c"
static const int JSON_number_start = 1;
static const int JSON_number_first_final = 6;
static const int JSON_number_error = 0;

static const int JSON_number_en_main = 1;

#line 39 "src/json_number.rl"

    
#line 37 "work/json_number.c"
	{
	cs = JSON_number_start;
	}
#line 41 "src/json_number.rl"
    
#line 43 "work/json_number.c"
	{
	if ( p == pe )
		goto _out;
	switch ( cs )
	{
case 1:
	switch( (*p) ) {
		case 45: goto st2;
		case 46: goto tr2;
		case 48: goto st6;
	}
	if ( 49 <= (*p) && (*p) <= 57 )
		goto st10;
	goto st0;
st0:
	goto _out0;
st2:
	if ( ++p == pe )
		goto _out2;
case 2:
	if ( (*p) == 48 )
		goto st6;
	if ( 49 <= (*p) && (*p) <= 57 )
		goto st10;
	goto st0;
st6:
	if ( ++p == pe )
		goto _out6;
case 6:
	switch( (*p) ) {
		case 13: goto tr8;
		case 32: goto tr8;
		case 44: goto tr8;
		case 46: goto tr2;
		case 58: goto tr8;
		case 69: goto tr9;
		case 93: goto tr8;
		case 101: goto tr9;
		case 125: goto tr8;
	}
	if ( 9 <= (*p) && (*p) <= 10 )
		goto tr8;
	goto st0;
tr8:
#line 21 "src/json_number.rl"
	{
		goto _out7;
    }
	goto st7;
st7:
	if ( ++p == pe )
		goto _out7;
case 7:
#line 97 "work/json_number.c"
	goto st0;
tr2:
#line 28 "src/json_number.rl"
	{d = 1;}
	goto st3;
st3:
	if ( ++p == pe )
		goto _out3;
case 3:
#line 107 "work/json_number.c"
	if ( 48 <= (*p) && (*p) <= 57 )
		goto st8;
	goto st0;
st8:
	if ( ++p == pe )
		goto _out8;
case 8:
	switch( (*p) ) {
		case 13: goto tr8;
		case 32: goto tr8;
		case 44: goto tr8;
		case 58: goto tr8;
		case 69: goto tr9;
		case 93: goto tr8;
		case 101: goto tr9;
		case 125: goto tr8;
	}
	if ( (*p) > 10 ) {
		if ( 48 <= (*p) && (*p) <= 57 )
			goto st8;
	} else if ( (*p) >= 9 )
		goto tr8;
	goto st0;
tr9:
#line 27 "src/json_number.rl"
	{d = 1;}
	goto st4;
st4:
	if ( ++p == pe )
		goto _out4;
case 4:
#line 139 "work/json_number.c"
	switch( (*p) ) {
		case 43: goto st5;
		case 45: goto st5;
	}
	if ( 48 <= (*p) && (*p) <= 57 )
		goto st9;
	goto st0;
st5:
	if ( ++p == pe )
		goto _out5;
case 5:
	if ( 48 <= (*p) && (*p) <= 57 )
		goto st9;
	goto st0;
st9:
	if ( ++p == pe )
		goto _out9;
case 9:
	switch( (*p) ) {
		case 13: goto tr8;
		case 32: goto tr8;
		case 44: goto tr8;
		case 58: goto tr8;
		case 93: goto tr8;
		case 125: goto tr8;
	}
	if ( (*p) > 10 ) {
		if ( 48 <= (*p) && (*p) <= 57 )
			goto st9;
	} else if ( (*p) >= 9 )
		goto tr8;
	goto st0;
st10:
	if ( ++p == pe )
		goto _out10;
case 10:
	switch( (*p) ) {
		case 13: goto tr8;
		case 32: goto tr8;
		case 44: goto tr8;
		case 46: goto tr2;
		case 58: goto tr8;
		case 69: goto tr9;
		case 93: goto tr8;
		case 101: goto tr9;
		case 125: goto tr8;
	}
	if ( (*p) > 10 ) {
		if ( 48 <= (*p) && (*p) <= 57 )
			goto st10;
	} else if ( (*p) >= 9 )
		goto tr8;
	goto st0;
	}
	_out0: cs = 0; goto _out; 
	_out2: cs = 2; goto _out; 
	_out6: cs = 6; goto _out; 
	_out7: cs = 7; goto _out; 
	_out3: cs = 3; goto _out; 
	_out8: cs = 8; goto _out; 
	_out4: cs = 4; goto _out; 
	_out5: cs = 5; goto _out; 
	_out9: cs = 9; goto _out; 
	_out10: cs = 10; goto _out; 

	_out: {}
	}
#line 42 "src/json_number.rl"

    if (cs >= JSON_number_first_final) {
	
		if (!state->validate) {
#ifdef USE_WCHAR_SCANF
			p_wchar2 temp = *p;
			*p = 0;

			if (d) {
#if SIZEOF_FLOAT_TYPE == SIZEOF_FLOAT
				if (1 != swscanf((wchar_t*)i, L"%f", &f)) {
#elif SIZEOF_FLOAT_TYPE == SIZEOF_DOUBLE
				if (1 != swscanf((wchar_t*)i, L"%lf", &f)) {
#elif SIZEOF_FLOAT_TYPE == SIZEOF_LONG_DOUBLE
				if (1 != swscanf((wchar_t*)i, L"%Lf", &f)) {
#else
# error "finding the correct size of FLOAT_TYPE didnt work"
#endif
					*p = temp;
					push_int((INT_TYPE)i);
					return NULL;
				}
				push_float(f);
			} else {
#if SIZEOF_INT_TYPE == SIZEOF_INT
				if (1 != swscanf((wchar_t*)i, L"%d", &d)) {
#elif SIZEOF_INT_TYPE == SIZEOF_LONG_INT
				if (1 != swscanf((wchar_t*)i, L"%ld", &d)) {
#elif SIZEOF_INT_TYPE == SIZEOF_LONG_LONG
				if (1 != swscanf((wchar_t*)i, L"%Ld", &d)) {
#else
# error "finding the correct size of INT_TYPE didnt work"
#endif
					*p = temp;
					push_int((INT_TYPE)i);
					return NULL;
				}
				push_int(d);
			}

			*p = temp;
#else
			ptrdiff_t len = (ptrdiff_t)(p - i); 

			char *temp = (char*)malloc(len+1);

			if (temp == NULL) {
				push_int((INT_TYPE)p);
				return NULL;
			}

			*(temp + len) = '\0';
			
			do {
				*(temp + len) = (char)(*(i + len));
			} while(len-- > 0);

			if (d == 1) {
#if FLOAT_TYPE == float
				if (1 != sscanf(temp, "%f", &f)) {
#elif SIZEOF_FLOAT_TYPE == SIZEOF_DOUBLE
				if (1 != sscanf(temp, "%lf", &f)) {
#elif SIZEOF_FLOAT_TYPE == SIZEOF_LONG_DOUBLE
				if (1 != sscanf(temp, "%Lf", &f)) {
#else
# error "finding the correct size of FLOAT_TYPE didnt work"
#endif
					free(temp);

					push_int((INT_TYPE)i);
					return NULL;
				}
				push_float(f);
			} else {
#if SIZEOF_INT_TYPE == SIZEOF_LONG_INT
				if (1 != sscanf(temp, "%ld", &d)) {
#elif SIZEOF_INT_TYPE == SIZEOF_INT
				if (1 != sscanf(temp, "%d", &d)) {
#elif SIZEOF_INT_TYPE == SIZEOF_LONG_LONG
				if (1 != sscanf(temp, "%Ld", &d)) {
#else
# error "finding the correct size of INT_TYPE didnt work"
#endif
					free(temp);

					push_int((INT_TYPE)i);
					return NULL;
				}
				push_int(d);
			}

			free(temp);
#endif
		}

		return p;
    }

    push_int((INT_TYPE)p);
    return NULL;
}

gotpike.org | Copyright © 2004 - 2012 | Pike is a trademark of Department of Computer and Information Science, Linköping University