Skip to content

Commit 40c7de0

Browse files
committed
feat: first version of the KDL 2.0 parser
1 parent 26c0783 commit 40c7de0

File tree

380 files changed

+4992
-7330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+4992
-7330
lines changed

.github/workflows/build.yml

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# This workflow will build a Java project with Gradle
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3-
41
name: Gradle CI
52

63
on:
74
push:
8-
branches: [ trunk ]
5+
branches:
6+
- trunk
97
pull_request:
10-
branches: [ trunk ]
8+
branches:
9+
- trunk
1110

1211
jobs:
1312
build:
14-
1513
runs-on: ubuntu-latest
16-
1714
steps:
18-
- uses: actions/checkout@v2
19-
- name: Set up JDK 1.8
20-
uses: actions/setup-java@v1
21-
with:
22-
java-version: 1.8
23-
- name: Build with Gradle
24-
run: gradle build
15+
- uses: actions/checkout@v4
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v2
18+
with:
19+
distribution: temurin
20+
java-version: 11
21+
- name: Setup Gradle
22+
uses: gradle/actions/setup-gradle@v3
23+
- name: Build with Gradle
24+
run: ./gradlew build

.github/workflows/release.yaml

-38
This file was deleted.

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Copyright 2022 Hannah Kolbeck
2+
Copyright 2024 Romain Delamare
23

34
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
45
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
@@ -12,4 +13,3 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1213
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1314
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1415
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15-

README.md

+25-45
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,47 @@
1-
# KDL4j
1+
# KDL4j v2
22

3-
A Java implementation of a parser for the [KDL Document Language](https://github.com/kdl-org/kdl).
3+
A Java implementation of a parser for the [KDL Document Language](https://github.com/kdl-org/kdl). Supports KDL
4+
version `2.0.0-draft.4`.
5+
6+
This library targets Java 11 as a minimum version.
47

58
## Status
69

710
![Gradle CI](https://github.com/hkolbeck/kdl4j/workflows/Gradle%20CI/badge.svg)
811

9-
This project is beta-quality. It's been extensively tested, but the spec it implements is still in flux.
10-
1112
## Usage
1213

1314
### Parsing
1415

1516
```java
16-
final KDLParser parser = new KDLParser();
17-
18-
final KDLDocument documentFromString = parser.parse("node_name \"arg\"")
19-
// OR
20-
final KDLDocument documentFromReader = parser.parse(new FileReader("some/file.kdl"))
17+
import kdl.parse.KDLParser;
18+
19+
// Parse from a String
20+
var documentFromString = KDLParser.parse("node_name \"arg\"");
21+
// Parse from an InputStream
22+
var documentFromReader = KDLParser.parse(new ByteArrayInputStream(/**/));
23+
// Parse from a file
24+
var documentFromReader = KDLParser.parse(Paths.get("path", "to", "file"));
2125
```
2226

23-
`KDLDocument` objects, and all descendants of `KDLObject`, are immutable and threadsafe, though that is not true of their
24-
`Builder` objects. If you need to make changes to a `KDLDocument`, use the `filter()` and `mutate()` functions explained below.
25-
26-
### Searching and Mutating Documents
27-
28-
Several utilities are provided for finding nodes in documents. Each presents the same interface, but the way they search
29-
the document differs. There are three search types:
30-
31-
* RootSearch - Searches entirely at the root, primarily used for mutations to the root as discussed below
32-
* GeneralSearch - Searches for nodes anywhere in the tree matching a single, possibly compound, node predicate
33-
* PathedSearch - Searches for nodes down a specified path. At each level a different node predicate can be specified
34-
35-
Each provides four methods for searching or mutating documents:
36-
37-
* `anyMatch(document)` - Returns true if any node matches the search, false otherwise
38-
* `filter(document, trim)` - Removes all nodes from the tree not on a branch that matches the predicates of the search. if
39-
`trim` is set, removes all their non-matching children
40-
* `list(document, trim)` - Produces a new document with all matching nodes at the root. If `trim` is set, removes all
41-
their non-matching children
42-
* `mutate(document, mutation)` - Applies a provided `Mutation` to every matching node in the tree, depth first.
43-
44-
There are 3 types of `Mutations` provided, and users may provide custom mutations. Provided are `AddMutation`,
45-
`SubtractMutation`, and `SetMutation`. Each performs functions hinted at by the name. See individual javadocs for details.
46-
4727
### Printing
4828

49-
By default, calling `document.toKDL()` or `document.writeKDL(writer)` will print the structure with:
50-
51-
* 4 space indents
52-
* No semicolons
53-
* Printable ASCII characters which can be escaped, escaped
54-
* Empty children printed
55-
* `null` arguments and properties with `null` values printed
56-
* `\n` (unicode `\u{0a}`) for newlines
29+
The `KDLPrinter` class allows printing a KDL document to a `String`, a `Writer`, an `OutputStream` or to a file. By
30+
default, it:
31+
32+
- prints one character tabulation for each indentation level
33+
- does not print node separators (`;`)
34+
- does not print braces for nodes without children
35+
- prints arguments and properties with null value
36+
- uses `E` as the exponent character in decimal values
5737

58-
Any of these can be changed by creating a new PrintConfig object and passing it into the print method. See the javadocs
59-
on PrintConfig for more information.
38+
Any of these can be changed by creating a `PrintConfiguration` and passing it to the `KDLPrinter` constructor.
6039

6140
## Contributing
6241

6342
Please read the Code of Conduct before opening any issues or pull requests.
6443

65-
Besides code fixes, the easiest way to contribute is by generating test cases. Check out
66-
[the test cases directory](https://github.com/hkolbeck/kdl4j/tree/trunk/src/test/resources/test_cases) to see the existing ones.
44+
Besides code fixes, the easiest way to contribute is by generating test cases. Check out
45+
[the test cases directory](src/test/resources/test_cases) to see the
46+
existing ones.
6747
See the README there for more details.

build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ java {
3737
}
3838
}
3939

40+
tasks.compileJava {
41+
options.javaModuleVersion = provider { version as String }
42+
}
43+
4044
tasks.test {
4145
useJUnitPlatform()
4246
finalizedBy(tasks.jacocoTestReport)
@@ -55,6 +59,8 @@ tasks.jacocoTestReport {
5559
val mockitoVersion = "5.10.0"
5660

5761
dependencies {
62+
implementation("jakarta.annotation:jakarta.annotation-api:2.1.1")
63+
5864
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
5965
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
6066
testImplementation("org.assertj:assertj-core:3.25.3")

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.3.0
1+
version=2.0.0.beta-1

src/main/java/kdl/Fuzzer.java

-16
This file was deleted.

src/main/java/kdl/KDLBoolean.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package kdl;
2+
3+
import jakarta.annotation.Nonnull;
4+
import java.util.Objects;
5+
import java.util.Optional;
6+
7+
/**
8+
* A KDL boolean value.
9+
*/
10+
public class KDLBoolean extends KDLValue<Boolean> {
11+
/**
12+
* Creates a new {@link KDLBoolean}.
13+
*
14+
* @param type the type of the value
15+
* @param value the value
16+
*/
17+
public KDLBoolean(@Nonnull Optional<String> type, boolean value) {
18+
super(type);
19+
this.value = value;
20+
}
21+
22+
@Nonnull
23+
@Override
24+
public Boolean getValue() {
25+
return value;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
var that = (KDLBoolean) o;
33+
return value == that.value && Objects.equals(type, that.type);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(value, type);
39+
}
40+
41+
private final boolean value;
42+
}

src/main/java/kdl/KDLDocument.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package kdl;
2+
3+
import jakarta.annotation.Nonnull;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* A KDL document.
11+
*/
12+
public class KDLDocument {
13+
/**
14+
* Creates a new document
15+
*
16+
* @param nodes the nodes in the document
17+
*/
18+
public KDLDocument(@Nonnull List<KDLNode> nodes) {
19+
this.nodes = Collections.unmodifiableList(nodes);
20+
}
21+
22+
/**
23+
* The nodes of the document.
24+
*
25+
* @return an immutable list containing the nodes of the document
26+
*/
27+
@Nonnull
28+
public List<KDLNode> getNodes() {
29+
return nodes;
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
var that = (KDLDocument) o;
37+
return Objects.equals(nodes, that.nodes);
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash(nodes);
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "KDLDocument[" + nodes.stream().map(KDLNode::toString).collect(Collectors.joining(", ")) + ']';
48+
}
49+
50+
@Nonnull
51+
private final List<KDLNode> nodes;
52+
}

0 commit comments

Comments
 (0)