Skip to content

Commit

Permalink
add abstract closed families
Browse files Browse the repository at this point in the history
  • Loading branch information
tek committed Apr 19, 2024
1 parent 6fb082e commit cd69808
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
10 changes: 9 additions & 1 deletion grammar/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,19 @@ module.exports = {

_tyfam_equations: $ => layout($, alias($._type_instance, $.equation)),

/**
* This syntax is valid in `.hs-boot` files.
*/
abstract_family: $ => layout_single($, '..'),

decl_tyfam: $ => seq(
'type',
'family',
$._tyfam,
optional_where_as($, $._tyfam_equations, $.equations),
optional_where($, choice(
alias($._tyfam_equations, $.equations),
$.abstract_family,
)),
),

// ------------------------------------------------------------------------
Expand Down
23 changes: 22 additions & 1 deletion grammar/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ semis = ($, rule) => sep1(semi($), rule),
// layout
// ------------------------------------------------------------------------

/**
* More general variant of `layout_sort`.
*/
layout_sort_single = ($, start, rule) => seq(
choice(start, alias($._cmd_layout_start_explicit, '{')),
rule,
$._layout_end,
)

/**
* Wrap a repeated rule in a layout.
* This is used for `where`, `let`, `of`, `if` and `do`, and the toplevel module.
Expand All @@ -47,7 +56,16 @@ layout_sort = ($, start, rule) => seq(
semi_opt($),
)),
$._layout_end,
),
)

/**
* Same as `layout`, but using `layout_sort_single`.
* This is necessary for braces without repeating layout elements.
* Usually it is enough to just use `braces` for this (e.g. records), but if the rule is in a choice with a full
* layout, we need to allow the layout start token since the scanner emits that unconditionally based on preceding
* tokens.
*/
layout_single = ($, rule) => layout_sort_single($, $._cmd_layout_start, rule)

/**
* Alias for `layout_sort` using the common layout type for the start token, which corresponds to declarations and GADT
Expand Down Expand Up @@ -90,7 +108,10 @@ module.exports = {
sep1,
sep2,
semis,
layout_sort,
layout_sort_single,
layout,
layout_single,
unboxed,
unboxed_tuple,
unboxed_sum,
Expand Down
17 changes: 17 additions & 0 deletions test/corpus/family.txt
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,20 @@ type instance ∀ a . A a = a
(type_variable))))
(type_name
(type_variable)))))

================================================================================
family: abstract closed family, hs-boot
================================================================================

type family A where
..

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

(haskell
(declarations
(type_family
(head
(type_name
(type)))
(abstract_family))))

0 comments on commit cd69808

Please sign in to comment.