Skip to content

Commit d3d54cb

Browse files
authored
Merge pull request #136 from nkrkv/fix-decorators-before-and
Fix decorators before `and` in `let` and `type` statements
2 parents 652c0af + 9eebffb commit d3d54cb

File tree

4 files changed

+99
-23
lines changed

4 files changed

+99
-23
lines changed

grammar.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module.exports = grammar({
7171
[$.record_field, $.record_pattern],
7272
[$.expression_statement, $.ternary_expression],
7373
[$._type_declaration],
74+
[$._let_binding],
7475
[$.let_binding, $.ternary_expression],
7576
[$.variant_identifier, $.module_identifier],
7677
[$.variant],
@@ -241,13 +242,19 @@ module.exports = grammar({
241242
choice('=', '+='),
242243
optional('private'),
243244
$._type,
244-
optional(seq(
245-
'and',
246-
$._type_declaration
247-
)),
245+
repeat(alias($._type_declaration_and, $.type_declaration)),
248246
)),
249247
),
250248

249+
_type_declaration_and: $ => seq(
250+
// New line here not necessary terminates the statement,
251+
// show this doubt to the parser
252+
repeat($._newline),
253+
repeat($.decorator),
254+
'and',
255+
$._type_declaration
256+
),
257+
251258
type_parameters: $ => seq(
252259
'<',
253260
commaSep1t($.type_identifier),
@@ -428,13 +435,19 @@ module.exports = grammar({
428435
'=',
429436
repeat($.decorator),
430437
$.expression,
431-
optional(seq(
432-
'and',
433-
$._let_binding,
434-
)),
438+
repeat(alias($._let_binding_and, $.let_binding)),
435439
)),
436440
),
437441

442+
_let_binding_and: $ => seq(
443+
// New line here not necessary terminates the statement,
444+
// show this doubt to the parser
445+
repeat($._newline),
446+
repeat($.decorator),
447+
'and',
448+
$._let_binding,
449+
),
450+
438451
_binding_pattern: $ => choice(
439452
$.value_identifier,
440453
$.tuple_pattern,

test/corpus/decorators.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,34 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
6767
(array)
6868
(number))))))
6969

70+
============================================
71+
Decorator before type `and`
72+
============================================
73+
74+
@deriving
75+
type foo = {
76+
foo: string
77+
}
78+
@deriving
79+
and bar = {bar: int}
80+
81+
82+
@deriving @hello and baz = {baz: float}
83+
84+
---
85+
86+
(source_file
87+
(decorated
88+
(decorator (decorator_identifier))
89+
(type_declaration
90+
(type_identifier)
91+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
92+
(type_declaration
93+
(decorator (decorator_identifier))
94+
(type_identifier)
95+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
96+
(type_declaration
97+
(decorator (decorator_identifier))
98+
(decorator (decorator_identifier))
99+
(type_identifier)
100+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))))))

test/corpus/let_bindings.txt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ let a = 5 and b = 4
200200
(let_binding
201201
(value_identifier)
202202
(number)
203-
(value_identifier)
204-
(number)))
203+
(let_binding
204+
(value_identifier)
205+
(number))))
205206

206207
============================================
207-
And (Fuctions)
208+
And (Functions)
208209
============================================
209210

210211
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
222223
(value_identifier)
223224
(arguments
224225
(value_identifier)))))
225-
(value_identifier)
226-
(function
226+
(let_binding
227227
(value_identifier)
228-
(binary_expression
228+
(function
229+
(value_identifier)
230+
(binary_expression
231+
(value_identifier)
232+
(number))))))
233+
234+
============================================
235+
And (decorated)
236+
============================================
237+
238+
@one
239+
let rec a = b
240+
@two @three
241+
and b = c
242+
243+
@four and c = 42
244+
245+
---
246+
247+
(source_file
248+
(decorated
249+
(decorator (decorator_identifier))
250+
(let_binding
251+
(value_identifier)
252+
(value_identifier)
253+
(let_binding
254+
(decorator (decorator_identifier))
255+
(decorator (decorator_identifier))
256+
(value_identifier)
257+
(value_identifier))
258+
(let_binding
259+
(decorator (decorator_identifier))
229260
(value_identifier)
230261
(number)))))
231262

test/corpus/type_declarations.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,16 @@ and teacher = {
385385
(property_identifier)
386386
(type_annotation
387387
(type_identifier))))
388-
(type_identifier)
389-
(record_type
390-
(record_type_field
391-
(property_identifier)
392-
(type_annotation
393-
(generic_type
394-
(type_identifier)
395-
(type_arguments
396-
(type_identifier))))))))
388+
(type_declaration
389+
(type_identifier)
390+
(record_type
391+
(record_type_field
392+
(property_identifier)
393+
(type_annotation
394+
(generic_type
395+
(type_identifier)
396+
(type_arguments
397+
(type_identifier)))))))))
397398

398399

399400
===========================================

0 commit comments

Comments
 (0)