Skip to content

Commit 46d0ca5

Browse files
committed
fix compliance tests for new slashdash impl
1 parent ae61060 commit 46d0ca5

File tree

4 files changed

+98
-44
lines changed

4 files changed

+98
-44
lines changed

src/document.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ second_node /* This time, the comment is here */ param=153 {
436436
/- comment
437437
/* block comment */
438438
inline /*comment*/ here
439-
another /-commend there
439+
another /-comment there
440440
441441
442442
after some whitespace

src/v2_parser.rs

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ pub(crate) fn document(input: &mut Input<'_>) -> PResult<KdlDocument> {
228228
/// `nodes := (line-space* node)* line-space*`
229229
fn nodes(input: &mut Input<'_>) -> PResult<KdlDocument> {
230230
let (leading, (nodes, _span), _final_terminator, trailing) = (
231-
repeat(0.., alt((line_space.void(), (slashdash, node).void())))
231+
repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
232232
.map(|()| ())
233233
.take(),
234234
separated(0.., node, node_terminator).with_span(),
235235
opt(node_terminator),
236-
repeat(0.., alt((line_space.void(), (slashdash, node).void())))
236+
repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
237237
.map(|()| ())
238238
.take(),
239239
)
@@ -258,10 +258,18 @@ fn nodes(input: &mut Input<'_>) -> PResult<KdlDocument> {
258258
/// node := base-node node-space* node-terminator
259259
/// final-node := base-node node-space* node-terminator?
260260
fn node(input: &mut Input<'_>) -> PResult<KdlNode> {
261-
let leading = repeat(0.., alt((line_space.void(), (slashdash, node).void())))
261+
let leading = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
262262
.map(|()| ())
263263
.take()
264264
.parse_next(input)?;
265+
let mut nd = base_node.parse_next(input)?;
266+
if let Some(fmt) = nd.format_mut() {
267+
fmt.leading = leading.into();
268+
}
269+
Ok(nd)
270+
}
271+
272+
fn base_node(input: &mut Input<'_>) -> PResult<KdlNode> {
265273
let ((ty, after_ty, name, entries, children), _span) = (
266274
opt(ty),
267275
node_space0.take(),
@@ -271,38 +279,40 @@ fn node(input: &mut Input<'_>) -> PResult<KdlNode> {
271279
(peek(node_space1), node_entry).map(|(_, e): ((), _)| e),
272280
)
273281
.map(|e: Vec<Option<KdlEntry>>| e.into_iter().flatten().collect::<Vec<KdlEntry>>()),
274-
opt((node_entry_leading.take(), around_node_children, node_children)),
282+
opt((before_node_children.take(), node_children)),
275283
)
276284
.with_span()
277285
.parse_next(input)?;
278286
let (before_terminator, terminator) = if children.is_some() {
279-
(around_node_children.take(), peek(opt(node_terminator).take())).parse_next(input)?
287+
(
288+
opt(slashdashed_children).take(),
289+
peek(opt(node_terminator).take()),
290+
)
291+
.parse_next(input)?
280292
} else {
281-
((node_entry_leading, around_node_children).take(), peek(opt(node_terminator).take())).parse_next(input)?
293+
(
294+
before_node_children.take(),
295+
peek(opt(node_terminator).take()),
296+
)
297+
.parse_next(input)?
282298
};
283299
let (before_inner_ty, ty, after_inner_ty) = ty.unwrap_or_default();
284300
let (before_children, children) = children
285-
.map(|(node_entry_trailing, before_children, children)| {
286-
(
287-
format!("{node_entry_trailing}{before_children}"),
288-
Some(children),
289-
)
290-
})
301+
.map(|(before_children, children)| (before_children.into(), Some(children)))
291302
.unwrap_or(("".into(), None));
292303
Ok(KdlNode {
293304
ty,
294305
name,
295306
entries,
296307
children,
297308
format: Some(KdlNodeFormat {
298-
leading: leading.into(),
299309
before_ty_name: before_inner_ty.into(),
300310
after_ty_name: after_inner_ty.into(),
301311
after_ty: after_ty.into(),
302312
before_children,
303313
before_terminator: before_terminator.into(),
304314
terminator: terminator.into(),
305-
trailing: "".into(),
315+
..Default::default()
306316
}),
307317
#[cfg(feature = "span")]
308318
span: _span.into(),
@@ -406,7 +416,11 @@ pub(crate) fn padded_node_entry(input: &mut Input<'_>) -> PResult<KdlEntry> {
406416

407417
/// `node-prop-or-arg := prop | value`
408418
fn node_entry(input: &mut Input<'_>) -> PResult<Option<KdlEntry>> {
409-
let (leading, mut entry) = (node_entry_leading.take(), alt((prop, value))).parse_next(input)?;
419+
let (leading, mut entry) = (
420+
(node_space0, opt((slashdashed_entries, node_space1))).take(),
421+
alt((prop, value)),
422+
)
423+
.parse_next(input)?;
410424
entry = entry.map(|mut e| {
411425
if let Some(fmt) = e.format_mut() {
412426
fmt.leading = leading.into();
@@ -416,18 +430,12 @@ fn node_entry(input: &mut Input<'_>) -> PResult<Option<KdlEntry>> {
416430
Ok(entry)
417431
}
418432

419-
fn node_entry_leading(input: &mut Input<'_>) -> PResult<()> {
420-
node_space0.parse_next(input)?;
421-
separated(
422-
0..,
423-
(slashdash, node_entry),
424-
node_space1,
425-
)
426-
.map(|()| ())
427-
.take()
428-
.map(|x| x.to_string())
429-
.parse_next(input)?;
430-
node_space0.parse_next(input)?;
433+
fn slashdashed_entries(input: &mut Input<'_>) -> PResult<()> {
434+
separated(1.., (slashdash, node_entry), node_space1)
435+
.map(|()| ())
436+
.take()
437+
.map(|x| x.to_string())
438+
.parse_next(input)?;
431439
Ok(())
432440
}
433441

@@ -520,12 +528,49 @@ fn entry_test() {
520528
);
521529
}
522530

523-
fn around_node_children(input: &mut Input<'_>) -> PResult<String> {
524-
repeat(0.., (node_space1, opt((slashdash, node_children))))
525-
.map(|()| ())
526-
.take()
527-
.map(|x| x.to_string())
528-
.parse_next(input)
531+
fn before_node_children(input: &mut Input<'_>) -> PResult<()> {
532+
alt((
533+
(
534+
node_space1,
535+
slashdashed_entries,
536+
// This second one will fail if `node_entry_leading` is empty.
537+
node_space1,
538+
slashdashed_children,
539+
)
540+
.take(),
541+
(node_space1, slashdashed_entries).take(),
542+
(node_space1, slashdashed_children).take(),
543+
node_space0.take(),
544+
))
545+
.void()
546+
.parse_next(input)?;
547+
node_space0.parse_next(input)?;
548+
Ok(())
549+
}
550+
551+
#[cfg(test)]
552+
#[test]
553+
fn before_node_children_test() {
554+
assert!(before_node_children.parse(new_input(" /- { }")).is_ok());
555+
assert!(before_node_children.parse(new_input(" /- { bar }")).is_ok());
556+
}
557+
558+
fn slashdashed_children(input: &mut Input<'_>) -> PResult<()> {
559+
node_space0.parse_next(input)?;
560+
separated(
561+
1..,
562+
(slashdash.void(), node_children.void()).void(),
563+
node_space1,
564+
)
565+
.map(|()| ())
566+
.parse_next(input)
567+
}
568+
569+
#[cfg(test)]
570+
#[test]
571+
fn around_children_test() {
572+
assert!(slashdashed_children.parse(new_input("/- { }")).is_ok());
573+
assert!(slashdashed_children.parse(new_input("/- { bar }")).is_ok());
529574
}
530575

531576
/// `node-children := '{' nodes final-node? '}'`
@@ -1263,8 +1308,20 @@ fn slashdash(input: &mut Input<'_>) -> PResult<()> {
12631308
#[cfg(test)]
12641309
#[test]
12651310
fn slashdash_tests() {
1266-
assert!(node.parse(new_input("/- foo bar\nnode")).is_ok());
12671311
assert!(document.parse(new_input("/- foo bar")).is_ok());
1312+
assert!(node.parse(new_input("/- foo\nbar baz")).is_ok());
1313+
assert!(node_entry.parse(new_input("/-commented tada")).is_ok());
1314+
assert!(node.parse(new_input("foo /- { }")).is_ok());
1315+
assert!(node.parse(new_input("foo /- { bar }")).is_ok());
1316+
assert!(node
1317+
.parse(new_input("/- foo bar\nnode /-1 2 { x }"))
1318+
.is_ok());
1319+
assert!(node
1320+
.parse(new_input("/- foo bar\nnode 2 /-3 { x }"))
1321+
.is_ok());
1322+
assert!(node
1323+
.parse(new_input("/- foo bar\nnode /-1 2 /-3 { x }"))
1324+
.is_ok());
12681325
}
12691326

12701327
/// `number := keyword-number | hex | octal | binary | decimal`
@@ -1513,11 +1570,7 @@ fn test_binary() {
15131570
fn signum(input: &mut Input<'_>) -> PResult<bool> {
15141571
let sign = opt(alt(('+', '-'))).parse_next(input)?;
15151572
let mult = if let Some(sign) = sign {
1516-
if sign == '+' {
1517-
true
1518-
} else {
1519-
false
1520-
}
1573+
sign == '+'
15211574
} else {
15221575
true
15231576
};

tests/compliance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn spec_compliance() -> miette::Result<()> {
7676
fn validate_res(
7777
res: Result<KdlDocument, KdlParseFailure>,
7878
path: &Path,
79-
src: &String,
79+
src: &str,
8080
) -> Result<(), ComplianceDiagnostic> {
8181
let file_name = path.file_name().unwrap();
8282
let expected_dir = path
@@ -94,9 +94,9 @@ fn validate_res(
9494
if actual != expected {
9595
return Err(ComplianceDiagnostic::ExpectationMismatch {
9696
file: path.into(),
97-
original: src.clone(),
98-
expected,
99-
actual,
97+
original: src.into(),
98+
expected: expected.replace('\n', "\\n").replace(" ", "."),
99+
actual: actual.replace('\n', "\\n").replace(" ", "."),
100100
});
101101
}
102102
} else if underscored.exists() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)