Skip to content

Commit

Permalink
fix: allow imports after the first declaration for resilience
Browse files Browse the repository at this point in the history
  • Loading branch information
tek committed May 4, 2024
1 parent 50a04bf commit fbd2cd5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
14 changes: 12 additions & 2 deletions grammar/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
namespace: _ => choice('pattern', 'type'),

_child_type: $ => seq(field('namespace', 'type'), field('type', $._tyconids)),

_child: $ => choice(
alias($._child_type, $.associated_type),
$._qname,
Expand Down Expand Up @@ -96,8 +96,18 @@ module.exports = {

/**
* Using `semi` at the end instead of `semi_opt` increases parser size by a full megabyte!!
*
* This allows imports after the first declaration to prevent the tree from jittering while typing an import:
*
* > import A
* > imp
* > import B
*
* The partially typed `imp` will be parsed as a `top_splice`, which forces `imports` to reduce after `import A`.
* The rest of the file will then be part of `declarations` and all following imports will be broken until the keyword
* has been completed.
*/
declarations: $ => seq(semis($, $.declaration), semi_opt($)),
declarations: $ => seq($.declaration, repeat(seq(semi($), choice($.declaration, $.import))), semi_opt($)),

_body: $ => seq(
choice($._cmd_layout_start, alias($._cmd_layout_start_explicit, '{')),
Expand Down
56 changes: 30 additions & 26 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6461,41 +6461,45 @@
"type": "SEQ",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "STRING",
"value": ";"
}
},
{
"type": "SYMBOL",
"name": "_cond_layout_semicolon"
}
]
"type": "REPEAT1",
"content": {
"type": "STRING",
"value": ";"
}
},
{
"type": "SYMBOL",
"name": "_cond_layout_semicolon"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "import"
}
]
}
}
]
]
}
},
{
"type": "CHOICE",
Expand Down
4 changes: 4 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,10 @@
{
"type": "declaration",
"named": true
},
{
"type": "import",
"named": true
}
]
}
Expand Down
22 changes: 22 additions & 0 deletions test/corpus/import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,25 @@ import A (A (type A, A, ..))
(name))
(constructor)
(all_names)))))))

================================================================================
import: partially typed import before the end of the block
================================================================================

import A
impo
import A

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

(haskell
(imports
(import
(module
(module_id))))
(declarations
(top_splice
(variable))
(import
(module
(module_id)))))

0 comments on commit fbd2cd5

Please sign in to comment.