Source file error_reporter.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
module type FAILED_PARSER =
sig
type t
type expect = string * Indent.expectation option
type semantic
val has_failed_syntax: t -> bool
val failed_expectations: t -> expect list
val failed_semantic: t -> semantic
val position: t -> Position.t
end
module Make (Parser: FAILED_PARSER) =
struct
open Fmlib_pretty
type doc = Pretty.t
type semantic = Parser.semantic
type item = char
type t = {
p: Parser.t;
semantic: semantic -> doc;
extractor: Source_extractor.t;
}
let make
(semantic_range: semantic -> Position.range)
(semantic: semantic -> doc)
(p: Parser.t)
: t
=
let open Parser in
{
p;
semantic;
extractor =
if has_failed_syntax p then
Source_extractor.of_position 5 (position p)
else
Source_extractor.of_range
5
(semantic_range (failed_semantic p));
}
let make_syntax (p: Parser.t): t =
assert (Parser.has_failed_syntax p);
let semantic _ = assert false
in
make semantic semantic p
let needs_more (r: t): bool =
Source_extractor.needs_more r.extractor
let put (c: char) (r: t): t =
{r with
extractor = Source_extractor.put c r.extractor
}
let put_end (r: t): t =
{r with
extractor = Source_extractor.put_end r.extractor
}
let document (r: t): doc =
let open Pretty in
let open Parser in
Source_extractor.document r.extractor
<+> cut <+>
(
if has_failed_syntax r.p then
Syntax_error.document
(Position.column (position r.p))
(failed_expectations r.p)
else
r.semantic (failed_semantic r.p)
)
let run_on_string (str: string) (r: t): doc =
{r with extractor =
Source_extractor.run_on_string str r.extractor}
|> document
let run_on_channel (ic: in_channel) (r: t): doc =
{r with extractor =
Source_extractor.run_on_channel ic r.extractor}
|> document
let run_on_channels
(ic: in_channel)
(width: int)
(oc: out_channel)
(r: t)
: unit
=
assert (0 < width);
let open Fmlib_pretty.Pretty
in
run_on_channel ic r
|> layout width
|> write_to_channel oc
end