From 8cb6ec6834647dc71f38882e20c64026efc0e9f4 Mon Sep 17 00:00:00 2001 From: Prashanth Mundkur Date: Mon, 13 Apr 2009 10:18:24 -0700 Subject: [PATCH] Directory re-org to put json conversion generator into its own subdirectory. --- libs/json/OMakefile | 28 ++-------- libs/json/README | 51 +++++++++++++++++++ libs/json/{ => gen_json_conv}/codegen.ml | 2 +- .../gen_json_conv.ml} | 0 libs/json/{ => gen_json_conv}/lexer.mll | 0 libs/json/{ => gen_json_conv}/parser.mly | 0 libs/json/{ => gen_json_conv}/syntax.ml | 0 .../tests}/OMakefile | 9 ++-- .../tests/test_json_conv.ml} | 0 .../tests}/test_types.ml | 0 libs/json/{base_conv.ml => json_conv.ml} | 0 libs/json/{base_conv.mli => json_conv.mli} | 0 libs/json/parser_tests/run_tests.sh | 2 +- libs/json/{ => parser_tests}/test_parser.ml | 0 14 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 libs/json/README rename libs/json/{ => gen_json_conv}/codegen.ml (99%) rename libs/json/{jsonc.ml => gen_json_conv/gen_json_conv.ml} (100%) rename libs/json/{ => gen_json_conv}/lexer.mll (100%) rename libs/json/{ => gen_json_conv}/parser.mly (100%) rename libs/json/{ => gen_json_conv}/syntax.ml (100%) rename libs/json/{jsonc_tests => gen_json_conv/tests}/OMakefile (78%) rename libs/json/{jsonc_tests/tester.ml => gen_json_conv/tests/test_json_conv.ml} (100%) rename libs/json/{jsonc_tests => gen_json_conv/tests}/test_types.ml (100%) rename libs/json/{base_conv.ml => json_conv.ml} (100%) rename libs/json/{base_conv.mli => json_conv.mli} (100%) rename libs/json/{ => parser_tests}/test_parser.ml (100%) diff --git a/libs/json/OMakefile b/libs/json/OMakefile index 9a9a338..175b489 100644 --- a/libs/json/OMakefile +++ b/libs/json/OMakefile @@ -5,36 +5,14 @@ OCAMLFLAGS += -dtypes JSON_FILES[] = json json_parse - base_conv + json_conv LIB = json JSON_LIB = $(OCamlLibrary $(LIB), $(JSON_FILES)) -section - TEST_PARSER_PROG = test_parser - OCAML_LIBS += json - TEST_PARSER = $(OCamlProgram $(TEST_PARSER_PROG), test_parser $(JSON_FILES)) - export TEST_PARSER - -section - GEN_FILES = parser.mli parser.ml lexer.ml - OCamlGeneratedFiles($(GEN_FILES)) - OCAML_OTHER_LIBS[] += unix - CONV_FILES[] = - lexer - parser - syntax - codegen - jsonc - - JSON_CONV_PROG = jsonc - JSON_CONV = $(OCamlProgram $(JSON_CONV_PROG), $(CONV_FILES)) - export JSON_CONV JSON_CONV_PROG - - -.DEFAULT: $(JSON_LIB) $(TEST_PARSER) $(JSON_CONV) +.DEFAULT: $(JSON_LIB) clean: rm -f $(filter-proper-targets $(ls R, .)) *.annot *.cmo -.SUBDIRS: jsonc_tests +.SUBDIRS: gen_json_conv parser_tests diff --git a/libs/json/README b/libs/json/README new file mode 100644 index 0000000..3e494e9 --- /dev/null +++ b/libs/json/README @@ -0,0 +1,51 @@ +This directory contains an incremental JSON parser, and a JSON<->OCaml +conversion function generator. + +JSON parser: +------------ + +The parser is incremental in the sense that it can take pieces of +input strings at a time that do not comprise a complete json value. +This is useful when the json value is produced piecemeal, for example, +when it arrives over a network. + +After consuming each string, the parser returns either (a) a +notification that it needs more input to complete parsing a value, or +(b) a parsed json value (along with any remaining unconsumed part of +the input), or (c) a parsing exception. + + +JSON <-> OCaml conversion function generator: +------------------------------------------- + +The generator (in gen_json_conv/) takes as input a file containing _only_ OCaml +type definitions, and generates an output file containing functions to +convert ocaml values of the specified types to and from json values. + +The input type definitions can only use a restriction of the Ocaml +type language, specified below: + + base_type := | | | + + type_expr := base_type + | type_expr 'option' + | '{' field ':' type_expr ';' field ':' type_expr ... } + | type_expr 'array' + | type_expr 'list' + | type_expr '*' type_expr + | constr_type_expr { '|' constr_type_expr } + + constr_type_expr := constr_name + | constr_name 'of' type_expr { '*' type_expr } + +That is, only mono-morphic types constructed from the specified base +types are allowed; function types, polymorphic types or polymorphic +variants, classes and exceptions are not handled, and result in parse +errors. + +The "None" value of an option type is mapped to the json "null" value. + +The generated functions are named _of_json and +_to_json for each input type defined. *_of_json functions +can fail with a conversion exception. + diff --git a/libs/json/codegen.ml b/libs/json/gen_json_conv/codegen.ml similarity index 99% rename from libs/json/codegen.ml rename to libs/json/gen_json_conv/codegen.ml index b7a0122..347afe2 100644 --- a/libs/json/codegen.ml +++ b/libs/json/gen_json_conv/codegen.ml @@ -324,7 +324,7 @@ let generate_header ff ifn = let call = String.concat " " (Array.to_list Sys.argv) in fprintf ff "(* This file has been auto-generated using \"%s\". *)@\n@\n" call; fprintf ff "open Json@\n"; - fprintf ff "open Base_conv@\n"; + fprintf ff "open Json_conv@\n"; fprintf ff "open %s@\n@\n" (String.capitalize md) let generate_one_defn ff td = diff --git a/libs/json/jsonc.ml b/libs/json/gen_json_conv/gen_json_conv.ml similarity index 100% rename from libs/json/jsonc.ml rename to libs/json/gen_json_conv/gen_json_conv.ml diff --git a/libs/json/lexer.mll b/libs/json/gen_json_conv/lexer.mll similarity index 100% rename from libs/json/lexer.mll rename to libs/json/gen_json_conv/lexer.mll diff --git a/libs/json/parser.mly b/libs/json/gen_json_conv/parser.mly similarity index 100% rename from libs/json/parser.mly rename to libs/json/gen_json_conv/parser.mly diff --git a/libs/json/syntax.ml b/libs/json/gen_json_conv/syntax.ml similarity index 100% rename from libs/json/syntax.ml rename to libs/json/gen_json_conv/syntax.ml diff --git a/libs/json/jsonc_tests/OMakefile b/libs/json/gen_json_conv/tests/OMakefile similarity index 78% rename from libs/json/jsonc_tests/OMakefile rename to libs/json/gen_json_conv/tests/OMakefile index 17ea766..466f50d 100644 --- a/libs/json/jsonc_tests/OMakefile +++ b/libs/json/gen_json_conv/tests/OMakefile @@ -1,6 +1,6 @@ .PHONY: clean -OCAMLFLAGS += -I .. +OCAMLFLAGS += -I .. -I ../.. test_types_json_conv.ml: test_types.ml $(JSON_CONV) ../$(JSON_CONV_PROG) -i $< -o $@ @@ -8,11 +8,12 @@ test_types_json_conv.ml: test_types.ml $(JSON_CONV) TESTER_FILES[] = test_types test_types_json_conv - tester + test_json_conv OCAML_LIBS[] += - ../json -TESTER_PROG = test_conv + ../../json + +TESTER_PROG = test_json_conv TESTER = $(OCamlProgram $(TESTER_PROG), $(TESTER_FILES)) .DEFAULT: $(TESTER) diff --git a/libs/json/jsonc_tests/tester.ml b/libs/json/gen_json_conv/tests/test_json_conv.ml similarity index 100% rename from libs/json/jsonc_tests/tester.ml rename to libs/json/gen_json_conv/tests/test_json_conv.ml diff --git a/libs/json/jsonc_tests/test_types.ml b/libs/json/gen_json_conv/tests/test_types.ml similarity index 100% rename from libs/json/jsonc_tests/test_types.ml rename to libs/json/gen_json_conv/tests/test_types.ml diff --git a/libs/json/base_conv.ml b/libs/json/json_conv.ml similarity index 100% rename from libs/json/base_conv.ml rename to libs/json/json_conv.ml diff --git a/libs/json/base_conv.mli b/libs/json/json_conv.mli similarity index 100% rename from libs/json/base_conv.mli rename to libs/json/json_conv.mli diff --git a/libs/json/parser_tests/run_tests.sh b/libs/json/parser_tests/run_tests.sh index 2e86a1e..1fd06f9 100755 --- a/libs/json/parser_tests/run_tests.sh +++ b/libs/json/parser_tests/run_tests.sh @@ -1,6 +1,6 @@ #!/bin/bash -PROG=../test_parser +PROG=./test_parser for f in `ls *pass*.json`; do $PROG $f > /dev/null 2>&1 diff --git a/libs/json/test_parser.ml b/libs/json/parser_tests/test_parser.ml similarity index 100% rename from libs/json/test_parser.ml rename to libs/json/parser_tests/test_parser.ml -- 2.39.5