From 86b5fe322ce8dbcb3da1be1cdd6c9f65ff3e405a Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 15 Apr 2023 21:48:45 -0300 Subject: [PATCH] include_statement: better support --- grammar.js | 25 ++++++++++++++++--------- test/corpus/modules.txt | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/grammar.js b/grammar.js index c1ed37e..a7c176d 100644 --- a/grammar.js +++ b/grammar.js @@ -113,7 +113,8 @@ module.exports = grammar({ [$.polyvar_type], [$.let_binding, $.or_pattern], [$.exception_pattern, $.or_pattern], - [$.type_binding, $._inline_type] + [$.type_binding, $._inline_type], + [$._module_structure, $.parenthesized_module_expression] ], rules: { @@ -176,7 +177,10 @@ module.exports = grammar({ include_statement: $ => seq( 'include', - $.module_expression, + choice( + $._module_definition, + parenthesize($._module_structure) + ) ), declaration: $ => choice( @@ -206,6 +210,11 @@ module.exports = grammar({ sep1('and', $.module_binding) ), + _module_structure: $ => seq( + $._module_definition, + optional($.module_type_annotation), + ), + _module_definition: $ => choice( $.block, $.module_expression, @@ -757,13 +766,7 @@ module.exports = grammar({ module_pack: $ => seq( 'module', - '(', - choice( - $.type_identifier_path, - $._module_definition, - ), - optional($.module_type_annotation), - ')' + parenthesize(choice($._module_structure, $.type_identifier_path)) ), call_arguments: $ => seq( @@ -1537,3 +1540,7 @@ function sep1(delimiter, rule) { function path(prefix, final) { return choice(final, seq(prefix, '.', final)) } + +function parenthesize(rule) { + return seq('(', rule, ')') +} diff --git a/test/corpus/modules.txt b/test/corpus/modules.txt index 94c2ad8..8cf4e38 100644 --- a/test/corpus/modules.txt +++ b/test/corpus/modules.txt @@ -32,6 +32,11 @@ include (Belt: module type of Belt with module Map.Inner := Belt.Map and module include module type of { include T } +include ( + { + let a = Js.log("Hello") + } +) --- @@ -78,7 +83,15 @@ include module type of { (include_statement (module_type_of - (block (include_statement (module_identifier)))))) + (block (include_statement (module_identifier))))) + + (include_statement + (block + (let_declaration + (let_binding (value_identifier) + (call_expression + (value_identifier_path (module_identifier) (value_identifier)) + (arguments (string (string_fragment))))))))) =========================================== Simple definition