Skip to content

Commit 5fd8013

Browse files
committed
cparser/ErrorReports.ml: remove the use of the deprecated library [MenhirLib.General] and of the deprecated function stack in Menhir's incremental API.
The new code requires Menhir 20170418 or newer.
1 parent e671e45 commit 5fd8013

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

cparser/ErrorReports.ml

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
open Lexing
1818
open Pre_parser.MenhirInterpreter
19-
module S = MenhirLib.General (* Streams *)
2019

2120
(* -------------------------------------------------------------------------- *)
2221

@@ -125,73 +124,53 @@ let shorten k text =
125124

126125
(* -------------------------------------------------------------------------- *)
127126

128-
(* [stack checkpoint] extracts the parser's stack out of a checkpoint. *)
127+
(* [env checkpoint] extracts a parser environment out of a checkpoint,
128+
which must be of the form [HandlingError env]. *)
129129

130-
let stack checkpoint =
130+
let env checkpoint =
131131
match checkpoint with
132132
| HandlingError env ->
133-
stack env
133+
env
134134
| _ ->
135-
assert false (* this cannot happen, I promise *)
135+
assert false
136136

137137
(* -------------------------------------------------------------------------- *)
138138

139139
(* [state checkpoint] extracts the number of the current state out of a
140-
parser checkpoint. *)
140+
checkpoint, which must be of the form [HandlingError env]. *)
141141

142142
let state checkpoint : int =
143-
match Lazy.force (stack checkpoint) with
144-
| S.Nil ->
145-
(* Hmm... The parser is in its initial state. Its number is
146-
usually 0. This is a BIG HACK. TEMPORARY *)
147-
0
148-
| S.Cons (Element (s, _, _, _), _) ->
149-
number s
150-
151-
(* -------------------------------------------------------------------------- *)
152-
153-
(* TEMPORARY move to MenhirLib.General *)
154-
155-
let rec drop n (xs : 'a S.stream) : 'a S.stream =
156-
match n, xs with
157-
| 0, _
158-
| _, lazy (S.Nil) ->
159-
xs
160-
| _, lazy (S.Cons (_, xs)) ->
161-
drop (n - 1) xs
143+
current_state_number (env checkpoint)
162144

163145
(* -------------------------------------------------------------------------- *)
164146

165-
(* [element checkpoint i] returns the [i]-th cell of the parser stack. The index
166-
[i] is 0-based. [i] should (ideally) be within bounds; we raise [Not_found]
167-
if it isn't. *)
168-
169-
let element checkpoint i : element =
170-
match Lazy.force (drop i (stack checkpoint)) with
171-
| S.Nil ->
172-
(* [i] is out of range. This could happen if the handwritten error
173-
messages are out of sync with the grammar, or if a mistake was
174-
made. We fail in a non-fatal way. *)
175-
raise Not_found
176-
| S.Cons (e, _) ->
177-
e
178-
179-
(* -------------------------------------------------------------------------- *)
147+
(* [range text checkpoint i] converts the stack index [i] to the fragment of
148+
the source text that corresponds to this stack entry. This text fragment is
149+
placed within single quotes and shortened if it is too long. We also ensure
150+
that it does not contain any special characters.
180151
181-
(* [range text e] converts the stack element [e] to the fragment of the source
182-
text that corresponds to this stack element. The fragment is placed within
183-
single quotes and shortened if it is too long. We also ensure that it does
184-
not contain any special characters. *)
152+
[text] is the source text. [checkpoint] represents the point where the
153+
parser detected a syntax error; it must be of the form [HandlingError env].
154+
[i] is a 0-based index into the stack. *)
185155

186156
let width = 30
187157

188-
let range text (e : element) : string =
189-
(* Extract the start and positions of this stack element. *)
190-
let Element (_, _, pos1, pos2) = e in
191-
(* Get the underlying source text fragment. *)
192-
let fragment = extract text (pos1, pos2) in
193-
(* Sanitize it and limit its length. Enclose it in single quotes. *)
194-
"'" ^ shorten width (sanitize (compress fragment)) ^ "'"
158+
let range text checkpoint (i : int) : string =
159+
(* Access the stack at index [i]. *)
160+
match get i (env checkpoint) with
161+
| None ->
162+
(* The index is out of range. This should not happen if [$i]
163+
keywords are correctly inside the syntax error message
164+
database. The integer [i] should always be a valid offset
165+
into the known suffix of the stack. *)
166+
"???"
167+
| Some e ->
168+
(* Extract the start and positions of this stack element. *)
169+
let Element (_, _, pos1, pos2) = e in
170+
(* Get the underlying source text fragment. *)
171+
let fragment = extract text (pos1, pos2) in
172+
(* Sanitize it and limit its length. Enclose it in single quotes. *)
173+
"'" ^ shorten width (sanitize (compress fragment)) ^ "'"
195174

196175
(* -------------------------------------------------------------------------- *)
197176

@@ -202,7 +181,7 @@ let range text (e : element) : string =
202181
let fragment text checkpoint message =
203182
try
204183
let i = int_of_string (Str.matched_group 1 message) in
205-
range text (element checkpoint i)
184+
range text checkpoint i
206185
with
207186
| Failure _ ->
208187
(* In principle, this should not happen, but if it does, let's cover up

0 commit comments

Comments
 (0)