mte/include/parse_config.ml

128 lines
3.2 KiB
OCaml
Raw Normal View History

2025-10-13 04:51:35 +02:00
(* parse config file
https://docs.taler.net/manpages/taler-exchange.conf.5.html
do not support "$"-path expansion*)
(* TODO
- integer
- duration
- YES|NO
- alt_unit_names json pair
type duration_element = {
number: int;
unit_: [ `Year | `Week | `Day | `Hour | `Minute | `Second ];
}
let integer =
take_while1 (function '0' .. '9' -> true | _ -> false) >>= fun s ->
match int_of_string_opt s with
| None -> fail "not an integer"
| Some i -> return i
let duration_value =
let number = whitespace *> integer in
let unit =
whitespace *> take_while1 (fun c -> not (is_whitespace c || is_eol c))
>>= function
| "year" | "years" -> return `Year
| "week" | "weeks" -> return `Week
| "day" | "days" -> return `Day
| "hour" | "hours" -> return `Hour
| "minute" | "minutes" -> return `Minute
| "s" -> return `Second
| _ -> fail "not a valid duration unit"
in
many1 (lift2 (fun number unit_ -> { number; unit_ }) number unit)
*)
let read_file file = In_channel.with_open_bin file In_channel.input_all
type item = {
key: string;
value: string;
}
2025-10-13 08:19:38 +02:00
type line =
| Blank
| Comment of string
2025-10-13 09:05:13 +02:00
| Header of string
2025-10-13 08:19:38 +02:00
| Item of item
2025-10-13 04:51:35 +02:00
2025-10-13 09:05:13 +02:00
open Angstrom
let is_eol = function '\n' | '\r' -> true | _ -> false
2025-10-13 04:51:35 +02:00
let is_whitespace = function ' ' | '\t' -> true | _ -> false
let whitespace = skip_while is_whitespace
2025-10-13 09:05:13 +02:00
let id =
2025-10-13 04:51:35 +02:00
let ident_char = function
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '-' -> true
| _ -> false
in
take_while1 ident_char >>| String.lowercase_ascii
2025-10-13 09:05:13 +02:00
let take_till_end_of_line =
take_till is_eol >>= fun s -> return s <* end_of_line
let blank = whitespace <* end_of_line >>| fun () -> Blank
let comment =
whitespace *> (char '#' <|> char '%') *> take_till_end_of_line >>| fun s ->
Comment s
let header = char '[' *> id <* char ']' <* end_of_line >>| fun s -> Header s
2025-10-13 08:19:38 +02:00
let item_value =
2025-10-13 04:51:35 +02:00
let unquoted_value =
take_while1 (fun c -> not (is_whitespace c || is_eol c))
in
let quoted_value =
2025-10-13 09:05:13 +02:00
char '"' *> take_till is_eol >>= fun s ->
match String.ends_with ~suffix:"\"" s with
| false -> fail "invalid quoted value"
| true ->
let value = String.sub s 0 (String.length s - 1) in
return value
2025-10-13 04:51:35 +02:00
in
2025-10-13 09:05:13 +02:00
quoted_value <|> unquoted_value
2025-10-13 04:51:35 +02:00
let item =
lift2
2025-10-13 08:19:38 +02:00
(fun key value -> Item { key; value })
2025-10-13 09:05:13 +02:00
id
(whitespace *> char '=' *> whitespace *> item_value)
<* end_of_line
2025-10-13 04:51:35 +02:00
2025-10-13 09:05:13 +02:00
let config = many (choice [ blank; comment; header; item ]) <* end_of_input
2025-10-13 04:51:35 +02:00
2025-10-13 08:19:38 +02:00
let pp_line ppf line =
2025-10-13 04:51:35 +02:00
let open Fmt in
2025-10-13 08:19:38 +02:00
match line with
| Blank -> Fmt.nop ppf ()
| Comment s -> pf ppf "#%s" s
2025-10-13 09:05:13 +02:00
| Header s -> pf ppf "[%s]" s
| Item { key; value } -> (
match String.contains value '"' || String.contains value ' ' with
| false -> pf ppf "%s = %s" key value
| true -> pf ppf "%s = \"%s\"" key value)
2025-10-13 04:51:35 +02:00
2025-10-13 08:19:38 +02:00
let pp ppf raw_line_l =
2025-10-13 04:51:35 +02:00
let open Fmt in
2025-10-13 09:05:13 +02:00
pf ppf "%a" (list ~sep:(any "\n") pp_line) raw_line_l
2025-10-13 04:51:35 +02:00
2025-10-13 08:19:38 +02:00
let parse s =
2025-10-13 09:05:13 +02:00
match parse_string ~consume:All config s with
| Error msg -> Fmt.failwith "config parse error: %s" msg
| Ok v -> v
2025-10-13 04:51:35 +02:00
let () =
let content = read_file "default.config" in
(*Fmt.pr "%s" content;*)
2025-10-13 08:19:38 +02:00
let config = parse content in
2025-10-13 04:51:35 +02:00
2025-10-13 08:19:38 +02:00
Fmt.pr "%a@." pp config;
2025-10-13 04:51:35 +02:00
()