package fmlib_parse

  1. Overview
  2. Docs

Source file source_extractor.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
(*
    The source extractor extracts from a bytestream the part which contains an
    error and marks it in a readable form.

    Examples:

    1. Error at an exact position displayed with two extra lines

        25 |      xxxx
        26 |
        27 |    line with error
                          ^

    2. Error within a range of a certain line (2 extra lines)

        25 |      xxxx
        26 |
        27 |    line with error
                          ^^^^^

    3. Error within a range of spanning more than one line (2 extra lines)

        25 |      xxxx
        26 |
                          v----------
        27 |    xxx yyy   error start
        28 |      err err err err err
        29 |          err err err
        30 |       err error end zzz
              -----------------^
*)


open Fmlib_pretty

module Pretty = Fmlib_pretty.Print



type t = {
    range: Position.range;      (* range which has to be extracted *)

    extra: int;                 (* number of lines above the range which should
                                   be extracted as well *)
    number_width: int;          (* width of the line numbers *)

    nline: int;                 (* the current line number *)

    line:  string;              (* the current line *)

    doc:   Print.doc;           (* the generated doc *)
}


let of_range
        (extra: int)
        (range: Position.range)
    : t
    =
    (* Make a source extractor which reports [extra] number of lines before the
     * error. *)
    assert (0 <= extra);
    assert (Position.is_valid_range range);
    let number_width =
        (Position.line (snd range) + 1)
        |> string_of_int
        |> String.length
    in
    {
        range;
        extra;
        number_width;
        nline = 0;
        line  = "";
        doc   = Pretty.empty;
    }


let of_position
        (extra: int)
        (pos: Position.t)
    : t
    =
    (* Make a source extractor which reports [extra] number of lines before the
     * error. *)
    of_range extra (pos, pos)




let needs_more (ext: t): bool =
    let _, p2 = ext.range in
    ext.nline <= Position.line p2




let is_in_range (p: t): bool =
    (* Is the current line within the range which should be extracted (i.e.
     * extra lines + error range)? *)
    let open Position in
    let pos1, pos2 = p.range in
    line pos1 <= p.nline + p.extra
    &&
    p.nline <= line pos2



let is_start_line (p: t): bool =
    (* Is the current line the start line of the part which should be extracted?
     *)
    let open Position in
    let pos1, pos2 = p.range in
    p.nline = line pos1
    &&
    line pos1 < line pos2


let is_end_line (p: t): bool =
    (* Is the current line the end line of the part which should be extracted?
     *)
    let open Position in
    let pos1, pos2 = p.range in
    line pos1 < p.nline
    &&
    p.nline = line pos2



let is_one_line (p: t): bool =
    (* Is the current line the only line of the error? *)
    let open Position in
    let pos1, pos2 = p.range in
    p.nline = line pos1
    &&
    line pos1 = line pos2



let source_separator: string =
    " | "





let source_indent (p: t): int =
    p.number_width
    +
    String.length source_separator





let source_line (p: t): Pretty.doc =
    (* The current line nicely formatted i.e. displayed as

        25 |  xxx yyy ... zzz

    *)
    let str =
        p.nline + 1 |> string_of_int
    in
    let n = p.number_width - String.length str
    in
    assert (0 <= n);
    Pretty.(
        fill n ' '
        <+>
        text str
        <+>
        text source_separator
        <+>
        text p.line
        <+>
        cut
    )





let start_line_marker (p: t): Pretty.doc =
    (* A line marker of the form

              v--------

       to mark the start of a multiline error
    *)
    let col = Position.column (fst p.range) in
    Pretty.(
        fill (source_indent p + col) ' '
        <+>
        char 'v'
        <+>
        fill 10 '-'
        <+>
        cut
    )





let end_line_marker (p: t): Pretty.doc =
    (* A line marker of the form

              --------^

       to mark the end of a multiline error
    *)
    let col = Position.column (snd p.range)
    and ind = source_indent p
    in
    Pretty.(
        fill ind ' '
        <+>
        fill (col - ind) '-'
        <+>
        char '^'
        <+>
        cut
    )


let one_line_marker (is_last: bool) (p: t): Pretty.doc =
    (* A line marker of the form

           ^^^^^

       to mark a one line error. If the marker marks a newline or the end of
       input, it is displayed as

          ^ end of line

       or

          ^ end of input

       If the marker marks a nonprintable ascii character it is displayed e.g. as

          ^ nonprintable ascii '\xFA'

       Furthermore end of input and end of line are annotated.
    *)

    let open Position in
    let pos1, pos2 = p.range in
    let c1 = Position.column pos1
    and c2 = Position.column pos2
    and bc = Position.byte_column pos2
    in
    assert (line pos1 = line pos2);
    assert (c1 <= c2);
    let len = max 1 (c2 - c1) in
    let open Pretty
    in
    let non_printable () =
        let len = String.length p.line in
        assert (bc < len);
        let open Printf in
        let hex i =
            assert (bc + i < len);
            Char.code p.line.[bc + i]
        in
        " nonprintable ascii(s): "
        ^
        if bc + 1 = len then
            sprintf "%X end of line" (hex 0)
        else if bc + 2 = len then
            sprintf "%X %X (end of line)" (hex 0) (hex 1)
        else if bc + 3 = len then
            sprintf "%X %X %X (end of line)"
                (hex 0) (hex 1) (hex 2)
        else if bc + 4 = len then
            sprintf "%X %X %X %X (end of line)"
                (hex 0)   (hex 1) (hex 2) (hex 3)
        else
            sprintf "%X %X %X %X ..."
                (hex 0)   (hex 1) (hex 2) (hex 3)
    in
    let annotation =
        if len = 1 && bc < String.length p.line
        then
            let ch = p.line.[bc] in
            if ch < ' ' || Char.chr 126 <= ch then
                text (non_printable ())
            else
                empty
        else if len = 1 && bc = String.length p.line then
            if is_last then
                text " end of input"
            else
                text " end of line"
        else
            Pretty.empty
    in
    Pretty.(
        fill (source_indent p + c1) ' '
        <+>
        fill len '^'
        <+>
        annotation
        <+>
        cut
    )


let receive_char (is_last: bool) (c: char) (p: t): t =
    let in_range = is_in_range p
    in
    if c = '\n' then
        let open Pretty in
        let doc =
            if in_range then
                p.doc
                <+>
                if is_start_line p then
                    start_line_marker p <+> source_line p
                else if is_one_line p then
                    source_line p <+> one_line_marker is_last p
                else if is_end_line p then
                    source_line p <+> end_line_marker p
                else
                    source_line p
            else
                p.doc
        in
        {
            p with
            line  = "";
            nline = p.nline + 1;
            doc
        }
    else (* c <> '\n' *)
        {
            p with
            line =
                if in_range then
                    p.line ^ String.make 1 c
                else
                    p.line;
        }




let put: char -> t -> t =
    receive_char false


let put_end: t -> t =
    receive_char true '\n'


let document (p: t): Pretty.doc =
    p.doc


let run_on_string  = Run_on.string  needs_more put put_end

let run_on_channel = Run_on.channel needs_more put put_end