Skip to content

Commit aa9f8b2

Browse files
authored
Merge pull request #197 from aspeddro/more-structured-ast
More structured ast
2 parents 2716313 + e567726 commit aa9f8b2

File tree

9 files changed

+835
-703
lines changed

9 files changed

+835
-703
lines changed

grammar.js

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = grammar({
5454
$.ternary_expression,
5555
$.mutation_expression,
5656
$.function,
57-
$.let_binding,
57+
$.let_declaration,
5858
],
5959
// Nested.Module.Path precendence
6060
[
@@ -83,9 +83,10 @@ module.exports = grammar({
8383
[$.array, $.array_pattern],
8484
[$.record_field, $.record_pattern],
8585
[$.expression_statement, $.ternary_expression],
86-
[$._type_declaration],
87-
[$._let_binding],
88-
[$.let_binding, $.ternary_expression],
86+
[$.type_declaration],
87+
[$.type_binding],
88+
[$.let_declaration],
89+
[$.let_declaration, $.ternary_expression],
8990
[$.variant_identifier, $.module_identifier],
9091
[$.variant, $.variant_pattern],
9192
[$.variant_declaration, $.function_type_parameter],
@@ -102,13 +103,15 @@ module.exports = grammar({
102103
[$.primary_expression, $.parameter, $._pattern],
103104
[$.parameter, $._pattern],
104105
[$.parameter, $.parenthesized_pattern],
106+
[$.parameter, $.tuple_item_pattern],
105107
[$.variant_declaration],
106108
[$.unit, $._function_type_parameter_list],
107109
[$.functor_parameter, $.module_primary_expression, $.module_identifier_path],
108110
[$._reserved_identifier, $.function],
109111
[$.polyvar_type],
110-
[$._let_binding, $.or_pattern],
111-
[$.exception_pattern, $.or_pattern]
112+
[$.let_binding, $.or_pattern],
113+
[$.exception_pattern, $.or_pattern],
114+
[$.type_binding, $._inline_type]
112115
],
113116

114117
rules: {
@@ -176,13 +179,13 @@ module.exports = grammar({
176179

177180
declaration: $ => choice(
178181
$.type_declaration,
179-
$.let_binding,
182+
$.let_declaration,
180183
$.module_declaration,
181184
$.external_declaration,
182185
$.exception_declaration,
183186
),
184187

185-
_module_binding: $ => prec.left(seq(
188+
module_binding: $ => prec.left(seq(
186189
field('name', choice($.module_identifier, $.type_identifier)),
187190
optional(seq(
188191
':',
@@ -191,20 +194,14 @@ module.exports = grammar({
191194
optional(seq(
192195
'=',
193196
field('definition', $._module_definition),
194-
optional($._module_binding_and)
195197
)),
196198
)),
197199

198-
_module_binding_and: $ => seq(
199-
'and',
200-
$._module_binding
201-
),
202-
203200
module_declaration: $ => seq(
204201
'module',
205202
optional('rec'),
206203
optional('type'),
207-
$._module_binding,
204+
sep1('and', $.module_binding)
208205
),
209206

210207
_module_definition: $ => choice(
@@ -273,31 +270,24 @@ module.exports = grammar({
273270
optional('export'),
274271
'type',
275272
optional('rec'),
276-
$._type_declaration
273+
sep1(
274+
seq(repeat($._newline), repeat($.decorator), 'and'),
275+
$.type_binding
276+
)
277277
),
278278

279-
_type_declaration: $ => seq(
280-
choice($.type_identifier, $.type_identifier_path),
279+
type_binding: $ => seq(
280+
field('name', choice($.type_identifier, $.type_identifier_path)),
281281
optional($.type_parameters),
282282
optional(seq(
283-
optional(seq('=', $._type)),
283+
optional(seq('=', $._non_function_inline_type)),
284284
optional(seq(
285285
choice('=', '+='),
286286
optional('private'),
287-
$._type,
287+
field('body', $._type),
288288
)),
289289
repeat($.type_constraint),
290290
)),
291-
optional(alias($._type_declaration_and, $.type_declaration))
292-
),
293-
294-
_type_declaration_and: $ => seq(
295-
// New line here not necessary terminates the statement,
296-
// show this doubt to the parser
297-
repeat($._newline),
298-
repeat($.decorator),
299-
'and',
300-
$._type_declaration
301291
),
302292

303293
type_parameters: $ => seq(
@@ -480,30 +470,33 @@ module.exports = grammar({
480470
),
481471
),
482472

483-
let_binding: $ => seq(
473+
let_declaration: $ => seq(
484474
choice('export', 'let'),
485475
optional('rec'),
486-
$._let_binding,
487-
),
488-
489-
_let_binding: $ => seq(
490-
$._pattern,
491-
optional($.type_annotation),
492-
optional(seq(
493-
'=',
494-
repeat($.decorator),
495-
$.expression,
496-
optional(alias($._let_binding_and, $.let_binding)),
497-
)),
476+
sep1(
477+
seq(repeat($._newline), repeat($.decorator), 'and'),
478+
$.let_binding
479+
)
498480
),
499481

500-
_let_binding_and: $ => seq(
501-
// New line here not necessary terminates the statement,
502-
// show this doubt to the parser
503-
repeat($._newline),
504-
repeat($.decorator),
505-
'and',
506-
$._let_binding,
482+
let_binding: $ => seq(
483+
field('pattern', $._pattern),
484+
choice(
485+
seq(
486+
$.type_annotation,
487+
optional(
488+
seq('=',
489+
repeat($.decorator),
490+
field('body', $.expression)
491+
)
492+
)
493+
),
494+
seq(
495+
'=',
496+
repeat($.decorator),
497+
field('body', $.expression),
498+
)
499+
)
507500
),
508501

509502
expression_statement: $ => $.expression,
@@ -820,7 +813,7 @@ module.exports = grammar({
820813
parameter: $ => seq(
821814
optional($.uncurry),
822815
choice(
823-
$._pattern,
816+
seq($._pattern, optional($.type_annotation)),
824817
$.labeled_parameter,
825818
$.unit,
826819
$.abstract_type
@@ -865,7 +858,6 @@ module.exports = grammar({
865858
$.range_pattern,
866859
$.exception_pattern
867860
),
868-
optional($.type_annotation),
869861
optional($.as_aliasing),
870862
)),
871863

@@ -938,14 +930,15 @@ module.exports = grammar({
938930
'}'
939931
),
940932

933+
tuple_item_pattern: $ => seq(
934+
repeat($.decorator),
935+
$._pattern,
936+
optional($.type_annotation),
937+
),
938+
941939
tuple_pattern: $ => seq(
942940
'(',
943-
commaSep2t(
944-
alias(
945-
seq(repeat($.decorator), $._pattern),
946-
$.tuple_item_pattern
947-
)
948-
),
941+
commaSep2t($.tuple_item_pattern),
949942
')',
950943
),
951944

queries/textobjects.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
; Classes (modules)
55
;------------------
66

7-
(module_declaration definition: ((_) @class.inner)) @class.outer
7+
(module_binding definition: ((_) @class.inner)) @class.outer
88

99
; Blocks
1010
;-------

test/corpus/decorators.txt

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,46 @@ call()
3939
---
4040

4141
(source_file
42-
(let_binding
43-
(value_identifier)
44-
(call_expression
45-
(parenthesized_expression
46-
(decorator (decorator_identifier))
47-
(value_identifier_path
48-
(module_identifier)
49-
(value_identifier)))
50-
(arguments
51-
(number)
52-
(character))))
53-
54-
(let_binding
55-
(value_identifier)
56-
(decorator (decorator_identifier))
57-
(call_expression
58-
(value_identifier_path
59-
(module_identifier)
60-
(value_identifier))
61-
(arguments
62-
(number)
63-
(character))))
42+
(let_declaration
43+
(let_binding
44+
(value_identifier)
45+
(call_expression
46+
(parenthesized_expression
47+
(decorator (decorator_identifier))
48+
(value_identifier_path
49+
(module_identifier)
50+
(value_identifier)))
51+
(arguments
52+
(number)
53+
(character)))))
6454

65-
(let_binding
66-
(value_identifier)
67-
(function
68-
(formal_parameters)
55+
(let_declaration
56+
(let_binding
57+
(value_identifier)
6958
(decorator (decorator_identifier))
7059
(call_expression
7160
(value_identifier_path
72-
(module_identifier_path
73-
(module_identifier)
74-
(module_identifier))
61+
(module_identifier)
7562
(value_identifier))
7663
(arguments
77-
(array)
78-
(number)))))
64+
(number)
65+
(character)))))
66+
67+
(let_declaration
68+
(let_binding
69+
(value_identifier)
70+
(function
71+
(formal_parameters)
72+
(decorator (decorator_identifier))
73+
(call_expression
74+
(value_identifier_path
75+
(module_identifier_path
76+
(module_identifier)
77+
(module_identifier))
78+
(value_identifier))
79+
(arguments
80+
(array)
81+
(number))))))
7982

8083
(decorated
8184
(decorator (decorator_identifier))
@@ -103,17 +106,18 @@ and bar = {bar: int}
103106
(decorated
104107
(decorator (decorator_identifier))
105108
(type_declaration
106-
(type_identifier)
107-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
108-
(type_declaration
109-
(decorator (decorator_identifier))
109+
(type_binding
110110
(type_identifier)
111-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
112-
(type_declaration
113-
(decorator (decorator_identifier))
114-
(decorator (decorator_identifier))
111+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
112+
(decorator (decorator_identifier))
113+
(type_binding
114+
(type_identifier)
115+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
116+
(decorator (decorator_identifier))
117+
(decorator (decorator_identifier))
118+
(type_binding
115119
(type_identifier)
116-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))))))
120+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))))))
117121

118122
============================================
119123
Decorator with type
@@ -130,15 +134,16 @@ let make = (~x, ~y) => React.string(x ++ y)
130134
(decorator_identifier)
131135
(decorator_arguments
132136
(type_annotation (type_identifier))))
133-
(let_binding
134-
(value_identifier)
135-
(function
136-
(formal_parameters
137-
(parameter (labeled_parameter (value_identifier)))
138-
(parameter (labeled_parameter (value_identifier))))
139-
(call_expression
140-
(value_identifier_path
141-
(module_identifier) (value_identifier))
142-
(arguments
143-
(binary_expression
144-
(value_identifier) (value_identifier))))))))
137+
(let_declaration
138+
(let_binding
139+
(value_identifier)
140+
(function
141+
(formal_parameters
142+
(parameter (labeled_parameter (value_identifier)))
143+
(parameter (labeled_parameter (value_identifier))))
144+
(call_expression
145+
(value_identifier_path
146+
(module_identifier) (value_identifier))
147+
(arguments
148+
(binary_expression
149+
(value_identifier) (value_identifier)))))))))

0 commit comments

Comments
 (0)