diff --git a/grammar.js b/grammar.js index d43b920..605f89b 100644 --- a/grammar.js +++ b/grammar.js @@ -71,6 +71,7 @@ module.exports = grammar({ [$.record_field, $.record_pattern], [$.expression_statement, $.ternary_expression], [$._type_declaration], + [$._let_binding], [$.let_binding, $.ternary_expression], [$.variant_identifier, $.module_identifier], [$.variant], @@ -241,13 +242,19 @@ module.exports = grammar({ choice('=', '+='), optional('private'), $._type, - optional(seq( - 'and', - $._type_declaration - )), + repeat(alias($._type_declaration_and, $.type_declaration)), )), ), + _type_declaration_and: $ => seq( + // New line here not necessary terminates the statement, + // show this doubt to the parser + repeat($._newline), + repeat($.decorator), + 'and', + $._type_declaration + ), + type_parameters: $ => seq( '<', commaSep1t($.type_identifier), @@ -428,13 +435,19 @@ module.exports = grammar({ '=', repeat($.decorator), $.expression, - optional(seq( - 'and', - $._let_binding, - )), + repeat(alias($._let_binding_and, $.let_binding)), )), ), + _let_binding_and: $ => seq( + // New line here not necessary terminates the statement, + // show this doubt to the parser + repeat($._newline), + repeat($.decorator), + 'and', + $._let_binding, + ), + _binding_pattern: $ => choice( $.value_identifier, $.tuple_pattern, diff --git a/test/corpus/decorators.txt b/test/corpus/decorators.txt index 1aabe26..3649de1 100644 --- a/test/corpus/decorators.txt +++ b/test/corpus/decorators.txt @@ -67,3 +67,34 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0) (array) (number)))))) +============================================ +Decorator before type `and` +============================================ + +@deriving +type foo = { + foo: string +} +@deriving +and bar = {bar: int} + + +@deriving @hello and baz = {baz: float} + +--- + +(source_file + (decorated + (decorator (decorator_identifier)) + (type_declaration + (type_identifier) + (record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))) + (type_declaration + (decorator (decorator_identifier)) + (type_identifier) + (record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))) + (type_declaration + (decorator (decorator_identifier)) + (decorator (decorator_identifier)) + (type_identifier) + (record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))))) diff --git a/test/corpus/let_bindings.txt b/test/corpus/let_bindings.txt index 2b02b0a..856b179 100644 --- a/test/corpus/let_bindings.txt +++ b/test/corpus/let_bindings.txt @@ -200,11 +200,12 @@ let a = 5 and b = 4 (let_binding (value_identifier) (number) - (value_identifier) - (number))) + (let_binding + (value_identifier) + (number)))) ============================================ -And (Fuctions) +And (Functions) ============================================ let rec a = x => x + b(x) and b = y => y - 1 @@ -222,10 +223,40 @@ let rec a = x => x + b(x) and b = y => y - 1 (value_identifier) (arguments (value_identifier))))) - (value_identifier) - (function + (let_binding (value_identifier) - (binary_expression + (function + (value_identifier) + (binary_expression + (value_identifier) + (number)))))) + +============================================ +And (decorated) +============================================ + +@one +let rec a = b +@two @three +and b = c + +@four and c = 42 + +--- + +(source_file + (decorated + (decorator (decorator_identifier)) + (let_binding + (value_identifier) + (value_identifier) + (let_binding + (decorator (decorator_identifier)) + (decorator (decorator_identifier)) + (value_identifier) + (value_identifier)) + (let_binding + (decorator (decorator_identifier)) (value_identifier) (number))))) diff --git a/test/corpus/type_declarations.txt b/test/corpus/type_declarations.txt index 458dc5d..10a31c3 100644 --- a/test/corpus/type_declarations.txt +++ b/test/corpus/type_declarations.txt @@ -385,15 +385,16 @@ and teacher = { (property_identifier) (type_annotation (type_identifier)))) - (type_identifier) - (record_type - (record_type_field - (property_identifier) - (type_annotation - (generic_type - (type_identifier) - (type_arguments - (type_identifier)))))))) + (type_declaration + (type_identifier) + (record_type + (record_type_field + (property_identifier) + (type_annotation + (generic_type + (type_identifier) + (type_arguments + (type_identifier))))))))) ===========================================