Skip to content

Commit

Permalink
Added support for the experimental pipe operators (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
aftix authored Dec 17, 2024
1 parent b93d6bb commit 9001e02
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT"
name = "rnix"
readme = "README.md"
repository = "https://github.com/nix-community/rnix-parser"
version = "0.11.0"
version = "0.11.1"

[[bench]]
harness = false
Expand Down
5 changes: 5 additions & 0 deletions src/ast/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum BinOpKind {
MoreOrEq,
NotEqual,
Or,
PipeRight,
PipeLeft,
}

impl BinOpKind {
Expand All @@ -43,6 +45,9 @@ impl BinOpKind {
TOKEN_NOT_EQUAL => Some(BinOpKind::NotEqual),
TOKEN_OR_OR => Some(BinOpKind::Or),

TOKEN_PIPE_RIGHT => Some(BinOpKind::PipeRight),
TOKEN_PIPE_LEFT => Some(BinOpKind::PipeLeft),

_ => None,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum SyntaxKind {
TOKEN_MORE_OR_EQ,
TOKEN_NOT_EQUAL,
TOKEN_OR_OR,
TOKEN_PIPE_RIGHT,
TOKEN_PIPE_LEFT,

// Identifiers and values
TOKEN_FLOAT,
Expand Down
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ macro_rules! T {
(>=) => ($crate::SyntaxKind::TOKEN_MORE_OR_EQ);
(!=) => ($crate::SyntaxKind::TOKEN_NOT_EQUAL);
(||) => ($crate::SyntaxKind::TOKEN_OR_OR);
("|>") => ($crate::SyntaxKind::TOKEN_PIPE_RIGHT);
("<|") => ($crate::SyntaxKind::TOKEN_PIPE_LEFT);
($kind:ident) => ($crate::SyntaxKind::$kind);
}
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@ where
// Always point this to the lowest-level math function there is
self.parse_implication()
}
fn parse_pipe_right(&mut self) -> Checkpoint {
self.parse_left_assoc(Self::parse_math, T!["|>"] | ())
}
fn parse_pipe_left(&mut self) -> Checkpoint {
self.parse_right_assoc(Self::parse_pipe_right, T!["<|"] | ())
}
/// Parse Nix code into an AST
pub fn parse_expr(&mut self) -> Checkpoint {
// Limit chosen somewhat arbitrarily
Expand Down Expand Up @@ -758,7 +764,7 @@ where
self.finish_node();
checkpoint
}
_ => self.parse_math(),
_ => self.parse_pipe_left(),
};
self.depth -= 1;
out
Expand Down
8 changes: 8 additions & 0 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ impl Tokenizer<'_> {
'-' => TOKEN_SUB,
'*' => TOKEN_MUL,
'/' => TOKEN_DIV,
'<' if self.peek() == Some('|') => {
self.next().unwrap();
TOKEN_PIPE_LEFT
}
'<' if kind == Some(IdentType::Store) => {
self.consume(is_valid_path_char);
if self.next() != Some('>') {
Expand All @@ -376,6 +380,10 @@ impl Tokenizer<'_> {
self.next().unwrap();
TOKEN_AND_AND
}
'|' if self.peek() == Some('>') => {
self.next().unwrap();
TOKEN_PIPE_RIGHT
}
'|' if self.peek() == Some('|') => {
self.next().unwrap();
TOKEN_OR_OR
Expand Down
15 changes: 15 additions & 0 deletions test_data/parser/success/pipe_left.expect
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_left.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.toString <| 1
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_left_assoc.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo <| builtins.toString <| 1
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_left_math.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.toString <| 1 + 1
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_mixed.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.toString <| 1 |> foo
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_mixed_math.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.toString <| 1 + 1 |> foo
15 changes: 15 additions & 0 deletions test_data/parser/success/pipe_right.expect
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_right.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 |> builtins.toString
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_right_assoc.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 |> builtins.toString |> foo
1 change: 1 addition & 0 deletions test_data/parser/success/pipe_right_math.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 + 1 |> builtins.toString

0 comments on commit 9001e02

Please sign in to comment.