Skip to content

Commit b1ed183

Browse files
authored
Replace newline token with automatic semicolons (#295)
1 parent ae672d6 commit b1ed183

5 files changed

Lines changed: 186618 additions & 200786 deletions

File tree

‎grammar.js‎

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export default grammar({
44
name: "rescript",
55

66
externals: ($) => [
7-
$._newline,
7+
$._automatic_semicolon,
8+
$._continuation,
89
$.block_comment,
910
'"',
1011
"`",
@@ -18,6 +19,7 @@ export default grammar({
1819
],
1920

2021
extras: ($) => [
22+
$._continuation,
2123
$.block_comment,
2224
$.line_comment,
2325
$.decorator,
@@ -34,7 +36,7 @@ export default grammar({
3436
$.module_primary_expression,
3537
],
3638

37-
inline: ($) => [$._module_definition],
39+
inline: ($) => [$._module_definition, $._semicolon],
3840

3941
precedences: ($) => [
4042
// + - Operators -> precendence
@@ -97,7 +99,6 @@ export default grammar({
9799
[$._record_element, $._record_single_field],
98100
[$._record_pun_field, $._record_single_pun_field],
99101
[$._record_field_name, $.record_pattern],
100-
[$._statement, $._one_or_more_statements],
101102
[$._statement, $._switch_body],
102103
[$._inline_type, $.function_type_parameters],
103104
[$.primary_expression, $.parameter, $._pattern],
@@ -118,27 +119,20 @@ export default grammar({
118119
[$._record_type_member, $._object_type_member],
119120
[$._non_function_inline_type, $.generic_type],
120121
[$._type_identifier, $.polymorphic_type],
121-
[$.type_declaration],
122-
[$.variant_type],
123122
],
124123

125124
rules: {
126-
source_file: ($) =>
127-
seq(repeat($._statement_delimeter), repeat($._statement)),
125+
source_file: ($) => repeat($._statement),
128126

129-
_statement: ($) => seq($.statement, repeat1($._statement_delimeter)),
127+
_statement: ($) => seq($.statement, $._semicolon),
130128

131-
_statement_delimeter: ($) => choice(";", $._newline),
129+
_semicolon: ($) => choice(";", $._automatic_semicolon),
132130

133131
line_comment: ($) => token(seq("//", /[^\n]*/)),
134132

135133
_one_or_more_statements: ($) =>
136-
seq(repeat($._statement), $.statement, optional($._statement_delimeter)),
134+
seq(repeat($._statement), $.statement, optional($._semicolon)),
137135

138-
// Like _one_or_more_statements but without a trailing delimiter.
139-
// Used as switch_match / try-catch bodies so that trailing NEWLINEs
140-
// (including those emitted between consecutive block comments) are
141-
// absorbed by the enclosing switch/try rule instead of being orphaned.
142136
_switch_body: ($) => seq(repeat($._statement), $.statement),
143137

144138
statement: ($) =>
@@ -150,18 +144,7 @@ export default grammar({
150144
),
151145

152146
block: ($) =>
153-
prec.right(
154-
seq(
155-
"{",
156-
optional(
157-
seq(
158-
$._one_or_more_statements,
159-
repeat($._statement_delimeter),
160-
),
161-
),
162-
"}",
163-
),
164-
),
147+
prec.right(seq("{", optional($._one_or_more_statements), "}")),
165148

166149
open_statement: ($) => seq("open", optional("!"), $.module_expression),
167150

@@ -272,7 +255,7 @@ export default grammar({
272255
optional("export"),
273256
"type",
274257
optional("rec"),
275-
sep1(seq(optional($._newline), "and"), $.type_binding),
258+
sep1("and", $.type_binding),
276259
),
277260

278261
type_binding: ($) =>
@@ -340,11 +323,10 @@ export default grammar({
340323
tuple_type: ($) => prec.dynamic(-1, seq("(", commaSep1t($._type), ")")),
341324

342325
variant_type: ($) =>
343-
seq(
344-
optional("|"),
345-
sep1(
346-
seq(repeat($._newline), "|"),
347-
choice($.variant_declaration, $.variant_type_spread),
326+
prec.left(
327+
seq(
328+
optional("|"),
329+
barSep1(choice($.variant_declaration, $.variant_type_spread)),
348330
),
349331
),
350332

@@ -613,7 +595,7 @@ export default grammar({
613595
"switch",
614596
$.expression,
615597
"{",
616-
repeat(seq($.switch_match, repeat($._statement_delimeter))),
598+
repeat(seq($.switch_match, optional($._semicolon))),
617599
"}",
618600
),
619601

@@ -644,7 +626,7 @@ export default grammar({
644626
$.expression,
645627
"catch",
646628
"{",
647-
repeat(seq($.switch_match, repeat($._statement_delimeter))),
629+
repeat(seq($.switch_match, optional($._semicolon))),
648630
"}",
649631
),
650632

@@ -1141,9 +1123,6 @@ export default grammar({
11411123
seq(
11421124
"(",
11431125
choice($._one_or_more_statements, $.type_annotation),
1144-
// explicit newline here because it won’t be reported otherwise by the scanner
1145-
// because we’re in parens
1146-
optional($._newline),
11471126
")",
11481127
),
11491128

0 commit comments

Comments
 (0)