Skip to content

Commit b0cbd1e

Browse files
authored
Merge pull request #15 from mbarbin/myconf-example
Add example (myconf)
2 parents ffe8f6a + 82558b4 commit b0cbd1e

File tree

21 files changed

+416
-3
lines changed

21 files changed

+416
-3
lines changed

.github/workflows/more-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
# the development workflow and as part of the main CI job. These are the
5959
# tests that are checked for every combination of os and ocaml-compiler.
6060
- name: Install dependencies
61-
run: opam install ./auto-format.opam ./auto-format-tests.opam --deps-only --with-test
61+
run: opam install ./auto-format.opam ./auto-format-tests.opam ./auto-format-example.opam --deps-only --with-test
6262

6363
- name: Build & Run tests
64-
run: opam exec -- dune build @all @runtest -p auto-format,auto-format-tests
64+
run: opam exec -- dune build @all @runtest -p auto-format,auto-format-tests,auto-format-example

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.0.XX (unreleased)
2+
3+
### Added
4+
5+
- Added an example using the lib (#15, @mbarbin).
6+
17
## 0.0.19 (2025-12-25)
28

39
### Added

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ The code documentation of the latest release is built with `odoc` and published
1111

1212
## Examples & tests
1313

14-
As of yet, there are no examples or tests in this repository. However, this library is extensively used and tested in the [bopkit](https://github.com/mbarbin/bopkit) project.
14+
The `example/` contains the implementation of a toy `key=value` config language, which includes a parser, a pretty-printer and `auto-fmt` utils built with the library.
15+
16+
You can see examples of cli usage in `example/test/run.t`, and an example of `dune fmt` integration in `example/test/dune`.
17+
18+
## See Also
19+
20+
This library is extensively used and tested in the [bopkit](https://github.com/mbarbin/bopkit) project.

example/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Myconf
2+
3+
**A toy key-value config DSL with support for auto-fmt.**
4+
5+
This directory contains a little project that implements a toy config language for `key=value` entries.
6+
7+
The goal is to serve as a code example using the auto-format library.
8+
9+
The code was mostly contributed by Claude, with manual review from @mbarbin.
10+
11+
## Dune Integration
12+
13+
The `myconf` config files located in `test/` are auto-formatted as part of the `dune fmt` build target.
14+
15+
## Using the auto-fmt CLI
16+
17+
Using the library `auto-format` give you back an subcommand `fmt` which you can integrate into an existing cli (e.g. one related to your custom language).
18+
19+
See usage examples in `test/run.t`.

example/bin/dune

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(executable
2+
(name main)
3+
(public_name myconf)
4+
(package auto-format-example)
5+
(flags :standard -w +a-4-40-41-42-44-45-48-66 -warn-error +a)
6+
(libraries auto-format cmdlang cmdlang-cmdliner-err-runner myconf)
7+
(instrumentation
8+
(backend bisect_ppx))
9+
(lint
10+
(pps ppx_js_style -allow-let-operators -check-doc-comments))
11+
(preprocess no_preprocessing))

example/bin/main.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let fmt_cmd =
2+
Auto_format.fmt_cmd
3+
(module struct
4+
let language_id = "myconf"
5+
let extensions = [ ".myconf" ]
6+
end)
7+
(module Myconf.Config)
8+
(module Myconf.Config_parser)
9+
(module Myconf.Config_pp)
10+
;;
11+
12+
let main =
13+
Cmdlang.Command.group ~summary:"Managing myconf configuration files." [ "fmt", fmt_cmd ]
14+
;;
15+
16+
let () = Cmdlang_cmdliner_err_runner.run main ~name:"myconf" ~version:"dev"

example/src/config.ml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Sexp = Sexplib0.Sexp
2+
3+
type value =
4+
| String of string
5+
| Int of int
6+
| Bool of bool
7+
8+
type entry =
9+
| Comment of string
10+
| Binding of
11+
{ loc : Loc.t
12+
; key : string
13+
; value : value
14+
}
15+
16+
type t = entry list
17+
18+
let equal_value v1 v2 =
19+
match v1, v2 with
20+
| String s1, String s2 -> String.equal s1 s2
21+
| Int i1, Int i2 -> Int.equal i1 i2
22+
| Bool b1, Bool b2 -> Bool.equal b1 b2
23+
| (String _ | Int _ | Bool _), _ -> false
24+
;;
25+
26+
let equal_entry e1 e2 =
27+
match e1, e2 with
28+
| Comment c1, Comment c2 -> String.equal c1 c2
29+
| Binding b1, Binding { loc; key; value } ->
30+
Loc.equal b1.loc loc && String.equal b1.key key && equal_value b1.value value
31+
| (Comment _ | Binding _), _ -> false
32+
;;
33+
34+
let equal t1 t2 = List.equal equal_entry t1 t2
35+
36+
let sexp_of_value = function
37+
| String s -> Sexp.List [ Sexp.Atom "String"; Sexp.Atom s ]
38+
| Int i -> Sexp.List [ Sexp.Atom "Int"; Sexp.Atom (Int.to_string i) ]
39+
| Bool b -> Sexp.List [ Sexp.Atom "Bool"; Sexp.Atom (Bool.to_string b) ]
40+
;;
41+
42+
let sexp_of_entry = function
43+
| Comment c -> Sexp.List [ Sexp.Atom "Comment"; Sexp.Atom c ]
44+
| Binding { loc; key; value } ->
45+
Sexp.List
46+
[ Sexp.Atom "Binding"
47+
; Sexp.List
48+
[ Sexp.List [ Sexp.Atom "loc"; Loc.sexp_of_t loc ]
49+
; Sexp.List [ Sexp.Atom "key"; Sexp.Atom key ]
50+
; Sexp.List [ Sexp.Atom "value"; sexp_of_value value ]
51+
]
52+
]
53+
;;
54+
55+
let sexp_of_t t = Sexp.List (List.map sexp_of_entry t)

example/src/config.mli

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type value =
2+
| String of string
3+
| Int of int
4+
| Bool of bool
5+
6+
type entry =
7+
| Comment of string
8+
| Binding of
9+
{ loc : Loc.t
10+
; key : string
11+
; value : value
12+
}
13+
14+
type t = entry list
15+
16+
val equal : t -> t -> bool
17+
val sexp_of_t : t -> Sexplib0.Sexp.t

example/src/config_parser.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type token = Parser.token
2+
type t = Config.t
3+
4+
let lexer = Lexer.read
5+
let parser = Parser.config

example/src/config_parser.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type t = Config.t
2+
3+
include Parsing_utils.S with type t := t

0 commit comments

Comments
 (0)