Skip to content

Commit a1352f0

Browse files
committed
Respond to review comments
1 parent 65da675 commit a1352f0

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

ocaml/parsing/lexer.mll

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,31 @@ type deferred_token =
134134
; end_pos : Lexing.position
135135
}
136136

137+
(* This queue will only ever have 0 or 1 elements in it. We use it
138+
instead of an [option ref] for its convenient interface.
139+
*)
137140
let deferred_tokens : deferred_token Queue.t = Queue.create ()
138141

139-
(* Effectively splits the current lexer production into two halves. The current
140-
call to the lexer will return the first half of the text matched by the
141-
production, and the next call to the lexer will return the second half (of
142-
length [len]) of the text matched by the production.
142+
(* Effectively splits the text in the lexer's current "window" (defined below)
143+
into two halves. The current call to the lexer will return the first half of
144+
the text in the window, and the next call to the lexer will return the second
145+
half (of length [len]) of the text in the window.
146+
147+
"window" refers to the text matched by a production of the lexer. It spans
148+
from [lexer.lex_start_p] to [lexer.lex_curr_p].
143149
144-
It accomplishes this by doing two things:
145-
- It sets the current bounds of the lexbuf to only account for the
150+
The function accomplishes this splitting by doing two things:
151+
- It sets the current window of the lexbuf to only account for the
146152
first half of the text. (The first half is of length: |text|-len.)
147-
- It sets the [next_token] ref such that, the next time the lexer is
148-
called, it will return the specified [token] *and* set the bounds of
149-
the lexbuf to account for the second half of the text. (The second half
153+
- It enqueues a token into [deferred_tokens] such that, the next time the
154+
lexer is called, it will return the specified [token] *and* set the window
155+
of the lexbuf to account for the second half of the text. (The second half
150156
is of length: |text|.)
151157
152-
This business with setting the bounds of the lexbuf is only so that error
158+
This business with setting the window of the lexbuf is only so that error
153159
messages point at the right place in the program text.
154160
*)
155-
let enqueue_token_from_end_of_lexbuf (lexbuf : Lexing.lexbuf) token ~len =
161+
let enqueue_token_from_end_of_lexbuf_window (lexbuf : Lexing.lexbuf) token ~len =
156162
let suffix_end = lexbuf.lex_curr_p in
157163
let suffix_start =
158164
{ suffix_end with pos_cnum = suffix_end.pos_cnum - len }
@@ -192,7 +198,7 @@ let enqueue_token_from_end_of_lexbuf (lexbuf : Lexing.lexbuf) token ~len =
192198
We accomplish this hack by setting some global mutable state upon seeing
193199
an identifier immediately followed by a hash. When that state is set, we
194200
will produce [HASH_SUFFIX] the next time the lexer is called. This is
195-
done in [enqueue_hash_suffix_from_end_of_lexbuf].
201+
done in [enqueue_hash_suffix_from_end_of_lexbuf_window].
196202
197203
Note [Lexing hack for hash operators]
198204
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -209,14 +215,14 @@ let enqueue_token_from_end_of_lexbuf (lexbuf : Lexing.lexbuf) token ~len =
209215
the style of Note [Lexing hack for float#], where the lexer consumes [x#~#]
210216
all at once, but produces LIDENT(x) from the current call to the lexer and
211217
HASHOP(#~#) from the next call to the lexer. This is done in
212-
[enqueue_hashop_from_end_of_lexbuf].
218+
[enqueue_hashop_from_end_of_lexbuf_window].
213219
*)
214220

215-
let enqueue_hash_suffix_from_end_of_lexbuf lexbuf =
216-
enqueue_token_from_end_of_lexbuf lexbuf HASH_SUFFIX ~len:1
221+
let enqueue_hash_suffix_from_end_of_lexbuf_window lexbuf =
222+
enqueue_token_from_end_of_lexbuf_window lexbuf HASH_SUFFIX ~len:1
217223

218-
let enqueue_hashop_from_end_of_lexbuf lexbuf ~hashop =
219-
enqueue_token_from_end_of_lexbuf lexbuf (HASHOP hashop)
224+
let enqueue_hashop_from_end_of_lexbuf_window lexbuf ~hashop =
225+
enqueue_token_from_end_of_lexbuf_window lexbuf (HASHOP hashop)
220226
~len:(String.length hashop)
221227

222228
(* Escaped chars are interpreted in strings unless they are in comments. *)
@@ -515,11 +521,11 @@ rule token = parse
515521
*)
516522
| (lowercase identchar * as name) ('#' symbolchar_or_hash+ as hashop)
517523
(* See Note [Lexing hack for hash operators] *)
518-
{ enqueue_hashop_from_end_of_lexbuf lexbuf ~hashop;
524+
{ enqueue_hashop_from_end_of_lexbuf_window lexbuf ~hashop;
519525
lookup_keyword name }
520526
| (lowercase identchar * as name) '#'
521527
(* See Note [Lexing hack for float#] *)
522-
{ enqueue_hash_suffix_from_end_of_lexbuf lexbuf;
528+
{ enqueue_hash_suffix_from_end_of_lexbuf_window lexbuf;
523529
lookup_keyword name }
524530
| lowercase identchar * as name
525531
{ lookup_keyword name }
@@ -530,12 +536,12 @@ rule token = parse
530536
('#' symbolchar_or_hash+ as hashop)
531537
(* See Note [Lexing hack for hash operators] *)
532538
{ warn_latin1 lexbuf;
533-
enqueue_hashop_from_end_of_lexbuf lexbuf ~hashop;
539+
enqueue_hashop_from_end_of_lexbuf_window lexbuf ~hashop;
534540
LIDENT name }
535541
| (lowercase_latin1 identchar_latin1 * as name) '#'
536542
(* See Note [Lexing hack for float#] *)
537543
{ warn_latin1 lexbuf;
538-
enqueue_hash_suffix_from_end_of_lexbuf lexbuf;
544+
enqueue_hash_suffix_from_end_of_lexbuf_window lexbuf;
539545
LIDENT name }
540546
| lowercase_latin1 identchar_latin1 * as name
541547
{ warn_latin1 lexbuf; LIDENT name }

ocaml/testsuite/tests/typing-layouts/parsing_unboxed_floats.ml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
* expect
44
*)
55

6-
(* Write tests that evidence how the difference between the
7-
types #c and float# is navigated.
6+
(* These tests show how potential ambiguities are resolved
7+
between the types #c and float#.
8+
*)
9+
10+
(* CR layouts: These tests will change a lot when we add real float
11+
to the type system, e.g. [C of float#] will be prohibited.
812
*)
913

1014
(* Basic syntax: float# is an unboxed float. *)
@@ -19,6 +23,21 @@ type t = C of float#
1923
type t = C : float# -> t
2024
|}];;
2125

26+
(* float# works as an argument to normal type constructors, not just classes,
27+
even though many of the rest of the tests in this file are concerned with
28+
classes.
29+
*)
30+
type t = float# list;;
31+
let f (_ : float# list) = ();;
32+
type t = C of float# list;;
33+
type t = C : float# list -> t;;
34+
[%%expect {|
35+
type t = float# list
36+
val f : float# list -> unit = <fun>
37+
type t = C of float# list
38+
type t = C : float# list -> t
39+
|}];;
40+
2241
class ['a] c = object(self)
2342
method x :'a = assert false
2443
end;;

0 commit comments

Comments
 (0)