refactor 🔨 (): clearify the idea of general parser.

This commit is contained in:
abdelkadous 2026-06-03 17:20:53 +01:00
parent 916adcf0fe
commit 5370c3dd05
5 changed files with 1432 additions and 1200 deletions

View File

@ -6,7 +6,7 @@ use std::fmt::Display;
use crate::KdlNodeFormat;
use crate::{
FormatConfig, KdlError, KdlNode, KdlValue,
v2_parser::{Input, KdlParser, KdlVersion},
v2_parser::{KdlParser, KdlVersion},
};
/// Represents a KDL
@ -374,14 +374,14 @@ impl KdlDocument {
/// Parses a KDL v2 string into a document.
pub fn parse_v2(s: &str) -> Result<Self, KdlError> {
let parser = KdlParser::new(KdlVersion::V2);
KdlParser::try_parse(|input: &mut Input<'_>| parser.document(input), s)
parser.try_parse(KdlParser::document, s)
}
/// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let parser = KdlParser::new(KdlVersion::V1);
KdlParser::try_parse(|input: &mut Input<'_>| parser.document(input), s)
parser.try_parse(KdlParser::document, s)
}
/// Takes a KDL v1 document string and returns the same document, but

View File

@ -2,7 +2,10 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};
use crate::{KdlError, KdlIdentifier, KdlValue, v2_parser};
use crate::{
KdlError, KdlIdentifier, KdlValue,
v2_parser::{self, KdlParser, KdlVersion},
};
/// KDL Entries are the "arguments" to KDL nodes: either a (positional)
/// [`Argument`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) or
@ -202,13 +205,15 @@ impl KdlEntry {
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::KdlParser::try_parse(v2_parser::padded_node_entry, s)
parser_v2.try_parse(KdlParser::padded_node_entry, s)
}
#[cfg(feature = "v1-fallback")]
{
v2_parser::KdlParser::try_parse(v2_parser::padded_node_entry, s)
parser_v2
.try_parse(KdlParser::padded_node_entry, s)
.or_else(|e| KdlEntry::parse_v1(s).map_err(|_| e))
}
}
@ -216,8 +221,8 @@ impl KdlEntry {
/// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlEntry, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
let parser_v1 = KdlParser::new(KdlVersion::V1);
parser_v1.try_parse(KdlParser::padded_node_entry, s)
}
/// Makes sure this entry is in v2 format.

View File

@ -2,7 +2,10 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};
use crate::{KdlError, KdlValue, v2_parser};
use crate::{
KdlError, KdlValue,
v2_parser::{KdlParser, KdlVersion},
};
/// Represents a KDL
/// [Identifier](https://github.com/kdl-org/kdl/blob/main/SPEC.md#identifier).
@ -95,13 +98,15 @@ impl KdlIdentifier {
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::KdlParser::try_parse(v2_parser::identifier, s)
parser_v2.try_parse(KdlParser::identifier, s)
}
#[cfg(feature = "v1-fallback")]
{
v2_parser::KdlParser::try_parse(v2_parser::identifier, s)
parser_v2
.try_parse(KdlParser::identifier, s)
.or_else(|e| KdlIdentifier::parse_v1(s).map_err(|_| e))
}
}
@ -109,8 +114,8 @@ impl KdlIdentifier {
/// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlIdentifier, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
let parser_v1 = KdlParser::new(KdlVersion::V1);
parser_v1.try_parse(KdlParser::identifier, s)
}
}

View File

@ -11,7 +11,7 @@ use miette::SourceSpan;
use crate::{
FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlError, KdlIdentifier, KdlValue,
v2_parser,
v2_parser::{self, KdlParser, KdlVersion},
};
/// Represents an individual KDL
@ -336,13 +336,15 @@ impl KdlNode {
/// to parse again as a KDL v1 node. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::KdlParser::try_parse(v2_parser::padded_node, s)
KdlParser::try_parse(|input: &mut Input<'_>| parser_v2.padded_node(input), s)
}
#[cfg(feature = "v1-fallback")]
{
v2_parser::KdlParser::try_parse(v2_parser::padded_node, s)
parser_v2
.try_parse(KdlParser::padded_node, s)
.or_else(|e| KdlNode::parse_v1(s).map_err(|_| e))
}
}
@ -350,8 +352,8 @@ impl KdlNode {
/// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlNode, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
let parser_v1 = KdlParser::new(KdlVersion::V1);
parser_v1.try_parse(KdlParser::padded_node, s)
}
/// Makes sure this node is in v2 format.
@ -813,7 +815,8 @@ impl FromStr for KdlNode {
type Err = KdlError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
v2_parser::KdlParser::try_parse(v2_parser::padded_node, input)
// TEST: need test
KdlNode::parse(input)
}
}

File diff suppressed because it is too large Load Diff