diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 17:52:21 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 17:52:21 +0100 |
| commit | 7b5baa29818c9fe6e33d50c8458aa2824274158e (patch) | |
| tree | b46d98b0ba039fc77bcf7a4aabd9f453acde8d91 /lib/configuration | |
| parent | 7f04f2f18bdaada855e2f3abfd64b420d9dba127 (diff) | |
Updated the configuration format
Diffstat (limited to 'lib/configuration')
| -rw-r--r-- | lib/configuration/ast.ml | 15 | ||||
| -rw-r--r-- | lib/configuration/ast.mli | 2 | ||||
| -rw-r--r-- | lib/configuration/expression_lexer.mll | 125 | ||||
| -rw-r--r-- | lib/configuration/expression_parser.mly | 133 | ||||
| -rw-r--r-- | lib/configuration/read_conf.ml | 9 | ||||
| -rw-r--r-- | lib/configuration/toml.ml_ | 133 | ||||
| -rw-r--r-- | lib/configuration/toml.mli_ | 1 |
7 files changed, 138 insertions, 280 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml index 5857902..36ba349 100644 --- a/lib/configuration/ast.ml +++ b/lib/configuration/ast.ml @@ -23,6 +23,7 @@ let get_content : setting -> string -> Model.Reference.t Model.Content.t option fun settings name -> Splay.find name settings.contents exception BrokenLink : ('a Type.Id.t * 'a * string) -> exn +exception NotImplemented : ('a Type.Id.t * 'a * string) -> exn (** Convert the content having fields referencing element by name into content having fields referencing other contents *) @@ -51,7 +52,19 @@ let rec convert : Model.Reference.v (fun () -> match get_content setting name with | Some v -> v - | None -> raise (BrokenLink (ty, value, name)))) + | None -> + (* Also check from the list catalog *) + begin match + List.find_opt setting.lists ~f:(fun { id; _ } -> + String.equal id name) + with + | Some v -> + ignore v; + raise_notrace + (NotImplemented (ty, value, name)) + | None -> + raise_notrace (BrokenLink (ty, value, name)) + end)) field); } in diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli index 5eda6f6..9fba7f6 100644 --- a/lib/configuration/ast.mli +++ b/lib/configuration/ast.mli @@ -21,6 +21,8 @@ exception BrokenLink : ('a Type.Id.t * 'a * string) -> exn (** Exception raised when a reference in the configuration targets a non-existing content *) +exception NotImplemented : ('a Type.Id.t * 'a * string) -> exn + val register_list : setting -> list' -> setting val check : setting -> (unit, exn) result diff --git a/lib/configuration/expression_lexer.mll b/lib/configuration/expression_lexer.mll index 1b4602a..041c2fd 100644 --- a/lib/configuration/expression_lexer.mll +++ b/lib/configuration/expression_lexer.mll @@ -1,93 +1,84 @@ { open Expression_parser - exception UnclosedQuote of { content: string ; line : int} + exception UnclosedQuote of { content : string; line : int } } +let spaces = [' ' '\t'] -let spaces = [ ' ' '\t' ] -let letters = [^ '$' '"' '\'' '(' ')' '[' ']' ':' '.' ',' '^' ' ' '\t' '\n' '\r' ] -let digit = [ '0'-'9' ] -let eol = [ '\r' '\n' ] +let letters = + [^'$' '"' '\'' '(' ')' '[' ']' ':' '.' ',' '^' ' ' '\t' '\n' '\r'] -let escaped = [ '\'' '\\'] +let digit = ['0'-'9'] + +let eol = ['\r' '\n'] + +let escaped = ['\'' '\\'] rule token = parse -| eol { Lexing.new_line lexbuf; token lexbuf } -| spaces { token lexbuf } -| '\'' { - try read_quoted_string (Buffer.create 17) lexbuf - with Failure _ -> + | eol { Lexing.new_line lexbuf; token lexbuf } + | spaces { token lexbuf } + | '\'' + { + try read_quoted_string (Buffer.create 17) lexbuf + with Failure _ -> let line = lexbuf.Lexing.lex_curr_p.pos_lnum and content = Bytes.to_string lexbuf.Lexing.lex_buffer in - raise (UnclosedQuote {line; content}) -} -| '"' { read_dquoted_string (Buffer.create 17) lexbuf } -| '#' { skip_comment lexbuf } -| '(' { L_PAREN } -| ')' { R_PAREN } -| '[' { L_BRACKET } -| ']' { R_BRACKET } -| ':' { COLUMN } -| '$' { DOLLAR } -| '.' { DOT } -| ',' { COMA } -| '^' { CONCAT_OPERATOR } -| '+' { BINARY_OPERATOR (Model.Dsl.Add) } -| '-' { BINARY_OPERATOR (Model.Dsl.Minus) } -| '/' { BINARY_OPERATOR (Model.Dsl.Division) } -| "and" { BOOL_OPERATOR (Model.Dsl.And) } -| "or" { BOOL_OPERATOR (Model.Dsl.Or) } -| '<' { INEQUALITY_OPERATOR (Model.Dsl.LT) } -| "<=" { INEQUALITY_OPERATOR (Model.Dsl.LTE) } -| '>' { INEQUALITY_OPERATOR (Model.Dsl.GT) } -| ">=" { INEQUALITY_OPERATOR (Model.Dsl.GTE) } -| "<>" { EQUALITY_OPERATOR (Model.Dsl.Different) } -| '=' { EQUALITY_OPERATOR (Model.Dsl.Equal) } -| digit+ as l { INTEGER l} -| '-' digit+ as l { INTEGER l} -| letters+ as l { IDENT l} -| eof { EOF } + raise (UnclosedQuote { line; content }) + } + | '"' { read_dquoted_string (Buffer.create 17) lexbuf } + | '#' { skip_comment lexbuf } + | '(' { L_PAREN } + | ')' { R_PAREN } + | '[' { L_BRACKET } + | ']' { R_BRACKET } + | ':' { COLUMN } + | '$' { DOLLAR } + | '.' { DOT } + | ',' { COMA } + | '^' { CONCAT_OPERATOR } + | '+' { BINARY_OPERATOR Model.Dsl.Add } + | '-' { BINARY_OPERATOR Model.Dsl.Minus } + | '/' { BINARY_OPERATOR Model.Dsl.Division } + | "and" { BOOL_OPERATOR Model.Dsl.And } + | "or" { BOOL_OPERATOR Model.Dsl.Or } + | '<' { INEQUALITY_OPERATOR Model.Dsl.LT } + | "<=" { INEQUALITY_OPERATOR Model.Dsl.LTE } + | '>' { INEQUALITY_OPERATOR Model.Dsl.GT } + | ">=" { INEQUALITY_OPERATOR Model.Dsl.GTE } + | "<>" { EQUALITY_OPERATOR Model.Dsl.Different } + | '=' { EQUALITY_OPERATOR Model.Dsl.Equal } + | digit+ as l { INTEGER l } + | '-' digit+ as l { INTEGER l } + | letters+ as l { IDENT l } + | eof { EOF } and skip_comment = parse - | [^ '\r' '\n' ] - { skip_comment lexbuf } - | eol - { token lexbuf } + | [^'\r' '\n'] { skip_comment lexbuf } + | eol { token lexbuf } (* Read the content until we got another one quote *) and read_quoted_string buf = parse - | [^ '\'' '\\' ]+ - { Buffer.add_string buf (Lexing.lexeme lexbuf); - read_quoted_string buf lexbuf - } - | "\\\'" - { Buffer.add_char buf '\''; + | [^'\'' '\\']+ + { + Buffer.add_string buf (Lexing.lexeme lexbuf); read_quoted_string buf lexbuf } + | "\\'" + { Buffer.add_char buf '\''; read_quoted_string buf lexbuf } | '\\' - { Buffer.add_char buf '\\'; - read_quoted_string buf lexbuf - } - | '\'' - { LITERAL (Buffer.contents buf) - } + { Buffer.add_char buf '\\'; read_quoted_string buf lexbuf } + | '\'' { LITERAL (Buffer.contents buf) } (* Read the content until we got another one quote *) and read_dquoted_string buf = parse - | [^ '"' '\\' ]+ - { Buffer.add_string buf (Lexing.lexeme lexbuf); + | [^'"' '\\']+ + { + Buffer.add_string buf (Lexing.lexeme lexbuf); read_dquoted_string buf lexbuf } | "\\\"" - { Buffer.add_char buf '"'; - read_dquoted_string buf lexbuf - } + { Buffer.add_char buf '"'; read_dquoted_string buf lexbuf } | '\\' - { Buffer.add_char buf '\\'; - read_dquoted_string buf lexbuf - } - | '"' - { - LITERAL (Buffer.contents buf) - } + { Buffer.add_char buf '\\'; read_dquoted_string buf lexbuf } + | '"' { LITERAL (Buffer.contents buf) } diff --git a/lib/configuration/expression_parser.mly b/lib/configuration/expression_parser.mly index 8341362..85ca5d3 100644 --- a/lib/configuration/expression_parser.mly +++ b/lib/configuration/expression_parser.mly @@ -1,68 +1,58 @@ -%token <string>IDENT -%token L_PAREN -%token R_PAREN +%token <string> IDENT +%token L_PAREN +%token R_PAREN %token L_BRACKET R_BRACKET -%token COLUMN DOLLAR -%token DOT -%token <string>LITERAL -%token <string>INTEGER +%token COLUMN DOLLAR +%token DOT +%token <string> LITERAL +%token <string> INTEGER %token COMA %token EOF %token CONCAT_OPERATOR - -%token <Model.Dsl.binary_operator>BINARY_OPERATOR -%token <Model.Dsl.binary_operator>INEQUALITY_OPERATOR -%token <Model.Dsl.binary_operator>EQUALITY_OPERATOR -%token <Model.Dsl.binary_operator>BOOL_OPERATOR +%token <Model.Dsl.binary_operator> BINARY_OPERATOR +%token <Model.Dsl.binary_operator> INEQUALITY_OPERATOR +%token <Model.Dsl.binary_operator> EQUALITY_OPERATOR +%token <Model.Dsl.binary_operator> BOOL_OPERATOR %start <string list Model.Dsl.t> path_expr %right BOOL_OPERATOR %right INEQUALITY_OPERATOR EQUALITY_OPERATOR -%right CONCAT_OPERATOR BINARY_OPERATOR +%right CONCAT_OPERATOR BINARY_OPERATOR %% -path_expr: - | expr_(path_, EOF) EOF { $1 } - | EOF { Model.Dsl.Empty } +path_expr: + | expr_(path_, EOF) EOF { $1 } + | EOF { Model.Dsl.Empty } -path_: - | COLUMN - path = separated_list(DOT, IDENT) - { Model.Dsl.ValuePath path } - | DOLLAR - path = separated_list(DOT, IDENT) - { Model.Dsl.ContentPath path } +path_: + | COLUMN path = separated_list(DOT, IDENT) + { Model.Dsl.ValuePath path } + | DOLLAR path = separated_list(DOT, IDENT) + { Model.Dsl.ContentPath path } arguments(PATH): - | L_PAREN - expr = separated_list(COMA, expr_(PATH, COMA)) - R_PAREN - { expr } + | L_PAREN expr = separated_list(COMA, expr_(PATH, COMA)) R_PAREN + { expr } group(PATH): - | L_BRACKET - expr = separated_list(COMA, expr_(PATH, COMA)) - R_BRACKET - { expr } + | L_BRACKET expr = separated_list(COMA, expr_(PATH, COMA)) + R_BRACKET + { expr } fixed(PATH): - | d = INTEGER - { Model.Dsl.Integer d } - | l = LITERAL - { - if String.equal String.empty l then - Model.Dsl.Empty - else - Model.Dsl.Literal l - } + | d = INTEGER { Model.Dsl.Integer d } + | l = LITERAL + { + if String.equal String.empty l then Model.Dsl.Empty else Model.Dsl.Literal l + } -%inline boperator: - | e = BINARY_OPERATOR { e } - | e = INEQUALITY_OPERATOR { e } - | e = EQUALITY_OPERATOR { e } - | e = BOOL_OPERATOR { e } +%inline boperator: + | e = BINARY_OPERATOR { e } + | e = INEQUALITY_OPERATOR { e } + | e = EQUALITY_OPERATOR { e } + | e = BOOL_OPERATOR { e } (* The expression evaluation receveive in parameters : 1. the way to build a path, as we have two distinct ways to build them in @@ -72,37 +62,24 @@ fixed(PATH): This prevent merlin to optimize thoses two path, and allow more precise error messages. *) expr_(PATH, ENDING_PHANTOM): - | L_PAREN - e = expr_(PATH, R_PAREN) - R_PAREN - { Model.Dsl.Expr e - } - | - p1 = expr_(PATH, ENDING_PHANTOM) - CONCAT_OPERATOR - p2 = expr_(PATH, COMA) - { match p2 with - | Model.Dsl.Function(Concat, args) -> Model.Dsl.Function(Concat, (p1::args)) - | _ -> Model.Dsl.Function(Concat, (p1::p2::[])) - } - | p1 = expr_(PATH, ENDING_PHANTOM) - op = boperator - p2 = expr_(PATH, COMA) - { Model.Dsl.BOperator (op, p1, p2) } - - | p1 = expr_(PATH, ENDING_PHANTOM) - op = EQUALITY_OPERATOR - p2 = group(PATH) - { Model.Dsl.GEquality(op, p1, p2) } - | p = PATH - { p } - | f = fixed(PATH) - { f } - | s = IDENT - args = arguments(PATH) - { Model.Dsl.function_of_name args s } + | L_PAREN e = expr_(PATH, R_PAREN) R_PAREN { Model.Dsl.Expr e } + | p1 = expr_(PATH, ENDING_PHANTOM) CONCAT_OPERATOR + p2 = expr_(PATH, COMA) + { + match p2 with + | Model.Dsl.Function (Concat, args) -> Model.Dsl.Function (Concat, p1 :: args) + | _ -> Model.Dsl.Function (Concat, [ p1; p2 ]) + } + | p1 = expr_(PATH, ENDING_PHANTOM) op = boperator + p2 = expr_(PATH, COMA) + { Model.Dsl.BOperator (op, p1, p2) } + | p1 = expr_(PATH, ENDING_PHANTOM) op = EQUALITY_OPERATOR + p2 = group(PATH) + { Model.Dsl.GEquality (op, p1, p2) } + | p = PATH { p } + | f = fixed(PATH) { f } + | s = IDENT args = arguments(PATH) + { Model.Dsl.function_of_name args s } -opt_arg(PATH, SEP): - | expr = expr_(PATH, COMA) - SEP - { expr } +opt_arg(PATH, SEP): + | expr = expr_(PATH, COMA) SEP { expr } diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml index f079e4e..66cd1fe 100644 --- a/lib/configuration/read_conf.ml +++ b/lib/configuration/read_conf.ml @@ -134,6 +134,15 @@ module Make (S : Decoders.Decode.S) = struct ^ "\". Expected existing list or content" in S.fail_with (Decoders.Error.make ~context message)) + | Error (Ast.NotImplemented (ty', context, name) as e) -> ( + match Type.Id.provably_equal ty ty' with + | None -> raise e + | Some Type.Equal -> + let message = + "Unsupported target \"" ^ name + ^ "\". Lists as target are not implemented yet" + in + S.fail_with (Decoders.Error.make ~context message)) | Error (Graph_tree.Cycle contents) -> let content_name = List.map contents ~f:(fun c -> Model.Content.id c) in let message = diff --git a/lib/configuration/toml.ml_ b/lib/configuration/toml.ml_ deleted file mode 100644 index 3648f2b..0000000 --- a/lib/configuration/toml.ml_ +++ /dev/null @@ -1,133 +0,0 @@ -open StdLabels - -let rec pp : - ?topLevel:bool -> ?path:string list -> Format.formatter -> Otoml.t -> unit = - fun ?(topLevel = true) ?(path = []) format -> function - | Otoml.TomlString v -> begin - match String.contains v '\n' with - | false -> - Format.pp_print_string format "\""; - Format.pp_print_string format v; - Format.pp_print_string format "\"" - | true -> - Format.pp_print_string format {|"""|}; - Format.pp_print_text format v; - Format.pp_print_string format {|"""|} - end - | Otoml.TomlInteger i -> Format.pp_print_int format i - | Otoml.TomlFloat f -> Format.pp_print_float format f - | Otoml.TomlBoolean b -> Format.pp_print_bool format b - | Otoml.TomlArray l -> begin - match (topLevel, l) with - | _, [] -> Format.pp_print_string format "[]" - | false, _ -> Format.pp_print_string format "..." - | true, l -> - Format.pp_print_string format "["; - Format.pp_print_break format 0 4; - Format.pp_open_vbox format 0; - Format.pp_print_list - ~pp_sep:(fun f () -> - Format.pp_print_string f ","; - Format.pp_print_cut f ()) - pp format l; - Format.pp_close_box format (); - Format.pp_print_cut format (); - Format.pp_print_string format "]" - end - | Otoml.TomlTable elements | Otoml.TomlInlineTable elements -> - pp_table ~path format elements - | Otoml.TomlTableArray t -> Format.pp_print_list pp format t - | Otoml.TomlOffsetDateTime _ | Otoml.TomlLocalDate _ - | Otoml.TomlLocalDateTime _ | Otoml.TomlLocalTime _ -> - () - -and pp_key_values : Format.formatter -> string * Otoml.t -> unit = - fun format (name, value) -> - Format.fprintf format "%s = %a" name (pp ~topLevel:false ~path:[]) value - -and pp_table : - ?path:string list -> Format.formatter -> (string * Otoml.t) list -> unit = - fun ?(path = []) format elements -> - (* Create two lists, one for the subtables, and one for the values. - - As a table is valid until the next table, we then start to print the - preoperties before printintg the subtables inside the element. - *) - let subtables, properties = - List.partition elements ~f:(fun (_, v) -> - match v with Otoml.TomlTable _ -> true | _ -> false) - in - - let () = - match properties with - | [] -> () - | _ -> - let isTopLevel = match path with [] -> true | _ -> false in - if not isTopLevel then begin - let path = List.rev path in - Format.pp_print_cut format (); - Format.fprintf format "[%a]" - (Format.pp_print_list - ~pp_sep:(fun f () -> Format.pp_print_string f ".") - Format.pp_print_string) - path; - Format.pp_print_break format 0 4; - Format.pp_open_vbox format 0 - end; - if isTopLevel then begin - Format.pp_print_list ~pp_sep:Format.pp_print_cut - (fun format v -> - match v with - | key, Otoml.TomlTable elements -> - pp_table ~path:(key :: path) format elements - | other -> pp_key_values format other) - format properties - end - else begin - Format.pp_print_string format "..." - end; - - if not isTopLevel then begin - Format.pp_close_box format () - end; - Format.pp_print_cut format () - in - (* Then go deeper inside each subtable *) - List.iter subtables ~f:(function - | name, Otoml.TomlTable v -> pp_table ~path:(name :: path) format v - | _ -> (* Because of the partition, this should not happen *) ()) - -module Decode = struct - module S = struct - type value = Otoml.t - - let pp : Format.formatter -> value -> unit = - fun formatter v -> - Format.pp_open_vbox formatter 0; - pp formatter v; - Format.pp_close_box formatter () - - let of_string : string -> (value, string) result = - Otoml.Parser.from_string_result - - let of_file : string -> (value, string) result = - Otoml.Parser.from_file_result - - let get_string : value -> string option = Otoml.get_opt Otoml.get_string - let get_int : value -> int option = Otoml.get_opt Otoml.get_integer - let get_float : value -> float option = Otoml.get_opt Otoml.get_float - let get_bool : value -> bool option = Otoml.get_opt Otoml.get_boolean - let get_null : value -> unit option = fun _ -> None - - let get_list : value -> value list option = - Otoml.get_opt @@ Otoml.get_array Fun.id - - let get_key_value_pairs : value -> (value * value) list option = - Otoml.get_opt (fun key -> - Otoml.get_table key |> List.map ~f:(fun (k, v) -> (Otoml.string k, v))) - - let to_list : value list -> value = Otoml.array - end - - include Decoders.Decode.Make (S) -end diff --git a/lib/configuration/toml.mli_ b/lib/configuration/toml.mli_ deleted file mode 100644 index 08d30b8..0000000 --- a/lib/configuration/toml.mli_ +++ /dev/null @@ -1 +0,0 @@ -module Decode : Decoders.Decode.S with type value = Otoml.t |
