Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions test/corpus/decorators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))))
43 changes: 37 additions & 6 deletions test/corpus/let_bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))))

Expand Down
19 changes: 10 additions & 9 deletions test/corpus/type_declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))))))


===========================================
Expand Down