diff options
Diffstat (limited to 'lib/jsongrm.y')
-rw-r--r-- | lib/jsongrm.y | 316 |
1 files changed, 0 insertions, 316 deletions
diff --git a/lib/jsongrm.y b/lib/jsongrm.y deleted file mode 100644 index 398d2e7..0000000 --- a/lib/jsongrm.y +++ /dev/null @@ -1,316 +0,0 @@ -%{ -/* This file is part of Eclat. - Copyright (C) 2012-2015 Sergey Poznyakoff. - - Eclat is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. - - Eclat 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ - -#include "libeclat.h" -#include <string.h> -#include "jsongrm.h" -#include "json.h" - -struct json_object *json_return_obj; - -static int yyerror(char const *s); - -static struct json_pair * -pairdup(struct json_pair *in) -{ - struct json_pair *out = grecs_malloc(sizeof(*out)); - out->k = in->k; - out->v = in->v; - return out; -} - -static void -pairfree(void *ptr) -{ - struct json_pair *p = ptr; - free(p->k); - json_object_free(p->v); - free(p); -} - -static void -objfree(void *ptr) -{ - struct json_object *o = ptr; - json_object_free(o); -} - -%} -%token <n> T_NUMBER -%token <s> T_STRING -%token <b> T_BOOL -%token T_NULL T_ERR - -%type <a> array -%type <list> objects objlist pairs pairlist -%type <p> pair -%type <obj> object -%type <o> assoc - -%union { - int b; - double n; - char *s; - struct json_array *a; - struct grecs_symtab *o; - struct json_object *obj; - struct grecs_list *list; - struct json_pair *p; -} -%% - -input : object - { - json_return_obj = $1; - } - ; - -object : T_NUMBER - { - $$ = json_object_create(json_number); - $$->v.n = $1; - } - | T_STRING - { - $$ = json_object_create(json_string); - $$->v.s = $1; - } - | T_BOOL - { - $$ = json_object_create(json_bool); - $$->v.b = $1; - } - | T_NULL - { - $$ = json_object_create(json_null); - } - | array - { - $$ = json_object_create(json_arr); - $$->v.a = $1; - } - | assoc - { - $$ = json_object_create(json_obj); - $$->v.o = $1; - } - ; - -array : '[' objects ']' - { - struct json_array *a = grecs_malloc(sizeof(*a)); - if (!$2) { - a->oc = 0; - a->ov = NULL; - } else { - size_t i; - struct grecs_list_entry *ep; - a->oc = $2->count; - a->ov = grecs_calloc(a->oc, sizeof(a->ov)); - for (i = 0, ep = $2->head; ep; i++, ep = ep->next) { - struct json_object *p = ep->data; - a->ov[i] = p; - } - } - $$ = a; - } - ; - -objects : /* empty */ - { - $$ = NULL; - } - | objlist - ; - -objlist : object - { - $$ = grecs_list_create(); - $$->free_entry = objfree; - grecs_list_append($$, $1); - } - | objlist ',' object - { - grecs_list_append($1, $3); - } - ; - -assoc : '{' pairs '}' - { - struct grecs_symtab *s; - - s = json_assoc_create(); - if ($2) { - struct grecs_list_entry *ep; - for (ep = $2->head; ep; ep = ep->next) { - struct json_pair *p = ep->data; - int install = 1; - grecs_symtab_lookup_or_install(s, p, &install); - if (install) { - p->k = NULL; - p->v = NULL; - } - } - grecs_list_free($2); - } - $$ = s; - } - ; - -pairs : /* empty */ - { - $$ = NULL; - } - | pairlist - ; - -pairlist: pair - { - $$ = grecs_list_create(); - $$->free_entry = pairfree; - grecs_list_append($$, $1); - } - | pairlist ',' pair - { - grecs_list_append($1, $3); - } - ; - -pair : T_STRING ':' object - { - struct json_pair *p = grecs_malloc(sizeof(*p)); - p->k = $1; - p->v = $3; - $$ = p; - } - ; -%% - -static int -yyerror(char const *s) -{ - jsonlex_diag(s, 0); - return 0; -} - -struct json_object * -json_object_create(int type) -{ - struct json_object *obj = grecs_zalloc(sizeof(*obj)); - obj->type = type; - return obj; -} - -void -json_object_free(struct json_object *obj) -{ - size_t i; - - if (!obj) - return; - - switch (obj->type) { - case json_bool: - case json_number: - break; - case json_string: - free(obj->v.s); - break; - case json_arr: - for (i = 0; i < obj->v.a->oc; i++) - json_object_free(obj->v.a->ov[i]); - free(obj->v.a); - break; - case json_obj: - grecs_symtab_free(obj->v.o); - } - free(obj); -} - -static unsigned -json_st_hash(void *data, unsigned long n_buckets) -{ - struct json_pair *p = data; - return grecs_hash_string(p->k, n_buckets); -} - -static int -json_st_cmp(const void *a, const void *b) -{ - struct json_pair const *pa = a; - struct json_pair const *pb = b; - return strcmp(pa->k, pb->k); -} - -static int -json_st_copy(void *a, void *b) -{ - struct json_pair *pa = a; - struct json_pair *pb = b; - memcpy(pa, pb, sizeof(*pa)); - return 0; -} - -static void -json_st_free(void *ptr) -{ - struct json_pair *p = ptr; -// printf("FREE %s\n", p->k); - free(p->k); - json_object_free(p->v); - free(p); -} - -struct grecs_symtab * -json_assoc_create() -{ - return grecs_symtab_create(sizeof(struct json_pair), - json_st_hash, - json_st_cmp, - json_st_copy, - NULL, - json_st_free); -} - -struct json_object * -json_parse_string(char const *input, size_t len) -{ - jsonlex_setup(input, len); - if (yyparse()) { - /* FIXME: error recovery */ - return NULL; - } - return json_return_obj; -} - -struct json_object * -json_object_lookup(struct json_object *obj, const char *ident) -{ - struct json_pair key, *p; - if (!obj || obj->type != json_obj) - return NULL; - key.k = ident; - p = grecs_symtab_lookup_or_install(obj->v.o, &key, NULL); - if (!p) - return NULL; - return p->v; -} - - - - - |