Skip to content

Commit

Permalink
Mostly update to KDL v2.0.0-draft.4
Browse files Browse the repository at this point in the history
- Equal sign is now defined as `=` | U+FE66 | U+FF1D | U+1F7F0
- BOM is allowed in the start of the document only
- Added \s escape sequence
- Disallowed invalid escape sequences
- Allowed EOF right after the \ line continuation
- Strings and identifiers cannot include characters from the "Disallowed
  Literal Code Points" list
- true, false, and null values are now written as #true, #false, #null
  respectively
- Changed the raw string syntax from r"raw" to #"raw"#
- Removed no-whitespace checks around = and type annotations
- Strings can now be written as bare identifiers
- Added multi-line "dedented" strings, newlines in single-line strings
  are disallowed
- Newlines in multi-line strings are normalized to \n
- Identifiers cannot start with .<digits> anymore (optionally prefixed by +-)
- Added #inf, #-inf, #nan numbers
- true, false, inf, -inf, nan are invalid identifiers
- Small documentation improvements, etc.

TODO: The correct by the spec handling of whitespace is still to be implemented.
TODO: The pretty-printer doesn't yet work well for some special symbols and
      generally isn't good enough in edge cases.

Other KDL v2 changes have alredy been implemented before, namely:
Line Tabulation as whitespace, \/ is no longer an escape sequence, \ whitespace
escape.
  • Loading branch information
eilvelia committed Apr 1, 2024
1 parent d238285 commit 02190a8
Show file tree
Hide file tree
Showing 16 changed files with 768 additions and 510 deletions.
11 changes: 3 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

## Unreleased

- Updated to KDL v2.0 (many breaking changes, see the KDL changelog).
- Reworked numbers: added the `Kdl.Num` module, including
`Kdl.Num.to_{string,float,int,int32,int64,nativeint}` and other functions.
- `Kdl.L`: Added `(.@!())` and `(.@!())<-` indexing operators as raising
versions of `(.@())` and `(.@()<-)`.
- Reworked numbers: added the `Kdl.Num` module,
`Kdl.Num.to_{string,float,int,int32,int64,nativeint}` and other functions.
- Updated to KDL v2.0:
- - Added Line Tabulation U+000B to whitespace characters.
- - Removed `\/` from escape sequences.
- - Added whitespace escape sequence: all whitespace (including multiple
newlines) is removed after `\`.
- - Identifiers cannot start with `r#` anymore.
- `interpret` now raises `Invalid_annotation` instead of `Failure`.
- `i8`, `i16`, etc. wrappers over `interpret` are removed.
- Dropped support for OCaml < 4.10.0.
Expand Down
77 changes: 0 additions & 77 deletions GRAMMAR.md

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

55 changes: 55 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Blue Oak Model License

Version 1.0.0

## Purpose

This license gives everyone as much permission to work with
this software as possible, while protecting contributors
from liability.

## Acceptance

In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.

## Copyright

Each contributor licenses you to do everything with this
software that would otherwise infringe that contributor's
copyright in it.

## Notices

You must ensure that everyone who gets a copy of
any part of this software from you, with or without
changes, also gets the text of this license or a link to
<https://blueoakcouncil.org/license/1.0.0>.

## Excuse

If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
ends immediately.

## Patent

Each contributor licenses you to do everything with this
software that would otherwise infringe any patent claims
they can license or become able to license.

## Reliability

No contributor can revoke this license.

## No Liability

***As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
[ci-badge]: https://github.com/Bannerets/ocaml-kdl/actions/workflows/ci.yml/badge.svg
[ci-page]: https://github.com/Bannerets/ocaml-kdl/actions/workflows/ci.yml

An OCaml implementation of the [KDL][] 1.0 document language.
An OCaml implementation of the [KDL][] Document Language, version 2.0.0.

[KDL]: https://github.com/kdl-org/kdl

```console
$ opam install kdl
```

**Note**: This is the working version for KDL v2. For the published version of
ocaml-kdl for KDL v1, see the `v0.1.0` tag.

## Features

- [x] Parsing
Expand Down
8 changes: 4 additions & 4 deletions kdl.opam
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ opam-version: "2.0"
name: "kdl"
version: "0.1.0"
synopsis: "OCaml implementation of the KDL document laguage"
maintainer: "github.com/Bannerets <[email protected]>"
authors: "github.com/Bannerets <[email protected]>"
license: "MIT"
tags: ["configuration-language" "data-serialization-language"]
maintainer: "https://github.com/Bannerets <[email protected]>"
authors: "https://github.com/Bannerets <[email protected]>"
license: "BlueOak-1.0.0"
tags: ["configuration-language" "data-serialization-format"]
homepage: "https://github.com/Bannerets/ocaml-kdl"
bug-reports: "https://github.com/Bannerets/ocaml-kdl/issues"
dev-repo: "git+https://github.com/Bannerets/ocaml-kdl.git"
Expand Down
30 changes: 17 additions & 13 deletions kdl.opam.locked
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ opam-version: "2.0"
name: "kdl"
version: "0.1.0"
synopsis: "OCaml implementation of the KDL document laguage"
maintainer: "github.com/Bannerets <[email protected]>"
authors: "github.com/Bannerets <[email protected]>"
license: "MIT"
tags: [ "configuration-language" "data-serialization-language"]
maintainer: "https://github.com/Bannerets <[email protected]>"
authors: "https://github.com/Bannerets <[email protected]>"
license: "BlueOak-1.0.0"
tags: [ "configuration-language" "data-serialization-format"]
homepage: "https://github.com/Bannerets/ocaml-kdl"
bug-reports: "https://github.com/Bannerets/ocaml-kdl/issues"
depends: [
Expand All @@ -16,27 +16,30 @@ depends: [
"base-unix" {= "base"}
"camlp-streams" {= "5.0.1" & with-doc}
"cmdliner" {= "1.2.0" & with-doc}
"conf-bash" {= "1" & with-test}
"cppo" {= "1.6.9" & with-doc}
"crunch" {= "3.3.1" & with-doc}
"csexp" {= "1.5.2" & with-test}
"dune" {= "3.11.1"}
"dune-configurator" {= "3.11.1" & with-test}
"dune" {= "3.14.2"}
"dune-configurator" {= "3.14.2" & with-test}
"fmt" {= "0.9.0" & with-doc}
"fpath" {= "0.7.3" & with-doc}
"gen" {= "1.1"}
"jane-street-headers" {= "v0.16.0" & with-test}
"jst-config" {= "v0.16.0" & with-test}
"menhir" {= "20230608"}
"menhirLib" {= "20230608"}
"menhirSdk" {= "20230608"}
"menhir" {= "20231231"}
"menhirCST" {= "20231231"}
"menhirLib" {= "20231231"}
"menhirSdk" {= "20231231"}
"ocaml" {= "4.14.1"}
"ocaml-base-compiler" {= "4.14.1"}
"ocaml-compiler-libs" {= "v0.12.4"}
"ocaml-config" {= "2"}
"ocaml-options-vanilla" {= "1"}
"ocamlbuild" {= "0.14.2" & with-doc}
"ocamlbuild" {= "0.14.3" & with-doc}
"ocamlfind" {= "1.9.6" & with-doc}
"odoc" {= "2.2.1" & with-doc}
"odoc-parser" {= "2.0.0" & with-doc}
"odoc" {= "2.4.1" & with-doc}
"odoc-parser" {= "2.4.1" & with-doc}
"ppx_assert" {= "v0.16.0" & with-test}
"ppx_base" {= "v0.16.0" & with-test}
"ppx_cold" {= "v0.16.0" & with-test}
Expand All @@ -50,7 +53,8 @@ depends: [
"ppx_inline_test" {= "v0.16.1" & with-test}
"ppx_optcomp" {= "v0.16.0" & with-test}
"ppx_sexp_conv" {= "v0.16.0" & with-test}
"ppxlib" {= "0.31.0"}
"ppxlib" {= "0.32.0"}
"ptime" {= "1.1.0" & with-doc}
"re" {= "1.11.0" & with-test}
"result" {= "1.5" & with-doc}
"sedlex" {= "3.2"}
Expand Down
21 changes: 14 additions & 7 deletions src/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module Num = struct
| `Decimal of string
]

(* Note: Float.to_string outputs "inf", "-inf", "nan" for the infinity,
neg_infinity, and nan values respectively; Float.of_string can parse those
strings. That makes the functions suitable for KDL's #inf, #-inf, #nan
as-is. *)

let to_string : [< t ] -> string = function
| `Int int -> Int.to_string int
| `Int_raw str
Expand Down Expand Up @@ -56,10 +61,12 @@ module Num = struct
let[@inline] to_unsigned_literal lit =
(* 42 -> 0u42, 0x42 -> 0x42, etc. somewhat hacky *)
if String.length lit >= 1 && lit.[0] = '-'
&& not (String.length lit = 2 && lit.[1] = '0') then None else
let with_prefix = String.length lit >= 3
&& (lit.[1] = 'x' || lit.[1] = 'o' || lit.[1] = 'b') in
Some (if with_prefix then lit else "0u" ^ lit)
&& not (String.length lit = 2 && lit.[1] = '0') then
None
else
let with_prefix = String.length lit >= 3
&& (lit.[1] = 'x' || lit.[1] = 'o' || lit.[1] = 'b') in
Some (if with_prefix then lit else "0u" ^ lit)
end

let to_int : [< t ] -> int option = function
Expand Down Expand Up @@ -236,10 +243,10 @@ type annot_value = string option * value
type prop = string * annot_value

type node = {
name : string; (** An identifier or a quoted string *)
name : string;
annot : string option;
args : annot_value list;
props : prop list; (** [props] is an assoc list; the order of [props] is unspecified *)
props : prop list; (** [props] is an assoc list; the order is unspecified *)
children : node list;
}

Expand All @@ -262,7 +269,7 @@ let equal_prop (name1, annot_value1 : prop) (name2, annot_value2 : prop) =

open struct
(* This is the implementation of List.equal from OCaml 4.14.0, present
for backward compatibility with OCaml < 4.12.0 *)
for backward compatibility with OCaml < 4.12.0 *)
let rec list_equal eq l1 l2 =
match l1, l2 with
| [], [] -> true
Expand Down
Loading

0 comments on commit 02190a8

Please sign in to comment.