Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KDL v2 Support #1

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.0

* Support for [KDL2](https://github.com/kdl-org/kdl/pull/286) added

# 0.3.0

* Started tracking changes with CHANGELOG file
Expand Down
213 changes: 141 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Kuddle

A KDL encoder and decoder for elixir.
Kuddle is a [KDL](https://github.com/kdl-org/kdl) Encoder and Decoder library for Elixir.

## Version 1 when?
It is currently up to date with KDL v2 Draft 4.

* When I feel the documentation is complete
* When I finally add line numbers for debugging
* When I finally add proper errors
## Installation (V2)

Only then will I annoint kuddle with version 1.0.0.
To add `kuddle` with v2 support to your project:

## Installation
```elixir
defp deps do
[
{:kuddle, "~> 1.0.0"},
]
end
```

To add `kuddle` to your project:
## Installation (V1)

To add `kuddle` with original v1 to your project:

```elixir
defp deps do
Expand All @@ -25,10 +31,10 @@ end
## Usage

```elixir
# Decode a KDL document
{:ok, kdl_doc, _rest} = Kuddle.decode(kdl_blob)
# Decode a KDL v2 document
{:ok, kdl_doc, []} = Kuddle.decode(kdl_blob)

# Encode a KDL document
# Encode a KDL v2 document
{:ok, kdl_blob} = Kuddle.encode(kdl_doc)

# Lookup nodes in a document
Expand All @@ -44,81 +50,144 @@ Check out [kuddle_config](https://github.com/IceDragon200/kuddle_config)

## Test Cases

`test/fixtures/test_cases` contains a copy of the official language tests
`test/fixtures/{v1,v2}/test_cases` contains a copy of the official language tests for their respective spec versions.

## Known Issues
## Supports

Some of the tests are still failing, mostly around parsing invalid values, since the parser is quite lax about the format of terms (the default value from the tokenizer)
* [x] Keywords

## Supports
```elixir
{:ok, nodes, []} = Kuddle.decode("""
node-true #true
node-false #false
node-null #null
node-nan #nan
node-inf #inf
node--inf #-inf
"""
)

[
%Kuddle.Node{
name: "node-true",
attributes: [
%Kuddle.Value{
type: :boolean,
value: true
}
]
},
%Kuddle.Node{
name: "node-false",
attributes: [
%Kuddle.Value{
type: :boolean,
value: false
}
]
},
%Kuddle.Node{
name: "node-null",
attributes: [
%Kuddle.Value{
type: :null,
value: nil
}
]
},
%Kuddle.Node{
name: "node-nan",
attributes: [
%Kuddle.Value{
type: :nan,
value: :nan
}
]
},
%Kuddle.Node{
name: "node-inf",
attributes: [
%Kuddle.Value{
type: :infinity,
value: :infinity
}
]
},
%Kuddle.Node{
name: "node--inf",
attributes: [
%Kuddle.Value{
type: :infinity,
value: :'-infinity'
}
]
}
] = ndoes
```

* [x] Nodes

```elixir
%Kuddle.Node{
name: "node",
annotations: [],
attributes: [],
children: [
%Kuddle.Node{
name: "node2",
annotations: [],
attributes: [],
children: [
%Kuddle.Node{
name: "node3",
annotations: [],
attributes: [],
children: nil,
}
]
{:ok, nodes, []} = Kuddle.decode("""
node {
node2 {
node3
}
]
} = Kuddle.decode("""
node {
node2 {
node3
}
"""
)

[
%Kuddle.Node{
name: "node",
children: [
%Kuddle.Node{
name: "node2",
children: [
%Kuddle.Node{
name: "node3",
}
]
}
}
"""
)
]
}
] = ndoes
```

* [x] Annotations

```elixir
%Kuddle.Node{
name: "node",
annotations: ["root"],
attributes: [],
children: [
%Kuddle.Node{
name: "node2",
annotations: [],
attributes: [
%Kuddle.Value{
type: :integer,
format: :dec,
annotations: ["u8"],
value: 23,
}
],
children: [
%Kuddle.Node{
name: "node3",
annotations: [],
attributes: [],
children: nil,
}
]
{:ok, nodes, []} = Kuddle.decode("""
(root)node {
node2 (u8)23 {
node3
}
]
} = Kuddle.decode("""
(root)node {
node2 (u8)23 {
node3
}
"""
)

[
%Kuddle.Node{
name: "node",
annotations: ["root"],
children: [
%Kuddle.Node{
name: "node2",
attributes: [
%Kuddle.Value{
type: :integer,
format: :dec,
annotations: ["u8"],
value: 23,
}
],
children: [
%Kuddle.Node{
name: "node3",
}
]
}
}
"""
)
]
}
] = nodes
```
14 changes: 8 additions & 6 deletions lib/kuddle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ defmodule Kuddle do
@moduledoc """
Kuddle is a KDL (https://github.com/kdl-org/kdl) encoder and decoder.

Kuddle is KDL 1.0.0 compliant and should be able to process most if not all KDL documents without
issue.
It is compliant with both the 1.x and 2.x specifications, simply use the appropriately versioned
module for your needs.

And yes UTF-8 works.
And yes UTF-8 still works.

V2 is the default.
"""
@type document :: Kuddle.Decoder.document()
@type document :: Kuddle.V2.Decoder.document()

@doc """
Decode a KDL document into kuddle nodes
Expand All @@ -20,7 +22,7 @@ defmodule Kuddle do
@spec decode(String.t()) ::
{:ok, document(), rest::String.t()}
| {:error, term()}
defdelegate decode(blob), to: Kuddle.Decoder
defdelegate decode(blob), to: Kuddle.V2.Decoder

@doc """
Encode a kuddle document as serialized KDL
Expand All @@ -33,7 +35,7 @@ defmodule Kuddle do
@spec encode(document()) ::
{:ok, String.t()}
| {:error, term()}
defdelegate encode(doc), to: Kuddle.Encoder
defdelegate encode(doc), to: Kuddle.V2.Encoder

@doc """
Select allows searching a document for particular nodes by name, and or attributes.
Expand Down
41 changes: 41 additions & 0 deletions lib/kuddle/tokens.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
defmodule Kuddle.Tokens do
@moduledoc """
Tokens emitted by the Tokenizers.
"""
import Record

@type token_meta :: {:token_meta, line_no::integer(), col_no::integer()}

@type open_annotation_token :: {:open_annotation, unused::any(), token_meta()}

@type close_annotation_token :: {:close_annotation, unused::any(), token_meta()}

@type open_block_token :: {:open_block, unused::any(), token_meta()}

@type close_block_token :: {:close_block, unused::any(), token_meta()}

@type slashdash_token :: {:slashdash, unused::any(), token_meta()}

@type comment_type :: :line | :span | :multiline

@type comment_token :: {:comment, {comment_type(), String.t()}, token_meta()}

@type dquote_string_token :: {:dquote_string, String.t(), token_meta()}

@type raw_string_token :: {:raw_string, String.t(), token_meta()}

@type space_token :: {:space, {String.t(), len::non_neg_integer()}, token_meta()}

@type newline_token :: {:nl, unused::any(), token_meta()}

@type equal_token :: {:=, unused::any(), token_meta()}

@type semicolon_token :: {:sc, unused::any(), token_meta()}

@type fold_token :: {:fold, unused::any(), token_meta()}

@type term_token :: {:term, String.t(), token_meta()}

defrecord :r_token_meta, :meta, [line_no: 1, col_no: 1]

# v1 Token - V1 uses asingle annotation token type, v2 uses this after extracting the tokens
# in the parsing step
defrecord :r_annotation_token, :annotation, [:value, :meta]

# v2 Tokens - V2 marks the brackets and expects the decoder to handle whatever is between them
defrecord :r_open_annotation_token, :open_annotation, [:value, :meta]
defrecord :r_close_annotation_token, :close_annotation, [:value, :meta]

defrecord :r_open_block_token, :open_block, [:value, :meta]

defrecord :r_close_block_token, :close_block, [:value, :meta]
Expand Down
Loading