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 support for % operator in case guards #96

Merged
merged 1 commit into from
Sep 1, 2024
Merged
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
3 changes: 2 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ module.exports = grammar({
binaryExpr(prec.left, 6, "*", $._case_clause_guard_expression),
binaryExpr(prec.left, 6, "*.", $._case_clause_guard_expression),
binaryExpr(prec.left, 6, "/", $._case_clause_guard_expression),
binaryExpr(prec.left, 6, "/.", $._case_clause_guard_expression)
binaryExpr(prec.left, 6, "/.", $._case_clause_guard_expression),
binaryExpr(prec.left, 6, "%", $._case_clause_guard_expression)
),
_case_clause_guard_unit: ($) =>
choice(
Expand Down
35 changes: 35 additions & 0 deletions test/corpus/cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,38 @@ case var {
(discard)))
(record
(constructor_name))))))

================================================================================
Case with int remainder in guard
================================================================================

case var {
_ if 11 % 2 == 0 -> True
Copy link
Member

Choose a reason for hiding this comment

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

what's the associativity of % - would it or *// or +/- take precedence for example in something like 11 + 1 % 2?

Copy link
Member

Choose a reason for hiding this comment

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

I believe in other languages like Rust it has the same precedence as multiplication and division so I think it's correct at 6. We can always revisit this later if expressions are sticking together incorrectly

_ -> False
}

--------------------------------------------------------------------------------

(source_file
(case
(case_subjects
(identifier))
(case_clauses
(case_clause
(case_clause_patterns
(case_clause_pattern
(discard)))
(case_clause_guard
(binary_expression
(binary_expression
(integer)
(integer))
(integer)))
(record
(constructor_name)))
(case_clause
(case_clause_patterns
(case_clause_pattern
(discard)))
(record
(constructor_name))))))
11 changes: 11 additions & 0 deletions test/highlight/cases.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn case_with_remainder() {
case todo {
_ if 1 % 2 == 0 -> todo
// ^ number
// ^ operator
// ^ number
// ^ operator
// ^ number
_ -> todo
}
}
Loading