Skip to content

Instantly share code, notes, and snippets.

@alavrik
Created August 17, 2011 04:28
Show Gist options
  • Save alavrik/1150815 to your computer and use it in GitHub Desktop.
Save alavrik/1150815 to your computer and use it in GitHub Desktop.
Piq as a configuration file format
let read_file filename =
let ch = open_in_bin filename in
let len = in_channel_length ch in
let buf = Buffer.create len in
Buffer.add_channel buf ch len;
close_in ch;
Buffer.contents buf
let read_config filename =
let contents = read_file filename in
try
Config_piqi_ext.parse_config contents `piq
with
Piqi_common.Error ((file, col, line), error) ->
failwith (Printf.sprintf "error at %s:%d:%d: %s" file col line error)
(* test *)
let _ =
let config = read_config "config.piq" in
Config_piqi_ext.print_config config
:config/config [
% the shortest form
[ "foo" .make ]
% explicit field name
[ .path "foo" .make ]
% more explicit field names
[ .path "foo" .generator.make ]
% alternative notation for representing enums (variants)
[ .path "foo" .generator (.make) ]
% the longest form
[
.path "foo"
.generator.make
]
% testing other enum values
[ "bar" .latexmk ]
[ "fum" .org ]
[ "baz" .tensile ]
]
.module config
.enum [
.name generator
.option [ .name make ]
.option [ .name latexmk ]
.option [ .name org ]
.option [ .name tensile ]
]
.record [
.name entry
.field [
.name path
.type string
]
.field [
.type generator
]
]
.list [
.name config
.type entry
]
# this build relies on OCamlMakefile build script, it can be found here:
#
# http://www.ocaml.info/home/ocaml_sources.html#ocaml-make
#
RESULT = config
SOURCES = \
$(PIQI_ML_FILES) \
config.ml
PACKS = piqi.lib
PIQI_FILES = config.piqi
PIQI_ML_FILES = config_piqi.ml config_piqi_ext.ml
PRE_TARGETS = $(PIQI_ML_FILES)
PIQIC = piqic
PIQIC_FLAGS = --pp --leave-tmp-files
all: native-code #byte-code debug-code
$(PIQI_ML_FILES): $(PIQI_FILES)
set -e; \
for i in $^; do \
$(PIQIC) ocaml-ext $(PIQIC_FLAGS) $$i ; \
done
clean::
rm -f *.tmp.ml
include OCamlMakefile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment