-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathxml.cat.m
189 lines (154 loc) · 5.23 KB
/
xml.cat.m
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
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2000, 2011 The University of Melbourne.
% Copyright (C) 2014, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%---------------------------------------------------------------------------%
%
% Main author: [email protected].
%
%---------------------------------------------------------------------------%
:- module xml.cat.
:- interface.
:- import_module io.
:- import_module list.
:- import_module map.
:- type catalog
---> catalog(map(public_id, system_id)).
:- type dirs == list(path).
:- type public_id == string.
:- type system_id == string.
:- type path == string.
:- type catalog_result(T)
---> catalog_ok(T)
; catalog_error(string).
:- pred load_catalog(string::in, dirs::in, catalog_result(catalog)::out,
io::di, io::uo) is det.
:- pred find_catalog(string::in, dirs::in, catalog_result(string)::out,
io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module int.
:- import_module prolog.
:- import_module string.
%---------------------------------------------------------------------------%
:- type entry
---> dtd(public_id, system_id)
; none.
load_catalog(Name, Dirs, Res, !IO) :-
find_catalog(Name, Dirs, Res0, !IO),
(
Res0 = catalog_ok(Path),
read_file(Res1, !IO),
(
Res1 = ok(CatChars),
gather_catalog_lines(1, CatLines0, CatChars, _),
decomment(CatLines0, CatLines),
parse(CatLines, Entries, Errors),
Cat0 = catalog(map.init),
list.foldl(add_entry, Entries, Cat0, Cat),
Res = catalog_ok(Cat),
list.foldl(
( pred(Msg::in, !.IO::di, !:IO::uo) is det :-
io.stderr_stream(StdErr, !IO),
io.format(StdErr, "%s: %s\n", [s(Path), s(Msg)], !IO)
), Errors, !IO)
;
Res1 = error(_, Err),
io.error_message(Err, Msg),
Res = catalog_error(Msg)
)
;
Res0 = catalog_error(Msg),
Res = catalog_error(Msg)
).
find_catalog(Name, [], catalog_error(Err), !IO) :-
string.format("`%s' not found", [s(Name)], Err).
find_catalog(Name, [Dir | Dirs], Res, !IO) :-
append_list([Dir, "/", Name], Path),
prolog.see(Path, Res0, !IO),
( if Res0 = ok then
Res = catalog_ok(Path)
else
find_catalog(Name, Dirs, Res, !IO)
).
:- type catalog_line
---> catalog_line(
line_number :: int,
line_chars :: list(char)
).
:- pred gather_catalog_lines(int::in, list(catalog_line)::out,
list(char)::in, list(char)::out) is det.
gather_catalog_lines(_N, [], [], []).
gather_catalog_lines(N, [Line | Lines]) -->
=([_ | _]),
gather_catalog_line(N, Line),
gather_catalog_lines(N + 1, Lines).
:- pred gather_catalog_line(int::in, catalog_line::out,
list(char)::in, list(char)::out) is det.
gather_catalog_line(LineNumber, catalog_line(LineNumber, Cs)) -->
collect_until('\n', Cs).
:- pred decomment(list(catalog_line)::in, list(catalog_line)::out) is det.
decomment(Lines0, Lines) :-
list.map(
( pred(Line0::in, Line::out) is det :-
Line0 = catalog_line(N, Cs0),
Line = catalog_line(N, Cs),
collect_until('#', Cs, Cs0, _)
), Lines0, Lines).
:- pred parse(list(catalog_line)::in,
list(entry)::out, list(string)::out) is det.
parse([], [], []).
parse([Line | Lines], Entries, Errors) :-
Line = catalog_line(N, Cs),
( if parse_entry(Entry, Cs, _) then
parse(Lines, Entries0, Errors),
Entries = [Entry | Entries0]
else
string.format("%d: syntax error", [i(N)], Msg),
parse(Lines, Entries, Errors0),
Errors = [Msg | Errors0]
).
:- pred parse_entry(entry::out, list(char)::in, list(char)::out) is semidet.
parse_entry(Entry) -->
ws,
( if
['P','U','B','L','I','C'], ws, string(PublicId), ws, string(SystemId)
then
{ Entry = dtd(PublicId, SystemId) }
else if =([]) then
{ Entry = none }
else
{ fail }
).
:- pred add_entry(entry::in, catalog::in, catalog::out) is det.
add_entry(none, Cat, Cat).
add_entry(dtd(PublicId, SystemId), catalog(Cat0), catalog(Cat)) :-
map.det_insert(PublicId, SystemId, Cat0, Cat).
:- pred ws(list(char)::in, list(char)::out) is det.
ws -->
( if [C], { char.is_whitespace(C) } then
ws
else
[]
).
:- pred string(string::out, list(char)::in, list(char)::out) is semidet.
string(Str) -->
['"'],
collect_until('"', Cs),
{ string.from_char_list(Cs, Str) }.
:- pred collect_until(char::in, list(char)::out,
list(char)::in, list(char)::out) is det.
collect_until(_C, [], [], []).
collect_until(C, Cs) -->
=([_ | _]),
[C0],
( if { C = C0 } then
{ Cs = [] }
else
collect_until(C, Cs0),
{ Cs = [C0 | Cs0] }
).