Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pipe operator #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ide/src/ide/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub(crate) fn highlight(
HlTag::Operator(HlOperator::Comparison)
}
T![+] | T![-] | T![*] | T![/] => HlTag::Operator(HlOperator::Arithmetic),
T![++] | T!["//"] => HlTag::Operator(HlOperator::Aggregation),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a collection operator. It may need another HlOperator variant like Pipe.

T![++] | T![|>] | T!["//"] => HlTag::Operator(HlOperator::Aggregation),
T!['{'] | T!['}'] | T!["${"] => HlTag::Punct(HlPunct::Brace),
T!['['] | T![']'] => HlTag::Punct(HlPunct::Bracket),
T!['('] | T![')'] => HlTag::Punct(HlPunct::Paren),
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/ty/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'db> InferCtx<'db> {
self.unify_var(lhs_ty, rhs_ty);
lhs_ty
}
BinaryOpKind::Concat => {
BinaryOpKind::Concat | BinaryOpKind::Pipe => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't unity arguments of pipe operators to be List.
They should follow the handling of Expr::Apply with two arguments flipped.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't quite follow this, need an example... speaking of the other threads, i can make the changes and push them right away
that's quite dumb, but i have basically zero experience in parsers 💀
this PR (plus patches in tree-sitter-nix, rnix-parser and alejandra) emerged from my need of using pipe operator w/o errors from the language server and with working formatter. then i showed all of this to friends and they kinda expected me to make PRs to the base repos 💀 💀 💀
btw i also opened a PR in tree-sitter-nix, but considering the status quo of rnix-parser and it being a dependency of alejandra, i am totally unsure how to work this out 😭
sorry for this silly venting in this thread

let ret_ty = Ty::List(self.new_ty_var()).intern(self);
self.unify_var(lhs_ty, ret_ty);
self.unify_var(rhs_ty, ret_ty);
Expand Down
3 changes: 3 additions & 0 deletions crates/syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum BinaryOpKind {
Imply,
Or,
And,
Pipe,

Equal,
NotEqual,
Expand Down Expand Up @@ -276,6 +277,7 @@ impl Expr {
BinaryOpKind::Imply => 1,
BinaryOpKind::Or => 3,
BinaryOpKind::And => 5,
BinaryOpKind::Pipe => 6,
BinaryOpKind::Equal | BinaryOpKind::NotEqual => 7,
BinaryOpKind::Less
| BinaryOpKind::Greater
Expand Down Expand Up @@ -382,6 +384,7 @@ asts! {
let op = match tok.kind() {
T![->] => BinaryOpKind::Imply,
T![&&] => BinaryOpKind::And,
T![|>] => BinaryOpKind::Pipe,
T![||] => BinaryOpKind::Or,
T![==] => BinaryOpKind::Equal,
T![!=] => BinaryOpKind::NotEqual,
Expand Down
1 change: 1 addition & 0 deletions crates/syntax/src/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def! {
LT_EQ = [<=],
MINUS_GT = [->],
NOT_EQ = [!=],
PIPE = [|>],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PIPE = [|>],
OR_GT = [|>],

Here we name symbols syntactically, not semantically.

OR2 = [||],
PLUS2 = [++],
QUOTE2 = ["''"],
Expand Down
1 change: 1 addition & 0 deletions crates/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ regex_dfa! {

DOT3 = r"\.\.\.",
MINUS_GT = r"->",
PIPE = r"\|>",
OR2 = r"\|\|",
AND2 = r"&&",
EQ2 = r"==",
Expand Down
1 change: 1 addition & 0 deletions crates/syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ impl SyntaxKind {
T![*] |
T![/] => (17, 18),
T![++] => (20, 19),
T![|>] => (21, 22),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This precedence is incorrect. According to Nix, it should have a lower precedence than logic-imply ->. So it should goes above to be (-1, 0), which underflows u8... Unfortunately, we need to shift all other BP values up by +10 (for simplicity and more future proof), so |> becomes (9, 10), -> becomes (12, 11), and etc.

// Postfix `?` => 21
// Prefix `-` => 23
_ if self.can_start_atom_expr() => (25, 26), // APPLY
Expand Down
Loading