@@ -228,12 +228,12 @@ pub(crate) fn document(input: &mut Input<'_>) -> PResult<KdlDocument> {
228228/// `nodes := (line-space* node)* line-space*`
229229fn 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?
260260fn 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`
408418fn 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,14 +528,51 @@ 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+ ) . take ( ) ,
540+ ( node_space1, slashdashed_entries) . take ( ) ,
541+ ( node_space1, slashdashed_children) . take ( ) ,
542+ node_space0. take ( ) ,
543+ ) )
544+ . void ( )
545+ . parse_next ( input) ?;
546+ node_space0. parse_next ( input) ?;
547+ Ok ( ( ) )
529548}
530549
550+ #[ cfg( test) ]
551+ #[ test]
552+ fn before_node_children_test ( ) {
553+ assert ! ( before_node_children. parse( new_input( " /- { }" ) ) . is_ok( ) ) ;
554+ assert ! ( before_node_children. parse( new_input( " /- { bar }" ) ) . is_ok( ) ) ;
555+ }
556+
557+ fn slashdashed_children ( input : & mut Input < ' _ > ) -> PResult < ( ) > {
558+ node_space0. parse_next ( input) ?;
559+ separated (
560+ 1 ..,
561+ ( slashdash. void ( ) , node_children. void ( ) ) . void ( ) ,
562+ node_space1,
563+ )
564+ . map ( |( ) | ( ) )
565+ . parse_next ( input)
566+ }
567+
568+ #[ cfg( test) ]
569+ #[ test]
570+ fn around_children_test ( ) {
571+ assert ! ( slashdashed_children. parse( new_input( "/- { }" ) ) . is_ok( ) ) ;
572+ assert ! ( slashdashed_children. parse( new_input( "/- { bar }" ) ) . is_ok( ) ) ;
573+ }
574+
575+
531576/// `node-children := '{' nodes final-node? '}'`
532577fn node_children ( input : & mut Input < ' _ > ) -> PResult < KdlDocument > {
533578 delimited ( "{" , nodes, cut_err ( "}" ) ) . parse_next ( input)
@@ -1263,8 +1308,20 @@ fn slashdash(input: &mut Input<'_>) -> PResult<()> {
12631308#[ cfg( test) ]
12641309#[ test]
12651310fn slashdash_tests ( ) {
1266- assert ! ( node. parse( new_input( "/- foo bar\n node" ) ) . is_ok( ) ) ;
12671311 assert ! ( document. parse( new_input( "/- foo bar" ) ) . is_ok( ) ) ;
1312+ assert ! ( node. parse( new_input( "/- foo\n bar 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\n node /-1 2 { x }" ) )
1318+ . is_ok( ) ) ;
1319+ assert ! ( node
1320+ . parse( new_input( "/- foo bar\n node 2 /-3 { x }" ) )
1321+ . is_ok( ) ) ;
1322+ assert ! ( node
1323+ . parse( new_input( "/- foo bar\n node /-1 2 /-3 { x }" ) )
1324+ . is_ok( ) ) ;
12681325}
12691326
12701327/// `number := keyword-number | hex | octal | binary | decimal`
0 commit comments