Skip to content

Commit 4e7d3c7

Browse files
Tweak grammar of primitive list in externals (rescript-lang#415)
Fixes rescript-lang/syntax#412 Currently the grammar allows for a list of primitives in an external declaration: i.e. `"hi" "hx"` in `external f: (int, int) => int = "hi" "hx"`. This stems from the fact that user primitives with arity greater than 5 should be implemented by two C functions. The first function, to be used in conjunction with the bytecode compiler ocamlc, receives two arguments: a pointer to an array of OCaml values (the values for the arguments), and an integer which is the number of arguments provided. The other function, to be used in conjunction with the native-code compiler ocamlopt, takes its arguments directly. However in the case of compiling to JS, we don't need to deal with this. In order to reduce some complexity, we'll now parse just one primitive.
1 parent ef395f6 commit 4e7d3c7

File tree

30 files changed

+134
-163
lines changed

30 files changed

+134
-163
lines changed

syntax/src/res_core.ml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5209,19 +5209,6 @@ and parseTypeDefinitionOrExtension ~attrs p =
52095209
let typeDefs = parseTypeDefinitions ~attrs ~name ~params ~startPos p in
52105210
TypeDef {recFlag; types = typeDefs}
52115211

5212-
and parsePrimitive p =
5213-
match p.Parser.token with
5214-
| String s -> Parser.next p; Some s
5215-
| _ -> None
5216-
5217-
and parsePrimitives p =
5218-
match (parseRegion ~grammar:Grammar.Primitive ~f:parsePrimitive p) with
5219-
| [] ->
5220-
let msg = "An external definition should have at least one primitive. Example: \"setTimeout\"" in
5221-
Parser.err p (Diagnostics.message msg);
5222-
[]
5223-
| primitives -> primitives
5224-
52255212
(* external value-name : typexp = external-declaration *)
52265213
and parseExternalDef ~attrs ~startPos p =
52275214
Parser.leaveBreadcrumb p Grammar.External;
@@ -5230,8 +5217,18 @@ and parseExternalDef ~attrs ~startPos p =
52305217
let name = Location.mkloc name loc in
52315218
Parser.expect ~grammar:(Grammar.TypeExpression) Colon p;
52325219
let typExpr = parseTypExpr p in
5220+
let equalStart = p.startPos in
5221+
let equalEnd = p.endPos in
52335222
Parser.expect Equal p;
5234-
let prim = parsePrimitives p in
5223+
let prim = match p.token with
5224+
| String s -> Parser.next p; [s]
5225+
| _ ->
5226+
Parser.err ~startPos:equalStart ~endPos:equalEnd p
5227+
(Diagnostics.message
5228+
("An external requires the name of the JS value you're referring to, like \""
5229+
^ name.txt ^ "\"."));
5230+
[]
5231+
in
52355232
let loc = mkLoc startPos p.prevEndPos in
52365233
let vb = Ast_helper.Val.mk ~loc ~attrs ~prim name typExpr in
52375234
Parser.eatBreadcrumb p;

syntax/src/res_grammar.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ type t =
5353
| Implementation
5454
| Attribute
5555
| TypeConstraint
56-
| Primitive
5756
| AtomicTypExpr
5857
| ListExpr
5958
| JsFfiImport
@@ -111,7 +110,6 @@ let toString = function
111110
| Implementation -> "implementation"
112111
| Attribute -> "an attribute"
113112
| TypeConstraint -> "constraints on a type"
114-
| Primitive -> "an external primitive"
115113
| AtomicTypExpr -> "a type"
116114
| ListExpr -> "an ocaml list expr"
117115
| PackageConstraint -> "a package constraint"
@@ -335,7 +333,6 @@ let isListElement grammar token =
335333
| TypeConstraint -> token = Constraint
336334
| PackageConstraint -> token = And
337335
| ConstructorDeclaration -> token = Bar
338-
| Primitive -> begin match token with Token.String _ -> true | _ -> false end
339336
| JsxAttribute -> isJsxAttributeStart token
340337
| JsFfiImport -> isJsFfiImportStart token
341338
| AttributePayload -> token = Lparen
@@ -363,8 +360,6 @@ let isListTerminator grammar token =
363360
| TypeConstraint, token when token <> Constraint -> true
364361
| PackageConstraint, token when token <> And -> true
365362
| ConstructorDeclaration, token when token <> Bar -> true
366-
| Primitive, Semicolon -> true
367-
| Primitive, token when isStructureItemStart token -> true
368363
| AttributePayload, Rparen -> true
369364

370365
| _ -> false

syntax/tests/idempotency/lwt/unix/lwt_bytes.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ external unsafe_get : t -> int -> char = "%caml_ba_unsafe_ref_1"
1717
external unsafe_set : t -> int -> char -> unit = "%caml_ba_unsafe_set_1"
1818

1919
[@@@ocaml.warning "-3"]
20-
external unsafe_fill : t -> int -> int -> char -> unit = "lwt_unix_fill_bytes" "noalloc"
20+
external unsafe_fill : t -> int -> int -> char -> unit = "noalloc"
2121
[@@@ocaml.warning "+3"]
2222

2323
let fill bytes ofs len ch =
@@ -31,9 +31,9 @@ let fill bytes ofs len ch =
3131
+-----------------------------------------------------------------+ *)
3232

3333
[@@@ocaml.warning "-3"]
34-
external unsafe_blit_from_bytes : Bytes.t -> int -> t -> int -> int -> unit = "lwt_unix_blit_from_bytes" "noalloc"
35-
external unsafe_blit_to_bytes : t -> int -> Bytes.t -> int -> int -> unit = "lwt_unix_blit_to_bytes" "noalloc"
36-
external unsafe_blit : t -> int -> t -> int -> int -> unit = "lwt_unix_blit" "noalloc"
34+
external unsafe_blit_from_bytes : Bytes.t -> int -> t -> int -> int -> unit = "noalloc"
35+
external unsafe_blit_to_bytes : t -> int -> Bytes.t -> int -> int -> unit = "noalloc"
36+
external unsafe_blit : t -> int -> t -> int -> int -> unit = "noalloc"
3737
[@@@ocaml.warning "+3"]
3838

3939
let blit_from_bytes src_buf src_ofs dst_buf dst_ofs len =
@@ -154,7 +154,7 @@ let recvfrom fd buf pos len flags =
154154
else
155155
wrap_syscall Read fd (fun () -> stub_recvfrom (unix_file_descr fd) buf pos len flags)
156156

157-
external stub_sendto : Unix.file_descr -> t -> int -> int -> Unix.msg_flag list -> Unix.sockaddr -> int = "lwt_unix_bytes_sendto_byte" "lwt_unix_bytes_sendto"
157+
external stub_sendto : Unix.file_descr -> t -> int -> int -> Unix.msg_flag list -> Unix.sockaddr -> int = "lwt_unix_bytes_sendto"
158158

159159
let sendto fd buf pos len flags addr =
160160
if pos < 0 || len < 0 || pos > length buf - len then
@@ -171,7 +171,7 @@ let map_file ~fd ?pos ~shared ?(size=(-1)) () =
171171
|> Bigarray.array1_of_genarray
172172

173173
[@@@ocaml.warning "-3"]
174-
external mapped : t -> bool = "lwt_unix_mapped" "noalloc"
174+
external mapped : t -> bool = "noalloc"
175175
[@@@ocaml.warning "+3"]
176176

177177
type advice =

syntax/tests/idempotency/lwt/unix/lwt_bytes.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ val fill : t -> int -> int -> char -> unit
9090
(** [fill buffer offset length value] puts [value] in all [length]
9191
bytes of [buffer] starting at offset [offset]. *)
9292

93-
external unsafe_fill : t -> int -> int -> char -> unit = "lwt_unix_fill_bytes" "noalloc"
93+
external unsafe_fill : t -> int -> int -> char -> unit = "noalloc"
9494
[@@ocaml.warning "-3"]
9595
(** Same as {!fill} but without bounds checking. *)
9696

@@ -141,7 +141,7 @@ val map_file : fd : Unix.file_descr -> ?pos : int64 -> shared : bool -> ?size :
141141
(** [map_file ~fd ?pos ~shared ?size ()] maps the file descriptor
142142
[fd] to an array of bytes. *)
143143

144-
external mapped : t -> bool = "lwt_unix_mapped" "noalloc"
144+
external mapped : t -> bool = "noalloc"
145145
[@@ocaml.warning "-3"]
146146
(** [mapped buffer] returns [true] iff [buffer] is a memory mapped
147147
file. *)

syntax/tests/idempotency/lwt/unix/lwt_unix.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ external start_job : 'a job -> async_method -> bool = "lwt_unix_start_job"
153153
if the job is already terminated. *)
154154

155155
[@@@ocaml.warning "-3"]
156-
external check_job : 'a job -> int -> bool = "lwt_unix_check_job" "noalloc"
156+
external check_job : 'a job -> int -> bool = "noalloc"
157157
(* Check whether that a job has terminated or not. If it has not
158158
yet terminated, it is marked so it will send a notification
159159
when it finishes. *)
@@ -294,7 +294,7 @@ type file_descr = {
294294
}
295295

296296
[@@@ocaml.warning "-3"]
297-
external is_socket : Unix.file_descr -> bool = "lwt_unix_is_socket" "noalloc"
297+
external is_socket : Unix.file_descr -> bool = "noalloc"
298298
[@@@ocaml.warning "+3"]
299299

300300
external guess_blocking_job : Unix.file_descr -> bool job = "lwt_unix_guess_blocking_job"
@@ -1602,7 +1602,7 @@ let recvfrom ch buf pos len flags =
16021602
let do_recvfrom = if Sys.win32 then Unix.recvfrom else stub_recvfrom in
16031603
wrap_syscall Read ch (fun () -> do_recvfrom ch.fd buf pos len flags)
16041604

1605-
external stub_sendto : Unix.file_descr -> Bytes.t -> int -> int -> Unix.msg_flag list -> Unix.sockaddr -> int = "lwt_unix_sendto_byte" "lwt_unix_sendto"
1605+
external stub_sendto : Unix.file_descr -> Bytes.t -> int -> int -> Unix.msg_flag list -> Unix.sockaddr -> int = "lwt_unix_sendto"
16061606

16071607
let sendto ch buf pos len flags addr =
16081608
if pos < 0 || len < 0 || pos > Bytes.length buf - len then
@@ -2448,10 +2448,10 @@ let handle_unix_error f x =
24482448
+-----------------------------------------------------------------+ *)
24492449

24502450
[@@@ocaml.warning "-3"]
2451-
external pool_size : unit -> int = "lwt_unix_pool_size" "noalloc"
2452-
external set_pool_size : int -> unit = "lwt_unix_set_pool_size" "noalloc"
2453-
external thread_count : unit -> int = "lwt_unix_thread_count" "noalloc"
2454-
external thread_waiting_count : unit -> int = "lwt_unix_thread_waiting_count" "noalloc"
2451+
external pool_size : unit -> int = "lwt_unix_pool_size"
2452+
external set_pool_size : int -> unit = "lwt_unix_set_pool_size"
2453+
external thread_count : unit -> int = "lwt_unix_thread_count"
2454+
external thread_waiting_count : unit -> int = "lwt_unix_thread_waiting_count"
24552455
[@@@ocaml.warning "+3"]
24562456

24572457
(* +-----------------------------------------------------------------+

syntax/tests/idempotency/ocaml/stdlib/gc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ external stat : unit -> stat = "caml_gc_stat"
4747
external quick_stat : unit -> stat = "caml_gc_quick_stat"
4848
external counters : unit -> (float * float * float) = "caml_gc_counters"
4949
external minor_words : unit -> (float [@unboxed])
50-
= "caml_gc_minor_words" "caml_gc_minor_words_unboxed"
50+
= "caml_gc_minor_words_unboxed"
5151
external get : unit -> control = "caml_gc_get"
5252
external set : control -> unit = "caml_gc_set"
5353
external minor : unit -> unit = "caml_gc_minor"

syntax/tests/idempotency/ocaml/stdlib/gc.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ external counters : unit -> float * float * float = "caml_gc_counters"
171171
is as fast as [quick_stat]. *)
172172

173173
external minor_words : unit -> (float [@unboxed])
174-
= "caml_gc_minor_words" "caml_gc_minor_words_unboxed"
174+
= "caml_gc_minor_words_unboxed"
175175
(** Number of words allocated in the minor heap since the program was
176176
started. This number is accurate in byte-code programs, but only an
177177
approximation in programs compiled to native code.

syntax/tests/idempotency/ocaml/stdlib/int32.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ external shift_right_logical : int32 -> int -> int32 = "%int32_lsr"
3030
external of_int : int -> int32 = "%int32_of_int"
3131
external to_int : int32 -> int = "%int32_to_int"
3232
external of_float : float -> int32
33-
= "caml_int32_of_float" "caml_int32_of_float_unboxed"
33+
= "caml_int32_of_float_unboxed"
3434
[@@unboxed] [@@noalloc]
3535
external to_float : int32 -> float
36-
= "caml_int32_to_float" "caml_int32_to_float_unboxed"
36+
= "caml_int32_to_float_unboxed"
3737
[@@unboxed] [@@noalloc]
3838
external bits_of_float : float -> int32
39-
= "caml_int32_bits_of_float" "caml_int32_bits_of_float_unboxed"
39+
= "caml_int32_bits_of_float_unboxed"
4040
[@@unboxed] [@@noalloc]
4141
external float_of_bits : int32 -> float
42-
= "caml_int32_float_of_bits" "caml_int32_float_of_bits_unboxed"
42+
= "caml_int32_float_of_bits_unboxed"
4343
[@@unboxed] [@@noalloc]
4444

4545
let zero = 0l

syntax/tests/idempotency/ocaml/stdlib/int32.mli

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,21 @@ external to_int : int32 -> int = "%int32_to_int"
114114
is exact. *)
115115

116116
external of_float : float -> int32
117-
= "caml_int32_of_float" "caml_int32_of_float_unboxed"
117+
= "caml_int32_of_float_unboxed"
118118
[@@unboxed] [@@noalloc]
119119
(** Convert the given floating-point number to a 32-bit integer,
120120
discarding the fractional part (truncate towards 0).
121121
The result of the conversion is undefined if, after truncation,
122122
the number is outside the range \[{!Int32.min_int}, {!Int32.max_int}\]. *)
123123

124124
external to_float : int32 -> float
125-
= "caml_int32_to_float" "caml_int32_to_float_unboxed"
125+
= "caml_int32_to_float_unboxed"
126126
[@@unboxed] [@@noalloc]
127127
(** Convert the given 32-bit integer to a floating-point number. *)
128128

129129
external of_string : string -> int32 = "caml_int32_of_string"
130130
(** Convert the given string to a 32-bit integer.
131-
The string is read in decimal (by default, or if the string
131+
The string is read in decimal (by default, or if the string
132132
begins with [0u]) or in hexadecimal, octal or binary if the
133133
string begins with [0x], [0o] or [0b] respectively.
134134
@@ -152,7 +152,7 @@ val to_string : int32 -> string
152152
(** Return the string representation of its argument, in signed decimal. *)
153153

154154
external bits_of_float : float -> int32
155-
= "caml_int32_bits_of_float" "caml_int32_bits_of_float_unboxed"
155+
= "caml_int32_bits_of_float_unboxed"
156156
[@@unboxed] [@@noalloc]
157157
(** Return the internal representation of the given float according
158158
to the IEEE 754 floating-point 'single format' bit layout.
@@ -161,7 +161,7 @@ external bits_of_float : float -> int32
161161
represent the mantissa. *)
162162

163163
external float_of_bits : int32 -> float
164-
= "caml_int32_float_of_bits" "caml_int32_float_of_bits_unboxed"
164+
= "caml_int32_float_of_bits_unboxed"
165165
[@@unboxed] [@@noalloc]
166166
(** Return the floating-point number whose internal representation,
167167
according to the IEEE 754 floating-point 'single format' bit layout,

syntax/tests/idempotency/ocaml/stdlib/int64.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ external shift_right_logical : int64 -> int -> int64 = "%int64_lsr"
3030
external of_int : int -> int64 = "%int64_of_int"
3131
external to_int : int64 -> int = "%int64_to_int"
3232
external of_float : float -> int64
33-
= "caml_int64_of_float" "caml_int64_of_float_unboxed"
33+
= "caml_int64_of_float_unboxed"
3434
[@@unboxed] [@@noalloc]
3535
external to_float : int64 -> float
36-
= "caml_int64_to_float" "caml_int64_to_float_unboxed"
36+
= "caml_int64_to_float_unboxed"
3737
[@@unboxed] [@@noalloc]
3838
external of_int32 : int32 -> int64 = "%int64_of_int32"
3939
external to_int32 : int64 -> int32 = "%int64_to_int32"
@@ -63,10 +63,10 @@ let of_string_opt s =
6363

6464

6565
external bits_of_float : float -> int64
66-
= "caml_int64_bits_of_float" "caml_int64_bits_of_float_unboxed"
66+
= "caml_int64_bits_of_float_unboxed"
6767
[@@unboxed] [@@noalloc]
6868
external float_of_bits : int64 -> float
69-
= "caml_int64_float_of_bits" "caml_int64_float_of_bits_unboxed"
69+
= "caml_int64_float_of_bits_unboxed"
7070
[@@unboxed] [@@noalloc]
7171

7272
type t = int64

syntax/tests/idempotency/ocaml/stdlib/int64.mli

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ external to_int : int64 -> int = "%int64_to_int"
115115
during the conversion. *)
116116

117117
external of_float : float -> int64
118-
= "caml_int64_of_float" "caml_int64_of_float_unboxed"
118+
= "caml_int64_of_float_unboxed"
119119
[@@unboxed] [@@noalloc]
120120
(** Convert the given floating-point number to a 64-bit integer,
121121
discarding the fractional part (truncate towards 0).
122122
The result of the conversion is undefined if, after truncation,
123123
the number is outside the range \[{!Int64.min_int}, {!Int64.max_int}\]. *)
124124

125125
external to_float : int64 -> float
126-
= "caml_int64_to_float" "caml_int64_to_float_unboxed"
126+
= "caml_int64_to_float_unboxed"
127127
[@@unboxed] [@@noalloc]
128128
(** Convert the given 64-bit integer to a floating-point number. *)
129129

@@ -150,7 +150,7 @@ external to_nativeint : int64 -> nativeint = "%int64_to_nativeint"
150150

151151
external of_string : string -> int64 = "caml_int64_of_string"
152152
(** Convert the given string to a 64-bit integer.
153-
The string is read in decimal (by default, or if the string
153+
The string is read in decimal (by default, or if the string
154154
begins with [0u]) or in hexadecimal, octal or binary if the
155155
string begins with [0x], [0o] or [0b] respectively.
156156
@@ -173,7 +173,7 @@ val to_string : int64 -> string
173173
(** Return the string representation of its argument, in decimal. *)
174174

175175
external bits_of_float : float -> int64
176-
= "caml_int64_bits_of_float" "caml_int64_bits_of_float_unboxed"
176+
= "caml_int64_bits_of_float_unboxed"
177177
[@@unboxed] [@@noalloc]
178178
(** Return the internal representation of the given float according
179179
to the IEEE 754 floating-point 'double format' bit layout.
@@ -182,7 +182,7 @@ external bits_of_float : float -> int64
182182
represent the mantissa. *)
183183

184184
external float_of_bits : int64 -> float
185-
= "caml_int64_float_of_bits" "caml_int64_float_of_bits_unboxed"
185+
= "caml_int64_float_of_bits_unboxed"
186186
[@@unboxed] [@@noalloc]
187187
(** Return the floating-point number whose internal representation,
188188
according to the IEEE 754 floating-point 'double format' bit layout,

syntax/tests/idempotency/ocaml/stdlib/nativeint.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ external shift_right_logical: nativeint -> int -> nativeint = "%nativeint_lsr"
3030
external of_int: int -> nativeint = "%nativeint_of_int"
3131
external to_int: nativeint -> int = "%nativeint_to_int"
3232
external of_float : float -> nativeint
33-
= "caml_nativeint_of_float" "caml_nativeint_of_float_unboxed"
33+
= "caml_nativeint_of_float_unboxed"
3434
[@@unboxed] [@@noalloc]
3535
external to_float : nativeint -> float
36-
= "caml_nativeint_to_float" "caml_nativeint_to_float_unboxed"
36+
= "caml_nativeint_to_float_unboxed"
3737
[@@unboxed] [@@noalloc]
3838
external of_int32: int32 -> nativeint = "%nativeint_of_int32"
3939
external to_int32: nativeint -> int32 = "%nativeint_to_int32"

syntax/tests/idempotency/ocaml/stdlib/nativeint.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ external to_int : nativeint -> int = "%nativeint_to_int"
132132
the conversion. *)
133133

134134
external of_float : float -> nativeint
135-
= "caml_nativeint_of_float" "caml_nativeint_of_float_unboxed"
135+
= "caml_nativeint_of_float_unboxed"
136136
[@@unboxed] [@@noalloc]
137137
(** Convert the given floating-point number to a native integer,
138138
discarding the fractional part (truncate towards 0).
@@ -141,7 +141,7 @@ external of_float : float -> nativeint
141141
\[{!Nativeint.min_int}, {!Nativeint.max_int}\]. *)
142142

143143
external to_float : nativeint -> float
144-
= "caml_nativeint_to_float" "caml_nativeint_to_float_unboxed"
144+
= "caml_nativeint_to_float_unboxed"
145145
[@@unboxed] [@@noalloc]
146146
(** Convert the given native integer to a floating-point number. *)
147147

@@ -158,7 +158,7 @@ external to_int32 : nativeint -> int32 = "%nativeint_to_int32"
158158

159159
external of_string : string -> nativeint = "caml_nativeint_of_string"
160160
(** Convert the given string to a native integer.
161-
The string is read in decimal (by default, or if the string
161+
The string is read in decimal (by default, or if the string
162162
begins with [0u]) or in hexadecimal, octal or binary if the
163163
string begins with [0x], [0o] or [0b] respectively.
164164

0 commit comments

Comments
 (0)