package kdl

  1. Overview
  2. Docs
OCaml implementation of the KDL Document Language

Install

Dune Dependency

Authors

Maintainers

Sources

v0.2.0.tar.gz
sha256=c6bff16d8b9194f1d99789ad45aaee948cdf3ebeba1a3e839d261e341329c4c7

Description

README

ocaml-kdl  

OCaml implementation of the KDL Document Language v2.

$ opam install kdl

Features

  • [x] Parsing

  • [x] Pretty-printing

  • [x] Lenses

  • [ ] KDL Query Language (KQL)

  • [ ] KDL Schema Language

  • [ ] Formatting-preserving editing

Usage

Example:

# let kdl = Kdl.of_string {|
    contents {
      section "First section" {
        paragraph "This is the first paragraph"
        paragraph "This is the second paragraph"
      }
    }
  |}
val kdl : (Kdl.t, Kdl.error) result = Ok
 [{Kdl.name = "contents"; annot = None; args = []; props = [];
   children =
    [{Kdl.name = "section"; annot = None;
      args = [(None, `String "First section")]; props = [];
      children =
       [{Kdl.name = "paragraph"; annot = None;
         args = [(None, `String "This is the first paragraph")]; props = [];
         children = []};
        {Kdl.name = "paragraph"; annot = None;
         args = [(None, `String "This is the second paragraph")]; props = [];
         children = []}]}]}]

Convert to a sexp-expression (uses the sexplib0 library):

# Kdl.sexp_of_t kdl |> Sexplib0.Sexp.to_string_hum |> print_endline
(contents
 (children
  (section (string "First section")
   (children (paragraph (string "This is the first paragraph"))
    (paragraph (string "This is the second paragraph"))))))

Pretty-print the KDL document back:

# Kdl.to_string kdl |> print_endline
contents {
  section "First section" {
    paragraph "This is the first paragraph"
    paragraph "This is the second paragraph"
  }
}

Similar to to.ml, ocaml-kdl provides lenses. The lenses can be composed using // or |--:

# let open Kdl.L in
  kdl.@(top // child "section" // child_nth 1 // first_arg // string_value)
- : string option = Some "This is the second paragraph"

Additionaly, type-annotated values can be "interpreted":

# let parsed = Kdl.of_string_exn "- (u8)220"
# Kdl.interpret Kdl.L.(parsed.@!(node "-" // first_arg))
- : [> Kdl.typed_value ] = `U8 220

For the full list of available functions, see the mli file.

Dependencies (5)

  1. sexplib0
  2. menhirLib
  3. menhir
  4. dune >= "3.0"
  5. ocaml >= "4.14"

Dev Dependencies (3)

  1. odoc with-doc
  2. zarith with-test
  3. ppx_expect with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.