Skip to content

Commit

Permalink
making some progress fixing things!
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 2, 2024
1 parent 45bd6b6 commit 4e042c9
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 78 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 = "7.2.0"
nom = "7.1.1"
thiserror = "1.0.40"
winnow = { version = "0.6.5", features = ["alloc", "unstable-recover"] }
winnow = { version = "0.6.20", features = ["alloc", "unstable-recover"] }

[dev-dependencies]
miette = { version = "7.2.0", features = ["fancy"] }
Expand Down
75 changes: 54 additions & 21 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,20 +412,51 @@ second_node /* This time, the comment is here */ param=153 {
Ok(())
}

#[test]
fn basic_parsing() -> miette::Result<()> {
let src = r#"
// Hello, world!
node 1
node two
node item="three";
node {
nested 1 2 3
nested_2 hi "world"
}
(type)node ("type")what?
+false #true
null_id null_prop=#null
foo indented
// normal comment?
/- comment
/* block comment */
inline /*comment*/ here
another /-commend there
after some whitespace
trailing /* multiline */
trailing // single line
"#;
let _doc: KdlDocument = src.parse()?;
// TODO: trailing `//` comments don't work. Multiline works fine?
Ok(())
}

#[test]
fn parsing() -> miette::Result<()> {
let src = "
// This is the first node
foo 1 2 \"three\" null true bar=\"baz\" {
foo 1 2 three #null #true bar=\"baz\" {
- 1
- 2
- \"three\"
(mytype)something (\"name\")\"else\"\r
- three
(mytype)something (\"name\")else\r
}
null_id null_prop=null
true_id true_prop=null
+false true
null_id null_prop=#null
true_id true_prop=#null
+false #true
bar \"indented\" // trailing whitespace after this\t
/*
Expand All @@ -435,7 +466,7 @@ Some random comment
a; b; c;
/-commented \"node\"
another /*foo*/ \"node\" /-1 /*bar*/ null;
another /*foo*/ \"node\" /-1 /*bar*/ #null;
final;";
let mut doc: KdlDocument = src.parse()?;

Expand All @@ -444,13 +475,13 @@ final;";
doc.get_dash_args("foo"),
vec![&1.into(), &2.into(), &"three".into()]
);
assert_eq!(doc.format(), Some(&Default::default()));

let foo = doc.get("foo").expect("expected a foo node");
assert_eq!(
foo.format().map(|f| &f.leading[..]),
doc.format().map(|f| &f.leading[..]),
Some("\n// This is the first node\n")
);

let foo = doc.get("foo").expect("expected a foo node");
assert_eq!(foo.format().map(|f| &f.trailing[..]), Some("\n"));
assert_eq!(&foo[2], &"three".into());
assert_eq!(&foo["bar"], &"baz".into());
assert_eq!(
Expand All @@ -477,19 +508,21 @@ final;";
let a = doc.get("a").expect("expected a node");
assert_eq!(
format!("{}", a),
"/*\nSome random comment\n */\n\na; ".to_string()
"/*\nSome random comment\n */\n\na;".to_string()
);

let b = doc.get("b").expect("expected a node");
assert_eq!(format!("{}", b), "b; ".to_string());
assert_eq!(format!("{}", b), " b;".to_string());

// Round-tripping works.
assert_eq!(format!("{}", doc), src);

// Programmatic manipulation works.
let mut node: KdlNode = "new\n".parse()?;
// Manual entry parsing preserves formatting/reprs.
node.push("\"blah\"=0xDEADbeef".parse::<KdlEntry>()?);
// Manual entry parsing preserves formatting/reprs. Note that
// if you're making KdlEntries this way, you need to inject
// your own whitespace (or format the node)
node.push(" \"blah\"=0xDEADbeef".parse::<KdlEntry>()?);
doc.nodes_mut().push(node);

assert_eq!(
Expand Down Expand Up @@ -667,7 +700,7 @@ foo 1 bar=0xdeadbeef {
this {
is (a)"cool" document="to" read=(int)5 10.1 (u32)0x45
and x="" {
"it" /*shh*/ "has"="💯" r##"the"##
"it" /*shh*/ "has"="💯" ##"the"##
Best🎊est
"syntax ever"
}
Expand All @@ -678,7 +711,7 @@ nice
inline { time; to; live "our" "dreams"; "y;all"; }
"####;

let doc: KdlDocument = input.parse().unwrap();
let doc: KdlDocument = input.parse()?;

// First check that all the identity-spans are correct
check_spans_for_doc(&doc, &input);
Expand Down Expand Up @@ -724,7 +757,7 @@ inline { time; to; live "our" "dreams"; "y;all"; }
// The node is what you expect, the whole line and its two braces
check_span(
r####"and x="" {
"it" /*shh*/ "has"="💯" r##"the"##
"it" /*shh*/ "has"="💯" ##"the"##
Best🎊est
"syntax ever"
}"####,
Expand All @@ -736,7 +769,7 @@ inline { time; to; live "our" "dreams"; "y;all"; }
// with extra newlines on both ends.
check_span(
r####"
"it" /*shh*/ "has"="💯" r##"the"##
"it" /*shh*/ "has"="💯" ##"the"##
Best🎊est
"syntax ever"
"####,
Expand All @@ -750,7 +783,7 @@ inline { time; to; live "our" "dreams"; "y;all"; }
// Now the "it" node, more straightforward
let it_node = and_node.children().unwrap().get("it").unwrap();
check_span(
r####""it" /*shh*/ "has"="💯" r##"the"##"####,
r####""it" /*shh*/ "has"="💯" ##"the"##"####,
it_node.span(),
&input,
);
Expand All @@ -760,7 +793,7 @@ inline { time; to; live "our" "dreams"; "y;all"; }
&input,
);
check_span(
r####"r##"the"##"####,
r####"##"the"##"####,
it_node.entry(0).unwrap().span(),
&input,
);
Expand Down
29 changes: 15 additions & 14 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use std::fmt::Write as _;

pub(crate) fn autoformat_leading(leading: &mut String, indent: usize, _no_comments: bool) {
pub(crate) fn autoformat_leading(leading: &mut String, indent: usize, no_comments: bool) {
if leading.is_empty() {
return;
}
// TODO
let mut result = String::new();
// if !no_comments {
// let input = leading.trim();
// let kdl_parser = crate::v1_parser::KdlParser { full_input: input };
// let comments = kdl_parser
// .parse(crate::v1_parser::leading_comments(&kdl_parser))
// .expect("invalid leading text");
// for line in comments {
// let trimmed = line.trim();
// if !trimmed.is_empty() {
// writeln!(result, "{:indent$}{}", "", trimmed, indent = indent).unwrap();
// }
// }
// }
if !no_comments {
let input = leading.trim();
let (maybe_val, errs) =
crate::v2_parser::try_parse(crate::v2_parser::leading_comments, input);
let (Some(comments), true) = (maybe_val, errs.is_empty()) else {
panic!("invalid leading text");
};
for line in comments {
let trimmed = line.trim();
if !trimmed.is_empty() {
writeln!(result, "{:indent$}{}", "", trimmed, indent = indent).unwrap();
}
}
}
write!(result, "{:indent$}", "", indent = indent).unwrap();
*leading = result;
}
Expand Down
13 changes: 4 additions & 9 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl FromStr for KdlNode {
type Err = KdlParseFailure;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let (maybe_val, errs) = dbg!(v2_parser::try_parse(v2_parser::padded_node, input));
let (maybe_val, errs) = v2_parser::try_parse(v2_parser::padded_node, input);
if let (Some(v), true) = (maybe_val, errs.is_empty()) {
Ok(v)
} else {
Expand Down Expand Up @@ -628,8 +628,6 @@ mod test {
}"#
.parse()?;
let mut right_node: KdlNode = "node param_name=103.0 { nested 1 2 3; }".parse()?;
dbg!(&left_node);
dbg!(&right_node);
assert_ne!(left_node, right_node);
left_node.clear_format_recursive();
right_node.clear_format_recursive();
Expand All @@ -655,16 +653,13 @@ mod test {
// })
// );

let node: KdlNode = r#"node "test" {
let node: KdlNode = r#"node test {
link "blah" anything=self
}
more stuff
"#
.parse::<KdlNode>()?;
dbg!(&node);
assert_eq!(node.entry(0), Some(&" \"test\"".parse()?));
assert_eq!(node.children().unwrap().len(), 1);
assert_eq!(node.entry(0), Some(&" test".parse()?));
assert_eq!(node.children().unwrap().nodes().len(), 1);

Ok(())
}
Expand Down
Loading

0 comments on commit 4e042c9

Please sign in to comment.