Skip to content
Draft
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
179 changes: 151 additions & 28 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export default grammar({
[$._record_element, $._record_single_field],
[$._record_pun_field, $._record_single_pun_field],
[$._record_field_name, $.record_pattern],
[$.primary_expression, $._record_field_name],
[$.primary_expression, $._record_field_name, $.record_pattern],
[$.primary_expression, $.object_field],
[$._statement, $._switch_body],
[$._inline_type, $.function_type_parameters],
[$.primary_expression, $.parameter, $._pattern],
Expand All @@ -150,6 +153,11 @@ export default grammar({
[$._record_type_member, $._object_type_member],
[$._non_function_inline_type, $.generic_type],
[$._type_identifier, $.polymorphic_type],
[$.functor, $.module_expression],
[$._jsx_child, $.tagged_template],
[$.call_expression, $._jsx_child],
[$.subscript_expression, $._jsx_child],
[$.expression, $.coercion_expression],
],

rules: {
Expand Down Expand Up @@ -253,7 +261,13 @@ export default grammar({

functor: ($) =>
seq(
field('parameters', $.functor_parameters),
field(
'parameters',
choice(
$.functor_parameters,
alias($.module_primary_expression, $.functor_parameter),
),
),
optional(field('return_module_type', $.module_type_annotation)),
'=>',
field('body', $._module_definition),
Expand All @@ -263,10 +277,14 @@ export default grammar({
seq('(', optional(commaSep1t($.functor_parameter)), ')'),

functor_parameter: ($) =>
seq($.module_identifier, optional($.module_type_annotation)),
choice(
seq($.module_identifier, optional($.module_type_annotation)),
seq('_', $.module_type_annotation),
$.unit,
),

module_type_annotation: ($) =>
seq(':', choice($.module_expression, $.block)),
seq(':', choice($.module_expression, $.block, $.functor)),

external_declaration: ($) =>
seq('external', $.value_identifier, $.type_annotation, '=', $.string),
Expand All @@ -276,6 +294,7 @@ export default grammar({
'exception',
$.variant_identifier,
optional($.variant_parameters),
optional($.type_annotation),
optional(
seq('=', choice($.variant_identifier, $.nested_variant_identifier)),
),
Expand All @@ -295,7 +314,12 @@ export default grammar({
optional($.type_parameters),
optional(
choice(
seq('=', $.extensible_type),
seq(
'=',
optional('private'),
$.extensible_type,
repeat($.type_constraint),
),
seq(
optional(seq('=', $._non_function_inline_type)),
optional(
Expand Down Expand Up @@ -339,6 +363,7 @@ export default grammar({
$.module_pack,
$.unit,
$.polymorphic_type,
$.extension_expression,
alias($._as_aliasing_non_function_inline_type, $.as_aliasing_type),
),

Expand Down Expand Up @@ -366,7 +391,15 @@ export default grammar({
seq(
$.variant_identifier,
optional($.variant_parameters),
optional($.type_annotation),
optional(
choice(
$.type_annotation,
seq(
'=',
choice($.variant_identifier, $.nested_variant_identifier),
),
),
),
),
),

Expand All @@ -387,7 +420,11 @@ export default grammar({
polyvar_declaration: ($) =>
prec.right(
choice(
seq($.polyvar_identifier, optional($.polyvar_parameters)),
seq(
$.polyvar_identifier,
optional($.polyvar_parameters),
repeat(seq('&', $._type)),
),
$._inline_type,
),
),
Expand Down Expand Up @@ -517,6 +554,7 @@ export default grammar({
$.lazy_expression,
$._jsx_element,
$.regex,
$.tagged_template,
),

parenthesized_expression: ($) =>
Expand Down Expand Up @@ -675,17 +713,29 @@ export default grammar({
prec(
'call',
seq(
field('function', $.primary_expression),
field('function', choice($.primary_expression, $.block)),
field('arguments', alias($.call_arguments, $.arguments)),
),
),

pipe_expression: ($) =>
prec.left(
seq(
choice($.primary_expression, $.block),
choice(
$.primary_expression,
$.block,
$.for_expression,
$.while_expression,
$.mutation_expression,
),
choice('->', '|>'),
choice($.primary_expression, $.block),
choice(
$.primary_expression,
$.block,
$.for_expression,
$.while_expression,
$.mutation_expression,
),
),
),

Expand Down Expand Up @@ -719,6 +769,7 @@ export default grammar({
optional(
choice(
'?',
$.type_annotation,
seq(
'=',
optional('?'),
Expand Down Expand Up @@ -791,11 +842,13 @@ export default grammar({
seq(
choice(
seq(optional('?'), $.value_identifier),
seq('?', $.parenthesized_pattern),
$._literal_pattern,
$._destructuring_pattern,
$.polyvar_type_pattern,
$.unit,
$.module_pack,
$.extension_expression,
$.lazy_pattern,
$.parenthesized_pattern,
$.or_pattern,
Expand Down Expand Up @@ -895,7 +948,11 @@ export default grammar({
dict_pattern_entry: ($) => seq($.string, ':', $._pattern),

_collection_element_pattern: ($) =>
seq(choice($._pattern, $.spread_pattern), optional($.as_aliasing)),
seq(
choice($._pattern, $.spread_pattern),
optional($.type_annotation),
optional($.as_aliasing),
),

spread_pattern: ($) =>
seq('...', choice($.value_identifier, $.list_pattern, $.array_pattern)),
Expand Down Expand Up @@ -1003,13 +1060,24 @@ export default grammar({
),

decorator_arguments: ($) =>
seq('(', choice(commaSept($.expression), $.type_annotation), ')'),
seq(
'(',
choice(
commaSept($.decorator_argument),
$.declaration,
$.type_annotation,
),
')',
),

decorator_argument: ($) =>
seq(optional('?'), $.expression, optional($.guard)),

subscript_expression: ($) =>
prec.right(
'member',
seq(
field('object', $.primary_expression),
field('object', choice($.primary_expression, $.block)),
'[',
field('index', $.expression),
']',
Expand All @@ -1020,7 +1088,7 @@ export default grammar({
prec(
'member',
seq(
field('record', $.primary_expression),
field('record', choice($.primary_expression, $.block)),
'.',
optional(
seq(
Expand Down Expand Up @@ -1051,7 +1119,7 @@ export default grammar({
for_expression: ($) =>
seq(
'for',
$.value_identifier,
field('pattern', $._pattern),
'in',
$.expression,
choice('to', 'downto'),
Expand Down Expand Up @@ -1106,23 +1174,35 @@ export default grammar({
),

coercion_expression: ($) =>
prec.left(
'coercion_relation',
choice(
choice(
prec.left(
'coercion_relation',
seq(
field('left', $.expression),
field('operator', ':>'),
field('right', $._type),
),
seq(
'(',
field('left', $.expression),
field('source_type', $.type_annotation),
field('operator', ':>'),
field('right', $._type),
')',
),
prec.dynamic(
1,
prec.left(
'coercion_relation',
seq(
'(',
field('left', $.expression),
field('source_type', $.type_annotation),
field('operator', ':>'),
field('right', $._type),
')',
),
),
),
seq(
field('left', $.primary_expression),
field('source_type', $.type_annotation),
field('operator', ':>'),
field('right', $._type),
),
),

unary_expression: ($) =>
Expand Down Expand Up @@ -1152,7 +1232,33 @@ export default grammar({
),

_extension_expression_payload: ($) =>
seq('(', choice($._one_or_more_statements, $.type_annotation), ')'),
seq(
'(',
choice(
$._one_or_more_statements,
$.type_annotation,
seq(
':',
repeat(seq($.let_declaration, $._semicolon)),
$.let_declaration,
optional($._semicolon),
),
),
')',
),

tagged_template: ($) =>
prec(
'call',
seq(
choice(
$.value_identifier,
$.value_identifier_path,
$.member_expression,
),
$.template_string,
),
),

variant: ($) =>
prec.right(
Expand Down Expand Up @@ -1242,15 +1348,27 @@ export default grammar({
),

constrain_type: ($) =>
seq('type', $._type, choice('=', ':='), optional('private'), $._type),
seq(
'type',
$._type,
choice('=', ':='),
optional('private'),
$._type,
repeat($.type_constraint),
),

functor_use: ($) =>
seq($.module_primary_expression, alias($.functor_arguments, $.arguments)),

functor_arguments: ($) =>
seq('(', optional(commaSep1t($._functor_argument)), ')'),

_functor_argument: ($) => choice($.module_expression, $.block),
_functor_argument: ($) =>
choice(
seq($.module_expression, optional($.module_type_annotation)),
$.block,
$.unit,
),

variant_identifier: ($) => /[A-Z][a-zA-Z0-9_']*/,

Expand All @@ -1264,7 +1382,12 @@ export default grammar({
),

type_identifier: ($) =>
choice($._identifier, /'[a-zA-Z0-9_']*/, $._escape_identifier),
choice(
$._identifier,
/'[a-zA-Z0-9_']*/,
seq('\u0027', $._escape_identifier),
$._escape_identifier,
),

_identifier: (_) => /[a-z_][a-zA-Z0-9_']*/,

Expand Down
Loading
Loading