Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit f7227ac

Browse files
Remove "to" and "downto" as keywords. (#327)
"to" is a commonly use identifier in interop scenarios. to/downto take a lot of expensive grammar real estate at a very high price. Since this is a handrolled parser, we can drop them as a keyword and parse them straight as identifier.
1 parent 4872b98 commit f7227ac

File tree

6 files changed

+15
-19
lines changed

6 files changed

+15
-19
lines changed

src/res_core.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,8 +3148,8 @@ and parseForRest hasOpeningParen pattern startPos p =
31483148
Parser.expect In p;
31493149
let e1 = parseExpr p in
31503150
let direction = match p.Parser.token with
3151-
| To -> Asttypes.Upto
3152-
| Downto -> Asttypes.Downto
3151+
| Lident "to" -> Asttypes.Upto
3152+
| Lident "downto" -> Asttypes.Downto
31533153
| token ->
31543154
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
31553155
Asttypes.Upto

src/res_token.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type t =
4444
| Lazy
4545
| Tilde
4646
| Question
47-
| If | Else | For | In | To | Downto | While | Switch
47+
| If | Else | For | In | While | Switch
4848
| When
4949
| EqualGreater | MinusGreater
5050
| External
@@ -131,8 +131,6 @@ let toString = function
131131
| Else -> "else"
132132
| For -> "for"
133133
| In -> "in"
134-
| To -> "to"
135-
| Downto -> "downto"
136134
| While -> "while"
137135
| Switch -> "switch"
138136
| When -> "when"
@@ -168,7 +166,6 @@ let keywordTable = function
168166
| "as" -> As
169167
| "assert" -> Assert
170168
| "constraint" -> Constraint
171-
| "downto" -> Downto
172169
| "else" -> Else
173170
| "exception" -> Exception
174171
| "export" -> Export
@@ -189,7 +186,6 @@ let keywordTable = function
189186
| "private" -> Private
190187
| "rec" -> Rec
191188
| "switch" -> Switch
192-
| "to" -> To
193189
| "true" -> True
194190
| "try" -> Try
195191
| "type" -> Typ
@@ -200,10 +196,10 @@ let keywordTable = function
200196
[@@raises Not_found]
201197

202198
let isKeyword = function
203-
| And | As | Assert | Constraint | Downto | Else | Exception | Export
199+
| And | As | Assert | Constraint | Else | Exception | Export
204200
| External | False | For | If | Import | In | Include | Land | Lazy
205201
| Let | List | Lor | Module | Mutable | Of | Open | Private | Rec
206-
| Switch | To | True | Try | Typ | When | While | With -> true
202+
| Switch | True | Try | Typ | When | While | With -> true
207203
| _ -> false
208204

209205
let lookupKeyword str =

tests/parsing/errors/structure/__snapshots__/parse.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,19 @@ let [%rescript.patternhole ] = 3
247247
Syntax error!
248248
parsing/errors/structure/letBindingPatternKeyword.js:1:5-8
249249
1 │ let open = 1
250-
2 │ let to = 3
250+
2 │ let for = 3
251251
3 │
252252
253253
\`open\` is a reserved keyword. Keywords need to be escaped: \\\\\\"open\\"
254254
255255
256256
Syntax error!
257-
parsing/errors/structure/letBindingPatternKeyword.js:2:5-6
257+
parsing/errors/structure/letBindingPatternKeyword.js:2:5-7
258258
1 │ let open = 1
259-
2 │ let to = 3
259+
2 │ let for = 3
260260
3 │
261261
262-
\`to\` is a reserved keyword. Keywords need to be escaped: \\\\\\"to\\"
262+
\`for\` is a reserved keyword. Keywords need to be escaped: \\\\\\"for\\"
263263
264264
265265
========================================================"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
let open = 1
2-
let to = 3
2+
let for = 3

tests/parsing/errors/typeDef/__snapshots__/parse.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ exports[`typeParams.js 1`] = `
211211
"=====Parsetree==========================================
212212
type nonrec 'a node = {
213213
_value: 'a Js.Nullable.value }
214-
type nonrec ('from, 'to) derivedNode =
214+
type nonrec ('from, 'for) derivedNode =
215215
{
216216
mutable value: 'to_ ;
217217
updateF: 'from -> 'to_ }
@@ -240,14 +240,14 @@ type nonrec ('from, 'foo) derivedNode =
240240
241241
242242
Syntax error!
243-
parsing/errors/typeDef/typeParams.js:5:26-27
243+
parsing/errors/typeDef/typeParams.js:5:26-28
244244
3 │ }
245245
4
246-
5type derivedNode<'from, 'to> = {
246+
5type derivedNode<'from, 'for> = {
247247
6 │ mutable value: 'to_,
248248
7updateF: 'from => 'to_,
249249
250-
\`to\` is a reserved keyword. Keywords need to be escaped: \\\\\\"to\\"
250+
\`for\` is a reserved keyword. Keywords need to be escaped: \\\\\\"for\\"
251251
252252
253253
Syntax error!

tests/parsing/errors/typeDef/typeParams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ type node('a) = {
22
_value: Js.Nullable.value<'a>
33
}
44

5-
type derivedNode<'from, 'to> = {
5+
type derivedNode<'from, 'for> = {
66
mutable value: 'to_,
77
updateF: 'from => 'to_,
88
}

0 commit comments

Comments
 (0)