This commit is contained in:
parent
e58398bee5
commit
9f452d257a
1 changed files with 93 additions and 91 deletions
|
|
@ -38,121 +38,123 @@ let duration_value =
|
||||||
|
|
||||||
let read_file file = In_channel.with_open_bin file In_channel.input_all
|
let read_file file = In_channel.with_open_bin file In_channel.input_all
|
||||||
|
|
||||||
type item = {
|
module Untyped = struct
|
||||||
key: string;
|
type item = {
|
||||||
value: string;
|
key: string;
|
||||||
}
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
type line =
|
type line =
|
||||||
| Blank
|
| Blank
|
||||||
| Comment of string
|
| Comment of string
|
||||||
| Header of string
|
| Header of string
|
||||||
| Item of item
|
| Item of item
|
||||||
|
|
||||||
type section = {
|
type section = {
|
||||||
header: string;
|
header: string;
|
||||||
items: item list;
|
items: item list;
|
||||||
}
|
}
|
||||||
|
|
||||||
open Angstrom
|
open Angstrom
|
||||||
|
|
||||||
let is_eol = function '\n' | '\r' -> true | _ -> false
|
let is_eol = function '\n' | '\r' -> true | _ -> false
|
||||||
let is_whitespace = function ' ' | '\t' -> true | _ -> false
|
let is_whitespace = function ' ' | '\t' -> true | _ -> false
|
||||||
let whitespace = skip_while is_whitespace
|
let whitespace = skip_while is_whitespace
|
||||||
|
|
||||||
let id =
|
let id =
|
||||||
let ident_char = function
|
let ident_char = function
|
||||||
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '-' -> true
|
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '-' -> true
|
||||||
| _ -> false
|
| _ -> false
|
||||||
in
|
in
|
||||||
take_while1 ident_char >>| String.lowercase_ascii
|
take_while1 ident_char >>| String.lowercase_ascii
|
||||||
|
|
||||||
let take_till_end_of_line =
|
let take_till_end_of_line =
|
||||||
take_till is_eol >>= fun s -> return s <* end_of_line
|
take_till is_eol >>= fun s -> return s <* end_of_line
|
||||||
|
|
||||||
let blank = whitespace <* end_of_line >>| fun () -> Blank
|
let blank = whitespace <* end_of_line >>| fun () -> Blank
|
||||||
|
|
||||||
let comment =
|
let comment =
|
||||||
whitespace *> (char '#' <|> char '%') *> take_till_end_of_line >>| fun s ->
|
whitespace *> (char '#' <|> char '%') *> take_till_end_of_line >>| fun s ->
|
||||||
Comment s
|
Comment s
|
||||||
|
|
||||||
let header = char '[' *> id <* char ']' <* end_of_line >>| fun s -> Header s
|
let header = char '[' *> id <* char ']' <* end_of_line >>| fun s -> Header s
|
||||||
|
|
||||||
let item_value =
|
let item_value =
|
||||||
let unquoted_value =
|
let unquoted_value =
|
||||||
take_while1 (fun c -> not (is_whitespace c || is_eol c))
|
take_while1 (fun c -> not (is_whitespace c || is_eol c))
|
||||||
in
|
in
|
||||||
let quoted_value =
|
let quoted_value =
|
||||||
char '"' *> take_till is_eol >>= fun s ->
|
char '"' *> take_till is_eol >>= fun s ->
|
||||||
match String.ends_with ~suffix:"\"" s with
|
match String.ends_with ~suffix:"\"" s with
|
||||||
| false -> fail "invalid quoted value"
|
| false -> fail "invalid quoted value"
|
||||||
| true ->
|
| true ->
|
||||||
let value = String.sub s 0 (String.length s - 1) in
|
let value = String.sub s 0 (String.length s - 1) in
|
||||||
return value
|
return value
|
||||||
in
|
in
|
||||||
quoted_value <|> unquoted_value
|
quoted_value <|> unquoted_value
|
||||||
|
|
||||||
let item =
|
let item =
|
||||||
lift2
|
lift2
|
||||||
(fun key value -> Item { key; value })
|
(fun key value -> Item { key; value })
|
||||||
id
|
id
|
||||||
(whitespace *> char '=' *> whitespace *> item_value)
|
(whitespace *> char '=' *> whitespace *> item_value)
|
||||||
<* end_of_line
|
<* end_of_line
|
||||||
|
|
||||||
let config = many (choice [ blank; comment; header; item ]) <* end_of_input
|
let config = many (choice [ blank; comment; header; item ]) <* end_of_input
|
||||||
|
|
||||||
let fold_sections l =
|
let fold_sections l =
|
||||||
let rec loop section_l item_l l =
|
let rec loop section_l item_l l =
|
||||||
match l with
|
match l with
|
||||||
| [] ->
|
| [] ->
|
||||||
if List.is_empty item_l then section_l
|
if List.is_empty item_l then section_l
|
||||||
else Fmt.failwith "invalid config structure"
|
else Fmt.failwith "invalid config structure"
|
||||||
| Blank :: tl | Comment _ :: tl -> loop section_l item_l tl
|
| Blank :: tl | Comment _ :: tl -> loop section_l item_l tl
|
||||||
| Item item :: tl -> loop section_l (item :: item_l) tl
|
| Item item :: tl -> loop section_l (item :: item_l) tl
|
||||||
| Header header :: tl ->
|
| Header header :: tl ->
|
||||||
let section = { header; items= item_l } in
|
let section = { header; items= item_l } in
|
||||||
loop (section :: section_l) [] tl
|
loop (section :: section_l) [] tl
|
||||||
in
|
in
|
||||||
loop [] [] (List.rev l)
|
loop [] [] (List.rev l)
|
||||||
|
|
||||||
let parse s =
|
let parse s =
|
||||||
match parse_string ~consume:All config s with
|
match parse_string ~consume:All config s with
|
||||||
| Error msg -> Fmt.failwith "config parse error: %s" msg
|
| Error msg -> Fmt.failwith "config parse error: %s" msg
|
||||||
| Ok v -> fold_sections v
|
| Ok v -> fold_sections v
|
||||||
|
|
||||||
module Pp_debug = struct
|
module Pp_debug = struct
|
||||||
let pp_item ppf { key; value } =
|
let pp_item ppf { key; value } =
|
||||||
let open Fmt in
|
let open Fmt in
|
||||||
match String.contains value '"' || String.contains value ' ' with
|
match String.contains value '"' || String.contains value ' ' with
|
||||||
| false -> pf ppf "%s = %s" key value
|
| false -> pf ppf "%s = %s" key value
|
||||||
| true -> pf ppf "%s = \"%s\"" key value
|
| true -> pf ppf "%s = \"%s\"" key value
|
||||||
|
|
||||||
let pp_line ppf line =
|
let pp_line ppf line =
|
||||||
let open Fmt in
|
let open Fmt in
|
||||||
match line with
|
match line with
|
||||||
| Blank -> Fmt.nop ppf ()
|
| Blank -> Fmt.nop ppf ()
|
||||||
| Comment s -> pf ppf "#%s" s
|
| Comment s -> pf ppf "#%s" s
|
||||||
| Header s -> pf ppf "[%s]" s
|
| Header s -> pf ppf "[%s]" s
|
||||||
| Item item -> pf ppf "%a" pp_item item
|
| Item item -> pf ppf "%a" pp_item item
|
||||||
|
|
||||||
let _pp_lines ppf raw_line_l =
|
let _pp_lines ppf raw_line_l =
|
||||||
let open Fmt in
|
let open Fmt in
|
||||||
pf ppf "%a" (list ~sep:(any "\n") pp_line) raw_line_l
|
pf ppf "%a" (list ~sep:(any "\n") pp_line) raw_line_l
|
||||||
|
|
||||||
let pp_section ppf { header; items } =
|
let pp_section ppf { header; items } =
|
||||||
let open Fmt in
|
let open Fmt in
|
||||||
pf ppf "[%s]@\n%a" header (list ~sep:(any "\n") pp_item) items
|
pf ppf "[%s]@\n%a" header (list ~sep:(any "\n") pp_item) items
|
||||||
|
|
||||||
let pp_config ppf l =
|
let pp_config ppf l =
|
||||||
let open Fmt in
|
let open Fmt in
|
||||||
pf ppf "%a" (list ~sep:(any "\n") pp_section) l
|
pf ppf "%a" (list ~sep:(any "\n") pp_section) l
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let content = read_file "default.config" in
|
let content = read_file "default.config" in
|
||||||
(*Fmt.pr "%s" content;*)
|
(*Fmt.pr "%s" content;*)
|
||||||
let config = parse content in
|
let config = Untyped.parse content in
|
||||||
(*Fmt.pr "%a@." pp_lines config;*)
|
(*Fmt.pr "%a@." pp_lines config;*)
|
||||||
Fmt.pr "%a@." Pp_debug.pp_config config;
|
Fmt.pr "%a@." Untyped.Pp_debug.pp_config config;
|
||||||
|
|
||||||
()
|
()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue