remove the space node in unboxed sums

pull/315/head
Torsten Schmits 2022-05-07 17:00:17 +07:00
parent 670fdac229
commit d602ceb36e
11 changed files with 356752 additions and 355663 deletions

@ -206,7 +206,7 @@ module.exports = grammar({
/**
* `(# | | ...` can start both `pat` and `exp`.
*/
[$.pat_sum_empty, $.exp_sum_empty],
[$._pat_unboxed_sum, $._exp_unboxed_sum],
/**
* The nullary unboxed tuple `(# #)` is indistinguishable between exp and pat.

@ -24,13 +24,6 @@ module.exports = {
repeat(seq($.comma, optional($._exp)))
),
exp_sum_empty: _ => ' ',
/**
* Unboxed sums must have at least one separating `|`, otherwise the expression would be a unary or nullary tuple.
*/
_exp_sum: $ => sep2('|', choice($._exp, $.exp_sum_empty)),
exp_tuple: $ => parens($._exp_tuple),
/**
@ -38,10 +31,20 @@ module.exports = {
* The nullary tuple may even have no space between the hashes, but this format coincides with the prefix notation of
* the `##` symop. Since the latter is already parsed by other rules and is valid in the same positions, it is left out
* here.
*
* The opening lexeme, `(#`, is parsed with a hardcoded trailing space in exp, pat and type. This is a hack that works
* around some peculiarities of the interactions with some features like TH and symbolic operators that would most
* likely be significantly more complex to implement correctly. As it stands, the grammar can't parse an unboxed sum
* exp without a leading space, as in `(#| x #)`.
*/
exp_unboxed_tuple: $ => seq('(# ', sep($.comma, optional($._exp)), $._unboxed_close),
exp_unboxed_sum: $ => seq('(# ', $._exp_sum, $._unboxed_close),
/**
* Unboxed sums must have at least one separating `|`, otherwise the expression would be a unary or nullary tuple.
*/
_exp_unboxed_sum: $ => sep2('|', optional($._exp)),
exp_unboxed_sum: $ => seq('(# ', $._exp_unboxed_sum, $._unboxed_close),
exp_list: $ => brackets(sep1($.comma, $._exp)),

@ -18,11 +18,9 @@ module.exports = {
pat_unboxed_tuple: $ => seq('(# ', sep($.comma, $._nested_pat), $._unboxed_close),
pat_sum_empty: _ => ' ',
_pat_unboxed_sum: $ => sep2('|', optional($._nested_pat)),
_pat_sum: $ => sep2('|', choice($._nested_pat, $.pat_sum_empty)),
pat_unboxed_sum: $ => seq('(# ', $._pat_sum, $._unboxed_close),
pat_unboxed_sum: $ => seq('(# ', $._pat_unboxed_sum, $._unboxed_close),
pat_list: $ => brackets(sep1($.comma, $._nested_pat)),

@ -1,3 +1,2 @@
examples/flatparse/src/FlatParse/Internal.hs
examples/flatparse/src/FlatParse/Stateful.hs
examples/flatparse/src/FlatParse/Basic.hs

112
src/grammar.json vendored

@ -2935,53 +2935,6 @@
}
]
},
"exp_sum_empty": {
"type": "STRING",
"value": " "
},
"_exp_sum": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_exp"
},
{
"type": "SYMBOL",
"name": "exp_sum_empty"
}
]
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "|"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_exp"
},
{
"type": "SYMBOL",
"name": "exp_sum_empty"
}
]
}
]
}
}
]
},
"exp_tuple": {
"type": "SEQ",
"members": [
@ -3061,6 +3014,47 @@
}
]
},
"_exp_unboxed_sum": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_exp"
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "|"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_exp"
},
{
"type": "BLANK"
}
]
}
]
}
}
]
},
"exp_unboxed_sum": {
"type": "SEQ",
"members": [
@ -3070,7 +3064,7 @@
},
{
"type": "SYMBOL",
"name": "_exp_sum"
"name": "_exp_unboxed_sum"
},
{
"type": "SYMBOL",
@ -5078,11 +5072,7 @@
}
]
},
"pat_sum_empty": {
"type": "STRING",
"value": " "
},
"_pat_sum": {
"_pat_unboxed_sum": {
"type": "SEQ",
"members": [
{
@ -5093,8 +5083,7 @@
"name": "_nested_pat"
},
{
"type": "SYMBOL",
"name": "pat_sum_empty"
"type": "BLANK"
}
]
},
@ -5115,8 +5104,7 @@
"name": "_nested_pat"
},
{
"type": "SYMBOL",
"name": "pat_sum_empty"
"type": "BLANK"
}
]
}
@ -5134,7 +5122,7 @@
},
{
"type": "SYMBOL",
"name": "_pat_sum"
"name": "_pat_unboxed_sum"
},
{
"type": "SYMBOL",
@ -9223,8 +9211,12 @@
"_context_constraints"
],
[
"pat_sum_empty",
"exp_sum_empty"
"_pat_unboxed_sum",
"_exp_unboxed_sum"
],
[
"exp_unboxed_tuple",
"pat_unboxed_tuple"
],
[
"exp_lambda_case"

@ -6209,11 +6209,6 @@
]
}
},
{
"type": "exp_sum_empty",
"named": true,
"fields": {}
},
{
"type": "exp_th_quoted_name",
"named": true,
@ -6631,7 +6626,7 @@
},
"children": {
"multiple": true,
"required": true,
"required": false,
"types": [
{
"type": "exp_apply",
@ -6709,10 +6704,6 @@
"type": "exp_section_right",
"named": true
},
{
"type": "exp_sum_empty",
"named": true
},
{
"type": "exp_th_quoted_name",
"named": true
@ -10475,11 +10466,6 @@
]
}
},
{
"type": "pat_sum_empty",
"named": true,
"fields": {}
},
{
"type": "pat_tuple",
"named": true,
@ -10731,7 +10717,7 @@
"fields": {},
"children": {
"multiple": true,
"required": true,
"required": false,
"types": [
{
"type": "pat_apply",
@ -10777,10 +10763,6 @@
"type": "pat_strict",
"named": true
},
{
"type": "pat_sum_empty",
"named": true
},
{
"type": "pat_tuple",
"named": true
@ -13999,10 +13981,6 @@
]
}
},
{
"type": " ",
"named": false
},
{
"type": "%",
"named": false

712197
src/parser.c vendored

File diff suppressed because it is too large Load Diff

2
src/scanner.c vendored

@ -365,7 +365,7 @@ static bool isws(uint32_t c) {
}
/**
* A token like a varsym can be terminated by whitespace of brackets.
* A token like a varsym can be terminated by whitespace or brackets.
*/
static bool token_end(uint32_t c) {
switch (c) {

@ -1191,6 +1191,7 @@ exp: unboxed sum
a :: (# A | (# A, A #) | A #)
a = (# a | | #)
a = (# | a #)
---
@ -1204,9 +1205,30 @@ a = (# a | | #)
(function
(variable)
(exp_unboxed_sum
(exp_name (variable))
(exp_sum_empty)
(exp_sum_empty))))
(exp_name (variable))))
(function
(variable)
(exp_unboxed_sum
(exp_name (variable)))))
================================================================================
exp: error: unboxed sum without space
================================================================================
a = (#| 5 #)
---
(haskell
(function
(variable)
(exp_section_right
(operator)
(exp_infix
(exp_literal (integer))
(operator)
(exp_name
(variable (MISSING _varid)))))))
================================================================================
exp: label

@ -363,12 +363,8 @@ a (# (# #) | | #) = a
(function
(variable)
(patterns
(pat_unboxed_sum
(pat_unboxed_tuple)
(pat_sum_empty)
(pat_sum_empty)))
(exp_name
(variable))))
(pat_unboxed_sum (pat_unboxed_tuple)))
(exp_name (variable))))
================================================================================
pat: signature

@ -101,6 +101,20 @@ signature: parenthesized operator fun
(haskell (function (operator) (exp_name (variable))))
================================================================================
signature: operator starting with hash
================================================================================
(#!) :: a
---
(haskell
(signature
(operator)
(type_name (type_variable))))
================================================================================
signature: HKT annotation
================================================================================