Functional Pipeline Operator
Relevant source files
The following files were used as context for generating this wiki page:
The Functional Pipeline Operator (|>) in Sploosh is a left-to-right syntactic construct designed to facilitate clear, readable data transformations. It allows developers to chain functions such that the expression on the left is passed as an argument to the function or method on the right. This operator is a core component of the Sploosh design philosophy, which emphasizes explicit control flow and a "one way to do everything" approach to minimize ambiguity for both human developers and AI coding agents.
Sources: VISION.md:86-88, README.md:104-106, VISION.md:46-48
Syntax and Lexical Representation
The pipeline operator is represented by the |> token. During the lexical analysis phase, the lexer identifies this specific sequence and classifies it as TokenKind::PipeGt.
Lexical Components
| Token Representation | Token Kind | Description |
|---|---|---|
| ` | >` | TokenKind::PipeGt |
? | TokenKind::Question | Error propagation operator often used in conjunction with pipelines. |
. | TokenKind::Dot | Used for method chaining within a pipe stage. |
Sources: crates/sploosh-lexer/src/lib.rs:59, crates/sploosh-lexer/src/lib.rs:207, crates/sploosh-parser/src/lib.rs:885-887
Parsing and Precedence
The pipeline operator is parsed as a binary operation with a specific binding power. In the recursive-descent parser, |> has a left-binding power of 8 and a right-binding power of 9, placing it higher than standard arithmetic operators like addition but lower than prefix operators and member access.
Pipe Stage Logic
The right-hand side (RHS) of a |> operator is defined as a pipe_stage. A pipe stage is not a general expression but a specific callee form, which can be:
- A function path (e.g.,
parse). - A function path with generic arguments (e.g.,
parse::<i64>). - A method chain (e.g.,
svc.parse). - An optional set of arguments (e.g.,
transform(arg1)).
The following flowchart illustrates the logical flow of parsing a pipeline expression:
The parser treats the pipe_stage as a callee; if a turbofish is provided without parentheses (e.g., x |> parse::<i64>), it is desugared into a zero-argument call.
Sources: crates/sploosh-parser/src/lib.rs:885-886, crates/sploosh-parser/src/lib.rs:434-458, crates/sploosh-parser/src/lib.rs:604-656
Error Propagation in Pipelines
Sploosh integrates the error propagation operator (?) deeply with the pipeline operator. According to the language specification §5.7, a trailing ? on a pipe stage applies to the accumulated result of the pipeline application up to that point.
Desugaring Logic
| Code Snippet | Internal AST Representation |
|---|---|
x |> f? | (x |> f)? |
a |> f? |> g? | ((a |> f)? |> g)? |
This ensures that errors are handled per stage, allowing a pipeline to stop execution and return a Result<T, E> if any intermediate step fails.
Sources: crates/sploosh-parser/src/lib.rs:446-455, crates/sploosh-parser/src/lib.rs:1095-1108, README.md:38-40
Implementation Constraints
During the current compiler bootstrap phase, certain pipeline features are restricted or not yet implemented:
- Closure Stages: The syntax
x |> (|v| ...)is reserved but not yet implemented because closures currently lack a parse production. - Non-final Turbofish: Using a turbofish on a non-final segment of a pipe stage (e.g.,
x |> svc::<T>.method) is currently rejected. - Callee Requirement: The RHS must be a valid callee; arithmetic or literal values are rejected (e.g.,
x |> 42is a parse error).
Sources: crates/AGENTS.md:30-34, crates/sploosh-parser/src/lib.rs:612-615, crates/sploosh-parser/src/lib.rs:643-646
Conclusion
The pipeline operator is a fundamental tool in Sploosh for constructing readable and robust data processing flows. By combining strict parsing rules for pipe_stages with integrated error propagation, Sploosh provides a deterministic way to express complex transformations that is easily interpreted by both compilers and AI models.