diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-12-18 17:33:16 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-12-18 18:27:03 +0200 |
commit | 9c0d1b58fd370bed9442ad8859d9c6e1f02cc643 (patch) | |
tree | 993dfff1d8236d6f1b81cd0de1325452b4ae84e6 | |
parent | d51a70af75b14720a1756d90ec53132596aa3ebf (diff) | |
download | grecs-9c0d1b58fd370bed9442ad8859d9c6e1f02cc643.tar.gz grecs-9c0d1b58fd370bed9442ad8859d9c6e1f02cc643.tar.bz2 |
Add basic JSON support functions.
* src/Make-inst.am: Add json.h
* src/Make-shared.am: Likewise.
* src/Make-static.am: Likewise.
* src/Make.am: Likewise.
* src/json-gram.y: New file.
* src/json-lex.l: New file.
* src/json.h: New file.
* src/yytrans: Translate json prefixes.
* src/.gitignore: Update.
* tests/json.c: New file.
* tests/json00.at: New file.
* tests/json01.at: New file.
* tests/Makefile.am: Add new tests; build json
* tests/testsuite.at: Add new tests.
* tests/.gitignore: Update.
* am/grecs.m4: New flag "json"
* configure.ac (GRECS_SETUP): Require json
* src/Make-inst.am (include_HEADERS): Assign GRECS_HDR value.
* src/Make-shared.a [GRECS_COND_INSTALLHEADERS] (grecsinclude_HEADERS)
[!GRECS_COND_INSTALLHEADERS] (noinst_HEADERS): Likewise.
* src/Make-static.am (noinst_HEADERS): Likewise.
* src/Make.am [GRECS_COND_JSON]: Define GRECS_JSON and GRECS_EXTRA_JSON.
-rw-r--r-- | am/grecs.m4 | 4 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/.gitignore | 5 | ||||
-rw-r--r-- | src/Make-inst.am | 2 | ||||
-rw-r--r-- | src/Make-shared.am | 7 | ||||
-rw-r--r-- | src/Make-static.am | 4 | ||||
-rw-r--r-- | src/Make.am | 12 | ||||
-rw-r--r-- | src/json-gram.y | 371 | ||||
-rw-r--r-- | src/json-lex.l | 204 | ||||
-rw-r--r-- | src/json.h | 68 | ||||
-rw-r--r-- | src/yytrans | 1 | ||||
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/Makefile.am | 10 | ||||
-rw-r--r-- | tests/json.c | 286 | ||||
-rw-r--r-- | tests/json00.at | 75 | ||||
-rw-r--r-- | tests/json01.at | 89 | ||||
-rw-r--r-- | tests/testsuite.at | 6 |
17 files changed, 1141 insertions, 6 deletions
diff --git a/am/grecs.m4 b/am/grecs.m4 index 9ba234d..5e558cb 100644 --- a/am/grecs.m4 +++ b/am/grecs.m4 | |||
@@ -70,6 +70,7 @@ AC_DEFUN([_GRECS_SET_OPTIONS], | |||
70 | # instead of pointers to the value and locus. | 70 | # instead of pointers to the value and locus. |
71 | # sockaddr-list Sockaddr type keeps a singly-linked list of addresses | 71 | # sockaddr-list Sockaddr type keeps a singly-linked list of addresses |
72 | # returned by getaddrinfo. | 72 | # returned by getaddrinfo. |
73 | # json Compile JSON support | ||
73 | # | 74 | # |
74 | # The pp-setup-file argument supplies the pathname of the preprocessor | 75 | # The pp-setup-file argument supplies the pathname of the preprocessor |
75 | # setup file in the source tree. It is ignored if std-pp-setup option is | 76 | # setup file in the source tree. It is ignored if std-pp-setup option is |
@@ -182,6 +183,9 @@ AC_DEFUN([GRECS_SETUP],[ | |||
182 | 183 | ||
183 | AM_CONDITIONAL([GRECS_COND_SOCKADDR_LIST], | 184 | AM_CONDITIONAL([GRECS_COND_SOCKADDR_LIST], |
184 | _GRECS_IF_OPTION_SET([sockaddr-list],[true],[false])) | 185 | _GRECS_IF_OPTION_SET([sockaddr-list],[true],[false])) |
186 | AM_CONDITIONAL([GRECS_COND_JSON], | ||
187 | _GRECS_IF_OPTION_SET([json],[true],[false])) | ||
188 | |||
185 | AC_SUBST([GRECS_SOCKADDR_LIST]) | 189 | AC_SUBST([GRECS_SOCKADDR_LIST]) |
186 | _GRECS_IF_OPTION_SET([sockaddr-list],[GRECS_SOCKADDR_LIST=1], | 190 | _GRECS_IF_OPTION_SET([sockaddr-list],[GRECS_SOCKADDR_LIST=1], |
187 | [GRECS_SOCKADDR_LIST=0]) | 191 | [GRECS_SOCKADDR_LIST=0]) |
diff --git a/configure.ac b/configure.ac index ebd7145..bcd81a0 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -37,6 +37,6 @@ AC_HEADER_STDC | |||
37 | 37 | ||
38 | # Grecs subsystem | 38 | # Grecs subsystem |
39 | 39 | ||
40 | GRECS_SETUP(., [install tests git2chg all-parsers]) | 40 | GRECS_SETUP(., [install tests git2chg all-parsers json]) |
41 | 41 | ||
42 | AC_OUTPUT | 42 | AC_OUTPUT |
diff --git a/src/.gitignore b/src/.gitignore index 322178c..11738ff 100644 --- a/src/.gitignore +++ b/src/.gitignore | |||
@@ -18,3 +18,8 @@ grecs.h | |||
18 | Make-inst.in | 18 | Make-inst.in |
19 | Make-shared.in | 19 | Make-shared.in |
20 | Make-static.in | 20 | Make-static.in |
21 | json-gram.c | ||
22 | json-gram.h | ||
23 | json-gram.output | ||
24 | json-lex.c | ||
25 | |||
diff --git a/src/Make-inst.am b/src/Make-inst.am index daf1c6f..f9cae0e 100644 --- a/src/Make-inst.am +++ b/src/Make-inst.am | |||
@@ -17,6 +17,6 @@ | |||
17 | include Make.am | 17 | include Make.am |
18 | lib_LTLIBRARIES=libgrecs.la | 18 | lib_LTLIBRARIES=libgrecs.la |
19 | libgrecs_la_SOURCES = $(GRECS_SRC) | 19 | libgrecs_la_SOURCES = $(GRECS_SRC) |
20 | include_HEADERS = grecs.h grecsopt.h wordsplit.h | 20 | include_HEADERS = $(GRECS_HDR) |
21 | m4datadir = $(datadir)/aclocal | 21 | m4datadir = $(datadir)/aclocal |
22 | dist_m4data_DATA = libgrecs.m4 | 22 | dist_m4data_DATA = libgrecs.m4 |
diff --git a/src/Make-shared.am b/src/Make-shared.am index 292218b..8f698c4 100644 --- a/src/Make-shared.am +++ b/src/Make-shared.am | |||
@@ -16,9 +16,10 @@ | |||
16 | include Make.am | 16 | include Make.am |
17 | noinst_LTLIBRARIES=libgrecs.la | 17 | noinst_LTLIBRARIES=libgrecs.la |
18 | libgrecs_la_SOURCES = $(GRECS_SRC) | 18 | libgrecs_la_SOURCES = $(GRECS_SRC) |
19 | |||
19 | if GRECS_COND_INSTALLHEADERS | 20 | if GRECS_COND_INSTALLHEADERS |
20 | grecsincludedir = @GRECS_INCLUDE_DIR@ | 21 | grecsincludedir = @GRECS_INCLUDE_DIR@ |
21 | grecsinclude_HEADERS = grecs.h grecsopt.h wordsplit.h | 22 | grecsinclude_HEADERS = $(GRECS_HDR) |
22 | else | 23 | else |
23 | noinst_HEADERS += grecs.h grecsopt.h wordsplit.h | 24 | noinst_HEADERS += $(GRECS_HDR) |
24 | endif \ No newline at end of file | 25 | endif |
diff --git a/src/Make-static.am b/src/Make-static.am index 63b6338..4050e47 100644 --- a/src/Make-static.am +++ b/src/Make-static.am | |||
@@ -16,4 +16,6 @@ | |||
16 | include Make.am | 16 | include Make.am |
17 | noinst_LIBRARIES=libgrecs.a | 17 | noinst_LIBRARIES=libgrecs.a |
18 | libgrecs_a_SOURCES = $(GRECS_SRC) | 18 | libgrecs_a_SOURCES = $(GRECS_SRC) |
19 | noinst_HEADERS += grecs.h grecsopt.h wordsplit.h | 19 | noinst_HEADERS += $(GRECS_HDR) |
20 | |||
21 | |||
diff --git a/src/Make.am b/src/Make.am index 11ef6df..15e0075 100644 --- a/src/Make.am +++ b/src/Make.am | |||
@@ -39,6 +39,11 @@ if GRECS_COND_GIT_PARSER | |||
39 | PARSER_DEFS += -DENABLE_GIT_PARSER | 39 | PARSER_DEFS += -DENABLE_GIT_PARSER |
40 | endif | 40 | endif |
41 | 41 | ||
42 | if GRECS_COND_JSON | ||
43 | GRECS_JSON = json-gram.y json-lex.l | ||
44 | GRECS_EXTRA_JSON = json-gram.h | ||
45 | endif | ||
46 | |||
42 | GRECS_SRC = \ | 47 | GRECS_SRC = \ |
43 | asprintf.c\ | 48 | asprintf.c\ |
44 | cidr.c\ | 49 | cidr.c\ |
@@ -64,11 +69,17 @@ GRECS_SRC = \ | |||
64 | txtacc.c\ | 69 | txtacc.c\ |
65 | version.c\ | 70 | version.c\ |
66 | wordsplit.c\ | 71 | wordsplit.c\ |
72 | $(GRECS_JSON)\ | ||
67 | $(GRECS_PARSER_BIND)\ | 73 | $(GRECS_PARSER_BIND)\ |
68 | $(GRECS_PARSER_DHCPD)\ | 74 | $(GRECS_PARSER_DHCPD)\ |
69 | $(GRECS_PARSER_GIT)\ | 75 | $(GRECS_PARSER_GIT)\ |
70 | $(GRECS_PARSER_META1) | 76 | $(GRECS_PARSER_META1) |
71 | 77 | ||
78 | GRECS_HDR = grecs.h grecsopt.h wordsplit.h | ||
79 | if GRECS_COND_JSON | ||
80 | GRECS_HDR += json.h | ||
81 | endif | ||
82 | |||
72 | if GRECS_COND_SOCKADDR_LIST | 83 | if GRECS_COND_SOCKADDR_LIST |
73 | GRECS_SRC += sockaddr.c | 84 | GRECS_SRC += sockaddr.c |
74 | endif | 85 | endif |
@@ -80,6 +91,7 @@ EXTRA_DIST=\ | |||
80 | grecs.hin\ | 91 | grecs.hin\ |
81 | $(GRECS_EXTRA_BIND)\ | 92 | $(GRECS_EXTRA_BIND)\ |
82 | $(GRECS_EXTRA_DHCPD)\ | 93 | $(GRECS_EXTRA_DHCPD)\ |
94 | $(GRECS_EXTRA_JSON)\ | ||
83 | $(GRECS_EXTRA_META1)\ | 95 | $(GRECS_EXTRA_META1)\ |
84 | $(PP_SETUP_FILE)\ | 96 | $(PP_SETUP_FILE)\ |
85 | Make.am Make-inst.am Make-shared.am Make-static.am | 97 | Make.am Make-inst.am Make-shared.am Make-static.am |
diff --git a/src/json-gram.y b/src/json-gram.y new file mode 100644 index 0000000..e92ad35 --- /dev/null +++ b/src/json-gram.y | |||
@@ -0,0 +1,371 @@ | |||
1 | %{ | ||
2 | /* This file is part of Grecs. | ||
3 | Copyright (C) 2012-2015 Sergey Poznyakoff. | ||
4 | |||
5 | Grecs is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 3, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | Grecs is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with Grecs. If not, see <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | #include "grecs.h" | ||
22 | #include <grecs-locus.h> | ||
23 | #include <string.h> | ||
24 | #include "json-gram.h" | ||
25 | #include "json.h" | ||
26 | |||
27 | struct json_value *json_return_obj; | ||
28 | |||
29 | extern int yylex(void); | ||
30 | static int yyerror(char const *s); | ||
31 | |||
32 | static void | ||
33 | pairfree(void *ptr) | ||
34 | { | ||
35 | struct json_pair *p = ptr; | ||
36 | free(p->k); | ||
37 | json_value_free(p->v); | ||
38 | free(p); | ||
39 | } | ||
40 | |||
41 | static void | ||
42 | objfree(void *ptr) | ||
43 | { | ||
44 | struct json_value *o = ptr; | ||
45 | json_value_free(o); | ||
46 | } | ||
47 | |||
48 | %} | ||
49 | |||
50 | %error-verbose | ||
51 | %locations | ||
52 | |||
53 | %token <n> T_NUMBER | ||
54 | %token <s> T_STRING | ||
55 | %token <b> T_BOOL | ||
56 | %token T_NULL T_ERR | ||
57 | |||
58 | %type <a> array | ||
59 | %type <list> objects objlist pairs pairlist | ||
60 | %type <p> pair | ||
61 | %type <obj> object | ||
62 | %type <o> assoc | ||
63 | |||
64 | %union { | ||
65 | int b; | ||
66 | double n; | ||
67 | char *s; | ||
68 | struct json_array *a; | ||
69 | struct grecs_symtab *o; | ||
70 | struct json_value *obj; | ||
71 | struct grecs_list *list; | ||
72 | struct json_pair *p; | ||
73 | } | ||
74 | %% | ||
75 | |||
76 | input : object | ||
77 | { | ||
78 | json_return_obj = $1; | ||
79 | } | ||
80 | ; | ||
81 | |||
82 | object : T_NUMBER | ||
83 | { | ||