Skip to content

Commit c9898cd

Browse files
authored
feat(parser): v2 parser (#70)
This is a major rewrite of kdl-rs to comply with the KDL v2 spec.
1 parent 6044ef9 commit c9898cd

File tree

290 files changed

+2960
-1290
lines changed

Some content is hidden

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

290 files changed

+2960
-1290
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
runs-on: ${{ matrix.os }}
2929
strategy:
3030
matrix:
31-
rust: [1.56.0, stable]
31+
rust: [1.70.0, stable]
3232
os: [ubuntu-latest, macOS-latest, windows-latest]
3333

3434
steps:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ default = ["span"]
1616
span = []
1717

1818
[dependencies]
19-
miette = "5.7.0"
19+
miette = "7.2.0"
2020
nom = "7.1.1"
2121
thiserror = "1.0.40"
22+
winnow = { version = "0.6.20", features = ["alloc", "unstable-recover"] }
2223

2324
[dev-dependencies]
24-
miette = { version = "5.7.0", features = ["fancy"] }
25+
miette = { version = "7.2.0", features = ["fancy"] }
2526
pretty_assertions = "1.3.0"

README.md

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,6 @@ assert_eq!(&doc.to_string(), node_str);
6666
[`KdlDocument`], [`KdlNode`], [`KdlEntry`], and [`KdlIdentifier`] can all
6767
be parsed and managed this way.
6868

69-
#### Query Engine
70-
71-
`kdl` includes a query engine for
72-
[KQL](https://github.com/kdl-org/kdl/blob/main/QUERY-SPEC.md), which lets you
73-
pick out nodes from a document using a CSS Selectors-style syntax.
74-
75-
Queries can be done from either a [`KdlDocument`] or a [`KdlNode`], with
76-
mostly the same semantics.
77-
78-
```rust
79-
use kdl::KdlDocument;
80-
81-
let doc = r#"
82-
a {
83-
b 1
84-
c 2
85-
d 3 {
86-
e prop="hello"
87-
}
88-
}
89-
"#.parse::<KdlDocument>().expect("failed to parse KDL");
90-
91-
let results = doc.query("a > b").expect("failed to parse query");
92-
assert_eq!(results, Some(&doc.nodes()[0].children().unwrap().nodes()[0]));
93-
94-
let results = doc.query_get("e", "prop").expect("failed to parse query");
95-
assert_eq!(results, Some(&"hello".into()));
96-
97-
let results = doc.query_get_all("a > []", 0).expect("failed to parse query").collect::<Vec<_>>();
98-
assert_eq!(results, vec![&1.into(), &2.into(), &3.into()]);
99-
```
100-
10169
### Error Reporting
10270

10371
[`KdlError`] implements [`miette::Diagnostic`] and can be used to display
@@ -154,6 +122,10 @@ means a few things:
154122
you [`KdlDocument::fmt`] in which case the original representation will be
155123
thrown away and the actual value will be used when serializing.
156124

125+
### Minimum Supported Rust Version
126+
127+
You must be at least `1.70.0` tall to get on this ride.
128+
157129
### License
158130

159131
The code in this repository is covered by [the Apache-2.0

examples/Cargo.kdl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package {
2-
name "kdl"
2+
name kdl
33
version "0.0.0"
4-
description "kat's document language"
4+
description "The kdl document language"
55
authors "Kat Marchán <[email protected]>"
6-
license-file "LICENSE.md"
6+
license-file LICENSE.md
77
edition "2018"
88
}
99

examples/ci.kdl

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
// This example is a GitHub Action if it used KDL syntax.
22
// See .github/workflows/ci.yml for the file this was based on.
3-
name "CI"
3+
name CI
44

5-
on "push" "pull_request"
5+
on push pull_request
66

77
env {
8-
RUSTFLAGS "-Dwarnings"
8+
RUSTFLAGS -Dwarnings
99
}
1010

1111
jobs {
1212
fmt_and_docs "Check fmt & build docs" {
13-
runs-on "ubuntu-latest"
13+
runs-on ubuntu-latest
1414
steps {
1515
step uses="actions/checkout@v1"
1616
step "Install Rust" uses="actions-rs/toolchain@v1" {
17-
profile "minimal"
18-
toolchain "stable"
19-
components "rustfmt"
20-
override true
17+
profile minimal
18+
toolchain stable
19+
components rustfmt
20+
override #true
2121
}
22-
step "rustfmt" run="cargo fmt --all -- --check"
23-
step "docs" run="cargo doc --no-deps"
22+
step rustfmt { run cargo fmt --all -- --check }
23+
step docs { run cargo doc --no-deps }
2424
}
2525
}
2626
build_and_test "Build & Test" {
2727
runs-on "${{ matrix.os }}"
2828
strategy {
2929
matrix {
30-
rust "1.46.0" "stable"
31-
os "ubuntu-latest" "macOS-latest" "windows-latest"
30+
rust "1.46.0" stable
31+
os ubuntu-latest macOS-latest windows-latest
3232
}
3333
}
3434

3535
steps {
3636
step uses="actions/checkout@v1"
3737
step "Install Rust" uses="actions-rs/toolchain@v1" {
38-
profile "minimal"
38+
profile minimal
3939
toolchain "${{ matrix.rust }}"
40-
components "clippy"
41-
override true
40+
components clippy
41+
override #true
4242
}
43-
step "Clippy" run="cargo clippy --all -- -D warnings"
44-
step "Run tests" run="cargo test --all --verbose"
43+
step Clippy { run cargo clippy --all -- -D warnings }
44+
step "Run tests" { run cargo test --all --verbose }
45+
step "Other Stuff" run="
46+
echo foo
47+
echo bar
48+
echo baz
49+
"
4550
}
4651
}
4752
}

examples/custom-errors.rs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
/// Show how to build your own diagnostics, without having to use the
2-
/// `fancy` feature or having `main()` return `miette::Result`
3-
use kdl::KdlDocument;
4-
use miette::Diagnostic;
5-
use miette::SourceSpan;
1+
// TODO(@zkat): Error stuff has changed, so this needs updating.
62

7-
#[derive(Debug)]
8-
pub struct MyError {
9-
pub message: String,
10-
}
3+
// /// Show how to build your own diagnostics, without having to use the
4+
// /// `fancy` feature or having `main()` return `miette::Result`
5+
// ///
6+
// use kdl::KdlDocument;
7+
// use miette::Diagnostic;
8+
// use miette::SourceSpan;
119

12-
fn parse(input: &str) -> Result<KdlDocument, MyError> {
13-
let doc = input.parse::<KdlDocument>();
14-
doc.map_err(|error| {
15-
let source = error
16-
.source_code()
17-
.expect("parse errors should have source code");
18-
let help = error.help.unwrap_or_default();
19-
let span: SourceSpan = error.span;
20-
let contents = source
21-
.read_span(&span, 0, 0)
22-
.expect("source should have span contents");
23-
// miette uses 0 based indexes, but humans prefer 1-based
24-
let line = contents.line() + 1;
25-
let column = contents.column() + 1;
26-
let message = format!(
27-
"line {}, column {}: {}\n help: {}",
28-
line, column, error, help
29-
);
30-
MyError { message }
31-
})
32-
}
10+
// #[derive(Debug)]
11+
// pub struct MyError {
12+
// pub message: String,
13+
// }
14+
15+
// fn parse(input: &str) -> Result<KdlDocument, MyError> {
16+
// let doc = input.parse::<KdlDocument>();
17+
// doc.map_err(|error| {
18+
// let source = error
19+
// .source_code()
20+
// .expect("parse errors should have source code");
21+
// let help = error.help.unwrap_or_default();
22+
// let span: SourceSpan = error.span;
23+
// let contents = source
24+
// .read_span(&span, 0, 0)
25+
// .expect("source should have span contents");
26+
// // miette uses 0 based indexes, but humans prefer 1-based
27+
// let line = contents.line() + 1;
28+
// let column = contents.column() + 1;
29+
// let message = format!(
30+
// "line {}, column {}: {}\n help: {}",
31+
// line, column, error, help
32+
// );
33+
// MyError { message }
34+
// })
35+
// }
3336

3437
fn main() {
35-
let input = r#"
36-
foo {
37-
bar {
38-
baz 1.
39-
}
40-
}
41-
"#;
42-
let err = parse(input).unwrap_err();
43-
eprintln!("{}", err.message);
44-
// Output:
45-
// line 4, column 14: Expected valid value.
46-
// help: Floating point numbers must be base 10, and have numbers after the decimal point.
38+
// let input = r#"
39+
// foo {
40+
// bar {
41+
// baz 1.
42+
// }
43+
// }
44+
// "#;
45+
// let err = parse(input).unwrap_err();
46+
// eprintln!("{}", err.message);
47+
// // Output:
48+
// // line 4, column 14: Expected valid value.
49+
// // help: Floating point numbers must be base 10, and have numbers after the decimal point.
4750
}

examples/insert-node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ words {
2727
let word_node = KdlNode::new(identifier);
2828
word_nodes.push(word_node);
2929
word_nodes.sort_by(sort_by_name);
30-
words_section.fmt();
30+
words_section.autoformat();
3131

3232
println!("{}", doc);
3333

0 commit comments

Comments
 (0)