From 0e4e91f31a3cdfb732e432cebb66b0e5deee5ba9 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 4 Dec 2022 14:47:14 -0300 Subject: [PATCH 1/5] fix: type constraint --- grammar.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/grammar.js b/grammar.js index 8448eb8..c48c5f8 100644 --- a/grammar.js +++ b/grammar.js @@ -259,20 +259,31 @@ module.exports = grammar({ optional('export'), 'type', optional('rec'), - $._type_declaration, + $._type_declaration ), _type_declaration: $ => seq( - choice($.type_identifier, $.type_identifier_path), - optional($.type_parameters), - optional(seq( - choice('=', '+='), - optional('private'), - $._type, - repeat($.type_constraint), - optional(seq('=', $._type)), - optional(alias($._type_declaration_and, $.type_declaration)), - )), + choice( + seq( + $.type_identifier, + optional($.type_parameters), + optional(seq('=', $._type)), + optional(seq( + '=', + optional('private'), + $._type, + )), + repeat($.type_constraint), + ), + seq( + choice($.type_identifier, $.type_identifier_path), + optional($.type_parameters), + '+=', + optional('private'), + $.variant_type, + ) + ), + optional(alias($._type_declaration_and, $.type_declaration)) ), _type_declaration_and: $ => seq( From b219dc17d2917e93a813fd719f22ef632aed8ed3 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 4 Dec 2022 14:56:06 -0300 Subject: [PATCH 2/5] add tests --- test/corpus/type_declarations.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/corpus/type_declarations.txt b/test/corpus/type_declarations.txt index 5335f1e..6efd84f 100644 --- a/test/corpus/type_declarations.txt +++ b/test/corpus/type_declarations.txt @@ -530,6 +530,7 @@ Type constraint type t<'a> = 'a constraint 'a = int type decorator<'a, 'b> = 'a => 'b constraint 'a = int constraint 'b = _ => _ +type t<'a> constraint 'a = t --- @@ -553,4 +554,11 @@ type decorator<'a, 'b> = 'a => 'b constraint 'a = int constraint 'b = _ => _ (type_identifier) (function_type (function_type_parameters (type_identifier)) - (type_identifier))))) + (type_identifier)))) + + (type_declaration + (type_identifier) + (type_parameters (type_identifier)) + (type_constraint + (type_identifier) + (type_identifier)))) From 102efc262cd6aabceb5f3ccd5cb7a6f01115aae7 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 4 Dec 2022 15:14:37 -0300 Subject: [PATCH 3/5] relax rule --- grammar.js | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/grammar.js b/grammar.js index c48c5f8..d00f51d 100644 --- a/grammar.js +++ b/grammar.js @@ -263,26 +263,18 @@ module.exports = grammar({ ), _type_declaration: $ => seq( - choice( - seq( - $.type_identifier, - optional($.type_parameters), - optional(seq('=', $._type)), - optional(seq( - '=', - optional('private'), - $._type, - )), - repeat($.type_constraint), - ), - seq( - choice($.type_identifier, $.type_identifier_path), - optional($.type_parameters), - '+=', + choice($.type_identifier, $.type_identifier_path), + optional($.type_parameters), + optional(seq( + optional($.type_parameters), + optional(seq('=', $._type)), + optional(seq( + choice('=', '+='), optional('private'), - $.variant_type, - ) - ), + $._type, + )), + repeat($.type_constraint), + )), optional(alias($._type_declaration_and, $.type_declaration)) ), From 1668ab26b24669a146a737ddd935a3c526d619f4 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 4 Dec 2022 15:24:47 -0300 Subject: [PATCH 4/5] remove redudant type_parameters --- grammar.js | 1 - 1 file changed, 1 deletion(-) diff --git a/grammar.js b/grammar.js index d00f51d..1a42add 100644 --- a/grammar.js +++ b/grammar.js @@ -266,7 +266,6 @@ module.exports = grammar({ choice($.type_identifier, $.type_identifier_path), optional($.type_parameters), optional(seq( - optional($.type_parameters), optional(seq('=', $._type)), optional(seq( choice('=', '+='), From 50d417cc2a6c01b24e13ee8d41a9391d183eb77f Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 4 Dec 2022 15:32:34 -0300 Subject: [PATCH 5/5] add tests --- test/corpus/type_declarations.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/corpus/type_declarations.txt b/test/corpus/type_declarations.txt index 6efd84f..8ee8df2 100644 --- a/test/corpus/type_declarations.txt +++ b/test/corpus/type_declarations.txt @@ -5,13 +5,16 @@ Opaque type t type t' type \"type" +type t and b --- (source_file (type_declaration (type_identifier)) (type_declaration (type_identifier)) - (type_declaration (type_identifier))) + (type_declaration (type_identifier)) + (type_declaration (type_identifier) + (type_declaration (type_identifier)))) =========================================== Export