Skip to content

Commit

Permalink
progress on new parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 3, 2024
1 parent c0f6020 commit 1cce72f
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ span = []
miette = "5.7.0"
nom = "7.1.1"
thiserror = "1.0.40"
winnow = "0.5.40"
winnow = { version = "0.6.5", features = ["alloc", "unstable-recover"] }

[dev-dependencies]
miette = { version = "5.7.0", features = ["fancy"] }
Expand Down
36 changes: 22 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ use {
std::convert::{TryFrom, TryInto},
};

#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("Failed to parse KDL document.")]
pub struct KdlParseFailure {
#[source_code]
pub input: String,

#[related]
pub errors: Vec<KdlDiagnostic>,
}

/// An error that occurs when parsing a KDL document.
/// The toplevel `Error` type for KDL: this is returned when a KDL document
/// failed to parse entirely.
///
/// This error implements [`miette::Diagnostic`] and can be used to display
/// detailed, pretty-printed diagnostic messages when using [`miette::Result`]
/// and the `"fancy"` feature flag for `miette`:
/// This diagnostic implements [`miette::Diagnostic`] and can be used to
/// display detailed, pretty-printed diagnostic messages when using
/// [`miette::Result`] and the `"fancy"` feature flag for `miette`:
///
/// ```no_run
/// fn main() -> miette::Result<()> {
Expand All @@ -45,6 +36,19 @@ pub struct KdlParseFailure {
/// help: Floating point numbers must be base 10, and have numbers after the decimal point.
/// ```
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("Failed to parse KDL document.")]
pub struct KdlParseFailure {
#[source_code]
pub input: String,

#[related]
pub diagnostics: Vec<KdlDiagnostic>,
}

/// An individual diagnostic message for a KDL parsing issue.
///
/// While generally signifying errors, they can also be treated as warnings.
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("{kind}")]
pub struct KdlDiagnostic {
/// Source string for the KDL document that failed to parse.
Expand All @@ -62,6 +66,10 @@ pub struct KdlDiagnostic {
#[help]
pub help: Option<&'static str>,

/// Severity level for the Diagnostic.
#[diagnostic(severity)]
pub severity: miette::Severity,

/// Specific error kind for this parser error.
pub kind: KdlErrorKind,
}
Expand Down
2 changes: 2 additions & 0 deletions src/query_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::query::{
};
use crate::v1_parser::{value, KdlParser};
use crate::{KdlDiagnostic, KdlErrorKind, KdlParseError, KdlValue};
use miette::Severity;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::combinator::{all_consuming, cut, map, opt, recognize};
Expand Down Expand Up @@ -47,6 +48,7 @@ impl<'a> KdlQueryParser<'a> {
} else {
KdlErrorKind::Context("a valid KQL query")
},
severity: Severity::Error,
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/v1_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::ops::RangeTo;

use crate::nom_compat::{many0, many1, many_till};
use miette::SourceSpan;
use miette::{Severity, SourceSpan};
use nom::branch::alt;
use nom::bytes::complete::{tag, take_until, take_while, take_while_m_n};
use nom::character::complete::{anychar, char, none_of, one_of};
Expand Down Expand Up @@ -58,6 +58,7 @@ impl<'a> KdlParser<'a> {
} else {
KdlErrorKind::Other
},
severity: Severity::Error,
}
})
}
Expand Down
Loading

0 comments on commit 1cce72f

Please sign in to comment.