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

v2 parser #70

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ default = ["span"]
span = []

[dependencies]
miette = "5.7.0"
miette = "7.2.0"
nom = "7.1.1"
thiserror = "1.0.40"
winnow = { version = "0.6.5", features = ["alloc", "unstable-recover"] }

[dev-dependencies]
miette = { version = "5.7.0", features = ["fancy"] }
miette = { version = "7.2.0", features = ["fancy"] }
pretty_assertions = "1.3.0"
6 changes: 3 additions & 3 deletions examples/Cargo.kdl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package {
name "kdl"
name kdl
version "0.0.0"
description "kat's document language"
description "The kdl document language"
authors "Kat Marchán <[email protected]>"
license-file "LICENSE.md"
license-file LICENSE.md
edition "2018"
}

Expand Down
39 changes: 22 additions & 17 deletions examples/ci.kdl
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
// This example is a GitHub Action if it used KDL syntax.
// See .github/workflows/ci.yml for the file this was based on.
name "CI"
name CI

on "push" "pull_request"
on push pull_request

env {
RUSTFLAGS "-Dwarnings"
RUSTFLAGS -Dwarnings
}

jobs {
fmt_and_docs "Check fmt & build docs" {
runs-on "ubuntu-latest"
runs-on ubuntu-latest
steps {
step uses="actions/checkout@v1"
step "Install Rust" uses="actions-rs/toolchain@v1" {
profile "minimal"
toolchain "stable"
components "rustfmt"
override true
profile minimal
toolchain stable
components rustfmt
override #true
}
step "rustfmt" run="cargo fmt --all -- --check"
step "docs" run="cargo doc --no-deps"
step rustfmt { run cargo fmt --all -- --check }
step docs { run cargo doc --no-deps }
}
}
build_and_test "Build & Test" {
runs-on "${{ matrix.os }}"
strategy {
matrix {
rust "1.46.0" "stable"
os "ubuntu-latest" "macOS-latest" "windows-latest"
rust "1.46.0" stable
os ubuntu-latest macOS-latest windows-latest
}
}

steps {
step uses="actions/checkout@v1"
step "Install Rust" uses="actions-rs/toolchain@v1" {
profile "minimal"
profile minimal
toolchain "${{ matrix.rust }}"
components "clippy"
override true
components clippy
override #true
}
step "Clippy" run="cargo clippy --all -- -D warnings"
step "Run tests" run="cargo test --all --verbose"
step Clippy { run cargo clippy --all -- -D warnings }
step "Run tests" { run cargo test --all --verbose }
step "Other Stuff" run="
echo foo
echo bar
echo baz
"
}
}
}
87 changes: 45 additions & 42 deletions examples/custom-errors.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
/// Show how to build your own diagnostics, without having to use the
/// `fancy` feature or having `main()` return `miette::Result`
use kdl::KdlDocument;
use miette::Diagnostic;
use miette::SourceSpan;
// TODO(@zkat): Error stuff has changed, so this needs updating.

#[derive(Debug)]
pub struct MyError {
pub message: String,
}
// /// Show how to build your own diagnostics, without having to use the
// /// `fancy` feature or having `main()` return `miette::Result`
// ///
// use kdl::KdlDocument;
// use miette::Diagnostic;
// use miette::SourceSpan;

fn parse(input: &str) -> Result<KdlDocument, MyError> {
let doc = input.parse::<KdlDocument>();
doc.map_err(|error| {
let source = error
.source_code()
.expect("parse errors should have source code");
let help = error.help.unwrap_or_default();
let span: SourceSpan = error.span;
let contents = source
.read_span(&span, 0, 0)
.expect("source should have span contents");
// miette uses 0 based indexes, but humans prefer 1-based
let line = contents.line() + 1;
let column = contents.column() + 1;
let message = format!(
"line {}, column {}: {}\n help: {}",
line, column, error, help
);
MyError { message }
})
}
// #[derive(Debug)]
// pub struct MyError {
// pub message: String,
// }

// fn parse(input: &str) -> Result<KdlDocument, MyError> {
// let doc = input.parse::<KdlDocument>();
// doc.map_err(|error| {
// let source = error
// .source_code()
// .expect("parse errors should have source code");
// let help = error.help.unwrap_or_default();
// let span: SourceSpan = error.span;
// let contents = source
// .read_span(&span, 0, 0)
// .expect("source should have span contents");
// // miette uses 0 based indexes, but humans prefer 1-based
// let line = contents.line() + 1;
// let column = contents.column() + 1;
// let message = format!(
// "line {}, column {}: {}\n help: {}",
// line, column, error, help
// );
// MyError { message }
// })
// }

fn main() {
let input = r#"
foo {
bar {
baz 1.
}
}
"#;
let err = parse(input).unwrap_err();
eprintln!("{}", err.message);
// Output:
// line 4, column 14: Expected valid value.
// help: Floating point numbers must be base 10, and have numbers after the decimal point.
// let input = r#"
// foo {
// bar {
// baz 1.
// }
// }
// "#;
// let err = parse(input).unwrap_err();
// eprintln!("{}", err.message);
// // Output:
// // line 4, column 14: Expected valid value.
// // help: Floating point numbers must be base 10, and have numbers after the decimal point.
}
Loading
Loading