|
| 1 | +(*********************************************************************************) |
| 2 | +(* Dunolint - A tool to lint and help manage files in dune projects *) |
| 3 | +(* Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com> *) |
| 4 | +(* *) |
| 5 | +(* This file is part of Dunolint. *) |
| 6 | +(* *) |
| 7 | +(* Dunolint is free software; you can redistribute it and/or modify it *) |
| 8 | +(* under the terms of the GNU Lesser General Public License as published by *) |
| 9 | +(* the Free Software Foundation either version 3 of the License, or any later *) |
| 10 | +(* version, with the LGPL-3.0 Linking Exception. *) |
| 11 | +(* *) |
| 12 | +(* Dunolint is distributed in the hope that it will be useful, but WITHOUT *) |
| 13 | +(* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *) |
| 14 | +(* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *) |
| 15 | +(* and the file `NOTICE.md` at the root of this repository for more details. *) |
| 16 | +(* *) |
| 17 | +(* You should have received a copy of the GNU Lesser General Public License *) |
| 18 | +(* and the LGPL-3.0 Linking Exception along with this library. If not, see *) |
| 19 | +(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *) |
| 20 | +(*********************************************************************************) |
| 21 | + |
| 22 | +let load_config_exn ~filename = |
| 23 | + let contents = In_channel.read_all filename in |
| 24 | + match Parsexp.Many_and_positions.parse_string contents with |
| 25 | + | Error parse_error -> |
| 26 | + let position = Parsexp.Parse_error.position parse_error in |
| 27 | + let message = Parsexp.Parse_error.message parse_error in |
| 28 | + let loc = |
| 29 | + Sexp_handler.loc_of_parsexp_range |
| 30 | + ~filename |
| 31 | + { start_pos = position; end_pos = position } |
| 32 | + in |
| 33 | + Err.raise ~loc [ Pp.text message ] |
| 34 | + | Ok (sexps, positions) -> |
| 35 | + (match Dunolint.Config.of_stanzas sexps with |
| 36 | + | t -> t |
| 37 | + | exception Sexp.Of_sexp_error (exn, sub) -> |
| 38 | + let range = |
| 39 | + match Parsexp.Positions.find_sub_sexp_in_list_phys positions sexps ~sub with |
| 40 | + | Some _ as range -> range |
| 41 | + | None -> None [@coverage off] |
| 42 | + in |
| 43 | + let loc = |
| 44 | + match range with |
| 45 | + | Some range -> Sexp_handler.loc_of_parsexp_range ~filename range |
| 46 | + | None -> Loc.of_file ~path:(Fpath.v filename) [@coverage off] |
| 47 | + in |
| 48 | + let message = |
| 49 | + match exn with |
| 50 | + | Failure str -> |
| 51 | + Pp.text (if String.is_suffix str ~suffix:"." then str else str ^ ".") |
| 52 | + | exn -> Err.exn exn [@coverage off] |
| 53 | + in |
| 54 | + Err.raise ~loc [ message ]) |
| 55 | +;; |
0 commit comments