Skip to content

Node pipes (experimental) - #15

Open
tachuris wants to merge 1 commit into
bgotink:mainfrom
tachuris:node-pipes
Open

tachuris wants to merge 1 commit into
bgotink:mainfrom
tachuris:node-pipes

Conversation

@tachuris

@tachuris tachuris commented Oct 21, 2026 •

Copy link
Copy Markdown

Its homepage introduces KDL as a document language that “looks like you're invoking a bunch of CLI commands.” Piping follows naturally from that pitch: if nodes look like commands, pipes are the missing piece for expressing composition in a familiar, fluent way.

Node pipes let you write deeply nested single-child structures as a left-to-right chain.

image

This can be enabled with the experimentalNodePipes parser flag. Documents using it are not valid KDL for other parsers.

Usage:

import {parse} from "@bgotink/kdl";

const document = parse(
	'task fetch url="https://example.com" #> timeout 5 #> retry times=3',
	{flags: {experimentalNodePipes: true}},
);

Semantics

A #> B is equivalent to B { A }: the node on the left becomes the sole child of the node on the right.
Chains are left-associative: A #> B #> C is equivalent to C { B { A } }.

The left-hand side of #> is always a single node, which may have children of its own:

image

The right-hand side of #> cannot have a children block. text "Hello" #> padding 8 { border } is invalid. Use the nested form instead.

The parsed document is identical to the nested form: queries, (de)serialization, and all other APIs see regular nested nodes.

The #> token

#> is a pipe wherever it appears between nodes, with or without surrounding whitespace: a #> b, a#>b, and a #>b are all equivalent.

Parsers without this extension reject documents that use node pipes.
Enabling the flag doesn't change the meaning of any valid KDL document, and the rules about when strings must be quoted are unchanged.

When the experimentalSuffixedNumbers flag is also enabled, #> immediately after a number is parsed as a number suffix.
In loop 4#> ease, 4 has the suffix tag >.
Add whitespace before the pipe in that case: loop 4 #> ease.

Other candidate tokens considered:

Candidate Today, without the flag Issue as a pipe token
|> -> >> :> ~> valid, but parses as an extra argument a conformant parser reads it as an argument instead of failing
=> valid, but parses as property a=">" changes the meaning of existing documents, needs a whitespace rule and an extra quoting rule
/> error: unexpected > looks like the comment starters //, /* and /-
\> error: expected newline after backslash confusing next to \, which continues a line in KDL
]> error: [ and ] are reserved the spec keeps [ and ] for its own future use

Only tokens that currently produce an error are safe: parsers without the feature reject documents that use the token.
#> won over those because:

  • # can't appear in identifiers, so it needs no whitespace rule and no change to the quoting rules.
  • /-#> works with no special case.
  • # already marks special syntax in KDL v2, as in #true, #null and #"raw"#.

Multiline chains

Chains can span multiple lines, with #> either ending a line or starting the next one:

image

Blank lines and comments are allowed between stages.
A line break never changes the meaning of a chain: a leading #> always continues the node right before it.
A semicolon ends the chain: a; #> b is an error, as is a #> at the start of a document or children block.

Slashdash

A slashdash before the first node of a chain comments out the entire chain:

image

A slashdash before a #> comments out that single stage:

image

Formatting

Parsing a pipe chain retains the pipe formatting, so format(parse(text)) writes the chain back as it was written.

The pipe form is only preserved while the node still has exactly one child, and that child is the same node object it was parsed with.
Editing that child in place (e.g. adding an argument) keeps the pipe form.
Removing the child, adding a second child, or replacing it with another node makes the formatter write the nested form instead.

There's no API to create pipe chains in code: pipe formatting only ever comes from parsing.
Both clearFormat() and formatCompact() drop pipe formatting and yield the nested form, which is valid KDL for any parser.

Locations

When storeLocations is enabled, the location of each node in a chain covers only that node's own text.
For text "Hello" #> padding 8, the padding node's location spans padding 8, and the text node's location spans text "Hello".

Whitespace

Slashdashed stages are retained as part of the surrounding whitespace.
Parsing that whitespace with parse(text, {as: "whitespace in document"}) yields a part {type: "slashdash", pipe: true, preface, value} where value is the commented-out node.

Limitations

Only supported by the KDL v2 parser.

@tachuris

Copy link
Copy Markdown
Author

You can try a VS Code extended grammar and language server that supports #> locally by cloning the node-pipes branch of my kdl-rs fork:

git clone -b node-pipes --single-branch --depth 1 git@github.com:tachuris/kdl-rs.git

Further instructions here:
https://github.com/tachuris/kdl-rs/blob/node-pipes/editors/vscode-kdl-pipe/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant