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

Allow 'as' keyword in binary pattern match expressions #71

Merged
merged 2 commits into from
Oct 29, 2023
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
13 changes: 10 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,10 @@ module.exports = grammar({
alias($._pattern_binary_expression, $.binary_expression)
),
_pattern_binary_expression: ($) =>
binaryExpr(prec.left, 1, "<>", $._pattern_expression),
choice(
binaryExpr(prec.left, 1, "<>", $._pattern_expression),
binaryExpr(prec.left, 1, "as", $.string, $.identifier)
),
_pattern: ($) =>
seq(
$._pattern_expression,
Expand Down Expand Up @@ -859,9 +862,13 @@ function series_of(rule, separator) {

// A binary expression with a left-hand side, infix operator, and then right-hand-side
// https://github.com/elixir-lang/tree-sitter-elixir/blob/de20391afe5cb03ef1e8a8e43167e7b58cc52869/grammar.js#L850-L859
function binaryExpr(assoc, precedence, operator, expr) {
function binaryExpr(assoc, precedence, operator, left, right = null) {
return assoc(
precedence,
seq(field("left", expr), field("operator", operator), field("right", expr))
seq(
field("left", left),
field("operator", operator),
field("right", right || left)
)
);
}
12 changes: 6 additions & 6 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@
(identifier) @variable
(discard) @comment.unused

; Operators
(binary_expression
operator: _ @operator)
(boolean_negation "!" @operator)
(integer_negation "-" @operator)

; Keywords
[
(visibility_modifier) ; "pub"
Expand All @@ -93,6 +87,12 @@
"use"
] @keyword

; Operators
(binary_expression
operator: _ @operator)
(boolean_negation "!" @operator)
(integer_negation "-" @operator)

; Punctuation
[
"("
Expand Down
78 changes: 78 additions & 0 deletions test/corpus/cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,81 @@ pub fn listed(names: List(String), person: Person) -> String {
(list_pattern)))
(record
(constructor_name))))))))

================================================================================
Pattern matching binaries with 'as'
================================================================================

// From https://gleam.run/news/v0.31-keeping-dependencies-explicit/#quality-of-life-improvements
case tag {
"category " as key <> value
| "region " as key <> value
| "priority " as key <> value -> {
let key = string.trim(key)
Ok(Tag(key, value))
}
_ -> Error(Nil)
}

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

(source_file
(comment)
(case
(case_subjects
(identifier))
(case_clauses
(case_clause
(case_clause_patterns
(case_clause_pattern
(binary_expression
(binary_expression
(string
(quoted_content))
(identifier))
(identifier)))
(case_clause_pattern
(binary_expression
(binary_expression
(string
(quoted_content))
(identifier))
(identifier)))
(case_clause_pattern
(binary_expression
(binary_expression
(string
(quoted_content))
(identifier))
(identifier))))
(block
(let
(identifier)
(function_call
(field_access
(identifier)
(label))
(arguments
(argument
(identifier)))))
(record
(constructor_name)
(arguments
(argument
(record
(constructor_name)
(arguments
(argument
(identifier))
(argument
(identifier)))))))))
(case_clause
(case_clause_patterns
(case_clause_pattern
(discard)))
(record
(constructor_name)
(arguments
(argument
(record
(constructor_name)))))))))
Loading