Skip to content

Commit

Permalink
Add a test case for record access syntax within guards
Browse files Browse the repository at this point in the history
This improvement was mentioned in the v0.31.0 release notes. This is
already supported so all I've done is remove the comment about how the
behavior does not yet exist upstream and add a test case.
  • Loading branch information
the-mikedavis committed Sep 26, 2023
1 parent 297031d commit 32c8f1e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
6 changes: 2 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ module.exports = grammar({
optional(seq(field("label", $.label), ":")),
field("value", $._constant_value)
),
// This is definitely a misnomer at this time as field access are actually
// not allowed as constant values (yet). This rule exists to parse remote
// function references which are generally indistinguishable from field
// accesses and so share an AST node.
// This rule exists to parse remote function references which are generally
// indistinguishable from field accesses and so share an AST node.
constant_field_access: ($) =>
seq(field("record", $.identifier), ".", field("field", $.label)),

Expand Down
76 changes: 76 additions & 0 deletions test/corpus/cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,79 @@ case value {}
(case
(case_subjects
(identifier))))

================================================================================
Case examples
================================================================================

// From https://gleam.run/news/v0.31-keeping-dependencies-explicit/#quality-of-life-improvements
pub fn listed(names: List(String), person: Person) -> String {
case names {
[name, ..names] if name == person.name -> True
[_, ..names] -> listed(names, person)
[] -> False
}
}

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

(source_file
(comment)
(function
(visibility_modifier)
(identifier)
(function_parameters
(function_parameter
(identifier)
(type
(type_identifier)
(type_arguments
(type_argument
(type
(type_identifier))))))
(function_parameter
(identifier)
(type
(type_identifier))))
(type
(type_identifier))
(function_body
(case
(case_subjects
(identifier))
(case_clauses
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern
(identifier)
(list_pattern_tail
(identifier)))))
(case_clause_guard
(binary_expression
(identifier)
(field_access
(identifier)
(label))))
(record
(constructor_name)))
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern
(discard)
(list_pattern_tail
(identifier)))))
(function_call
(identifier)
(arguments
(argument
(identifier))
(argument
(identifier)))))
(case_clause
(case_clause_patterns
(case_clause_pattern
(list_pattern)))
(record
(constructor_name))))))))

0 comments on commit 32c8f1e

Please sign in to comment.