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

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Parser.JSON2
Viewing contents of Public_Parser_JSON2-0.4/json.cmod

#line 1 "json.rl"
// vim:syntax=ragel
/* 
 * Pike CMOD parser for JSON.
 * Copyright (C) 2007 Arne Goedeke
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include 
#include "global.h"
#include "interpret.h"
#include "stralloc.h"
#include "mapping.h"
#include "svalue.h"
#include "operators.h"
#include "array.h"
#include "builtin_functions.h"
#include "module.h"
#include "gc.h"

#include "json.h"

p_wchar2 *_parse_JSON(p_wchar2* p, p_wchar2* pe, struct parser_state *state); 

#include "json_string.c"
#include "json_number.c"
#include "json_array.c"
#include "json_mapping.c"
#ifdef JSON_CUSTOM_TYPES
# include "json_custom.c"
#endif


#line 48 "json.cmod/json.cmod"
static const int JSON_start = 1;

static const int JSON_first_final = 12;

static const int JSON_error = 0;

#line 79 "json.rl"


p_wchar2 *_parse_JSON(p_wchar2 *p, p_wchar2 *pe, struct parser_state *state) {
    p_wchar2 *i = p;
    int cs;
    int c = 0;

    
#line 64 "json.cmod/json.cmod"
	{
	cs = JSON_start;
	}
#line 87 "json.rl"
    
#line 70 "json.cmod/json.cmod"
	{
	if ( p == pe )
		goto _out;
	switch ( cs )
	{
st1:
	if ( ++p == pe )
		goto _out1;
case 1:
	switch( (*p) ) {
		case 13: goto st1;
		case 32: goto st1;
		case 34: goto tr2;
		case 43: goto tr3;
		case 91: goto tr4;
		case 102: goto st2;
		case 110: goto st6;
		case 116: goto st9;
		case 123: goto tr8;
	}
	if ( (*p) < 45 ) {
		if ( 9 <= (*p) && (*p) <= 10 )
			goto st1;
	} else if ( (*p) > 46 ) {
		if ( 48 <= (*p) && (*p) <= 57 )
			goto tr3;
	} else
		goto tr3;
	goto st0;
st0:
	goto _out0;
tr2:
#line 60 "json.rl"
	{ PARSE(string, p); {p = (( i))-1;} }
	goto st12;
tr3:
#line 61 "json.rl"
	{ PARSE(number, p); {p = (( i))-1;} }
	goto st12;
tr4:
#line 63 "json.rl"
	{ PARSE(array, p); {p = (( i))-1;} }
	goto st12;
tr8:
#line 62 "json.rl"
	{ PARSE(mapping, p); {p = (( i))-1;} }
	goto st12;
tr12:
#line 67 "json.rl"
	{ PUSH_SPECIAL("false"); }
	goto st12;
tr15:
#line 68 "json.rl"
	{ PUSH_SPECIAL("null"); }
	goto st12;
tr18:
#line 66 "json.rl"
	{ PUSH_SPECIAL("true"); }
	goto st12;
st12:
	if ( ++p == pe )
		goto _out12;
case 12:
#line 78 "json.rl"
	{ if (state->level != 0) goto _out12; }
#line 136 "json.cmod/json.cmod"
	switch( (*p) ) {
		case 13: goto st12;
		case 32: goto st12;
	}
	if ( 9 <= (*p) && (*p) <= 10 )
		goto st12;
	goto st0;
st2:
	if ( ++p == pe )
		goto _out2;
case 2:
	if ( (*p) == 97 )
		goto st3;
	goto st0;
st3:
	if ( ++p == pe )
		goto _out3;
case 3:
	if ( (*p) == 108 )
		goto st4;
	goto st0;
st4:
	if ( ++p == pe )
		goto _out4;
case 4:
	if ( (*p) == 115 )
		goto st5;
	goto st0;
st5:
	if ( ++p == pe )
		goto _out5;
case 5:
	if ( (*p) == 101 )
		goto tr12;
	goto st0;
st6:
	if ( ++p == pe )
		goto _out6;
case 6:
	if ( (*p) == 117 )
		goto st7;
	goto st0;
st7:
	if ( ++p == pe )
		goto _out7;
case 7:
	if ( (*p) == 108 )
		goto st8;
	goto st0;
st8:
	if ( ++p == pe )
		goto _out8;
case 8:
	if ( (*p) == 108 )
		goto tr15;
	goto st0;
st9:
	if ( ++p == pe )
		goto _out9;
case 9:
	if ( (*p) == 114 )
		goto st10;
	goto st0;
st10:
	if ( ++p == pe )
		goto _out10;
case 10:
	if ( (*p) == 117 )
		goto st11;
	goto st0;
st11:
	if ( ++p == pe )
		goto _out11;
case 11:
	if ( (*p) == 101 )
		goto tr18;
	goto st0;
	}
	_out1: cs = 1; goto _out; 
	_out0: cs = 0; 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 88 "json.rl"

    if (cs >= JSON_first_final) {
	return p;
    }

    if (!state->validate && c > 0) pop_n_elems(c);

#ifdef JUMP
    if (!state->validate) {
	Pike_error("Error parsing JSON at '%c'\n", (char)*p);
    }
#endif

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

/*! @module Public
 */

/*! @module Parser
 */

/*! @module JSON2
 *! This module contains a parser to parse JSON into native pike types. The parser has been written
 *! in c using ragel (@[http://www.cs.queensu.ca/~thurston/ragel/]).
 *! The parser is supposed to handle Unicode strings (internally 8, 16 and 32 bit wide strings that is).
 *! 
 *! Have a look at @[http://www.json.org] or @[http://www.ietf.org/rfc/rfc4627.txt?number=4627] for
 *! information about what JSON is.
 */

/*! @decl int validate(string s)
 *!
 *! Takes a string and checks if it is valid JSON.
 *! 
 *! @returns
 *! 	In case the string contains valid JSON @expr{-1@} is returned. It is then guarenteed to be parsed
 *! 	without errors by @[parse()].
 *! 	In case the string is not valid JSON, the integer position inside the string
 *! 	where the error occures is returned.
 */
PIKEFUN int validate(string data) {
    p_wchar2 *ret;
    struct parser_state state;

    JSON_CONVERT(data, ret);

    state.validate = 1;
    state.level = 0;
#ifdef JSON_CUSTOM_TYPES
    state.custom_chars = NULL;
#endif
    state.start = ret;
    state.stop = ret + data->len;
    state.data = data;

    pop_stack();
    if (_parse_JSON(ret, ret + data->len, &state) == NULL) {
	push_int((int)ret);
	f_minus(2);
	push_int(4);
	o_divide();
    } else {
	push_int(-1);
    }

    free(ret);
    return;
}

/*! @decl array|mapping|string|float|int parse(string s)
 *!
 *! Parses a JSON-formatted string and returns the corresponding pike data type.
 *! 
 *! @throws
 *! 	Throws an exception in case the data contained in @expr{s@} is not valid
 *! 	JSON.
 */
PIKEFUN mixed parse(string data) {
    p_wchar2 *ret, *stop;
    struct parser_state state;
    // we wont be building more than one string at once.
    JSON_CONVERT(data, ret);

    state.validate = 0;
    state.level = 0;
#ifdef JSON_CUSTOM_TYPES
    state.custom_chars = NULL;
#endif
    state.start = ret;
    state.stop = ret + data->len;
    state.data = data;

    stop = _parse_JSON(ret, ret + data->len, &state);

    if (stop != NULL) {
	free(ret);
	stack_pop_keep_top();

	return;
    }

    push_int((int)ret);
    // calculate offset in string where parsing failed
    f_minus(2);
    push_int(4);
    // divide by 4 to get offset in chars
    o_divide();
    push_int(((struct svalue*)(Pike_sp - 1))->u.integer);
    push_int(10);
    // we want 10 chars..
    f_add(2);
    f_index(3);
    free(ret);
    if (((struct svalue*)(Pike_sp - 1))->type != PIKE_T_STRING) {
	Pike_error("Parsing JSON failed and I dont know where.\n");
    } else {
	push_text("Parsing JSON failed at '%s'.\n");
	stack_swap();
	f_sprintf(2);
	f_aggregate(1);
	f_throw(1);
    }
}

/*! @endmodule
 */
/*! @endmodule
 */
/*! @endmodule
 */


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